246 lines
18 KiB
JavaScript
246 lines
18 KiB
JavaScript
import test from "node:test";
|
||
import assert from "node:assert/strict";
|
||
import { assignMarshals, orderDaySessions, buildDaySchedule, getAutopilotAction, normalizeRaceDays, moveInDayOrder, pickBoardDate, fixedBreakWindow, withRaceSetupStart, eventScheduleFromDayRows } from "../src/day_schedule_logic.js";
|
||
|
||
const events = [
|
||
{ id: "e4", name: "Cup #1 4WD", date: "2026-11-08", mode: "race" },
|
||
{ id: "e2", name: "Cup #1 2WD", date: "2026-11-08", mode: "race" },
|
||
{ id: "other", name: "Other day", date: "2026-11-09", mode: "race" },
|
||
];
|
||
const s = (id, eventId, type, name, extra = {}) => ({ id, eventId, type, name, mode: "race", status: "ready", durationMin: 5, ...extra });
|
||
const sessions = [
|
||
s("a4", "e4", "final", "A Final 1"), s("q4", "e4", "qualification", "Q1 H1"), s("q4b", "e4", "qualification", "Q2 H1"),
|
||
s("b2", "e2", "final", "B Final 1"), s("a2", "e2", "final", "A Final 1"), s("q2b", "e2", "qualification", "Q2 H1"),
|
||
s("q2", "e2", "qualification", "Q1 H1"), s("q2h2", "e2", "qualification", "Q1 H2"), s("p2", "e2", "practice", "Seeding"),
|
||
s("x", "other", "qualification", "Q1 H1"),
|
||
];
|
||
|
||
test("the day runs every class step by step: practice, each qualifying round, finals lowest first", () => {
|
||
const ids = orderDaySessions({ events, sessions, date: "2026-11-08" }).map((item) => item.id);
|
||
assert.deepEqual(ids, ["p2", "q2", "q2h2", "q4", "q2b", "q4b", "b2", "a2", "a4"]);
|
||
});
|
||
|
||
test("a manual day order and a class's own order are kept", () => {
|
||
const ordered = orderDaySessions({ events, sessions, date: "2026-11-08" });
|
||
const manual = moveInDayOrder(ordered, "q4", -1);
|
||
assert.deepEqual(orderDaySessions({ events, sessions, date: "2026-11-08", manualOrder: manual }).slice(0, 4).map((item) => item.id), ["p2", "q2", "q4", "q2h2"]);
|
||
const swapped = events.map((event) => (event.id === "e2" ? { ...event, sessionOrder: ["q2h2", "q2"] } : event));
|
||
assert.deepEqual(orderDaySessions({ events: swapped, sessions, date: "2026-11-08" }).slice(1, 3).map((item) => item.id), ["q2h2", "q2"]);
|
||
});
|
||
|
||
test("the schedule cascades from the start time and a late or paused day pushes the rest", () => {
|
||
const start = Date.UTC(2026, 10, 8, 10, 0);
|
||
const list = [s("one", "e2", "qualification", "Q1 H1"), s("two", "e2", "qualification", "Q1 H2")];
|
||
const day = { startAt: start, gapMin: 3 };
|
||
const plan = buildDaySchedule(list, day, start - 3600000);
|
||
assert.deepEqual(plan.map((row) => row.start), [start, start + 8 * 60000]);
|
||
assert.equal(plan[1].upcomingIndex, 1);
|
||
// Nothing started 4 minutes after the planned time: expected now, everything after moves.
|
||
const late = buildDaySchedule(list, day, start + 4 * 60000);
|
||
assert.equal(late[0].start, start + 4 * 60000);
|
||
assert.equal(late[0].delayed, true);
|
||
assert.equal(late[1].start, start + 12 * 60000);
|
||
// A heat running with a 2 minute pause ends 2 minutes later.
|
||
const running = [{ ...list[0], status: "running", startedAt: start + 60000, totalPausedMs: 120000 }, list[1]];
|
||
const live = buildDaySchedule(running, day, start + 2 * 60000);
|
||
assert.equal(live[0].state, "live");
|
||
assert.equal(live[0].end, start + 8 * 60000);
|
||
assert.equal(live[1].start, start + 11 * 60000);
|
||
});
|
||
|
||
test("the autopilot waits for the planned time minus the count-down, never while something runs or the day is paused", () => {
|
||
const start = Date.UTC(2026, 10, 8, 10, 0);
|
||
const rows = buildDaySchedule([s("one", "e2", "qualification", "Q1 H1")], { startAt: start }, start - 600000);
|
||
const prologueMs = () => 120000;
|
||
assert.deepEqual(getAutopilotAction(rows, { day: {}, now: start - 300000, prologueMs }).reason, "until");
|
||
assert.equal(getAutopilotAction(rows, { day: {}, now: start - 120000, prologueMs }).type, "start");
|
||
assert.equal(getAutopilotAction(rows, { day: {}, now: start, busy: true, prologueMs }).reason, "running");
|
||
assert.equal(getAutopilotAction(rows, { day: { pausedAt: start }, now: start, prologueMs }).reason, "paused");
|
||
assert.equal(getAutopilotAction([], { day: {}, now: start }).reason, "done");
|
||
assert.deepEqual(getAutopilotAction([], { day: {}, now: start, finalsPending: ["Buggy 4WD", "Buggy 2WD"] }), { type: "wait", reason: "finals", classes: ["Buggy 4WD", "Buggy 2WD"] });
|
||
assert.equal(getAutopilotAction(rows, { day: {}, now: start - 120000, prologueMs, finalsPending: ["Buggy 4WD"] }).type, "start", "a heat still to run goes first");
|
||
});
|
||
|
||
test("race days are stored one per date, and the board shows today or the next day still to race", () => {
|
||
const days = normalizeRaceDays([{ date: "2026-11-08", gapMin: 0 }, { date: "bad" }, { date: "2026-11-08", gapMin: 5, autopilot: true }]);
|
||
assert.equal(days.length, 1);
|
||
assert.equal(days[0].gapMin, 5);
|
||
assert.equal(normalizeRaceDays([{ date: "2026-11-08", gapMin: 0 }])[0].gapMin, 0);
|
||
assert.equal(pickBoardDate({ events, sessions, today: "2026-11-08" }), "2026-11-08");
|
||
assert.equal(pickBoardDate({ events, sessions, today: "2026-11-01" }), "2026-11-08");
|
||
assert.equal(pickBoardDate({ events, sessions, today: "2026-12-01" }), "2026-12-01");
|
||
});
|
||
|
||
test("a heat is due for its call within callMin, once, and not while put off or the day is paused", async () => {
|
||
const { getDueCall } = await import("../src/day_schedule_logic.js");
|
||
const start = Date.UTC(2026, 10, 8, 10, 0);
|
||
const rows = buildDaySchedule([s("one", "e2", "qualification", "Q1 H1"), s("two", "e2", "qualification", "Q1 H2")], { startAt: start, gapMin: 3 }, start - 3600000);
|
||
const day = { callMin: 10 };
|
||
assert.equal(getDueCall(rows, { day, now: start - 11 * 60000 }), null);
|
||
assert.equal(getDueCall(rows, { day, now: start - 10 * 60000 }).session.id, "one");
|
||
const called = rows.map((row, i) => (i === 0 ? { ...row, session: { ...row.session, calledAt: start - 600000 } } : row));
|
||
assert.equal(getDueCall(called, { day, now: start - 5 * 60000 }), null, "the next heat is not due yet");
|
||
assert.equal(getDueCall(called, { day, now: start }).session.id, "two");
|
||
assert.equal(getDueCall(rows, { day, now: start - 5 * 60000, snoozedUntil: () => start - 3 * 60000 }), null, "put off");
|
||
assert.equal(getDueCall(rows, { day: { ...day, pausedAt: 1 }, now: start }), null);
|
||
});
|
||
|
||
test("breaks: between heats, between rounds, before the finals (lunch) and an extra break before one heat", () => {
|
||
const start = new Date("2026-11-08T09:00:00").getTime();
|
||
const list = [
|
||
s("p1", "e2", "practice", "Practice 1"), s("p2", "e2", "practice", "Practice 2"),
|
||
s("q1", "e2", "qualification", "Q1 H1"), s("q2", "e2", "qualification", "Q1 H2"),
|
||
s("q3", "e2", "qualification", "Q2 H1"),
|
||
s("f1", "e2", "final", "B Final 1"), s("f2", "e2", "final", "A Final 1"),
|
||
];
|
||
const at = (plan, id) => (plan.find((row) => row.session.id === id).start - start) / 60000;
|
||
const day = { date: "2026-11-08", startAt: start, gapMin: 2, roundGapMin: 10, finalsGapMin: 45, breaks: { q3: 15 } };
|
||
const plan = buildDaySchedule(list, day, start - 3600000);
|
||
// 5-minute heats: p1 0, p2 7, round gap -> q1 22, q2 29, round gap + 15 extra -> q3 59, lunch -> f1 109, f2 116.
|
||
assert.deepEqual(list.map((x) => at(plan, x.id)), [0, 7, 22, 29, 59, 109, 116]);
|
||
assert.equal(plan.find((row) => row.session.id === "q3").breakMin, 25);
|
||
// Without round / finals gaps every break is the heat gap, as before.
|
||
const flat = buildDaySchedule(list, { date: "2026-11-08", startAt: start, gapMin: 2 }, start - 3600000);
|
||
assert.deepEqual(list.map((x) => at(flat, x.id)), [0, 7, 14, 21, 28, 35, 42]);
|
||
// No finals gap: the round gap is used before the finals.
|
||
const noLunch = buildDaySchedule(list, { ...day, finalsGapMin: null, breaks: {} }, start - 3600000);
|
||
assert.equal(at(noLunch, "f1") - at(noLunch, "q3"), 15);
|
||
});
|
||
|
||
test("finals created after a finished qualifier wait for the finals break, measured from the end of qualifying", () => {
|
||
const start = new Date("2026-11-08T09:00:00").getTime();
|
||
const qualEnd = start + 5 * 60000;
|
||
const list = [s("q1", "e2", "qualification", "Q1 H1", { status: "finished", startedAt: start, endedAt: qualEnd }), s("f1", "e2", "final", "A Final 1")];
|
||
const plan = buildDaySchedule(list, { date: "2026-11-08", startAt: start, gapMin: 2, finalsGapMin: 45 }, qualEnd + 20 * 60000);
|
||
assert.equal(plan[1].start, qualEnd + 45 * 60000, "finals made 20 min later still start 45 min after qualifying");
|
||
assert.equal(getAutopilotAction(plan, { day: {}, now: qualEnd + 20 * 60000 }).reason, "until");
|
||
});
|
||
|
||
test("race day plan keeps the round and finals gaps and drops empty breaks", () => {
|
||
const [day] = normalizeRaceDays([{ date: "2026-11-08", gapMin: 2, roundGapMin: "10", finalsGapMin: "", breaks: { q3: 15, q4: 0, "": 5 } }]);
|
||
assert.equal(day.roundGapMin, 10);
|
||
assert.equal(day.finalsGapMin, null);
|
||
assert.deepEqual(day.breaks, { q3: 15 });
|
||
});
|
||
|
||
test("a break at fixed clock times (lunch 12-13): no heat starts or runs inside it", () => {
|
||
const at = (hhmm) => new Date(`2026-11-08T${hhmm}:00`).getTime();
|
||
const list = [s("q1", "e2", "qualification", "Q1 H1"), s("q2", "e2", "qualification", "Q1 H2"), s("q3", "e2", "qualification", "Q1 H3")];
|
||
const day = { date: "2026-11-08", startAt: at("11:46"), gapMin: 2, breakFrom: "12:00", breakTo: "13:00", breakLabel: "Lunch" };
|
||
const plan = buildDaySchedule(list, day, at("08:00"));
|
||
// q1 11:46-11:51, q2 11:53-11:58 (ends before lunch), q3 would start 12:00 -> 13:00.
|
||
assert.deepEqual(plan.map((row) => new Date(row.start).toTimeString().slice(0, 5)), ["11:46", "11:53", "13:00"]);
|
||
assert.deepEqual(plan.map((row) => row.afterFixedBreak), [false, false, true]);
|
||
assert.equal(plan[2].delayed, false, "moved for lunch is not late");
|
||
// A heat that would start before lunch but run into it waits too.
|
||
const into = buildDaySchedule(list, { ...day, startAt: at("11:50") }, at("08:00"));
|
||
assert.equal(new Date(into[1].start).toTimeString().slice(0, 5), "13:00", "11:57-12:02 would run into lunch");
|
||
// During lunch, the next heat still waits for 13:00 (the autopilot doesn't start it).
|
||
const lunch = buildDaySchedule([{ ...list[0], status: "finished", startedAt: at("11:46"), endedAt: at("11:51") }, list[1]], day, at("12:20"));
|
||
assert.equal(lunch[1].start, at("13:00"));
|
||
assert.equal(getAutopilotAction(lunch, { day, now: at("12:20") }).reason, "until");
|
||
// Invalid or backwards times mean no break.
|
||
assert.equal(fixedBreakWindow({ ...day, breakTo: "11:00" }), null);
|
||
assert.equal(normalizeRaceDays([{ ...day, breakFrom: "25:00" }])[0].breakFrom, null);
|
||
assert.equal(fixedBreakWindow(day).label, "Lunch");
|
||
});
|
||
|
||
test("several fixed breaks a day: each moves the heat that would hit it, the old single break carries over", () => {
|
||
const at = (hhmm) => new Date(`2026-11-08T${hhmm}:00`).getTime();
|
||
const list = ["a", "b", "c", "d"].map((id, i) => s(id, "e2", "qualification", `Q1 H${i + 1}`));
|
||
const day = { date: "2026-11-08", startAt: at("11:50"), gapMin: 2, fixedBreaks: [{ from: "15:00", to: "15:20", label: "Prisutdelning" }, { from: "12:00", to: "13:00", label: "Lunch" }, { from: "13:05", to: "13:15", label: "Banunderhåll" }] };
|
||
const plan = buildDaySchedule(list, day, at("08:00"));
|
||
// a 11:50-11:55; b 11:57 runs into lunch -> 13:00, but 13:00-13:05 ends as the track break starts: ok;
|
||
// c 13:07 runs into the track break -> 13:15; d 13:22.
|
||
assert.deepEqual(plan.map((row) => new Date(row.start).toTimeString().slice(0, 5)), ["11:50", "13:00", "13:15", "13:22"]);
|
||
assert.deepEqual(plan.map((row) => row.afterBreak?.label || ""), ["", "Lunch", "Banunderhåll", ""]);
|
||
// Sorted, and an overlapping or backwards break is dropped.
|
||
const normalized = normalizeRaceDays([{ date: "2026-11-08", fixedBreaks: [{ from: "13:00", to: "14:00" }, { from: "12:00", to: "13:30" }, { from: "16:00", to: "15:00" }] }])[0];
|
||
assert.deepEqual(normalized.fixedBreaks.map((item) => item.from), ["12:00"]);
|
||
// A day saved with the single break keeps it as the first entry.
|
||
const old = normalizeRaceDays([{ date: "2026-11-08", breakFrom: "12:00", breakTo: "12:45", breakLabel: "Lunch" }])[0];
|
||
assert.deepEqual(old.fixedBreaks, [{ from: "12:00", to: "12:45", label: "Lunch" }]);
|
||
assert.equal(normalizeRaceDays([{ date: "2026-11-08", breakFrom: "12:00", breakTo: "12:45", fixedBreaks: [] }])[0].breakFrom, null, "an emptied list wins over the old fields");
|
||
});
|
||
|
||
test("the drivers' board shows the lunch while it is on, and before the heat after it", async () => {
|
||
const { renderDriverBoard } = await import("../src/day_schedule_view.js");
|
||
const at = (hhmm) => new Date(`2026-11-08T${hhmm}:00`).getTime();
|
||
const day = { date: "2026-11-08", startAt: at("11:55"), gapMin: 2, breakFrom: "12:00", breakTo: "13:00", breakLabel: "Lunch" };
|
||
const list = [s("q1", "e2", "qualification", "Q1 H1", { status: "finished", startedAt: at("11:46"), endedAt: at("11:51") }), s("q2", "e2", "qualification", "Q1 H2")];
|
||
const rows = buildDaySchedule(list, day, at("12:20"));
|
||
const html = renderDriverBoard({ t: (k) => k, escapeHtml: String, date: "2026-11-08", rows, getClassLabel: () => "2WD", getSessionGridEntries: () => [], getRemainingLabel: () => "", day, now: at("12:20") });
|
||
assert.match(html, /board-break">🍽 Lunch 12:00–13:00/);
|
||
assert.match(html, /board-break-note">🍽 Lunch 12:00–13:00/);
|
||
const before = renderDriverBoard({ t: (k) => k, escapeHtml: String, date: "2026-11-08", rows, getClassLabel: () => "2WD", getSessionGridEntries: () => [], getRemainingLabel: () => "", day, now: at("11:58") });
|
||
assert.doesNotMatch(before, /class="board-live board-break"/, "no lunch banner before lunch");
|
||
});
|
||
|
||
test("a heat already run between pending ones doesn't pull the rest of the day back to now", () => {
|
||
const T = (h) => new Date(`2026-11-12T${h}:00`).getTime();
|
||
const sessions = [
|
||
{ id: "p", type: "practice", name: "practice 1", status: "ready", durationMin: 5 },
|
||
{ id: "x", type: "practice", name: "other", status: "finished", durationMin: 5, startedAt: T("19:00"), endedAt: T("19:05") },
|
||
{ id: "q1", type: "qualification", name: "Q1 H1", status: "ready", durationMin: 5 },
|
||
{ id: "y", type: "qualification", name: "other q", status: "finished", durationMin: 5, startedAt: T("19:30"), endedAt: T("19:35") },
|
||
{ id: "q2", type: "qualification", name: "Q2 H1", status: "ready", durationMin: 5 },
|
||
];
|
||
const rows = buildDaySchedule(sessions, { startAt: T("23:40"), gapMin: 3 }, T("23:39")).filter((row) => row.state === "upcoming");
|
||
assert.deepEqual(rows.map((row) => row.start), [T("23:40"), T("23:48"), T("23:56")], "one after the other from the first start, not all at 23:39");
|
||
});
|
||
|
||
test("one start time: Race Day's first start; a Race setup start time from before fills in a day without one", () => {
|
||
const at = new Date("2026-11-14T09:30:00").getTime();
|
||
const events = [{ id: "e", date: "2026-11-14", raceConfig: { scheduleStartAt: at } }];
|
||
assert.equal(withRaceSetupStart({ date: "2026-11-14", startAt: null }, events).startAt, at);
|
||
const own = new Date("2026-11-14T10:00:00").getTime();
|
||
assert.equal(withRaceSetupStart({ date: "2026-11-14", startAt: own }, events).startAt, own, "Race Day's own time wins");
|
||
assert.equal(withRaceSetupStart({ date: "2026-11-15", startAt: null }, events).startAt, null, "another day is not touched");
|
||
});
|
||
|
||
test("Race setup shows its race's heats with Race Day's times (lunch and other classes included)", () => {
|
||
const T = (h) => new Date(`2026-11-14T${h}:00`).getTime();
|
||
const sessions = [
|
||
{ id: "a1", eventId: "a", type: "qualification", name: "Q1 H1", status: "ready", durationMin: 5 },
|
||
{ id: "b1", eventId: "b", type: "qualification", name: "Q1 H1", status: "ready", durationMin: 5 },
|
||
{ id: "a2", eventId: "a", type: "qualification", name: "Q2 H1", status: "ready", durationMin: 5 },
|
||
];
|
||
const rows = buildDaySchedule(sessions, { date: "2026-11-14", startAt: T("11:50"), gapMin: 2, breakFrom: "12:00", breakTo: "13:00" }, T("08:00"));
|
||
const own = eventScheduleFromDayRows(rows, "a");
|
||
assert.deepEqual(own.map((row) => row.sessionId), ["a1", "a2"]);
|
||
assert.equal(own[0].plannedStart, T("11:50"));
|
||
assert.equal(own[1].plannedStart, T("13:07"), "the other class would run into lunch, goes 13:00-13:05, then a2 after the gap");
|
||
});
|
||
|
||
test("marshals: the drivers of the heat before (or after), never someone driving the heat", () => {
|
||
const entrants = { h1: ["a", "b"], h2: ["c", "d", "a"], h3: ["e"] };
|
||
const rows = ["h1", "h2", "h3"].map((id) => ({ session: { id } }));
|
||
const of = (session) => entrants[session.id].map((id) => ({ driverId: id, name: id.toUpperCase() }));
|
||
const names = (list) => list.map((row) => row.marshals.map((entry) => entry.name).join(""));
|
||
assert.deepEqual(names(assignMarshals(rows, "previous", of)), ["", "B", "CDA"], "a drives h2 too, so only b marshals it");
|
||
assert.deepEqual(names(assignMarshals(rows, "next", of)), ["CD", "E", ""]);
|
||
assert.deepEqual(names(assignMarshals(rows, "off", of)), ["", "", ""]);
|
||
assert.equal(normalizeRaceDays([{ date: "2026-11-08", marshalRule: "bogus" }])[0].marshalRule, "off");
|
||
});
|
||
|
||
test("a class's qualifying start time holds its qualifiers until then; practice and other days are not moved", () => {
|
||
const now = new Date("2026-11-08T10:00:00").getTime();
|
||
const qualAt = new Date("2026-11-08T11:30:00").getTime();
|
||
const day = { date: "2026-11-08", gapMin: 2, qualStartAt: { e2: qualAt } };
|
||
const daySessions = [s("p", "e2", "practice", "practice 1"), s("q1", "e2", "qualification", "Q1 H1"), s("q2", "e2", "qualification", "Q1 H2")];
|
||
const rows = buildDaySchedule(daySessions, day, now);
|
||
assert.equal(rows[0].start, now, "practice now");
|
||
assert.equal(rows[1].start, qualAt, "first qualifier at the chosen time");
|
||
assert.equal(rows[2].start, qualAt + 5 * 60000 + 2 * 60000, "the next one after it as usual");
|
||
assert.equal(normalizeRaceDays([day])[0].qualStartAt.e2, qualAt);
|
||
});
|
||
|
||
test("autopilot waits for 'Generate qualifying from practice' when the next heat is a preliminary qualifier", () => {
|
||
const rows = [{ state: "done", session: s("p", "e2", "practice", "practice 1") }, { state: "upcoming", start: 0, session: s("q1", "e2", "qualification", "Q1 H1") }];
|
||
const waiting = getAutopilotAction(rows, { day: {}, now: 10, seedPending: { e2: "2WD" } });
|
||
assert.equal(waiting.type, "wait");
|
||
assert.equal(waiting.reason, "seed");
|
||
assert.deepEqual(waiting.classes, ["2WD"]);
|
||
assert.equal(getAutopilotAction(rows, { day: {}, now: 10, seedPending: {} }).type, "start", "built from practice: starts as before");
|
||
});
|