mirror of
https://github.com/block/buzz.git
synced 2026-10-31 14:31:02 +00:00
Signed-off-by: Wes <wesbillman@users.noreply.github.com> Co-authored-by: Brain <21994759fc7a6fa6b965551d35cfd7897d262f2495467f2d78694ddcfa6a5c7e@sprout-oss.stage.blox.sqprod.co>
62 lines
1.9 KiB
JavaScript
62 lines
1.9 KiB
JavaScript
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");
|
|
});
|