// A THREAD READ's READINGS AND TUNINGS (lane line-rerun, S3 of the Orpheus re-tune plan): the whole read's, one // piece's (--for), each piece's (--tuned, `read.tuned`); the parts cache key unchanged when none is asked; a re-read // that joins the list unpicked. No editor (a stub tts); temp dirs; invented people. 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_B, syntheticVoice, withSilence } from "yt-dlp-transcript-common/testing/audio/syntheticVoice.mjs"; import { ThreadReadError, nextPartReading, partKey, threadReadPlan, threadTake, tuneSig, upToDate } from "./postThreadRead.mjs"; import { threadTunedOf, tuningFlags } from "./postNarrationCli.mjs"; import { playingTuned } from "./postScreensCli.mjs"; import { threadArgvOf } from "./castProject.mjs"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-thread-tuned-")); after(() => rm(ROOT, { recursive: true, force: true })); const wavOf = async (file, seconds = 0.6) => { await mkdir(path.dirname(file), { recursive: true }); await writeFile(file, encodeWav16(withSilence(syntheticVoice({ ...VOICE_B, seconds }), { before: 0.1, after: 0.1 }))); }; const stubAsk = (asked) => async (body) => { asked.push(body); await wavOf(body.out); return { jobId: `job-${asked.length}`, result: { wav: body.out, ...(body.reading !== undefined ? { reading: body.reading } : {}) } }; }; const NO_RED = { terms: [], markup: {}, slurs: null }; const IMG = { src: "posts-media/bridge-x/7/7_1.png", kind: "image", width: 900, height: 1400 }; const ITEM = { type: "post", id: "p-7", post: "bridge-x/7", platform: "x", author: "Bridge Fan", handle: "bridgefan", date: "2026-10-02T10:00:00.000Z", text: "Here is what they sent.", media: [IMG], narration: { script: "Here is what they sent." }, screens: [{ id: "s1", media: 0, kind: "dm", read: "all", lines: [{ who: "rivertalk", text: "Are the tolls going up?" }, { who: "bridgefan", text: "Not this year." }] }], }; async function project(name, item = ITEM) { const dir = path.join(ROOT, name); await mkdir(path.join(dir, "narration"), { recursive: true }); 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: "orpheus", voice: "zac" } }, timeline: [item], }, null, 2)); return { id: name, dir }; } const read = async (p) => JSON.parse(await readFile(path.join(p.dir, "video.manifest.json"), "utf8")); const LAYERS = { map: {}, cast: {}, item: null, names: {} }; test("partKey: no reading and no tuning (or reading 0, a tuning of the defaults) is the key as before -- literally", () => { assert.equal(partKey("orpheus", "zac", "Hello there."), "4346bef9f0b9c27145409883bb57d513ba3112f5"); assert.equal(partKey("orpheus", "zac", "Hello there.", null, { reading: 0, tuning: { temperature: 0.55, top_p: 0.85 } }), "4346bef9f0b9c27145409883bb57d513ba3112f5"); assert.equal(partKey("kokoro", "af_heart", "Hello there.", 1.1), "93d7f42c6ed1c5ac5fe0ae346a3ace3abd007740"); const r1 = partKey("orpheus", "zac", "Hello there.", null, { reading: 1 }); assert.notEqual(r1, "4346bef9f0b9c27145409883bb57d513ba3112f5"); assert.notEqual(partKey("orpheus", "zac", "Hello there.", null, { tuning: { temperature: 0.7 } }), r1); assert.equal(tuneSig({ reading: 2, tuning: { top_p: 0.9, temperature: 0.7 } }), '|r2|t{"temperature":0.7,"top_p":0.9}'); }); test("--reading/--for/--tuned as a thread read's tuned map; refused in their own words", () => { const flags = (o) => ({ str: (k) => (k in o ? o[k] : null), multi: (k) => o[`multi:${k}`] ?? [] }); const a = flags({ "--reading": "next", "--for": "screen:s1:1", "--temperature": "0.7" }); assert.deepEqual(threadTunedOf(tuningFlags(a.str), a), { "screen:s1:1": { reading: "next", tuning: { temperature: 0.7 } } }); const b = flags({ "--reading": "2" }); assert.deepEqual(threadTunedOf(tuningFlags(b.str), b), { "*": { reading: 2 } }); const c = flags({ "multi:--tuned": ["post=reading:3,top_p:0.9", "screen:s1:0=reading:1"] }); assert.deepEqual(threadTunedOf(null, c), { post: { reading: 3, tuning: { top_p: 0.9 } }, "screen:s1:0": { reading: 1 } }); assert.throws(() => threadTunedOf(null, flags({ "multi:--tuned": ["post=pitch:3"] })), /not one of reading, temperature/); assert.throws(() => threadTunedOf(null, flags({ "multi:--tuned": ["bogus=reading:2"] })), /not a piece of a read/); const ld = flags({ "--for": "linked:12", "--reading": "1" }); assert.throws(() => threadTunedOf(tuningFlags(ld.str), ld), /--for names one piece/, "a part's role names one digit for linked"); assert.equal(tuneSig({ tuning: { takes: 3, temperature: 0.7 } }), '|t{"temperature":0.7}', "takes 3 keys as an untuned part's"); assert.throws(() => threadTunedOf(null, flags({ "--for": "post" })), /--for goes with --reading/); const d = flags({ "--for": "nowhere", "--reading": "1" }); assert.throws(() => threadTunedOf(tuningFlags(d.str), d), /--for names one piece/); }); test("ONE LINE READ AGAIN: --for screen:s1:1 --reading next asks only that line (reading 1), the rest from the cache; the take joins unpicked and records the reading", async () => { const p = await project("one-line"); const asked = []; const first = await threadTake(p, (await read(p)).timeline[0], { red: NO_RED, ask: stubAsk(asked), lanes: null, ...LAYERS }); assert.equal(asked.length, 3); assert.ok(asked.every((b) => b.reading === undefined && b.tuning === undefined), "an untuned read asks as before"); const e1 = (await read(p)).timeline[0]; assert.equal(e1.narration.pick, 0); assert.equal(nextPartReading(e1, "screen:s1:1", first.plan.pieces.find((x) => x.role === "screen:s1:1").text), 1); const again = await threadTake(p, e1, { red: NO_RED, ask: stubAsk(asked), lanes: null, ...LAYERS, tuned: { "screen:s1:1": { reading: "next", tuning: { temperature: 0.7 } } }, keepPick: true }); assert.equal(asked.length, 4, "one part asked"); assert.deepEqual([asked[3].reading, asked[3].tuning], [1, { temperature: 0.7 }]); const e2 = (await read(p)).timeline[0]; assert.equal(e2.narration.takes.length, 2); assert.equal(e2.narration.pick, 0, "--no-pick: the playing take stays the one"); const t2 = e2.narration.takes[1]; assert.deepEqual(Object.keys(t2.read.tuned), ["screen:s1:1"]); assert.equal(t2.read.tuned["screen:s1:1"].reading, 1); assert.equal(t2.parts.find((x) => x.role === "screen:s1:1").reading, 1); assert.equal(t2.parts.find((x) => x.role === "post").reading, undefined); assert.ok(again.parts.filter((x) => x.role !== "screen:s1:1").every((x) => x.cached)); // Next again: 2. await threadTake(p, e2, { red: NO_RED, ask: stubAsk(asked), lanes: null, ...LAYERS, tuned: { "screen:s1:1": { reading: "next" } }, keepPick: true }); assert.equal(asked.at(-1).reading, 2); }); test("the playing read's readings are KEPT by a later read while each piece says the same words (posts screen read), and asked again exactly by --tuned (threadArgvOf)", async () => { const p = await project("keep"); const asked = []; await threadTake(p, (await read(p)).timeline[0], { red: NO_RED, ask: stubAsk(asked), lanes: null, ...LAYERS, tuned: { "screen:s1:0": { reading: 3 } } }); const e = (await read(p)).timeline[0]; const inherit = playingTuned(e); assert.equal(inherit["screen:s1:0"].reading, 3); // The same words: kept -- up to date, nothing asked. const r = await threadTake(p, e, { red: NO_RED, ask: stubAsk(asked), lanes: null, ...LAYERS, inherit }); assert.equal(r.upToDate, true); // The line's words changed: its reading is not carried to other words. const changed = { ...e, screens: [{ ...e.screens[0], lines: [{ who: "rivertalk", text: "Are the fares going up?" }, e.screens[0].lines[1]] }] }; const plan = threadReadPlan({ timeline: [changed], render: {} }, changed, { red: NO_RED, ...LAYERS, inherit }); assert.equal(plan.pieces.find((x) => x.role === "screen:s1:0").reading, undefined); assert.deepEqual(threadArgvOf(e.narration.takes[0].read).filter((x, i, a) => x === "--tuned" || a[i - 1] === "--tuned"), ["--tuned", "screen:s1:0=reading:3"]); }); test("UP TO DATE IS PER PIECE (review 1, finding 2): a line asked at another reading is never 'up to date' because another line already has that reading", () => { const mk = (r) => ["screen:s1:0", "screen:s1:1", "screen:s1:2"].map((role, i) => ({ role, voice: "leo", engine: "orpheus", text: `line ${i}`, file: `x${i}`, start: i, seconds: 1, ...(r[i] ? { reading: r[i] } : {}) })); const post = { role: "post", voice: "zac", engine: "orpheus", text: "his words", file: "p", start: 9, seconds: 1 }; const want = "his words\n\nline 0\n\nline 1\n\nline 2"; const e = { narration: { pick: 0, takes: [{ file: "t.wav", seconds: 5, script: want, voice: "zac+leo", engine: "orpheus", parts: [post, ...mk([0, 1, 0])], read: { kind: "thread" } }] } }; assert.equal(nextPartReading(e, "screen:s1:2", "line 2"), 1); assert.equal(upToDate(e, { want, voice: "zac+leo", engine: "orpheus", pieces: [post, ...mk([0, 1, 1])] }), false); assert.equal(upToDate(e, { want, voice: "zac+leo", engine: "orpheus", pieces: [post, ...mk([0, 1, 0])] }), true); }); test("A RECORDED PIECE asked to be read again is refused in a sentence; a read with nothing synthesized to read again too; an unknown piece too", async () => { const p = await project("recorded"); const asked = []; await threadTake(p, (await read(p)).timeline[0], { red: NO_RED, ask: stubAsk(asked), lanes: null, ...LAYERS }); const e = (await read(p)).timeline[0]; // His line from take 1 (a recording of his words would be read in his piece's place). await wavOf(path.join(p.dir, "narration/posts/7/take-9.recorded-me.wav")); const rec = { file: "narration/posts/7/take-9.recorded-me.wav", engine: "recorded", voice: "me", seconds: 1, script: "Here is what they sent.", source: { kind: "recorded", voice: "me" } }; const withRec = { ...e, narration: { ...e.narration, takes: [...e.narration.takes, rec] } }; await assert.rejects(() => threadTake(p, withRec, { red: NO_RED, ask: stubAsk(asked), lanes: null, ...LAYERS, postTake: 2, tuned: { post: { reading: "next" } } }), (err) => err instanceof ThreadReadError && /is read from take 2/.test(err.message)); await assert.rejects(() => threadTake(p, e, { red: NO_RED, ask: stubAsk(asked), lanes: null, ...LAYERS, tuned: { "screen:s9:0": { reading: "next" } } }), (err) => err instanceof ThreadReadError && /no piece screen:s9:0/.test(err.message)); });