feat(houseofrc): fetch club events, classes and entries with transponders via the timing API (1.81.0)
This commit is contained in:
1 parent
9c1f5713e6
commit
b0f2b92c04
15 files changed
+247
-149
No files matched your search
+17
-12
@@ -74,12 +74,15 @@ export async function runHouseOfRcEventSmoke({ evaluate, navigate, savedState, w
|
||||
const realFetch = window.fetch;
|
||||
window.fetch = async (url, options) => {
|
||||
const json = (data) => new Response(JSON.stringify(data), { status: 200, headers: { "Content-Type": "application/json" } });
|
||||
if (String(url).includes("/api/houseofrc/events")) return json({ ok: true, events: [{ id: 2251, name: "HoR Deltävling 2", start: "2026-12-05T07:00:00Z", city: "Sundsvall", country: "SE", url: "https://houseofrc.com/events/2251" }] });
|
||||
if (String(url).includes("/api/houseofrc/entries")) return json({ ok: true, event: { id: 2251, name: "HoR Deltävling 2", start: "2026-12-05T07:00:00Z", city: "Sundsvall", url: "https://houseofrc.com/events/2251" }, entries: [
|
||||
{ houseOfRcId: 901, name: "HoR Anna", number: 7, club: "Sundsvall RC", car: "XRAY X12" },
|
||||
{ houseOfRcId: 902, name: "HoR Bo", number: null, club: "", car: "" },
|
||||
{ houseOfRcId: 903, name: "HoR Skipped", number: null, club: "", car: "" },
|
||||
] });
|
||||
if (String(url).includes("/api/houseofrc/events")) return json({ ok: true, organizationId: 233, events: [{ id: 2251, name: "HoR Deltävling 2", start: "2026-12-05T07:00:00Z", city: "Sundsvall", country: "SE", url: "https://houseofrc.com/events/2251" }] });
|
||||
if (String(url).includes("/api/houseofrc/entries")) return json({ ok: true, event: { id: 2251, name: "HoR Deltävling 2", start: "2026-12-05T07:00:00Z", city: "Sundsvall", url: "https://houseofrc.com/events/2251" },
|
||||
classes: [{ id: 555, name: "HoR 2wd" }, { id: 556, name: "HoR 4wd" }],
|
||||
entries: [
|
||||
{ entryId: 11, classId: 555, houseOfRcId: 901, name: "HoR Anna", number: 7, transponders: ["7710001"], club: "Sundsvall RC", car: "XRAY X12" },
|
||||
{ entryId: 12, classId: 555, houseOfRcId: 902, name: "HoR Bo", number: null, transponders: [], club: "", car: "" },
|
||||
{ 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: "" },
|
||||
] });
|
||||
return realFetch(url, options);
|
||||
};
|
||||
})()`);
|
||||
@@ -88,7 +91,8 @@ export async function runHouseOfRcEventSmoke({ evaluate, navigate, savedState, w
|
||||
await evaluate(`document.querySelector('[data-hor-event="2251"]').click()`);
|
||||
await waitFor(async () => evaluate(`Boolean(document.querySelector("[data-hor-apply]"))`), "the entries");
|
||||
const statuses = await evaluate(`[...document.querySelectorAll("#houseOfRcEventOverlay tbody .pill")].map((pill) => pill.textContent.trim())`);
|
||||
assert.equal(statuses.length, 3);
|
||||
assert.equal(statuses.length, 3, "only the chosen House of RC class");
|
||||
assert.equal(await evaluate(`document.querySelector("[data-hor-class]").value`), "__new__", "no class with that name: offered as a new class");
|
||||
await evaluate(`(() => {
|
||||
const select = document.querySelector("[data-hor-class]");
|
||||
select.value = "test_class"; select.dispatchEvent(new Event("change", { bubbles: true }));
|
||||
@@ -97,7 +101,7 @@ export async function runHouseOfRcEventSmoke({ evaluate, navigate, savedState, w
|
||||
await waitFor(async () => (await savedState()).drivers.some((driver) => driver.name === "HoR Anna"), "new drivers saved");
|
||||
const drivers = (await savedState()).drivers;
|
||||
const anna = drivers.find((driver) => driver.name === "HoR Anna");
|
||||
assert.deepEqual([anna.classId, anna.club, anna.brand, anna.houseOfRcId], ["test_class", "Sundsvall RC", "XRAY X12", "901"]);
|
||||
assert.deepEqual([anna.classId, anna.club, anna.brand, anna.houseOfRcId, anna.transponder], ["test_class", "Sundsvall RC", "XRAY X12", "901", "7710001"], "the entry's transponder comes along");
|
||||
assert.equal(drivers.some((driver) => driver.name === "HoR Skipped"), false, "an unticked entry is not created");
|
||||
const ticked = await evaluate(`[...document.querySelectorAll(".wizard-participant:checked")].map((box) => box.value)`);
|
||||
const ids = drivers.filter((driver) => ["HoR Anna", "HoR Bo"].includes(driver.name)).map((driver) => driver.id);
|
||||
@@ -105,15 +109,16 @@ export async function runHouseOfRcEventSmoke({ evaluate, navigate, savedState, w
|
||||
await evaluate(`document.getElementById("raceWizardNext").click()`);
|
||||
await evaluate(`document.getElementById("raceWizardNext").click()`);
|
||||
await evaluate(`document.getElementById("raceWizardCreate").click()`);
|
||||
await waitFor(async () => (await savedState()).events.some((event) => event.name === "HoR Deltävling 2"), "the race is created");
|
||||
const event = (await savedState()).events.find((item) => item.name === "HoR Deltävling 2");
|
||||
assert.deepEqual([event.date, event.classId, event.houseOfRc?.eventId], ["2026-12-05", "test_class", 2251]);
|
||||
await waitFor(async () => (await savedState()).events.some((event) => event.houseOfRc?.eventId === 2251), "the race is created");
|
||||
const event = (await savedState()).events.find((item) => item.houseOfRc?.eventId === 2251);
|
||||
assert.deepEqual([event.date, event.classId, event.houseOfRc?.eventId, event.houseOfRc?.classId], ["2026-12-05", "test_class", 2251, 555]);
|
||||
assert.deepEqual(Object.values(event.houseOfRc.entryIds).sort(), [11, 12], "each driver's House of RC entry, for the results");
|
||||
assert.deepEqual([...event.raceConfig.driverIds].sort(), [...ids].sort());
|
||||
console.log("PASS House of RC event import: pick an event and class, entered drivers fill the race wizard, the race keeps the House of RC event id");
|
||||
} finally {
|
||||
await evaluate(`document.getElementById("houseOfRcEventOverlay")?.remove()`).catch(() => {});
|
||||
const current = await savedState();
|
||||
const eventIds = current.events.filter((event) => event.name === "HoR Deltävling 2").map((event) => event.id);
|
||||
const eventIds = current.events.filter((event) => event.houseOfRc?.eventId === 2251).map((event) => event.id);
|
||||
Object.assign(current, {
|
||||
drivers: current.drivers.filter((driver) => !String(driver.name).startsWith("HoR ")),
|
||||
events: current.events.filter((event) => !eventIds.includes(event.id)),
|
||||
|
||||
@@ -38,27 +38,33 @@ test("House of RC apply: writes only the ticked fields and links drivers whose n
|
||||
assert.equal(changed, 4, "c and e are linked by name even without ticks");
|
||||
});
|
||||
|
||||
test("House of RC entries: linked, same name, other class and new drivers", async () => {
|
||||
const { planHouseOfRcEntries, applyHouseOfRcEntries } = await import("../src/houseofrc_logic.js");
|
||||
test("House of RC entries: matched on member, transponder or name; other class and new drivers", async () => {
|
||||
const { planHouseOfRcEntries, applyHouseOfRcEntries, matchHouseOfRcClass } = await import("../src/houseofrc_logic.js");
|
||||
const register = [
|
||||
{ id: "d1", name: "Anna Andersson", classId: "2wd", transponder: "111", houseOfRcId: "5" },
|
||||
{ id: "d2", name: "Bo Berg", classId: "2wd", transponder: "222" },
|
||||
{ id: "d3", name: "Cid Ceder", classId: "4wd", transponder: "333", houseOfRcId: "7", profileId: "p3", club: "ÖRC" },
|
||||
{ id: "d2", name: "Robban", classId: "2wd", transponder: "222" },
|
||||
{ id: "d3", name: "bo berg", classId: "2wd", transponder: "" },
|
||||
{ id: "d4", name: "Cid Ceder", classId: "4wd", transponder: "333", houseOfRcId: "7", profileId: "p3", club: "ÖRC" },
|
||||
];
|
||||
const entries = [
|
||||
{ houseOfRcId: 5, name: "Anna A.", club: "ÖRC", car: "Xray XB2" },
|
||||
{ houseOfRcId: 6, name: "bo berg", club: "", car: "" },
|
||||
{ houseOfRcId: 7, name: "Cid Ceder", club: "ÖRC", car: "Associated B7" },
|
||||
{ houseOfRcId: 8, name: "Dan Ny", club: "Sundsvall RC", car: "Mugen MSB1" },
|
||||
{ entryId: 1, houseOfRcId: 5, name: "Anna A.", transponders: ["111"], club: "ÖRC", car: "Xray XB2" },
|
||||
{ entryId: 2, houseOfRcId: 6, name: "Robert Nilsson", transponders: ["222"], club: "", car: "" },
|
||||
{ entryId: 3, houseOfRcId: 9, name: "Bo Berg", transponders: ["444"], club: "", car: "" },
|
||||
{ entryId: 4, houseOfRcId: 7, name: "Cid Ceder", transponders: ["555"], club: "ÖRC", car: "Associated B7" },
|
||||
{ entryId: 5, houseOfRcId: 8, name: "Dan Ny", transponders: ["666", "667"], club: "Sundsvall RC", car: "Mugen MSB1" },
|
||||
];
|
||||
const plan = planHouseOfRcEntries(entries, register, "2wd");
|
||||
assert.deepEqual(plan.map((row) => row.status), ["linked", "name", "copy", "new"]);
|
||||
assert.deepEqual(plan.map((row) => `${row.status}:${row.driverId || row.sourceId || ""}`), ["linked:d1", "linked:d2", "linked:d3", "copy:d4", "new:"], "member, then transponder, then name");
|
||||
let n = 0;
|
||||
const ids = applyHouseOfRcEntries(register, plan, { classId: "2wd", selected: new Set([0, 1, 2, 3]), makeDriver: (fields) => ({ id: `new${++n}`, ...fields }) });
|
||||
assert.deepEqual(ids, ["d1", "d2", "new1", "new2"], "existing drivers reused, the others created, in entry order");
|
||||
assert.equal(register.find((d) => d.id === "d2").houseOfRcId, "6", "same name gets linked");
|
||||
const { driverIds, entryIds } = applyHouseOfRcEntries(register, plan, { classId: "2wd", selected: new Set([0, 1, 2, 3, 4]), makeDriver: (fields) => ({ id: `new${++n}`, ...fields }) });
|
||||
assert.deepEqual(driverIds, ["d1", "d2", "d3", "new1", "new2"]);
|
||||
assert.deepEqual(entryIds, { d1: 1, d2: 2, d3: 3, new1: 4, new2: 5 }, "each driver remembers their House of RC entry");
|
||||
assert.deepEqual([register[1].name, register[1].houseOfRcId], ["Robban", "6"], "our name is kept, the member is linked");
|
||||
assert.equal(register[2].transponder, "444", "an empty transponder is filled from the entry");
|
||||
const cid = register.find((d) => d.id === "new1");
|
||||
assert.deepEqual([cid.classId, cid.transponder, cid.profileId, cid.brand], ["2wd", "333", "p3", "Associated B7"], "other class: same person, profile and transponder, car from the entry");
|
||||
assert.deepEqual([cid.classId, cid.transponder, cid.profileId, cid.brand], ["2wd", "555", "p3", "Associated B7"], "other class: same profile, this entry's transponder");
|
||||
const dan = register.find((d) => d.id === "new2");
|
||||
assert.deepEqual([dan.name, dan.transponder, dan.club, dan.houseOfRcId], ["Dan Ny", "", "Sundsvall RC", "8"]);
|
||||
assert.deepEqual([dan.name, dan.transponder, dan.club, dan.houseOfRcId], ["Dan Ny", "666", "Sundsvall RC", "8"]);
|
||||
assert.equal(matchHouseOfRcClass([{ id: "c1", name: "Offroad 2WD foam" }], "offroad 2wd Foam"), "c1");
|
||||
assert.equal(matchHouseOfRcClass([{ id: "c1", name: "Touring" }], "F1"), "");
|
||||
});
|
||||
@@ -733,18 +733,29 @@ 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";
|
||||
const missing = ok && req.url.startsWith("/v1/events/") && req.url !== "/v1/events/2251";
|
||||
res.writeHead(!ok ? 401 : missing ? 404 : 200, { "Content-Type": "application/json" });
|
||||
if (missing) return res.end(JSON.stringify({ message: "Not found." }));
|
||||
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" });
|
||||
if (missing || forbidden) return res.end(JSON.stringify({ message: forbidden ? "This API key may not access this event." : "Not found." }));
|
||||
if (ok && req.url.startsWith("/v1/events?")) {
|
||||
return res.end(JSON.stringify({ data: [
|
||||
{ id: 2251, name: "Deltävling 2 Sundsvall", type: "race", status: "live", start: "2026-12-05T07:00:00Z", city: "Sundsvall", country: "SE", web_url: "https://houseofrc.com/events/2251" },
|
||||
{ id: 3001, name: "Winter Cup", type: "race", status: null, start: "2026-12-06T07:00:00Z", city: "Oslo", country: "NO" },
|
||||
], links: { next: null }, meta: {} }));
|
||||
}
|
||||
if (ok && req.url === "/v1/events/2251") return res.end(JSON.stringify({ data: { id: 2251, name: "Deltävling 2 Sundsvall", type: "race", start: "2026-12-05T07:00:00Z", city: "Sundsvall", country: "SE", web_url: "https://houseofrc.com/events/2251" } }));
|
||||
if (ok && req.url === "/results?event_id=2251") {
|
||||
return res.end(JSON.stringify([{ user_id: 68, name: "Anna Andersson", firstname: "Anna", lastname: "Andersson", number: 7, points: 0, club: "Östersunds RC", country: "Sweden", product: "X12", brand1: "XRAY" }]));
|
||||
if (ok && req.url === "/v1/timing/events/2251/entries") {
|
||||
return res.end(JSON.stringify({ data: {
|
||||
event: { id: 2251, name: "Deltävling 2 Sundsvall", type: "race", start: "2026-12-05T07:00:00Z", city: "Sundsvall", country: "SE", web_url: "https://houseofrc.com/events/2251" },
|
||||
classes: [{ id: 555, name: "Offroad 2wd", order: 1 }, { id: 556, name: "Camping" }],
|
||||
entries: [
|
||||
{ id: 98765, class_id: 555, user_id: 68, status: "confirmed", driver_number: 7, transponders: ["2939397"], club: { id: 233, name: "Jämtlands MK" }, car: { id: 1, name: "X12" }, brands: [{ id: 70, name: "XRAY" }, null, null, null, null] },
|
||||
{ id: 98766, class_id: 555, user_id: 69, status: "entrycancelled", transponders: [], club: null, car: null, brands: [] },
|
||||
],
|
||||
users: [
|
||||
{ id: 68, name: "Anna Andersson", country: "SE", email: "anna@example.com", phone: "070-1234567", emergency_contact: { name: "Bo", info: "070-7654321" }, age: 41 },
|
||||
{ id: 69, name: "Cancelled Carl", country: "SE", email: "carl@example.com" },
|
||||
],
|
||||
} }));
|
||||
}
|
||||
if (ok && req.url.startsWith("/timing/users")) {
|
||||
// Newest owner first; an unknown number is simply missing.
|
||||
@@ -753,7 +764,7 @@ test("House of RC: the key is stored server-side only, never returned, and the t
|
||||
{ id: 9, name: "Old Owner", transponder: "1234567" },
|
||||
]));
|
||||
}
|
||||
res.end(JSON.stringify(ok ? { data: { user: { id: 68, name: "Test Club" }, rate_limit: 60 } } : { error: "unauthorized" }));
|
||||
res.end(JSON.stringify(ok ? { data: { organization: { id: 233, name: "Test Club" }, rate_limit: 60 } } : { error: "unauthorized" }));
|
||||
});
|
||||
await new Promise((resolve) => mock.listen(0, "127.0.0.1", resolve));
|
||||
const dataDir = mkdtempSync(join(tmpdir(), "live-rc-test-hor-"));
|
||||
@@ -779,19 +790,25 @@ test("House of RC: the key is stored server-side only, never returned, and the t
|
||||
const saved = await (await post("/api/houseofrc/key", { apiKey: " hrc_test_secret_key_1234 " })).json();
|
||||
assert.deepEqual([saved.configured, saved.source, saved.keyHint], [true, "file", "…1234"]);
|
||||
const test = await (await post("/api/houseofrc/test", {})).json();
|
||||
assert.deepEqual([test.ok, test.user.name, test.rateLimit], [true, "Test Club", 60]);
|
||||
assert.deepEqual([test.ok, test.organization.name, test.rateLimit], [true, "Test Club", 60], "a club key answers with its organization");
|
||||
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 events = await (await post("/api/houseofrc/events", { from: "2026-12-01T00:00:00Z", to: "2026-12-31T23:59:59Z", country: "se" })).json();
|
||||
assert.deepEqual(events.events.map((event) => event.id), [2251], "filtered to the chosen country");
|
||||
assert.match(seen.at(-1).url, /^\/v1\/events\?start_from=2026-12-01.*sort=start&per_page=100&page=1$/);
|
||||
const entries = await (await post("/api/houseofrc/entries", { eventId: "https://houseofrc.com/events/2251" })).json();
|
||||
const events = await (await post("/api/houseofrc/events", { from: "2026-12-01", to: "2026-12-31" })).json();
|
||||
assert.equal(events.organizationId, 233);
|
||||
assert.equal(seen.at(-1).url, "/v1/events?parent_id=233&from=2026-12-01&to=2026-12-31&sort=start&per_page=100&page=1", "a club key lists the club's own events");
|
||||
const response = await post("/api/houseofrc/entries", { eventId: "https://houseofrc.com/events/2251" });
|
||||
const raw = await response.text();
|
||||
assert.equal(seen.at(-1).url, "/v1/timing/events/2251/entries");
|
||||
assert.ok(!/anna@|070-|emergency|"age"/.test(raw), "email, phone, emergency contact and age never reach the browser");
|
||||
const entries = JSON.parse(raw);
|
||||
assert.equal(entries.event.name, "Deltävling 2 Sundsvall");
|
||||
assert.deepEqual(entries.entries, [{ houseOfRcId: 68, name: "Anna Andersson", number: 7, club: "Östersunds RC", country: "Sweden", car: "XRAY X12", brand: "XRAY", points: 0 }], "a pasted link works; car = make + model");
|
||||
assert.deepEqual(entries.classes.map((item) => item.name), ["Offroad 2wd", "Camping"]);
|
||||
assert.deepEqual(entries.entries, [{ entryId: 98765, classId: 555, houseOfRcId: 68, name: "Anna Andersson", country: "SE", number: 7, transponders: ["2939397"], club: "Jämtlands MK", car: "XRAY X12", status: "confirmed" }], "cancelled entries left out; car = make + model");
|
||||
assert.equal((await post("/api/houseofrc/entries", { eventId: "nonsense" })).status, 400);
|
||||
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 keyFile = join(dataDir, "houseofrc.json");
|
||||
assert.equal(statSync(keyFile).mode & 0o777, 0o600, "only the server user can read the key file");
|
||||
|
||||
Reference in new issue
Block a user