stale.test.ts
This file
Path
packages/agent-kit/test/stale.test.ts
270 lines · 7.3 KiB · TypeScript
Last changed by
Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
History of this fileTake it
The walk stopped after 200 commits: lines older than 678c8b8 point at it.
Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8
lines 1–270why, one step back1import { describe, expect, it } from "@effect/vitest";2import { Brand } from "@repo/brand";3import { Effect, Option } from "effect";45import {6 describedIn,7 frontmatter,8 isStale,9 readManaged,10} from "../src/generated-file.ts";11import { projectSkill } from "../src/project-skill.ts";12import {13 landed,14 later,15 policy,16 policyWithNewLint,17 project,18} from "./fixtures.ts";1920const helloDigest =21 "sha256:5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03";2223const recordedHash = (file: string) =>24 /^ {2}content-hash: "(?<hash>sha256:[0-9a-f]{64})"$/mu.exec(file)?.groups25 ?.hash ?? "";2627const contributeSkill = (input: {28 readonly policy: typeof policy;29 readonly landed: typeof landed;30}) =>31 projectSkill({ ...input, project }).pipe(32 Effect.map((files) => files[0] ?? { content: "", path: "" })33 );3435describe(frontmatter, () => {36 it.effect(37 "says who manages the file, the landing it came from and its body's SHA-256",38 () =>39 Effect.gen(function* () {40 const file = yield* frontmatter({41 body: "hello\n",42 description: "Say hello: politely.",43 landed: Option.some(landed),44 name: "hello",45 });4647 expect(file).toBe(48 [49 "---",50 "name: hello",51 'description: "Say hello: politely."',52 "metadata:",53 ` managed-by: "${Brand.slug}"`,54 ` landed: "${landed}"`,55 ` content-hash: "${helloDigest}"`,56 "---",57 "hello",58 "",59 ].join("\n")60 );61 })62 );6364 it.effect("leaves the landing out when the file came from none", () =>65 Effect.gen(function* () {66 const file = yield* frontmatter({67 body: "hello\n",68 description: "Say hello.",69 landed: Option.none(),70 name: "hello",71 });7273 expect(file).not.toContain("landed:");74 expect(recordedHash(file)).toBe(helloDigest);75 })76 );77});7879describe(readManaged, () => {80 it.effect("reads back the landing, the hash and the body", () =>81 Effect.gen(function* () {82 const file = yield* frontmatter({83 body: "hello\n",84 description: "Say hello.",85 landed: Option.some(landed),86 name: "hello",87 });8889 expect(readManaged(file)).toStrictEqual(90 Option.some({91 body: "hello\n",92 hash: helloDigest,93 landed: Option.some(landed),94 })95 );96 })97 );9899 it("is None for a file without the frontmatter", () => {100 expect(readManaged("# Contribute\n\nBy hand.\n")).toStrictEqual(101 Option.none()102 );103 });104105 it("is None for a file someone else manages", () => {106 const file = [107 "---",108 "name: hello",109 'description: "Say hello."',110 "metadata:",111 ' managed-by: "someone-else"',112 ` content-hash: "${helloDigest}"`,113 "---",114 "hello",115 "",116 ].join("\n");117118 expect(readManaged(file)).toStrictEqual(Option.none());119 });120});121122describe(describedIn, () => {123 it.effect(124 "reads back the description frontmatter wrote, quotes and colons kept",125 () =>126 Effect.gen(function* () {127 const description = 'Answer one of datekit\'s asks: say "hello".';128129 const file = yield* frontmatter({130 body: "hello\n",131 description,132 landed: Option.some(landed),133 name: "hello",134 });135136 expect(describedIn(file)).toStrictEqual(Option.some(description));137 })138 );139140 it("reads a plain description, as a hand-written skill gives one", () => {141 expect(142 describedIn("---\nname: hello\ndescription: Say hello.\n---\nhello\n")143 ).toStrictEqual(Option.some("Say hello."));144 });145146 it("is None without the frontmatter, or without a description in it", () => {147 expect([148 describedIn("description: Say hello.\n\nhello\n"),149 describedIn("---\nname: hello\n---\ndescription: Say hello.\n"),150 describedIn("---\nname: hello\ndescription: \n---\nhello\n"),151 ]).toStrictEqual([Option.none(), Option.none(), Option.none()]);152 });153});154155describe(isStale, () => {156 it.effect("is None for a fresh copy", () =>157 Effect.gen(function* () {158 const current = yield* contributeSkill({ landed, policy });159160 expect(yield* isStale(current.content, current)).toStrictEqual(161 Option.none()162 );163 })164 );165166 it.effect(167 "is None for a copy from an earlier landing that left the policy's text alone",168 () =>169 Effect.gen(function* () {170 const onDisk = yield* contributeSkill({ landed, policy });171 const current = yield* contributeSkill({ landed: later, policy });172173 expect(onDisk.content).not.toBe(current.content);174 expect(yield* isStale(onDisk.content, current)).toStrictEqual(175 Option.none()176 );177 })178 );179180 it.effect(181 "names the copy's landing and hash when the run block changed since",182 () =>183 Effect.gen(function* () {184 const onDisk = yield* contributeSkill({ landed, policy });185186 const current = yield* contributeSkill({187 landed: later,188 policy: policyWithNewLint,189 });190191 expect(recordedHash(onDisk.content)).not.toBe(192 recordedHash(current.content)193 );194 expect(yield* isStale(onDisk.content, current)).toStrictEqual(195 Option.some({196 hash: recordedHash(onDisk.content),197 landed: Option.some(landed),198 })199 );200 })201 );202203 it.effect(204 "names the copy's landing and hash when its body was edited by hand",205 () =>206 Effect.gen(function* () {207 const current = yield* contributeSkill({ landed, policy });208 const edited = current.content.replace("bun test {path}", "npm test");209210 expect(edited).not.toBe(current.content);211 expect(yield* isStale(edited, current)).toStrictEqual(212 Option.some({213 hash: recordedHash(current.content),214 landed: Option.some(landed),215 })216 );217 })218 );219220 it.effect(221 "is Some, with no landing and the file's own hash, for a copy without the frontmatter",222 () =>223 Effect.gen(function* () {224 const current = yield* contributeSkill({ landed, policy });225226 expect(yield* isStale("hello\n", current)).toStrictEqual(227 Option.some({ hash: helloDigest, landed: Option.none() })228 );229 })230 );231232 it.effect(233 "is Some for a copy whose frontmatter was taken off, though its body is current",234 () =>235 Effect.gen(function* () {236 const current = yield* contributeSkill({ landed, policy });237238 const body = Option.map(239 readManaged(current.content),240 (managed) => managed.body241 );242243 expect(Option.isSome(body)).toBeTruthy();244245 const bare = Option.getOrElse(body, () => "");246247 expect(yield* isStale(bare, current)).toStrictEqual(248 Option.some({249 hash: recordedHash(current.content),250 landed: Option.none(),251 })252 );253 })254 );255256 it.effect(257 "compares a file the generators give without frontmatter as it stands",258 () =>259 Effect.gen(function* () {260 const current = { content: "# Ours\n", path: "AGENTS.md" };261262 expect(yield* isStale("# Ours\n", current)).toStrictEqual(263 Option.none()264 );265 expect(yield* isStale("hello\n", current)).toStrictEqual(266 Option.some({ hash: helloDigest, landed: Option.none() })267 );268 })269 );270});