// The pipeline chain's plan (release 27 S7): its steps for the job runner, // where it resumes, the windows it asks the editor for, and `umtool chain // --dry`. Plain node, temp dirs only. // // Run with: node --test umtool/lib/report/chain.test.mjs import assert from "node:assert/strict"; import test, { after } from "node:test"; import { execFileSync } from "node:child_process"; import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { CHAIN_RUN, CHAIN_STEP_NAMES, DEFAULT_PROJECTS_ROOT, REPO_ROOT, chainProject, configuredProjectsRoot, chainStart, chainSteps, chainWindows, chainStepsOf, CHAIN_OPTIONS, cutWindows, DRAFT_STEP_TASKS, doneSteps, originFromSweep, } from "./chain.mjs"; const HERE = path.dirname(fileURLToPath(import.meta.url)); const ROOT = mkdtempSync(path.join(os.tmpdir(), "umtool-chain-")); after(() => rmSync(ROOT, { recursive: true, force: true })); test("the steps, in order, each one process of the executor under tsx, from the checkout", () => { assert.deepEqual(CHAIN_STEP_NAMES, ["current", "windows", "prepare", "posts", "manifest", "voices", "video", "podcast", "attach", "dedupe"]); assert.equal(REPO_ROOT, path.resolve(HERE, "..", "..", "..")); assert.equal(CHAIN_RUN, path.join(HERE, "chain-run.mjs")); const steps = chainSteps({ article: "my-article", n: 3, projectsRoot: "/p", node: "node" }); assert.equal(steps.length, 10); assert.equal(steps[0].label, "1/10 make the iteration current"); assert.deepEqual(steps[0].argv, ["node", "--import", "tsx", CHAIN_RUN, "step", "my-article", "3", "current", "--projects-root", "/p"]); assert.ok(steps.every((s) => s.cwd === REPO_ROOT && Object.keys(s.env).length === 0), "no secret rides in a step"); assert.ok(steps.every((s) => s.timeoutMs > 0)); assert.deepEqual(steps.filter((s) => s.ndjson).map((s) => s.argv[7]), ["voices", "video", "podcast"]); const late = chainSteps({ article: "my-article", n: 3, projectsRoot: "/p", start: "video", final: true, siteOrigin: "https://a.example", node: "node" }); assert.deepEqual(late.map((s) => s.argv[7]), ["video", "podcast", "attach", "dedupe"]); assert.equal(late[0].label, "7/10 build the video"); assert.deepEqual(late[0].argv.slice(8), ["--projects-root", "/p", "--site-origin", "https://a.example", "--final"]); assert.deepEqual(chainSteps({ article: "a", n: 1, start: null }), []); assert.throws(() => chainSteps({ article: "Bad", n: 1 }), /not an article id/); assert.throws(() => chainSteps({ article: "a", n: 0 }), /not an iteration number/); assert.throws(() => chainSteps({ article: "a", n: 1, start: "nope" }), /no chain step/); assert.equal(chainProject("/p", "a", 2), path.join("/p", "a", "i2")); }); test("the deck option (release 29 G1): a step before the flips, off by default — without it the chain is as it was", () => { assert.deepEqual(CHAIN_OPTIONS, ["deck", "stopBeforeRender"]); const plain = chainStepsOf("draft-all").map((s) => s.name); assert.ok(!plain.includes("deck")); const withDeck = chainStepsOf("draft-all", { deck: true }).map((s) => s.name); assert.deepEqual(withDeck.filter((n) => n !== "deck"), plain); assert.equal(withDeck.indexOf("deck"), withDeck.indexOf("draft-flips") - 1); assert.equal(withDeck.indexOf("deck"), withDeck.indexOf("draft-teaser") + 1); // Not the video chain's; an option the chain is not asked for adds nothing. assert.deepEqual(chainStepsOf("video", { deck: true }).map((s) => s.name), CHAIN_STEP_NAMES); assert.deepEqual(chainStepsOf("draft-all", { deck: false }).map((s) => s.name), plain); // The step asks no model: no --model on it, and the labels count it. const steps = chainSteps({ article: "art", n: 2, projectsRoot: "/p", node: "node", mode: "draft-all", model: "qwen3:8b", deck: true }); const deck = steps.find((s) => s.argv[7] === "deck"); assert.ok(deck && !deck.argv.includes("--model")); assert.equal(steps.length, withDeck.length); assert.match(deck.label, new RegExp(`^${withDeck.indexOf("deck") + 1}/${withDeck.length} add the on-screen deck`)); assert.deepEqual( chainSteps({ article: "art", n: 2, projectsRoot: "/p", node: "node", mode: "draft-all" }).map((s) => s.argv[7]), plain, ); assert.throws(() => chainSteps({ article: "art", n: 2, projectsRoot: "/p", deck: true }), /the deck is Draft everything's option/); // Resume: a chain that ran to the threads without the deck starts again at the deck with it. const ran = ["current", "prepare", "posts", "manifest", "draft-cut", "cut-windows", "draft-onscreen", "draft-teaser", "draft-flips", "draft-threads"]; assert.equal(chainStart({ done: ran, mode: "draft-all", deck: true }), "deck"); assert.equal(chainStart({ done: ran, mode: "draft-all" }), "draft-narrate"); assert.ok(doneSteps({ steps: [{ task: "chain:deck", status: "failed" }] }).includes("deck"), "optional: a failed deck step is passed"); }); test("resume: the first step not recorded done; --from names one; all done is nothing", () => { const it = { steps: [ { task: "narrate", status: "done" }, { task: "chain:current", status: "done" }, { task: "chain:windows", status: "done" }, { task: "chain:prepare", status: "failed" }, ], }; assert.deepEqual(doneSteps(it), ["current", "windows"]); assert.equal(chainStart({ done: doneSteps(it) }), "prepare"); assert.equal(chainStart({ done: [] }), "current"); assert.equal(chainStart({ done: CHAIN_STEP_NAMES }), null); assert.equal(chainStart({ from: "manifest", done: CHAIN_STEP_NAMES }), "manifest"); assert.throws(() => chainStart({ from: "nope" }), /--from must be one of/); }); test("Draft everything: the drafts between the manifest and the fast build, YouTube after it; the default chain unchanged", () => { const names = chainStepsOf("draft-all").map((s) => s.name); assert.deepEqual(names, [ "current", "prepare", "posts", "manifest", "draft-cut", "cut-windows", "draft-onscreen", "draft-teaser", "draft-flips", "draft-threads", "draft-narrate", "voices", "video", "podcast", "draft-youtube", "draft-social", ]); assert.deepEqual(chainStepsOf("video").map((s) => s.name), CHAIN_STEP_NAMES); assert.ok(chainStepsOf("video").every((s) => !s.optional), "make the video has no optional step"); assert.deepEqual(DRAFT_STEP_TASKS, ["cut", "onscreen", "teaser", "flips", "threads", "narrate", "youtube", "social"]); assert.throws(() => chainStepsOf("nope"), /no chain "nope"/); const steps = chainSteps({ article: "art", n: 2, projectsRoot: "/p", node: "node", mode: "draft-all", model: "qwen3:8b", final: true }); assert.equal(steps.length, 16); assert.equal(steps[4].label, "5/16 draft the cut: which clips, in what order"); // demo epic D1: the cut's windows right after the cut is drafted, not every citation's before it. assert.equal(steps[5].label, "6/16 fetch the cut's clip windows (editor)"); // The model rides on the draft steps only; the build is the fast one (--final is ignored). for (const s of steps) { const draft = s.argv[7].startsWith("draft-"); assert.equal(s.argv.includes("--model"), draft, s.argv[7]); assert.ok(!s.argv.includes("--final"), s.argv[7]); } assert.deepEqual(steps[4].argv.slice(8), ["--projects-root", "/p", "--mode", "draft-all", "--model", "qwen3:8b"]); // No model: each task's route chooses. assert.ok(chainSteps({ article: "art", n: 2, projectsRoot: "/p", mode: "draft-all" }).every((s) => !s.argv.includes("--model"))); assert.throws(() => chainSteps({ article: "art", n: 2, projectsRoot: "/p", mode: "draft-all", model: "a b" }), /not a model name/); // attach is not in Draft everything. assert.throws(() => chainSteps({ article: "art", n: 2, projectsRoot: "/p", mode: "draft-all", start: "attach" }), /no chain step "attach"/); }); test("Draft everything resumes past a draft that ran — done, skipped or failed — but not past a failed code step", () => { const it = { steps: ["current", "windows", "prepare", "posts", "manifest"].map((s) => ({ task: `chain:${s}`, status: "done" })).concat([ { task: "chain:draft-cut", status: "done" }, { task: "chain:cut-windows", status: "done" }, { task: "chain:draft-onscreen", status: "failed" }, { task: "chain:draft-teaser", status: "skipped" }, { task: "chain:draft-flips", status: "cancelled" }, ]), }; const done = doneSteps(it); assert.deepEqual(done.slice(5), ["draft-cut", "cut-windows", "draft-onscreen", "draft-teaser"]); assert.equal(chainStart({ done, mode: "draft-all" }), "draft-flips"); // The default chain never sees the drafts: it resumes at voices. assert.equal(chainStart({ done }), "voices"); const failedCode = { steps: [{ task: "chain:current", status: "done" }, { task: "chain:prepare", status: "failed" }] }; assert.equal(chainStart({ done: doneSteps(failedCode), mode: "draft-all" }), "prepare"); assert.equal(chainStart({ from: "draft-threads", mode: "draft-all" }), "draft-threads"); assert.throws(() => chainStart({ from: "draft-threads" }), /--from must be one of/); const all = chainStepsOf("draft-all").map((s) => s.name); assert.equal(chainStart({ done: all, mode: "draft-all" }), null); }); test("chainWindows: each cited moment padded to the cut's fetchPad and the citation's own pad, rounded outward, once", () => { const report = { id: "art", citations: { c1: { kind: "video", channel: "synth-kitchen", id: "v1", start: 10.004, end: 20.001, quote: "a" }, // Inside c1's window: no second window. c2: { kind: "video", channel: "synth-kitchen", id: "v1", start: 12, end: 18, quote: "b" }, // Its own pad is wider than the fetchPad after. c3: { kind: "audio", channel: "synth-other", id: "a1", start: 1, end: 5, pad: { after: 8 }, quote: "c" }, p1: { kind: "post", channel: "synth-posts", id: "9", quote: "d" }, s1: { kind: "source", source: "s", quote: "e" }, }, }; const body = chainWindows(report, { requestedBy: "test" }); assert.equal(body.requestedBy, "test"); assert.deepEqual(body.items, [ { slug: "synth-kitchen", id: "v1", from: 7, to: 23.01, clipId: "art#c1", reason: "article art c1", pad: 3 }, { slug: "synth-other", id: "a1", from: 0, to: 13, clipId: "art#c3", reason: "article art c3", pad: 3 }, ]); assert.deepEqual(chainWindows({ id: "x", citations: {} }).items, []); }); test("originFromSweep: the archive a sweep's moment links name", () => { assert.equal(originFromSweep("see [it](https://synth.example/?v=synth-kitchen%2Fv1&t=5)"), "https://synth.example"); assert.equal(originFromSweep("[x](http://localhost:8768/?v=a%2Fb)"), "http://localhost:8768"); assert.equal(originFromSweep("no links"), null); assert.equal(originFromSweep(null), null); }); test("umtool chain --dry: the plan, resuming where the iteration left off; refusals", () => { const articles = path.join(ROOT, "reports"); const dir = path.join(articles, "art", "iterations", "2"); mkdirSync(dir, { recursive: true }); writeFileSync( path.join(dir, "iteration.json"), JSON.stringify({ n: 2, status: "done", steps: [{ task: "chain:current", status: "done" }, { task: "chain:windows", status: "done" }] }), ); const umtool = path.join(HERE, "..", "..", "bin", "umtool.mjs"); const env = { ...process.env, ARTICLES_DIR: articles }; const run = (...args) => execFileSync(process.execPath, [umtool, "chain", ...args], { env, encoding: "utf8", cwd: ROOT }); const plan = JSON.parse(run("art", "2", "--dry", "--json", "--projects-root", path.join(ROOT, "projects"))); assert.equal(plan.start, "prepare"); assert.deepEqual(plan.steps.map((s) => s.argv[7]), ["prepare", "posts", "manifest", "voices", "video", "podcast", "attach", "dedupe"]); assert.ok(plan.steps.every((s) => s.argv.includes(path.join(ROOT, "projects")))); const text = run("art", "2", "--dry", "--from", "video"); assert.match(text, /# art i2 — the chain from "video"/); assert.match(text, /\(cd .* && .*chain-run\.mjs step art 2 video/); assert.throws(() => run("art", "9", "--dry"), (e) => /has no iteration i9/.test(String(e.stderr))); assert.throws(() => run("art", "2", "--dry", "--from", "nope"), (e) => /--from must be one of/.test(String(e.stderr))); // --draft-all: the same resume point, the drafts in the plan, the model on them. const all = JSON.parse(run("art", "2", "--dry", "--json", "--draft-all", "--model", "claude-sonnet-5-5", "--projects-root", path.join(ROOT, "projects"))); assert.equal(all.start, "prepare"); assert.deepEqual(all.steps.map((s) => s.argv[7]).slice(3, 9), ["draft-cut", "cut-windows", "draft-onscreen", "draft-teaser", "draft-flips", "draft-threads"]); assert.deepEqual(all.steps.map((s) => s.argv[7]).slice(-2), ["draft-youtube", "draft-social"]); assert.ok(all.steps.at(-1).argv.includes("claude-sonnet-5-5")); assert.throws(() => run("art", "2", "--dry", "--model", "qwen3:8b"), (e) => /--model is Draft everything's/.test(String(e.stderr))); assert.throws(() => run("art", "2", "--dry", "--draft-all", "--final"), (e) => /builds the fast cut/.test(String(e.stderr))); // --deck (release 29 G1): the deck step before the flips; Draft everything's only. const deck = JSON.parse(run("art", "2", "--dry", "--json", "--draft-all", "--deck", "--projects-root", path.join(ROOT, "projects"))); assert.deepEqual(deck.steps.map((s) => s.argv[7]).slice(7, 9), ["deck", "draft-flips"]); assert.throws(() => run("art", "2", "--dry", "--deck"), (e) => /--deck is Draft everything's option/.test(String(e.stderr))); }); test("planChain: the record, the resume point and the steps; a queued iteration is refused", async () => { const { planChain } = await import("./chain.mjs"); const articles = path.join(ROOT, "plan-reports"); const put = (n, record) => { const dir = path.join(articles, "art", "iterations", String(n)); mkdirSync(dir, { recursive: true }); writeFileSync(path.join(dir, "iteration.json"), JSON.stringify(record)); }; put(1, { n: 1, status: "done", steps: [] }); put(2, { n: 2, status: "queued", steps: [] }); put(3, { n: 3, status: "done", steps: ["current", "windows", "prepare", "posts", "manifest", "voices", "video", "podcast", "attach", "dedupe"].map((s) => ({ task: `chain:${s}`, status: "done" })) }); const plan = planChain({ articlesDir: articles, article: "art", n: 1, projectsRoot: "/p" }); assert.equal(plan.start, "current"); assert.equal(plan.steps.length, 10); assert.deepEqual(planChain({ articlesDir: articles, article: "art", n: 3 }).steps, []); assert.equal(planChain({ articlesDir: articles, article: "art", n: 3, from: "attach" }).steps.length, 2); assert.throws(() => planChain({ articlesDir: articles, article: "art", n: 2 }), /i2 is queued/); assert.throws(() => planChain({ articlesDir: articles, article: "art", n: 7 }), /has no iteration i7/); }); test("the projects root comes from settings.generation.projectsRoot (release 27 S6), else the default", () => { const dir = mkdtempSync(path.join(os.tmpdir(), "chain-settings-")); try { const file = path.join(dir, "settings.json"); assert.equal(configuredProjectsRoot(path.join(dir, "absent.json")), DEFAULT_PROJECTS_ROOT); writeFileSync(file, JSON.stringify({ generation: { projectsRoot: "" } })); assert.equal(configuredProjectsRoot(file), DEFAULT_PROJECTS_ROOT); writeFileSync(file, JSON.stringify({ generation: { projectsRoot: "/srv/projects" } })); assert.equal(configuredProjectsRoot(file), "/srv/projects"); writeFileSync(file, JSON.stringify({ generation: { projectsRoot: "~/elsewhere" } })); assert.equal(configuredProjectsRoot(file), path.join(os.homedir(), "elsewhere")); } finally { rmSync(dir, { recursive: true, force: true }); } }); // demo epic D1: windows for the cut; stop before render; the whole-recording allowance. test("cutWindows: the manifest's clips, padded and rounded outward, once per moment", () => { const body = cutWindows( { slug: "art-i2", provenance: { channelSlug: "fallback" }, timeline: [ { type: "card", id: "t0" }, { type: "clip", id: "c1", channel: "alpha", video: "v1", start: 10.004, end: 20 }, { type: "clip", id: "c2", channel: "alpha", video: "v1", start: 12, end: 18 }, // inside c1's window { type: "clip", id: "c3", video: "v2", start: 1, end: 4 }, // the manifest's channel { type: "clip", id: "bad", channel: "alpha", video: "v3" }, ], }, { pad: 3, requestedBy: "t" }, ); assert.deepEqual(body, { requestedBy: "t", items: [ { slug: "alpha", id: "v1", from: 7, to: 23, clipId: "art-i2#c1", reason: "cut art-i2 c1", pad: 3 }, { slug: "fallback", id: "v2", from: 0, to: 7, clipId: "art-i2#c3", reason: "cut art-i2 c3", pad: 3 }, ], }); }); test("stopBeforeRender ends either chain before the voices; the allowance rides on every step", () => { const video = chainStepsOf("video", { stopBeforeRender: true }).map((s) => s.name); assert.deepEqual(video, ["current", "windows", "prepare", "posts", "manifest"]); const all = chainStepsOf("draft-all", { stopBeforeRender: true }).map((s) => s.name); assert.equal(all.at(-1), "draft-narrate"); assert.ok(!all.includes("voices") && !all.includes("video")); // All done before the render: nothing to run; without the option, it resumes at the voices. assert.equal(chainStart({ done: video, stopBeforeRender: true }), null); assert.equal(chainStart({ done: video }), "voices"); const steps = chainSteps({ article: "art", n: 2, projectsRoot: "/p", node: "node", stopBeforeRender: true, allowWholeRecording: true }); assert.equal(steps.length, 5); assert.ok(steps.every((s) => s.argv.includes("--allow-whole-recording"))); assert.ok(chainSteps({ article: "art", n: 2, projectsRoot: "/p", node: "node" }).every((s) => !s.argv.includes("--allow-whole-recording") && !s.argv.includes("--mode"))); });