// The redaction list's writer and CLI (`umtool posts redact`), the corpus's // files read beside it (post-redact-files.mjs), and a take's synthesized read // with the terms left out while its script is kept. Made-up terms only. import assert from "node:assert/strict"; import { execFileSync } from "node:child_process"; 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 { postsCommand } from "./postsCli.mjs"; import { makeTake } from "./postNarrationCli.mjs"; import { projectRedaction } from "./postRedaction.mjs"; const ROOT = await mkdtemp(path.join(tmpdir(), "umtool-post-redact-")); 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)); 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 redact: add, ls (numbered, as remove takes them), remove -- the posts' words untouched", async () => { const p = await project("r", [item("1", { text: "I am Quentin Example" })]); let r = await cli(["redact", "r", "add", "Quentin Example", "--note", "a made-up name"]); assert.equal(r.code, 0, r.err); r = await cli(["redact", "r", "add", "Exam", "--substring"]); assert.equal(r.code, 0, r.err); const m = await manifest(p); assert.deepEqual(m.render.posts.redact, [{ term: "Quentin Example", note: "a made-up name" }, { term: "Exam", wholeWord: false }]); assert.equal(m.timeline[0].text, "I am Quentin Example", "the snapshot is never rewritten"); r = await cli(["redact", "r", "ls"]); assert.match(r.out, /r: 2 term\(s\) of its own; 0 from the corpus, 0 post\(s\) marked up/); assert.match(r.out, /1\. Quentin Example {2}-- a made-up name/); assert.match(r.out, /2\. Exam {2}\(inside words too\)/); // The same term twice is refused. r = await cli(["redact", "r", "add", "quentin example"]); assert.equal(r.code, 1); r = await cli(["redact", "r", "remove", "1"]); assert.equal(r.code, 0, r.err); assert.deepEqual((await manifest(p)).render.posts.redact, [{ term: "Exam", wholeWord: false }]); assert.equal((await cli(["redact", "r", "remove", "5"])).code, 1); await cli(["redact", "r", "remove", "1"]); assert.equal((await manifest(p)).render.posts, undefined, "an empty list leaves no key"); }); test("the corpus's terms and a channel's markup are read with the project's list; a broken file fails the read", async () => { const p = await project("c", [item("1")], { posts: { redact: [{ term: "Zed" }] } }); await mkdir(path.join(CH, "subj-x"), { recursive: true }); await writeFile(path.join(ROOT, "redactions.json"), JSON.stringify([{ term: "Quentin Example", note: "everywhere" }])); await writeFile(path.join(CH, "subj-x", "posts-markup.json"), JSON.stringify({ "1": { redact: [{ start: 0, end: 4 }] } })); const red = await projectRedaction(p.dir, await manifest(p)); assert.deepEqual(red.terms.map((t) => t.term), ["Quentin Example", "Zed"]); assert.deepEqual(Object.keys(red.markup), ["subj-x/1"]); assert.match((await cli(["redact", "c", "ls"])).out, /1 term\(s\) of its own; 1 from the corpus, 1 post\(s\) marked up/); await writeFile(path.join(ROOT, "redactions.json"), "{ not json"); await assert.rejects(projectRedaction(p.dir, await manifest(p)), /does not parse/); await rm(path.join(ROOT, "redactions.json")); await rm(path.join(CH, "subj-x", "posts-markup.json")); }); test("a take's synthesized read leaves the term out; the script and the take's record keep it", async () => { const p = await project("t", [item("1", { narration: { script: "Hello, I am Quentin Example. Welcome." } })], { posts: { redact: [{ term: "Quentin Example" }] } }); const asked = []; const ask = async (body) => { asked.push(body); wav(body.out, 1); return { jobId: "job-1", result: { wav: body.out, engine: body.engine, voice: body.voice, seconds: 1 } }; }; const r = await makeTake(p, (await manifest(p)).timeline[0], { ask }); assert.equal(asked[0].text, "Hello, I am. Welcome."); assert.doesNotMatch(asked[0].text, /quentin/i); assert.equal(r.take.script, "Hello, I am Quentin Example. Welcome."); assert.equal((await manifest(p)).timeline[0].narration.script, "Hello, I am Quentin Example. Welcome."); }); test("posts set-item media..blur: on, strength, label, boxes -- merged; off keeps the settings; the file's entry otherwise untouched", async () => { const p = await project("b", [item("1", { media: [{ src: "posts-media/subj-x/1/1.mp4", kind: "video", seconds: 3, width: 320, height: 180 }] })]); let r = await cli(["set-item", "b", "p-1", "media.0.blur=on", "media.0.blur.strength=4", "media.0.blur.boxes=0,0,0.5,0.5;0.5,0.5,0.25,0.25"]); assert.equal(r.code, 0, r.err); assert.deepEqual((await manifest(p)).timeline[0].media[0].blur, { on: true, strength: 4, boxes: [[0, 0, 0.5, 0.5], [0.5, 0.5, 0.25, 0.25]] }); r = await cli(["set-item", "b", "p-1", "media.0.blur.label=off", "media.0.blur.boxes=off"]); assert.equal(r.code, 0, r.err); assert.deepEqual((await manifest(p)).timeline[0].media[0].blur, { on: true, strength: 4, label: false }); r = await cli(["set-item", "b", "p-1", "media.0.blur=off"]); assert.deepEqual((await manifest(p)).timeline[0].media[0].blur, { on: false, strength: 4, label: false }); r = await cli(["set-item", "b", "p-1", "media.0.blur.strength=40"]); assert.equal(r.code, 1, "refused in the build's words"); assert.equal((await manifest(p)).timeline[0].media[0].src, "posts-media/subj-x/1/1.mp4"); });