Files
simstudioai--sim/apps/sim/app/api/tools/stt/route.test.ts
T
wehub-resource-sync d25d482dc2
CI / Migrate Dev DB (push) Has been skipped
CI / Detect Version (push) Has been cancelled
CI / Migrate DB (push) Has been cancelled
CI / Build Dev ECR (./docker/app.Dockerfile, ECR_APP) (push) Has been cancelled
CI / Build Dev ECR (./docker/db.Dockerfile, ECR_MIGRATIONS) (push) Has been cancelled
CI / Build Dev ECR (./docker/pii.Dockerfile, ECR_PII) (push) Has been cancelled
CI / Build Dev ECR (./docker/realtime.Dockerfile, ECR_REALTIME) (push) Has been cancelled
CI / Deploy Trigger.dev (Dev) (push) Has been cancelled
CI / Build AMD64 (./docker/app.Dockerfile, ECR_APP, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build AMD64 (./docker/db.Dockerfile, ECR_MIGRATIONS, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build AMD64 (./docker/pii.Dockerfile, ECR_PII, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build AMD64 (./docker/realtime.Dockerfile, ECR_REALTIME, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/app.Dockerfile, ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/db.Dockerfile, ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/pii.Dockerfile, ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Build ARM64 (GHCR Only) (./docker/realtime.Dockerfile, ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/migrations) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/pii) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/realtime) (push) Has been cancelled
CI / Create GHCR Manifests (ghcr.io/simstudioai/simstudio) (push) Has been cancelled
CI / Check Docs Changes (push) Has been cancelled
CI / Process Docs (push) Has been cancelled
CI / Create GitHub Release (push) Has been cancelled
CI / Test and Build (push) Has been cancelled
Publish CLI Package / publish-npm (push) Has been cancelled
Publish Python SDK / publish-pypi (push) Has been cancelled
Publish TypeScript SDK / publish-npm (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:20:55 +08:00

116 lines
3.5 KiB
TypeScript

/**
* @vitest-environment node
*/
import {
createMockRequest,
hybridAuthMockFns,
inputValidationMock,
inputValidationMockFns,
} from '@sim/testing'
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { PayloadSizeLimitError } from '@/lib/core/utils/stream-limits'
const { mockIsInternalFileUrl, mockDownloadFileFromStorage, mockResolveInternalFileUrl } =
vi.hoisted(() => ({
mockIsInternalFileUrl: vi.fn(),
mockDownloadFileFromStorage: vi.fn(),
mockResolveInternalFileUrl: vi.fn(),
}))
vi.mock('@/lib/core/security/input-validation.server', () => inputValidationMock)
vi.mock('@/lib/uploads/utils/file-utils', () => ({
isInternalFileUrl: mockIsInternalFileUrl,
getMimeTypeFromExtension: vi.fn(() => 'application/octet-stream'),
}))
vi.mock('@/lib/uploads/utils/file-utils.server', () => ({
downloadFileFromStorage: mockDownloadFileFromStorage,
resolveInternalFileUrl: mockResolveInternalFileUrl,
}))
vi.mock('@/app/api/files/authorization', () => ({
assertToolFileAccess: vi.fn().mockResolvedValue(null),
}))
vi.mock('@/lib/audio/extractor', () => ({
isVideoFile: vi.fn(() => false),
extractAudioFromVideo: vi.fn(),
}))
import { POST } from '@/app/api/tools/stt/route'
const PINNED_IP = '93.184.216.34'
const baseBody = {
provider: 'whisper',
apiKey: 'test-api-key',
audioUrl: 'https://example.com/audio.mp3',
}
function mockSecureFetchResponse(body: { ok?: boolean; contentType?: string }) {
return {
ok: body.ok ?? true,
status: 200,
statusText: '',
headers: new Headers({ 'content-type': body.contentType ?? 'audio/mpeg' }),
body: null,
text: async () => '',
json: async () => ({}),
arrayBuffer: async () => new ArrayBuffer(8),
}
}
describe('POST /api/tools/stt', () => {
beforeEach(() => {
vi.clearAllMocks()
hybridAuthMockFns.mockCheckInternalAuth.mockResolvedValue({
success: true,
userId: 'user-1',
authType: 'internal_jwt',
})
inputValidationMockFns.mockValidateUrlWithDNS.mockResolvedValue({
isValid: true,
resolvedIP: PINNED_IP,
originalHostname: 'example.com',
})
mockIsInternalFileUrl.mockReturnValue(false)
vi.stubGlobal(
'fetch',
vi.fn().mockResolvedValue({
ok: true,
json: async () => ({ text: 'hello world', language: 'en', duration: 1.2 }),
})
)
})
it('bounds the audioUrl download and rejects oversized responses cleanly', async () => {
inputValidationMockFns.mockSecureFetchWithPinnedIP.mockRejectedValueOnce(
new PayloadSizeLimitError({
label: 'response body',
maxBytes: 100 * 1024 * 1024,
observedBytes: 200 * 1024 * 1024,
})
)
const response = await POST(createMockRequest('POST', baseBody))
expect(response.status).toBe(413)
const data = (await response.json()) as { error: string }
expect(data.error).toMatch(/exceeds the maximum supported size/i)
const call = inputValidationMockFns.mockSecureFetchWithPinnedIP.mock.calls[0]
expect(call[1]).toBe(PINNED_IP)
expect(call[2]).toMatchObject({ maxResponseBytes: 100 * 1024 * 1024 })
})
it('transcribes a normal, well-under-cap audio download successfully', async () => {
inputValidationMockFns.mockSecureFetchWithPinnedIP.mockResolvedValueOnce(
mockSecureFetchResponse({})
)
const response = await POST(createMockRequest('POST', baseBody))
expect(response.status).toBe(200)
const data = (await response.json()) as { transcript: string }
expect(data.transcript).toBe('hello world')
})
})