feat(results): lap chart per qualifying heat and final in the race overview, print and PDF (1.89.32)
This commit is contained in:
1 parent
01998952a7
commit
6a036899ab
15 files changed
+245
-14
No files matched your search
@@ -0,0 +1,64 @@
|
||||
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<b>", timestamps: [1, 3] }, { name: "B", timestamps: [2, 4] }]), { escapeHtml, title: "Lap chart", lapLabel: "Lap" });
|
||||
assert.equal((svg.match(/<polyline/g) || []).length, 2);
|
||||
assert.ok(svg.includes("1. A<b>") && svg.includes("2. B"));
|
||||
assert.ok(!svg.includes("<b>"));
|
||||
});
|
||||
|
||||
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");
|
||||
});
|
||||
Reference in new issue
Block a user