import test from "node:test"; import assert from "node:assert/strict"; import { isSuspectLap, getSuspectReferenceLaps, listPendingSuspectLaps, releaseSettledSuspectLaps, SUSPECT_LAP_RATIO, MIN_REFERENCE_LAPS } from "../src/suspect_laps_logic.js"; import { syncTrainingLedger } from "../src/training_logic.js"; const lap = (lapMs, extra = {}) => ({ competitorKey: "d1", lapMs, timestamp: lapMs, validLap: true, ...extra }); test("a lap well under 85 % of the driver's own median is suspect - only once there are 5 normal laps", () => { assert.equal(SUSPECT_LAP_RATIO, 0.85); assert.equal(MIN_REFERENCE_LAPS, 5); const reference = [20000, 20500, 19800, 20200, 21000]; // median 20 200 -> limit 17 170 assert.equal(isSuspectLap(17000, reference), true, "a shortcut"); assert.equal(isSuspectLap(17500, reference), false, "a genuinely good lap"); assert.equal(isSuspectLap(12000, reference.slice(0, 4)), false, "too little to compare with yet"); }); test("the reference is the driver's own recent laps on the same layout - flagged ones included", () => { const passings = [ lap(20000), lap(20000), lap(30000, { stintBoundary: true }), lap(9000, { validLap: false }), lap(15000, { suspect: true }), lap(25000, { competitorKey: "other" }), lap(40000, { trackLayoutId: "old" }), lap(21000), ]; assert.deepEqual(getSuspectReferenceLaps(passings, "d1", ""), [20000, 20000, 15000, 21000]); }); test("a driver who gets faster during the day is not flagged lap after lap", () => { // Slow warm-up laps around 23 s, then a real pace around 19 s (84 % of 23 s). const passings = [23000, 23500, 22800, 23200, 24000, 23100].map((lapMs, index) => lap(lapMs, { timestamp: index })); const flagged = []; [19000, 19200, 18900, 19100, 18800, 19300, 19000, 18900].forEach((lapMs, index) => { const suspect = isSuspectLap(lapMs, getSuspectReferenceLaps(passings, "d1", "")); passings.push(lap(lapMs, { timestamp: 100 + index, suspect })); if (suspect) flagged.push(lapMs); releaseSettledSuspectLaps(passings, "d1", ""); }); assert.ok(flagged.length <= 5, `only the first laps at the new pace look odd (${flagged.length})`); assert.deepEqual(listPendingSuspectLaps({ passings }), [], "and they lose the flag once the pace is clear"); }); test("a real shortcut among normal laps stays flagged", () => { const passings = [20000, 20500, 19800, 20200, 21000, 20300].map((lapMs, index) => lap(lapMs, { timestamp: index })); passings.push(lap(15000, { timestamp: 10, suspect: isSuspectLap(15000, getSuspectReferenceLaps(passings, "d1", "")) })); passings.push(lap(20100, { timestamp: 11 })); releaseSettledSuspectLaps(passings, "d1", ""); assert.deepEqual(listPendingSuspectLaps({ passings }).map((passing) => passing.lapMs), [15000]); }); test("pending suspect laps are listed newest first; a removed one is no longer pending", () => { const result = { passings: [lap(15000, { suspect: true, timestamp: 1 }), lap(16000, { suspect: true, validLap: false, timestamp: 2 }), lap(14000, { suspect: true, timestamp: 3 })] }; assert.deepEqual(listPendingSuspectLaps(result).map((passing) => passing.lapMs), [14000, 15000]); }); test("a suspect lap counts in the training stats until it is removed", () => { let n = 0; const uid = (prefix) => `${prefix}_${++n}`; const state = { sessions: [{ id: "op", name: "Open Practice", eventId: "e" }], events: [{ id: "e", name: "Club" }], resultsBySession: { op: { passings: [{ ...lap(20000), driverId: "d1", timestamp: 1000 }, { ...lap(15000, { suspect: true }), driverId: "d1", timestamp: 2000 }] } }, }; assert.deepEqual(syncTrainingLedger({}, state, { uid }).ledger.map((entry) => entry.lapMs), [20000, 15000]); Object.assign(state.resultsBySession.op.passings[1], { validLap: false, invalidReason: "manual_invalid" }); assert.deepEqual(syncTrainingLedger({}, state, { uid }).ledger.map((entry) => entry.lapMs), [20000]); });