Files

222 lines
11 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { createState, reduce, isSending } from "../herdr/herdr-composer/mods/herdr-composer/state.js";
test("createState starts empty", () => {
const s = createState();
assert.deepEqual(s.drafts, {});
assert.deepEqual(s.queue, {});
assert.equal(s.menu, null);
});
test("draft then send clears the draft", () => {
let s = reduce(createState(), { type: "draft", agent: "a", text: "hi" });
assert.equal(s.drafts.a, "hi");
s = reduce(s, { type: "send", agent: "a" });
assert.equal(s.drafts.a, "");
});
test("reduce never mutates", () => {
const s = createState();
const n = reduce(s, { type: "draft", agent: "a", text: "x" });
assert.notEqual(s, n);
assert.deepEqual(s.drafts, {});
});
test("queue adds item and isSending sees only sending state", () => {
let s = reduce(createState(), { type: "queue", agent: "a", item: { id: "1", text: "t", createdAt: 1 } });
assert.equal(s.queue.a[0].state, "queued");
assert.equal(isSending(s, "a"), false);
s = reduce(s, { type: "restore", agent: "a", items: [{ id: "2", text: "t", state: "sending", createdAt: 2 }] });
assert.equal(isSending(s, "a"), true);
assert.equal(isSending(s, "b"), false);
});
test("queue without createdAt throws", () => {
assert.throws(() => reduce(createState(), { type: "queue", agent: "a", item: { id: "1", text: "t" } }), TypeError);
});
test("restart turns sending into uncertain for that agent only", () => {
let s = reduce(createState(), { type: "restore", agent: "a", items: [{ id: "1", text: "t", state: "sending", createdAt: 1 }] });
s = reduce(s, { type: "restore", agent: "b", items: [{ id: "2", text: "t", state: "sending", createdAt: 1 }] });
s = reduce(s, { type: "restart", agent: "a" });
assert.equal(s.queue.a[0].state, "uncertain");
assert.equal(s.queue.b[0].state, "sending");
});
test("attach and removeAttachment", () => {
let s = reduce(createState(), { type: "attach", agent: "a", file: { id: "f", name: "x", path: "/x", bytes: 1 } });
assert.equal(s.attachments.a[0].kind, "file");
assert.equal(s.attachments.a[0].state, "ready");
s = reduce(s, { type: "removeAttachment", agent: "a", id: "f" });
assert.deepEqual(s.attachments.a, []);
});
test("notice sets and clears", () => {
let s = reduce(createState(), { type: "notice", notice: { text: "x" } });
assert.deepEqual(s.notice, { text: "x" });
s = reduce(s, { type: "notice", notice: null });
assert.equal(s.notice, null);
});
const dr = (created_ms, text = "szkic") => ({
text, caret: 2, attachments: [], identity: { created_ms, app: "codex" }, savedAt: 1,
});
test("restore_draft with the same identity restores silently", () => {
const s = reduce(createState(), { type: "restore_draft", agent: "t2", draft: dr(5), identity: { created_ms: 5, app: "codex" } });
assert.equal(s.drafts.t2, "szkic");
assert.equal(s.carets.t2, 2);
assert.equal(s.notice, null);
});
test("restore_draft with other created_ms holds the whole queue and warns", () => {
let s = reduce(createState(), { type: "restore", agent: "t2", items: [
{ id: "1", text: "a", state: "queued", createdAt: 1 },
{ id: "2", text: "b", state: "held", createdAt: 2 },
{ id: "3", text: "c", state: "uncertain", createdAt: 3 },
] });
s = reduce(s, { type: "restore_draft", agent: "t2", draft: dr(5), identity: { created_ms: 9, app: "codex" } });
assert.equal(s.drafts.t2, "szkic");
assert.deepEqual(s.notice, { text: "Szkic z poprzedniej sesji agenta", level: "warn" });
assert.deepEqual(s.queue.t2.map((i) => i.state), ["held", "held", "uncertain"]);
});
test("restore_draft restores attachments and never mutates", () => {
const before = createState();
const att = { id: "a", name: "f", path: "p", bytes: 1, kind: "file", state: "ready" };
const s = reduce(before, { type: "restore_draft", agent: "t2", draft: { ...dr(5), attachments: [att] }, identity: { created_ms: 5, app: null } });
assert.deepEqual(s.attachments.t2, [att]);
assert.deepEqual(before.drafts, {});
});
test("restore_draft without a draft changes nothing", () => {
const s = createState();
assert.equal(reduce(s, { type: "restore_draft", agent: "t2", draft: null, identity: { created_ms: 1, app: null } }), s);
});
test("draft action keeps the caret", () => {
const s = reduce(createState(), { type: "draft", agent: "a", text: "hi", caret: 1 });
assert.equal(s.carets.a, 1);
});
const q3 = () => ({ type: "restore", agent: "t2", items: [
{ id: "1", text: "a", state: "queued", createdAt: 1 },
{ id: "2", text: "b", state: "sending", createdAt: 2 },
{ id: "3", text: "c", state: "uncertain", createdAt: 3 },
] });
test("restore_draft keepCurrent: mismatch holds the queue, keeps what the user typed", () => {
let s = reduce(createState(), q3());
s = reduce(s, { type: "draft", agent: "t2", text: "nowy tekst", caret: 4 });
s = reduce(s, { type: "restore_draft", agent: "t2", draft: dr(5), identity: { created_ms: 9, app: null }, keepCurrent: true });
assert.equal(s.drafts.t2, "nowy tekst");
assert.equal(s.carets.t2, 4);
assert.deepEqual(s.queue.t2.map((i) => i.state), ["held", "held", "uncertain"]);
assert.equal(s.notice.text, "Szkic z poprzedniej sesji agenta");
});
test("restore_draft: unknown live identity holds the queue; notice only when saved identity was known", () => {
let s = reduce(createState(), q3());
const live = reduce(s, { type: "restore_draft", agent: "t2", draft: dr(5), identity: { created_ms: null, app: null } });
assert.deepEqual(live.queue.t2.map((i) => i.state), ["held", "held", "uncertain"]);
assert.equal(live.notice.text, "Szkic z poprzedniej sesji agenta");
const both = reduce(s, { type: "restore_draft", agent: "t2", draft: dr(null), identity: { created_ms: null, app: null } });
assert.deepEqual(both.queue.t2.map((i) => i.state), ["held", "held", "uncertain"]);
assert.equal(both.notice, null);
assert.equal(both.drafts.t2, "szkic");
});
test("restore_draft without a draft: unknown live identity still holds the queue", () => {
const s = reduce(createState(), q3());
const out = reduce(s, { type: "restore_draft", agent: "t2", draft: null, identity: { created_ms: null, app: null } });
assert.deepEqual(out.queue.t2.map((i) => i.state), ["held", "held", "uncertain"]);
assert.equal(out.notice, null);
});
test("hold_queue holds queued and sending, keeps uncertain", () => {
const s = reduce(reduce(createState(), q3()), { type: "hold_queue", agent: "t2" });
assert.deepEqual(s.queue.t2.map((i) => i.state), ["held", "held", "uncertain"]);
});
const mk = (...items) => reduce(createState(), { type: "restore", agent: "a", items });
const it = (id, state, extra = {}) => ({ id, text: id, state, createdAt: 1, ...extra });
const states = (s) => s.queue.a.map((i) => i.state);
test("countdown_start starts the first queued entry, uncertain untouched", () => {
let s = mk(it("u", "uncertain"), it("q1", "queued"), it("q2", "queued"));
s = reduce(s, { type: "countdown_start", agent: "a", id: "q1" });
assert.deepEqual(states(s), ["uncertain", "countdown", "queued"]);
assert.equal(s.queue.a[1].left, 3);
});
test("countdown_start ignores a non-first queued entry and a busy queue", () => {
const s = mk(it("q1", "queued"), it("q2", "queued"));
assert.equal(reduce(s, { type: "countdown_start", agent: "a", id: "q2" }), s);
const c = reduce(s, { type: "countdown_start", agent: "a", id: "q1" });
assert.equal(reduce(c, { type: "countdown_start", agent: "a", id: "q2" }), c);
const w = mk(it("s", "sending"), it("q", "queued"));
assert.equal(reduce(w, { type: "countdown_start", agent: "a", id: "q" }), w);
});
test("countdown_tick decrements and stops at 0", () => {
let s = reduce(mk(it("q", "queued")), { type: "countdown_start", agent: "a", id: "q" });
for (let i = 0; i < 5; i++) s = reduce(s, { type: "countdown_tick", agent: "a", id: "q" });
assert.equal(s.queue.a[0].left, 0);
assert.equal(s.queue.a[0].state, "countdown");
});
test("countdown_cancel -> held, countdown_reset -> queued, both drop left", () => {
const c = reduce(mk(it("q", "queued")), { type: "countdown_start", agent: "a", id: "q" });
const h = reduce(c, { type: "countdown_cancel", agent: "a", id: "q" });
assert.deepEqual(h.queue.a[0], { id: "q", text: "q", state: "held", createdAt: 1 });
const r = reduce(c, { type: "countdown_reset", agent: "a", id: "q" });
assert.deepEqual(r.queue.a[0], { id: "q", text: "q", state: "queued", createdAt: 1 });
});
test("hold_all holds queued and countdown; sending and uncertain stay", () => {
const s = mk(it("a1", "queued"), it("c", "countdown", { left: 2 }), it("s", "sending"), it("u", "uncertain"), it("h", "held"));
const out = reduce(s, { type: "hold_all", agent: "a" });
assert.deepEqual(states(out), ["held", "held", "sending", "uncertain", "held"]);
assert.equal(out.queue.a[1].left, undefined);
});
test("claim takes a countdown entry", () => {
const s = reduce(mk(it("c", "countdown", { left: 0 })), { type: "claim", agent: "a", id: "c" });
assert.equal(s.queue.a[0].state, "sending");
});
test("restart turns a countdown into held", () => {
const s = reduce(mk(it("c", "countdown", { left: 2 })), { type: "restart", agent: "a" });
assert.deepEqual(states(s), ["held"]);
});
test("restore_draft turns an unfinished attachment into an error", () => {
const att = (id, state) => ({ id, name: id, path: "p", bytes: 1, kind: "file", state });
const draft = { text: "t", caret: 1, attachments: [att("a", "adding"), att("b", "ready")], identity: { created_ms: 5, app: null }, savedAt: 1 };
const s = reduce(createState(), { type: "restore_draft", agent: "t2", draft, identity: { created_ms: 5, app: null } });
assert.equal(s.attachments.t2[0].state, "error");
assert.ok(s.attachments.t2[0].error.length > 0);
assert.equal(s.attachments.t2[1].state, "ready");
});
test("attachment states: adding -> ready with the remote path, or error; retry goes back to adding", () => {
let s = reduce(createState(), { type: "attach", agent: "a", file: { id: "1", name: "f", path: "p", bytes: 1, state: "adding" } });
assert.equal(s.attachments.a[0].state, "adding");
s = reduce(s, { type: "attachment_update", agent: "a", id: "1", patch: { state: "ready", remotePath: "/r/f" } });
assert.deepEqual([s.attachments.a[0].state, s.attachments.a[0].remotePath], ["ready", "/r/f"]);
s = reduce(s, { type: "attachment_update", agent: "a", id: "1", patch: { state: "error", error: "x" } });
assert.equal(s.attachments.a[0].error, "x");
s = reduce(s, { type: "attachment_update", agent: "a", id: "1", patch: { state: "adding" } });
assert.equal(s.attachments.a[0].error, undefined);
assert.equal(reduce(s, { type: "attachment_update", agent: "a", id: "zz", patch: { state: "ready" } }), s);
});
test("requeue takes an uncertain entry back to the queue too (the person's \"Ponów\")", () => {
const s = reduce(mk(it("u", "uncertain", { reason: "x" })), { type: "requeue", agent: "a", id: "u" });
assert.deepEqual(s.queue.a[0], { id: "u", text: "u", state: "queued", createdAt: 1 });
const q = mk(it("q", "queued"));
assert.equal(reduce(q, { type: "requeue", agent: "a", id: "q" }), q);
});