Files
clean-architecture-frontend…/tests/unit/removal-fixture.test.ts
T
DongHyeonka f08c93ab2e fix(removal): 문서를 읽는 테스트 제외 목록을 모든 제거 드릴이 같이 쓴다
문제
FE-GATE-020의 제거 드릴은 정해진 목록만 fixture에 복사하고, 그 목록에
README.md·CHANGELOG.md·docs/가 없다. 그런데 fixture에서 도는 test:all에는
그 문서를 읽는 테스트 두 개(documentation-readiness,
changelog-review-ids)가 들어 있다. sample-removal 드릴만 두 테스트를
지웠고, browser-file-storage·realtime·optional-recipe 드릴은 제거 대상과
무관하게 `test` 검사에서 ENOENT로 항상 실패했다. 세 드릴의 실패는 merge
게이트를 빨갛게 하고 release 단계도 막는다.

고른 방법
- scripts/lib/removal-fixture.ts에 문서 목록
  (REMOVAL_FIXTURE_DOCUMENT_TARGETS)과 그 문서를 읽는 테스트 목록
  (REMOVAL_FIXTURE_DOCUMENT_TESTS)을 한 번만 둔다. prepareRemovalFixture는
  문서를 복사하지 않은 fixture에서 그 테스트를 지운다. 문서를 복사하는
  드릴(test:feature-scaffold)에서는 그대로 돈다.
- sample-removal 드릴의 자체 제외 두 줄을 지운다.
- 버린 대안: 모든 드릴이 문서까지 복사. capability 드릴은 문서가
  설명하는 runtime을 지우므로, 문서 테스트가 지운 경로를 실패로 본다.
  드릴마다 문서 예외가 필요해진다. 드릴마다 제외를 복사. 지금 고장의
  원인(갈라진 목록)을 되풀이한다.
- 단위 테스트가 목록을 고정한다: 문서 없이 복사하면 제외, 문서를
  복사하면 제외 없음, 제외 대상이 실제로 문서를 읽음, 다섯 드릴이 모두
  prepareRemovalFixture를 쓰고 자체 제외 목록을 두지 않음.

위험·한계
- 저장소 루트 문서를 읽는 테스트가 더 생기면 이 목록에 더해야 한다.
  더하지 않으면 capability 드릴이 다시 ENOENT로 실패한다(조용히 통과하지는
  않는다).
- 세 드릴의 실제 실행 결과는 이 커밋 메시지에 적지 않았다(공유 잠금 대기).

검증
- RED: 새 테스트 "is the only exclusion list"가 수정 전 sample 드릴의 자체
  목록 때문에 실패.
- tests/unit/removal-fixture.test.ts 12건 통과, 바꾼 파일 eslint 통과.

Refs: docs/reviews/2026-11-03-maturity-review/02-adapters-browser-files-cache.md 02-06
2026-11-04 21:01:33 +09:00

245 lines
10 KiB
TypeScript

import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
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", "config/ci/contract-baseline.json", "package.json"]) {
await writeFile(path.join(root, file), await readFile(file));
}
return root;
}
// D-13: a removal takes the same step a downstream project takes — it records
// the reduced contract as the new baseline — and then loads canonically.
it("prunes removed command and evidence references and re-baselines the contract", async () => {
const root = await copyCiContract("removal-fixture-contract-");
await pruneRemovalFixtureCiContract({
root,
removedScripts: new Set(["test:reference-feature"]),
removedEvidencePathFragments: ["reference-feature.xml"],
});
const contract = await loadCiGateContract(root);
expect(contract.commands.some(({ script }) => script === "test:reference-feature")).toBe(false);
expect(contract.artifacts.some(({ path }) => path.includes("reference-feature.xml"))).toBe(false);
expect(contract.gates.some(({ commandIds }) => commandIds.includes("test-reference-feature"))).toBe(false);
expect(contract.gates.some(({ evidenceArtifactIds }) =>
evidenceArtifactIds.includes("artifact-artifacts-tests-reference-feature-xml")
)).toBe(false);
const baseline = JSON.parse(
await readFile(path.join(root, "config/ci/contract-baseline.json"), "utf8"),
) as Record<string, unknown>;
expect(baseline).toMatchObject({ commands: contract.commands.length, owner: "removal-fixture" });
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. The baseline reason then ended in a blank ("removal fixture pruned
// ") and the baseline schema refused it before any gate ran.
it("re-baselines a removal that prunes no CI script", async () => {
const root = await copyCiContract("removal-fixture-no-scripts-");
await pruneRemovalFixtureCiContract({
root,
removedScripts: new Set(),
removedEvidencePathFragments: [],
});
const baseline = JSON.parse(
await readFile(path.join(root, "config/ci/contract-baseline.json"), "utf8"),
) as Record<string, unknown>;
expect(baseline["reason"]).toBe("removal fixture pruned no CI scripts");
await expect(loadCiGateContract(root)).resolves.toBeDefined();
});
it("rejects a contract that no longer matches its committed baseline", async () => {
const root = await copyCiContract("removal-fixture-baseline-drift-");
const baselinePath = path.join(root, "config/ci/contract-baseline.json");
const baseline = JSON.parse(await readFile(baselinePath, "utf8")) as Record<string, number>;
await writeFile(baselinePath, JSON.stringify({ ...baseline, commands: baseline.commands! + 1 }));
await expect(loadCiGateContract(root)).rejects.toThrow(
/contract-baseline\.json[\s\S]*commands[\s\S]*update:ci-contract-baseline/u,
);
});
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);
});
// 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([]);
});
it("names tests that exist and read the documents", async () => {
for (const test of REMOVAL_FIXTURE_DOCUMENT_TESTS) {
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(");
for (const test of REMOVAL_FIXTURE_DOCUMENT_TESTS) {
expect(source, drill).not.toContain(test);
}
}
});
});
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("reference-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);
});
});