// The chat overlay (chat.mjs, chrome-chat.mjs, modules/chat.mjs, build-video.mjs chatClipCues): // the span/gap mapping, the message windowing, the missing-file case, the page. import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdtemp, mkdir, rm, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import path from "node:path"; import { CHAT_CUES_FILE, chatSchedule, chatStates, chatWanted, chatWindow, clampChars, resolveChat, splitChatCue, validateChat, validateChatEntries, } from "./chat.mjs"; import { chatGeometry, chatHtml } from "./chrome-chat.mjs"; import { deckSchedule } from "./deck.mjs"; import chat from "./modules/chat.mjs"; import { moduleById, scheduleSlices } from "./modules/index.mjs"; const RENDER = { width: 1920, height: 1080, fps: 30, palette: { bg: "#101219", fg: "#f2f0ea", muted: "#979caa", accent: "#e5534b", amber: "#ffc860" }, chrome: { engine: "hyperframes", layout: "deck", deck: {} }, }; const cue = (start, text) => ({ start, end: start + 1, text }); // A stream's chat, one message a second from source second 100. const CUES = Array.from({ length: 40 }, (_, i) => cue(100 + i, `user${i}: message ${i}`)); test("splitChatCue and clampChars: author and text, long lines cut", () => { assert.deepEqual(splitChatCue("ann: hi there: friend"), { author: "ann", text: "hi there: friend" }); assert.deepEqual(splitChatCue("no author here"), { author: "", text: "no author here" }); assert.deepEqual(splitChatCue(" a b "), { author: "", text: "a b" }); assert.equal(clampChars("abcdef", 4), "abc…"); assert.equal(clampChars("abc", 4), "abc"); }); test("chatWindow: the clip's source span on the segment's clock, thinned, capped", () => { const w = chatWindow({ cues: CUES, sourceFrom: 110, seconds: 10, minGap: 0 }); // Source 110..119 -> segment 0..9; 120 is past the end. assert.deepEqual(w.map((m) => m.at), [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); assert.equal(w[0].text, "message 10"); assert.equal(w[0].author, "user10"); // Thinning: one message per 2.5 s keeps every third. assert.deepEqual(chatWindow({ cues: CUES, sourceFrom: 110, seconds: 10, minGap: 2.5 }).map((m) => m.at), [0, 3, 6, 9]); // Length cap. const long = chatWindow({ cues: [cue(5, `x: ${"w".repeat(500)}`)], sourceFrom: 0, seconds: 10, maxChars: 40 }); assert.equal([...long[0].text].length, 40); assert.match(long[0].text, /…$/); // Empty messages, a cue before the span and a non-list. assert.deepEqual(chatWindow({ cues: [cue(5, "ann: "), cue(1, "b: early")], sourceFrom: 2, seconds: 10 }), []); assert.deepEqual(chatWindow({ cues: null, sourceFrom: 2, seconds: 10 }), []); }); test("chatWindow: gaps -- a message said in a gap is not shown, a later one lands earlier", () => { // Source 110..120 with seconds 112..115 taken out: plays 5 s + 5 s. const gaps = [{ from: 112, to: 115 }]; const w = chatWindow({ cues: CUES, sourceFrom: 110, seconds: 7, gaps, minGap: 0 }); // Segment seconds 0..6 show source 110,111,(112 edge),115..119: 113,114 gone. const texts = w.map((m) => m.text); assert.ok(!texts.includes("message 13") && !texts.includes("message 14"), "gap messages dropped"); assert.deepEqual(w.find((m) => m.text === "message 15").at, 2, "source 115 is 5 s in, less the 3 s gap before it"); assert.deepEqual(w.find((m) => m.text === "message 11").at, 1); }); test("chatSchedule: the cut's clock, the first clip from its start and the next from its dissolve's middle", () => { const segments = [ { id: "a", start: 0, duration: 10, cues: CUES, sourceFrom: 110 }, { id: "b", start: 9.5, duration: 10 }, { id: "c", start: 19, duration: 10, cues: CUES, sourceFrom: 120, gaps: [{ from: 125, to: 130 }] }, ]; const s = chatSchedule({ segments, D: 1, total: 29, render: RENDER }); assert.deepEqual(s.clips.map((c) => [c.segment, c.in, c.out]), [["a", 0, 10], ["c", 19.5, 29]]); // a: source 110.. -> cut 0..; thinned at the 0.6 s default to every 1 s message. assert.equal(s.clips[0].messages[0].at, 0); // c starts at 19, so its first shown message is the one at or after 19.5. assert.ok(s.clips[1].messages[0].at >= 19.5); assert.ok(s.clips[1].messages.every((m) => m.text !== "message 27"), "a message in the gap (source 125-130) is not shown"); assert.equal(chatSchedule({ segments: [{ id: "a", start: 0, duration: 5 }], D: 1, total: 5, render: RENDER }), null); }); test("chatStates: the last `lines` messages up to the newest, each held until the next", () => { const clip = { in: 0, out: 5, messages: [1, 2, 3, 4].map((n) => ({ at: n, author: "a", text: `m${n}` })) }; const st = chatStates(clip, 2); assert.deepEqual(st.map((s) => [s.at, s.until, s.rows.map((r) => r.text)]), [[1, 2, ["m1"]], [2, 3, ["m1", "m2"]], [3, 4, ["m2", "m3"]], [4, 5, ["m3", "m4"]]]); }); test("validateChat and validateChatEntries: bounds, unknown keys, a boolean on a clip under the deck", () => { assert.deepEqual(validateChat(undefined), []); assert.deepEqual(validateChat({ position: "left", lines: 8, width: 300, maxChars: 80, minGap: 1 }), []); assert.match(validateChat({ position: "middle" })[0], /position must be/); assert.match(validateChat({ lines: 99 })[0], /lines must be a whole number from 3 to 16/); assert.match(validateChat({ colour: 1 })[0], /colour is not a chat setting/); assert.match(validateChat([])[0], /must be an object/); const m = (e, render = RENDER) => ({ render, timeline: [e] }); assert.deepEqual(validateChatEntries(m({ type: "clip", id: "a", chat: true })), []); assert.match(validateChatEntries(m({ type: "clip", id: "a", chat: "on" }))[0], /must be true or false/); assert.match(validateChatEntries(m({ type: "card", id: "a", chat: true }))[0], /only a clip/); assert.match(validateChatEntries(m({ type: "clip", id: "a", chat: true }, { ...RENDER, chrome: undefined }))[0], /drawn by the deck/); assert.equal(chatWanted({ type: "clip", chat: true }), true); assert.equal(chatWanted({ type: "clip", chat: false }), false); assert.equal(chatWanted({ type: "card", chat: true }), false); }); test("deckSchedule carries the chat only for a clip that has cues; the module draws exactly then", () => { const entries = [ { type: "clip", id: "a", video: "v", start: 110, end: 120, chat: true }, { type: "clip", id: "b", video: "w", start: 0, end: 10 }, ]; const base = { entries, durs: [10, 10], D: 0.5, render: RENDER, estimated: true }; assert.equal(deckSchedule(base).chat, undefined, "no chat: the schedule is what it always was"); const doc = deckSchedule({ ...base, chat: { cues: new Map([["a", CUES]]), clock: new Map([["a", 110]]) } }); assert.equal(doc.chat.clips.length, 1); assert.equal(doc.chat.clips[0].segment, "a"); assert.equal(chat.on(doc, RENDER), true); assert.equal(chat.on({}, RENDER), false); assert.match(chat.check({}), /needs a schedule/); assert.equal(moduleById("chat"), chat); assert.equal(scheduleSlices(doc).chat, undefined, "the page reads the whole schedule, as stats does"); }); test("the chat page: a state per message, newest at the bottom, the geometry beside the footage", () => { const doc = deckSchedule({ entries: [{ type: "clip", id: "a", video: "v", start: 110, end: 120, chat: true }], durs: [10], D: 0.5, render: RENDER, estimated: true, chat: { cues: new Map([["a", CUES]]), clock: new Map([["a", 110]]) }, }); const { html, items } = chatHtml(doc, RENDER, { fonts: { regular: "r.ttf", bold: "b.ttf" } }); assert.match(html, /data-composition-id="chat"/); assert.equal(items.length, doc.chat.clips[0].messages.length); const last = items[items.length - 1].html; assert.ok(last.indexOf("message 10") < last.indexOf("message 19") || !last.includes("message 10"), "oldest first, newest last"); assert.match(last, /message 19/); assert.ok(html.includes('id="chat-data"')); const g = chatGeometry(RENDER); const left = chatGeometry({ ...RENDER, chrome: { ...RENDER.chrome, chat: { position: "left", width: 300 } } }); assert.ok(g.x > left.x && left.width === 300); assert.throws(() => chatHtml({ ...doc, chat: undefined }, RENDER), /plays none/); assert.equal(resolveChat(RENDER).lines, 10); }); test("chatClipCues: no chat file is a note, never a failure; a file's cues are read for the clip", async () => { const { chatClipCues } = await import("./build-video.mjs"); const dir = await mkdtemp(path.join(tmpdir(), "umtool-chat-")); try { // No corpus configured in a unit test: every clip is a note and the build goes on. const none = await chatClipCues({ entries: [{ type: "clip", id: "a", video: "v1", chat: true }], segments: [], provenance: { channelSlug: "chan" }, render: RENDER }); assert.equal(none.cues.size, 0); assert.equal(CHAT_CUES_FILE, "live_chat.cues.json"); await mkdir(path.join(dir, "chan", "data", "v1"), { recursive: true }); await writeFile(path.join(dir, "chan", "data", "v1", CHAT_CUES_FILE), JSON.stringify({ cues: CUES })); } finally { await rm(dir, { recursive: true, force: true }); } });