Open History to see the last commit
1import { describe, expect, it } from 'vitest'
2
3import { mistralModelPage, tip } from '../fixtures/mistral-model-page.ts'
4import { provider } from './mistral.ts'
5
6describe('mistral classify', () => {
7 it('maps generation paths to the claimed activities', () => {
8 expect(provider.classify('/v1/chat/completions', {})).toBe('chat')
9 expect(provider.classify('/v1/fim/completions', {})).toBe('chat')
10 expect(provider.classify('/v1/agents/completions', {})).toBe('chat')
11 expect(provider.classify('/v1/embeddings', {})).toBe('embeddings')
12 expect(provider.classify('/v1/moderations', {})).toBe('moderation')
13 expect(provider.classify('/v1/chat/moderations', {})).toBe('moderation')
14 expect(provider.classify('/v1/audio/transcriptions', {})).toBe('audio')
15 expect(provider.classify('/v1/audio/transcriptions#stream', {})).toBe(
16 'audio',
17 )
18 expect(provider.classify('/v1/audio/speech', {})).toBe('audio')
19 })
20
21 it('drops platform, admin, files, fine-tune, batch, and classifiers that are not moderation', () => {
22 expect(provider.classify('/v1/files', {})).toBeNull()
23 expect(
24 provider.classify('/v1/fine_tuning/models/{model_id}', {}),
25 ).toBeNull()
26 expect(provider.classify('/v1/batch/jobs', {})).toBeNull()
27 expect(provider.classify('/v1/admin/users', {})).toBeNull()
28 expect(provider.classify('/v1/agents', {})).toBeNull()
29 expect(provider.classify('/v1/conversations', {})).toBeNull()
30 expect(provider.classify('/v1/ocr', {})).toBeNull()
31 expect(provider.classify('/v1/classifications', {})).toBeNull()
32 expect(provider.classify('/v1/chat/classifications', {})).toBeNull()
33 expect(provider.classify('/v1/models', {})).toBeNull()
34 })
35})
36
37describe('mistral listModels', () => {
38 it('skips with an empty catalog when MISTRAL_API_KEY is absent', async () => {
39 const { models, skipped } = await provider.listModels({})
40 expect(models).toEqual([])
41 expect(skipped).toBe('mistral: MISTRAL_API_KEY not set — skipped')
42 })
43
44 it('maps live rows and sends the bearer key', async () => {
45 const original = globalThis.fetch
46 const calls: Array<{ url: string; auth: string | null }> = []
47 globalThis.fetch = ((url: string, init?: RequestInit) => {
48 const href = String(url)
49 calls.push({
50 url: href,
51 auth: new Headers(init?.headers).get('authorization'),
52 })
53 if (!href.startsWith('https://api.mistral.ai/')) {
54 return original(url, init)
55 }
56 return Promise.resolve(
57 new Response(
58 JSON.stringify({
59 object: 'list',
60 data: [
61 { id: 'mistral-small-latest', created: 1_700_000_000 },
62 { id: 'mistral-embed' },
63 ],
64 }),
65 ),
66 )
67 }) as typeof fetch
68 try {
69 const { models, skipped } = await provider.listModels({
70 MISTRAL_API_KEY: 'mistral-test',
71 })
72 expect(skipped).toBeUndefined()
73 expect(calls[0]?.url).toBe('https://api.mistral.ai/v1/models')
74 expect(calls[0]?.auth).toBe('Bearer mistral-test')
75 expect(
76 models.map(
77 ({
78 pricing: _pricing,
79 factSources: _sources,
80 reasoning: _reasoning,
81 modalities: _modalities,
82 serverTools: _tools,
83 maxOutput: _maxOutput,
84 requestMap: _requestMap,
85 absent: _absent,
86 ...rest
87 }) => rest,
88 ),
89 ).toEqual([
90 {
91 rawId: 'mistral-small-latest',
92 releasedAt: 1_700_000_000,
93 activity: null,
94 },
95 { rawId: 'mistral-embed', releasedAt: null, activity: 'embeddings' },
96 ])
97 } finally {
98 globalThis.fetch = original
99 }
100 })
101})
102
103describe('mistral listModels modalities', () => {
104 it('puts the modalities a model page states on the rows it names', async () => {
105 const pages: Record<string, string> = {
106 'https://api.mistral.ai/v1/models': JSON.stringify({
107 data: [
108 {
109 id: 'mistral-small-latest',
110 capabilities: { completion_chat: true },
111 aliases: ['mistral-vibe-cli-fast'],
112 },
113 {
114 id: 'mistral-vibe-cli-fast',
115 capabilities: { completion_chat: true },
116 aliases: ['mistral-small-latest'],
117 },
118 { id: 'mistral-unpriced', capabilities: { completion_chat: true } },
119 ],
120 }),
121 'https://docs.mistral.ai/inference/pricing': `<h2>Flagship models</h2>
122<p>Prices /M Tokens</p>
123<table>
124<tr><td><a href="/models/mistral-small-4-0-26-03">Small</a></td><td>$0.1</td><td>—</td><td>$0.3</td></tr>
125</table>`,
126 'https://docs.mistral.ai/resources/changelogs': '',
127 'https://docs.mistral.ai/models/mistral-small-4-0-26-03':
128 mistralModelPage(
129 ['mistral-small-2603', 'mistral-small-latest'],
130 [
131 [
132 tip('Text input'),
133 tip('Image input'),
134 tip('Reasoning output'),
135 tip('Text output'),
136 ],
137 ],
138 ),
139 'https://docs.mistral.ai/studio/conversations/reasoning.md':
140 '- `mistral-small-latest`: Supports adjustable reasoning via the `reasoning_effort` parameter.',
141 'https://docs.mistral.ai/models':
142 '<a href="/models/not-a-listed-id">x</a>',
143 'https://docs.mistral.ai/openapi.yaml': `components:
144 schemas:
145 ChatCompletionRequest:
146 additionalProperties: false
147 properties:
148 max_tokens: { type: integer }
149 reasoning_effort: { type: string }
150 messages:
151 items:
152 discriminator:
153 propertyName: role
154 mapping:
155 system: '#/components/schemas/SystemMessage'
156 user: '#/components/schemas/UserMessage'
157 assistant: '#/components/schemas/AssistantMessage'
158 tool: '#/components/schemas/ToolMessage'
159`,
160 }
161 const original = globalThis.fetch
162 globalThis.fetch = ((url: string) => {
163 const body = pages[String(url)]
164 return Promise.resolve(
165 body === undefined
166 ? new Response('not found', { status: 404 })
167 : new Response(body),
168 )
169 }) as typeof fetch
170 try {
171 const { models } = await provider.listModels({ MISTRAL_API_KEY: 'k' })
172 const [small, vibe, unpriced] = models
173 expect(small?.modalities).toEqual({
174 input: ['text', 'image'],
175 output: ['text'],
176 })
177 expect(small?.factSources?.modalities).toMatchObject({
178 derivation: 'docs-derived',
179 sourceUrl: 'https://docs.mistral.ai/models/mistral-small-4-0-26-03',
180 path: 'modalities',
181 })
182 expect(small?.pricing).toBeTruthy()
183 expect(small?.requestMap).toMatchObject({
184 maxTokensField: 'max_tokens',
185 developerRole: false,
186 reasoningEffort: true,
187 thinking: { on: { reasoning_effort: 'high' }, off: null, levels: null },
188 })
189 expect(small?.factSources?.requestMap).toMatchObject({
190 derivation: 'docs-derived',
191 sourceUrl: 'https://docs.mistral.ai/openapi.yaml',
192 })
193 // The page names only small-latest. The listing says vibe is that model.
194 expect(vibe?.modalities).toEqual(small?.modalities)
195 expect(vibe?.pricing).toEqual(small?.pricing)
196 expect(vibe?.factSources?.modalities?.sourceUrl).toBe(
197 'https://docs.mistral.ai/models/mistral-small-4-0-26-03',
198 )
199 // No page names this id: unknown, not a guess, and not a cleared price.
200 expect(unpriced?.modalities).toBeUndefined()
201 expect(unpriced?.factSources?.modalities).toBeUndefined()
202 expect(unpriced?.absent).toBeUndefined()
203 } finally {
204 globalThis.fetch = original
205 }
206 })
207})
208
209describe('mistral fetchSpec', () => {
210 it('loads the public OpenAPI document without a key', async () => {
211 const fixture = JSON.stringify({
212 openapi: '3.1.0',
213 info: { title: 'Mistral AI API', version: '1.0.0' },
214 paths: {
215 '/v1/chat/completions': { post: { summary: 'Chat Completion' } },
216 },
217 })
218 const original = globalThis.fetch
219 const urls: Array<string> = []
220 globalThis.fetch = ((url: string) => {
221 urls.push(String(url))
222 return Promise.resolve(new Response(fixture))
223 }) as typeof fetch
224 try {
225 const fetched = await provider.fetchSpec({})
226 expect(urls).toEqual(['https://docs.mistral.ai/openapi.yaml'])
227 expect(fetched.outputStrategy).toBe('post-200')
228 expect(fetched.specs).toHaveLength(1)
229 expect(fetched.specs[0]?.info?.title).toBe('Mistral AI API')
230 expect(fetched.sources[0]?.url).toBe(
231 'https://docs.mistral.ai/openapi.yaml',
232 )
233 expect(fetched.sources[0]?.hash).toMatch(/^[0-9a-f]{64}$/)
234 } finally {
235 globalThis.fetch = original
236 }
237 })
238})
239
240describe('mistral provider metadata', () => {
241 it('exports the seed fields required for auto-registration', () => {
242 expect(provider.id).toBe('mistral')
243 expect(provider.displayName).toBe('Mistral')
244 expect(provider.authEnvVar).toBe('MISTRAL_API_KEY')
245 expect(provider.defaultDerivation).toBe('upstream-spec')
246 expect(provider.specSourceUrl).toBe('https://docs.mistral.ai/openapi.yaml')
247 expect(provider.modelsEndpoint).toBe('https://api.mistral.ai/v1/models')
248 })
249})