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

catalog.test.ts

303 lines7.9 KB

1import { describe, expect, it } from 'vitest'
2import {
3 alreadySynced,
4 hasImageOutput,
5 outputsText,
6 parseCatalogModels,
7 selectNativeInserts,
8 skipNativeModelReason,
9 toSyncModel,
10} from './catalog'
11import type { CatalogModel } from './catalog'
12​
13function native(overrides: Partial<CatalogModel> = {}): CatalogModel {
14 return {
15 provider: 'anthropic',
16 rawId: 'claude-sonnet-5',
17 activity: 'chat',
18 firstSeenAt: 1_780_000_000,
19 deprecatedAt: null,
20 contextWindow: null,
21 maxOutput: null,
22 inputModalities: ['text'],
23 outputModalities: ['text'],
24 capabilities: [],
25 reasoningMandatory: false,
26 ...overrides,
27 }
29​
30describe('parseCatalogModels', () => {
31 it('reads modelschemas listModels payloads and drops rows without rawId', () => {
32 const models = parseCatalogModels({
33 count: 2,
34 models: [
35 {
36 provider: 'openai',
37 rawId: 'gpt-5',
38 activity: 'chat',
39 contextWindow: 400_000,
40 maxOutput: 128_000,
41 modalities: { input: ['text', 'image'], output: ['text'] },
42 capabilities: { tools: true, reasoning: true, temperature: false },
43 firstSeenAt: 1_754_425_777,
44 deprecatedAt: null,
45 },
46 { provider: 'openai', activity: 'chat' },
47 ],
48 })
49 expect(models).toHaveLength(1)
50 expect(models[0]).toMatchObject({
51 rawId: 'gpt-5',
52 contextWindow: 400_000,
53 inputModalities: ['text', 'image'],
54 capabilities: ['tools', 'reasoning'],
55 })
56 })
57​
58 it('maps BytePlus object capabilities onto supported_parameters', () => {
59 const models = parseCatalogModels({
60 models: [
61 {
62 rawId: 'deepseek-v4-pro-ga-260813',
63 activity: 'chat',
64 firstSeenAt: 1_790_000_000,
65 deprecatedAt: null,
66 capabilities: {
67 tools: { function_calling: true },
68 structured_outputs: { json_schema: false, json_object: false },
69 maxReasoningTokens: 393216,
70 },
71 },
72 ],
73 })
74 expect(models[0]?.capabilities).toEqual([
75 'tools',
76 'tool_choice',
77 'reasoning',
78 'include_reasoning',
79 ])
80 })
81})
82​
83describe('parseCatalogModels drift', () => {
84 const row = { rawId: 'm', firstSeenAt: 1, deprecatedAt: null }
85​
86 it('reads reasoning.mandatory', () => {
87 const [model] = parseCatalogModels({
88 models: [{ ...row, reasoning: { mode: 'adaptive', mandatory: true } }],
89 })
90 expect(model?.reasoningMandatory).toBe(true)
91 })
92​
93 it('throws when no row has a rawId', () => {
94 expect(() => parseCatalogModels({ models: [{ id: 'm' }] })).toThrow(
95 'none of 1 rows has a rawId',
96 )
97 })
98​
99 it('throws when firstSeenAt is gone, so the age check cannot pass everything', () => {
100 expect(() =>
101 parseCatalogModels({ models: [{ rawId: 'm', deprecatedAt: null }] }),
102 ).toThrow('no row has a firstSeenAt')
103 })
104​
105 it('throws when deprecatedAt is gone, so deprecated rows cannot pass', () => {
106 expect(() =>
107 parseCatalogModels({ models: [{ rawId: 'm', firstSeenAt: 1 }] }),
108 ).toThrow('no row has a deprecatedAt key')
109 })
110​
111 it('accepts an empty models array', () => {
112 expect(parseCatalogModels({ models: [] })).toEqual([])
113 })
114})
115​
116describe('toSyncModel', () => {
117 it('takes every field from the native row, and fills nothing', () => {
118 expect(
119 toSyncModel(
120 native({
121 rawId: 'grok-5',
122 provider: 'grok',
123 contextWindow: 2_000_000,
124 capabilities: ['tools'],
125 }),
126 'grok',
127 ),
128 ).toEqual({
129 nativeId: 'grok-5',
130 contextWindow: 2_000_000,
131 maxOutput: null,
132 inputModalities: ['text'],
133 outputModalities: ['text'],
134 supportedParameters: ['tools'],
135 reasoningMandatory: false,
136 })
137 })
138})
139​
140describe('skipNativeModelReason', () => {
141 const cutoff = 1_700_000_000
142​
143 it('skips dated Anthropic snapshots, non-chat activity, and old rows', () => {
144 expect(
145 skipNativeModelReason(
146 native({ rawId: 'claude-haiku-4-5-20251001' }),
147 'anthropic',
148 [],
149 cutoff,
150 ['chat'],
151 ),
152 ).toBe('dated snapshot')
153 expect(
154 skipNativeModelReason(
155 native({ activity: 'image', rawId: 'grok-imagine-image' }),
156 'grok',
157 [],
158 cutoff,
159 ['chat'],
160 ),
161 ).toBe('activity image')
162 expect(
163 skipNativeModelReason(
164 native({ firstSeenAt: cutoff - 1 }),
165 'anthropic',
166 [],
167 cutoff,
168 ['chat'],
169 ),
170 ).toBe('too old')
171 })
172​
173 it('keeps a current chat model', () => {
174 expect(
175 skipNativeModelReason(native(), 'anthropic', [], cutoff, ['chat']),
176 ).toBeNull()
177 })
178​
179 it('treats transcribe ids as non-chat even when activity is chat', () => {
180 expect(
181 skipNativeModelReason(
182 native({ rawId: 'gemini-3.5-transcribe', provider: 'gemini' }),
183 'gemini',
184 [],
185 cutoff,
186 ['chat'],
187 ),
188 ).toBe('non-chat family')
189 })
190​
191 it('skips a row with no input modalities instead of guessing them', () => {
192 expect(
193 skipNativeModelReason(
194 native({ inputModalities: [] }),
195 'gemini',
196 [],
197 cutoff,
198 ['chat'],
199 ),
200 ).toBe('no modalities')
201 })
202​
203 it('keeps Groq rows with no activity', () => {
204 expect(
205 skipNativeModelReason(
206 native({ activity: null, rawId: 'qwen/qwen3.8-27b' }),
207 'groq',
208 [],
209 cutoff,
210 [null],
211 ),
212 ).toBeNull()
213 })
214})
215​
216describe('outputsText', () => {
217 it('treats empty modalities plus unset activity as chat', () => {
218 const model = toSyncModel(
219 native({
220 activity: null,
221 rawId: 'qwen/qwen3.8-27b',
222 outputModalities: [],
223 }),
224 'groq',
225 )
226 expect(outputsText(model, null)).toBe(true)
227 })
228})
229​
230describe('selectNativeInserts', () => {
231 const rules = {
232 provider: 'anthropic' as const,
233 skipPatterns: ['claude-legacy-'],
234 acceptedActivities: ['chat'],
235 cutoffTimestamp: 0,
236 }
237​
238 it('inserts a new model with no price and no second source', () => {
239 const selection = selectNativeInserts(
240 [native({ rawId: 'claude-new-1' })],
241 rules,
242 () => false,
243 )
244 expect(selection.inserts.map(({ model }) => model.nativeId)).toEqual([
245 'claude-new-1',
246 ])
247 })
248​
249 it('skips a model that is already in the file', () => {
250 const selection = selectNativeInserts(
251 [native({ rawId: 'claude-new-1' })],
252 rules,
253 () => true,
254 )
255 expect(selection.skipped).toEqual({ 'already synced': 1 })
256 })
257​
258 it('counts every other skip by reason', () => {
259 const selection = selectNativeInserts(
260 [
261 native({ rawId: 'claude-art-1', outputModalities: ['image'] }),
262 native({ rawId: 'claude-old-1', deprecatedAt: 1 }),
263 native({ rawId: 'claude-sonnet-6:thinking' }),
264 native({ rawId: 'claude-legacy-2' }),
265 native({ rawId: 'claude-blank-1', inputModalities: [] }),
266 ],
267 rules,
268 () => false,
269 )
270 expect(selection.inserts).toEqual([])
271 expect(selection.skipped).toEqual({
272 'image output': 1,
273 deprecated: 1,
274 'routing variant': 1,
275 'skip pattern': 1,
276 'no modalities': 1,
277 })
278 })
279})
280​
281describe('hasImageOutput / alreadySynced', () => {
282 it('treats image activity and image output as image models', () => {
283 expect(hasImageOutput(native({ activity: 'image' }))).toBe(true)
284 expect(
285 hasImageOutput(native({ outputModalities: ['image', 'text'] })),
286 ).toBe(true)
287 expect(hasImageOutput(native({ outputModalities: ['text'] }))).toBe(false)
288 })
289​
290 it('matches existing ids with dots or dashes', () => {
291 expect(
292 alreadySynced(
293 'claude-sonnet-4.5',
294 new Set(['claude-sonnet-4-5']),
295 new Set(),
296 ),
297 ).toBe(true)
298 expect(
299 alreadySynced('gpt-5.6-luna', new Set(), new Set(['GPT_5_6_LUNA'])),
300 ).toBe(true)
301 expect(alreadySynced('gpt-5.6-luna', new Set(), new Set())).toBe(false)
302 })
303})