feat(control): Race Controller Master, one browser runs the race and others follow or take over (1.77.0)
This commit is contained in:
1 parent
54989d8597
commit
cf3a4c5fdb
16 files changed
+316
-9
No files matched your search
@@ -6,7 +6,7 @@ const localPause = ms => new Promise(resolve => setTimeout(resolve, ms));
|
||||
// Attaches an independent CDP connection to one browser page target. Needed because the
|
||||
// popup opened by window.open() is a second, separate page target with its own JS/DOM,
|
||||
// distinct from the admin page the harness is already attached to.
|
||||
async function attach(target) {
|
||||
export async function attach(target) {
|
||||
const socket = new WebSocket(target.webSocketDebuggerUrl);
|
||||
await new Promise((resolve, reject) => { socket.addEventListener("open", resolve, { once: true }); socket.addEventListener("error", reject, { once: true }); });
|
||||
let nextId = 0;
|
||||
|
||||
@@ -0,0 +1,52 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,7 @@ import { runRaceDayQuickSmoke } from "./browser_race_day_quick.js";
|
||||
import { runDayScheduleSmoke } from "./browser_day_schedule.js";
|
||||
import { runObsPopupSmoke } from "./browser_obs_popup.js";
|
||||
import { runDecoderReconnectSmoke } from "./browser_decoder_reconnect.js";
|
||||
import { runRaceControllerSmoke } from "./browser_race_controller.js";
|
||||
import assert from "node:assert/strict";
|
||||
import { spawn } from "node:child_process";
|
||||
import { existsSync, mkdtempSync, rmSync } from "node:fs";
|
||||
@@ -190,6 +191,7 @@ try {
|
||||
await runRaceDayStartSequencesSmoke({ evaluate, navigate, waitFor, pause, base, dialogs });
|
||||
await runObsPopupSmoke({ evaluate, navigate, fill, savedState, waitFor, pause, base, debugPort, dialogs, realClick });
|
||||
await runDecoderReconnectSmoke({ evaluate, navigate, savedState, waitFor, pause, base });
|
||||
await runRaceControllerSmoke({ evaluate, navigate, savedState, waitFor, pause, base, debugPort });
|
||||
await navigate("classes", "classForm");
|
||||
await send("Network.emulateNetworkConditions", { offline: true, latency: 0, downloadThroughput: -1, uploadThroughput: -1 });
|
||||
await fill("classForm", { name: "Offline Draft" });
|
||||
|
||||
@@ -688,3 +688,41 @@ test("compressed request bodies are refused before anything is decompressed (CVE
|
||||
rmSync(dataDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("Race Controller Master: one browser holds the lock, take-over needs force and the admin PIN", { timeout: 20000 }, async () => {
|
||||
const dataDir = mkdtempSync(join(tmpdir(), "live-rc-test-controller-"));
|
||||
const port = await reservePort();
|
||||
const baseUrl = `http://127.0.0.1:${port}`;
|
||||
const child = spawn(process.execPath, [join(projectRoot, "server.js")], {
|
||||
cwd: projectRoot,
|
||||
env: { ...process.env, HOST: "127.0.0.1", PORT: String(port), RC_TIMING_DATA_DIR: dataDir, RC_TIMING_PID_FILE: "" },
|
||||
stdio: ["ignore", "pipe", "pipe"],
|
||||
});
|
||||
const post = (path, body, cookie = "") => fetch(`${baseUrl}${path}`, { method: "POST", headers: { "Content-Type": "application/json", ...(cookie ? { Cookie: cookie } : {}) }, body: JSON.stringify(body) });
|
||||
const master = async () => (await (await fetch(`${baseUrl}/api/controller`)).json()).master;
|
||||
|
||||
try {
|
||||
await waitForServer(child);
|
||||
assert.equal(await master(), null, "nobody is master at start");
|
||||
assert.equal((await post("/api/controller/claim", { clientId: "a", name: "Klubb-laptop" })).status, 200);
|
||||
assert.equal((await master()).name, "Klubb-laptop");
|
||||
assert.equal((await post("/api/controller/claim", { clientId: "b" })).status, 409, "a second browser cannot just claim it");
|
||||
assert.equal((await master()).clientId, "a");
|
||||
assert.equal((await post("/api/controller/claim", { clientId: "b", force: true })).status, 200, "take over with force");
|
||||
assert.equal((await master()).clientId, "b");
|
||||
assert.equal((await post("/api/controller/claim", { clientId: "a" })).status, 409, "the old master's heartbeat does not win it back");
|
||||
|
||||
const cookie = (await post("/api/auth/pin", { newPin: "4711" })).headers.get("set-cookie").split(";")[0];
|
||||
assert.equal((await post("/api/controller/claim", { clientId: "b" })).status, 200, "the master renews without logging in");
|
||||
const quiet = await post("/api/controller/claim", { clientId: "c" });
|
||||
assert.equal(quiet.status, 401);
|
||||
assert.equal((await quiet.json()).error, "login_needed", "a quiet claim on page load does not open the PIN dialog");
|
||||
assert.equal((await (await post("/api/controller/claim", { clientId: "c", force: true })).json()).error, "auth_required", "take over asks for the PIN");
|
||||
assert.equal((await post("/api/controller/claim", { clientId: "c", force: true }, cookie)).status, 200, "logged in, take over works");
|
||||
assert.equal((await post("/api/controller/release", { clientId: "c" })).status, 200);
|
||||
assert.equal(await master(), null, "release frees the lock");
|
||||
} finally {
|
||||
await stopServer(child);
|
||||
rmSync(dataDir, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user