// post-slurs.mjs: the slur filter -- the list, the matcher (its spellings and // no substring matches), the masks on the card, the read (skip, stand-in, // beep), the switch -- and its place in the redaction (post-redact.mjs). // The words come from slurs.json: no test spells one out. import { test } from "node:test"; import assert from "node:assert/strict"; import { readFileSync } from "node:fs"; import { BLACKOUT_MARK, SLUR_GAP, bleepFilters, cutSlurState, decodePlaceholder, encodePlaceholder, maskSlurs, placeholdersHtml, resolveSlurs, scriptTokens, slurListErrors, slurMatches, slurReport, slursOn, takeBleeps, validateSlursRender, } from "./post-slurs.mjs"; import { REDACT_MARK, barsHtml, dropMarks, redactEntry, redactionOn, shownLine, spokenRead, validatePostsRender } from "./post-redact.mjs"; import { cutSlurs, defaultSlurList } from "./post-redact-files.mjs"; import { postCardHtml } from "./post-item.mjs"; import { scriptWordsOf } from "./word-zoom.mjs"; const LIST = JSON.parse(readFileSync(new URL("./slurs.json", import.meta.url), "utf8")); const byId = (id) => LIST.entries.find((e) => e.id === id); const F = byId("f-slur").word; // the f-slur, six letters const FS = byId("f-slur").forms.find((f) => f === `${F}s`); const R = byId("r-word").word; // a mild one const N = byId("n-word").word; const ON = resolveSlurs(undefined, LIST); const spaced = (w, sep = " ") => [...w].join(sep); const leet = (w) => w.replace("a", "@").replace("o", "0"); test("the shipped list is well formed, and every entry has forms, a level and a stand-in", () => { assert.deepEqual(slurListErrors(LIST), []); assert.equal(defaultSlurList().entries.length, LIST.entries.length); assert.ok(LIST.entries.some((e) => e.level === "mild") && LIST.entries.some((e) => e.level === "strong")); assert.match(slurListErrors({ entries: [{ id: "x", level: "loud", word: "X1", forms: [], say: "" }] }).join(" "), /level.*word.*forms.*say/); }); test("matching: whole words, without case, through look-alike letters and a word spelt out", () => { const hit = (t) => slurMatches(t, ON).map((h) => t.slice(h.start, h.end)); assert.deepEqual(hit(`you ${F}`), [F]); assert.deepEqual(hit(`${F.toUpperCase()}!`), [F.toUpperCase()]); assert.deepEqual(hit(`two ${FS}, ok`), [FS]); assert.deepEqual(hit(`a ${leet(F)} here`), [leet(F)]); assert.deepEqual(hit(`${F[0]}*${F.slice(2)}`), [`${F[0]}*${F.slice(2)}`]); assert.deepEqual(hit(`a ${spaced(F)} b`), [spaced(F)]); assert.deepEqual(hit(`a ${spaced(F, ".")} b`), [spaced(F, ".")]); assert.deepEqual(hit(`a ${spaced(F, "-")}.`), [spaced(F, "-")]); assert.deepEqual(hit(`#${F} @x_${F}`), [F, F]); assert.deepEqual(hit(`${F}'s thing`), [F]); // The id says which slur it was. assert.equal(slurMatches(`${N}s`, ON)[0].id, "n-word"); }); test("no substring matches: a slur inside a longer word, or a word that only looks like one, is left alone", () => { const none = (t) => assert.deepEqual(slurMatches(t, ON), [], t); none("Scunthorpe"); none("niggardly praise"); none("fire retardant"); none(`${R}ant`); none("raccoon, spice, spicy, Fagin, faggio, kiker, troonish, gooky, tardis"); none(`x${F}`); none(`${F}2`); // Spelt apart only with a separator between EVERY letter: "it's pic" is not one. none("it's pic, the s pic, s pi c"); // Mixed spacing (some letters together) is not a spelling the filter reads. none(`${N.slice(0, 3)} ${N.slice(3)}`); }); test("the level, the project's additions and exceptions", () => { const strong = resolveSlurs({ level: "strong" }, LIST); assert.equal(slurMatches(R, strong).length, 0); assert.equal(slurMatches(R, ON).length, 1); assert.equal(slurMatches(F, strong).length, 1); const own = resolveSlurs({ add: ["zorbo", { word: "blerg", forms: ["blergs"], mask: "b***", say: "b-word" }], except: [FS, "r-word"] }, LIST); assert.equal(slurMatches("a zorbo and two blergs", own).length, 2); assert.equal(own.entries.find((e) => e.word === "zorbo").mask, "*****", "a project's own word is masked whole -- no first letter"); assert.equal(own.entries.find((e) => e.word === "blerg").mask, "b***", "its own mask, when it gives one"); assert.equal(slurMatches(FS, own).length, 0, "an excepted form"); assert.equal(slurMatches(F, own).length, 1, "the rest of its slur"); assert.equal(slurMatches(R, own).length, 0, "an excepted slur, by its id"); assert.equal(slursOn(resolveSlurs({ on: false }, LIST)), false); assert.equal(slursOn(resolveSlurs({}, LIST, { uncensored: true })), false); assert.equal(resolveSlurs({}, LIST, { uncensored: true }).entries.length, ON.entries.length, "what would be masked is still known"); }); test("validateSlursRender refuses what the filter cannot use; render.posts carries it", () => { assert.deepEqual(validateSlursRender({ on: false, level: "strong", visual: "placeholder", voice: "beep", add: ["zorbo"], except: ["n-word"] }), []); const errs = validateSlursRender({ on: "yes", level: "loud", visual: "blur", voice: "hum", add: [3], except: [""], nope: 1 }).join(" | "); for (const k of ["nope", ".on", ".level", ".visual", ".voice", "add[0]", "except[0]"]) assert.ok(errs.includes(k), k); assert.match(validatePostsRender({ posts: { slurs: { visual: "blur" } } }).join(" "), /slurs\.visual/); }); test("the card's masks: a blackout is the redaction's bar; a placeholder is a pill with the mask, never the word", () => { assert.equal(BLACKOUT_MARK, REDACT_MARK); const b = maskSlurs(`you ${F}!`, { ...ON, visual: "blackout" }).text; assert.equal(b, `you ${REDACT_MARK.repeat(F.length)}!`); const p = maskSlurs(`you ${F}! ${F[0].toUpperCase()}${F.slice(1)}`, { ...ON, visual: "placeholder" }).text; assert.equal(decodePlaceholder(p), `you ${F[0]}*****! ${F[0].toUpperCase()}*****`); assert.ok(!p.toLowerCase().includes(F)); // Spelt apart: as many tokens as before (the card's word indices hold), the first the mask, the rest gaps. const sp = maskSlurs(`a ${spaced(F)} b`, { ...ON, visual: "placeholder" }).text; assert.equal(sp.split(/\s+/).length, `a ${spaced(F)} b`.split(/\s+/).length); assert.equal(sp.split(SLUR_GAP).length - 1, F.length - 1); assert.equal(placeholdersHtml(sp), `a ${F[0]}*****${" ".repeat(F.length - 1)} b`); assert.equal(decodePlaceholder(encodePlaceholder("n*****")), "n*****"); assert.equal(dropMarks(sp), `a ${F[0]}***** b`); // Off: untouched. assert.equal(maskSlurs(F, resolveSlurs({ on: false }, LIST)).text, F); }); const post = (text, extra = {}) => ({ id: "p1", type: "post", platform: "x", post: "chan/1", author: "Some One", handle: "someone", date: "2026-01-01T00:00:00Z", text, ...extra, }); test("redactEntry masks the slurs with the redaction, and the card's HTML never holds the word", () => { const e = post(`what a ${F}, said the ${leet(F)}`, { quoted: { author: "Q", handle: "q", text: `${FS} again` }, parents: [{ author: "P", handle: "p", text: `${spaced(F)} indeed` }] }); const red = { terms: [{ term: "said" }], slurs: { ...ON, visual: "placeholder" } }; assert.ok(redactionOn({ terms: [], slurs: ON })); const shown = redactEntry(e, red); for (const v of [shown.text, shown.quoted.text, shown.parents[0].text]) assert.equal(slurMatches(decodePlaceholder(v), ON).length, 0, v); const html = postCardHtml(e, { width: 1280, height: 720, redaction: red }); const low = html.toLowerCase(); for (const w of [F, FS, leet(F), spaced(F)]) assert.ok(!low.includes(w), `the page holds ${w.length}-letter form`); assert.ok(!low.includes(">said<"), "the redaction still applies"); assert.ok((html.match(/data-post-slur=""/g) ?? []).length >= 4); assert.ok(html.includes(`${F[0]}*****`)); assert.ok(html.includes("data-post-blanked"), "the redacted term's bar"); // Blackout: bars, no pill. const bhtml = postCardHtml(e, { width: 1280, height: 720, redaction: { terms: [], slurs: { ...ON, visual: "blackout" } } }); assert.ok(!bhtml.toLowerCase().includes(F) && !bhtml.includes('data-post-slur=""') && bhtml.includes("data-post-blanked")); // UNCENSORED: the words, the redaction still on. const u = postCardHtml(e, { width: 1280, height: 720, redaction: { terms: [{ term: "said" }], slurs: resolveSlurs({}, LIST, { uncensored: true }) } }); assert.ok(u.includes(F) && !u.includes(">said<")); // The entry itself is never touched. assert.ok(e.text.includes(F)); }); test("the read: skip leaves the slur out, placeholder says its stand-in, beep says it (the render covers it); off says it", () => { const e = post("x"); const red = (o) => ({ terms: [{ term: "Quentin" }], slurs: resolveSlurs(o, LIST) }); const text = `Quentin called him a ${F}, twice.`; assert.deepEqual(spokenRead(text, e, red({ voice: "skip" })), { text: "called him a, twice.", slurs: "skip" }); // The default (the operator's "pill + beep"): said, for the render's beep. assert.deepEqual(spokenRead(text, e, red({})), { text: `called him a ${F}, twice.`, slurs: "said" }); assert.deepEqual(spokenRead(text, e, red({ voice: "placeholder" })), { text: "called him a f-slur, twice.", slurs: "placeholder" }); assert.deepEqual(spokenRead(text, e, red({ voice: "beep" })), { text: `called him a ${F}, twice.`, slurs: "said" }); assert.deepEqual(spokenRead(text, e, red({ on: false })), { text: `called him a ${F}, twice.`, slurs: "said" }); assert.deepEqual(spokenRead("nothing here", e, red({})), { text: "nothing here", slurs: null }); // A caption: left out (blackout), or its mask (placeholder); the redaction too. assert.equal(shownLine(`Quentin, you ${F}`, e, red({ visual: "blackout" })), "you"); assert.equal(shownLine(`Quentin, you ${F}`, e, red({})), `you ${F[0]}*****`); }); test("takeBleeps: a take that says a slur is covered at its words' times; without timings it is refused", () => { const script = ` He said ${F} and then ${spaced(F)} again.`; const toks = scriptTokens(script); assert.deepEqual(toks.map((t) => t.w), scriptWordsOf(script), "the take's words are one per these"); const words = toks.map((t, i) => ({ w: t.w, start: i, end: i + 0.5 })); const take = { file: "narration/posts/1/take-1.orpheus-zac.wav", script, words, seconds: 20 }; const s = resolveSlurs({ voice: "beep" }, LIST); const b = takeBleeps(take, s); // "He"(0) "said"(1) F(2) "and"(3) "then"(4) f,a,g,g,o,t (5..10) "again."(11) assert.deepEqual(b, { mode: "beep", spans: [[1.96, 2.56], [4.96, 10.56]] }); assert.equal(takeBleeps(take, resolveSlurs({ voice: "skip" }, LIST)).mode, "mute"); assert.equal(takeBleeps(take, resolveSlurs({}, LIST)).mode, "beep", "the default"); assert.equal(takeBleeps(take, resolveSlurs({ on: false }, LIST)), null); assert.equal(takeBleeps({ ...take, slurs: "skip" }, s), null, "its audio left them out"); assert.match(takeBleeps({ ...take, words: undefined }, s).error, /no word timings/); assert.match(takeBleeps({ ...take, words: words.slice(1) }, s).error, /not of its script/); const f = bleepFilters("v", "vb", b, { seconds: 20 }).join(";"); assert.match(f, /volume=0:enable='between\(t,1\.96,2\.56\)\+between\(t,4\.96,10\.56\)'/); assert.match(f, /aevalsrc=exprs='0\.18\*sin\(2\*PI\*1000\*t\)/); assert.doesNotMatch(bleepFilters("v", "vb", { mode: "mute", spans: [[1, 2]] }).join(";"), /aevalsrc/); }); test("slurReport counts what the cut would mask, per slur and per post; cutSlurState says which a take is", () => { const tl = [post(`${F} ${F}`, { id: "a" }), post("clean", { id: "b", narration: { script: `${N}` } }), { id: "c", type: "clip" }]; const r = slurReport(tl, resolveSlurs({ on: false }, LIST)); assert.equal(r.total, 3); assert.deepEqual(r.bySlur["f-slur"], { mask: `${F[0]}*****`, n: 2, posts: ["a"] }); assert.deepEqual(r.posts.map((p) => p.id), ["a", "b"]); assert.deepEqual(r.posts[1].where, ["script"]); assert.equal(cutSlurState({ timeline: tl }), "censored"); assert.equal(cutSlurState({ timeline: tl, render: { posts: { slurs: { on: false } } } }), "uncensored"); assert.equal(cutSlurState({ timeline: [{ type: "clip" }] }), null); assert.equal(cutSlurs({ render: { posts: { slurs: { level: "strong" } } } }).level, "strong"); }); // EDGE MASKS (render.posts.slurs.edges; operator, 5 Nov): the n-word's "-a" form and the f-slur told apart by // their last letter. The forms are taken from the list by shape, never spelt here. const NA = byId("n-word").forms.find((f) => f.length === 5 && f.endsWith("a")); const NAS = byId("n-word").forms.find((f) => f.length === 6 && f.endsWith("s") && f.startsWith(NA.slice(0, 4))); const NER = byId("n-word").forms.find((f) => f.endsWith("r") && f.length === 6); const star = (w) => `${w[0]}${"*".repeat(w.length - 2)}${w.at(-1)}`; test("edge masks: every form of an edged slur its first letter, stars, its last letter, in the case written", () => { const E = resolveSlurs({ edges: [NA, F] }, LIST); const shown = (t, s = E) => decodePlaceholder(maskSlurs(t, s).text); // The operator's examples, by shape: n***a, n****s, f****t, f*****s, N***A. assert.equal(star(NA), "n***a"); assert.equal(shown(NA), "n***a"); assert.equal(shown(NAS), "n****s"); assert.equal(shown(F), "f****t"); assert.equal(shown(FS), "f*****s"); assert.equal(shown(NA.toUpperCase()), "N***A"); assert.equal(shown(`${NA[0].toUpperCase()}${NA.slice(1)}`), "N***a", "each end in its own case"); // A form names the WHOLE slur: its other forms are edge-masked too (the "-er" one is told apart as well). assert.equal(shown(NER), "n****r"); // By its id, or its word, the same. assert.equal(shown(F, resolveSlurs({ edges: ["f-slur"] }, LIST)), "f****t"); assert.equal(shown(NA, resolveSlurs({ edges: [byId("n-word").word] }, LIST)), "n***a"); // A look-alike at an end is read back as its letter; spelt apart, one pill, the rest gaps (the indices hold). assert.equal(shown(leet(F)), "f****t"); assert.equal(shown(`${NA.slice(0, 4)}@`), "n***a"); assert.equal(shown(`${NA.slice(0, 4)}@`.toUpperCase()), "N***A", "a look-alike end follows an all-caps word"); const sp = maskSlurs(`a ${spaced(F)} b`, E).text; assert.equal(sp.split(/\s+/).length, `a ${spaced(F)} b`.split(/\s+/).length); assert.equal(placeholdersHtml(sp), `a f****t${" ".repeat(F.length - 1)} b`); // Only the slurs named: another keeps its first-letter mask. assert.equal(shown(R), `${R[0]}${"*".repeat(R.length - 1)}`); // The card's pill, a caption, the chapter line: all through the one mask. const html = postCardHtml(post(`you ${F}, ${NA.toUpperCase()}`), { width: 1280, height: 720, redaction: { terms: [], slurs: E } }); assert.ok(html.includes('data-post-slur="">f****t') && html.includes('data-post-slur="">N***A')); for (const w of [F, NA]) assert.ok(!html.toLowerCase().includes(w)); assert.equal(shownLine(`you ${F}`, post("x"), { terms: [], slurs: E }), "you f****t"); // ls / slurReport: each form's mask the cut draws, "/" between. const r = slurReport([post(`${F} ${FS} ${F.toUpperCase()}`, { id: "a" })], E); assert.deepEqual(r.bySlur["f-slur"], { mask: "f****t/f*****s", n: 3, posts: ["a"], edge: true }); // A take's refusal names the edge mask too. assert.match(takeBleeps({ file: "t.wav", script: `said ${NA}` }, E).error, /\(n\*\*\*a\)/); // The read is not a mask: the stand-in is said as before. assert.deepEqual(spokenRead(`a ${NA}`, post("x"), { terms: [], slurs: resolveSlurs({ edges: [NA], voice: "placeholder" }, LIST) }), { text: "a n-word", slurs: "placeholder" }); }); test("edge masks: refused when not words; a cut without edges is unchanged (no key on an entry, the same masks)", () => { assert.deepEqual(validateSlursRender({ edges: [NA, "f-slur"] }), []); const errs = validateSlursRender({ edges: "x" }).concat(validateSlursRender({ edges: [3, ""] })).join(" | "); assert.match(errs, /edges must be a list/); assert.match(errs, /edges\[0\]/); assert.match(errs, /edges\[1\]/); // Without edges: no entry carries `edge`, so the render key and every mask are as before. assert.ok(ON.entries.every((e) => !("edge" in e))); assert.deepEqual(resolveSlurs({ edges: [] }, LIST), ON); assert.equal(decodePlaceholder(maskSlurs(`${NA} ${F.toUpperCase()}`, ON).text), `n***** F*****`); assert.equal(slurReport([post(`${F} ${FS}`, { id: "a" })], ON).bySlur["f-slur"].mask, `${F[0]}*****`); }); test("barsHtml draws placeholders and gaps with the redaction's bars", () => { const html = barsHtml(`${REDACT_MARK.repeat(3)} ${encodePlaceholder("k***")}${SLUR_GAP}`); assert.equal(html, ` k***`); }); test("takeSlurBleeps (post-render.mjs): a take's state is its own or its source's; no script of its own reads the post's; refused in the build's words", async () => { const { takeSlurBleeps } = await import("./post-render.mjs"); const script = `you ${F} there`; const words = scriptTokens(script).map((t, i) => ({ w: t.w, start: i, end: i + 0.5 })); const red = { terms: [], slurs: resolveSlurs({ voice: "beep" }, LIST) }; const entry = (takes) => ({ id: "p1", type: "post", narration: { script, takes } }); const tts = { file: "narration/posts/1/take-1.orpheus-zac.wav", seconds: 3, words }; assert.deepEqual(takeSlurBleeps(entry([tts]), tts, red), { mode: "beep", spans: [[0.96, 1.56]], seconds: 3 }); // Skipped at synthesis: nothing to cover -- and a take derived from it reads the same. const skipped = { ...tts, slurs: "skip", words: undefined }; const derived = { file: "narration/posts/1/take-2.enhance-x.wav", seconds: 3, source: { kind: "take", from: "take-1.orpheus-zac" } }; assert.equal(takeSlurBleeps(entry([skipped, derived]), derived, red), null); assert.throws(() => takeSlurBleeps(entry([{ ...tts, words: undefined }]), { ...tts, words: undefined }, red), /p1: .*no word timings to beep.*posts narrate

