import { spawnSync, type SpawnSyncReturns } from "node:child_process"; import { access, cp, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { afterAll, beforeAll, describe, expect, it } from "vitest"; import { localBuildEnvironment } from "../../scripts/lib/build-environment.ts"; import { verifyBuildManifestOutputs } from "../../scripts/lib/build-manifest-outputs.ts"; import { linkFixtureNodeModules } from "../../scripts/lib/fixture-node-modules.ts"; import { selectRepositoryPaths } from "../../scripts/lib/repository-layout.ts"; import { buildManifestArtifactSchema } from "../../src/contracts/release-artifacts.ts"; import { turnOnServiceWorker } from "../e2e/support/service-worker-builds.ts"; import { childProcessTimeout } from "../helpers/child-process-timeout.ts"; /** * 1105/12-04. Every build wrote `dist`, `.generated/frontend-runtime` and its * `artifacts/` evidence at fixed paths, so a second build in one checkout * emptied `dist` while the first run's preview still served it. A build given * RUN_OUTPUT_ROOT (what a Playwright config sets under TEST_RUN_ID) writes all * of them under that folder and leaves the checkout's own alone. * * One real build of a temp copy with the worker on, so every step that reads * or writes those paths runs: the app build, the profile config, the worker * asset list, the worker build and the manifests. */ const ROOT = ".tmp/runs/build-probe"; const repoRoot = process.cwd(); const skipped = new Set(selectRepositoryPaths("checkout-copy-skip")); let copy: string | undefined; let failed: SpawnSyncReturns | undefined; beforeAll(async () => { copy = await mkdtemp(path.join(tmpdir(), "run-output-root-")); await cp(repoRoot, copy, { recursive: true, filter: (source) => !skipped.has(path.relative(repoRoot, source).split(path.sep)[0]!), }); await linkFixtureNodeModules(copy, repoRoot); await turnOnServiceWorker(copy); // What another run in the same checkout is serving and reading. await mkdir(path.join(copy, "dist"), { recursive: true }); await writeFile(path.join(copy, "dist/served-by-another-run.txt"), "keep\n"); await mkdir(path.join(copy, ".generated/frontend-runtime"), { recursive: true }); await writeFile(path.join(copy, ".generated/frontend-runtime/keep.txt"), "keep\n"); const env = { ...localBuildEnvironment(), RUN_OUTPUT_ROOT: ROOT, VITE_BUILD_ID: "run-output-probe", RELEASE_ID: "run-output-probe", }; for (const script of ["scripts/generate-product-feature-index.ts", "scripts/build-frontend.ts"]) { const run = spawnSync(process.execPath, [script], { cwd: copy, encoding: "utf8", env }); if (run.status !== 0) { failed = run; break; } } }, childProcessTimeout(17_748)); afterAll(async () => { if (copy) await rm(copy, { recursive: true, force: true }); }); describe("a build with RUN_OUTPUT_ROOT (1105/12-04)", () => { it("builds", () => { expect(failed, `${failed?.stdout ?? ""}\n${failed?.stderr ?? ""}`).toBeUndefined(); }); it("leaves the checkout's dist, generated runtime and evidence alone", async () => { expect(await readFile(path.join(copy!, "dist/served-by-another-run.txt"), "utf8")).toBe("keep\n"); expect( await readFile(path.join(copy!, ".generated/frontend-runtime/keep.txt"), "utf8"), ).toBe("keep\n"); await expect(access(path.join(copy!, "dist/index.html"))).rejects.toThrow(); await expect(access(path.join(copy!, "artifacts/release/build-manifest.json"))).rejects.toThrow(); }); it("writes dist, the generated runtime and the evidence under the root", async () => { const root = path.join(copy!, ROOT); for (const file of [ "dist/index.html", "dist/config.json", "dist/release-manifest.json", "dist/runtime-config.schema.json", "dist/service-worker.js", "dist/.vite/manifest.json", ".generated/frontend-runtime/contract-set.ts", ".generated/frontend-runtime/service-worker-assets.ts", "artifacts/release/build-manifest.json", "artifacts/quality/vite-module-inventory.json", ]) { await access(path.join(root, file)); } // Its paths are relative to the folder the build wrote. const manifest = buildManifestArtifactSchema.parse( JSON.parse(await readFile(path.join(root, "artifacts/release/build-manifest.json"), "utf8")), ); expect(await verifyBuildManifestOutputs(manifest, { repositoryRoot: root })).toEqual([]); }); it("points the generated modules' imports at the checkout's src", async () => { for (const file of ["contract-set.ts", "service-worker-assets.ts"]) { const generated = path.join(copy!, ROOT, ".generated/frontend-runtime", file); const specifier = /^import type .+ from "([^"]+)";$/mu.exec( await readFile(generated, "utf8"), )?.[1]; expect(specifier, file).toMatch(/^(?:\.\.\/)+src\/contracts\//u); await access(path.resolve(path.dirname(generated), specifier!)); } }); });