Files
Live_RC/tests/pdf_render.test.js
T

42 lines
2.9 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { createRequire } from "node:module";
const { buildStyledPdf } = createRequire(import.meta.url)("../server/pdf_render.js");
const base = { title: "Resultat", subtitle: "", brandName: "Klubben", brandTagline: "", footer: "RC RaceCore", theme: "classic", logoDataUrl: "" };
test("the PDF has real tables: Helvetica fonts, a filled header row, podium marks and page numbers", () => {
const rows = Array.from({ length: 70 }, (_, i) => [String(i + 1), `Förare ${i + 1}`, String(20 - (i % 5)), "0:14.812"]);
const pdf = buildStyledPdf({ ...base, sections: [{ title: "A-final", headers: ["Pos", "Förare", "Varv", "Bästa varv"], rows }] }).toString("latin1");
assert.ok(pdf.includes("/BaseFont /Helvetica ") && pdf.includes("/BaseFont /Helvetica-Bold"), "Helvetica and Helvetica-Bold");
assert.ok(!pdf.includes("Courier"), "no typewriter font any more");
assert.ok(pdf.includes("0.831 0.686 0.216 rg"), "gold mark for the winner");
assert.match(pdf, /\/Count 2 /, "70 rows run onto a second page");
assert.equal((pdf.match(/\(Bästa varv\) Tj/g) || []).length, 2, "the table header repeats on the new page");
assert.ok(pdf.includes("(1 / 2) Tj") && pdf.includes("(2 / 2) Tj"), "page numbers");
});
test("a long name is shortened with … instead of running into the next column", () => {
const pdf = buildStyledPdf({ ...base, sections: [{ title: "", headers: ["Pos", "Förare", ...Array.from({ length: 14 }, (_, i) => `R${i + 1}`)], rows: [["1", "A".repeat(120), ...Array.from({ length: 14 }, () => "(100)")]] }] });
assert.ok(pdf.includes(Buffer.from([0x85])), "ellipsis as a WinAnsi byte");
});
test("places 1-3 get gold, silver and bronze medals - in the app tables and drawn in the PDF", async () => {
const { medal } = await import("../src/medal.js");
assert.equal(medal(1), '<span class="medal medal-1">1</span>');
assert.equal(medal("3"), '<span class="medal medal-3">3</span>');
assert.equal(medal(4), "4");
const pdf = buildStyledPdf({ ...base, sections: [{ title: "", headers: ["Pos", "Förare"], rows: [["1", "A"], ["2", "B"], ["3", "C"], ["4", "D"]] }] }).toString("latin1");
assert.ok(pdf.includes("0.941 0.773 0.247 rg") && pdf.includes("0.812 0.835 0.863 rg") && pdf.includes("0.827 0.541 0.298 rg"), "gold, silver and bronze medal faces");
assert.ok(pdf.includes("(4) Tj"), "place 4 stays a plain number");
});
test("A-final places 1-3 also get medals, and TQ stands out", async () => {
const { medal, qualCell } = await import("../src/medal.js");
assert.equal(medal("A2"), '<span class="medal medal-2">A2</span>');
assert.equal(medal("B1"), "B1");
assert.match(qualCell("TQ", (v) => v), /tq-badge/);
const pdf = buildStyledPdf({ ...base, sections: [{ title: "", headers: ["Pos", "Kval", "Förare"], rows: [["A1", "4", "Al"], ["A2", "TQ", "Bea"]] }] }).toString("latin1");
assert.ok(pdf.includes("(A1) Tj") && pdf.includes("/F2 9 Tf") && pdf.includes("(TQ) Tj"));
});