import { spawnSync } from "node:child_process"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { afterEach, describe, expect, it } from "vitest"; import { childProcessTimeout } from "../helpers/child-process-timeout.ts"; /** * 1103/15-03. An over-budget build printed only * "Bundle budget or manifest integrity failed: " with nothing after the * colon, so the first budget failure a project meets named neither the limit * nor the excess. check-bundle runs here on a hand-made build output. */ const roots: string[] = []; afterEach(async () => { await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true }))); }); type Output = Readonly<{ path: string; gzipBytes: number }>; async function checkBundle( options: Readonly<{ outputs: readonly Output[]; manifest: Record; budgets: Record; /** RUN_OUTPUT_ROOT for the run, and the build written under it. */ runOutput?: Readonly<{ root: string; outputs: readonly Output[] }>; }>, ) { const root = await mkdtemp(path.join(tmpdir(), "check-bundle-output-")); roots.push(root); const write = async (file: string, value: unknown) => { await mkdir(path.dirname(path.join(root, file)), { recursive: true }); await writeFile(path.join(root, file), `${JSON.stringify(value, null, 2)}\n`); }; const writeBuild = async (buildRoot: string, outputs: readonly Output[]) => { await write(path.posix.join(buildRoot, "dist/.vite/manifest.json"), options.manifest); await write(path.posix.join(buildRoot, "artifacts/performance/bundle.json"), { schemaVersion: 1, generatedAt: "2026-11-04T00:00:00.000Z", context: { nodeVersion: "v24.14.0", packageManager: "pnpm@11.17.0", runnerImage: "fixture" }, outputs: outputs.map((output) => ({ path: `dist/${output.path}`, bytes: output.gzipBytes * 3, gzipBytes: output.gzipBytes, sha256: "a".repeat(64), })), }); }; await write("config/performance/budgets.json", { schemaVersion: 1, bundle: options.budgets }); await writeBuild(".", options.outputs); if (options.runOutput) await writeBuild(options.runOutput.root, options.runOutput.outputs); const result = spawnSync(process.execPath, [path.resolve("scripts/check-bundle.ts")], { cwd: root, encoding: "utf8", // Empty is unset (scripts/lib/run-isolation.ts), whatever the runner has. env: { ...process.env, RUN_OUTPUT_ROOT: options.runOutput?.root ?? "" }, }); return { status: result.status, stdout: result.stdout, stderr: result.stderr }; } const MANIFEST = { "index.html": { file: "assets/index-A.js", isEntry: true, imports: ["_boot"], }, // The entry's own dynamic imports are initial (09-03); a route's are lazy. _boot: { file: "assets/boot-B.js", dynamicImports: ["src/lazy.tsx"] }, "src/lazy.tsx": { file: "assets/lazy-C.js" }, }; const OUTPUTS = [ { path: "assets/index-A.js", gzipBytes: 400 }, { path: "assets/boot-B.js", gzipBytes: 500 }, { path: "assets/lazy-C.js", gzipBytes: 300 }, ]; const BUDGETS = { initialJsGzipBytes: 1_000, initialJsFiles: 2, lazyChunkGzipBytes: 400 }; describe("check:bundle failure output (1103/15-03)", { timeout: childProcessTimeout(730) }, () => { it("passes and prints the total within budget", async () => { const result = await checkBundle({ outputs: OUTPUTS, manifest: MANIFEST, budgets: BUDGETS }); expect(result.stderr).toBe(""); expect(result.status).toBe(0); expect(result.stdout).toContain("initial JS 900 / 1000 gzip bytes"); }); it("names the measured initial total, the budget and the excess", async () => { const result = await checkBundle({ outputs: OUTPUTS, manifest: MANIFEST, budgets: { ...BUDGETS, initialJsGzipBytes: 899 }, }); expect(result.status).toBe(1); expect(result.stderr).toContain("Bundle budget failed (config/performance/budgets.json):"); expect(result.stderr).toContain("initial JavaScript 900 gzip bytes > budget 899 (over by 1)"); expect(result.stderr).not.toContain("manifest integrity"); }); it("names each lazy chunk over its budget", async () => { const result = await checkBundle({ outputs: OUTPUTS, manifest: MANIFEST, budgets: { ...BUDGETS, lazyChunkGzipBytes: 250 }, }); expect(result.status).toBe(1); expect(result.stderr).toContain( "lazy chunk assets/lazy-C.js 300 gzip bytes > budget 250 (over by 50)", ); }); // 1103/15-05. The entry preloads every initial file at once; over HTTP/1.1 // they queue for six connections. A change that adds an initial file is a // decision recorded in the budget, not a side effect. it("fails when the initial JavaScript has more files than the budget", async () => { const result = await checkBundle({ outputs: OUTPUTS, manifest: MANIFEST, budgets: { ...BUDGETS, initialJsFiles: 1 }, }); expect(result.status).toBe(1); expect(result.stderr).toContain("initial JavaScript files 2 > budget 1 (over by 1)"); }); it("reports a manifest integrity failure apart from the budget", async () => { const result = await checkBundle({ outputs: OUTPUTS.filter(({ path: file }) => file !== "assets/boot-B.js"), manifest: MANIFEST, budgets: BUDGETS, }); expect(result.status).toBe(1); expect(result.stderr).toContain("Bundle manifest integrity failed:"); expect(result.stderr).toContain("output missing from the inventory: assets/boot-B.js"); expect(result.stderr).not.toContain("Bundle budget failed"); }); }); /** * 1106/12-02. Under RUN_OUTPUT_ROOT scripts/build-frontend.ts writes the build * below that folder, and check:bundle (scripts/generate-supply-chain.ts, then * scripts/check-bundle.ts) read the checkout's dist/ and artifacts/ anyway: a * build over budget under .tmp/ passed on the checkout's earlier build * (review run 12-R2). Both stop and name the variable instead. */ describe("check:bundle with RUN_OUTPUT_ROOT (1106/12-02)", { timeout: childProcessTimeout(1_041) }, () => { it("does not pass the checkout's earlier build while the build writes elsewhere", async () => { const result = await checkBundle({ outputs: OUTPUTS, manifest: MANIFEST, budgets: BUDGETS, runOutput: { root: ".tmp/runs/other", outputs: OUTPUTS.map((output) => ({ ...output, gzipBytes: output.gzipBytes * 4 })), }, }); expect(result.stdout).not.toContain("Bundle budget: PASS"); expect(result.status).toBe(1); expect(result.stderr).toContain("RUN_OUTPUT_ROOT=.tmp/runs/other"); }); it("measures the checkout's build when the output root is the checkout", async () => { const result = await checkBundle({ outputs: OUTPUTS, manifest: MANIFEST, budgets: BUDGETS, runOutput: { root: ".", outputs: OUTPUTS }, }); expect(result.stderr).toBe(""); expect(result.status).toBe(0); expect(result.stdout).toContain("initial JS 900 / 1000 gzip bytes"); }); it("stops scripts/generate-supply-chain.ts before it reads the checkout's build", async () => { const root = await mkdtemp(path.join(tmpdir(), "generate-supply-chain-output-root-")); roots.push(root); const result = spawnSync(process.execPath, [path.resolve("scripts/generate-supply-chain.ts")], { cwd: root, encoding: "utf8", env: { ...process.env, RUN_OUTPUT_ROOT: ".tmp/runs/other" }, }); expect(result.status).toBe(1); expect(result.stderr).toContain( "scripts/generate-supply-chain.ts reads the build at this checkout's paths", ); expect(result.stderr).toContain("RUN_OUTPUT_ROOT=.tmp/runs/other"); }); });