Files
Live_RC/tests/browser_race_day_quick.js
T

111 lines
9.5 KiB
JavaScript

import assert from "node:assert/strict";
// The easy path for a club evening: the Dashboard's "Start practice" creates and starts an
// Open Practice on Race Day; Race Day's quick actions add a track layout (the running practice
// moves onto it) and switch sessions; Timing no longer has the run controls, only a link.
export async function runRaceDayQuickSmoke({ evaluate, navigate, savedState, waitFor, base, dialogs }) {
// The layout prompts are expected here; drop them afterwards so later "no unexpected dialog" checks stay meaningful.
const dialogsAtStart = dialogs.length;
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();
// No Open Practice to continue, and nothing running: "Start practice" must create one.
await patchState({
sessions: seeded.sessions.map((session) => (session.type === "open_practice" || ["running", "paused"].includes(session.status) ? { ...session, status: "finished", endedAt: Date.now() } : session)),
events: [...seeded.events, { id: "rq_event", name: "RQ Race", mode: "race", raceConfig: {} }],
activeSessionId: "",
trackLayouts: [],
});
const before = new Set((await savedState()).sessions.map((session) => session.id));
let created = null;
try {
await navigate("dashboard", "dashboardQuickPractice");
await evaluate(`document.getElementById("dashboardQuickPractice").click()`);
await waitFor(async () => (await savedState()).sessions.some((session) => !before.has(session.id) && session.type === "open_practice" && session.status === "running"), "a new Open Practice is created and running");
created = (await savedState()).sessions.find((session) => !before.has(session.id) && session.type === "open_practice");
await waitFor(async () => (await savedState()).activeSessionId === created.id, "and selected");
await waitFor(async () => (await evaluate(`location.search`)).includes("race_day") && Boolean(await evaluate(`document.getElementById("raceDayQuickPractice")`)), "on Race Day");
await waitFor(async () => (await evaluate(`document.getElementById("raceDayQuickPractice").textContent`)).includes(created.name), "the quick button shows the running practice");
assert.equal(await evaluate(`Boolean(document.getElementById("raceDayNewPracticeDay"))`), true, "New practice day is on Race Day for a practice");
console.log("PASS Start practice on the Dashboard creates, starts and opens an Open Practice on Race Day");
await evaluate(`document.querySelector('.race-day-quick [data-track-layout-new]').click()`);
await waitFor(async () => ((await savedState()).trackLayouts || []).length === 1, "a layout is created from the quick actions");
const layoutId = (await savedState()).trackLayouts[0].id;
await waitFor(async () => (await savedState()).sessions.find((session) => session.id === created.id)?.trackLayoutId === layoutId, "the running practice moved onto it");
console.log("PASS New track layout from Race Day's quick actions moves the running practice onto it");
await patchState({ sessions: [...(await savedState()).sessions, { id: "rq_heat", eventId: "rq_event", name: "RQ Heat 1", type: "heat", status: "ready", durationMin: 5 }] });
await navigate("race_day", "raceDaySessionSelect");
await evaluate(`(() => { const select = document.getElementById("raceDaySessionSelect"); select.value = "rq_heat"; select.dispatchEvent(new Event("change")); })()`);
await waitFor(async () => (await savedState()).activeSessionId === "rq_heat", "the session picker switches session");
assert.equal(await evaluate(`Boolean(document.getElementById("raceDayNewPracticeDay"))`), false, "no New practice day for a heat");
assert.equal(await evaluate(`Boolean(document.getElementById("raceDayResetSession"))`), false, "nothing to run again for a heat not yet run");
console.log("PASS Race Day's session picker switches the selected session");
// A finished heat can be picked again and reset to run again: clock back to zero, no laps.
const ranAt = Date.now() - 600000;
await patchState({
sessions: [...(await savedState()).sessions, { id: "rq_done", eventId: "rq_event", name: "RQ Heat 2", type: "qualification", status: "finished", durationMin: 5, startedAt: ranAt, endedAt: ranAt + 300000, finishedByTimer: true }],
resultsBySession: { ...(await savedState()).resultsBySession, rq_done: { passings: [{ id: "p1", timestamp: ranAt + 20000, lapMs: 20000 }], competitors: {}, adjustments: [] } },
});
await navigate("race_day", "raceDaySessionSelect");
assert.ok(await evaluate(`[...document.getElementById("raceDaySessionSelect").options].some((option) => option.value === "rq_done")`), "the finished heat is in the list");
await evaluate(`(() => { const select = document.getElementById("raceDaySessionSelect"); select.value = "rq_done"; select.dispatchEvent(new Event("change")); })()`);
await waitFor(async () => Boolean(await evaluate(`document.getElementById("raceDayResetSession")`)), "Run again for the finished heat");
assert.equal(await evaluate(`document.getElementById("raceDayResetSession").textContent.trim()`), "↻ Run again");
// The page refreshes its data from the server every 15 s without redrawing; Run again must
// still reset the heat itself, not an old copy of it (the clock used to stay at 4:32).
await new Promise((resolve) => setTimeout(resolve, 16000));
await evaluate(`document.getElementById("raceDayResetSession").click()`);
await waitFor(async () => (await savedState()).sessions.find((session) => session.id === "rq_done")?.status === "ready", "the heat is ready to run again");
const reset = (await savedState()).sessions.find((session) => session.id === "rq_done");
assert.deepEqual([reset.startedAt || null, reset.endedAt || null, reset.finishedByTimer], [null, null, false], "the clock is back at zero");
assert.equal((await savedState()).resultsBySession.rq_done?.passings?.length || 0, 0, "its laps are cleared");
await waitFor(async () => Boolean(await evaluate(`document.getElementById("raceDayStart")`)), "Start is offered again");
assert.equal(await evaluate(`document.querySelector("[data-race-day-clock]").textContent.trim()`), "05:00", "the clock shows the full time again");
console.log("PASS a finished heat can be picked and reset: clock to zero, laps cleared, Start again");
// The race controller follows the heat on Race Day: order, laps, last/best lap, estimate.
const liveAt = Date.now() - 60000;
const competitor = (key, name, laps, lastLapMs, bestLapMs, elapsedMs) => ({ key, driverName: name, displayName: name, laps, lastLapMs, bestLapMs, startTimestamp: liveAt, lastTimestamp: liveAt + elapsedMs });
await patchState({
sessions: [...(await savedState()).sessions.filter((session) => session.status !== "running"), { id: "rq_live", eventId: "rq_event", name: "RQ Heat 3", type: "heat", status: "running", durationMin: 5, startMode: "mass", startedAt: liveAt }],
resultsBySession: { ...(await savedState()).resultsBySession, rq_live: { passings: [], adjustments: [], competitors: {
slow: competitor("slow", "Slow Sam", 2, 26000, 25000, 52000),
fast: competitor("fast", "Fast Fia", 3, 19000, 18500, 57000),
} } },
activeSessionId: "rq_live",
});
await navigate("race_day", "raceDayLive");
const liveRows = await evaluate(`JSON.stringify([...document.querySelectorAll("#raceDayLive tbody tr")].map((row) => [...row.cells].map((cell) => cell.textContent.trim())))`);
const [first, second] = JSON.parse(liveRows);
assert.deepEqual([first[0], first[1], first[2], second[1], second[2]], ["1", "Fast Fia", "3", "Slow Sam", "2"], "the running order with laps");
assert.match(first[3], /19\.0/, "last lap");
assert.match(first[4], /18\.5/, "best lap");
assert.match(first[7], /^16 · /, "estimated laps when the time is up");
assert.equal(await evaluate(`document.querySelector("#raceDayLive .is-fastest")?.closest("tr").cells[1].textContent.includes("Fast Fia")`), true, "the heat's fastest lap is marked");
await patchState({ sessions: (await savedState()).sessions.map((session) => (session.id === "rq_live" ? { ...session, status: "finished", endedAt: Date.now() } : session)) });
console.log("PASS Race Day shows the live race: order, laps, last and best lap, gaps and estimated finish");
await navigate("timing", "activeSessionSelect");
assert.equal(await evaluate(`Boolean(document.getElementById("startSession") || document.getElementById("resetSession") || document.getElementById("stopSession"))`), false, "no run controls on Timing");
await evaluate(`document.getElementById("timingOpenRaceDay").click()`);
await waitFor(async () => Boolean(await evaluate(`document.getElementById("raceDayQuickPractice")`)), "the link opens Race Day");
console.log("PASS Timing is for following: run controls moved to Race Day, with a link there");
} finally {
dialogs.splice(dialogsAtStart);
const current = await savedState();
await patchState({
sessions: current.sessions.filter((session) => session.id !== "rq_heat").map((session) => (created && session.id === created.id ? { ...session, status: "finished", endedAt: Date.now() } : session)),
events: current.events.filter((event) => event.id !== "rq_event"),
trackLayouts: [],
activeSessionId: "",
});
}
}