Files
Live_RC/tests/browser_race_controller.js
T

71 lines
5.8 KiB
JavaScript

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");
await waitFor(async () => await evaluate('!document.getElementById("controllerBanner").hidden && document.getElementById("controllerBanner").classList.contains("is-master")'), "the master sees that it is the master");
assert.ok(await evaluate('Boolean(document.getElementById("controllerRelease"))'), "with a Release button");
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 () => Boolean(await second.evaluate('document.getElementById("controllerTakeOver")')), "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/);
// The follower cannot pause, resume or reset the heat (speaker panel or Race Day).
const before = await savedState();
const heat = { id: "rc_ctrl_heat", eventId: before.events[0]?.id || "", name: "RC Ctrl Heat", type: "final", mode: "race", status: "paused", startMode: "position", durationMin: 5, startedAt: Date.now() - 60000, pausedAt: Date.now() - 30000, totalPausedMs: 0, driverIds: [] };
await patchState({ sessions: [...before.sessions, heat], activeSessionId: heat.id });
await second.evaluate(`location.href = ${JSON.stringify(base + "/?view=race_day")}; true`);
await second.waitFor(async () => Boolean(await second.evaluate('document.querySelector(\'#speakerPanel [data-race-audio-cue="race_resumed"]\')')) && Boolean(await second.evaluate('document.getElementById("controllerTakeOver")')), "follower on Race Day");
await second.evaluate('document.querySelector(\'#speakerPanel [data-race-audio-cue="race_resumed"]\').click(); true');
await pause(1500);
assert.equal((await savedState()).sessions.find((session) => session.id === heat.id).status, "paused", "the follower's Resume does nothing");
assert.equal(await second.evaluate('document.getElementById("controllerBanner").classList.contains("is-flash")'), true, "the banner points at Take over");
// The follower is updated live from the server, without a reload.
const renamed = await savedState();
await patchState({ sessions: renamed.sessions.map((session) => (session.id === heat.id ? { ...session, name: "RC Ctrl Heat Live" } : session)) });
await second.waitFor(async () => (await second.evaluate("document.querySelector('.content').innerText")).includes("RC Ctrl Heat Live"), "the follower shows the change within seconds", 5000);
const cleaned = await savedState();
await patchState({ sessions: cleaned.sessions.filter((session) => session.id !== heat.id), activeSessionId: before.activeSessionId || null });
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 () => Boolean(await evaluate('document.getElementById("controllerTakeOver")')), "the old master sees the follow 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 live without the decoder or pause/resume, 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();
}
}