Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import { createSyncClient, fetchSyncCatalogs } from './modelschemas'
3
4function clientReturning(respond: (provider: string) => Response) {
5 return createSyncClient({
6 apiKey: '',
7 fetch: async (input) => {
8 const url = new URL(input instanceof Request ? input.url : String(input))
9 return respond(url.searchParams.get('provider') ?? '')
10 },
11 })
12}
13
14const row = (rawId: string) => ({ rawId, firstSeenAt: 1, deprecatedAt: null })
15
16const json = (body: unknown, status = 200) =>
17 new Response(JSON.stringify(body), {
18 status,
19 headers: { 'Content-Type': 'application/json' },
20 })
21
22describe('fetchSyncCatalogs', () => {
23 it('fails when modelschemas is down', async () => {
24 const client = clientReturning(() =>
25 json({ error: { message: 'unavailable' } }, 503),
26 )
27 await expect(fetchSyncCatalogs(client)).rejects.toThrow('unavailable')
28 })
29
30 it('fails on an empty openrouter catalog instead of dropping prices', async () => {
31 const client = clientReturning((provider) =>
32 json({ models: provider === 'openrouter' ? [] : [row('x')] }),
33 )
34 await expect(fetchSyncCatalogs(client)).rejects.toThrow(
35 'empty openrouter catalog',
36 )
37 })
38
39 it('fails on an empty native catalog instead of reporting no new models', async () => {
40 const client = clientReturning((provider) =>
41 json({ models: provider === 'groq' ? [] : [row('x')] }),
42 )
43 await expect(fetchSyncCatalogs(client)).rejects.toThrow(
44 'empty groq catalog',
45 )
46 })
47
48 it('fails when one native provider errors', async () => {
49 const client = clientReturning((provider) =>
50 provider === 'mistral'
51 ? json({ error: { message: 'mistral down' } }, 500)
52 : json({ models: [row('x')] }),
53 )
54 await expect(fetchSyncCatalogs(client)).rejects.toThrow('mistral down')
55 })
56
57 it('fails on a payload with no models array', async () => {
58 const client = clientReturning(() => json({ data: [] }))
59 // From client 0.2, `listModels` maps `models` to brand the ids, so it
60 // throws before `parseCatalogModels` can name the missing array.
61 await expect(fetchSyncCatalogs(client)).rejects.toThrow()
62 })
63
64 it('returns each native catalog next to openrouter', async () => {
65 const client = clientReturning((provider) =>
66 json({ models: [row(`${provider}-model`)] }),
67 )
68 const catalogs = await fetchSyncCatalogs(client)
69 expect(catalogs.openrouter[0]?.rawId).toBe('openrouter-model')
70 expect(catalogs.native.mistral[0]?.rawId).toBe('mistral-model')
71 })
72})