// Hidden regions (lane clip-kit, slice E): the rules, a recording's boxes on every clip of it, a clip's own, the graph. import { test } from "node:test"; import assert from "node:assert/strict"; import { chmodSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; import path from "node:path"; import { recordingRegionsOf, censorParts, censorProblems, censorRecordOf, censorRegionsOf, clipCensorErrors, namesClip, renderCensorErrors, stillCensorArgs, validateCensor } from "./clip-censor.mjs"; import { buildVideo, segmentFraming, setProgressMode } from "./build-video.mjs"; import { mergeCensor, withOverrides } from "./vertical-plan.mjs"; import { parseBox, withRecording } from "../lib/report/censorCli.mjs"; const clip = (o = {}) => ({ type: "clip", id: "c1", channel: "chan", video: "rec1", start: 100, end: 160, ...o }); const CHAT = { video: "rec1", boxes: [[0.75, 0, 0.25, 1]], style: "pixelate" }; const TAB = { video: "rec1", channel: "chan", boxes: [{ box: [0, 0.6, 0.3, 0.4], from: 120, to: 130 }] }; test("the rules: boxes of the picture (1 to 8), a box's own span, a style and a strength; per recording, its video", () => { assert.deepEqual(renderCensorErrors([CHAT, TAB]), []); assert.deepEqual(clipCensorErrors({ boxes: [[0.1, 0.1, 0.2, 0.2]], style: "black", from: 101, to: 110 }), []); assert.deepEqual(clipCensorErrors(false), []); const no = (v, re, f = clipCensorErrors) => assert.match(f(v).join("\n"), re, JSON.stringify(v)); no({ boxes: [] }, /a list of 1 to 8 boxes/); no({ boxes: [[0.9, 0, 0.3, 1]] }, /boxes\[0\] is \[x, y, w, h\]/); no({ boxes: [{ box: [0, 0, 0.5, 0.5], from: 5, to: 5.1 }] }, /under 0\.2 s/); no({ boxes: [[0, 0, 0.5, 0.5]], style: "smudge" }, /style is one of blur, black, pixelate/); no({ boxes: [[0, 0, 0.5, 0.5]], strength: 20 }, /strength is 1–10/); no("yes", /is false \(none, not even the recording's\)/); no([{ boxes: [[0, 0, 0.5, 0.5]] }], /video is the recording's id/, renderCensorErrors); no([{ ...CHAT, hidden: true }], /hidden is not a key/, renderCensorErrors); assert.match(validateCensor({ render: {}, timeline: [{ type: "post", id: "p", censor: false }] }).join(), /censor is a clip's/); }); test("a clip's regions: its recording's (any channel, or the one named) and its own; `censor: false` none at all", () => { assert.ok(namesClip(CHAT, clip()) && namesClip(TAB, clip()) && !namesClip(TAB, clip({ channel: "other" })) && !namesClip(CHAT, clip({ video: "rec2" }))); const r = censorRegionsOf(clip({ censor: { boxes: [[0.1, 0.1, 0.1, 0.1]], style: "black" } }), { censor: [CHAT, TAB] }); assert.deepEqual(r.map((g) => [g.style, g.from, g.to]), [["pixelate", null, null], ["blur", 120, 130], ["black", null, null]]); assert.deepEqual(censorRegionsOf(clip({ censor: false }), { censor: [CHAT] }), []); assert.deepEqual(censorRegionsOf(clip({ video: "rec2" }), { censor: [CHAT] }), []); // The channel from the provenance when the clip names none. assert.equal(censorRegionsOf({ type: "clip", video: "rec1" }, { censor: [TAB] }, { channelSlug: "chan" }).length, 1); }); test("the graph: blurred and pixelated regions split, cut and laid back; black filled; a span on the segment's clock; none: nothing", () => { assert.deepEqual(censorParts("0:v", "vcen", [], { width: 1920, height: 1080 }), []); const regions = censorRegionsOf(clip({ censor: { boxes: [[0, 0, 0.1, 0.1]], style: "black" } }), { censor: [CHAT, TAB] }); const parts = censorParts("0:v", "vcen", regions, { width: 1920, height: 1080, from: 110, gaps: [{ from: 115, to: 117 }], seconds: 40 }); assert.equal(parts[0], "[0:v]split[vcen_s0a][vcen_s0b]"); assert.match(parts[1], /^\[vcen_s0b\]crop=480:1080:1440:0,scale=\d+:\d+:flags=neighbor,scale=480:1080:flags=neighbor\[vcen_s0c\]$/); assert.equal(parts[2], "[vcen_s0a][vcen_s0c]overlay=x=1440:y=0[vcen_c0]"); assert.match(parts[4], /gblur=sigma=/); // 120–130 source s, from 110 with 2 s of gap before: 8–18 on the segment's clock. assert.equal(parts[5], "[vcen_s1a][vcen_s1c]overlay=x=0:y=648:enable='between(t,8.000,18.000)'[vcen_c1]"); assert.equal(parts[6], "[vcen_c1]drawbox=x=0:y=0:w=192:h=108:color=black@1:t=fill[vcen]"); }); test("umtool censor's arguments: a box, a box over a span; a recording's set replaced, removed", () => { assert.deepEqual(parseBox("0.75,0,0.25,1"), [0.75, 0, 0.25, 1]); assert.deepEqual(parseBox("0,0.6,0.3,0.4@120-130"), { box: [0, 0.6, 0.3, 0.4], from: 120, to: 130 }); assert.throws(() => parseBox("a,b"), /x,y,w,h/); const list = [CHAT, { video: "rec2", boxes: [[0, 0, 0.5, 0.5]] }]; assert.deepEqual(withRecording(list, { video: "rec1" }, { video: "rec1", boxes: [[0, 0, 1, 0.1]] }).map((r) => [r.video, r.boxes.length]), [["rec2", 1], ["rec1", 1]]); assert.deepEqual(withRecording(list, { video: "rec1" }, null).map((r) => r.video), ["rec2"]); }); test("a chrome pass is refused when the hidden regions changed since the segments were cut (censorProblems)", () => { const e = clip({ censor: { boxes: [[0.1, 0.1, 0.2, 0.2]], style: "black" } }); const want = censorRecordOf(censorRegionsOf(e, {}, {})); assert.deepEqual(censorProblems({ entries: [e], records: [{ censor: want }], render: {} }), [], "the same boxes: passes"); const [msg] = censorProblems({ entries: [e], records: [{}], render: {} }); assert.match(msg, /1 segment\(s\) hide other regions than this cut asks for: c1 \(0 box\(es\) hidden on disk, 1 asked for\)/); assert.match(msg, /run a normal build/); // A recording's box added since (render.censor) is a change too; one taken off as well. assert.equal(censorProblems({ entries: [clip()], records: [{}], render: { censor: [CHAT] } }).length, 1); assert.equal(censorProblems({ entries: [clip()], records: [{ censor: want }], render: {} }).length, 1); // A clip with no picture (audio only) records that it hid nothing; legacy segments with no boxes asked pass. assert.deepEqual(censorProblems({ entries: [e], records: [{ censorNoPicture: true }], render: {} }), []); assert.deepEqual(censorProblems({ entries: [clip()], records: [null], render: {} }), []); }); test("--chrome-only / --chrome-preview: built, a box added, refused before any chrome is rendered", async () => { setProgressMode(() => {}); const dir = mkdtempSync(path.join(tmpdir(), "censor-chrome-")); const env = process.env.HYPERFRAMES_BIN; try { const mark = path.join(dir, "rendered"); const stub = path.join(dir, "hf-stub.sh"); writeFileSync(stub, `#!/bin/sh\ntouch '${mark}'\nexit 1\n`); chmodSync(stub, 0o755); process.env.HYPERFRAMES_BIN = stub; const render = { width: 1920, height: 1080, fps: 30, transition: 0, palette: { bg: "#000000", fg: "#ffffff", muted: "#888888", accent: "#c8752a", amber: "#ffc860" }, chrome: { engine: "hyperframes", layout: "deck", deck: {} } }; const manifestPath = path.join(dir, "m.json"); const c = { id: "c1", type: "clip", video: "a", start: 0, end: 10, date: "2024-09-05" }; // The segment as a build left it: no boxes. const segDir = path.join(dir, "out", "sourced", "segments"); mkdirSync(segDir, { recursive: true }); writeFileSync(path.join(segDir, "c1.mp4"), ""); writeFileSync(path.join(segDir, "c1.cut.json"), JSON.stringify({ version: 1, framing: segmentFraming(render, false) })); // Then a box on the clip, and one on its recording. for (const m of [{ timeline: [{ ...c, censor: { boxes: [[0.75, 0, 0.25, 1]], style: "black" } }], render }, { timeline: [c], render: { ...render, censor: [{ video: "a", boxes: [[0, 0, 0.5, 0.5]] }] } }]) { writeFileSync(manifestPath, JSON.stringify({ slug: "refuse", provenance: { siteOrigin: "https://example.test", channelSlug: "chan" }, ...m })); for (const opts of [{ chromeOnly: true }, { chromePreview: { at: 1, dur: 2 } }]) { await assert.rejects(buildVideo({ manifestPath, opts }), /hide other regions than this cut asks for: c1/); } } assert.equal((await import("node:fs")).existsSync(mark), false, "nothing rendered before the refusal"); } finally { if (env === undefined) delete process.env.HYPERFRAMES_BIN; else process.env.HYPERFRAMES_BIN = env; rmSync(dir, { recursive: true, force: true }); } }); test("a montage cannot hide regions yet: refused by name, its own box or its recording's", () => { const montage = { style: "punchy" }; assert.match(validateCensor({ render: { montage }, timeline: [clip({ censor: { boxes: [[0, 0, 0.5, 0.5]] } })] }).join(), /timeline c1: a montage cannot hide regions yet \(1 box\(es\)\)/); assert.match(validateCensor({ render: { montage, censor: [CHAT] }, timeline: [clip()] }).join(), /a montage cannot hide regions yet/); assert.deepEqual(validateCensor({ render: { montage }, timeline: [clip()] }), [], "a montage with no boxes as before"); assert.deepEqual(validateCensor({ render: { censor: [CHAT] }, timeline: [clip()] }), [], "boxes without a montage as before"); }); test("a still's boxes (stillCensorArgs): those over its second, the whole box; none, no args; a blur after them", () => { const regions = censorRegionsOf(clip({ censor: { boxes: [{ box: [0.5, 0, 0.5, 1], from: 120, to: 130 }], style: "black" } }), {}, {}); assert.deepEqual(stillCensorArgs(regions, 110, { width: 1920, height: 1080 }), [], "outside its span: the still as it was"); const args = stillCensorArgs(regions, 125, { width: 1920, height: 1080 }); assert.equal(args[0], "-filter_complex"); assert.match(args[1], /drawbox=x=960:y=0:w=960:h=1080/); assert.deepEqual(args.slice(2), ["-map", "[cstill]"]); const blurred = stillCensorArgs(regions, 125, { width: 1920, height: 1080 }, ["gblur=sigma=20"]); assert.match(blurred[1], /\[cstill0\]gblur=sigma=20\[cstill\]$/); }); test("a batch's snapshot never takes a box off (mergeCensor, withOverrides): the clip's now and the snapshot's, both", () => { const A = [0, 0, 0.2, 0.2]; const B = [0.5, 0.5, 0.2, 0.2]; assert.deepEqual(mergeCensor({ boxes: [A], style: "black" }, null), { boxes: [A], style: "black" }); assert.deepEqual(mergeCensor(undefined, { boxes: [B] }), { boxes: [B] }); assert.deepEqual(mergeCensor({ boxes: [A], style: "black" }, { boxes: [B, A], style: "blur" }).boxes, [A, B], "a union, no box twice; the clip's style"); assert.equal(mergeCensor({ boxes: [A] }, null).style, undefined); assert.equal(mergeCensor(false, null), false, "a clip that opts out of its recording's boxes, as written"); assert.equal(mergeCensor(undefined, null), undefined); const tl = [{ type: "clip", id: "c1", video: "v", start: 0, end: 4, censor: { boxes: [A] } }]; assert.deepEqual(withOverrides(tl, { c1: { censor: null, vertical: null } })[0].censor, { boxes: [A] }, "a box added after the batch is kept"); assert.equal("censor" in withOverrides([{ ...tl[0], censor: undefined }], { c1: { censor: null } })[0], false); }); test("a recording's regions where no clip exists yet (recordingRegionsOf): render.censor matched on its video and channel", () => { const render = { censor: [CHAT, TAB, { video: "other", boxes: [[0, 0, 0.5, 0.5]] }] }; assert.equal(recordingRegionsOf("rec1", "chan", render, {}).length, 2); assert.equal(recordingRegionsOf("rec1", "elsewhere", render, {}).length, 1, "TAB names its channel"); assert.deepEqual(recordingRegionsOf("rec2", null, render, {}), []); assert.deepEqual(recordingRegionsOf(null, null, render, {}), []); });