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>): Promise { 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 = {}; 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: `src` is searched by substring (1105/16-04). 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", ]); }); });