Files
Live_RC/tests/day_schedule.test.js
T

87 lines
5.9 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { orderDaySessions, buildDaySchedule, getAutopilotAction, normalizeRaceDays, moveInDayOrder, pickBoardDate } 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);
});