fix(stt-proxy): stop paid fan-out on empty transcripts and redact provider errors

This commit is contained in:
Yun Chan 2026-09-28 02:16:19 +09:00
parent 1a4c39cb94
commit 1eb22af1f3
9 changed files with 1107 additions and 380 deletions

View file

@ -0,0 +1,37 @@
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: <html>secret')), 'error', 'other')
assertEquals(sttTransportFailure('boom'), 'error', 'non-error')
assertEquals(sttHttpFailure(502), 'http_502', 'http')
})