// The chain's executor (release 27 S7), run as the job runner runs it — one // process a step under tsx — over a temp article store and synthetic // fixtures: the steps that need no editor (current, windows with nothing to // fetch, manifest seeded from the parent, dedupe), each recorded in the // iteration; and its refusals. // // Run with: node --test umtool/lib/report/chain-run.test.mjs import assert from "node:assert/strict"; import test, { after } from "node:test"; import { spawnSync } from "node:child_process"; import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, statSync, writeFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import { fileURLToPath } from "node:url"; import { CHAIN_RUN, REPO_ROOT } from "./chain.mjs"; const HERE = path.dirname(fileURLToPath(import.meta.url)); const KIT = path.join(HERE, "..", "..", "..", "common", "testing", "draft"); const ROOT = mkdtempSync(path.join(os.tmpdir(), "umtool-chain-run-")); after(() => rmSync(ROOT, { recursive: true, force: true })); const TRANSCRIPTS = path.join(ROOT, "transcripts"); const ARTICLES = path.join(TRANSCRIPTS, "reports"); const PROJECTS = path.join(ROOT, "projects"); const ENV = { ...process.env, TRANSCRIPTS_DIR: TRANSCRIPTS, SITES_DIR: path.join(TRANSCRIPTS, "sites"), SETTINGS_FILE: path.join(ROOT, "settings.json"), EXPORT_PUBLIC_DIR: path.join(ROOT, "public"), EXPORT_INDEX_DIR: path.join(ROOT, ".export-index"), EXPORT_BUILDS_DIR: path.join(ROOT, ".export-builds"), // Never the live editor. ARCHILYZER_EDITOR_URL: "http://127.0.0.1:9", WORKER_TOKEN: "", }; delete ENV.ARTICLES_DIR; const put = (file, data) => { mkdirSync(path.dirname(file), { recursive: true }); writeFileSync(file, typeof data === "string" || Buffer.isBuffer(data) ? data : `${JSON.stringify(data, null, 2)}\n`); }; const json = (file) => JSON.parse(readFileSync(file, "utf8")); const NOW = "2026-10-13T12:00:00.000Z"; const iteration = (n, over = {}) => ({ format: "archilyzer-iteration", version: 1, article: "synth-basic", n, kind: "sweep", model: "qwen3:8b", parent: null, copyOf: null, question: "What does the baker say?", status: "done", usage: {}, steps: [], createdAt: NOW, updatedAt: NOW, ...over, }); const basic = json(path.join(KIT, "reports", "basic.report.json")); const iter = (n) => path.join(ARTICLES, "synth-basic", "iterations", String(n)); put(ENV.SETTINGS_FILE, {}); put(path.join(iter(1), "iteration.json"), iteration(1)); put(path.join(iter(1), "report.json"), basic); put(path.join(iter(1), "sweep.md"), readFileSync(path.join(KIT, "sweeps", "basic.md"), "utf8")); put(path.join(iter(2), "iteration.json"), iteration(2, { kind: "revise-report", parent: 1, feedback: "Say it plainer.", question: undefined })); put(path.join(iter(2), "report.json"), { ...basic, title: "What the baker says, plainer" }); put(path.join(iter(3), "iteration.json"), iteration(3, { status: "queued" })); // i1's video project: a narrated title card and its voice. const parentProject = path.join(PROJECTS, "synth-basic", "i1"); const parentManifest = json(path.join(KIT, "manifests", "basic.video.manifest.json")); parentManifest.timeline[0] = { ...parentManifest.timeline[0], seconds: "auto", narration: { text: "What the baker says." } }; put(path.join(parentProject, "video.manifest.json"), parentManifest); put(path.join(parentProject, "out", "narration", "voice1.wav"), Buffer.alloc(300, 2)); function step(n, name, ...extra) { const r = spawnSync(process.execPath, ["--import", "tsx", CHAIN_RUN, "step", "synth-basic", String(n), name, "--projects-root", PROJECTS, ...extra], { cwd: REPO_ROOT, env: ENV, encoding: "utf8", timeout: 120_000, }); return { code: r.status, out: `${r.stdout}\n${r.stderr}` }; } const recorded = (n) => Object.fromEntries(json(path.join(iter(n), "iteration.json")).steps.map((s) => [s.task, s])); test("current: the iteration becomes the article, its project recorded", () => { const r = step(2, "current"); assert.equal(r.code, 0, r.out); assert.equal(json(path.join(ARTICLES, "synth-basic", "report.json")).title, "What the baker says, plainer"); const it = json(path.join(iter(2), "iteration.json")); assert.equal(it.projectDir, path.join(PROJECTS, "synth-basic", "i2")); const s = recorded(2)["chain:current"]; assert.equal(s.status, "done"); assert.match(s.detail, /the article is i2: “What the baker says, plainer”/); assert.equal(typeof s.minutes, "number"); assert.equal(json(path.join(ARTICLES, "synth-basic", "iterations", "current.json")).report, 2); }); test("windows: the cited windows are asked of the editor — and its failure fails the step, recorded", () => { // The editor here is a closed port: the step fails, says why, and the plan is on disk. const r = step(2, "windows"); assert.equal(r.code, 1, r.out); const body = json(path.join(PROJECTS, "synth-basic", "i2", "windows.json")); assert.ok(body.items.length > 0); assert.ok(body.items.every((i) => i.pad === 3 && i.clipId.startsWith("synth-basic#"))); assert.equal(body.requestedBy, "umtool chain synth-basic i2"); const s = recorded(2)["chain:windows"]; assert.equal(s.status, "failed"); assert.match(s.detail, /fetch-windows ended/); }); test("manifest: to-manifest into the project, its origin from the lineage's sweep, narration and voices seeded from the parent", () => { const r = step(2, "manifest"); assert.equal(r.code, 0, r.out); const project = path.join(PROJECTS, "synth-basic", "i2"); const m = json(path.join(project, "video.manifest.json")); assert.equal(m.timeline[0].heading, "What the baker says, plainer"); assert.equal(m.timeline[0].foot, ""); assert.equal(m.timeline[0].narration?.text, "What the baker says."); assert.equal(m.provenance.siteOrigin, "https://synth.example"); assert.ok(existsSync(path.join(project, "report.json"))); assert.equal( statSync(path.join(project, "out", "narration", "voice1.wav")).ino, statSync(path.join(parentProject, "out", "narration", "voice1.wav")).ino, ); assert.match(recorded(2)["chain:manifest"].detail, /from i1: 1 narration\(s\) carried, 1 voice file\(s\) linked/); // Run again: the manifest is kept, never made over an edited one. const again = step(2, "manifest"); assert.equal(again.code, 0, again.out); assert.match(recorded(2)["chain:manifest"].detail, /kept/); }); test("dedupe: the article's projects share identical segments", () => { const seg = Buffer.alloc(70 * 1024, 5); put(path.join(PROJECTS, "synth-basic", "i1", "out", "sourced", "segments", "c01.mp4"), seg); put(path.join(PROJECTS, "synth-basic", "i2", "out", "sourced", "segments", "c01.mp4"), seg); const r = step(2, "dedupe"); assert.equal(r.code, 0, r.out); assert.match(recorded(2)["chain:dedupe"].detail, /^1 file\(s\) linked/); }); test("refusals: a queued iteration, an unknown step, no such iteration", () => { const queued = step(3, "current"); assert.equal(queued.code, 2); assert.match(queued.out, /i3 is queued — the chain runs over a done iteration/); const nope = step(2, "nope"); assert.equal(nope.code, 2); assert.match(nope.out, /no step "nope"/); const none = step(9, "current"); assert.equal(none.code, 2); assert.match(none.out, /has no iteration i9/); }); test("manifest: with no sweep.md naming an origin, the archive the sweep read (scope.source) is the QR origin (demo epic D1)", () => { const dir = path.join(ARTICLES, "synth-remote", "iterations", "1"); put(path.join(dir, "iteration.json"), iteration(1, { article: "synth-remote", scope: { channels: ["synth-kitchen"], source: "remote:https://stub.test" } })); put(path.join(dir, "report.json"), { ...basic, id: "synth-remote" }); const run = (name) => spawnSync(process.execPath, ["--import", "tsx", CHAIN_RUN, "step", "synth-remote", "1", name, "--projects-root", PROJECTS], { cwd: REPO_ROOT, env: ENV, encoding: "utf8", timeout: 120_000, }); for (const name of ["current", "manifest"]) { const r = run(name); assert.equal(r.status, 0, `${name}: ${r.stdout}\n${r.stderr}`); } const m = json(path.join(PROJECTS, "synth-remote", "i1", "video.manifest.json")); assert.equal(m.provenance.siteOrigin, "https://stub.test"); }); test("windows: an article swept over an archive that is no longer connected asks nothing of it, and says to connect it again (demo epic D1 re-check)", () => { // synth-remote's lineage read remote:https://stub.test (the test above); settings.json names no archive. const env = { ...ENV }; delete env.TRANSCRIPT_SITE_URL; const r = spawnSync(process.execPath, ["--import", "tsx", CHAIN_RUN, "step", "synth-remote", "1", "windows", "--projects-root", PROJECTS], { cwd: REPO_ROOT, env, encoding: "utf8", timeout: 120_000, }); assert.equal(r.status, 1, `${r.stdout}\n${r.stderr}`); const s = Object.fromEntries(json(path.join(ARTICLES, "synth-remote", "iterations", "1", "iteration.json")).steps.map((x) => [x.task, x]))["chain:windows"]; assert.equal(s.status, "failed"); assert.match(s.detail, /swept over a published archive, and its archive https:\/\/stub\.test is no longer connected — connect it again/); // Refused before the plan is written or the editor asked. assert.equal(existsSync(path.join(PROJECTS, "synth-remote", "i1", "windows.json")), false); }); test("cut-windows (Draft everything, demo epic D1): the cut's windows the project already holds are not asked of the editor", () => { // i2's project: its manifest (from the manifest test) and the cut's window in out/clips-raw. const project = path.join(PROJECTS, "synth-basic", "i2"); const m = json(path.join(project, "video.manifest.json")); const clips = (m.timeline ?? []).filter((e) => e.type === "clip"); assert.ok(clips.length > 0); for (const c of clips) { const from = Math.max(0, Math.floor((c.start - 3) * 100) / 100); const to = Math.ceil((c.end + 3) * 100) / 100; put(path.join(project, "out", "clips-raw", `${c.video}_${from.toFixed(2)}-${to.toFixed(2)}.mp4`), Buffer.alloc(64, 1)); } // The editor is a closed port: had the step asked it, it would have failed. const r = step(2, "cut-windows", "--mode", "draft-all"); assert.equal(r.code, 0, r.out); assert.match(recorded(2)["chain:cut-windows"].detail, /the cut's \d+ window\(s\) are on disk already/); });