A big installation outgrew the localStorage quota; saveState() threw QuotaExceededError and, since connecting saves first, the decoder never came online. The local copy is only a cache of the server state, so a write that doesn't fit now drops it instead of throwing. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
36 lines
1.4 KiB
JavaScript
36 lines
1.4 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { writeLocalCache } from "../src/local_cache.js";
|
|
|
|
function fakeStorage({ quota = Infinity } = {}) {
|
|
const items = new Map();
|
|
return {
|
|
items,
|
|
setItem(key, value) {
|
|
if (String(value).length > quota) throw Object.assign(new Error("Setting the value exceeded the quota."), { name: "QuotaExceededError" });
|
|
items.set(key, String(value));
|
|
},
|
|
removeItem(key) { items.delete(key); },
|
|
};
|
|
}
|
|
|
|
test("writes the state when it fits", () => {
|
|
const storage = fakeStorage();
|
|
assert.deepEqual(writeLocalCache(storage, "k", { a: 1 }), { ok: true });
|
|
assert.equal(storage.items.get("k"), '{"a":1}');
|
|
});
|
|
|
|
test("a full storage never throws and drops the stale cache instead of keeping an old copy", () => {
|
|
const storage = fakeStorage({ quota: 20 });
|
|
writeLocalCache(storage, "k", { a: 1 });
|
|
const result = writeLocalCache(storage, "k", { results: "x".repeat(100) });
|
|
assert.equal(result.ok, false);
|
|
assert.equal(result.error.name, "QuotaExceededError");
|
|
assert.equal(storage.items.has("k"), false, "the outdated copy is removed so a reload hydrates from the server");
|
|
});
|
|
|
|
test("a storage that throws on remove too is still survived", () => {
|
|
const storage = { setItem() { throw new Error("blocked"); }, removeItem() { throw new Error("blocked"); } };
|
|
assert.equal(writeLocalCache(storage, "k", {}).ok, false);
|
|
});
|