// A THREAD READ with his own line from an existing take (postThreadRead.mjs --post-take): the parent synthesized in // its author's voice, his line the take's file as one part; the parts say where each came from; a thread take, a take // out of range, a lost file refused; up to date only for the same take; the drift guard reads the take's part as the // take it is; a re-read names it again; --speed still reaches the synthesized parts. 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 { takeReads } from "umtool-report-to-video/cast"; import { validatePostNarration } from "umtool-report-to-video/post-narration"; import { postsCommand } from "./postsCli.mjs"; import { postTakeOf, threadTake, upToDate, threadReadPlan } from "./postThreadRead.mjs"; import { threadArgvOf } from "./castProject.mjs"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-post-take-")); 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.2, after: 0.2 }))); }; const stubAsk = (asked) => async (body) => { asked.push(body); await wavOf(body.out, VOICE_B, 1); return { jobId: `job-${asked.length}`, result: { wav: body.out } }; }; const DIR = path.join(ROOT, "pt"); const project = { id: "pt", dir: DIR }; // p-1 answers @guest; its takes: 1 a recording, 2 that recording converted to a stock voice, 3 a thread read. const T1 = { file: "narration/posts/1/take-1.recorded.wav", engine: "recorded", voice: "operator", seconds: 1.6, script: "My own line, recorded.", source: { kind: "recorded", voice: "operator" } }; const T2 = { file: "narration/posts/1/take-2.derived.wav", engine: "derived", voice: "am_michael", seconds: 1.6, script: "My own line, recorded.", source: { kind: "take", from: "take-1.recorded" }, steps: [{ op: "convert", voice: "kokoro:am_michael" }] }; const T3 = { file: "narration/posts/1/take-3.kokoro-thread.wav", engine: "kokoro", voice: "af_heart+am_michael", seconds: 3, script: "x", read: { kind: "thread", parents: "0" } }; { await mkdir(DIR, { recursive: true }); for (const t of [T1, T2, T3]) await wavOf(path.join(DIR, t.file), VOICE_A, 1.2); await writeFile(path.join(DIR, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: "pt", title: "Post take", provenance: { channelsDir: "../channels", channelSlug: "chan" }, render: { width: 1920, height: 1080, fps: 30, postNarration: { engine: "kokoro", voice: "af_heart" } }, timeline: [{ type: "post", id: "p-1", post: "chan/1", platform: "x", author: "Host", handle: "host", date: "2026-10-30T10:00:00.000Z", text: "my words", parents: [{ id: "20", handle: "guest", author: "The Guest", date: "2026-10-30T09:00:00.000Z", text: "the guest asks a question" }], narration: { script: "My own line, recorded.", takes: [T1, T2, T3], pick: 1 }, }], }, null, 2)); await mkdir(path.join(DIR, "narration"), { recursive: true }); await writeFile(path.join(DIR, "narration", "voices.json"), JSON.stringify({ guest: "kokoro:bf_emma" })); } const manifest = async () => JSON.parse(await readFile(path.join(DIR, "video.manifest.json"), "utf8")); const item = async () => (await manifest()).timeline[0]; 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("the take a thread read's own line may be: 1-based as --list numbers them; a thread take or one out of range refused", async () => { const e = await item(); assert.equal(postTakeOf(e, 2).take.file, T2.file); assert.throws(() => postTakeOf(e, 0), /--post-take takes a take's number/); assert.throws(() => postTakeOf(e, 4), /has 3 take\(s\)/); assert.throws(() => postTakeOf(e, 3), /is a thread read itself/); assert.throws(() => postTakeOf(e, "x"), /--post-take takes/); }); test("--post-take: the parent synthesized, his line the take; each part says where it came from; up to date for the same take only", async () => { const e = await item(); const asked = []; const r = await threadTake(project, e, { red: null, ask: stubAsk(asked), lanes: null, postTake: 2, speed: 1.2 }); assert.deepEqual(asked.map((b) => [b.voice, b.speed, b.text]), [["bf_emma", 1.2, "the guest asks a question"]], "only the parent asked of the editor, at --speed"); assert.deepEqual(r.take.parts.map((p) => [p.role, p.voice, p.file.startsWith("narration/parts-cache/") ? "cache" : p.file, p.take ?? null]), [ ["parent:0", "bf_emma", "cache", null], ["post", "am_michael", T2.file, "take-2.derived"], ]); assert.equal(r.take.parts[1].text, "My own line, recorded."); assert.deepEqual(r.take.read.postTake, { n: 2, file: T2.file }); assert.equal(r.take.read.speed, 1.2); assert.equal(r.take.engine, "kokoro", "the synthesized pieces' engine names the take"); assert.match(r.take.file, /take-4\.kokoro-thread\.wav$/); const after1 = await item(); assert.deepEqual(validatePostNarration(after1), [], "the manifest takes it"); assert.equal(after1.narration.pick, 3, "picked"); assert.equal(after1.narration.script, "My own line, recorded.", "his script untouched"); // Up to date: the same take, the same words -- another take is not. const again = await threadTake(project, after1, { red: null, ask: stubAsk(asked), lanes: null, postTake: 2, speed: 1.2 }); assert.equal(again.upToDate, true); assert.equal(upToDate(after1, threadReadPlan(await manifest(), after1, { map: { guest: "kokoro:bf_emma" }, speed: 1.2, postTake: postTakeOf(after1, 1) })), false); assert.equal(upToDate(after1, threadReadPlan(await manifest(), after1, { map: { guest: "kokoro:bf_emma" }, speed: 1.2 })), false, "a synthesized line is not the take's"); // The drift guard reads his part as the converted take it is; the parent as synthesized. const reads = takeReads(after1); assert.deepEqual(reads.map((x) => [x.role, x.kind, x.voice]), [["parent:0", "tts", "bf_emma"], ["post", "converted", "am_michael"]]); // A re-read names the same take again, by its number now. assert.deepEqual(threadArgvOf(after1.narration.takes[3].read, { takes: after1.narration.takes }), ["--speed", "1.2", "--post-take", "2"]); }); test("through the CLI: --post-take on a thread read; refused off one, on a thread take, and for a lost file", async () => { let c = await posts(["narrate", "pt", "p-1", "--take", "--thread", "--post-take", "1", "--dry-run", "--json"]); assert.equal(c.code, 0, c.text); assert.deepEqual(c.json().pieces.map((p) => [p.role, p.voice, p.take?.n ?? null]), [["parent:0", "bf_emma", null], ["post", "operator", 1]]); assert.deepEqual(c.json().parts.map((p) => p.take ?? null), [null, "take-1.recorded"]); c = await posts(["narrate", "pt", "p-1", "--take", "--post-take", "1"]); assert.equal(c.code, 2); assert.match(c.text, /--post-take is a thread read's/); c = await posts(["narrate", "pt", "p-1", "--take", "--thread", "--post-take", "3", "--dry-run", "--json"]); assert.equal(c.code, 1); assert.match(c.json().error, /is a thread read itself/); await rm(path.join(DIR, T1.file)); c = await posts(["narrate", "pt", "p-1", "--take", "--thread", "--post-take", "1", "--json"]); assert.equal(c.code, 1); assert.match(c.json().error, /its file .* is not there/); });