51 lines
3.2 KiB
JavaScript
51 lines
3.2 KiB
JavaScript
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);
|
|
});
|
|
|
|
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);
|
|
});
|