import assert from "node:assert/strict"; import test from "node:test"; import { SEGMENT_MIN_S, SEGMENT_TAIL_S, findSegments, pickSegment, scopeOverlapsSegment } from "./segments.mjs"; import { HOST, SUBJECT, TWO_HOST, TWO_SUBJECT_A, TWO_SUBJECT_B, TWO_VIDEOS, reactionStream, twoVideoStream } from "../../testing/clips/reactionStream.mjs"; test("findSegments: two narrated videos in one stream; the host's intro is not one, his reaction after a video is folded into it", () => { const s = twoVideoStream(); const segs = findSegments({ turns: s.turns, duration: s.duration }); assert.equal(segs.length, 2); const [a, b] = segs; assert.deepEqual([a.id, a.subject, a.host], ["s1", TWO_SUBJECT_A, TWO_HOST]); assert.deepEqual([b.id, b.subject, b.host], ["s2", TWO_SUBJECT_B, TWO_HOST]); // Each starts on its video's first line (the intro and the time between are not a video's)… assert.equal(a.from, TWO_VIDEOS[0][0]); assert.equal(b.from, TWO_VIDEOS[1][0]); // …and ends on a turn's end inside SEGMENT_TAIL_S after its last line: the host's // turn 1350–1420 runs past 1100 + 300, so the tail stops at the turn before it. assert.equal(a.to, 1347); assert.ok(a.to - TWO_VIDEOS[0][1] <= SEGMENT_TAIL_S); // The second: its outro to the stream's last turn; the host's 100 s pause inside it does not split it. assert.equal(b.to, 2390); assert.match(a.why, /^subject 1 holds \d+% of long-turn time; 247 s after its last line kept/); // Deterministic. assert.deepEqual(findSegments({ turns: s.turns, duration: s.duration }), segs); // The operator's host gives the same answer; a cluster named host is left out of the counting. assert.deepEqual(findSegments({ turns: s.turns, duration: s.duration, host: TWO_HOST }), segs); assert.deepEqual(findSegments({ turns: s.turns, duration: s.duration, operator: { [TWO_HOST]: "host" } }), segs); }); test("findSegments: one narrated video is one segment; a tail never runs into the next video; a short span is dropped", () => { const one = reactionStream(); const segs = findSegments({ turns: one.turns, duration: one.duration }); assert.equal(segs.length, 1); assert.deepEqual([segs[0].from, segs[0].to, segs[0].subject, segs[0].host], [0, 600, SUBJECT, HOST]); // Two videos with only 60 s of host between them: the tail stops at the next video's first line. const turns = []; for (let t = 0; t < 600; t += 60) turns.push({ start: t, end: t + 55, speaker: 1 }, { start: t + 55, end: t + 59, speaker: 0 }); for (let t = 610; t < 660; t += 6) turns.push({ start: t, end: t + 5, speaker: 0 }); for (let t = 660; t < 1260; t += 60) turns.push({ start: t, end: t + 55, speaker: 2 }, { start: t + 55, end: t + 59, speaker: 0 }); const two = findSegments({ turns, duration: 1260 }); assert.equal(two.length, 2); assert.ok(two[0].to <= two[1].from, `${two[0].to} ≤ ${two[1].from}`); assert.equal(two[1].from, 660); // A 2-minute clip of another narrator inside the host's talk: under SEGMENT_MIN_S, dropped. const short = []; for (let t = 0; t < 900; t += 6) short.push({ start: t, end: t + 5, speaker: 0 }); short.push({ start: 300, end: 360, speaker: 3 }, { start: 361, end: 420, speaker: 3 }); assert.ok(120 < SEGMENT_MIN_S); assert.deepEqual(findSegments({ turns: short.sort((x, y) => x.start - y.start), duration: 900 }), []); assert.deepEqual(findSegments({ turns: [], duration: 100 }), []); }); test("pickSegment: the one named, else the longest; an unknown id is refused; none is null", () => { const segs = [ { id: "s1", from: 0, to: 100 }, { id: "s2", from: 200, to: 500 }, { id: "s3", from: 600, to: 900 }, ]; assert.equal(pickSegment(segs)?.id, "s2"); assert.equal(pickSegment(segs, "s3")?.id, "s3"); assert.throws(() => pickSegment(segs, "s9"), /no segment s9 \(there are s1, s2, s3\)/); assert.equal(pickSegment([]), null); }); test("scopeOverlapsSegment: half of the shorter shared; an open scope runs to the end; the whole recording covers every segment", () => { const seg = { from: 1000, to: 2000 }; assert.equal(scopeOverlapsSegment([0, 1900], seg, 3000), true); assert.equal(scopeOverlapsSegment([0, 1200], seg, 3000), false); assert.equal(scopeOverlapsSegment([1500, null], seg, 3000), true); assert.equal(scopeOverlapsSegment(null, seg, 3000), true); assert.equal(scopeOverlapsSegment([2100, 2900], seg, 3000), false); // The live case's shape: a hand scope 0–6300 and the auto-scope's first segment. assert.equal(scopeOverlapsSegment([0, 6300], { from: 292.68, to: 6318.09 }, 9367), true); assert.equal(scopeOverlapsSegment([0, 6300], { from: 6501.93, to: 9276.01 }, 9367), false); });