|
| 1 | +/** |
| 2 | + * @vitest-environment node |
| 3 | + */ |
| 4 | +import { |
| 5 | + createMockRequest, |
| 6 | + hybridAuthMockFns, |
| 7 | + inputValidationMock, |
| 8 | + inputValidationMockFns, |
| 9 | +} from '@sim/testing' |
| 10 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 11 | +import { PayloadSizeLimitError } from '@/lib/core/utils/stream-limits' |
| 12 | + |
| 13 | +const { mockIsInternalFileUrl, mockDownloadFileFromStorage, mockResolveInternalFileUrl } = |
| 14 | + vi.hoisted(() => ({ |
| 15 | + mockIsInternalFileUrl: vi.fn(), |
| 16 | + mockDownloadFileFromStorage: vi.fn(), |
| 17 | + mockResolveInternalFileUrl: vi.fn(), |
| 18 | + })) |
| 19 | + |
| 20 | +vi.mock('@/lib/core/security/input-validation.server', () => inputValidationMock) |
| 21 | +vi.mock('@/lib/uploads/utils/file-utils', () => ({ |
| 22 | + isInternalFileUrl: mockIsInternalFileUrl, |
| 23 | + getMimeTypeFromExtension: vi.fn(() => 'application/octet-stream'), |
| 24 | +})) |
| 25 | +vi.mock('@/lib/uploads/utils/file-utils.server', () => ({ |
| 26 | + downloadFileFromStorage: mockDownloadFileFromStorage, |
| 27 | + resolveInternalFileUrl: mockResolveInternalFileUrl, |
| 28 | +})) |
| 29 | +vi.mock('@/app/api/files/authorization', () => ({ |
| 30 | + assertToolFileAccess: vi.fn().mockResolvedValue(null), |
| 31 | +})) |
| 32 | +vi.mock('@/lib/audio/extractor', () => ({ |
| 33 | + isVideoFile: vi.fn(() => false), |
| 34 | + extractAudioFromVideo: vi.fn(), |
| 35 | +})) |
| 36 | + |
| 37 | +import { POST } from '@/app/api/tools/stt/route' |
| 38 | + |
| 39 | +const PINNED_IP = '93.184.216.34' |
| 40 | + |
| 41 | +const baseBody = { |
| 42 | + provider: 'whisper', |
| 43 | + apiKey: 'test-api-key', |
| 44 | + audioUrl: 'https://example.com/audio.mp3', |
| 45 | +} |
| 46 | + |
| 47 | +function mockSecureFetchResponse(body: { ok?: boolean; contentType?: string }) { |
| 48 | + return { |
| 49 | + ok: body.ok ?? true, |
| 50 | + status: 200, |
| 51 | + statusText: '', |
| 52 | + headers: new Headers({ 'content-type': body.contentType ?? 'audio/mpeg' }), |
| 53 | + body: null, |
| 54 | + text: async () => '', |
| 55 | + json: async () => ({}), |
| 56 | + arrayBuffer: async () => new ArrayBuffer(8), |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +describe('POST /api/tools/stt', () => { |
| 61 | + beforeEach(() => { |
| 62 | + vi.clearAllMocks() |
| 63 | + hybridAuthMockFns.mockCheckInternalAuth.mockResolvedValue({ |
| 64 | + success: true, |
| 65 | + userId: 'user-1', |
| 66 | + authType: 'internal_jwt', |
| 67 | + }) |
| 68 | + inputValidationMockFns.mockValidateUrlWithDNS.mockResolvedValue({ |
| 69 | + isValid: true, |
| 70 | + resolvedIP: PINNED_IP, |
| 71 | + originalHostname: 'example.com', |
| 72 | + }) |
| 73 | + mockIsInternalFileUrl.mockReturnValue(false) |
| 74 | + |
| 75 | + vi.stubGlobal( |
| 76 | + 'fetch', |
| 77 | + vi.fn().mockResolvedValue({ |
| 78 | + ok: true, |
| 79 | + json: async () => ({ text: 'hello world', language: 'en', duration: 1.2 }), |
| 80 | + }) |
| 81 | + ) |
| 82 | + }) |
| 83 | + |
| 84 | + it('bounds the audioUrl download and rejects oversized responses cleanly', async () => { |
| 85 | + inputValidationMockFns.mockSecureFetchWithPinnedIP.mockRejectedValueOnce( |
| 86 | + new PayloadSizeLimitError({ |
| 87 | + label: 'response body', |
| 88 | + maxBytes: 100 * 1024 * 1024, |
| 89 | + observedBytes: 200 * 1024 * 1024, |
| 90 | + }) |
| 91 | + ) |
| 92 | + |
| 93 | + const response = await POST(createMockRequest('POST', baseBody)) |
| 94 | + |
| 95 | + expect(response.status).toBe(413) |
| 96 | + const data = (await response.json()) as { error: string } |
| 97 | + expect(data.error).toMatch(/exceeds the maximum supported size/i) |
| 98 | + |
| 99 | + const call = inputValidationMockFns.mockSecureFetchWithPinnedIP.mock.calls[0] |
| 100 | + expect(call[1]).toBe(PINNED_IP) |
| 101 | + expect(call[2]).toMatchObject({ maxResponseBytes: 100 * 1024 * 1024 }) |
| 102 | + }) |
| 103 | + |
| 104 | + it('transcribes a normal, well-under-cap audio download successfully', async () => { |
| 105 | + inputValidationMockFns.mockSecureFetchWithPinnedIP.mockResolvedValueOnce( |
| 106 | + mockSecureFetchResponse({}) |
| 107 | + ) |
| 108 | + |
| 109 | + const response = await POST(createMockRequest('POST', baseBody)) |
| 110 | + |
| 111 | + expect(response.status).toBe(200) |
| 112 | + const data = (await response.json()) as { transcript: string } |
| 113 | + expect(data.transcript).toBe('hello world') |
| 114 | + }) |
| 115 | +}) |
0 commit comments