191 lines
15 KiB
JavaScript
191 lines
15 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");
|
|
const installMock = `(() => {
|
|
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, 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: window.__horEntries || [
|
|
{ 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: "" },
|
|
] });
|
|
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);
|
|
};
|
|
})()`;
|
|
await evaluate(installMock);
|
|
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, "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 }));
|
|
})()`);
|
|
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, 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");
|
|
await waitFor(async () => ((await savedState()).classes.find((item) => item.id === "test_class")?.houseOfRcNames || []).includes("HoR 2wd"), "the class choice is remembered on the server");
|
|
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.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");
|
|
|
|
// Later: Bo withdraws, Cecilia enters late. Manage → "Fetch changes from House of RC".
|
|
await evaluate(`window.__horEntries = [
|
|
{ entryId: 11, classId: 555, houseOfRcId: 901, name: "HoR Anna", number: 7, transponders: ["7710001"], club: "Sundsvall RC", car: "XRAY X12" },
|
|
{ entryId: 16, classId: 555, houseOfRcId: 906, name: "HoR Late Cecilia", number: null, transponders: ["7710006"], club: "", car: "" },
|
|
]`);
|
|
await waitFor(async () => evaluate(`Boolean(document.getElementById("houseOfRcRefreshEntries"))`), "the refresh button in Manage");
|
|
await evaluate(`document.getElementById("houseOfRcRefreshEntries").click()`);
|
|
await waitFor(async () => evaluate(`Boolean(document.querySelector("#houseOfRcRefreshOverlay [data-hor-apply]"))`), "the changes");
|
|
const shown = await evaluate(`({ add: document.querySelectorAll("[data-hor-add]").length, remove: [...document.querySelectorAll("[data-hor-remove]")].map((box) => box.closest("tr").children[1].textContent) })`);
|
|
assert.deepEqual(shown, { add: 1, remove: ["HoR Bo"] });
|
|
await evaluate(`document.querySelector("#houseOfRcRefreshOverlay [data-hor-apply]").click()`);
|
|
await waitFor(async () => (await savedState()).drivers.some((driver) => driver.name === "HoR Late Cecilia"), "the late entry is created");
|
|
{
|
|
const saved = await savedState();
|
|
const race = saved.events.find((item) => item.houseOfRc?.eventId === 2251);
|
|
const name = (id) => saved.drivers.find((driver) => driver.id === id)?.name;
|
|
assert.deepEqual(race.raceConfig.driverIds.map(name), ["HoR Anna", "HoR Late Cecilia"], "Bo out, Cecilia in, order kept");
|
|
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.lastSend?.ok), "how 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");
|
|
|
|
// "Send heats automatically": a qualifying heat finishes, the race controller sends it.
|
|
await evaluate(`document.getElementById("houseOfRcAutoSend").click()`);
|
|
await waitFor(async () => (await savedState()).events.find((item) => item.houseOfRc?.eventId === 2251)?.houseOfRc.autoSend === true, "auto-send saved");
|
|
{
|
|
const saved = await savedState();
|
|
const race = saved.events.find((item) => item.houseOfRc?.eventId === 2251);
|
|
const heat = saved.sessions.find((session) => session.eventId === race.id && session.type === "qualification");
|
|
Object.assign(heat, { status: "finished", startedAt: Date.now() - 400000, endedAt: Date.now() - 100000 });
|
|
await fetch(base + "/api/state", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(saved) });
|
|
}
|
|
await navigate("race_day", "raceDaySessionSelect");
|
|
await evaluate(installMock);
|
|
await waitFor(async () => evaluate(`Boolean(window.__horPosted)`), "sent by itself after the heat", 30000);
|
|
assert.ok((await evaluate(`window.__horPosted.entries`)).every((entry) => !("ranking" in entry)), "automatic sending never sends placings");
|
|
await waitFor(async () => (await savedState()).events.find((item) => item.houseOfRc?.eventId === 2251)?.houseOfRc.lastSend?.signature, "the send is remembered");
|
|
console.log("PASS House of RC auto-send: a finished heat is sent by the race controller without placings");
|
|
} finally {
|
|
await evaluate(`document.getElementById("houseOfRcEventOverlay")?.remove()`).catch(() => {});
|
|
const current = await savedState();
|
|
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 ")),
|
|
classes: current.classes.map(({ houseOfRcNames, ...item }) => item),
|
|
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) });
|
|
}
|
|
}
|