Fix qualifying/finals/bump-up/judging/pause-timing bugs

A thorough audit of practice/qualifying/heats/finals, bump-up,
judging and pause/timer code (requested by the user) found several
real, concrete bugs, fixed together:

- Qualifying reseed sorted heats as text ("H10" before "H2"),
  scrambling grids at 10+ heats; now sorts numerically and reports a
  clear warning instead of silently dropping/ghosting heats when the
  participant count changes.
- A driver with zero laps in a finished heat/final (DNS) vanished
  entirely - not just from that round, but silently from every future
  reseeded round and the final grid. Now backfilled as a real,
  last-place "DNS" row.
- Bump-up did nothing at all with "reserve bump slots" off (mains
  were already seeded full, so the old capacity check always
  blocked it), and a promoted driver stayed double-entered in their
  old main's upcoming leg. An oversized bump count could also blow
  the main-letter naming past "Z". All three fixed.
- Judging undo restored an absolute snapshot instead of reversing its
  own delta - undoing two penalties out of order could leave a live
  penalty behind two "undone" log entries. Now always nets correctly
  regardless of undo order.
- A competitor's elapsed time didn't exclude paused time (only the
  session clock did), making a paused heat's results incomparable to
  an unpaused one in qualifying ranking. Finishing while paused also
  dropped the final pause segment, so the displayed elapsed time kept
  growing after finish. Both fixed.

Verified with new unit tests for every fix (including a new
session_control.test.js - that file had zero prior coverage) and a
real headless-browser run confirming the judging-undo and bump-up
fixes end to end through the actual UI.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
larssandandClaude Sonnet 5 committed 2026-09-10 17:41:10 +02:00
1 parent 15b574a37d
commit f0838bc5db
14 files changed
+464 -36

No files matched your search

