import { after, before, test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { CHANNELS } from "yt-dlp-transcript-common/testing/draft/corpus.mjs"; import { jsonReply, startFakeOllama, type FakeOllama } from "yt-dlp-transcript-common/testing/llm/fakeOllama.mjs"; import { enqueueReportDraft, parseReportDraftBody, type ReportDraftDeps } from "yt-dlp-transcript-common/controller/reportDraft"; import { parseVerdictReviewBody, reviewProposal } from "yt-dlp-transcript-common/controller/reportVerdicts"; import { sanitizeGeneration, type GenerationSettings } from "yt-dlp-transcript-common/lib/generationSettings"; import { getRegistry } from "yt-dlp-transcript-common/jobs/registry"; import { readVerdictProposals } from "yt-dlp-transcript-common/lib/report/verdictProposals"; import { articleFile } from "yt-dlp-transcript-common/lib/report/registry"; import type { Paths } from "yt-dlp-transcript-common/lib/paths"; // Run with: // pnpm -C editor exec tsx --test "app/api/ops/_verdicts.test.ts" // // Verdict proposals (release 29 M8) end to end in this process: report-draft // {task: "verdicts"} — the controller the route adapts — over the synthetic // fact-check (r02, r04, r07 unruled) in a temp article store, the synthetic // recordings' cues on disk and the fake ollama; then verdict-review's // controller (the editor's /api/ops/verdict-review adapts it). const RULED = path.join(path.dirname(new URL(import.meta.url).pathname), "../../../../common/testing/draft/reports/ruled.report.json"); let tmp: string; let ol: FakeOllama; let paths: Paths; let gen: GenerationSettings; let id: string; before(async () => { tmp = await mkdtemp(path.join(os.tmpdir(), "verdict-proposals-")); ol = await startFakeOllama(); const report = JSON.parse(await readFile(RULED, "utf8")); for (const s of report.sections) for (const c of s.claims) if (["r02", "r04", "r07"].includes(c.id)) delete c.verdict; id = report.id; await mkdir(path.join(tmp, "reports", id), { recursive: true }); await writeFile(path.join(tmp, "reports", id, "report.json"), JSON.stringify(report)); for (const ch of CHANNELS) { for (const r of ch.records) { const rec = path.join(tmp, "channels", ch.slug, "data", r.id); await mkdir(rec, { recursive: true }); await writeFile(path.join(rec, "transcript.cues.json"), JSON.stringify({ cues: r.cues })); } } paths = { jobsDir: path.join(tmp, ".jobs"), articlesDir: path.join(tmp, "reports"), exportIndexDir: path.join(tmp, ".export-index"), channelsDir: path.join(tmp, "channels"), generationUsageFile: path.join(tmp, ".generation", "usage.jsonl"), ollamaUrl: ol.url, } as Paths; gen = sanitizeGeneration({}); }); after(async () => { await ol.close(); await rm(tmp, { recursive: true, force: true }); }); const deps = (g: Partial = {}): ReportDraftDeps => ({ paths, settings: () => ({ generation: { ...gen, ...g } }), corpusTools: async () => { throw new Error("no corpus tools for verdicts"); } }) as unknown as ReportDraftDeps; test("the body: a verdicts job takes claims, never an iteration; claims are a verdicts job's", () => { assert.deepEqual(parseReportDraftBody({ task: "verdicts", article: "a", claims: ["c1", "c2"] }), { task: "verdicts", article: "a", claims: ["c1", "c2"] }); assert.throws(() => parseReportDraftBody({ task: "verdicts", article: "a", iteration: 2 }), /takes no iteration/); assert.throws(() => parseReportDraftBody({ task: "sweep", article: "a", question: "q", channels: ["c"], claims: ["c1"] }), /"claims" is a verdicts job's/); assert.throws(() => parseVerdictReviewBody({ article: "a", claim: "c1", decision: "maybe" }), /"approve" or "reject"/); assert.throws(() => parseVerdictReviewBody({ article: "a", claim: "c1", decision: "approve" }), /"verdict" is the proposed verdict you were shown/); assert.throws(() => parseVerdictReviewBody({ article: "a", claim: "c1", decision: "approve", verdict: "PARTLY" }), /"at" is the proposal's `at`/); }); test("refused until a model clears the bar; then a report-draft job on draft:local writes proposals, never a verdict; approve sets it", async () => { const none = await enqueueReportDraft({ task: "verdicts", article: id }, deps()); assert.equal(none.result.ok, false); assert.match((none.result as { error: string }).error, /models is empty/); const unlisted = await enqueueReportDraft({ task: "verdicts", article: id, model: "qwen3:8b" }, deps({ verdicts: { ...gen.verdicts, models: ["claude-opus-5-5"] } })); assert.match((unlisted.result as { error: string }).error, /has not cleared the verdict bench's bar/); const before = await readFile(articleFile(paths, id), "utf8"); for (let i = 0; i < 3; i++) ol.push(jsonReply({ parts: [{ part: "what she said", finding: "shown wrong" }], checkable: true, reason: "the window shows the opposite" })); const out = await enqueueReportDraft({ task: "verdicts", article: id }, deps({ verdicts: { ...gen.verdicts, models: ["qwen3:8b"], window: { mode: "turn", seconds: 30 } } })); assert.ok(out.result.ok, (out.result as { error?: string }).error); assert.equal("iteration" in out ? out.iteration : null, 0); const job = getRegistry().get(out.result.jobId)!; assert.equal(job.kind, "report-draft"); assert.equal(job.queueKey, "draft:local"); void out.result.stream.cancel(); const done = await out.result.done; const log = await readFile(job.logPath, "utf8"); assert.equal(done.status, "done", log); assert.match(log, /@@report-draft \{"article":"synth-ruled","iteration":0,"task":"verdicts","status":"done"/); assert.match(log, /unloaded qwen3:8b from ollama/); // The window is settings.generation.verdicts.window's. assert.match(log, /window the speaker's turn within 30 s/); assert.deepEqual(ol.unloads.at(-1), "qwen3:8b"); assert.equal(await readFile(articleFile(paths, id), "utf8"), before, "report.json untouched"); const f = (await readVerdictProposals(id, paths))!; assert.deepEqual(Object.keys(f.proposals).sort(), ["r02", "r04", "r07"]); // The job's mark is cleared when it ends; the window was the settings' (logged by the task). assert.equal(f.job, undefined); // A compare-and-set: the verdict and `at` the operator was shown. await assert.rejects(reviewProposal({ article: id, claim: "r04", decision: "approve", verdict: "PARTLY", at: f.proposals.r04!.at }, { paths }), (e: any) => e.status === 409); const p = await reviewProposal({ article: id, claim: "r04", decision: "approve", verdict: "CONTRADICTED", at: f.proposals.r04!.at }, { paths }); assert.equal(p.status, "approved"); const after = JSON.parse(await readFile(articleFile(paths, id), "utf8")); assert.equal(after.sections.flatMap((s: any) => s.claims).find((c: any) => c.id === "r04").verdict, "CONTRADICTED"); });