import test from "node:test"; import assert from "node:assert/strict"; import { createRequire } from "node:module"; import { buildLapChart, lapChartTicks, renderLapChartSvg } from "../src/lap_chart_logic.js"; import { buildRaceOverview } from "../src/race_overview_logic.js"; const { buildStyledPdf } = createRequire(import.meta.url)("../server/pdf_render.js"); const escapeHtml = (value) => String(value).replace(/[&<>"]/g, (c) => ({ "&": "&", "<": "<", ">": ">", '"': """ })[c]); test("the place after each lap is the order the drivers crossed the line for that lap", () => { const chart = buildLapChart([ { name: "Anna", timestamps: [1200, 2100, 3000] }, // passes Bo on lap 2 { name: "Bo", timestamps: [1000, 2200, 3300] }, { name: "Cid", timestamps: [1500, 2900] }, // one lap down ]); assert.equal(chart.laps, 3); assert.deepEqual(chart.series.map((s) => s.positions), [[2, 1, 1], [1, 2, 2], [3, 3, null]]); assert.equal(chart.series[0].name, "Anna"); assert.match(chart.series[0].color, /^#[0-9a-f]{6}$/); }); test("no chart without at least two drivers and two laps", () => { assert.equal(buildLapChart([{ name: "A", timestamps: [1, 2, 3] }]), null); assert.equal(buildLapChart([{ name: "A", timestamps: [1] }, { name: "B", timestamps: [2] }]), null); assert.equal(buildLapChart([{ name: "A", timestamps: [1, 2] }, { name: "B", timestamps: [] }]), null); }); test("lap ticks start at 1, end on the last lap and stay about ten", () => { assert.deepEqual(lapChartTicks(5), [1, 2, 3, 4, 5]); assert.deepEqual(lapChartTicks(21), [1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 21]); assert.ok(lapChartTicks(120).length <= 14); }); test("the SVG has a line per driver and the names (escaped) in finish order", () => { const svg = renderLapChartSvg(buildLapChart([{ name: "A", timestamps: [1, 3] }, { name: "B", timestamps: [2, 4] }]), { escapeHtml, title: "Lap chart", lapLabel: "Lap" }); assert.equal((svg.match(/")); }); test("the race overview gives qualifying and finals a lap chart, practice none", () => { const sessions = [ { id: "p", type: "practice", status: "finished" }, { id: "q", type: "qualification", status: "finished" }, ]; const passings = { A: [1, 3, 5], B: [2, 4, 6] }; const overview = buildRaceOverview({ id: "e" }, { sessions, buildLeaderboard: () => [{ key: "A", driverName: "A" }, { key: "B", driverName: "B" }], buildPracticeStandings: () => [], buildQualifyingStandings: () => [], buildFinalStandings: () => [], getCompetitorPassings: (session, row) => passings[row.key].map((timestamp) => ({ timestamp })), }); const heat = (type) => overview.phases.find((phase) => phase.type === type).heats[0]; assert.equal(heat("practice").lapChart, null); assert.equal(heat("qualification").lapChart.laps, 3); }); test("the PDF draws the lap chart as coloured lines with the names", () => { const chart = { title: "Varvdiagram", lapLabel: "Varv", ...buildLapChart([{ name: "Anna", timestamps: [1, 3, 5] }, { name: "Bo", timestamps: [2, 4, 6] }]) }; const pdf = buildStyledPdf({ title: "R", subtitle: "", brandName: "K", brandTagline: "", footer: "", theme: "classic", logoDataUrl: "", sections: [{ title: "A-final", headers: ["Pos", "Förare"], rows: [["1", "Anna"], ["2", "Bo"]], chart }] }).toString("latin1"); assert.ok(pdf.includes("(Varvdiagram) Tj")); assert.ok(pdf.includes("(1. Anna) Tj") && pdf.includes("(2. Bo) Tj")); assert.ok(pdf.includes("0.902 0.224 0.275 RG"), "the first driver's red line"); }); test("lap table: a column per driver in order, each driver's best lap and the heat's fastest", async () => { const { buildLapTable } = await import("../src/lap_chart_logic.js"); const table = buildLapTable([{ name: "A", laps: [18000, 17500, 17900] }, { name: "B", laps: [17400, 18100] }]); assert.deepEqual(table.names, ["A", "B"]); assert.deepEqual(table.rows, [[18000, 17400], [17500, 18100], [17900, null]]); assert.deepEqual(table.best, [1, 0]); assert.deepEqual(table.fastest, { row: 0, col: 1 }); assert.equal(buildLapTable([{ name: "A", laps: [] }]), null); }); test("lap table: the PDF draws the times with the best laps bold", () => { const lapTable = { title: "Varvtider", lapLabel: "Varv", legend: "Fet = bästa", names: ["Anna", "Bo"], rows: [["17.400", "18.100"], ["17.900", "17.600"]], best: [0, 1], fastest: { row: 0, col: 0 } }; const pdf = buildStyledPdf({ title: "R", subtitle: "", brandName: "K", brandTagline: "", footer: "", theme: "classic", logoDataUrl: "", sections: [{ title: "Q1 H1", headers: ["Pos", "Förare"], rows: [["1", "Anna"], ["2", "Bo"]], lapTable }] }).toString("latin1"); assert.match(pdf, /Varvtider/); assert.match(pdf, /\/F2 [\d.]+ Tf [\d.]+ [\d.]+ Td \(17\.400\)/, "fastest lap in bold"); assert.match(pdf, /\/F1 [\d.]+ Tf [\d.]+ [\d.]+ Td \(18\.100\)/, "an ordinary lap in regular"); });