Open History to see the last commit
1import { afterEach, expect, it } from 'vitest'
2import docs from './fixtures/nvidia-native-reasoning-discovery.json'
3import absentBuild from './fixtures/nvidia-build-absent-page.json'
4import {
5 parseNvidiaInfer,
6 nvidiaReasoning,
7 nvidiaInferNamesModel,
8 nvidiaStatedModelIds,
9 NVIDIA_REFERENCE_INDEXES,
10} from './nvidia-openapi.ts'
11import {
12 discoverNvidiaSitemap,
13 NVIDIA_SITEMAP_URL,
14 parseNvidiaInferSitemap,
15} from './nvidia-sitemap.ts'
16import { provider, NVIDIA_MODELS_URL } from './adapters/nvidia.ts'
17
18const native = docs as Record<string, string>
19const source = (slug: string) =>
20 'https://docs.api.nvidia.com/nim/reference/' + slug + '-infer.md'
21const originalFetch = globalThis.fetch
22afterEach(() => {
23 globalThis.fetch = originalFetch
24})
25function serve(overrides: Record<string, string> = {}) {
26 globalThis.fetch = async (input) => {
27 const url = String(input)
28 const body =
29 overrides[url] ??
30 native[url] ??
31 (absentBuild.cards as Record<string, string>)[url] ??
32 (url.startsWith('https://build.nvidia.com/') &&
33 !url.endsWith('.md') &&
34 (native[`${url}.md`] ||
35 (absentBuild.cards as Record<string, string>)[`${url}.md`])
36 ? absentBuild.html
37 : undefined)
38 if (
39 body === undefined &&
40 url.startsWith('https://build.nvidia.com/') &&
41 url.endsWith('.md')
42 )
43 return new Response(absentBuild.html, { status: 404 })
44 if (body === undefined) throw new Error('unexpected native source ' + url)
45 return new Response(body)
46 }
47}
48it('accepts native title wrappers only as model IDs and preserves schema identity mismatches', () => {
49 for (const [id, slug, mode] of [
50 ['google/gemma-4-31b-it', 'google-gemma-4-31b-it', 'toggle'],
51 ['google/diffusiongemma-26b-a4b-it', 'diffusiongemma-26b-a4b-it', 'toggle'],
52 ['meta/muse-glimmer-30b', 'meta-muse-glimmer-30b', 'effort'],
53 ] as const) {
54 const parsed = parseNvidiaInfer(native[source(slug)]!)!
55 expect(nvidiaStatedModelIds(parsed.document)).toEqual([id])
56 expect(nvidiaInferNamesModel(id, parsed.document)).toBe(true)
57 expect(parsed.reasoning?.mode).toBe(mode)
58 expect(parsed.reasoning?.mandatory).toBe(mode === 'toggle' ? false : null)
59 }
60 const muse = parseNvidiaInfer(native[source('meta-muse-glimmer-30b')]!)!
61 expect(muse.reasoning?.efforts).toEqual([
62 'none',
63 'minimal',
64 'low',
65 'medium',
66 'high',
67 'max',
68 ])
69 const wrong = parseNvidiaInfer(
70 native[source('nvidia-nemotron-3-5-content-safety')]!,
71 )!
72 expect(
73 nvidiaInferNamesModel('nvidia/nemotron-3.5-content-safety', wrong.document),
74 ).toBe(false)
75 expect(nvidiaStatedModelIds(wrong.document)).toEqual([
76 'nvidia/nemotron-3-nano-omni-30b-a3b-reasoning',
77 ])
78})
79it('does not borrow controls from model prompt protocols or request examples', () => {
80 for (const slug of [
81 'nvidia-deepseek-v4_1-flash',
82 'poolside-laguna-xs-2-1',
83 'nvidia-llama-3_1-nemotron-safety-guard-8b-v3',
84 ])
85 expect(parseNvidiaInfer(native[source(slug)]!)?.reasoning).toBeUndefined()
86})
87it('discovers native schema-owned bindings even when slugs omit or change the namespace', async () => {
88 serve()
89 const ids = [
90 'google/gemma-4-31b-it',
91 'google/diffusiongemma-26b-a4b-it',
92 'meta/muse-glimmer-30b',
93 'deepseek-ai/deepseek-v4.1-flash',
94 'poolside/laguna-xs-2.1',
95 ]
96 const result = await discoverNvidiaSitemap(ids)
97 expect(result.rows.map((row) => row.rawId).sort()).toEqual([...ids].sort())
98 expect(result.failures.failed).toBe(0)
99 expect(result.unavailable).toEqual([])
100 expect(
101 result.rows.find((row) => row.rawId === 'deepseek-ai/deepseek-v4.1-flash')
102 ?.inferUrl,
103 ).toBe(source('nvidia-deepseek-v4_1-flash').slice(0, -3))
104})
105it('reports a mismatched source schema instead of assigning another model data', async () => {
106 serve()
107 const result = await discoverNvidiaSitemap([
108 'nvidia/nemotron-3.5-content-safety',
109 ])
110 expect(result.rows).toEqual([])
111 expect(result.unavailable).toEqual(['nvidia/nemotron-3.5-content-safety'])
112 expect(result.failures.failed).toBe(1)
113 expect(result.failures.first[0]?.error).toContain(
114 'does not identify its candidate model',
115 )
116})
117it('refuses malformed sitemap or schema documents and surfaces source failures', async () => {
118 expect(() =>
119 parseNvidiaInferSitemap(
120 '<urlset><loc>https://docs.api.nvidia.com/nim/reference/model-infer</loc>',
121 ),
122 ).toThrow('urlset')
123 expect(() =>
124 parseNvidiaInferSitemap(
125 '<urlset><loc>https://third-party.test/model-infer</loc></urlset>',
126 ),
127 ).toThrow('no native infer')
128 serve({ [source('google-gemma-4-31b-it')]: 'not a schema' })
129 const result = await discoverNvidiaSitemap(['google/gemma-4-31b-it'])
130 expect(result.rows).toEqual([])
131 expect(result.failures.failed).toBe(1)
132 expect(result.unavailable).toEqual(['google/gemma-4-31b-it'])
133})
134it('uses discovered schema facts and binding for the native production row and fetched spec', async () => {
135 const listing = JSON.parse(native[NVIDIA_MODELS_URL]!) as {
136 data: Array<{ id: string }>
137 }
138 const own = listing.data.filter((row) => row.id === 'google/gemma-4-31b-it')
139 expect(own).toHaveLength(1)
140 serve({
141 [NVIDIA_MODELS_URL]: JSON.stringify({ ...listing, data: own }),
142 ...Object.fromEntries(
143 NVIDIA_REFERENCE_INDEXES.slice(1).map((url) => [
144 url,
145 '---\n---\n# No relevant models\n',
146 ]),
147 ),
148 })
149 const result = await provider.listModels({})
150 expect(result.models[0]?.reasoning).toEqual({
151 mode: 'toggle',
152 mandatory: false,
153 })
154 expect(result.models[0]?.schemaEndpointId).toBe('google/gemma-4-31b-it')
155 expect(result.models[0]?.factSources?.reasoning?.sourceUrl).toBe(
156 source('google-gemma-4-31b-it'),
157 )
158 const fetched = await provider.fetchSpec({})
159 expect(fetched.specs).toEqual([])
160 expect(fetched.bundledEndpoints).toHaveLength(1)
161 expect(fetched.bundledEndpoints?.[0]).toMatchObject({
162 publicId: 'google/gemma-4-31b-it',
163 path: '/chat/completions',
164 })
165})
166
167it('reports an unreadable native discovery source for every affected model', async () => {
168 serve({ [NVIDIA_SITEMAP_URL]: 'not a sitemap' })
169 const result = await discoverNvidiaSitemap(['google/gemma-4-31b-it'])
170 expect(result.rows).toEqual([])
171 expect(result.failures.failed).toBe(1)
172 expect(result.failures.first[0]?.source).toBe(NVIDIA_SITEMAP_URL)
173 expect(result.unavailable).toEqual(['google/gemma-4-31b-it'])
174})
175
176it('keeps enum-only mandatory unknown and requires normative disabling meaning', () => {
177 const parsed = parseNvidiaInfer(native[source('meta-muse-glimmer-30b')]!)!
178 expect(nvidiaReasoning(parsed.document)?.mandatory).toBeNull()
179 const text = JSON.stringify(parsed.document)
180 for (const [note, expected] of [
181 ['none disables reasoning.', false],
182 ['none does not disable reasoning.', null],
183 ['Do not assume that none disables reasoning.', null],
184 ['```\nnone disables reasoning.\n```', null],
185 ] as const) {
186 const modified = text.replace(
187 'How much reasoning the model should do before answering.',
188 JSON.stringify(note).slice(1, -1) +
189 ' How much reasoning the model should do before answering.',
190 )
191 expect(
192 nvidiaReasoning(JSON.parse(modified) as typeof parsed.document)
193 ?.mandatory,
194 ).toBe(expected)
195 }
196})
197it('validates singleton cache bindings on every cache hit', async () => {
198 const id = 'google/gemma-4-31b-it'
199 for (const value of [
200 [id, 'wrong/model'],
201 [id, id],
202 { 0: id },
203 [null],
204 ['other/model'],
205 ] as const) {
206 serve()
207 const kv = {
208 get: async (key: string) =>
209 key.includes('nvidia-sitemap-binding:') ? JSON.stringify(value) : null,
210 put: async () => {},
211 } as unknown as KVNamespace
212 const result = await discoverNvidiaSitemap([id], kv)
213 expect(result.rows).toEqual([])
214 expect(result.failures.failed).toBe(1)
215 expect(result.unavailable).toEqual([id])
216 }
217})
218for (const malformed of [true, false])
219 it(
220 'reports malformed or wrong-model selected reference-index schemas visibly ' +
221 malformed,
222 async () => {
223 const id = 'google/gemma-4-31b-it'
224 const url = source('google-gemma-4-31b-it')
225 const all = JSON.parse(native[NVIDIA_MODELS_URL]!) as {
226 data: Array<{ id: string }>
227 }
228 const index =
229 '---\n---\n| Model | Endpoint |\n| --- | --- |\n| [google / gemma-4-31b-it](https://docs.api.nvidia.com/nim/reference/google-gemma-4-31b-it) | [Infer](' +
230 url.slice(0, -3) +
231 ') |\n'
232 serve({
233 [NVIDIA_MODELS_URL]: JSON.stringify({
234 ...all,
235 data: all.data.filter((row) => row.id === id),
236 }),
237 [NVIDIA_REFERENCE_INDEXES[0]!]: index,
238 [url]: malformed
239 ? 'not a schema'
240 : native[source('nvidia-nemotron-3-5-content-safety')]!,
241 ...Object.fromEntries(
242 NVIDIA_REFERENCE_INDEXES.slice(1).map((indexUrl) => [
243 indexUrl,
244 '---\n---\n# No relevant models\n',
245 ]),
246 ),
247 })
248 const result = await provider.listModels({})
249 expect(result.models[0]?.capabilities).toContain('reasoning')
250 expect(result.models[0]?.reasoning).toBeUndefined()
251 expect(result.models[0]?.absent?.reasoning).toBe('unavailable')
252 expect(result.docsFailures?.failed).toBe(1)
253 expect(result.docsFailures?.first[0]?.error).toContain(
254 malformed ? 'no OpenAPI' : 'identity conflict',
255 )
256 if (!malformed)
257 expect(result.docsFailures?.first[0]?.error).toContain(
258 'nvidia/nemotron-3-nano-omni-30b-a3b-reasoning',
259 )
260 await expect(provider.fetchSpec({})).rejects.toThrow(
261 'parsed 0 generation specs',
262 )
263 },
264 )
265
266it('native absent Build pages preserve independent owned contracts without consuming failure budget', async () => {
267 const listing = JSON.parse(native[NVIDIA_MODELS_URL]!) as {
268 data: Array<{ id: string }>
269 }
270 const ids = [
271 'google/gemma-4-31b-it',
272 'google/diffusiongemma-26b-a4b-it',
273 'meta/muse-glimmer-30b',
274 'deepseek-ai/deepseek-v4.1-flash',
275 'poolside/laguna-xs-2.1',
276 ]
277 const data = listing.data.filter((row) => ids.includes(row.id))
278 expect(data).toHaveLength(ids.length)
279 serve({
280 [NVIDIA_MODELS_URL]: JSON.stringify({ ...listing, data }),
281 ...Object.fromEntries(
282 NVIDIA_REFERENCE_INDEXES.slice(1).map((url) => [
283 url,
284 '---\n---\n# No relevant models\n',
285 ]),
286 ),
287 })
288 const result = await provider.listModels({})
289 expect(result.docsFailures).toEqual({ failed: 0, skipped: 0, first: [] })
290 expect(result.models).toHaveLength(ids.length)
291 expect(result.models.every((row) => row.schemaEndpointId === row.rawId)).toBe(
292 true,
293 )
294 expect(
295 result.models.find((row) => row.rawId === 'google/gemma-4-31b-it')
296 ?.reasoning?.mode,
297 ).toBe('toggle')
298 expect(
299 result.models.find((row) => row.rawId === 'meta/muse-glimmer-30b')
300 ?.reasoning?.mode,
301 ).toBe('effort')
302})