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

opencode-routes.test.ts

314 lines9.9 KB

1import { afterEach, expect, it, vi } from 'vitest'
2import { OPENCODE_GO_MODELS_URL } from './opencode-go-facts.ts'
3import {
4 classifyOpenCodeRoute,
5 documentedRoutes,
6 OPENCODE_ROUTE_DOCS_URL,
7 openCodeRouteSpec,
8} from './opencode-routes.ts'
9​
10/** inference.mdx excerpt (2026-10-21). */
11const ZEN_DOCS = `# Inference API
12​
13## Endpoints
14​
15| API | Path |
16| ----------------------- | --------------------------------------------------------- |
17| OpenAI Chat Completions | \`/inference/openai/v1/chat/completions\` |
18| OpenAI Responses | \`/inference/openai/v1/responses\` |
19| Anthropic Messages | \`/inference/anthropic/v1/messages\` |
20| Gemini | \`/inference/google/v1beta/models/<model>:generateContent\` |
21​
22### Gemini
23​
24\`\`\`bash
25curl -X POST "https://opencode.ai/inference/google/v1beta/models/gemini-3.1-pro:generateContent"
26\`\`\`
28​
29/** go.mdx excerpt (2026-10-21). */
30const GO_DOCS = `# Go
31​
32## Endpoints
33​
34| Model | Model ID | Endpoint | AI SDK Package |
35| ------ | -------- | -------------------------------------------------------------- | --------------------------- |
36| GPT | gpt | \`https://opencode.ai/inference/go/openai/v1/responses\` | \`@ai-sdk/openai\` |
37| Haiku | haiku | \`https://opencode.ai/inference/go/anthropic/v1/messages\` | \`@ai-sdk/anthropic\` |
38| Gemini | gemini | \`https://opencode.ai/inference/go/google/v1beta/models/gemini\` | \`@ai-sdk/google\` |
39​
40## Privacy
41​
42\`https://opencode.ai/inference/go/openai/v1/chat/completions\`
44​
45/** Models list shaped like GET /inference/go/v1/models (2026-11-17). */
46const GO_LISTING = {
47 object: 'list',
48 data: [
49 {
50 id: 'gpt',
51 api: {
52 responses: {
53 base_url: 'https://opencode.ai/inference/go/openai/v1',
54 sdk: { aisdk: '@ai-sdk/openai' },
55 },
56 },
57 },
58 {
59 id: 'haiku',
60 api: {
61 messages: {
62 base_url: 'https://opencode.ai/inference/go/anthropic/v1',
63 sdk: { aisdk: '@ai-sdk/anthropic' },
64 },
65 },
66 },
67 {
68 id: 'kimi',
69 api: {
70 'chat-completions': {
71 base_url: 'https://opencode.ai/inference/go/openai/v1',
72 sdk: {
73 aisdk: '@ai-sdk/openai-compatible',
74 opencodeai: '@opencode/ai/providers/openai-compatible',
75 },
76 compatibility: { reasoning_field: 'reasoning_content' },
77 },
78 },
79 cost: { input: 1, output: 2 },
80 limit: { context: 128000 },
81 capabilities: { tools: true },
82 },
83 ],
85​
86const original = globalThis.fetch
87afterEach(() => {
88 globalThis.fetch = original
89})
90function serve(text: string) {
91 globalThis.fetch = vi.fn(async () => new Response(text))
93function fetched(): Array<string> {
94 return (globalThis.fetch as ReturnType<typeof vi.fn>).mock.calls.map((call) =>
95 String(call[0]),
96 )
98​
99it('reads only the Endpoints table, with origins stripped', () => {
100 expect([...documentedRoutes(GO_DOCS, 'go')]).toEqual([
101 '/inference/go/openai/v1/responses',
102 '/inference/go/anthropic/v1/messages',
103 '/inference/go/google/v1beta/models/gemini',
104 ])
105 expect(() => documentedRoutes('# Inference', 'docs')).toThrow(
106 'documents no endpoint paths',
107 )
108})
109it('sources Zen protocol paths from the inference docs, with no schema bodies', async () => {
110 serve(ZEN_DOCS)
111 const result = await openCodeRouteSpec(
112 [
113 {
114 schemaEndpointId: 'inference/openai/v1/chat/completions',
115 activity: 'chat',
116 },
117 { schemaEndpointId: 'inference/openai/v1/responses', activity: 'chat' },
118 { schemaEndpointId: 'inference/anthropic/v1/messages', activity: 'chat' },
119 {
120 schemaEndpointId:
121 'inference/google/v1beta/models/gemini:generateContent',
122 activity: 'chat',
123 },
124 { schemaEndpointId: null, activity: null },
125 ],
126 { url: 'https://models.dev/api.json', text: 'catalog fixture' },
127 false,
128 )
129 expect(Object.keys(result.specs[0]?.paths ?? {})).toEqual([
130 '/inference/openai/v1/chat/completions',
131 '/inference/openai/v1/responses',
132 '/inference/anthropic/v1/messages',
133 '/inference/google/v1beta/models/gemini:generateContent',
134 ])
135 const operation =
136 result.specs[0]?.paths?.['/inference/anthropic/v1/messages']?.post ?? {}
137 expect(operation).not.toHaveProperty('requestBody')
138 expect(operation.responses).toEqual({})
139 expect(classifyOpenCodeRoute('/inference/v1/messages', operation)).toBe(
140 'chat',
141 )
142 expect(classifyOpenCodeRoute('/inference/v1/messages', {})).toBeNull()
143 expect(fetched()).toEqual([OPENCODE_ROUTE_DOCS_URL])
144 const source = operation['x-modelschemas-method-source'] as {
145 url: string
146 hash: string
147 }
148 expect(source.url).toBe(OPENCODE_ROUTE_DOCS_URL)
149 expect(source.hash).toMatch(/^[a-f0-9]{64}$/)
150 expect(result.sources.map((s) => s.url)).toEqual([
151 'https://models.dev/api.json',
152 ])
153})
154it('checks Go ids against routes from the models list api field', async () => {
155 serve(JSON.stringify(GO_LISTING))
156 const result = await openCodeRouteSpec(
157 [
158 {
159 schemaEndpointId: 'inference/go/openai/v1/responses',
160 activity: 'chat',
161 },
162 {
163 schemaEndpointId: 'inference/go/anthropic/v1/messages',
164 activity: 'chat',
165 },
166 ],
167 { url: 'docs', text: 'docs' },
168 true,
169 )
170 expect(Object.keys(result.specs[0]?.paths ?? {})).toEqual([
171 '/inference/go/openai/v1/responses',
172 '/inference/go/anthropic/v1/messages',
173 '/inference/go/google/v1beta/models/gemini-4-argon-contributor:generateContent',
174 '/inference/go/google/v1beta/models/gemini-4-argon-contributor:streamGenerateContent',
175 ])
176 expect(fetched()).toEqual([OPENCODE_GO_MODELS_URL])
177 const source = result.specs[0]?.paths?.['/inference/go/openai/v1/responses']
178 ?.post?.['x-modelschemas-method-source'] as { url: string; hash: string }
179 expect(source.url).toBe(OPENCODE_GO_MODELS_URL)
180 expect(source.hash).toMatch(/^[a-f0-9]{64}$/)
181 const parameters = result.specs[0]?.paths?.[
182 '/inference/go/openai/v1/responses'
183 ]?.post?.parameters as Array<{ name: string; required: boolean }>
184 expect(parameters.map((header) => header.name)).toEqual([
185 'x-opencode-session',
186 'User-Agent',
187 ])
188 expect(parameters.every((header) => header.required)).toBe(true)
189})
190it.each([
191 ['inference/openai/v1/messages', false],
192 ['inference/anthropic/v1/chat/completions', false],
193 ['inference/go/openai/v1/chat/completions', false],
194 ['inference/go/anthropic/v1/chat/completions', true],
195 ['inference/go/google/v1beta/models/gemini:generateContent', true],
196 ['inference/go/openai/v1/embeddings', true],
197 ['inference/v1/messages', false],
198 ['v1/chat/completions', true],
199 ['v1/responses', true],
200 ['v1/embeddings', true],
201])('rejects %s, a route the docs do not list', async (id, go) => {
202 serve(go ? JSON.stringify(GO_LISTING) : ZEN_DOCS)
203 await expect(
204 openCodeRouteSpec(
205 [{ schemaEndpointId: id, activity: 'chat' }],
206 { url: 'docs', text: 'docs' },
207 go,
208 ),
209 ).rejects.toThrow(`${go ? 'states' : 'documents'} no /${id} route`)
210})
211it('accepts the interactions route as route-only, with no request schema', async () => {
212 serve(
213 JSON.stringify({
214 object: 'list',
215 data: [
216 ...GO_LISTING.data,
217 {
218 id: 'gemini-4-argon',
219 api: {
220 'generate-content': {
221 base_url: 'https://opencode.ai/inference/go/google/v1beta',
222 sdk: { opencodeai: '@opencode/ai/providers/google' },
223 },
224 interactions: {
225 base_url: 'https://opencode.ai/inference/go/google/v1beta',
226 sdk: {
227 opencodeai: '@opencode/ai/providers/google/interactions',
228 },
229 },
230 },
231 },
232 ],
233 }),
234 )
235 const path = '/inference/go/google/v1beta/interactions'
236 const result = await openCodeRouteSpec(
237 [
238 {
239 schemaEndpointId: 'inference/go/openai/v1/responses',
240 activity: 'chat',
241 },
242 {
243 schemaEndpointId: 'inference/go/google/v1beta/interactions',
244 activity: 'chat',
245 },
246 ],
247 { url: 'docs', text: 'docs' },
248 true,
249 )
250 const operation = result.specs[0]?.paths?.[path]?.post ?? {}
251 expect(operation).not.toHaveProperty('requestBody')
252 expect(operation.responses).toEqual({})
253 expect(classifyOpenCodeRoute(path, operation)).toBe('chat')
254 expect(result.specs[0]?.paths).not.toHaveProperty(
255 '/inference/go/google/v1beta/models/gemini-4-argon:generateContent',
256 )
257})
258it('rejects a Go listing row with no api entry or an unknown protocol', async () => {
259 serve(
260 JSON.stringify({
261 object: 'list',
262 data: [{ id: 'gpt', object: 'model' }],
263 }),
264 )
265 await expect(
266 openCodeRouteSpec(
267 [
268 {
269 schemaEndpointId: 'inference/go/openai/v1/responses',
270 activity: 'chat',
271 },
272 ],
273 { url: 'docs', text: 'docs' },
274 true,
275 ),
276 ).rejects.toThrow('opencode-go: gpt has no api entry')
277 serve(
278 JSON.stringify({
279 object: 'list',
280 data: [
281 {
282 id: 'gpt',
283 api: {
284 embeddings: {
285 base_url: 'https://opencode.ai/inference/go/openai/v1',
286 },
287 },
288 },
289 ],
290 }),
291 )
292 await expect(
293 openCodeRouteSpec(
294 [
295 {
296 schemaEndpointId: 'inference/go/openai/v1/responses',
297 activity: 'chat',
298 },
299 ],
300 { url: 'docs', text: 'docs' },
301 true,
302 ),
303 ).rejects.toThrow('opencode-go: gpt has unknown api protocol embeddings')
304})
305it('rejects a failed docs source', async () => {
306 globalThis.fetch = vi.fn(async () => new Response('gone', { status: 404 }))
307 await expect(
308 openCodeRouteSpec(
309 [{ schemaEndpointId: 'v1/messages', activity: 'chat' }],
310 { url: 'docs', text: 'docs' },
311 true,
312 ),
313 ).rejects.toThrow()
314})