sttMock.ts
579 bytes
| 1 | import type { SttProvider } from './types' |
|---|---|
| 2 | |
| 3 | export function createSttMock(): SttProvider { |
| 4 | let counter = 0 |
| 5 | |
| 6 | return { |
| 7 | async transcribe(audio: Buffer): Promise<string> { |
| 8 | const echoed = decodeIfUtf8Text(audio) |
| 9 | if (echoed !== null) return echoed |
| 10 | counter += 1 |
| 11 | return `mock transcript ${counter}` |
| 12 | }, |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | function decodeIfUtf8Text(buffer: Buffer): string | null { |
| 17 | if (buffer.length === 0) return null |
| 18 | const decoded = buffer.toString('utf8') |
| 19 | const roundTrip = Buffer.from(decoded, 'utf8') |
| 20 | return roundTrip.equals(buffer) ? decoded : null |
| 21 | } |
| 22 | |