import { readFileSync } from "node:fs"; import { describe, expect, it } from "vitest"; import { loadCiGateContract } from "../../scripts/contracts/ci-gates.ts"; import { REMOVAL_DRILL_SUITES_OUTSIDE_COVERAGE, SCAFFOLD_DRILL_COVERED_SUITES, scaffoldDrillCommands, } from "../../scripts/lib/feature-scaffold-drill.ts"; import { gatedTestPaths } from "../../scripts/lib/ungated-tests.ts"; /** * FE-GATE-020's drills ran each test file twice in one copy: the plain suites * and then test:coverage over the same paths (feature scaffold), test:all and * then test:coverage (feature removal). Each runs once now. These tests keep * the claim true: a path added to a plain suite or to test:all that coverage * does not run fails here instead of silently leaving the drill. */ const scripts = ( JSON.parse(readFileSync("package.json", "utf8")) as { scripts: Record } ).scripts; const readConfig = (file: string) => readFileSync(file, "utf8"); const pathsOf = (entries: readonly string[]) => gatedTestPaths(scripts, entries, readConfig); const within = (target: string, roots: readonly string[]) => roots.some((root) => target === root || target.startsWith(`${root}/`)); // A copy that removed the optional recipes has no test:recipes; the drill then // runs only what the copy's package.json still has (scripts/test-sample-removal.ts). const outsideCoverage = REMOVAL_DRILL_SUITES_OUTSIDE_COVERAGE.filter((script) => Object.hasOwn(scripts, script)); describe("removal drill test paths", () => { const coverage = pathsOf(["test:coverage"]); it("leave out of the scaffold drill only suites test:coverage runs whole", () => { expect(coverage.length).toBeGreaterThan(0); for (const suite of SCAFFOLD_DRILL_COVERED_SUITES) { const paths = pathsOf([suite]); expect(paths.length, suite).toBeGreaterThan(0); expect(paths.filter((target) => !within(target, coverage)), suite).toEqual([]); } }); it("run every test:all path in the feature removal: coverage and what it leaves out", () => { const run = [...coverage, ...pathsOf(outsideCoverage)]; expect(pathsOf(["test:all"]).filter((target) => !within(target, run))).toEqual([]); }); it("name as outside coverage only suites coverage does not already run", () => { for (const suite of outsideCoverage) { expect(pathsOf([suite]).some((target) => !within(target, coverage)), suite).toBe(true); } }); it("run the covered suites plain again when the copy's merge stage has no test:coverage", async () => { const contract = await loadCiGateContract(process.cwd()); const coverageIds = new Set( contract.commands.filter(({ script }) => script === "test:coverage").map(({ id }) => id), ); const withoutCoverage = { ...contract, gates: contract.gates.map((gate) => ({ ...gate, commandIds: gate.commandIds.filter((id) => !coverageIds.has(id)), })), }; const withCoverage = scaffoldDrillCommands(contract).map(({ script }) => script); const without = scaffoldDrillCommands(withoutCoverage).map(({ script }) => script); for (const suite of SCAFFOLD_DRILL_COVERED_SUITES) { expect(withCoverage, suite).not.toContain(suite); expect(without, suite).toContain(suite); } }); });