p1 --words.*--uncensored/); assert.equal(takeSlurBleeps(entry([tts]), tts, { terms: [], slurs: resolveSlurs({}, LIST, { uncensored: true }) }), null); assert.equal(takeSlurBleeps(entry([tts]), tts, null), null); }); test("with read-follow: the pill sits in its word's slot inside the read's block, as a redaction bar does", () => { const e = post(`First para here.\n\nThen you ${F} and more words follow here.`); const html = postCardHtml(e, { width: 1280, height: 720, follow: true, redaction: { terms: [{ term: "more" }], slurs: ON } }); assert.match(html, /data-post-follow=/); assert.ok(!html.toLowerCase().includes(F)); assert.match(html, /Then<\/span> you<\/span> f\*{5}<\/span><\/span> and<\/span> { const { postSegmentArgs } = await import("./post-render.mjs"); const { postItemPlan, postLayout } = await import("./post-item.mjs"); const e = post(`you ${F} there`, { narration: { script: `you ${F} there`, takes: [{ file: "narration/posts/1/take-1.orpheus-zac.wav", seconds: 3, script: `you ${F} there` }] } }); const plan = postItemPlan(e, {}); const box = { width: 1440, height: 810 }; const args = (bleeps) => { const argv = postSegmentArgs({ entry: e, box, plan, layout: postLayout(e, box), cards: { read: "/c/read.png" }, files: [], render: { fps: 30, audioRate: 48000 }, outPath: "/o/p1.mp4", narration: { file: "/t/take.wav", gainDb: 0, ...(bleeps ? { bleeps } : {}) }, }); return argv[argv.indexOf("-filter_complex") + 1]; }; const plain = args(null); const beeped = args({ mode: "beep", spans: [[0.96, 1.56]], seconds: 3 }); assert.doesNotMatch(plain, /pnbl/); assert.match(beeped, /\[\d+:a\]aresample=48000,aformat=sample_fmts=fltp:channel_layouts=stereo,volume=0:enable='between\(t,0\.96,1\.56\)'\[pnbl_m\]/); assert.match(beeped, /\[pnbl_m\]\[pnbl_t\]amix=inputs=2:normalize=0:duration=first\[pnbl\];\[pnbl\]aresample=48000/); // Everything after the voice's input is the same mix. assert.equal(beeped.slice(beeped.indexOf("[pnbl]aresample")).replace("[pnbl]", ""), plain.slice(plain.search(/\[\d+:a\]aresample/)).replace(/^\[\d+:a\]/, "")); });