import assert from "node:assert/strict"; import { test } from "node:test"; import type { JobSnapshot } from "../jobs.ts"; import { projectJobBoard } from "../panel.ts"; import { JobBoard } from "../board.ts"; import { applyProgress, createProgress } from "../display.ts"; const job = (extra: Partial = {}): JobSnapshot => ({ id: "d0001", kind: "review", model: "hosted/reviewer", task: "Review card layout", status: "running", local: false, failed: false, background: true, activity: [], ...extra }); const limits = { maxLocalConcurrent: 1 }; test("panel projection carries full card identity and task while excluding raw thinking and inactivity clocks", () => { const progress = createProgress(); applyProgress(progress, { mark: "…", name: "thinking", args: "PRIVATE first" }); const initial = job({ thinking: progress.thinking, quietForMs: 10, activity: [{ name: "thinking", mark: "…", args: "SECRET" }, { name: "read", mark: "✓", args: "file.ts", id: "tool-1" }] }); const first = projectJobBoard([initial], limits)!; assert.equal(first.cards[0].model, "hosted/reviewer"); assert.equal(first.cards[0].task, "Review card layout"); assert.equal(first.cards[0].phase, "thinking"); assert.equal(first.cards[0].jobId, "d0001"); assert.doesNotMatch(JSON.stringify(first), /PRIVATE|SECRET|quietForMs|tool-1/); applyProgress(progress, { mark: "…", name: "thinking", args: "PRIVATE second" }); assert.deepEqual(projectJobBoard([{ ...initial, thinking: progress.thinking, quietForMs: 999 }], limits), first); initial.activity[1].args = "mutated"; assert.equal((first.cards[0].activity as any[])[0].args, "file.ts", "projection owns its tool snapshots"); }); test("panel preserves active acceptance order, queue/wrap/warnings and local rates, excluding terminal jobs", () => { const state = projectJobBoard([job(), job({ id: "d0002", status: "queued", reason: "gpu", local: true, wrapped: true }), job({ id: "d0003", status: "done" }), job({ id: "d0004", local: true, tg: "tg 40/s", recordingError: "Recording incomplete" })], limits)!; assert.deepEqual(state.cards.map((d) => d.jobId), ["d0001", "d0002", "d0004"]); assert.equal(state.cards[1].reason, "gpu"); assert.equal(state.cards[1].wrapped, true); assert.equal(state.cards[2].tg, "tg 40/s"); assert.equal(state.cards[2].recordingError, "Recording incomplete"); assert.equal(state.summary, "delegate 2 run 1 wait local 1/1"); assert.equal(projectJobBoard([job({ status: "done" })], limits), undefined); assert.equal(projectJobBoard([job({ tg: "tg 90/s" })], limits)!.cards[0].tg, undefined); }); test("panel clock counts whole seconds from queue/start time and resets when promoted", () => { const queued = job({ status: "queued", queuedAt: 1000, reasoning: "high" }); const project = (extra: Partial = {}, now = 3999) => projectJobBoard([{ ...queued, ...extra }], limits, now)!.cards[0]; assert.equal(project().elapsedMs, 2000); assert.equal(project({}, 3000).elapsedMs, 2000, "subsecond changes do not repaint"); assert.equal(project({ status: "running", startedAt: 3000 }).elapsedMs, 0); assert.equal(project({ status: "running", startedAt: 3000 }, 195000).elapsedMs, 192000); assert.equal(project({}, 0).elapsedMs, 0, "backward clock changes cannot show negative time"); assert.equal(project({ queuedAt: undefined }).elapsedMs, undefined); assert.equal(project({ status: "running", startedAt: NaN }).elapsedMs, undefined); assert.equal(project().reasoning, "high"); assert.equal(projectJobBoard([queued], limits)!.cards[0].elapsedMs, undefined, "non-TUI projections omit clocks"); }); test("resource waiting projects its group without copying lease handles or unrendered capacity data", () => { const initial = job({ status: "queued", reason: "resource", resource: { key: "shared-server", capacity: 1, state: "waiting" } }); const state = projectJobBoard([initial], limits)!; assert.equal(state.cards[0].reason, "resource"); assert.deepEqual(state.cards[0].resource, { key: "shared-server" }); initial.resource!.key = "changed"; assert.deepEqual(state.cards[0].resource, { key: "shared-server" }); assert.doesNotMatch(JSON.stringify(state), /capacity|inherited|descriptor/); }); test("equivalent full-card snapshots reuse the mounted component; expansion is read on each render", () => { let component: any, expanded = false, paints = 0, mounts = 0; const ui: any = { getToolsExpanded: () => expanded, setWidget(_key: string, factory: any) { if (factory) { mounts++; component = factory({ terminal: { rows: 24 }, requestRender() { paints++; } }); } } }; const board = new JobBoard((state: NonNullable>, _width, maxRows, _theme, expanded) => [`${state.cards[0].task} · ${expanded} · ${maxRows}`]); const progress = createProgress(); for (const args of ["one", "two"]) { applyProgress(progress, { mark: "…", name: "thinking", args }); board.paint(ui, "tui", projectJobBoard([job({ thinking: progress.thinking })], limits)); } assert.equal(mounts, 1); assert.equal(paints, 0); assert.deepEqual(component.render(80), ["Review card layout · false · 12"]); expanded = true; assert.deepEqual(component.render(80), ["Review card layout · true · 12"]); board.paint(ui, "tui", projectJobBoard([job({ wrapped: true })], limits)); assert.equal(paints, 1); board.close(ui); });