// client-id.mjs: a tab keeps its presence id across a reload, two tabs // do not share one, and a duplicated tab (which copies its original's // sessionStorage) gives the copied id up. import test from 'node:test' import assert from 'node:assert/strict' import { tabClientId, claimClientId, clientIdKey } from './client-id.mjs' import { createPresence } from './presence.mjs' function memoryStorage () { const m = new Map() return { getItem: k => (m.has(k) ? m.get(k) : null), setItem: (k, v) => { m.set(k, String(v)) }, copy () { const c = memoryStorage(); for (const [k, v] of m) c.setItem(k, v); return c } } } let n = 0 const make = () => (++n).toString(16).padStart(16, '0') test('a reload reuses the id this tab stored for the deck', () => { const tab = memoryStorage() const first = tabClientId({ storage: tab, deck: 'd1', make }) const reloaded = tabClientId({ storage: tab, deck: 'd1', make }) assert.equal(reloaded, first) assert.equal(tab.getItem(clientIdKey('d1')), first) }) test('two tabs, and two decks in one tab, get different ids', () => { const a = tabClientId({ storage: memoryStorage(), deck: 'd1', make }) const b = tabClientId({ storage: memoryStorage(), deck: 'd1', make }) assert.notEqual(a, b) const tab = memoryStorage() assert.notEqual(tabClientId({ storage: tab, deck: 'd1', make }), tabClientId({ storage: tab, deck: 'd2', make })) }) test('a stored value that is not an id, or storage that throws, gives a fresh id', () => { const tab = memoryStorage() tab.setItem(clientIdKey('d1'), '