diff --git a/scripts/lib/feature-residue.ts b/scripts/lib/feature-residue.ts index 615dc42b..79f595de 100644 --- a/scripts/lib/feature-residue.ts +++ b/scripts/lib/feature-residue.ts @@ -33,14 +33,19 @@ import path from "node:path"; * 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. + * `product-feature`, `KO_MESSAGES` for `messages`) does not count. * 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. + * a test title), and no text rule tells that from a reference. + * - 0925 FOLLOW-UP 09-07. A spec that opens the feature by its URL alone + * (`page.goto("/invoices")`) failed only in FE-GATE-008, after the drill + * passed. `routePaths` are the removed feature's route paths: a spec with a + * string that starts with one, up to its first parameter (`/invoices` of + * `/invoices/:id`) and followed by the string's end or `/`, `?`, `#`, is + * residue. A path that is `/` up to its first parameter is not searched. + * What is still not found: a spec that names only a one-word ID, or builds + * the URL from parts. 03's hand list removes the feature's specs. * * The drill applies the same rule to a sample a project lists in * scripts/lib/removable-template-features.ts. @@ -60,10 +65,21 @@ async function filesUnder(directory: string): Promise { return nested.flat(); } +/** The part of a route path before its first parameter or wildcard, or null for `/`. */ +function staticRoutePrefix(routePath: string): string | null { + const segments: string[] = []; + for (const segment of routePath.split("/").filter(Boolean)) { + if (segment.startsWith(":") || segment.includes("*")) break; + segments.push(segment); + } + return segments.length === 0 ? null : `/${segments.join("/")}`; +} + export async function productFeatureResidue( root: string, featureId: string, directory: string, + routePaths: readonly string[] = [], ): Promise { const nameSet = new Set([featureId, directory]); const names = [...nameSet].map(escapeRegExp).join("|"); @@ -83,6 +99,13 @@ export async function productFeatureResidue( specNames.length === 0 ? null : new RegExp(`(? prefix !== null)), + ].map(escapeRegExp); + const routeString = + prefixes.length === 0 + ? null + : new RegExp(`["'\`](?:${prefixes.join("|")})(?=["'\`/?#])`, "u"); const playwrightSpec = /\.spec\.[cm]?[jt]sx?$/u; const residue: string[] = []; const relative = (file: string) => path.relative(root, file).split(path.sep).join("/"); @@ -97,7 +120,9 @@ export async function productFeatureResidue( if ( pathNames(name) || imported.test(text) || - (wholeName !== null && playwrightSpec.test(name) && wholeName.test(text)) + (playwrightSpec.test(name) && + ((wholeName !== null && wholeName.test(text)) || + (routeString !== null && routeString.test(text)))) ) { residue.push(name); } diff --git a/scripts/test-sample-removal.ts b/scripts/test-sample-removal.ts index 7437e854..389aefad 100644 --- a/scripts/test-sample-removal.ts +++ b/scripts/test-sample-removal.ts @@ -10,7 +10,8 @@ import { spawnSync } from "node:child_process"; import path from "node:path"; import { fileURLToPath } from "node:url"; -import { ROUTE_FEATURE_OWNER } from "../src/features/installed-feature-contracts.ts"; +import { PLATFORM_ROUTE_REGISTRY } from "../src/contracts/routes.ts"; +import { ROUTE_FEATURE_OWNER, routePath } from "../src/features/installed-feature-contracts.ts"; import { loadCiGateContract } from "./contracts/ci-gates.ts"; import { createFileSystemDeclarationSource } from "./generate-product-feature-index.ts"; import { productFeatureResidue } from "./lib/feature-residue.ts"; @@ -163,6 +164,15 @@ const located = await locateFeature(featureId); const featureRouteIds = Object.entries(ROUTE_FEATURE_OWNER) .filter(([, owner]) => owner === featureId) .map(([routeId]) => routeId); +// 0925 FOLLOW-UP 09-07. A spec that opens the feature by one of these paths is +// residue (scripts/lib/feature-residue.ts). A platform path the feature took +// over (its home) goes back to the platform with the removal. +const platformRoutePaths = new Set( + Object.values(PLATFORM_ROUTE_REGISTRY).map((route) => route.path), +); +const featureRoutePaths = featureRouteIds + .map(routePath) + .filter((candidate) => !platformRoutePaths.has(candidate)); const fixtureRoot = await createRemovalFixtureRoot(`${featureId}-removal`); const pnpmCli = requireRemovalFixtureEnvironment("npm_execpath"); @@ -454,7 +464,14 @@ try { // 1105/16-04. Template tests use example feature IDs as data; only what // still points at the removed feature counts (scripts/lib/feature-residue.ts). - const residue = await productFeatureResidue(fixtureRoot, featureId, located.directory); + // 0925 FOLLOW-UP 09-07. The drill runs no Playwright spec; one that still + // opens the feature's URL is found here instead of in FE-GATE-008. + const residue = await productFeatureResidue( + fixtureRoot, + featureId, + located.directory, + featureRoutePaths, + ); const builtResiduePattern = new RegExp( `${escapeRegExp(featureId)}|${escapeRegExp(located.directory)}`, "i", diff --git a/tests/unit/feature-residue.test.ts b/tests/unit/feature-residue.test.ts index ec2c2511..9adfd627 100644 --- a/tests/unit/feature-residue.test.ts +++ b/tests/unit/feature-residue.test.ts @@ -85,6 +85,47 @@ describe("product feature residue (1105/16-04)", () => { ]); }); + // 0925 FOLLOW-UP 09-07. No step of the drill runs a Playwright spec, so a + // spec that opens the removed feature by its URL alone passed the drill and + // failed FE-GATE-008 afterwards. + it("counts a Playwright spec that opens one of the feature's route paths", async () => { + const root = await fixture({ + "tests/e2e/route-only.spec.ts": 'test("opens", ({ page }) => page.goto("/invoice"));\n', + "tests/e2e/detail.spec.ts": "await page.goto(`/invoice/${id}?tab=lines`);\n", + "tests/visual/hash.visual.spec.ts": "await page.goto('/invoice#total');\n", + "tests/e2e/longer-path.spec.ts": 'await page.goto("/invoices-archive");\n', + "tests/e2e/other-base.spec.ts": 'await page.goto("/admin/invoice");\n', + // The same text in a vitest test is data (1105/16-04). + "tests/unit/route.test.ts": 'const target = "/invoice";\n', + }); + + expect(await productFeatureResidue(root, ID, ID, ["/invoice", "/invoice/:invoiceId"])).toEqual([ + "tests/e2e/detail.spec.ts", + "tests/e2e/route-only.spec.ts", + "tests/visual/hash.visual.spec.ts", + ]); + }); + + it("does not search a route path that is the root before its first parameter", async () => { + const root = await fixture({ + "tests/e2e/home.spec.ts": 'await page.goto("/");\nawait page.goto("/?lang=ko");\n', + }); + + expect(await productFeatureResidue(root, ID, ID, ["/", "/:invoiceId", "*"])).toEqual([]); + }); + + it("does not count the template's own specs for the route paths of a generated feature", 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"); + } + const root = await fixture(specs); + + // create:feature product-feature declares PRODUCT_LIST at /product. + expect(await productFeatureResidue(root, "product-feature", "product-feature", ["/product"])).toEqual([]); + }); + it("does not count a spec whose longer names only contain the ID", async () => { const root = await fixture({ "tests/e2e/builds.spec.ts": [