// `umtool cast assign` on disk (castCli.mjs assignCommand; cast-assign.mjs): the rule's proposals kept apart in the // project's characters file (one writer, an undo point), applied to nothing; --dry-run writes nothing; accept makes the // character and names it on that author's items (never a picked read); change and reject; a convert target refused. // Every project is a temp dir; no editor. 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 { castCommand } from "./castCli.mjs"; import { projectCharactersFile, readProjectCharacters } from "./castStore.mjs"; import { castAssignRouteOf } from "./castAssignModel.mjs"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-cast-assign-")); after(() => rm(ROOT, { recursive: true, force: true })); const cast = async (args, extra = {}) => { const lines = []; const code = await castCommand(args, { reportsRoot: ROOT, log: (s) => lines.push(s), err: (s) => lines.push(s), ...extra }); return { code, text: lines.join("\n"), json: () => JSON.parse(lines.join("\n")) }; }; const DIR = path.join(ROOT, "assign"); const take = { file: "narration/posts/x-1/take-1.kokoro.wav", engine: "kokoro", voice: "af_heart", seconds: 2, script: "Mine." }; { const post = (id, handle, author, o = {}) => ({ type: "post", id: `p-${id}`, post: `chan/${id}`, platform: "x", author, handle, date: `2026-10-30T1${id}:00:00.000Z`, text: `words of ${id}.`, ...o }); await mkdir(DIR, { recursive: true }); await writeFile(path.join(DIR, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: "assign", title: "Assign", provenance: { channelsDir: "../channels", channelSlug: "chan" }, render: { width: 1920, height: 1080, fps: 30, postNarration: { engine: "kokoro", voice: "af_heart" } }, timeline: [ post("1", "hostman", "Host Man"), post("2", "newbie", "New Person", { narration: { takes: [take], pick: 0 } }), post("3", "newbie", "New Person", { narration: { character: "hostman" } }), post("4", "other", "Someone Else"), ], }, null, 2)); await mkdir(path.join(ROOT, ".umtool"), { recursive: true }); await writeFile(path.join(ROOT, ".umtool", "cast.json"), JSON.stringify({ format: "umtool-cast", version: 2, characters: [{ id: "hostman", name: "Host Man", handles: ["hostman"], synthetic: { voice: "kokoro:am_adam" } }] })); } const manifest = async () => JSON.parse(await readFile(path.join(DIR, "video.manifest.json"), "utf8")); test("assign --dry-run: the plan, nothing written", async () => { const r = await cast(["assign", "assign", "--dry-run", "--json"]); assert.equal(r.code, 0, r.text); const j = r.json(); assert.equal(j.dryRun, true); assert.deepEqual(j.authors.map((a) => [a.handle, a.state, a.character]), [["hostman", "cast", "hostman"], ["newbie", "propose", "newbie"], ["other", "propose", "other"]]); assert.deepEqual(j.proposed.map((x) => x.synthetic.voice), ["kokoro:af_alloy", "kokoro:am_echo"], "past the cast's am_adam (and the narrator's af_heart)"); assert.equal((await readProjectCharacters(DIR)).exists, false, "no file made"); }); test("assign: proposals kept apart, applied to nothing; again: nothing new; accept names the items; change; reject; undo", async () => { const before = JSON.stringify(await manifest()); const r = await cast(["assign", "assign"]); assert.equal(r.code, 0, r.text); assert.match(r.text, /2 proposed now/); assert.match(r.text, /applied to nothing until accepted/); const doc = JSON.parse(await readFile(projectCharactersFile(DIR), "utf8")); assert.deepEqual(doc.characters, [], "no character made"); assert.deepEqual(doc.proposed.map((x) => [x.id, x.by]), [["newbie", "rule"], ["other", "rule"]]); assert.equal(JSON.stringify(await manifest()), before, "no item touched"); // The project view lists them (the Voices page reads it), and a second run proposes nothing new. const view = (await cast(["project", "assign", "--json"])).json(); assert.deepEqual(view.proposed.map((x) => [x.id, x.items]), [["newbie", ["p-2"]], ["other", ["p-4"]]], "p-3 names its own character"); const again = (await cast(["assign", "assign", "--json"])).json(); assert.deepEqual(again.proposed, []); assert.deepEqual(again.held.map((x) => x.id), ["newbie", "other"]); // Change: a voice that is not a stock one is refused; a stock one, a name, anonymity are taken. assert.equal((await cast(["assign", "assign", "--change", "newbie", "--voice", "kokoro:nobody"])).code, 1); const refused = await cast(["assign", "assign", "--change", "newbie", "--convert-voice", "kokoro:am_echo"]); assert.equal(refused.code, 1); assert.match(refused.text, /accept it first/); const ch = await cast(["assign", "assign", "--change", "newbie", "--voice", "kokoro:bm_george", "--speed", "1.1", "--anonymous", "--json"]); assert.equal(ch.code, 0, ch.text); assert.deepEqual(ch.json().proposal.synthetic, { voice: "kokoro:bm_george", speed: 1.1 }); assert.equal(ch.json().proposal.shown, "Anon 1"); assert.equal(ch.json().proposal.by, "person", "a changed row is a person's"); // Accept: this video's character; that author's items that name none name it -- the picked take untouched. const ac = await cast(["assign", "assign", "--accept", "newbie", "--json"]); assert.equal(ac.code, 0, ac.text); assert.deepEqual(ac.json().items, [{ id: "p-2", character: "newbie" }]); const m = await manifest(); assert.equal(m.timeline[1].narration.character, "newbie"); assert.deepEqual(m.timeline[1].narration.takes, [take], "the picked read is never touched"); assert.equal(m.timeline[1].narration.pick, 0); assert.equal(m.timeline[2].narration.character, "hostman", "a person's choice stays"); const after1 = (await readProjectCharacters(DIR)).doc; assert.deepEqual(after1.characters.map((c) => [c.id, c.anonymous ?? false, c.shown ?? null, c.synthetic]), [["newbie", true, "Anon 1", { voice: "kokoro:bm_george", speed: 1.1 }]]); assert.deepEqual(after1.proposed.map((x) => x.id), ["other"]); // Reject: dropped; undo puts it back. assert.equal((await cast(["assign", "assign", "--reject", "nobody"])).code, 1); assert.equal((await cast(["assign", "assign", "--reject", "other"])).code, 0); assert.equal((await readProjectCharacters(DIR)).doc.proposed, undefined, "none left: the key is gone"); assert.equal((await cast(["project", "assign", "undo"])).code, 0); assert.deepEqual((await readProjectCharacters(DIR)).doc.proposed.map((x) => x.id), ["other"]); // One ask at a time. assert.equal((await cast(["assign", "assign", "--accept", "other", "--reject", "other"])).code, 1); assert.equal((await cast(["assign", "assign", "--accept-all"])).code, 0); assert.deepEqual((await readProjectCharacters(DIR)).doc.characters.map((c) => c.id), ["newbie", "other"]); assert.equal((await manifest()).timeline[3].narration.character, "other"); }); test("assign --model: on request only; the model's checked answer laid over the rule's rows, the rule's kept where it fails", async () => { const D2 = path.join(ROOT, "assign-model"); await mkdir(D2, { recursive: true }); const post = (id, handle, author) => ({ type: "post", id, post: `chan/${id}`, platform: "x", author, handle, date: "2026-10-30T10:00:00.000Z", text: `words of ${id}.` }); await writeFile(path.join(D2, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: "assign-model", title: "M", provenance: { channelsDir: "../channels", channelSlug: "chan" }, render: { width: 1920, height: 1080, fps: 30, postNarration: { engine: "kokoro", voice: "af_heart" } }, timeline: [post("p-1", "alpha", "Alpha"), post("p-2", "beta", "Beta"), post("p-3", "hostman", "Host Man")], }, null, 2)); const asked = []; const model = async (project, handles) => { asked.push(handles); return { model: "qwen3:8b", answers: [ { handle: "alpha", character: "The First", voice: "orpheus:tara", speed: 0.9, direction: "slow,\u0007 low" }, { handle: "beta", character: "", voice: "/etc/passwd", speed: 12 }, ] }; }; // Without --model nothing is asked. assert.equal((await cast(["assign", "assign-model", "--dry-run", "--json"], { model })).code, 0); assert.deepEqual(asked, []); // --model --dry-run asks nothing either: it says who would be asked. const dry = await cast(["assign", "assign-model", "--model", "--dry-run", "--json"], { model }); assert.equal(dry.code, 0, dry.text); assert.match(dry.json().model.summary, /would be asked about 2 author\(s\) \(@alpha, @beta\)/); assert.deepEqual(asked, []); const r = await cast(["assign", "assign-model", "--model", "--json"], { model }); assert.equal(r.code, 0, r.text); assert.deepEqual(asked, [["alpha", "beta"]], "only the authors the rule proposed for"); const by = Object.fromEntries(r.json().proposed.map((x) => [x.id, x])); assert.deepEqual([by.alpha.by, by.alpha.name, by.alpha.synthetic, by.alpha.recorded, by.alpha.model], ["model", "The First", { voice: "orpheus:tara", speed: 0.9 }, { direction: "slow, low" }, "qwen3:8b"]); assert.equal(by.beta.by, "rule", "nothing of the model's valid: the rule's row"); assert.match(by.beta.synthetic.voice, /^kokoro:/); assert.match(r.json().model.summary, /^1 of 2 proposal\(s\) took the model's answer/); const held = (await readProjectCharacters(D2)).doc.proposed; assert.deepEqual(held.map((x) => [x.id, x.by]), [["alpha", "model"], ["beta", "rule"]]); // Again: the held rule row (beta) is asked about and improved in its place; the model's own row is not asked again. const again = await cast(["assign", "assign-model", "--model", "--json"], { model: async (_p, handles) => ({ model: "qwen3:8b", answers: handles.map((h) => ({ handle: h, character: "Second", voice: "kokoro:bm_lewis" })) }) }); assert.equal(again.code, 0, again.text); assert.deepEqual(again.json().held.map((x) => [x.id, x.by, x.synthetic.voice]), [["alpha", "model", "orpheus:tara"], ["beta", "model", "kokoro:bm_lewis"]]); assert.deepEqual((await readProjectCharacters(D2)).doc.proposed.map((x) => [x.id, x.by]), [["alpha", "model"], ["beta", "model"]]); // A model that is not heard: exit 1, said; the rule's rows written all the same. await cast(["assign", "assign-model", "--reject-all"]); const down = await cast(["assign", "assign-model", "--model", "--json"], { model: async () => { throw new Error("the editor at http://localhost:1 did not answer"); } }); assert.equal(down.code, 1); assert.match(down.json().model.summary, /not heard .*the rule's proposals stand/); assert.deepEqual((await readProjectCharacters(D2)).doc.proposed.map((x) => [x.id, x.by]), [["alpha", "rule"], ["beta", "rule"]]); // The button's route words. assert.deepEqual(castAssignRouteOf({ routes: { "cast-assign": "local" }, models: { local: "qwen3:8b" } }), { route: "local", model: "qwen3:8b", why: null }); assert.match(castAssignRouteOf({ routes: { "cast-assign": "code" } }).why, /routed to Claude Code/); assert.match(castAssignRouteOf(null).why, /not read/); }); test("a person's row and an anonymous author are never sent to a model, and a person's row is never overwritten", async () => { const D3 = path.join(ROOT, "assign-person"); await mkdir(D3, { recursive: true }); const post = (id, handle, author, o = {}) => ({ type: "post", id, post: `chan/${id}`, platform: "x", author, handle, date: "2026-10-30T10:00:00.000Z", text: `words of ${id}.`, ...o }); await writeFile(path.join(D3, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: "assign-person", title: "P", provenance: { channelsDir: "../channels", channelSlug: "chan" }, render: { width: 1920, height: 1080, fps: 30, postNarration: { engine: "kokoro", voice: "af_heart" } }, timeline: [post("p-1", "edited", "Edited Person"), post("p-2", "secret", "Secret Person"), post("p-3", "named", "Named Person", { narration: { character: "anon-x" } }), post("p-4", "named", "Named Person")], }, null, 2)); await mkdir(path.join(D3, "narration"), { recursive: true }); await writeFile(path.join(D3, "narration", "characters.json"), JSON.stringify({ format: "umtool-project-characters", version: 1, characters: [{ id: "anon-x", name: "Anon X", anonymous: true, shown: "Anon X", handles: [] }] })); // The rule: @named is covered by an anonymous character (an item names it): never proposed for. const plan = (await cast(["assign", "assign-person", "--json"])).json(); assert.deepEqual(plan.authors.map((a) => [a.handle, a.state]), [["edited", "propose"], ["secret", "propose"], ["named", "anonymous"]]); // A person changes two rows: one's voice, one made anonymous. assert.equal((await cast(["assign", "assign-person", "--change", "edited", "--voice", "kokoro:bm_lewis"])).code, 0); assert.equal((await cast(["assign", "assign-person", "--change", "secret", "--anonymous"])).code, 0); const asked = []; const r = await cast(["assign", "assign-person", "--model", "--json"], { model: async (_p, handles) => { asked.push(handles); return { model: "m", answers: [] }; } }); assert.equal(r.code, 0, r.text); assert.deepEqual(asked, [], "nothing to send: a person's rows and an anonymous author are never sent"); const rows = (await readProjectCharacters(D3)).doc.proposed; assert.deepEqual(rows.map((x) => [x.id, x.by, x.synthetic.voice, x.anonymous ?? false]), [["edited", "person", "kokoro:bm_lewis", false], ["secret", "person", rows[1].synthetic.voice, true]]); });