// A character is a preset, on disk (castStore.mjs, castCli.mjs, characterLayers.mjs, postThreadRead.mjs, // postNarrationCli.mjs): a version 1 cast read as version 2 and written as it (undo puts the old file back), the // preset flags of `umtool cast set`, a project's OWN characters (one writer, an undo point a write, anonymous ones // "Anon 1", "Anon 2"), "Add to the pool" / "Keep in this video only", a post item's own character, the chain's answer // and its source in `umtool cast project`, a preset's SPEED on a single take and on a thread read's parts (and the // flag over it), and a recording made a read with its character's recorded preset. No editor: the tts and voice ops // are stubs; every project is a temp dir. import assert from "node:assert/strict"; import { existsSync } from "node:fs"; 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 { CastError, castFile, castWrite, projectCharactersFile, projectCharactersWrite, readCast, readCastFile, readProjectCharacters, undoCast, undoProjectCharacters } from "./castStore.mjs"; import { castCommand } from "./castCli.mjs"; import { readLayers, recordedTrimFor } from "./characterLayers.mjs"; import { importRecording, makeTake } from "./postNarrationCli.mjs"; import { postsCommand } from "./postsCli.mjs"; import { partKey, readParts, threadReadPlan, threadTake, upToDate } from "./postThreadRead.mjs"; import { itemReadVoice } from "./readVoice.mjs"; import { threadArgvOf } from "./castProject.mjs"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-characters-")); after(() => rm(ROOT, { recursive: true, force: true })); const wavOf = async (file, voice, seconds = 1.5, silence = 0.3) => { await mkdir(path.dirname(file), { recursive: true }); await writeFile(file, encodeWav16(withSilence(syntheticVoice({ ...voice, seconds }), { before: silence, after: silence }))); }; const run = (command, key) => async (args, extra = {}) => { const lines = []; const code = await command(args, { [key]: ROOT, log: (s) => lines.push(s), out: (s) => lines.push(s), err: (s) => lines.push(s), ...extra }); return { code, lines, text: () => lines.join("\n"), json: () => JSON.parse(lines.join("\n")) }; }; const cast = run(castCommand, "reportsRoot"); const posts = run(postsCommand, "root"); const stubAsk = (asked) => async (body) => { asked.push(body); await wavOf(body.out, body.voice === "am_michael" ? VOICE_A : VOICE_B, 1.2); return { jobId: `job-${asked.length}`, result: { wav: body.out, engine: body.engine, voice: body.voice } }; }; // THE PROJECT: @hostman's post (p-1), his answer to @guest (p-2: a thread), @guest's own post (p-3), a stranger's (p-4). const DIR = path.join(ROOT, "chars"); { const post = (id, handle, author, o = {}) => ({ type: "post", id: `p-${id}`, post: `hostx/${id}`, platform: "x", author, handle, date: `2026-10-30T1${id}:00:00.000Z`, text: `words of ${id} from ${handle}.`, ...o }); await mkdir(DIR, { recursive: true }); await writeFile(path.join(DIR, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: "chars", title: "Characters", provenance: { channelsDir: "../channels", channelSlug: "chan" }, render: { width: 1920, height: 1080, fps: 30, palette: { bg: "#000000", fg: "#ffffff", muted: "#888888", accent: "#c8752a" }, postNarration: { engine: "kokoro", voice: "af_heart" } }, timeline: [ post("1", "hostman", "Host Man", { narration: { script: "Host's first line, read aloud." } }), post("2", "hostman", "Host Man", { parents: [{ id: "20", handle: "guest", author: "The Guest", date: "2026-10-30T09:00:00.000Z", text: "the guest asks a question" }], narration: { script: "Host answers the guest." } }), post("3", "guest", "The Guest", { narration: { script: "The guest's own line." } }), post("4", "stranger", "A Stranger", { narration: { script: "A stranger says a thing." } }), ], }, null, 2)); } const project = { id: "chars", dir: DIR }; const manifest = async () => JSON.parse(await readFile(path.join(DIR, "video.manifest.json"), "utf8")); const item = async (id) => (await manifest()).timeline.find((e) => e.id === id); test("the store: a version 1 cast reads as version 2, the first write makes it version 2, undo puts the old file back", async () => { const file = castFile(ROOT); const v1 = `${JSON.stringify({ format: "umtool-cast", version: 1, characters: [ { id: "hostman", name: "Host Man", handles: ["hostman"], voice: "kokoro:am_michael", speakers: ["Host"] }, { id: "me", name: "Me", handles: ["operator"], voice: "kokoro:bm_george", convert: { engine: "seed-vc", pitch: "contour", semitones: 2 }, narrator: true }, ] }, null, 1)}\n`; await mkdir(path.dirname(file), { recursive: true }); await writeFile(file, v1); const r = await readCastFile(file); assert.equal(r.error, undefined); assert.equal(r.cast.version, 2, "in memory: version 2"); assert.deepEqual(r.cast.characters[1], { id: "me", name: "Me", handles: ["operator"], synthetic: { voice: "kokoro:bm_george" }, recorded: { convert: { engine: "seed-vc", pitch: "contour", semitones: 2 } }, narrator: true, voice: "kokoro:bm_george", convert: { engine: "seed-vc", pitch: "contour", semitones: 2 }, }, "its presets, and the voice / convert they answer to"); assert.equal(await readFile(file, "utf8"), v1, "reading writes nothing"); // A write: version 2 on disk, the presets alone (no voice / convert beside them). const w = await castWrite(file, (chars) => { assert.equal(chars[0].voice, undefined, "the mutation is handed version 2's characters"); chars[0].synthetic.speed = 1.1; }, { token: r.token, what: "hostman at 1.1" }); const disk = JSON.parse(await readFile(file, "utf8")); assert.equal(disk.version, 2); assert.deepEqual(disk.characters[0], { id: "hostman", name: "Host Man", handles: ["hostman"], synthetic: { voice: "kokoro:am_michael", speed: 1.1 }, speakers: ["Host"] }); assert.deepEqual(disk.characters[1], { id: "me", name: "Me", handles: ["operator"], synthetic: { voice: "kokoro:bm_george" }, recorded: { convert: { engine: "seed-vc", pitch: "contour", semitones: 2 } }, narrator: true }); assert.equal(w.undo, 1); // A view written back with its PRESET changed: the preset wins over the view's own alias (which still says the old voice). const view = (await readCastFile(file)).cast.characters[0]; await castWrite(file, (chars) => { chars[0] = { ...view, synthetic: { ...view.synthetic, voice: "kokoro:am_onyx" } }; }, { what: "a view, its preset changed" }); assert.deepEqual(JSON.parse(await readFile(file, "utf8")).characters[0].synthetic, { voice: "kokoro:am_onyx", speed: 1.1 }); // …and the old way of setting one (`c.voice = …`) is still the newer word; neither leaves an alias on disk. await castWrite(file, (chars) => { chars[0].voice = "kokoro:am_michael"; }, { what: "the old way" }); assert.deepEqual(JSON.parse(await readFile(file, "utf8")), disk); await undoCast(file); await undoCast(file); // A refusal writes nothing: a convert target that is a file. await assert.rejects(castWrite(file, (chars) => { chars[0].recorded = { convert: { voice: "narration/me.wav" } }; }), /never a file or a recording/); assert.deepEqual(JSON.parse(await readFile(file, "utf8")), disk); // Undo: the version 1 text, byte for byte -- and it reads the same. const u = await undoCast(file); assert.equal(u.undone, "hostman at 1.1"); assert.equal(await readFile(file, "utf8"), v1); assert.equal((await readCast(ROOT)).cast.characters[0].voice, "kokoro:am_michael"); }); test("umtool cast set: the preset fields -- speed, clean, trim and its pad, the direction; default takes one off", async () => { let r = await cast(["set", "hostman", "--speed", "1.15", "--enhance", "off", "--trim", "on", "--trim-pad", "120", "--direction", "higher, brisk, amused", "--convert-engine", "seed-vc", "--convert-pitch", "source", "--json"]); assert.equal(r.code, 0, r.text()); assert.deepEqual(r.json().character.synthetic, { voice: "kokoro:am_michael", speed: 1.15 }); assert.deepEqual(r.json().character.recorded, { convert: { engine: "seed-vc", pitch: "source" }, enhance: false, trim: { on: true, padMs: 120 }, direction: "higher, brisk, amused" }); assert.equal(r.json().character.voice, "kokoro:am_michael", "the view still says its voice"); assert.equal(JSON.parse(await readFile(castFile(ROOT), "utf8")).version, 2, "the write made the file version 2"); r = await cast(["ls"]); assert.match(r.text(), /hostman\s+kokoro:am_michael ×1\.15\s+Host Man @hostman convert: seed-vc -> kokoro:am_michael, pitch source; not cleaned; trim pad 120 ms; direction: "higher, brisk, amused"/); // `default` takes a key off; --no-speed the speed; "" the direction. r = await cast(["set", "hostman", "--enhance", "default", "--trim", "default", "--convert-pitch", "default", "--no-speed", "--direction", "", "--json"]); assert.equal(r.code, 0, r.text()); assert.deepEqual(r.json().character.synthetic, { voice: "kokoro:am_michael" }); assert.deepEqual(r.json().character.recorded, { convert: { engine: "seed-vc" } }); for (const [args, re] of [ [["--speed", "3"], /--speed is a number from 0\.5 to 2/], [["--enhance", "maybe"], /--enhance is on, off or default/], [["--trim-pad", "9000"], /--trim-pad is a number of milliseconds from 0 to 2000/], [["--direction", "x".repeat(201)], /--direction is one line for the person reading \(at most 200/], [["--convert-voice", "me.wav"], /--convert-voice is a STOCK voice with its engine.*never a file or a recording/], [["--anonymous"], /an anonymous character is one video's, never the pool's/], [["--no-voice"], /a cast character has a stock voice/], ]) { r = await cast(["set", "hostman", ...args]); assert.equal(r.code, 1, args.join(" ")); assert.match(r.text(), re); } assert.deepEqual((await readCast(ROOT)).cast.characters[0].recorded, { convert: { engine: "seed-vc" } }, "a refusal writes nothing"); r = await cast(["set", "hostman", "--speed", "1.15"]); assert.equal(r.code, 0, r.text()); }); test("a project's own characters: one writer, an undo point a write, anonymous ones Anon 1, Anon 2; voices.json left alone", async () => { await mkdir(path.join(DIR, "narration"), { recursive: true }); const voices = path.join(DIR, "narration", "voices.json"); const mapText = `${JSON.stringify({ stranger: "kokoro:am_onyx" }, null, 1)}\n`; await writeFile(voices, mapText); assert.equal((await readProjectCharacters(DIR)).exists, false); let r = await cast(["project", "chars", "add", "anon-a", "--anonymous", "--handles", "@Guest", "--voice", "kokoro:am_puck", "--speed", "0.9", "--direction", "flat, tired", "--enhance", "off", "--json"]); assert.equal(r.code, 0, r.text()); assert.deepEqual(r.json().character, { id: "anon-a", name: "Anon 1", anonymous: true, shown: "Anon 1", handles: ["guest"], synthetic: { voice: "kokoro:am_puck", speed: 0.9 }, recorded: { enhance: false, direction: "flat, tired" }, voice: "kokoro:am_puck", }); r = await cast(["project", "chars", "add", "anon-b", "--anonymous", "--name", "the second one", "--json"]); assert.deepEqual([r.json().character.shown, r.json().character.name, r.json().character.synthetic], ["Anon 2", "the second one", undefined], "the next shown name; no voice of its own"); const file = projectCharactersFile(DIR); assert.equal(file, path.join(DIR, "narration", "characters.json")); const disk = JSON.parse(await readFile(file, "utf8")); assert.deepEqual([disk.format, disk.version, disk.characters.length], ["umtool-project-characters", 1, 2]); assert.equal(disk.characters[0].voice, undefined, "the presets alone on disk"); assert.ok(existsSync(file.replace(/\.json$/, ".undo.json"))); // Refusals, in the layer's words -- nothing written. for (const [args, re] of [ [["add", "anon-a", "--name", "Again"], /is one of chars's characters already/], [["add", "plain"], /takes --name "…" \(or --anonymous/], [["add", "plain", "--name", "Plain", "--shown", "X"], /--shown is an anonymous character's shown name/], [["add", "plain", "--name", "Plain", "--narrator"], /the narrator is a cast character/], [["add", "plain", "--name", "Plain", "--voice", "/tmp/me.wav"], /--voice is a STOCK voice with its engine/], [["add", "plain", "--name", "Plain", "--convert-voice", "seedvc:/home/me.wav"], /never a file or a recording/], [["add", "plain", "--name", "Plain", "--handles", "guest"], /@guest is anon-a's already/], [["set", "nobody", "--speed", "1"], /no nobody among chars's characters/], [["pool", "anon-a"], /anon-a is anonymous: it stays in this video/], [["pool", "anon-b"], /anonymous/], [["frobnicate"], /nothing \(the view\), add, set, rm, undo, pool or keep/], ]) { r = await cast(["project", "chars", ...args]); assert.equal(r.code, 1, args.join(" ")); assert.match(r.text(), re); } assert.deepEqual(JSON.parse(await readFile(file, "utf8")), disk); // The store itself: a stale token refused, a broken file never overwritten. const read = await readProjectCharacters(DIR); await assert.rejects(projectCharactersWrite(DIR, () => {}, { token: "stale" }), (e) => e instanceof CastError && e.status === 409); await projectCharactersWrite(DIR, (chars) => { chars[1].shown = "The Second"; }, { token: read.token, what: "rename" }); assert.equal((await readProjectCharacters(DIR)).doc.characters[1].shown, "The Second"); const u = await undoProjectCharacters(DIR); assert.equal(u.undone, "rename"); assert.equal((await readProjectCharacters(DIR)).doc.characters[1].shown, "Anon 2"); // set, rm, undo through the CLI. r = await cast(["project", "chars", "set", "anon-b", "--shown", "Witness", "--voice", "kokoro:af_bella", "--json"]); assert.deepEqual([r.json().character.shown, r.json().character.voice], ["Witness", "kokoro:af_bella"]); r = await cast(["project", "chars", "set", "anon-b", "--no-voice", "--json"]); assert.equal(r.json().character.synthetic, undefined); r = await cast(["project", "chars", "rm", "anon-b"]); assert.equal(r.code, 0, r.text()); assert.equal((await readProjectCharacters(DIR)).doc.characters.length, 1); r = await cast(["project", "chars", "undo"]); assert.match(r.text(), /undone: rm anon-b \(chars\)/); assert.equal((await readProjectCharacters(DIR)).doc.characters.length, 2); await cast(["project", "chars", "rm", "anon-b"]); assert.equal(await readFile(voices, "utf8"), mapText, "voices.json is not rewritten"); // A broken file: read as none, said, never overwritten. const good = await readFile(file, "utf8"); await writeFile(file, "{ nope"); assert.match((await readProjectCharacters(DIR)).error, /not JSON/); r = await cast(["project", "chars", "add", "x", "--name", "X"]); assert.equal(r.code, 1); assert.match(r.text(), /fix the file first/); assert.equal(await readFile(file, "utf8"), "{ nope"); r = await cast(["project", "chars"]); assert.match(r.text(), /this video's characters are NOT read/); await writeFile(file, good); }); test("cast project: the chain's answer and its source for each author; a post item's own character", async () => { let r = await cast(["project", "chars", "--json"]); assert.equal(r.code, 0, r.text()); let v = r.json(); const row = (key) => v.characters.find((c) => c.key === key); // hostman: the cast's character, its speed with it. guest: this video's anonymous character, over nothing. assert.deepEqual([row("hostman").voice.spec, row("hostman").voice.from, row("hostman").voice.speed, row("hostman").character.layer], ["kokoro:am_michael", "cast", 1.15, "cast"]); assert.deepEqual([row("anon-a").voice.spec, row("anon-a").voice.from, row("anon-a").voice.speed, row("anon-a").voice.character], ["kokoro:am_puck", "project", 0.9, "anon-a"]); assert.deepEqual([row("anon-a").name, row("anon-a").character.anonymous, row("anon-a").character.shown, row("anon-a").character.layer], ["Anon 1", true, "Anon 1", "project"], "shown under its shown name"); assert.deepEqual(row("anon-a").recorded, { convert: { engine: null, voice: "kokoro:am_puck" }, enhance: false, trim: { on: true, padMs: null }, direction: "flat, tired" }); // stranger: the voice map -- a project character with only a synthetic voice. assert.deepEqual([row("@stranger").voice.spec, row("@stranger").voice.from, row("@stranger").character], ["kokoro:am_onyx", "project", null]); assert.deepEqual(v.own.map((c) => [c.id, c.handles]), [["anon-a", ["guest"]]]); assert.deepEqual(v.itemCharacters, []); assert.equal(v.charactersFile, projectCharactersFile(DIR)); r = await cast(["project", "chars"]); assert.match(r.text(), /hostman\s+kokoro:am_michael ×1\.15\s+\(cast: hostman\)/); assert.match(r.text(), /anon-a\s+kokoro:am_puck ×0\.9\s+\(project: anon-a\).*shown as "Anon 1"/); assert.match(r.text(), /a recording of them: converted to kokoro:am_puck; not cleaned; edges trimmed; direction: "flat, tired"/); assert.match(r.text(), /@stranger\s+kokoro:am_onyx\s+\(project\)/); // `posts voices ls` answers through the same layers: this video's own character, the map, the cast. r = await posts(["voices", "chars", "ls", "--json"]); assert.equal(r.code, 0, r.text()); const author = (h) => r.json().authors.find((a) => a.handle === h); assert.deepEqual([author("guest").voice, author("guest").from, author("guest").own, author("guest").speed], ["kokoro:am_puck", "project", "anon-a", 0.9]); assert.deepEqual([author("stranger").voice, author("stranger").from, author("stranger").own, author("stranger").mapped], ["kokoro:am_onyx", "project", undefined, true]); assert.deepEqual([author("hostman").voice, author("hostman").from, author("hostman").character], ["kokoro:am_michael", "cast", "hostman"]); r = await posts(["voices", "chars", "ls"]); assert.match(r.text(), /@guest kokoro:am_puck ×0\.9 \(this video's character: anon-a\)/); // A POST ITEM's OWN CHARACTER: p-4 (the stranger's) read as the cast's `me`. r = await posts(["character", "chars", "p-4", "ghost"]); assert.equal(r.code, 2); assert.match(r.text(), /no character ghost among chars's own or in the cast/); r = await posts(["character", "chars", "p-4", "me", "--json"]); assert.equal(r.code, 0, r.text()); assert.deepEqual([r.json().character, r.json().layer, r.json().changed, r.json().voice.spec, r.json().voice.from], ["me", "cast", "set", "kokoro:bm_george", "item"]); assert.equal((await item("p-4")).narration.character, "me"); r = await posts(["character", "chars", "p-4"]); assert.match(r.text(), /p-4: read as me \(Me; the cast's\): kokoro:bm_george \(item: me\)/); r = await cast(["project", "chars", "--json"]); v = r.json(); assert.deepEqual(v.itemCharacters.map((x) => [x.id, x.character, x.found, x.layer, x.voice.spec, x.voice.from]), [["p-4", "me", true, "cast", "kokoro:bm_george", "item"]]); assert.deepEqual([row("@stranger").voice.spec, row("@stranger").voice.from], ["kokoro:am_onyx", "project"], "the handle's own answer is still the map's"); // The single take asks the same chain (readVoice.mjs), and a thread read's plan. assert.deepEqual(await itemReadVoice(project, await item("p-4"), { render: (await manifest()).render }), { engine: "kokoro", voice: "bm_george", from: "item", character: "me" }); // The project's own first: an id both layers have. await projectCharactersWrite(DIR, (chars) => { chars.push({ id: "me", name: "Me, here", handles: [], synthetic: { voice: "kokoro:am_echo", speed: 1.3 } }); }); assert.deepEqual(await itemReadVoice(project, await item("p-4"), { render: (await manifest()).render }), { engine: "kokoro", voice: "am_echo", from: "item", speed: 1.3, character: "me" }); await undoProjectCharacters(DIR); // --clear: by its author's handle again. r = await posts(["character", "chars", "p-4", "--clear", "--json"]); assert.deepEqual([r.json().character, r.json().changed, r.json().voice.from], [null, "cleared", "project"]); assert.equal((await item("p-4")).narration.character, undefined); r = await posts(["character", "chars", "p-4", "me", "--clear"]); assert.equal(r.code, 2); }); test("a preset's speed on a single take: the flag, then the character's, then the item's own", async () => { const m = await manifest(); const asked = []; // hostman (the cast: kokoro am_michael ×1.15): his take is asked at 1.15, and says so. let t = await makeTake(project, await item("p-1"), { render: m.render, ask: stubAsk(asked), redaction: null }); assert.deepEqual([asked.at(-1).engine, asked.at(-1).voice, asked.at(-1).speed], ["kokoro", "am_michael", 1.15]); assert.equal(t.take.speed, 1.15); // The flag over it. t = await makeTake(project, await item("p-1"), { render: m.render, speed: 0.8, ask: stubAsk(asked), redaction: null }); assert.equal(asked.at(-1).speed, 0.8); // The item's own speed (narration.speed: set on this one item) beats its character's; the flag beats both. const own = { ...(await item("p-1")), narration: { ...(await item("p-1")).narration, speed: 0.7 } }; t = await makeTake(project, own, { render: m.render, ask: stubAsk(asked), redaction: null }); assert.equal(asked.at(-1).speed, 0.7); t = await makeTake(project, own, { render: m.render, speed: 1.4, ask: stubAsk(asked), redaction: null }); assert.equal(asked.at(-1).speed, 1.4); // The stranger (the voice map: no preset): no speed asked. t = await makeTake(project, await item("p-4"), { render: m.render, ask: stubAsk(asked), redaction: null }); assert.deepEqual([asked.at(-1).voice, "speed" in asked.at(-1)], ["am_onyx", false]); assert.equal(t.take.speed, undefined); }); test("a thread read: each part at its character's speed, the flag over them all, Orpheus none -- and another speed is another part", async () => { const m = await manifest(); const e2 = await item("p-2"); const L = await readLayers(DIR); const plan = threadReadPlan(m, e2, { map: L.map, cast: L.castMap, item: L.itemOf(e2) }); assert.deepEqual(plan.pieces.map((p) => [p.role, p.voice, p.from, p.speed ?? null, p.character ?? null]), [["parent:0", "am_puck", "project", 0.9, "anon-a"], ["post", "am_michael", "cast", 1.15, "hostman"]]); const parts = readParts(plan); assert.deepEqual(parts.map((p) => p.speed), [0.9, 1.15]); assert.equal(parts[1].key, partKey("kokoro", "am_michael", parts[1].text, 1.15)); assert.notEqual(parts[1].key, partKey("kokoro", "am_michael", parts[1].text), "the same words at another speed are another part"); assert.equal(partKey("kokoro", "am_michael", "hi", 1), partKey("kokoro", "am_michael", "hi"), "at 1: the key as before"); // --speed: every part at that one. assert.deepEqual(threadReadPlan(m, e2, { map: L.map, cast: L.castMap, speed: 1.4 }).pieces.map((p) => p.speed), [1.4, 1.4]); // Orpheus has no speed: a piece in it is asked none, whatever the flag says. assert.deepEqual(threadReadPlan(m, e2, { map: { guest: "orpheus:tara" }, cast: L.castMap, speed: 1.4 }).pieces.map((p) => [p.engine, p.speed ?? null]), [["orpheus", null], ["kokoro", 1.4]]); // His own words at the item's own speed (narration.speed), as a single take -- over his character's; the others' untouched. assert.deepEqual(threadReadPlan(m, { ...e2, narration: { ...e2.narration, speed: 0.75 } }, { map: {}, cast: {} }).pieces.map((p) => [p.role, p.speed ?? null]), [["parent:0", null], ["post", 0.75]]); assert.deepEqual(threadReadPlan(m, { ...e2, narration: { ...e2.narration, speed: 0.75 } }, { map: L.map, cast: L.castMap }).pieces.map((p) => [p.role, p.speed ?? null]), [["parent:0", 0.9], ["post", 0.75]]); // Made: the editor is asked each part at its speed, the take's parts keep it, and the same read again is up to date. const asked = []; const r = await threadTake(project, e2, { manifest: m, red: null, ask: stubAsk(asked), lanes: null }); assert.deepEqual(asked.map((b) => [b.voice, b.speed]), [["am_puck", 0.9], ["am_michael", 1.15]]); assert.deepEqual(r.take.parts.map((p) => [p.role, p.voice, p.speed]), [["parent:0", "am_puck", 0.9], ["post", "am_michael", 1.15]]); assert.equal(r.take.read.speed, undefined, "a preset's speed is the character's, not the read's"); const after = await item("p-2"); const again = await threadTake(project, after, { red: null, ask: stubAsk(asked), lanes: null }); assert.equal(again.upToDate, true); assert.equal(asked.length, 2); // The flag: not up to date; the parts asked again at it (new cache keys), the read records it, and a re-read keeps it. assert.equal(upToDate(after, threadReadPlan(await manifest(), after, { map: L.map, cast: L.castMap, speed: 1.4 })), false); const fast = await threadTake(project, after, { red: null, speed: 1.4, ask: stubAsk(asked), lanes: null }); assert.deepEqual(asked.slice(2).map((b) => b.speed), [1.4, 1.4]); assert.equal(fast.take.read.speed, 1.4); assert.deepEqual(threadArgvOf(fast.take.read), ["--speed", "1.4"]); // Through the CLI: --speed reaches a thread read (it was ignored), and a bad one is refused in the flag's name. let c = await posts(["narrate", "chars", "p-2", "--take", "--thread", "--speed", "1.4", "--dry-run", "--json"]); assert.equal(c.code, 0, c.text()); assert.deepEqual(c.json().pieces.map((p) => p.speed), [1.4, 1.4]); assert.equal(c.json().upToDate, true); c = await posts(["narrate", "chars", "p-2", "--take", "--thread", "--speed", "9", "--dry-run"]); assert.equal(c.code, 2); assert.match(c.text(), /--speed is a number from 0\.5 to 2/); }); test("\"Add to the pool\" and \"Keep in this video only\"", async () => { // Keep an author with no character here: a character of this video, in the voice they read in now. let r = await cast(["project", "chars", "keep", "@stranger", "--json"]); assert.equal(r.code, 0, r.text()); assert.deepEqual(r.json().character, { id: "stranger", name: "A Stranger", handles: ["stranger"], synthetic: { voice: "kokoro:am_onyx" }, voice: "kokoro:am_onyx" }); // Keep a cast character here: a copy, without its narrator mark; changing it leaves the pool's as it is. r = await cast(["project", "chars", "keep", "me", "--json"]); assert.equal(r.code, 0, r.text()); assert.equal(r.json().character.narrator, undefined); assert.deepEqual(r.json().character.recorded, { convert: { engine: "seed-vc", pitch: "contour", semitones: 2 } }); r = await cast(["project", "chars", "set", "me", "--convert-semitones", "-3", "--json"]); assert.equal(r.json().character.recorded.convert.semitones, -3); assert.equal((await readCast(ROOT)).cast.characters.find((c) => c.id === "me").recorded.convert.semitones, 2, "the pool's is untouched"); r = await cast(["project", "chars", "keep", "me"]); assert.equal(r.code, 1); assert.match(r.text(), /me is one of chars's characters already/); // `use` for this project: this video's own character takes it (its preset answers before the voice map). r = await cast(["use", "chars", "@stranger", "kokoro:am_fenrir", "--json"]); assert.equal(r.code, 0, r.text()); assert.equal((await readProjectCharacters(DIR)).doc.characters.find((c) => c.id === "stranger").voice, "kokoro:am_fenrir"); assert.deepEqual(JSON.parse(await readFile(path.join(DIR, "narration", "voices.json"), "utf8")), { stranger: "kokoro:am_onyx" }, "the map is left as it was"); // An anonymous character never reaches the pool through `use --everywhere` either. r = await cast(["use", "chars", "anon-a", "kokoro:am_onyx", "--everywhere"]); assert.equal(r.code, 1); assert.match(r.text(), /anon-a is anonymous: it stays in this video, never the pool/); r = await cast(["use", "chars", "@guest", "kokoro:am_onyx", "--everywhere"]); assert.equal(r.code, 1, "by its handle too"); assert.equal((await readCast(ROOT)).cast.characters.some((c) => (c.handles ?? []).includes("guest")), false); // Add to the pool: into the cast, out of the project; one the cast has already is refused. r = await cast(["project", "chars", "pool", "me"]); assert.equal(r.code, 1); assert.match(r.text(), /me is in the cast already/); r = await cast(["project", "chars", "pool", "stranger", "--json"]); assert.equal(r.code, 0, r.text()); assert.deepEqual([r.json().scope, r.json().changed], ["cast", "pooled"]); assert.deepEqual((await readCast(ROOT)).cast.characters.find((c) => c.id === "stranger").synthetic, { voice: "kokoro:am_fenrir" }); assert.equal((await readProjectCharacters(DIR)).doc.characters.some((c) => c.id === "stranger"), false); r = await cast(["undo"]); assert.match(r.text(), /undone: stranger into the pool \(from chars\)/); await cast(["project", "chars", "rm", "me"]); }); test("a recording made a read with its character's recorded preset: clean off, trim off, the direction; the flags over them", async () => { const rec = path.join(ROOT, "my-read.wav"); await wavOf(rec, VOICE_B, 2, 0.6); const ops = []; const askOp = async (route, body) => { ops.push(route.split("/").at(-1)); await wavOf(body.out, VOICE_A, 2); return { jobId: `op-${ops.length}`, result: { engine: body.engine ?? "openvoice", voice: body.voice } }; }; // p-3 is @guest's: this video's anonymous character (enhance off, direction) -- not cleaned, converted to its voice. let r = await importRecording(project, await item("p-3"), rec, { ask: askOp }); assert.deepEqual(r.character, { id: "anon-a", name: "Anon 1", voice: "kokoro:am_puck", layer: "project" }, "named as it is shown"); assert.equal(r.direction, "flat, tired"); assert.deepEqual(ops, ["voice-convert"]); assert.deepEqual(r.takes.map((t) => t.step), ["recorded", "converted to kokoro:am_puck"]); assert.match(r.notes[0], /not cleaned: anon-a's recorded preset says so/); assert.equal(r.disclosure, "Narration: voice converted to a synthetic voice (openvoice · kokoro:am_puck)", "the disclosure line is what it was"); const kept = (await item("p-3")).narration.takes[0]; assert.match(kept.note, /recorded for Anon 1/); const trimmed = kept.seconds; assert.ok(trimmed < 2.6, `its edges trimmed (${trimmed} s of 3.2)`); // The command over the preset: clean it this once. ops.length = 0; r = await importRecording(project, await item("p-3"), rec, { clean: true, ask: askOp }); assert.deepEqual(ops, ["voice-enhance", "voice-convert"]); // The preset says trim off: kept as recorded, edges and all; a pad is the room kept. await cast(["project", "chars", "set", "anon-a", "--trim", "off"]); r = await importRecording(project, await item("p-3"), rec, { convert: false, ask: askOp }); const whole = (await item("p-3")).narration.takes.find((t) => t.file === r.takes[0].file).seconds; assert.ok(whole > 3, `not trimmed (${whole} s)`); await cast(["project", "chars", "set", "anon-a", "--trim", "on", "--trim-pad", "400"]); r = await importRecording(project, await item("p-3"), rec, { convert: false, ask: askOp }); const padded = (await item("p-3")).narration.takes.find((t) => t.file === r.takes[0].file).seconds; assert.ok(padded > trimmed + 0.3 && padded < whole, `the pad kept (${padded} s between ${trimmed} and ${whole})`); // The record page's two paths ask the same preset (characterLayers.mjs recordedTrimFor): the room kept; none with no character. assert.deepEqual(await recordedTrimFor(DIR, await item("p-3")), { on: true, padMs: 400 }); assert.deepEqual(await recordedTrimFor(DIR, await item("p-1")), { on: true, padMs: null }, "the cast's character: trimmed, the recorder's own room"); assert.deepEqual(await recordedTrimFor(DIR, { id: "x", handle: "nobody-at-all" }), { on: true, padMs: null }, "nobody's: the narrator's"); // --as: a character of this video's by id, the cast's by id; the item's own character before its author's. r = await importRecording(project, await item("p-1"), rec, { as: "anon-a", convert: false, ask: askOp }); assert.equal(r.character.layer, "flag"); await assert.rejects(importRecording(project, await item("p-1"), rec, { as: "ghost", ask: askOp }), /--as ghost: no such character in this video's characters or the cast/); await posts(["character", "chars", "p-3", "me"]); ops.length = 0; r = await importRecording(project, await item("p-3"), rec, { ask: askOp }); assert.deepEqual([r.character.id, r.character.layer], ["me", "item"]); assert.deepEqual(r.takes.map((t) => t.step), ["recorded", "cleaned", "converted to kokoro:bm_george (pitch: contour +2 st)"]); await posts(["character", "chars", "p-3", "--clear"]); }); test("posts narrate --derive --op convert: the character's recorded preset unless a flag says otherwise; no character and no voice is refused", async () => { // p-3 is @guest's (anon-a: kokoro:am_puck); the cast's narrator `me` converts by seed-vc, contour +2. const rec = path.join(ROOT, "derive-read.wav"); await wavOf(rec, VOICE_B, 1.5, 0.3); const asked = []; const ask = async (route, body) => { asked.push({ op: route.split("/").at(-1), ...body }); await wavOf(body.out, VOICE_A, 1.5); return { jobId: `d-${asked.length}`, result: { engine: body.engine ?? "openvoice", voice: body.voice ?? "zac" } }; }; const r0 = await importRecording(project, await item("p-3"), rec, { convert: false, clean: false, ask }); const n = (await item("p-3")).narration.takes.findIndex((t) => t.file === r0.takes[0].file) + 1; // No flags: anon-a's stock voice -- never the editor's default (the stub's "zac"). let r = await posts(["narrate", "chars", "p-3", "--derive", String(n), "--op", "convert", "--json"], { ask }); assert.equal(r.code, 0, r.text()); assert.deepEqual([asked.at(-1).op, asked.at(-1).voice, asked.at(-1).engine, asked.at(-1).pitch], ["voice-convert", "kokoro:am_puck", undefined, undefined]); assert.equal(r.json().take.steps[0].voice, "kokoro:am_puck"); // --as: another character's whole convert entry; a flag over one key of it. r = await posts(["narrate", "chars", "p-3", "--derive", String(n), "--op", "convert", "--as", "me"], { ask }); assert.equal(r.code, 0, r.text()); assert.deepEqual([asked.at(-1).engine, asked.at(-1).voice, asked.at(-1).pitch, asked.at(-1).semitones], ["seed-vc", "kokoro:bm_george", "contour", 2]); assert.match(r.text(), /as me: seed-vc -> kokoro:bm_george \(pitch: contour \+2 st\)/); r = await posts(["narrate", "chars", "p-3", "--derive", String(n), "--op", "convert", "--as", "me", "--semitones", "-1", "--voice", "kokoro:am_onyx"], { ask }); assert.deepEqual([asked.at(-1).engine, asked.at(-1).voice, asked.at(-1).pitch, asked.at(-1).semitones], ["seed-vc", "kokoro:am_onyx", "contour", -1]); r = await posts(["narrate", "chars", "p-3", "--derive", String(n), "--op", "convert", "--as", "ghost"], { ask }); assert.equal(r.code, 2); assert.match(r.text(), /--as ghost: no such character/); // An enhance asks no character. const before = asked.length; r = await posts(["narrate", "chars", "p-3", "--derive", String(n), "--op", "enhance"], { ask }); assert.equal(r.code, 0, r.text()); assert.equal(asked.length, before + 1); assert.equal("voice" in asked.at(-1), false); });