Watch
0
0
Fork
You've already forked hyperhive
0
hyperhive/frontend/packages/swarm-ui/test/bash-tasks.test.mjs
iris 5de20f31dd swarm: list an agent's bash tasks from their published status
GET /api/agents/{name}/bash-tasks reads the agent's keys of the bash-task-status
bucket and reports each task's state, command and times, with a live task whose
daemon stopped reporting as stale. The swarm UI shows them in a background tasks
section of the agent card, running ones first. The task output stays unread.
2026-11-15 19:51:37 +01:00

69 lines
2.3 KiB
JavaScript

// What the bash task list means: which tasks are live, the words for a state,
// the order, and the count. Run by `npm test`, which imports the `.ts` module
// as it is.
import assert from "node:assert/strict";
import { test } from "node:test";
import {
isLive,
runningTasksLabel,
sortTasks,
stateWord,
} from "../src/agent/terminal/bash-tasks.ts";
const task = (name, state, created, exit = null) => ({
task: name,
state,
cmd: "make",
created_unix: created,
since_unix: created + 1,
exit,
});
test("only a queued or running task, still reporting, is live", () => {
assert.equal(isLive(task("a", "pending", 1)), true);
assert.equal(isLive(task("a", "running", 1)), true);
for (const s of ["stale", "done", "timed_out", "interrupted", "killed"]) {
assert.equal(isLive(task("a", s, 1)), false, s);
}
});
test("the state word says what the age counts from", () => {
assert.equal(stateWord(task("a", "pending", 1)), "queued");
assert.equal(stateWord(task("a", "running", 1)), "started");
assert.equal(stateWord(task("a", "stale", 1)), "stopped reporting");
assert.equal(stateWord(task("a", "done", 1, 0)), "finished");
assert.equal(stateWord(task("a", "done", 1)), "finished");
assert.equal(stateWord(task("a", "done", 1, 2)), "exited 2");
assert.equal(stateWord(task("a", "timed_out", 1)), "timed out");
assert.equal(stateWord(task("a", "interrupted", 1)), "interrupted");
assert.equal(stateWord(task("a", "killed", 1)), "killed");
});
test("live tasks come first, each group newest first, ties by id", () => {
const list = [
task("old-done", "done", 10),
task("new-done", "done", 50),
task("old-run", "running", 20),
task("new-run", "running", 60),
task("b", "running", 40),
task("a", "running", 40),
];
assert.deepEqual(
sortTasks(list).map((t) => t.task),
["new-run", "a", "b", "old-run", "new-done", "old-done"],
);
});
test("the count names the live tasks only", () => {
assert.equal(runningTasksLabel([]), "0 running tasks");
assert.equal(runningTasksLabel([task("a", "running", 1)]), "1 running task");
assert.equal(
runningTasksLabel([
task("a", "running", 1),
task("b", "pending", 1),
task("c", "done", 1),
task("d", "stale", 1),
]),
"2 running tasks",
);
});