// A THREAD READ's writers and CLI (post-thread.mjs; postThreadCli.mjs, // postsCli.mjs set-item thread.*, posts narrate --cut thread.*, the import's // roles, manifest.mjs updatePostItem's guard): the roles stored, what each // context post is on screen set, and a setting that would narrate hidden text // refused with the fix. import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { after, test } from "node:test"; import { postsCommand } from "./postsCli.mjs"; import { postItemPatchOf } from "./postsCli.mjs"; import { mixPatchOf } from "./postNarrationCli.mjs"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-post-thread-")); after(() => rm(ROOT, { recursive: true, force: true })); const REPORTS = path.join(ROOT, "reports"); const PARENT = "The first thing anyone notices is the noise from the street.\n\nThen the lights in the hall go out without warning."; const REPLY = "That is exactly what I was saying yesterday morning."; const parts = () => [ { n: 1, para: 0, text: "The first thing anyone notices is the noise from the street.", start: 0, seconds: 3.6 }, { n: 2, para: 1, text: "Then the lights in the hall go out without warning.", start: 4.2, seconds: 3.2 }, { n: 3, para: 100, text: REPLY, start: 8.2, seconds: 3.1 }, ]; const item = (id, o = {}) => ({ type: "post", id: `p-${id}`, post: `subj-x/${id}`, platform: "x", author: "THE SUBJECT", handle: "Subject", date: "2026-10-30T10:00:00.000Z", text: REPLY, parents: [{ id: "900", author: "Neighbour", handle: "neighbour_one", date: "2026-10-29T10:00:00.000Z", text: PARENT }], ...o, }); const narrated = (id, o = {}) => item(id, { narration: { script: REPLY, takes: [{ file: `narration/posts/${id}/take-1.orpheus-thread.wav`, engine: "orpheus", voice: "leo+zac", seconds: 11.3, parts: parts() }], pick: 0 }, ...o }); async function project(name, timeline) { const dir = path.join(REPORTS, name); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: name, title: name, provenance: { siteOrigin: "" }, render: { width: 1920, height: 1080, fps: 30 }, timeline }, null, 2)); return { id: name, dir }; } const manifest = async (p) => JSON.parse(await readFile(path.join(p.dir, "video.manifest.json"), "utf8")); const cli = async (args) => { const lines = []; const errs = []; const code = await postsCommand(args, { root: REPORTS, out: (s) => lines.push(s), err: (s) => errs.push(s) }); return { code, out: lines.join("\n"), err: errs.join("\n") }; }; test("posts roles: the read piece by piece (inferred), --write stores them, --set sets one by hand", async () => { const p = await project("roles", [narrated("1")]); const ls = await cli(["roles", "roles"]); assert.equal(ls.code, 0, ls.err); assert.match(ls.out, /1 narrated item\(s\) read in parts, 1 threaded, 0 refused/); assert.match(ls.out, /parts 1,2 {2}parent:0 {2}inferred {2}shown full/); assert.match(ls.out, /parts 3 {2}post {2}inferred {2}shown full/); const w = await cli(["roles", "roles", "--write"]); assert.match(w.out, /stored the roles of 1 read/); assert.deepEqual((await manifest(p)).timeline[0].narration.takes[0].parts.map((x) => x.role), ["parent:0", "parent:0", "post"]); assert.match((await cli(["roles", "roles", "--write"])).out, /stored already/); const set = await cli(["roles", "roles", "p-1", "--set", "2=post"]); assert.equal(set.code, 0, set.err); assert.deepEqual((await manifest(p)).timeline[0].narration.takes[0].parts.map((x) => x.role), ["parent:0", "post", "post"]); assert.match((await cli(["roles", "roles", "p-1", "--set", "9=post"])).err, /parts 1-3/); assert.match((await cli(["roles", "roles", "p-1", "--set", "1=somewhere"])).err, /a role is post, quoted/); }); test("set-item thread.*: the knobs merge; hiding a parent the read reads is REFUSED with the fix; context for an unread one is fine", async () => { const p = await project("knobs", [narrated("1"), item("2", { narration: { script: REPLY, takes: [{ file: "narration/posts/2/take-1.orpheus-zac.wav", seconds: 3.1, parts: [{ n: 1, para: 0, text: REPLY, start: 0, seconds: 3.1 }] }], pick: 0 } })]); assert.deepEqual(postItemPatchOf(["thread.shrink=0.5", "thread.parent.1=hidden", "thread.carry=off", "thread.reveal=on"]).thread, { shrink: 0.5, parent: ["auto", "hidden"], carry: false, reveal: true }); assert.deepEqual(postItemPatchOf(["thread=auto"]), { thread: null }); const ok = await cli(["set-item", "knobs", "p-1", "thread.shrink=0.5", "thread.dim=0.4"]); assert.equal(ok.code, 0, ok.err); assert.deepEqual((await manifest(p)).timeline[0].thread, { shrink: 0.5, dim: 0.4 }); // --no-reread keeps the guard's refusal (without it, set-item re-joins the take first: postReread.test.mjs). const bad = await cli(["set-item", "knobs", "p-1", "thread.parent=hidden", "--no-reread"]); assert.notEqual(bad.code, 0); assert.match(bad.err, /part 1 reads the post it answers, which is drawn hidden.*thread\.parent=full/); assert.equal((await manifest(p)).timeline[0].thread.parent, undefined); // Follow off with a read parent: refused too. assert.match((await cli(["set-item", "knobs", "p-1", "follow=off"])).err, /read-follow is off/); // p-2 reads only his words: its parent may be context or hidden. assert.equal((await cli(["set-item", "knobs", "p-2", "thread.parent=hidden"])).code, 0); assert.equal((await manifest(p)).timeline[1].thread.parent, "hidden"); assert.equal((await cli(["set-item", "knobs", "p-1", "thread=auto"])).code, 0); assert.equal((await manifest(p)).timeline[0].thread, undefined); }); test("the cut's thread knobs (posts narrate --cut thread.*), and the import stores each part's role", async () => { assert.deepEqual(mixPatchOf(["thread.shrink=0.55", "thread.parent=excerpt", "thread.carry=auto", "thread.reveal=off"], { cut: true }).thread, { shrink: 0.55, parent: "excerpt", carry: "auto", reveal: false }); const p = await project("cut", [item("1")]); const r = await cli(["narrate", "cut", "--cut", "thread.shrink=0.55", "thread.pan=1.2"]); assert.equal(r.code, 0, r.err); assert.deepEqual((await manifest(p)).render.postNarration.thread, { shrink: 0.55, pan: 1.2 }); assert.notEqual((await cli(["narrate", "cut", "--cut", "thread.shrink=9"])).code, 0); // The import: a batch's take with its parts (no roles) comes in with each part's role stored. await mkdir(path.join(p.dir, "narration", "posts", "1"), { recursive: true }); execFileSync("ffmpeg", ["-v", "error", "-y", "-f", "lavfi", "-i", "sine=f=220:d=11.3", "-ar", "48000", "-ac", "1", path.join(p.dir, "narration", "posts", "1", "take-1.orpheus-thread.wav")]); await writeFile(path.join(p.dir, "narration", "posts", "index.json"), JSON.stringify([{ id: "1", entry: "p-1", script: REPLY, takes: [{ file: "narration/posts/1/take-1.orpheus-thread.wav", engine: "orpheus", voice: "leo+zac", seconds: 11.3, parts: parts().map((x, i) => ({ ...x, voice: i < 2 ? "leo" : "zac" })) }], pick: 0 }])); const imp = await cli(["narrate", "cut", "--import"]); assert.equal(imp.code, 0, imp.err); const got = (await manifest(p)).timeline[0].narration.takes[0].parts; assert.deepEqual(got.map((x) => [x.role, x.voice]), [["parent:0", "leo"], ["parent:0", "leo"], ["post", "zac"]]); }); test("timeline threads: the cut's conversations (split, tree order, a re-read with its fix); --write gathers one in tree order in ONE write, undoable", async () => { const { timelineCommand } = await import("./timelineCli.mjs"); const reply = (id, parents, text, date) => item(id, { text, date, parents, narration: { script: text, takes: [{ file: `narration/posts/${id}/take-1.orpheus-zac.wav`, engine: "orpheus", voice: "zac", seconds: 4, parts: [{ n: 1, para: 0, text, start: 0, seconds: 3.6 }] }], pick: 0 } }); const root = { id: "771", author: "THE SUBJECT", handle: "Subject", date: "2026-10-30T06:00:00.000Z", text: "The root of it all, said once and for everyone." }; const a = reply("776", [root], "His own reply under the root, the first branch.", "2026-10-30T06:20:00.000Z"); const b = reply("779", [{ id: "775", handle: "drac_two", text: "A stranger answering the root." }, root], "His answer to the stranger, the second branch.", "2026-10-30T06:27:00.000Z"); const x = reply("831", [{ id: "825", handle: "saber", text: "Another conversation entirely." }], "A reply in another conversation.", "2026-10-30T06:40:00.000Z"); const c = reply("848", [{ id: "834", handle: "fox_one", text: "A stranger answering his own reply." }, { id: "776", author: "THE SUBJECT", handle: "Subject", text: "His own reply under the root, the first branch." }], "His answer to the fox, under the first branch.", "2026-10-30T07:10:00.000Z"); const p = await project("threads", [a, b, x, c]); const run = async (args) => { const lines = []; const errs = []; const code = await timelineCommand(args, { root: REPORTS, out: (s) => lines.push(s), err: (s) => errs.push(s) }); return { code, out: lines.join("\n"), err: errs.join("\n") }; }; const ls = await run(["threads", "threads"]); assert.equal(ls.code, 0, ls.err); assert.match(ls.out, /1 conversation\(s\)/); assert.match(ls.out, /root 771 {2}3 items {2}SPLIT into 2 runs/); assert.match(ls.out, /tree order: p-776 p-848 p-779 {2}\(umtool timeline threads threads --write --conversation 771\)/); const w = await run(["threads", "threads", "--write", "--conversation", "p-848"]); assert.equal(w.code, 0, w.err); assert.match(w.out, /gathered 1 conversation\(s\)/); assert.deepEqual((await manifest(p)).timeline.map((e) => e.id), ["p-776", "p-848", "p-779", "p-831"]); assert.match((await run(["threads", "threads", "--write"])).out, /already together in tree order/); assert.notEqual((await run(["threads", "threads", "--write", "--conversation", "nope"])).code, 0); await run(["threads", "undo"]); assert.deepEqual((await manifest(p)).timeline.map((e) => e.id), ["p-776", "p-779", "p-831", "p-848"], "one undo point"); }); test("set-item's censor knobs: a band in one knob, its edge, its style; off clears them", () => { assert.deepEqual(postItemPatchOf(["media.0.censor=below:0.38:blur"]).media, { 0: { blur: { on: true, band: { below: 0.38 }, style: "blur" } } }); assert.deepEqual(postItemPatchOf(["media.1.censor=between:0.2,0.6:pixelate:8"]).media, { 1: { blur: { on: true, band: { between: [0.2, 0.6] }, style: "pixelate", strength: 8 } } }); assert.deepEqual(postItemPatchOf(["media.0.censor=off"]).media, { 0: { blur: { on: false, band: null, style: null } } }); assert.deepEqual(postItemPatchOf(["media.0.blur.band=above:0.3:left:0.5", "media.0.blur.style=black"]).media, { 0: { blur: { band: { above: 0.3, left: 0.5 }, style: "black" } } }); assert.deepEqual(postItemPatchOf(["media.0.blur.band=off", "media.0.blur.style=off"]).media, { 0: { blur: { band: null, style: null } } }); assert.throws(() => postItemPatchOf(["media.0.censor=below"]), /takes a fraction/); assert.throws(() => postItemPatchOf(["media.0.blur.style=below:0.3"]), /blur, black or pixelate/); });