156 lines
6.8 KiB
JavaScript
156 lines
6.8 KiB
JavaScript
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);
|
|
});
|
|
|
|
import { segments, formatMoment, entryMeta, panelState, moveIndex, otherTab } from "../mod/ovis-composer/history.js";
|
|
|
|
test("segments splits a text on the ranges", () => {
|
|
assert.deepEqual(segments("uruchom testy", [[8, 12]]), [
|
|
{ text: "uruchom ", hit: false }, { text: "test", hit: true }, { text: "y", hit: false },
|
|
]);
|
|
assert.deepEqual(segments("ab", []), [{ text: "ab", hit: false }]);
|
|
assert.deepEqual(segments("abab", [[0, 2], [2, 4]]), [{ text: "ab", hit: true }, { text: "ab", hit: true }]);
|
|
assert.deepEqual(segments("", []), []);
|
|
assert.deepEqual(segments("ab", [[0, 99]]), [{ text: "ab", hit: true }]);
|
|
});
|
|
|
|
test("formatMoment is DD.MM HH:MM in local time", () => {
|
|
assert.equal(formatMoment(new Date(2026, 10, 19, 9, 5).getTime()), "19.11 09:05");
|
|
assert.equal(formatMoment(new Date(2026, 0, 2, 23, 59).getTime()), "02.01 23:59");
|
|
assert.equal(formatMoment(NaN), "");
|
|
});
|
|
|
|
test("entryMeta: history says when, where and who; a favourite says when it was saved", () => {
|
|
const at = new Date(2026, 10, 19, 9, 5).getTime();
|
|
assert.equal(entryMeta({ sentAt: at, spaceName: "herdr", tabName: "Ovis Fast", agentName: "claude-code" }, "history"), "19.11 09:05 · herdr · Ovis Fast");
|
|
assert.equal(entryMeta({ sentAt: at, spaceName: "", agentName: "" }, "history"), "19.11 09:05 · ? · ?");
|
|
assert.equal(entryMeta({ sentAt: at, spaceName: "s", tabName: "", agentName: "codex" }, "history"), "19.11 09:05 · s · codex");
|
|
assert.equal(entryMeta({ createdAt: at }, "favorites"), "zapisano 19.11 09:05");
|
|
});
|
|
|
|
test("panelState: counts, hits, and which empty state to show", () => {
|
|
const h = [entry(1, "alfa"), entry(2, "beta")];
|
|
const f = [{ id: "f", text: "gamma", createdAt: 3 }];
|
|
let s = panelState({ history: h, favorites: f, tab: "history", query: "" });
|
|
assert.deepEqual([s.counts, s.hits.length, s.empty], [{ history: 2, favorites: 1 }, 2, null]);
|
|
s = panelState({ history: h, favorites: f, tab: "favorites", query: "gam" });
|
|
assert.deepEqual([s.hits.length, s.empty], [1, null]);
|
|
assert.equal(panelState({ history: [], favorites: f, tab: "history", query: "" }).empty, "history");
|
|
assert.equal(panelState({ history: h, favorites: [], tab: "favorites", query: "" }).empty, "favorites");
|
|
assert.equal(panelState({ history: h, favorites: f, tab: "history", query: "zzz" }).empty, "query");
|
|
});
|
|
|
|
test("moveIndex wraps; otherTab flips", () => {
|
|
assert.equal(moveIndex(0, -1, 3), 2);
|
|
assert.equal(moveIndex(2, 1, 3), 0);
|
|
assert.equal(moveIndex(1, 1, 3), 2);
|
|
assert.equal(moveIndex(0, 1, 0), 0);
|
|
assert.equal(otherTab("history"), "favorites");
|
|
assert.equal(otherTab("favorites"), "history");
|
|
});
|