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

nvidia-native-reasoning-discovery.test.ts

303 lines10.8 KB

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