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

fact-sources.test.ts

352 lines10.2 KB

1import { describe, expect, it } from 'vitest'
2​
3import {
4 factDiscrepancies,
5 listingSources,
6 mergeListingAndSchema,
7 openRouterJoinIds,
8 schemaRung,
9 tagDocsFacts,
10 walkRequestSchema,
11} from './fact-sources.ts'
12​
13const chatSchema = {
14 properties: {
15 tools: { type: 'array' },
16 tool_choice: { type: 'string' },
17 temperature: { type: 'number' },
18 top_p: { type: 'number' },
19 max_tokens: { type: 'integer' },
20 thinking: { type: 'object' },
21 messages: {
22 type: 'array',
23 items: {
24 properties: {
25 content: {
26 anyOf: [
27 { type: 'string' },
28 {
29 type: 'array',
30 items: {
31 properties: {
32 type: { enum: ['text', 'image'] },
33 image_url: { type: 'object' },
34 },
35 },
36 },
37 ],
38 },
39 },
40 },
41 },
42 },
44​
45describe('walkRequestSchema', () => {
46 it('maps request properties to OpenRouter parameter names', () => {
47 const walk = walkRequestSchema(chatSchema, {
48 derivation: 'upstream-spec',
49 endpointId: 'v1/messages',
50 sourceUrl: 'https://api.anthropic.com/openapi.json',
51 })
52 expect(walk?.flags.sort()).toEqual(
53 [
54 'max_tokens',
55 'reasoning',
56 'temperature',
57 'tool_choice',
58 'tools',
59 'top_p',
60 ].sort(),
61 )
62 expect(walk?.sources.capabilities?.tools).toMatchObject({
63 derivation: 'upstream-spec',
64 endpointId: 'v1/messages',
65 path: '/properties/tools',
66 })
67 })
68​
69 it('walks Gemini generationConfig and responseSchema', () => {
70 const walk = walkRequestSchema(
71 {
72 properties: {
73 generationConfig: {
74 properties: {
75 temperature: { type: 'number' },
76 topP: { type: 'number' },
77 topK: { type: 'integer' },
78 maxOutputTokens: { type: 'integer' },
79 responseSchema: { type: 'object' },
80 },
81 },
82 tools: { type: 'array' },
83 },
84 },
85 {
86 derivation: 'upstream-spec',
87 endpointId: 'v1beta/models/{modelsId}:generateContent',
88 },
89 )
90 expect(walk?.flags.sort()).toEqual(
91 [
92 'max_tokens',
93 'response_format',
94 'structured_outputs',
95 'temperature',
96 'tools',
97 'top_k',
98 'top_p',
99 ].sort(),
100 )
101 })
102})
103​
104describe('mergeListingAndSchema', () => {
105 // The shape of Mistral's /v1/chat/completions and Gemini's
106 // generateContent bodies: media content parts beside a plain-string part.
107 const multimodalRoute = {
108 properties: {
109 tools: { type: 'array' },
110 messages: {
111 type: 'array',
112 items: {
113 properties: {
114 content: {
115 anyOf: [
116 { type: 'string' },
117 {
118 type: 'array',
119 items: {
120 anyOf: [
121 { properties: { text: { type: 'string' } } },
122 { properties: { image_url: { type: 'object' } } },
123 { properties: { input_audio: { type: 'object' } } },
124 ],
125 },
126 },
127 ],
128 },
129 },
130 },
131 },
132 },
133 }
134​
135 it('never fills modalities from the route schema', () => {
136 const walk = walkRequestSchema(multimodalRoute, {
137 derivation: 'upstream-spec',
138 endpointId: 'v1/chat/completions',
139 })
140 // A text-only model and an audio model bind the same route. Walked, both
141 // stored `{ input: ['image', 'audio'], output: [] }`: no text, no output.
142 for (const rawId of ['codestral-2508', 'voxtral-small-latest']) {
143 const merged = mergeListingAndSchema({ rawId, activity: 'chat' }, walk)
144 expect(merged.modalities).toBeNull()
145 expect(merged.factSources?.modalities).toBeUndefined()
146 // Capability flags still come from the schema.
147 expect(merged.capabilities).toEqual(['tools'])
148 }
149 })
150​
151 it('keeps the modalities the listing states', () => {
152 const walk = walkRequestSchema(multimodalRoute, {
153 derivation: 'upstream-spec',
154 endpointId: 'v1/chat/completions',
155 })
156 const modalities = { input: ['text'], output: ['text'] }
157 const merged = mergeListingAndSchema(
158 { rawId: 'codestral-2508', modalities },
159 walk,
160 )
161 expect(merged.modalities).toEqual(modalities)
162 expect(merged.factSources?.modalities).toEqual({ derivation: 'listing' })
163 })
164​
165 it('keeps listing flags and fills the rest from the schema', () => {
166 const walk = walkRequestSchema(chatSchema, {
167 derivation: 'upstream-spec',
168 endpointId: 'v1/messages',
169 })
170 const merged = mergeListingAndSchema(
171 {
172 rawId: 'claude-sonnet-4-5',
173 capabilities: ['reasoning', 'structured_outputs'],
174 contextWindow: 200_000,
175 },
176 walk,
177 )
178 expect(merged.contextWindow).toBe(200_000)
179 expect(merged.capabilities).toEqual(
180 expect.arrayContaining([
181 'reasoning',
182 'structured_outputs',
183 'tools',
184 'temperature',
185 ]),
186 )
187 expect(merged.factSources?.contextWindow?.derivation).toBe('listing')
188 expect(merged.factSources?.capabilities?.reasoning?.derivation).toBe(
189 'listing',
190 )
191 expect(merged.factSources?.capabilities?.tools?.derivation).toBe(
192 'upstream-spec',
193 )
194 })
195​
196 it('adds no schema flags to an exact capability list', () => {
197 const walk = walkRequestSchema(chatSchema, {
198 derivation: 'upstream-spec',
199 endpointId: 'chat/completions',
200 })
201 const merged = mergeListingAndSchema(
202 {
203 rawId: 'glm-4.5v',
204 capabilities: ['temperature'],
205 exactCapabilities: true,
206 },
207 walk,
208 )
209 expect(merged.capabilities).toEqual(['temperature'])
210 expect(Object.keys(merged.factSources?.capabilities ?? {})).toEqual([
211 'temperature',
212 ])
213 })
214​
215 it('does not walk in a flag the listing says the model does not support', () => {
216 const walk = walkRequestSchema(chatSchema, {
217 derivation: 'upstream-spec',
218 endpointId: 'chat/completions',
219 })
220 const merged = mergeListingAndSchema(
221 { rawId: 'm', unsupportedCapabilities: ['tools'] },
222 walk,
223 )
224 expect(merged.capabilities).not.toContain('tools')
225 expect(merged.capabilities).toContain('temperature')
226 // The no keeps its listing source; the walk does not overwrite it.
227 expect(merged.factSources?.capabilities?.tools).toEqual({
228 derivation: 'listing',
229 })
230 })
231​
232 it('adds no schema flags to a stored map kept as unavailable', () => {
233 const walk = walkRequestSchema(chatSchema, {
234 derivation: 'upstream-spec',
235 endpointId: 'x',
236 })
237 const merged = mergeListingAndSchema(
238 { rawId: 'kept', capabilities: { reasoning: true, tools: false } },
239 walk,
240 )
241 expect(merged.capabilities).toEqual({ reasoning: true, tools: false })
242 expect(merged.factSources?.capabilities).toBeUndefined()
243 })
244})
245​
246describe('provenance helpers', () => {
247 it('defaults untagged listing fields', () => {
248 expect(listingSources({ rawId: 'x', contextWindow: 8_000 })).toEqual({
249 contextWindow: { derivation: 'listing' },
250 })
251 })
252​
253 it('overlays provider tags on listing defaults', () => {
254 expect(
255 listingSources({
256 rawId: 'x',
257 contextWindow: 8_000,
258 maxOutput: 4_000,
259 factSources: { contextWindow: { derivation: 'docs-derived' } },
260 }),
261 ).toEqual({
262 contextWindow: { derivation: 'docs-derived' },
263 maxOutput: { derivation: 'listing' },
264 })
265 })
266​
267 it('tags docs facts per field', () => {
268 const sources = tagDocsFacts(
269 {
270 contextWindow: 400_000,
271 maxOutput: 128_000,
272 modalities: { input: ['text'], output: ['text'] },
273 capabilities: ['tools'],
274 },
275 'https://developers.openai.com/api/docs/models/gpt-5.md',
276 )
277 expect(sources.contextWindow?.derivation).toBe('docs-derived')
278 expect(sources.capabilities?.tools?.path).toBe('capabilities.tools')
279 })
280​
281 it('skips generated schemas at the rung check', () => {
282 expect(schemaRung('generated')).toBeNull()
283 expect(schemaRung('upstream-spec')).toBe('upstream-spec')
284 expect(schemaRung(null)).toBeNull()
285 })
286})
287​
288describe('OpenRouter compare', () => {
289 it('joins native ids onto OpenRouter slugs', () => {
290 expect(openRouterJoinIds('openai', 'gpt-5-2025-08-07')).toEqual([
291 'openai/gpt-5-2025-08-07',
292 'openai/gpt-5',
293 ])
294 expect(
295 openRouterJoinIds('anthropic', 'claude-sonnet-4-5-20250929'),
296 ).toEqual([
297 'anthropic/claude-sonnet-4-5-20250929',
298 'anthropic/claude-sonnet-4-5',
299 'anthropic/claude-sonnet-4.5',
300 ])
301 expect(openRouterJoinIds('grok', 'grok-4.6')).toEqual(['x-ai/grok-4.6'])
302 expect(openRouterJoinIds('grok', 'grok-4.20-0309-non-reasoning')).toEqual([
303 'x-ai/grok-4.20-0309-non-reasoning',
304 'x-ai/grok-4.20',
305 ])
306 expect(openRouterJoinIds('grok', 'grok-4.20-multi-agent-0309')).toEqual([
307 'x-ai/grok-4.20-multi-agent-0309',
308 'x-ai/grok-4.20-multi-agent',
309 ])
310 expect(openRouterJoinIds('fal', 'x')).toEqual([])
311 })
312​
313 it('reports flags we have that OpenRouter lacks, and the reverse', () => {
314 const diffs = factDiscrepancies(
315 {
316 contextWindow: 200_000,
317 maxOutput: 8_192,
318 modalities: { input: ['text', 'image'], output: ['text'] },
319 capabilities: { tools: true, temperature: true, seed: false },
320 factSources: {
321 capabilities: { tools: { derivation: 'upstream-spec' } },
322 },
323 },
324 {
325 rawId: 'anthropic/claude-sonnet-4.5',
326 contextWindow: 200_000,
327 maxOutput: 8_192,
328 modalities: { input: ['text'], output: ['text'] },
329 capabilities: { tools: true, top_p: true },
330 },
331 )
332 expect(diffs).toEqual(
333 expect.arrayContaining([
334 expect.objectContaining({
335 field: 'modalities',
336 oursDerivation: null,
337 }),
338 expect.objectContaining({
339 field: 'capabilities.temperature',
340 ours: true,
341 openrouter: false,
342 oursDerivation: null,
343 }),
344 expect.objectContaining({
345 field: 'capabilities.top_p',
346 ours: false,
347 openrouter: true,
348 }),
349 ]),
350 )
351 })
352})