// On-screen anonymising (post-anon.mjs): ANONYMITY BELONGS TO THE HANDLE, PROJECT-WIDE -- one set of anonymous handles // (an anonymous character's, and the author of every item that names one by id), consulted by every drawn surface: // the card (head, parents, quoted, repost, Replying to, thread tiles, scene tiles), the words (mentions and platform // URLs, in ONE pass), deck popup posts (no screenshot, no QR), the promise panel and a chat replay; the thread shot // never drawn with anonymity in the video; an unresolvable state refused; flags of the manifest's own stripped; what a // read says (spokenRead) and the lint. The review's probes (review-c3/probe1-2.mjs) are cases here. A card with none // is byte-identical to before. // // Run: node --test umtool/report-to-video/post-anon.test.mjs import assert from "node:assert/strict"; import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { after, test } from "node:test"; import { AnonymityError, anonymiseChatClips, anonymiseEntry, anonymiseManifest, anonymiseWords, anonymityProblems, anonymousHandles, pseudonymOf, speakAnonymised } from "./post-anon.mjs"; import { postCardHtml, postDeckText, sceneTileHtml } from "./post-item.mjs"; import { sceneRefOf } from "./post-conversation.mjs"; import { readProjectCharactersFor } from "./build-video.mjs"; import { validateProjectCharacters } from "./cast.mjs"; import { spokenRead } from "./post-redact.mjs"; import { postWho } from "./chrome-posts.mjs"; const TMP = await mkdtemp(path.join(tmpdir(), "umtool-post-anon-")); after(() => rm(TMP, { recursive: true, force: true })); const doc = (characters) => ({ format: "umtool-project-characters", version: 1, characters }); const ANON = { id: "anon-1", name: "Real Guy", anonymous: true, shown: "Anon 1", handles: ["realguy"] }; const base = (o = {}) => ({ type: "post", id: "p1", post: "x/1", platform: "x", author: "Real Guy", handle: "RealGuy", badge: "blue", date: "2026-01-01T00:00:00Z", text: "hello", ...o }); const card = (e, o = {}) => postCardHtml(e, { width: 1920, height: 1080, avatars: { realguy: "assets/real.jpg", host: "assets/host.jpg" }, ...o }); const LEAKS = ["RealGuy", "realguy", "REALGUY", "Real Guy", "assets/real.jpg", "real.bsky.social"]; const leaks = (html) => LEAKS.filter((s) => html.includes(s)); const pass = (e, d = doc([ANON]), more = []) => anonymiseManifest({ timeline: [e, ...more] }, d).timeline; test("probe 1-2: the handle decides the identity, whatever character an item names (a cast id, another character, a stale id)", () => { for (const [label, e, d] of [ ["baseline", base(), doc([ANON])], ["names a voice character", base({ narration: { character: "voicey" } }), doc([ANON, { id: "voicey", name: "Voicey", handles: [], synthetic: { voice: "kokoro:am_adam" } }])], ["names a cast-only id", base({ narration: { character: "narrator" } }), doc([ANON])], ["names a stale id", base({ narration: { character: "gone" } }), doc([ANON])], ]) { const [out] = pass(e, d); assert.deepEqual([out.author, out.handle, out.anonymous, out.badge], ["Anon 1", "Anon_1", true, undefined], label); const html = card(out); assert.deepEqual(leaks(html), [], label); assert.match(html, /data-post-avatar="anon"/, label); } }); test("probe 2: an anonymous character named on an item by id, holding no handles, covers its author everywhere", () => { const anonNoH = { ...ANON, handles: [] }; const tl = anonymiseManifest({ timeline: [ base({ narration: { character: "anon-1" } }), base({ id: "p2", author: "Other", handle: "other", text: "@RealGuy lol" }), base({ id: "p3", text: "my second post" }), base({ id: "p4", author: "Other", handle: "other", text: "x", quoted: { id: "q", author: "Real Guy", handle: "RealGuy", badge: "gold", text: "q" }, parents: [{ id: "9", author: "Real Guy", handle: "realguy", text: "x" }] }), ] }, doc([anonNoH])).timeline; assert.deepEqual(tl.map((e) => e.author), ["Anon 1", "Other", "Anon 1", "Other"]); for (const e of tl) assert.deepEqual(leaks(card(e)), [], e.id); assert.equal(tl[1].text, "@Anon_1 lol"); assert.deepEqual([tl[3].quoted.author, tl[3].parents[0].author], ["Anon 1", "Anon 1"]); // Unresolvable: an item naming an anonymous character with no handle of its own -- refused, never drawn in clear. assert.throws(() => anonymiseManifest({ timeline: [base({ handle: "", narration: { character: "anon-1" } })] }, doc([anonNoH])), AnonymityError); // One handle under two shown names: refused. assert.throws(() => anonymousHandles(doc([ANON, { id: "anon-2", name: "B", anonymous: true, shown: "Anon 2", handles: [] }]), { timeline: [base({ narration: { character: "anon-2" } })] }), /shown as "Anon 1" \(anon-1's handles\) and as "Anon 2" \(p1 names anon-2\)/); }); test("probe 3 / re-check 2: a URL of an anonymous author's is collapsed to its host; @mentions keep the pseudonym; one pass", () => { const set = anonymousHandles(doc([ANON, { id: "anon-b", name: "B", anonymous: true, shown: "Anon B", handles: ["real.bsky.social"] }])); const cases = [ ["see https://x.com/RealGuy/status/1850000000000000000 now", "see x.com/… now"], ["https://mobile.twitter.com/realguy and twitter.com/REALGUY?s=20", "twitter.com/… and twitter.com/…"], ["@Real.Bsky.Social. hi https://bsky.app/profile/real.bsky.social/post/3k", "@Anon_B. hi bsky.app/…"], ["https://nitter.net/realguy/status/1 https://xcancel.com/realguy https://fxtwitter.com/realguy/status/1", "nitter.net/… xcancel.com/… fxtwitter.com/…"], ["https://vxtwitter.com/realguy/status/1 https://fixupx.com/realguy https://www.instagram.com/realguy/", "vxtwitter.com/… fixupx.com/… instagram.com/…"], ["https://x.com/%72ealguy/status/1 and https://x.com/@realguy", "x.com/… and x.com/…"], ["https://x.com/search?q=from%3Arealguy https://twitter.com/intent/user?screen_name=realguy", "x.com/… twitter.com/…"], ["x.com/realgu… and twitter.com/realguy/sta…", "x.com/… and twitter.com/…"], ["x.com/realguy.", "x.com/…."], ["@realguy's take (@RealGuy)", "@Anon_1's take (@Anon_1)"], ]; for (const [i, o] of cases) assert.equal(anonymiseWords(i, set), o, i); // Not theirs: another domain, a longer handle, an e-mail, a truncated start under 4 characters, another account. for (const same of ["netflix.com/RealGuy @realguys me@realguy.com", "x.com/rea…", "https://x.com/public/status/1"]) assert.equal(anonymiseWords(same, set), same); // A status URL of a post an anonymous author wrote, on any account's path or host. const [own] = pass(base({ post: "x/1850000000000000000", url: "https://x.com/RealGuy/status/1850000000000000000" }), doc([ANON]), [base({ id: "p2", author: "P", handle: "p", text: "see https://x.com/i/web/status/1850000000000000000 and https://archive.example/s/1850000000000000000/status/1850000000000000000" })]); assert.equal(own.url, "x.com/…"); const tl2 = anonymiseManifest({ timeline: [base({ post: "x/1850000000000000000" }), base({ id: "p2", author: "P", handle: "p", text: "see https://x.com/i/web/status/1850000000000000000" })] }, doc([ANON])).timeline; assert.equal(tl2[1].text, "see x.com/…"); // RT text, a quoted post's url, the drawn cards. const [rt] = pass(base({ author: "P", handle: "p", text: "RT @RealGuy: something", repostedBy: { author: "Real Guy", handle: "RealGuy" } })); assert.deepEqual(leaks(card(rt)), []); const [q] = pass(base({ author: "P", handle: "p", quoted: { id: "q", author: "Real Guy", handle: "RealGuy", text: "q", url: "https://x.com/RealGuy/status/123456" } })); assert.equal(q.quoted.url, "x.com/…"); assert.deepEqual(leaks(card(q)), []); // NO CHAINING. const chain = doc([ANON, { id: "anon-2", name: "B", anonymous: true, shown: "Bob Two", handles: ["bob"] }]); assert.equal(pass(base({ author: "P", handle: "p", text: "@bob and @RealGuy" }), chain)[0].text, "@Bob_Two and @Anon_1"); }); test("re-check 1: the set is the PROJECT's -- an item naming the character in another variant, or dropped from the cut, still covers its author", async () => { const { selectVariant } = await import("./build-video.mjs"); const anonNoH = { ...ANON, handles: [] }; const whole = { timeline: [ base({ id: "p1", handle: "realguy", text: "mine", variant: "full", narration: { character: "anon-1" } }), base({ id: "p2", author: "Pub", handle: "pub", text: "@realguy is wrong", parents: [{ id: "9", author: "Real Guy", handle: "realguy", text: "q" }] }), ] }; const set = anonymousHandles(doc([anonNoH]), whole); for (const v of ["full", "sourced"]) { const a = anonymiseManifest(selectVariant(whole, v), doc([anonNoH]), { set }); const p2 = a.timeline.find((e) => e.id === "p2"); assert.deepEqual([p2.text, p2.parents[0].author], ["@Anon_1 is wrong", "Anon 1"], v); } // projectAnonymousSet: the project's manifest on disk and a take's manifest, unioned. const { projectAnonymousSet } = await import("./project-characters-file.mjs"); const dir = path.join(TMP, "whole"); await mkdir(path.join(dir, "narration"), { recursive: true }); await writeFile(path.join(dir, "narration", "characters.json"), JSON.stringify(doc([anonNoH]))); await writeFile(path.join(dir, "video.manifest.json"), JSON.stringify(whole)); const take = { timeline: [whole.timeline[1]] }; const r = await projectAnonymousSet(dir, take); assert.deepEqual([...r.set.keys()], ["realguy"], "the take's cut drops p1; the project's manifest still names it"); }); test("probe 4: deck popup posts -- the shown name, no screenshot, no QR; a thread shot never drawn with anonymity in the video", () => { const m = anonymiseManifest({ timeline: [base({ author: "P", handle: "p", replyingTo: "public", parentShot: "posts-media/x/shot-thread.png" }), { type: "clip", id: "c1" }], posts: [ { id: "x1", handle: "RealGuy", author: "Real Guy", platform: "x", text: "mine", url: "https://x.com/RealGuy/status/1", shot: "shots/a.png", thread: true, siteUrl: "https://site/x" }, { id: "x2", handle: "public", author: "Public", platform: "x", text: "cc @realguy", url: "https://x.com/public/status/2", shot: "shots/b.png", thread: true }, ], }, doc([ANON])); assert.equal(m.timeline[0].parentShot, undefined, "the shot may picture an anonymous grandparent: never drawn"); const [a, b] = m.posts; assert.deepEqual([a.author, a.handle, a.anonymous, a.noQr, a.shot, a.thread, a.siteUrl], ["Anon 1", "Anon_1", true, true, undefined, undefined, undefined]); assert.equal(a.url, "x.com/…"); assert.deepEqual(postWho(a), { name: "Anon 1", platform: postWho(b).platform }); assert.deepEqual([b.shot, b.thread, b.text], ["shots/b.png", undefined, "cc @Anon_1"], "a public author's own shot stays; its thread shot does not"); assert.deepEqual(m.anonymousHandles, { realguy: "Anon 1" }); // A video with no anonymous character: the same object, nothing dropped. const plain = { timeline: [base({ author: "P", handle: "p", parentShot: "s.png" })], posts: [{ id: "x", handle: "p", shot: "s.png", thread: true }] }; assert.equal(anonymiseManifest(plain, doc([])), plain); }); test("should-fix c: the manifest's own anonymity flags are stripped -- only the pass sets them", () => { const hand = { timeline: [base({ author: "Pub", handle: "pub", subjects: ["someoneelse"], anonymous: true, parents: [{ id: "9", author: "Private Person", handle: "privp", text: "x", anonymous: true }] })], anonymousHandles: { privp: "X" } }; const m = anonymiseManifest(hand, null); assert.notEqual(m, hand); assert.equal(m.timeline[0].anonymous, undefined); assert.equal(m.timeline[0].parents[0].anonymous, undefined); assert.equal(m.anonymousHandles, undefined); const html = card(m.timeline[0]); assert.ok(!html.includes("Private Person"), "the private parent is blurred by the card's rule again"); }); test("the card: the shown name alone, a neutral avatar, the rule only on a page that draws one; Replying to, repost; deck text", () => { const [e] = pass(base()); const html = card(e); assert.match(html, /data-post-anon="">Anon 1<\/span><\/span>/); assert.ok(!/class="handle"/.test(html)); assert.match(html, /class="avatar anon" data-post-avatar="anon"[^>]*>A<\/div>/); assert.match(html, /\.avatar\.anon \{ background: #a2957f !important;/); const host = base({ author: "Host", handle: "host", badge: undefined }); const plain = card(host); assert.ok(!plain.includes(".avatar.anon")); assert.equal(card(anonymiseManifest({ timeline: [host] }, doc([])).timeline[0]), plain, "byte-identical"); const [r] = pass(base({ author: "P", handle: "p", replyingTo: "REALGUY", repostedBy: { handle: "realguy", author: "Real Guy" } })); const h2 = card(r); assert.deepEqual(leaks(h2), []); assert.match(h2, /Anon 1 reposted/); assert.equal(postDeckText(pass(base())[0]).subtitle, "Anon 1 on X"); // Redacted wins. assert.match(card(pass(base({ author: "P", handle: "p", redact: true, parents: [{ id: "9", handle: "realguy", author: "Real Guy", text: "q" }] }))[0]), /data-post-avatar="redacted"/); }); test("a thread read's tiles and a scene's tile draw the shown name too", () => { const [e] = pass(base({ author: "Host", handle: "host", parents: [{ id: "9", handle: "realguy", author: "Real Guy", date: "2026-01-01T00:00:00Z", text: "the question asked" }] })); const html = card(e, { follow: true, thread: { tiles: [{ scope: "parent:0", mode: "full" }, { scope: "post", mode: "full" }] } }); assert.deepEqual(leaks(html), []); assert.match(html, /data-thread-tile="parent:0"[^]*data-post-avatar="anon"/); const [own] = pass(base()); assert.equal(sceneRefOf(own, "post").anonymous, true); assert.equal(sceneRefOf(e, "parent:0").anonymous, true); const tile = sceneTileHtml({ scope: "scene:p1", mode: "scene", entry: own }, { k: 1, pal: { accent: "#c8752a", muted: "#a2957f" }, size: () => 30, avatars: { realguy: "assets/real.jpg" } }); assert.deepEqual(leaks(tile), []); }); test("should-fix b: two anonymous characters are never shown alike; ones with no shown name get distinct defaults", () => { const dup = validateProjectCharacters(doc([{ id: "a", name: "A", anonymous: true, shown: "Anon 1", handles: ["aa"] }, { id: "b", name: "B", anonymous: true, shown: "anon 1", handles: ["bb"] }])); assert.match(dup.join(), /never shown alike/); const set = anonymousHandles(doc([{ id: "anon-4", name: "D", anonymous: true, handles: ["dave"] }, { id: "anon-3", name: "C", anonymous: true, handles: ["carl"] }, { id: "z", name: "Z", anonymous: true, shown: "Anon 1", handles: ["zed"] }])); assert.deepEqual(Object.fromEntries(set), { dave: "Anon 3", carl: "Anon 2", zed: "Anon 1" }, "by id order, past the names in use"); assert.equal(anonymiseWords("@carl vs @dave", set), "@Anon_2 vs @Anon_3"); }); test("probe 6: what a synthesized read says -- the shown name, never the handle; the handles it replaced", () => { assert.deepEqual(speakAnonymised("thanks @RealGuy, see https://x.com/realguy/status/1", { realguy: "Anon 1" }), { text: "thanks Anon 1, see x.com/…", handles: ["realguy"] }); // A bare handle as a whole word too -- when it can be told from words; a short or common one only by its @form. assert.equal(speakAnonymised("@realguy said it. realguy, stop. Realguys no.", { realguy: "Anon 1" }).text, "Anon 1 said it. Anon 1, stop. Realguys no."); assert.equal(speakAnonymised("@news said: the news is out", { news: "Anon N" }).text, "Anon N said: the news is out"); const said = spokenRead("RT @RealGuy: hi", { id: "p" }, { anon: { realguy: "Anon 1" } }); assert.ok(!/realguy/i.test(said.text), said.text); assert.deepEqual(said.anonymised, ["realguy"]); assert.equal(spokenRead("hi @public", { id: "p" }, { anon: { realguy: "Anon 1" } }).anonymised, undefined); }); test("the lint: a picked read that says an anonymous handle or name is an ERROR; a name written in drawn words, shots and QR left out, attachments -- WARNINGS", () => { const take = (o) => ({ file: "narration/posts/1/take-1.wav", seconds: 2, ...o }); const m = { timeline: [ base({ id: "p1", narration: { takes: [take({ engine: "recorded", script: "I am @RealGuy" })], pick: 0 } }), base({ id: "p2", author: "P", handle: "p", text: "Real Guy said so", narration: { takes: [take({ engine: "kokoro", script: "thanks @RealGuy", said: "thanks Anon 1" })], pick: 0 } }), base({ id: "p3", author: "P", handle: "p", parentShot: "s.png", narration: { takes: [take({ engine: "kokoro", script: "as Real Guy put it" })], pick: 0 } }), ], posts: [{ id: "x1", handle: "realguy", author: "Real Guy", text: "x" }], }; const r = anonymityProblems(m, doc([ANON])); assert.deepEqual(r.errors.map((x) => x.replace(/:.*/, "")), ["timeline p1", "timeline p3"]); assert.match(r.errors[0], /says Anon 1's handle/); assert.match(r.errors[1], /says Anon 1's display name/); for (const x of [...r.errors, ...r.warnings]) assert.ok(!/RealGuy|Real Guy|realguy/.test(x), `the lint never repeats the real name: ${x}`); assert.ok(r.warnings.some((w) => /p2: its words write out Anon 1's display name/.test(w))); assert.ok(r.warnings.some((w) => /p3: its captured thread shot is not drawn/.test(w))); assert.ok(r.warnings.some((w) => /posts\[0\] x1: by an anonymous author -- drawn as "Anon 1", without its screenshot and its QR code/.test(w))); assert.ok(r.warnings.some((w) => /attachments .* yours to check/.test(w))); assert.deepEqual(anonymityProblems(m, doc([])), { errors: [], warnings: [] }); }); test("should-fix i: a chat replay's author and words; the promise panel reads the same set (anonymousHandles on the manifest)", () => { const set = new Map([["realguy", "Anon 1"]]); const clips = [{ segment: "c1", messages: [{ at: 1, author: "RealGuy", text: "hi" }, { at: 2, author: "fan", text: "@realguy lol" }, { at: 3, author: "fan", text: "plain" }] }]; const out = anonymiseChatClips(clips, set); assert.deepEqual(out[0].messages.map((m) => [m.author, m.text]), [["Anon 1", "hi"], ["fan", "@Anon_1 lol"], ["fan", "plain"]]); assert.equal(anonymiseChatClips(clips, new Map()), clips); assert.equal(pseudonymOf("Anon 1"), "Anon_1"); }); test("the build reads this video's characters beside the manifest: none is none; a broken or refused file stops it", async () => { const dir = path.join(TMP, "p"); await mkdir(path.join(dir, "narration"), { recursive: true }); assert.equal(await readProjectCharactersFor(dir), null); await writeFile(path.join(dir, "narration", "characters.json"), JSON.stringify(doc([ANON]))); assert.deepEqual(await readProjectCharactersFor(dir), doc([ANON])); await writeFile(path.join(dir, "narration", "characters.json"), "{ not json"); await assert.rejects(readProjectCharactersFor(dir), /not JSON.*drawn under its own name/); await writeFile(path.join(dir, "narration", "characters.json"), JSON.stringify({ ...doc([]), characters: [{ id: "BAD", name: "x" }] })); await assert.rejects(readProjectCharactersFor(dir), /is refused/); }); test("anonymiseEntry on its own takes the set", () => { assert.equal(anonymiseEntry(base(), new Map([["realguy", "Anon 1"]])).author, "Anon 1"); }); test("re-check: a short or common handle said bare is a WARNING; an anonymous author's unclassified link is a WARNING", () => { const d = doc([{ id: "n", name: "N", anonymous: true, shown: "Anon N", handles: ["news"] }, ANON]); const take = (script) => ({ file: "narration/posts/1/take-1.wav", seconds: 2, engine: "recorded", script }); const r = anonymityProblems({ timeline: [ base({ id: "p1", author: "P", handle: "p", narration: { takes: [take("the news is out")], pick: 0 } }), base({ id: "p2", text: "my blog https://myblog.example/about and https://x.com/public" }), ] }, d); assert.deepEqual(r.errors, []); assert.ok(r.warnings.some((w) => /p1: its picked read says a word that is Anon N's handle/.test(w))); assert.ok(r.warnings.some((w) => /p2: Anon 1's words carry a link the pass cannot classify \(myblog.example\/…\)/.test(w))); }); test("should-fix a/b: a shown name that is a real author's handle or name, or another character's handle, is refused -- naming characters, never the real handle", () => { const pat = doc([{ id: "a2", name: "Bob", anonymous: true, shown: "Pat", handles: ["bob"] }]); const m = { timeline: [base({ id: "p", author: "Pat", handle: "pat", text: "@bob said" })] }; assert.match(validateProjectCharacters(pat, "c", { manifests: [m] }).join(), /a2's shown name "Pat" is also an author's handle in p/); assert.throws(() => anonymiseManifest(m, pat), /a2's shown name "Pat"/); const two = doc([ANON, { id: "a2", name: "B", anonymous: true, shown: "realguy", handles: ["bob"] }]); assert.match(validateProjectCharacters(two).join(), /a2's shown name "realguy" is anon-1's handle/); // The unresolvable message names characters and items, never the handle. const r = anonymityProblems({ timeline: [base({ id: "p3", handle: "realguy", narration: { character: "a3" } })] }, doc([ANON, { id: "a3", name: "O", anonymous: true, shown: "Anon 3", handles: [] }])); assert.equal(r.errors.length, 1); assert.ok(!/realguy/i.test(r.errors[0]), r.errors[0]); }); test("re-check 2: no deck QR for an anonymous author's item, with citeUrl or an archive channel", async () => { const { deckQrUrl } = await import("./deck.mjs"); const prov = { siteOrigin: "https://archive.example" }; const e = base({ post: "realguy-x/1850000000000000000", url: "https://x.com/realguy/status/1850000000000000000" }); for (const x of [e, { ...e, citeUrl: e.url }, { ...e, siteChannel: "realguy-x" }]) { const [a] = anonymiseManifest({ provenance: prov, timeline: [x] }, doc([ANON])).timeline; assert.equal(deckQrUrl(a, prov, { links: "archive" }), null); assert.equal(deckQrUrl(a, prov, { links: "original" }), null); assert.equal(a.citeUrl, undefined); } assert.ok(deckQrUrl(base({ author: "P", handle: "p", url: "https://x.com/p/status/1" }), prov), "a public author's keeps its code"); }); test("re-check 3: the podcast's show notes cite an anonymous author's post by the shown name, with no link", async () => { const { citedSources, showNotes } = await import("./build-podcast.mjs"); const m = anonymiseManifest({ title: "T", provenance: {}, timeline: [], posts: [{ id: "pp", platform: "x", author: "Real Guy", handle: "realguy", date: "2026-01-01", text: "x", url: "https://x.com/realguy/status/1850000000000000000" }, { id: "pq", platform: "x", author: "Pub", handle: "pub", date: "2026-01-01", text: "y", url: "https://x.com/pub/status/2" }] }, doc([ANON])); const notes = showNotes({ manifest: m, chapters: [], sources: citedSources(m, [], []) }); assert.ok(!/realguy|Real Guy|1850000000000000000/i.test(notes), notes); assert.match(notes, /^- Anon 1 \(x, 2026-01-01\)$/m); assert.match(notes, /^- Pub \(x, 2026-01-01\) — $/m); });