// Auto-assignment by rule (cast-assign.mjs), pure: the rotation, the stated pronouns, the plan for each author, the // proposals' validation, and a model's answer checked field by field and merged into the rule's row. // // Run: node --test umtool/report-to-video/cast-assign.test.mjs import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { test } from "node:test"; import { ASSIGN_ROTATION, assignPlan, checkModelAnswer, freeIdOf, mergeModelProposal, plainLine, rotationVoice, statedPronounsOf, voiceGender } from "./cast-assign.mjs"; import { STOCK_VOICES, isStockVoice, proposalCharacter, proposalErrors, validateProjectCharacters } from "./cast.mjs"; test("the stock voices are the editor's (common/lib/ttsApps.ts KOKORO_VOICES, ORPHEUS_VOICES)", () => { const ts = readFileSync(new URL("../../common/lib/ttsApps.ts", import.meta.url), "utf8"); const list = (name) => [...new RegExp(`export const ${name} = \\[([^\\]]*)\\]`).exec(ts)[1].matchAll(/"([^"]+)"/g)].map((m) => m[1]); assert.deepEqual([...STOCK_VOICES.kokoro], list("KOKORO_VOICES")); assert.deepEqual([...STOCK_VOICES.orpheus], list("ORPHEUS_VOICES")); assert.ok(isStockVoice("kokoro:am_michael") && isStockVoice("orpheus:zac")); for (const bad of ["am_michael", "kokoro:nobody", "orpheus:am_michael", "kokoro:../x.wav", "", null]) assert.equal(isStockVoice(bad), false, String(bad)); }); test("the rotation: every Kokoro voice once, a male and a female in turn; the next unused, the hint's gender only", () => { assert.equal(ASSIGN_ROTATION.length, STOCK_VOICES.kokoro.length); assert.equal(new Set(ASSIGN_ROTATION).size, ASSIGN_ROTATION.length); assert.deepEqual(ASSIGN_ROTATION.slice(0, 4), ["kokoro:am_adam", "kokoro:af_alloy", "kokoro:am_echo", "kokoro:af_aoede"]); assert.equal(rotationVoice(new Set()), "kokoro:am_adam"); assert.equal(rotationVoice(new Set(["kokoro:am_adam"])), "kokoro:af_alloy"); assert.equal(rotationVoice(new Set(["kokoro:am_adam"]), { hint: "m" }), "kokoro:am_echo"); assert.equal(rotationVoice(new Set(), { hint: "f" }), "kokoro:af_alloy"); assert.equal(voiceGender("bf_emma"), "f"); // All used: the one this run used least, in the rotation's order. const all = new Set(ASSIGN_ROTATION); assert.equal(rotationVoice(all, { taken: ["kokoro:am_adam"] }), "kokoro:af_alloy"); }); test("stated pronouns only: the capture's own words, never a guess from a name", () => { assert.equal(statedPronounsOf({ pronouns: "she/her" }), "she"); assert.equal(statedPronounsOf({ bio: "Writer. he/him. Opinions mine." }), "he"); assert.equal(statedPronounsOf({ bio: "they / them" }), "they"); assert.equal(statedPronounsOf({ author: "Mary Smith", bio: "mother of two" }), null); assert.equal(statedPronounsOf({}), null); }); const post = (id, handle, author, o = {}) => ({ type: "post", id, handle, author, text: "words", ...o }); test("the plan: each author's character from the layers; one with none a PROPOSAL -- rotation past the voices in use, the id from the handle", () => { const manifest = { render: {}, timeline: [ post("p1", "Host", "Host Man"), post("p2", "guest", "The Guest"), post("p3", "stranger", "A Stranger", { pronouns: "she/her" }), post("p4", "mapped", "In The Map"), post("p5", "chosen", "Chose", { narration: { character: "host" } }), post("p6", "kirsche.v", "Kirsche"), { type: "clip", id: "c1" }, post("p7", "host", "Host Man"), ], }; const cast = { format: "umtool-cast", version: 2, characters: [{ id: "host", name: "Host Man", handles: ["host"], synthetic: { voice: "kokoro:am_adam" } }] }; const project = { format: "umtool-project-characters", version: 1, characters: [{ id: "guest", name: "The Guest", handles: ["guest"] }] }; const plan = assignPlan(manifest, { project, cast, map: { mapped: "kokoro:af_alloy" }, used: ["kokoro:af_heart"] }); const by = Object.fromEntries(plan.authors.map((a) => [a.handle, a])); assert.deepEqual(plan.authors.map((a) => a.handle), ["host", "guest", "stranger", "mapped", "chosen", "kirsche.v"], "in the order they come on, once each"); assert.equal(by.host.state, "cast"); assert.deepEqual(by.host.items, ["p1", "p7"]); assert.equal(by.guest.state, "project"); assert.equal(by.mapped.state, "map"); assert.equal(by.chosen.state, "item", "every item of theirs names its own character: left alone"); assert.equal(by.stranger.state, "propose"); // She/her: the first female voice not used (af_alloy is the map's, af_heart the narrator's). assert.equal(by.stranger.voice, "kokoro:af_aoede"); assert.match(by.stranger.proposal.why, /states she/); // Neutral: the first unused in the rotation (am_adam is the cast's). assert.equal(by["kirsche.v"].voice, "kokoro:am_echo"); assert.equal(by["kirsche.v"].proposal.id, "kirsche-v"); assert.deepEqual(plan.proposals.map((x) => [x.id, x.name, x.handles, x.synthetic, x.by]), [ ["stranger", "A Stranger", ["stranger"], { voice: "kokoro:af_aoede" }, "rule"], ["kirsche-v", "Kirsche", ["kirsche.v"], { voice: "kokoro:am_echo" }, "rule"], ]); for (const x of plan.proposals) assert.deepEqual(proposalErrors(x, "p"), []); // Deterministic: the same plan again. assert.deepEqual(assignPlan(manifest, { project, cast, map: { mapped: "kokoro:af_alloy" }, used: ["kokoro:af_heart"] }), plan); // Held already: kept as it is, not proposed again; a held one whose author has a character now is moot. const held = { ...project, proposed: [plan.proposals[0], { id: "gone", name: "Gone", handles: ["guest"], synthetic: { voice: "kokoro:bm_fable" }, by: "rule" }] }; const again = assignPlan(manifest, { project: held, cast, map: { mapped: "kokoro:af_alloy" } }); assert.equal(again.authors.find((a) => a.handle === "stranger").state, "proposed"); assert.deepEqual(again.proposals.map((x) => x.id), ["kirsche-v"]); assert.notEqual(again.proposals[0].synthetic.voice, "kokoro:af_aoede", "a held proposal's voice is in use"); assert.deepEqual(again.moot, ["gone"]); }); test("an id is free of every character and proposal", () => { assert.equal(freeIdOf("Guest", new Set()), "guest"); assert.equal(freeIdOf("guest", new Set(["guest", "guest-2"])), "guest-3"); assert.equal(freeIdOf("___", new Set()), "character"); }); test("a proposal is refused unless its voice is a STOCK one, its preset synthetic (a direction at most), its author named", () => { const ok = { id: "anon", name: "Anon", handles: ["anon"], synthetic: { voice: "kokoro:am_echo" }, by: "rule", why: "no character" }; assert.deepEqual(proposalErrors(ok, "p"), []); assert.deepEqual(proposalCharacter(ok), { id: "anon", name: "Anon", handles: ["anon"], synthetic: { voice: "kokoro:am_echo" } }); assert.match(proposalErrors({ ...ok, synthetic: { voice: "kokoro:nobody" } }, "p").join(), /one of the stock voices/); assert.match(proposalErrors({ ...ok, synthetic: { voice: "voices/me.wav" } }, "p").join(), /stock/); assert.match(proposalErrors({ ...ok, recorded: { convert: { voice: "kokoro:am_echo" } } }, "p").join(), /direction line at most/); assert.match(proposalErrors({ ...ok, handles: [] }, "p").join(), /one handle at least/); assert.match(proposalErrors({ ...ok, by: "magic" }, "p").join(), /by must be/); assert.match(proposalErrors({ ...ok, why: "two\nlines" }, "p").join(), /one line/); const doc = { format: "umtool-project-characters", version: 1, characters: [{ id: "anon", name: "A", handles: [] }], proposed: [ok, ok] }; const errs = validateProjectCharacters(doc).join("; "); assert.match(errs, /is a character already/); assert.match(errs, /two proposals are "anon"/); }); test("the model's answer: untrusted -- a stock voice, a speed in bounds, one plain line; the rule's row stays when nothing is valid", () => { assert.deepEqual(checkModelAnswer({ character: "The Skeptic", voice: "kokoro:bm_george", speed: 1.15, direction: "dry,\u0007 wry\nand slow" }), { refused: [], name: "The Skeptic", voice: "kokoro:bm_george", speed: 1.15, direction: "dry, wry and slow" }); const bad = checkModelAnswer({ character: "\u0000", voice: "/etc/passwd", speed: 9, direction: "" }); assert.equal(bad.voice, undefined); assert.equal(bad.speed, undefined); assert.equal(bad.refused.length, 3); assert.equal(checkModelAnswer({ voice: "kokoro:am_echo", speed: "1.2" }).speed, undefined, "a speed is a number, not words"); assert.equal(plainLine("a\u2028b\tc", 10), "a b c"); const rule = { id: "stranger", name: "A Stranger", handles: ["stranger"], synthetic: { voice: "kokoro:af_aoede" }, by: "rule", why: "no character" }; const m = mergeModelProposal(rule, { character: "The Stranger", voice: "orpheus:tara", speed: 0.9, direction: "warm" }, { model: "qwen3:8b" }); assert.deepEqual(m.proposal, { id: "stranger", name: "The Stranger", handles: ["stranger"], synthetic: { voice: "orpheus:tara", speed: 0.9 }, by: "model", why: "the model's proposal (name, voice, speed, direction)", recorded: { direction: "warm" }, model: "qwen3:8b" }); assert.deepEqual(proposalErrors(m.proposal, "p"), []); assert.equal(mergeModelProposal(rule, { voice: "nope", speed: 7 }).proposal, rule, "nothing valid: the rule's row"); assert.equal(mergeModelProposal(rule, "not even an object").proposal, rule); // Part right: the right part kept, the rest said. const part = mergeModelProposal(rule, { voice: "kokoro:bm_lewis", speed: 5 }); assert.equal(part.proposal.synthetic.voice, "kokoro:bm_lewis"); assert.equal(part.proposal.synthetic.speed, undefined); assert.match(part.proposal.why, /refused: speed 5/); });