import test from "node:test"; import assert from "node:assert/strict"; import { createRequire } from "node:module"; import { buildPublicRaceDaySnapshot } from "../src/my_race_logic.js"; const require = createRequire(import.meta.url); const { pickDriverRaceDay, sanitizePublicRaceDaySnapshot } = require("../server/public_training_profile.js"); const today = (() => { const d = new Date(); return `${d.getFullYear()}-${String(d.getMonth() + 1).padStart(2, "0")}-${String(d.getDate()).padStart(2, "0")}`; })(); const at = (min) => Date.now() + min * 60000; test("the snapshot carries ids only: entries in grid order, marshals, results of heats run", () => { const rows = [ { session: { id: "q1", name: "Q1 H1" }, start: at(-20), state: "done", marshals: [] }, { session: { id: "q2", name: "Q2 H1", startMode: "position" }, start: at(25), state: "upcoming", marshals: [{ driverId: "d3", name: "Helin" }] }, ]; const snapshot = buildPublicRaceDaySnapshot({ date: today, rows, getClassLabel: () => "2WD", getEntries: (session) => (session.id === "q1" ? [{ driverId: "d1", name: "Roy" }, { driverId: "d2", name: "Anders" }] : [{ driverId: "d2" }, { driverId: "d1" }]), getResults: () => [{ driverId: "d2", laps: 17, resultDisplay: "17/5:03.4", displayName: "Anders" }, { driverId: "d1", laps: 16, resultDisplay: "16/5:01.0" }], isGridStart: (session) => session.startMode === "position", }); assert.deepEqual(snapshot.sessions[0].results, [{ driverId: "d2", position: 1, laps: 17, result: "17/5:03.4" }, { driverId: "d1", position: 2, laps: 16, result: "16/5:01.0" }]); assert.deepEqual(snapshot.sessions[1], { id: "q2", label: "2WD · Q2 H1", start: Math.round(rows[1].start), state: "upcoming", gridStart: true, entries: ["d2", "d1"], marshals: ["d3"], results: [] }); assert.doesNotMatch(JSON.stringify(snapshot), /Roy|Anders|Helin/, "no names leave the race computer"); }); test("a driver's mobile profile gets only their own heats, marshal duties and results, today only", () => { const snapshot = sanitizePublicRaceDaySnapshot({ date: today, sessions: [ { id: "q1", label: "2WD · Q1 H1", start: at(-20), state: "done", entries: ["d1", "d2"], marshals: [], results: [{ driverId: "d2", position: 1, laps: 17, result: "17/5:03.4" }, { driverId: "d1", position: 2, laps: 16, result: "16/5:01.0" }] }, { id: "q2", label: "2WD · Q2 H1", start: at(25), state: "upcoming", gridStart: true, entries: ["d2", "d1"], marshals: ["d3"], results: [] }, { id: "q3", label: "4WD · Q1 H1", start: at(40), state: "upcoming", entries: ["d9"], marshals: ["d1"], results: [] }, ] }); const roy = pickDriverRaceDay(snapshot, ["d1"]); assert.deepEqual(roy.heats, [{ label: "2WD · Q2 H1", start: snapshot.sessions[1].start, live: false, gridPosition: 2 }]); assert.deepEqual(roy.marshal.map((item) => item.label), ["4WD · Q1 H1"]); assert.deepEqual(roy.results, [{ label: "2WD · Q1 H1", position: 2, laps: 16, result: "16/5:01.0" }]); assert.doesNotMatch(JSON.stringify(roy), /d2|d3|d9/, "no other driver's ids"); assert.equal(pickDriverRaceDay(snapshot, ["nobody"]), null, "not racing today: no card"); assert.equal(pickDriverRaceDay({ ...snapshot, date: "2020-01-01" }, ["d1"]), null, "another day's snapshot is ignored"); assert.equal(sanitizePublicRaceDaySnapshot({ date: "bad" }), null); });