Files
clean-architecture-frontend…/tests/unit/removal-fixture.test.ts
donghyeon.kang f6b2b96c53 ci(gates): 검사기 자기 시험과 gating 검사를 main 단계로 내리고 계약 기준선을 지운다
merge 단계(=PR)가 변경이 아니라 검사기 자신을 시험하는 명령까지 돈다.
검사기가 나쁜 입력을 거부하는지 보는 `*:fixture` 명령 15개, 모든 시험
파일과 검증 script가 게이트에 걸렸는지 보는 ungated 검사, CI 계약의
개수·해시 기준선(`config/ci/contract-baseline.json`)이다. 기준선은 게이트
명령 하나를 더하거나 빼도 OWNER·REASON을 붙여 다시 받아야 했다.
소유자 결정(2026-09-16)은 개수·배선 검사를 main 단계에 두는 것이다.

고른 방법(design-docs repo-structure/03-ci.md 7.3절 F2, README.md 3.3절,
소유자 승인 2026-11-07):
- 새 게이트 FE-GATE-029 `checker-self-tests`, 새 단계 `main`
  (`MAIN_READY`, 승격을 막지 않는다), 새 job `main_gate`. 브랜치 push와
  `workflow_dispatch`(stage=main)에서만 돈다. PR과 태그에서는 돌지 않는다.
- FE-GATE-001·005·008·010·013의 `*:fixture` 명령 15개와 그 증거
  산출물 10개를 FE-GATE-029로 옮긴다.
- `check:ci`의 ungated 검사를 `scripts/check-ci-gating.ts`
  (`check:ci:gating`)로 떼어 FE-GATE-029에 둔다. `check:ci`는 계약·
  workflow 검사만 PR에서 돈다.
- 기준선 파일, `update:ci-contract-baseline`, 그 검사를 지운다(소유자
  결정). 계약 모양은 ci-gates.ts의 의미 검사가 지킨다. 개수를 고정하던
  단위 시험의 개수 단언도 뺐다.

남긴 것: 음성 타입 fixture(`check:types:fixtures`, FE-GATE-003)는
이름이 `*:fixture`여도 merge에 둔다. 검사기가 아니라 src의 포트·계약
타입을 시험한다. `TelemetryPort.emit`을 넓히는 변경을 PR에서 막는다(정리
3단계 9.3절 변이로 확인). merge 단계 명령 참조 64 → 49.

위험: 검사기를 망가뜨린 PR은 merge를 통과하고 main에서 빨개진다. 그
대가로 PR이 검사기 자기 시험을 기다리지 않는다(설계 7.5절의 같은 교환).
FE-GATE-029의 명령이 앞 게이트 산출물 없이 새 checkout에서 도는지는
desktop에서 따로 확인했다(PR 본문).

검증(desktop): check:ci, check:ci:gating, check:types, CI 시험 통과.
merge 단계·FE-GATE-029 실행 결과는 PR 본문에 적는다.

Refs: design-docs repo-structure/03-ci.md 7.3·7.4절, README.md 3.3절
2026-11-08 21:56:37 +09:00

