feat: history and favorites model
Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
1 parent
86ca415d96
commit
c655e46dbf
4 files changed
+353
-25
No files matched your search
@@ -0,0 +1,108 @@
|
||||
import test from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { record, search, toggleFavorite, isFavorite } from "../mod/ovis-composer/history.js";
|
||||
import { loadHistory, saveHistory, loadFavorites, saveFavorites, KEYS } 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) };
|
||||
};
|
||||
const entry = (i, text = `t${i}`, over = {}) => ({
|
||||
id: `h${i}`, text, sentAt: i, agent: "t2", agentName: "claude-code", space: "s1", spaceName: "herdr", ...over,
|
||||
});
|
||||
|
||||
test("record keeps duplicates", () => {
|
||||
let list = record([], entry(1, "same"));
|
||||
list = record(list, entry(2, "same"));
|
||||
assert.equal(list.length, 2);
|
||||
assert.deepEqual(list.map((e) => e.id), ["h1", "h2"]);
|
||||
});
|
||||
|
||||
test("record keeps 500, the oldest drop out, and does not mutate", () => {
|
||||
let list = [];
|
||||
for (let i = 0; i < 500; i++) list.push(entry(i));
|
||||
const before = list.length;
|
||||
const out = record(list, entry(500));
|
||||
assert.equal(list.length, before);
|
||||
assert.equal(out.length, 500);
|
||||
assert.equal(out[0].id, "h1");
|
||||
assert.equal(out[499].id, "h500");
|
||||
});
|
||||
|
||||
test("search is case-insensitive and returns ranges", () => {
|
||||
const out = search([entry(1, "uruchom testy")], "TEST");
|
||||
assert.equal(out.length, 1);
|
||||
assert.deepEqual(out[0].ranges, [[8, 12]]);
|
||||
assert.equal(out[0].item.text, "uruchom testy");
|
||||
});
|
||||
|
||||
test("search finds every occurrence", () => {
|
||||
const out = search([entry(1, "test a test")], "test");
|
||||
assert.deepEqual(out[0].ranges, [[0, 4], [7, 11]]);
|
||||
});
|
||||
|
||||
test("search folds Polish letters by case", () => {
|
||||
assert.equal(search([entry(1, "jedziemy do łódź")], "ŁÓDŹ").length, 1);
|
||||
assert.equal(search([entry(1, "JEDZIEMY DO ŁÓDŹ")], "łódź").length, 1);
|
||||
assert.deepEqual(search([entry(1, "do ŁÓDŹ")], "łódź")[0].ranges, [[3, 7]]);
|
||||
});
|
||||
|
||||
test("search: newest first; empty query gives everything; no match gives nothing", () => {
|
||||
const list = [entry(1, "a"), entry(3, "b"), entry(2, "c")];
|
||||
assert.deepEqual(search(list, "").map((r) => r.item.id), ["h3", "h2", "h1"]);
|
||||
assert.deepEqual(search(list, " ").map((r) => r.item.id), ["h3", "h2", "h1"]);
|
||||
assert.deepEqual(search(list, "").map((r) => r.ranges), [[], [], []]);
|
||||
assert.deepEqual(search(list, "zzz"), []);
|
||||
assert.deepEqual(search([], "x"), []);
|
||||
});
|
||||
|
||||
test("search orders favourites by createdAt", () => {
|
||||
const favs = [{ id: "a", text: "x", createdAt: 1 }, { id: "b", text: "x2", createdAt: 5 }];
|
||||
assert.deepEqual(search(favs, "x").map((r) => r.item.id), ["b", "a"]);
|
||||
});
|
||||
|
||||
test("toggleFavorite adds by trimmed text, removes on a second toggle", () => {
|
||||
let favs = toggleFavorite([], " hello ", 10);
|
||||
assert.equal(favs.length, 1);
|
||||
assert.equal(favs[0].text, "hello");
|
||||
assert.equal(favs[0].createdAt, 10);
|
||||
assert.equal(isFavorite(favs, "hello "), true);
|
||||
favs = toggleFavorite(favs, "hello", 20);
|
||||
assert.deepEqual(favs, []);
|
||||
assert.equal(isFavorite(favs, "hello"), false);
|
||||
});
|
||||
|
||||
test("toggleFavorite: empty text changes nothing; limit 100 drops the oldest; ids are unique", () => {
|
||||
const empty = [];
|
||||
assert.equal(toggleFavorite(empty, " ", 1), empty);
|
||||
let favs = [];
|
||||
for (let i = 0; i < 101; i++) favs = toggleFavorite(favs, `f${i}`, 1000);
|
||||
assert.equal(favs.length, 100);
|
||||
assert.equal(favs[0].text, "f1");
|
||||
assert.equal(new Set(favs.map((f) => f.id)).size, 100);
|
||||
});
|
||||
|
||||
test("storage: old history entries load with empty space/agentName", () => {
|
||||
const store = memStore();
|
||||
store.set(KEYS.history, [{ id: "a", text: "old", at: 7, agent: "t2" }]);
|
||||
const [e] = loadHistory(store);
|
||||
assert.equal(e.sentAt, 7);
|
||||
assert.equal(e.agentName, "");
|
||||
assert.equal(e.space, "");
|
||||
assert.equal(e.spaceName, "");
|
||||
});
|
||||
|
||||
test("storage: history and favourites roundtrip in the new shape", () => {
|
||||
const store = memStore();
|
||||
saveHistory(store, [entry(1)]);
|
||||
assert.deepEqual(loadHistory(store), [entry(1)]);
|
||||
const fav = { id: "f", text: "x", createdAt: 3 };
|
||||
saveFavorites(store, [fav]);
|
||||
assert.deepEqual(loadFavorites(store), [fav]);
|
||||
});
|
||||
|
||||
test("storage: old favourites (at) load as createdAt", () => {
|
||||
const store = memStore();
|
||||
store.set(KEYS.favorites, [{ id: "f", text: "x", at: 4 }]);
|
||||
assert.equal(loadFavorites(store)[0].createdAt, 4);
|
||||
});
|
||||
Reference in new issue
Block a user