Files
herdrMario/tests/state.test.mjs
T
2026-11-19 11:14:54 +01:00

102 lines
4.3 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { createState, reduce, isSending } from "../mod/ovis-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);
});