304 lines
12 KiB
TypeScript

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";
import { parseAsync } from "@babel/core";
import { afterEach, describe, expect, it } from "vitest";
import { loadCiGateContract } from "../../scripts/contracts/ci-gates.ts";
import {
assertNoRuntimeImports,
pruneRemovalFixtureCiContract,
REMOVAL_FIXTURE_COPY_TARGETS,
REMOVAL_FIXTURE_DOCUMENT_TARGETS,
REMOVAL_FIXTURE_DOCUMENT_TESTS,
removalFixtureExcludedTests,
removeRuntimeDependentTests,
renderRemovalJunit,
runtimeImportGraph,
} from "../../scripts/lib/removal-fixture.ts";
const temporaryRoots: string[] = [];
afterEach(async () => {
await Promise.all(
temporaryRoots.splice(0).map((root) => rm(root, { recursive: true, force: true })),
);
});
it("rejects an empty scanned module universe", async () => {
const root = await mkdtemp(path.join(tmpdir(), "removal-fixture-empty-modules-"));
temporaryRoots.push(root);
await expect(runtimeImportGraph(root, ["src/runtime"])).rejects.toThrow(
/scanned module universe is empty/i,
);
});
it("rejects a runtime removal scan that discovers no dependent tests", async () => {
const root = await mkdtemp(path.join(tmpdir(), "removal-fixture-zero-dependent-tests-"));
temporaryRoots.push(root);
await mkdir(path.join(root, "src/runtime"), { recursive: true });
await mkdir(path.join(root, "tests"), { recursive: true });
await writeFile(path.join(root, "src/runtime/index.ts"), "export const runtime = true;\n");
await writeFile(path.join(root, "tests/unrelated.test.ts"), "export const unrelated = true;\n");
await expect(removeRuntimeDependentTests(root, ["src/runtime"])).rejects.toThrow(
/no runtime-dependent tests discovered/i,
);
});
it("allows zero runtime imports after the runtime has been removed", async () => {
const root = await mkdtemp(path.join(tmpdir(), "removal-fixture-post-removal-"));
temporaryRoots.push(root);
await mkdir(path.join(root, "tests"), { recursive: true });
await writeFile(path.join(root, "tests/unrelated.test.ts"), "export const unrelated = true;\n");
await expect(
assertNoRuntimeImports(root, ["src/runtime"], "fixture runtime"),
).resolves.toBeUndefined();
});
async function copyCiContract(prefix: string): Promise<string> {
const root = await mkdtemp(path.join(tmpdir(), prefix));
temporaryRoots.push(root);
await mkdir(path.join(root, "config/ci"), { recursive: true });
for (const file of ["config/ci/gates.json", "package.json"]) {
await writeFile(path.join(root, file), await readFile(file));
}
return root;
}
it("prunes removed command and evidence references and loads the reduced contract", async () => {
const root = await copyCiContract("removal-fixture-contract-");
// A command with evidence of its own, as a removed sample's tests had. Not
// one a removal drill deletes: this test also runs inside those copies
// (test:recipes is gone in the optional-recipe removal copy).
await pruneRemovalFixtureCiContract({
root,
removedScripts: new Set(["test:integration"]),
removedEvidencePathFragments: ["integration.xml"],
});
const contract = await loadCiGateContract(root);
expect(contract.commands.some(({ script }) => script === "test:integration")).toBe(false);
expect(contract.artifacts.some(({ path }) => path.includes("integration.xml"))).toBe(false);
expect(contract.gates.some(({ commandIds }) => commandIds.includes("test-integration"))).toBe(false);
expect(contract.gates.some(({ evidenceArtifactIds }) =>
evidenceArtifactIds.includes("artifact-artifacts-tests-integration-xml")
)).toBe(false);
const packageDocument = JSON.parse(await readFile(path.join(root, "package.json"), "utf8")) as {
scripts: Record<string, string>;
};
expect(packageDocument.scripts["check:ci"]).not.toMatch(/reduced/u);
});
// 1103/16-02. A product feature owns no CI script, so its removal prunes
// nothing, and the contract still loads.
it("loads a removal that prunes no CI script", async () => {
const root = await copyCiContract("removal-fixture-no-scripts-");
await pruneRemovalFixtureCiContract({
root,
removedScripts: new Set(),
removedEvidencePathFragments: [],
});
await expect(loadCiGateContract(root)).resolves.toBeDefined();
});
it("rejects pruning that leaves a reduced gate without commands", async () => {
const root = await copyCiContract("removal-fixture-empty-gate-");
await expect(pruneRemovalFixtureCiContract({
root,
removedScripts: new Set(["test:runtime-schema"]),
removedEvidencePathFragments: ["runtime-schema.xml"],
})).rejects.toThrow(/commandIds|too small|at least 1/i);
});
type BabelOptions = NonNullable<Parameters<typeof parseAsync>[1]>;
type BabelParserPlugin = NonNullable<NonNullable<BabelOptions["parserOpts"]>["plugins"]>[number];
type SyntaxNode = { type: string } & Record<string, unknown>;
/**
* 1106/12-01. The document paths the code of `source` spells out: string and
* template literal parts that are a document target (README.md, CHANGELOG.md,
* docs) or a path under one. Comments are not code.
*/
async function documentsNamedInCode(file: string, source: string): Promise<string[]> {
const plugins: BabelParserPlugin[] = [["typescript", { dts: false }]];
const tree = (await parseAsync(source, {
filename: file,
babelrc: false,
configFile: false,
sourceType: "module",
parserOpts: { plugins },
})) as unknown as SyntaxNode | null;
if (!tree) throw new Error(`cannot parse ${file}`);
const strings: string[] = [];
const visit = (node: SyntaxNode): void => {
const literal = node["value"];
if (node.type === "StringLiteral" && typeof literal === "string") strings.push(literal);
if (node.type === "TemplateElement") {
const cooked = (literal as { cooked?: unknown } | undefined)?.cooked;
if (typeof cooked === "string") strings.push(cooked);
}
for (const [key, value] of Object.entries(node)) {
if (key === "comments" || key.endsWith("Comments")) continue;
for (const child of Array.isArray(value) ? value : [value]) {
if (child && typeof child === "object" && typeof (child as SyntaxNode).type === "string") {
visit(child as SyntaxNode);
}
}
}
};
visit(tree);
return strings.filter((value) =>
REMOVAL_FIXTURE_DOCUMENT_TARGETS.some(
(target) => value === target || value.startsWith(`${target}/`),
),
);
}
// 1103/02-06. Three FE-GATE-020 drills copied the repository without its
// documents and still ran the tests that read them, so each failed its `test`
// check on ENOENT whatever it removed. Only the sample drill excluded them.
describe("document tests in a removal fixture", () => {
it("leaves the document tests out exactly when the documents are not copied", () => {
expect(removalFixtureExcludedTests(REMOVAL_FIXTURE_COPY_TARGETS))
.toEqual(REMOVAL_FIXTURE_DOCUMENT_TESTS);
expect(removalFixtureExcludedTests([
...REMOVAL_FIXTURE_COPY_TARGETS,
...REMOVAL_FIXTURE_DOCUMENT_TARGETS,
])).toEqual([]);
});
// 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) {
expect(existsSync(test), test).toBe(documentsPresent);
if (documentsPresent) {
// 1106/12-01. Named in its code, not in a comment. A comment naming
// the contract document let backend-contract-pin.test.ts on the list,
// and its fourteen checks of config and code left those drills with
// the one that read the document.
expect(await documentsNamedInCode(test, await readFile(test, "utf8")), test).not.toEqual([]);
}
}
});
it("counts a document named in code, not one named in a comment", async () => {
const commentOnly = [
"// Reads docs/architecture/a.md (README.md, CHANGELOG.md).",
"/** docs/manual/b.md */",
'const pin = JSON.parse(readFileSync("config/pin.json", "utf8"));',
"readFileSync(pin.document);",
].join("\n");
expect(await documentsNamedInCode("comment-only.test.ts", commentOnly)).toEqual([]);
const reads = [
'readFileSync("CHANGELOG.md", "utf8");',
"const name: string = 'a';",
"readFileSync(`docs/manual/${name}.md`, 'utf8');",
'z.string().startsWith("docs/");',
'const notADocument = "src/docs/x.ts";',
].join("\n");
expect((await documentsNamedInCode("reads.test.ts", reads)).sort()).toEqual([
"CHANGELOG.md",
"docs/",
"docs/manual/",
]);
});
it("is the only exclusion list: every drill prepares its fixture through it", async () => {
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, name).not.toContain(test);
}
}
expect(drills).toEqual(expect.arrayContaining(["test-sample-removal.ts", "test-feature-scaffold.ts"]));
});
});
describe("release evidence fixture copy", () => {
it("keeps the release evidence and leaves the regenerated trees behind", async () => {
const { copyReleaseEvidenceTree, RELEASE_EVIDENCE_REGENERATED_TREES } =
await import("../../scripts/lib/removal-fixture.ts");
const { RELEASE_CANDIDATE_EVIDENCE_PATHS } = await import(
"../../scripts/lib/release-candidate.ts"
);
const { access, readdir } = await import("node:fs/promises");
// The source is a fake checkout, not process.cwd(): release evidence is
// git-ignored, so a fresh clone has none of it and the copy would be
// checked against nothing.
const source = await mkdtemp(path.join(tmpdir(), "release-evidence-source-"));
const target = await mkdtemp(path.join(tmpdir(), "release-evidence-target-"));
temporaryRoots.push(source, target);
const evidencePaths = RELEASE_CANDIDATE_EVIDENCE_PATHS.filter((evidence) =>
evidence.startsWith("artifacts/")
);
expect(evidencePaths.length).toBeGreaterThan(0);
for (const relative of [
...evidencePaths,
...RELEASE_EVIDENCE_REGENERATED_TREES.map((tree) => `${tree}/trace.zip`),
]) {
await mkdir(path.dirname(path.join(source, relative)), { recursive: true });
await writeFile(path.join(source, relative), "{}\n");
}
await copyReleaseEvidenceTree(source, target);
// Every release artifact has to survive the copy; a fixture missing one
// cannot build a candidate at all, and every provider suite then fails
// while constructing its own fixture.
for (const evidence of evidencePaths) {
await expect(access(path.join(target, evidence)), evidence).resolves.toBeUndefined();
}
// The regenerated trees are why this is a filter and not a plain copy: they
// are tens of megabytes of traces and coverage HTML. They still exist,
// because the repository inventory expects the directories.
for (const tree of RELEASE_EVIDENCE_REGENERATED_TREES) {
await expect(readdir(path.join(target, tree)), tree).resolves.toEqual([]);
}
});
});
describe("removal JUnit report (14-08)", () => {
it("counts every failure and gives each check its own cause", () => {
const xml = renderRemovalJunit("sample-feature-removal", [
{ name: "typecheck", passed: false, failure: "corepack pnpm check:types exited 2" },
{ name: "architecture", passed: true },
{ name: "build", passed: false, failure: "corepack pnpm build exited 1" },
{
name: "fixture-id-residue",
passed: false,
failure: "residue: tests/a.test.ts, tests/<b>&c.ts",
},
]);
expect(xml).toContain('tests="4" failures="3"');
expect(xml).toContain(
'<testcase name="typecheck"><failure message="corepack pnpm check:types exited 2" /></testcase>',
);
expect(xml).toContain('<testcase name="architecture"></testcase>');
expect(xml).toContain(
'<failure message="corepack pnpm build exited 1" />',
);
expect(xml).toContain(
'message="residue: tests/a.test.ts, tests/&lt;b&gt;&amp;c.ts"',
);
expect(xml.match(/residue:/gu)).toHaveLength(1);
});
});