47 lines
3.1 KiB
JavaScript
47 lines
3.1 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { buildPracticeHeats, rebuildPracticeHeats, parsePracticeHeatName, syncParticipantsToSessions } from "../src/event_race_logic.js";
|
|
|
|
let n = 0;
|
|
const t = (key) => ({ "session.practice": "träning" }[key] || key);
|
|
const buildRaceSession = (eventId, name, type, durationMin, overrides = {}) => ({ id: `s${++n}`, eventId, name, type, durationMin, status: "ready", startedAt: null, driverIds: overrides.driverIds || [] });
|
|
const drivers = (count) => Array.from({ length: count }, (_, i) => `d${i + 1}`);
|
|
const event = (count, carsPerHeat, extra = {}) => ({ id: "e1", raceConfig: { driverIds: drivers(count), carsPerHeat, qualDurationMin: 5, ...extra } });
|
|
|
|
test("practice heats: each round is split into balanced heats of at most cars per heat", () => {
|
|
const sessions = buildPracticeHeats(event(12, 5), 2, { t, buildRaceSession });
|
|
assert.deepEqual(sessions.map((s) => s.name), ["träning 1 H1", "träning 1 H2", "träning 1 H3", "träning 2 H1", "träning 2 H2", "träning 2 H3"]);
|
|
assert.deepEqual(sessions.slice(0, 3).map((s) => s.driverIds.length), [4, 4, 4]);
|
|
assert.ok(sessions.every((s) => s.generated && s.type === "practice"));
|
|
});
|
|
|
|
test("practice heats: everyone in one heat keeps the plain name", () => {
|
|
assert.deepEqual(buildPracticeHeats(event(6, 10), 1, { t, buildRaceSession }).map((s) => s.name), ["träning 1"]);
|
|
});
|
|
|
|
test("practice heats: rebuilt for a new heat size, also from an older race without heats", () => {
|
|
const ev = event(10, 5);
|
|
const state = { sessions: [
|
|
{ id: "old1", eventId: "e1", name: "träning 1", type: "practice", status: "ready", driverIds: drivers(10) },
|
|
{ id: "old2", eventId: "e1", name: "träning 2", type: "practice", status: "ready", driverIds: drivers(10) },
|
|
{ id: "q", eventId: "e1", name: "Q1 H1", type: "qualification", status: "ready", driverIds: [] },
|
|
], resultsBySession: {} };
|
|
assert.equal(rebuildPracticeHeats(ev, { state, t, buildRaceSession }), 4);
|
|
assert.deepEqual(state.sessions.filter((s) => s.type === "practice").map((s) => `${s.name}:${s.driverIds.length}`), ["träning 1 H1:5", "träning 1 H2:5", "träning 2 H1:5", "träning 2 H2:5"]);
|
|
assert.ok(state.sessions.some((s) => s.id === "q"), "qualifying untouched");
|
|
});
|
|
|
|
test("practice heats: never rebuilt once one has run", () => {
|
|
const state = { sessions: [{ id: "p", eventId: "e1", name: "träning 1", type: "practice", status: "finished", startedAt: 1, driverIds: [] }], resultsBySession: {} };
|
|
assert.equal(rebuildPracticeHeats(event(10, 5), { state, t, buildRaceSession }), -1);
|
|
assert.equal(state.sessions[0].id, "p");
|
|
});
|
|
|
|
test("practice heats: a new participant joins the smallest heat of every round", () => {
|
|
const sessions = buildPracticeHeats(event(9, 5), 2, { t, buildRaceSession });
|
|
syncParticipantsToSessions(sessions, drivers(9), [...drivers(9), "new"]);
|
|
const withNew = sessions.filter((s) => s.driverIds.includes("new")).map((s) => s.name);
|
|
assert.deepEqual(withNew, ["träning 1 H2", "träning 2 H2"]);
|
|
assert.deepEqual(parsePracticeHeatName("practice 3 H2"), { round: 3, heat: 2 });
|
|
});
|