refactor(live-atc): extract normalizeManualFreq to shared/utils, add tests

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
itsrubberduck
2026-07-10 09:07:33 +02:00
parent fb25af913a
commit 5e266c0733
3 changed files with 37 additions and 11 deletions

View File

@@ -1523,6 +1523,7 @@ import { loadPizzicatoLite } from '../../shared/utils/pizzicatoLite'
import type { PizzicatoLite } from '../../shared/utils/pizzicatoLite'
import { createNoiseGenerators, getReadabilityProfile } from '../../shared/utils/radioEffects'
import { createAtisAudioLoop, type AtisAudioLoop } from '../../shared/utils/atisAudioLoop'
import { normalizeManualFreq } from '../../shared/utils/frequency'
import type {
CandidateTraceElimination,
CandidateTraceEntry,
@@ -4937,17 +4938,6 @@ const onPresetSelectStandby = (opt: { value: string | number }) => {
const manualFreqActive = ref('')
const manualFreqStandby = ref('')
// Accepts inputs like "121.5", "121,500" or "118" and normalises to a valid
// VHF airband frequency string (118.000136.975). Returns null when invalid so
// callers/UI can disable the action.
function normalizeManualFreq(input: string): string | null {
const raw = input.trim().replace(',', '.')
if (!raw) return null
const num = Number(raw)
if (!Number.isFinite(num) || num < 118 || num >= 137) return null
return num.toFixed(3)
}
function applyManualFrequency(target: 'active' | 'standby', close?: () => void) {
const model = target === 'active' ? manualFreqActive : manualFreqStandby
const freq = normalizeManualFreq(model.value)

10
shared/utils/frequency.ts Normal file
View File

@@ -0,0 +1,10 @@
// Accepts inputs like "121.5", "121,500" or "118" and normalises to a valid
// VHF airband frequency string (118.000136.975). Returns null when invalid so
// callers/UI can disable the action.
export function normalizeManualFreq(input: string): string | null {
const raw = input.trim().replace(',', '.')
if (!raw) return null
const num = Number(raw)
if (!Number.isFinite(num) || num < 118 || num >= 137) return null
return num.toFixed(3)
}

View File

@@ -0,0 +1,26 @@
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { normalizeManualFreq } from '../../shared/utils/frequency.ts'
test('normalizeManualFreq pads a bare integer to three decimals', () => {
assert.equal(normalizeManualFreq('121'), '121.000')
})
test('normalizeManualFreq accepts a comma decimal separator', () => {
assert.equal(normalizeManualFreq('121,3'), '121.300')
})
test('normalizeManualFreq rejects values outside the VHF airband', () => {
assert.equal(normalizeManualFreq('99.000'), null)
assert.equal(normalizeManualFreq('110.000'), null)
assert.equal(normalizeManualFreq('140.000'), null)
})
test('normalizeManualFreq accepts the lower airband boundary', () => {
assert.equal(normalizeManualFreq('118'), '118.000')
})
test('normalizeManualFreq rejects garbage input', () => {
assert.equal(normalizeManualFreq('abc'), null)
assert.equal(normalizeManualFreq(''), null)
})