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 native catalog instead of reporting no new models', async () => {
31 const client = clientReturning((provider) =>
32 json({ models: provider === 'groq' ? [] : [row('x')] }),
33 )
34 await expect(fetchSyncCatalogs(client)).rejects.toThrow(
35 'empty groq catalog',
36 )
37 })
38
39 it('fails when one native provider errors', async () => {
40 const client = clientReturning((provider) =>
41 provider === 'mistral'
42 ? json({ error: { message: 'mistral down' } }, 500)
43 : json({ models: [row('x')] }),
44 )
45 await expect(fetchSyncCatalogs(client)).rejects.toThrow('mistral down')
46 })
47
48 it('fails on a payload with no models array', async () => {
49 const client = clientReturning(() => json({ data: [] }))
50 // From client 0.2, `listModels` maps `models` to brand the ids, so it
51 // throws before `parseCatalogModels` can name the missing array.
52 await expect(fetchSyncCatalogs(client)).rejects.toThrow()
53 })
54
55 it('returns each native catalog, and does not fetch openrouter', async () => {
56 const asked: Array<string> = []
57 const client = clientReturning((provider) => {
58 asked.push(provider)
59 return json({ models: [row(`${provider}-model`)] })
60 })
61 const catalogs = await fetchSyncCatalogs(client)
62 expect(catalogs.mistral[0]?.rawId).toBe('mistral-model')
63 expect(asked).not.toContain('openrouter')
64 })
65})