feat(practice): open practice settings with ranking by best N in a row or best X minutes (1.68.0)

This commit is contained in:
larssand committed 2026-11-05 22:38:33 +01:00
1 parent 8a8b2417fd
commit a7d2d0b9ae
11 files changed
+243 -7

No files matched your search

+54
View File
@@ -0,0 +1,54 @@
import assert from "node:assert/strict";
// Open practice settings (Events → Settings on an Open Practice row): the ranking chosen there
// re-sorts the list everywhere - here the overlay - and the setting is saved on the session.
export async function runPracticeRankingSmoke({ evaluate, navigate, savedState, waitFor, base }) {
const patchState = async (patch) => {
const current = await savedState();
Object.assign(current, patch);
const response = await fetch(base + "/api/state", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(current) });
assert.equal(response.ok, true);
};
const seeded = await savedState();
const event = { id: "rk_event", name: "RK Practice", classId: "", mode: "race", raceConfig: { practiceOnly: true } };
const startedAt = Date.now() - 600000;
const practice = { id: "rk_practice", eventId: event.id, name: "RK Open Practice", type: "open_practice", durationMin: 600, status: "running", startedAt };
// Fast: one quick lap, then slow. Steady: three even laps - better over 2 laps in a row.
const laps = (id, tp, lapTimes) => {
let ts = startedAt + 5000;
return [{ driverId: id, driverName: id, competitorKey: id, transponder: tp, lapMs: 0, timestamp: ts, validLap: true },
...lapTimes.map((lapMs) => { ts += lapMs; return { driverId: id, driverName: id, competitorKey: id, transponder: tp, lapMs, timestamp: ts, validLap: true }; })];
};
const competitor = (id, tp, lapTimes) => ({ key: id, driverId: id, driverName: id, transponder: tp, laps: lapTimes.length, bestLapMs: Math.min(...lapTimes), lastLapMs: lapTimes.at(-1), startTimestamp: startedAt });
await patchState({
drivers: [...seeded.drivers, { id: "RK Fast", name: "RK Fast", classId: "", transponder: "8440001" }, { id: "RK Steady", name: "RK Steady", classId: "", transponder: "8440002" }],
events: [...seeded.events, event],
sessions: [...seeded.sessions.map((session) => (["running", "paused"].includes(session.status) ? { ...session, status: "finished", endedAt: Date.now() } : session)), practice],
resultsBySession: {
...seeded.resultsBySession,
[practice.id]: {
passings: [...laps("RK Fast", "8440001", [17000, 25000, 25000]), ...laps("RK Steady", "8440002", [18000, 18000, 18000])].sort((a, b) => a.timestamp - b.timestamp),
competitors: { "RK Fast": competitor("RK Fast", "8440001", [17000, 25000, 25000]), "RK Steady": competitor("RK Steady", "8440002", [18000, 18000, 18000]) },
},
},
activeSessionId: practice.id,
});
const firstOnOverlay = () => evaluate(`document.querySelector("[data-overlay-competitor]")?.textContent || ""`);
await navigate("overlay", "overlayFullscreen");
await waitFor(async () => /RK Fast/.test(await firstOnOverlay()), "best lap ranks the one quick lap first");
await navigate("race_setup", "raceWizardStepForm");
await evaluate(`document.getElementById("event-edit-rk_event").click()`);
await waitFor(async () => evaluate(`Boolean(document.querySelector('#eventEditForm [name="practiceRanking"]'))`), "settings window with ranking");
await evaluate(`(() => { const form = document.getElementById("eventEditForm"); form.elements.date.value = "2026-01-01"; form.elements.practiceRanking.value = "consecutive"; form.elements.practiceRankLaps.value = "2"; form.elements.minLapSec.value = "10"; form.elements.maxLapSec.value = "40"; form.requestSubmit(); })()`);
await waitFor(async () => (await savedState()).sessions.some((session) => session.id === practice.id && session.practiceRanking === "consecutive" && session.practiceRankLaps === 2), "ranking saved on the practice");
const saved = (await savedState()).events.find((item) => item.id === event.id);
assert.equal(saved.raceConfig.minLapMs, 10000);
assert.equal(saved.raceConfig.maxLapMs, 40000);
await navigate("overlay", "overlayFullscreen");
await waitFor(async () => /RK Steady/.test(await firstOnOverlay()), "best 2 in a row ranks the steady driver first");
assert.match(await firstOnOverlay(), /2 (laps|varv)/);
console.log("PASS open practice settings: ranking by best N laps in a row re-sorts the overlay");
}
+2
View File
@@ -12,6 +12,7 @@ import { runTtsSettingsSmoke } from "./browser_tts_settings.js";
import { runTrackLayoutsSmoke } from "./browser_track_layouts.js";
import { runPrologueSmoke } from "./browser_prologue.js";
import { runPracticeResetSmoke } from "./browser_practice_reset.js";
import { runPracticeRankingSmoke } from "./browser_practice_ranking.js";
import { runSuspectLapsSmoke } from "./browser_suspect_laps.js";
import { runOverlayDriverLinkSmoke } from "./browser_overlay_driver_link.js";
import { runProfileClassSmoke } from "./browser_profile_class.js";
@@ -166,6 +167,7 @@ try {
await runTrackLayoutsSmoke({ evaluate, navigate, savedState, waitFor, base, dialogs });
await runPrologueSmoke({ evaluate, navigate, savedState, waitFor, base, dialogs });
await runPracticeResetSmoke({ evaluate, navigate, savedState, waitFor, base, dialogs });
await runPracticeRankingSmoke({ evaluate, navigate, savedState, waitFor, base });
await runSuspectLapsSmoke({ evaluate, navigate, savedState, waitFor, base, dialogs });
await runOverlayDriverLinkSmoke({ evaluate, navigate, savedState, waitFor, base, send });
await runDecoderClockSmoke({ evaluate, navigate, savedState, waitFor, base });
+35
View File
@@ -0,0 +1,35 @@
import test from "node:test";
import assert from "node:assert/strict";
import { getPracticeRankMetric, formatPracticeRankGap } from "../src/timing_logic.js";
// Passings 1 s apart start; each lap's timestamp follows the previous by its lapMs.
function run(startTs, laps) {
const passings = [{ timestamp: startTs, lapMs: 0 }];
let ts = startTs;
laps.forEach((lapMs) => { ts += lapMs; passings.push({ timestamp: ts, lapMs }); });
return passings;
}
test("best lap ranking gives no metric", () => {
assert.equal(getPracticeRankMetric({ practiceRanking: "best_lap" }, run(0, [18000, 17000])), null);
});
test("best N in a row stays inside one run (a stop in the pits splits)", () => {
const passings = [...run(0, [18000, 17000, 30000]), ...run(10_000_000, [16000, 16000])];
const metric = getPracticeRankMetric({ practiceRanking: "consecutive", practiceRankLaps: 3 }, passings);
assert.equal(metric.totalMs, 65000);
assert.equal(metric.lapCount, 3);
assert.equal(getPracticeRankMetric({ practiceRanking: "consecutive", practiceRankLaps: 2 }, passings).totalMs, 32000);
assert.equal(getPracticeRankMetric({ practiceRanking: "consecutive", practiceRankLaps: 4 }, passings), null);
});
test("best X minutes: most laps inside the window, then the shorter time", () => {
const fast = getPracticeRankMetric({ practiceRanking: "window", practiceRankMin: 1 }, run(0, [20000, 20000, 19000, 25000]));
assert.deepEqual([fast.lapCount, fast.totalMs], [3, 59000]);
const slow = getPracticeRankMetric({ practiceRanking: "window", practiceRankMin: 1 }, run(0, [21000, 21000, 21000]));
assert.deepEqual([slow.lapCount, slow.totalMs], [2, 42000]);
assert.ok(fast.comparableMs < slow.comparableMs, "more laps ranks first");
assert.equal(formatPracticeRankGap({ practiceMetric: slow }, { practiceMetric: fast }), "+1L");
const tie = getPracticeRankMetric({ practiceRanking: "window", practiceRankMin: 1 }, run(0, [20000, 20000, 19500]));
assert.equal(formatPracticeRankGap({ practiceMetric: tie }, { practiceMetric: fast }), "+0.500s");
});
+1
View File
@@ -1,4 +1,5 @@
import "./auth.test.js";
import "./practice_rank.test.js";
import "./team_race_logic.test.js";
import "./race_overview.test.js";
import "./leader_call_logic.test.js";