36 lines
2.0 KiB
JavaScript
36 lines
2.0 KiB
JavaScript
import test from "node:test";
|
|
import assert from "node:assert/strict";
|
|
import { getPracticeRankMetric, formatPracticeRankGap } from "../src/timing_logic.js";
|
|
|
|
// Passings 1 s apart start; each lap's timestamp follows the previous by its lapMs.
|
|
function run(startTs, laps) {
|
|
const passings = [{ timestamp: startTs, lapMs: 0 }];
|
|
let ts = startTs;
|
|
laps.forEach((lapMs) => { ts += lapMs; passings.push({ timestamp: ts, lapMs }); });
|
|
return passings;
|
|
}
|
|
|
|
test("best lap ranking gives no metric", () => {
|
|
assert.equal(getPracticeRankMetric({ practiceRanking: "best_lap" }, run(0, [18000, 17000])), null);
|
|
});
|
|
|
|
test("best N in a row stays inside one run (a stop in the pits splits)", () => {
|
|
const passings = [...run(0, [18000, 17000, 30000]), ...run(10_000_000, [16000, 16000])];
|
|
const metric = getPracticeRankMetric({ practiceRanking: "consecutive", practiceRankLaps: 3 }, passings);
|
|
assert.equal(metric.totalMs, 65000);
|
|
assert.equal(metric.lapCount, 3);
|
|
assert.equal(getPracticeRankMetric({ practiceRanking: "consecutive", practiceRankLaps: 2 }, passings).totalMs, 32000);
|
|
assert.equal(getPracticeRankMetric({ practiceRanking: "consecutive", practiceRankLaps: 4 }, passings), null);
|
|
});
|
|
|
|
test("best X minutes: most laps inside the window, then the shorter time", () => {
|
|
const fast = getPracticeRankMetric({ practiceRanking: "window", practiceRankMin: 1 }, run(0, [20000, 20000, 19000, 25000]));
|
|
assert.deepEqual([fast.lapCount, fast.totalMs], [3, 59000]);
|
|
const slow = getPracticeRankMetric({ practiceRanking: "window", practiceRankMin: 1 }, run(0, [21000, 21000, 21000]));
|
|
assert.deepEqual([slow.lapCount, slow.totalMs], [2, 42000]);
|
|
assert.ok(fast.comparableMs < slow.comparableMs, "more laps ranks first");
|
|
assert.equal(formatPracticeRankGap({ practiceMetric: slow }, { practiceMetric: fast }), "+1L");
|
|
const tie = getPracticeRankMetric({ practiceRanking: "window", practiceRankMin: 1 }, run(0, [20000, 20000, 19500]));
|
|
assert.equal(formatPracticeRankGap({ practiceMetric: tie }, { practiceMetric: fast }), "+0.500s");
|
|
});
|