Files
clean-architecture-frontend…/tests/unit/feature-residue.test.ts
T
DongHyeonka aff267db76 fix(scripts): 기능 제거 드릴의 src 잔여물 검사가 이름 단위로 찾는다
문제: 제거 드릴(`test:sample-removal -- --feature=<id>`)의 잔여물 검사는
`src`의 경로와 본문을 대소문자 무시 부분 문자열로 찾는다. 그래서 합법
ID `product-feature`를 지우면 템플릿 파일 열 개(`product-features.ts`,
`product-features-port.ts`와 그것을 가져오는 파일들)와 템플릿 시험 이름
여섯(`product-feature-switch.test.tsx` 등)이 잔여물로 잡혀 드릴이
실패한다. 매뉴얼 예시 ID `invoice-feature`는 걸리지 않아 남아 있었다.

고른 방법: spec 본문에 이미 쓰는 규칙에 맞춘다. 본문은 이름 단위
(대소문자 구분, 앞뒤에 `[A-Za-z0-9_-]` 없음), 경로는 세그먼트가 이름과
같거나 그 첫 `.` 앞부분이 같을 때만 센다. `src/.generated/`는 기능 목록에서
다시 만드는 파생물이라 뺀다.
- 버린 대안: 「파일 이름이 ID로 시작」. 템플릿의 `product-feature-*` 시험
  이름이 그대로 걸린다.

위험·한계: 한 단어 ID(`search` 등)는 템플릿 본문에 보통 낱말로 있어 여전히
잡힐 수 있다. 본문 규칙으로는 자료와 참조를 가릴 수 없다(spec은 한 단어
이름을 아예 보지 않는다).

검증(desktop): 구현을 main으로 되돌린 상태에서 새 시험 2개 실패(RED),
고친 뒤 feature-residue·feature-removal-argument 11개 통과, eslint·
check:types rc=0. 로그 /tmp/claude-gates/w2fe2-res1/.

Refs: 1105/16-04
2026-11-13 04:57:04 +09:00

175 lines
7.6 KiB
TypeScript

