import { test } from "node:test"; import assert from "node:assert/strict"; import { compareRank, framesCut, frameTotals, rankOf, statsLine, titleOf, valueAt } from "./frames-cut.mjs"; // A synthetic frame-scan sidecar (the shape common/lib/frameScan-server.ts writes). const scan = () => ({ format: "archilyzer-frame-scan", profile: "poker", source: { savedVideo: "source-media.mp4", durationSeconds: 400 }, sampling: { everySeconds: 10, from: 0, to: 400 }, derived: { tournaments: [ { key: "synthetic cup", name: "Synthetic Cup", from: 30, to: 200, series: { stack: [[30, 20000], [90, 41000], [120, 9000], [180, 0]], stackBB: [[30, 100], [90, 136.7], [120, 22.5]], playersLeft: [[30, 85], [90, 60], [120, 9]], level: [[30, 3], [90, 4], [120, 5]] }, facts: {}, }, { key: "viewer $1000", name: "Viewer $1000", from: 250, to: 400, series: { stack: [[250, 1500], [300, 3100, "spoken"]] }, facts: {} }, ], events: [ { t: 35, kind: "buy-in", detail: "said", source: "spoken" }, { t: 90, kind: "double-up", detail: "20,000 → 41,000 chips (2.1×)", source: "series" }, { t: 120, kind: "big-loss", detail: "41,000 → 9,000 chips", source: "series" }, { t: 120, kind: "final-table", detail: "9 players left", source: "series" }, { t: 180, kind: "bust", detail: "out from 9,000 chips", source: "series" }, { t: 180, kind: "eliminated", detail: "shown on screen", source: "frame" }, ], segments: [], flags: [], }, }); test("valueAt, frameTotals, statsLine: the running stats at a second", () => { assert.equal(valueAt([[30, 1], [90, 2]], 89), 1); assert.equal(valueAt([[30, 1]], 10), null); const at100 = frameTotals(scan(), 100); assert.deepEqual(at100, { tournament: "synthetic cup", name: "Synthetic Cup", stack: 41000, stackBB: 136.7, playersLeft: 60, level: 4 }); assert.equal(statsLine(at100), "Level 4 · 60 left · 41,000 chips (136.7 BB)"); assert.equal(frameTotals(scan(), 310).stack, 3100); assert.equal(frameTotals(scan(), 5), null); assert.equal(statsLine(null), ""); }); test("framesCut: a clip around each event, overlapping windows merged, the highest-ranked event titles it", () => { const { timeline, frameScan, skipped, seconds } = framesCut(scan(), { channel: "synth", video: "s1" }); assert.equal(skipped, 0); // 90 (70–115) and 120 (100–145) overlap: one clip; 180 alone (the banner folded into the bust). // The double-up is a scoop and leads the merged clip (release 31's ranking). assert.deepEqual(timeline.map((c) => [c.id, c.start, c.end, c.cite, c.onscreen.title]), [ ["f01", 70, 145, 90, "Doubles up"], ["f02", 160, 205, 180, "Busts out"], ]); assert.equal(seconds, 120); assert.deepEqual(timeline[0].frameEvents.map((e) => e.kind), ["double-up", "final-table", "big-loss"]); assert.equal(timeline[0].onscreen.subtitle, "Level 4 · 60 left · 41,000 chips (136.7 BB)"); assert.equal(timeline[1].quote, ""); assert.equal(timeline[0].drafted.onscreen.model, "code"); assert.equal(frameScan.file, "frame-scan.poker.json"); assert.deepEqual(frameScan.overlay[0].points.map((p) => [p.at, p.stack]), [[70, 20000], [90, 41000], [120, 9000]]); assert.equal(frameScan.tournaments.length, 2); }); test("framesCut: cut on sentence edges where cues exist, clamped to the video", () => { const cues = [ { start: 60, end: 66, text: "so I shove." }, { start: 66, end: 72, text: "He calls with" }, { start: 72, end: 80, text: "queens and I hold." }, { start: 140, end: 150, text: "nine left now" }, { start: 150, end: 152, text: "here we go." }, ]; const { timeline } = framesCut(scan(), { channel: "synth", video: "s1", cues, pre: 20, post: 25 }); assert.equal(timeline[0].start, 66); assert.equal(timeline[0].end, 152); }); test("framesCut: at most maxClips, the most important kept, in time order", () => { const { timeline, skipped } = framesCut(scan(), { channel: "synth", video: "s1", maxClips: 2, pre: 1, post: 1 }); assert.equal(skipped, 2); assert.deepEqual(timeline.map((c) => c.onscreen.title), ["Doubles up", "Busts out"]); assert.throws(() => framesCut(scan(), {}), /channel and the video/); }); test("framesCut: a big pot and a knockout before a plain pot; one kind twice within a minute is one", () => { const sc = scan(); sc.derived.events = [ { t: 30, kind: "pot", detail: "takes the pot 1,600 (8 BB)", source: "log" }, { t: 60, kind: "big-pot", detail: "takes the pot 12,000 (60 BB)", source: "log" }, { t: 150, kind: "knockout", detail: "SynthHero knocked out Villain", source: "log" }, { t: 180, kind: "bust", detail: "out", source: "series" }, { t: 200, kind: "bust", detail: "SynthHero has finished the tournament in 9th", source: "log" }, ]; // A 30-s budget: the three 10-s windows fill it, the pot's 16 s would pass it. const { timeline, skipped } = framesCut(sc, { channel: "synth", video: "s1", pre: 5, post: 5, target: 30 }); assert.deepEqual(timeline.map((c) => [c.cite, c.onscreen.title]), [[60, "Takes a big pot"], [150, "Knocks one out"], [180, "Busts out"]]); assert.equal(skipped, 1); // With room, the plain pot is a (short) clip too. const roomy = framesCut(sc, { channel: "synth", video: "s1", pre: 5, post: 5 }); assert.deepEqual(roomy.timeline.map((c) => [c.cite, c.start, c.end]), [[30, 24, 40], [60, 55, 65], [150, 145, 155], [180, 175, 185]]); }); // Release 31: the events lane P3's derivation makes, as the sidecar holds them. const R31 = [ { t: 0, kind: "level-up", detail: "blinds 500/1,000", source: "log", rule: "log-blinds", evidence: { bigBlind: 1000 }, confidence: "unconfirmed" }, { t: 300, kind: "level-up", detail: "blinds 600/1,200 (from 1,000), level 2", source: "log", evidence: { bigBlind: 1200, from: 1000 }, confidence: "confirmed" }, { t: 400, kind: "players-left", detail: "100 players left (under 100)", source: "series", evidence: { milestone: 100, playersLeft: 100 }, confidence: "confirmed" }, { t: 500, kind: "big-pot", detail: "takes the pot 40,000 (33 BB)", source: "log", evidence: { pot: 40000, potBB: 33.3 }, confidence: "confirmed" }, { t: 700, kind: "pot", detail: "takes the pot 3,000 (3 BB)", source: "log", evidence: { pot: 3000, potBB: 2.5 }, confidence: "confirmed" }, { t: 1000, kind: "all-in", detail: "all in with 81,500: doubles up (170,000)", source: "log", evidence: { outcome: "doubled" }, confidence: "confirmed" }, { t: 1010, kind: "double-up", detail: "81,500 → 170,000 chips (2.1×)", source: "series" }, { t: 1300, kind: "all-in", detail: "calls all in with 170,000: loses chips (120,000)", source: "frame", evidence: { outcome: "lost" }, confidence: "confirmed" }, { t: 1400, kind: "players-left", detail: "45 players left (under 50)", source: "series", evidence: { milestone: 50, playersLeft: 45 }, confidence: "confirmed" }, { t: 2600, kind: "final-table", detail: "9 players left", source: "series", evidence: { milestone: 9, playersLeft: 9 }, confidence: "confirmed" }, { t: 2900, kind: "bust", detail: "out from 120,000 chips", source: "series" }, { t: 2900, kind: "all-in", detail: "all in with 120,000: busts", source: "frame", evidence: { outcome: "busted" }, confidence: "confirmed" }, ]; const r31scan = () => { const sc = scan(); sc.source.durationSeconds = 3000; sc.derived.tournaments = [{ key: "synth main", name: "SYNTH MAIN", from: 0, to: 2930, series: { stack: [[0, 50000], [500, 80000], [1010, 170000], [1300, 120000], [2900, 0]], playersLeft: [[0, 120], [400, 100], [1400, 45], [2600, 9]] }, facts: {} }]; sc.derived.events = R31; return sc; }; test("rankOf / compareRank: scoops (big pots, won all-ins), then milestones, swings, pots, level-ups last; confirmed first", () => { const order = [...R31].sort(compareRank).map((e) => `${e.t} ${e.kind}`); assert.deepEqual(order, [ "1000 all-in", "500 big-pot", "1010 double-up", "2900 bust", "2600 final-table", "1400 players-left", "400 players-left", "1300 all-in", "2900 all-in", "700 pot", "300 level-up", "0 level-up", ]); assert.equal(rankOf({ kind: "buy-in" }), null); assert.equal(titleOf(R31[5]), "All in, doubles up"); assert.equal(titleOf(R31[2]), "Down to 100"); assert.equal(titleOf(R31[8]), "Under 50 left"); assert.equal(titleOf(R31[0]), "Blinds go up"); }); test("framesCut: the budget is filled first-first; a window that would pass it is passed over; the overlay rides each clip", () => { const { timeline, frameScan, seconds, skipped } = framesCut(r31scan(), { channel: "synth", video: "s1", target: 180, stats: true }); // The all-in and the double-up (980–1035), the big pot (480–525), the bust // (2880–2925, the busted all-in inside it for free) = 145 s; the final // table's and the milestones' 45 s would pass 180, so the plain pot's 16 s // and the confirmed level-up's 16 s take the rest (177 s). assert.deepEqual(timeline.map((c) => [c.cite, c.start, c.end, c.onscreen.title]), [ [300, 294, 310, "Blinds go up"], [500, 480, 525, "Takes a big pot"], [700, 694, 710, "Takes a pot"], [1000, 980, 1035, "All in, doubles up"], [2900, 2880, 2925, "Busts out"], ]); assert.equal(seconds, 177); assert.equal(skipped, 5); // With the stats region on, the subtitle is the event's own words. assert.equal(timeline[3].onscreen.subtitle, "all in with 81,500: doubles up (170,000)"); assert.deepEqual(timeline[3].frameEvents.map((e) => [e.kind, e.confidence ?? null]), [["all-in", "confirmed"], ["double-up", null]]); // Every clip keeps its overlay points: the start, then each change inside it. assert.deepEqual(frameScan.overlay.map((o) => o.clip), ["f01", "f02", "f03", "f04", "f05"]); assert.deepEqual(frameScan.overlay[3].points.map((p) => [p.at, p.stack, p.playersLeft]), [[980, 80000, 100], [1010, 170000, 100]]); // The default budget (240 s) takes the final table too. const roomy = framesCut(r31scan(), { channel: "synth", video: "s1" }); assert.ok(roomy.timeline.some((c) => c.cite === 2600)); assert.ok(roomy.seconds <= 240); });