// A clip's picture blurred (report-to-video clip-blur.mjs), from umtool's side: // the writer (updateClip merges `blur` as it does `audio`; a default is not // written; a window save may not leave a partial blur's span outside), the // clip editor's `blur` op (token-guarded) and its view, a take's `--set // render.blur=off` (the uncensored variant), and New clip from selection -- // the entry the timeline route's `insert` writes, its quote read from the // cues, its citation moved, a bad blur refused in the build's words. // // Run with: pnpm test:scripts import assert from "node:assert/strict"; import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import test from "node:test"; import { MANIFEST_NAME, StaleToken, StructureRefused, entryFromSpec, insertEntry, manifestToken, updateClip, updateRender } from "./manifest.mjs"; import { clipEditorView, runClipEditorOp } from "./clipEditor.mjs"; import { citeUrlAt, newClipEntry, nextFreeId, parseClipSpec, quoteFromCues, spanQuote } from "./clipEditorModel.mjs"; import { patchFromSets } from "./takeMaker.mjs"; import { mergePatch } from "umtool-report-to-video/montage-styles"; import { BLUR_CAPTION_DEFAULT, clipBlurOf } from "umtool-report-to-video/clip-blur"; import { validatorErrors } from "umtool-report-to-video/lint"; const manifest = () => ({ schemaVersion: 1, slug: "huck", provenance: { channelSlug: "chan", siteOrigin: "https://example.test" }, render: { width: 640, height: 360, fps: 15 }, timeline: [ { type: "clip", id: "eps-film-it", video: "BZCS-Azx_iE", start: 140.04, end: 228.35, cite: 140, section: 2, citeUrl: "https://www.youtube.com/watch?v=BZCS-Azx_iE&t=140s", onscreen: { title: "He films it", subtitle: "a stream" }, quote: "q", }, { type: "clip", id: "next", video: "BZCS-Azx_iE", start: 400, end: 410, section: 3 }, ], }); async function project() { const dir = await mkdtemp(path.join(tmpdir(), "umtool-blur-")); await writeFile(path.join(dir, MANIFEST_NAME), JSON.stringify(manifest(), null, 2) + "\n"); return { id: "reports/huck", dir }; } const read = async (dir) => JSON.parse(await readFile(path.join(dir, MANIFEST_NAME), "utf8")); const entry = (m, id) => m.timeline.find((e) => e.id === id); test("updateClip blur: on, merged key by key, defaults not written, null clears; refused in the build's words", async () => { const { dir } = await project(); try { await updateClip(dir, "eps-film-it", { blur: { on: true } }); assert.deepEqual(entry(await read(dir), "eps-film-it").blur, { on: true }); await updateClip(dir, "eps-film-it", { blur: { caption: " Blurred for the platforms ", strength: "8", from: 150.004 } }); assert.deepEqual(entry(await read(dir), "eps-film-it").blur, { on: true, caption: "Blurred for the platforms", strength: 8, from: 150 }); // The default caption and strength are absent, not written. await updateClip(dir, "eps-film-it", { blur: { caption: BLUR_CAPTION_DEFAULT, strength: 5, from: null } }); assert.deepEqual(entry(await read(dir), "eps-film-it").blur, { on: true }); await assert.rejects(updateClip(dir, "eps-film-it", { blur: { strength: 40 } }), /strength must be a number from 2 to 15/); await assert.rejects(updateClip(dir, "eps-film-it", { blur: { to: 300 } }), /to 300 must lie inside the clip's window 140\.04–228\.35/); await assert.rejects(updateClip(dir, "eps-film-it", { blur: { nope: 1 } }), /blur\.nope is not a blur setting/); await assert.rejects(updateClip(dir, "next", { blur: { caption: "x" } }), /on must be true/, "a caption alone is not a blur"); // A window save may not leave a partial span outside. await updateClip(dir, "eps-film-it", { blur: { from: 200, to: 220 } }); await assert.rejects(updateClip(dir, "eps-film-it", { end: 210 }), /to 220 must lie inside the clip's window/); await updateClip(dir, "eps-film-it", { end: 225 }); // `on: false` (or null) and null clear it all. await updateClip(dir, "eps-film-it", { blur: { on: false } }); assert.equal("blur" in entry(await read(dir), "eps-film-it"), false); await updateClip(dir, "eps-film-it", { blur: { on: true } }); await updateClip(dir, "eps-film-it", { blur: null }); assert.equal("blur" in entry(await read(dir), "eps-film-it"), false); } finally { await rm(dir, { recursive: true, force: true }); } }); test("the clip editor's blur op: token-guarded, writes the key, the view carries it and what a new clip copies", async () => { const p = await project(); try { const view = await clipEditorView(p); assert.deepEqual(view.blur, { caption: BLUR_CAPTION_DEFAULT, captionMax: 120, strength: { min: 2, max: 15, default: 5 }, rendered: true }); const c = view.clips.find((x) => x.id === "eps-film-it"); assert.equal(c.blur, null); assert.equal(c.section, 2); assert.equal(c.citeUrl, "https://www.youtube.com/watch?v=BZCS-Azx_iE&t=140s"); assert.equal(c.ownChannel, null); await assert.rejects(runClipEditorOp(p, { op: "blur", clip: "eps-film-it", blur: { on: true }, token: "stale" }), StaleToken); const out = await runClipEditorOp(p, { op: "blur", clip: "eps-film-it", blur: { on: true, caption: "Blurred here for TOS" }, token: view.token }); assert.deepEqual(entry(await read(p.dir), "eps-film-it").blur, { on: true, caption: "Blurred here for TOS" }); assert.deepEqual(out.view.clips.find((x) => x.id === "eps-film-it").blur, { on: true, caption: "Blurred here for TOS" }); await assert.rejects(runClipEditorOp(p, { op: "blur", clip: "eps-film-it", blur: "yes" }), /blur is \{ on: true/); await runClipEditorOp(p, { op: "blur", clip: "eps-film-it", blur: null }); assert.equal("blur" in entry(await read(p.dir), "eps-film-it"), false); // The uncensored variant in the Look: render.blur "off" (and "on" is no key at all). await updateRender(p.dir, { blur: "off" }); assert.equal((await read(p.dir)).render.blur, "off"); assert.equal((await clipEditorView(p)).blur.rendered, false); await updateRender(p.dir, { blur: "on" }); assert.equal("blur" in (await read(p.dir)).render, false); await assert.rejects(updateRender(p.dir, { blur: "maybe" }), /render\.blur must be "on"/); } finally { await rm(p.dir, { recursive: true, force: true }); } }); test("a take's --set render.blur=off: the uncensored variant of the same cut", () => { const m = manifest(); m.timeline[0].blur = { on: true }; const patch = patchFromSets(["render.blur=off"]); assert.deepEqual(patch, { render: { blur: "off" } }); const take = mergePatch(m, patch); assert.equal(take.render.blur, "off"); assert.deepEqual(validatorErrors(take).filter((e) => /blur/.test(e)), []); assert.equal(clipBlurOf(take.timeline[0], take.render), null); assert.notEqual(clipBlurOf(m.timeline[0], m.render), null, "the project's own cut stays blurred"); }); test("quoteFromCues: in order, a rolled-over cue said once, … where the cues are not adjacent", () => { const cues = [ { start: 228, end: 231, text: "and then he" }, { start: 231, end: 233.5, text: "starts shaking" }, { start: 233.5, end: 235, text: "starts shaking" }, { start: 240, end: 242, text: "call the vet" }, { start: 300, end: 302, text: "out of span" }, ]; assert.equal(quoteFromCues(cues, 228.36, 298), "and then he starts shaking … call the vet"); assert.equal(quoteFromCues(cues, 228.36, 298, { gap: 10 }), "and then he starts shaking call the vet"); assert.equal(quoteFromCues([], 0, 10), ""); // The words win when there are any. const words = [{ w: "only", start: 229, end: 229.5 }, { w: "words", start: 230, end: 230.5 }]; assert.equal(spanQuote({ words, cues }, 228.36, 298), "only words"); assert.equal(spanQuote({ words: [], cues }, 239, 243), "call the vet"); }); test("citeUrlAt, nextFreeId, parseClipSpec: the citation moved, an id the route would keep, the spec", () => { assert.equal(citeUrlAt("https://www.youtube.com/watch?v=BZCS-Azx_iE&t=140s", 228.36), "https://www.youtube.com/watch?v=BZCS-Azx_iE&t=228s"); assert.equal(citeUrlAt("https://youtu.be/x?t=140", 228.9), "https://youtu.be/x?t=228"); assert.equal(citeUrlAt("https://rumble.com/v1.html#t=140", 228), "https://rumble.com/v1.html#t=228"); assert.equal(citeUrlAt("https://mirror.example/vod/1", 228), "https://mirror.example/vod/1", "no t= — kept, it may cite a mirror"); assert.equal(citeUrlAt("not a url", 5), "not a url"); assert.equal(nextFreeId(["eps-film-it", "next"], "eps-film-it"), "eps-film-it-2"); assert.equal(nextFreeId(["eps-film-it", "eps-film-it-2"], "eps-film-it"), "eps-film-it-3"); assert.equal(nextFreeId([], "new clip!"), "new-clip"); assert.deepEqual(parseClipSpec(" chan/BZCS-Azx_iE@228.36-298 "), { channel: "chan", video: "BZCS-Azx_iE", start: 228.36, end: 298 }); assert.equal(parseClipSpec("chan/vid"), null); // The writer reads a spec by the same rule. assert.deepEqual(entryFromSpec([], "chan/v1@3-6"), { type: "clip", id: "v1-3", channel: "chan", video: "v1", start: 3, end: 6 }); }); test("New clip from selection: the entry copies the recording, section, title and citation; inserted after the clip; a bad blur refused", async () => { const p = await project(); try { const view = await clipEditorView(p); const like = view.clips.find((x) => x.id === "eps-film-it"); const e = newClipEntry(like, { start: 228.36, end: 298.004, id: "eps-film-it-2", quote: " and then he starts shaking ", blur: true }); assert.deepEqual(e, { type: "clip", id: "eps-film-it-2", video: "BZCS-Azx_iE", start: 228.36, end: 298, cite: 228, citeUrl: "https://www.youtube.com/watch?v=BZCS-Azx_iE&t=228s", section: 2, onscreen: { title: "He films it" }, quote: "and then he starts shaking", blur: { on: true }, }); // The default caption is written as none; a blank id is the route's to make. assert.deepEqual(newClipEntry(like, { start: 1, end: 2, blur: { caption: BLUR_CAPTION_DEFAULT } }).blur, { on: true }); assert.equal("id" in newClipEntry(like, { start: 1, end: 2, id: " " }), false); // An own channel is carried; the cut's provenance channel is not written. assert.equal(newClipEntry({ ...like, ownChannel: "mirror" }, { start: 1, end: 2 }).channel, "mirror"); // The timeline route's insert (what the form posts): token-guarded, after the clip. await assert.rejects(insertEntry(p.dir, "eps-film-it", e, { token: "stale" }), StaleToken); const r = await insertEntry(p.dir, "eps-film-it", e, { token: await manifestToken(p.dir) }); assert.equal(r.id, "eps-film-it-2"); assert.equal(r.at, 1); const m = await read(p.dir); assert.deepEqual(m.timeline.map((x) => x.id), ["eps-film-it", "eps-film-it-2", "next"]); assert.deepEqual(entry(m, "eps-film-it-2").blur, { on: true }); // The same id again: the route keeps the clip under a fresh one. const again = await insertEntry(p.dir, "eps-film-it", e); assert.equal(again.id, "eps-film-it-2-2"); // A blur the build would refuse is refused at the insert, in its words. await assert.rejects(insertEntry(p.dir, "next", { ...e, id: "bad", blur: { on: true, strength: 99 } }), (err) => err instanceof StructureRefused && /strength must be a number from 2 to 15/.test(err.message)); } finally { await rm(p.dir, { recursive: true, force: true }); } });