import test from "node:test"; import assert from "node:assert/strict"; import { announceRaceStartedHelper, handleSessionTimerTickHelper, selectSpeechVoice, speakTextHelper, } from "../src/runtime_services.js"; test("speech voice selection prefers the chosen voice when it speaks the line's language, else the language", () => { const voices = [ { name: "English", lang: "en-US", voiceURI: "voice-en", localService: true }, { name: "Swedish", lang: "sv-SE", voiceURI: "voice-sv", localService: true }, ]; assert.equal(selectSpeechVoice(voices, "voice-en", "en-US")?.voiceURI, "voice-en"); assert.equal(selectSpeechVoice(voices, "voice-sv", "en-US")?.voiceURI, "voice-en", "a Swedish voice never reads an English line"); assert.equal(selectSpeechVoice(voices, "", "sv-SE")?.voiceURI, "voice-sv"); assert.equal(selectSpeechVoice(voices, "missing", "de-DE"), null); }); test("race start only creates the overlay event - the old start tone and voice are gone (race audio owns the start)", async () => { const calls = []; announceRaceStartedHelper( { id: "session_1", name: "A Final", type: "final" }, { // Old saved settings from the removed legacy panel must not bring the extra cues back. state: { settings: { audioEnabled: true, startSoundEnabled: true, finishVoiceEnabled: true, raceAudio: { enabled: false } } }, t: (key) => key, pushOverlayEvent: (type, label, options) => calls.push([type, label, options]), playStartCue: () => calls.push(["tone"]), speakText: (value, options) => calls.push(["speech", value, options]), } ); await new Promise((resolve) => setTimeout(resolve, 700)); assert.deepEqual(calls, [["start", "A Final • timing.start", { silentCue: true }]]); }); test("session timer emits each race-audio clock marker once without changing finish rules", () => { const active = { id: "s1", eventId: "e1", type: "final", status: "running", followUpSec: 30, audioAnnouncements: [] }; const announced = []; let remainingMs = 59_000; let saves = 0; const run = () => handleSessionTimerTickHelper({ getActiveSession: () => active, isUntimedSession: () => false, getSessionTiming: () => ({ remainingMs, followUpRemainingMs: 30_000 }), saveState: () => { saves += 1; }, getLastFinishAnnouncementSessionId: () => null, setLastFinishAnnouncementSessionId: () => {}, announceRaceFinished: () => announced.push("race_finished"), announceRaceClockEvent: (key) => announced.push(key), getFinishWait: () => wait, now: () => clock, }); let clock = 1_000_000; let wait = { allFinished: false, closeAt: clock + 20_000 }; run(); run(); remainingMs = 29_000; run(); remainingMs = 9_000; run(); remainingMs = 0; run(); run(); assert.deepEqual(announced, ["one_minute_remaining", "thirty_seconds_remaining", "ten_seconds_remaining", "time_expired"], "said once, and no separate 'final lap'"); assert.equal(active.status, "running", "waits for the cars to finish their lap"); assert.ok(active.followUpStartedAt); assert.equal(active.finishWaitUntil, wait.closeAt); assert.ok(saves >= 4); wait = { allFinished: true, closeAt: null }; run(); assert.equal(active.status, "finished", "closes as soon as every car has crossed after its time"); assert.deepEqual(announced.slice(-1), ["race_finished"]); }); test("session timer: a car still out closes the heat at the wait limit; other windows stay quiet", () => { const make = () => ({ id: "s2", eventId: "e1", type: "qualification", status: "running", startedAt: 1, followUpSec: 10, audioAnnouncements: [] }); const deps = (active, extra) => ({ getActiveSession: () => active, isUntimedSession: () => false, getSessionTiming: () => ({ remainingMs: 0, targetMs: 300_000 }), saveState: () => {}, getLastFinishAnnouncementSessionId: () => null, setLastFinishAnnouncementSessionId: () => {}, announceRaceFinished: () => {}, announceRaceClockEvent: (key) => extra.announced.push(key), ...extra.deps, }); const owner = make(); const ownerLog = { announced: [], deps: { getFinishWait: () => ({ allFinished: false, closeAt: 50_000 }), now: () => 50_001 } }; handleSessionTimerTickHelper(deps(owner, ownerLog)); assert.equal(owner.status, "finished"); const other = make(); const otherLog = { announced: [], deps: { isClockOwner: () => false, now: () => 310_000 } }; handleSessionTimerTickHelper(deps(other, otherLog)); assert.deepEqual(otherLog.announced, [], "a second window never calls the clock"); assert.equal(other.status, "running", "and leaves closing the heat to the race browser"); }); test("race audio can ignore a mismatched configured voice and select the announcement language", () => { const originalWindow = globalThis.window; const OriginalUtterance = globalThis.SpeechSynthesisUtterance; const spoken = []; const voices = [ { name: "English", lang: "en-US", voiceURI: "voice-en", localService: true }, { name: "Swedish", lang: "sv-SE", voiceURI: "voice-sv", localService: true }, ]; globalThis.window = { speechSynthesis: { speaking: false, pending: false, cancel() {}, getVoices: () => voices, speak: (utterance) => spoken.push(utterance), }, }; globalThis.SpeechSynthesisUtterance = class { constructor(text) { this.text = text; } }; try { // audioEnabled false: the removed legacy "Sound on" switch used to silence every race-audio // voice (while effects still played). It must no longer have any effect. const started = speakTextHelper("Race finished", { state: { settings: { audioEnabled: false, speechVoiceUri: "voice-sv", speechRate: 1, speechPitch: 1 } }, currentLanguage: () => "sv", }, { language: "en", voiceUri: "", rate: 1.1, pitch: 0.85 }); assert.equal(started, true); assert.equal(spoken[0].lang, "en-US"); assert.equal(spoken[0].voice.voiceURI, "voice-en"); assert.equal(spoken[0].rate, 1.1); assert.equal(spoken[0].pitch, 0.85); } finally { globalThis.window = originalWindow; globalThis.SpeechSynthesisUtterance = OriginalUtterance; } });