Files
Live_RC/tests/overlay_leaderboard.test.js
T

74 lines
6.0 KiB
JavaScript

import test from "node:test";
import assert from "node:assert/strict";
import { renderOverlayLeaderboard } from "../src/overlays.js";
import { readFileSync } from "node:fs";
const deps = { t: (key) => key, escapeHtml: (value) => String(value), formatTeamActiveMemberLabel: () => "", formatLap: (ms) => String(ms), formatPredictedLapDelta: () => "" };
const rows = (count) => Array.from({ length: count }, (_, index) => ({ key: `k${index}`, driverId: `d${index}`, displayName: `Driver ${index + 1}`, laps: 10, resultDisplay: "-", bestLapMs: 20000 + index }));
test("the overlay leaderboard lists every driver - compact rows beyond 12", () => {
const html = renderOverlayLeaderboard(rows(18), deps);
assert.equal((html.match(/data-overlay-competitor=/g) || []).length, 18, "all 18, not the first 12");
assert.match(html, /overlay-race-list-compact/);
assert.doesNotMatch(renderOverlayLeaderboard(rows(12), deps), /overlay-race-list-compact/, "12 or fewer keep the full rows");
});
test("the overlay page passes the whole leaderboard (OBS cuts to its own rows setting)", () => {
const source = readFileSync(new URL("../src/misc_views.js", import.meta.url), "utf8");
assert.doesNotMatch(source, /buildLeaderboard\(active\)\.slice\(0, 12\)/);
});
test("sponsor heats: the overlay shows the driver's car for this heat", () => {
const html = renderOverlayLeaderboard([{ key: "a", driverId: "a", displayName: "Anna", heatCarName: "Rental 2", laps: 3, resultDisplay: "-", bestLapMs: 17500 }], deps);
assert.match(html, /overlay-heat-car[^>]*>🚗 Rental 2/);
});
test("open practice: the last lap takes the result's place (the result only repeats the best lap)", () => {
const row = { key: "a", driverId: "a", displayName: "Anna", laps: 12, resultDisplay: "0:17.100", bestLapMs: 17100, lastLapMs: 17842 };
const practice = renderOverlayLeaderboard([row], deps, { lastLap: true });
assert.match(practice, /<label>table\.last_lap<\/label>\s*<strong>17842<\/strong>/);
assert.doesNotMatch(practice, /<label>table\.result<\/label>/);
const race = renderOverlayLeaderboard([row], deps);
assert.match(race, /<label>table\.result<\/label>\s*<strong>0:17\.100<\/strong>/);
assert.match(renderOverlayLeaderboard([{ ...row, lastLapMs: 0 }], deps, { lastLap: true }), /<label>table\.last_lap<\/label>\s*<strong>-<\/strong>/);
});
test("OBS tower: a Last column with each passing's lap time, next to laps", async () => {
const { renderObsOverlay } = await import("../src/overlays.js");
const config = { layout: "broadcast", rows: 10, showLaps: true, showLast: true, showResult: true, showBest: true, showGap: true };
const obsDeps = { ...deps, formatLap: (ms) => (ms / 1000).toFixed(3), getVisiblePassings: () => [], getEventName: () => "", getSessionTypeLabel: () => "", getStartModeLabel: () => "", getStatusLabel: () => "", getObsOverlayConfig: () => config, normalizeStartMode: (v) => v, renderPositionGrid: () => "", getSessionGridEntries: () => [], getSessionGridOrder: () => [] };
const html = renderObsOverlay({ eventId: "e", name: "Open practice", type: "open_practice", status: "running" }, [{ key: "p", driverId: "p", displayName: "Pia", laps: 23, resultDisplay: "0:15.833", bestLapMs: 15833, lastLapMs: 16120 }], {}, {}, {}, obsDeps);
assert.match(html, /obs-tv-laps">23<\/span><span class="obs-tv-cell obs-tv-last">16\.120<\/span>/);
assert.doesNotMatch(html, /obs-tv-result">0:15/, "practice still drops the result that repeats the best lap");
config.showLast = false;
assert.doesNotMatch(renderObsOverlay({ eventId: "e", name: "Q1", type: "qualification", status: "running" }, [{ key: "p", driverId: "p", displayName: "Pia", laps: 23, resultDisplay: "23/5:02", bestLapMs: 15833, lastLapMs: 16120 }], {}, {}, {}, obsDeps), /obs-tv-last/);
});
test("an Open Practice ranked on laps in a row or minutes keeps its result", async () => {
const { isOpenPracticeSession } = await import("../src/overlays.js");
assert.equal(isOpenPracticeSession({ type: "open_practice" }), true);
assert.equal(isOpenPracticeSession({ type: "open_practice", practiceRanking: "consecutive" }), false);
assert.equal(isOpenPracticeSession({ type: "free_practice", practiceRanking: "window" }), false);
assert.equal(isOpenPracticeSession({ type: "qualification" }), false);
});
test("practice ranked on 3 laps in a row: only result, gap and best lap as times (position, driver and next-lap bar stay)", async () => {
const { isPracticeRankedSession } = await import("../src/overlays.js");
assert.equal(isPracticeRankedSession({ type: "open_practice", practiceRanking: "consecutive" }), true);
assert.equal(isPracticeRankedSession({ type: "open_practice" }), false);
assert.equal(isPracticeRankedSession({ type: "final", practiceRanking: "consecutive" }), false);
assert.equal(isPracticeRankedSession({ type: "qualification", seedBestLapCount: 3 }), true, "qualifier on best 3 laps: same view");
assert.equal(isPracticeRankedSession({ type: "practice", seedBestLapCount: 3 }), true);
assert.equal(isPracticeRankedSession({ type: "qualification" }), false, "qualifier on laps and time: full view, like a final");
assert.equal(isPracticeRankedSession({ type: "final" }), false);
const row = { key: "a", driverId: "a", displayName: "Anna", laps: 12, resultDisplay: "3 varv 55.123", gapDisplay: "+0.747s", gapAhead: "+0.2", lapDelta: "-0.1", bestLapMs: 18201, lastLapMs: 18777, predictedRemainingMs: 9000 };
const html = renderOverlayLeaderboard([row], deps, { practiceRank: true });
assert.match(html, /overlay-race-list-ranked/);
assert.match(html, /pos-pill pos-1">1</);
assert.match(html, /<label>table\.result<\/label>\s*<strong>3 varv 55\.123<\/strong>/);
assert.match(html, /<label>table\.gap<\/label>\s*<strong>\+0\.747s<\/strong>/);
assert.match(html, /<label>table\.best_lap<\/label>\s*<strong>18201<\/strong>/);
assert.match(html, /class="overlay-prediction"/, "the next-lap bar stays");
for (const gone of ["table.laps", "table.ahead_gap", "table.own_delta"]) assert.doesNotMatch(html, new RegExp(gone.replace(".", "\\.")), gone);
});