import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; class Worker extends EventTarget { constructor(state = "activated") { super(); this.state = state; } transition(state) { this.state = state; this.dispatchEvent(new Event("statechange")); } } async function harness({ pathname = "/portal", controller = new Worker(), registrationRejects = false } = {}) { const container = new EventTarget(); container.controller = controller; // Leave registration pending to reproduce the missed asynchronous window. container.register = vi.fn(() => registrationRejects ? Promise.reject(new Error("synthetic unavailable")) : new Promise(() => {})); const win = new EventTarget(); win.navigator = { serviceWorker: container }; win.location = { pathname, reload: vi.fn() }; win.caches = { keys: vi.fn().mockResolvedValue(["app-api-runtime", "app-static-runtime", "workbox-precache-v2-synthetic", "unrelated-cache"]), delete: vi.fn().mockResolvedValue(true), }; const notices = []; const offline = vi.fn(); win.addEventListener("hyb-oa:pwa-update-ready", e => notices.push(e.detail)); win.addEventListener("hyb-oa:pwa-offline-ready", offline); vi.stubGlobal("window", win); const pwa = await import("../../../../client/src/pwa"); const change = worker => { container.controller = worker; container.dispatchEvent(new Event("controllerchange")); }; return { pwa, win, container, notices, offline, change }; } beforeEach(() => { vi.resetModules(); vi.stubEnv("DEV", false); vi.stubEnv("VITE_PWA_ENABLED", "true"); vi.stubEnv("BASE_URL", "/"); }); afterEach(() => { vi.unstubAllGlobals(); vi.unstubAllEnvs(); }); describe("native PWA update lifecycle", () => { it("observes takeover before register resolves, waits for activation and refreshes once", async () => { const h = await harness(); h.pwa.registerPwaIfEnabled(); h.pwa.registerPwaIfEnabled(); expect(h.container.register).toHaveBeenCalledTimes(1); const next = new Worker("activating"); h.change(next); h.change(next); await Promise.resolve(); expect(h.win.location.reload).not.toHaveBeenCalled(); next.transition("activated"); await vi.waitFor(() => expect(h.win.location.reload).toHaveBeenCalledTimes(1)); h.change(new Worker()); await Promise.resolve(); expect(h.win.location.reload).toHaveBeenCalledTimes(1); expect(h.win.caches.delete.mock.calls.map(([name]) => name).sort()).toEqual(["app-api-runtime", "app-static-runtime"]); }); it("first installation becomes offline-ready without a reload or cache deletion", async () => { const h = await harness({ controller: null }); h.pwa.registerPwaIfEnabled(); const first = new Worker("activating"); h.change(first); first.transition("activated"); await vi.waitFor(() => expect(h.offline).toHaveBeenCalledOnce()); expect(h.win.location.reload).not.toHaveBeenCalled(); expect(h.win.caches.delete).not.toHaveBeenCalled(); h.change(new Worker()); await vi.waitFor(() => expect(h.win.location.reload).toHaveBeenCalledOnce()); }); it.each(["/login", "/change-password", "/forgot-password", "/reset-password/token"])("defers updates on %s until the user applies them", async pathname => { const h = await harness({ pathname }); h.pwa.registerPwaIfEnabled(); h.change(new Worker()); await vi.waitFor(() => expect(h.notices).toHaveLength(1)); expect(h.win.location.reload).not.toHaveBeenCalled(); expect(h.win.caches.delete).not.toHaveBeenCalled(); h.notices[0].applyUpdate(); h.notices[0].applyUpdate(); await vi.waitFor(() => expect(h.win.location.reload).toHaveBeenCalledTimes(1)); }); it("ignores a superseded/redundant controller and then accepts the current one", async () => { const h = await harness(); h.pwa.registerPwaIfEnabled(); const stale = new Worker("activating"); h.change(stale); stale.transition("redundant"); await Promise.resolve(); expect(h.win.location.reload).not.toHaveBeenCalled(); h.change(new Worker()); await vi.waitFor(() => expect(h.win.location.reload).toHaveBeenCalledOnce()); }); it("preserves explicit logout/manual-refresh precache clearing", async () => { const h = await harness(); await h.pwa.clearPwaRuntimeCaches(); expect(h.win.caches.delete.mock.calls.map(([name]) => name).sort()).toEqual(["app-api-runtime", "app-static-runtime", "workbox-precache-v2-synthetic"]); }); it("still refreshes once when runtime cache deletion is unavailable", async () => { const h = await harness(); h.win.caches.keys.mockRejectedValue(new Error("synthetic unavailable")); h.pwa.registerPwaIfEnabled(); h.change(new Worker()); await vi.waitFor(() => expect(h.win.location.reload).toHaveBeenCalledOnce()); }); it.each(["development", "disabled", "unsupported"])("does not register when %s", async mode => { const h = await harness(); if (mode === "development") vi.stubEnv("DEV", true); if (mode === "disabled") vi.stubEnv("VITE_PWA_ENABLED", "false"); if (mode === "unsupported") delete h.win.navigator.serviceWorker; h.pwa.registerPwaIfEnabled(); expect(h.container.register).not.toHaveBeenCalled(); }); it("handles registration failure without a refresh", async () => { const h = await harness({ registrationRejects: true }); h.pwa.registerPwaIfEnabled(); await Promise.resolve(); expect(h.win.location.reload).not.toHaveBeenCalled(); }); });