Open History to see the last commit
1import { afterEach, describe, expect, it } from 'vitest'
2import fixtures from '../fixtures/meta-docs.json'
3import { provider } from './meta.ts'
4import { classifyAndBundle } from '../../ingest/sync.ts'
5import {
6 META_MODELS,
7 META_PRICING,
8 META_REFERENCE,
9 parseMetaModels,
10 parseMetaPricing,
11 parseMetaSchemas,
12} from '../meta-docs.ts'
13
14const originalFetch = globalThis.fetch
15const docs = fixtures as Record<string, string>
16function serve(overrides: Record<string, string> = {}) {
17 const urls: Array<string> = []
18 globalThis.fetch = async (input) => {
19 const url = String(input)
20 urls.push(url)
21 const body = overrides[url] ?? docs[url]
22 if (body === undefined) throw new Error(`unexpected source ${url}`)
23 return new Response(body)
24 }
25 return urls
26}
27afterEach(() => {
28 globalThis.fetch = originalFetch
29})
30describe('Meta native docs', () => {
31 it('refreshes all nine hosted IDs and their own modes without a key', async () => {
32 const urls = serve()
33 const { models, skipped } = await provider.listModels({})
34 expect(skipped).toBeUndefined()
35 expect(models.map((m) => m.rawId)).toEqual([
36 'muse-wonder-1.0',
37 'muse-spark-1.3',
38 'muse-spark-1.3-contributor',
39 'muse-spark-1.2',
40 'muse-spark-1.2-contributor',
41 'muse-spark-1.1',
42 'muse-image-1.0',
43 'muse-voice-transcribe-1.0',
44 'sam-3.1',
45 ])
46 const spark = models.find((m) => m.rawId === 'muse-spark-1.3')
47 const contributor = models.find(
48 (m) => m.rawId === 'muse-spark-1.3-contributor',
49 )
50 const wonder = models.find((m) => m.rawId === 'muse-wonder-1.0')
51 expect(spark).toMatchObject({
52 displayName: 'Muse Spark 1.3',
53 activity: 'chat',
54 modalities: {
55 input: ['text', 'image', 'video', 'audio', 'pdf'],
56 output: ['text'],
57 },
58 contextWindow: 1048576,
59 maxOutput: null,
60 capabilities: ['tools', 'structured_outputs', 'reasoning'],
61 providerMetadata: { family: 'Muse Spark', tier: 'Standard' },
62 reasoning: {
63 mandatory: true,
64 efforts: ['minimal', 'low', 'medium', 'high', 'xhigh', 'max'],
65 },
66 })
67 expect(contributor?.providerMetadata).toEqual({
68 family: 'Muse Spark',
69 tier: 'Contributor',
70 })
71 expect(contributor?.reasoning?.efforts).not.toContain('max')
72 expect(
73 models.find((m) => m.rawId === 'muse-spark-1.2')?.reasoning?.efforts,
74 ).not.toContain('max')
75 expect(wonder).toMatchObject({
76 displayName: 'Muse Wonder',
77 activity: 'chat',
78 contextWindow: 1048576,
79 providerMetadata: { family: 'Muse Wonder', tier: 'Standard' },
80 reasoning: {
81 mode: 'effort',
82 mandatory: true,
83 efforts: ['minimal', 'low', 'medium', 'high', 'xhigh', 'max'],
84 },
85 })
86 expect(wonder?.pricing).toMatchObject({
87 tables: {
88 rate: {
89 base: {
90 input_tokens: 4 / 1e6,
91 output_tokens: 20 / 1e6,
92 cache_read_tokens: 0.2 / 1e6,
93 },
94 },
95 },
96 })
97 expect(spark?.pricing).toMatchObject({
98 tables: {
99 rate: {
100 base: {
101 input_tokens: 1.25 / 1e6,
102 output_tokens: 4.25 / 1e6,
103 cache_read_tokens: 0.15 / 1e6,
104 },
105 },
106 },
107 })
108 expect(contributor?.pricing).toMatchObject({
109 tables: {
110 rate: {
111 base: {
112 input_tokens: 0.1 / 1e6,
113 output_tokens: 0.2 / 1e6,
114 cache_read_tokens: 0.002 / 1e6,
115 },
116 },
117 },
118 })
119 expect(models.find((m) => m.rawId === 'muse-spark-1.1')?.pricing).toEqual(
120 spark?.pricing,
121 )
122 expect(
123 models
124 .filter((m) => m.activity !== 'chat')
125 .map((m) => [m.rawId, m.displayName]),
126 ).toEqual([
127 ['muse-image-1.0', 'Muse Image'],
128 ['muse-voice-transcribe-1.0', 'Muse Voice Transcribe'],
129 ['sam-3.1', 'Segment Anything Model'],
130 ])
131 expect(
132 models.find((m) => m.rawId === 'muse-image-1.0')?.pricing,
133 ).toMatchObject({ tables: { rate: { base: { generated_images: 0.01 } } } })
134 expect(
135 models.find((m) => m.rawId === 'muse-voice-transcribe-1.0')?.pricing,
136 ).toMatchObject({
137 tables: { rate: { base: { audio_seconds: 0.18 / 3600 } } },
138 })
139 expect(models.find((m) => m.rawId === 'sam-3.1')?.pricing).toMatchObject({
140 tables: {
141 rate: {
142 base: { segmented_images: 2.5 / 1000, video_frames: 0.2 / 1000 },
143 },
144 },
145 })
146 expect(
147 urls.every((url) => url.startsWith('https://dev.meta.ai/docs/')),
148 ).toBe(true)
149 })
150 it('reads native chat, image and ASR schema resources and POST references', async () => {
151 const urls = serve()
152 const fetched = await provider.fetchSpec({})
153 expect(fetched.skipped).toBeUndefined()
154 expect(fetched.specs).toHaveLength(6)
155 expect(fetched.sources).toHaveLength(6)
156 const bundled = classifyAndBundle(provider, fetched)
157 expect(bundled.warnings).toEqual([])
158 expect(bundled.endpoints).toHaveLength(6)
159 expect(
160 bundled.endpoints.every((endpoint) => endpoint.input && endpoint.output),
161 ).toBe(true)
162 expect(fetched.specs.map((s) => Object.keys(s.paths ?? {}))).toEqual([
163 ['/v1/chat/completions'],
164 ['/v1/responses'],
165 ['/v1/messages'],
166 ['/v1/images/generations'],
167 ['/v1/images/edits'],
168 ['/v1/asr/transcribe'],
169 ])
170 expect(
171 urls.every((url) => url.startsWith('https://dev.meta.ai/docs/')),
172 ).toBe(true)
173 })
174 it('preserves nested array fields, explicit required markers, enums and constraints', () => {
175 const schemas = parseMetaSchemas(
176 docs[`${META_REFERENCE}chat-completions/schemas`] ?? '',
177 )
178 expect(schemas['create-chat-completion-request']).toMatchObject({
179 required: ['messages', 'model'],
180 properties: {
181 messages: { type: 'array', minItems: 1 },
182 frequency_penalty: { minimum: -2, maximum: 2 },
183 },
184 })
185 expect(schemas['create-chat-completion-response']).toMatchObject({
186 properties: {
187 choices: {
188 items: {
189 properties: {
190 finish_reason: {
191 enum: [
192 'stop',
193 'length',
194 'tool_calls',
195 'content_filter',
196 'function_call',
197 ],
198 },
199 },
200 },
201 },
202 },
203 })
204 const response = parseMetaSchemas(
205 docs[`${META_REFERENCE}responses/schemas`] ?? '',
206 )
207 expect(
208 Object.values(response).some((schema) =>
209 JSON.stringify(schema).includes('"x-source-type":null'),
210 ),
211 ).toBe(true)
212 })
213 it('preserves native multipart content and marks ASR unpublished fields unknown', async () => {
214 serve()
215 const fetched = await provider.fetchSpec({})
216 const edits = fetched.specs[4]?.paths?.['/v1/images/edits']?.post
217 expect(edits?.requestBody).toMatchObject({
218 content: {
219 'application/json': {
220 schema: { $ref: '#/components/schemas/edit-image-body-json-param' },
221 },
222 'multipart/form-data': {
223 schema: { $ref: '#/components/schemas/create-image-edit-request' },
224 },
225 },
226 })
227 const asr = fetched.specs[5]?.paths?.['/v1/asr/transcribe']?.post
228 expect(asr?.requestBody).toMatchObject({
229 content: {
230 'multipart/form-data': {
231 schema: { type: 'object', 'x-source-fields': null },
232 },
233 },
234 })
235 expect(
236 provider.generationEndpointId?.({
237 activity: 'audio',
238 rawId: 'muse-voice-transcribe-1.0',
239 }),
240 ).toBe('v1/asr/transcribe')
241 })
242
243 it('fails an empty source instead of removing stored rows', async () => {
244 serve({ [META_MODELS]: '<h1>Models</h1>' })
245 await expect(provider.listModels({})).rejects.toThrow(
246 'model table listed no models',
247 )
248 })
249 it('leaves pricing null when the provider publishes no quote for a newly listed model', () => {
250 const models = parseMetaModels(docs[META_MODELS] ?? '')
251 models.push({
252 rawId: 'synthetic-unsourced',
253 providerMetadata: { family: 'Synthetic' },
254 })
255 const prices = parseMetaPricing(docs[META_PRICING] ?? '', models, {
256 url: META_PRICING,
257 hash: 'fixture',
258 extractedAt: '2026-10-09',
259 })
260 expect(prices.has('synthetic-unsourced')).toBe(false)
261 })
262 it('fails unknown types and structurally missing schema cells', () => {
263 const source = docs[`${META_REFERENCE}chat-completions/schemas`] ?? ''
264 expect(() =>
265 parseMetaSchemas(
266 source.replace('integer (unixtime)', 'undocumented-primitive'),
267 ),
268 ).toThrow('unknown schema type')
269 expect(() =>
270 parseMetaSchemas(source.replace(/<td\b[^>]*>[\s\S]*?<\/td>/, '')),
271 ).toThrow('schema field row changed')
272 })
273 it('rejects malformed published quotes rather than filling old amounts', () => {
274 const models = parseMetaModels(docs[META_MODELS] ?? '')
275 expect(() =>
276 parseMetaPricing(
277 (docs[META_PRICING] ?? '').replace('$1.25', '$unknown'),
278 models,
279 { url: META_PRICING, hash: 'fixture', extractedAt: '2026-10-09' },
280 ),
281 ).toThrow('unreadable token price')
282 expect(() =>
283 parseMetaPricing(
284 (docs[META_PRICING] ?? '').replace('$0.01 per image', '$0.01'),
285 models,
286 { url: META_PRICING, hash: 'fixture', extractedAt: '2026-10-12' },
287 ),
288 ).toThrow('unreadable image price')
289 })
290 it('fails a models page whose chat tables it cannot read instead of dropping them', async () => {
291 const page = docs[META_MODELS] ?? ''
292 for (const [html, error] of [
293 [
294 page.replace(
295 '<th>Variant</th><th>Model API (pay-as-you-go)</th>',
296 '<th>Variant</th><th>Model API</th>',
297 ),
298 'model availability headers changed',
299 ],
300 [
301 page.replace('<th>Variant</th>', '<th>Edition</th>'),
302 'chat section listed no models',
303 ],
304 [
305 page
306 .replace('<th>Variant</th>', '<th>Edition</th>')
307 .replace('<th>Version</th>', '<th>Release</th>')
308 .replaceAll('-token context window', ' token window'),
309 'model page listed no chat models',
310 ],
311 [
312 page.replace('All take text, image', 'All accept text, image'),
313 'missing model modalities',
314 ],
315 [
316 page.replace(
317 'and have a 1,048,576-token context window',
318 'and have a 1,048,576-token context window (1.1: a 262,144-token context window)',
319 ),
320 'unreadable model context',
321 ],
322 [
323 page.replace('<td>Contributor mode</td>', '<td>Discounted mode</td>'),
324 'unreadable model availability',
325 ],
326 ] as const) {
327 serve({ [META_MODELS]: html })
328 await expect(provider.listModels({})).rejects.toThrow(error)
329 }
330 })
331 it('reads every output modality the chat prose states', () => {
332 const models = parseMetaModels(
333 (docs[META_MODELS] ?? '').replace(
334 'returns text, has a',
335 'returns text and images, has a',
336 ),
337 )
338 expect(
339 models.find((m) => m.rawId === 'muse-wonder-1.0')?.modalities,
340 ).toEqual({
341 input: ['text', 'image', 'video', 'audio', 'pdf'],
342 output: ['text', 'images'],
343 })
344 })
345 it('fails token prices whose mode is unstated, ambiguous or contradicts the model tier', () => {
346 const models = parseMetaModels(docs[META_MODELS] ?? '')
347 const page = docs[META_PRICING] ?? ''
348 for (const [html, error] of [
349 [
350 page.replace(' in Contributor mode', ' as variants'),
351 'token price mode unstated',
352 ],
353 [
354 page.replace(
355 ' in Contributor mode',
356 ' in Contributor mode or in Standard mode',
357 ),
358 'ambiguous token price mode',
359 ],
360 [
361 page.replace(' in Contributor mode', ' in Standard mode'),
362 'token price mode disagrees with model tier',
363 ],
364 [
365 page.replace(
366 'and <code>muse-spark-1.1</code> in Standard mode',
367 'and <code>muse-spark-1.1</code> in Standard mode; <code>muse-spark-1.1</code> in Standard mode',
368 ),
369 'duplicate model price',
370 ],
371 [page.replace('$0.10', 'Not available'), 'incomplete token pricing'],
372 ] as const)
373 expect(() =>
374 parseMetaPricing(html, models, {
375 url: META_PRICING,
376 hash: 'fixture',
377 extractedAt: '2026-10-12',
378 }),
379 ).toThrow(error)
380 })
381 it('fails a family price table it does not recognise instead of clearing the price', () => {
382 const models = parseMetaModels(docs[META_MODELS] ?? '')
383 const page = docs[META_PRICING] ?? ''
384 const wonder = page.indexOf('<p>Model: <code>muse-wonder-1.0</code>')
385 expect(() =>
386 parseMetaPricing(
387 page.slice(0, wonder) +
388 page
389 .slice(wonder)
390 .replace(
391 '<th>Standard mode (per 1M tokens)</th>',
392 '<th>Standard (per 1M tokens)</th>',
393 ),
394 models,
395 { url: META_PRICING, hash: 'fixture', extractedAt: '2026-10-12' },
396 ),
397 ).toThrow('unknown pricing table')
398 })
399 it('fails when a pay-as-you-go chat ID gets no token price', () => {
400 const models = parseMetaModels(docs[META_MODELS] ?? '')
401 expect(() =>
402 parseMetaPricing(
403 (docs[META_PRICING] ?? '').replace(
404 /(<p>Model: <code>muse-wonder-1\.0<\/code>[\s\S]*?)<th>Usage<\/th>/,
405 '$1<th>Meter</th>',
406 ),
407 models,
408 { url: META_PRICING, hash: 'fixture', extractedAt: '2026-10-12' },
409 ),
410 ).toThrow('token-priced model has no price')
411 })
412 it('fails a reasoning effort restriction it cannot read', async () => {
413 const page = docs['https://dev.meta.ai/docs/reasoning'] ?? ''
414 for (const cell of [
415 'Standard mode only.',
416 'Supported by Muse Wonder and Muse Spark 1.3.',
417 'Requires <code>muse-wonder-1.0</code>.',
418 'Supported by <code>muse-wonder-1.0</code>; not available with Contributor-mode variants.',
419 ]) {
420 serve({
421 'https://dev.meta.ai/docs/reasoning': page.replace(
422 /Supported by <code>[\s\S]*?<\/td>/,
423 `${cell}</td>`,
424 ),
425 })
426 await expect(provider.listModels({})).rejects.toThrow(
427 'unreadable reasoning effort restriction',
428 )
429 }
430 })
431 it('leaves reasoning unset for a chat family the page does not say always reasons', async () => {
432 serve({
433 'https://dev.meta.ai/docs/reasoning': (
434 docs['https://dev.meta.ai/docs/reasoning'] ?? ''
435 ).replace(
436 'which Muse Wonder and Muse Spark do not support',
437 'which Muse Spark does not support',
438 ),
439 })
440 const { models } = await provider.listModels({})
441 expect(
442 models.find((m) => m.rawId === 'muse-wonder-1.0')?.reasoning,
443 ).toBeNull()
444 expect(
445 models.find((m) => m.rawId === 'muse-spark-1.1')?.reasoning,
446 ).toMatchObject({ mode: 'effort', mandatory: true })
447 })
448})