import test from "node:test"; import assert from "node:assert/strict"; import { advanceSessionReplay, buildSessionReplaySnapshot, createSessionReplayRuntime, loadSessionReplay, pauseSessionReplay, playSessionReplay, resetSessionReplay, setSessionReplaySpeed, } from "../src/session_replay.js"; const passings = [ { timestamp: 1000, competitorKey: "a", driverName: "Alpha", transponder: "1", lapMs: 0, validLap: true }, { timestamp: 1200, competitorKey: "b", driverName: "Beta", transponder: "2", lapMs: 0, validLap: true }, { timestamp: 11000, competitorKey: "a", driverName: "Alpha", transponder: "1", lapMs: 10000, validLap: true }, { timestamp: 11700, competitorKey: "b", driverName: "Beta", transponder: "2", lapMs: 10500, validLap: true }, { timestamp: 20500, competitorKey: "a", driverName: "Alpha", transponder: "1", lapMs: 9500, validLap: true }, { timestamp: 21000, competitorKey: "b", driverName: "Beta", transponder: "2", lapMs: 9300, validLap: false }, ]; test("session replay builds an isolated race leaderboard at the selected time", () => { const runtime = createSessionReplayRuntime(); loadSessionReplay(runtime, { id: "race_1", name: "A final", type: "final" }, { passings: [...passings].reverse() }); runtime.elapsedMs = 15000; const snapshot = buildSessionReplaySnapshot(runtime); assert.equal(snapshot.visiblePassings.length, 4); assert.deepEqual(snapshot.leaderboard.map((row) => [row.driverName, row.laps]), [["Alpha", 1], ["Beta", 1]]); assert.equal(snapshot.latestPassing.driverName, "Beta"); assert.equal(runtime.passings.length, 6); }); test("practice replay ranks best valid lap and ignores invalid laps", () => { const runtime = createSessionReplayRuntime(); loadSessionReplay(runtime, { id: "practice_1", name: "Practice", type: "practice" }, { passings }); runtime.elapsedMs = runtime.durationMs; const snapshot = buildSessionReplaySnapshot(runtime); assert.equal(snapshot.leaderboard[0].driverName, "Alpha"); assert.equal(snapshot.leaderboard[0].bestLapMs, 9500); assert.equal(snapshot.leaderboard[1].bestLapMs, 10500); assert.equal(snapshot.leaderboard[1].laps, 1); }); test("session replay play, speed, pause, finish, and reset are deterministic", () => { const runtime = createSessionReplayRuntime(); loadSessionReplay(runtime, { id: "race_1", type: "final" }, { passings }); assert.equal(playSessionReplay(runtime, 100), true); setSessionReplaySpeed(runtime, 5, 1100); assert.equal(runtime.elapsedMs, 1000); advanceSessionReplay(runtime, 2100); assert.equal(runtime.elapsedMs, 6000); assert.equal(pauseSessionReplay(runtime, 2200), true); assert.equal(runtime.status, "paused"); playSessionReplay(runtime, 3000); advanceSessionReplay(runtime, 10000); assert.equal(runtime.status, "finished"); assert.equal(runtime.elapsedMs, runtime.durationMs); resetSessionReplay(runtime); assert.equal(runtime.status, "paused"); assert.equal(runtime.elapsedMs, 0); });