// post-screens.mjs: a post's SCREENS -- its text screenshots, transcribed, drawn on its card and read in the voices of // the people in them. The data and its validation, the shapes masked (never drawn, never said), what is read and drawn // of a screen, the read's roles and pieces, the card's tiles and its schedule, the plan's media, the anonymity pass, // the lint's warnings. import assert from "node:assert/strict"; import { test } from "node:test"; import { postCardHtml, postItemPlan, validatePostItem } from "./post-item.mjs"; import { chronoPieces, partRoles, threadLayout, threadReadErrors, validateThreadReadWarnings } from "./post-thread.mjs"; import { followBlocks, followStateAt, followStateKey, isDrawnState } from "./read-follow.mjs"; import { anonymiseEntry, anonymityProblems } from "./post-anon.mjs"; import { redactEntry } from "./post-redact.mjs"; import { excerptRun, maskShapes, parseScreenRole, screenHeader, screenLineSaid, screenReadLines, screenRows, screenWarnings, screenWho, shapeSpans, tileOfRole, validateScreens, } from "./post-screens.mjs"; const VID = { src: "posts-media/savvy-x/2117/2117_1.mp4", kind: "video", width: 1920, height: 1080, seconds: 61.742, audio: true, start: 3.3, end: 8 }; const IMG = { src: "posts-media/savvy-x/2117/2117_2.jpg", kind: "image", width: 900, height: 1400 }; const S1 = { id: "s1", media: 0, at: [3.5, 8], kind: "dm", read: "all", lines: [ { who: "TheQuartering", text: "what's your email so I can refund your order", time: "11:32 AM" }, { who: "MadamSavvy", text: "where is my scale video", time: "11:53 AM" }, { who: "TheQuartering", text: "you HONESTLY believe that I have lied about weight loss?" }, { who: "TheQuartering", text: "You can't be this dumb", time: "11:58 AM" }, ], }; const S2 = { id: "s2", media: 1, kind: "email", title: "Refund email", read: [{ line: 0 }, { line: 1, text: "Total amount refunded: $99.95" }], lines: [ { who: "coffee brand coffee", text: "Some items in your order have been refunded" }, { text: "Total amount refunded: $99.95 USD. It may take up to 10 days for this refund to appear in your account." }, { text: "Order number 4419283 · reply to support@coffeebrand.com" }, ], }; const POST = "Hey, @thequartering You told me your video was ready to go.\n\nYour turn."; const base = (o = {}) => ({ type: "post", id: "p-2117", post: "savvy-x/2117", platform: "x", author: "Savvy", handle: "MadamSavvy", date: "2026-11-02T01:40:36.000Z", text: POST, subjects: ["MadamSavvy"], media: [VID, IMG], screens: [S1, S2], ...o }); /** A thread read's take: his words, then each screen line (stored roles), timed by words. */ function narrated(e, lines = [["s1", 0], ["s1", 1], ["s1", 2], ["s1", 3], ["s2", 0], ["s2", 1]]) { const parts = []; let t = 0; const add = (text, role, piece, gap) => { t += parts.length ? gap : 0; const sec = Math.round((text.split(/\s+/).length / 3) * 100) / 100; parts.push({ n: parts.length + 1, para: piece * 100, text, role, start: Math.round(t * 1000) / 1000, seconds: sec }); t += sec; }; add("Hey, at thequartering You told me your video was ready to go.", "post", 0, 0); add("Your turn.", "post", 0, 0.6); lines.forEach(([id, n], i) => { const sc = e.screens.find((s) => s.id === id); add(screenLineSaid(sc, n), `screen:${id}:${n}`, 1 + i, i && lines[i - 1][0] === id ? 0.35 : 0.8); }); return { ...e, narration: { script: POST, pick: 0, takes: [{ file: "narration/posts/2117/take-1.orpheus-thread.wav", engine: "orpheus", voice: "tara+leo", seconds: Math.round(t * 1000) / 1000, script: "x", parts }] } }; } test("the data: a well-formed item with screens is taken; each wrong field is named", () => { assert.deepEqual(validatePostItem(narrated(base())), []); const bad = (sc, re) => assert.ok(validateScreens(base({ screens: [sc] })).some((x) => re.test(x)), `${JSON.stringify(sc).slice(0, 80)} -> ${re}`); bad({ ...S1, id: "S 1" }, /\.id must be a short name/); bad({ ...S1, media: 7 }, /\.media must be the index/); bad({ ...S1, at: undefined }, /\.at must be \[from, to\]: the seconds of the video/); bad({ ...S1, at: [3, 70] }, /inside 0–61\.742/); bad({ ...S2, at: [1, 2] }, /only a video or a GIF has a span/); bad({ ...S1, kind: "chat" }, /\.kind must be one of dm, email, post, text/); bad({ ...S1, read: [{ line: 0, text: "refund your email" }] }, /is not a run of line 0's words/); bad({ ...S1, read: [{ line: 0 }, { line: 0 }] }, /line 0 is read twice/); bad({ ...S1, lines: [{ text: "x", redact: true }, ...S1.lines], read: [{ line: 0 }] }, /line 0 is redacted -- it is never read/); bad({ ...S1, ref: "a/b" }, /\.ref must be \/ \(a screenshot of a post only\)/); bad({ ...S1, checked: { by: "me" } }, /\.checked must be \{ by, at \}/); bad({ ...S1, lines: [{ who: "x", text: "y", colour: "red" }] }, /\.colour is not a line's field/); const two = validateScreens(base({ screens: [S1, { ...S2, id: "s1" }] })); assert.ok(two.some((x) => /is the id of another screen/.test(x))); // An excerpt names a run of the line's own words; its edge punctuation need not match. assert.deepEqual(excerptRun("Total amount refunded: $99.95 USD. It may", "total amount refunded: $99.95."), { start: 0, end: 29 }); assert.equal(excerptRun("hop on the scale fatso", "on scale"), null); }); test("THE SHAPES: an e-mail, a phone number, an order or long number are masked -- absent from the card's page AND from what the part says", () => { const kinds = (t) => shapeSpans(t).map((s) => s.kind); assert.deepEqual(kinds("reply to support@coffeebrand.com"), ["email"]); assert.deepEqual(kinds("call +1 (555) 201-4477 today"), ["number"]); assert.deepEqual(kinds("Order number 4419283 shipped"), ["order"]); assert.deepEqual(kinds("your code #88213 is ready"), ["order"]); assert.deepEqual(kinds("id 2117069332987425196"), ["number"]); // Amounts, times and small numbers stay. assert.deepEqual(kinds("Total $104.90 USD, $1,000,000 at 12:18 PM, 400 pounds, Oct 31, 290"), []); assert.equal(maskShapes("a@b.co").text.includes("a@b.co"), false); // A line that READS a shape: the card never draws it, the read never says it. const sc = { ...S2, read: "all" }; const e = narrated(base({ screens: [S1, sc] }), [["s1", 0], ["s2", 0], ["s2", 1], ["s2", 2]]); assert.deepEqual(validatePostItem(e), []); const said = screenLineSaid(sc, 2); assert.ok(!said.includes("4419283") && !said.includes("support@") && !said.includes("coffeebrand"), said); assert.ok(/reply to/.test(said)); const plan = postItemPlan(e, {}); const html = postCardHtml(e, { width: 1600, height: 900, follow: true, thread: plan.follow.thread }); assert.ok(html.includes('data-screen-line="2"'), "the line is drawn"); for (const v of ["4419283", "support@coffeebrand.com", "coffeebrand"]) assert.ok(!html.includes(v), `${v} is not in the page`); assert.ok(html.includes('data-post-blanked=""'), "a bar stands where it was"); // The read's piece says it as the part does: the shape left out. const piece = chronoPieces(e).find((p) => p.role === "screen:s2:2"); assert.ok(piece && !/4419283|support@/.test(piece.text)); }); test("what is read and drawn: all, a list with an excerpt, none; lines not read left out (a gap row) or drawn dim; a redacted line never", () => { assert.deepEqual(screenReadLines(S1).map((r) => r.n), [0, 1, 2, 3]); const r2 = screenReadLines(S2); assert.deepEqual(r2.map((r) => [r.n, r.cutBefore, r.cutAfter]), [[0, false, false], [1, false, true]]); assert.equal(screenLineSaid(S2, 1), "Total amount refunded: $99.95"); const rows = screenRows(S2); assert.deepEqual(rows.map((r) => (r.gap ? `gap${r.lines}` : `${r.n}:${r.read}`)), ["0:true", "1:true", "gap1"]); assert.equal(rows[1].text, "Total amount refunded: $99.95"); const dim = screenRows({ ...S2, unread: "dim" }); assert.deepEqual(dim.map((r) => (r.gap ? "gap" : `${r.n}:${r.read}`)), ["0:true", "1:true", "2:false"]); assert.ok(!dim[2].text.includes("4419283"), "a dim line is masked too"); assert.deepEqual(screenReadLines({ ...S1, read: "none" }), []); const red = { ...S1, lines: [S1.lines[0], { text: "my address is 12 High St", redact: true }, S1.lines[1]], read: "all" }; assert.deepEqual(screenReadLines(red).map((r) => r.n), [0, 2]); assert.ok(!JSON.stringify(screenRows({ ...red, unread: "dim" })).includes("High St"), "a redacted line is never drawn"); }); test("who: the post's author by handle or name; another by handle, or a character's name resolved; the side follows", () => { const e = base(); assert.deepEqual(screenWho("MadamSavvy", e), { self: true, handle: "MadamSavvy", name: "MadamSavvy" }); assert.equal(screenWho("savvy", e).self, true); assert.deepEqual(screenWho("TheQuartering", e), { self: false, handle: "TheQuartering", name: "TheQuartering" }); assert.deepEqual(screenWho("Jeremy Hambly", e, { "jeremy hambly": "TheQuartering" }), { self: false, handle: "TheQuartering", name: "Jeremy Hambly" }); assert.equal(screenWho("coffee brand coffee", e).handle, null); }); test("the read: roles screen:: stored on the take; the card threads -- his post, then each screen the take reads, a tile", () => { const e = narrated(base()); const roles = partRoles(e).map((r) => r.role); assert.deepEqual(roles, ["post", "post", "screen:s1:0", "screen:s1:1", "screen:s1:2", "screen:s1:3", "screen:s2:0", "screen:s2:1"]); assert.deepEqual(parseScreenRole("screen:s1:3"), { id: "s1", n: 3 }); assert.equal(tileOfRole("screen:s1:3"), "screen:s1"); const L = threadLayout(e, {}); assert.equal(L.on, true); assert.deepEqual(L.tiles.map((t) => t.scope), ["post", "screen:s1", "screen:s2"]); // A screen the take does not read is not drawn (a warning says so). const one = narrated(base(), [["s1", 0], ["s1", 1]]); assert.deepEqual(threadLayout(one, {}).tiles.map((t) => t.scope), ["post", "screen:s1"]); // The blocks: one a line, named by its role. const b = followBlocks(e).blocks.filter((x) => String(x.scope).startsWith("screen:")); assert.deepEqual(b.map((x) => [x.scope, x.line, x.part]), [["screen:s1", 0, 2], ["screen:s1", 1, 3], ["screen:s1", 2, 4], ["screen:s1", 3, 5], ["screen:s2", 0, 6], ["screen:s2", 1, 7]]); // The pieces a thread read reads: after his words, in the screens' order. assert.deepEqual(chronoPieces(e).map((p) => p.role), ["post", "screen:s1:0", "screen:s1:1", "screen:s1:2", "screen:s1:3", "screen:s2:0", "screen:s2:1"]); assert.equal(chronoPieces(e)[1].who, "TheQuartering"); }); test("the schedule: his post gives way to a strip as the first screen comes in; a screen read collapses as the next comes in; the last stays", () => { const e = narrated(base()); const plan = postItemPlan(e, {}); const rows = Object.fromEntries(plan.follow.thread.tiles.map((t) => [t.scope, t])); const s1 = plan.follow.blocks.find((b) => b.scope === "screen:s1" && b.line === 0).start; const s2 = plan.follow.blocks.find((b) => b.scope === "screen:s2" && b.line === 0).start; assert.deepEqual(rows.post.to, [0.62, 0.5]); assert.ok(Math.abs(rows.post.t0 - (s1 - 0.4)) < 0.01); assert.deepEqual([rows["screen:s1"].from, rows["screen:s1"].to, rows["screen:s1"].then.to], [[1, 0], [1, 1], [0, 0]]); assert.ok(Math.abs(rows["screen:s1"].then.t0 - (s2 - 0.4)) < 0.01); assert.equal(rows["screen:s2"].then, undefined); // The state says which screen is up (its badge); none before; part of the key; never "the page as drawn". const path = { start: 0, segments: [], H: 600, C: 900, tiles: {} }; assert.equal(followStateAt(plan.follow, path, 1).screen, null); const mid = followStateAt(plan.follow, path, s1 + 1); assert.equal(mid.screen, "s1"); assert.equal(followStateAt(plan.follow, path, s2 + 1).screen, "s2"); assert.ok(followStateKey(mid).includes('"screen":"s1"')); assert.equal(isDrawnState({ op: {}, offset: 0, fadeTop: 0, fadeBottom: 0, screen: "s1" }), false); }); test("the media: its own attachments until the first screen is up, then each screen's span -- a video's last frame HELD, an image held", () => { const e = narrated(base()); const plan = postItemPlan(e, {}); const st = plan.steps.map((s) => ({ kind: s.kind, at: s.at, sec: s.seconds, from: s.srcFrom, to: s.srcTo, screen: s.screen ?? null, hold: s.hold ?? false })); const s1At = st.find((s) => s.screen === "s1"); const s2At = st.find((s) => s.screen === "s2"); assert.ok(s1At && s2At); assert.deepEqual([s1At.kind, s1At.from, s1At.to, s1At.hold], ["video", 3.5, 8, true]); assert.ok(s1At.sec > 8 - 3.5, "the read outlasts the span: held"); assert.equal(s2At.kind, "image"); assert.ok(Math.abs(s2At.at + s2At.sec - plan.total) < 0.01, "the last screen to the item's end"); const own = st.filter((s) => !s.screen); assert.ok(own.length >= 1 && Math.abs(own[own.length - 1].at + own[own.length - 1].sec - s1At.at) < 0.01, "its own run up to the first screen"); assert.equal(plan.held, 0); // A picture-only post: its picture is in its card, no slot -- no screen step. const pic = narrated(base({ text: "@thequartering", media: [IMG], screens: [{ ...S2, media: 0 }] }), [["s2", 0]]); assert.ok(postItemPlan(pic, {}).steps.every((s) => !s.screen)); }); test("the card: the screen typeset -- its header names the author's shown name and says transcribed; a badge pins it; speakers by the privacy rule", () => { const e = narrated(base()); const plan = postItemPlan(e, {}); const html = postCardHtml(e, { width: 1600, height: 900, follow: true, thread: plan.follow.thread }); assert.ok(html.includes("DMs shown in Savvy's video") || html.includes("DMs shown in Savvy's video")); assert.ok(html.includes('data-screen-transcribed=""')); assert.ok(html.includes('data-screen-badge="s1"') && html.includes("DMs · transcribed")); assert.ok(html.includes("Refund email shown in Savvy") && html.includes("picture")); assert.ok(html.includes('data-screen-excerpt=""'), "an excerpted screen says so"); // Each read line is a block of the read-follow. for (const n of [0, 1, 2, 3]) assert.ok(new RegExp(`data-screen-line="${n}">
|data-screen-line="${n}">
')); assert.ok(!html.includes(">TheQuartering<")); const shown = postCardHtml({ ...e, redact: false }, { width: 1600, height: 900, follow: true, thread: plan.follow.thread }); assert.ok(shown.includes(">TheQuartering<")); // The header: a blurred author is "the post's". assert.equal(screenHeader(e, S1, { authorHidden: true }).text, "DMs shown in the post's video"); assert.equal(screenHeader(e, S1).badge, "DMs · transcribed"); }); test("anonymity: a speaker who is an anonymous handle is drawn under the shown name; the header the author's shown name; a display name refused", () => { const set = new Map([["thequartering", "Anon 1"], ["madamsavvy", "Anon 2"]]); const e = anonymiseEntry(narrated(base({ screens: [{ ...S1, lines: [...S1.lines, { who: "TheQuartering", text: "ask @TheQuartering yourself" }] }] }), [["s1", 0]]), set); const lines = e.screens[0].lines; assert.equal(lines[0].who, "Anon 1"); assert.equal(lines[0].anonymous, true); assert.equal(lines[4].text, "ask @Anon_1 yourself"); assert.equal(e.author, "Anon 2"); assert.equal(screenHeader(e, e.screens[0]).text, "DMs shown in Anon 2's video"); const plan = postItemPlan(e, {}); const html = postCardHtml(e, { width: 1600, height: 900, follow: true, thread: plan.follow.thread }); assert.ok(!/TheQuartering|MadamSavvy|Savvy's|Savvy's/.test(html.replace(/Anon 2/g, "")), "no real name or handle in the page"); assert.ok(html.includes(">Anon 1<")); // A stray flag is the pass's alone: stripped on the way in. const stray = anonymiseEntry(base({ screens: [{ ...S1, lines: [{ ...S1.lines[0], anonymous: true }] }] }), new Map([["someone", "Anon 9"]])); assert.equal(stray.screens[0].lines[0].anonymous, undefined); // The lint: a speaker written as an anonymous author's display name is an ERROR. const doc = { characters: [{ id: "tq", name: "Jeremy", handles: ["TheQuartering"], anonymous: true, shown: "Anon 1" }] }; const m = { timeline: [base({ screens: [{ ...S1, lines: [{ who: "Jeremy", text: "hi" }] }] }), { type: "post", id: "p-x", post: "tq-x/1", platform: "x", author: "Jeremy", handle: "TheQuartering", date: "2026-11-01T00:00:00Z", text: "x" }] }; assert.ok(anonymityProblems(m, doc).errors.some((x) => /screen s1 line 0's speaker writes Anon 1's display name/.test(x))); }); test("the guard: a part reading a line the screen does not read, a redacted line, or a screen it does not carry is refused, with the fix", () => { const e = narrated(base()); assert.deepEqual(threadReadErrors(e, {}), []); const unread = { ...e, screens: [{ ...S1, read: [{ line: 0 }, { line: 1 }, { line: 3 }] }, S2] }; assert.ok(threadReadErrors(unread, {}).some((x) => /part 5 reads line 2 of screen s1, which the screen does not read/.test(x) && /posts screen

