Open History to see the last commit
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 },
124 }),
125 ])
126 expect(result.docsFailures?.first).toEqual(
127 expect.arrayContaining([
128 expect.objectContaining({
129 source: GEMINI_PRICING_URL,
130 error: 'gemini pricing page: parsed 0 model rows',
131 }),
132 ]),
133 )
134 expect(result.skipped).toBeUndefined()
135 })
136})
137
138describe('gemini video models (#658)', () => {
139 /** https://aistudio.google.com/docs/models/gemini-omni-flash.md, 2026-11-16. */
140 const OMNI_PAGE = readFileSync(
141 new URL('./fixtures/gemini-omni-flash.md.txt', import.meta.url),
142 'utf8',
143 )
144 const INDEX = `
145{% table variant="model" %}
146 * Model
147 * Endpoint
148
149 ---
150
151 * [Gemini Omni Flash](/docs/models/gemini-omni-flash)
152 * \`gemini-omni-1.2-flash\`
153 \`gemini-omni-1.1-flash\`
154{% /table %}
155`
156 // The Models API lists Omni exactly like a chat model.
157 const LISTING = JSON.stringify({
158 models: [
159 {
160 name: 'models/gemini-omni-1.2-flash',
161 displayName: 'Gemini Omni 1.2 Flash',
162 supportedGenerationMethods: ['generateContent', 'countTokens'],
163 },
164 ],
165 })
166
167 function stub(pages: Record<string, string>) {
168 vi.stubGlobal('fetch', (input: string) => {
169 const url = String(input)
170 if (
171 url.startsWith(
172 'https://generativelanguage.googleapis.com/v1beta/models',
173 )
174 ) {
175 return Promise.resolve(new Response(LISTING))
176 }
177 if (url === GEMINI_DISCOVERY_URL) {
178 return Promise.resolve(
179 new Response(
180 JSON.stringify({
181 schemas: {
182 ThinkingConfig: {
183 properties: { thinkingBudget: { description: 'budget' } },
184 },
185 },
186 }),
187 ),
188 )
189 }
190 const body = pages[url]
191 return Promise.resolve(
192 body === undefined
193 ? new Response('missing', { status: 500 })
194 : new Response(body),
195 )
196 })
197 }
198
199 it('lists a model whose page states an output video limit as video, on its own route', async () => {
200 stub({
201 [GEMINI_MODELS_INDEX_URL]: INDEX,
202 'https://aistudio.google.com/docs/models/gemini-omni-flash.md': OMNI_PAGE,
203 })
204
205 const result = await geminiProvider.listModels({ GEMINI_API_KEY: KEY })
206
207 expect(result.models).toEqual([
208 expect.objectContaining({
209 rawId: 'gemini-omni-1.2-flash',
210 activity: 'video',
211 schemaEndpointId: 'gemini-omni-1.2-flash',
212 }),
213 ])
214 })
215
216 it('keeps the stored activity and route when the model page cannot be read', async () => {
217 stub({ [GEMINI_MODELS_INDEX_URL]: INDEX })
218
219 const result = await geminiProvider.listModels({ GEMINI_API_KEY: KEY })
220
221 expect(result.models).toHaveLength(1)
222 expect(result.models[0]).toMatchObject({
223 rawId: 'gemini-omni-1.2-flash',
224 activity: null,
225 schemaEndpointId: null,
226 absent: {
227 activity: 'unavailable',
228 schemaEndpointId: 'unavailable',
229 requestMap: 'unavailable',
230 },
231 })
232 })
233})