import test from "node:test"; import assert from "node:assert/strict"; import { applyCompetitorCorrection, setCompetitorRaceStatus, logRaceControlCall, countOfficialWarnings, undoJudgingAdjustment, getJudgeFilteredLog } from "../src/judging_logic.js"; import { buildQualifyingStandings, buildFinalStandings, getQualifyingPointsValue, isHighPointsTable, compareNumberSet, hasQualifyingPrimaryTie, buildQualifyingTieBreakNote } from "../src/timing_logic.js"; function harness() { const result = { passings: [], competitors: { d1: { key: "d1", driverName: "One" }, d2: { key: "d2", driverName: "Two" } }, adjustments: [] }; let id = 0; const deps = { ensureSessionResult: () => result, uid: (p) => `${p}_${++id}`, t: (key) => key, formatLap: (ms) => `${ms}ms`, saveState: () => {}, findPassingByUndoMarker: () => null, recalculateCompetitorFromPassings: () => {} }; return { result, deps, session: { id: "s1", status: "finished" } }; } test("a time penalty after the race keeps its reason in the log", () => { const { result, deps, session } = harness(); applyCompetitorCorrection(session, result.competitors.d1, { timeMsDelta: 10000, reason: "Unfair overtake" }, deps); assert.equal(result.competitors.d1.manualTimeAdjustmentMs, 10000); assert.match(result.adjustments[0].detail, /Unfair overtake/); assert.equal(result.adjustments[0].reason, "Unfair overtake"); }); test("DSQ / DNF are set, logged with the reason, and undone back to what was there", () => { const { result, deps, session } = harness(); assert.equal(setCompetitorRaceStatus(session, result.competitors.d1, "dnf", {}, deps), true); assert.equal(setCompetitorRaceStatus(session, result.competitors.d1, "dsq", { reason: "Car not legal" }, deps), true); assert.equal(result.competitors.d1.raceStatus, "dsq"); assert.equal(result.adjustments[1].detail, "Car not legal"); assert.equal(setCompetitorRaceStatus(session, result.competitors.d1, "dsq", {}, deps), false, "no change, nothing logged"); assert.equal(undoJudgingAdjustment(session, result.adjustments[1].id, deps), true); assert.equal(result.competitors.d1.raceStatus, "dnf"); assert.equal(undoJudgingAdjustment(session, result.adjustments[0].id, deps), true); assert.equal(result.competitors.d1.raceStatus, undefined); }); test("race-control calls are logged; official warnings are counted, undone ones not", () => { const { result, deps, session } = harness(); logRaceControlCall(session, result.competitors.d2, "stop_and_go", { reason: "Hit a car" }, deps); const first = logRaceControlCall(session, result.competitors.d2, "official_warning", {}, deps); logRaceControlCall(session, result.competitors.d2, "official_warning", {}, deps); assert.equal(logRaceControlCall(session, result.competitors.d2, "nonsense", {}, deps), null); assert.equal(countOfficialWarnings(result, "d2"), 2); undoJudgingAdjustment(session, first.id, deps); assert.equal(countOfficialWarnings(result, "d2"), 1); assert.equal(getJudgeFilteredLog(result.adjustments, "calls").length, 3); }); const qualDeps = (rowsBySession, entrants) => ({ getSessionsForEvent: () => Object.keys(rowsBySession).map((id) => ({ id, type: "qualification", name: id, status: "finished" })), buildLeaderboard: (session) => rowsBySession[session.id], getSessionEntrants: () => entrants, isHighPointsTable, getQualifyingPointsValue, compareNumberSet, hasQualifyingPrimaryTie, buildQualifyingTieBreakNote: (row, tieBreak) => buildQualifyingTieBreakNote(row, tieBreak, { t: (key) => key, formatLap: (ms) => `${ms}` }), formatLap: (ms) => `${ms}`, formatRaceClock: (ms) => `${ms}`, t: (key) => key, }); test("EFRA: a DSQ in a qualifying round scores last-place points there, its laps don't count", () => { const entrants = [{ id: "d1", name: "One" }, { id: "d2", name: "Two" }, { id: "d3", name: "Three" }]; const rows = { q1: [ { driverId: "d2", driverName: "Two", comparisonMs: 15000, bestLapMs: 15000, resultDisplay: "a" }, { driverId: "d3", driverName: "Three", comparisonMs: 16000, bestLapMs: 16000, resultDisplay: "b" }, { driverId: "d1", driverName: "One", comparisonMs: Number.MAX_SAFE_INTEGER, bestLapMs: 14000, resultDisplay: "DSQ", raceStatus: "dsq" }, ], }; const standings = buildQualifyingStandings({ id: "e", raceConfig: { qualifyingPointsTable: "rank_low" } }, qualDeps(rows, entrants)); assert.deepEqual(standings.map((row) => row.driverId), ["d2", "d3", "d1"]); assert.equal(standings[2].bestSingleLapMs, Number.MAX_SAFE_INTEGER, "the disqualified run's fastest lap doesn't count"); }); test("a DSQ in a final is classified last in it, behind a driver who did not start", () => { const sessions = [{ id: "a", type: "final", name: "A Final", status: "finished" }]; const rows = [ { driverId: "d1", driverName: "One", totalElapsedMs: 300000, raceStatus: "dsq" }, { driverId: "d2", driverName: "Two", totalElapsedMs: 301000 }, ]; const standings = buildFinalStandings({ id: "e", raceConfig: {} }, { getSessionsForEvent: () => sessions, buildLeaderboard: () => rows, getSessionEntrants: () => [{ id: "d1", name: "One" }, { id: "d2", name: "Two" }, { id: "d3", name: "Three" }], t: (key) => key, }); assert.deepEqual(standings.map((row) => row.driverId), ["d2", "d3", "d1"]); });