diff --git a/tests/unit/service-worker-runtime.test.ts b/tests/unit/service-worker-runtime.test.ts index 64cb3fd5..bd57ca01 100644 --- a/tests/unit/service-worker-runtime.test.ts +++ b/tests/unit/service-worker-runtime.test.ts @@ -768,6 +768,45 @@ describe("service worker worker-side protocol", () => { expect(fixture.scope.skipWaiting).toHaveBeenCalledTimes(1); }); + // 1101/06-04 (M3, M9). An activation nonce is single-use, and only a client + // the worker asked to drain can acknowledge it. + it("refuses a reused nonce and a drain reply from a client it did not ask", async () => { + const fixture = workerScope(); + const runtime = createServiceWorkerRuntime(fixture.scope as never, { + identity, + handlers: [], + manifest: null, + runtimeConfigUrl: "/runtime-config.json", + releaseManifestUrl: "/release-manifest.json", + }); + const message = (kind: "ACTIVATE_REQUEST" | "CLIENT_DRAINED") => + createServiceWorkerMessage({ + kind, + sourceBuildId: "page-build", + targetBuildId: "worker-build", + nonce: "activation-1", + }); + + const activation = runtime.onActivateRequest(message("ACTIVATE_REQUEST")); + let settled = false; + void activation.then(() => { + settled = true; + }); + await Promise.resolve(); + runtime.onClientMessage(message("CLIENT_DRAINED"), "client-a"); + runtime.onClientMessage(message("CLIENT_DRAINED"), "client-unasked"); + for (let turn = 0; turn < 10; turn += 1) await Promise.resolve(); + expect(settled).toBe(false); + expect(fixture.scope.skipWaiting).not.toHaveBeenCalled(); + + runtime.onClientMessage(message("CLIENT_DRAINED"), "client-b"); + await expect(activation).resolves.toBe("ACCEPTED"); + await expect( + runtime.onActivateRequest(message("ACTIVATE_REQUEST")), + ).resolves.toBe("REJECTED"); + expect(fixture.scope.skipWaiting).toHaveBeenCalledTimes(1); + }); + it("enumerates uncontrolled window clients before an activation drain", async () => { const fixture = workerScope(); const runtime = createServiceWorkerRuntime(fixture.scope as never, { @@ -1039,6 +1078,41 @@ describe("service worker static asset install", () => { return { result, put }; } + // 1101/06-04 (M1). The precache holds only public assets: no cookie or + // credential goes with the request, a redirect is refused rather than + // followed to another resource, and the HTTP cache is bypassed. + it("fetches each asset without credentials, redirects or the HTTP cache", async () => { + const fetcher = vi.fn( + async (_url: RequestInfo | URL, _init?: RequestInit) => + new Response(new Uint8Array([1]), { + status: 200, + headers: { "content-type": "text/javascript" }, + }), + ); + await expect( + installStaticAssets( + { + ...manifest, + assets: [{ ...manifest.assets[0]!, contentType: "text/javascript" }], + }, + { + caches: { + open: vi.fn(async () => ({ put: vi.fn(async () => {}) }) as unknown as Cache), + delete: vi.fn(async () => true), + }, + fetcher, + digest: vi.fn(async () => manifest.assets[0]!.sha256), + }, + ), + ).resolves.toMatchObject({ kind: "INSTALLED" }); + expect(fetcher).toHaveBeenCalledOnce(); + expect(fetcher.mock.calls[0]![1]).toMatchObject({ + cache: "no-store", + credentials: "omit", + redirect: "error", + }); + }); + it("installs JavaScript served under a JavaScript MIME synonym (R-6.5)", async () => { const { result, put } = installOne( new Response(new Uint8Array([1]), { @@ -1339,6 +1413,31 @@ describe("service worker static asset install", () => { await controller.stop(); }); + // 1101/06-04 (N8). A first install has no active worker to replace. It + // activates on its own, so it is never announced as an update waiting. + it("does not announce a first install as an update waiting", async () => { + const browser = pageContainer({ controlled: true }); + const controller = pageController(browser.container); + await expect(controller.start()).resolves.toMatchObject({ kind: "ACTIVE" }); + const updateWaiting = vi.fn(); + controller.onUpdateWaiting?.(updateWaiting); + const registration = browser.registration as unknown as EventTarget & { + installing: ServiceWorker | null; + waiting: ServiceWorker | null; + active: ServiceWorker | null; + }; + registration.active = null; + const first = fakeWorker([], "installing"); + registration.installing = first as unknown as ServiceWorker; + registration.dispatchEvent(new Event("updatefound")); + registration.installing = null; + registration.waiting = first as unknown as ServiceWorker; + setWorkerState(first, "installed"); + + expect(updateWaiting).not.toHaveBeenCalled(); + await controller.stop(); + }); + it("accepts a post-commit reply after the waiting worker was promoted (R-6.1)", async () => { const browser = pageContainer({ waiting: true }); const controller = pageController(browser.container);