import { test } from "node:test"; import assert from "node:assert/strict"; import { draftTargetFromLog, editorProgress, editorStateText, foldRunning, isLane, moreText, ownJobLabel, queueText, rankOf, spendFromLog, } from "./running.mjs"; test("rank: generation first, then what umtool or a research run asked for, then the rest", () => { const research = new Set(["acq1"]); assert.equal(rankOf({ id: "a", kind: "report-draft" }, research), 0); assert.equal(rankOf({ id: "b", kind: "tts-file" }, research), 0); assert.equal(rankOf({ id: "acq1", kind: "import-archive-org" }, research), 1); assert.equal(rankOf({ id: "c", kind: "fetch-window", detail: "umtool · reports/x/c01" }, research), 1); assert.equal(rankOf({ id: "d", kind: "fetch-window", detail: "mcp · x" }, research), 2); assert.equal(rankOf({ id: "e", kind: "download-missing" }, research), 2); assert.ok(isLane("auto-download")); assert.ok(!isLane("auto-download-unit")); }); test("spend and target from a draft's log", () => { const log = [ "sweep of api-shondo i2 on claude-opus-5-5 (the Claude API, cap $5)", "turn 1: search_transcripts — ~$0.04", "turn 2: get_transcripts, get_transcripts — ~$0.41", ]; assert.equal(spendFromLog(log), 0.41); assert.equal(spendFromLog(["no money here"]), null); assert.deepEqual(draftTargetFromLog(log), { task: "sweep", article: "api-shondo", n: 2 }); assert.equal(draftTargetFromLog(["corpus local"]), null); }); test("progress and queue in words", () => { assert.deepEqual(editorProgress({ metric: "turns", initial: 0, current: 3, target: 12 }), { fraction: 0.25, text: "3/12 turns" }); assert.deepEqual(editorProgress({ metric: "downloads", initial: 4, target: 8 }), { fraction: 0.5, text: "4/8 downloads" }); assert.equal(editorProgress({ metric: "x", initial: 0, target: 0 }), null); assert.equal(editorProgress(undefined), null); assert.equal(queueText({ position: 1 }), "queued · next"); assert.equal(queueText({ position: 4 }), "queued · 3 ahead"); assert.equal(queueText(undefined), "queued"); }); test("umtool's own kinds in words", () => { assert.deepEqual(ownJobLabel("chain zeta i2"), { label: "Make the video", target: "zeta i2", article: "zeta", n: 2, generation: true }); assert.equal(ownJobLabel("draft-all zeta i3").label, "Draft everything"); assert.deepEqual(ownJobLabel("build reports/x (final)", "reports/x"), { label: "Build", target: "reports/x", article: null, n: null, generation: false }); assert.equal(ownJobLabel("check-sources (2 projects)").label, "Check sources"); }); test("fold: running rows first (umtool's own on top), then queued generation; the rest is counted", () => { const at = (n) => 1000 + n; const items = [ { id: "lane1", state: "running", lane: true, rank: 2, startedAt: at(0) }, { id: "lane2", state: "running", lane: true, rank: 2, startedAt: at(0) }, { id: "sync", state: "running", rank: 2, startedAt: at(1) }, { id: "draft", state: "running", rank: 0, startedAt: at(5) }, { id: "own", state: "running", own: true, rank: 0, startedAt: at(9) }, { id: "q-draft", state: "queued", rank: 0, startedAt: at(2) }, { id: "q-dl1", state: "queued", rank: 2, startedAt: at(3) }, { id: "q-dl2", state: "queued", rank: 2, startedAt: at(4) }, { id: "done", state: "done", rank: 0, startedAt: at(0) }, ]; const f = foldRunning(items, 5); assert.deepEqual(f.rows.map((r) => r.id), ["own", "draft", "sync", "q-draft"]); assert.deepEqual(f.more, { running: 0, queued: 2, lanes: 2 }); assert.equal(f.running, 3); assert.equal(f.queued, 3); assert.equal(moreText(f.more), "+ 2 queued · 2 lanes"); const tight = foldRunning(items, 2); assert.deepEqual(tight.rows.map((r) => r.id), ["own", "draft"]); assert.deepEqual(tight.more, { running: 1, queued: 3, lanes: 2 }); assert.equal(moreText(tight.more), "+ 1 running · 3 queued · 2 lanes"); assert.equal(moreText({ running: 0, queued: 0, lanes: 1 }), "+ 1 lane"); assert.equal(moreText({ running: 0, queued: 0, lanes: 0 }), null); }); test("the three ways the editor's jobs are missing are three sentences", () => { const said = ["unreachable", "editor-token", "no-token", "denied", "refused"].map(editorStateText); assert.equal(new Set(said).size, said.length); assert.equal(editorStateText("unreachable"), "Editor unreachable"); assert.equal(editorStateText("editor-token"), "Editor's WORKER_TOKEN not set"); assert.equal(editorStateText(null), null); }); test("a recurring report's tick (R5) is a generation row; its first log line names its schedule", async () => { const { rankOf, tickTargetFromLog, spendFromLog } = await import("./running.mjs"); assert.equal(rankOf({ id: "t1", kind: "report-tick" }), 0); const log = ["series cohost: ticked now", "window 2026-10-07 … 2026-10-14: 3 recordings match", "sweep cohost-2026-10-14 i1: ~$0.42 so far"]; assert.deepEqual(tickTargetFromLog(log), { shape: "series", id: "cohost" }); assert.deepEqual(tickTargetFromLog(["living the-shop: the 2026-10-15T10:00:00.000Z window"]), { shape: "living", id: "the-shop" }); assert.equal(tickTargetFromLog(["sweep of x i1 on m"]), null); assert.equal(spendFromLog(log), 0.42); });