import assert from "node:assert/strict"; import { startFakeDecoder } from "./browser_fake_decoder.js"; import { attach } from "./browser_obs_popup.js"; // Race Controller Master: a second admin browser follows (no decoder, no start) until it // takes over; the first one then lets go of the decoder. export async function runRaceControllerSmoke({ evaluate, navigate, savedState, waitFor, pause, base, debugPort }) { const controller = async () => (await (await fetch(base + "/api/controller")).json()).master; const patchState = async (patch) => { const current = await savedState(); Object.assign(current, patch); assert.equal((await fetch(base + "/api/state", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(current) })).ok, true); }; const decoder = await startFakeDecoder(); let second = null; let target = null; try { const seeded = await savedState(); await patchState({ settings: { ...seeded.settings, autoReconnect: true, wsUrl: `ws://127.0.0.1:${decoder.port}` } }); await navigate("settings", "settingsForm"); const firstId = await evaluate('sessionStorage.getItem("rc_controller_client_id")'); await waitFor(async () => (await controller())?.clientId === firstId, "the first admin browser is the master"); await waitFor(async () => await evaluate('document.getElementById("connectionBadge")?.classList.contains("badge-online")'), "master connects to the decoder"); assert.equal(await evaluate('document.getElementById("controllerBanner").hidden'), true, "no banner on the master"); const connectsBefore = decoder.totalConnections(); target = await (await fetch(`http://127.0.0.1:${debugPort}/json/new?${encodeURIComponent(base + "/?view=settings")}`, { method: "PUT" })).json(); second = await attach(target); await second.waitFor(async () => (await second.evaluate('document.getElementById("controllerBanner")?.hidden')) === false, "second browser shows the follow banner"); await pause(1500); assert.equal(decoder.totalConnections(), connectsBefore, "the second browser does not connect to the decoder"); assert.match(await second.evaluate('document.getElementById("controllerBanner").textContent'), /Race Controller Master/); await second.evaluate('window.confirm = () => true; document.getElementById("controllerTakeOver").click(); true'); const secondId = await second.evaluate('sessionStorage.getItem("rc_controller_client_id")'); await waitFor(async () => (await controller())?.clientId === secondId, "take over moves the master"); await waitFor(async () => decoder.totalConnections() > connectsBefore, "the new master connects to the decoder"); await waitFor(async () => (await evaluate('document.getElementById("controllerBanner").hidden')) === false, "the old master sees the banner", 12000); assert.equal(await evaluate("document.getElementById('connectionBadge').classList.contains('badge-online')"), false, "the old master lets go of the decoder"); console.log("PASS Race Controller Master: a second browser follows without the decoder, takes over, and the first one lets go"); // Back to the first browser for the rest of the suite. await evaluate('window.confirm = () => true; document.getElementById("controllerTakeOver").click(); true'); await waitFor(async () => (await controller())?.clientId === firstId, "first browser takes back the master"); } finally { second?.close(); if (target) await fetch(`http://127.0.0.1:${debugPort}/json/close/${target.id}`).catch(() => {}); const cleanup = await savedState(); await patchState({ settings: { ...cleanup.settings, autoReconnect: false } }); await decoder.close(); } }