import assert from "node:assert/strict"; export async function runRaceSetupSmoke({ evaluate, navigate, fill, savedState, waitFor, pause }) { const click = id => evaluate(`document.getElementById(${JSON.stringify(id)}).click()`); const submit = id => evaluate(`document.getElementById(${JSON.stringify(id)}).requestSubmit()`); const findEvent = async name => (await savedState()).events.find(event => event.name === name); async function editEvent(event, view, readyForm) { await click("event-edit-" + event.id); await fill("eventEditForm", { name: event.name + " Updated" }); await submit("eventEditForm"); await waitFor(async () => Boolean(await findEvent(event.name + " Updated")), "event name saved"); await navigate(view, readyForm); await click("event-edit-" + event.id); assert.equal(await evaluate('document.getElementById("eventEditForm").elements.namedItem("name").value'), event.name + " Updated"); await click("eventEditCancel"); await click("event-manage-" + event.id); } await navigate("events", "eventForm"); await fill("eventForm", { name: "Browser Sponsor", date: "2026-09-08", classId: "test_class" }); await submit("eventForm"); await waitFor(async () => Boolean(await findEvent("Browser Sponsor")), "create sponsor event"); const sponsor = await findEvent("Browser Sponsor"); await editEvent(sponsor, "events", "eventForm"); await fill("sessionForm", { name: "Sponsor Heat", type: "heat", durationMin: "5", startMode: "mass" }); await submit("sessionForm"); await waitFor(async () => (await savedState()).sessions.some(session => session.name === "Sponsor Heat" && session.eventId === sponsor.id), "sponsor session"); console.log("PASS sponsor-event create/edit/reload and session creation"); await navigate("race_setup", "raceWizardStepForm"); await fill("raceWizardStepForm", { name: "Browser Endurance", date: "2026-09-08", classId: "test_class", presetId: "endurance" }); await click("raceWizardNext"); await click("wizardSelectAllParticipants"); await click("raceWizardNext"); await evaluate('document.querySelector("#raceWizardPlanForm [name=createTeamRace]").checked = true'); await fill("raceWizardPlanForm", { teamRaceDurationMin: "45" }); await click("raceWizardNext"); await click("raceWizardCreate"); await waitFor(async () => Boolean(await findEvent("Browser Endurance")), "wizard race creation"); const race = await findEvent("Browser Endurance"); // A wizard race with practice + qualifying: the qualifying is real heats (Q H, // generated), so "Create qualifying from practice" later replaces them instead of adding more. await navigate("race_setup", "raceWizardStepForm"); await fill("raceWizardStepForm", { name: "Browser Club Race", date: "2026-09-08", classId: "test_class", presetId: "club_qualifying" }); await click("raceWizardNext"); await click("wizardSelectAllParticipants"); await click("raceWizardNext"); await evaluate('(() => { const form = document.getElementById("raceWizardPlanForm"); form.elements.createPractice.checked = true; form.elements.createQualifying.checked = true; form.elements.createPractice.dispatchEvent(new Event("change", { bubbles: true })); form.elements.createQualifying.dispatchEvent(new Event("change", { bubbles: true })); })()'); await fill("raceWizardPlanForm", { practiceSessions: "1", qualifyingRounds: "2" }); await click("raceWizardNext"); await click("raceWizardCreate"); await waitFor(async () => Boolean(await findEvent("Browser Club Race")), "wizard club race creation"); { const club = await findEvent("Browser Club Race"); const clubSessions = (await savedState()).sessions.filter((session) => session.eventId === club.id); const quals = clubSessions.filter((session) => session.type === "qualification"); assert.equal(clubSessions.filter((session) => session.type === "practice").length, 1, "one practice"); assert.ok(quals.length >= 2 && quals.every((session) => /^Q[12] H\d+$/.test(session.name) && session.generated), `qualifying heats per round: ${quals.map((session) => session.name)}`); assert.equal(club.raceConfig.qualifyingRounds, 2); console.log("PASS wizard builds qualifying as Q H heats"); // A driver left out can be ticked again, and the heats not run yet follow the list. await click("event-manage-" + club.id); await waitFor(async () => (await evaluate('document.querySelectorAll(".race-participant").length')) > 0, "participant list"); const first = await evaluate('document.querySelector(".race-participant:checked").value'); await evaluate(`document.querySelector('.race-participant[value="${first}"]').click()`); await waitFor(async () => !(await savedState()).sessions.some((session) => session.eventId === club.id && session.type === "qualification" && session.driverIds.includes(first)), "unticked driver leaves the heats"); await waitFor(async () => Boolean(await evaluate(`Boolean(document.querySelector('.race-participant[value="${first}"]:not(:checked)'))`)), "unticked driver still listed"); await evaluate(`document.querySelector('.race-participant[value="${first}"]').click()`); await waitFor(async () => (await savedState()).sessions.some((session) => session.eventId === club.id && session.type === "qualification" && session.driverIds.includes(first)), "ticked again: back in a heat"); console.log("PASS race participants can be changed after creation and the heats follow"); } await navigate("race_setup", "raceWizardStepForm"); await editEvent(race, "race_setup", "raceWizardStepForm"); const current = await savedState(); const driver = current.drivers.find(item => item.name === "Browser Driver Updated"); const car = current.cars.find(item => item.name === "Browser Car Updated"); const raceState = () => findEvent("Browser Endurance Updated"); await fill("teamForm", { teamName: "Browser Team" }); await evaluate(`document.querySelector('[form="teamForm"][name="teamDriverIds"][value="${driver.id}"]').checked=true`); await submit("teamForm"); await waitFor(async () => (await raceState()).raceConfig.teams.some(team => team.name === "Browser Team"), "team create"); const team = (await raceState()).raceConfig.teams.find(item => item.name === "Browser Team"); assert.deepEqual(team.driverIds, [driver.id]); await click("team-edit-" + team.id); await fill("teamEditForm", { teamName: "Browser Team Updated" }); await evaluate(`document.querySelector('#teamEditForm [name="teamCarIds"][value="${car.id}"]').checked=true`); await pause(5500); assert.equal(await evaluate('document.getElementById("teamEditForm").elements.namedItem("teamName").value'), "Browser Team Updated"); await click("teamEditSave"); await waitFor(async () => (await raceState()).raceConfig.teams.some(item => item.name === "Browser Team Updated" && item.carIds.includes(car.id)), "team membership update"); await navigate("race_setup", "raceWizardStepForm"); await click("event-manage-" + race.id); await click("team-edit-" + team.id); assert.equal(await evaluate('document.getElementById("teamEditForm").elements.namedItem("teamName").value'), "Browser Team Updated"); assert.ok(await evaluate(`document.querySelector('#teamEditForm [name="teamCarIds"][value="${car.id}"]').checked`)); await click("teamEditCancel"); await fill("sessionForm", { name: "Editable Practice", type: "open_practice", durationMin: "5" }); await submit("sessionForm"); await waitFor(async () => (await savedState()).sessions.some(session => session.name === "Editable Practice"), "practice create"); const session = (await savedState()).sessions.find(item => item.name === "Editable Practice"); await click("session-edit-" + session.id); await fill("sessionEditForm", { name: "Edited Team Session", type: "team_race", durationMin: "45", followUpSec: "30" }); await submit("sessionEditForm"); await waitFor(async () => (await savedState()).sessions.some(item => item.id === session.id && item.type === "team_race" && item.durationMin === 45 && item.followUpSec === 30), "session edit type and duration"); await fill("raceFormatForm", { minLapSec: "30", maxLapSec: "120" }); await submit("raceFormatForm"); await waitFor(async () => (await raceState()).raceConfig.minLapMs === 30000 && (await raceState()).raceConfig.maxLapMs === 120000, "lap limits saved"); await navigate("race_setup", "raceWizardStepForm"); await click("event-manage-" + race.id); await click("session-edit-" + session.id); assert.equal(await evaluate('document.getElementById("sessionEditForm").elements.namedItem("type").value'), "team_race"); await click("sessionEditCancel"); assert.equal(await evaluate('document.getElementById("raceFormatForm").elements.namedItem("minLapSec").value'), "30.0"); await click("team-delete-" + team.id); await waitFor(async () => !(await raceState()).raceConfig.teams.some(item => item.id === team.id), "team deletion"); console.log("PASS race wizard/edit/reload, team create/member-edit/delete, session type/duration and lap limits"); // Running order: the sessions are listed in it, and ▲ moves one earlier. for (const name of ["Q1 H2", "Q1 H1"]) { await fill("sessionForm", { name, type: "qualification", durationMin: "5" }); await submit("sessionForm"); await waitFor(async () => (await savedState()).sessions.some((item) => item.name === name && item.eventId === race.id), name + " created"); } const listed = async () => evaluate(`[...document.querySelectorAll("[data-session-move][data-step='1']")].map((button) => button.dataset.sessionMove)`); const sessionNames = async () => { const ids = await listed(); const all = (await savedState()).sessions; return ids.map((id) => all.find((item) => item.id === id)?.name).filter((name) => /^Q1/.test(name)); }; await waitFor(async () => (await sessionNames()).join() === "Q1 H1,Q1 H2", "listed in running order although Q1 H1 was made last"); const q12 = (await savedState()).sessions.find((item) => item.name === "Q1 H2" && item.eventId === race.id); await evaluate(`document.querySelector('[data-session-move="${q12.id}"][data-step="-1"]').click()`); await waitFor(async () => (await sessionNames()).join() === "Q1 H2,Q1 H1", "Q1 H2 moved up"); const q11 = (await savedState()).sessions.find((item) => item.name === "Q1 H1" && item.eventId === race.id); await waitFor(async () => { const order = (await raceState()).sessionOrder || []; return order.includes(q12.id) && order.indexOf(q12.id) < order.indexOf(q11.id); }, "the order is saved on the race"); console.log("PASS Race Setup lists sessions in running order and ▲▼ change it"); // The race list: races from past dates are folded away under Done; search finds them. await navigate("race_setup", "raceWizardStepForm"); const raceId = (await raceState()).id; assert.equal(await evaluate(`document.querySelector('[data-event-row="${raceId}"]').closest("details")?.id || ""`), "eventListDone", "a race from a past date is under Done"); assert.equal(await evaluate(`document.getElementById("eventListDone").open`), false, "Done is folded by default"); if (await evaluate(`Boolean(document.getElementById("eventListSearch"))`)) { await evaluate(`(() => { const box = document.getElementById("eventListSearch"); box.value = "endurance"; box.dispatchEvent(new Event("input")); })()`); assert.equal(await evaluate(`document.getElementById("eventListDone").open`), true, "search opens Done"); assert.equal(await evaluate(`document.querySelector('[data-event-row="${raceId}"]').hidden`), false); assert.ok(await evaluate(`[...document.querySelectorAll("[data-event-row]")].some((row) => row.hidden)`), "other races are filtered out"); } console.log("PASS Race Setup groups races into today, upcoming and a folded Done list, with search"); // Manage: sessions grouped per step; print/export/delete live in the ⋯ menu. await click("event-manage-" + raceId); await waitFor(async () => Boolean(await evaluate(`document.querySelector('details[data-session-group="qual_1"]')`)), "qualifying round 1 is its own group"); assert.equal(await evaluate(`document.querySelector('details[data-session-group="qual_1"]').open`), true, "a step not yet run is open"); assert.equal(await evaluate(`Boolean(document.getElementById("session-delete-${q11.id}").closest("details.row-menu"))`), true, "delete is in the ⋯ menu"); console.log("PASS Manage groups sessions per step with the rarer actions in a ⋯ menu"); }