feat: dock model and storage (herdr-composer.dock)

Co-Authored-By: Ovis Fast (Claude) <[email protected]>
This commit is contained in:
FactorandOvis Fast committed 2026-11-19 15:57:40 +01:00
1 parent 4bb0e85612
commit 6b1f946d25
4 files changed
+401 -1

No files matched your search

+121
View File
@@ -0,0 +1,121 @@
import test from "node:test";
import assert from "node:assert/strict";
import {
DOCK_LIMIT, LABEL_MAX, defaultLabel, normalize, isPinned, pin, unpin, remove, move,
rename, toggleMode, dockAction, dockMenu, rowMenu, starMenu,
} from "../herdr/herdr-composer/mods/herdr-composer/dock.js";
const btn = (id, text, extra = {}) => ({ id, label: text, text, mode: "send", ...extra });
const three = () => [btn("1", "a"), btn("2", "b"), btn("3", "c")];
test("limits", () => {
assert.equal(DOCK_LIMIT, 30);
assert.equal(LABEL_MAX, 16);
});
test("defaultLabel", () => {
assert.equal(defaultLabel("/handoff"), "/handoff");
assert.equal(defaultLabel("npm test i raport po polsku"), "npm test i rapo…");
assert.equal(defaultLabel(" a\n b "), "a b");
});
test("normalize: rubbish, bad modes, duplicates, label", () => {
assert.deepEqual(normalize("x"), []);
assert.deepEqual(normalize(null), []);
const out = normalize([
{ id: "1", text: "a", mode: "send", label: "" },
{ id: "2", text: " a ", mode: "send", label: "x" },
{ id: "3", text: "b", mode: "zly" },
]);
assert.deepEqual(out, [{ id: "1", label: "a", text: "a", mode: "send" }]);
});
test("normalize: limit 30", () => {
const raw = Array.from({ length: 35 }, (_, i) => ({ id: `i${i}`, text: `t${i}`, mode: "send", label: "l" }));
assert.equal(normalize(raw).length, 30);
});
test("pin", () => {
const d = pin([], " /clear ", "9");
assert.deepEqual(d, [{ id: "9", label: "/clear", text: "/clear", mode: "send" }]);
assert.equal(pin(d, "/clear", "10"), d);
assert.equal(pin(d, " ", "x"), d);
const full = Array.from({ length: 30 }, (_, i) => btn(`i${i}`, `t${i}`));
assert.equal(pin(full, "new", "n"), full);
assert.equal(isPinned(d, " /clear "), true);
assert.equal(isPinned(d, "other"), false);
});
test("unpin and remove", () => {
const d = three();
assert.deepEqual(unpin(d, " b ").map((b) => b.id), ["1", "3"]);
assert.deepEqual(remove(d, "1").map((b) => b.id), ["2", "3"]);
assert.equal(d.length, 3);
});
test("move: edge no change, swap, no mutation", () => {
const d = three();
const copy = JSON.parse(JSON.stringify(d));
assert.deepEqual(move(d, "1", -1).map((b) => b.id), ["1", "2", "3"]);
assert.deepEqual(move(d, "3", 1).map((b) => b.id), ["1", "2", "3"]);
assert.deepEqual(move(d, "1", 1).map((b) => b.id), ["2", "1", "3"]);
assert.deepEqual(d, copy);
});
test("rename and toggleMode", () => {
const d = three();
assert.equal(rename(d, "1", " ")[0].label, defaultLabel("a"));
assert.equal(rename(d, "1", " Moje ")[0].label, "Moje");
assert.equal(d[0].label, "a");
const once = toggleMode(d, "1");
assert.equal(once[0].mode, "insert");
assert.equal(toggleMode(once, "1")[0].mode, "send");
assert.equal(d[0].mode, "send");
});
test("dockAction", () => {
assert.equal(dockAction({ mode: "insert" }, { hasAgent: true }), "insert");
assert.equal(dockAction({ mode: "send" }, { hasAgent: true }), "send");
assert.equal(dockAction({ mode: "send" }, { hasAgent: false }), "none");
});
test("dockMenu", () => {
const d = three();
const m = dockMenu(d, "1");
assert.deepEqual(m.map((i) => i.command), ["rename", "mode", "left", "right", "remove"]);
assert.equal(m[0].label, "Zmień nazwę");
assert.equal(m[1].label, "Tylko wstawiaj");
assert.equal(m[2].label, "Przesuń w lewo");
assert.equal(m[2].disabled, true);
assert.notEqual(m[3].disabled, true);
assert.equal(m[4].label, "Usuń z docka");
assert.equal(m[4].danger, true);
const last = dockMenu(d, "3");
assert.equal(last[3].disabled, true);
const ins = dockMenu(toggleMode(d, "2"), "2");
assert.equal(ins[1].label, "Wysyłaj od razu");
});
test("rowMenu", () => {
const h = rowMenu({ tab: "history", favorite: false, pinned: false });
assert.deepEqual(h.map((i) => i.command), ["insert", "favorite"]);
assert.equal(h[0].label, "Wstaw");
assert.equal(h[1].label, "Dodaj do ulubionych");
const f = rowMenu({ tab: "favorites", favorite: true, pinned: true });
assert.deepEqual(f.map((i) => i.command), ["insert", "pin", "favorite"]);
assert.equal(f[1].label, "Odepnij z docka");
assert.equal(f[2].label, "Usuń z ulubionych");
assert.equal(f[2].danger, true);
const g = rowMenu({ tab: "favorites", favorite: true, pinned: false });
assert.equal(g[1].label, "Przypnij do docka");
});
test("starMenu", () => {
assert.equal(starMenu({ text: "", pinned: false })[0].disabled, true);
assert.equal(starMenu({ text: " ", pinned: false })[0].disabled, true);
const on = starMenu({ text: "x", pinned: false });
assert.equal(on[0].command, "pin");
assert.equal(on[0].label, "Przypnij bieżący tekst do docka");
assert.notEqual(on[0].disabled, true);
assert.equal(starMenu({ text: "x", pinned: true })[0].label, "Odepnij bieżący tekst z docka");
});