diff --git a/tests/unit/cross-tab-invalidation.test.ts b/tests/unit/cross-tab-invalidation.test.ts index d9fb4d7b..318f0564 100644 --- a/tests/unit/cross-tab-invalidation.test.ts +++ b/tests/unit/cross-tab-invalidation.test.ts @@ -385,6 +385,101 @@ describe("browser cross-context host", () => { ]); }); + // 1101/02-04 (2), N38. sessionStorage and every other Storage area raise + // the same `storage` event, so the host trusts only the localStorage object + // it captured, compared by identity. The transport tests above hand the core + // a ready-made area verdict, so removing that comparison passed them all. + it("takes a storage pulse only from the localStorage object it captured", () => { + type Listener = (event: unknown) => void; + type Tab = { + listeners: Set; + localStorage: Record; + sessionStorage: Record; + }; + const tabs: Tab[] = []; + let arrivingFrom: "localStorage" | "sessionStorage" | "lookalike" = + "localStorage"; + const storageArea = (tab: Tab) => { + const values = new Map(); + const notify = (key: string, newValue: string | null) => { + const from = arrivingFrom; + for (const other of tabs) { + if (other === tab) continue; + // Same shape, another object: identity, not shape, is the test. + const area = + from === "lookalike" ? { ...other.localStorage } : other[from]; + for (const listener of [...other.listeners]) { + listener({ key, newValue, storageArea: area }); + } + } + }; + return { + getItem: (key: string) => values.get(key) ?? null, + setItem(key: string, value: string) { + values.set(key, value); + notify(key, value); + }, + removeItem(key: string) { + values.delete(key); + notify(key, null); + }, + }; + }; + const hostOfNewTab = (): Record => { + const tab: Tab = { + listeners: new Set(), + localStorage: {}, + sessionStorage: {}, + }; + tab.localStorage = storageArea(tab); + tab.sessionStorage = storageArea(tab); + tabs.push(tab); + // No BroadcastChannel, so the storage pulse is the only transport. + return { + crypto: globalThis.crypto, + localStorage: tab.localStorage, + sessionStorage: tab.sessionStorage, + addEventListener(type: string, listener: Listener) { + if (type === "storage") tab.listeners.add(listener); + }, + removeEventListener(type: string, listener: Listener) { + if (type === "storage") tab.listeners.delete(listener); + }, + }; + }; + const topics = [{ topic: TOPIC, topicVersion: 1 }]; + const publisher = createBrowserCrossContextInvalidationFromHost({ + host: hostOfNewTab(), + cacheEpoch: CACHE_EPOCH, + topics, + }); + const receiver = createBrowserCrossContextInvalidationFromHost({ + host: hostOfNewTab(), + cacheEpoch: CACHE_EPOCH, + topics, + }); + if (!publisher || !receiver) throw new Error("host runtime not created"); + const received = vi.fn(); + receiver.subscribe(received); + + for (const area of ["sessionStorage", "lookalike"] as const) { + arrivingFrom = area; + expect( + publisher.publish({ topic: TOPIC, topicVersion: 1 }), + ).toMatchObject({ ok: true }); + } + expect(received).not.toHaveBeenCalled(); + + arrivingFrom = "localStorage"; + expect( + publisher.publish({ topic: TOPIC, topicVersion: 1 }), + ).toMatchObject({ ok: true }); + expect(received).toHaveBeenCalledOnce(); + + publisher.close(); + receiver.close(); + }); + it("derives a valid, stable cache epoch from any release id (Q-6.2-9)", () => { const releaseIds = [ "1.2.3+sha.abc",