agents-block.test.ts
This file
Path
packages/agent-kit/test/agents-block.test.ts
119 lines · 3.5 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–119why, one step back1import { describe, expect, it } from "@effect/vitest";2import { Brand } from "@repo/brand";3import { Effect } from "effect";45import { agentsBlock, upsertAgentsBlock } from "../src/agents-block.ts";6import { contentHash } from "../src/generated-file.ts";7import {8 landed,9 later,10 policy,11 policyWithNewLint,12 project,13} from "./fixtures.ts";1415const end = `<!-- END ${Brand.slug} contribute -->`;1617const beginWith = (digit: string) =>18 `<!-- BEGIN ${Brand.slug} contribute (generated; content-hash: sha256:${digit.repeat(64)}) -->`;1920const block = {21 begin: beginWith("1"),22 body: "## Contributing\n\nNew text.\n",23 end,24};2526const older = {27 begin: beginWith("0"),28 body: "## Contributing\n\nOld text,\nover two lines.\n",29 end,30};3132const written = ({ begin, body }: typeof block) => `${begin}\n${body}${end}`;3334describe(upsertAgentsBlock, () => {35 it("inserts the block at the end of an AGENTS.md without one", () => {36 const existing = "# Project\n\nOur own words.\n";3738 expect(upsertAgentsBlock(existing, block)).toBe(39 `${existing}\n${written(block)}\n`40 );41 });4243 it("starts an empty AGENTS.md with the block alone", () => {44 expect(upsertAgentsBlock("", block)).toBe(`${written(block)}\n`);45 });4647 it("puts a blank line after a last line that has no newline", () => {48 expect(upsertAgentsBlock("# Project", block)).toBe(49 `# Project\n\n${written(block)}\n`50 );51 });5253 it("replaces only the text between the markers, whatever the old hash", () => {54 const before = "# Project\n\nOur own words.\n\n";55 const after = "\n## Ours again\n\nMore of our words.\n";5657 expect(upsertAgentsBlock(`${before}${written(older)}${after}`, block)).toBe(58 `${before}${written(block)}${after}`59 );60 });6162 it("is a no-op when applied twice", () => {63 const appended = upsertAgentsBlock("# Project\n\nOur own words.\n", block);6465 const replaced = upsertAgentsBlock(66 `# Project\n\n${written(older)}\n\nAfter.\n`,67 block68 );6970 expect(upsertAgentsBlock(appended, block)).toBe(appended);71 expect(upsertAgentsBlock(replaced, block)).toBe(replaced);72 });7374 it("leaves another tool's markers alone", () => {75 const existing =76 "# Project\n\n<!-- BEGIN other contribute -->\nTheirs.\n<!-- END other contribute -->\n";7778 expect(upsertAgentsBlock(existing, block)).toBe(79 `${existing}\n${written(block)}\n`80 );81 });82});8384describe(agentsBlock, () => {85 it.effect(86 "brackets its body between markers, the first carrying the body's hash",87 () =>88 Effect.gen(function* () {89 const generated = yield* agentsBlock({ landed, policy, project });90 const hash = yield* contentHash(generated.body);9192 expect(hash).toMatch(/^sha256:[0-9a-f]{64}$/u);93 expect(generated.begin).toBe(94 `<!-- BEGIN ${Brand.slug} contribute (generated; content-hash: ${hash}) -->`95 );96 expect(generated.end).toBe(end);97 expect(generated.body).toContain(`${Brand.cli} check`);98 })99 );100101 it.effect(102 "changes its hash when the run block changes, not the landing",103 () =>104 Effect.gen(function* () {105 const first = yield* agentsBlock({ landed, policy, project });106 const again = yield* agentsBlock({ landed: later, policy, project });107108 const changed = yield* agentsBlock({109 landed: later,110 policy: policyWithNewLint,111 project,112 });113114 expect(again).toStrictEqual(first);115 expect(changed.begin).not.toBe(first.begin);116 expect(changed.body).toContain("bunx oxlint --deny-warnings");117 })118 );119});