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