Open History to see the last commit
1import { afterEach, expect, it, vi } from 'vitest'
2import {
3 classifyOpenCodeRoute,
4 documentedRoutes,
5 OPENCODE_GO_ROUTE_DOCS_URL,
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\`\`\`
27`
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\`
43`
44
45const original = globalThis.fetch
46afterEach(() => {
47 globalThis.fetch = original
48})
49function serve(text: string) {
50 globalThis.fetch = vi.fn(async () => new Response(text))
51}
52function fetched(): Array<string> {
53 return (globalThis.fetch as ReturnType<typeof vi.fn>).mock.calls.map((call) =>
54 String(call[0]),
55 )
56}
57
58it('reads only the Endpoints table, with origins stripped', () => {
59 expect([...documentedRoutes(GO_DOCS, 'go')]).toEqual([
60 '/inference/go/openai/v1/responses',
61 '/inference/go/anthropic/v1/messages',
62 '/inference/go/google/v1beta/models/gemini',
63 ])
64 expect(() => documentedRoutes('# Inference', 'docs')).toThrow(
65 'documents no endpoint paths',
66 )
67})
68it('sources Zen protocol paths from the inference docs, with no schema bodies', async () => {
69 serve(ZEN_DOCS)
70 const result = await openCodeRouteSpec(
71 [
72 {
73 schemaEndpointId: 'inference/openai/v1/chat/completions',
74 activity: 'chat',
75 },
76 { schemaEndpointId: 'inference/openai/v1/responses', activity: 'chat' },
77 { schemaEndpointId: 'inference/anthropic/v1/messages', activity: 'chat' },
78 {
79 schemaEndpointId:
80 'inference/google/v1beta/models/gemini:generateContent',
81 activity: 'chat',
82 },
83 { schemaEndpointId: null, activity: null },
84 ],
85 { url: 'https://models.dev/api.json', text: 'catalog fixture' },
86 false,
87 )
88 expect(Object.keys(result.specs[0]?.paths ?? {})).toEqual([
89 '/inference/openai/v1/chat/completions',
90 '/inference/openai/v1/responses',
91 '/inference/anthropic/v1/messages',
92 '/inference/google/v1beta/models/gemini:generateContent',
93 ])
94 const operation =
95 result.specs[0]?.paths?.['/inference/anthropic/v1/messages']?.post ?? {}
96 expect(operation).not.toHaveProperty('requestBody')
97 expect(operation.responses).toEqual({})
98 expect(classifyOpenCodeRoute('/inference/v1/messages', operation)).toBe(
99 'chat',
100 )
101 expect(classifyOpenCodeRoute('/inference/v1/messages', {})).toBeNull()
102 expect(fetched()).toEqual([OPENCODE_ROUTE_DOCS_URL])
103 const source = operation['x-modelschemas-method-source'] as {
104 url: string
105 hash: string
106 }
107 expect(source.url).toBe(OPENCODE_ROUTE_DOCS_URL)
108 expect(source.hash).toMatch(/^[a-f0-9]{64}$/)
109 expect(result.sources.map((s) => s.url)).toEqual([
110 'https://models.dev/api.json',
111 ])
112})
113it('checks Go ids against the full documented /inference/go/<protocol>/ path', async () => {
114 serve(GO_DOCS)
115 const result = await openCodeRouteSpec(
116 [
117 {
118 schemaEndpointId: 'inference/go/openai/v1/responses',
119 activity: 'chat',
120 },
121 {
122 schemaEndpointId: 'inference/go/anthropic/v1/messages',
123 activity: 'chat',
124 },
125 ],
126 { url: 'docs', text: 'docs' },
127 true,
128 )
129 expect(Object.keys(result.specs[0]?.paths ?? {})).toEqual([
130 '/inference/go/openai/v1/responses',
131 '/inference/go/anthropic/v1/messages',
132 ])
133 expect(fetched()).toEqual([OPENCODE_GO_ROUTE_DOCS_URL])
134})
135it.each([
136 ['inference/openai/v1/messages', false],
137 ['inference/anthropic/v1/chat/completions', false],
138 ['inference/go/openai/v1/chat/completions', false],
139 ['inference/go/anthropic/v1/chat/completions', true],
140 ['inference/go/google/v1beta/models/gemini:generateContent', true],
141 ['inference/v1/messages', false],
142 ['v1/chat/completions', true],
143 ['v1/responses', true],
144 ['v1/embeddings', true],
145])('rejects %s, a route the docs do not list', async (id, go) => {
146 serve(go ? GO_DOCS : ZEN_DOCS)
147 await expect(
148 openCodeRouteSpec(
149 [{ schemaEndpointId: id, activity: 'chat' }],
150 { url: 'docs', text: 'docs' },
151 go,
152 ),
153 ).rejects.toThrow(`documents no /${id} route`)
154})
155it('rejects a failed docs source', async () => {
156 globalThis.fetch = vi.fn(async () => new Response('gone', { status: 404 }))
157 await expect(
158 openCodeRouteSpec(
159 [{ schemaEndpointId: 'v1/messages', activity: 'chat' }],
160 { url: 'docs', text: 'docs' },
161 true,
162 ),
163 ).rejects.toThrow()
164})