// post-item.mjs: a post as a timeline item -- the validator, the duration // rule, the plan of what plays when, the privacy rule and the layout. import assert from "node:assert/strict"; import { test } from "node:test"; import { POST_GIF, POST_IMAGE_SECONDS, POST_PARAPHRASE_MAX, POST_PICTURE_MIN, POST_READ, authorRedaction, contextReadSeconds, contextText, cardAuthors, cardText, mediaSeconds, mediaTimeAt, postCardHtml, postDeckText, postItemPlan, postItemSeconds, postLayout, readingSeconds, ruleSeconds, stepAt, stepBoxes, validatePostItem, wordCount, } from "./post-item.mjs"; import { deckText, estimatedDuration, deckQrUrl, resolveDeck, validateDeckMarkers } from "./deck.mjs"; const base = (o = {}) => ({ type: "post", id: "p1", post: "someone-x/2116017249706225668", platform: "x", author: "MR. OBVIOUS", handle: "ObviousRises", date: "2026-10-30T04:00:00.000Z", text: "one two three four five six", url: "https://x.com/ObviousRises/status/2116017249706225668", ...o, }); const video = (o = {}) => ({ src: "posts-media/someone-x/1/1_1.mp4", kind: "video", seconds: 6.2, width: 1920, height: 1080, audio: true, ...o }); const image = (o = {}) => ({ src: "posts-media/someone-x/1/1_1.jpg", kind: "image", width: 1200, height: 900, ...o }); test("a valid post item passes; each field is refused in words", () => { assert.deepEqual(validatePostItem(base({ media: [video(), image()], engagement: { likes: 3 } })), []); const bad = validatePostItem(base({ post: "nope", platform: "myspace", date: "soon", hold: 0.2, media: [video({ src: "/abs.mp4" }), image({ start: 1 }), { src: "a.png", kind: "sound" }] })); assert.ok(bad.some((e) => e.includes(".post must be /"))); assert.ok(bad.some((e) => e.includes(".platform must be one of"))); assert.ok(bad.some((e) => e.includes(".date must be"))); assert.ok(bad.some((e) => e.includes(".hold must be 1–120"))); assert.ok(bad.some((e) => e.includes("media[0].src must be relative"))); assert.ok(bad.some((e) => e.includes("media[1].start: only a video or a GIF is trimmed"))); assert.ok(bad.some((e) => e.includes("media[2].kind must be one of"))); assert.ok(validatePostItem(base({ media: [video({ start: 3, end: 3.2 })] })).some((e) => e.includes("at least 0.5 s after start"))); assert.ok(validatePostItem(base({ media: [video({ end: 9 })] })).some((e) => e.includes("a second inside it"))); assert.ok(validatePostItem(base({ media: [video({ crop: [0.5, 0, 0.6, 1] })] })).some((e) => e.includes(".crop must be"))); assert.ok(validatePostItem(base({ parents: Array.from({ length: 9 }, () => ({})) })).some((e) => e.includes("at most 8"))); assert.deepEqual(validatePostItem(base({ parents: [{}, {}, {}] })), []); assert.ok(validatePostItem(base({ media: [video(), video(), video(), video(), video()] })).some((e) => e.includes("at most 4 attachments"))); }); test("the pacing rule: the item is its read; a long video is cut to it, pictures share it; hold overrides", () => { assert.equal(wordCount(" a b\nc "), 3); // Six words: 1 + 6/3 = 3 s (the floor). assert.equal(readingSeconds(base()), POST_READ.min); const long = base({ text: Array.from({ length: 30 }, () => "w").join(" ") }); assert.equal(readingSeconds(long), POST_READ.lead + 30 / POST_READ.wps); assert.equal(readingSeconds(base({ text: Array.from({ length: 900 }, () => "w").join(" ") })), POST_READ.max); // A repost's "RT @x: " is its reposted line, not words to read. assert.equal(cardText(base({ text: "RT @ObviousRises: hello there", repostedBy: { handle: "ObviousRises" } })), "hello there"); // A parent and a quote add their own reading, clamped 1–4. assert.equal(readingSeconds(base({ parents: [{ text: "a" }], quoted: { text: Array.from({ length: 60 }, () => "q").join(" ") } })), 3 + 1 + 4); // Each attachment's own length (what a sequence of them would take). assert.equal(mediaSeconds(video({ start: 1, end: 4 })), 3); assert.equal(mediaSeconds(image()), POST_IMAGE_SECONDS); assert.equal(mediaSeconds({ kind: "gif", seconds: 1.2 }), 3.6); // looped to ≥ 3 s: three passes assert.equal(mediaSeconds({ kind: "gif", seconds: 25 }), POST_GIF.max); // A 6.2 s video under a 3 s read is cut to the read; an image beside it gets the minimum. assert.equal(ruleSeconds(base({ media: [video()] })), 3); assert.equal(ruleSeconds(base({ media: [video(), image()] })), 3 + POST_PICTURE_MIN); // An out point set is kept as the choice: its span plays. assert.equal(ruleSeconds(base({ media: [video({ start: 1, end: 5 })] })), 4); assert.equal(postItemSeconds(base({ media: [video()], hold: 5 })), 5); // Pictures share the read, each at least the minimum. assert.equal(ruleSeconds(base({ media: [image(), image(), image(), image()] })), 4 * POST_PICTURE_MIN); assert.equal(ruleSeconds(base({ text: long.text, media: [image(), image()] })), 11); // A grid of images is one picture: the read. assert.equal(ruleSeconds(base({ mediaLayout: "grid", media: [image(), image(), image()] })), 3); // The deck's estimate is the rule. assert.equal(estimatedDuration(base({ media: [video()] })), 3); }); test("the plan: the attachments from the first frame, like a video, while the words stay; a hold cuts or holds", () => { const p = postItemPlan(base({ text: Array.from({ length: 30 }, () => "w").join(" "), media: [video({ start: 1, end: 3 }), image()] })); assert.equal(p.reading, 0); assert.equal(p.total, 11); assert.deepEqual(p.steps.map((s) => [s.kind, s.at, s.seconds]), [["video", 0, 2], ["image", 2, 9]]); assert.equal(p.steps[0].srcFrom, 1); assert.equal(p.steps[0].srcTo, 3); assert.equal(p.steps[0].sound, true); assert.equal(stepAt(p, 1).kind, "video"); assert.equal(stepAt(p, 4).kind, "image"); assert.equal(mediaTimeAt(p.steps[0], 1), 2); const cut = postItemPlan(base({ media: [video(), image()], hold: 2 })); assert.deepEqual(cut.steps.map((s) => [s.kind, s.seconds]), [["video", 2]]); assert.equal(cut.steps[0].srcTo, 2); const held = postItemPlan(base({ media: [image()], hold: 12 })); assert.equal(held.steps[0].seconds, 3); assert.equal(held.held, 9); assert.equal(postItemPlan(base({ media: [video({ mute: true })] })).steps[0].sound, false); const gif = postItemPlan(base({ media: [{ src: "g.mp4", kind: "gif", seconds: 1.2 }] })); assert.equal(gif.steps[0].loop, true); assert.equal(mediaTimeAt(gif.steps[0], 1.5), 0.3); // A short video under a long read: held (or looped). const short = postItemPlan(base({ text: Array.from({ length: 30 }, () => "w").join(" "), media: [video({ seconds: 4 })] })); assert.equal(short.total, 11); assert.equal(short.held, 7); }); test("the privacy rule: another author is blurred unless the subject, gold/grey verified, or redact:false", () => { assert.deepEqual(authorRedaction({ handle: "someone" }, { subjects: ["ObviousRises"] }), { redact: true, reason: "private" }); assert.deepEqual(authorRedaction({ handle: "obviousrises" }, { subjects: ["ObviousRises"] }), { redact: false, reason: "subject" }); assert.deepEqual(authorRedaction({ handle: "NASA", badge: "gold" }), { redact: false, reason: "verified" }); assert.deepEqual(authorRedaction({ handle: "WhiteHouse", badge: "grey" }), { redact: false, reason: "verified" }); assert.deepEqual(authorRedaction({ handle: "bought", badge: "blue" }), { redact: true, reason: "blue-check" }); assert.deepEqual(authorRedaction({ handle: "x" }, { redact: false }), { redact: false, reason: "off" }); assert.deepEqual(authorRedaction({ handle: "NASA", badge: "gold" }, { redact: true }), { redact: true, reason: "forced" }); assert.deepEqual(authorRedaction({}), { redact: true, reason: "unknown" }); const e = base({ text: "@KacamnGamesYT you are wrong", parents: [{ handle: "KacamnGamesYT", author: "Kacamn", text: "a question" }] }); assert.deepEqual(cardAuthors(e).map((a) => [a.where, a.redact]), [["parents[0]", true]]); const html = postCardHtml(e, { width: 1440, height: 810 }); // The words as read (each word is its own span on the card: word-zoom.mjs). const text = html.replace(/<[^>]+>/g, ""); // The parent's name and handle are never in the page, and the mention of them is blurred. assert.ok(!html.includes("KacamnGamesYT")); assert.ok(!html.includes("Kacamn")); assert.ok(text.includes("a question")); assert.ok(html.includes('data-redacted=""')); // A self-thread (his own parent) is drawn plainly. const self = postCardHtml(base({ parents: [{ handle: "ObviousRises", author: "MR. OBVIOUS", text: "first" }] }), { width: 1440, height: 810 }); assert.ok(!self.includes('data-redacted=""')); // "Replying to" with no parent archived. assert.ok(postCardHtml(base({ replyingTo: "private_person" }), { width: 1440, height: 810 }).includes("Replying to ")); }); test("ONE LAYOUT: the text alone, or the words beside the attachments the whole time -- never a stack", () => { const box = { width: 1440, height: 810 }; assert.equal(postLayout(base(), box).mode, "text"); const side = postLayout(base({ media: [video()] }), box); assert.equal(side.mode, "side"); assert.ok(side.slot.x > side.card.x + side.card.width); const words = Array.from({ length: 480 }, () => "word").join(" "); const long = postLayout(base({ text: words, media: [video()] }), box); assert.equal(long.mode, "side"); // A media box keeps its aspect inside the slot; a grid of two lays them side by side. const [b] = stepBoxes({ items: [{ i: 0, width: 1920, height: 1080 }] }, side.slot); assert.ok(Math.abs(b.width / b.height - 16 / 9) < 0.02); const two = stepBoxes({ items: [{ i: 0, width: 100, height: 100 }, { i: 1, width: 100, height: 100 }] }, { x: 0, y: 0, width: 412, height: 200 }); assert.ok(two[1].x > two[0].x + two[0].width); }); test("the deck: a post item's date is its title, its author and platform the subtitle; its QR the post", () => { const deck = resolveDeck({ chrome: { engine: "hyperframes", layout: "deck" } }); const t = deckText(base(), null, {}, deck, false); assert.equal(t.title, "Oct 30, 2026"); assert.equal(t.subtitle, "MR. OBVIOUS @ObviousRises on X"); assert.equal(postDeckText(base({ repostedBy: { handle: "ObviousRises" } })).subtitle, "MR. OBVIOUS @ObviousRises reposted on X"); assert.equal(deckQrUrl(base(), {}, { links: "original" }), base().url); // A marker may follow a post item. const m = { render: { chrome: { engine: "hyperframes", layout: "deck", deck: { markers: [{ after: "p1", label: "Then" }] } } }, timeline: [base(), { ...base(), id: "p2" }] }; assert.deepEqual(validateDeckMarkers(m), []); }); test("a context post's PARAPHRASE: validated, drawn in place of its words with a label, its words counted for the read", () => { const long = "So I thought I was done on this topic but I spent all morning watching three videos sent to me and here is my long review of all of them in detail"; const quoted = { author: "Someone Else", handle: "someone_else", badge: "gold", date: "2026-11-02T16:21:59.000Z", text: long }; const parent = { author: "A Parent", handle: "a_parent", badge: "gold", date: "2026-11-02T15:00:00.000Z", text: "the original parent words, quite a few of them here" }; // Validation: a string, at most POST_PARAPHRASE_MAX characters; absent or empty is the original. assert.deepEqual(validatePostItem(base({ quoted: { ...quoted, paraphrase: "He reviewed three videos." }, parents: [{ ...parent, paraphrase: "A question." }] })), []); assert.match(validatePostItem(base({ quoted: { ...quoted, paraphrase: 3 } })).join(" "), /quoted\.paraphrase must be a string/); assert.match(validatePostItem(base({ quoted: { ...quoted, paraphrase: "x".repeat(POST_PARAPHRASE_MAX + 1) } })).join(" "), /quoted\.paraphrase is longer than 600/); assert.match(validatePostItem(base({ parents: [{ ...parent, paraphrase: "y".repeat(601) }] })).join(" "), /parents\[0\]\.paraphrase is longer than 600/); assert.deepEqual(validatePostItem(base({ quoted: { ...quoted, paraphrase: "x".repeat(POST_PARAPHRASE_MAX) } })), []); // contextText: the paraphrase, trimmed; blank or absent is the original. assert.equal(contextText({ ...quoted, paraphrase: " He reviewed three videos. " }), "He reviewed three videos."); assert.equal(contextText({ ...quoted, paraphrase: " " }), long); assert.equal(contextText(quoted), long); // The reading-time rule counts the paraphrase's words for that context. const short = { ...quoted, paraphrase: "He reviewed three videos." }; // 4 words: 1.33 s assert.equal(contextReadSeconds(quoted), 4); // 31 words: capped at 4 s assert.equal(Math.round(contextReadSeconds(short) * 1000) / 1000, 1.333); assert.equal(readingSeconds(base({ quoted: short })), Math.round((3 + 4 / 3) * 1000) / 1000); // its own read: 1 + 6 words / 3 assert.ok(readingSeconds(base({ quoted: short })) < readingSeconds(base({ quoted }))); // The card: the paraphrase and the label, never the original words. const html = postCardHtml(base({ quoted: short, parents: [{ ...parent, paraphrase: "A parent asked a question." }] }), { width: 1920, height: 1080 }); // The words as read (each word is its own span on the card: word-zoom.mjs). const text = html.replace(/<[^>]+>/g, ""); assert.ok(text.includes("He reviewed three videos.")); assert.ok(text.includes("A parent asked a question.")); assert.ok(!text.includes("spent all morning"), "the quoted post's original words are not on the card"); assert.ok(!text.includes("the original parent words"), "the parent's original words are not on the card"); assert.equal(html.match(/data-post-paraphrased=""[^>]*>paraphrased]+>/g, "").includes("spent all morning")); assert.ok(!plain.includes("data-post-paraphrased")); // The author stays under the blur rule: a private quoted author is blurred over a paraphrase too. const priv = postCardHtml(base({ quoted: { ...short, badge: "blue", handle: "private_person" } }), { width: 1920, height: 1080 }); assert.ok(priv.includes('data-post-quoted="" data-redacted=""')); assert.ok(!priv.includes("private_person")); assert.ok(priv.replace(/<[^>]+>/g, "").includes("He reviewed three videos.")); });