import test from "node:test"; import assert from "node:assert/strict"; import { groupParticipantChoices, participantCopySources, renderParticipantPicker } from "../src/participant_picker.js"; const classes = [{ id: "r", name: "Rookie" }, { id: "b2", name: "Rookie 2WD" }, { id: "b4", name: "Rookie 4WD" }, { id: "j4", name: "Junior 4WD" }]; const drivers = [ { id: "1", name: "Ola", classId: "b2" }, { id: "2", name: "Ada", classId: "b2" }, { id: "3", name: "Kim", classId: "b4" }, { id: "4", name: "Eli", classId: "j4" }, { id: "5", name: "Noa", classId: "" }, ]; const escapeHtml = (v) => String(v).replace(/[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c]); const t = (key, params = {}) => key.replace(/\{(\w+)\}/g, (_, k) => params[k]) + Object.values(params).join(" "); test("drivers are grouped by class: the race's own class first (even empty), then classes with someone ticked", () => { const groups = groupParticipantChoices(drivers, classes, { primaryClassId: "r", selectedIds: ["4"], noClassLabel: "No class" }); assert.deepEqual(groups.map((g) => g.name), ["Rookie", "Junior 4WD", "Rookie 2WD", "Rookie 4WD", "No class"]); assert.equal(groups[0].primary, true); assert.equal(groups[0].drivers.length, 0); assert.deepEqual(groups.find((g) => g.key === "b2").drivers.map((d) => d.name), ["Ada", "Ola"]); assert.equal(groups.find((g) => g.key === "j4").selected, 1); }); test("the picker renders a checkbox per driver with the caller's attributes, checked as selected", () => { const html = renderParticipantPicker({ pickerKey: "x", drivers, classes, selectedIds: ["1"], primaryClassId: "b2", inputAttrs: () => 'class="race-participant"', t, escapeHtml }); assert.equal((html.match(/class="race-participant"/g) || []).length, 5); assert.match(html, /class="race-participant" value="1" data-picker-driver checked/); assert.match(html, /data-picker-group="b2" open/, "own class open"); assert.doesNotMatch(html, /data-picker-group="j4" open/, "other classes folded"); }); test("copy sources: earlier races with drivers, the series' rounds first, then newest", () => { const events = [ { id: "a", mode: "race", name: "Club", date: "2026-10-01" }, { id: "b", mode: "race", name: "Cup 1", date: "2026-09-01" }, { id: "c", mode: "race", name: "Empty", date: "2026-11-01" }, { id: "d", mode: "track", name: "Practice", date: "2026-11-02" }, { id: "me", mode: "race", name: "Cup 2", date: "2026-11-10" }, ]; const ids = { a: ["1"], b: ["1", "2"], c: [], d: ["3"], me: ["4"] }; const sources = participantCopySources(events, { currentEventId: "me", seriesEventIds: ["b", "me"], getDriverIds: (e) => ids[e.id] }); assert.deepEqual(sources.map((s) => s.id), ["b", "a"]); assert.equal(sources[0].label, "๐Ÿ† 2026-09-01 ยท Cup 1 (2)"); });