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

schema-binding.test.ts

195 lines5.2 KB

1import { describe, expect, it } from 'vitest'
2​
3import { pinModelField, resolveSchemaEndpointId } from './schema-binding.ts'
4​
5describe('resolveSchemaEndpointId', () => {
6 it('binds grok / openai / anthropic catalog rows onto generation routes', () => {
7 expect(
8 resolveSchemaEndpointId({
9 providerId: 'grok',
10 rawId: 'grok-imagine-image-2.0',
11 activity: 'image',
12 }),
13 ).toBe('v1/images/generations')
14 expect(
15 resolveSchemaEndpointId({
16 providerId: 'openai',
17 rawId: 'gpt-image-2',
18 activity: 'image',
19 }),
20 ).toBe('images/generations')
21 expect(
22 resolveSchemaEndpointId({
23 providerId: 'openai',
24 rawId: 'whisper-1',
25 activity: 'audio',
26 }),
27 ).toBe('audio/transcriptions')
28 expect(
29 resolveSchemaEndpointId({
30 providerId: 'anthropic',
31 rawId: 'claude-sonnet-4-5',
32 activity: 'chat',
33 }),
34 ).toBe('v1/messages')
35 expect(
36 resolveSchemaEndpointId({
37 providerId: 'openrouter',
38 rawId: 'google/veo-3.1',
39 activity: 'video',
40 }),
41 ).toBe('videos/google/veo-3.1')
42 expect(
43 resolveSchemaEndpointId({
44 providerId: 'bfl',
45 rawId: 'flux-2-pro',
46 activity: 'image',
47 }),
48 ).toBe('v1/flux-2-pro')
49 expect(
50 resolveSchemaEndpointId({
51 providerId: 'byteplus',
52 rawId: 'seedream-5-0-260128',
53 activity: 'image',
54 }),
55 ).toBe('images/generations')
56 })
57​
58 it('binds Gemini Imagen from leftover methods until schemaEndpointId is stored', () => {
59 expect(
60 resolveSchemaEndpointId({
61 providerId: 'gemini',
62 rawId: 'imagen-4.0-generate-001',
63 activity: 'image',
64 capabilities: ['predict'],
65 }),
66 ).toBe('v1beta/models/{modelsId}:predict')
67 expect(
68 resolveSchemaEndpointId({
69 providerId: 'gemini',
70 rawId: 'gemini-2.5-flash-image',
71 activity: 'image',
72 capabilities: ['generateContent'],
73 }),
74 ).toBe('v1beta/models/{modelsId}:generateContent')
75 expect(
76 resolveSchemaEndpointId({
77 providerId: 'gemini',
78 rawId: 'imagen-4.0-generate-001',
79 activity: 'image',
80 schemaEndpointId: 'v1beta/models/{modelsId}:predict',
81 capabilities: ['reasoning', 'temperature'],
82 }),
83 ).toBe('v1beta/models/{modelsId}:predict')
84 })
85​
86 it('uses the raw id for model-grained providers and null when unbound', () => {
87 expect(
88 resolveSchemaEndpointId({
89 providerId: 'fal',
90 rawId: 'xai/grok-imagine-image/v2.0/text-to-image',
91 activity: 'image',
92 }),
93 ).toBe('xai/grok-imagine-image/v2.0/text-to-image')
94 expect(
95 resolveSchemaEndpointId({
96 providerId: 'grok',
97 rawId: 'grok-voice-think-fast-2.0',
98 activity: 'audio',
99 }),
100 ).toBeNull()
101 expect(
102 resolveSchemaEndpointId({
103 providerId: 'openrouter',
104 rawId: 'openai/gpt-4o',
105 activity: null,
106 }),
107 ).toBeNull()
108 })
109})
110​
111describe('pinModelField', () => {
112 it('narrows a string model property to a one-id enum and keeps other fields', () => {
113 const schema = {
114 type: 'object',
115 properties: {
116 model: {
117 type: 'string',
118 description: 'Which model to run',
119 example: 'grok-imagine-image',
120 },
121 prompt: { type: 'string' },
122 },
123 }
124 expect(pinModelField(schema, 'grok-imagine-image-2.0')).toEqual({
125 type: 'object',
126 properties: {
127 model: {
128 type: 'string',
129 description: 'Which model to run',
130 example: 'grok-imagine-image-2.0',
131 enum: ['grok-imagine-image-2.0'],
132 },
133 prompt: { type: 'string' },
134 },
135 })
136 })
137​
138 it('leaves schemas without a model property unchanged', () => {
139 const schema = {
140 type: 'object',
141 properties: { prompt: { type: 'string' } },
142 }
143 expect(pinModelField(schema, 'grok-4')).toBe(schema)
144 })
145})
146​
147describe('explicit native schema binding', () => {
148 it('retains provider-grained routes declared by native adapters', () => {
149 expect(
150 resolveSchemaEndpointId({
151 providerId: 'meta',
152 rawId: 'MuseSpark',
153 activity: 'chat',
154 schemaEndpointId: null,
155 }),
156 ).toBe('v1/chat/completions')
157 })
158 it('does not infer model-grain or generation endpoints for guarded providers', () => {
159 for (const providerId of [
160 'cloudflare-ai-gateway',
161 'cloudflare-workers-ai',
162 'opencode',
163 'opencode-go',
164 'kimi-code-plan-global',
165 'kimi-code-plan-cn',
166 'zhipuai-coding-plan',
167 ]) {
168 expect(
169 resolveSchemaEndpointId({
170 providerId,
171 rawId: 'unpublished/model',
172 activity: 'chat',
173 schemaEndpointId: null,
174 }),
175 ).toBeNull()
176 }
177 })
178 it('retains a poll-verified explicit binding and legacy unguarded model grain', () => {
179 expect(
180 resolveSchemaEndpointId({
181 providerId: 'cloudflare-ai-gateway',
182 rawId: 'native/model',
183 activity: 'chat',
184 schemaEndpointId: 'native/model',
185 }),
186 ).toBe('native/model')
187 expect(
188 resolveSchemaEndpointId({
189 providerId: 'fal',
190 rawId: 'fal-ai/native',
191 activity: 'image',
192 }),
193 ).toBe('fal-ai/native')
194 })
195})