feat(audio): practice hype calls and leader calls in qualifying and finals (1.61.0)

This commit is contained in:
larssand committed 2026-11-05 08:19:45 +01:00
1 parent f602778895
commit 94ec278f53
13 files changed
+334 -9

No files matched your search

+10
View File
@@ -298,3 +298,13 @@ test("non-passing decoder messages do not persist race state", () => {
assert.equal(Boolean(changed), false);
assert.equal(harness.getSaveCount(), 0);
});
test("practice hype: 50 laps calls a lap milestone; a race session never gets hype calls", () => {
const practice = createHarness({ sessionType: "open_practice" });
for (let lap = 1; lap <= 50; lap += 1) practice.passing(1_000 + lap * 20_000, "main");
const milestone = practice.audioCalls.find((item) => item.key === "practice_lap_milestone");
assert.equal(milestone?.variables.laps, 50);
const race = createHarness({ sessionType: "heat" });
for (let lap = 1; lap <= 50; lap += 1) race.passing(1_000 + lap * (20_000 - lap * 10), "main");
assert.equal(race.audioEvents.some((key) => key.startsWith("practice_")), false);
});
+66
View File
@@ -0,0 +1,66 @@
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);
});
+15 -2
View File
@@ -138,12 +138,16 @@ test("after the count-down's 3 and the tone call it is quiet until the tone 1-3
}
});
test("during a race only the passing tone plays by default - no names, personal bests or leader calls", () => {
test("during a race only the passing tone and leader calls play by default - no personal bests", () => {
const race = { type: "heat" };
assert.equal(normalizeRaceAudioSettings({}).raceCallouts, "passing");
["personal_best", "fastest_lap", "track_record", "new_leader", "position_gain", "position_loss"].forEach((key) => {
["personal_best", "fastest_lap", "track_record", "position_gain", "position_loss"].forEach((key) => {
assert.equal(isSilencedDuringRace(key, {}, race), true, key);
});
assert.equal(isSilencedDuringRace("new_leader", {}, race), false, "leader change is called by default");
assert.equal(isSilencedDuringRace("leader_update", {}, race), false);
assert.equal(isSilencedDuringRace("leader_update", { raceLeaderCalls: "change" }, race), true);
assert.equal(isSilencedDuringRace("new_leader", { raceLeaderCalls: "off" }, race), true);
["transponder_pass", "final_lap", "one_minute_remaining", "race_finished", "race_winner", "clock_running"].forEach((key) => {
assert.equal(isSilencedDuringRace(key, {}, race), false, `${key} is not a per-passing call`);
});
@@ -333,3 +337,12 @@ test("lap times are spoken as seconds with hundredths, no leading zero minute",
assert.equal(formatSpokenLapTime(65320, "sv"), "1 minut 5,32");
assert.equal(formatSpokenLapTime(125000, "en"), "2 minutes 5.00");
});
test("practice hype calls only sound in practice and can be turned off", () => {
assert.equal(isSilencedDuringRace("practice_lap_milestone", {}, { type: "open_practice" }), false);
assert.equal(isSilencedDuringRace("practice_lap_milestone", {}, { type: "practice" }), false);
["qualification", "heat", "final", "team_race"].forEach((type) => {
assert.equal(isSilencedDuringRace("practice_duel", { raceCallouts: "full" }, { type }), true, type);
});
assert.equal(isSilencedDuringRace("practice_close_to_best", { practiceHype: false }, { type: "open_practice" }), true);
});
+1
View File
@@ -1,4 +1,5 @@
import "./auth.test.js";
import "./practice_hype.test.js";
import "./race_day_fixes.test.js";
import "./race_list.test.js";
import "./day_schedule.test.js";