import assert from "node:assert/strict"; import test from "node:test"; import { setLocalStorageItemWithRecovery } from "./localStorageQuota.ts"; function makeQuotaLocalStorage({ maxEntries }) { const store = new Map(); return { store, get length() { return store.size; }, key: (i) => [...store.keys()][i] ?? null, getItem: (key) => store.get(key) ?? null, setItem(key, value) { if (!store.has(key) && store.size >= maxEntries) { throw new Error("QuotaExceededError"); } store.set(key, value); }, removeItem: (key) => store.delete(key), }; } function install(ls) { if (typeof globalThis.window === "undefined") { globalThis.window = {}; } globalThis.window.localStorage = ls; globalThis.localStorage = ls; } test("writes normally when under quota", () => { const ls = makeQuotaLocalStorage({ maxEntries: 10 }); install(ls); assert.equal(setLocalStorageItemWithRecovery("k", "v"), true); assert.equal(ls.getItem("k"), "v"); }); test("evicts pure caches and retries on quota failure", () => { const ls = makeQuotaLocalStorage({ maxEntries: 2 }); install(ls); ls.store.set("buzz-channel-messages.v1:relay:chan", "big"); ls.store.set("buzz-channels.v1:relay", "big"); assert.equal(setLocalStorageItemWithRecovery("k", "v"), true); assert.equal(ls.getItem("k"), "v"); assert.equal(ls.getItem("buzz-channel-messages.v1:relay:chan"), null); assert.equal(ls.getItem("buzz-channels.v1:relay"), null); }); test("returns false when eviction frees nothing", () => { const ls = makeQuotaLocalStorage({ maxEntries: 2 }); install(ls); ls.store.set("buzz-workspaces", "keep"); ls.store.set("buzz-active-workspace-id", "keep"); assert.equal(setLocalStorageItemWithRecovery("k", "v"), false); assert.equal(ls.getItem("k"), null); assert.equal(ls.getItem("buzz-workspaces"), "keep"); });