// The manifest writer is the operator (release 28, M0): every umtool write // that changes a drafted field removes its drafted mark -- one test per field // -- an unrelated write keeps every mark, a re-save of the same value keeps // it, an orphan is dropped, and "Use this" (writeDraftedField) writes an // alternative as the operator's. Over the S0 kit's `full` manifest. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { readFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { fileURLToPath } from "node:url"; import { MANIFEST_NAME, StructureRefused, moveEntry, updateChrome, updateNarration, updateOnscreen, updateTeaser, writeDraftedField, } from "./manifest.mjs"; import { lintManifest } from "umtool-report-to-video/lint"; const HERE = path.dirname(fileURLToPath(import.meta.url)); const full = () => JSON.parse(readFileSync(path.join(HERE, "../../../common/testing/draft/manifests/full.video.manifest.json"), "utf8")); const P = (task) => ({ task, model: "qwen3:8b", at: "2026-10-13T10:00:00.000Z", iteration: 1, attempt: 1 }); // Every field drafted: c02's on-screen text, the contest card's narration, // the teaser's lines, the threads, the cut's order, the YouTube text. function drafted() { const m = full(); const e = (id) => m.timeline.find((x) => x.id === id); e("c02").onscreen = { title: "The oven" }; e("c02").drafted = { onscreen: P("onscreen") }; e("the-contest").drafted = { narration: P("narrate") }; e("fin").drafted = { lines: P("teaser") }; m.publish = { youtube: { title: "T", description: "D" } }; m.drafted = { timeline: P("cut"), "render.chrome.threads": P("threads"), "publish.youtube": P("youtube") }; return m; } async function project(t, manifest = drafted()) { const dir = await mkdtemp(path.join(tmpdir(), "umtool-drafted-")); t.after(() => rm(dir, { recursive: true, force: true })); await writeFile(path.join(dir, MANIFEST_NAME), JSON.stringify(manifest, null, 2) + "\n"); const read = async () => JSON.parse(await readFile(path.join(dir, MANIFEST_NAME), "utf8")); return { dir, read }; } /** Every mark, as `:`. */ const marks = (m) => [ ...m.timeline.flatMap((e) => Object.keys(e.drafted ?? {}).map((f) => `${e.id}:${f}`)), ...Object.keys(m.drafted ?? {}).map((f) => `*:${f}`), ]; const ALL = ["c02:onscreen", "the-contest:narration", "fin:lines", "*:timeline", "*:render.chrome.threads", "*:publish.youtube"]; test("each field: the operator's edit removes its mark, and only its own", async (t) => { const edits = [ ["on-screen text", (dir) => updateOnscreen(dir, { c02: { title: "Mine" } }), "c02:onscreen"], ["teaser lines", (dir) => updateTeaser(dir, "fin", { lines: ["One", "Two", "Three"] }), "fin:lines"], ["narration words", (dir) => updateNarration(dir, "the-contest", "My own words."), "the-contest:narration"], ["the cut's order", (dir) => moveEntry(dir, "c03", 3), "*:timeline"], ["the threads", async (dir) => { const m = JSON.parse(await readFile(path.join(dir, MANIFEST_NAME), "utf8")); const chrome = { ...m.render.chrome, threads: { list: [...m.render.chrome.threads.list.map((x) => ({ ...x })), { id: "lease", label: "The lease" }] } }; return updateChrome(dir, chrome); }, "*:render.chrome.threads"], ["the YouTube text", (dir) => writeDraftedField(dir, { field: "publish.youtube", value: { title: "Mine", description: "D" } }), "*:publish.youtube"], ]; for (const [what, edit, mark] of edits) { const p = await project(t); assert.deepEqual(marks(await p.read()), ALL); await edit(p.dir); assert.deepEqual(marks(await p.read()), ALL.filter((x) => x !== mark), what); } }); test("a write that leaves a drafted field as it was keeps its mark; an orphan is dropped", async (t) => { const p = await project(t); // c01's text (the operator's) changes; c02's re-saved exactly as it is. await updateOnscreen(p.dir, { c01: { title: "Edited" }, c02: { title: "The oven" } }); assert.deepEqual(marks(await p.read()), ALL); // A chrome save that leaves the threads alone keeps their mark. const m = await p.read(); await updateChrome(p.dir, { ...m.render.chrome, factcheck: { stamp: { seconds: 2 } } }); assert.deepEqual(marks(await p.read()), ALL); // Clearing c02's text leaves its mark an orphan: dropped by the write. await updateOnscreen(p.dir, { c02: null }); assert.deepEqual(marks(await p.read()), ALL.filter((x) => x !== "c02:onscreen")); }); test("writeDraftedField: Use this writes an alternative as the operator's, checked as its editor checks it", async (t) => { const p = await project(t); // c01's text is the operator's; an alternative replaces it, unmarked. await writeDraftedField(p.dir, { entry: "c01", field: "onscreen", value: { title: "Not for sale?" } }); const m = await p.read(); assert.deepEqual(m.timeline.find((e) => e.id === "c01").onscreen, { title: "Not for sale?" }); assert.equal(m.timeline.find((e) => e.id === "c01").drafted, undefined); // A drafted field taken from an alternative is the operator's too. await writeDraftedField(p.dir, { entry: "fin", field: "lines", value: ["Synth Kitchen", "Next: the lease"] }); assert.ok(!marks(await p.read()).includes("fin:lines")); // Refusals leave the file as it was. const before = await readFile(path.join(p.dir, MANIFEST_NAME), "utf8"); await assert.rejects(writeDraftedField(p.dir, { entry: "fin", field: "lines", value: [] }), StructureRefused); await assert.rejects(writeDraftedField(p.dir, { field: "timeline", value: [] }), /applied by the cut task/); await assert.rejects(writeDraftedField(p.dir, { entry: "c01", field: "quote", value: "x" }), /not a drafted field/); await assert.rejects(writeDraftedField(p.dir, { field: "publish.youtube", value: { title: 3 } }), StructureRefused); assert.equal(await readFile(path.join(p.dir, MANIFEST_NAME), "utf8"), before); }); test("writeDraftedField: a threads alternative's clips set each clip's thread with the list (release 28 M9)", async (t) => { const p = await project(t); const clipIds = (await p.read()).timeline.filter((e) => e.type === "clip").map((e) => e.id); const [first, ...others] = clipIds; const clips = Object.fromEntries([[first, "solo"], ...others.map((id) => [id, "rest"])]); const list = [{ id: "solo", label: "One clip" }, { id: "rest", label: "The others" }]; await writeDraftedField(p.dir, { field: "render.chrome.threads", value: { list, clips } }); const m = await p.read(); assert.deepEqual(m.render.chrome.threads, { list }, "the field is the list alone"); assert.equal(m.timeline.find((e) => e.id === first).thread, "solo"); assert.ok(others.every((id) => m.timeline.find((e) => e.id === id).thread === "rest")); assert.ok(!marks(m).includes("*:render.chrome.threads"), "taken, it is the operator's"); // A clip the cut lacks, or a thread left with no clip, is refused; the file stays. const before = await readFile(path.join(p.dir, MANIFEST_NAME), "utf8"); await assert.rejects(writeDraftedField(p.dir, { field: "render.chrome.threads", value: { list, clips: { nope: "solo" } } }), /clips this cut does not have: nope/); await assert.rejects( writeDraftedField(p.dir, { field: "render.chrome.threads", value: { list, clips: Object.fromEntries(clipIds.map((id) => [id, "rest"])) } }), /thread solo has no clip/, ); assert.equal(await readFile(path.join(p.dir, MANIFEST_NAME), "utf8"), before); }); test("writeDraftedField: Use this on a social post (release 29 G2) writes it as the operator's, within its platform's count", async (t) => { const m = drafted(); m.publish.x = { post: "The drafted post." }; m.drafted["publish.x"] = P("social"); const p = await project(t, m); await writeDraftedField(p.dir, { field: "publish.x", value: { post: "My post.\n\nhttps://archilyzer.example/articles/a/", thread: ["Two."] } }); const after = await p.read(); assert.deepEqual(after.publish.x, { post: "My post.\n\nhttps://archilyzer.example/articles/a/", thread: ["Two."] }); assert.equal(after.drafted["publish.x"], undefined, "the operator's now"); assert.deepEqual(after.publish.youtube, { title: "T", description: "D" }, "the other publish keys kept"); await writeDraftedField(p.dir, { field: "publish.bluesky", value: { post: "A Bluesky post." } }); assert.equal((await p.read()).publish.bluesky.post, "A Bluesky post."); await assert.rejects( writeDraftedField(p.dir, { field: "publish.bluesky", value: { post: "x".repeat(301) } }), (e) => e instanceof StructureRefused && /publish\.bluesky\.post is 301 graphemes \(at most 300\)/.test(e.message), ); }); test("a social post past its count is a lint warning, never a refusal of the cut's edits (release 29 review)", async (t) => { const m = drafted(); m.publish.x = { post: "x".repeat(300) }; m.publish.videoUrl = "not a url"; const p = await project(t, m); // A structural edit of the cut goes through. await moveEntry(p.dir, "c03", 3); const after = await p.read(); assert.equal(after.publish.x.post.length, 300); const lint = lintManifest(after, { validators: true }); assert.ok(lint.warnings.some((w) => /publish\.x\.post is 300 characters as X counts them/.test(w)), lint.warnings.join("\n")); assert.ok(lint.warnings.some((w) => /publish\.videoUrl must be the video's http\(s\) URL/.test(w))); assert.ok(!lint.errors.some((e) => /publish\./.test(e)), lint.errors.join("\n")); });