// The video page's reading correction (release 32, lane R3), over a temp // channel: the reading nearest a second (a duplicate shown as its copy), a // save that keeps only what differs from the model's reading, a misread, a // clear — each re-deriving at once, with no model and no frame. import { test } from "node:test"; import assert from "node:assert/strict"; import { mkdir, mkdtemp, rm } from "node:fs/promises"; import os from "node:os"; import path from "node:path"; import { syntheticPokerHighlights } from "../testing/frames/pokerReadings.mjs"; import { frameScanSidecar, loadFrameScan, type FrameScanRecord } from "../lib/frameScan-server"; import type { Paths } from "../lib/paths"; import { correctFrameReading, frameReadingAt } from "./frameCorrections"; const SLUG = "synth-channel"; const ID = "vid1"; const fx = syntheticPokerHighlights(); const NOW = () => new Date("2026-10-16T12:00:00Z"); async function setup(): Promise<{ paths: Paths; videoDir: string; done: () => Promise }> { const root = await mkdtemp(path.join(os.tmpdir(), "frame-corrections-")); const channelsDir = path.join(root, "channels"); const videoDir = path.join(channelsDir, SLUG, "data", ID); await mkdir(videoDir, { recursive: true }); // 2905 is a duplicate of the read frame at 2900. 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: 299, skippedDuplicates: 1 }, model: { route: "api", model: "claude-haiku-4-5", promptVersion: 3 }, hints: { hero: fx.hero }, costUsd: 0.5, runs: [], readings: fx.readings, duplicates: [[2905, 2900]], spoken: fx.spoken, derived: { tournaments: [], events: [], segments: [], flags: [], misreads: [] }, updatedAt: "2026-10-15T00:00:00Z", } as unknown as FrameScanRecord; await frameScanSidecar("poker").write(videoDir, rec); return { paths: { channelsDir } as Paths, videoDir, done: () => rm(root, { recursive: true, force: true }) }; } test("frameReadingAt: the nearest reading, its fields as read and as used, the seconds either side; refusals", async () => { const { paths, done } = await setup(); try { const r = await frameReadingAt(paths, { slug: SLUG, id: ID, t: 1003 }); assert.ok(r.ok, JSON.stringify(r)); assert.equal(r.view.t, 1000); assert.equal(r.view.prev, 990); assert.equal(r.view.next, 1010); assert.equal(r.view.copyOf, null); const stack = r.view.fields.find((f) => f.path === "hero.stack")!; assert.equal(stack.read, 0); assert.equal(stack.value, 0); assert.equal(stack.corrected, false); const dup = await frameReadingAt(paths, { slug: SLUG, id: ID, t: 2906 }); assert.ok(dup.ok); assert.equal(dup.view.t, 2905); assert.equal(dup.view.copyOf, 2900); assert.equal(dup.view.prev, 2900); assert.match(((await frameReadingAt(paths, { slug: SLUG, id: "nope", t: 1 })) as { error: string }).error, /has no frame-scan\.poker\.json/); assert.match(((await frameReadingAt(paths, { slug: "../x", id: ID, t: 1 })) as { error: string }).error, /channel slug/); } finally { await done(); } }); test("correctFrameReading: only what differs is kept; the derivation uses it at once; a misread; a clear; the model's reading untouched", async () => { const { paths, videoDir, done } = await setup(); try { const before = await frameReadingAt(paths, { slug: SLUG, id: ID, t: 1500 }); assert.ok(before.ok); // The form sends every field; only the stack differs. const set = Object.fromEntries(before.view.fields.map((f) => [f.path, f.value])); set["hero.stack"] = "133,333"; const saved = await correctFrameReading(paths, { slug: SLUG, id: ID, t: 1500, set }, { now: NOW }); assert.ok(saved.ok, JSON.stringify(saved)); assert.deepEqual(saved.correction, { t: 1500, at: "2026-10-16T12:00:00.000Z", by: "person", set: { "hero.stack": 133333 } }); assert.equal(saved.view.fields.find((f) => f.path === "hero.stack")!.corrected, true); let rec = (await loadFrameScan(videoDir, "poker"))!; assert.deepEqual(rec.corrections, [saved.correction]); assert.equal((rec.readings.find((r) => r.t === 1500)!.hero as { stack: number }).stack, 120000, "the model's reading stays as read"); assert.equal(rec.rederivedAt, "2026-10-16T12:00:00.000Z"); assert.ok(rec.derived.tournaments.length >= 1, "derived again"); // The reading at 2900 set aside (its duplicate at 2905 with it). const mis = await correctFrameReading(paths, { slug: SLUG, id: ID, t: 2900, misread: true, note: "not the hero's seat" }, { now: NOW }); assert.ok(mis.ok); assert.equal(mis.view.misread, true); const dup = await frameReadingAt(paths, { slug: SLUG, id: ID, t: 2905 }); assert.ok(dup.ok && dup.view.misread, "a duplicate of a misread is set aside too"); rec = (await loadFrameScan(videoDir, "poker"))!; assert.deepEqual(rec.corrections!.map((c) => c.t), [1500, 2900]); assert.ok(rec.derived.events.every((e) => e.t !== 2900), JSON.stringify(rec.derived.events.filter((e) => e.t >= 2890))); // A save that differs in nothing removes the correction; a clear does too. const same = await correctFrameReading(paths, { slug: SLUG, id: ID, t: 1500, set: { "hero.stack": 120000 } }, { now: NOW }); assert.ok(same.ok && same.correction === null); const cleared = await correctFrameReading(paths, { slug: SLUG, id: ID, t: 2900, clear: true }, { now: NOW }); assert.ok(cleared.ok); rec = (await loadFrameScan(videoDir, "poker"))!; assert.equal(rec.corrections, undefined); // Refusals. assert.match(((await correctFrameReading(paths, { slug: SLUG, id: ID, t: 1234, misread: true })) as { error: string }).error, /no frame was read at 1234 s/); assert.match(((await correctFrameReading(paths, { slug: SLUG, id: ID, t: 1500, set: { "hero.stack": -5 } })) as { error: string }).error, /Hero stack/); assert.match(((await correctFrameReading(paths, { slug: SLUG, id: ID, t: 1500, set: { bogus: 1 } })) as { error: string }).error, /"bogus" is not a field/); } finally { await done(); } });