263 lines
9.9 KiB
JavaScript
263 lines
9.9 KiB
JavaScript
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, loadDock, saveDock,
|
|
} from "../herdr/herdr-composer/mods/herdr-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("herdr-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"), "herdr-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("herdr-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, "herdr-composer.draft");
|
|
assert.equal(draftKey("t2"), "herdr-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("herdr-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 "../herdr/herdr-composer/mods/herdr-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("herdr-composer.userMin", "x");
|
|
assert.equal(loadUserMin(store), 0);
|
|
store.set("herdr-composer.userMin", -5);
|
|
assert.equal(loadUserMin(store), 0);
|
|
saveUserMin(store, NaN);
|
|
assert.equal(store.get("herdr-composer.userMin"), -5);
|
|
});
|
|
|
|
// --- the rename ovis-composer -> herdr-composer: nothing the person kept is lost
|
|
|
|
import { LEGACY_PREFIX, loadFavorites } from "../herdr/herdr-composer/mods/herdr-composer/storage.js";
|
|
|
|
const OLD = "ovis-composer.";
|
|
const NEW = "herdr-composer.";
|
|
|
|
test("migration: the legacy prefix", () => {
|
|
assert.equal(LEGACY_PREFIX, "ovis-composer.");
|
|
assert.equal(KEYS.history, "herdr-composer.history");
|
|
assert.equal(queueKey("t2"), "herdr-composer.queue:t2");
|
|
assert.equal(draftKey("t2"), "herdr-composer.draft:t2");
|
|
});
|
|
|
|
test("migration: the new key wins over the old one", () => {
|
|
const store = memStore();
|
|
store.set(OLD + "history", [{ id: "old", text: "old", at: 1 }]);
|
|
store.set(NEW + "history", [{ id: "new", text: "new", sentAt: 2 }]);
|
|
assert.deepEqual(loadHistory(store).map((e) => e.id), ["new"]);
|
|
});
|
|
|
|
test("migration: history from the old key is read and written under the new one; the old stays", () => {
|
|
const store = memStore();
|
|
const old = [{ id: "old", text: "old", at: 1 }];
|
|
store.set(OLD + "history", old);
|
|
assert.deepEqual(loadHistory(store).map((e) => e.id), ["old"]);
|
|
assert.deepEqual(store.get(NEW + "history"), old);
|
|
assert.deepEqual(store.get(OLD + "history"), old);
|
|
});
|
|
|
|
test("migration: favourites, usage, queue, draft and userMin come across the same way", () => {
|
|
const store = memStore();
|
|
store.set(OLD + "favorites", [{ id: "f", text: "x", at: 4 }]);
|
|
store.set(OLD + "usage", { review: 2 });
|
|
store.set(OLD + "queue:t2", [{ id: "1", text: "t", state: "held", createdAt: 5 }]);
|
|
store.set(OLD + "draft:t2", { text: "szkic", caret: 2, attachments: [], identity: { created_ms: 9, app: null }, savedAt: 1 });
|
|
store.set(OLD + "userMin", 300);
|
|
|
|
assert.equal(loadFavorites(store)[0].createdAt, 4);
|
|
assert.deepEqual(loadUsage(store), { review: 2 });
|
|
assert.equal(loadQueue(store, "t2")[0].state, "held");
|
|
assert.equal(loadDraft(store, "t2").text, "szkic");
|
|
assert.equal(loadUserMin(store), 300);
|
|
|
|
for (const key of ["favorites", "usage", "queue:t2", "draft:t2", "userMin"]) {
|
|
assert.notEqual(store.get(NEW + key), undefined, key);
|
|
assert.notEqual(store.get(OLD + key), undefined, key);
|
|
}
|
|
});
|
|
|
|
test("migration: neither key -> the defaults, and nothing is written", () => {
|
|
const store = memStore();
|
|
assert.deepEqual(loadHistory(store), []);
|
|
assert.deepEqual(loadFavorites(store), []);
|
|
assert.deepEqual(loadUsage(store), {});
|
|
assert.deepEqual(loadQueue(store, "t2"), []);
|
|
assert.equal(loadDraft(store, "t2"), null);
|
|
assert.equal(loadUserMin(store), 0);
|
|
assert.equal(store.get(NEW + "history"), undefined);
|
|
assert.equal(store.get(NEW + "draft:t2"), undefined);
|
|
});
|
|
|
|
test("migration: rubbish in the old key is ignored as before, and not copied", () => {
|
|
const store = memStore();
|
|
store.set(OLD + "history", "x");
|
|
store.set(OLD + "favorites", [{ nope: 1 }]);
|
|
store.set(OLD + "queue:t2", [{ id: "1" }]);
|
|
store.set(OLD + "draft:t2", [1, 2]);
|
|
store.set(OLD + "usage", { a: -1, b: "x" });
|
|
store.set(OLD + "userMin", -3);
|
|
assert.deepEqual(loadHistory(store), []);
|
|
assert.deepEqual(loadFavorites(store), []);
|
|
assert.deepEqual(loadQueue(store, "t2"), []);
|
|
assert.equal(loadDraft(store, "t2"), null);
|
|
assert.deepEqual(loadUsage(store), {});
|
|
assert.equal(loadUserMin(store), 0);
|
|
for (const key of ["history", "favorites", "queue:t2", "draft:t2", "usage", "userMin"]) {
|
|
assert.equal(store.get(NEW + key), undefined, key);
|
|
}
|
|
});
|
|
|
|
test("migration: a store that cannot be written to still reads the old data", () => {
|
|
const raw = new Map([[OLD + "history", [{ id: "old", text: "old", at: 1 }]]]);
|
|
const store = { get: (k) => raw.get(k), set: () => { throw new Error("read-only"); } };
|
|
assert.deepEqual(loadHistory(store).map((e) => e.id), ["old"]);
|
|
});
|
|
|
|
test("dock roundtrip under herdr-composer.dock", () => {
|
|
const store = memStore();
|
|
const d = [{ id: "1", label: "a", text: "a", mode: "send" }, { id: "2", label: "b", text: "b", mode: "insert" }];
|
|
saveDock(store, d);
|
|
assert.equal(KEYS.dock, "herdr-composer.dock");
|
|
assert.deepEqual(store.get("herdr-composer.dock"), d);
|
|
assert.deepEqual(loadDock(store), d);
|
|
});
|
|
|
|
test("dock does not read the legacy key", () => {
|
|
const store = memStore();
|
|
store.set("ovis-composer.dock", [{ id: "1", label: "a", text: "a", mode: "send" }]);
|
|
assert.deepEqual(loadDock(store), []);
|
|
assert.equal(store.get("herdr-composer.dock"), undefined);
|
|
});
|
|
|
|
test("dock rubbish loads as empty", () => {
|
|
const store = memStore();
|
|
store.set("herdr-composer.dock", "smieci");
|
|
assert.deepEqual(loadDock(store), []);
|
|
store.set("herdr-composer.dock", { a: 1 });
|
|
assert.deepEqual(loadDock(store), []);
|
|
});
|