Files
herdrMario/tests/storage.test.mjs
T
2026-11-19 11:06:12 +01:00

59 lines
2.0 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import {
HISTORY_LIMIT, queueKey, loadHistory, saveHistory, loadQueue, saveQueue, loadUsage, saveUsage,
} from "../mod/ovis-composer/storage.js";
const memStore = () => {
const m = new Map();
return { get: (k) => m.get(k), set: (k, v) => void m.set(k, v) };
};
test("saveHistory truncates to 500, keeping the newest", () => {
const store = memStore();
const items = Array.from({ length: 520 }, (_, i) => ({ id: `i${i}`, text: `t${i}`, at: i }));
saveHistory(store, items);
const out = loadHistory(store);
assert.equal(HISTORY_LIMIT, 500);
assert.equal(out.length, 500);
assert.equal(out[0].id, "i20");
assert.equal(out[499].id, "i519");
});
test("history drops invalid entries on load", () => {
const store = memStore();
store.set("ovis-composer.history", [{ id: "a", text: "x", at: 1 }, { id: "b" }, null]);
assert.equal(loadHistory(store).length, 1);
});
test("queueKey is per agent", () => {
assert.equal(queueKey("a1"), "ovis-composer.queue:a1");
});
test("queue roundtrip; keeps sending state as written", () => {
const store = memStore();
const q = [{ id: "1", text: "t", state: "sending", createdAt: 5 }];
saveQueue(store, "a", q);
assert.deepEqual(loadQueue(store, "a"), q);
assert.deepEqual(loadQueue(store, "b"), []);
});
test("saveQueue ignores empty agent", () => {
const store = memStore();
saveQueue(store, "", [{ id: "1", text: "t", state: "queued", createdAt: 1 }]);
assert.equal(store.get("ovis-composer.queue:"), undefined);
});
test("usage keeps only non-negative finite numbers", () => {
const store = memStore();
saveUsage(store, { review: 3, bad: -1, worse: "x" });
assert.deepEqual(loadUsage(store), { review: 3 });
});
test("rubbish list does not wipe stored history", () => {
const store = memStore();
saveHistory(store, [{ id: "a", text: "x", at: 1 }]);
saveHistory(store, [{ nope: 1 }]);
assert.equal(loadHistory(store).length, 1);
});