import { sttHttpFailure, sttOutcomeFromBilledAnswer, sttTransportFailure } from './stt-contract.ts' function assertEquals(actual: unknown, expected: unknown, message: string): void { const a = JSON.stringify(actual) const e = JSON.stringify(expected) if (a !== e) throw new Error(`${message}: expected ${e}, got ${a}`) } const valid = { confidence: 0.9, language_code: 'ko', duration_seconds: 1, provider: 'groq' } Deno.test('a billed empty transcript is no_speech, not a failure', () => { assertEquals(sttOutcomeFromBilledAnswer({ ...valid, transcript: '' }), { kind: 'no_speech' }, 'empty') assertEquals(sttOutcomeFromBilledAnswer({ ...valid, transcript: ' \n ' }), { kind: 'no_speech' }, 'whitespace') }) Deno.test('a billed answer without a usable transcript is a billed invalid_response', () => { const invalid = { kind: 'retryable', failure: 'invalid_response', billed: true, unavailable: false } assertEquals(sttOutcomeFromBilledAnswer({ ...valid, transcript: 42 }), invalid, 'non-string transcript') assertEquals(sttOutcomeFromBilledAnswer({ ...valid, transcript: 'hi', confidence: 2 }), invalid, 'bad confidence') }) Deno.test('a billed real transcript is normalized', () => { assertEquals( sttOutcomeFromBilledAnswer({ ...valid, transcript: ' hi ' }), { kind: 'ok', result: { transcript: 'hi', ...valid } }, 'ok', ) }) Deno.test('transport failures map to fixed codes only', () => { assertEquals(sttTransportFailure(new TypeError('error sending request for url (https://internal/x)')), 'network_error', 'network') assertEquals(sttTransportFailure(new DOMException('t', 'TimeoutError')), 'timeout', 'timeout') assertEquals(sttTransportFailure(new DOMException('a', 'AbortError')), 'timeout', 'abort') assertEquals(sttTransportFailure(new SyntaxError('Unexpected token < in JSON: secret')), 'error', 'other') assertEquals(sttTransportFailure('boom'), 'error', 'non-error') assertEquals(sttHttpFailure(502), 'http_502', 'http') })