From 11a0fd0f4905d2cdb32063fea061bab04a6f1ab9 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Thu, 5 Nov 2026 22:30:16 +0900 Subject: [PATCH] =?UTF-8?q?test(cross-context):=20host=EA=B0=80=20?= =?UTF-8?q?=EC=BA=A1=EC=B2=98=ED=95=9C=20localStorage=EC=9D=98=20pulse?= =?UTF-8?q?=EB=A7=8C=20=EB=B0=9B=EB=8A=94=EC=A7=80=20=EA=B3=A0=EC=A0=95?= =?UTF-8?q?=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제: cross-context host는 `storage` 이벤트의 `storageArea`를 캡처한 `localStorage` 객체와 동일성으로 비교해 sessionStorage 등 다른 영역의 같은 key 이벤트를 거절한다(`browser-cross-context-host.ts` `storageEventTarget`). transport 테스트는 core에 이미 판정된 영역 값을 넘기므로 이 비교를 지워도 (변이 N38) cross-context 테스트 46건이 모두 통과한다(1101/02-04 ②, 지난 커밋 `3bfe6a8`이 하네스가 필요해 미룸). 고른 방법: `createBrowserCrossContextInvalidationFromHost`에 탭 둘을 흉내 낸 host를 넘기는 단위 테스트를 둔다. host에는 BroadcastChannel이 없어 storage pulse만 쓴다. 한 탭의 `localStorage.setItem`이 다른 탭 listener에 `storage` 이벤트를 보낸다. 이벤트의 `storageArea`가 받는 탭의 sessionStorage이거나 모양만 같은 다른 객체이면 구독자가 받지 않고, 받는 탭의 localStorage이면 한 번 받는다. 버린 대안: browser spec에 sessionStorage `storage` 이벤트 행을 더하기. 실제 브라우저 증거는 더 강하지만 3엔진 spec 시간이 늘고, 비교 하나를 고정하는 데는 host 단위 테스트로 충분하다. 위험·한계: 테스트만 더한다. 실제 브라우저의 `StorageEvent.storageArea` 동작은 이 테스트가 보지 않는다(기존 browser spec 몫). 검증: desktop batch에서 확인 - node scripts/run-vitest.ts run tests/unit/cross-tab-invalidation.test.ts - 변이 N38: `storageArea === expectedLocalStorage` 비교를 지우면(늘 `EXPECTED_LOCAL_STORAGE`) 새 테스트가 실패해야 한다. Refs: 1101/02-04 --- tests/unit/cross-tab-invalidation.test.ts | 95 +++++++++++++++++++++++ 1 file changed, 95 insertions(+) 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",