import test from "node:test"; import assert from "node:assert/strict"; import fs from "node:fs"; import vm from "node:vm"; import { buildStintLapRows, renderStintLapCards } from "../src/stint_laps_view.js"; import { buildTrainingStintAnalysis } from "../src/training_logic.js"; const escapeHtml = (value) => String(value).replace(/[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c]); const labels = { stint: "Stint", laps: "Varv", best: "Bästa", average: "Snitt", duration: "Tid", lap: "Varvtider", excluded: "Startvarv", empty: "Inga" }; const fmt = (ms) => (ms / 1000).toFixed(3); test("buildStintLapRows marks best, slow and the out lap, with deltas against the best lap", () => { const rows = buildStintLapRows([{ lapMs: 150000, excluded: true }, { lapMs: 13500 }, { lapMs: 13201 }, { lapMs: 13400 }, { lapMs: 16000 }]); assert.deepEqual(rows.map((row) => row.number), [1, 2, 3, 4, 5]); assert.equal(rows[0].excluded, true); assert.equal(rows[0].deltaMs, null, "the out lap is not compared"); assert.equal(rows[2].best, true); assert.equal(rows[1].deltaMs, 299); assert.equal(rows[4].slow, true, "over 15% above the median"); assert.equal(rows[2].barPct, 100); }); test("renderStintLapCards lists every lap and keeps open cards open", () => { const stints = [{ id: "s1", number: 2, startedAt: 1, lapCount: 2, bestLapMs: 13201, averageLapMs: 13300, durationMs: 26601, laps: [{ lapMs: 13400 }, { lapMs: 13201 }] }]; const html = renderStintLapCards(stints, { formatLap: fmt, formatDuration: fmt, escapeHtml, labels, openIds: new Set(["s1"]) }); assert.equal((html.match(/class="stint-lap[ "]/g) || []).length, 2); assert.match(html, /data-stint-id="s1" open/); assert.match(html, /\+0\.199/); assert.match(html, /★/); assert.match(renderStintLapCards([], { escapeHtml, labels }), /Inga/); }); test("buildTrainingStintAnalysis rows carry their laps for the fold-out list", () => { const ledger = [10_000, 23_000, 36_500].map((timestamp, index) => ({ driverId: "d1", sourceId: "tp", sessionId: "p1", timestamp, lapMs: [10_000, 13_000, 13_500][index] })); const { rows } = buildTrainingStintAnalysis({ training: { ledger } }, "d1"); assert.deepEqual(rows[0].laps.map((lap) => lap.lapMs), [10_000, 13_000, 13_500]); assert.equal(rows[0].averageLapMs, 12_167); }); test("the day's fastest lap is purple, over every stint that day", () => { const day = new Date(2026, 10, 9, 18).getTime(); const stints = [ { id: "a", number: 1, startedAt: day, lapCount: 2, bestLapMs: 13300, laps: [{ lapMs: 13500 }, { lapMs: 13300 }] }, { id: "b", number: 2, startedAt: day + 3600000, lapCount: 2, bestLapMs: 13100, laps: [{ lapMs: 13100 }, { lapMs: 13400 }] }, ]; const html = renderStintLapCards(stints.slice(0, 1), { formatLap: fmt, formatDuration: fmt, escapeHtml, labels, allStints: stints }); assert.doesNotMatch(html, /is-day-best/, "the shown stint's best is not the day's best"); assert.match(html, /class="stint-lap is-best"/); const both = renderStintLapCards(stints, { formatLap: fmt, formatDuration: fmt, escapeHtml, labels }); assert.equal((both.match(/class="stint-lap is-day-best"/g) || []).length, 1); assert.equal(buildStintLapRows([{ lapMs: 13100 }], { dayBestMs: 13100 })[0].dayBest, true); }); // The driver's page goes to the internet through the club router, whose nginx publishes only // public_training.js/.css - an import from it is a 404 and the page spins forever. So it // carries its own copy of the stint cards, which must render exactly like the module. test("the driver's page imports nothing and its stint-card copy matches the module", () => { const source = fs.readFileSync(new URL("../src/public_training.js", import.meta.url), "utf8"); assert.doesNotMatch(source, /^\s*import\b|\bimport\(/m, "public_training.js must stay one self-contained file"); const copy = source.slice(source.indexOf("// ---- Stint cards: a copy"), source.indexOf("// ---- end of copy ----")); const context = vm.createContext({}); vm.runInContext(`${copy}\nthis.render = renderStintLapCards; this.rows = buildStintLapRows;`, context); const day = new Date(2026, 10, 9, 18).getTime(); const stints = [ { id: "a", number: 1, startedAt: day, lapCount: 3, bestLapMs: 13300, averageLapMs: 13500, durationMs: 40000, consistencyPct: 2.5, laps: [{ lapMs: 150000, excluded: true }, { lapMs: 13500 }, { lapMs: 13300 }, { lapMs: 16000 }] }, { id: "b", number: 2, startedAt: day + 3600000, lapCount: 2, bestLapMs: 13100, averageLapMs: 13250, durationMs: 26500, consistencyPct: null, laps: [{ lapMs: 13100 }, { lapMs: 13400 }] }, ]; const options = { formatLap: fmt, formatDuration: fmt, escapeHtml, labels: { ...labels, consistency: "Jämnhet" }, openIds: new Set(["b"]) }; assert.equal(context.render(stints, options), renderStintLapCards(stints, options)); assert.equal(JSON.stringify(context.rows(stints[0].laps, { dayBestMs: 13100 })), JSON.stringify(buildStintLapRows(stints[0].laps, { dayBestMs: 13100 }))); });