import test from "node:test"; import assert from "node:assert/strict"; import { orderSessionsForRunning, planSeedingGroups } from "../src/event_race_logic.js"; import { getSeriesRoundNextStep } from "../src/series_logic.js"; import { buildRaceDayQueue } from "../src/race_day_logic.js"; const s = (name, type, status = "ready") => ({ id: name, name, type, status, eventId: "e" }); test("sessions run practice, then qualifying round by round, then finals leg by leg from the lowest", () => { const sessions = [ s("A Final 1", "final"), s("A Final 2", "final"), s("B Final 1", "final"), s("B Final 2", "final"), s("Q1 H1", "qualification"), s("Q2 H1", "qualification"), s("Q1 H2", "qualification"), s("Q2 H2", "qualification"), s("Seeding practice", "practice"), ]; assert.deepEqual(orderSessionsForRunning(sessions).map((item) => item.name), ["Seeding practice", "Q1 H1", "Q1 H2", "Q2 H1", "Q2 H2", "B Final 1", "A Final 1", "B Final 2", "A Final 2"]); }); test("Race Day's next heat follows the running order", () => { const sessions = [s("Q1 H1", "qualification", "finished"), s("Q2 H1", "qualification"), s("Q1 H2", "qualification")]; assert.equal(buildRaceDayQueue({ sessions, activeSessionId: "Q1 H1" }).next.name, "Q1 H2"); }); test("a series round's next step walks seeding -> qualifying -> heats -> finals -> done", () => { assert.deepEqual(getSeriesRoundNextStep([s("Seeding", "practice")]), { kind: "seeding", session: s("Seeding", "practice"), notStarted: true }); assert.deepEqual(getSeriesRoundNextStep([s("Seeding 1", "practice", "finished"), s("Seeding 2", "practice")]), { kind: "seeding", session: s("Seeding 2", "practice"), notStarted: false }); assert.deepEqual(getSeriesRoundNextStep([s("Seeding", "practice", "finished")]), { kind: "make_qualifying" }); assert.equal(getSeriesRoundNextStep([s("Seeding", "practice", "running")]).kind, "live"); const quals = [s("Q2 H1", "qualification"), s("Q1 H1", "qualification", "finished"), s("Q1 H2", "qualification")]; assert.equal(getSeriesRoundNextStep([s("Seeding", "practice"), ...quals]).session.name, "Q1 H2", "an unrun seeding practice is skipped once qualifying exists"); assert.deepEqual(getSeriesRoundNextStep([s("Q1 H1", "qualification", "finished")]), { kind: "make_finals" }); assert.equal(getSeriesRoundNextStep([s("Q1 H1", "qualification", "finished"), s("A Final 1", "final"), s("B Final 1", "final")]).session.name, "B Final 1"); assert.deepEqual(getSeriesRoundNextStep([s("Q1 H1", "qualification", "finished"), s("A Final 1", "final", "finished")]), { kind: "done" }); }); test("seeding groups: even split by max size, top of the standings last with fastest-last", () => { const ids = Array.from({ length: 10 }, (_, index) => `d${index + 1}`); assert.deepEqual(planSeedingGroups(ids, 5, "fastest_first"), [ids.slice(0, 5), ids.slice(5)]); assert.deepEqual(planSeedingGroups(ids, 5, "fastest_last"), [ids.slice(5), ids.slice(0, 5)]); assert.deepEqual(planSeedingGroups(ids.slice(0, 6), 3, "fastest_first").map((group) => group.length), [3, 3]); assert.deepEqual(planSeedingGroups(ids, 0, "fastest_first"), [ids], "0 = everyone together"); const fifty = Array.from({ length: 50 }, (_, index) => `x${index}`); assert.deepEqual(planSeedingGroups(fifty, 6, "fastest_first").map((group) => group.length), [6, 6, 6, 6, 6, 5, 5, 5, 5]); }); test("a round's hand-set result replaces the calculated one in the standings", async () => { const { normalizeSeries, getSeriesDayResult } = await import("../src/series_logic.js"); const series = normalizeSeries({ id: "s", manualResults: { e1: ["roy", "wilmer", "roy", ""], e2: [] } }); assert.deepEqual(series.manualResults, { e1: ["roy", "wilmer"] }, "duplicates and empty lists are dropped"); const calculated = [{ driverId: "patrik", driverName: "Patrik", hasLap: true }, { driverId: "roy", driverName: "Roy", hasLap: true }]; const names = { roy: "Roy Gjemble", wilmer: "Wilmer Nilsson" }; assert.deepEqual(getSeriesDayResult(series, "e1", calculated, (id) => names[id]).map((row) => row.driverName), ["Roy Gjemble", "Wilmer Nilsson"]); assert.equal(getSeriesDayResult(series, "e2", calculated), calculated, "no hand-set order: the calculated result"); }); test("running order: a recreated heat keeps its place, and ▲▼ set an order of your own", async () => { const { moveSessionInRunningOrder } = await import("../src/event_race_logic.js"); const s = (id, name, type = "qualification") => ({ id, name, type }); // Q1 H2 was deleted and made again: it is last in the list but runs after Q1 H1. const sessions = [s("p", "Seeding practice", "practice"), s("q11", "Q1 H1"), s("q21", "Q2 H1"), s("q22", "Q2 H2"), s("a1", "A Final 1", "final"), s("q12", "Q1 H2")]; const names = (list) => list.map((item) => item.name); assert.deepEqual(names(orderSessionsForRunning(sessions)), ["Seeding practice", "Q1 H1", "Q1 H2", "Q2 H1", "Q2 H2", "A Final 1"]); // Move Q2 H2 before Q2 H1. const manual = moveSessionInRunningOrder(sessions, [], "q22", -1); assert.deepEqual(names(orderSessionsForRunning(sessions, manual)), ["Seeding practice", "Q1 H1", "Q1 H2", "Q2 H2", "Q2 H1", "A Final 1"]); // A session made afterwards (not in the manual order) still gets its own place. const more = [...sessions, s("q31", "Q3 H1")]; assert.deepEqual(names(orderSessionsForRunning(more, manual)), ["Seeding practice", "Q1 H1", "Q1 H2", "Q2 H2", "Q2 H1", "Q3 H1", "A Final 1"]); assert.deepEqual(moveSessionInRunningOrder(sessions, manual, "p", -1), manual, "the first can't move up"); });