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

opencode-catalog.test.ts

314 lines9.0 KB

1import { describe, expect, it } from 'vitest'
2import {
3 OPENCODE_CATALOG_URL,
4 parseOpenCodeCatalog,
5} from './opencode-catalog.ts'
6​
7// Synthetic fixtures exercise catalog fields, never production model data.
8const HASH = 'a'.repeat(64)
9​
10function payload(extra: Record<string, unknown> = {}) {
11 return {
12 opencode: {
13 npm: '@ai-sdk/openai-compatible',
14 api: 'https://opencode.ai/inference/v1',
15 models: {
16 synthetic: {
17 id: 'synthetic',
18 limit: { context: 1234, output: 234 },
19 modalities: { input: ['text', 'image'], output: ['text'] },
20 tool_call: true,
21 structured_output: false,
22 reasoning: true,
23 reasoning_options: [{ type: 'effort', values: ['none', 'high'] }],
24 interleaved: { field: 'reasoning_content' },
25 ...extra,
26 },
27 },
28 },
29 }
31describe('OpenCode native catalog', () => {
32 it('extracts only stated facts and records first-party provenance', () => {
33 const model = parseOpenCodeCatalog(
34 payload(),
35 'opencode',
36 'digest',
37 ).synthetic
38 expect(model).toMatchObject({
39 contextWindow: 1234,
40 maxOutput: 234,
41 modalities: { input: ['text', 'image'], output: ['text'] },
42 capabilities: {
43 tools: true,
44 structured_outputs: false,
45 reasoning: true,
46 vision: true,
47 },
48 reasoning: {
49 mode: 'effort',
50 mandatory: false,
51 efforts: ['none', 'high'],
52 },
53 requestMap: {
54 replayReasoningContent: true,
55 thinking: null,
56 developerRole: null,
57 },
58 })
59 expect(model?.factSources?.contextWindow).toEqual({
60 derivation: 'listing',
61 sourceUrl: OPENCODE_CATALOG_URL,
62 sourceHash: 'digest',
63 path: 'opencode.models.synthetic.limit.context',
64 })
65 })
66 it('does not infer mandatory thinking from an effort list without off', () => {
67 expect(
68 parseOpenCodeCatalog(
69 payload({ reasoning_options: [{ type: 'effort', values: ['high'] }] }),
70 'opencode',
71 'h',
72 ).synthetic?.reasoning?.mandatory,
73 ).toBeNull()
74 })
75 it('recognizes budget plus toggle and leaves absent replay unknown', () => {
76 const model = parseOpenCodeCatalog(
77 payload({
78 reasoning_options: [{ type: 'toggle' }, { type: 'budget_tokens' }],
79 interleaved: undefined,
80 }),
81 'opencode',
82 'h',
83 ).synthetic
84 expect(model?.reasoning).toEqual({ mode: 'budget', mandatory: false })
85 expect(model?.requestMap).toBeNull()
86 })
87 it('leaves missing facts null rather than borrowing another provider', () => {
88 const data = {
89 opencode: {
90 npm: '@ai-sdk/openai-compatible',
91 api: 'https://opencode.ai/inference/v1',
92 models: { synthetic: { id: 'synthetic' } },
93 },
94 unrelated: { models: payload().opencode.models },
95 }
96 expect(parseOpenCodeCatalog(data, 'opencode', 'h').synthetic).toMatchObject(
97 {
98 contextWindow: null,
99 maxOutput: null,
100 capabilities: null,
101 modalities: null,
102 reasoning: null,
103 requestMap: null,
104 },
105 )
106 expect(() => parseOpenCodeCatalog(data, 'opencode-go', 'h')).toThrow(
107 /no provider/,
108 )
109 })
110 it.each([
111 { limit: { context: 'unknown' } },
112 { modalities: { input: 'text', output: [] } },
113 { tool_call: 'yes' },
114 { reasoning_options: [{ type: 'effort', values: null }] },
115 { interleaved: {} },
116 ])('rejects malformed catalog facts %j', (fields) => {
117 expect(() =>
118 parseOpenCodeCatalog(payload(fields), 'opencode', 'h'),
119 ).toThrow(/unreadable/)
120 })
121})
122​
123it('uses explicit catalog dates and weights with field provenance', () => {
124 const model = parseOpenCodeCatalog(
125 payload({
126 release_date: '2026-09-22',
127 knowledge: '2026-04',
128 open_weights: false,
129 }),
130 'opencode',
131 'hash',
132 ).synthetic
133 expect(model).toMatchObject({
134 releasedAt: Date.UTC(2026, 8, 22) / 1000,
135 knowledgeCutoff: '2026-04',
136 openWeights: false,
137 })
138 expect(model?.providerMetadata?.release_date).toBe('2026-09-22')
139 expect(model?.factSources?.knowledgeCutoff?.path).toBe(
140 'opencode.models.synthetic.knowledge',
141 )
142 expect(model?.factSources?.openWeights?.path).toBe(
143 'opencode.models.synthetic.open_weights',
144 )
145 const unknown = parseOpenCodeCatalog(payload(), 'opencode', 'hash').synthetic
146 expect(unknown).toMatchObject({
147 releasedAt: null,
148 knowledgeCutoff: null,
149 openWeights: null,
150 })
151})
152it.each([
153 { release_date: '2026-02-30' },
154 { release_date: '2026-13-01' },
155 { release_date: 123 },
156 { knowledge: '2026-00' },
157 { knowledge: '2026-02-30' },
158 { knowledge: 'unknown' },
159 { open_weights: 'true' },
160])('rejects malformed sourced dates or weights %j', (fields) => {
161 expect(() =>
162 parseOpenCodeCatalog(payload(fields), 'opencode', 'hash'),
163 ).toThrow(/unreadable/)
164})
165​
166it('derives the route from the package, and reads no Zen price from cost', () => {
167 const model = parseOpenCodeCatalog(
168 payload({
169 name: 'Synthetic',
170 provider: { npm: '@ai-sdk/anthropic' },
171 cost: { input: 4, output: 20 },
172 }),
173 'opencode',
174 HASH,
175 ).synthetic
176 expect(model).toMatchObject({
177 displayName: 'Synthetic',
178 activity: 'chat',
179 schemaEndpointId: 'inference/v1/messages',
180 factSources: {
181 schemaEndpointId: {
182 derivation: 'listing',
183 sourceUrl: OPENCODE_CATALOG_URL,
184 path: 'opencode.models.synthetic.provider.npm',
185 },
186 },
187 })
188 expect(model?.pricing).toBeUndefined()
189 expect(model?.factSources?.pricing).toBeUndefined()
190})
191​
192it('uses the provider package when the model states none', () => {
193 const model = parseOpenCodeCatalog(payload(), 'opencode', HASH).synthetic
194 expect(model?.schemaEndpointId).toBe('inference/v1/chat/completions')
195 expect(model?.factSources?.schemaEndpointId?.path).toBe('opencode.npm')
196})
197​
198it('routes each Zen model on its own protocol api, not the provider default', () => {
199 const data = {
200 opencode: {
201 npm: '@ai-sdk/openai-compatible',
202 api: 'https://opencode.ai/inference/openai/v1',
203 models: {
204 open: {
205 id: 'open',
206 provider: { api: 'https://opencode.ai/inference/openai/v1' },
207 },
208 gpt: {
209 id: 'gpt',
210 provider: {
211 npm: '@ai-sdk/openai',
212 api: 'https://opencode.ai/inference/openai/v1',
213 },
214 },
215 claude: {
216 id: 'claude',
217 provider: {
218 npm: '@ai-sdk/anthropic',
219 api: 'https://opencode.ai/inference/anthropic/v1',
220 },
221 },
222 gemini: {
223 id: 'gemini',
224 provider: {
225 npm: '@ai-sdk/google',
226 api: 'https://opencode.ai/inference/google/v1beta',
227 },
228 },
229 },
230 },
231 }
232 const byId = parseOpenCodeCatalog(data, 'opencode', 'h')
233 expect(
234 Object.fromEntries(
235 Object.entries(byId).map(([id, model]) => [id, model.schemaEndpointId]),
236 ),
237 ).toEqual({
238 open: 'inference/openai/v1/chat/completions',
239 gpt: 'inference/openai/v1/responses',
240 claude: 'inference/anthropic/v1/messages',
241 gemini: 'inference/google/v1beta/models/gemini:generateContent',
242 })
243 expect(
244 Object.fromEntries(
245 Object.entries(byId).map(([id, model]) => [
246 id,
247 model.factSources?.schemaEndpointId?.path,
248 ]),
249 ),
250 ).toEqual({
251 open: 'opencode.models.open.provider',
252 gpt: 'opencode.models.gpt.provider',
253 claude: 'opencode.models.claude.provider',
254 gemini: 'opencode.models.gemini.provider',
255 })
256})
257​
258it.each([
259 { provider: { api: 'https://example.com/inference/anthropic/v1' } },
260 { provider: { api: 42 } },
261 { provider: { api: '' } },
262])('rejects a model api that is not an opencode.ai path %j', (fields) => {
263 expect(() => parseOpenCodeCatalog(payload(fields), 'opencode', 'h')).toThrow(
264 /opencode: .*synthetic/,
265 )
266})
267​
268it('leaves an unmapped package without a route', () => {
269 const model = parseOpenCodeCatalog(
270 payload({ provider: { npm: '@ai-sdk/mistral' } }),
271 'opencode',
272 HASH,
273 ).synthetic
274 expect(model?.schemaEndpointId).toBeUndefined()
275 expect(model?.activity).toBeUndefined()
276})
277​
278it('routes Go rows on the model api, like Zen', () => {
279 const data = {
280 'opencode-go': {
281 npm: '@ai-sdk/openai-compatible',
282 api: 'https://opencode.ai/inference/go/v1',
283 models: {
284 synthetic: {
285 id: 'synthetic',
286 provider: {
287 npm: '@ai-sdk/openai',
288 api: 'https://opencode.ai/inference/go/openai/v1',
289 },
290 },
291 },
292 },
293 }
294 const model = parseOpenCodeCatalog(data, 'opencode-go', HASH).synthetic
295 expect(model).toMatchObject({
296 schemaEndpointId: 'inference/go/openai/v1/responses',
297 activity: 'chat',
298 factSources: {
299 schemaEndpointId: { path: 'opencode-go.models.synthetic.provider' },
300 },
301 })
302})
303​
304it('throws when a Go block states no api', () => {
305 const data = {
306 'opencode-go': {
307 npm: '@ai-sdk/openai-compatible',
308 models: { synthetic: { id: 'synthetic' } },
309 },
310 }
311 expect(() => parseOpenCodeCatalog(data, 'opencode-go', HASH)).toThrow(
312 'opencode-go: models.dev api is missing',
313 )
314})