import { readFile } from "node:fs/promises"; import { afterEach, describe, expect, it, vi } from "vitest"; /** * 1103/09-05. The static optional-capability selection (ยง3.5) is the * product's choice, so it lives in a product-owned file. Turning the Service * Worker on is an edit there; `installed-runtime-capabilities.ts` stays the * template's and only validates and projects what the product selected. */ const source = (file: string) => readFile(new URL(`../../${file}`, import.meta.url), "utf8"); const ACTIVE = Object.freeze({ mode: "ACTIVE" as const, scriptPath: "service-worker.js" as const, handlers: Object.freeze(["PWA_STATIC_ASSETS" as const]), }); afterEach(() => { vi.doUnmock("../../src/features/product-capabilities.ts"); vi.resetModules(); }); async function installedWith(selection: Record) { vi.resetModules(); vi.doMock("../../src/features/product-capabilities.ts", () => ({ PRODUCT_RUNTIME_CAPABILITY_SELECTION: Object.freeze({ realtime: Object.freeze([]), webWorkers: Object.freeze([]), serviceWorker: null, offlineCommands: null, ...selection, }), })); return (await import("../../src/features/installed-runtime-capabilities.ts")) .INSTALLED_RUNTIME_CAPABILITIES; } describe("1103/09-05 the optional runtime selection is product-owned", () => { it("declares the selection in the product-owned file only", async () => { const product = await source("src/features/product-capabilities.ts"); const installed = await source("src/features/installed-runtime-capabilities.ts"); expect(product).toContain("Product-owned."); // tests/e2e/support/service-worker-builds.ts premise 1 edits this line. expect( product.match(/const SERVICE_WORKER: InstalledServiceWorkerSelection \| null = /gu), ).toHaveLength(1); expect(installed).not.toContain("Product-owned."); expect(installed).not.toMatch(/const (REALTIME|WEB_WORKERS|SERVICE_WORKER|OFFLINE_COMMANDS)\b/u); }); it("projects the product's Service Worker selection", async () => { const installed = await installedWith({ serviceWorker: ACTIVE }); expect(installed.serviceWorker).toEqual(ACTIVE); expect(Object.isFrozen(installed)).toBe(true); }); it("still validates what the product selected", async () => { await expect( installedWith({ webWorkers: Object.freeze([Object.freeze({ taskGroupId: "" })]) }), ).rejects.toThrow(TypeError); }); });