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

gemini-list.test.ts

301 lines8.6 KB

1import { readFileSync } from 'node:fs'
2​
3import { afterEach, describe, expect, it, vi } from 'vitest'
4​
5import { GEMINI_MODELS_INDEX_URL } from './gemini-features.ts'
6import { GEMINI_PRICING_URL } from './gemini-pricing.ts'
7import { GEMINI_DISCOVERY_URL } from './gemini-request.ts'
8import { geminiProvider } from './gemini.ts'
9​
10const KEY = 'test-gemini-key-123'
11​
12afterEach(() => {
13 vi.unstubAllGlobals()
14})
15​
16describe('gemini listModels', () => {
17 it('sends the key as a header so a failed fetch never quotes it', async () => {
18 const seen: Array<{ url: string; key: string | null }> = []
19 vi.stubGlobal(
20 'fetch',
21 vi.fn((input: string, init?: RequestInit) => {
22 if (
23 !input.startsWith(
24 'https://generativelanguage.googleapis.com/v1beta/models?',
25 )
26 ) {
27 // Docs loads run alongside the listing; leave them pending.
28 return new Promise<Response>(() => {})
29 }
30 seen.push({
31 url: input,
32 key: new Headers(init?.headers).get('x-goog-api-key'),
33 })
34 return Promise.resolve(
35 new Response('bad', { status: 400, statusText: 'Bad Request' }),
36 )
37 }),
38 )
39​
40 const error = await geminiProvider.listModels({ GEMINI_API_KEY: KEY }).then(
41 () => null,
42 (err: unknown) => err,
43 )
44​
45 expect(error).toBeInstanceOf(Error)
46 expect((error as Error).message).toContain('400 Bad Request')
47 expect((error as Error).message).not.toContain(KEY)
48 expect(seen).toHaveLength(1)
49 expect(seen[0]?.url).not.toContain(KEY)
50 expect(seen[0]?.key).toBe(KEY)
51 })
52​
53 it('writes the listing when the pricing page parses nothing (#567)', async () => {
54 vi.stubGlobal('fetch', (input: string) => {
55 const url = String(input)
56 if (
57 url.startsWith(
58 'https://generativelanguage.googleapis.com/v1beta/models',
59 )
60 ) {
61 return Promise.resolve(
62 new Response(
63 JSON.stringify({
64 models: [
65 {
66 name: 'models/gemini-2.5-flash',
67 displayName: 'Gemini 2.5 Flash',
68 supportedGenerationMethods: ['generateContent'],
69 inputTokenLimit: 1048576,
70 outputTokenLimit: 65536,
71 },
72 ],
73 }),
74 ),
75 )
76 }
77 if (url === GEMINI_DISCOVERY_URL) {
78 return Promise.resolve(
79 new Response(
80 JSON.stringify({
81 schemas: {
82 ThinkingConfig: {
83 properties: { thinkingBudget: { description: 'budget' } },
84 },
85 Content: {
86 properties: {
87 role: {
88 description: "The role must be either 'user' or 'model'.",
89 },
90 },
91 },
92 },
93 }),
94 ),
95 )
96 }
97 if (url === GEMINI_PRICING_URL) {
98 return Promise.resolve(new Response('<html><body>moved</body></html>'))
99 }
100 return Promise.resolve(new Response('moved', { status: 404 }))
101 })
102​
103 const result = await geminiProvider.listModels({ GEMINI_API_KEY: KEY })
104​
105 expect(result.models).toEqual([
106 expect.objectContaining({
107 rawId: 'gemini-2.5-flash',
108 displayName: 'Gemini 2.5 Flash',
109 // The model page is what tells chat from video (#658): unread, the
110 // stored activity and route are kept.
111 activity: null,
112 schemaEndpointId: null,
113 contextWindow: 1048576,
114 maxOutput: 65536,
115 absent: {
116 pricing: 'unavailable',
117 modalities: 'unavailable',
118 serverTools: 'unavailable',
119 reasoning: 'unavailable',
120 requestMap: 'unavailable',
121 activity: 'unavailable',
122 schemaEndpointId: 'unavailable',
123 // The changelog is unread too: its alias links are kept (#709).
124 aliases: 'unavailable',
125 // So is the Gemma card, the only maker source (#710).
126 maker: 'unavailable',
127 openWeights: 'unavailable',
128 },
129 }),
130 ])
131 expect(result.docsFailures?.first).toEqual(
132 expect.arrayContaining([
133 expect.objectContaining({
134 source: GEMINI_PRICING_URL,
135 error: 'gemini pricing page: parsed 0 model rows',
136 }),
137 ]),
138 )
139 expect(result.skipped).toBeUndefined()
140 })
141})
142​
143describe('gemini video models (#658)', () => {
144 /** https://aistudio.google.com/docs/models/gemini-omni-flash.md, 2026-11-16. */
145 const OMNI_PAGE = readFileSync(
146 new URL('./fixtures/gemini-omni-flash.md.txt', import.meta.url),
147 'utf8',
148 )
149 /** https://aistudio.google.com/docs/models/genie-4.md, 2026-11-20. */
150 const GENIE_PAGE = readFileSync(
151 new URL('./fixtures/gemini-genie-4.md.txt', import.meta.url),
152 'utf8',
153 )
154 const INDEX = `
155{% table variant="model" %}
156 * Model
157 * Endpoint
158​
159 ---
160​
161 * [Gemini Omni Flash](/docs/models/gemini-omni-flash)
162 * \`gemini-omni-1.2-flash\`
163 \`gemini-omni-1.1-flash\`
164{% /table %}
166 // The Models API lists Omni exactly like a chat model.
167 const LISTING = JSON.stringify({
168 models: [
169 {
170 name: 'models/gemini-omni-1.2-flash',
171 displayName: 'Gemini Omni 1.2 Flash',
172 supportedGenerationMethods: ['generateContent', 'countTokens'],
173 },
174 ],
175 })
176​
177 function stub(pages: Record<string, string>, listing = LISTING) {
178 vi.stubGlobal('fetch', (input: string) => {
179 const url = String(input)
180 if (
181 url.startsWith(
182 'https://generativelanguage.googleapis.com/v1beta/models',
183 )
184 ) {
185 return Promise.resolve(new Response(listing))
186 }
187 if (url === GEMINI_DISCOVERY_URL) {
188 return Promise.resolve(
189 new Response(
190 JSON.stringify({
191 schemas: {
192 ThinkingConfig: {
193 properties: { thinkingBudget: { description: 'budget' } },
194 },
195 },
196 }),
197 ),
198 )
199 }
200 const body = pages[url]
201 return Promise.resolve(
202 body === undefined
203 ? new Response('missing', { status: 500 })
204 : new Response(body),
205 )
206 })
207 }
208​
209 it('lists a model whose page states an output video limit as video, on its own route', async () => {
210 stub({
211 [GEMINI_MODELS_INDEX_URL]: INDEX,
212 'https://aistudio.google.com/docs/models/gemini-omni-flash.md': OMNI_PAGE,
213 })
214​
215 const result = await geminiProvider.listModels({ GEMINI_API_KEY: KEY })
216​
217 expect(result.models).toEqual([
218 expect.objectContaining({
219 rawId: 'gemini-omni-1.2-flash',
220 activity: 'video',
221 schemaEndpointId: 'gemini-omni-1.2-flash',
222 }),
223 ])
224 })
225​
226 it('lists genie-4 as video because the page states an output video stream (#697)', async () => {
227 stub(
228 {
229 [GEMINI_MODELS_INDEX_URL]: `
230{% table variant="model" %}
231 * Model
232 * Endpoint
233​
234 ---
235​
236 * [Genie 4](/docs/models/genie-4)
237 * \`genie-4\`
238​
239 ---
240​
241 * [Gemini 2.5 Flash](/docs/models/gemini-2.5-flash)
242 * \`gemini-2.5-flash\`
243{% /table %}
244`,
245 'https://aistudio.google.com/docs/models/genie-4.md': GENIE_PAGE,
246 'https://aistudio.google.com/docs/models/gemini-2.5-flash.md': `
247## gemini-2.5-flash
248​
249| Model code | \`gemini-2.5-flash\` |
250| Supported data types | **Inputs** Text **Output** Text |
251`,
252 },
253 JSON.stringify({
254 models: [
255 {
256 name: 'models/genie-4',
257 displayName: 'Genie 4',
258 supportedGenerationMethods: ['generateContent'],
259 },
260 {
261 name: 'models/gemini-2.5-flash',
262 displayName: 'Gemini 2.5 Flash',
263 supportedGenerationMethods: ['generateContent'],
264 },
265 ],
266 }),
267 )
268​
269 const result = await geminiProvider.listModels({ GEMINI_API_KEY: KEY })
270​
271 expect(result.models).toEqual([
272 expect.objectContaining({
273 rawId: 'genie-4',
274 activity: 'video',
275 schemaEndpointId: 'genie-4',
276 }),
277 expect.objectContaining({
278 rawId: 'gemini-2.5-flash',
279 activity: 'chat',
280 }),
281 ])
282 })
283​
284 it('keeps the stored activity and route when the model page cannot be read', async () => {
285 stub({ [GEMINI_MODELS_INDEX_URL]: INDEX })
286​
287 const result = await geminiProvider.listModels({ GEMINI_API_KEY: KEY })
288​
289 expect(result.models).toHaveLength(1)
290 expect(result.models[0]).toMatchObject({
291 rawId: 'gemini-omni-1.2-flash',
292 activity: null,
293 schemaEndpointId: null,
294 absent: {
295 activity: 'unavailable',
296 schemaEndpointId: 'unavailable',
297 requestMap: 'unavailable',
298 },
299 })
300 })
301})