import { existsSync, readFileSync } from "node:fs"; import path from "node:path"; import { describe, expect, it } from "vitest"; import { REMOVABLE_TEMPLATE_FEATURES } from "../../scripts/lib/removable-template-features.ts"; /** * 1105/15-03. Two copies of the list of files `vite build` reads from the * root (the initial JavaScript measurement and the * `VITE_PRODUCT_FEATURES=none` build test) had to change together each time * vite.config.ts imported a new local file (7f9a515, 799ab7f), and a missed * copy failed with an unexplained "Could not resolve". There is one list * now, and it has to cover what vite.config.ts imports. */ const LIST_MODULE = "scripts/lib/vite-build-inputs.ts"; const MEASUREMENT = "scripts/scale/measure-feature-shape-growth.ts"; // 1106/10-01. The measurement copies the reference sample and is one of its // files (scripts/lib/removable-template-features.ts), so test:sample-removal // deletes it with the sample. It is left out only where its owner is not // installed: the owner's feature-module.ts is gone, the rule // verify:documentation uses. The build test is a template file and is always // read. Filtering both on "the file exists" let a renamed consumer drop out of // the check unnoticed. The owner is found by its files, not written here: the // sample drill fails on any test that still names the sample. const MEASUREMENT_OWNER = REMOVABLE_TEMPLATE_FEATURES.find((feature) => feature.extraPaths.includes(MEASUREMENT), ); const OWNER_INSTALLED = MEASUREMENT_OWNER !== undefined && existsSync(`src/features/${MEASUREMENT_OWNER.featureId}/feature-module.ts`); const CONSUMERS = [ "tests/unit/product-feature-selection-build.test.ts", ...(OWNER_INSTALLED ? [MEASUREMENT] : []), ]; const LOCAL_IMPORT = /\bfrom\s+["'](\.{1,2}\/[^"']+)["']|\bimport\s+["'](\.{1,2}\/[^"']+)["']/gu; /** Every repository file vite.config.ts reaches through relative imports. */ function localImportGraph(entry: string): string[] { const seen = new Set(); const pending = [entry]; while (pending.length > 0) { const file = pending.pop()!; if (seen.has(file)) continue; seen.add(file); const source = readFileSync(file, "utf8"); for (const match of source.matchAll(LOCAL_IMPORT)) { const specifier = match[1] ?? match[2]!; const resolved = path .relative(process.cwd(), path.resolve(path.dirname(file), specifier)) .split(path.sep) .join("/"); if (existsSync(resolved)) pending.push(resolved); } } return [...seen]; } describe("vite build inputs (1105/15-03)", () => { it("defines the list in one module that both builds import", () => { // The measurement may be missing only because a removable feature owns it. expect(MEASUREMENT_OWNER, `no removable template feature owns ${MEASUREMENT}`).toBeDefined(); for (const consumer of CONSUMERS) { const source = readFileSync(consumer, "utf8"); expect(source, consumer).not.toMatch(/\bconst BUILD_INPUTS\s*=/u); expect(source, consumer).toMatch(/\bVITE_BUILD_INPUTS\b/u); expect(source, consumer).toContain("vite-build-inputs.ts"); } }); it("covers every local file vite.config.ts imports", async () => { // Imported here, not at the top, so the first test still reports the // copies when the module is missing. const { VITE_BUILD_INPUTS } = await import("../../scripts/lib/vite-build-inputs.ts"); const covered = (file: string) => VITE_BUILD_INPUTS.some((input) => file === input || file.startsWith(`${input}/`)); const missing = localImportGraph("vite.config.ts").filter((file) => !covered(file)); expect( missing, `vite.config.ts reads files ${LIST_MODULE} does not copy; add them there`, ).toEqual([]); }); });