// A person's corrections of frame readings (release 32, lane R3): checked by // field kind, laid over the readings by the derivation (the person's value // wins; a misread is set aside, and its duplicates with it), kept across a // re-derive, and never written over the model's readings. import { test } from "node:test"; import assert from "node:assert/strict"; import { syntheticPokerHighlights } from "../../testing/frames/pokerReadings.mjs"; import { derivePoker, type PokerReading } from "./poker"; import { deriveOf, rederiveRecord } from "./scan"; import { CorrectionInputError, applyCorrections, checkCorrectionSet, coerceCorrections, upsertCorrection, type ReadingCorrection, } from "./corrections"; import { coerceFrameScan, type FrameScanRecord } from "../frameScan-server"; type R = PokerReading & { t: number }; const fx = syntheticPokerHighlights(); const AT = "2026-10-16T00:00:00.000Z"; const person = (t: number, more: Partial): ReadingCorrection => ({ t, at: AT, by: "person", ...more }); test("checkCorrectionSet: each field by its kind; unknown fields and bad values refused with the field's name", () => { assert.deepEqual(checkCorrectionSet("poker", { "hero.stack": "12,400", "tournament.playersLeft": 85, phase: "table", "hero.cards": "as kd", log: "a\n\n b " }), { "hero.stack": 12400, "tournament.playersLeft": 85, phase: "table", "hero.cards": ["As", "Kd"], log: ["a", "b"], }); assert.deepEqual(checkCorrectionSet("poker", { "hand.pot": "", "tournament.name": " Synth Cup " }), { "hand.pot": null, "tournament.name": "Synth Cup" }); assert.equal(checkCorrectionSet("poker", {}), undefined); assert.throws(() => checkCorrectionSet("poker", { "hero.name": "x" }), CorrectionInputError); assert.throws(() => checkCorrectionSet("poker", { "hero.stack": -1 }), /Hero stack: a number/); assert.throws(() => checkCorrectionSet("poker", { "tournament.playersLeft": 8.5 }), /Players left: a whole number/); assert.throws(() => checkCorrectionSet("poker", { phase: null }), /Phase: is required/); assert.throws(() => checkCorrectionSet("poker", { phase: "dealing" }), /Phase: one of/); assert.throws(() => checkCorrectionSet("poker", { "hero.cards": "Ax" }), /"Ax" is not a card/); }); test("applyCorrections: a set field takes the person's value, null removes it, a misread is dropped; idempotent", () => { const rs: R[] = [ { t: 0, phase: "table", hero: { stack: 100 }, tournament: { playersLeft: 9 } }, { t: 10, phase: "table", hero: { stack: 999999 } }, { t: 20, phase: "lobby" }, ]; const cs = [person(0, { set: { "tournament.playersLeft": null, "tournament.blinds.bb": 200 } }), person(10, { misread: true }), person(20, { set: { phase: "break" } })]; const once = applyCorrections(rs, cs); assert.deepEqual(once, [ { t: 0, phase: "table", hero: { stack: 100 }, tournament: { blinds: { bb: 200 } } }, { t: 20, phase: "break" }, ]); assert.deepEqual(applyCorrections(once, cs), once); // The model's readings are untouched. assert.deepEqual(rs[0].tournament, { playersLeft: 9 }); // A cleared field that leaves its object empty takes the object with it. assert.deepEqual(applyCorrections([{ t: 5, phase: "table", hero: { stack: 1 } }], [person(5, { set: { "hero.stack": null } })]), [{ t: 5, phase: "table" }]); }); test("deriveOf: the derivation over the corrected readings is the derivation of the readings as the person says they are", () => { const readings = fx.readings as R[]; const cs: ReadingCorrection[] = []; for (let t = 1500; t < 1600; t += 10) cs.push(person(t, { set: { "hero.stack": 133333 } })); cs.push(person(2900, { misread: true })); const byHand = readings .filter((r) => r.t !== 2900) .map((r) => (r.t >= 1500 && r.t < 1600 ? { ...r, hero: { ...(r.hero ?? {}), stack: 133333 } } : r)); const o = { everySeconds: 10, hero: fx.hero }; const got = deriveOf("poker", readings, [], fx.spoken, { ...o, corrections: cs }); assert.deepEqual(got, derivePoker(byHand, fx.spoken, o)); assert.notDeepEqual(got, deriveOf("poker", readings, [], fx.spoken, o)); assert.ok(got.tournaments[0].series.stack.some(([t, v]) => t === 1500 && v === 133333), "the person's stack is in the series"); }); test("deriveOf: a duplicate copies the corrected reading; a misread is copied nowhere; a correction at the duplicate's own second applies to it", () => { const rs: R[] = [ { t: 0, phase: "table", hero: { stack: 100 } }, { t: 10, phase: "table", hero: { stack: 200 } }, ]; const dups: [number, number][] = [[20, 10], [30, 0]]; const seen: R[][] = []; const spy = (cs: ReadingCorrection[]) => { const corrected = applyCorrections(rs, cs); const byT = new Map(corrected.map((r) => [r.t, r])); const copies = dups.flatMap(([t, s]) => (byT.get(s) ? [{ ...byT.get(s)!, t }] : [])); seen.push(applyCorrections([...corrected, ...copies], cs).sort((a, b) => a.t - b.t)); return deriveOf("poker", rs, dups, [], { everySeconds: 10, corrections: cs }); }; const a = spy([person(10, { set: { "hero.stack": 250 } }), person(0, { misread: true }), person(20, { set: { "hero.stack": 260 } })]); assert.deepEqual(seen[0], [ { t: 10, phase: "table", hero: { stack: 250 } }, { t: 20, phase: "table", hero: { stack: 260 } }, ]); assert.deepEqual(a, derivePoker(seen[0], [], { everySeconds: 10 })); }); test("the sidecar keeps the corrections (one per second, the newest) and a re-derive applies them", () => { const rec = { format: "archilyzer-frame-scan", version: 1, profile: "poker", source: { savedVideo: "source-media.mp4", durationSeconds: 3000 }, sampling: { everySeconds: 10, from: 0, to: 3000, width: 1280, keyframesOnly: true, frames: 300, kept: 300, skippedDuplicates: 0 }, model: { route: "api", model: "claude-haiku-4-5", promptVersion: 3 }, hints: { hero: fx.hero }, costUsd: 0, runs: [], readings: fx.readings, duplicates: [], spoken: fx.spoken, derived: { tournaments: [], events: [], segments: [], flags: [], misreads: [] }, updatedAt: AT, corrections: [person(2900, { set: { "hero.stack": 5 } }), person(2900, { misread: true }), { t: "x" }, { t: 5, at: AT, by: "person" }], }; const coerced = coerceFrameScan(rec) as FrameScanRecord; assert.deepEqual(coerced.corrections, [person(2900, { misread: true })]); const out = rederiveRecord(coerced); assert.deepEqual(out.corrections, coerced.corrections); assert.deepEqual(out.derived, derivePoker((fx.readings as R[]).filter((r) => r.t !== 2900), fx.spoken, { everySeconds: 10, durationSeconds: 3000, hero: fx.hero })); assert.equal(out.readings.length, fx.readings.length, "the model's readings are kept as read"); assert.deepEqual(coerceCorrections(undefined), []); assert.deepEqual(upsertCorrection([person(1, { misread: true }), person(3, { misread: true })], 1, null), [person(3, { misread: true })]); });