import assert from "node:assert/strict"; import { startFakeDecoder } from "./browser_fake_decoder.js"; // Race Day start through the prologue (count-down), driven by real decoder passings: Start // begins the count-down, drivers go green as their transponder crosses, a driver registered // with the wrong number gets the unknown transponder linked from the panel, the count-down // can be held and resumed, "Go now" warns about anyone missing, and a count-down that runs // out starts the heat on its own. export async function runPrologueSmoke({ evaluate, navigate, savedState, waitFor, base, dialogs }) { 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 statuses = () => evaluate(`Object.fromEntries([...document.querySelectorAll("[data-warmup-driver]")].map((row) => [row.querySelector("strong").textContent, row.querySelector(".warmup-status").textContent.trim()]))`); const clockText = () => evaluate(`document.querySelector(".race-day-control-panel [data-prologue-clock]")?.textContent || ""`); const sessionStatus = async (id) => (await savedState()).sessions.find((session) => session.id === id)?.status; const dialogsAtStart = dialogs.length; const decoder = await startFakeDecoder(); const classId = "wu_class"; const drivers = [ { id: "wu_a", name: "WU Anna", classId, transponder: "8110001" }, { id: "wu_b", name: "WU Bo", classId, transponder: "8110002" }, { id: "wu_c", name: "WU Cia", classId, transponder: "8110003" }, // registered with the wrong number ]; const event = { id: "wu_event", name: "WU Race", classId, mode: "race", raceConfig: { minLapMs: 3000, maxLapMs: 60000, finalPrologueSec: 90 } }; const heat = { id: "wu_heat", eventId: event.id, name: "WU Heat 1", type: "heat", durationMin: 5, status: "ready" }; const heat2 = { id: "wu_heat2", eventId: event.id, name: "WU Heat 2", type: "heat", durationMin: 5, status: "ready" }; const seeded = await savedState(); await patchState({ classes: [...seeded.classes, { id: classId, name: "WU Class" }], drivers: [...seeded.drivers, ...drivers], events: [...seeded.events, event], sessions: [...seeded.sessions.map((session) => (["running", "paused"].includes(session.status) ? { ...session, status: "finished" } : session)), heat, heat2], activeSessionId: heat.id, settings: { ...seeded.settings, autoReconnect: true, wsUrl: `ws://127.0.0.1:${decoder.port}`, raceAudio: { ...seeded.settings.raceAudio, enabled: false } }, }); try { await navigate("race_day", "raceDayStart"); await waitFor(async () => evaluate('document.getElementById("connectionBadge")?.classList.contains("badge-online")'), "decoder connected"); await waitFor(async () => Boolean(await evaluate(`document.querySelector("[data-warmup-driver]")`)), "transponder check shown for a ready heat"); assert.equal(await evaluate(`document.getElementById("raceDayPrologueLength").value`), "90", "count-down length defaults to the event's setting"); assert.deepEqual(await evaluate(`[...document.getElementById("raceDayPrologueLength").options].map((option) => option.value)`), ["0", "30", "60", "90", "120", "180", "300"]); decoder.sendPassing("8110001"); // before the count-down starts - must not count // Wait for the frame to actually reach the page (decoder diagnostics count it) before // starting, otherwise it could arrive after the start and rightly count. await waitFor(async () => evaluate(`import("/src/decoder_diagnostics.js").then(({ decoderDiagnostics }) => JSON.stringify(decoderDiagnostics.snapshot?.() || decoderDiagnostics).includes("8110001"))`), "pre-count-down passing received"); // Rare flake seen twice in ~10 suite runs (not reproducible with extra logging): the // passing counted as after the start. A short settle keeps this scenario about the UI; // the before/after rule itself is covered deterministically in prologue_logic.test.js. await new Promise((resolve) => setTimeout(resolve, 750)); await evaluate(`(() => { document.getElementById("raceDayPrologueLength").value = "120"; document.getElementById("raceDayStart").click(); })()`); await waitFor(async () => /^[12]:\d\d$/.test(await clockText()), "count-down running"); assert.deepEqual(await statuses(), { "WU Anna": "⏳", "WU Bo": "⏳", "WU Cia": "⏳" }, "a passing from before the count-down doesn't count"); assert.equal(await sessionStatus(heat.id), "ready", "the heat itself hasn't started"); decoder.sendPassing("8110001"); decoder.sendPassing("8119999"); // Cia's real transponder; Bo never shows up await waitFor(async () => JSON.stringify(await statuses()) === JSON.stringify({ "WU Anna": "✅", "WU Bo": "⏳", "WU Cia": "⏳" }), "Anna goes green, Bo and Cia still missing"); console.log("PASS drivers go green as their transponder crosses during the count-down"); await evaluate(`(() => { const select = document.querySelector('[data-warmup-driver="wu_c"] [data-warmup-assign]'); select.value = "8119999"; select.dispatchEvent(new Event("change")); })()`); await waitFor(async () => (await statuses())["WU Cia"] === "✅", "Cia green after linking the unknown transponder"); await waitFor(async () => (await savedState()).drivers.find((driver) => driver.id === "wu_c").transponder === "8119999", "Cia's transponder saved"); assert.match(await evaluate(`document.body.innerText`), /Transponder check[\s\S]{0,40}2\/3/, "preflight shows how many have been seen"); console.log("PASS an unknown transponder seen during the count-down can be linked to a driver in one step"); await evaluate(`document.querySelector('[data-prologue="hold"]').click()`); await waitFor(async () => Boolean(await evaluate(`document.querySelector('[data-prologue="resume"]')`)), "count-down on hold"); const held = await clockText(); await new Promise((resolve) => setTimeout(resolve, 1500)); assert.equal(await clockText(), held, "a held count-down doesn't move"); await evaluate(`document.querySelector('[data-prologue="resume"]').click()`); await waitFor(async () => (await clockText()) !== held, "count-down moves again after resume"); await evaluate(`document.querySelector('[data-prologue="plus"]').click()`); await waitFor(async () => /^[23]:\d\d$/.test(await clockText()), "+1 min adds a minute"); console.log("PASS the count-down can be held (cars on the grid), resumed and extended"); const before = dialogs.length; await evaluate(`document.querySelector('[data-prologue="go"]').click()`); await waitFor(async () => dialogs.slice(before).some((dialog) => dialog.type === "confirm" && /WU Bo/.test(dialog.message) && !/WU Anna/.test(dialog.message)), "Go now warns about the driver not seen"); await waitFor(async () => (await sessionStatus(heat.id)) === "running", "confirming starts the heat anyway"); console.log("PASS Go now warns about drivers not seen during the count-down but can still start the heat"); // A count-down that runs out starts the next heat by itself - no click, no dialog. await patchState({ sessions: (await savedState()).sessions.map((session) => (session.id === heat.id ? { ...session, status: "finished", endedAt: Date.now() } : session)), activeSessionId: heat2.id }); await navigate("race_day", "raceDayStart"); await waitFor(async () => (await evaluate(`document.querySelector(".race-day-hero h2")?.textContent`)) === heat2.name, "next heat selected"); const dialogsBefore = dialogs.length; await evaluate(`(() => { document.getElementById("raceDayPrologueLength").value = "30"; document.getElementById("raceDayStart").click(); })()`); await waitFor(async () => Boolean(await evaluate(`document.querySelector('[data-prologue="to10"]')`)), "count-down running"); await evaluate(`document.querySelector('[data-prologue="to10"]').click()`); await waitFor(async () => (await sessionStatus(heat2.id)) === "running", "heat starts on its own when the count-down runs out", 15000); assert.equal(dialogs.length, dialogsBefore, "no confirm when the count-down runs out"); console.log("PASS the heat starts on its own when the count-down runs out"); } finally { dialogs.splice(dialogsAtStart); const current = await savedState(); await patchState({ settings: { ...current.settings, autoReconnect: false }, sessions: current.sessions.map((session) => ([heat.id, heat2.id].includes(session.id) ? { ...session, status: "finished", endedAt: Date.now() } : session)), activeSessionId: "", }); await decoder.close(); } }