import assert from "node:assert/strict"; // Drivers page: the search field keeps focus while a whole name is typed (it used to lose it // after the first letter, because every keystroke re-renders the page), and "Test // pronunciation" speaks the spoken name - the unsaved one in the form - through race audio. export async function runDriversPageSmoke({ evaluate, navigate, savedState, waitFor, base }) { const patchState = async (patch) => { const current = await savedState(); Object.assign(current, patch); const response = await fetch(base + "/api/state", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(current) }); assert.equal(response.ok, true); }; const seeded = await savedState(); await patchState({ drivers: [...seeded.drivers, { id: "dp_anders", name: "DP Anders", speechName: "Anders the Rocket", transponder: "8550001" }], settings: { ...seeded.settings, ttsEngine: "browser", raceAudio: { ...seeded.settings.raceAudio, enabled: true, muted: false, language: "sv", events: {} } }, }); try { await navigate("drivers", "driverForm"); // Type letter by letter, like a person. for (const letter of "DP And") { const ok = await evaluate(`(() => { const input = document.getElementById("driverBrandFilter"); if (document.activeElement !== input) input.focus(); input.value += ${JSON.stringify(letter)}; input.setSelectionRange(input.value.length, input.value.length); input.dispatchEvent(new Event("input", { bubbles: true })); return document.activeElement?.id === "driverBrandFilter"; })()`); assert.equal(ok, true, `focus kept after typing "${letter}"`); } assert.equal(await evaluate(`document.getElementById("driverBrandFilter").value`), "DP And"); assert.match(await evaluate(`document.querySelector(".panel table, table")?.innerText || document.body.innerText`), /DP Anders/); console.log("PASS the driver search keeps focus so a whole name can be typed"); // Capture what the browser voice is asked to say. await evaluate(`(() => { window.__spoken = []; window.speechSynthesis.speak = (utterance) => { window.__spoken.push(utterance.text); setTimeout(() => utterance.onend?.(), 10); }; })()`); await evaluate(`document.querySelector('[data-driver-voice-row="dp_anders"]').click()`); await waitFor(async () => (await evaluate(`window.__spoken.join(" | ")`)).includes("Anders the Rocket"), "the saved spoken name is read out"); // In the add form: an unsaved nickname is what you hear. await evaluate(`(() => { window.__spoken = []; const form = document.getElementById("driverForm"); form.elements.name.value = "New Kid"; form.elements.speechName.value = "Kiddo"; form.querySelector("[data-driver-voice-test]").click(); })()`); await waitFor(async () => (await evaluate(`window.__spoken.join(" | ")`)).includes("Kiddo"), "the unsaved nickname is read out"); assert.equal((await savedState()).drivers.some((driver) => driver.name === "New Kid"), false, "testing doesn't save the driver"); console.log("PASS Test pronunciation reads the spoken name (also an unsaved one) through race audio"); // Class filter, grouping and selecting several drivers for one delete. const before = await savedState(); const classId = before.classes[0]?.id || ""; await patchState({ drivers: [...before.drivers, { id: "dp_bulk1", name: "DP Bulk One", classId, transponder: "8550101" }, { id: "dp_bulk2", name: "DP Bulk Two", classId, transponder: "8550102" }] }); await navigate("drivers", "driverForm"); await evaluate(`(() => { const input = document.getElementById("driverBrandFilter"); input.value = "DP Bulk"; input.dispatchEvent(new Event("input", { bubbles: true })); return true; })()`); await waitFor(async () => (await evaluate(`document.querySelectorAll("[data-driver-select]").length`)) === 2, "two drivers match the search"); assert.equal(await evaluate(`document.querySelectorAll(".driver-class-group").length >= 1`), true, "grouped per class by default"); await evaluate(`(() => { const select = document.getElementById("driverClassFilter"); select.value = ${JSON.stringify(classId)}; select.dispatchEvent(new Event("change", { bubbles: true })); return true; })()`); assert.equal(await evaluate(`document.querySelectorAll("[data-driver-select]").length`), 2, "the class filter keeps the class's drivers"); await evaluate(`(() => { const all = document.getElementById("driverSelectAll"); all.checked = true; all.dispatchEvent(new Event("change", { bubbles: true })); return true; })()`); assert.match(await evaluate(`document.getElementById("driverDeleteSelected").textContent`), /\(2\)/); await evaluate(`window.__realConfirm = window.confirm; window.__confirms = []; window.confirm = (message) => { window.__confirms.push(message); return true; }; document.getElementById("driverDeleteSelected").click(); true`); await waitFor(async () => !(await savedState()).drivers.some((driver) => driver.id.startsWith("dp_bulk")), "both selected drivers deleted"); assert.match(await evaluate(`window.__confirms[0] || ""`), /DP Bulk One/, "the confirm names the drivers"); await evaluate(`window.confirm = window.__realConfirm; const select = document.getElementById("driverClassFilter"); select.value = ""; select.dispatchEvent(new Event("change", { bubbles: true })); true`); console.log("PASS the driver list filters on class, groups per class and deletes several selected drivers after a confirm"); } finally { const current = await savedState(); await patchState({ drivers: current.drivers.filter((driver) => driver.id !== "dp_anders" && !driver.id.startsWith("dp_bulk")), settings: { ...current.settings, raceAudio: seeded.settings.raceAudio, ttsEngine: seeded.settings.ttsEngine } }); } }