import { mkdir, mkdtemp, readdir, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { afterAll, describe, expect, it } from "vitest";
import { productFeatureResidue } from "../../scripts/lib/feature-residue.ts";
/**
* 1105/16-04. The manual's example ID is `invoice-feature` (01 1단계), and
* its step 11 removes that feature with
* `test:sample-removal -- --feature=invoice-feature`. The drill searched every
* file under `src` and `tests` for the ID as a substring, so the template's
* own tests/unit/create-feature.test.ts, which writes that example ID 51
* times as data, failed the removal (`fixture-id-residue`), and the advice
* was to edit that template file.
*/
const ID = "invoice-feature";
const roots: string[] = [];
afterAll(async () => {
await Promise.all(roots.map((root) => rm(root, { recursive: true, force: true })));
});
async function fixture(files: Readonly<Record<string, string>>): Promise<string> {
const root = await mkdtemp(path.join(tmpdir(), "feature-residue-"));
roots.push(root);
for (const [file, content] of Object.entries(files)) {
await mkdir(path.dirname(path.join(root, file)), { recursive: true });
await writeFile(path.join(root, file), content);
}
return root;
}
// Built at run time so this file does not itself read as an import of the
// feature when the repository is searched below.
const importOf = (target: string) => ["import { x } from", JSON.stringify(target)].join(" ");
describe("product feature residue (1105/16-04)", () => {
it("does not count the template's own tests that name the manual's example ID", async () => {
expect(await productFeatureResidue(process.cwd(), ID, ID)).toEqual([]);
});
it("counts what still points at the removed feature", async () => {
const root = await fixture({
"src/contracts/leftover.ts": `export const OWNER = "${ID}";\n`,
"src/platform/clean.ts": "export const NOTHING = 1;\n",
[`tests/e2e/${ID}.spec.ts`]: "test('opens', () => {});\n",
"tests/unit/imports-feature.test.ts": `${importOf(`../../src/features/${ID}/feature-module.ts`)};\n`,
"tests/unit/imports-feature-tests.test.ts": `${importOf(`../features/${ID}/helpers.ts`)};\n`,
"tests/unit/example-data.test.ts": `const request = { id: "${ID}" };\nconst path = "src/features/${ID}/feature-module.ts";\n`,
});
expect(await productFeatureResidue(root, ID, ID)).toEqual([
"src/contracts/leftover.ts",
`tests/e2e/${ID}.spec.ts`,
"tests/unit/imports-feature-tests.test.ts",
"tests/unit/imports-feature.test.ts",
]);
});
// 1106/16-01. test:all is vitest only and the drill runs no Playwright
// spec, so a spec that names the removed feature in its text and nowhere
// else was caught by no step.
it("counts a Playwright spec that names the feature only in its text", async () => {
const spec = [
`test.describe("${ID}", () => {`,
' test("opens", async ({ page }) => {',
' await page.goto("/invoice");',
" });",
"});",
"",
].join("\n");
const root = await fixture({
"tests/e2e/checkout.spec.ts": spec,
"tests/visual/billing.visual.spec.ts": `const FEATURE = '${ID}';\n`,
// The same text in a vitest test is data (1105/16-04).
"tests/unit/checkout.test.ts": spec,
"tests/e2e/route-only.spec.ts": 'test("opens", ({ page }) => page.goto("/invoice"));\n',
});
expect(await productFeatureResidue(root, ID, ID)).toEqual([
"tests/e2e/checkout.spec.ts",
"tests/visual/billing.visual.spec.ts",
]);
});
it("does not count a spec whose longer names only contain the ID", async () => {
const root = await fixture({
"tests/e2e/builds.spec.ts": [
'run("scripts/generate-product-feature-index.ts");',
'const attribute = "data-product-feature";',
'const registry = "product-features";',
"",
].join("\n"),
});
expect(
await productFeatureResidue(root, "product-feature", "product-feature"),
).toEqual([]);
});
// 1106/16-01 (diff review DR-02). The spec search ignored case and took
// `_` as a boundary, so removing a feature named `messages` counted the
// template's accessibility spec (`KO_MESSAGES`), and `status` or `reports`
// counted a dozen template specs that use the word.
it("does not count the template's own specs for common feature IDs", async () => {
const specs: Record<string, string> = {};
for (const entry of await readdir("tests", { recursive: true })) {
const file = path.join("tests", entry).split(path.sep).join("/");
if (/\.spec\.[cm]?[jt]sx?$/u.test(file)) specs[file] = await readFile(file, "utf8");
}
expect(Object.keys(specs)).toContain("tests/e2e/accessibility.spec.ts");
// A copy of the specs alone: a one-word ID that the template's source uses
// as a word (`status`) is still found in `src` text (feature-residue.ts).
const root = await fixture(specs);
for (const id of ["product-feature", "messages", "status", "reports"]) {
expect(await productFeatureResidue(root, id, id), id).toEqual([]);
}
});
it("finds the name in a spec as written, and the folder name by the same rule", async () => {
const root = await fixture({
"tests/e2e/checkout.spec.ts": `test.describe("${ID}", () => {});\n`,
"tests/e2e/documents.spec.ts": 'const FOLDER = "invoice-docs";\n',
"tests/e2e/title-case.spec.ts": 'test.describe("Invoice-Feature", () => {});\n',
"tests/e2e/longer-key.spec.ts": `const KEY = "${ID}_v2";\n`,
});
expect(await productFeatureResidue(root, ID, "invoice-docs")).toEqual([
"tests/e2e/checkout.spec.ts",
"tests/e2e/documents.spec.ts",
]);
});
it("matches the folder name as well as the ID", async () => {
const root = await fixture({
"src/contracts/leftover.ts": 'export const DIRECTORY = "invoices";\n',
"tests/unit/x.test.ts": `${importOf("../../src/features/invoices/index.ts")};\n`,
});
expect(await productFeatureResidue(root, ID, "invoices")).toEqual([
"src/contracts/leftover.ts",
"tests/unit/x.test.ts",
]);
});
// 1112 round9 (1105/16-04 remainder). `src` was searched by case-insensitive
// substring over path and text, so a legitimate ID such as
// `product-feature` counted ten template files (`product-features.ts`,
// `product-features-port.ts` and their importers) as residue.
it("does not count the template's own source and tests for an ID that longer names contain", async () => {
expect(await productFeatureResidue(process.cwd(), "product-feature", "product-feature")).toEqual([]);
});
it("counts a source path or text that names the ID as a whole name, and ignores the generated index", async () => {
const root = await fixture({
"src/contracts/leftover.ts": 'export const OWNER = "product-feature";\n',
"src/features/product-feature/stale.ts": "export const STALE = 1;\n",
"src/contracts/product-feature-port.ts": "export const PORT = 1;\n",
"src/contracts/product-features.ts": 'export const KEY = "data-product-feature";\n',
"src/contracts/title-case.ts": 'export const TITLE = "Product-Feature";\n',
"src/.generated/product-feature-index.ts": 'export const IDS = ["product-feature"];\n',
});
// A longer file name that starts with the ID is not the feature's file;
// the template's own tests/component/product-feature-switch.test.tsx is one.
expect(await productFeatureResidue(root, "product-feature", "product-feature")).toEqual([
"src/contracts/leftover.ts",
"src/features/product-feature/stale.ts",
]);
});
});