From 5e266c07330b45d171c83d9b850bb75a0f47262b Mon Sep 17 00:00:00 2001 From: itsrubberduck Date: Fri, 10 Jul 2026 09:07:33 +0200 Subject: [PATCH] refactor(live-atc): extract normalizeManualFreq to shared/utils, add tests Co-Authored-By: Claude Sonnet 5 --- app/pages/live-atc.vue | 12 +----------- shared/utils/frequency.ts | 10 ++++++++++ tests/shared/frequency.test.ts | 26 ++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 11 deletions(-) create mode 100644 shared/utils/frequency.ts create mode 100644 tests/shared/frequency.test.ts diff --git a/app/pages/live-atc.vue b/app/pages/live-atc.vue index 2927eee..dbf3ef0 100644 --- a/app/pages/live-atc.vue +++ b/app/pages/live-atc.vue @@ -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.000–136.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) diff --git a/shared/utils/frequency.ts b/shared/utils/frequency.ts new file mode 100644 index 0000000..0db89a9 --- /dev/null +++ b/shared/utils/frequency.ts @@ -0,0 +1,10 @@ +// Accepts inputs like "121.5", "121,500" or "118" and normalises to a valid +// VHF airband frequency string (118.000–136.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) +} diff --git a/tests/shared/frequency.test.ts b/tests/shared/frequency.test.ts new file mode 100644 index 0000000..06f9e44 --- /dev/null +++ b/tests/shared/frequency.test.ts @@ -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) +})