import test from "node:test"; import assert from "node:assert/strict"; import { pickPracticeHype, countFasterStreak, getAverageGain, getClassRank, getLastTimeBestMs, findDuelRival, MIN_GAP_MS } from "../src/practice_hype_logic.js"; const base = (over = {}) => ({ laps: [20000, 20500, 20300], lapMs: 20300, previousBestMs: 20000, lapCount: 3, now: 1_000_000, memory: {}, ...over }); test("lap milestones every 50 laps, once each", () => { const memory = {}; assert.deepEqual(pickPracticeHype(base({ lapCount: 50, memory })), { key: "practice_lap_milestone", variables: { laps: 50 } }); assert.equal(pickPracticeHype(base({ lapCount: 50, memory, now: 2_000_000 })), null); assert.equal(pickPracticeHype(base({ lapCount: 49, memory: {} })), null); }); test("close to best: within 0.1 s of the best, not on the best itself", () => { const laps = [20000, 20700, 20800, 21000, 20080]; assert.deepEqual(pickPracticeHype(base({ laps, lapMs: 20080, lapCount: 5 })), { key: "practice_close_to_best", variables: { gapMs: 80 } }); assert.equal(pickPracticeHype(base({ laps, lapMs: 20200, lapCount: 5 })), null); }); test("three faster laps in a row", () => { assert.equal(countFasterStreak([21000, 20900, 20800, 20700]), 3); assert.equal(countFasterStreak([21000, 20900, 21100]), 0); const hype = pickPracticeHype(base({ laps: [22000, 21000, 20900, 20800], lapMs: 20800, previousBestMs: 20000 })); assert.equal(hype.key, "practice_faster_streak"); }); test("average going down compares the first 10 clean laps with the last 10", () => { const laps = [...Array(10).fill(21000), 40000, ...Array(10).fill(20500)]; const gain = getAverageGain(laps); assert.equal(gain.gainMs, 500); assert.equal(gain.lastAvgMs, 20500); assert.equal(getAverageGain(Array(19).fill(20000)), null); }); test("class rank and duel rival", () => { const rows = [{ key: "a", bestLapMs: 20000 }, { key: "b", bestLapMs: 20100 }, { key: "c", bestLapMs: 0 }]; assert.deepEqual(getClassRank(rows, "b"), { rank: 2, size: 2 }); const now = 500000; const rival = findDuelRival([{ key: "a", bestLapMs: 20150, lastTimestamp: now - 1000 }, { key: "x", bestLapMs: 20050, lastTimestamp: now - 600000 }], { key: "b", bestLapMs: 20000, now }); assert.equal(rival.key, "a"); assert.equal(rival.gapMs, 150); const hype = pickPracticeHype(base({ lapMs: 19900, rival: { gapMs: 150, name: "Per" } })); assert.deepEqual(hype, { key: "practice_duel", variables: { gapMs: 150, ahead: true, rival: "Per" } }); }); test("faster than last time: best of the most recent earlier day, called once", () => { const day = (d, h = 12) => new Date(2026, 10, d, h).getTime(); const lists = [[ { driverId: "d1", trackLayoutId: "L", lapMs: 19000, timestamp: day(1) }, { driverId: "d1", trackLayoutId: "L", lapMs: 20000, timestamp: day(3) }, { driverId: "d1", trackLayoutId: "L", lapMs: 20400, timestamp: day(3, 13) }, { driverId: "d1", trackLayoutId: "other", lapMs: 15000, timestamp: day(4) }, { driverId: "d1", trackLayoutId: "L", lapMs: 18000, timestamp: day(5) }, ]]; assert.equal(getLastTimeBestMs(lists, { driverId: "d1", layoutId: "L", dayStartTs: new Date(2026, 10, 5).getTime() }), 20000); const memory = {}; assert.deepEqual(pickPracticeHype(base({ lapMs: 19700, lastTimeBestMs: 20000, memory })), { key: "practice_beat_last_time", variables: { gapMs: 300 } }); assert.notEqual(pickPracticeHype(base({ lapMs: 19600, previousBestMs: 19700, lastTimeBestMs: 20000, memory, now: 9_000_000 }))?.key, "practice_beat_last_time"); }); test("at most one call every 30 s per driver", () => { const memory = {}; const laps = [20000, 20700, 20800, 21000, 20080]; assert.ok(pickPracticeHype(base({ laps, lapMs: 20080, lapCount: 5, memory }))); assert.equal(pickPracticeHype(base({ laps: [22000, 21000, 20900, 20800], lapMs: 20800, memory, now: 1_000_000 + MIN_GAP_MS - 1 })), null); });