|
| 1 | +import { env } from 'cloudflare:test'; |
| 2 | +import { describe, it, expect, beforeEach } from 'vitest'; |
| 3 | +import { ModelCatalog, type FetchFn } from '../src/models'; |
| 4 | + |
| 5 | +const modelsDevResponse = { |
| 6 | + anthropic: { |
| 7 | + id: 'anthropic', |
| 8 | + npm: '@ai-sdk/anthropic', |
| 9 | + name: 'Anthropic', |
| 10 | + models: { |
| 11 | + 'claude-sonnet-4-5': { |
| 12 | + id: 'claude-sonnet-4-5', |
| 13 | + name: 'Claude Sonnet 4.5', |
| 14 | + limit: { context: 200000, output: 64000 }, |
| 15 | + }, |
| 16 | + }, |
| 17 | + }, |
| 18 | + openai: { |
| 19 | + id: 'openai', |
| 20 | + npm: '@ai-sdk/openai', |
| 21 | + name: 'OpenAI', |
| 22 | + models: { |
| 23 | + 'gpt-4o': { |
| 24 | + id: 'gpt-4o', |
| 25 | + name: 'GPT-4o', |
| 26 | + limit: { context: 128000, output: 16384 }, |
| 27 | + }, |
| 28 | + }, |
| 29 | + }, |
| 30 | + groq: { |
| 31 | + id: 'groq', |
| 32 | + npm: '@ai-sdk/groq', |
| 33 | + name: 'Groq', |
| 34 | + models: { 'llama-3': { id: 'llama-3', name: 'Llama 3' } }, |
| 35 | + }, |
| 36 | +}; |
| 37 | + |
| 38 | +function fakeFetch(status: number, body: unknown): FetchFn { |
| 39 | + return async () => new Response(JSON.stringify(body), { status }); |
| 40 | +} |
| 41 | + |
| 42 | +describe('ModelCatalog', () => { |
| 43 | + beforeEach(async () => { |
| 44 | + await env.MODELS.delete('models:anthropic'); |
| 45 | + await env.MODELS.delete('models:openai'); |
| 46 | + }); |
| 47 | + |
| 48 | + describe('refresh', () => { |
| 49 | + it('fetches models from models.dev and stores them in KV', async () => { |
| 50 | + const catalog = new ModelCatalog(env.MODELS, fakeFetch(200, modelsDevResponse)); |
| 51 | + |
| 52 | + await catalog.refresh(); |
| 53 | + |
| 54 | + const anthropicRaw = await env.MODELS.get('models:anthropic'); |
| 55 | + const openaiRaw = await env.MODELS.get('models:openai'); |
| 56 | + expect(JSON.parse(anthropicRaw!)).toEqual(modelsDevResponse.anthropic.models); |
| 57 | + expect(JSON.parse(openaiRaw!)).toEqual(modelsDevResponse.openai.models); |
| 58 | + }); |
| 59 | + |
| 60 | + it('does not store models for unsupported providers', async () => { |
| 61 | + const catalog = new ModelCatalog(env.MODELS, fakeFetch(200, modelsDevResponse)); |
| 62 | + |
| 63 | + await catalog.refresh(); |
| 64 | + |
| 65 | + const groqRaw = await env.MODELS.get('models:groq'); |
| 66 | + expect(groqRaw).toBeNull(); |
| 67 | + }); |
| 68 | + |
| 69 | + it('throws on non-200 responses', async () => { |
| 70 | + const catalog = new ModelCatalog(env.MODELS, fakeFetch(500, { error: 'server error' })); |
| 71 | + |
| 72 | + await expect(catalog.refresh()).rejects.toThrow('models.dev returned 500'); |
| 73 | + }); |
| 74 | + |
| 75 | + it('handles a provider missing from the response', async () => { |
| 76 | + const partial = { anthropic: modelsDevResponse.anthropic }; |
| 77 | + const catalog = new ModelCatalog(env.MODELS, fakeFetch(200, partial)); |
| 78 | + |
| 79 | + await catalog.refresh(); |
| 80 | + |
| 81 | + const anthropicRaw = await env.MODELS.get('models:anthropic'); |
| 82 | + const openaiRaw = await env.MODELS.get('models:openai'); |
| 83 | + expect(JSON.parse(anthropicRaw!)).toEqual(modelsDevResponse.anthropic.models); |
| 84 | + expect(openaiRaw).toBeNull(); |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('getModels', () => { |
| 89 | + it('returns models from KV', async () => { |
| 90 | + await env.MODELS.put('models:anthropic', JSON.stringify(modelsDevResponse.anthropic.models)); |
| 91 | + const catalog = new ModelCatalog(env.MODELS); |
| 92 | + |
| 93 | + const models = await catalog.getModels('anthropic'); |
| 94 | + |
| 95 | + expect(models).toEqual(modelsDevResponse.anthropic.models); |
| 96 | + }); |
| 97 | + |
| 98 | + it('returns an empty object when KV has no data', async () => { |
| 99 | + const catalog = new ModelCatalog(env.MODELS); |
| 100 | + |
| 101 | + const models = await catalog.getModels('anthropic'); |
| 102 | + |
| 103 | + expect(models).toEqual({}); |
| 104 | + }); |
| 105 | + }); |
| 106 | +}); |
0 commit comments