Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
2import { resolveVertexGeminiOptions } from '../src/auth'
3import { VertexAuthError } from '../src/errors'
4
5const VERTEX_ENV = [
6 'GOOGLE_CLOUD_PROJECT',
7 'GOOGLE_VERTEX_PROJECT',
8 'GOOGLE_CLOUD_LOCATION',
9 'GOOGLE_VERTEX_LOCATION',
10 'GOOGLE_VERTEX_API_KEY',
11] as const
12
13describe('resolveVertexGeminiOptions', () => {
14 beforeEach(() => {
15 for (const name of VERTEX_ENV) {
16 vi.stubEnv(name, '')
17 }
18 })
19
20 afterEach(() => {
21 vi.unstubAllEnvs()
22 })
23
24 it('uses factory project and location and sets vertexai', () => {
25 const options = resolveVertexGeminiOptions({
26 project: 'my-project',
27 location: 'europe-west1',
28 })
29
30 expect(options).toEqual({
31 project: 'my-project',
32 location: 'europe-west1',
33 vertexai: true,
34 })
35 })
36
37 it('reads project and location from the environment', () => {
38 vi.stubEnv('GOOGLE_CLOUD_PROJECT', 'env-project')
39 vi.stubEnv('GOOGLE_CLOUD_LOCATION', 'us-central1')
40
41 const options = resolveVertexGeminiOptions()
42
43 expect(options).toEqual({
44 project: 'env-project',
45 location: 'us-central1',
46 vertexai: true,
47 })
48 })
49
50 it('lets factory fields win over env', () => {
51 vi.stubEnv('GOOGLE_CLOUD_PROJECT', 'env-project')
52 vi.stubEnv('GOOGLE_CLOUD_LOCATION', 'us-central1')
53
54 const options = resolveVertexGeminiOptions({
55 project: 'factory-project',
56 location: 'europe-west1',
57 })
58
59 expect(options.project).toBe('factory-project')
60 expect(options.location).toBe('europe-west1')
61 })
62
63 it('accepts an express apiKey without project or location', () => {
64 const options = resolveVertexGeminiOptions({
65 apiKey: 'express-key',
66 })
67
68 expect(options).toEqual({
69 apiKey: 'express-key',
70 vertexai: true,
71 })
72 })
73
74 it('reads GOOGLE_VERTEX_API_KEY for express mode', () => {
75 vi.stubEnv('GOOGLE_VERTEX_API_KEY', 'env-express-key')
76
77 const options = resolveVertexGeminiOptions()
78
79 expect(options.apiKey).toBe('env-express-key')
80 expect(options.vertexai).toBe(true)
81 })
82
83 it('does not read GEMINI_API_KEY', () => {
84 vi.stubEnv('GEMINI_API_KEY', 'studio-key')
85
86 expect(() => resolveVertexGeminiOptions()).toThrow(VertexAuthError)
87 })
88
89 it('throws when project, location, and express apiKey are all missing', () => {
90 expect(() => resolveVertexGeminiOptions()).toThrow(VertexAuthError)
91 expect(() => resolveVertexGeminiOptions()).toThrow(
92 /project and location, or an express apiKey/,
93 )
94 })
95
96 it('treats empty factory credentials as absent and falls back to env', () => {
97 vi.stubEnv('GOOGLE_CLOUD_PROJECT', 'env-project')
98 vi.stubEnv('GOOGLE_CLOUD_LOCATION', 'us-central1')
99 vi.stubEnv('GOOGLE_VERTEX_API_KEY', 'env-express-key')
100
101 const options = resolveVertexGeminiOptions({
102 project: '',
103 location: '',
104 apiKey: '',
105 })
106
107 expect(options).toEqual({
108 project: 'env-project',
109 location: 'us-central1',
110 apiKey: 'env-express-key',
111 vertexai: true,
112 })
113 })
114
115 it('throws when factory credentials are empty and env is empty', () => {
116 expect(() =>
117 resolveVertexGeminiOptions({
118 project: '',
119 location: '',
120 apiKey: '',
121 }),
122 ).toThrow(VertexAuthError)
123 })
124
125 it('forwards googleAuthOptions', () => {
126 const googleAuthOptions = {
127 keyFilename: '/path/to/sa.json',
128 }
129
130 const options = resolveVertexGeminiOptions({
131 project: 'my-project',
132 location: 'europe-west1',
133 googleAuthOptions,
134 })
135
136 expect(options.googleAuthOptions).toBe(googleAuthOptions)
137 })
138})