Files
herdrMario/tests/delivery.test.mjs
T
2026-11-19 11:22:30 +01:00

73 lines
2.8 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { decide, gateAfter, onRestart, shouldDrain, drain } from "../mod/ovis-composer/delivery.js";
test("decide idle/done -> send", () => {
assert.equal(decide("idle").kind, "send");
assert.equal(decide("done").kind, "send");
});
test("decide working -> queue, blocked -> send (reply to the agent)", () => {
assert.equal(decide("working").kind, "queue");
assert.equal(decide("blocked").kind, "send");
});
test("decide unknown status -> queue", () => {
assert.equal(decide("error").kind, "queue");
});
test("gateAfter needs a seen working turn", () => {
assert.equal(gateAfter("idle", false), false);
assert.equal(gateAfter("idle", true), true);
assert.equal(gateAfter("done", true), true);
assert.equal(gateAfter("working", true), false);
});
test("onRestart sending -> uncertain, others untouched", () => {
const out = onRestart([
{ id: "1", state: "sending" },
{ id: "2", state: "queued" },
{ id: "3", state: "held" },
]);
assert.deepEqual(out.map((i) => i.state), ["uncertain", "queued", "held"]);
});
test("onRestart tolerates non-array", () => {
assert.deepEqual(onRestart(null), []);
});
test("shouldDrain: only working|blocked -> idle|done, not after a stop by the composer", () => {
const all = ["idle", "working", "blocked", "done"];
for (const prev of all) {
for (const next of all) {
const want = (prev === "working" || prev === "blocked") && (next === "idle" || next === "done");
assert.equal(shouldDrain(prev, next, false), want, `${prev}->${next}`);
assert.equal(shouldDrain(prev, next, true), false, `${prev}->${next} stopped`);
}
}
});
test("onRestart: countdown -> held without left, sending -> uncertain", () => {
const out = onRestart([
{ id: "1", state: "countdown", left: 2 },
{ id: "2", state: "sending" },
]);
assert.deepEqual(out, [{ id: "1", state: "held" }, { id: "2", state: "uncertain" }]);
});
test("drain sends an entry whose countdown ran out", async () => {
const { createState, reduce } = await import("../mod/ovis-composer/state.js");
let s = reduce(createState(), { type: "queue", agent: "t2", item: { id: "1", text: "a", createdAt: 1 } });
s = reduce(s, { type: "countdown_start", agent: "t2", id: "1" });
const calls = [];
const ctx = { agent: "t2", terminal: "t2", status: () => "idle", gate: () => true,
hc: async (args) => (calls.push(args), { code: 0, stdout: "{}", stderr: "" }) };
let r = await drain(ctx, s);
assert.equal(r.sent, false); // left is still 3
for (let i = 0; i < 3; i++) s = reduce(s, { type: "countdown_tick", agent: "t2", id: "1" });
r = await drain(ctx, s);
assert.equal(r.sent, true);
assert.equal(calls.length, 1);
assert.deepEqual(r.state.queue.t2, []);
});