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); }); 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"]); });