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

vercel.test.ts

490 lines14.5 KB

1import { price } from '@modelschemas/rate-card'
2import type { RateCard } from '@modelschemas/rate-card'
3import { afterEach, describe, expect, it } from 'vitest'
4​
5import { OPENAI_SPEC_URL } from '../openai.ts'
6​
7import {
8 parseVercelModels,
9 provider,
10 VERCEL_MODELS_URL,
11 VERCEL_SERVER_URL,
12 vercelReasoning,
13} from './vercel.ts'
14​
15/**
16 * Excerpt of https://ai-gateway.vercel.sh/v1/models (2026-10-08).
17 * `pricing` values are the gateway's USD-per-token strings.
18 */
19const FIXTURE = {
20 object: 'list',
21 data: [
22 {
23 id: 'alibaba/qwen-3-14b',
24 name: 'Qwen3-14B',
25 type: 'language',
26 context_window: 40960,
27 max_tokens: 16384,
28 modalities: { input: ['text'], output: ['text'] },
29 reasoning_options: [{ type: 'toggle' }],
30 supported_parameters: [
31 'max_tokens',
32 'temperature',
33 'tools',
34 'reasoning',
35 'not_a_flag',
36 ],
37 pricing: {
38 input: '0.00000012',
39 output: '0.00000024',
40 input_cache_read: '0.00000006',
41 input_cache_write: '0.00000015',
42 fast: { input: '0.000009', output: '0.000009' },
43 service_tiers: {
44 flex: { input: '0.00000001', output: '0.00000001' },
45 },
46 },
47 released: 1745798400,
48 },
49 {
50 id: 'alibaba/qwen-3-235b',
51 name: 'Qwen3-235B',
52 type: 'language',
53 modalities: { input: ['text'], output: ['text'] },
54 reasoning_options: [
55 { type: 'toggle' },
56 { type: 'effort', values: ['none', 'low', 'medium', 'high'] },
57 ],
58 supported_parameters: ['max_tokens', 'stop'],
59 pricing: { input: '0.00000018' },
60 },
61 {
62 id: 'google/imagen',
63 type: 'image',
64 modalities: { input: ['text'], output: ['image'] },
65 },
66 {
67 id: 'inclusionai/ling-3.1-flash',
68 type: 'language',
69 supported_parameters: ['max_tokens'],
70 pricing: { input: '0', output: '0' },
71 },
72 ],
74​
75/** OpenAI's document, trimmed: Vercel's compat routes are generated from it. */
76const OPENAI_SPEC = {
77 openapi: '3.1.0',
78 info: { title: 'OpenAI' },
79 paths: {
80 '/chat/completions': { post: { summary: 'chat' } },
81 '/embeddings': { post: { summary: 'embed' } },
82 '/batches': { post: { summary: 'batch' } },
83 },
85​
86const SOURCE: RateCard['source'] = {
87 url: VERCEL_MODELS_URL,
88 hash: 'abc',
89 extractedAt: '2026-10-08T00:00:00.000Z',
91​
92const originalFetch = globalThis.fetch
93​
94afterEach(() => {
95 globalThis.fetch = originalFetch
96})
97​
98describe('vercel', () => {
99 it('lists gateway models from the Vercel payload and generates the compat spec', async () => {
100 const urls: Array<string> = []
101 globalThis.fetch = ((url: string) => {
102 urls.push(String(url))
103 if (String(url) === VERCEL_MODELS_URL) {
104 return Promise.resolve(new Response(JSON.stringify(FIXTURE)))
105 }
106 if (String(url) === OPENAI_SPEC_URL) {
107 return Promise.resolve(new Response(JSON.stringify(OPENAI_SPEC)))
108 }
109 return Promise.reject(new Error(`unexpected fetch: ${String(url)}`))
110 }) as typeof fetch
111​
112 const listed = await provider.listModels({})
113 const spec = await provider.fetchSpec({})
114​
115 expect(listed.skipped).toBeUndefined()
116 expect(listed.models.map((model) => model.rawId)).toEqual([
117 'alibaba/qwen-3-14b',
118 'alibaba/qwen-3-235b',
119 'google/imagen',
120 'inclusionai/ling-3.1-flash',
121 ])
122 expect(listed.models[0]).toMatchObject({
123 displayName: 'Qwen3-14B',
124 activity: 'chat',
125 contextWindow: 40960,
126 maxOutput: 16384,
127 modalities: { input: ['text'], output: ['text'] },
128 capabilities: ['max_tokens', 'temperature', 'tools', 'reasoning'],
129 exactCapabilities: true,
130 // A toggle and nothing else: "an on/off control".
131 reasoning: { mode: 'toggle', mandatory: false },
132 requestMap: {
133 thinking: {
134 on: { reasoning: { enabled: true } },
135 off: { reasoning: { enabled: false } },
136 levels: null,
137 },
138 maxTokensField: 'max_tokens',
139 developerRole: null,
140 reasoningEffort: null,
141 },
142 factSources: {
143 pricing: {
144 derivation: 'listing',
145 sourceUrl: VERCEL_MODELS_URL,
146 path: 'pricing',
147 },
148 capabilities: {
149 tools: {
150 derivation: 'listing',
151 sourceUrl: VERCEL_MODELS_URL,
152 path: 'supported_parameters.tools',
153 },
154 },
155 reasoning: {
156 derivation: 'listing',
157 sourceUrl: VERCEL_MODELS_URL,
158 sourceHash: expect.stringMatching(/^[0-9a-f]{64}$/) as string,
159 path: 'reasoning.enabled',
160 },
161 },
162 releasedAt: 1745798400,
163 })
164 expect(listed.models[0]?.pricing).toMatchObject({
165 tables: {
166 rate: {
167 base: {
168 input_tokens: 0.00000012,
169 output_tokens: 0.00000024,
170 cache_read_tokens: 0.00000006,
171 cache_write_tokens: 0.00000015,
172 },
173 },
174 },
175 })
176 // fast and flex are not the standard rate.
177 const base = (
178 listed.models[0]!.pricing as {
179 tables: { rate: { base: Record<string, number> } }
180 }
181 ).tables.rate.base
182 expect(base.input_tokens).toBe(0.00000012)
183 // An effort row's thinking body is the level list, not a control path.
184 expect(listed.models[1]?.factSources).not.toHaveProperty('reasoning')
185 expect(listed.models[1]).toMatchObject({
186 pricing: null,
187 capabilities: ['max_tokens', 'stop'],
188 exactCapabilities: true,
189 reasoning: {
190 mode: 'effort',
191 mandatory: false,
192 efforts: ['none', 'low', 'medium', 'high'],
193 },
194 requestMap: {
195 maxTokensField: 'max_tokens',
196 thinking: {
197 on: { reasoning: { effort: 'high' } },
198 off: { reasoning: { effort: 'none' } },
199 levels: {
200 off: 'none',
201 minimal: null,
202 low: 'low',
203 medium: 'medium',
204 high: 'high',
205 xhigh: null,
206 max: null,
207 },
208 },
209 },
210 })
211 expect(listed.models[2]).toMatchObject({
212 activity: 'image',
213 pricing: null,
214 })
215 expect(listed.models[2]).not.toHaveProperty('factSources')
216 expect(listed.models[2]).not.toHaveProperty('requestMap')
217 expect(listed.models[2]).not.toHaveProperty('capabilities')
218 // A published zero is free, not a card.
219 expect(listed.models[3]?.pricing).toBeNull()
220 expect(listed.models[3]?.requestMap).toMatchObject({
221 maxTokensField: 'max_tokens',
222 thinking: null,
223 })
224 expect(spec.skipped).toBeUndefined()
225 expect(Object.keys(spec.specs[0]?.paths ?? {})).toEqual([
226 '/chat/completions',
227 '/embeddings',
228 ])
229 expect(spec.specs[0]?.servers).toEqual([{ url: VERCEL_SERVER_URL }])
230 expect(urls).toEqual([VERCEL_MODELS_URL, OPENAI_SPEC_URL])
231 })
232​
233 it('re-quotes the standard schedule above an inclusive context min', () => {
234 const [model] = parseVercelModels(
235 {
236 data: [
237 {
238 id: 'alibaba/qwen-3.6-max-preview',
239 type: 'language',
240 pricing: {
241 input: '0.000001',
242 output: '0.000002',
243 input_cache_read: '0.0000001',
244 input_tiers: [
245 { cost: '0.000001', min: 0, max: 128000 },
246 { cost: '0.000002', min: 128000 },
247 ],
248 output_tiers: [
249 { cost: '0.000002', max: 128000 },
250 { cost: '0.000004', min: 128000 },
251 ],
252 input_cache_read_tiers: [
253 { cost: '0.0000001', min: 0, max: 128000 },
254 { cost: '0.0000002', min: 128000 },
255 ],
256 regional: { eu: { input: '0.000009', output: '0.000009' } },
257 },
258 },
259 ],
260 },
261 SOURCE,
262 )
263 const card = model?.pricing as RateCard
264 expect(
265 price(card, {}, { input_tokens: 127999, output_tokens: 0 }),
266 ).toBeCloseTo(127999 * 0.000001)
267 expect(
268 price(card, {}, { input_tokens: 128000, output_tokens: 0 }),
269 ).toBeCloseTo(128000 * 0.000002)
270 expect(
271 price(
272 card,
273 {},
274 { input_tokens: 0, output_tokens: 0, cache_read_tokens: 128000 },
275 ),
276 ).toBeCloseTo(128000 * 0.0000002)
277 })
278​
279 it('keeps the base cache rate below a tier that omits min 0', () => {
280 const [model] = parseVercelModels(
281 {
282 data: [
283 {
284 id: 'spacexai/grok-4.20-non-reasoning-beta',
285 type: 'language',
286 pricing: {
287 input: '0.000002',
288 output: '0.000006',
289 input_cache_read: '0.0000002',
290 input_cache_read_tiers: [{ cost: '0.0000004', min: 200001 }],
291 },
292 },
293 ],
294 },
295 SOURCE,
296 )
297 const card = model?.pricing as RateCard
298 const usage = (cache_read_tokens: number) => ({
299 input_tokens: 0,
300 output_tokens: 0,
301 cache_read_tokens,
302 })
303 expect(price(card, {}, usage(200000))).toBeCloseTo(200000 * 0.0000002)
304 expect(price(card, {}, usage(200001))).toBeCloseTo(200001 * 0.0000004)
305 })
306​
307 it('uses the toggle when an effort list has no high', () => {
308 const [model] = parseVercelModels(
309 {
310 data: [
311 {
312 id: 'alibaba/qwen3.8-max',
313 type: 'language',
314 supported_parameters: ['max_completion_tokens'],
315 reasoning_options: [
316 { type: 'toggle' },
317 { type: 'effort', values: ['low', 'medium', 'xhigh'] },
318 ],
319 },
320 ],
321 },
322 SOURCE,
323 )
324 expect(model?.reasoning).toEqual({
325 mode: 'effort',
326 mandatory: false,
327 efforts: ['low', 'medium', 'xhigh'],
328 })
329 expect(model?.requestMap).toMatchObject({
330 maxTokensField: 'max_completion_tokens',
331 thinking: {
332 on: { reasoning: { enabled: true } },
333 off: { reasoning: { enabled: false } },
334 levels: null,
335 },
336 })
337 })
338​
339 it('stores no thinking body for a budget with no high token count', () => {
340 const [model] = parseVercelModels(
341 {
342 data: [
343 {
344 id: 'alibaba/qwen3-235b-a22b-thinking',
345 type: 'language',
346 supported_parameters: ['max_tokens'],
347 reasoning_options: [{ type: 'budget_tokens', min: 1, max: 81920 }],
348 },
349 ],
350 },
351 SOURCE,
352 )
353 expect(model?.reasoning).toEqual({ mode: 'budget', mandatory: null })
354 expect(model?.requestMap).toMatchObject({
355 maxTokensField: 'max_tokens',
356 thinking: null,
357 })
358 expect(model?.factSources?.reasoning).toMatchObject({
359 path: 'reasoning.max_tokens',
360 sourceUrl: VERCEL_MODELS_URL,
361 })
362 })
363​
364 it('throws when a tier schedule cannot be read', () => {
365 const row = (pricing: unknown) => ({
366 data: [{ id: 'openai/gpt-6-astra', type: 'language', pricing }],
367 })
368 expect(() =>
369 parseVercelModels(row({ input: '0.00001', input_tiers: 'nope' }), SOURCE),
370 ).toThrow(/input_tiers is not an array/)
371 expect(() =>
372 parseVercelModels(
373 row({
374 input: '0.00001',
375 output: '0.00002',
376 input_tiers: [{ cost: '0.00002', min: 0 }],
377 }),
378 SOURCE,
379 ),
380 ).toThrow(/disagrees with its first tier/)
381 expect(() =>
382 parseVercelModels(
383 row({
384 input: '0.00001',
385 output: '0.00002',
386 input_tiers: [
387 { cost: '0.00001', min: 0, max: 100 },
388 { cost: '0.00002', min: 200 },
389 ],
390 }),
391 SOURCE,
392 ),
393 ).toThrow(/ranges do not meet/)
394 expect(() =>
395 parseVercelModels(
396 {
397 data: [
398 {
399 id: 'openai/gpt-6-astra',
400 type: 'language',
401 supported_parameters: 'max_tokens',
402 },
403 ],
404 },
405 SOURCE,
406 ),
407 ).toThrow(/supported_parameters is not a string array/)
408 })
409​
410 it('reads each control the row lists, and nothing it does not know', () => {
411 const options = (...reasoning_options: Array<unknown>) =>
412 vercelReasoning({ reasoning_options })
413 // anthropic/claude-sonnet-4.5: a switch and a budget.
414 expect(
415 options({ type: 'toggle' }, { type: 'budget_tokens', min: 1024 }),
416 ).toEqual({ mode: 'budget', mandatory: false })
417 // minimax/minimax-m3: a budget alone says nothing about turning it off.
418 expect(options({ type: 'budget_tokens' })).toEqual({
419 mode: 'budget',
420 mandatory: null,
421 })
422 // An effort entry keeps its reading whatever sits beside it.
423 expect(
424 options(
425 { type: 'toggle' },
426 { type: 'effort', values: ['low', 'medium', 'xhigh'] },
427 { type: 'budget_tokens' },
428 ),
429 ).toEqual({
430 mode: 'effort',
431 mandatory: false,
432 efforts: ['low', 'medium', 'xhigh'],
433 })
434 // A control type this does not know, an effort entry without values,
435 // or no controls: nothing is stored.
436 expect(options({ type: 'toggle' }, { type: 'auto' })).toBeNull()
437 expect(options({ type: 'switch' })).toBeNull()
438 expect(options({ type: 'toggle' }, { type: 'effort' })).toBeNull()
439 expect(
440 options({ type: 'toggle' }, { type: 'effort', values: [] }),
441 ).toBeNull()
442 expect(options()).toBeNull()
443 expect(vercelReasoning({})).toBeNull()
444 expect(vercelReasoning({ reasoning_options: 'toggle' })).toBeNull()
445 })
446​
447 it('clears the price of a row whose price varies by route (#346)', () => {
448 // ai-gateway.vercel.sh/v1/models, 2026-10-22: the top-level price is
449 // one route's (beam: the free `reflection` route; qwen-3-235b: vertex,
450 // not the cheaper deepinfra), and the gateway picks the route.
451 const [beam, qwen, plain] = parseVercelModels(
452 {
453 data: [
454 {
455 id: 'reflection/beam',
456 type: 'language',
457 pricing: {
458 input: '0',
459 output: '0',
460 input_cache_read: '0',
461 varies_by_provider: true,
462 },
463 },
464 {
465 id: 'alibaba/qwen-3-235b',
466 type: 'language',
467 pricing: {
468 input: '0.00000022',
469 output: '0.00000088',
470 varies_by_provider: true,
471 },
472 },
473 {
474 id: 'alibaba/qwen-3-14b',
475 type: 'language',
476 pricing: { input: '0.00000012', output: '0.00000024' },
477 },
478 ],
479 },
480 SOURCE,
481 )
482 for (const row of [beam, qwen]) {
483 expect(row?.pricing).toBeNull()
484 expect(row?.absent).toEqual({ pricing: 'cleared' })
485 expect(row?.factSources?.pricing).toBeUndefined()
486 }
487 expect(plain?.pricing).not.toBeNull()
488 expect(plain?.absent).toBeUndefined()
489 })
490})