125 lines
8.8 KiB
JavaScript
125 lines
8.8 KiB
JavaScript
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_")) });
|
|
}
|
|
}
|
|
|
|
// Race wizard → "Fetch from House of RC": pick an event, choose our class, the entered drivers
|
|
// fill the wizard; the created event remembers the House of RC event for the results later.
|
|
export async function runHouseOfRcEventSmoke({ evaluate, navigate, savedState, waitFor, base }) {
|
|
try {
|
|
await navigate("race_setup", "raceWizardStepForm");
|
|
await evaluate(`(() => {
|
|
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: "" },
|
|
] });
|
|
return realFetch(url, options);
|
|
};
|
|
})()`);
|
|
await evaluate(`document.getElementById("raceWizardHouseOfRc").click()`);
|
|
await waitFor(async () => evaluate(`Boolean(document.querySelector("[data-hor-event]"))`), "the event list");
|
|
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);
|
|
await evaluate(`(() => {
|
|
const select = document.querySelector("[data-hor-class]");
|
|
select.value = "test_class"; select.dispatchEvent(new Event("change", { bubbles: true }));
|
|
})()`);
|
|
await evaluate(`(() => { document.querySelector('[data-hor-entry="2"]').checked = false; document.querySelector("[data-hor-apply]").click(); })()`);
|
|
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.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);
|
|
assert.deepEqual([...ticked].sort(), [...ids].sort(), "the wizard is on Participants with exactly the entered drivers");
|
|
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]);
|
|
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);
|
|
Object.assign(current, {
|
|
drivers: current.drivers.filter((driver) => !String(driver.name).startsWith("HoR ")),
|
|
events: current.events.filter((event) => !eventIds.includes(event.id)),
|
|
sessions: current.sessions.filter((session) => !eventIds.includes(session.eventId)),
|
|
});
|
|
await fetch(base + "/api/state", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(current) });
|
|
}
|
|
}
|