/** * Unit tests — public/cache-nuke.js (the single client-side recovery owner). * * The file is a vanilla-JS IIFE served from public/ (no bundler), so these * tests execute it inside a `vm` sandbox with a hand-rolled DOM/storage stub * and assert the recovery CONTRACT: * * - first visit records the build id, never reloads * - deploy drift (stored buildId != /api/version) → one full nuke+reload * - STALE HTML SHELL (page token != live buildId) → nuke EVEN IF * localStorage already holds the new buildId (the poisoned-desktop trap * from the 2026-11-16 incident that left users on a dead dark page) * - matching token+buildId → calm, zero reloads * - untrustworthy ids (dev/unknown/runtime-*) never trigger nukes * - /api/version failure → no reload, no crash (offline tolerance) * - the nuke cap (2/session) renders the recovery banner INSTEAD of a * third reload — reload loops are impossible by construction * - `elbaz:edge-challenge` (dispatched by trpc.tsx on cf-mitigated 403s): * first event → exactly one recovery navigation; persistent challenge → * guidance banner, never a loop * - __nukeAndReload is exported for the index.html self-heal listener */ import { describe, it, expect, beforeEach } from "vitest"; import { readFileSync } from "node:fs"; import { fileURLToPath } from "node:url"; import path from "node:path"; import vm from "node:vm"; const HERE = path.dirname(fileURLToPath(import.meta.url)); const CACHE_NUKE_SRC = readFileSync( path.resolve(HERE, "../../public/cache-nuke.js"), "utf-8" ); class FakeElement { style: { cssText: string } = { cssText: "" }; children: FakeElement[] = []; attributes = new Map(); listeners = new Map void>>(); textContent = ""; innerHTML = ""; constructor( private doc: FakeDocument, public tagName: string, ) {} setAttribute(key: string, value: string): void { this.attributes.set(key, value); if (key === "id") this.doc.byId.set(value, this); } getAttribute(key: string): string | null { return this.attributes.get(key) ?? null; } appendChild(child: FakeElement): void { this.children.push(child); if (child.attributes.get("id")) { this.doc.byId.set(child.attributes.get("id") as string, child); } } addEventListener(_type: string, fn: () => void): void { // click listeners are registered but never fired by these tests void fn; } } class FakeDocument { byId = new Map(); body = new FakeElement(this, "BODY"); documentElement = new FakeElement(this, "HTML"); readyState = "complete"; scriptTags: FakeElement[] = []; getElementById(id: string): FakeElement | null { return this.byId.get(id) ?? null; } createElement(tag: string): FakeElement { return new FakeElement(this, tag); } querySelectorAll(selector: string): FakeElement[] { // Faithful enough: the recovery code only ever queries its own tag. return selector.includes("cache-nuke.js") ? this.scriptTags : []; } } class FakeStorage { private map = new Map(); getItem(key: string): string | null { return this.map.has(key) ? (this.map.get(key) as string) : null; } setItem(key: string, value: string): void { this.map.set(key, String(value)); } removeItem(key: string): void { this.map.delete(key); } clear(): void { this.map.clear(); } } interface SandboxOptions { token: string | null; versionBody: { buildId?: string } | null; versionFails?: boolean; localStorageSeed?: Record; sessionStorageSeed?: Record; } interface Harness { reloads: number; cachesCleared: number; workersUnregistered: number; dispatchEdgeChallenge: () => void; sandbox: Record; doc: FakeDocument; localStorage: FakeStorage; sessionStorage: FakeStorage; } function flush(): Promise { return new Promise((resolve) => setTimeout(resolve, 8)); } function createHarness(opts: SandboxOptions): Harness { const doc = new FakeDocument(); const localStorage = new FakeStorage(); const sessionStorage = new FakeStorage(); for (const [k, v] of Object.entries(opts.localStorageSeed ?? {})) { localStorage.setItem(k, v); } for (const [k, v] of Object.entries(opts.sessionStorageSeed ?? {})) { sessionStorage.setItem(k, v); } const harness: Harness = { reloads: 0, cachesCleared: 0, workersUnregistered: 0, dispatchEdgeChallenge: () => {}, sandbox: {}, doc, localStorage, sessionStorage, }; // The token the page's own