import assert from "node:assert/strict"; import { startFakeDecoder } from "./browser_fake_decoder.js"; // The decoder's clock is set by hand and missed the switch to winter time: one hour ahead. // RaceCore never changes it, but Race Day's preflight and the decoder diagnostics say so - from // passings before the heat starts (warm-up), so it can be fixed before the first lap counts. export async function runDecoderClockSmoke({ 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 decoder = await startFakeDecoder(); const seeded = await savedState(); await patchState({ events: [...seeded.events, { id: "dc_event", name: "DC Race", mode: "race", raceConfig: {} }], sessions: [...seeded.sessions.map((session) => (["running", "paused"].includes(session.status) ? { ...session, status: "finished", endedAt: Date.now() } : session)), { id: "dc_heat", eventId: "dc_event", name: "DC Heat", type: "heat", status: "ready", durationMin: 5 }], activeSessionId: "dc_heat", settings: { ...seeded.settings, autoReconnect: true, wsUrl: `ws://127.0.0.1:${decoder.port}` }, }); const preflightText = () => evaluate(`document.getElementById("view").innerText`); try { await navigate("race_day", "raceDaySessionSelect"); await waitFor(async () => evaluate('document.getElementById("connectionBadge")?.classList.contains("badge-online")'), "decoder connected"); assert.doesNotMatch(await preflightText(), /Decoder clock/, "nothing before a passing"); decoder.sendPassing("8660001", Date.now() + 3600000); await waitFor(async () => /Decoder clock[\s\S]*1 h ahead of the computer/.test(await preflightText()), "Race Day warns that the decoder is one hour ahead"); assert.equal((await savedState()).sessions.find((session) => session.id === "dc_heat").status, "ready", "measured during warm-up, nothing started"); console.log("PASS Race Day warns before the start when the decoder's clock is an hour off (summer/winter time)"); await navigate("settings", "settingsForm"); await evaluate(`document.querySelector('[data-settings-tab="connection"]').click()`); await evaluate(`(() => { const details = document.getElementById("decoderDiagnostics"); details.open = true; details.dispatchEvent(new Event("toggle")); })()`); decoder.sendPassing("8660001", Date.now() + 3600000); await waitFor(async () => /Decoder clock: 1 h ahead of the computer/.test(await evaluate(`document.getElementById("decoderDiagnostics").innerText`)), "the diagnostics show it too"); console.log("PASS the decoder diagnostics show how far the decoder's clock is off"); } finally { const current = await savedState(); await patchState({ settings: { ...current.settings, autoReconnect: false }, sessions: current.sessions.filter((session) => session.id !== "dc_heat"), events: current.events.filter((event) => event.id !== "dc_event"), activeSessionId: "", }); await decoder.close(); } }