// The slur filter's writer and CLI (`umtool posts slurs`), a take's read under // each voice (skip, placeholder, beep -- word-timed, or skipped when it cannot // be), and the uncensored take (`umtool take … --uncensored`). The slurs come // from the shipped list; no test spells one out, and no CLI line prints one. import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { readFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { after, test } from "node:test"; import { postsCommand } from "./postsCli.mjs"; import { makeTake } from "./postNarrationCli.mjs"; import { projectRedaction } from "./postRedaction.mjs"; import { takeCommand } from "./takeCli.mjs"; import { PROJECT_KINDS } from "../projects/kinds.mjs"; const LIST = JSON.parse(readFileSync(new URL("../../report-to-video/slurs.json", import.meta.url), "utf8")); const F = LIST.entries.find((e) => e.id === "f-slur").word; const N = LIST.entries.find((e) => e.id === "n-word").word; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-post-slurs-")); after(() => rm(ROOT, { recursive: true, force: true })); const REPORTS = path.join(ROOT, "reports"); const CH = path.join(ROOT, "channels"); const wav = (f, secs) => execFileSync("ffmpeg", ["-v", "error", "-y", "-f", "lavfi", "-i", `sine=f=220:d=${secs}`, "-ar", "48000", "-ac", "1", f]); const item = (id, o = {}) => ({ type: "post", id: `p-${id}`, post: `subj-x/${id}`, platform: "x", author: "THE SUBJECT", handle: "Subject", date: "2026-10-30T10:00:00.000Z", text: `post ${id}`, ...o, }); async function project(name, timeline, render = {}) { const dir = path.join(REPORTS, name); await mkdir(dir, { recursive: true }); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify({ schemaVersion: 1, slug: name, title: name, provenance: { siteOrigin: "", channelSlug: "subj-x", channelsDir: CH }, render: { width: 1920, height: 1080, fps: 30, ...render }, timeline, }, null, 2)); await writeFile(path.join(dir, "project.json"), JSON.stringify({ kind: PROJECT_KINDS.find((k) => k.makesTakes).id, title: name }) + "\n"); return { id: name, dir }; } const manifest = async (p) => JSON.parse(await readFile(path.join(p.dir, "video.manifest.json"), "utf8")); const cli = async (args) => { const lines = []; const errs = []; const code = await postsCommand(args, { root: REPORTS, out: (s) => lines.push(s), err: (s) => errs.push(s) }); return { code, out: lines.join("\n"), err: errs.join("\n") }; }; test("posts slurs ls: what the cut would mask, per slur and per post, masks only -- and each read's state", async () => { const p = await project("ls", [ item("1", { text: `you ${F}, and ${F} again` }), item("2", { text: "clean", quoted: { author: "Q", handle: "q", text: `a ${N}` } }), item("3", { text: "nothing", narration: { script: `he said ${F}`, takes: [{ file: "narration/posts/3/take-1.orpheus-zac.wav", seconds: 2, script: `he said ${F}` }] } }), item("4"), ]); const r = await cli(["slurs", "ls", "ls"]); assert.equal(r.code, 0, r.err); assert.match(r.out, /the slur filter is ON -- level all, visual placeholder, voice beep/); assert.match(r.out, /masks 4 word\(s\) in 3 post item\(s\)/); assert.match(r.out, /f\*{5} +f-slur +×3 +in 2 post\(s\): p-1, p-3/); assert.match(r.out, /p-2 +n\*{5} ×1 +\(quoted\.text\)/); assert.match(r.out, /p-3 +NEEDS WORD TIMINGS/); assert.ok(!r.out.toLowerCase().includes(F) && !r.out.toLowerCase().includes(N), "ls never prints a slur"); const j = JSON.parse((await cli(["slurs", "ls", "--json"])).out); assert.equal(j.total, 4); assert.deepEqual(j.bySlur["f-slur"].posts, ["p-1", "p-3"]); assert.equal(j.reads[0].needs, "word timings"); // Timed: covered (beeped, as the default voice beep has it). const m = await manifest(p); m.timeline[2].narration.takes[0].words = [{ w: "he", start: 0, end: 0.3 }, { w: "said", start: 0.3, end: 0.6 }, { w: F, start: 0.6, end: 1 }]; await writeFile(path.join(p.dir, "video.manifest.json"), JSON.stringify(m)); assert.match((await cli(["slurs", "ls", "ls"])).out, /p-3 +beeped \(1 span\(s\)\)/); }); test("posts slurs edges [--off]: one writer; ls says the edges and the masks, never the word", async () => { const NA = LIST.entries.find((e) => e.id === "n-word").forms.find((f) => f.length === 5 && f.endsWith("a")); const p = await project("edges", [item("1", { text: `you ${F}, ${NA}` })]); const run = async (...a) => { const r = await cli(["slurs", "edges", ...a]); for (const w of [F, NA]) assert.ok(!(r.out + r.err).toLowerCase().includes(w), "no line prints a slur"); return r; }; let r = await run("edges", NA); assert.equal(r.code, 0, r.err); assert.match(r.out, /n\*\*\*a: edge-masked/); assert.deepEqual(JSON.parse((await run("edges", "f-slur", "--json")).out).slurs.edges, ["n***a", "f-slur"], "a write's JSON shows the edges by mask"); assert.deepEqual((await manifest(p)).render.posts.slurs, { edges: [NA, "f-slur"] }); r = await run("ls"); assert.match(r.out, /edges: n\*\*\*a, f-slur \(first letter, stars, last letter\)/); assert.match(r.out, /f\*{4}t +f-slur +×1/); assert.match(r.out, /n\*{3}a +n-word +×1/); const j = JSON.parse((await run("ls", "--json")).out); assert.deepEqual(j.edges, ["n***a", "f-slur"]); assert.equal(j.bySlur["n-word"].mask, "n***a"); assert.equal((await run("edges", NA)).code, 1, "already on the edges list"); assert.equal((await run("edges", "zorbo")).code, 2, "names no slur"); assert.equal((await run("edges")).code, 2); r = await run("edges", NA, "--off"); assert.equal(r.code, 0, r.err); assert.match(r.out, /n\*\*\*a: back to the first-letter mask/); assert.equal((await run("edges", NA, "--off")).code, 1, "not on the edges list"); await run("edges", "f-slur", "--off"); assert.equal((await manifest(p)).render.posts, undefined, "no edges, no key"); // The project's own word may be edged too (added first). await run("add", "zorbo"); assert.match((await run("edges", "zorbo")).out, /z\*\*\*o: edge-masked/); assert.equal((await manifest(p)).timeline[0].text, `you ${F}, ${NA}`, "the snapshot is never rewritten"); }); test("posts slurs on|off|level|visual|voice|add|except|remove: one writer, refused in the build's words, the posts untouched", async () => { const p = await project("w", [item("1", { text: `a ${F} and a zorbo` })]); const ok = async (...a) => { const r = await cli(["slurs", "w", ...a]); assert.equal(r.code, 0, r.err); assert.ok(!r.out.toLowerCase().includes(F), "no line prints a slur"); return r.out; }; assert.match(await ok("off"), /OFF: this is the uncensored cut/); assert.deepEqual((await manifest(p)).render.posts.slurs, { on: false }); await ok("on"); assert.equal((await manifest(p)).render.posts, undefined, "the defaults leave no key"); await ok("level", "strong"); await ok("visual", "blackout"); await ok("voice", "skip"); assert.match(await ok("add", "zorbo"), / \*{5} is masked in this cut/); await ok("except", F); assert.deepEqual((await manifest(p)).render.posts.slurs, { level: "strong", visual: "blackout", voice: "skip", add: ["zorbo"], except: [F] }); const red = await projectRedaction(p.dir, await manifest(p)); assert.equal(red.slurs.visual, "blackout"); assert.ok(red.slurs.entries.some((e) => e.word === "zorbo")); assert.ok(!red.slurs.entries.some((e) => e.forms.includes(F))); await ok("remove", F); await ok("level", "all"); assert.deepEqual((await manifest(p)).render.posts.slurs, { visual: "blackout", voice: "skip", add: ["zorbo"] }); // The defaults (placeholder + beep) back: their keys go. await ok("visual", "placeholder"); await ok("voice", "beep"); assert.deepEqual((await manifest(p)).render.posts.slurs, { add: ["zorbo"] }); assert.equal((await cli(["slurs", "w", "add", "zorbo"])).code, 1, "already on the list"); assert.equal((await cli(["slurs", "w", "voice", "hum"])).code, 2); assert.equal((await cli(["slurs", "w", "remove", "nothing"])).code, 1); assert.equal((await cli(["slurs", "w", "sideways"])).code, 2); assert.equal((await manifest(p)).timeline[0].text, `a ${F} and a zorbo`, "the snapshot is never rewritten"); assert.match((await cli(["slurs", "--help"])).err, /THE SLUR FILTER/); }); const asker = (asked) => async (body) => { asked.push(body); wav(body.out, 1); return { jobId: `job-${asked.length}`, result: { wav: body.out, engine: body.engine, voice: body.voice, seconds: 1 } }; }; test("a take's read: skip leaves the slur out, placeholder says its stand-in; the take records which; the script keeps the word", async () => { const script = `Hello, you ${F}. Welcome.`; for (const [voice, said, state] of [["skip", "Hello, you. Welcome.", "skip"], ["placeholder", "Hello, you f-slur. Welcome.", "placeholder"]]) { const p = await project(`r-${voice}`, [item("1", { narration: { script } })], { posts: { slurs: { voice } } }); const asked = []; const r = await makeTake(p, (await manifest(p)).timeline[0], { ask: asker(asked) }); assert.equal(asked[0].text, said); assert.equal(r.take.slurs, state); assert.equal(r.take.script, script); assert.equal((await manifest(p)).timeline[0].narration.takes[0].slurs, state); } // The filter off (an uncensored project): said, and recorded so -- a later censored cut will cover it. const p = await project("r-off", [item("1", { narration: { script } })], { posts: { slurs: { on: false } } }); const asked = []; assert.equal((await makeTake(p, (await manifest(p)).timeline[0], { ask: asker(asked) })).take.slurs, "said"); assert.equal(asked[0].text, script); // No slur: no state. const q = await project("r-none", [item("1", { narration: { script: "Hello there." } })]); assert.equal((await makeTake(q, (await manifest(q)).timeline[0], { ask: asker([]) })).take.slurs, undefined); }); test("a beep take: said, then word-timed so the render can beep it; when it cannot be timed, synthesized again with the slur left out", async () => { const script = `Hello, you ${F}. Welcome.`; const p = await project("b", [item("1", { narration: { script } })], { posts: { slurs: { voice: "beep" } } }); const asked = []; const ops = []; const askOp = async (route, body) => { ops.push(route); await writeFile(body.out, JSON.stringify({ words: [{ w: "Hello,", start: 0, end: 0.3 }, { w: "you", start: 0.3, end: 0.5 }, { w: `${F}.`, start: 0.5, end: 0.9 }, { w: "Welcome.", start: 1, end: 1.4 }] })); return { jobId: "t-1", result: {} }; }; const r = await makeTake(p, (await manifest(p)).timeline[0], { ask: asker(asked), askOp }); assert.equal(asked.length, 1); assert.equal(asked[0].text, script); assert.deepEqual(ops, ["/api/ops/transcribe"]); assert.equal(r.take.slurs, "said"); assert.equal(r.take.words.length, 4); assert.deepEqual(r.take.words[2], { w: `${F}.`, start: 0.5, end: 0.9 }); // The editor cannot: the read again, the slur left out. const q = await project("b2", [item("1", { narration: { script } })], { posts: { slurs: { voice: "beep" } } }); const asked2 = []; const notes = []; const r2 = await makeTake(q, (await manifest(q)).timeline[0], { ask: asker(asked2), askOp: async () => { throw new Error("no parakeet worker"); }, emit: (_e, f) => notes.push(f.message) }); assert.deepEqual(asked2.map((b) => b.text), [script, "Hello, you. Welcome."]); assert.equal(r2.take.slurs, "skip"); assert.equal(r2.take.words, undefined); assert.match(notes.join("\n"), /could not be word-timed for the beep \(no parakeet worker\)/); }); test("a beep take with the transcription lane HELD: never waits on it -- kept as read, the timing deferred to `posts narrate --words`; a timing past its deadline likewise", async () => { const script = `Hello, you ${F}. Welcome.`; const p = await project("held", [item("1", { narration: { script } })], { posts: { slurs: { voice: "beep" } } }); const asked = []; const notes = []; let transcribed = 0; // A transcribe op that would wait forever while the lane is held (a promise that never settles): never asked. const askOp = async () => { transcribed += 1; return new Promise(() => {}); }; const r = await makeTake(p, (await manifest(p)).timeline[0], { ask: asker(asked), askOp, laneHeld: async () => true, emit: (_e, f) => notes.push(f.message) }); assert.equal(transcribed, 0, "the held lane is never waited on"); assert.equal(asked.length, 1, "one read, the slur said (for the beep)"); assert.equal(r.take.slurs, "said"); assert.equal(r.take.words, undefined); assert.deepEqual(r.deferred, { words: "umtool posts narrate held p-1 --words" }); assert.match(notes.join("\n"), /transcription lane is held -- its words are NOT timed now .* umtool posts narrate held p-1 --words/); assert.equal((await manifest(p)).timeline[0].narration.takes.length, 1, "the take is kept"); // Not held, but the transcription takes longer than its bound: the same deferral, never an unbounded wait. const q = await project("slow", [item("1", { narration: { script } })], { posts: { slurs: { voice: "beep" } } }); const notes2 = []; const { OpDeadline } = await import("umtool-report-to-video/narrate-via-editor"); const slowOp = async (route, _body, o) => { assert.equal(o.deadlineMs, 50); throw new OpDeadline(route, "t-9", 50); }; const r2 = await makeTake(q, (await manifest(q)).timeline[0], { ask: asker([]), askOp: slowOp, laneHeld: async () => false, timingDeadlineMs: 50, emit: (_e, f) => notes2.push(f.message) }); assert.equal(r2.take.slurs, "said"); assert.deepEqual(r2.deferred, { words: "umtool posts narrate slow p-1 --words" }); assert.match(notes2.join("\n"), /did not finish within 0 s .* time its words later/); }); test("askEditorOp's deadline: a job still queued past it is given up on (OpDeadline), never polled forever", async () => { const { askEditorOp, OpDeadline } = await import("umtool-report-to-video/narrate-via-editor"); const http = await import("node:http"); const srv = http.createServer((req, res) => { res.setHeader("content-type", "application/json"); if (req.method === "POST") return res.end(JSON.stringify({ ok: true, jobId: "j-1" })); res.end(JSON.stringify({ ok: true, job: { status: "queued", queue: { position: 1, queued: 1 } } })); }); await new Promise((ok) => srv.listen(0, "127.0.0.1", ok)); const url = `http://127.0.0.1:${srv.address().port}`; try { await assert.rejects(askEditorOp("/api/ops/transcribe", { path: "/x.wav" }, { editor: { url, token: "t" }, pollMs: 10, deadlineMs: 60 }), (e) => e instanceof OpDeadline && /did not finish/.test(e.message)); } finally { srv.close(); } }); test("umtool take … --uncensored: the slur filter off in the take's patch, its label and take.json say so", async () => { const p = await project("u", [item("1", { text: `you ${F}` })]); await mkdir(path.join(p.dir, "out", "clips-raw"), { recursive: true }); const seen = []; const deps = { render: async (job) => { seen.push(JSON.parse(await readFile(job.manifestPath, "utf8"))); const file = path.join(job.outDir, `${job.slug}.mp4`); await writeFile(file, "render"); return { file }; }, probe: async () => 1, }; const errs = []; const io = { reportsRoot: REPORTS, log: () => {}, err: (s) => errs.push(s), deps }; assert.equal(await takeCommand(["u", "--id", "unc", "--label", "Cut", "--full", "--uncensored"], io), 0, errs.join("\n")); const take = JSON.parse(await readFile(path.join(p.dir, "takes", "unc", "take.json"), "utf8")); assert.equal(take.label, "Cut (uncensored)"); assert.equal(take.slurs, "uncensored"); assert.deepEqual(take.patch, { render: { posts: { slurs: { on: false } } } }); assert.equal(seen[0].render.posts.slurs.on, false); assert.equal(await takeCommand(["u", "--id", "cen", "--label", "Cut", "--full"], io), 0, errs.join("\n")); assert.equal(JSON.parse(await readFile(path.join(p.dir, "takes", "cen", "take.json"), "utf8")).slurs, "censored"); // A censored take is not rebuilt uncensored: that is a take of its own. assert.equal(await takeCommand(["u", "--id", "cen", "--rebuild", "--uncensored"], io), 2); assert.match(errs.at(-1), /made censored/); // The project's manifest is not touched. assert.equal((await manifest(p)).render.posts, undefined); });