import { test } from "node:test"; import assert from "node:assert/strict"; import { closeSchema } from "../llm/structured"; import { coercePokerReading, derivePoker, pokerSchema, pokerSystem, pokerUser, POKER_PROFILE, type PokerReading } from "./poker"; import type { SpokenReading } from "./types"; type R = PokerReading & { t: number }; const table = (t: number, stack: number | undefined, more: Partial> = {}, extra: Partial = {}): R => ({ t, phase: "table", // A stats header's "position 1 of 85": players left is the field's only beside a position. tournament: { name: "Main Event", playersLeft: 85, position: 1, totalPlayers: 120, level: 3, blinds: { sb: 100, bb: 200 }, ...more }, ...(stack !== undefined ? { hero: { stack } } : {}), ...extra, }); const derive = (rs: R[], spoken: SpokenReading[] = []) => derivePoker(rs, spoken, { everySeconds: 10 }); const series = (d: ReturnType, field: string, key?: string) => (key ? d.tournaments.find((t) => t.key === key)! : d.tournaments[0]).series[field]; // ─── coerce ─── test("coerce: a reading's fields are checked; unreadable is absent, never zero", () => { const r = coercePokerReading({ frame: 1, phase: "table", tournament: { name: " Main Event ", level: 7, smallBlind: 500, bigBlind: 1000, ante: null, playersLeft: 85, totalPlayers: 1200, prizePool: null }, hero: { stack: 23456.4, stackInBigBlinds: null, seat: null, cards: ["Ah", "10d"] }, hand: { pot: 4500, board: ["7c", "7c"], action: null }, event: null, }); assert.deepEqual(r, { phase: "table", tournament: { name: "Main Event", level: 7, blinds: { sb: 500, bb: 1000 }, playersLeft: 85, totalPlayers: 1200 }, hero: { stack: 23456, cards: ["Ah", "Td"] }, hand: { pot: 4500 }, }); }); test("coerce: refuses what is not a reading, and drops misread pairs", () => { assert.equal(coercePokerReading(null), null); assert.equal(coercePokerReading({ phase: "dancing" }), null); const r = coercePokerReading({ phase: "table", tournament: { smallBlind: 400, bigBlind: 200, playersLeft: 90, totalPlayers: 40 }, hero: { stack: -5 }, event: "fireworks" }); assert.deepEqual(r, { phase: "table", tournament: { playersLeft: 90 } }); }); // ─── the ask ─── test("the prompt carries rules, a hero hint when given, and no sample reading", () => { const s = pokerSystem({ hero: "SynthHero" }); assert.match(s, /null/); assert.match(s, /"SynthHero"/); assert.doesNotMatch(s, /\{"phase"/); assert.doesNotMatch(pokerSystem(), /screen name is/); assert.equal(pokerUser([{ t: 65 }, { t: 3725 }]), "2 frames, in order: frame 1 at 1:05; frame 2 at 1:02:05. Read each."); }); test("the schema asks for exactly n readings and closes for a strict model", () => { const s = pokerSchema(3) as { properties: { frames: { minItems: number; maxItems: number } } }; assert.equal(s.properties.frames.minItems, 3); assert.equal(s.properties.frames.maxItems, 3); const closed = JSON.stringify(closeSchema(pokerSchema(1))); assert.match(closed, /"additionalProperties":false/); assert.ok(POKER_PROFILE.framesPerAsk.local === 1); }); // ─── derive ─── test("a stack that jumps 100× for one frame is a misread, not a double-up", () => { const d = derive([table(0, 20000), table(10, 2000000), table(20, 20500), table(30, 21000)]); assert.deepEqual(d.misreads.filter((m) => m.field === "stack"), [{ t: 10, field: "stack", value: 2000000 }]); assert.deepEqual(d.events.filter((e) => e.kind === "double-up"), []); assert.deepEqual(series(d, "stack"), [[0, 20000], [20, 20500], [30, 21000]]); }); test("a double-up held by the next frame is an event; a big loss too", () => { const d = derive([table(0, 20000), table(10, 20000), table(20, 41000), table(30, 41500), table(40, 15000), table(50, 15000), table(60, 15500)]); const kinds = d.events.map((e) => [e.t, e.kind]); assert.deepEqual(kinds, [[20, "double-up"], [40, "big-loss"]]); assert.match(d.events[0].detail, /20,000 → 41,000 chips \(2\.0×\)/); }); test("stack in big blinds is the client's when shown, else the stack over the big blind", () => { const d = derive([table(0, 20000), table(10, 30000, {}, { hero: { stack: 30000, bb: 150 } })]); assert.deepEqual(series(d, "stackBB"), [[0, 100], [10, 150]]); }); test("a bust: the stack at 0 and no chips after it in that tournament", () => { const d = derive([table(0, 9000), table(10, 9000), table(20, 0, {}, { event: "eliminated" }), { t: 30, phase: "lobby", tournament: { name: "Main Event" } }]); const bust = d.events.find((e) => e.kind === "bust")!; assert.equal(bust.t, 20); assert.match(bust.detail, /out from 9,000 chips/); assert.ok(d.events.some((e) => e.kind === "eliminated" && e.source === "frame")); }); test("a 0 for one frame between two stacks is a misread, not a bust", () => { const d = derive([table(0, 9000), table(10, 0), table(20, 9100)]); assert.equal(d.events.filter((e) => e.kind === "bust").length, 0); }); test("the final table: players left first at 9 or fewer in a larger field", () => { const d = derive([table(0, 1, { playersLeft: 12 }), table(10, 1, { playersLeft: 10 }), table(20, 1, { playersLeft: 9 }), table(30, 1, { playersLeft: 8 })]); assert.deepEqual(d.events.filter((e) => e.kind === "final-table").map((e) => e.t), [20]); }); test("a win: one player left and the hero with chips", () => { const d = derive([table(0, 50000, { playersLeft: 2 }), table(10, 240000, { playersLeft: 1 })]); assert.ok(d.events.some((e) => e.kind === "won" && e.t === 10)); }); test("frame events are one per run: the same banner for a minute is one moment", () => { // Release 31: an all-in banner is the hero's only with the hero's chips in // the middle (the cards shown, the stack read 0); with chips in front of // the hero it is another player's shove. const shove = (t: number): R => ({ t, phase: "table", hero: { stack: 0, cards: ["As", "Kd"] }, event: "all-in" }); const d = derive([table(-10, 1000), shove(0), shove(10), table(20, 1000), shove(100), table(110, 1000), table(120, 1000, {}, { event: "all-in" })]); assert.deepEqual(d.events.filter((e) => e.kind === "all-in").map((e) => e.t), [0, 100]); assert.deepEqual(d.events.filter((e) => e.kind === "eliminated").map((e) => e.t), []); }); test("phase segments: a one-frame blip between two of another phase is absorbed", () => { const d = derive([ { t: 0, phase: "lobby" }, { t: 10, phase: "lobby" }, table(20, 100), { t: 30, phase: "other" }, table(40, 100), { t: 50, phase: "break" }, { t: 60, phase: "break" }, ]); assert.deepEqual(d.segments.map((s) => [s.from, s.to, s.phase]), [[0, 20, "lobby"], [20, 50, "table"], [50, 70, "break"]]); assert.equal(d.segments[1].tournament, "main event"); }); test("two tournaments in one stream never share a series: the second starts after a confirmed bust", () => { const viewer = (t: number, stack: number): R => ({ t, phase: "table", tournament: { name: "Viewer $1000", playersLeft: 30, position: 5, totalPlayers: 40 }, hero: { stack } }); const d = derive([table(0, 20000), table(10, 21000), table(15, 0, {}, { event: "eliminated" }), { t: 20, phase: "lobby" }, viewer(30, 1500), viewer(40, 3100), viewer(50, 3200)]); assert.deepEqual(d.tournaments.map((t) => [t.key, t.name, t.from]), [["main event", "Main Event", 0], ["viewer $1000", "Viewer $1000", 30]]); assert.deepEqual(series(d, "stack", "main event"), [[0, 20000], [10, 21000], [15, 0]]); assert.deepEqual(series(d, "stack", "viewer $1000"), [[30, 1500], [40, 3100], [50, 3200]]); // The viewer tournament's double-up is its own. assert.ok(d.events.some((e) => e.kind === "double-up" && e.tournament === "viewer $1000")); }); test("unnamed readings are the tournament they sit in; a misread field size is not another tournament", () => { const d = derive([ table(0, 1000, { totalPlayers: 1200 }), { t: 10, phase: "table", tournament: { totalPlayers: 1200 }, hero: { stack: 1100 } }, { t: 20, phase: "table", tournament: { totalPlayers: 40 }, hero: { stack: 1150 } }, ]); assert.deepEqual(d.tournaments.map((t) => t.key), ["main event"]); }); test("a spoken number that disagrees with a nearby frame is flagged, never trusted; one that agrees is quiet", () => { const spoken: SpokenReading[] = [ { t: 12, field: "playersLeft", value: 85, text: "eighty five players left" }, { t: 22, field: "playersLeft", value: 40, text: "forty players left" }, ]; const d = derive([table(0, 1000), table(10, 1000), table(20, 1000)], spoken); assert.deepEqual(d.flags, [{ t: 22, field: "playersLeft", spoken: 40, frame: 85, frameT: 20, tournament: "main event" }]); }); test("a spoken number where no frame read the field fills the series, marked spoken, and makes no event", () => { const spoken: SpokenReading[] = [{ t: 400, field: "stack", value: 24000, text: "I have twenty four thousand" }]; const rs = [table(0, 20000), ...Array.from({ length: 59 }, (_, i) => table(10 + i * 10, undefined)), table(600, 20500)]; const d = derive(rs, spoken); assert.deepEqual(series(d, "stack"), [[0, 20000], [400, 24000, "spoken"], [600, 20500]]); assert.equal(d.events.filter((e) => e.kind === "double-up").length, 0); }); test("spoken facts land in the tournament's facts when no frame read them; a buy-in is an event", () => { const spoken: SpokenReading[] = [{ t: 5, field: "buyIn", value: 2750, text: "$2,750 buy in" }]; const d = derive([table(0, 1000), table(10, 1000)], spoken); assert.equal(d.tournaments[0].facts.buyIn, 2750); assert.equal(d.tournaments[0].facts.totalPlayers, 120); assert.ok(d.events.some((e) => e.kind === "buy-in" && e.source === "spoken")); }); test("derive is pure over its input order", () => { const rs = [table(20, 41000), table(0, 20000), table(10, 20000), table(30, 41000)]; assert.deepEqual(derive(rs), derive([...rs].sort((a, b) => a.t - b.t))); }); // ─── The client's log (prompt version 2) ─── test("coerce: the log's lines kept as written (trimmed, the last eight), the hero's bounty, the average stack", () => { const r = coercePokerReading({ phase: "table", tournament: { averageStack: 300000, position: 32, playersLeft: 85 }, hero: { stack: 355050, bounty: 625 }, log: [" Dealer: SynthA takes the pot 1,000. ", 7, "", "Blinds are now 1,800/3,600"], })!; assert.deepEqual(r.log, ["Dealer: SynthA takes the pot 1,000.", "Blinds are now 1,800/3,600"]); assert.equal(r.hero?.bounty, 625); assert.equal(r.tournament?.averageStack, 300000); }); const logged = (t: number, stack: number, log: string[], more: Partial> = {}): R => ({ ...table(t, stack, more), log }); test("the hero's pots from the log, each line once while it scrolls; 25 big blinds or more is a big pot", () => { const rs = [ logged(0, 20000, ["Dealer: SynthHero takes the pot 1,600."]), logged(10, 21600, ["Dealer: SynthHero takes the pot 1,600.", "Dealer: Villain takes the pot 3,000."]), logged(20, 26600, ["Dealer: SynthHero takes the pot 1,600.", "Dealer: Villain takes the pot 3,000.", "Dealer: SynthHero takes the main pot 6,000 with One pair Tens."]), ]; const d = derivePoker(rs, [], { everySeconds: 10, hero: "SynthHero" }); const pots = d.events.filter((e) => e.source === "log").map((e) => [e.t, e.kind, e.detail]); assert.deepEqual(pots, [ [0, "pot", "takes the pot 1,600 (8 BB)"], [20, "big-pot", "takes the main pot 6,000 (30 BB) with One pair Tens"], ]); // Without the hero's name, the log says nothing about the hero. assert.equal(derivePoker(rs, [], { everySeconds: 10 }).events.filter((e) => e.source === "log").length, 0); }); test("a knockout and a bust from the log; the blinds from 'Blinds are now' when the frame did not read them", () => { const noBlinds = (t: number, stack: number, log: string[]): R => ({ t, phase: "table", tournament: { name: "Main Event" }, hero: { stack }, log }); const rs = [ noBlinds(0, 50000, ["Blinds are now 1,800/3,600"]), noBlinds(10, 90000, ["Blinds are now 1,800/3,600", "SynthHero knocked out Villain and wins a $625 bounty"]), noBlinds(20, 0, ["Villain2 knocked out SynthHero"]), ]; const d = derivePoker(rs, [], { everySeconds: 10, hero: "SynthHero" }); assert.deepEqual(d.tournaments[0].series.bigBlind, [[0, 3600]]); assert.deepEqual(d.tournaments[0].series.stackBB, [[0, 13.9], [10, 25], [20, 0]]); assert.deepEqual(d.events.filter((e) => e.source === "log" && e.kind !== "level-up").map((e) => [e.t, e.kind]), [[10, "knockout"], [20, "bust"]]); }); test("after the hero is out, the next table with chips is another tournament, whatever its name", () => { const d = derive([table(0, 5000), table(10, 0, {}, { event: "eliminated" }), { t: 20, phase: "lobby" }, table(30, 1500, { playersLeft: 40, totalPlayers: 40 }), table(40, 1600, { playersLeft: 39, totalPlayers: 40 })]); assert.deepEqual(d.tournaments.map((t) => [t.key, t.from]), [["main event", 0], ["main event#2", 30]]); assert.deepEqual(series(d, "stack", "main event#2"), [[30, 1500], [40, 1600]]); }); test("the stats header: the hero's place and the average stack are series", () => { const d = derive([table(0, 300000, { position: 40, playersLeft: 90, averageStack: 280000 }), table(10, 310000, { position: 32, playersLeft: 85, averageStack: 300000 })]); assert.deepEqual(series(d, "position"), [[0, 40], [10, 32]]); assert.deepEqual(series(d, "averageStack"), [[0, 280000], [10, 300000]]); }); test("players left from the log's finishing places when the frame did not show them", () => { const rs: R[] = [ { t: 0, phase: "table", tournament: { name: "Main Event" }, hero: { stack: 1000 }, log: ["SynthA has finished the tournament in 88th", "SynthB has finished the tournament in 87th"] }, { t: 10, phase: "table", tournament: { name: "Main Event", playersLeft: 80 }, hero: { stack: 1000 }, log: ["SynthB has finished the tournament in 87th"] }, ]; const d = derivePoker(rs, [], { everySeconds: 10 }); // The second frame's 80 has no position beside it (not the header): the log's 87th says 86. assert.deepEqual(d.tournaments[0].series.playersLeft, [[0, 86]]); }); test("a spoken number that fits neither reading either side of a gap is flagged, not drawn", () => { const spoken: SpokenReading[] = [{ t: 400, field: "stack", value: 60000, text: "sixty thousand" }]; const rs = [table(0, 20000), ...Array.from({ length: 59 }, (_, i) => table(10 + i * 10, undefined)), table(600, 20500)]; const d = derive(rs, spoken); assert.deepEqual(series(d, "stack"), [[0, 20000], [600, 20500]]); assert.deepEqual(d.flags.map((f) => [f.t, f.field, f.spoken, f.frame]), [[400, "stack", 60000, 20500]]); });