import test from "node:test"; import assert from "node:assert/strict"; import { HISTORY_LIMIT, queueKey, loadHistory, saveHistory, loadQueue, saveQueue, loadUsage, saveUsage, KEYS, draftKey, loadDraft, saveDraft, } 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); }); const draft = (over = {}) => ({ text: "hello", caret: 3, attachments: [], identity: { created_ms: 1794818438639, app: "claude-code" }, savedAt: 10, ...over, }); test("draftKey is per agent", () => { assert.equal(KEYS.draft, "ovis-composer.draft"); assert.equal(draftKey("t2"), "ovis-composer.draft:t2"); }); test("draft roundtrip", () => { const store = memStore(); const d = draft({ attachments: [{ id: "a", name: "f.txt", path: "C:/f.txt", bytes: 3, kind: "file", state: "ready" }] }); saveDraft(store, "t2", d); assert.deepEqual(loadDraft(store, "t2"), d); assert.equal(loadDraft(store, "t3"), null); }); test("loadDraft: rubbish in store gives null", () => { const store = memStore(); for (const bad of ["x", [], 5, { text: 1 }]) { store.set(draftKey("t2"), bad); assert.equal(loadDraft(store, "t2"), null); } }); test("draft keeps text over 20000 chars whole", () => { const store = memStore(); const text = "a".repeat(25000); saveDraft(store, "t2", draft({ text, caret: 25000 })); assert.equal(loadDraft(store, "t2").text.length, 25000); }); test("saveDraft ignores rubbish and empty agent", () => { const store = memStore(); saveDraft(store, "t2", draft()); saveDraft(store, "t2", "x"); saveDraft(store, "", draft()); assert.equal(loadDraft(store, "t2").text, "hello"); assert.equal(store.get("ovis-composer.draft:"), undefined); }); test("empty draft overwrites the old one", () => { const store = memStore(); saveDraft(store, "t2", draft()); saveDraft(store, "t2", draft({ text: "", caret: 0 })); assert.equal(loadDraft(store, "t2").text, ""); }); test("loadDraft: text-only draft still loads, with defaults", () => { const store = memStore(); store.set(draftKey("t2"), { text: "a" }); assert.deepEqual(loadDraft(store, "t2"), { text: "a", caret: 1, attachments: [], identity: { created_ms: null, app: null }, savedAt: 0, }); }); test("saveDraft does not overwrite a known created_ms with null", () => { const store = memStore(); saveDraft(store, "t2", draft()); saveDraft(store, "t2", draft({ text: "nowy", identity: { created_ms: null, app: null } })); const out = loadDraft(store, "t2"); assert.equal(out.text, "nowy"); assert.equal(out.identity.created_ms, 1794818438639); saveDraft(store, "t2", draft({ text: "x", identity: { created_ms: 7, app: null } })); assert.equal(loadDraft(store, "t2").identity.created_ms, 7); }); test("queue roundtrip keeps a countdown entry (restart turns it into held)", () => { const store = memStore(); const q = [{ id: "1", text: "t", state: "countdown", createdAt: 5, left: 2 }]; saveQueue(store, "a", q); assert.deepEqual(loadQueue(store, "a"), q); }); import { loadUserMin, saveUserMin } from "../mod/ovis-composer/storage.js"; test("userMin: the height the person dragged the composer to; 0 is none", () => { const store = memStore(); assert.equal(loadUserMin(store), 0); saveUserMin(store, 300); assert.equal(loadUserMin(store), 300); saveUserMin(store, 0); assert.equal(loadUserMin(store), 0); store.set("ovis-composer.userMin", "x"); assert.equal(loadUserMin(store), 0); store.set("ovis-composer.userMin", -5); assert.equal(loadUserMin(store), 0); saveUserMin(store, NaN); assert.equal(store.get("ovis-composer.userMin"), -5); });