diff --git a/scripts/lib/feature-scaffold-drill.ts b/scripts/lib/feature-scaffold-drill.ts index f125bdbf..bbbd06f7 100644 --- a/scripts/lib/feature-scaffold-drill.ts +++ b/scripts/lib/feature-scaffold-drill.ts @@ -88,12 +88,16 @@ export function scaffoldDrillCommands( })); } } - // An exclusion the merge stage no longer runs would hide nothing today and - // silently drop the command if it came back under the same name. - const stale = Object.keys(excluded).filter((script) => !mergeScripts.has(script)); + // An exclusion for a command that moved out of the merge stage would hide + // nothing today and silently drop it if it came back. A command the contract + // no longer has at all (a removal drill pruned its capability) is not stale. + const contractScripts = new Set(contract.commands.map(({ script }) => script)); + const stale = Object.keys(excluded).filter( + (script) => contractScripts.has(script) && !mergeScripts.has(script), + ); if (stale.length > 0) { throw new TypeError( - `the scaffold drill excludes scripts the merge stage does not run: ${stale.join(", ")}`, + `the scaffold drill excludes scripts the contract runs outside the merge stage: ${stale.join(", ")}`, ); } return Object.freeze(selected); diff --git a/tests/unit/feature-scaffold-drill.test.ts b/tests/unit/feature-scaffold-drill.test.ts index 414ae81a..603118db 100644 --- a/tests/unit/feature-scaffold-drill.test.ts +++ b/tests/unit/feature-scaffold-drill.test.ts @@ -49,12 +49,34 @@ describe("feature scaffold drill commands", () => { expect(scaffoldDrillCommands(contract).map(({ name }) => name)).toContain("FE-GATE-010:check-probe"); }); - it("refuses an exclusion for a script the merge stage does not run", async () => { + it("refuses an exclusion for a command the contract runs outside the merge stage", async () => { const contract = await loadCiGateContract(process.cwd()); + const merge = new Set(contract.stages.find(({ id }) => id === "merge")!.gateIds); + const mergeCommandIds = new Set( + contract.gates.filter(({ id }) => merge.has(id)).flatMap(({ commandIds }) => commandIds), + ); + const mergeScripts = new Set( + contract.commands.filter(({ id }) => mergeCommandIds.has(id)).map(({ script }) => script), + ); + const outside = contract.commands.find(({ script }) => !mergeScripts.has(script))!; expect(() => scaffoldDrillCommands(contract, { ...SCAFFOLD_DRILL_EXCLUDED_SCRIPTS, - "check:gone": "no longer in the merge stage", - })).toThrow(/does not run: check:gone/u); + [outside.script]: "not in the merge stage", + })).toThrow(`outside the merge stage: ${outside.script}`); + }); + + // A removal drill prunes its capability's commands from the contract, and + // this file runs inside that fixture too. + it("accepts an exclusion whose command the contract no longer has", async () => { + const contract = structuredClone(await loadCiGateContract(process.cwd())); + const removed = contract.commands.find(({ script }) => + Object.hasOwn(SCAFFOLD_DRILL_EXCLUDED_SCRIPTS, script) + )!; + contract.commands = contract.commands.filter(({ id }) => id !== removed.id); + for (const gate of contract.gates) { + gate.commandIds = gate.commandIds.filter((id) => id !== removed.id); + } + expect(() => scaffoldDrillCommands(contract)).not.toThrow(); }); it("is what test:feature-scaffold runs", async () => { diff --git a/tests/unit/removal-fixture.test.ts b/tests/unit/removal-fixture.test.ts index b34457c0..e48b8283 100644 --- a/tests/unit/removal-fixture.test.ts +++ b/tests/unit/removal-fixture.test.ts @@ -1,4 +1,5 @@ -import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { existsSync } from "node:fs"; +import { mkdir, mkdtemp, readdir, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; @@ -149,26 +150,31 @@ describe("document tests in a removal fixture", () => { ])).toEqual([]); }); - it("names tests that exist and read the documents", async () => { + // This file also runs inside the capability fixtures, which hold neither + // the documents nor these tests; the two go together. + it("names tests that read the documents, present exactly where the documents are", async () => { + const documentsPresent = REMOVAL_FIXTURE_DOCUMENT_TARGETS.every((target) => existsSync(target)); for (const test of REMOVAL_FIXTURE_DOCUMENT_TESTS) { - await expect(readFile(test, "utf8")).resolves.toMatch(/README\.md|CHANGELOG\.md/u); + expect(existsSync(test), test).toBe(documentsPresent); + if (documentsPresent) { + await expect(readFile(test, "utf8")).resolves.toMatch(/README\.md|CHANGELOG\.md/u); + } } }); it("is the only exclusion list: every drill prepares its fixture through it", async () => { - for (const drill of [ - "scripts/test-sample-removal.ts", - "scripts/test-feature-scaffold.ts", - "scripts/test-browser-file-storage-runtime-removal.ts", - "scripts/test-realtime-runtime-removal.ts", - "scripts/test-optional-recipe-removal.ts", - ]) { - const source = await readFile(drill, "utf8"); - expect(source, drill).toContain("prepareRemovalFixture("); + const drills: string[] = []; + for (const name of await readdir("scripts")) { + if (!/^test-.*\.ts$/u.test(name)) continue; + const source = await readFile(path.join("scripts", name), "utf8"); + if (!source.includes("./lib/removal-fixture.ts")) continue; + drills.push(name); + expect(source, name).toContain("prepareRemovalFixture("); for (const test of REMOVAL_FIXTURE_DOCUMENT_TESTS) { - expect(source, drill).not.toContain(test); + expect(source, name).not.toContain(test); } } + expect(drills).toEqual(expect.arrayContaining(["test-sample-removal.ts", "test-feature-scaffold.ts"])); }); });