import assert from "node:assert/strict"; import { createHash } from "node:crypto"; import { mkdtempSync, rmSync, writeFileSync } from "node:fs"; import os from "node:os"; import path from "node:path"; import test from "node:test"; import { CHAT_LAG_DEFAULT_S, CHAT_WEIGHT, chatBins, chatLag, chatMessage, chatMoments, isChatLaugh, readChatCues, } from "./chat.mjs"; import { LAUGH_WEIGHT, analyze } from "./clips.mjs"; import { LAUGHS, reactionChat, reactionStream, twoVideoStream } from "../../testing/clips/reactionStream.mjs"; test("isChatLaugh: the laugh tokens, as words, emotes or emoji; the author is not the message", () => { for (const t of ["a: lol", "b: LMAOOO", "c: lmfao", "d: KEKW KEKW", "e: LUL", "f: OMEGALUL", "g: hahaha", "h: HAHA", "i: 😂", "j: that's it 🤣", "k: W", "l: w w", "m: :face-with-tears-of-joy:", "n: lol."]) { assert.equal(isChatLaugh(t), true, t); } for (const t of ["a: what", "b: Wow", "c: ha", "d: lolipop", "e: W rizz", "lol: hello", "", "f: Whale"]) assert.equal(isChatLaugh(t), false, t); assert.equal(chatMessage("viewer: hi: there"), "hi: there"); }); test("chatBins: messages and laughing messages per 2 s bin", () => { const b = chatBins([{ start: 0.5, text: "a: hi" }, { start: 1.9, text: "b: lol" }, { start: 2, text: "c: lol" }, { start: 9.9, text: "d: x" }, { start: 50, text: "late" }], { duration: 10 }); assert.deepEqual(b, { hop: 2, rate: [2, 1, 0, 0, 1], laugh: [1, 1, 0, 0, 0] }); }); test("chatLag: the chat's delay found against the laugh scan; the default when there is no scan or no clear peak", () => { const s = reactionStream(); for (const lag of [6, 12, 3]) { const b = chatBins(reactionChat({ lag }), { duration: s.duration }); const got = chatLag(b.laugh, s.laughScan); assert.equal(got.how, "xcorr"); assert.ok(Math.abs(got.lagS - lag) <= 1.5, `lag ${lag}: found ${got.lagS}`); } const b = chatBins(reactionChat({}), { duration: s.duration }); assert.deepEqual(chatLag(b.laugh, null), { lagS: CHAT_LAG_DEFAULT_S, how: "default", r: null, why: "no laugh scores to line the chat up with" }); // A scan that does not vary has nothing to line up with. assert.equal(chatLag(b.laugh, { ...s.laughScan, scores: s.laughScan.scores.map(() => 0.1) }).how, "default"); // Chat that laughs at random times: a weak peak. const random = chatBins(reactionChat({ laughs: [[40, 41], [170, 171], [450, 451]], lag: 0 }), { duration: s.duration }); const weak = chatLag(random.laugh, s.laughScan); assert.equal(weak.how, "default"); assert.match(weak.why, /weak peak/); }); test("chatMoments: a burst over the local median is a moment, moved back by the lag; only inside the scope; under a host laugh's weight", () => { const s = reactionStream(); const b = chatBins(reactionChat({ lag: 6, chatOnly: [200] }), { duration: s.duration }); const ms = chatMoments(b, { lagS: 6 }); assert.equal(ms.length, LAUGHS.length + 1); for (const [start] of LAUGHS) assert.ok(ms.some((m) => Math.abs(m.t - start) <= 2), `a moment near ${start}`); assert.ok(ms.some((m) => Math.abs(m.t - 194) <= 2), "the chat-only burst"); assert.ok(ms.every((m) => m.score > 0 && m.score <= CHAT_WEIGHT && m.messages >= 3)); assert.ok(CHAT_WEIGHT < LAUGH_WEIGHT.host); assert.deepEqual(chatMoments(b, { lagS: 6, from: 300, to: 400 }).map((m) => Math.round(m.t)), [340]); // A steady trickle with no burst: nothing. assert.deepEqual(chatMoments(chatBins(reactionChat({ laughs: [] }), { duration: 600 }), { lagS: 6 }), []); }); test("readChatCues: live_chat.cues.json beside the recording, or null", async () => { const dir = mkdtempSync(path.join(os.tmpdir(), "chat-")); try { assert.equal(await readChatCues(dir), null); writeFileSync(path.join(dir, "live_chat.cues.json"), JSON.stringify({ version: 1, source: "whisper", cues: [] })); assert.equal(await readChatCues(dir), null); writeFileSync(path.join(dir, "live_chat.cues.json"), JSON.stringify({ version: 1, source: "live_chat", cues: [{ start: 1, end: 6, text: "a: lol" }, { start: "x", text: "b" }] })); assert.deepEqual(await readChatCues(dir), { source: "live_chat.cues.json", cues: [{ start: 1, text: "a: lol" }] }); } finally { rmSync(dir, { recursive: true, force: true }); } }); // Release 35's clips.mjs (a62cdc98) over the synthetic streams, hashed: with // no chat the analysis must be EXACTLY what it was. const RELEASE_35 = { "one-model": "0fbdc3b365476394", "one-text": "fe4990b2624fa99f", "two-model": "8a7c1e0c8e1418c0", "two-text": "f631bc75e4d205b3" }; const digest = (x: unknown) => createHash("sha256").update(JSON.stringify(x)).digest("hex").slice(0, 16); test("analyze: no chat is release 35 byte for byte; a chat replay adds `chat` moments and the lag", () => { for (const [name, s] of [["one", reactionStream()], ["two", twoVideoStream()]] as const) { for (const mode of ["model", "text"] as const) { const o = { turns: s.turns, duration: s.duration, words: s.words, params: { target: 120 }, laughScan: mode === "model" ? s.laughScan : null }; for (const chat of [undefined, null, { cues: [] }]) { const a = analyze({ ...o, chat }); assert.equal(digest({ moments: a.moments, proposals: a.proposals, laughs: a.laughs, speakers: a.speakers }), RELEASE_35[`${name}-${mode}`], `${name}-${mode} chat ${JSON.stringify(chat)}`); assert.equal(a.chat, null); } } } const s = reactionStream(); const cues = reactionChat({ lag: 6, chatOnly: [200] }); const a = analyze({ turns: s.turns, duration: s.duration, words: s.words, params: { target: 120 }, laughScan: s.laughScan, chat: { source: "live_chat.cues.json", cues } }); assert.ok(a.chat); assert.equal(a.chat.source, "live_chat.cues.json"); assert.equal(a.chat.lag.how, "xcorr"); assert.ok(Math.abs(a.chat.lagS - 6) <= 1.5); assert.equal(a.chat.messages, cues.length); assert.equal(a.chat.bins.hop, 2); assert.equal(a.chat.bins.rate.length, 300); const chats = a.moments.filter((m) => m.kind === "chat"); assert.equal(chats.length, 4); // The chat-only burst (no laugh there) becomes a proposal of its own, said plainly. const lone = a.proposals.find((p) => p.moments.some((id) => chats.find((m) => m.id === id && Math.abs(m.t - 194) < 3))); assert.ok(lone, "a proposal for the chat-only burst"); assert.match(lone.why, /chat laughs \(20 messages\)/); // A host laugh still outscores the chat it caused. const host = a.moments.filter((m) => m.kind === "laugh").reduce((x, m) => Math.max(x, m.score), 0); assert.ok(chats.every((m) => m.score < host)); // Scoped: only the scope's bursts. const scoped = analyze({ turns: s.turns, duration: s.duration, words: s.words, params: { scope: [300, 450] }, laughScan: s.laughScan, chat: { cues } }); assert.deepEqual(scoped.moments.filter((m) => m.kind === "chat").map((m) => Math.round(m.t / 10) * 10), [340]); });