179 lines
7.6 KiB
JavaScript
179 lines
7.6 KiB
JavaScript
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, menuTransition, applyDock, add, withinGuard,
|
|
} 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");
|
|
});
|
|
|
|
test("normalize: duplicate ids dropped (first wins), labels trimmed", () => {
|
|
const out = normalize([
|
|
{ id: "1", text: "a", mode: "send", label: " Moje " },
|
|
{ id: "1", text: "b", mode: "send", label: "x" },
|
|
{ id: "2", text: "c", mode: "send", label: " " },
|
|
]);
|
|
assert.deepEqual(out, [
|
|
{ id: "1", label: "Moje", text: "a", mode: "send" },
|
|
{ id: "2", label: "c", text: "c", mode: "send" },
|
|
]);
|
|
});
|
|
|
|
test("menuTransition: open replaces, close only closes its own menu", () => {
|
|
const A = { kind: "dock", id: "A" };
|
|
const B = { kind: "dock", id: "B" };
|
|
assert.deepEqual(menuTransition(null, { ...A, open: true }), A);
|
|
// open A, a command (menu closed), open B -> B is open
|
|
let state = menuTransition(null, { ...A, open: true });
|
|
state = null;
|
|
assert.deepEqual(menuTransition(state, { ...B, open: true }), B);
|
|
// open A, then open B, then the late close of A -> B stays
|
|
state = menuTransition(A, { ...B, open: true });
|
|
assert.deepEqual(menuTransition(state, { ...A, open: false }), B);
|
|
// close of the open one closes; a close with nothing open stays null
|
|
assert.equal(menuTransition(B, { ...B, open: false }), null);
|
|
assert.equal(menuTransition(null, { ...A, open: false }), null);
|
|
// same id, other kind is another menu
|
|
assert.deepEqual(menuTransition(B, { kind: "star", id: "B", open: false }), B);
|
|
});
|
|
|
|
test("applyDock: works on the stored list, so another copy's change survives", () => {
|
|
const stored = [btn("1", "a"), btn("2", "from-copy-A")];
|
|
// copy B held only ["a"]; it pins "b" - the result keeps A's pin
|
|
const out = applyDock(stored, (d) => pin(d, "b", "3"));
|
|
assert.deepEqual(out.map((b) => b.id), ["1", "2", "3"]);
|
|
assert.deepEqual(applyDock("rubbish", (d) => pin(d, "x", "9")).map((b) => b.id), ["9"]);
|
|
assert.deepEqual(applyDock(stored, (d) => remove(d, "1")).map((b) => b.id), ["2"]);
|
|
});
|
|
|
|
test("add: label, mode, empty text, duplicate, limit", () => {
|
|
const a = add([], { id: "1", text: " /handoff ", label: "", mode: "send" });
|
|
assert.deepEqual(a, [{ id: "1", label: "/handoff", text: "/handoff", mode: "send" }]);
|
|
const b = add(a, { id: "2", text: "npm test", label: " Testy ", mode: "insert" });
|
|
assert.deepEqual(b[1], { id: "2", label: "Testy", text: "npm test", mode: "insert" });
|
|
assert.equal(add(b, { id: "3", text: " ", label: "x", mode: "send" }), b);
|
|
assert.equal(add(b, { id: "3", text: "npm test", label: "x", mode: "send" }), b);
|
|
const full = Array.from({ length: DOCK_LIMIT }, (_, i) => btn(`f${i}`, `t${i}`));
|
|
assert.equal(add(full, { id: "n", text: "new", label: "", mode: "send" }), full);
|
|
assert.equal(add([], { id: "1", text: "x", label: "", mode: "bogus" })[0].mode, "send");
|
|
});
|
|
|
|
test("withinGuard: only just after the moment", () => {
|
|
assert.equal(withinGuard(1100, 1000, 250), true);
|
|
assert.equal(withinGuard(1250, 1000, 250), false);
|
|
assert.equal(withinGuard(5000, 0, 250), false);
|
|
});
|