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

cohere.test.ts

244 lines8.4 KB

1import { readFileSync } from 'node:fs'
2​
3import { describe, expect, it } from 'vitest'
4​
5import { chatRequestMap } from '../request-map.ts'
6import { provider } from './cohere.ts'
7​
8const fixture = (name: string) =>
9 readFileSync(new URL(`../fixtures/${name}`, import.meta.url), 'utf8')
10​
11describe('cohere classify', () => {
12 it('maps chat, embed, and audio generation paths', () => {
13 expect(provider.classify('/v1/chat', {})).toBe('chat')
14 expect(provider.classify('/v2/chat', {})).toBe('chat')
15 expect(provider.classify('/v1/generate', {})).toBe('chat')
16 expect(provider.classify('/v1/embed', {})).toBe('embeddings')
17 expect(provider.classify('/v2/embed', {})).toBe('embeddings')
18 expect(provider.classify('/v2/audio/transcriptions', {})).toBe('audio')
19 })
20​
21 it('drops rerank, classify, datasets, finetune, and admin', () => {
22 expect(provider.classify('/v1/rerank', {})).toBeNull()
23 expect(provider.classify('/v2/rerank', {})).toBeNull()
24 expect(provider.classify('/v1/classify', {})).toBeNull()
25 expect(provider.classify('/v1/datasets', {})).toBeNull()
26 expect(provider.classify('/v1/finetuning/finetuned-models', {})).toBeNull()
27 expect(provider.classify('/v2/batches', {})).toBeNull()
28 expect(provider.classify('/v1/models', {})).toBeNull()
29 })
30})
31​
32describe('cohere request map', () => {
33 it('states only what the /v2/chat spec does', () => {
34 expect(chatRequestMap('cohere', 'command-a-03-2025', 'chat')).toMatchObject(
35 {
36 maxTokensField: 'max_tokens',
37 developerRole: false,
38 reasoningEffort: false,
39 thinking: null,
40 strictTools: null,
41 },
42 )
43 expect(chatRequestMap('cohere', 'embed-v4.0', 'embeddings')).toBeNull()
44 })
45})
46​
47describe('cohere listModels', () => {
48 it('skips when COHERE_API_KEY is absent', async () => {
49 const result = await provider.listModels({})
50 expect(result.models).toEqual([])
51 expect(result.skipped).toBe('cohere: COHERE_API_KEY not set — skipped')
52 })
53​
54 it('maps models[] pages (not OpenAI data[])', async () => {
55 const original = globalThis.fetch
56 globalThis.fetch = ((url: string) => {
57 const href = String(url)
58 if (href.endsWith('/docs/models.md')) {
59 return Promise.resolve(new Response(fixture('cohere-models.md.txt')))
60 }
61 if (href.endsWith('aya-expanse.md')) {
62 return Promise.resolve(new Response('| `c4ai-aya-expanse-32b` |'))
63 }
64 if (href.includes('docs.cohere.com')) {
65 return Promise.resolve(
66 new Response(
67 `Cohere's reasoning models are *hybrid*, meaning reasoning can be enabled (in which case they think) or disabled (in which case they do not).\nmodel="command-a-reasoning-08-2025"\n"type": "disabled" turns off thinking\ntoken_budget: 500\n`,
68 ),
69 )
70 }
71 if (href === 'https://cohere.com/pricing') {
72 return Promise.resolve(new Response(fixture('cohere-pricing.html.txt')))
73 }
74 if (href.includes('page_token=page-2')) {
75 return Promise.resolve(
76 Response.json({
77 models: [
78 {
79 name: 'embed-v4.0',
80 endpoints: ['embed'],
81 context_length: 128000,
82 features: null,
83 },
84 ],
85 }),
86 )
87 }
88 // Rows as `GET /v1/models` returned them on 2026-10-07.
89 return Promise.resolve(
90 Response.json({
91 models: [
92 {
93 name: 'command-r7b-12-2024',
94 endpoints: ['generate', 'chat'],
95 context_length: 132000,
96 features: [
97 'logprobs',
98 'json_mode',
99 'json_schema',
100 'strict_tools',
101 'safety_modes',
102 'tools',
103 'tool_choice',
104 'citations',
105 ],
106 },
107 {
108 name: 'north-mini-code-1-0',
109 endpoints: ['generate', 'chat'],
110 context_length: 436000,
111 features: [
112 'logprobs',
113 'strict_tools',
114 'safety_modes',
115 'tools',
116 'reasoning',
117 ],
118 },
119 {
120 name: 'c4ai-aya-vision-32b',
121 endpoints: ['chat'],
122 context_length: 16384,
123 features: ['logprobs', 'vision', 'citations'],
124 },
125 {
126 name: 'c4ai-aya-expanse-32b',
127 endpoints: ['generate', 'chat'],
128 context_length: 128000,
129 features: null,
130 },
131 {
132 name: 'command-r7b-arabic-02-2025',
133 endpoints: ['generate', 'chat'],
134 context_length: 128000,
135 features: ['logprobs', 'tools', 'tool_choice'],
136 },
137 ],
138 next_page_token: 'page-2',
139 }),
140 )
141 }) as typeof fetch
142 try {
143 const result = await provider.listModels({
144 COHERE_API_KEY: 'test-key',
145 })
146 expect(result.skipped).toBeUndefined()
147 const byId = new Map(result.models.map((m) => [m.rawId, m]))
148​
149 const r7b = byId.get('command-r7b-12-2024')
150 expect(r7b).toMatchObject({
151 activity: 'chat',
152 contextWindow: 132000,
153 maxOutput: 4000,
154 modalities: { input: ['text'], output: ['text'] },
155 capabilities: [
156 'tools',
157 'tool_choice',
158 'structured_outputs',
159 'response_format',
160 'logprobs',
161 ],
162 exactCapabilities: true,
163 schemaEndpointId: 'v2/chat',
164 })
165 expect(r7b?.reasoning).toBeUndefined()
166 expect(r7b?.pricing).toMatchObject({
167 tables: {
168 rate: { base: { input_tokens: 3.75e-8, output_tokens: 1.5e-7 } },
169 },
170 })
171 expect(r7b?.factSources).toMatchObject({
172 maxOutput: {
173 derivation: 'docs-derived',
174 sourceUrl: 'https://docs.cohere.com/docs/models.md',
175 },
176 modalities: { sourceUrl: 'https://docs.cohere.com/docs/models.md' },
177 pricing: { sourceUrl: 'https://cohere.com/pricing' },
178 })
179​
180 // The guide names one model; the listing's flag carries the rest.
181 const north = byId.get('north-mini-code-1-0')
182 expect(north?.reasoning).toEqual({ mode: 'budget', mandatory: false })
183 expect(north?.maxOutput).toBe(64000)
184 expect(north?.pricing).toBeUndefined()
185​
186 const vision = byId.get('c4ai-aya-vision-32b')
187 expect(vision?.capabilities).toEqual(['logprobs'])
188 expect(vision?.modalities).toEqual({
189 input: ['text', 'image'],
190 output: ['text'],
191 })
192​
193 // No `features`: flags stay unknown, and the shared body adds none.
194 const expanse = byId.get('c4ai-aya-expanse-32b')
195 expect(expanse?.capabilities).toBeUndefined()
196 expect(expanse?.exactCapabilities).toBe(true)
197 expect(expanse?.maxOutput).toBe(4000)
198 expect(expanse?.pricing).toMatchObject({
199 tables: { rate: { base: { input_tokens: 5e-7 } } },
200 })
201​
202 // Absent from the models page: listing facts only, no output cap.
203 const arabic = byId.get('command-r7b-arabic-02-2025')
204 expect(arabic?.maxOutput).toBeUndefined()
205 expect(arabic?.modalities).toEqual({ input: ['text'], output: ['text'] })
206 expect(arabic?.factSources).toBeUndefined()
207 expect(arabic?.pricing).toBeUndefined()
208​
209 expect(byId.get('embed-v4.0')).toEqual({
210 rawId: 'embed-v4.0',
211 contextWindow: 128000,
212 activity: 'embeddings',
213 deprecated: false,
214 })
215 } finally {
216 globalThis.fetch = original
217 }
218 })
219})
220​
221describe('cohere fetchSpec', () => {
222 it('loads the public YAML without a key', async () => {
223 const original = globalThis.fetch
224 const specYaml = 'openapi: "3.1.0"\ninfo:\n title: Cohere\npaths: {}\n'
225 globalThis.fetch = ((url: string) => {
226 expect(String(url)).toBe(
227 'https://raw.githubusercontent.com/cohere-ai/cohere-developer-experience/main/cohere-openapi.yaml',
228 )
229 return Promise.resolve(new Response(specYaml))
230 }) as typeof fetch
231 try {
232 const result = await provider.fetchSpec({})
233 expect(result.outputStrategy).toBe('post-200')
234 expect(result.specs).toHaveLength(1)
235 expect(result.specs[0]?.info?.title).toBe('Cohere')
236 expect(result.sources[0]?.url).toBe(
237 'https://raw.githubusercontent.com/cohere-ai/cohere-developer-experience/main/cohere-openapi.yaml',
238 )
239 expect(result.sources[0]?.hash).toMatch(/^[0-9a-f]{64}$/)
240 } finally {
241 globalThis.fetch = original
242 }
243 })
244})