import { readdir, readFile } from "node:fs/promises"; import path from "node:path"; /** * 1105/16-04. What a product feature leaves behind after * `test:sample-removal -- --feature=` deleted its folders, in the copy the * drill made. * * - `src/`: any file whose path or text still names the feature ID or its * folder. Platform source never names a product feature, so a name left * there is a reference the type checker cannot see (a catalog key, a * runtime override, a link). * - `tests/`: a file named after the feature, or one that imports from the * feature's `src/features//` or `tests/features//`. A vitest * test's text is not searched for the bare name: the template's own tests * use example IDs as data (`tests/unit/create-feature.test.ts` writes * `invoice-feature`, the manual's example, 51 times), and the drill flagged * them as residue of a product feature that took that name. A vitest test * that still depends on the removed feature fails `test:all`, which the * drill runs. * - 1106/16-01. `test:all` runs vitest only (the `*.test.ts(x)` files under * `tests/`, vitest.config.ts), and no step of the drill runs a Playwright * spec. So a spec (`*.spec.ts`) that names the feature only in its text, * such as `test.describe("invoice-feature", ...)`, is residue: its text is * searched for the ID or folder name as written, case-sensitive, with no * identifier character (`[A-Za-z0-9_-]`) on either side. A longer name * that contains it (`generate-product-feature-index.ts` for * `product-feature`, `KO_MESSAGES` for `messages`) does not count. A spec * that names only the feature's routes or URLs is not found; 03's hand list * removes the feature's specs. * Diff review DR-02: only a name with a hyphen is searched. A one-word ID * or folder (`status`, `reports`, `messages`) is an ordinary word in the * template's own specs (`status: 200`, `getByRole("status")`, "reports" in * a test title), and no text rule tells that from a reference, so a spec * that names only a one-word ID is not found either. * * The reference sample keeps its own, wider pattern in * scripts/test-sample-removal.ts: the template owns all of its files. */ function escapeRegExp(value: string): string { return value.replace(/[.*+?^${}()|[\]\\]/gu, "\\$&"); } async function filesUnder(directory: string): Promise { const entries = await readdir(directory, { withFileTypes: true }).catch(() => []); const nested = await Promise.all( entries.map((entry) => { const target = path.join(directory, entry.name); return entry.isDirectory() ? filesUnder(target) : Promise.resolve([target]); }), ); return nested.flat(); } export async function productFeatureResidue( root: string, featureId: string, directory: string, ): Promise { const names = [...new Set([featureId, directory])].map(escapeRegExp).join("|"); const named = new RegExp(names, "iu"); const imported = new RegExp( String.raw`(?:\bfrom\s*|\bimport\s*\(\s*|\bimport\s+|\bmock\s*\(\s*)["'][^"'\n]*/features/(?:${names})/`, "u", ); const specNames = [...new Set([featureId, directory])] .filter((name) => name.includes("-")) .map(escapeRegExp); const wholeName = specNames.length === 0 ? null : new RegExp(`(? path.relative(root, file).split(path.sep).join("/"); for (const file of await filesUnder(path.join(root, "src"))) { const name = relative(file); if (named.test(`${name}\n${await readFile(file, "utf8")}`)) residue.push(name); } for (const file of await filesUnder(path.join(root, "tests"))) { const name = relative(file); const text = await readFile(file, "utf8"); if ( named.test(name) || imported.test(text) || (wholeName !== null && playwrightSpec.test(name) && wholeName.test(text)) ) { residue.push(name); } } return residue.sort(); }