Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

deepgram-pricing.test.ts

158 lines4.9 KB

1import { describe, expect, it } from 'vitest'
2​
3import { price } from '@modelschemas/rate-card'
4import type { RateCard } from '@modelschemas/rate-card'
5​
6import { deepgramCard, parseDeepgramPricing } from './deepgram-pricing.ts'
7​
8const SOURCE = {
9 url: 'https://deepgram.com/pricing',
10 hash: 'abc',
11 extractedAt: '2026-10-03T00:00:00.000Z',
13​
14type Row = [product: string, amount: number, unit?: string]
15​
16function offer(product: string, plan: string, amount: number, unit: string) {
17 return {
18 '@type': 'Offer',
19 name: `${product} — ${plan}`,
20 category: plan,
21 price: amount,
22 priceCurrency: 'USD',
23 priceSpecification: {
24 '@type': 'UnitPriceSpecification',
25 price: amount,
26 priceCurrency: 'USD',
27 unitText: unit,
28 },
29 }
31​
32function page(rows: Array<Row>, extra: Array<object> = []): string {
33 const items = rows.map(([product, amount, unit]) =>
34 offer(
35 product,
36 'Pay as you go',
37 amount,
38 unit ??
39 (product.startsWith('Text-to-speech: ')
40 ? '1,000 characters'
41 : 'minute'),
42 ),
43 )
44 const ld = {
45 '@context': 'https://schema.org',
46 '@graph': [
47 {
48 '@type': 'OfferCatalog',
49 name: 'Rates by product',
50 itemListElement: [...items, ...extra],
51 },
52 ],
53 }
54 return `<html><script type="application/ld+json">${JSON.stringify(ld)}</script></html>`
56​
57const ROWS: Array<Row> = [
58 ['Speech-to-text: Flux STT English (Streaming)', 0.0077],
59 ['Speech-to-text: Flux STT Multilingual (Streaming)', 0.0078],
60 ['Speech-to-text: Nova-3 Monolingual (Streaming)', 0.0048],
61 ['Speech-to-text: Nova-3 Multilingual (Streaming)', 0.0058],
62 ['Speech-to-text: Nova-3 Monolingual (Pre-recorded)', 0.0043],
63 ['Speech-to-text: Nova-3 Multilingual (Pre-recorded)', 0.0052],
64 ['Speech-to-text: Whisper Large (Pre-recorded)', 0.0048],
65 ['Text-to-speech: Flux TTS', 0.045],
66 ['Text-to-speech: Aura-2', 0.03],
67 ['Text-to-speech: Aura-1', 0.015],
69​
70const PAGE = page(ROWS, [
71 offer(
72 'Speech-to-text: Nova-3 Monolingual (Streaming)',
73 'Growth',
74 9.99,
75 'minute',
76 ),
77])
78​
79function usd(
80 card: RateCard | null,
81 usage: Record<string, number | string>,
82): number {
83 if (!card) throw new Error('missing card')
84 return price(card, {}, usage)
86​
87describe('deepgram pricing page', () => {
88 it('reads Pay As You Go offers from the JSON-LD catalog, not Growth', () => {
89 const rates = parseDeepgramPricing(PAGE)
90 expect(rates.get('Speech-to-text: Flux STT English (Streaming)')).toBe(
91 0.0077,
92 )
93 expect(rates.get('Text-to-speech: Aura-2')).toBe(0.03)
94 expect(rates.get('Text-to-speech: Aura-1')).toBe(0.015)
95 expect(rates.get('Speech-to-text: Nova-3 Monolingual (Streaming)')).toBe(
96 0.0048,
97 )
98 })
99​
100 it('refuses the page when a required rate carries the wrong unit or is missing', () => {
101 const wrongUnit = ROWS.map((row): Row =>
102 row[0] === 'Text-to-speech: Aura-2' ? [row[0], row[1], 'minute'] : row,
103 )
104 expect(parseDeepgramPricing(page(wrongUnit))).toEqual(new Map())
105 expect(parseDeepgramPricing(page(ROWS.slice(1)))).toEqual(new Map())
106 })
107​
108 it('refuses conflicting Pay as you go rates and ignores a Growth-category offer', () => {
109 const aura = offer(
110 'Text-to-speech: Aura-2',
111 'Pay as you go',
112 0.04,
113 '1,000 characters',
114 )
115 expect(parseDeepgramPricing(page(ROWS, [aura]))).toEqual(new Map())
116 const growth = { ...aura, category: 'Growth' }
117 expect(
118 parseDeepgramPricing(page(ROWS, [growth])).get('Text-to-speech: Aura-2'),
119 ).toBe(0.03)
120 })
121​
122 it('shares one Aura-2 character rate and prices Nova-3 by mode', () => {
123 const rates = parseDeepgramPricing(PAGE)
124 const aura = deepgramCard('aura-2-thalia-en', rates, SOURCE)
125 const aura1 = deepgramCard('aura-asteria-en', rates, SOURCE)
126 const fluxVoice = deepgramCard('flux-alexis-en', rates, SOURCE)
127 const nova = deepgramCard('nova-3-general', rates, SOURCE)
128 const medical = deepgramCard('nova-3-medical', rates, SOURCE)
129 expect(usd(aura, { characters: 1000 })).toBeCloseTo(0.03, 9)
130 expect(usd(aura1, { characters: 1000 })).toBeCloseTo(0.015, 9)
131 expect(usd(fluxVoice, { characters: 1000 })).toBeCloseTo(0.045, 9)
132 expect(
133 usd(nova, {
134 audio_seconds: 60,
135 listen_mode: 'streaming',
136 language_scope: 'monolingual',
137 }),
138 ).toBeCloseTo(0.0048, 9)
139 expect(
140 usd(nova, {
141 audio_seconds: 60,
142 listen_mode: 'pre_recorded',
143 language_scope: 'multilingual',
144 }),
145 ).toBeCloseTo(0.0052, 9)
146 expect(
147 usd(medical, { audio_seconds: 60, listen_mode: 'pre_recorded' }),
148 ).toBeCloseTo(0.0043, 9)
149 expect(deepgramCard('nova-2-general', rates, SOURCE)).toBeNull()
150 expect(deepgramCard('whisper-large', rates, SOURCE)).toBeNull()
151 expect(deepgramCard('enhanced-general', rates, SOURCE)).toBeNull()
152 expect(
153 usd(deepgramCard('flux-general-en', rates, SOURCE), {
154 audio_seconds: 60,
155 }),
156 ).toBeCloseTo(0.0077, 9)
157 })
158})