Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2import {
3 alreadySynced,
4 findOpenRouterEnrichment,
5 hasFullPricing,
6 hasImageOutput,
7 openRouterRawIdCandidates,
8 outputsText,
9 parseCatalogModels,
10 pricingLines,
11 selectNativeInserts,
12 skipNativeModelReason,
13 toSyncModel,
14} from './catalog'
15import type { CatalogModel } from './catalog'
16
17function native(overrides: Partial<CatalogModel> = {}): CatalogModel {
18 return {
19 provider: 'anthropic',
20 rawId: 'claude-sonnet-5',
21 activity: 'chat',
22 firstSeenAt: 1_780_000_000,
23 deprecatedAt: null,
24 contextWindow: null,
25 maxOutput: null,
26 inputModalities: [],
27 outputModalities: [],
28 pricing: {
29 inputPerMillion: undefined,
30 outputPerMillion: undefined,
31 },
32 capabilities: [],
33 reasoningMandatory: false,
34 ...overrides,
35 }
36}
37
38describe('parseCatalogModels', () => {
39 it('reads modelschemas listModels payloads and drops rows without rawId', () => {
40 const models = parseCatalogModels({
41 count: 2,
42 models: [
43 {
44 provider: 'openai',
45 rawId: 'gpt-5',
46 activity: 'chat',
47 contextWindow: 400_000,
48 maxOutput: 128_000,
49 modalities: { input: ['text', 'image'], output: ['text'] },
50 pricing: {
51 per: 'token',
52 inputPerMillion: 1.25,
53 outputPerMillion: 10,
54 },
55 capabilities: ['tools', 'reasoning'],
56 firstSeenAt: 1_754_425_777,
57 deprecatedAt: null,
58 },
59 { provider: 'openai', activity: 'chat' },
60 ],
61 })
62 expect(models).toHaveLength(1)
63 expect(models[0]).toMatchObject({
64 rawId: 'gpt-5',
65 contextWindow: 400_000,
66 inputModalities: ['text', 'image'],
67 capabilities: ['tools', 'reasoning'],
68 pricing: { inputPerMillion: 1.25, outputPerMillion: 10 },
69 })
70 })
71
72 it('maps BytePlus object capabilities onto supported_parameters', () => {
73 const models = parseCatalogModels({
74 models: [
75 {
76 rawId: 'deepseek-v4-pro-ga-260813',
77 activity: 'chat',
78 firstSeenAt: 1_790_000_000,
79 deprecatedAt: null,
80 capabilities: {
81 tools: { function_calling: true },
82 structured_outputs: { json_schema: false, json_object: false },
83 maxReasoningTokens: 393216,
84 },
85 },
86 ],
87 })
88 expect(models[0]?.capabilities).toEqual([
89 'tools',
90 'tool_choice',
91 'reasoning',
92 'include_reasoning',
93 ])
94 })
95})
96
97describe('parseCatalogModels drift', () => {
98 const row = { rawId: 'm', firstSeenAt: 1, deprecatedAt: null }
99
100 it('reads reasoning.mandatory', () => {
101 const [model] = parseCatalogModels({
102 models: [{ ...row, reasoning: { mode: 'adaptive', mandatory: true } }],
103 })
104 expect(model?.reasoningMandatory).toBe(true)
105 })
106
107 it('throws when no row has a rawId', () => {
108 expect(() => parseCatalogModels({ models: [{ id: 'm' }] })).toThrow(
109 'none of 1 rows has a rawId',
110 )
111 })
112
113 it('throws when firstSeenAt is gone, so the age check cannot pass everything', () => {
114 expect(() =>
115 parseCatalogModels({ models: [{ rawId: 'm', deprecatedAt: null }] }),
116 ).toThrow('no row has a firstSeenAt')
117 })
118
119 it('throws when deprecatedAt is gone, so deprecated rows cannot pass', () => {
120 expect(() =>
121 parseCatalogModels({ models: [{ rawId: 'm', firstSeenAt: 1 }] }),
122 ).toThrow('no row has a deprecatedAt key')
123 })
124
125 it('accepts an empty models array', () => {
126 expect(parseCatalogModels({ models: [] })).toEqual([])
127 })
128})
129
130describe('openRouterRawIdCandidates', () => {
131 it('hyphen/dot-maps Anthropic ids and strips dated snapshots', () => {
132 expect(
133 openRouterRawIdCandidates(
134 native({ rawId: 'claude-haiku-4-5-20251001' }),
135 'anthropic',
136 ),
137 ).toEqual([
138 'anthropic/claude-haiku-4-5-20251001',
139 'anthropic/claude-haiku-4-5',
140 'anthropic/claude-haiku-4.5-20251001',
141 'anthropic/claude-haiku-4.5',
142 ])
143 })
144
145 it('prefixes Gemini with google/ and Grok with x-ai/', () => {
146 expect(
147 openRouterRawIdCandidates(
148 native({ rawId: 'gemini-3.1-pro', provider: 'gemini' }),
149 'gemini',
150 ),
151 ).toEqual(['google/gemini-3.1-pro'])
152 expect(
153 openRouterRawIdCandidates(
154 native({ rawId: 'grok-4.6', provider: 'grok' }),
155 'grok',
156 ),
157 ).toEqual(['x-ai/grok-4.6'])
158 })
159})
160
161describe('findOpenRouterEnrichment / toSyncModel', () => {
162 it('joins a hyphenated Anthropic id to the dotted OpenRouter row', () => {
163 const enrich = native({
164 provider: 'openrouter',
165 rawId: 'anthropic/claude-sonnet-4.5',
166 contextWindow: 200_000,
167 maxOutput: 64_000,
168 inputModalities: ['text', 'image'],
169 outputModalities: ['text'],
170 pricing: { inputPerMillion: 3, outputPerMillion: 15 },
171 capabilities: ['tools', 'temperature'],
172 })
173 const row = native({ rawId: 'claude-sonnet-4-5' })
174 const found = findOpenRouterEnrichment(row, 'anthropic', [enrich])
175 expect(found?.rawId).toBe('anthropic/claude-sonnet-4.5')
176 const synced = toSyncModel(row, found, 'anthropic')
177 expect(synced.nativeId).toBe('claude-sonnet-4-5')
178 expect(synced.contextWindow).toBe(200_000)
179 expect(synced.supportedParameters).toEqual(['tools', 'temperature'])
180 expect(synced.pricing.inputPerMillion).toBe(3)
181 })
182
183 it('keeps native capabilities and modalities; OpenRouter only fills empty pricing', () => {
184 const enrich = native({
185 provider: 'openrouter',
186 rawId: 'anthropic/claude-sonnet-4.5',
187 contextWindow: 1_000_000,
188 inputModalities: ['text', 'image', 'file'],
189 outputModalities: ['text'],
190 pricing: { inputPerMillion: 3, outputPerMillion: 15 },
191 capabilities: ['tools', 'temperature', 'include_reasoning', 'top_k'],
192 })
193 const row = native({
194 rawId: 'claude-sonnet-4-5',
195 contextWindow: 200_000,
196 inputModalities: ['text', 'image'],
197 outputModalities: ['text'],
198 capabilities: ['tools', 'temperature'],
199 })
200 const synced = toSyncModel(row, enrich, 'anthropic')
201 expect(synced.contextWindow).toBe(200_000)
202 expect(synced.inputModalities).toEqual(['text', 'image'])
203 expect(synced.supportedParameters).toEqual(['tools', 'temperature'])
204 expect(synced.pricing.inputPerMillion).toBe(3)
205 })
206
207 it('fills a missing price side from OpenRouter without replacing the known one', () => {
208 const synced = toSyncModel(
209 native({ pricing: { inputPerMillion: 2, outputPerMillion: undefined } }),
210 native({ pricing: { inputPerMillion: 3, outputPerMillion: 15 } }),
211 'anthropic',
212 )
213 expect(synced.pricing).toEqual({ inputPerMillion: 2, outputPerMillion: 15 })
214 })
215})
216
217describe('skipNativeModelReason', () => {
218 const cutoff = 1_700_000_000
219
220 it('skips dated Anthropic snapshots, non-chat activity, and old rows', () => {
221 expect(
222 skipNativeModelReason(
223 native({ rawId: 'claude-haiku-4-5-20251001' }),
224 'anthropic',
225 [],
226 cutoff,
227 ['chat'],
228 ),
229 ).toBe('dated snapshot')
230 expect(
231 skipNativeModelReason(
232 native({ activity: 'image', rawId: 'grok-imagine-image' }),
233 'grok',
234 [],
235 cutoff,
236 ['chat'],
237 ),
238 ).toBe('activity image')
239 expect(
240 skipNativeModelReason(
241 native({ firstSeenAt: cutoff - 1 }),
242 'anthropic',
243 [],
244 cutoff,
245 ['chat'],
246 ),
247 ).toBe('too old')
248 })
249
250 it('keeps a current chat model', () => {
251 expect(
252 skipNativeModelReason(native(), 'anthropic', [], cutoff, ['chat']),
253 ).toBeNull()
254 })
255
256 it('treats transcribe ids as non-chat even when activity is chat', () => {
257 expect(
258 skipNativeModelReason(
259 native({ rawId: 'gemini-3.5-transcribe', provider: 'gemini' }),
260 'gemini',
261 [],
262 cutoff,
263 ['chat'],
264 ),
265 ).toBe('non-chat family')
266 })
267
268 it('keeps Groq rows with no activity', () => {
269 expect(
270 skipNativeModelReason(
271 native({ activity: null, rawId: 'qwen/qwen3.8-27b' }),
272 'groq',
273 [],
274 cutoff,
275 [null],
276 ),
277 ).toBeNull()
278 })
279})
280
281describe('outputsText', () => {
282 it('treats empty modalities plus unset activity as chat', () => {
283 const model = toSyncModel(
284 native({ activity: null, rawId: 'qwen/qwen3.8-27b' }),
285 undefined,
286 'groq',
287 )
288 expect(outputsText(model, null)).toBe(true)
289 })
290})
291
292describe('selectNativeInserts', () => {
293 const priced = { inputPerMillion: 3, outputPerMillion: 15 }
294 const rules = {
295 provider: 'anthropic' as const,
296 skipPatterns: ['claude-legacy-'],
297 acceptedActivities: ['chat'],
298 requireOpenRouterEnrich: true,
299 cutoffTimestamp: 0,
300 }
301 const openrouter = [
302 native({ rawId: 'anthropic/claude-sonnet-6', pricing: priced }),
303 native({ rawId: 'anthropic/claude-art-1', pricing: priced }),
304 ]
305
306 it('inserts a priced model and holds back one OpenRouter has not priced', () => {
307 const selection = selectNativeInserts(
308 [native({ rawId: 'claude-sonnet-6' }), native({ rawId: 'claude-new-1' })],
309 rules,
310 openrouter,
311 () => false,
312 )
313 expect(selection.inserts.map(({ model }) => model.nativeId)).toEqual([
314 'claude-sonnet-6',
315 ])
316 expect(selection.inserts[0]?.model.pricing).toEqual(priced)
317 expect(selection.heldBack).toEqual(['claude-new-1'])
318 })
319
320 it('does not hold back a model that is already in the file', () => {
321 const selection = selectNativeInserts(
322 [native({ rawId: 'claude-new-1' })],
323 rules,
324 openrouter,
325 () => true,
326 )
327 expect(selection.heldBack).toEqual([])
328 expect(selection.skipped).toEqual({ 'already synced': 1 })
329 })
330
331 it('counts every other skip by reason', () => {
332 const selection = selectNativeInserts(
333 [
334 native({ rawId: 'claude-art-1', outputModalities: ['image'] }),
335 native({ rawId: 'claude-old-1', deprecatedAt: 1 }),
336 native({ rawId: 'claude-sonnet-6:thinking' }),
337 native({ rawId: 'claude-legacy-2' }),
338 ],
339 rules,
340 openrouter,
341 () => false,
342 )
343 expect(selection.inserts).toEqual([])
344 expect(selection.skipped).toEqual({
345 'image output': 1,
346 deprecated: 1,
347 'routing variant': 1,
348 'skip pattern': 1,
349 })
350 })
351
352 it('inserts a Groq model with no price when the price gate is off', () => {
353 const selection = selectNativeInserts(
354 [native({ provider: 'groq', rawId: 'qwen/qwen9', activity: null })],
355 {
356 ...rules,
357 provider: 'groq',
358 acceptedActivities: [null, 'chat'],
359 requireOpenRouterEnrich: false,
360 },
361 openrouter,
362 () => false,
363 )
364 expect(selection.inserts).toHaveLength(1)
365 expect(selection.heldBack).toEqual([])
366 })
367})
368
369describe('pricingLines / hasFullPricing', () => {
370 it('leaves an unknown price out instead of writing 0', () => {
371 const lines = pricingLines({
372 inputPerMillion: undefined,
373 outputPerMillion: undefined,
374 })
375 expect(lines).toEqual([' pricing: {', ' },'])
376 expect(lines.join('\n')).not.toContain('normal: 0')
377 })
378
379 it('writes only the known side, rounded', () => {
380 const block = pricingLines({
381 inputPerMillion: 0.09999999999999999,
382 outputPerMillion: undefined,
383 }).join('\n')
384 expect(block).toContain('normal: 0.1,')
385 expect(block).not.toContain('output')
386 })
387
388 it('needs both prices for providers whose ModelMeta requires them', () => {
389 expect(
390 hasFullPricing({ inputPerMillion: 1, outputPerMillion: undefined }),
391 ).toBe(false)
392 expect(hasFullPricing({ inputPerMillion: 0, outputPerMillion: 0 })).toBe(
393 true,
394 )
395 })
396})
397
398describe('hasImageOutput / alreadySynced', () => {
399 it('treats image activity and image output as image models', () => {
400 expect(hasImageOutput(native({ activity: 'image' }))).toBe(true)
401 expect(
402 hasImageOutput(native({ outputModalities: ['image', 'text'] })),
403 ).toBe(true)
404 expect(hasImageOutput(native({ outputModalities: ['text'] }))).toBe(false)
405 })
406
407 it('matches existing ids with dots or dashes', () => {
408 expect(
409 alreadySynced(
410 'claude-sonnet-4.5',
411 new Set(['claude-sonnet-4-5']),
412 new Set(),
413 ),
414 ).toBe(true)
415 expect(
416 alreadySynced('gpt-5.6-luna', new Set(), new Set(['GPT_5_6_LUNA'])),
417 ).toBe(true)
418 expect(alreadySynced('gpt-5.6-luna', new Set(), new Set())).toBe(false)
419 })
420})