// 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

read --dry-run` lists the pieces, her line from the take that plays", async () => { const item = { ...ITEM, screens: [{ id: "s1", media: 0, at: [3.5, 8], kind: "dm", read: [{ line: 0 }], lines: [{ who: "TheQuartering", text: "what's your email" }] }] }; await project("dry", item); const r = await posts(["screen", "dry", "p-2117", "read", "--dry-run", "--json"]); assert.equal(r.code, 0, r.text); const j = r.json(); assert.deepEqual(j.pieces.map((x) => [x.role, x.take?.n ?? null]), [["post", 1], ["screen:s1:0", null]]); assert.equal(j.pieces[1].voice, "am_michael"); }); // --------------------------------------------------------------------------- // REVIEW 1 (12 Nov): the read's side of the must-fixes. // --------------------------------------------------------------------------- const DM = (lines, read = "all") => ({ id: "s1", media: 0, at: [3.5, 8], kind: "dm", read, lines }); test("MUST 6: a --line-take whose take says something else than the line is REFUSED; one that says it records what the TAKE says", async () => { const item = { ...ITEM, screens: [DM([{ who: "TheQuartering", text: "You can't be this dumb" }, { who: "TheQuartering", text: "something else said here" }])] }; const p = await project("m6", item); const e = (await read(p)).timeline[0]; // Take 1 is the post's own read: not this line's words. await assert.rejects(() => threadTake(p, e, { red: NO_RED, ask: stubAsk([]), lanes: null, lineTakes: { "screen:s1:0": 1 } }), /--line-take screen:s1:0=1: its take does not say line 0 of screen s1/); // Take 2 says it (its script): the part's text is the TAKE's words, the line it stands for stored beside them. const r = await threadTake(p, e, { red: NO_RED, ask: stubAsk([]), lanes: null, postTake: 1, lineTakes: { "screen:s1:0": 2 } }); const part = r.take.parts.find((x) => x.role === "screen:s1:0"); assert.equal(part.text, T2.script); assert.equal(part.line, "You can't be this dumb"); assert.equal(r.take.read.lineTakes["screen:s1:0"].line, "You can't be this dumb"); }); test("MUST 1: each part of a screens read stores the line it was made from; the guard holds it to the line as it is now", async () => { const item = { ...ITEM, screens: [DM([{ who: "TheQuartering", text: "hop on the scale fatso" }, { who: "MadamSavvy", text: "you first" }])] }; const p = await project("m1", item); const r = await threadTake(p, (await read(p)).timeline[0], { red: NO_RED, ask: stubAsk([]), lanes: null, postTake: 1 }); assert.deepEqual(r.take.parts.filter((x) => x.role.startsWith("screen:")).map((x) => x.line), ["hop on the scale fatso", "you first"]); const now = (await read(p)).timeline[0]; assert.deepEqual(threadReadErrors(now, {}), []); const edited = structuredClone(now); edited.screens[0].lines[1].text = "you first, big boy"; assert.ok(threadReadErrors(edited, {}).some((x) => /line 1 of screen s1 changed since the read/.test(x))); // The line takes the read used are re-attached only while their line is the same. const withLt = structuredClone(now); withLt.narration.takes[withLt.narration.pick].read.lineTakes = { "screen:s1:1": { n: 2, file: T2.file, line: "you first" } }; assert.deepEqual(defaultLineTakes(withLt), { "screen:s1:1": 2 }); withLt.screens[0].lines[1].text = "you first, big boy"; assert.deepEqual(defaultLineTakes(withLt), {}); }); test("MUST 1 (race): the screens changed while the read was being made: it is not kept (run it again; its parts are cached)", async () => { const item = { ...ITEM, screens: [DM([{ who: "TheQuartering", text: "hop on the scale fatso" }])] }; const p = await project("race", item); const e = (await read(p)).timeline[0]; const ask = async (body) => { // A bench edit lands while the tts is busy. const m = await read(p); m.timeline[0].screens[0].lines[0].text = "hop on the scale, fatso, now"; await writeFile(path.join(p.dir, "video.manifest.json"), JSON.stringify(m, null, 2)); return stubAsk([])(body); }; await assert.rejects(() => threadTake(p, e, { red: NO_RED, ask, lanes: null, postTake: 1 }), /changed while its read was made/); assert.equal((await read(p)).timeline[0].narration.takes.length, 2, "nothing kept"); }); test("SHOULD a: `posts screen … read` keeps the playing thread read's options (parents, skips, voices, quoted)", async () => { const t = { ...T1, file: "narration/posts/2117/take-3.kokoro-thread.wav", read: { kind: "thread", parents: "all", skip: ["9"], quoted: false, linked: false, seen: true, voiceFor: { someone: "kokoro:bf_emma" }, voice: "af_bella", speed: 1.1 } }; const e = { ...ITEM, narration: { ...ITEM.narration, takes: [T1, T2, t], pick: 2 } }; assert.deepEqual(screenReadOptions(e, { flags: new Map(), flag: () => null, multi: () => [] }), { parents: "all", skip: ["9"], quoted: false, linked: false, readSeen: true, overrides: { someone: "kokoro:bf_emma" }, voice: "af_bella", speed: 1.1 }); // A flag given wins. const o = screenReadOptions(e, { flags: new Map([["--no-quoted", true]]), flag: (n) => (n === "--parents" ? "0" : null), multi: (n) => (n === "--voice-for" ? ["someone=kokoro:am_adam"] : []) }); assert.equal(o.parents, "0"); assert.deepEqual(o.overrides, { someone: "kokoro:am_adam" }); }); test("bench and CLI: removing a line remaps the read (one rule: withoutLines)", () => { const sc = DM([{ text: "a" }, { text: "b" }, { text: "c" }, { text: "d" }], [{ line: 0 }, { line: 2 }, { line: 3, text: "d" }]); assert.deepEqual(withoutLines(sc, [0]).read, [{ line: 1 }, { line: 2, text: "d" }]); assert.deepEqual(withoutLines(sc, [0]).lines.map((l) => l.text), ["b", "c", "d"]); assert.deepEqual(withoutLines({ ...sc, read: "all" }, [1]).read, "all"); }); test("SHOULD g: an item WITHOUT screens: removing or hiding an attachment is not held to the thread-read guard (as before)", async () => { const p = await project("g", { ...ITEM, narration: { ...ITEM.narration, takes: [{ ...T1, parts: [{ n: 1, para: 0, text: "words no card holds at all", start: 0, seconds: 1, role: "quoted" }] }], pick: 0 } }); await updatePostItem(p.dir, "p-2117", { media: { 1: null } }); assert.equal((await read(p)).timeline[0].media.length, 1); }); // --------------------------------------------------------------------------- // REVIEW 2 (12 Nov). // --------------------------------------------------------------------------- test("MUST 2 (q1): a line take must say the line -- at most one word more or less, never a shape, a handle or a redacted term", () => { const line = "please call me at tonight after work ok"; assert.equal(saysLine("please call me at 555 201 4477 tonight after work ok", line), false, "the masked number said"); assert.equal(saysLine("a b c d e f my ssn is 123456789", "a b c d e f"), false, "extra words"); assert.equal(saysLine("please call me at @secretkid tonight after work ok", line), false, "a handle"); assert.equal(saysLine("please call me at secretkid tonight after work ok", line), false, "a name-like extra word, with no set given (q1)"); assert.equal(saysLine("please call me at secretkid tonight after work ok", line, { handles: ["secretkid"] }), false, "an anonymous handle, bare"); assert.equal(saysLine("please call me at Jenny tonight after work ok", line, { terms: ["Jenny"] }), false, "a redaction term"); assert.equal(saysLine("please call me at tonight after work, okay", line), false, "a word swapped is an extra word (round 3: none allowed)"); assert.equal(saysLine("please call me tonight after work ok", line), true, "one word fewer"); assert.equal(saysLine("please call me tonight after ok", line), false, "two fewer"); }); test("SHOULD (re-import): the batch import keeps a part's line and speaker", () => { const [p] = normalizeParts([{ n: 1, para: 100, text: "x", start: 0, seconds: 1, role: "screen:s1:0", line: "hop on it", speaker: "TheQuartering" }]); assert.equal(p.line, "hop on it"); assert.equal(p.speaker, "TheQuartering"); }); test("ROUND 3 (q3): a line take says the line's words IN ORDER -- no extra word, at most one missing, never a negation missing", () => { assert.equal(saysLine("my pin is nine thanks", "my pin is thanks"), false, "a spelt digit as the extra word"); assert.equal(saysLine("I did not take it", "I did take it"), false, "meaning flipped by one short word"); assert.equal(saysLine("I never said no", "I never said yes"), false, "a substitution"); assert.equal(saysLine("ask jenny about it", "ask about it"), false, "a redacted short name, with no term given"); assert.equal(saysLine("ask savvy about it", "ask about it", { handles: ["madamsavvy"] }), false, "an anonymous author's short display name"); assert.equal(saysLine("ask JENNY about it", "ask about it", { terms: ["jenny"] }), false); assert.equal(saysLine("take it about ask", "ask about take it"), false, "out of order"); assert.equal(saysLine("I did take it", "I did not take it"), false, "a negation left out"); assert.equal(saysLine("I didn't take it", "I didn't take it"), true); assert.equal(saysLine("I take it", "I didn't take it"), false, "an n't word left out"); assert.equal(saysLine("I did take it", "I did take it now"), true, "one word fewer (not a negation)"); assert.equal(saysLine("Ask about it.", "ask about it"), true, "case and punctuation"); }); test("ROUND 3: the thread read refuses a line take that says a per-post redaction term (termsFor, not only the project's)", async () => { const item = { ...ITEM, screens: [DM([{ who: "TheQuartering", text: "You can't be this dumb" }])] }; const p = await project("r3", item); const e = (await read(p)).timeline[0]; // The post's own markup term (the item's, not the project's list) -- the take says it, the line as said leaves it out. const red = { ...NO_RED, markup: { [e.post]: { redact: [{ term: "dumb" }] } } }; await assert.rejects(() => threadTake(p, e, { red, ask: stubAsk([]), lanes: null, postTake: 1, lineTakes: { "screen:s1:0": 2 } }), /--line-take screen:s1:0=2: its take does not say line 0 of screen s1 \(it says a redacted term\)/); });