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

meta.test.ts

217 lines7.1 KB

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
27afterEach(() => {
28 globalThis.fetch = originalFetch
29})
30describe('Meta native docs', () => {
31 it('refreshes all eight hosted IDs and their own tiers without a key', async () => {
32 const urls = serve()
33 const { models, skipped } = await provider.listModels({})
34 expect(skipped).toBeUndefined()
35 expect(models).toHaveLength(8)
36 const spark = models.find((m) => m.rawId === 'muse-spark-1.3')
37 const contributor = models.find(
38 (m) => m.rawId === 'muse-spark-1.3-contributor',
39 )
40 expect(spark).toMatchObject({
41 contextWindow: 1048576,
42 maxOutput: null,
43 reasoning: {
44 mandatory: true,
45 efforts: ['minimal', 'low', 'medium', 'high', 'xhigh', 'max'],
46 },
47 })
48 expect(contributor?.reasoning?.efforts).not.toContain('max')
49 expect(spark?.pricing).toMatchObject({
50 tables: {
51 rate: {
52 base: {
53 input_tokens: 1.25 / 1e6,
54 output_tokens: 4.25 / 1e6,
55 cache_read_tokens: 0.15 / 1e6,
56 },
57 },
58 },
59 })
60 expect(contributor?.pricing).toMatchObject({
61 tables: {
62 rate: { base: { input_tokens: 0.1 / 1e6, output_tokens: 0.2 / 1e6 } },
63 },
64 })
65 expect(
66 models.find((m) => m.rawId === 'muse-image-1.0')?.pricing,
67 ).toMatchObject({ tables: { rate: { base: { generated_images: 0.01 } } } })
68 expect(
69 models.find((m) => m.rawId === 'muse-voice-transcribe-1.0')?.pricing,
70 ).toMatchObject({
71 tables: { rate: { base: { audio_seconds: 0.18 / 3600 } } },
72 })
73 expect(models.find((m) => m.rawId === 'sam-3.1')?.pricing).toMatchObject({
74 tables: {
75 rate: {
76 base: { segmented_images: 2.5 / 1000, video_frames: 0.2 / 1000 },
77 },
78 },
79 })
80 expect(
81 urls.every((url) => url.startsWith('https://dev.meta.ai/docs/')),
82 ).toBe(true)
83 })
84 it('reads native chat, image and ASR schema resources and POST references', async () => {
85 const urls = serve()
86 const fetched = await provider.fetchSpec({})
87 expect(fetched.skipped).toBeUndefined()
88 expect(fetched.specs).toHaveLength(6)
89 expect(fetched.sources).toHaveLength(6)
90 const bundled = classifyAndBundle(provider, fetched)
91 expect(bundled.warnings).toEqual([])
92 expect(bundled.endpoints).toHaveLength(6)
93 expect(
94 bundled.endpoints.every((endpoint) => endpoint.input && endpoint.output),
95 ).toBe(true)
96 expect(fetched.specs.map((s) => Object.keys(s.paths ?? {}))).toEqual([
97 ['/v1/chat/completions'],
98 ['/v1/responses'],
99 ['/v1/messages'],
100 ['/v1/images/generations'],
101 ['/v1/images/edits'],
102 ['/v1/asr/transcribe'],
103 ])
104 expect(
105 urls.every((url) => url.startsWith('https://dev.meta.ai/docs/')),
106 ).toBe(true)
107 })
108 it('preserves nested array fields, explicit required markers, enums and constraints', () => {
109 const schemas = parseMetaSchemas(
110 docs[`${META_REFERENCE}chat-completions/schemas`] ?? '',
111 )
112 expect(schemas['create-chat-completion-request']).toMatchObject({
113 required: ['messages', 'model'],
114 properties: {
115 messages: { type: 'array', minItems: 1 },
116 frequency_penalty: { minimum: -2, maximum: 2 },
117 },
118 })
119 expect(schemas['create-chat-completion-response']).toMatchObject({
120 properties: {
121 choices: {
122 items: {
123 properties: {
124 finish_reason: {
125 enum: [
126 'stop',
127 'length',
128 'tool_calls',
129 'content_filter',
130 'function_call',
131 ],
132 },
133 },
134 },
135 },
136 },
137 })
138 const response = parseMetaSchemas(
139 docs[`${META_REFERENCE}responses/schemas`] ?? '',
140 )
141 expect(
142 Object.values(response).some((schema) =>
143 JSON.stringify(schema).includes('"x-source-type":null'),
144 ),
145 ).toBe(true)
146 })
147 it('preserves native multipart content and marks ASR unpublished fields unknown', async () => {
148 serve()
149 const fetched = await provider.fetchSpec({})
150 const edits = fetched.specs[4]?.paths?.['/v1/images/edits']?.post
151 expect(edits?.requestBody).toMatchObject({
152 content: {
153 'application/json': {
154 schema: { $ref: '#/components/schemas/edit-image-body-json-param' },
155 },
156 'multipart/form-data': {
157 schema: { $ref: '#/components/schemas/create-image-edit-request' },
158 },
159 },
160 })
161 const asr = fetched.specs[5]?.paths?.['/v1/asr/transcribe']?.post
162 expect(asr?.requestBody).toMatchObject({
163 content: {
164 'multipart/form-data': {
165 schema: { type: 'object', 'x-source-fields': null },
166 },
167 },
168 })
169 expect(
170 provider.generationEndpointId?.({
171 activity: 'audio',
172 rawId: 'muse-voice-transcribe-1.0',
173 }),
174 ).toBe('v1/asr/transcribe')
175 })
176​
177 it('fails an empty source instead of removing stored rows', async () => {
178 serve({ [META_MODELS]: '<h1>Models</h1>' })
179 await expect(provider.listModels({})).rejects.toThrow(
180 'model table listed no models',
181 )
182 })
183 it('leaves pricing null when the provider publishes no quote for a newly listed model', () => {
184 const models = parseMetaModels(docs[META_MODELS] ?? '')
185 models.push({
186 rawId: 'synthetic-unsourced',
187 providerMetadata: { family: 'Synthetic' },
188 })
189 const prices = parseMetaPricing(docs[META_PRICING] ?? '', models, {
190 url: META_PRICING,
191 hash: 'fixture',
192 extractedAt: '2026-10-09',
193 })
194 expect(prices.has('synthetic-unsourced')).toBe(false)
195 })
196 it('fails unknown types and structurally missing schema cells', () => {
197 const source = docs[`${META_REFERENCE}chat-completions/schemas`] ?? ''
198 expect(() =>
199 parseMetaSchemas(
200 source.replace('integer (unixtime)', 'undocumented-primitive'),
201 ),
202 ).toThrow('unknown schema type')
203 expect(() =>
204 parseMetaSchemas(source.replace(/<td\b[^>]*>[\s\S]*?<\/td>/, '')),
205 ).toThrow('schema field row changed')
206 })
207 it('rejects malformed published quotes rather than filling old amounts', () => {
208 const models = parseMetaModels(docs[META_MODELS] ?? '')
209 expect(() =>
210 parseMetaPricing(
211 (docs[META_PRICING] ?? '').replace('$1.25', '$unknown'),
212 models,
213 { url: META_PRICING, hash: 'fixture', extractedAt: '2026-10-09' },
214 ),
215 ).toThrow('unreadable token price')
216 })
217})