import { test } from "node:test"; import assert from "node:assert/strict"; import { hasLiveCapturedLines, liveChatClockOf, liveChatClockZero, parseLiveChat, parseLiveChatDetailed, } from "./liveChat"; // Run with: // pnpm --filter yt-dlp-transcript-common exec tsx --test lib/liveChat.test.ts // // The pure half of lane chat-clock: the parser's two clocks and the // recording's zero. normalizeLiveChat.test.ts drives it through the files. const T0 = Date.UTC(2026, 0, 2, 3, 0, 0); function line(o: { offsetMs: number; live?: boolean; postedMs?: number; id?: string; text?: string }): string { const renderer: Record = { message: { simpleText: o.text ?? "hi" }, authorName: { simpleText: "viewer1" }, }; if (o.id) renderer.id = o.id; if (o.postedMs !== undefined) renderer.timestampUsec = String(o.postedMs * 1000); const actions = [{ addChatItemAction: { item: { liveChatTextMessageRenderer: renderer } } }]; return o.live ? JSON.stringify({ replayChatItemAction: { actions }, videoOffsetTimeMsec: String(o.offsetMs), isLive: true }) : JSON.stringify({ replayChatItemAction: { actions, videoOffsetTimeMsec: String(o.offsetMs) } }); } test("without a zero, the parser answers exactly as before (live and replay lines alike)", () => { const src = [line({ offsetMs: 5000, live: true, postedMs: T0 + 9000 }), line({ offsetMs: 2000 })].join("\n"); assert.deepEqual(parseLiveChat(src).map((c) => c.start), [2, 5]); }); test("a live line with no timestamp of its own takes the capture zero before it", () => { const capture = T0 + 600_000; const src = [ line({ offsetMs: 1000, live: true, postedMs: capture + 1000, id: "a" }), line({ offsetMs: 4000, live: true, id: "b" }), ].join("\n"); const { cues, stats } = parseLiveChatDetailed(src, { liveZeroMs: T0 }); assert.deepEqual(cues.map((c) => c.start), [601, 604]); assert.equal(stats.captureZeroMs, capture); }); test("two capture sessions in one file: each message by its own posting time", () => { const src = [ line({ offsetMs: 1000, live: true, postedMs: T0 + 101_000, id: "a" }), line({ offsetMs: 1000, live: true, postedMs: T0 + 901_000, id: "b" }), ].join("\n"); assert.deepEqual(parseLiveChat(src, { liveZeroMs: T0 }).map((c) => c.start), [101, 901]); }); test("the cheap scan for a live capture", () => { assert.equal(hasLiveCapturedLines(line({ offsetMs: 0, live: true })), true); assert.equal(hasLiveCapturedLines(line({ offsetMs: 0 })), false); }); test("the recording's zero", () => { const meta = { release_timestamp: T0 / 1000 }; const part = (fromStart: boolean, startedAt = new Date(T0 + 50_000).toISOString()) => ({ fromStart, startedAt }); // From the start, landed or still recording; no recording at all. assert.deepEqual(liveChatClockZero(meta, { status: "landed", landedPart: 0, parts: [part(true)] }), { zeroMs: T0, basis: "broadcast-start", certain: true, }); assert.equal(liveChatClockZero(meta, { status: "recording", parts: [part(true)] }).zeroMs, T0); assert.equal(liveChatClockZero(meta, null).zeroMs, T0); // A playlist reach that missed the first 60 s of the broadcast. assert.equal( liveChatClockZero(meta, { status: "landed", landedPart: 0, parts: [part(true)], reach: { fromStart: false, missingSeconds: 60 } }).zeroMs, T0 + 60_000, ); // From now: the part's start, not certain. assert.deepEqual(liveChatClockZero(meta, { status: "landed", landedPart: 0, parts: [part(false)] }), { zeroMs: T0 + 50_000, basis: "recording-start", certain: false, }); // From now with no readable start: no zero (never the broadcast's start). for (const startedAt of [undefined, "not a date"]) { assert.deepEqual( liveChatClockZero(meta, { status: "landed", landedPart: 0, parts: [{ fromStart: false, ...(startedAt ? { startedAt } : {}) }] }), { zeroMs: null, basis: "capture-start", certain: false }, ); } // The landed part missing from the record: the record's own mode decides. assert.deepEqual(liveChatClockZero(meta, { status: "landed", landedPart: 0, fromStart: false, parts: [] }), { zeroMs: null, basis: "capture-start", certain: false, }); assert.equal(liveChatClockZero(meta, { status: "landed", landedPart: 0, fromStart: true, parts: [] }).zeroMs, T0); // A recording that landed nothing did not make the media. assert.equal(liveChatClockZero(meta, { status: "failed", parts: [part(false)] }).basis, "broadcast-start"); // Nothing known. assert.deepEqual(liveChatClockZero({}, null), { zeroMs: null, basis: "capture-start", certain: false }); }); test("the clock record", () => { const stats = { liveLines: 2, replayLines: 1, duplicatesDropped: 1, captureZeroMs: T0 + 2_234_980 }; assert.deepEqual(liveChatClockOf({ zeroMs: T0, basis: "broadcast-start", certain: true }, stats), { basis: "broadcast-start", certain: true, zeroAt: new Date(T0).toISOString(), captureStartedAt: new Date(T0 + 2_234_980).toISOString(), shiftSeconds: 2234.98, liveLines: 2, replayLines: 1, duplicatesDropped: 1, }); assert.equal( liveChatClockOf({ zeroMs: null, basis: "capture-start", certain: false }, { ...stats, liveLines: 0 }).basis, "replay", ); });