test(cross-context): host가 캡처한 localStorage의 pulse만 받는지 고정한다

문제: 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
This commit is contained in:
DongHyeonka
2026-11-06 02:07:47 +09:00
parent dc7c95b005
commit 11a0fd0f49
+95
View File
@@ -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<Listener>;
localStorage: Record<string, unknown>;
sessionStorage: Record<string, unknown>;
};
const tabs: Tab[] = [];
let arrivingFrom: "localStorage" | "sessionStorage" | "lookalike" =
"localStorage";
const storageArea = (tab: Tab) => {
const values = new Map<string, string>();
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<string, unknown> => {
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",