feat(control): Race Controller Master, one browser runs the race and others follow or take over (1.77.0)

This commit is contained in:
larssand committed 2026-11-07 17:36:15 +01:00
1 parent 54989d8597
commit cf3a4c5fdb
16 files changed
+316 -9

No files matched your search

@@ -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 });
}
});