// A post's SCREENS from the CLI and in the read (postScreensCli.mjs, postThreadRead.mjs; post-screens.mjs): the text // lines a person pastes, the `set` keys, the writer (one write, refused in the build's words; an attachment a screen // is in cannot go, the others' indices follow), check / rm / list, and the THREAD READ with screens -- each line in // its speaker's voice, her own lines in hers, a line from an existing take (--line-take), her own line from the take // that plays (never made again), the short pause between lines, --no-screens. No editor (a stub tts); temp dirs. import assert from "node:assert/strict"; 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 { encodeWav16 } from "yt-dlp-transcript-common/lib/montage/voiceMatch.mjs"; import { VOICE_A, VOICE_B, syntheticVoice, withSilence } from "yt-dlp-transcript-common/testing/audio/syntheticVoice.mjs"; import { postsCommand } from "./postsCli.mjs"; import { applyPairs, defaultLineTakes, defaultPostTake, lineOfText, linesOfText, screenReadOptions } from "./postScreensCli.mjs"; import { withoutLines } from "umtool-report-to-video/post-screens"; import { characterNames, joinTimes, readParts, saysLine, threadReadPlan, threadTake } from "./postThreadRead.mjs"; import { normalizeParts } from "./postNarrationCli.mjs"; import { threadArgsOf } from "./postSay.mjs"; import { threadArgvOf } from "./castProject.mjs"; import { updatePostItem } from "./manifest.mjs"; import { threadReadErrors } from "umtool-report-to-video/post-thread"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-screens-")); after(() => rm(ROOT, { recursive: true, force: true })); const wavOf = async (file, voice, seconds = 1.2) => { await mkdir(path.dirname(file), { recursive: true }); await writeFile(file, encodeWav16(withSilence(syntheticVoice({ ...voice, seconds }), { before: 0.1, after: 0.1 }))); }; const stubAsk = (asked) => async (body) => { asked.push(body); await wavOf(body.out, VOICE_B, 0.6); return { jobId: `job-${asked.length}`, result: { wav: body.out } }; }; const NO_RED = { terms: [], markup: {}, slurs: null }; const VID = { src: "posts-media/savvy-x/2117/2117_1.mp4", kind: "video", width: 1920, height: 1080, seconds: 61.742, audio: true }; const IMG = { src: "posts-media/savvy-x/2117/2117_2.jpg", kind: "image", width: 900, height: 1400 }; const T1 = { file: "narration/posts/2117/take-1.recorded.wav", engine: "recorded", voice: "operator", seconds: 1.4, script: "Hey, you told me. Your turn.", source: { kind: "recorded", voice: "operator" } }; const T2 = { file: "narration/posts/2117/take-2.recorded-tq.wav", engine: "recorded", voice: "tq", seconds: 1.0, script: "You can't be this dumb", source: { kind: "recorded", voice: "tq" } }; const ITEM = { type: "post", id: "p-2117", post: "savvy-x/2117", platform: "x", author: "Savvy", handle: "MadamSavvy", date: "2026-11-02T01:40:36.000Z", text: "Hey, you told me. Your turn.", subjects: ["MadamSavvy"], media: [VID, IMG], narration: { script: "Hey, you told me. Your turn.", takes: [T1, T2], pick: 0 }, }; async function project(name, item = ITEM) { const dir = path.join(ROOT, name); await mkdir(dir, { recursive: true }); for (const t of item.narration?.takes ?? []) await wavOf(path.join(dir, t.file), VOICE_A, 1); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: name, title: name, provenance: { channelsDir: "../channels", channelSlug: "chan" }, render: { width: 1920, height: 1080, fps: 30, postNarration: { engine: "kokoro", voice: "af_heart" } }, timeline: [item], }, null, 2)); await mkdir(path.join(dir, "narration"), { recursive: true }); await writeFile(path.join(dir, "narration", "voices.json"), JSON.stringify({ thequartering: "kokoro:am_michael" })); return { id: name, dir }; } const read = async (p) => JSON.parse(await readFile(path.join(p.dir, "video.manifest.json"), "utf8")); const posts = async (args) => { const lines = []; const code = await postsCommand(args, { root: ROOT, out: (s) => lines.push(s), err: (s) => lines.push(s) }); return { code, text: lines.join("\n"), json: () => JSON.parse(lines.join("\n")) }; }; test("text lines as a person pastes them: `[right] who: words | time`, `- words`, `[redact] …`", () => { assert.deepEqual(lineOfText("TheQuartering: what's your email | 11:32 AM"), { who: "TheQuartering", text: "what's your email", time: "11:32 AM" }); assert.deepEqual(lineOfText("[right] MadamSavvy: where is my scale video"), { who: "MadamSavvy", side: "right", text: "where is my scale video" }); assert.deepEqual(lineOfText("- Total amount refunded: $99.95"), { text: "Total amount refunded: $99.95" }); assert.deepEqual(lineOfText("[redact] Order #4419283"), { text: "Order #4419283", redact: true }); assert.deepEqual(lineOfText("see https://x.com/a"), { text: "see https://x.com/a" }); assert.equal(linesOfText("a: b\n\n \nc: d\n").length, 2); }); test("set keys: kind, title, at, read, an excerpt, a line's who / side / redact, a line added and one removed (the read follows)", () => { const sc = { id: "s1", media: 0, at: [1, 2], kind: "dm", read: "all", lines: [{ who: "a", text: "one two three" }, { who: "b", text: "four" }, { who: "a", text: "five six" }] }; const x = applyPairs(sc, ["kind=text", "title=\"A note\"", "at=3.5,8", "read=0,2", "read.0=\"two three\"", "line.1.side=right", "line.1.redact=on", "line.add=\"c: seven\""]); assert.deepEqual([x.kind, x.title, x.at], ["text", "A note", [3.5, 8]]); assert.deepEqual(x.read, [{ line: 0, text: "two three" }, { line: 2 }]); assert.deepEqual(x.lines[1], { who: "b", text: "four", side: "right", redact: true }); assert.deepEqual(x.lines[3], { who: "c", text: "seven" }); const y = applyPairs(x, ["line.0=off"]); assert.deepEqual(y.read, [{ line: 1 }]); assert.equal(y.lines[0].who, "b"); assert.throws(() => applyPairs(sc, ["colour=red"]), /not a screen's knob/); assert.throws(() => applyPairs(sc, ["line.9.who=x"]), /the screen has lines 0–2/); assert.equal(sc.kind, "dm", "a copy"); }); test("the CLI: add from a pasted file, list, set, check, rm -- one write each, refused in the build's words; the item's attachments guarded", async () => { const p = await project("cli"); const file = path.join(ROOT, "s1.txt"); await writeFile(file, "TheQuartering: what's your email so I can refund your order | 11:32 AM\nMadamSavvy: where is my scale video | 11:53 AM\nTheQuartering: You can't be this dumb\n"); let r = await posts(["screen", "cli", "p-2117", "add", "--media", "0", "--at", "3.5,8", "--kind", "dm", "--from", file, "--json"]); assert.equal(r.code, 0, r.text); const j = r.json(); assert.equal(j.screens[0].id, "s1"); assert.equal(j.screens[0].lines.length, 3); assert.ok(j.warnings.some((w) => /transcription not checked/.test(w))); assert.ok(j.warnings.some((w) => /does not read screen s1/.test(w))); r = await posts(["screen", "cli", "p-2117", "add", "--media", "1", "--kind", "email", "--line", "coffee: Some items in your order have been refunded", "--line", "- Order number 4419283", "--read", "0"]); assert.equal(r.code, 0, r.text); assert.match(r.text, /added screen s2/); assert.match(r.text, /NOT CHECKED/); // Refused in the build's words: an excerpt not in the line. r = await posts(["screen", "cli", "p-2117", "set", "s1", "read.0=\"refund my email\""]); assert.equal(r.code, 1); assert.match(r.text, /is not a run of line 0's words/); r = await posts(["screen", "cli", "p-2117", "check", "s1", "--by", "op"]); assert.equal(r.code, 0, r.text); assert.equal((await read(p)).timeline[0].screens[0].checked.by, "op"); // A word changed: the check goes. r = await posts(["screen", "cli", "p-2117", "set", "s1", "line.2.text=You cant be this dumb"]); assert.equal(r.code, 0, r.text); assert.equal((await read(p)).timeline[0].screens[0].checked, undefined); r = await posts(["screen", "cli", "p-2117", "list"]); assert.match(r.text, /s1 {2}dm {2}attachment 0 \(video\) at 3\.5–8 s/); assert.match(r.text, /\* {2}0 TheQuartering: what's your email/); // The attachment a screen is in cannot go; one before it moves its index. await assert.rejects(() => updatePostItem(p.dir, "p-2117", { media: { 1: null } }), /attachment 1 carries screen s2/); r = await posts(["screen", "cli", "p-2117", "rm", "s2"]); assert.equal(r.code, 0, r.text); await updatePostItem(p.dir, "p-2117", { mediaAdd: [IMG] }); await updatePostItem(p.dir, "p-2117", { media: { 0: null } }).catch((e) => assert.match(String(e.errors ?? e.message), /attachment 0 carries screen s1/)); // --dry-run checks and writes nothing. r = await posts(["screen", "cli", "p-2117", "add", "--media", "1", "--kind", "dm", "--line", "a: b", "--dry-run"]); assert.equal(r.code, 0, r.text); assert.equal((await read(p)).timeline[0].screens.length, 1); r = await posts(["screen", "cli", "p-2117", "frob"]); assert.equal(r.code, 2); }); test("the read: after her words, each line in its speaker's voice (hers in hers), a character's name resolved, --no-screens, the pauses", async () => { const e = { ...ITEM, screens: [{ id: "s1", media: 0, at: [3.5, 8], kind: "dm", read: "all", lines: [ { who: "TheQuartering", text: "what's your email so I can refund your order" }, { who: "MadamSavvy", text: "where is my scale video" }, { who: "Jeremy", text: "You can't be this dumb" }, ] }] }; const m = { render: { postNarration: { engine: "kokoro", voice: "af_heart" } }, timeline: [e] }; const plan = threadReadPlan(m, e, { red: NO_RED, map: { thequartering: "kokoro:am_michael" }, names: characterNames({ cast: { characters: [{ id: "tq", name: "Jeremy", handles: ["TheQuartering"] }] } }) }); assert.deepEqual(plan.pieces.map((p) => [p.role, p.voice]), [["post", "af_heart"], ["screen:s1:0", "am_michael"], ["screen:s1:1", "af_heart"], ["screen:s1:2", "am_michael"]]); assert.equal(threadReadPlan(m, e, { red: NO_RED, screens: false }).pieces.length, 1); const parts = readParts(plan); const { meta } = joinTimes(parts, parts.map(() => 1)); assert.deepEqual(meta.map((x) => x.gapBefore), [0, 0.8, 0.35, 0.35]); assert.deepEqual(meta.map((x) => x.role), ["post", "screen:s1:0", "screen:s1:1", "screen:s1:2"]); }); test("the read made: her line from the take that plays (not made again), a line from a recorded take (--line-take), each part's role stored; the guard is quiet", async () => { const item = { ...ITEM, screens: [{ id: "s1", media: 0, at: [3.5, 8], kind: "dm", read: "all", lines: [ { who: "TheQuartering", text: "what's your email so I can refund your order" }, { who: "MadamSavvy", text: "where is my scale video" }, { who: "TheQuartering", text: "You can't be this dumb" }, ] }] }; const p = await project("made", item); assert.equal(defaultPostTake(item), 1); const asked = []; const r = await threadTake(p, (await read(p)).timeline[0], { red: NO_RED, ask: stubAsk(asked), lanes: null, postTake: defaultPostTake(item), lineTakes: { "screen:s1:2": 2 } }); assert.equal(asked.length, 2, "only the two lines no take reads are synthesized"); const take = r.take; assert.deepEqual(take.parts.map((x) => [x.role, x.take ?? null]), [["post", "take-1.recorded"], ["screen:s1:0", null], ["screen:s1:1", null], ["screen:s1:2", "take-2.recorded-tq"]]); assert.equal(take.parts[3].text, "You can't be this dumb"); assert.deepEqual(take.read.lineTakes, { "screen:s1:2": { n: 2, file: T2.file, line: "You can't be this dumb" } }); assert.equal(take.read.postTake.file, T1.file); const after1 = (await read(p)).timeline[0]; assert.deepEqual(threadReadErrors(after1, {}), []); // Asked again: up to date (the same takes, the same words). const again = await threadTake(p, after1, { red: NO_RED, ask: stubAsk(asked), lanes: null, postTake: defaultPostTake(after1), lineTakes: { "screen:s1:2": 2 } }); assert.equal(again.upToDate, true); // The flags that ask for it again carry the line take; the default for her own line is the take the thread read used. assert.match(threadArgsOf(take.read), /--line-take screen:s1:2=2/); assert.deepEqual(threadArgvOf(take.read, { takes: after1.narration.takes }).slice(-4), ["--post-take", "1", "--line-take", "screen:s1:2=2"]); assert.equal(defaultPostTake(after1), 1); // A line take that is a thread read, or out of range, is refused. await assert.rejects(() => threadTake(p, after1, { red: NO_RED, ask: stubAsk(asked), lanes: null, lineTakes: { "screen:s1:2": 9 } }), /--line-take screen:s1:2=/); }); test("the CLI's read: `posts screen