diff --git a/tests/e2e/support/fixture-builds.ts b/tests/e2e/support/fixture-builds.ts index 23a95e3f..933fa022 100644 --- a/tests/e2e/support/fixture-builds.ts +++ b/tests/e2e/support/fixture-builds.ts @@ -65,17 +65,41 @@ function run( } } -export async function prepareFixtureBuilds( - prefix: string, - builds: readonly FixtureBuild[], -): Promise> { - // An edit's source text names the build (two specs never share a prefix). - const fingerprint = JSON.stringify( - builds.map((build) => [build.name, build.env ?? {}, build.edit?.toString() ?? ""]), - ); +export type EditedCopyBuild = Readonly<{ + name: Name; + /** VITE_BUILD_ID and RELEASE_ID of this build. */ + buildId: string; + edit?: (root: string) => Promise; + env?: Readonly>; +}>; + +/** + * 0929/11-07. The one way an e2e spec gets real production builds of an + * edited copy of the checkout; prepareFixtureBuilds and + * prepareServiceWorkerBuilds (service-worker-builds.ts) are its two shapes. + * It copies the checkout (without `checkout-copy-skip`), links node_modules, + * runs `prepare` once, then for each build runs `edit`, builds, moves `dist` + * to `dist-` and asks `collect` what the spec needs from that build. + * `key` and `fingerprint` name the result in the e2e run + * (shared-fixture-builds.ts), which removes the copy when preparing fails. + */ +export async function buildEditedCopies( + input: Readonly<{ + key: string; + fingerprint: string; + prepare?: (root: string) => Promise; + builds: readonly EditedCopyBuild[]; + collect?: (name: Name, root: string) => Promise; + }>, +): Promise>; + collected: Readonly>; + shared: boolean; +}>> { const { root, value, shared } = await sharedFixtureBuild( - `fixture-${prefix}`, - fingerprint, + input.key, + input.fingerprint, async (root) => { await cp(repoRoot, root, { recursive: true, @@ -83,19 +107,36 @@ export async function prepareFixtureBuilds( !skipped.has(path.relative(repoRoot, source).split(path.sep)[0]!), }); await linkFixtureNodeModules(root, repoRoot); + await input.prepare?.(root); const dist = {} as Record; - for (const build of builds) { + const collected = {} as Record; + for (const build of input.builds) { await build.edit?.(root); - const buildId = `${prefix}-${build.name}`; - run(root, "scripts/generate-product-feature-index.ts", buildId, build.env); - run(root, "scripts/build-frontend.ts", buildId, build.env); + run(root, "scripts/generate-product-feature-index.ts", build.buildId, build.env); + run(root, "scripts/build-frontend.ts", build.buildId, build.env); dist[build.name] = path.join(root, `dist-${build.name}`); await rename(path.join(root, "dist"), dist[build.name]); + collected[build.name] = input.collect ? await input.collect(build.name, root) : (null as Collected); } - return dist; + return { dist, collected }; }, ); - return { root, dist: value, shared }; + return { root, ...value, shared }; +} + +export async function prepareFixtureBuilds( + prefix: string, + builds: readonly FixtureBuild[], +): Promise> { + // An edit's source text names the build (two specs never share a prefix). + const { root, dist, shared } = await buildEditedCopies({ + key: `fixture-${prefix}`, + fingerprint: JSON.stringify( + builds.map((build) => [build.name, build.env ?? {}, build.edit?.toString() ?? ""]), + ), + builds: builds.map((build) => ({ ...build, buildId: `${prefix}-${build.name}` })), + }); + return { root, dist, shared }; } export async function removeFixtureBuilds( diff --git a/tests/e2e/support/service-worker-builds.ts b/tests/e2e/support/service-worker-builds.ts index c96b1961..ef4bc723 100644 --- a/tests/e2e/support/service-worker-builds.ts +++ b/tests/e2e/support/service-worker-builds.ts @@ -1,12 +1,5 @@ -import { spawnSync } from "node:child_process"; import { existsSync } from "node:fs"; -import { - cp, - readFile, - rename, - rm, - writeFile, -} from "node:fs/promises"; +import { readFile, rm, writeFile } from "node:fs/promises"; import { createServer, type Server } from "node:http"; import type { AddressInfo } from "node:net"; import path from "node:path"; @@ -14,11 +7,8 @@ import { pathToFileURL } from "node:url"; import { gzipSync } from "node:zlib"; import { SERVICE_WORKER_ASSETS_OUTPUT } from "../../../scripts/generate-service-worker-assets.ts"; -import { localBuildEnvironment } from "../../../scripts/lib/build-environment.ts"; -import { linkFixtureNodeModules } from "../../../scripts/lib/fixture-node-modules.ts"; -import { selectRepositoryPaths } from "../../../scripts/lib/repository-layout.ts"; import type { StaticAssetManifestV1 } from "../../../src/contracts/service-worker.ts"; -import { sharedFixtureBuild } from "./shared-fixture-builds.ts"; +import { buildEditedCopies, editOnce } from "./fixture-builds.ts"; /** * R3-6 test helper: two real production builds with the Service Worker on, @@ -54,11 +44,6 @@ import { sharedFixtureBuild } from "./shared-fixture-builds.ts"; * text it expected. */ -const repoRoot = path.resolve(import.meta.dirname, "../../.."); -// 1105/12-04. `.tmp` holds other runs' builds and results (.tmp/runs/), -// which change while this copy is made; fixture-builds.ts skips it too. -const skipped = new Set(selectRepositoryPaths("checkout-copy-skip")); - export type BuildName = "a" | "b"; export type ServiceWorkerBuilds = Readonly<{ root: string; @@ -101,17 +86,8 @@ export function createServiceWorkerRuntimeHost( } `; -export async function replaceOnce( - file: string, - search: string, - replacement: string, -): Promise { - const source = await readFile(file, "utf8"); - if (source.split(search).length !== 2) { - throw new Error(`Expected exactly one "${search}" in ${file}.`); - } - await writeFile(file, source.replace(search, replacement)); -} +/** 0929/11-07. The same one-occurrence edit as fixture-builds.ts, not a copy of it. */ +export const replaceOnce = editOnce; /** * Premise 1. Replaces the value of the one `SERVICE_WORKER` declaration in @@ -167,25 +143,12 @@ async function precachedUrls(root: string, name: BuildName): Promise { return SERVICE_WORKER_ASSETS.assets.map((asset) => asset.url).sort(); } -function run(cwd: string, script: string, buildId: string): void { - const result = spawnSync("node", [script], { - cwd, - encoding: "utf8", - // 1103/11-08. A local build, not the CI job's release build. - env: { ...localBuildEnvironment(), VITE_BUILD_ID: buildId, RELEASE_ID: buildId }, - }); - if (result.status !== 0) { - throw new Error( - `${script} failed for ${buildId}:\n${result.stdout}\n${result.stderr}`, - ); - } -} - /** * `beforeBuild` edits the temp copy right before build `name` (06-03: a * broken worker A, then a `REMOVE_REGISTRATION` release B). Edits persist * into the later build. `key` names this pair of builds in the e2e run - * (shared-fixture-builds.ts); each spec passes its own. + * (shared-fixture-builds.ts); each spec passes its own. The copy and the + * builds are fixture-builds.ts's `buildEditedCopies` (0929/11-07). */ export async function prepareServiceWorkerBuilds( options: Readonly<{ @@ -193,38 +156,27 @@ export async function prepareServiceWorkerBuilds( beforeBuild?: (name: BuildName, root: string) => Promise; }>, ): Promise { - const { root, value, shared } = await sharedFixtureBuild( - `sw-${options.key}`, - options.beforeBuild?.toString() ?? "", - async (root) => { - await cp(repoRoot, root, { - recursive: true, - filter: (source) => - !skipped.has(path.relative(repoRoot, source).split(path.sep)[0]!), - }); - await linkFixtureNodeModules(root, repoRoot); + const { root, dist, collected, shared } = await buildEditedCopies({ + key: `sw-${options.key}`, + fingerprint: options.beforeBuild?.toString() ?? "", + prepare: async (root) => { await turnOnServiceWorker(root); const hostFile = path.join(root, "src/bootstrap/register-service-worker.ts"); - await replaceOnce( + await editOnce( hostFile, "export function createServiceWorkerRuntimeHost(", "function createServiceWorkerRuntimeHostUnobserved(", ); await writeFile(hostFile, (await readFile(hostFile, "utf8")) + HOST_PROBE); - - const dist = { a: path.join(root, "dist-a"), b: path.join(root, "dist-b") }; - const precached: Record = { a: [], b: [] }; - for (const name of ["a", "b"] as const) { - await options.beforeBuild?.(name, root); - run(root, "scripts/generate-product-feature-index.ts", `sw-e2e-${name}`); - run(root, "scripts/build-frontend.ts", `sw-e2e-${name}`); - await rename(path.join(root, "dist"), dist[name]); - precached[name] = await precachedUrls(root, name); - } - return { dist, precached }; }, - ); - return { root, ...value, shared }; + builds: (["a", "b"] as const).map((name) => ({ + name, + buildId: `sw-e2e-${name}`, + edit: async (root: string) => options.beforeBuild?.(name, root), + })), + collect: (name, root) => precachedUrls(root, name), + }); + return { root, dist, precached: collected, shared }; } export async function removeServiceWorkerBuilds( diff --git a/tests/unit/e2e-build-helpers.test.ts b/tests/unit/e2e-build-helpers.test.ts new file mode 100644 index 00000000..0a57b4bf --- /dev/null +++ b/tests/unit/e2e-build-helpers.test.ts @@ -0,0 +1,36 @@ +import { readFile, readdir } from "node:fs/promises"; +import path from "node:path"; + +import { describe, expect, it } from "vitest"; + +const SUPPORT = "tests/e2e/support"; + +// 0929/11-07. The Service Worker helper kept its own copy of the checkout +// copy, the build runner and the one-occurrence edit, so a fix to one (the +// local build env of 1103/11-08, the copy skip list of 1105/12-04) had to be +// made twice. Both helpers now go through fixture-builds.ts. +describe("e2e build helpers have one copy-and-build path (0929/11-07)", () => { + it("builds Service Worker fixtures through buildEditedCopies", async () => { + const source = await readFile(path.join(SUPPORT, "service-worker-builds.ts"), "utf8"); + + expect(source).toMatch(/import \{[^}]*\bbuildEditedCopies\b[^}]*\} from "\.\/fixture-builds\.ts";/u); + expect(source).toContain("buildEditedCopies({"); + for (const copy of ["spawnSync", "sharedFixtureBuild", "linkFixtureNodeModules", "await cp(", "function run("]) { + expect(source, copy).not.toContain(copy); + } + }); + + it("keeps the build runner and the one-occurrence edit in fixture-builds.ts only", async () => { + const files = (await readdir(SUPPORT)).filter((file) => file.endsWith(".ts")); + const defining = async (pattern: RegExp) => { + const found: string[] = []; + for (const file of files) { + if (pattern.test(await readFile(path.join(SUPPORT, file), "utf8"))) found.push(file); + } + return found; + }; + + expect(await defining(/^function run\(/mu)).toEqual(["fixture-builds.ts"]); + expect(await defining(/split\(search\)\.length !== 2/u)).toEqual(["fixture-builds.ts"]); + }); +});