diff --git a/tests/e2e/support/service-worker-builds.ts b/tests/e2e/support/service-worker-builds.ts index 9481cbb9..406f4bdf 100644 --- a/tests/e2e/support/service-worker-builds.ts +++ b/tests/e2e/support/service-worker-builds.ts @@ -58,6 +58,23 @@ export const ACTIVE_SELECTION = `const SERVICE_WORKER: InstalledServiceWorkerSel handlers: ["PWA_STATIC_ASSETS"], };`; +const SELECTION_DECLARATION = + /const SERVICE_WORKER: InstalledServiceWorkerSelection \| null = [^;]*;/gu; + +/** + * 0929/06-04. Writes `ACTIVE_SELECTION` over the copy's selection whatever its + * current value. Matching only the template's `= null;` line stopped every + * worker spec in a product that already ships the worker. + */ +export async function selectActiveServiceWorker(root: string): Promise { + const file = path.join(root, "src/features/installed-runtime-capabilities.ts"); + const source = await readFile(file, "utf8"); + if (source.match(SELECTION_DECLARATION)?.length !== 1) { + throw new Error(`Expected exactly one SERVICE_WORKER selection in ${file}.`); + } + await writeFile(file, source.replace(SELECTION_DECLARATION, ACTIVE_SELECTION)); +} + const HOST_PROBE = ` export function createServiceWorkerRuntimeHost( input: ServiceWorkerHostInput, @@ -116,33 +133,36 @@ export async function prepareServiceWorkerBuilds( }> = {}, ): Promise { const root = await mkdtemp(path.join(tmpdir(), "ca-sw-builds-")); - await cp(repoRoot, root, { - recursive: true, - filter: (source) => - !skipped.has(path.relative(repoRoot, source).split(path.sep)[0]!), - }); - await linkFixtureNodeModules(root, repoRoot); - await replaceOnce( - path.join(root, "src/features/installed-runtime-capabilities.ts"), - "const SERVICE_WORKER: InstalledServiceWorkerSelection | null = null;", - ACTIVE_SELECTION, - ); - const hostFile = path.join(root, "src/bootstrap/register-service-worker.ts"); - await replaceOnce( - hostFile, - "export function createServiceWorkerRuntimeHost(", - "function createServiceWorkerRuntimeHostUnobserved(", - ); - await writeFile(hostFile, (await readFile(hostFile, "utf8")) + HOST_PROBE); + try { + await cp(repoRoot, root, { + recursive: true, + filter: (source) => + !skipped.has(path.relative(repoRoot, source).split(path.sep)[0]!), + }); + await linkFixtureNodeModules(root, repoRoot); + await selectActiveServiceWorker(root); + const hostFile = path.join(root, "src/bootstrap/register-service-worker.ts"); + await replaceOnce( + 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") }; - 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]); + const dist = { a: path.join(root, "dist-a"), b: path.join(root, "dist-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]); + } + return { root, dist }; + } catch (error) { + // The spec never receives `root` from a failed setup, so afterAll cannot + // remove it. + await rm(root, { recursive: true, force: true }); + throw error; } - return { root, dist }; } export async function removeServiceWorkerBuilds( diff --git a/tests/unit/service-worker-build-output.test.ts b/tests/unit/service-worker-build-output.test.ts index ae24bfa0..d049053d 100644 --- a/tests/unit/service-worker-build-output.test.ts +++ b/tests/unit/service-worker-build-output.test.ts @@ -1,16 +1,19 @@ -import { access, readFile } from "node:fs/promises"; +import { access, 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 { generateRuntimeConfig } from "../../scripts/generate-runtime-config.ts"; import { - editOnce, type FixtureBuilds, prepareFixtureBuilds, removeFixtureBuilds, } from "../e2e/support/fixture-builds.ts"; -import { ACTIVE_SELECTION } from "../e2e/support/service-worker-builds.ts"; +import { + ACTIVE_SELECTION, + selectActiveServiceWorker, +} from "../e2e/support/service-worker-builds.ts"; /** * 0929/06-01. Step 6 of `scripts/build-frontend.ts`, the Service Worker Vite @@ -39,12 +42,7 @@ beforeAll(async () => { { name: "active", env: PROFILE, - edit: (root) => - editOnce( - path.join(root, "src/features/installed-runtime-capabilities.ts"), - "const SERVICE_WORKER: InstalledServiceWorkerSelection | null = null;", - ACTIVE_SELECTION, - ), + edit: selectActiveServiceWorker, }, ]); }, 180_000); @@ -95,3 +93,49 @@ describe("Service Worker build output (0929/06-01, 0929/06-02)", () => { ).toBe("/app/config.json"); }); }); + +// 0929/06-04. The worker fixtures must also run in a product that already +// ships the worker, so the selection is replaced whatever its value. +describe("selectActiveServiceWorker (0929/06-04)", () => { + it.each([ + ["null", "null"], + [ + "ACTIVE", + `{ + mode: "ACTIVE", + scriptPath: "service-worker.js", + handlers: ["PWA_STATIC_ASSETS", "OFFLINE_SYNC_WAKEUP"], +}`, + ], + [ + "REMOVE_REGISTRATION", + `{ + mode: "REMOVE_REGISTRATION", + scriptPath: "service-worker.js", +}`, + ], + ])("turns a %s selection into the active one", async (_label, value) => { + const root = await mkdtemp(path.join(tmpdir(), "sw-select-")); + try { + const file = path.join( + root, + "src/features/installed-runtime-capabilities.ts", + ); + await mkdir(path.dirname(file), { recursive: true }); + const around = (selection: string) => + `const WEB_WORKERS = Object.freeze([]);\n\n${selection}\n\nconst OFFLINE_COMMANDS: X | null = null;\n`; + await writeFile( + file, + around( + `const SERVICE_WORKER: InstalledServiceWorkerSelection | null = ${value};`, + ), + ); + + await selectActiveServiceWorker(root); + + expect(await readFile(file, "utf8")).toBe(around(ACTIVE_SELECTION)); + } finally { + await rm(root, { recursive: true, force: true }); + } + }); +});