feat(houseofrc): preview and send results to the event's entries, heats or final placings (1.83.0)

This commit is contained in:
larssand committed 2026-11-08 20:44:23 +01:00
1 parent f4a62b92d2
commit ebdc6104e6
17 files changed
+295 -15

No files matched your search

+20
View File
@@ -83,6 +83,10 @@ export async function runHouseOfRcEventSmoke({ evaluate, navigate, savedState, w
{ entryId: 13, classId: 555, houseOfRcId: 903, name: "HoR Skipped", number: null, transponders: [], club: "", car: "" },
{ entryId: 14, classId: 556, houseOfRcId: 904, name: "HoR Other Class", number: null, transponders: [], club: "", car: "" },
] });
if (String(url).includes("/api/houseofrc/results")) {
window.__horPosted = JSON.parse(options.body);
return json({ ok: true, updated: window.__horPosted.entries.length, entries: [] });
}
return realFetch(url, options);
};
})()`);
@@ -137,6 +141,22 @@ export async function runHouseOfRcEventSmoke({ evaluate, navigate, savedState, w
assert.equal(Object.keys(race.houseOfRc.entryIds).length, 2);
}
console.log("PASS House of RC re-fetch in Manage: late entries added, withdrawn drivers taken out");
// Manage → "Send results to House of RC": nothing raced yet, so heats mode, no placings.
await waitFor(async () => evaluate(`Boolean(document.getElementById("houseOfRcSendResults"))`), "the send results button in Manage");
await evaluate(`document.getElementById("houseOfRcSendResults").click()`);
await waitFor(async () => evaluate(`Boolean(document.querySelector("#houseOfRcResultsOverlay [data-hor-send]"))`), "the results preview");
assert.equal(await evaluate(`document.querySelectorAll("#houseOfRcResultsOverlay [data-hor-result-row]").length`), 2);
assert.equal(await evaluate(`document.querySelector('[name=horResultsMode][value=final]').disabled`), true, "no finals yet: final results cannot be picked");
await evaluate(`document.querySelector("#houseOfRcResultsOverlay [data-hor-send]").click()`);
await waitFor(async () => evaluate(`Boolean(window.__horPosted)`), "the results are posted");
const posted = await evaluate(`window.__horPosted`);
assert.equal(posted.eventId, 2251);
assert.deepEqual(posted.entries.map((entry) => entry.id).sort(), [11, 16]);
assert.ok(posted.entries.every((entry) => !("ranking" in entry)), "no placing sent before the finals");
await waitFor(async () => Boolean((await savedState()).events.find((item) => item.houseOfRc?.eventId === 2251)?.houseOfRc.resultsSentAt), "when it was sent is saved");
await evaluate(`document.getElementById("houseOfRcResultsOverlay")?.remove()`);
console.log("PASS House of RC results: preview in Manage, heats sent to the race's entries without placings");
} finally {
await evaluate(`document.getElementById("houseOfRcEventOverlay")?.remove()`).catch(() => {});
const current = await savedState();
+34
View File
@@ -105,3 +105,37 @@ test("House of RC re-fetch: late entries are added, withdrawn ones offered for r
assert.equal(drivers.find((driver) => driver.id === "late").transponder, "9");
assert.deepEqual(diffHouseOfRcEntries({ ...event, raceConfig: { driverIds: ids } }, entries, drivers), { added: [], withdrawn: [] }, "fetching again finds nothing new");
});
test("House of RC results: heats and qualifying without placings, then final placings by main", async () => {
const { buildHouseOfRcResults, formatHouseOfRcTime } = await import("../src/houseofrc_logic.js");
assert.deepEqual([formatHouseOfRcTime(13201), formatHouseOfRcTime(303412), formatHouseOfRcTime(0)], ["13.201", "5:03.412", ""]);
const event = { raceConfig: { driverIds: ["a", "b", "h"] }, houseOfRc: { eventId: 1, entryIds: { a: 11, b: 12, gone: 13 } } };
const drivers = [{ id: "a", name: "Anna" }, { id: "b", name: "Bo" }, { id: "h", name: "Hand Added" }];
const heat = (name, rows) => ({ session: { name }, rows });
const overview = { phases: [
{ type: "qualification", standings: [{ driverId: "b", rank: 1 }, { driverId: "a", rank: 2 }], heats: [
heat("Kval 1", [{ pos: 1, driverId: "b", laps: 12, timeMs: 300500, bestLapMs: 13201 }, { pos: 2, driverId: "a", laps: 11, timeMs: 301000, bestLapMs: 14002 }]),
] },
{ type: "final", standings: [{ driverId: "a", rank: "A1" }, { driverId: "b", rank: "A2" }], heats: [
heat("A-final", [{ pos: 1, driverId: "a", laps: 22, timeMs: 303412, bestLapMs: 12950 }, { pos: 2, driverId: "b", laps: 21, timeMs: 0, bestLapMs: 13100, raceStatus: "dnf" }]),
] },
] };
const live = buildHouseOfRcResults(event, overview, { drivers });
assert.deepEqual(live.missing, ["Hand Added"], "a hand-added driver has no entry to write to");
assert.deepEqual(live.entries.map((entry) => entry.id), [11, 12], "only participants; a removed driver's entry is not sent");
assert.ok(live.entries.every((entry) => !("ranking" in entry)), "no placing yet, so nobody is notified");
const bo = live.entries[1];
assert.equal(bo.qualification, 1);
assert.equal(bo.tq, true);
assert.deepEqual(bo.data.result.heats[1], { heat: "A-final", laps: 21, time: "DNF", best_lap: "13.100", position: 2 });
assert.equal(bo.data.result.best_lap, "13.100");
const done = buildHouseOfRcResults(event, overview, { final: true, drivers });
assert.deepEqual(done.entries.map((entry) => [entry.id, entry.ranking, entry.data.result.main, entry.data.result.main_position]), [[11, 1, "A", 1], [12, 2, "A", 2]]);
assert.equal(done.entries[0].laps_time, "22/5:03.412");
assert.deepEqual(done.rows.map((row) => row.name), ["Anna", "Bo"], "preview in final order");
const bumped = { phases: [{ type: "final", standings: [{ driverId: "b", rank: "A1" }, { driverId: "a", rank: "A2" }, { driverId: "h", rank: "B1" }, { driverId: "b", rank: "B2" }], heats: [] }] };
const withBump = buildHouseOfRcResults({ ...event, houseOfRc: { entryIds: { a: 11, b: 12, h: 13 } } }, bumped, { final: true, drivers });
assert.deepEqual(withBump.entries.map((entry) => [entry.id, entry.ranking, entry.data.result.main, entry.data.result.bumped_from]), [[11, 2, "A", undefined], [12, 1, "A", ["B"]], [13, 3, "B", undefined]], "a bumped driver counts once, in the higher main");
const noQual = buildHouseOfRcResults(event, { phases: [] }, { drivers });
assert.ok(!("qualification" in noQual.entries[0]), "before any qualifying, House of RC's value is left alone");
});
@@ -733,6 +733,20 @@ test("House of RC: the key is stored server-side only, never returned, and the t
const mock = createServer((req, res) => {
seen.push({ url: req.url, auth: req.headers.authorization });
const ok = req.headers.authorization === "Bearer hrc_test_secret_key_1234";
if (ok && req.method === "POST" && req.url === "/v1/timing/events/2251/entries") {
let raw = "";
req.on("data", (chunk) => { raw += chunk; });
req.on("end", () => {
const body = JSON.parse(raw);
seen.at(-1).body = body;
const bad = body.entries.filter((entry) => entry.id !== 98765);
res.writeHead(bad.length ? 422 : 200, { "Content-Type": "application/json" });
res.end(JSON.stringify(bad.length
? { message: "The given data was invalid.", errors: { "entries.0.id": ["Not an entry of this event."] } }
: { data: { updated: 1, entries: [{ id: 98765, ranking: body.entries[0].ranking ?? null, user_id: 68 }] } }));
});
return;
}
const forbidden = ok && req.url === "/v1/timing/events/3001/entries";
const missing = ok && req.url.startsWith("/v1/timing/events/") && !forbidden && req.url !== "/v1/timing/events/2251/entries";
res.writeHead(!ok ? 401 : forbidden ? 403 : missing ? 404 : 200, { "Content-Type": "application/json" });
@@ -810,6 +824,16 @@ test("House of RC: the key is stored server-side only, never returned, and the t
assert.equal((await (await post("/api/houseofrc/entries", { eventId: "999" })).json()).error, "event_not_found");
assert.equal((await (await post("/api/houseofrc/entries", { eventId: "3001" })).json()).error, "not_your_event");
const sent = await post("/api/houseofrc/results", { eventId: 2251, entries: [{ id: 98765, ranking: 1, qualification: 2, tq: false, laps_time: "22/5:03.412", data: { result: { main: "A", main_position: 1 } }, name: "dropped" }] });
assert.equal(sent.status, 200);
assert.deepEqual(await sent.json(), { ok: true, status: 200, updated: 1, entries: [{ id: 98765, ranking: 1 }] });
assert.equal(seen.at(-1).auth, "Bearer hrc_test_secret_key_1234");
assert.deepEqual(Object.keys(seen.at(-1).body.entries[0]), ["id", "ranking", "qualification", "tq", "laps_time", "data"], "only result fields go out");
const refused = await (await post("/api/houseofrc/results", { eventId: 2251, entries: [{ id: 111, ranking: 1 }] })).json();
assert.equal(refused.error, "rejected");
assert.deepEqual(refused.errors, { "entries.0.id": ["Not an entry of this event."] });
assert.equal((await post("/api/houseofrc/results", { eventId: 2251, entries: [] })).status, 400);
const keyFile = join(dataDir, "houseofrc.json");
assert.equal(statSync(keyFile).mode & 0o777, 0o600, "only the server user can read the key file");
for (const path of ["/api/houseofrc/status", "/api/state"]) {