// The derivation against the shapes the first live scan showed (release 30, // lane P follow-up): pokerReadings.mjs, synthetic values, one part per // failure. Each test names the failure it pins. import { test } from "node:test"; import assert from "node:assert/strict"; import { syntheticPokerReadings, SYNTH_HERO, SYNTH_TITLE } from "../../testing/frames/pokerReadings.mjs"; import { canonicalTitle, derivePoker, looksLikeTitle, nameSimilarity, type PokerReading } from "./poker"; import { rederiveRecord } from "./scan"; import type { FrameScanRecord } from "../frameScan-server"; type R = PokerReading & { t: number }; const fx = syntheticPokerReadings(); const d = derivePoker(fx.readings as R[], fx.spoken, { everySeconds: 10, hero: fx.hero }); const kinds = (k: string) => d.events.filter((e) => e.kind === k).map((e) => e.t); const tour = d.tournaments[0]; test("one tournament: hand descriptions, a seat's name and misreads of the title do not switch it", () => { assert.equal(d.tournaments.length, 1); assert.equal(tour.name, SYNTH_TITLE); assert.equal(tour.from, 0); }); test("a title: not a hand, not a seat the log names, not the hero; misreads cluster to one", () => { assert.equal(looksLikeTitle("Three of a Kind Eights"), false); assert.equal(looksLikeTitle("with Three of a Kind Jacks."), false); assert.equal(looksLikeTitle("SeatAlpha", { speakers: ["SeatAlpha"] }), false); assert.equal(looksLikeTitle("SeatAIpha", { speakers: ["SeatAlpha"] }), false); assert.equal(looksLikeTitle("SynthHer0", { hero: SYNTH_HERO }), false); assert.equal(looksLikeTitle("SYNTH NLH PKO"), true); assert.ok(nameSimilarity("SYNTHNLHKAO", "SYNTH NLH PKO") >= 0.75); assert.equal(canonicalTitle(["SYNTH", "SYNTH NLH PKO", "Synth NLH PKO", "SYNTH NLH PKO", "SYNTH NLH10", "Other Cup"]), "SYNTH NLH PKO"); assert.equal(canonicalTitle(["Alpha Cup", "Zed Open"]), null); assert.equal(canonicalTitle(["Lone Cup"]), "Lone Cup"); }); test("a second tournament only after a confirmed bust and a screen away from the table", () => { const rs: R[] = [ { t: 0, phase: "table", tournament: { name: "Cup One" }, hero: { stack: 5000 } }, { t: 10, phase: "table", tournament: { name: "Cup Two" }, hero: { stack: 5100 } }, // A single 0 between two stacks, then a break: not a bust (not held, no banner). { t: 20, phase: "table", hero: { stack: 0 } }, { t: 30, phase: "break" }, { t: 40, phase: "table", hero: { stack: 5200 } }, // A held 0, the banner, a lobby, then chips: the next tournament. { t: 50, phase: "table", hero: { stack: 0 }, event: "eliminated" }, { t: 60, phase: "table", hero: { stack: 0 } }, { t: 70, phase: "lobby" }, { t: 80, phase: "table", tournament: { name: "Cup Three" }, hero: { stack: 1500 } }, ]; const x = derivePoker(rs, [], { everySeconds: 10 }); assert.equal(x.tournaments.length, 2); assert.equal(x.tournaments[1].from, 80); assert.equal(x.tournaments[1].name, "Cup Three"); }); test("a mid-hand dip that returns, and a single wild misread, make no double-up or big loss", () => { assert.deepEqual(kinds("double-up"), []); assert.deepEqual(kinds("big-loss"), []); const set = d.misreads.filter((m) => m.field === "stack").map((m) => m.t); for (const t of [320, 330, 340, 350, 370]) assert.ok(set.includes(t), `${t} set aside`); assert.ok(!tour.series.stack.some(([, v]) => v === 153000 || v === 140000)); }); test("a double-up needs its new level held by the next reading", () => { const base = (t: number, stack: number): R => ({ t, phase: "table", tournament: { name: "Cup" }, hero: { stack } }); const once = derivePoker([base(0, 10000), base(10, 10000), base(20, 25000), base(200, 9000), base(210, 9000)], [], { everySeconds: 10 }); assert.deepEqual(once.events.filter((e) => e.kind === "double-up"), []); const held = derivePoker([base(0, 10000), base(10, 10000), base(20, 25000), base(30, 25500), base(40, 25500)], [], { everySeconds: 10 }); assert.deepEqual(held.events.filter((e) => e.kind === "double-up").map((e) => e.t), [20]); }); test("players left only from the stats header: a table's seats and a bare count are not the field; no final table from them", () => { assert.deepEqual(kinds("final-table"), []); const left = tour.series.playersLeft; assert.ok(!left.some(([, v]) => v <= 9), JSON.stringify(left)); assert.deepEqual(left.find(([t]) => t === 90), [90, 85, "spoken"]); assert.ok(left.some(([t, v]) => t === 200 && v === 85)); }); test("a flagged reading makes no event; the final table needs a larger field read EARLIER", () => { const h = (t: number, left: number, position = 1): R => ({ t, phase: "table", tournament: { name: "Cup", playersLeft: left, position }, hero: { stack: 1000 } }); // 7 left read first and held, the field (85) said beside it: flagged, and nothing larger before it. const early = derivePoker([h(0, 7, 3), h(10, 7, 3), h(20, 7, 3)], [{ t: 20, field: "playersLeft", value: 85, text: "85 left" }], { everySeconds: 10 }); assert.deepEqual(early.events.filter((e) => e.kind === "final-table"), []); assert.equal(early.flags.length, 1); // Players left never rise: a 7 followed within ten minutes by 80 is a misread. const rises = derivePoker([h(0, 7, 3), h(10, 7, 3), h(400, 80)], [], { everySeconds: 10 }); assert.deepEqual(rises.tournaments[0].series.playersLeft, [[400, 80]]); // A real one: 12, 10, then 9. const real = derivePoker([h(0, 12), h(10, 10), h(20, 9), h(30, 9)], [], { everySeconds: 10 }); assert.deepEqual(real.events.filter((e) => e.kind === "final-table").map((e) => e.t), [20]); }); test("the log's blinds are the truth: a big blind misread is set aside, stack in big blinds stays sane", () => { assert.ok(d.misreads.some((m) => m.field === "bigBlind" && m.t === 480 && m.value === 100)); assert.ok(tour.series.bigBlind.every(([, v]) => v === 2000), JSON.stringify(tour.series.bigBlind)); assert.ok(tour.series.stackBB.every(([, v]) => v <= 200), JSON.stringify(tour.series.stackBB)); }); test("before the log says any, blinds only go up and never above the first the log says", () => { const b = (t: number, bb: number | undefined, log?: string[]): R => ({ t, phase: "table", tournament: { name: "Cup", ...(bb ? { blinds: { bb } } : {}) }, hero: { stack: 100000 }, ...(log ? { log } : {}) }); const x = derivePoker([b(0, 600), b(10, 1200), b(20, 300), b(30, 2400), b(40, undefined, ["Blinds are now 900/1,800"])], [], { everySeconds: 10 }); assert.deepEqual(x.tournaments[0].series.bigBlind, [[0, 600], [10, 1200], [40, 1800]]); }); test("the model's 'won' is not an event; an 'eliminated' with chips after it is not either; the real end is", () => { assert.deepEqual(kinds("won"), []); assert.deepEqual(kinds("eliminated"), [650]); assert.deepEqual(kinds("bust"), [650]); // The hero's pot from the log (60,000 at 2,000: 30 BB). assert.deepEqual(d.events.filter((e) => e.source === "log" && e.kind !== "level-up").map((e) => [e.t, e.kind]), [[540, "big-pot"]]); }); test("rederiveRecord: derived worked out again from the stored readings, no model, the rest kept", () => { const rec = { format: "archilyzer-frame-scan", version: 1, profile: "poker", source: { savedVideo: "source-media.mp4", durationSeconds: 700 }, sampling: { everySeconds: 10, from: 0, to: 700, width: 1280, keyframesOnly: true, frames: 70, kept: 69, skippedDuplicates: 1 }, model: { route: "api", model: "claude-haiku-4-5", promptVersion: 2 }, hints: { hero: SYNTH_HERO }, costUsd: 1.23, runs: [{ at: "2026-10-15T00:00:00Z", model: "claude-haiku-4-5", route: "api", frames: 70, read: 69, failed: 0, costUsd: 1.23 }], readings: fx.readings.filter((r) => r.t !== 680), duplicates: [[680, 670]], spoken: fx.spoken, derived: { tournaments: [], events: [], segments: [], flags: [], misreads: [] }, updatedAt: "2026-10-15T00:00:00Z", } as unknown as FrameScanRecord; const out = rederiveRecord(rec, { now: () => new Date("2026-10-16T00:00:00Z") }); assert.equal(out.derived.tournaments.length, 1); assert.deepEqual(out.derived, derivePoker(fx.readings as R[], fx.spoken, { everySeconds: 10, durationSeconds: 700, hero: SYNTH_HERO })); assert.equal(out.costUsd, 1.23); assert.deepEqual(out.runs, rec.runs); assert.equal(out.rederivedAt, "2026-10-16T00:00:00.000Z"); // With cues, the spoken numbers are read again. const withCues = rederiveRecord(rec, { cues: [{ start: 5, end: 8, text: "ninety players left" }] }); assert.deepEqual(withCues.spoken.map((s) => [s.t, s.field, s.value]), [[5, "playersLeft", 90]]); });