import { test, expect, type Page } from "@playwright/test"; import { copyFileSync, existsSync, mkdirSync, readFileSync, rmSync } from "node:fs"; import { execFileSync } from "node:child_process"; import path from "node:path"; import { fileURLToPath } from "node:url"; import * as urls from "./urls"; // --------------------------------------------------------------------------- // THE VOICES PAGE (operator, 6 Nov 2026: "pick different voices for different characters, and in UI alone be able // to try rendering different ones to compare … and try to maintain consistent voices for different repeating // characters"; components/projects/VoicesPanel.tsx, /api/report/voices, lib/report/castCli.mjs). Fixture: // e2e/fixtures/voices-fixture.mjs -- Host's clips (the real voice), @hostman's two items (one read in orpheus zac), // @guest's. Every step a press: // // the characters, each voice and where it comes from (umtool cast project) // Audition: candidates added, Render -> a job (the stub editor's tts, then the fake embedder's closeness), // played side by side at one loudness beside the real voice // Use everywhere -> the cast; the re-read confirm -> queued; the item re-read in the new voice // Use for this project -> the project's voice map; the lint's override note // a recording dropped on an item -> kept, converted to the character's stock voice // the cast: remove, undo // a character's presets (CharacterPresets.tsx): Keep in this video only -> its voice, speed, clean, direction saved; // an anonymous character of this video ("Anon 1"), never offered -- and refused -- the pool; an item read as a // character of its own, and cleared // proposed characters (ProposedCharacters.tsx, umtool cast assign): Propose by rule -> Change -> Accept -> Reject // --------------------------------------------------------------------------- const HERE = path.dirname(fileURLToPath(import.meta.url)); const FIXTURE = path.join(HERE, "..", ".e2e-song"); const P = "reports/voices-fixture"; const DIR = path.join(FIXTURE, "reports", "voices-fixture"); const MANIFEST = path.join(DIR, "video.manifest.json"); const CAST = path.join(FIXTURE, ".umtool", "cast.json"); // This video's own characters (lib/report/castStore.mjs projectCharactersWrite). const CHARACTERS = path.join(DIR, "narration", "characters.json"); const SHOTS = process.env.E2E_VOICES_SHOTS ?? null; const REC = path.join(FIXTURE, "voices-recording.wav"); type Take = { file: string; engine?: string; voice?: string; steps?: { op: string; voice?: string }[] }; type Entry = { id: string; narration?: { takes?: Take[]; pick?: number } }; const entry = (id: string) => (JSON.parse(readFileSync(MANIFEST, "utf8")) as { timeline: Entry[] }).timeline.find((e) => e.id === id)!; const picked = (id: string) => { const n = entry(id).narration ?? {}; return (n.takes ?? [])[n.pick ?? 0] ?? null; }; const shot = async (page: Page, name: string, el?: ReturnType) => { if (!SHOTS) return; mkdirSync(SHOTS, { recursive: true }); if (el) await el.screenshot({ path: path.join(SHOTS, `${name}.png`) }); else await page.screenshot({ path: path.join(SHOTS, `${name}.png`), fullPage: true }); }; test.beforeAll(() => { if (!existsSync(`${MANIFEST}.pristine`)) copyFileSync(MANIFEST, `${MANIFEST}.pristine`); if (!existsSync(REC)) execFileSync("ffmpeg", ["-v", "error", "-y", "-f", "lavfi", "-i", "sine=frequency=500:sample_rate=48000:duration=2", "-ac", "1", REC]); }); const reset = () => { copyFileSync(`${MANIFEST}.pristine`, MANIFEST); for (const f of [CAST, CAST.replace(/\.json$/, ".undo.json"), path.join(DIR, "narration", "voices.json"), CHARACTERS, CHARACTERS.replace(/\.json$/, ".undo.json")]) rmSync(f, { force: true }); for (const d of ["auditions", "uploads", "parts-cache"]) rmSync(path.join(DIR, "narration", d), { recursive: true, force: true }); for (const id of ["1", "2", "3"]) { const dir = path.join(DIR, "narration", "posts", id); if (!existsSync(dir)) continue; for (const f of execFileSync("ls", ["-A", dir], { encoding: "utf8" }).split("\n").filter(Boolean)) if (f !== "take-1.orpheus-zac.wav") rmSync(path.join(dir, f), { recursive: true, force: true }); } rmSync(path.join(DIR, "revisions"), { recursive: true, force: true }); }; test.beforeEach(() => { test.setTimeout(240_000); reset(); }); test.afterAll(reset); test("each character's voice and where it comes from; an audition side by side with its closeness to the real voice; Use everywhere and the re-reads", async ({ page }) => { await page.goto(urls.voicesHref(P)); const host = page.locator('[data-voices-character="@hostman"]'); const guest = page.locator('[data-voices-character="@guest"]'); // The page's first read on a cold server (the project's characters, the cast, the engines): give it time. await expect(host).toBeVisible({ timeout: 30_000 }); // Nothing in the cast yet: his own items read in the narrator's voice; the guest's too, and as a context post the default. await expect(host).toHaveAttribute("data-voice-from", "narrator"); await expect(host.locator("[data-voice-now]")).toHaveAttribute("data-voice-now", "orpheus:zac"); await expect(host.locator("[data-voice-speaker]")).toHaveAttribute("data-voice-speaker", "Host Man"); await expect(host.locator("[data-voice-drift]")).toHaveAttribute("data-voice-drift", "0"); await expect(guest).toContainText("1 item · 1 as context"); await expect(guest).toContainText("as a context post: orpheus · leo (the default)"); await expect(page.locator("[data-cast-list]")).toHaveAttribute("data-cast-list", "0"); await shot(page, "1-characters"); // Audition him: his current voice is the first candidate; two Kokoro voices added from the engine's list. await host.locator("[data-voices-audition-open]").click(); // The audition box (its key follows the character into the cast: @hostman, then hostman). const box = page.locator("[data-audition-panel]"); await expect(box).toHaveAttribute("data-audition-panel", "@hostman"); await expect(box.locator('[data-audition-candidate="orpheus:zac"]')).toBeVisible(); await expect(box.locator("[data-audition-said]")).toContainText("Host's first line, read aloud."); for (const v of ["am_michael", "bm_george"]) { await box.locator("[data-audition-engine]").selectOption("kokoro"); await box.locator("[data-audition-voice]").selectOption(v); await box.locator("[data-audition-add]").click(); } await expect(box.locator("[data-audition-candidates]")).toHaveAttribute("data-audition-candidates", "3"); await expect(box.locator('[data-audition-result="kokoro:am_michael"]')).toHaveAttribute("data-audition-state", "missing"); await expect(box.locator("[data-audition-render]")).toHaveText("Render 3 candidates"); await box.locator("[data-audition-render]").click(); await expect(box.locator("[data-audition-job]")).toHaveAttribute("data-audition-job", "done", { timeout: 120_000 }); await expect(box.locator("[data-audition-results]")).toHaveAttribute("data-audition-results", "3"); for (const spec of ["orpheus:zac", "kokoro:am_michael", "kokoro:bm_george"]) { const card = box.locator(`[data-audition-result="${spec}"]`); await expect(card).toHaveAttribute("data-audition-state", "ready"); await expect(card.locator("[data-audition-score]")).toHaveAttribute("data-audition-score", /^-?\d\.\d{3}$/); await expect(card).toContainText("closeness to Host Man"); await expect(card).toContainText(/-2\d\.\d LUFS|-19\.\d LUFS|-20\.\d LUFS/); } // The real voice beside them: a reference, never a target. await expect(box.locator('[data-audition-reference="Host Man"]')).toContainText("never a target"); const ref = await box.locator("[data-audition-reference-audio]").getAttribute("src"); expect((await page.request.get(ref!)).status()).toBe(200); const src = await box.locator('[data-audition-audio="kokoro:am_michael"]').getAttribute("src"); expect((await page.request.get(src!)).headers()["content-type"]).toBe("audio/wav"); // The editor was asked once a candidate, each in its own engine. const asked = (JSON.parse(readFileSync(path.join(FIXTURE, "editor-stub.requests.json"), "utf8")) as { route?: string; engine?: string; voice?: string; text?: string }[]) .filter((r) => r.route === "tts" && r.text === "Host's first line, read aloud."); expect(asked.map((r) => `${r.engine}:${r.voice}`).sort()).toEqual(["kokoro:am_michael", "kokoro:bm_george", "orpheus:zac"]); await expect(box.locator("[data-audition-render]")).toHaveText("Score again"); await shot(page, "2-audition", box); // Use everywhere: the cast; then the re-read his picked take (orpheus zac) asks for, confirmed and queued. await box.locator('[data-audition-result="kokoro:am_michael"] [data-audition-use="cast"]').click(); const confirm = box.locator("[data-reread-confirm]"); await expect(confirm).toHaveAttribute("data-reread-confirm", "1"); await expect(confirm.locator('[data-reread-item="p-1"]')).toContainText("post orpheus · zac → kokoro · am_michael"); // The cast on disk is version 2: the voice is the character's synthetic preset. const cast = JSON.parse(readFileSync(CAST, "utf8")) as { version: number; characters: { id: string; synthetic: { voice: string }; handles: string[] }[] }; expect(cast.version).toBe(2); expect(cast.characters).toEqual([{ id: "hostman", name: "Host Man", handles: ["hostman"], synthetic: { voice: "kokoro:am_michael" } }]); await expect(page.locator('[data-voices-character="hostman"]')).toHaveAttribute("data-voice-from", "cast"); await expect(page.locator('[data-voices-character="hostman"] [data-voice-drift]')).toHaveAttribute("data-voice-drift", "1"); await shot(page, "3-reread-confirm", confirm); await confirm.locator("[data-reread-queue]").click(); await expect(box.locator("[data-reread-queued]")).toHaveAttribute("data-reread-queued", "1"); // The queue makes it: a new take in the cast's voice, picked (awaiting approval). await expect.poll(() => picked("p-1")?.voice ?? null, { timeout: 120_000 }).toBe("am_michael"); expect(picked("p-1")!.engine).toBe("kokoro"); await page.reload(); await expect(page.locator('[data-voices-character="hostman"] [data-voice-drift]')).toHaveAttribute("data-voice-drift", "0"); await expect(page.locator('[data-cast-row="hostman"]')).toContainText("kokoro · am_michael"); await shot(page, "4-after-reread"); }); test("Use for this project: the project's own map, shown as an override of the cast; the cast removed and put back", async ({ page }) => { await page.goto(urls.voicesHref(P)); const guest = page.locator('[data-voices-character="@guest"]'); await guest.locator("[data-voices-audition-open]").click(); const box = page.locator("[data-audition-panel]"); await expect(box).toHaveAttribute("data-audition-panel", "@guest"); // No real voice named for the guest: candidates are not scored. await box.locator("[data-audition-line]").selectOption("stock"); await expect(box.locator("[data-audition-said]")).toContainText("you cannot make this stuff up"); await box.locator("[data-audition-engine]").selectOption("kokoro"); await box.locator("[data-audition-voice]").selectOption("af_heart"); await box.locator("[data-audition-add]").click(); await box.locator("[data-audition-render]").click(); await expect(box.locator("[data-audition-job]")).toHaveAttribute("data-audition-job", "done", { timeout: 120_000 }); await expect(box.locator("[data-audition-no-reference]")).toBeVisible(); await expect(box.locator('[data-audition-result="kokoro:af_heart"]')).toHaveAttribute("data-audition-state", "ready"); // Everywhere first (the cast), then this project's own choice over it. await box.locator('[data-audition-result="kokoro:af_heart"] [data-audition-use="cast"]').click(); await expect(page.locator('[data-cast-row="guest"]')).toBeVisible(); await box.locator('[data-audition-result="orpheus:zac"] [data-audition-use="project"]').click(); await expect(page.locator('[data-voices-character="guest"]')).toHaveAttribute("data-voice-from", "project"); expect(JSON.parse(readFileSync(path.join(DIR, "narration", "voices.json"), "utf8"))).toEqual({ guest: "orpheus:zac" }); await expect(page.locator("[data-voices-overrides]")).toContainText("@guest reads in orpheus · zac here — the cast says kokoro · af_heart (an override on purpose)"); await shot(page, "5-override"); // The cast: removed, then put back by Undo. await page.locator('[data-cast-remove="guest"]').click(); await expect(page.locator("[data-cast-list]")).toHaveAttribute("data-cast-list", "0"); await page.locator("[data-cast-undo]").click(); await expect(page.locator('[data-cast-row="guest"]')).toBeVisible(); await expect(page.locator("[data-voices-status]")).toContainText("undone: rm guest"); }); test("a recording dropped on an item: kept as recorded, converted to the character's stock voice, picked", async ({ page }) => { await page.goto(urls.voicesHref(P)); // Put the host in the cast (Use everywhere, from an audition of his current voice: nothing to render twice). await page.locator('[data-voices-character="@hostman"] [data-voices-audition-open]').click(); // The audition box (its key follows the character into the cast: @hostman, then hostman). const box = page.locator("[data-audition-panel]"); await expect(box).toHaveAttribute("data-audition-panel", "@hostman"); await box.locator("[data-audition-render]").click(); await expect(box.locator("[data-audition-job]")).toHaveAttribute("data-audition-job", "done", { timeout: 120_000 }); await box.locator('[data-audition-result="orpheus:zac"] [data-audition-use="cast"]').click(); await expect(page.locator('[data-cast-row="hostman"]')).toBeVisible(); // The guest's item, read by the operator, made the host's voice (as Host Man). const rec = page.locator("[data-recording-import]"); await rec.locator("[data-recording-item]").selectOption("p-3"); await rec.locator("[data-recording-as]").selectOption("hostman"); await rec.locator("[data-recording-file]").setInputFiles(REC); await expect(rec).toContainText("voices-recording.wav"); await shot(page, "6-recording", rec); await rec.locator("[data-recording-send]").click(); await expect.poll(() => picked("p-3")?.steps?.[0]?.op ?? null, { timeout: 120_000 }).toBe("convert"); const takes = entry("p-3").narration!.takes!; expect(takes[0].engine).toBe("recorded"); expect(picked("p-3")!.steps![0].voice).toBe("orpheus:zac"); expect(picked("p-3")!.voice).toBe("zac"); await expect(page.locator("[data-voices-status]")).toContainText("p-3: the recording is in"); // The editor's convert was asked for a STOCK voice, never a file. const conv = (JSON.parse(readFileSync(path.join(FIXTURE, "editor-stub.requests.json"), "utf8")) as { route?: string; voice?: string }[]).filter((r) => r.route === "voice-convert").at(-1)!; expect(conv.voice).toBe("orpheus:zac"); }); test("a character's presets; this video's own characters -- an anonymous one is Anon 1 and never enters the pool; an item's own character", async ({ page }) => { type Own = { id: string; name: string; anonymous?: boolean; shown?: string; handles?: string[]; synthetic?: { voice?: string; speed?: number }; recorded?: { enhance?: boolean; direction?: string } }; const own = () => (JSON.parse(readFileSync(CHARACTERS, "utf8")) as { format: string; characters: Own[] }).characters; const itemCharacter = (id: string) => (entry(id).narration as { character?: string } | undefined)?.character ?? null; await page.goto(urls.voicesHref(P)); const host = page.locator('[data-voices-character="@hostman"]'); await expect(host).toBeVisible({ timeout: 30_000 }); // Keep in this video only: an author with no character gets one here, in the voice they read in now; its presets open. await host.locator("[data-character-keep]").click(); const ed = page.locator('[data-preset-editor="hostman"]'); await expect(ed).toHaveAttribute("data-preset-scope", "project"); expect(own().map((c) => [c.id, c.handles, c.synthetic?.voice])).toEqual([["hostman", ["hostman"], "orpheus:zac"]]); const row = page.locator('[data-voices-character="hostman"]'); await expect(row).toHaveAttribute("data-voice-from", "project"); await expect(row.locator('[data-voices-layer="project"]')).toBeVisible(); // Both presets, edited by hand: a Kokoro voice at 1.1, not cleaned, a direction for the reader. await ed.locator('[data-preset-engine="voice"]').selectOption("kokoro"); await ed.locator("[data-preset-speed]").fill("1.1"); await ed.locator("[data-preset-enhance]").uncheck(); await ed.locator("[data-preset-direction]").fill("higher, brisk, amused"); await ed.locator("[data-preset-save]").click(); await expect(page.locator("[data-voices-status]")).toContainText("its presets saved for this video only"); const saved = own()[0]; expect(saved.synthetic?.voice).toMatch(/^kokoro:/); expect(saved.synthetic?.speed).toBe(1.1); expect(saved.recorded).toEqual({ enhance: false, direction: "higher, brisk, amused" }); await expect(row.locator("[data-voice-speed]")).toHaveAttribute("data-voice-speed", "1.1"); await expect(page.locator('[data-project-character="hostman"]')).toContainText("not cleaned"); await shot(page, "7-presets", ed); // An anonymous character of this video's, for the guest: shown as Anon 1. const mine = page.locator("[data-project-characters]"); await mine.locator("[data-project-character-id]").fill("anon-a"); await mine.locator("[data-project-character-handles]").fill("guest"); await mine.locator("[data-project-character-anonymous]").check(); await mine.locator("[data-project-character-add-go]").click(); const anon = page.locator('[data-project-character="anon-a"]'); await expect(anon).toContainText("Anon 1"); await expect(anon).toContainText("anonymous"); expect(own().find((c) => c.id === "anon-a")).toMatchObject({ anonymous: true, shown: "Anon 1", handles: ["guest"] }); await expect(page.locator('[data-voices-character="anon-a"]')).toContainText("Anon 1"); // It is never offered the pool -- and the route refuses it (the CLI's words), the cast untouched. await expect(anon.locator("[data-character-pool]")).toHaveCount(0); await expect(page.locator('[data-project-character="hostman"] [data-character-pool]')).toBeVisible(); const refused = await page.evaluate(async (project) => { const r = await fetch("/api/report/voices", { method: "POST", headers: { "content-type": "application/json" }, body: JSON.stringify({ project, op: "character-pool", id: "anon-a" }) }); return { status: r.status, error: ((await r.json()) as { error?: string }).error ?? "" }; }, P); expect(refused.status).toBe(400); expect(refused.error).toContain("anon-a is anonymous: it stays in this video"); expect(existsSync(CAST)).toBe(false); await shot(page, "8-own-characters", mine); // An item read as a character of its own, then by its author's handle again. await mine.locator("[data-item-character-item]").selectOption("p-3"); await mine.locator("[data-item-character-pick]").selectOption("hostman"); await mine.locator("[data-item-character-set]").click(); const it = mine.locator('[data-item-character="p-3"]'); await expect(it).toHaveAttribute("data-item-character-found", "yes"); await expect(it).toContainText("this video's"); expect(itemCharacter("p-3")).toBe("hostman"); await it.locator("[data-item-character-clear]").click(); await expect(it).toHaveCount(0); expect(itemCharacter("p-3")).toBe(null); // Undo: the last change to this video's characters (the anonymous one added) is taken back. await mine.locator("[data-project-characters-undo]").click(); await expect(anon).toHaveCount(0); expect(own().map((c) => c.id)).toEqual(["hostman"]); }); test("proposed characters by rule, no model: Propose, Change, Accept, Reject -- nothing applied before a press, no take touched", async ({ page }) => { type Doc = { characters: { id: string; anonymous?: boolean; shown?: string; synthetic?: { voice?: string } }[]; proposed?: { id: string; by: string; anonymous?: boolean; synthetic?: { voice?: string } }[] }; const doc = () => JSON.parse(readFileSync(CHARACTERS, "utf8")) as Doc; const itemCharacter = (id: string) => (entry(id).narration as { character?: string } | undefined)?.character ?? null; const before = readFileSync(MANIFEST, "utf8"); const take1 = JSON.stringify(picked("p-1")); await page.goto(urls.voicesHref(P)); const box = page.locator("[data-proposed-characters]"); await expect(box).toHaveAttribute("data-proposed-characters", "0", { timeout: 30_000 }); await box.locator("[data-proposals-make]").click(); await expect(page.locator("[data-voices-status]")).toContainText("2 character(s) proposed"); await expect(box).toHaveAttribute("data-proposed-characters", "2"); expect(doc().characters).toEqual([]); expect((doc().proposed ?? []).map((x) => [x.id, x.by])).toEqual([["hostman", "rule"], ["guest", "rule"]]); for (const x of doc().proposed ?? []) expect(x.synthetic?.voice).toMatch(/^kokoro:/); expect(readFileSync(MANIFEST, "utf8")).toBe(before); const guest = box.locator('[data-proposal="guest"]'); await expect(guest.locator("[data-proposal-items]")).toContainText("p-3"); // Change: another stock voice, anonymous. await guest.locator('[data-proposal-change="guest"]').click(); await guest.locator('[data-preset-engine="proposal guest"]').selectOption("kokoro"); await guest.locator('[data-preset-voice="proposal guest"]').selectOption("bm_george"); await guest.locator("[data-proposal-anonymous]").check(); await guest.locator('[data-proposal-save="guest"]').click(); await expect(page.locator("[data-voices-status]")).toContainText("proposal guest changed"); await expect(box.locator('[data-proposal="guest"] [data-proposal-voice]')).toHaveAttribute("data-proposal-voice", "kokoro:bm_george"); expect((doc().proposed ?? []).find((x) => x.id === "guest")).toMatchObject({ anonymous: true, synthetic: { voice: "kokoro:bm_george" } }); expect(readFileSync(MANIFEST, "utf8")).toBe(before); await shot(page, "9-proposed", box); // Accept: this video's character, named on the guest's item -- no take touched. await box.locator('[data-proposal-accept="guest"]').click(); await expect(page.locator("[data-voices-status]")).toContainText("this video's character now"); await expect(box).toHaveAttribute("data-proposed-characters", "1"); expect(doc().characters.map((c) => [c.id, c.anonymous, c.shown, c.synthetic?.voice])).toEqual([["guest", true, "Anon 1", "kokoro:bm_george"]]); expect(itemCharacter("p-3")).toBe("guest"); expect(itemCharacter("p-1")).toBe(null); expect(JSON.stringify(picked("p-1"))).toBe(take1); await expect(page.locator('[data-voices-character="guest"]')).toContainText("Anon 1"); // Reject: dropped, nothing else changes. await box.locator('[data-proposal-reject="hostman"]').click(); await expect(box).toHaveAttribute("data-proposed-characters", "0"); expect(doc().proposed).toBeUndefined(); expect(itemCharacter("p-1")).toBe(null); }); test("the Cut tab's Reads and voices reaches the Voices page", async ({ page }) => { await page.goto(urls.projectHref(P, { tab: "cut" })); const { openPanel } = await import("./panels"); await openPanel(page, "options", "cut-reads"); await page.locator("[data-voices-link]").click(); await expect(page).toHaveURL(/\/voices$/); await expect(page.locator("[data-voices-panel]")).toBeVisible(); });