feat(houseofrc): fetch driver names and brands by transponder (1.79.0)

This commit is contained in:
larssand committed 2026-11-07 22:45:38 +01:00
1 parent 0c54e8b58a
commit 3829ca1cab
17 files changed
+319 -7

No files matched your search

+66
View File
@@ -0,0 +1,66 @@
import assert from "node:assert/strict";
// Drivers → "Fetch from House of RC": one lookup for all transponders; only empty fields are
// pre-ticked, an existing name is kept unless ticked; a new driver's transponder fills the name.
export async function runHouseOfRcSmoke({ evaluate, navigate, savedState, waitFor, base }) {
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 seeded = await savedState();
await patchState({ drivers: [...seeded.drivers,
{ id: "hor_a", name: "7700001", transponder: "7700001", brand: "" },
{ id: "hor_b", name: "Bosse", transponder: "7700002", brand: "Team Berg" },
] });
try {
await navigate("drivers", "driverForm");
await evaluate(`(() => {
const realFetch = window.fetch;
window.__horAsked = [];
window.fetch = async (url, options) => {
if (String(url).includes("/api/houseofrc/transponders")) {
const asked = JSON.parse(options.body).transponders;
window.__horAsked.push(asked);
const members = {};
if (asked.includes("7700001")) members["7700001"] = { id: 68, name: "Anna Andersson", brand: "Associated", model: "B7" };
if (asked.includes("7700002")) members["7700002"] = { id: 70, name: "Bo Berg", brand: "Xray", model: "XB4" };
if (asked.includes("7700003")) members["7700003"] = { id: 72, name: "Nya Nisse", brand: "Mugen", model: "MBX8" };
return new Response(JSON.stringify({ ok: true, asked: asked.length, members }), { status: 200, headers: { "Content-Type": "application/json" } });
}
return realFetch(url, options);
};
})()`);
await evaluate(`document.getElementById("driversHouseOfRc").click()`);
await waitFor(async () => evaluate(`Boolean(document.querySelector("[data-hor-apply]"))`), "the import list opens");
const boxes = await evaluate(`[...document.querySelectorAll("[data-hor-field]")].map((box) => box.dataset.horDriver + ":" + box.dataset.horField + ":" + box.checked)`);
assert.deepEqual(boxes, ["hor_a:name:true", "hor_a:brand:true", "hor_b:name:false", "hor_b:brand:false"], "only empty fields are pre-ticked");
assert.equal(await evaluate(`window.__horAsked.length`), 1, "one lookup for all transponders");
await evaluate(`(() => { document.querySelector('[data-hor-driver="hor_b"][data-hor-field="brand"]').checked = true; document.querySelector("[data-hor-apply]").click(); })()`);
await waitFor(async () => {
const drivers = (await savedState()).drivers;
const a = drivers.find((driver) => driver.id === "hor_a");
return a?.name === "Anna Andersson";
}, "the ticked changes are saved");
const drivers = (await savedState()).drivers;
const a = drivers.find((driver) => driver.id === "hor_a");
const b = drivers.find((driver) => driver.id === "hor_b");
assert.deepEqual([a.name, a.brand, a.houseOfRcId], ["Anna Andersson", "Associated", "68"]);
assert.deepEqual([b.name, b.brand, b.houseOfRcId], ["Bosse", "Xray", undefined], "unticked name is kept, so not linked");
console.log("PASS House of RC driver import: one lookup, only empty fields pre-ticked, nothing overwritten unless ticked");
await evaluate(`(() => {
const form = document.getElementById("driverForm");
form.elements.transponder.value = "7700003";
form.elements.transponder.dispatchEvent(new Event("change", { bubbles: true }));
})()`);
await waitFor(async () => (await evaluate(`document.getElementById("driverForm").elements.name.value`)) === "Nya Nisse", "a known transponder fills the new driver's name");
assert.equal(await evaluate(`document.getElementById("driverForm").elements.brand.value`), "Mugen");
console.log("PASS House of RC fills an empty name and brand when a known transponder is typed for a new driver");
} finally {
await evaluate(`document.getElementById("houseOfRcImportOverlay")?.remove()`).catch(() => {});
const current = await savedState();
await patchState({ drivers: current.drivers.filter((driver) => !driver.id.startsWith("hor_")) });
}
}
+2
View File
@@ -22,6 +22,7 @@ import { runProfileClassSmoke } from "./browser_profile_class.js";
import { runDecoderClockSmoke } from "./browser_decoder_clock.js";
import { runCorrectionsScopeSmoke } from "./browser_corrections_scope.js";
import { runDriversPageSmoke } from "./browser_drivers_page.js";
import { runHouseOfRcSmoke } from "./browser_houseofrc.js";
import { runSeriesSmoke } from "./browser_series.js";
import { runTrainingSourceSmoke } from "./browser_training_source.js";
import { runDecoderUrlSmoke } from "./browser_decoder_url.js";
@@ -181,6 +182,7 @@ try {
await runDecoderClockSmoke({ evaluate, navigate, savedState, waitFor, base });
await runCorrectionsScopeSmoke({ evaluate, navigate, savedState, waitFor, base });
await runDriversPageSmoke({ evaluate, navigate, savedState, waitFor, base });
await runHouseOfRcSmoke({ evaluate, navigate, savedState, waitFor, base });
await runSeriesSmoke({ evaluate, navigate, savedState, waitFor, base, dialogs });
await runTrainingSourceSmoke({ evaluate, navigate, savedState, waitFor, base });
await runDecoderUrlSmoke({ evaluate, navigate, savedState, waitFor, base });
+36
View File
@@ -0,0 +1,36 @@
import test from "node:test";
import assert from "node:assert/strict";
import { planHouseOfRcDriverUpdates, applyHouseOfRcDriverUpdates } from "../src/houseofrc_logic.js";
const members = {
"1001": { id: 68, name: "Anna Andersson", brand: "Associated", model: "B7" },
"1002": { id: 70, name: "Bo Berg", brand: "Xray", model: "XB4" },
"1003": { id: 71, name: "Cecilia Carlsson", brand: "", model: "" },
};
const drivers = () => [
{ id: "a", name: "1001", transponder: "1001", brand: "" },
{ id: "b", name: "Bosse", transponder: "1002", brand: "Team Berg" },
{ id: "c", name: "cecilia carlsson", transponder: "1003", brand: "" },
{ id: "d", name: "Dan", transponder: "9999", brand: "" },
{ id: "g", name: "", transponder: "1001", brand: "", guestEventId: "e1" },
];
test("House of RC plan: only matched drivers, pre-ticked only where our field is empty or just the transponder number", () => {
const plan = planHouseOfRcDriverUpdates(drivers(), members);
assert.deepEqual(plan.map((row) => row.driverId), ["a", "b", "c"], "unknown transponders and sponsor guests are left out");
const [a, b, c] = plan;
assert.deepEqual([a.nameDiffers, a.nameDefault, a.brandDiffers, a.brandDefault], [true, true, true, true]);
assert.deepEqual([b.nameDiffers, b.nameDefault, b.brandDiffers, b.brandDefault], [true, false, true, false], "an existing name or brand is never pre-ticked");
assert.deepEqual([c.nameDiffers, c.brandDiffers], [false, false], "same name ignoring case and spaces; no brand from HoR");
});
test("House of RC apply: writes only the ticked fields and links drivers whose name matches", () => {
const list = drivers();
const changed = applyHouseOfRcDriverUpdates(list, members, { a: { name: true, brand: true }, b: { name: false, brand: true } });
const byId = Object.fromEntries(list.map((driver) => [driver.id, driver]));
assert.deepEqual([byId.a.name, byId.a.brand, byId.a.houseOfRcId], ["Anna Andersson", "Associated", "68"]);
assert.deepEqual([byId.b.name, byId.b.brand, byId.b.houseOfRcId], ["Bosse", "Xray", undefined], "name kept, so not linked (could be a borrowed transponder)");
assert.deepEqual([byId.c.name, byId.c.houseOfRcId], ["cecilia carlsson", "71"], "same person: linked without renaming");
assert.equal(byId.g.name, "", "sponsor guests untouched");
assert.equal(changed, 3);
});
+1
View File
@@ -1,5 +1,6 @@
import "./auth.test.js";
import "./backend_url.test.js";
import "./houseofrc_logic.test.js";
import "./judging_penalties.test.js";
import "./practice_rank.test.js";
import "./team_race_logic.test.js";
@@ -734,6 +734,13 @@ test("House of RC: the key is stored server-side only, never returned, and the t
seen.push({ url: req.url, auth: req.headers.authorization });
const ok = req.headers.authorization === "Bearer hrc_test_secret_key_1234";
res.writeHead(ok ? 200 : 401, { "Content-Type": "application/json" });
if (ok && req.url.startsWith("/timing/users")) {
// Newest owner first; an unknown number is simply missing.
return res.end(JSON.stringify([
{ id: 5, name: "Bo Berg", transponder: "1234567", model: { name: "B7", brand: { name: "Associated" } } },
{ id: 9, name: "Old Owner", transponder: "1234567" },
]));
}
res.end(JSON.stringify(ok ? { data: { user: { id: 68, name: "Test Club" }, rate_limit: 60 } } : { error: "unauthorized" }));
});
await new Promise((resolve) => mock.listen(0, "127.0.0.1", resolve));
@@ -762,6 +769,9 @@ test("House of RC: the key is stored server-side only, never returned, and the t
const test = await (await post("/api/houseofrc/test", {})).json();
assert.deepEqual([test.ok, test.user.name, test.rateLimit], [true, "Test Club", 60]);
assert.equal(seen.at(-1).url, "/v1/me");
const lookup = await (await post("/api/houseofrc/transponders", { transponders: ["1234567", "7654321", "1234567", "bad value"] })).json();
assert.equal(seen.at(-1).url, "/timing/users?transponders=1234567%2C7654321", "deduplicated, invalid numbers dropped");
assert.deepEqual(lookup.members, { 1234567: { id: 5, name: "Bo Berg", model: "B7", brand: "Associated" } }, "the newest owner wins");
const keyFile = join(dataDir, "houseofrc.json");
assert.equal(statSync(keyFile).mode & 0o777, 0o600, "only the server user can read the key file");