// A RE-READ COMES BACK FRESH (lane reads-find, the orchestrator's ruling 2): a plain press passes back only the values // the LINE owned (`tuningOwn`); the character's and the settings' -- takes included -- are resolved now. The character's // takes change -> its read is stale -> a plain press re-reads it -> picked -> fresh. A line that owned its takes keeps // them. No editor (a stub tts); temp dirs; invented people. import "./noLiveEditor.mjs"; 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 { tuningStale } from "umtool-report-to-video/cast"; import { makeTake } from "./postNarrationCli.mjs"; import { resolverFor } from "./castProject.mjs"; import { manifestToken, updatePostNarration } from "./manifest.mjs"; import { lineOwnOf, madeOwnOf, plainPressAskOf } from "./tuneAsk.mjs"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-plain-press-")); after(() => rm(ROOT, { recursive: true, force: true })); delete process.env.UMTOOL_CAST_FILE; const LIVELY = { temperature: 0.75, top_p: 0.92, repeat_penalty: 1.25 }; const NO_RED = { terms: [], markup: {}, slurs: null }; const wavOf = async (file) => { await mkdir(path.dirname(file), { recursive: true }); await writeFile(file, encodeWav16(withSilence(syntheticVoice({ ...VOICE_B, seconds: 0.6 }), { before: 0.1, after: 0.1 }))); }; /** The editor's answer: what was asked over the defaults, the setting's 3 takes when none was asked. */ const stubAsk = (asked) => async (body) => { asked.push(body); await wavOf(body.out); return { jobId: `job-${asked.length}`, result: { wav: body.out, engine: body.engine, voice: body.voice, tuning: { temperature: 0.55, top_p: 0.85, top_k: 50, min_p: 0, repeat_penalty: 1.2, takes: 3, ...(body.tuning ?? {}) }, reading: body.reading ?? 0 } }; }; const castDoc = (tuning) => ({ format: "umtool-cast", version: 2, characters: [{ id: "ferry", name: "Ferry Teller", handles: ["invented_ferry"], synthetic: { voice: "orpheus:tara", tuning } }] }); const ITEM = { type: "post", id: "p-7", post: "ferry-x/7", platform: "x", author: "Ferry Teller", handle: "invented_ferry", date: "2026-10-02T10:00:00.000Z", text: "Here is what they sent.", narration: { script: "Here is what they sent." } }; async function project(name, cast) { const root = path.join(ROOT, name); const dir = path.join(root, "p"); await mkdir(path.join(dir, "narration"), { recursive: true }); await mkdir(path.join(root, ".umtool"), { recursive: true }); await writeFile(path.join(root, ".umtool", "cast.json"), JSON.stringify(cast, null, 1)); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: name, title: name, render: { width: 1920, height: 1080, fps: 30, postNarration: { engine: "orpheus", voice: "zac" } }, timeline: [ITEM] }, null, 2)); return { id: name, dir, root }; } const read = async (p) => JSON.parse(await readFile(path.join(p.dir, "video.manifest.json"), "utf8")); const setCast = (p, cast) => writeFile(path.join(p.root, ".umtool", "cast.json"), JSON.stringify(cast, null, 1)); /** The bench's untouched Read again, as its ask is built (ReadAgain: the Tune's start, the line's own values). */ const plainPress = (take, characterTuning) => plainPressAskOf({ madeWith: take.tuning ?? null, baseline: { character: { name: "Ferry Teller", tuning: characterTuning }, madeOwn: madeOwnOf(take.tuning, take.tuningAsked) }, own: lineOwnOf(take) }); const pick = async (p, n) => updatePostNarration(p.dir, "p-7", { pick: n }, { token: await manifestToken(p.dir) }); test("the character's takes change: its read is stale; a plain press re-reads it with the takes resolved now; picked, it is fresh", async () => { const p = await project("takes-change", castDoc(LIVELY)); const asked = []; const first = (await makeTake(p, (await read(p)).timeline[0], { ask: stubAsk(asked), redaction: NO_RED })).take; assert.equal(first.tuningAsked, "preset"); assert.equal(first.tuning.takes, 3, "made with the editor's setting"); assert.equal("tuningOwn" in first, false, "the line owned nothing"); // The character names 4 takes now: stale. const now = { ...LIVELY, takes: 4 }; await setCast(p, castDoc(now)); assert.deepEqual(tuningStale(await read(p), resolverFor({ cast: castDoc(now) }), { settingTakes: 3 }).map((r) => r.id), ["p-7"]); // A plain press asks nothing of its own: the character's 4 takes are resolved now, not the last read's 3. const ask = plainPress(first, now); assert.equal(ask, null); const again = (await makeTake(p, (await read(p)).timeline[0], { ask: stubAsk(asked), redaction: NO_RED, tuning: ask, keepPick: true })).take; assert.deepEqual(asked.at(-1).tuning, now); assert.equal(again.tuning.takes, 4); // Not picked yet: still stale (what plays is the old read). Picked: fresh. assert.equal(tuningStale(await read(p), resolverFor({ cast: castDoc(now) }), { settingTakes: 3 }).length, 1); await pick(p, 1); assert.deepEqual(tuningStale(await read(p), resolverFor({ cast: castDoc(now) }), { settingTakes: 3 }), []); }); test("a line that owned its takes keeps them through a plain press (tuningOwn), the sampler still the character's", async () => { const p = await project("takes-own", castDoc(LIVELY)); const asked = []; const own = (await makeTake(p, (await read(p)).timeline[0], { ask: stubAsk(asked), redaction: NO_RED, tuning: { takes: 5 } })).take; assert.deepEqual(own.tuningOwn, { takes: 5 }); assert.equal(own.tuningAsked, "preset"); const now = { ...LIVELY, takes: 4 }; await setCast(p, castDoc(now)); const ask = plainPress(own, now); assert.deepEqual(ask, { takes: 5 }); const again = (await makeTake(p, (await read(p)).timeline[0], { ask: stubAsk(asked), redaction: NO_RED, tuning: ask, keepPick: true })).take; assert.equal(again.tuning.takes, 5); assert.deepEqual(again.tuningOwn, { takes: 5 }); assert.equal(again.tuning.temperature, 0.75, "the character's sampler"); // REVIEW 1, SHOULD 3: a value the line owns is never judged against the character's -- picked, it is fresh (it was // stale for ever before: 5 takes against the character's 4, passed back on every press). await pick(p, 1); assert.deepEqual(tuningStale(await read(p), resolverFor({ cast: castDoc(now) }), { settingTakes: 3 }), []); assert.deepEqual(tuningStale(await read(p), resolverFor({ cast: castDoc({ ...LIVELY, temperature: 0.4, takes: 4 }) }), { settingTakes: 3 }).map((r) => r.id), ["p-7"], "the sampler it does not own is still judged"); }); test("an older take (no tuningOwn) asked true owns every recorded value; one recorded \"preset\" owns none (the known limit)", () => { const made = { temperature: 0.9, top_p: 0.85, top_k: 50, min_p: 0, repeat_penalty: 1.2, takes: 5 }; assert.deepEqual(plainPress({ tuning: made, tuningAsked: true }, LIVELY), { temperature: 0.9, top_p: 0.85, top_k: 50, min_p: 0, repeat_penalty: 1.2, takes: 5 }); assert.equal(plainPress({ tuning: { ...LIVELY, top_k: 50, min_p: 0, takes: 5 }, tuningAsked: "preset" }, LIVELY), null); });