// Lane L4: the `propose-groups` task on the fake ollama and the fake // Anthropic — one ask, the answer checked (a kept group and every refusal), // written as group-proposals.json; the code route's answer read as a // model's; the manifest byte-identical after every run. 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 { startFakeAnthropic, toolTurn, type FakeAnthropic } from "../../../../testing/llm/fakeAnthropic.mjs"; import { jsonReply, startFakeOllama, PROMPT_CAP_TOKENS, type FakeOllama } from "../../../../testing/llm/fakeOllama.mjs"; import { checkGroupProposals, cutSections, markGroupProposals, readGroupProposals } from "../../groupProposals.mjs"; import { GROUPS_CODE_MODEL, ProposeGroupsRefusal, groupsView, planProposeGroups, runProposeGroups } from "./proposeGroups"; import type { LlmConfig } from "../../../llm"; let tmp: string; let api: FakeAnthropic; let ol: FakeOllama; let llm: LlmConfig; before(async () => { tmp = await mkdtemp(path.join(os.tmpdir(), "propose-groups-")); api = await startFakeAnthropic(); ol = await startFakeOllama(); llm = { anthropic: { baseUrl: api.url, env: { ANTHROPIC_API_KEY: api.key } }, ollama: { baseUrl: ol.url } }; }); after(async () => { await api.close(); await ol.close(); await rm(tmp, { recursive: true, force: true }); }); const clip = (id: string, i: number, extra: Record = {}) => ({ type: "clip", id, channel: "c", video: "v1", start: 10 * i, end: 10 * i + 8, quote: `words of ${id}`, ...extra }); /** §0 c01 c02 | §1 "Two" c03 card c04 | §2 "Three" c05 c06; g1 = c06. */ function manifestOf() { return { slug: "s", render: { chrome: { engine: "hyperframes", layout: "deck", deck: { markers: [{ after: "c02", label: "Two" }, { after: "c04", label: "Three" }] } } }, timeline: [ clip("c01", 0, { onscreen: { title: "24 Oct 2020" } }), clip("c02", 1), clip("c03", 2), { type: "card", id: "k1", heading: "a card" }, clip("c04", 3), clip("c05", 4), clip("c06", 5), ], groups: [{ id: "g1", section: 2, claim: "Taken already", ids: ["c06"] }], }; } async function project(name: string) { const dir = path.join(tmp, name); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify(manifestOf(), null, 2) + "\n"); return dir; } const ANSWER = { groups: [ { section: 0, claim: "She opens on the claim", ids: ["c02", "c01"], why: "both say it" }, { section: 1, claim: "A card between does not break it", ids: ["c03", "c04"], why: "w" }, { section: 2, claim: "Already grouped", ids: ["c05", "c06"], why: "w" }, { section: 0, claim: "Not together", ids: ["c01", "c03"], why: "w" }, { section: 1, claim: "Across a boundary", ids: ["c02", "c03"], why: "w" }, { section: 2, claim: "x".repeat(161), ids: ["c05"], why: "w" }, { section: 2, claim: "", ids: ["c05"], why: "w" }, { section: 2, claim: "Unknown clip", ids: ["zz"], why: "w" }, { section: 0, claim: "Twice", ids: ["c01"], why: "w" }, { section: 2, claim: "Lone claim", ids: ["c05"], why: "its own" }, ], }; test("checkGroupProposals: a kept group and every refusal, in words; the sections read as umtool reads them", () => { const m = manifestOf(); const cut = cutSections(m); assert.deepEqual([...cut.sectionOf.entries()], [["c01", 0], ["c02", 0], ["c03", 1], ["c04", 1], ["c05", 2], ["c06", 2]]); assert.deepEqual(cut.labels, [null, "Two", "Three"]); const r = checkGroupProposals(m, ANSWER); assert.deepEqual(r.groups, [ { section: 0, claim: "She opens on the claim", ids: ["c01", "c02"], why: "both say it" }, { section: 1, claim: "A card between does not break it", ids: ["c03", "c04"], why: "w" }, { section: 2, claim: "Lone claim", ids: ["c05"], why: "its own" }, ]); assert.deepEqual(r.refused.map((x) => x.why), [ "c06 (g1) is in a group already", "its clips are not together: c02 stands between c01 and c03", "it spans §0 and §1: a group never crosses a section boundary", "its claim is 161 characters (at most 160)", "it has no claim", "zz is not a clip of the cut", "c01 is in another proposed group (the proposal \"She opens on the claim\")", ]); // Across a boundary, where nothing else refuses it first. assert.deepEqual(checkGroupProposals(m, { groups: [{ claim: "x", ids: ["c04", "c05"] }] }).refused.map((x) => x.why), ["it spans §1 and §2: a group never crosses a section boundary"]); // No markers: one section. assert.equal(checkGroupProposals({ timeline: m.timeline }, { groups: [{ claim: "x", ids: ["c04", "c05"] }] }).groups[0]!.section, 0); }); for (const [route, model] of [ ["local", "qwen3:8b"], ["api", "claude-sonnet-5-5"], ] as const) { test(`on the ${route} model: one ask over the cut's view, the answer checked, group-proposals.json written, the manifest untouched`, async () => { const dir = await project(route); const before = await readFile(path.join(dir, "video.manifest.json"), "utf8"); let prompt = ""; if (route === "local") { ol.push((body: { messages: { role: string; content: string }[] }) => { prompt = body.messages.find((m) => m.role === "user")!.content; return jsonReply(ANSWER); }); } else { api.push((body: { messages: { content: unknown }[] }) => { prompt = JSON.stringify(body.messages[0]!.content); return toolTurn("answer", ANSWER); }); } const out = await runProposeGroups({ project: dir, model, request: "by claim" }, { llm, now: () => new Date("2026-11-02T00:00:00.000Z") }); assert.equal(out.status, "done"); assert.match(out.summary, new RegExp(`^3 groups proposed, 7 refused on ${model}`)); assert.ok(prompt.includes("§1 Two"), prompt.slice(0, 300)); assert.ok(prompt.includes("c06: (taken: in g1)")); assert.ok(prompt.includes('c01: [24 Oct 2020] \\"words of c01\\"') || prompt.includes('c01: [24 Oct 2020] "words of c01"')); const { doc } = await readGroupProposals(dir); assert.equal(doc!.model, model); assert.equal(doc!.request, "by claim"); assert.deepEqual(doc!.groups.map((g) => [g.id, g.status, g.ids.join(" ")]), [["p1", "proposed", "c01 c02"], ["p2", "proposed", "c03 c04"], ["p3", "proposed", "c05"]]); assert.equal(doc!.refused.length, 7); assert.equal(await readFile(path.join(dir, "video.manifest.json"), "utf8"), before, "nothing applied"); const marked = markGroupProposals(doc!, { p1: { status: "accepted", group: "g2" }, p9: { status: "rejected" } }, "t"); assert.deepEqual(marked.groups.map((g) => g.status), ["accepted", "proposed", "proposed"]); assert.equal(marked.groups[0]!.group, "g2"); }); } test("the code route: a session's answer is checked as a model's, written as claude-code's; junk refused; no model asked", async () => { const dir = await project("code"); const before = await readFile(path.join(dir, "video.manifest.json"), "utf8"); const asked = api.requests.length + ol.requests.length; const out = await runProposeGroups({ project: dir, model: GROUPS_CODE_MODEL, answers: { groups: [{ section: 0, claim: "Two clips", ids: ["c01", "c02"], why: "" }] } }, { llm }); assert.equal(out.status, "done"); assert.equal(api.requests.length + ol.requests.length, asked); const { doc } = await readGroupProposals(dir); assert.equal(doc!.model, "claude-code"); assert.deepEqual(doc!.groups.map((g) => g.claim), ["Two clips"]); await assert.rejects(runProposeGroups({ project: dir, model: GROUPS_CODE_MODEL, answers: { nope: 1 } }, { llm }), /the session's answer is refused: the answer has no groups list/); assert.equal(await readFile(path.join(dir, "video.manifest.json"), "utf8"), before); }); test("refusals and the plan: no manifest, an API ask over its cap; the local view fitted to the prompt cap however long the cut", async () => { await assert.rejects(runProposeGroups({ project: path.join(tmp, "none"), model: "qwen3:8b" }, { llm }), ProposeGroupsRefusal); const dir = await project("cap"); await assert.rejects(runProposeGroups({ project: dir, model: "claude-sonnet-5-5", maxUsd: 0.000001 }, { llm }), /is over its cap/); const m = manifestOf() as { timeline: unknown[] }; for (let i = 10; i <= 900; i++) m.timeline.push(clip(`r${i}`, i, { quote: "a long quote ".repeat(40) })); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify(m)); const plan = await planProposeGroups({ project: dir, model: "qwen3:8b" }, { llm }); assert.ok(plan.estimate.inputTokens <= PROMPT_CAP_TOKENS, String(plan.estimate.inputTokens)); assert.equal(plan.estimate.usd, 0); assert.ok(groupsView(m as Record, { maxChars: 2000 }).text.length <= 2000); const apiPlan = await planProposeGroups({ project: dir, model: "claude-sonnet-5-5" }, { llm }); assert.ok(apiPlan.estimate.usd > 0); });