generated.test.ts
This file
Path
packages/project/test/generated.test.ts
310 lines · 8.5 KiB · TypeScript
Last changed by
Merge build/maintainer-wording into mainoscabrielbefore adoption4c1a571
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311import { describe, expect, it } from "@effect/vitest";import { creditsOf, parseTrailers } from "@repo/git/trailers";import type { LandingView } from "@repo/protocol/domain/landing";import type { Policy } from "@repo/protocol/domain/policy";import { RunBlock } from "@repo/protocol/domain/run";import { Handle, LandedSha } from "@repo/protocol/ids";import { aProject } from "@repo/testkit/scenarios";import { DateTime, Effect, Option, Schema } from "effect";
import { platformHandle } from "../src/attention/effects.ts";import { generatedFollowUp } from "../src/generated.ts";import type { Generate } from "../src/generated.ts";
const scenario = aProject({ deciders: ["alice", "dora"], people: ["bob"] });
const { initializer, project } = scenario;
const alice = initializer;
const dora = Handle.make("dora");
const bob = Handle.make("bob");
const parent = LandedSha.make("1".repeat(40));
const landed = LandedSha.make("2".repeat(40));
const before: Policy = { ...scenario.policy, at: parent };
const unchanged: Policy = { ...scenario.policy, at: landed };
const withUnit: Policy = { ...unchanged, run: Schema.decodeSync(RunBlock)({ checks: { lint: "bun run lint", types: "bun run typecheck", unit: "bun run test", }, test: "bun test {path}", }),};
const skill = `.agents/skills/${project}-contribute/SKILL.md`;
const claudeSkill = `.claude/skills/${project}-contribute/SKILL.md`;
const generate: Generate = ({ landed: at, policy, read }) => Effect.gen(function* () { const lanes = Object.entries(policy.run.checks) .map(([lane, command]) => `${lane}: ${command}`) .join(", ");
const content = `---\nlanded: ${at}\n---\nLanes: ${lanes}\n`; const block = `<!-- BEGIN -->\nLanes: ${lanes}\n<!-- END -->`; const existing = Option.getOrElse(yield* read("AGENTS.md"), () => ""); const begin = existing.indexOf("<!-- BEGIN -->"); const end = existing.indexOf("<!-- END -->");
const agents = begin === -1 || end === -1 ? `${existing}${block}\n` : `${existing.slice(0, begin)}${block}${existing.slice(end + "<!-- END -->".length)}`;
return [ { content, path: skill }, { content, path: claudeSkill }, { content: agents, path: "AGENTS.md" }, ]; });
const ownAgents = [ "# Datekit", "", "Our notes.", "", "<!-- BEGIN -->", "Lanes: lint: bun run lint", "<!-- END -->", "", "More notes.", "",].join("\n");
const treeOf = (files: Readonly<Record<string, string>>) => (path: string): Effect.Effect<Option.Option<string>> => Effect.succeed(Option.fromUndefinedOr(files[path]));
const at = DateTime.makeUnsafe("2026-10-07T12:00:00Z");
const aliceLands: LandingView = { assisted: [], author: alice, credits: [{ handle: alice, role: "Signed-off-by" }], landedAt: at, parent, project, sha: landed,};
const firstPush: LandingView = { assisted: [], author: alice, credits: [], landedAt: at, project, sha: landed,};
const followUp = (input: { readonly before: Option.Option<Policy>; readonly after: Policy; readonly landing: LandingView; readonly tree?: Readonly<Record<string, string>>;}) => generatedFollowUp( { after: input.after, before: input.before, initializer, landing: input.landing, project, read: treeOf(input.tree ?? { "AGENTS.md": ownAgents }), }, generate );
const subjectOf = (message: string) => message.split("\n")[0] ?? "";
const creditOf = (landing: LandingView) => Effect.map( followUp({ after: withUnit, before: Option.some(before), landing }), Option.map(({ credits }) => credits) );
describe(generatedFollowUp, () => { it.effect( "follows a landing that changed the run block with the regenerated files alone, credited to its maintainer", () => Effect.gen(function* () { const made = yield* followUp({ after: withUnit, before: Option.some(before), landing: aliceLands, });
const lanes = "Lanes: lint: bun run lint, types: bun run typecheck, unit: bun run test";
const skillText = `---\nlanded: ${landed}\n---\n${lanes}\n`;
expect(Option.map(made, ({ files }) => files)).toStrictEqual( Option.some([ { content: skillText, path: skill }, { content: skillText, path: claudeSkill }, { content: [ "# Datekit", "", "Our notes.", "", "<!-- BEGIN -->", lanes, "<!-- END -->", "", "More notes.", "", ].join("\n"), path: "AGENTS.md", }, ]) );
const { author, credits, message, requestId } = Option.getOrThrow(made);
expect({ author, credits, requestId, subject: subjectOf(message), trailers: creditsOf(message).credits, trailersRequest: parseTrailers(message).find( ({ key }) => key === "Request-Id" )?.value, }).toStrictEqual({ author: platformHandle, credits: [{ handle: alice, role: "Decided-by" }], requestId: `gen-${landed}`, subject: "Regenerate the contribute skill and the AGENTS.md block", trailers: [{ handle: alice, role: "Decided-by" }], trailersRequest: `gen-${landed}`, }); }) );
it.effect("lands nothing after a landing that changed only src/", () => Effect.gen(function* () { expect( yield* followUp({ after: unchanged, before: Option.some(before), landing: aliceLands, tree: {}, }) ).toStrictEqual(Option.none()); }) );
it.effect("makes the same files from the same input", () => Effect.gen(function* () { const input = { after: withUnit, before: Option.some(before), landing: aliceLands, };
expect(yield* followUp(input)).toStrictEqual(yield* followUp(input)); }) );
it.effect( "lands nothing when the tree already holds the regenerated files, as after a replay", () => Effect.gen(function* () { const made = Option.getOrThrow( yield* followUp({ after: withUnit, before: Option.some(before), landing: aliceLands, }) );
const regenerated = Object.fromEntries( made.files.map(({ content, path }) => [path, content]) );
expect( yield* followUp({ after: withUnit, before: Option.some(before), landing: aliceLands, tree: regenerated, }) ).toStrictEqual(Option.none()); }) );
it.effect( "follows the first adoption, which had no policy before, crediting the initializer", () => Effect.gen(function* () { const made = yield* followUp({ after: unchanged, before: Option.none(), landing: firstPush, tree: {}, });
expect( Option.map(made, ({ credits, files }) => ({ credits, paths: files.map(({ path }) => path), })) ).toStrictEqual( Option.some({ credits: [{ handle: alice, role: "Decided-by" }], paths: [skill, claudeSkill, "AGENTS.md"], }) ); }) );
it.effect( "credits the landing's Decided-by first, then its author if they decide the config, else the initializer", () => Effect.gen(function* () { const decidedByDora: LandingView = { ...aliceLands, author: bob, credits: [ { handle: bob, role: "Signed-off-by" }, { handle: dora, role: "Decided-by" }, ], };
const doraLands: LandingView = { ...aliceLands, author: dora, credits: [{ handle: dora, role: "Signed-off-by" }], };
const bobAlone: LandingView = { ...aliceLands, author: bob, credits: [{ handle: bob, role: "Signed-off-by" }], };
expect({ decidedBy: yield* creditOf(decidedByDora), deciderAuthor: yield* creditOf(doraLands), neither: yield* creditOf(bobAlone), }).toStrictEqual({ decidedBy: Option.some([{ handle: dora, role: "Decided-by" }]), deciderAuthor: Option.some([{ handle: dora, role: "Decided-by" }]), neither: Option.some([{ handle: alice, role: "Decided-by" }]), }); }) );});