+48 -7
View File
@@ -82,7 +82,12 @@ export function getSessionTiming(session, nowTs = Date.now(), { getSessionTarget
const targetMs = getSessionTargetMs(session);
const startedAt = Number(session?.startedAt || 0);
const pausedAt = Number(session?.pausedAt || 0) || 0;
const clockNow = pausedAt || nowTs;
const endedAt = Number(session?.endedAt || 0) || 0;
// A finished session's elapsed time is a fixed historical fact - without this, reopening
// or simply leaving a finished session's timing view open would show its "elapsed" time
// still counting up against the real clock, using how long ago it finished rather than
// how long the session itself actually ran.
const clockNow = session?.status === "finished" && endedAt ? endedAt : (pausedAt || nowTs);
const totalPausedMs = Math.max(0, Number(session?.totalPausedMs || 0) || 0);
const elapsedMs = startedAt ? Math.max(0, clockNow - startedAt - totalPausedMs) : 0;
const followUpMs = Math.max(0, Number(session?.followUpSec || 0) || 0) * 1000;
@@ -172,8 +177,15 @@ export function buildLeaderboard(session, { ensureSessionResult, getSessionTarge
const latestAnyPassing = allPassings.length ? allPassings[allPassings.length - 1] : null;
const latestPassing = passings.length ? passings[passings.length - 1] : null;
const lastPassingTs = latestPassing ? Number(latestPassing.timestamp || 0) : Number(row.lastTimestamp || 0) || 0;
// Excludes paused wall-clock time, the same way the session's own countdown already
// does (getSessionTiming) - otherwise a heat that had a track call isn't comparable to
// one that didn't, even though nobody actually lost race time to the stoppage. Correct
// as long as passings aren't recorded while paused (they aren't - the decoder ignores
// them), so a driver's last pass is always from before or after the full pause, never
// mid-pause.
const totalPausedMs = Math.max(0, Number(session?.totalPausedMs || 0) || 0);
const rawElapsedMs = lastPassingTs
? Math.max(0, lastPassingTs - Number(row.startTimestamp || session.startedAt || lastPassingTs))
? Math.max(0, lastPassingTs - Number(row.startTimestamp || session.startedAt || lastPassingTs) - totalPausedMs)
: getCompetitorElapsedMs(session, row);
const manualLapAdjustment = Number(row.manualLapAdjustment || 0) || 0;
const manualTimeAdjustmentMs = Number(row.manualTimeAdjustmentMs || 0) || 0;
@@ -350,7 +362,8 @@ export function getCompetitorElapsedMs(session, row) {
if (!startTs || !row?.lastTimestamp) {
return 0;
}
return Math.max(0, row.lastTimestamp - startTs);
const totalPausedMs = Math.max(0, Number(session?.totalPausedMs || 0) || 0);
return Math.max(0, row.lastTimestamp - startTs - totalPausedMs);
}
@@ -537,8 +550,26 @@ export function buildQualifyingStandings(event, { getSessionsForEvent, buildLead
sessions.forEach((session) => {
const rows = buildLeaderboard(session);
const entrantCount = Math.max(rows.length, getSessionEntrants(session).length || 0);
rows.forEach((row, index) => {
const entrants = getSessionEntrants(session);
const entrantCount = Math.max(rows.length, entrants.length || 0);
// A driver assigned to this heat who never recorded a single valid passing (dead
// battery, transponder issue, no-show) has no entry in buildLeaderboard's rows at all -
// without this they'd silently vanish not just from this round's standings, but from
// every future round they'd otherwise be reseeded into, and from the final grid
// entirely. Only backfilled once the heat has actually finished - a still-running or
// not-yet-started heat's assigned drivers simply haven't raced yet, which is different.
const rowsWithDns = session.status === "finished"
? [
...rows,
...entrants
.filter((driver) => !rows.some((row) => (row.driverId || row.key) === driver.id))
// bestLapMs is deliberately omitted (not 0) - Number.isFinite(0) is true, and a
// "0ms" lap would otherwise count as the fastest single lap of the round for
// the best_lap tie-break, ahead of every driver who actually raced.
.map((driver) => ({ driverId: driver.id, driverName: driver.name, comparisonMs: Number.MAX_SAFE_INTEGER, resultDisplay: t("events.dns") })),
]
: rows;
rowsWithDns.forEach((row, index) => {
const key = row.driverId || row.key;
if (!competitorMap.has(key)) {
competitorMap.set(key, {
@@ -867,20 +898,30 @@ export function ensureSessionDriverOrder(session, { getSessionEntrants }) {
export function buildFinalStandings(event, { getSessionsForEvent, buildLeaderboard }) {
export function buildFinalStandings(event, { getSessionsForEvent, buildLeaderboard, getSessionEntrants, t }) {
const sessions = getSessionsForEvent(event.id).filter((session) => session.type === "final");
const groupedByMain = new Map();
const countedFinalLegs = Math.max(1, Number(event.raceConfig?.countedFinalLegs || 1) || 1);
sessions.forEach((session) => {
const rows = buildLeaderboard(session);
// See the identical note in buildQualifyingStandings above: a driver assigned to this
// final who never recorded a valid passing must not vanish from the results entirely.
const rowsWithDns = session.status === "finished"
? [
...rows,
...getSessionEntrants(session)
.filter((driver) => !rows.some((row) => (row.driverId || row.key) === driver.id))
.map((driver) => ({ driverId: driver.id, driverName: driver.name, totalElapsedMs: Number.MAX_SAFE_INTEGER, resultDisplay: t("events.dns") })),
]
: rows;
const mainMatch = String(session.name || "").match(/^([A-Z])/i);
const mainKey = mainMatch ? mainMatch[1].toUpperCase() : "A";
if (!groupedByMain.has(mainKey)) {
groupedByMain.set(mainKey, new Map());
}
const competitorMap = groupedByMain.get(mainKey);
rows.forEach((row, index) => {
rowsWithDns.forEach((row, index) => {
const key = row.driverId || row.key;
if (!competitorMap.has(key)) {
competitorMap.set(key, {