|
1 | 1 | import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest' |
2 | 2 | import { sttApi } from './stt' |
| 3 | +import { FetchError } from './fetchWrapper' |
3 | 4 |
|
4 | 5 | describe('WAV extension selection logic', () => { |
5 | 6 | const originalFetch = global.fetch |
@@ -69,6 +70,18 @@ describe('WAV extension selection logic', () => { |
69 | 70 | expect(audioFile.name).toBe('recording.m4a') |
70 | 71 | }) |
71 | 72 |
|
| 73 | + it('should return webm for audio/webm;codecs=opus blob', async () => { |
| 74 | + const blob = new Blob([], { type: 'audio/webm;codecs=opus' }) |
| 75 | + mockFetch.mockResolvedValueOnce(createMockResponse(true, { text: 'test' })) |
| 76 | + |
| 77 | + await sttApi.transcribe(blob, 'test-user') |
| 78 | + |
| 79 | + const callArgs = mockFetch.mock.calls[0] |
| 80 | + const formData = callArgs[1]?.body as FormData |
| 81 | + const audioFile = formData.get('audio') as File |
| 82 | + expect(audioFile.name).toBe('recording.webm') |
| 83 | + }) |
| 84 | + |
72 | 85 | it('should default to wav for unknown types', async () => { |
73 | 86 | const blob = new Blob([], { type: 'audio/unknown' }) |
74 | 87 | mockFetch.mockResolvedValueOnce(createMockResponse(true, { text: 'test' })) |
@@ -123,3 +136,85 @@ describe('WAV extension selection logic', () => { |
123 | 136 | expect(formData.get('audio')).toBeInstanceOf(File) |
124 | 137 | }) |
125 | 138 | }) |
| 139 | + |
| 140 | +describe('sttApi.transcribe cancellation and timeout', () => { |
| 141 | + const originalFetch = global.fetch |
| 142 | + |
| 143 | + beforeEach(() => { |
| 144 | + vi.stubGlobal('fetch', vi.fn()) |
| 145 | + }) |
| 146 | + |
| 147 | + afterEach(() => { |
| 148 | + vi.restoreAllMocks() |
| 149 | + global.fetch = originalFetch |
| 150 | + }) |
| 151 | + |
| 152 | + it('throws 499 CANCELED when caller aborts', async () => { |
| 153 | + const blob = new Blob([], { type: 'audio/webm;codecs=opus' }) |
| 154 | + const abortController = new AbortController() |
| 155 | + |
| 156 | + const mockFetch = global.fetch as ReturnType<typeof vi.fn> |
| 157 | + mockFetch.mockImplementation((_url: string, options: RequestInit) => { |
| 158 | + const signal = options.signal as AbortSignal |
| 159 | + return new Promise((_resolve, reject) => { |
| 160 | + if (signal.aborted) { |
| 161 | + const err = new Error('The operation was aborted') |
| 162 | + err.name = 'AbortError' |
| 163 | + reject(err) |
| 164 | + return |
| 165 | + } |
| 166 | + signal.addEventListener('abort', () => { |
| 167 | + const err = new Error('The operation was aborted') |
| 168 | + err.name = 'AbortError' |
| 169 | + reject(err) |
| 170 | + }, { once: true }) |
| 171 | + }) |
| 172 | + }) |
| 173 | + |
| 174 | + const promise = sttApi.transcribe(blob, 'test-user', abortController.signal) |
| 175 | + |
| 176 | + abortController.abort() |
| 177 | + |
| 178 | + await expect(promise).rejects.toThrow(FetchError) |
| 179 | + await expect(promise).rejects.toMatchObject({ |
| 180 | + statusCode: 499, |
| 181 | + code: 'CANCELED', |
| 182 | + }) |
| 183 | + }) |
| 184 | + |
| 185 | + it('throws 408 TIMEOUT when transcription times out', async () => { |
| 186 | + vi.useFakeTimers() |
| 187 | + |
| 188 | + const blob = new Blob([], { type: 'audio/webm;codecs=opus' }) |
| 189 | + |
| 190 | + const mockFetch = global.fetch as ReturnType<typeof vi.fn> |
| 191 | + mockFetch.mockImplementation((_url: string, options: RequestInit) => { |
| 192 | + const signal = options.signal as AbortSignal |
| 193 | + return new Promise((_resolve, reject) => { |
| 194 | + if (signal.aborted) { |
| 195 | + const err = new Error('The operation was aborted') |
| 196 | + err.name = 'AbortError' |
| 197 | + reject(err) |
| 198 | + return |
| 199 | + } |
| 200 | + signal.addEventListener('abort', () => { |
| 201 | + const err = new Error('The operation was aborted') |
| 202 | + err.name = 'AbortError' |
| 203 | + reject(err) |
| 204 | + }, { once: true }) |
| 205 | + }) |
| 206 | + }) |
| 207 | + |
| 208 | + const promise = sttApi.transcribe(blob, 'test-user') |
| 209 | + |
| 210 | + vi.advanceTimersByTime(60000) |
| 211 | + |
| 212 | + await expect(promise).rejects.toThrow(FetchError) |
| 213 | + await expect(promise).rejects.toMatchObject({ |
| 214 | + statusCode: 408, |
| 215 | + code: 'TIMEOUT', |
| 216 | + }) |
| 217 | + |
| 218 | + vi.useRealTimers() |
| 219 | + }) |
| 220 | +}) |
0 commit comments