// The browser-local trigger switches. Storage is foreign input: it can be // absent, hold another version's shape, or refuse to write, and none of those // may stop the plugin from working with its defaults. import { afterEach, test } from 'node:test' import assert from 'node:assert/strict' import { STORAGE_KEY, readStoredKinds, writeStoredKinds } from '../../src/client/core/storage.js' /** Install a storage double, optionally one that throws. */ function useStorage(behaviour) { const data = new Map() globalThis.localStorage = { getItem: (key) => { if (behaviour === 'read-throws') throw new Error('denied') return data.has(key) ? data.get(key) : null }, setItem: (key, value) => { if (behaviour === 'write-throws') throw new Error('quota') data.set(key, String(value)) }, } return data } afterEach(() => { delete globalThis.localStorage }) test('everything is on when this browser has no stored choice', () => { useStorage() assert.deepEqual(readStoredKinds(), { completion: true, approval: true, question: true }) }) test('without storage at all the defaults still apply', () => { assert.deepEqual(readStoredKinds(), { completion: true, approval: true, question: true }) assert.doesNotThrow(() => writeStoredKinds({ completion: false, approval: true, question: true })) }) test('a stored switch survives a round trip, and unknown or broken values do not', () => { const data = useStorage() writeStoredKinds({ completion: false, approval: true, question: false }) assert.equal(data.get(STORAGE_KEY), JSON.stringify({ completion: false, approval: true, question: false })) assert.deepEqual(readStoredKinds(), { completion: false, approval: true, question: false }) data.set(STORAGE_KEY, '{ not json') assert.deepEqual(readStoredKinds(), { completion: true, approval: true, question: true }) data.set(STORAGE_KEY, JSON.stringify({ completion: 'yes', approval: false })) assert.deepEqual(readStoredKinds(), { completion: true, approval: false, question: true }) data.set(STORAGE_KEY, JSON.stringify({ somethingElse: false })) assert.deepEqual(readStoredKinds(), { completion: true, approval: true, question: true }) }) test('storage that refuses to be read or written never throws', () => { useStorage('read-throws') assert.deepEqual(readStoredKinds(), { completion: true, approval: true, question: true }) useStorage('write-throws') assert.doesNotThrow(() => writeStoredKinds({ completion: false, approval: false, question: false })) })