// @vitest-environment jsdom import { describe, expect, it, vi } from "vitest"; import { createRuntimeComposition } from "../../src/bootstrap/create-runtime-composition.ts"; import type { ProductComposition } from "../../src/bootstrap/product-composition-contract.ts"; /** * 1103/09-05. A product that turns the worker on wires its activation blockers * (a tab with unsaved edits) and the page controller's observer in its own * `product-composition.ts`, not in the template's composition file. This runs * the real composition with an ACTIVE selection, a product composition that * returns `serviceWorker`, and a registration whose update is waiting. */ const product = vi.hoisted(() => ({ observed: [] as Array>, dirty: true, })); vi.mock("../../src/features/installed-runtime-capabilities.ts", async (importOriginal) => { const original = await importOriginal(); return { INSTALLED_RUNTIME_CAPABILITIES: Object.freeze({ ...original.INSTALLED_RUNTIME_CAPABILITIES, serviceWorker: Object.freeze({ mode: "ACTIVE", scriptPath: "service-worker.js", handlers: Object.freeze(["PWA_STATIC_ASSETS"]), }), }), }; }); /** The product's `src/bootstrap/product-composition.ts`. */ vi.mock("../../src/bootstrap/product-composition.ts", () => ({ composeProductSessionOwner: () => null, composeProduct: (): ProductComposition => ({ capabilities: {}, credentialProviders: {}, serviceWorker: { blockers: [() => product.dirty], observe(observation) { product.observed.push(observation); // The composition drops a throwing product sink. throw new Error("product sink failure"); }, }, }), })); const origin = "http://localhost"; const runtimeConfig = { APP_ENV: "local", API_BASE_URL: "http://localhost:8080", REQUEST_TIMEOUT_MS: 10_000, MAX_RETRY_ATTEMPTS: 2, TELEMETRY_ENABLED: false, AUTH_MODE: "demo", CONFIG_SCHEMA_VERSION: "1", API_CONTRACT_VERSION: "1", RELEASE_MANIFEST_URL: "/release-manifest.json", BUILD_ID: "local-build", RELEASE_ID: "local-release", }; const releaseManifest = { schemaVersion: 1, appVersion: "0.2.0", buildId: "local-build", commitSha: "local", configSchemaVersion: "1", apiContractVersion: "1", assetManifestHash: "test-hash", releaseId: "local-release", builtAt: "2026-07-26T00:00:00.000Z", routeChunks: { "route-home": "assets/home.js", "route-examples-ui": "assets/ui.js", "route-examples-states": "assets/states.js", "route-examples-auth": "assets/auth.js", "route-not-found": "assets/not-found.js", }, }; async function fetcher(input: RequestInfo | URL): Promise { const url = typeof input === "string" ? input : input instanceof URL ? input.href : input.url; return Response.json( url.includes("release-manifest") ? releaseManifest : runtimeConfig, ); } /** This page's worker is active and the next build's is waiting. */ function updateWaitingContainer(): ServiceWorkerContainer { const worker = (scriptURL: string, state: string) => ({ scriptURL, state, postMessage: vi.fn(), addEventListener() {}, removeEventListener() {}, }); const register = vi.fn( async (scriptURL: string | URL, options?: RegistrationOptions) => { const script = new URL(scriptURL, origin).href; return { scope: new URL(options?.scope ?? "/", origin).href, installing: null, waiting: worker(script, "installed"), active: worker(script, "activated"), addEventListener() {}, removeEventListener() {}, update: vi.fn(async () => {}), } as unknown as ServiceWorkerRegistration; }, ); return { controller: null, register, getRegistration: vi.fn(async () => undefined), getRegistrations: vi.fn(async () => []), addEventListener() {}, removeEventListener() {}, } as unknown as ServiceWorkerContainer; } describe("1103/09-05 the product wires the worker in product-composition.ts", () => { it("passes the product's blockers and observer to the page controller", async () => { const container = updateWaitingContainer(); const composition = await createRuntimeComposition({ fetcher, host: { location: { origin }, navigator: { serviceWorker: container }, }, }); try { await composition.optional.startAfterMount(); expect(container.register).toHaveBeenCalledTimes(1); expect(product.observed).toEqual([ { event: "register", outcome: "UPDATE_WAITING" }, ]); await expect( composition.optional.serviceWorker!.requestActivation(), ).resolves.toEqual({ kind: "BLOCKED_DIRTY_CLIENT" }); expect(product.observed.at(-1)).toEqual({ event: "activation", outcome: "BLOCKED_DIRTY_CLIENT", }); } finally { await composition.dispose(); } }); });