61 lines
2.4 KiB
JavaScript
61 lines
2.4 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);
|
|
});
|