// post-censor.mjs: a censored attachment -- the knob's check, the blur in the // encode (the whole frame, or its boxes), the label, a channel's markup. import assert from "node:assert/strict"; import { execFileSync, spawnSync } from "node:child_process"; import { mkdtempSync, rmSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { after, test } from "node:test"; import { bandBox, bandErrors, bandText, blurErrors, blurFilters, blurOf, blurSigma, censorEntry, censorSpecOf, labelFilter, pixelGrid } from "./post-censor.mjs"; import { postItemPlan, postLayout, stepBoxes, validatePostItem } from "./post-item.mjs"; const stepBoxesOf = (e, layout) => stepBoxes(postItemPlan(e).steps[0], layout.slot)[0]; import { postSegmentArgs } from "./post-render.mjs"; import { validatePostsMarkup } from "./post-redact.mjs"; const box = { width: 1440, height: 810 }; const entry = (blur) => ({ type: "post", id: "p1", post: "c-x/1", platform: "x", author: "A", handle: "a", date: "2026-10-30T04:00:00Z", text: "it does a thing", media: [{ src: "m/v.mp4", kind: "video", seconds: 3, width: 320, height: 180, audio: false, ...(blur ? { blur } : {}) }], }); const graphOf = (e, fontfile = null) => { const argv = postSegmentArgs({ entry: e, box, plan: postItemPlan(e), layout: postLayout(e, box), cards: { read: "/c/read.png" }, files: ["/m/v.mp4"], render: { fps: 30 }, outPath: "/o.mp4", fontfile }); return argv[argv.indexOf("-filter_complex") + 1]; }; test("the knob: on, a strength 1-10, boxes of fractions, a label or false; validated with the item", () => { assert.deepEqual(blurErrors({ on: true, strength: 4, boxes: [[0.1, 0.1, 0.5, 0.5]], label: false }, "b"), []); const bad = blurErrors({ on: "yes", strength: 11, boxes: [[0.8, 0, 0.5, 0.5]], label: 3, extra: 1 }, "b"); assert.equal(bad.length, 5, bad.join("; ")); assert.ok(validatePostItem(entry({ on: true, strength: 0 })).some((e) => e.includes("media[0].blur.strength must be 1–10"))); assert.deepEqual(blurOf({ blur: { on: true } }), { strength: 7, boxes: [], band: null, label: "Censored", style: "blur" }); assert.equal(blurOf({ blur: { on: false, strength: 3 } }), null, "off keeps its settings and blurs nothing"); }); test("the encode: the whole frame gblurred, or each box split, cropped, blurred and laid back; the label drawn on the frame", () => { const plain = graphOf(entry(null)); assert.doesNotMatch(plain, /gblur/); const whole = graphOf(entry({ on: true }), "/f/bold.ttf"); assert.match(whole, /\[m0_0r\]gblur=sigma=[\d.]+:steps=3\[m0_0\]/); assert.match(whole, /drawtext=fontfile='\/f\/bold.ttf':text='Censored'/); const boxes = graphOf(entry({ on: true, strength: 5, boxes: [[0, 0, 0.5, 0.5], [0.5, 0.5, 0.5, 0.5]], label: "It: 100%" }), "/f/bold.ttf"); assert.equal((boxes.match(/gblur/g) ?? []).length, 2); assert.match(boxes, /\[m0_0r\]split/); assert.match(boxes, /text='It\\: 100\\%'/); assert.doesNotMatch(graphOf(entry({ on: true, label: false }), "/f/bold.ttf"), /drawtext/); assert.equal(blurSigma(7, 1000), 28); assert.deepEqual(blurFilters("a", "b", { blur: { on: false } }, { width: 10, height: 10 }), ["[a]null[b]"]); }); test("a channel's markup censors an attachment in every project; the item's own blur wins", () => { const red = { terms: [], markup: { "c-x/1": { censor: [{ media: 0 }] } } }; const e = entry(null); const out = censorEntry(e, red); assert.deepEqual(out.media[0].blur, { on: true }); assert.equal(e.media[0].blur, undefined, "the entry is never rewritten"); const own = entry({ on: false }); assert.equal(censorEntry(own, red), own); assert.equal(censorEntry(e, { terms: [] }), e); assert.deepEqual(validatePostsMarkup({ "1": { censor: [{ media: 0, blur: { strength: 3 } }] } }), []); assert.equal(validatePostsMarkup({ "1": { censor: [{ media: 9 }, { media: 0, blur: { strength: 30 } }] } }).length, 2); }); test("THE BAND's geometry: below, above, between, left and right, as a box of fractions; their meeting; nothing to censor refused", () => { assert.deepEqual(bandBox({ below: 0.38 }), [0, 0.38, 1, 0.62]); assert.deepEqual(bandBox({ above: 0.2 }), [0, 0, 1, 0.2]); assert.deepEqual(bandBox({ between: [0.2, 0.6] }), [0, 0.2, 1, 0.4]); assert.deepEqual(bandBox({ left: 0.5 }), [0, 0, 0.5, 1]); assert.deepEqual(bandBox({ right: 0.75 }), [0.75, 0, 0.25, 1]); assert.deepEqual(bandBox({ below: 0.4, left: 0.5 }), [0, 0.4, 0.5, 0.6], "both: where they meet (the bottom-left)"); assert.equal(bandBox({}), null); assert.equal(bandBox({ below: 0.999 }), null, "a sliver is no band"); assert.deepEqual(bandErrors({ below: 0.38 }, "b"), []); assert.equal(bandErrors({ below: 0.3, above: 0.6 }, "b").length, 1, "one edge across"); assert.ok(bandErrors({ below: 1.2 }, "b").some((e) => e.includes("fraction of the picture"))); assert.ok(bandErrors({ between: [0.6, 0.2] }, "b").some((e) => e.includes("a < b"))); assert.ok(bandErrors({ top: 0.2 }, "b").some((e) => e.includes("not a band edge"))); assert.ok(bandErrors({}, "b").some((e) => e.includes("names no edge"))); // On the item: validated with it; the band is a region beside the boxes, the label at its corner. assert.deepEqual(blurErrors({ on: true, strength: 9, band: { below: 0.38 }, style: "pixelate" }, "b"), []); assert.ok(blurErrors({ on: true, style: "smudge" }, "b").some((e) => e.includes("style must be one of blur, black, pixelate"))); assert.ok(validatePostItem(entry({ on: true, band: { below: 2 } })).some((e) => e.includes("media[0].blur.band.below"))); const bl = blurOf({ blur: { on: true, strength: 9, boxes: [[0.1, 0.1, 0.2, 0.2]], band: { below: 0.38 } } }); assert.deepEqual(bl, { strength: 9, boxes: [[0.1, 0.1, 0.2, 0.2], [0, 0.38, 1, 0.62]], band: [0, 0.38, 1, 0.62], label: "Censored", style: "blur" }); assert.deepEqual(pixelGrid(7, 1, 1, 500, 280), [18, 10]); }); test("the censor in the knob's words: an edge and its place, a style, a strength", () => { assert.deepEqual(censorSpecOf("below:0.38:blur"), { band: { below: 0.38 }, style: "blur" }); assert.deepEqual(censorSpecOf("between:0.2,0.6:black"), { band: { between: [0.2, 0.6] }, style: "black" }); assert.deepEqual(censorSpecOf("below:0.4:left:0.5:pixelate:8"), { band: { below: 0.4, left: 0.5 }, style: "pixelate", strength: 8 }); assert.deepEqual(censorSpecOf("pixelate"), { style: "pixelate" }); assert.match(censorSpecOf("below").error, /takes a fraction/); assert.match(censorSpecOf("below:0.3:smudge").error, /not an edge/); assert.match(censorSpecOf("below:0.3:11").error, /strength must be 1–10/); assert.equal(bandText({ between: [0.2, 0.6], left: 0.5 }), "between:0.2,0.6:left:0.5"); }); test("the encode of a band: blurred, black or pixelated under the line, the label at the band's corner", () => { const blurred = graphOf(entry({ on: true, strength: 9, band: { below: 0.5 } }), "/f/bold.ttf"); assert.match(blurred, /\[m0_0r\]split\[m0_0_k0a\]\[m0_0_k0b\]/); assert.equal((blurred.match(/gblur/g) ?? []).length, 1); const black = graphOf(entry({ on: true, band: { below: 0.5 }, style: "black", label: false })); assert.match(black, /drawbox=x=0:y=\d+:w=\d+:h=\d+:color=black@1:t=fill/); assert.doesNotMatch(black, /gblur|split/); const pix = graphOf(entry({ on: true, band: { above: 0.5 }, style: "pixelate" })); assert.match(pix, /scale=\d+:\d+:flags=neighbor,scale=\d+:\d+:flags=neighbor/); const whole = graphOf(entry({ on: true, style: "pixelate" })); assert.match(whole, /\[m0_0r\]scale=\d+:\d+:flags=neighbor/); // The label: at the band's top-left, not the attachment's. const lf = labelFilter("a", "b", { blur: { on: true, band: { below: 0.5 } } }, { x: 100, y: 50, width: 400, height: 200 }, { from: 0, to: 3, fontfile: "/f.ttf" }); assert.match(lf, /x=113:y=163:/); const lf0 = labelFilter("a", "b", { blur: { on: true } }, { x: 100, y: 50, width: 400, height: 200 }, { from: 0, to: 3, fontfile: "/f.ttf" }); assert.match(lf0, /x=113:y=63:/); }); const ROOT = mkdtempSync(path.join(tmpdir(), "post-censor-")); after(() => rmSync(ROOT, { recursive: true, force: true })); test("the encode runs: a blurred frame is smoother than the clean one (ffmpeg)", () => { const src = path.join(ROOT, "v.mp4"); execFileSync("ffmpeg", ["-v", "error", "-y", "-f", "lavfi", "-i", "testsrc=s=320x180:d=3:r=30", "-c:v", "libx264", "-pix_fmt", "yuv420p", src]); const png = path.join(ROOT, "card.png"); execFileSync("ffmpeg", ["-v", "error", "-y", "-f", "lavfi", "-i", "color=c=black:s=1440x810", "-frames:v", "1", png]); const detail = (blur) => { const e = entry(blur); const out = path.join(ROOT, `o-${blur ? "b" : "c"}.mp4`); const argv = postSegmentArgs({ entry: e, box, plan: postItemPlan(e), layout: postLayout(e, box), cards: { read: png }, files: [src], render: { fps: 30 }, outPath: out, encode: ["-c:v", "libx264", "-preset", "ultrafast"] }); execFileSync("ffmpeg", argv); // The frame's edge energy: a blur takes most of it away. const r = spawnSync("ffmpeg", ["-v", "info", "-ss", "1", "-i", out, "-frames:v", "1", "-vf", "edgedetect,signalstats,metadata=print:key=lavfi.signalstats.YAVG", "-f", "null", "-"], { encoding: "utf8" }); return `${r.stdout}${r.stderr}`; }; const yavg = (s) => Number(/YAVG=([\d.]+)/.exec(s)?.[1] ?? NaN); const clean = yavg(detail(null)); const blurred = yavg(detail({ on: true, label: false })); assert.ok(Number.isFinite(clean) && Number.isFinite(blurred), `${clean} ${blurred}`); assert.ok(blurred < clean * 0.6, `edges: clean ${clean}, blurred ${blurred}`); }); test("the encode runs: a band below the middle blurs the foot and leaves the head sharp (ffmpeg)", () => { const src = path.join(ROOT, "vb.mp4"); execFileSync("ffmpeg", ["-v", "error", "-y", "-f", "lavfi", "-i", "testsrc=s=320x180:d=3:r=30", "-c:v", "libx264", "-pix_fmt", "yuv420p", src]); const png = path.join(ROOT, "cardb.png"); execFileSync("ffmpeg", ["-v", "error", "-y", "-f", "lavfi", "-i", "color=c=black:s=1440x810", "-frames:v", "1", png]); const run = (blur, name) => { const e = entry(blur); const out = path.join(ROOT, `${name}.mp4`); const layout = postLayout(e, box); execFileSync("ffmpeg", postSegmentArgs({ entry: e, box, plan: postItemPlan(e), layout, cards: { read: png }, files: [src], render: { fps: 30 }, outPath: out, encode: ["-c:v", "libx264", "-preset", "ultrafast"] })); return { out, b: stepBoxesOf(e, layout) }; }; const energy = ({ out, b }, half) => { const y = half === "top" ? b.y : b.y + Math.floor(b.height / 2); const r = spawnSync("ffmpeg", ["-v", "info", "-ss", "1", "-i", out, "-frames:v", "1", "-vf", `crop=${b.width}:${Math.floor(b.height / 2) - 4}:${b.x}:${y + 2},edgedetect,signalstats,metadata=print:key=lavfi.signalstats.YAVG`, "-f", "null", "-"], { encoding: "utf8" }); return Number(/YAVG=([\d.]+)/.exec(`${r.stdout}${r.stderr}`)?.[1] ?? NaN); }; const clean = run(null, "clean-b"); const band = run({ on: true, strength: 9, band: { below: 0.5 }, label: false }, "band-b"); assert.ok(Math.abs(energy(band, "top") - energy(clean, "top")) < energy(clean, "top") * 0.15, "the head as sharp as the clean frame"); assert.ok(energy(band, "bottom") < energy(clean, "bottom") * 0.5, `the foot blurred (${energy(band, "bottom")} vs ${energy(clean, "bottom")})`); });