p-2117 read/.test(x))); const gone = { ...e, screens: [S1] }; assert.ok(threadReadErrors(gone, {}).some((x) => /reads screen s2, which the item does not carry/.test(x))); const redacted = { ...e, screens: [{ ...S1, lines: S1.lines.map((l, n) => (n === 3 ? { ...l, redact: true } : l)) }, S2] }; assert.ok(threadReadErrors(redacted, {}).some((x) => /line 3 of screen s1, which is redacted/.test(x))); }); test("the redaction's terms mark a screen's words and speakers: the card draws bars, the read leaves them out", () => { const red = { terms: [{ term: "dumb" }, { term: "TheQuartering" }] }; const out = redactEntry(base(), red); assert.ok(!out.screens[0].lines[3].text.includes("dumb")); assert.ok(!out.screens[0].lines[0].who.includes("TheQuartering")); const e = narrated(base()); const plan = postItemPlan(e, {}); const html = postCardHtml(e, { width: 1600, height: 900, follow: true, thread: plan.follow.thread, redaction: red }); assert.ok(!html.includes("this dumb")); }); test("the lint's warnings: not checked, a masked shape, a read not made, a speaker blurred; a checked screen read whole is quiet", () => { const e = narrated(base(), [["s1", 0], ["s1", 1]]); const w = screenWarnings(e, { narrated: true, takeRoles: partRoles(e).map((r) => r.role) }); assert.ok(w.some((x) => /screen s1's transcription not checked/.test(x))); assert.ok(w.some((x) => /screen s2 line 2 holds an e-mail address and an order or reference number/.test(x) || /screen s2 line 2 holds/.test(x))); assert.ok(w.some((x) => /does not read lines 2, 3 of screen s1/.test(x))); assert.ok(w.some((x) => /does not read screen s2/.test(x))); assert.ok(w.some((x) => /screen s1 line 0's speaker is drawn blurred/.test(x))); const quiet = narrated(base({ redact: false, screens: [{ ...S1, checked: { by: "op", at: "2026-11-12T10:00:00Z" } }] }), [["s1", 0], ["s1", 1], ["s1", 2], ["s1", 3]]); assert.deepEqual(screenWarnings(quiet, { narrated: true, takeRoles: partRoles(quiet).map((r) => r.role) }), []); assert.ok(screenWarnings(base(), { narrated: false }).some((x) => /drawn and read only on a narrated item/.test(x))); // Through the cut's lint warnings. assert.ok(validateThreadReadWarnings({ timeline: [e] }).some((x) => /transcription not checked/.test(x))); });