108 lines
4.3 KiB
JavaScript
108 lines
4.3 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { decide, gateAfter, onRestart, shouldDrain, shouldStartCountdown, 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, []);
|
|
});
|
|
|
|
const sc = (over = {}) => ({
|
|
status: "idle", queue: [{ id: "1", state: "queued" }], gateOpen: true, stopped: false, ...over,
|
|
});
|
|
|
|
test("shouldStartCountdown: idle/done + queued + open gate", () => {
|
|
assert.equal(shouldStartCountdown(sc()), true);
|
|
assert.equal(shouldStartCountdown(sc({ status: "done" })), true);
|
|
});
|
|
|
|
test("shouldStartCountdown: not while the agent works, is blocked or unknown", () => {
|
|
for (const status of ["working", "blocked", "error"]) {
|
|
assert.equal(shouldStartCountdown(sc({ status })), false, status);
|
|
}
|
|
});
|
|
|
|
test("shouldStartCountdown: held does not start, uncertain in front does not block", () => {
|
|
assert.equal(shouldStartCountdown(sc({ queue: [{ state: "held" }] })), false);
|
|
assert.equal(shouldStartCountdown(sc({ queue: [{ state: "uncertain" }, { state: "queued" }] })), true);
|
|
assert.equal(shouldStartCountdown(sc({ queue: [] })), false);
|
|
});
|
|
|
|
test("shouldStartCountdown: nothing starts while another entry counts or sends", () => {
|
|
assert.equal(shouldStartCountdown(sc({ queue: [{ state: "countdown" }, { state: "queued" }] })), false);
|
|
assert.equal(shouldStartCountdown(sc({ queue: [{ state: "sending" }, { state: "queued" }] })), false);
|
|
});
|
|
|
|
test("shouldStartCountdown: stopped or closed gate blocks", () => {
|
|
assert.equal(shouldStartCountdown(sc({ stopped: true })), false);
|
|
assert.equal(shouldStartCountdown(sc({ gateOpen: false })), false);
|
|
});
|
|
|
|
test("shouldStartCountdown tolerates a non-array queue", () => {
|
|
assert.equal(shouldStartCountdown(sc({ queue: null })), false);
|
|
});
|