import assert from "node:assert/strict"; import { startFakeDecoder } from "./browser_fake_decoder.js"; // Decoder alarm (src/decoder_watch.js): the decoder goes away mid-heat -> red banner with // Reconnect / Silence; it comes back -> green "working again, X s without timing". export async function runDecoderAlarmSmoke({ evaluate, navigate, fill, savedState, waitFor }) { const click = id => evaluate(`document.getElementById(${JSON.stringify(id)}).click()`); const submit = id => evaluate(`document.getElementById(${JSON.stringify(id)}).requestSubmit()`); const findEvent = async name => (await savedState()).events.find(event => event.name === name); const findSession = async name => (await savedState()).sessions.find(session => session.name === name); const patchState = async patch => { const current = await savedState(); Object.assign(current, patch); assert.equal((await fetch(new URL("/api/state", await evaluate("location.origin")), { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(current) })).ok, true); }; const banner = () => evaluate(`(() => { const el = document.getElementById("decoderAlarm"); return { hidden: el.hidden, kind: el.dataset.kind, text: el.textContent, reconnect: Boolean(el.querySelector('[data-decoder-alarm="reconnect"]')), silence: Boolean(el.querySelector('[data-decoder-alarm="silence"]')) }; })()`); let decoder = await startFakeDecoder(); const port = decoder.port; try { const seeded = await savedState(); await patchState({ settings: { ...seeded.settings, autoReconnect: true, wsUrl: `ws://127.0.0.1:${port}` } }); await navigate("events", "eventForm"); await waitFor(async () => await evaluate('document.getElementById("connectionBadge")?.classList.contains("badge-online")'), "fake decoder connected"); await fill("eventForm", { name: "Decoder Alarm Event", date: "2026-09-10", classId: "test_class" }); await submit("eventForm"); await waitFor(async () => Boolean(await findEvent("Decoder Alarm Event")), "create event"); await click("event-manage-" + (await findEvent("Decoder Alarm Event")).id); await fill("sessionForm", { name: "Decoder Alarm Heat", type: "open_practice", durationMin: "5" }); await submit("sessionForm"); await waitFor(async () => Boolean(await findSession("Decoder Alarm Heat")), "create heat"); await patchState({ activeSessionId: (await findSession("Decoder Alarm Heat")).id }); await navigate("race_day", "raceDayStart"); await waitFor(async () => (await evaluate('document.getElementById("raceDayStart")?.disabled')) === false, "start enabled"); assert.equal((await banner()).hidden, true, "no alarm while the decoder is fine"); await click("raceDayStart"); await waitFor(async () => (await findSession("Decoder Alarm Heat")).status === "running", "heat running"); await decoder.close(); // the decoder goes away and stays away await waitFor(async () => (await banner()).kind === "lost", "red banner once the decoder has been gone 3 s", 10000); const lost = await banner(); assert.match(lost.text, /Decodern tappade anslutningen|Decoder disconnected/); assert.ok(lost.reconnect && lost.silence, "Reconnect and Silence buttons"); await evaluate(`document.querySelector('[data-decoder-alarm="silence"]').click()`); await waitFor(async () => !(await banner()).silence && (await banner()).kind === "lost", "silenced: the banner stays, the button goes"); decoder = await startFakeDecoder(port); await evaluate(`document.querySelector('[data-decoder-alarm="reconnect"]').click()`); await waitFor(async () => (await banner()).kind === "ok" && !(await banner()).hidden, "green banner when the decoder is back"); assert.match((await banner()).text, /\d+ s/); await waitFor(async () => (await banner()).hidden, "the green banner goes after 10 s", 15000); console.log("PASS decoder alarm: red banner when the decoder drops mid-heat, silence, reconnect, then 'working again'"); } finally { // Pause the heat through the UI (like the OBS popup test), so no running heat leaks into later tests. await evaluate('document.getElementById("raceDayPause")?.click()').catch(() => {}); await waitFor(async () => (await findSession("Decoder Alarm Heat"))?.status !== "running", "heat paused").catch(() => {}); const cleanup = await savedState(); await patchState({ settings: { ...cleanup.settings, autoReconnect: false } }); await decoder.close(); } }