import test from "node:test"; import assert from "node:assert/strict"; import { groupEventsForList, getEventProgress } from "../src/race_list_logic.js"; const s = (id, eventId, name, status = "ready", type = "qualification") => ({ id, eventId, name, status, type }); const sessions = [ s("a1", "a", "Q1 H1", "finished"), s("a2", "a", "Q1 H2"), s("b1", "b", "Q1 H1", "finished"), s("c1", "c", "Q1 H1"), s("d1", "d", "Q1 H1", "running"), ]; const events = [ { id: "a", name: "Cup #2 2WD", date: "2026-11-08" }, { id: "b", name: "Cup #1 2WD", date: "2026-11-01" }, { id: "c", name: "Old unfinished", date: "2026-10-01" }, { id: "d", name: "Live", date: "2026-10-30" }, { id: "e", name: "Cup #2 4WD", date: "2026-11-08" }, { id: "f", name: "Today", date: "2026-11-02" }, ]; const getSessions = (id) => sessions.filter((item) => item.eventId === id); test("races are grouped today / upcoming / done, per date", () => { const groups = groupEventsForList(events, getSessions, "2026-11-02"); assert.deepEqual(groups.today.flatMap((day) => day.items.map((item) => item.event.id)), ["d", "f"], "today's date and anything running"); assert.deepEqual(groups.upcoming.map((day) => [day.date, day.items.map((item) => item.event.id)]), [["2026-11-08", ["a", "e"]]], "same day together"); assert.deepEqual(groups.done.map((day) => day.date), ["2026-11-01", "2026-10-01"], "newest done first"); }); test("progress says what is running or next and how many are run", () => { assert.deepEqual(getEventProgress(events[0], getSessions("a")), { total: 2, done: 1, live: "", next: "Q1 H2" }); assert.equal(getEventProgress(events[3], getSessions("d")).live, "Q1 H1"); }); test("a race's sessions are grouped per step, and a step with every heat run is done", async () => { const { groupSessionsBySteps } = await import("../src/race_list_logic.js"); const groups = groupSessionsBySteps([ s("p", "x", "Seeding", "finished", "practice"), s("q1", "x", "Q1 H1", "finished"), s("q2", "x", "Q1 H2", "finished"), s("q3", "x", "Q2 H1", "running"), s("q4", "x", "Q2 H2"), s("b", "x", "B Final 1", "ready", "final"), s("a", "x", "A Final 1", "ready", "final"), ]); assert.deepEqual(groups.map((group) => [group.step.id, group.items.length, group.state]), [ ["practice", 1, "done"], ["qual_1", 2, "done"], ["qual_2", 2, "live"], ["final", 2, "pending"], ]); assert.equal(groups[3].items[0].index, 5, "keeps each session's place in the whole list"); });