import { test } from "node:test"; import assert from "node:assert/strict"; import { foldModelLog, type ModelCallRow, type ModelLogRunRow } from "./callLog"; import { LIST_ITEMS, PAGE_ROWS, modelLogView, viewQueryOf } from "./callLogView"; // Run with: pnpm -C common exec tsx --test lib/llm/callLogView.test.ts // // The model log as the panel shows it (demo epic D3): runs picked by task, // job, number, time or newest; groups by phase a page at a time; heads and a // few items only in the list; counts per filter. const run = (n: number, task: string, at: string, jobId?: string): ModelLogRunRow => ({ v: 1, t: "run", run: n, at, task, ...(jobId ? { jobId } : {}) }); const call = (id: string, phase: string, outcome: ModelCallRow["decision"]["outcome"], extra: Partial = {}): ModelCallRow => ({ v: 1, t: "call", id, run: Number(id.split(".")[0]), at: "2026-11-11T10:00:00.000Z", task: "sweep", phase, model: "qwen3:8b", route: "local", promptChars: 10, promptHead: "q", answerChars: 1, answerHead: "a", seconds: 1, decision: { outcome, why: "" }, ...extra, }); function log() { const rows: object[] = [run(1, "sweep", "2026-11-11T10:00:00.000Z", "j1"), run(2, "narrate", "2026-11-11T11:00:00.000Z", "j2"), run(3, "sweep", "2026-11-11T12:00:00.000Z", "j3")]; rows.push(call("1.1", "terms", "kept")); for (let i = 2; i <= 60; i++) rows.push(call(`1.${i}`, "reading", i % 3 ? "kept" : "dropped", { items: Array.from({ length: 8 }, (_, k) => ({ text: `q${k}`, outcome: "kept" as const, why: "" })) })); rows.push(call("1.61", "grouping", "kept")); rows.push(call("2.1", "draft", "rewritten", { task: "narrate" })); rows.push(call("3.1", "terms", "kept")); return foldModelLog(rows.map((r) => JSON.stringify(r)).join("\n")); } test("every run, groups in the sweep's order, a page each; heads and a few items in the list", () => { const v = modelLogView(log()); assert.equal(v.run, null); assert.deepEqual(v.groups.map((g) => [g.phase, g.calls]), [["terms", 2], ["reading", 59], ["grouping", 1], ["draft", 1]]); const reading = v.groups[1]!; assert.equal(reading.rows.length, PAGE_ROWS); assert.equal(reading.more, 59 - PAGE_ROWS); assert.equal(reading.rows[0]!.items!.length, LIST_ITEMS); assert.equal(reading.rows[0]!.itemsTotal, 8); // A call is in each filter its decision or one of its items is in: every reading call kept items. assert.deepEqual(v.counts, { all: 63, kept: 62, dropped: 20, rewritten: 1, "n/a": 0 }); }); test("a phase's next page; the outcome filter", () => { const page = modelLogView(log(), { phase: "reading", offset: 50 }); assert.deepEqual(page.groups.map((g) => [g.phase, g.offset, g.rows.length, g.more]), [["reading", 50, 9, 0]]); const dropped = modelLogView(log(), { outcome: "dropped" }); assert.deepEqual(dropped.groups.map((g) => [g.phase, g.calls]), [["reading", 20]]); assert.equal(dropped.counts.all, 63, "the counts are before the filter"); }); test("the run: a task's newest, a job's, one by number, the one nearest a time; a job not yet run shows none", () => { assert.equal(modelLogView(log(), { task: "sweep", latest: true }).run, 3); assert.equal(modelLogView(log(), { task: "narrate", latest: true }).summary.calls, 1); assert.equal(modelLogView(log(), { jobId: "j1" }).summary.calls, 61); assert.equal(modelLogView(log(), { run: 3 }).summary.calls, 1); assert.equal(modelLogView(log(), { near: "2026-11-11T10:00:02.000Z" }).run, 1); assert.equal(modelLogView(log(), { jobId: "j-later" }).summary.calls, 0); }); test("the query from a URL: whole numbers, an outcome by name, an ISO time", () => { assert.deepEqual(viewQueryOf(new URLSearchParams("task=sweep&latest=1&outcome=dropped&phase=reading&offset=25&limit=10")), { task: "sweep", latest: true, outcome: "dropped", phase: "reading", offset: 25, limit: 10 }); assert.throws(() => viewQueryOf(new URLSearchParams("outcome=maybe")), /"outcome" is one of/); assert.throws(() => viewQueryOf(new URLSearchParams("offset=-1")), /whole number/); assert.throws(() => viewQueryOf(new URLSearchParams("near=yesterday")), /ISO time/); });