// post-media-scene.mjs: consecutive post items with the same attachment are one // scene -- the planner over the combo counter's groups, the item as the scene // plays it, the column's tiles and its schedule, the knobs' checks. import assert from "node:assert/strict"; import { test } from "node:test"; import { mediaSceneById, mediaSceneOf, mediaScenes, sceneEntry, sceneLayout, sceneRows, sceneSchedule, validatePostScene, validatePostSceneRender } from "./post-media-scene.mjs"; import { repeatGroups } from "./post-combo.mjs"; import { postCardHtml, postItemPlan, validatePostItem, validatePostItems } from "./post-item.mjs"; import { followStateAt, followPath } from "./read-follow.mjs"; import { threadKnobs } from "./post-thread.mjs"; const gif = (id, o = {}) => [{ src: `posts-media/c-x/${id}/${id}_1.mp4`, kind: "gif", seconds: 0.8, width: 500, height: 280, ...o }]; const post = (id, text, o = {}) => ({ type: "post", id, post: `c-x/${id.replace(/^p-/, "")}`, platform: "x", author: "Mr. Subject", handle: "subject", date: "2026-11-02T22:15:00.000Z", text, ...o }); const clip = (id) => ({ type: "clip", id, video: "v", start: 0, end: 5 }); /** The cut: a clip, the same GIF three times running (one a join with none), another GIF, the first GIF again apart. */ function cut() { return [ clip("c1"), post("p-1", "Phat ass jiggle", { scene: "join" }), post("p-2", "It jiggle.", { media: gif("2", { blur: { on: true, strength: 9, band: { below: 0.38 } }, push: true }) }), post("p-3", "@Movail2 it jiggle", { media: gif("3", { start: 0.1 }), parents: [{ id: "77", handle: "Movail2", text: "Ready for more lessons?" }] }), post("p-4", "Something else", { media: gif("4") }), clip("c2"), post("p-5", "The jiggle, once more", { media: gif("5") }), ]; } /** Prints as post-combo-prints.mjs makes them: 2, 3 and 5 the same bytes; 4 another. */ const PRINTS = { "p-2:0": { sha256: "aa" }, "p-3:0": { sha256: "aa" }, "p-4:0": { sha256: "bb" }, "p-5:0": { sha256: "aa" } }; const groupsOf = (tl) => repeatGroups(tl, (id, i) => PRINTS[`${id}:${i}`]); test("THE SCENES: neighbours sharing a repeat group (the combo counter's matcher), a join item joined, a clip or another attachment breaks them", () => { const tl = cut(); const g = groupsOf(tl); assert.equal(g.length, 1, "one repeat group: p-2, p-3, p-5"); const { scenes, byAt } = mediaScenes(tl, g); assert.equal(scenes.length, 1); assert.deepEqual(scenes[0].ids, ["p-1", "p-2", "p-3"], "p-5 is the same GIF, but a clip and another GIF stand between"); assert.deepEqual(scenes[0].anchor, { id: "p-2", at: 2, media: 0 }, "the anchor: its first to carry it"); assert.deepEqual(scenes[0].members.map((m) => m.media), [null, 0, 0]); assert.equal(byAt.get(6), undefined); assert.equal(mediaScenes(tl, g), mediaScenes(tl, g), "memoised: the same plan for the same timeline and groups"); // Off for the cut: none. assert.equal(mediaScenes(tl, g, { posts: { scene: { on: false } } }).scenes.length, 0); // One item out (scene: false) breaks it: p-1 and p-2 alone (a join and one carrier) are still a scene. const out = cut(); out[3].scene = false; assert.deepEqual(mediaScenes(out, groupsOf(out)).scenes.map((s) => s.ids), [["p-1", "p-2"]]); // A join item with nothing to join is in no scene; one carrier alone is none either. const lone = [post("p-1", "words", { scene: "join" }), post("p-4", "x", { media: gif("4") })]; assert.equal(mediaScenes(lone, []).scenes.length, 0); // A join after a scene joins it. const after = [post("p-2", "a", { media: gif("2") }), post("p-3", "b", { media: gif("3") }), post("p-9", "c", { scene: "join" })]; assert.deepEqual(mediaScenes(after, groupsOf(after)).scenes[0].ids, ["p-2", "p-3", "p-9"]); }); test("an item's scene: its place, the earlier messages' cards, the anchor; the item as the scene plays it", () => { const tl = cut(); const g = groupsOf(tl); assert.equal(mediaSceneOf(tl, 0, g), null, "a clip"); const s1 = mediaSceneOf(tl, 1, g); assert.deepEqual([s1.n, s1.of, s1.media, s1.anchor.id, s1.before.length], [0, 3, null, "p-2", 0]); const s3 = mediaSceneById(tl, "p-3", g); assert.deepEqual([s3.n, s3.of, s3.media, s3.before.map((b) => b.id)], [2, 3, 0, ["p-1", "p-2"]]); assert.equal(s3.before[1].media, undefined, "a card, not the entry: no media, no narration"); // p-3 plays the ANCHOR's look (its file, its censor) with its OWN timing; never pushed in. const e3 = sceneEntry(tl[3], s3); assert.equal(e3.media[0].src, "posts-media/c-x/2/2_1.mp4"); assert.deepEqual(e3.media[0].blur, { on: true, strength: 9, band: { below: 0.38 } }); assert.equal(e3.media[0].start, 0.1, "its own in point"); assert.equal(e3.media[0].push, undefined); assert.equal(tl[3].media[0].src, "posts-media/c-x/3/3_1.mp4", "the entry is never rewritten"); // The anchor itself: not pushed in either (the picture stays put through the scene). assert.equal(sceneEntry(tl[2], mediaSceneOf(tl, 2, g)).media[0].push, undefined); // A join item gains the attachment: a slot beside its words, the same picture. const e1 = sceneEntry(tl[1], s1); assert.equal(e1.media.length, 1); assert.equal(e1.media[0].src, "posts-media/c-x/2/2_1.mp4"); assert.equal(postItemPlan(e1).steps.length, 1); // A channel's markup on the anchor is laid on first (the build's censorEntry). const marked = sceneEntry(tl[3], s3, { censor: (e) => ({ ...e, media: e.media.map((m) => ({ ...m, blur: { on: true, style: "black" } })) }) }); assert.deepEqual(marked.media[0].blur, { on: true, style: "black" }); assert.equal(sceneEntry(tl[4], null), tl[4]); }); test("THE COLUMN: the earlier messages as tiles over the card -- the one just before carried, older ones context -- its own after", () => { const tl = cut(); const g = groupsOf(tl); const s3 = mediaSceneOf(tl, 3, g); const L = sceneLayout(null, tl[3], s3); assert.deepEqual(L.tiles.map((t) => `${t.scope}:${t.mode}`), ["scene:0:context", "scene:1:carry", "parent:0:context", "post:full"]); assert.equal(L.tiles[1].entry.id, "p-2"); assert.equal(sceneLayout(null, tl[1], mediaSceneOf(tl, 1, g)), null, "the scene's first: nothing above it"); // A narrated item's thread layout is the base: its own tiles kept, the scene's above them. const base = { on: true, roles: [{ role: "parent:0" }], tiles: [{ scope: "parent:0", mode: "full", read: true }, { scope: "post", mode: "full", read: true }] }; const N = sceneLayout(base, tl[3], s3); assert.deepEqual(N.tiles.map((t) => `${t.scope}:${t.mode}`), ["scene:0:context", "scene:1:carry", "parent:0:full", "post:full"]); assert.deepEqual(N.roles, base.roles); // The post it answers IS an earlier message (carried by the thread read): drawn once, as the thread's. const reply = cut(); reply[3].parents = [{ id: "2", handle: "subject", text: "It jiggle." }]; const carried = { on: true, roles: [], tiles: [{ scope: "parent:0", mode: "carry", read: false }, { scope: "post", mode: "full", read: true }] }; const C = sceneLayout(carried, reply[3], mediaSceneOf(reply, 3, groupsOf(reply))); assert.deepEqual(C.tiles.map((t) => `${t.scope}:${t.mode}`), ["scene:0:context", "parent:0:carry", "post:full"]); }); test("THE SCHEDULE (un-narrated): the message before in full, shrinking and dimming as its words come in; the older ones small; the plan carries it", () => { const tl = cut(); const g = groupsOf(tl); const s3 = mediaSceneOf(tl, 3, g); const e3 = sceneEntry(tl[3], s3); const k = threadKnobs(e3, undefined); const L = sceneLayout(null, e3, s3); const sch = sceneSchedule(k, { layout: L, blocks: [{ start: 1.2 }, { start: 2 }], lead: 0.4 }); const row = (sc) => sch.tiles.find((t) => t.scope === sc); assert.deepEqual([row("scene:1").from, row("scene:1").to, row("scene:1").t0, row("scene:1").t1], [[1, 1], [0.62, 0.5], 0.8, 1.5]); assert.deepEqual([row("scene:0").from, row("scene:0").to], [[0.62, 0.5], [0.62, 0.5]]); assert.deepEqual([row("post").from, row("post").to], [[1, 0], [1, 1]], "his words revealed under it"); const rows = sceneRows(sch, L); assert.equal(rows.tiles.find((t) => t.scope === "scene:0").entry.id, "p-1"); assert.equal(rows.scene, "s1"); // The plan: postItemPlan carries the column on its follow (read-follow on by default). const plan = postItemPlan(e3, undefined, { mediaScene: s3 }); assert.deepEqual(plan.follow.thread.tiles.map((t) => t.scope), ["scene:0", "scene:1", "parent:0", "post"]); assert.equal(plan.follow.thread.scene, "s1"); // Follow off: no column (each card alone), the attachment still the scene's. assert.equal(postItemPlan({ ...e3, follow: false }, undefined, { mediaScene: s3 }).follow, undefined); // The state on the clock: at 0 the one before in full and his post hidden; after, small and his post up. const geo = { H: 900, C: 1400, blocks: {}, tiles: { "scene:0": { top: 0, height: 300 }, "scene:1": { top: 322, height: 300 }, "parent:0": { top: 644, height: 200 }, post: { top: 866, height: 300 } } }; const path = followPath(plan.follow, geo); const at0 = followStateAt(plan.follow, path, 0).tiles; const late = followStateAt(plan.follow, path, 5).tiles; assert.deepEqual([at0["scene:1"][0], at0.post[1]], [1, 0]); assert.deepEqual([late["scene:1"][0], late["scene:1"][1], late.post[1]], [0.62, 0.5, 1]); }); test("the card draws the scene's messages as tiles above his own (redacted, their authors under their own rule)", () => { const tl = cut(); const g = groupsOf(tl); const s3 = mediaSceneOf(tl, 3, g); const e3 = sceneEntry(tl[3], s3); const plan = postItemPlan(e3, undefined, { mediaScene: s3 }); const html = postCardHtml(e3, { width: 1440, height: 810, follow: true, thread: plan.follow.thread, redaction: { terms: [{ term: "jiggle" }] } }); assert.match(html, /data-thread-slot="scene:0"/); assert.match(html, /data-scene-post="p-2"[^>]*>|data-thread-tile="scene:1"[^>]*data-scene-post="p-2"/); assert.match(html, /data-post-thread="4"/); assert.match(html, /Phat ass/, "the earliest message's words drawn"); assert.doesNotMatch(html, /Phat ass jiggle/, "a redacted word in an earlier message is a bar too"); assert.ok(html.indexOf('data-thread-slot="scene:1"') < html.indexOf('data-thread-slot="post"'), "oldest first, his own last"); }); test("the knobs' checks: an item's scene false or join; the cut's { on }", () => { assert.deepEqual(validatePostScene({ id: "p", scene: "join" }), []); assert.deepEqual(validatePostScene({ id: "p", scene: false }), []); assert.match(validatePostScene({ id: "p", scene: true })[0], /must be false .* or "join"/); assert.ok(validatePostItem({ ...post("p-1", "x"), scene: "yes" }).some((e) => e.includes(".scene must be"))); assert.deepEqual(validatePostSceneRender({ posts: { scene: { on: false } } }), []); assert.equal(validatePostSceneRender({ posts: { scene: { on: 1, x: 2 } } }).length, 2); assert.ok(validatePostItems({ timeline: [], render: { posts: { scene: "no" } } }).some((e) => e.includes("render.posts.scene must be"))); });