diff --git a/tests/unit/service-worker-build-output.test.ts b/tests/unit/service-worker-build-output.test.ts index 13ee660b..b3eedb97 100644 --- a/tests/unit/service-worker-build-output.test.ts +++ b/tests/unit/service-worker-build-output.test.ts @@ -1,4 +1,13 @@ -import { access, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; +import { spawnSync } from "node:child_process"; +import { + access, + mkdir, + mkdtemp, + readFile, + rm, + symlink, + writeFile, +} from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; @@ -94,6 +103,55 @@ describe("Service Worker build output (0929/06-01, 0929/06-02)", () => { }); }); +// 0929/06-05. A new worker and the self-unregistering one are the only way to +// recover a client without a page. Served from an edge that caches root `.js` +// for long, they stay out of reach for that TTL, so a build that ships a +// worker has its script's Cache-Control checked like the page's. +describe("worker script hosting headers (0929/06-05)", () => { + async function verifyHosting(workerCacheControl: string) { + const root = builds!.root; + await rm(path.join(root, "dist"), { recursive: true, force: true }); + await symlink(builds!.dist.active, path.join(root, "dist")); + const fixture = JSON.parse( + await readFile("config/hosting/response-headers.fixture.json", "utf8"), + ) as { responses: Record> }; + fixture.responses["serviceWorker"]!["cache-control"] = workerCacheControl; + await writeFile( + path.join(root, "config/hosting/response-headers.fixture.json"), + JSON.stringify(fixture), + ); + const env: NodeJS.ProcessEnv = { ...process.env }; + delete env["HOSTING_BASE_URL"]; + const result = spawnSync(process.execPath, ["scripts/verify-hosting-headers.ts"], { + cwd: root, + encoding: "utf8", + env, + }); + const record = JSON.parse( + await readFile(path.join(root, "artifacts/release/hosting-headers.json"), "utf8"), + ) as { results: { surface: string; header: string; passed: boolean }[] }; + return { + status: result.status, + stderr: result.stderr, + failed: record.results + .filter((entry) => !entry.passed) + .map((entry) => `${entry.surface} ${entry.header}`), + }; + } + + it("leaves the declared worker headers UNVERIFIED, not failed", async () => { + const result = await verifyHosting("no-cache"); + expect(result.failed, result.stderr).toEqual([]); + expect(result.status).toBe(3); + }); + + it("fails when the worker script is served with a long TTL", async () => { + const result = await verifyHosting("public, max-age=31536000, immutable"); + expect(result.failed).toEqual(["serviceWorker cache-control"]); + expect(result.status).toBe(1); + }); +}); + // 0929/06-04, 0929/11-03. The worker fixtures must also run in a product that // already ships the worker: an inactive selection is turned on, and an ACTIVE // one is kept so the specs exercise that product's own handlers.