feat(results): save results as PDF automatically on the server and ask to print when the finals are done (1.89.28)

This commit is contained in:
larssand committed 2026-11-14 15:58:23 +01:00
1 parent ae829123cc
commit e98abd92d9
16 files changed
+253 -6

No files matched your search

+23
View File
@@ -0,0 +1,23 @@
import test from "node:test";
import assert from "node:assert/strict";
import { resultsAutosavePlan } from "../src/results_autosave.js";
const event = { id: "e1", mode: "race" };
const heat = { id: "q1", eventId: "e1", type: "qualification", status: "finished" };
const finals = [{ id: "a1", eventId: "e1", type: "final", status: "finished" }, { id: "a2", eventId: "e1", type: "final", status: "ready" }];
test("every heat: the heat's PDF; the last final also gives the full results and the print question", () => {
assert.deepEqual(resultsAutosavePlan({ mode: "heats", event, sessions: [heat, ...finals], session: heat }), { heat: true, results: false, askPrint: false });
assert.deepEqual(resultsAutosavePlan({ mode: "heats", autoPrint: true, event, sessions: [heat, ...finals], session: finals[0] }), { heat: true, results: false, askPrint: false }, "A2 still to run");
const done = finals.map((item) => ({ ...item, status: "finished" }));
assert.deepEqual(resultsAutosavePlan({ mode: "heats", autoPrint: true, event, sessions: [heat, ...done], session: done[1] }), { heat: true, results: true, askPrint: true });
});
test("final results only, off, and practice on the track", () => {
const done = finals.map((item) => ({ ...item, status: "finished" }));
assert.deepEqual(resultsAutosavePlan({ mode: "finals", event, sessions: done, session: heat }), { heat: false, results: false, askPrint: false });
assert.deepEqual(resultsAutosavePlan({ mode: "finals", event, sessions: done, session: done[1] }), { heat: false, results: true, askPrint: false });
assert.deepEqual(resultsAutosavePlan({ mode: "off", autoPrint: true, event, sessions: done, session: done[1] }), { heat: false, results: false, askPrint: true }, "print question without saving");
assert.equal(resultsAutosavePlan({ mode: "heats", event: { id: "t", mode: "track" }, sessions: [], session: { ...heat, eventId: "t" } }).heat, false);
assert.equal(resultsAutosavePlan({ mode: "heats", event, sessions: [], session: { ...heat, status: "running" } }).heat, false);
});
+1
View File
@@ -58,3 +58,4 @@ import "./country_flags.test.js";
import "./pdf_flags.test.js";
import "./local_fonts.test.js";
import "./decoder_watch.test.js";
import "./results_autosave.test.js";
@@ -567,6 +567,35 @@ test("PDF export renders Swedish åäö/ÅÄÖ correctly instead of mojibake", {
}
});
test("autopilot print: a result PDF is saved on the server, listed and served, a newer one overwrites it", { timeout: 20000 }, async () => {
const dataDir = mkdtempSync(join(tmpdir(), "live-rc-test-autosave-"));
const port = await reservePort();
const baseUrl = `http://127.0.0.1:${port}`;
const child = spawn(process.execPath, [join(projectRoot, "server.js")], {
cwd: projectRoot,
env: { ...process.env, HOST: "127.0.0.1", PORT: String(port), RC_TIMING_DATA_DIR: dataDir, RC_TIMING_PID_FILE: "" },
stdio: ["ignore", "pipe", "pipe"],
});
try {
await waitForServer(child);
const save = (rows) => fetch(`${baseUrl}/api/results/autosave`, { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ date: "2026-11-14", title: "Klubbrace", filename: "Klubbrace_åäö_A_final_1.pdf", sections: [{ title: "A final 1", headers: ["Pos", "Förare"], rows }] }) });
const first = await (await save([["1", "Roy"]])).json();
assert.deepEqual(first, { ok: true, date: "2026-11-14", name: "Klubbrace_aao_A_final_1.pdf" });
await save([["1", "Anders"]]);
const { files } = await (await fetch(`${baseUrl}/api/results/saved`)).json();
assert.equal(files.length, 1, "the newer version overwrote the old one");
const pdf = await fetch(`${baseUrl}/api/results/saved/2026-11-14/Klubbrace_aao_A_final_1.pdf`);
assert.equal(pdf.headers.get("content-type"), "application/pdf");
assert.ok(Buffer.from(await pdf.arrayBuffer()).toString("latin1").includes("Anders"));
assert.ok(existsSync(join(dataDir, "results", "2026-11-14", "Klubbrace_aao_A_final_1.pdf")));
assert.equal((await fetch(`${baseUrl}/api/results/saved/2026-11-14/..%2F..%2Frc_timing.sqlite`)).status, 404, "no way out of the results folder");
assert.equal((await fetch(`${baseUrl}/api/results/saved/x/a.pdf`)).status, 404);
} finally {
await stopServer(child);
rmSync(dataDir, { recursive: true, force: true });
}
});
test("an admin PIN blocks unauthenticated writes, and the login cookie lets them through", { timeout: 20000 }, async () => {
const dataDir = mkdtempSync(join(tmpdir(), "live-rc-test-auth-"));
const port = await reservePort();