import { readFile } from "node:fs/promises"; import { describe, expect, it } from "vitest"; import { checkPublicServiceWorkers, classifyLiveHostingBaseUrl, hostingVerdict, probeLiveRelease, servedEntryScript, } from "../../scripts/lib/hosting-probe.ts"; import { SERVICE_WORKER_SCRIPT_PATH } from "../../src/contracts/service-worker.ts"; describe("public service worker in dist", () => { const app = ["index.html", "config.json", "assets/index-abc.js"]; const active = { mode: "ACTIVE", scriptPath: "service-worker.js", handlers: ["PWA_STATIC_ASSETS"], } as const; const removal = { mode: "REMOVE_REGISTRATION", scriptPath: "service-worker.js" } as const; const purge = { mode: "PURGE_OWNED_RESOURCES", scriptPath: "service-worker.js" } as const; it("accepts the selected worker when the selection is ACTIVE", () => { expect( checkPublicServiceWorkers([...app, "service-worker.js"], active), ).toMatchObject({ expected: true, observed: true, passed: true }); }); // 06-03 (D-5). REMOVE_REGISTRATION ships the self-unregistering worker. it("accepts the selected worker when the selection is REMOVE_REGISTRATION", () => { expect( checkPublicServiceWorkers([...app, "service-worker.js"], removal), ).toMatchObject({ expected: true, observed: true, passed: true }); }); it("accepts a worker-free dist when no worker is selected", () => { expect(checkPublicServiceWorkers(app, null)).toMatchObject({ expected: false, passed: true, }); }); it.each([ ["no selection", [...app, "service-worker.js"], null], ["a PURGE_OWNED_RESOURCES selection", [...app, "service-worker.js"], purge], ["REMOVE_REGISTRATION without the worker", app, removal], ["ACTIVE without the worker", app, active], ["ACTIVE with an extra worker", [...app, "service-worker.js", "assets/sw.js"], active], ["ACTIVE with the worker off the root", [...app, "assets/service-worker.js"], active], ] as const)("rejects %s", (_, files, selection) => { expect(checkPublicServiceWorkers(files, selection)).toMatchObject({ passed: false, reason: expect.stringContaining("dist worker scripts"), }); }); }); describe("live hosting evidence target", () => { it("accepts a canonical production HTTPS root", () => { expect( classifyLiveHostingBaseUrl("https://frontend.example.test/"), ).toMatchObject({ passed: true, observedOrigin: "https://frontend.example.test", }); }); it.each([ ["http://frontend.example.test/", "requires HTTPS"], ["https://localhost:4173/", "not live deployment evidence"], ["https://127.0.0.1/", "not live deployment evidence"], ["https://frontend.example.test/app/", "canonical root URL"], ["https://user:secret@frontend.example.test/", "must not contain credentials"], ])("rejects %s", (url, reason) => { expect(classifyLiveHostingBaseUrl(url)).toMatchObject({ passed: false, reason: expect.stringContaining(reason), }); }); }); // 13-05 (D-14). The declared-header fixture proves nothing about hosting, so it // is UNVERIFIED, and a live probe checks the release actually served. describe("hosting header verdict", () => { it("passes only a live probe of the deployed release", () => { expect(hostingVerdict("live", true)).toBe("PASS"); expect(hostingVerdict("fixture", true)).toBe("UNVERIFIED"); expect(hostingVerdict("fixture", false)).toBe("FAIL"); expect(hostingVerdict("invalid-live", false)).toBe("FAIL"); expect(hostingVerdict("live", false)).toBe("FAIL"); }); it("probes the entry script named by the served index.html", () => { expect( servedEntryScript( '', ), ).toBe("/app/assets/index-Dpjd89Th.js"); expect(servedEntryScript("maintenance")).toBeNull(); }); }); // 1101/13-04. The router uses browser history, so a reload or a shared link of // any route reaches the host as a path the build has no file for. The host has // to answer it with the release's index.html; without that fallback `/` still // opens and only deep links are 404, which no other check sees. describe("SPA fallback and worker script surfaces (1101/13-04)", () => { type Surface = Partial<{ path: string; cacheControl: string; contentTypes: string[]; securityHeaders: boolean; requiredWhen: string; }>; const surfaces = async () => (JSON.parse(await readFile("config/hosting/cache-policy.json", "utf8")) as { surfaces: Record; }).surfaces; it("declares an app route the build has no file for, served like index.html", async () => { const { appRoute, index } = await surfaces(); expect(appRoute).toMatchObject({ cacheControl: index?.cacheControl, contentTypes: index?.contentTypes, securityHeaders: true, }); expect(appRoute?.path).toMatch(/^\/[^.]+$/u); const { responses } = JSON.parse( await readFile("config/hosting/response-headers.fixture.json", "utf8"), ) as { responses: Record }; expect(responses["appRoute"]).toEqual(responses["index"]); }); it("declares the worker script, served no-cache, only for a build that ships it", async () => { expect((await surfaces())["serviceWorker"]).toMatchObject({ path: `/${SERVICE_WORKER_SCRIPT_PATH}`, cacheControl: "no-cache", requiredWhen: "service-worker-shipped", }); }); const INDEX = ''; const PATHS = { index: "/", runtimeConfig: "/config.json", appRoute: "/__probe/deep-link" }; const RELEASE: Record = { "/": [200, INDEX], "/config.json": [200, "{}"], "/assets/index-A1.js": [200, ""], }; const host = (routes: Record) => async (url: URL) => { const [status, body] = routes[url.pathname] ?? [404, "Not Found"]; const response = new Response(body, { status, headers: { "cache-control": "no-cache" } }); Object.defineProperty(response, "url", { value: url.href }); return response; }; const base = new URL("https://frontend.example.test/"); const failed = async (routes: Record) => (await probeLiveRelease(base, PATHS, host(routes))).results .filter((result) => !result.passed) .map((result) => `${result.surface} ${result.header}`); it("passes a host that answers every route with the release's index.html", async () => { expect(await failed({ ...RELEASE, "/__probe/deep-link": [200, INDEX] })).toEqual([]); }); it("fails a host without the fallback", async () => { expect(await failed(RELEASE)).toEqual(["appRoute http-status", "appRoute entry-script"]); }); it("fails a fallback that answers 200 with a page that is not the release's index.html", async () => { expect( await failed({ ...RELEASE, "/__probe/deep-link": [200, "

Not found

"] }), ).toEqual(["appRoute entry-script"]); }); });