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

gemini-list.test.ts

47 lines1.4 KB

1import { afterEach, describe, expect, it, vi } from 'vitest'
2​
3import { geminiProvider } from './gemini.ts'
4​
5const KEY = 'test-gemini-key-123'
6​
7afterEach(() => {
8 vi.unstubAllGlobals()
9})
10​
11describe('gemini listModels', () => {
12 it('sends the key as a header so a failed fetch never quotes it', async () => {
13 const seen: Array<{ url: string; key: string | null }> = []
14 vi.stubGlobal(
15 'fetch',
16 vi.fn((input: string, init?: RequestInit) => {
17 if (
18 !input.startsWith(
19 'https://generativelanguage.googleapis.com/v1beta/models?',
20 )
21 ) {
22 // Docs loads run alongside the listing; leave them pending.
23 return new Promise<Response>(() => {})
24 }
25 seen.push({
26 url: input,
27 key: new Headers(init?.headers).get('x-goog-api-key'),
28 })
29 return Promise.resolve(
30 new Response('bad', { status: 400, statusText: 'Bad Request' }),
31 )
32 }),
33 )
34​
35 const error = await geminiProvider.listModels({ GEMINI_API_KEY: KEY }).then(
36 () => null,
37 (err: unknown) => err,
38 )
39​
40 expect(error).toBeInstanceOf(Error)
41 expect((error as Error).message).toContain('400 Bad Request')
42 expect((error as Error).message).not.toContain(KEY)
43 expect(seen).toHaveLength(1)
44 expect(seen[0]?.url).not.toContain(KEY)
45 expect(seen[0]?.key).toBe(KEY)
46 })
47})