import assert from "node:assert/strict"; // A driver with several classes (one car each) went out with the 4WD, started a pass on the // phone and saw no laps: the profile opened on the first registered class, and passes and // exercises only count the selected class. It now opens on the class driven last, and points // out a switch when the latest laps today are in another class. export async function runProfileClassSmoke({ evaluate, savedState, waitFor, base, send }) { const seeded = await savedState(); const token = "browser-profile-class-token-123456789012"; const now = Date.now(); const lap = (driverId, minutesAgo, lapMs) => ({ id: `pc_${driverId}_${minutesAgo}`, sourceKey: `pc_${driverId}_${minutesAgo}`, driverId, sessionId: "pc_session", timestamp: now - minutesAgo * 60000, lapMs }); const state = { ...seeded, classes: [...seeded.classes, { id: "pc_2wd", name: "PC 2WD" }, { id: "pc_4wd", name: "PC 4WD" }, { id: "pc_truck", name: "PC Truck" }], drivers: [...seeded.drivers, { id: "pc_driver_2wd", profileId: "pc_profile", name: "PC Driver", classId: "pc_2wd" }, { id: "pc_driver_4wd", profileId: "pc_profile", name: "PC Driver", classId: "pc_4wd" }, { id: "pc_driver_truck", profileId: "pc_profile", name: "PC Driver", classId: "pc_truck" }], training: { ...(seeded.training || {}), shares: [...(seeded.training?.shares || []), { driverId: "pc_driver_2wd", token, enabled: true }], ledger: [...(seeded.training?.ledger || []), lap("pc_driver_2wd", 300, 20000), lap("pc_driver_2wd", 299, 19800), lap("pc_driver_4wd", 3, 17000), lap("pc_driver_4wd", 2, 16800)], }, }; const response = await fetch(base + "/api/state", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(state) }); assert.equal(response.ok, true); const selectedClass = () => evaluate(`document.getElementById("profileClass")?.value`); await send("Page.navigate", { url: `${base}/public-training/${token}?lang=en` }); await waitFor(async () => (await selectedClass()) === "pc_4wd", "the profile opens on the 4WD, the class driven last"); assert.equal(await evaluate(`Boolean(document.querySelector("[data-switch-class]"))`), false, "no switch hint when the right class is shown"); console.log("PASS the mobile profile opens on the class driven last, not the first registered one"); await send("Page.navigate", { url: `${base}/public-training/${token}?lang=en&class=pc_2wd` }); await waitFor(async () => Boolean(await evaluate(`document.querySelector("[data-switch-class]")`)), "switch hint shown on another class"); assert.match(await evaluate(`document.querySelector(".profile-class-switch").innerText`), /PC 4WD/); await evaluate(`document.querySelector("[data-switch-class]").click()`); await waitFor(async () => (await selectedClass()) === "pc_4wd", "the button switches to the 4WD"); assert.match(await evaluate(`location.search`), /class=pc_4wd/); assert.equal(await evaluate(`Boolean(document.querySelector("[data-switch-class]"))`), false); console.log("PASS on another class, the profile says the latest laps are in the 4WD and switches in one tap"); // Pro: a clean practice view with today's numbers and the stint cards only, // remembered on the phone and kept in the link. const visible = (selector) => evaluate(`(() => { const el = document.querySelector(${JSON.stringify(selector)}); return Boolean(el && el.offsetParent); })()`); assert.equal(await visible(".profile-pro"), false, "Standard by default"); await evaluate(`document.querySelector('[data-profile-mode="pro"]').click()`); await waitFor(() => visible(".profile-pro"), "Pro view shown"); assert.match(await evaluate(`location.search`), /mode=pro/); assert.equal(await visible(".profile-performance"), false, "stint statistics, goals and exercises are hidden"); assert.equal(await visible(".profile-motivation-grid"), false); assert.equal(await evaluate(`document.querySelectorAll(".profile-pro .stint-laps-card").length`), 1, "today's stint"); await evaluate(`document.querySelector(".profile-pro .stint-laps-card > summary").click()`); assert.equal(await evaluate(`document.querySelectorAll(".profile-pro .stint-lap").length`), 2, "both laps listed"); await send("Page.navigate", { url: `${base}/public-training/${token}?lang=en` }); await waitFor(() => visible(".profile-pro"), "the phone remembers Pro"); await evaluate(`document.querySelector('[data-profile-mode="standard"]').click()`); await waitFor(async () => !(await visible(".profile-pro")) && (await visible(".profile-motivation-grid")), "back to Standard"); assert.doesNotMatch(await evaluate(`location.search`), /mode=/); console.log("PASS the mobile profile has a Standard / Pro switch: Pro shows only today's numbers and the fold-out stints, and is remembered"); }