import test from "node:test"; import assert from "node:assert/strict"; import { shouldCallPracticePersonalBest, PRACTICE_PB_DEFAULTS } from "../src/practice_callouts_logic.js"; import { normalizeRaceAudioSettings } from "../src/race_audio.js"; // Run a driver's laps (lapMs at 20 s intervals) through the rules and return the called laps. function calledLaps(laps, rules = PRACTICE_PB_DEFAULTS) { let best = null; let lastCall = null; const called = []; laps.forEach((lapMs, index) => { const now = (index + 1) * 20000; if (shouldCallPracticePersonalBest({ lapNumber: index + 1, lapMs, previousBestMs: best, lastCall, now, rules })) { lastCall = { at: now, lapMs }; called.push(index + 1); } if (!best || lapMs < best) best = lapMs; }); return called; } test("fresh pack: no personal best calls during the first ten laps of the day", () => { const laps = [24000, 23000, 22500, 22000, 21600, 21300, 21000, 20800, 20600, 20500]; assert.deepEqual(calledLaps(laps), []); assert.equal(calledLaps(laps, { warmupLaps: 0, cooldownSec: 0, minGainMs: 0 }).length, 9, "without the rules every lap is a call"); }); test("after the warm-up a real improvement is called, then at most once a minute per driver", () => { const laps = [...Array(10).fill(21000), 20500, 20300, 20100, 21000]; // Lap 11 is called; laps 12-13 (20 and 40 s later) fall inside the cooldown. assert.deepEqual(calledLaps(laps), [11]); const later = [...Array(10).fill(21000), 20500, 21000, 21000, 21000, 20300]; assert.deepEqual(calledLaps(later), [11, 15], "a better lap after the minute is called again"); }); test("tiny improvements are called once they add up to the minimum gain", () => { const rules = { warmupLaps: 0, cooldownSec: 0, minGainMs: 100 }; // 19.95 is the first best (called); 19.88 is only 0.07 better than that; 19.84 adds up to 0.11. assert.deepEqual(calledLaps([20000, 19950, 19880, 19840, 19770], rules), [2, 4]); }); test("a lap that is not a best is never called", () => { assert.equal(shouldCallPracticePersonalBest({ lapNumber: 20, lapMs: 20000, previousBestMs: 19000, now: 0, rules: { warmupLaps: 0, cooldownSec: 0, minGainMs: 0 } }), false); assert.equal(shouldCallPracticePersonalBest({ lapNumber: 20, lapMs: 20000, previousBestMs: null, now: 0 }), false, "the first timed lap is no personal best"); }); test("the settings default to 10 laps, 60 s and 0.1 s and clamp what is typed", () => { const defaults = normalizeRaceAudioSettings({}); assert.deepEqual([defaults.practicePbWarmupLaps, defaults.practicePbCooldownSec, defaults.practicePbMinGainMs], [10, 60, 100]); const typed = normalizeRaceAudioSettings({ practicePbWarmupLaps: 99, practicePbCooldownSec: -5, practicePbMinGainMs: 0 }); assert.deepEqual([typed.practicePbWarmupLaps, typed.practicePbCooldownSec, typed.practicePbMinGainMs], [30, 0, 0]); });