Open History to see the last commit
1import { afterEach, describe, expect, it } from 'vitest'
2
3import { DEEPGRAM_PRICING_URL } from '../deepgram-pricing.ts'
4import { provider } from './deepgram.ts'
5
6const originalFetch = globalThis.fetch
7
8afterEach(() => {
9 globalThis.fetch = originalFetch
10})
11
12function pricingPage(rows: Array<[string, number, string]>): string {
13 const offers = rows.map(([product, price, unitText]) => ({
14 '@type': 'Offer',
15 name: `${product} — Pay as you go`,
16 category: 'Pay as you go',
17 price,
18 priceCurrency: 'USD',
19 priceSpecification: { price, priceCurrency: 'USD', unitText },
20 }))
21 return `<script type="application/ld+json">${JSON.stringify({ '@graph': offers })}</script>`
22}
23
24describe('deepgram provider', () => {
25 it('exports seed metadata for auto-registration', () => {
26 expect(provider.id).toBe('deepgram')
27 expect(provider.displayName).toBe('Deepgram')
28 expect(provider.authEnvVar).toBe('DEEPGRAM_API_KEY')
29 expect(provider.specSourceUrl).toBe(
30 'https://developers.deepgram.com/openapi.json',
31 )
32 expect(provider.modelsEndpoint).toBe('https://api.deepgram.com/v1/models')
33 expect(provider.defaultDerivation).toBe('upstream-spec')
34 })
35})
36
37describe('deepgram classify', () => {
38 it('maps listen, speak, and agent converse to audio', () => {
39 expect(provider.classify('/v1/listen', {})).toBe('audio')
40 expect(provider.classify('/v2/listen', {})).toBe('audio')
41 expect(provider.classify('/v1/speak', {})).toBe('audio')
42 expect(provider.classify('/v2/speak', {})).toBe('audio')
43 expect(provider.classify('/v1/agent/converse', {})).toBe('audio')
44 })
45
46 it('drops project, key, auth, read, and models surfaces', () => {
47 expect(provider.classify('/v1/projects', {})).toBeNull()
48 expect(provider.classify('/v1/projects/{project_id}/keys', {})).toBeNull()
49 expect(provider.classify('/v1/projects/{project_id}/agents', {})).toBeNull()
50 expect(provider.classify('/v1/auth/grant', {})).toBeNull()
51 expect(provider.classify('/v1/read', {})).toBeNull()
52 expect(provider.classify('/v1/models', {})).toBeNull()
53 })
54})
55
56describe('deepgram listModels', () => {
57 it('skips when the API key is absent', async () => {
58 const result = await provider.listModels({})
59 expect(result.models).toEqual([])
60 expect(result.skipped).toBe('deepgram: DEEPGRAM_API_KEY not set — skipped')
61 })
62
63 it('lists STT and TTS models with Token auth', async () => {
64 const calls: Array<{ url: string; init?: RequestInit }> = []
65 globalThis.fetch = ((url: string, init?: RequestInit) => {
66 calls.push({ url: String(url), init })
67 if (String(url) === DEEPGRAM_PRICING_URL) {
68 return Promise.resolve(
69 new Response(
70 pricingPage([
71 [
72 'Speech-to-text: Flux STT English (Streaming)',
73 0.0065,
74 'minute',
75 ],
76 [
77 'Speech-to-text: Flux STT Multilingual (Streaming)',
78 0.0078,
79 'minute',
80 ],
81 [
82 'Speech-to-text: Nova-3 Monolingual (Streaming)',
83 0.0048,
84 'minute',
85 ],
86 [
87 'Speech-to-text: Nova-3 Multilingual (Streaming)',
88 0.0058,
89 'minute',
90 ],
91 [
92 'Speech-to-text: Nova-3 Monolingual (Pre-recorded)',
93 0.0043,
94 'minute',
95 ],
96 [
97 'Speech-to-text: Nova-3 Multilingual (Pre-recorded)',
98 0.0052,
99 'minute',
100 ],
101 ['Text-to-speech: Flux TTS', 0.045, '1,000 characters'],
102 ['Text-to-speech: Aura-2', 0.03, '1,000 characters'],
103 ['Text-to-speech: Aura-1', 0.015, '1,000 characters'],
104 ]),
105 ),
106 )
107 }
108 return Promise.resolve(
109 new Response(
110 JSON.stringify({
111 stt: [{ name: 'Nova-3', canonical_name: 'nova-3' }],
112 tts: [
113 { name: 'Aura-2 Thalia', canonical_name: 'aura-2-thalia-en' },
114 { name: 'Nova-2', canonical_name: 'nova-2-general' },
115 ],
116 }),
117 ),
118 )
119 }) as typeof fetch
120
121 const result = await provider.listModels({ DEEPGRAM_API_KEY: 'test-key' })
122 expect(result.skipped).toBeUndefined()
123 expect(
124 result.models.map(
125 ({ pricing: _pricing, factSources: _sources, ...model }) => model,
126 ),
127 ).toEqual([
128 {
129 rawId: 'nova-3',
130 displayName: 'Nova-3',
131 activity: 'audio',
132 modalities: { input: ['audio'], output: ['text'] },
133 schemaEndpointId: 'v1/listen',
134 },
135 {
136 rawId: 'aura-2-thalia-en',
137 displayName: 'Aura-2 Thalia',
138 activity: 'audio',
139 modalities: { input: ['text'], output: ['audio'] },
140 schemaEndpointId: 'v1/speak',
141 },
142 {
143 rawId: 'nova-2-general',
144 displayName: 'Nova-2',
145 activity: 'audio',
146 modalities: { input: ['text'], output: ['audio'] },
147 schemaEndpointId: 'v1/speak',
148 },
149 ])
150 expect(result.models[0]?.pricing).toBeTruthy()
151 expect(result.models[1]?.pricing).toBeTruthy()
152 expect(result.models[2]?.pricing ?? null).toBeNull()
153 expect(calls[0]).toEqual({
154 url: 'https://api.deepgram.com/v1/models',
155 init: { headers: { Authorization: 'Token test-key' } },
156 })
157 expect(calls[1]?.url).toBe(DEEPGRAM_PRICING_URL)
158 })
159})
160
161describe('deepgram fetchSpec', () => {
162 it('loads the public OpenAPI document without a key', async () => {
163 const spec = {
164 openapi: '3.1.0',
165 info: { title: 'REST API', version: '1.0.0' },
166 paths: {
167 '/v1/listen': { post: { summary: 'Transcribe' } },
168 },
169 }
170 const urls: Array<string> = []
171 globalThis.fetch = ((url: string) => {
172 urls.push(String(url))
173 return Promise.resolve(new Response(JSON.stringify(spec)))
174 }) as typeof fetch
175
176 const result = await provider.fetchSpec({})
177 expect(urls).toEqual(['https://developers.deepgram.com/openapi.json'])
178 expect(result.skipped).toBeUndefined()
179 expect(result.outputStrategy).toBe('post-200')
180 expect(result.specs).toHaveLength(1)
181 expect(result.specs[0]?.info?.title).toBe('REST API')
182 expect(result.sources[0]?.url).toBe(
183 'https://developers.deepgram.com/openapi.json',
184 )
185 expect(result.sources[0]?.hash).toMatch(/^[0-9a-f]{64}$/)
186 })
187})