// TASK cut (release 28 M1) over the S0 kit's manifests and the basic report, // against the fake ollama: each code check refuses what it should, the better // of two attempts is kept, the timeline is BUILT by code from the entries it // had (and the umtool lint passes on it), the order's owner is the M1 ruling's, // and an operator's order is never replaced. Real iterations store and project // in a temp dir; the build's validators loaded from this checkout. import { after, before, test } from "node:test"; import assert from "node:assert/strict"; import { execFile } from "node:child_process"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { promisify } from "node:util"; import { jsonReply, startFakeOllama, type FakeOllama } from "../../../../testing/llm/fakeOllama.mjs"; import { createIteration, projectOf, readIteration, updateIteration, writeIterationFile } from "../../iterations"; import { reportToManifest } from "../../convertManifest"; import { buildCut, checkCut, cut, cutCandidates, isConverterOrder, parseCut, pinnedEntries, CUT_SPEC } from "./cut"; import { loadDeckChecks, type DeckChecks } from "./deckChecks"; import type { DraftFile } from "./media"; import type { TaskDeps } from "./types"; const HERE = path.dirname(fileURLToPath(import.meta.url)); const ROOT = path.resolve(HERE, "../../../../.."); const DRAFT = new URL("../../../../testing/draft/", import.meta.url); const kit = async (rel: string) => JSON.parse(await readFile(new URL(rel, DRAFT), "utf8")); const run = promisify(execFile); let tmp: string; let ol: FakeOllama; let deck: DeckChecks; before(async () => { tmp = await mkdtemp(path.join(os.tmpdir(), "cut-task-")); ol = await startFakeOllama(); deck = await loadDeckChecks(ROOT); }); after(async () => { await ol.close(); await rm(tmp, { recursive: true, force: true }); }); let caseN = 0; async function project(manifest: unknown, report?: unknown) { caseN++; const paths = { articlesDir: path.join(tmp, `reports-${caseN}`), exportIndexDir: path.join(tmp, `idx-${caseN}`) }; const projectsRoot = path.join(tmp, `projects-${caseN}`); await mkdir(paths.articlesDir, { recursive: true }); await createIteration("synth-basic", { kind: "sweep", model: "qwen3:8b", question: "q", scope: { channels: ["synth-kitchen"] } }, paths); const r = report ?? (await kit("reports/basic.report.json")); await writeIterationFile("synth-basic", 1, "report.json", r, paths); const dir = projectOf(projectsRoot, "synth-basic", 1); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), `${JSON.stringify(manifest, null, 2)}\n`); await writeFile(path.join(dir, "report.json"), JSON.stringify(r)); await updateIteration("synth-basic", 1, { status: "done", projectDir: dir }, paths); const lines: string[] = []; const progress: unknown[] = []; const deps: TaskDeps = { llm: { ollama: { baseUrl: ol.url } }, corpus: { source: "none", tools: [], call: async () => "" }, cuesOf: async () => null, paths, log: (l) => lines.push(l), progress: (p) => progress.push(p), now: () => new Date("2026-10-13T12:00:00.000Z"), deckChecks: async () => deck, }; const manifestNow = async () => JSON.parse(await readFile(path.join(dir, "video.manifest.json"), "utf8")); const draft = async () => JSON.parse(await readFile(path.join(dir, "cut-draft.json"), "utf8")) as DraftFile; const input = (o: Record = {}) => ({ article: "synth-basic", n: 1, model: "qwen3:8b", projectsRoot, ...o }); return { dir, paths, projectsRoot, deps, lines, progress, manifestNow, draft, input }; } const ids = (m: { timeline: { id: string }[] }) => m.timeline.map((e) => e.id); test("cut, the recorded run: the better of two attempts, the timeline built from the entries it had, marked, and the umtool lint passes", async () => { const m = await kit("manifests/basic.video.manifest.json"); assert.equal(m.drafted.timeline.model, "code", "the converter marks its order"); const p = await project(m); const r = await kit("runs/cut.json"); ol.push(...r.turns); const out = await cut(p.input({ maxClips: 20 }), p.deps); assert.equal(out.status, "done", `${out.summary}\n${p.lines.join("\n")}`); assert.equal(ol.remaining(), 0); assert.match(out.summary, /^1 cut\(s\) drafted \(undo: revisions\/.*-auto-before-cut\.manifest\.json\)$/); const after = await p.manifestNow(); assert.deepEqual(ids(after), ["title", "the-contest", "c04", "c06", "the-bakery", "c03", "c01"]); // Entries untouched: the same objects, moved. for (const e of after.timeline) assert.deepEqual(e, m.timeline.find((x: { id: string }) => x.id === e.id)); assert.deepEqual(after.drafted.timeline, { task: "cut", model: "qwen3:8b", at: "2026-10-13T12:00:00.000Z", iteration: 1, attempt: 2 }); const file = await p.draft(); const u = file.units.cut as { attempts: { hard: string[] }[]; applied: string }; assert.equal(u.applied, "applied"); assert.equal(u.attempts.length, 2); assert.match(u.attempts[0]!.hard.join("\n"), /not a candidate of this cut: c09/); assert.match(u.attempts[0]!.hard.join("\n"), /chosen more than once: c01/); assert.deepEqual(p.progress, [ { metric: "entries", current: 0, total: 1 }, { metric: "entries", current: 1, total: 1 }, ]); const it = (await readIteration("synth-basic", 1, p.paths))!; assert.equal(it.status, "done"); assert.equal(it.steps.find((s) => s.task === "cut")!.status, "done"); // The umtool lint over the written manifest (a node child, as a build runs it). const lint = await run(process.execPath, [path.join(ROOT, "umtool/report-to-video/lint.mjs"), path.join(p.dir, "video.manifest.json"), "--json"]).catch((e) => e); const res = JSON.parse(lint.stdout); assert.deepEqual(res.errors, [], lint.stdout); }); test("cut's checks: ids, twice, the cap and the floor, a pinned id, headings, and what would not build", async () => { const report = await kit("reports/basic.report.json"); const unitOf = async (m: unknown, maxClips = 20) => { const p = await project(m, report); const ctx = { manifest: m, report, input: p.input({ maxClips }), handle: { deps: p.deps, log: () => {} } } as never; return { u: ((await CUT_SPEC.units(ctx)) as never[])[0] as never, m: m as never }; }; const basic = await kit("manifests/basic.video.manifest.json"); const { u } = await unitOf(basic, 4); const g = (...groups: [string, string[]][]) => ({ groups: groups.map(([heading, clips]) => ({ heading, clips })) }); const hard = (v: ReturnType, man: unknown = basic, unit: never = u) => checkCut(v, unit, man as never).hard; assert.deepEqual(hard(g(["The bakery", ["c01", "c02"]], ["The contest", ["c04"]])), []); assert.match(hard(g(["The bakery", ["c01", "c02", "c99"]]))[0]!, /not a candidate of this cut: c99/); assert.match(hard(g(["The bakery", ["c01", "c02", "c02"]])).join(), /chosen more than once: c02/); assert.match(hard(g(["The bakery", ["c01", "c02", "c03"]], ["The contest", ["c04", "c05"]])).join(), /5 ids chosen \(at most 4\)/); assert.match(hard(g(["The bakery", ["c01", "c02"]])).join(), /2 ids chosen \(at least 3\)/); assert.match(hard(g(["The oven", ["c01", "c02", "c03"]])).join(), /"The oven" is not a section heading of this cut/); assert.match(hard(g(["The bakery", ["c01"]], ["the bakery", ["c02", "c03"]])).join(), /"the bakery" heads two groups/); // A post rides on c02: it must stay. const withPost = { ...basic, posts: [{ id: "p01", platform: "x", date: "2026-01-05", text: "x", url: "https://x.com/i/status/1", attachTo: "c02" }] }; const pu = (await unitOf(withPost)).u; assert.match(hard(g(["The bakery", ["c01", "c03"]], ["The contest", ["c04"]]), withPost, pu).join(), /c02 must stay \(post p01 rides on it\)/); // The full kit's threads: dropping every contest clip leaves a thread with none. const full = await kit("manifests/full.video.manifest.json"); const fu = (await unitOf(full)).u; assert.match(hard(g(["The bakery", ["c01", "c02", "c03"]]), full, fu).join(), /the cut would not build: render\.chrome\.threads: thread contest has no clip/); assert.deepEqual(hard(g(["The contest", ["c05"]], ["The bakery", ["c03", "c01"]]), full, fu), []); // parse: a flat list is one group; empty groups go. assert.deepEqual(parseCut({ clips: [" c01 ", "c02"] }), { groups: [{ heading: "", clips: ["c01", "c02"] }] }); assert.throws(() => parseCut({ nope: 1 }), /no "groups"/); }); test("buildCut: cards kept for sections with entries, a card with nothing dropped, head and tail in place, twins together, nothing invented", async () => { const full = await kit("manifests/full.video.manifest.json"); const report = await kit("reports/basic.report.json"); const cands = cutCandidates(full, report); assert.deepEqual(cands.map((c) => [c.citation, c.entry, c.chapter]), [ ["c01", "c01", "The bakery"], ["c02", "c02", "The bakery"], ["c03", "c03", "The bakery"], ["c04", "c04", "The contest"], ["c05", "c05", "The contest"], ["c06", "c06", "The contest"], ]); // c07 is cited by no citation of this report: after the last candidate, it stays with the teaser. const t = buildCut(full, cands, { groups: [{ heading: "The contest", clips: ["c06", "c04"] }, { heading: "The bakery", clips: ["c03"] }] }); assert.deepEqual(t.map((e) => e.id), ["title", "the-contest", "c06", "c04", "the-bakery", "c03", "c07", "fin"]); // No chapter for a group with no heading; the bakery card dropped (nothing left in it). const t2 = buildCut(full, cands, { groups: [{ heading: "The contest", clips: ["c04", "c05", "c02"] }] }); assert.deepEqual(t2.map((e) => e.id), ["title", "the-contest", "c04", "c05", "c02", "c07", "fin"]); // Variant twins move together; a hand-added entry stays in its chapter. const tw = structuredClone(full); const i = tw.timeline.findIndex((e: { id: string }) => e.id === "c02"); tw.timeline.splice(i + 1, 0, { ...tw.timeline[i], variant: "full", start: 26 }, { type: "image", id: "still-1", src: "s.png", seconds: 4, title: "A still" }); const t3 = buildCut(tw, cutCandidates(tw, report), { groups: [{ heading: "The contest", clips: ["c04"] }, { heading: "The bakery", clips: ["c02"] }] }); assert.deepEqual(t3.map((e) => `${e.id}${e.variant ? `/${e.variant}` : ""}`), ["title", "the-contest", "c04", "the-bakery", "still-1", "c02", "c02/full", "c07", "fin"]); // Pins: posts and flips. const flips = await kit("manifests/flips.video.manifest.json"); assert.deepEqual([...pinnedEntries(flips).keys()], ["c01", "c07"]); }); test("who owns the order (the M1 ruling): code's is drafted over, the converter's unmarked too; a model's needs force; the operator's never", async () => { const basic = await kit("manifests/basic.video.manifest.json"); const report = await kit("reports/basic.report.json"); const answer = jsonReply({ groups: [{ heading: "The bakery", clips: ["c03", "c02", "c01"] }] }); // (2) No mark, the converter's order: code-made. const unmarked = structuredClone(basic); delete unmarked.drafted; assert.equal(await isConverterOrder(unmarked, report), true); const p1 = await project(unmarked); ol.push(answer); assert.equal((await cut(p1.input(), p1.deps)).status, "done"); assert.deepEqual(ids(await p1.manifestNow()), ["title", "the-bakery", "c03", "c02", "c01"]); // A model's draft: skipped without force, redrafted with it. const again = await cut(p1.input(), p1.deps); assert.equal(again.status, "failed"); assert.match(String((await p1.draft()).units.cut!.skipped), /has a value \(a draft\); force re-drafts it/); ol.push(answer); assert.equal((await cut(p1.input({ force: true }), p1.deps)).status, "done"); // The operator's: unmarked and not the converter's order — never replaced, even forced. const mine = structuredClone(unmarked); const [a, b] = [2, 3].map((i) => mine.timeline[i]); mine.timeline.splice(2, 2, b, a); assert.equal(await isConverterOrder(mine, report), false); const p2 = await project(mine); const skipped = await cut(p2.input(), p2.deps); assert.equal(skipped.status, "failed"); assert.match(String((await p2.draft()).units.cut!.skipped), /the operator's/); ol.push(answer); const forced = await cut(p2.input({ force: true }), p2.deps); assert.equal(forced.status, "done"); assert.match(forced.summary, /0 cut\(s\) drafted, 1 as alternatives/); assert.deepEqual(ids(await p2.manifestNow()), ids(mine), "the operator's order stands"); const alt = (await p2.draft()).alternatives; assert.deepEqual([alt.length, alt[0]!.reason, (alt[0]!.value as { id: string }[]).map((e) => e.id).join(" ")], [1, "operator", "title the-bakery c03 c02 c01"]); assert.equal(ol.remaining(), 0); }); test("the converter marks its timeline as code's, at the day it was made", async () => { const made = await reportToManifest(await kit("reports/basic.report.json"), { today: "2026-10-13" }); assert.ok(made.ok); assert.deepEqual((made.manifest as { drafted: unknown }).drafted, { timeline: { task: "convert", model: "code", at: "2026-10-13T00:00:00.000Z", attempt: 1 } }); });