test(live-atc): cover the wait auto-tune actually depends on

The decision to tune was tested; the three seconds between announcing it and
doing it were not, and that gap is where the feature can go wrong — it is the
only window in which the radio moves on its own.

Moves the scheduling into a testable unit and wires the composable to it, so
the tested code is the code that runs. Covered: nothing is announced when no
change is due, the change is announced immediately but made only after the
delay, and it is dropped when the pilot tuned the radio themselves, when the
session ended, and when a second handoff superseded it — the last of which would
otherwise have tuned to a stale frequency.

Leaving the flight or unmounting now cancels a pending change explicitly rather
than relying on the fire-time guards to notice.
This commit is contained in:
itsrubberduck
2026-07-27 10:04:07 +02:00
parent cd4c640040
commit db1c9948dc
3 changed files with 204 additions and 45 deletions

View File

@@ -48,6 +48,86 @@ export function announcementFor(frequency: string): string {
/**
* The change due right now, or null when none is.
*/
export interface AutoTuneSchedulerDeps {
/** Speak and log the announcement. Called immediately, before the wait. */
announce: (text: string) => void
/** Dial the frequency in. Called only if the change is still due. */
tune: (frequency: string) => void
/** The session the change belongs to; a different one invalidates it. */
currentSessionId: () => string | null
/** What is tuned right now; a change means the pilot reached for the radio. */
currentActive: () => string | undefined
setTimeoutFn?: (fn: () => void, ms: number) => unknown
clearTimeoutFn?: (handle: unknown) => void
/** Reason a pending change was dropped, for the log. */
onCancelled?: (reason: 'session_changed' | 'tuned_manually' | 'superseded') => void
}
export interface AutoTuneScheduler {
/** Announce and schedule, or do nothing when no change is due. */
schedule: (input: AutoTuneInput) => AutoTunePlan | null
/** Drop a pending change without tuning. */
cancel: () => void
readonly pending: boolean
}
/**
* Announces the change, waits, then makes it — unless something happened in
* between that means it should no longer happen.
*
* The wait is where this earns its keep: between announcing and tuning, the
* session can end, the pilot can tune the radio themselves, or another handoff
* can supersede this one. All three must drop the change silently rather than
* moving the radio out from under the pilot.
*/
export function createAutoTuneScheduler(deps: AutoTuneSchedulerDeps): AutoTuneScheduler {
const setTimer = deps.setTimeoutFn ?? ((fn, ms) => setTimeout(fn, ms))
const clearTimer = deps.clearTimeoutFn ?? ((h) => clearTimeout(h as any))
let handle: unknown = null
const cancel = () => {
if (handle !== null) {
clearTimer(handle)
handle = null
}
}
return {
get pending() {
return handle !== null
},
cancel,
schedule(input: AutoTuneInput) {
if (handle !== null) {
cancel()
deps.onCancelled?.('superseded')
}
const plan = planAutoTune(input)
if (!plan) return null
deps.announce(plan.announcement)
const sessionAtArm = deps.currentSessionId()
const activeAtArm = deps.currentActive()
handle = setTimer(() => {
handle = null
if (deps.currentSessionId() !== sessionAtArm) {
deps.onCancelled?.('session_changed')
return
}
if (deps.currentActive() !== activeAtArm) {
deps.onCancelled?.('tuned_manually')
return
}
deps.tune(plan.frequency)
}, plan.delayMs)
return plan
},
}
}
export function planAutoTune(input: AutoTuneInput): AutoTunePlan | null {
if (!input.enabled) return null