import test from "node:test"; import assert from "node:assert/strict"; 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); });