import { after, before, test } from "node:test"; import assert from "node:assert/strict"; import { chmod, mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { createCorpusTools } from "yt-dlp-transcript-mcp/callTool"; import { ORIGIN, writeDraftCorpus } from "yt-dlp-transcript-common/testing/draft/corpus.mjs"; import { FAKE_KEY, startFakeAnthropic, textTurn, type FakeAnthropic } from "yt-dlp-transcript-common/testing/llm/fakeAnthropic.mjs"; import { startFakeOllama, type FakeOllama } from "yt-dlp-transcript-common/testing/llm/fakeOllama.mjs"; import { enqueueReportDraft, parseReportDraftBody } from "yt-dlp-transcript-common/controller/reportDraft"; import { sanitizeGeneration, type GenerationSettings } from "yt-dlp-transcript-common/lib/generationSettings"; import { getRegistry } from "yt-dlp-transcript-common/jobs/registry"; import { dayUsage, readGenerationUsage, usageModelsOf } from "yt-dlp-transcript-common/controller/generationUsage"; import { modelLogFilesIn, readModelLog } from "yt-dlp-transcript-common/lib/llm/callLog-server"; import { createIteration, iterationDir, projectOf, readIteration, readIterationFile, updateIteration, writeIterationFile } from "yt-dlp-transcript-common/lib/report/iterations"; import type { Paths } from "yt-dlp-transcript-common/lib/paths"; // Run with: // pnpm -C editor exec tsx --test "app/api/ops/_reportDraft.test.ts" // // The `report-draft` job (release 27 S6) end to end in this process: the // controller the route adapts, the MCP's corpus tools over the synthetic // corpus, the fake Anthropic and ollama servers, a temp store, jobs dir and // usage ledger. The key is a mode-600 file holding the fake's key. const DRAFT = path.join(path.dirname(new URL(import.meta.url).pathname), "../../../../common/testing/draft"); let tmp: string; let api: FakeAnthropic; let ol: FakeOllama; let source: string; let paths: Paths; let gen: GenerationSettings; before(async () => { tmp = await mkdtemp(path.join(os.tmpdir(), "report-draft-")); source = `local:${await writeDraftCorpus(path.join(tmp, "corpus"))}`; api = await startFakeAnthropic(); ol = await startFakeOllama(); const keyFile = path.join(tmp, "anthropic.env"); await writeFile(keyFile, `ANTHROPIC_API_KEY=${FAKE_KEY}\n`); await chmod(keyFile, 0o600); await mkdir(path.join(tmp, "channels"), { recursive: true }); paths = { jobsDir: path.join(tmp, ".jobs"), articlesDir: path.join(tmp, "reports"), exportIndexDir: path.join(tmp, ".export-index"), exportBuildsDir: path.join(tmp, ".export-builds"), sitesDir: path.join(tmp, "sites"), channelsDir: path.join(tmp, "channels"), generationUsageFile: path.join(tmp, ".generation", "usage.jsonl"), ollamaUrl: ol.url, } as Paths; gen = sanitizeGeneration({ apiKeyFile: keyFile, anthropicBaseUrl: api.url, citationOrigin: ORIGIN, projectsRoot: path.join(tmp, "projects"), caps: { sweep: 2, daily: 20, max: 5 }, }); }); after(async () => { await api.close(); await ol.close(); await rm(tmp, { recursive: true, force: true }); }); const deps = (g: Partial = {}, memAvailableMb: number | null | (() => number | null) = 8000) => ({ paths, corpusTools: (s: string) => createCorpusTools({ source: s }), settings: () => ({ generation: { ...gen, ...g } }), // The local model's memory floor reads this, not the machine's, and waits // milliseconds, not minutes. memAvailableMb: async () => (typeof memAvailableMb === "function" ? memAvailableMb() : memAvailableMb), memoryWait: { pollMs: 10, maxWaitMs: 300 }, }); // ARCHILYZER_LOCAL_ONLY for one test's body (demo epic D5), restored after. async function localOnly(body: () => Promise): Promise { const was = process.env.ARCHILYZER_LOCAL_ONLY; process.env.ARCHILYZER_LOCAL_ONLY = "1"; try { return await body(); } finally { if (was === undefined) delete process.env.ARCHILYZER_LOCAL_ONLY; else process.env.ARCHILYZER_LOCAL_ONLY = was; } } const run = async (name: string) => JSON.parse(await readFile(path.join(DRAFT, "runs", `${name}.json`), "utf8")); test("the body: each task's needs, and the refusals in words", () => { assert.throws(() => parseReportDraftBody({ task: "nope", article: "a" }), /"task" is one of sweep, revise-report, revise-video, narrate/); assert.throws(() => parseReportDraftBody({ task: "sweep", article: "a", channels: ["x"] }), /needs its question/); assert.throws(() => parseReportDraftBody({ task: "revise-report", article: "a", iteration: 1 }), /needs feedback/); assert.throws(() => parseReportDraftBody({ task: "narrate", article: "a" }), /names the iteration it voices/); assert.throws(() => parseReportDraftBody({ task: "sweep", article: "a", question: "q", channels: "x", source: "ftp:x" }), /corpus handle/); assert.deepEqual(parseReportDraftBody({ task: "sweep", article: "a", question: " q ", channels: "x, y" }), { task: "sweep", article: "a", question: "q", channels: ["x", "y"] }); }); test("a media draft (release 28): a step body as narrate's — its iteration, no copy; entries a step's; an unknown task refused", () => { for (const task of ["narrate", "cut", "onscreen", "teaser", "flips", "threads", "youtube"]) { assert.throws(() => parseReportDraftBody({ task, article: "a" }), task === "narrate" ? /names the iteration it voices/ : new RegExp(`a ${task} draft names the iteration it drafts on`)); assert.throws(() => parseReportDraftBody({ task, article: "a", iteration: 1, copyOf: 1 }), /is a step, not an iteration: it has no copy/); assert.deepEqual(parseReportDraftBody({ task, article: "a", iteration: 2, model: "qwen3:8b", entries: "c01, c02", force: true }), { task, article: "a", iteration: 2, model: "qwen3:8b", entries: ["c01", "c02"], force: true, }); } assert.throws(() => parseReportDraftBody({ task: "revise-video", article: "a", iteration: 1, feedback: "f", entries: ["c01"] }), /"entries" is a media draft's/); assert.throws(() => parseReportDraftBody({ task: "cuts", article: "a", iteration: 1 }), /"task" is one of .*cut, onscreen, teaser, flips, threads, youtube/); }); test("extractorModel (release 28 F2): a sweep's, a Claude model the price table knows", () => { const sweep = { task: "sweep", article: "a", question: "q", channels: ["x"] }; assert.equal(parseReportDraftBody({ ...sweep, extractorModel: "claude-haiku-5-5" }).extractorModel, "claude-haiku-5-5"); assert.equal(parseReportDraftBody({ ...sweep, extractorModel: "claude-haiku-4-5-20251001" }).extractorModel, "claude-haiku-4-5-20251001"); assert.throws(() => parseReportDraftBody({ ...sweep, extractorModel: "qwen3:8b" }), /"extractorModel" is a Claude model id/); assert.throws(() => parseReportDraftBody({ ...sweep, extractorModel: "claude-nonsense-9" }), /"extractorModel" is a Claude model id/); assert.throws(() => parseReportDraftBody({ task: "revise-report", article: "a", iteration: 1, feedback: "f", extractorModel: "claude-haiku-5-5" }), /"extractorModel" is a sweep's/); }); test("refused before any iteration: a task routed to Claude Code with no model, a cap over max", async () => { const code = await enqueueReportDraft({ task: "sweep", article: "synth-refused", question: "q", channels: ["synth-kitchen"], source }, deps()); assert.equal(code.result.ok, false); assert.match((code.result as { error: string }).error, /routed to Claude Code/); const big = await enqueueReportDraft({ task: "sweep", article: "synth-refused", model: "claude-sonnet-5-5", question: "q", channels: ["synth-kitchen"], maxUsd: 9, source }, deps()); assert.match((big.result as { error: string }).error, /over the settings' max of \$5/); assert.equal(await readIteration("synth-refused", 1, paths), null); }); test("an API sweep: a report-draft job on draft:api, the iteration done with its job, the usage in the ledger", async () => { const r = await run("sweep-agent"); api.push(...r.turns); const out = await enqueueReportDraft( { task: "sweep", article: "synth-api", model: r.model, question: r.question, channels: r.channels, source }, deps(), ); assert.ok(out.result.ok && "iteration" in out, (out.result as { error?: string }).error); const job = getRegistry().get(out.result.jobId)!; assert.equal(job.kind, "report-draft"); assert.equal(job.queueKey, "draft:api"); 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-api","iteration":1,"task":"sweep","status":"done"/); assert.ok(!log.includes(FAKE_KEY), "the key is never in the log"); const it = (await readIteration("synth-api", out.iteration, paths))!; assert.equal(it.status, "done"); assert.equal(it.jobId, out.result.jobId); const rows = await readGenerationUsage(paths.generationUsageFile); assert.deepEqual(rows.map((x) => x.model).sort(), ["claude-haiku-4-5", "claude-sonnet-5-5"]); const jobId = out.result.jobId; assert.ok(rows.every((x) => x.jobId === jobId && x.article === "synth-api" && x.iteration === 1)); assert.equal(api.requestHeaders.at(-1)!["x-api-key"], FAKE_KEY, "the key file's key"); // The job's progress (release 28 F3): the agent's model turns, the runner's own count. assert.deepEqual(job.progress, { metric: "turns", initial: 0, target: 150, current: 6 }); }); test("an API sweep with its own extractorModel: the batches are read on it, its spend in the ledger", async () => { const r = await run("sweep-agent"); api.push(...r.turns); const before = api.requests.length; const out = await enqueueReportDraft( { task: "sweep", article: "synth-extractor", model: r.model, extractorModel: "claude-haiku-5-5", question: r.question, channels: r.channels, source }, deps(), ); assert.ok(out.result.ok && "iteration" in out, (out.result as { error?: string }).error); void out.result.stream.cancel(); const done = await out.result.done; const job = getRegistry().get(out.result.jobId)!; assert.equal(done.status, "done", await readFile(job.logPath, "utf8")); const models = api.requests.slice(before).map((q: { model: string }) => q.model); assert.ok(models.includes("claude-haiku-5-5"), models.join(",")); assert.ok(!models.includes("claude-haiku-4-5"), "not the settings' extractor"); const it = (await readIteration("synth-extractor", out.iteration, paths))!; assert.ok(it.usage["claude-haiku-5-5"]!.calls === 1); }); test("seed (umtool R2): a new sweep's starting report — text, sized, a new sweep's only", () => { const sweep = { task: "sweep", article: "a", question: "q", channels: ["x"] }; assert.equal(parseReportDraftBody({ ...sweep, seed: "# Board\n" }).seed, "# Board\n"); assert.throws(() => parseReportDraftBody({ ...sweep, seed: " " }), /"seed" is markdown text/); assert.throws(() => parseReportDraftBody({ ...sweep, seed: "x".repeat(200_001) }), /"seed" is at most 200000 characters/); assert.throws(() => parseReportDraftBody({ task: "sweep", article: "a", iteration: 1, seed: "# B" }), /"seed" starts a new sweep/); assert.throws(() => parseReportDraftBody({ task: "revise-report", article: "a", iteration: 1, feedback: "f", seed: "# B" }), /"seed" starts a new sweep/); }); test("a seeded sweep: refused on a local model; on the API the seed is kept as seed.md and the agent is told to start from it", async () => { const seed = '# Board\n\n## Pinned\n\n- The pier was never approved: "the pier was never approved" — [Kitchen @ 0:10](https://example.test/?v=synth-kitchen%2Fk1&t=10)\n'; const local = await enqueueReportDraft({ task: "sweep", article: "synth-seed-local", model: "qwen3:8b", question: "q", channels: ["synth-kitchen"], source, seed }, deps()); assert.equal(local.result.ok, false); assert.match((local.result as { error: string }).error, /a seeded sweep runs the agent on a Claude model/); assert.equal(await readIteration("synth-seed-local", 1, paths), null); const r = await run("sweep-agent"); api.push(...r.turns); const before = api.requests.length; const out = await enqueueReportDraft({ task: "sweep", article: "synth-seeded", model: r.model, question: r.question, channels: r.channels, source, seed }, deps()); assert.ok(out.result.ok && "iteration" in out, (out.result as { error?: string }).error); void out.result.stream.cancel(); const done = await out.result.done; const job = getRegistry().get(out.result.jobId)!; const log = await readFile(job.logPath, "utf8"); assert.equal(done.status, "done", log); assert.match(log, /seeded \(\d+ chars\)/); assert.equal(await readIterationFile("synth-seeded", out.iteration, "seed.md", paths), seed); const first = JSON.stringify(api.requests[before]); assert.match(first, /THE REPORT FILE ALREADY HOLDS THE OPERATOR'S RESEARCH BOARD/); }); test("the daily cap: a job that would start past the day's total is refused, naming it", async () => { const before = await readGenerationUsage(paths.generationUsageFile); const total = before.reduce((s, x) => s + x.usd, 0); assert.ok(total > 0); const out = await enqueueReportDraft( { task: "sweep", article: "synth-daily", model: "claude-sonnet-5-5", question: "q", channels: ["synth-kitchen"], source }, deps({ caps: { ...gen.caps, daily: total / 2 } }), ); assert.equal(out.result.ok, false); assert.match((out.result as { error: string }).error, /the day's Claude API spend is \$\d+\.\d\d .* daily cap \(settings\.generation\.caps\.daily\) — refused/); assert.equal(await readIteration("synth-daily", 1, paths), null, "no iteration made"); }); test("job cancel stops a running draft: the request in flight aborted, the iteration cancelled", async () => { let release!: () => void; const held = new Promise((r) => (release = r)); let arrived!: () => void; const inFlight = new Promise((r) => (arrived = r)); api.push(async () => { arrived(); await held; return textTurn("too late"); }); const out = await enqueueReportDraft( { task: "sweep", article: "synth-cancel", model: "claude-sonnet-5-5", question: "q", channels: ["synth-kitchen"], source }, deps(), ); assert.ok(out.result.ok && "iteration" in out); void out.result.stream.cancel(); await inFlight; assert.equal(getRegistry().cancel(out.result.jobId), true); const done = await out.result.done; release(); assert.equal(done.status, "cancelled"); let it = await readIteration("synth-cancel", out.iteration, paths); for (let i = 0; i < 50 && it?.status !== "cancelled"; i++) { await new Promise((r) => setTimeout(r, 20)); it = await readIteration("synth-cancel", out.iteration, paths); } assert.equal(it!.status, "cancelled"); }); test("a local narration: draft:local, a step on the iteration, ollama unloaded when the job ends", async () => { const r = await run("narrate"); ol.push(...r.turns); const article = "synth-basic"; await createIteration(article, { kind: "sweep", model: "claude-sonnet-5-5", question: "q", scope: { channels: ["synth-kitchen"] } }, paths); const report = JSON.parse(await readFile(path.join(DRAFT, "reports", "basic.report.json"), "utf8")); await writeIterationFile(article, 1, "report.json", report, paths); const dir = projectOf(gen.projectsRoot, article, 1); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), await readFile(path.join(DRAFT, "manifests", "basic.video.manifest.json"), "utf8")); await writeFile(path.join(dir, "report.json"), JSON.stringify(report)); await updateIteration(article, 1, { status: "done", projectDir: dir }, paths); const out = await enqueueReportDraft({ task: "narrate", article, iteration: 1, model: r.model, source }, deps()); assert.ok(out.result.ok && "iteration" in out, (out.result as { error?: string }).error); assert.equal(getRegistry().get(out.result.jobId)!.queueKey, "draft:local"); void out.result.stream.cancel(); const done = await out.result.done; assert.equal(done.status, "done", await readFile(getRegistry().get(out.result.jobId)!.logPath, "utf8")); assert.deepEqual(ol.unloads, ["qwen3:8b"]); const it = (await readIteration(article, 1, paths))!; assert.equal(it.status, "done"); const step = it.steps.find((s) => s.task === "narrate")!; assert.equal(step.status, "done"); assert.equal(step.jobId, out.result.jobId); const m = JSON.parse(await readFile(path.join(dir, "video.manifest.json"), "utf8")); assert.ok(m.timeline.find((e: { id: string }) => e.id === "the-bakery").narration.text); }); test("a media draft (youtube, release 28 M6): a step on the done iteration, through the job, its field written", async () => { // synth-basic i1 is done (the narration test above). const r = await run("youtube"); ol.push(...r.turns); const out = await enqueueReportDraft({ task: "youtube", article: "synth-basic", iteration: 1, model: r.model, source }, deps()); assert.ok(out.result.ok && "iteration" in out, (out.result as { error?: string }).error); assert.equal(out.iteration, 1); assert.equal(getRegistry().get(out.result.jobId)!.queueKey, "draft:local"); void out.result.stream.cancel(); const done = await out.result.done; assert.equal(done.status, "done", await readFile(getRegistry().get(out.result.jobId)!.logPath, "utf8")); const it = (await readIteration("synth-basic", 1, paths))!; assert.equal(it.status, "done", "the iteration is left as it was"); const step = it.steps.find((s) => s.task === "youtube")!; assert.equal(step.status, "done"); const m = JSON.parse(await readFile(path.join(projectOf(gen.projectsRoot, "synth-basic", 1), "video.manifest.json"), "utf8")); assert.equal(m.publish.youtube.title, "What the baker of Synth Kitchen says about the bakery"); assert.equal(m.drafted["publish.youtube"].task, "youtube"); const notDone = await enqueueReportDraft({ task: "cut", article: "synth-basic", iteration: 9, model: "qwen3:8b", source }, deps()); assert.match((notDone.result as { error: string }).error, /has no iteration i9/); }); test("a local sweep (demo epic D3): its calls in the iteration's model log; the ledger lists its local model at no cost, and no Claude model", async () => { const r = await run("sweep-pipeline"); ol.push(...r.turns); const spentBefore = await dayUsage(paths.generationUsageFile); const out = await enqueueReportDraft({ task: "sweep", article: "synth-local-only", model: r.model, question: r.question, channels: r.channels, source }, deps()); assert.ok(out.result.ok && "iteration" in out, (out.result as { error?: string }).error); assert.equal(getRegistry().get(out.result.jobId)!.queueKey, "draft:local"); void out.result.stream.cancel(); const jobId = out.result.jobId; const done = await out.result.done; assert.equal(done.status, "done", await readFile(getRegistry().get(jobId)!.logPath, "utf8")); const log = await readModelLog(modelLogFilesIn(iterationDir(paths, "synth-local-only", out.iteration))); assert.equal(log.runs[0]!.jobId, jobId); assert.deepEqual([...new Set(log.calls.map((c) => c.phase))], ["terms", "reading", "grouping"]); assert.ok(log.calls.every((c) => c.route === "local")); const rows = (await readGenerationUsage(paths.generationUsageFile)).filter((x) => x.jobId === jobId); assert.ok(rows.length > 0, "a local run is in the ledger"); assert.ok(rows.every((x) => x.route === "local" && x.usd === 0 && x.article === "synth-local-only" && x.iteration === out.iteration && x.task === "sweep")); const models = usageModelsOf(rows); assert.deepEqual(models.map((m) => [m.model, m.route, m.calls]), [["qwen3:8b", "local", log.calls.length]]); assert.ok(!models.some((m) => m.route === "api" || m.model.startsWith("claude")), "a local-only run lists no cloud model"); assert.deepEqual(await dayUsage(paths.generationUsageFile), spentBefore, "the cap's total and jobs count are the API's alone"); }); test("the local model's memory floor (demo epic D5): under it the job WAITS, saying so, then fails with the sentence; ollama is never asked", async () => { const r = await run("sweep-pipeline"); const asked = ol.requests.length; const out = await enqueueReportDraft({ task: "sweep", article: "synth-floor", model: r.model, question: r.question, channels: r.channels, source }, deps({}, 1000)); assert.ok(out.result.ok && "iteration" in out, (out.result as { error?: string }).error); void out.result.stream.cancel(); const done = await out.result.done; assert.equal(done.status, "failed"); const log = await readFile(getRegistry().get(out.result.jobId)!.logPath, "utf8"); assert.match(log, /waiting for memory — 1000 MB available, the floor is 3072 MB \(LOCAL_MODEL_MIN_FREE_MB\); waits up to 0 s/); const it = (await readIteration("synth-floor", out.iteration, paths))!; assert.equal(it.status, "failed"); assert.match(it.error ?? "", /1000 MB available is under the 3072 MB a local model's run starts with, after waiting 0 s/); assert.equal(ol.requests.length, asked, "no call to the local model"); }); test("the memory floor: a job that waited goes on once memory is back (an existing job is delayed, never failed)", async () => { const r = await run("sweep-pipeline"); const own = await startFakeOllama({ turns: r.turns }); const reads = [1000, 2000, 8000]; try { const out = await enqueueReportDraft( { task: "sweep", article: "synth-floor-back", model: r.model, question: r.question, channels: r.channels, source }, { ...deps({ ollamaUrl: own.url }, () => reads.shift() ?? 8000), memoryWait: { pollMs: 10, maxWaitMs: 60_000 } }, ); assert.ok(out.result.ok && "iteration" in out, (out.result as { error?: string }).error); void out.result.stream.cancel(); const done = await out.result.done; const log = await readFile(getRegistry().get(out.result.jobId)!.logPath, "utf8"); assert.equal(done.status, "done", log); assert.match(log, /waiting for memory — 1000 MB available/); assert.match(log, /memory: 8000 MB available — going on/); } finally { await own.close(); } }); test("the memory floor: a cancel ends the wait at once", async () => { const r = await run("sweep-pipeline"); const asked = ol.requests.length; const out = await enqueueReportDraft( { task: "sweep", article: "synth-floor-cancel", model: r.model, question: r.question, channels: r.channels, source }, { ...deps({}, 1000), memoryWait: { pollMs: 50, maxWaitMs: 600_000 } }, ); assert.ok(out.result.ok && "iteration" in out, (out.result as { error?: string }).error); void out.result.stream.cancel(); for (let i = 0; i < 100 && !/waiting for memory/.test(await readFile(getRegistry().get(out.result.jobId)!.logPath, "utf8").catch(() => "")); i++) await new Promise((ok) => setTimeout(ok, 20)); const t0 = Date.now(); getRegistry().cancel(out.result.jobId); const done = await out.result.done; assert.equal(done.status, "cancelled"); assert.ok(Date.now() - t0 < 5000, "not the ten minutes"); assert.equal(ol.requests.length, asked); }); test("local only (demo epic D5): an API model, the API route, a Claude Code session's answers and an ollama cloud model are refused at the ask — no request reaches either fake", async () => { const apiBefore = api.requests.length; const olBefore = ol.requests.length; await localOnly(async () => { const byModel = await enqueueReportDraft({ task: "sweep", article: "synth-lo-1", model: "claude-sonnet-5-5", question: "q", channels: ["synth-kitchen"], source }, deps()); assert.equal(byModel.result.ok, false); assert.match((byModel.result as { error: string }).error, /local only \(ARCHILYZER_LOCAL_ONLY=1\): "claude-sonnet-5-5" is not a local model/); const byRoute = await enqueueReportDraft( { task: "sweep", article: "synth-lo-2", question: "q", channels: ["synth-kitchen"], source }, deps({ routes: { ...gen.routes, sweep: "api" } }), ); assert.match((byRoute.result as { error: string }).error, /"claude-sonnet-5-5" is not a local model/); const code = await enqueueReportDraft({ task: "sweep", article: "synth-lo-3", question: "q", channels: ["synth-kitchen"], source }, deps({ routes: { ...gen.routes, sweep: "code" } })); assert.match((code.result as { error: string }).error, /routed to Claude Code/); const extractor = await enqueueReportDraft( { task: "sweep", article: "synth-lo-4", model: "qwen3:8b", extractorModel: "claude-haiku-4-5", question: "q", channels: ["synth-kitchen"], source }, deps(), ); assert.match((extractor.result as { error: string }).error, /"claude-haiku-4-5" is not a local model/); const cloud = await enqueueReportDraft({ task: "sweep", article: "synth-lo-5", model: "gpt-oss:120b-cloud", question: "q", channels: ["synth-kitchen"], source }, deps()); assert.match((cloud.result as { error: string }).error, /"gpt-oss:120b-cloud" is an ollama cloud model/); for (const a of ["synth-lo-1", "synth-lo-2", "synth-lo-3", "synth-lo-4", "synth-lo-5"]) assert.equal(await readIteration(a, 1, paths), null, `${a}: no iteration made`); }); assert.equal(api.requests.length, apiBefore, "the fake Claude API was never asked"); assert.equal(ol.requests.length, olBefore, "the fake ollama was never asked"); }); test("local only: a local sweep still runs, on the local model alone", async () => { const r = await run("sweep-pipeline"); // Its own fake ollama: the shared one's script may hold an earlier test's turns. const own = await startFakeOllama({ turns: r.turns }); const apiBefore = api.requests.length; try { await localOnly(async () => { const out = await enqueueReportDraft({ task: "sweep", article: "synth-lo-local", model: r.model, question: r.question, channels: r.channels, source }, deps({ ollamaUrl: own.url })); assert.ok(out.result.ok && "iteration" in out, (out.result as { error?: string }).error); void out.result.stream.cancel(); const done = await out.result.done; assert.equal(done.status, "done", await readFile(getRegistry().get(out.result.jobId)!.logPath, "utf8")); }); assert.ok(own.requests.length > 0, "the local model was asked"); assert.deepEqual([...new Set(own.shows)], [r.model], "after its /api/show said it is local"); } finally { await own.close(); } assert.equal(api.requests.length, apiBefore, "the Claude API was not"); });