import { afterEach, describe, expect, it } from 'vitest' import { recall } from './recall' /** * What the store promises about text it did not write. * * Storage outlives the code that wrote it, so the interesting cases are all * about a value from another build: a half-written entry, an array of the wrong * shape, a string that is not JSON at all. Each has to cost the entry and not * the list — and none of them may reach a caller, because what a caller does * next with one of these is a lookup. */ interface Seen { kind: 'thing' | 'holder' where: string at: string } const whole = (held: unknown): held is Seen => { if (typeof held !== 'object' || held === null) return false const { kind, where, at } = held as Record return ( (kind === 'thing' || kind === 'holder') && typeof where === 'string' && where !== '' && typeof at === 'string' && /^0x[0-9a-f]+$/i.test(at) ) } /** Two entries being the same entry. A holder is the same holder everywhere; * everything else belongs to one venue. */ const same = (a: Seen, b: Seen): boolean => a.kind === b.kind && a.at === b.at && (a.kind === 'holder' || a.where === b.where) const KEY = 'test.searched' const store = (most?: number) => recall({ key: KEY, most, whole, same }) const thing = (at: string, where = 'lux'): Seen => ({ kind: 'thing', where, at }) /** A store, and a store that has stopped answering — which is what private * browsing and an embedded frame both look like from here. */ const kept = new Map() const held = { getItem: (k: string) => kept.get(k) ?? null, setItem: (k: string, v: string) => void kept.set(k, v), removeItem: (k: string) => void kept.delete(k), } const refuses = { getItem: () => { throw new Error('denied') }, setItem: () => { throw new Error('denied') }, removeItem: () => { throw new Error('denied') }, } const put = (v: unknown) => Object.defineProperty(globalThis, 'localStorage', { value: v, configurable: true }) afterEach(() => { kept.clear() put(undefined) }) describe('recall', () => { it('remembers nothing where there is no store at all', () => { expect(store().read()).toStrictEqual([]) }) it('costs the entry and not the list', () => { put(held) kept.set( KEY, JSON.stringify([ thing('0xaa'), { kind: 'thing', where: 'lux' }, { kind: 'nothing', where: 'lux', at: '0xbb' }, { kind: 'thing', where: '', at: '0xcc' }, thing('0xdd'), ]), ) expect(store().read().map((s) => s.at)).toStrictEqual(['0xaa', '0xdd']) }) it('reads nothing out of text that is not a list', () => { put(held) for (const raw of ['', 'null', '{"a":1}', 'not json at all']) { kept.set(KEY, raw) expect(store().read()).toStrictEqual([]) } }) it('puts the newest at the head and keeps the entry once', () => { const one = thing('0xaa') const two = thing('0xbb') const had = [one, two] expect(store().keep(had, two).map((s) => s.at)).toStrictEqual(['0xbb', '0xaa']) }) it('tells two venues at one address apart, and one holder from itself', () => { const here = thing('0xaa', 'lux') const there = thing('0xaa', 'zoo') expect(store().keep([here], there)).toHaveLength(2) const holder = { kind: 'holder', where: 'lux', at: '0xaa' } as const const elsewhere = { kind: 'holder', where: 'zoo', at: '0xaa' } as const expect(store().keep([holder], elsewhere)).toHaveLength(1) }) it('holds no more than it was told to', () => { put(held) const it = store(3) let had = it.read() for (const at of ['0xa', '0xb', '0xc', '0xd']) had = it.keep(had, thing(at)) expect(had.map((s) => s.at)).toStrictEqual(['0xd', '0xc', '0xb']) expect(it.read()).toHaveLength(3) }) it('bounds a list somebody else wrote too', () => { put(held) kept.set(KEY, JSON.stringify(['0xa', '0xb', '0xc', '0xd'].map((at) => thing(at)))) expect(store(2).read()).toHaveLength(2) }) it('costs this visit and nothing more where the store refuses', () => { put(refuses) const it = store() expect(it.read()).toStrictEqual([]) // The list still stands for as long as the page does, which is the whole // promise: nothing was written, and nothing threw at the caller. expect(it.keep([], thing('0xaa')).map((s) => s.at)).toStrictEqual(['0xaa']) expect(it.forget()).toStrictEqual([]) }) it('forgets in the store and not only on the page', () => { put(held) const it = store() it.keep([], thing('0xaa')) expect(it.read()).toHaveLength(1) expect(it.forget()).toStrictEqual([]) expect(it.read()).toStrictEqual([]) expect(kept.has(KEY)).toBe(false) }) })