import { access, readFile } from "node:fs/promises"; import path from "node:path"; import { afterAll, 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"; /** * 0929/06-01. Step 6 of `scripts/build-frontend.ts`, the Service Worker Vite * build, must not change the app output. Vite copies `public/` into `outDir` * unless the config turns it off, so the worker build replaced the profile * `dist/config.json` from step 4 with `public/config.json` (local, demo auth, * loopback API, root release manifest). The build and `verify-release` passed; * only release admission refused it. * * The defect is in how two Vite runs share `dist`, not in one function, so this * runs the real build over a temp copy with the worker on (one build, ~2s). * A check of `publicDir === false` on the config object would only restate the * fix, and the config cannot load without the inputs step 5 generates. */ const PROFILE = Object.freeze({ APP_PROFILE: "production", VITE_ROUTER_BASE_PATH: "/app/", }); // prepareFixtureBuilds sets VITE_BUILD_ID and RELEASE_ID to `-`. const BUILD_ID = "sw-profile-active"; let builds: FixtureBuilds<"active"> | undefined; afterAll(async () => { await removeFixtureBuilds(builds); }); describe("Service Worker build output (0929/06-01)", () => { it("keeps the profile runtime config step 4 wrote", async () => { builds = await prepareFixtureBuilds("sw-profile", [ { name: "active", env: PROFILE, edit: (root) => editOnce( path.join(root, "src/features/installed-runtime-capabilities.ts"), "const SERVICE_WORKER: InstalledServiceWorkerSelection | null = null;", ACTIVE_SELECTION, ), }, ]); const dist = builds.dist.active; // The worker build really ran; otherwise nothing below tests step 6. await access(path.join(dist, "service-worker.js")); const shipped: unknown = JSON.parse( await readFile(path.join(dist, "config.json"), "utf8"), ); expect(shipped).toEqual( await generateRuntimeConfig("production", { ...process.env, ...PROFILE, VITE_BUILD_ID: BUILD_ID, RELEASE_ID: BUILD_ID, }), ); }, 180_000); });