import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, mkdir, readFile, rm, stat, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { CHANNELS } from "../testing/draft/corpus.mjs"; import type { Paths } from "../lib/paths"; import { benchMain, benchOptionsOf } from "./reports-bench-verdicts"; const RULED = path.join(import.meta.dirname, "../testing/draft/reports/ruled.report.json"); // A temp corpus: the ruled fact-check in the article store, and the synthetic // recordings' cues on disk, as `diskCuesOf` reads them. async function corpus() { const dir = await mkdtemp(path.join(os.tmpdir(), "bench-cli-")); const report = JSON.parse(await readFile(RULED, "utf8")); await mkdir(path.join(dir, "reports", report.id), { recursive: true }); await writeFile(path.join(dir, "reports", report.id, "report.json"), JSON.stringify(report)); for (const ch of CHANNELS) { for (const r of ch.records) { const rec = path.join(dir, "channels", ch.slug, "data", r.id); await mkdir(rec, { recursive: true }); await writeFile(path.join(rec, "transcript.cues.json"), JSON.stringify({ cues: r.cues })); } } const paths = { articlesDir: path.join(dir, "reports"), exportIndexDir: path.join(dir, ".export-index"), channelsDir: path.join(dir, "channels"), generationUsageFile: path.join(dir, ".generation", "usage.jsonl"), } as Paths; return { dir, paths, done: () => rm(dir, { recursive: true, force: true }) }; } function capture() { const logs: string[] = []; const errors: string[] = []; return { out: { log: (s: string) => logs.push(s), error: (s: string) => errors.push(s) }, logs, errors }; } test("the flags: lists split, numbers read, the options judged before anything runs", () => { assert.deepEqual(benchOptionsOf({ models: "claude-sonnet-5-5, qwen3:8b", report: "a,b", window: "30", "max-usd": "2.5", limit: "4", run: "vb-x" }), { models: ["claude-sonnet-5-5", "qwen3:8b"], reports: ["a", "b"], windowS: 30, maxUsd: 2.5, limit: 4, runId: "vb-x", }); assert.throws(() => benchOptionsOf({}), /at least one model/); assert.throws(() => benchOptionsOf({ models: "m", window: "soon" }), /--window takes a number/); assert.throws(() => benchOptionsOf({ models: "m", run: "Not An Id" }), /--run is a run id/); }); test("--dry: the items over the local corpus's cues, the skipped claims and the estimate; nothing asked, nothing written", async () => { const c = await corpus(); try { const cap = capture(); let asked = 0; const code = await benchMain({ models: "claude-sonnet-5-5,claude-opus-5-5,qwen3:8b", dry: true }, { paths: c.paths, runOps: async () => ++asked, out: cap.out }); assert.equal(code, 0); assert.equal(asked, 0); assert.match(cap.logs[0]!, /^7 items from 1 fact-check\(s\), a 60 s window; 1 claims skipped, 0 citations dropped$/); assert.match(cap.logs[1]!, /^ {2}prompt version \d+, items [0-9a-f]{16}$/); assert.ok(cap.logs.includes(" skipped 1: no citation")); assert.ok(cap.logs.some((l) => /^ {2}synth-ruled\/r04 {2}CONTRADICTED {2}1 quote\(s\), \d+ cues$/.test(l))); assert.ok(cap.logs.some((l) => /^ {2}qwen3:8b \(local\): 7 calls, .*~\$0\.0000$/.test(l))); // No claim text without --json. assert.ok(!cap.logs.join("\n").includes("The article says")); await assert.rejects(stat(path.join(c.dir, ".generation")), /ENOENT/); } finally { await c.done(); } }); test("a run goes through the editor (pnpm ops bench-verdicts); --here refuses a local model", async () => { const c = await corpus(); try { const seen: string[][] = []; const code = await benchMain( { models: "claude-sonnet-5-5,qwen3:8b", "max-usd": "5", run: "vb-real", wait: true }, { paths: c.paths, runOps: async (argv) => (seen.push(argv), 0), out: capture().out }, ); assert.equal(code, 0); assert.deepEqual(seen, [["bench-verdicts", "--json", JSON.stringify({ models: ["claude-sonnet-5-5", "qwen3:8b"], run: "vb-real", maxUsd: 5 }), "--wait"]]); // V1: the window mode, the stratified limit and items-from ride along. seen.length = 0; assert.equal(await benchMain({ models: "qwen3:8b", "window-mode": "turn", limit: "20" }, { paths: c.paths, runOps: async (argv) => (seen.push(argv), 0), out: capture().out }), 0); assert.deepEqual(JSON.parse(seen[0]![2]!), { models: ["qwen3:8b"], limit: 20, windowMode: "turn" }); seen.length = 0; assert.equal(await benchMain({ models: "qwen3:8b", "items-from": "vb-r28", run: "vb-r29-q1" }, { paths: c.paths, runOps: async (argv) => (seen.push(argv), 0), out: capture().out }), 0); assert.deepEqual(JSON.parse(seen[0]![2]!), { models: ["qwen3:8b"], run: "vb-r29-q1", itemsFrom: "vb-r28" }); const bad = capture(); assert.equal(await benchMain({ models: "qwen3:8b", "items-from": "vb-r28", limit: "5" }, { paths: c.paths, runOps: async () => 0, out: bad.out }), 2); assert.match(bad.errors.join("\n"), /items as they are: drop the limit/); assert.equal(await benchMain({ models: "qwen3:8b", "window-mode": "sentence" }, { paths: c.paths, runOps: async () => 0, out: capture().out }), 2); const cap = capture(); assert.equal(await benchMain({ models: "qwen3:8b", here: true }, { paths: c.paths, runOps: async () => 0, out: cap.out }), 2); assert.match(cap.errors.join("\n"), /qwen3:8b runs on the GPU, which only the editor arbitrates/); } finally { await c.done(); } });