-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathprovider.test.ts
More file actions
223 lines (182 loc) · 7.88 KB
/
Copy pathprovider.test.ts
File metadata and controls
223 lines (182 loc) · 7.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import { getApiKeyFromEnv, getProviderSettings, providerRequiresApiKey } from "../provider.js"
// Bedrock-relevant AWS environment variables. Cleared before each test so the
// suite is hermetic regardless of the host machine's ambient AWS configuration.
const AWS_ENV_KEYS = [
"AWS_REGION",
"AWS_DEFAULT_REGION",
"AWS_PROFILE",
"AWS_ACCESS_KEY_ID",
"AWS_SECRET_ACCESS_KEY",
"AWS_SESSION_TOKEN",
"AWS_BEDROCK_API_KEY",
]
describe("getApiKeyFromEnv", () => {
const originalEnv = process.env
beforeEach(() => {
// Reset process.env before each test.
process.env = { ...originalEnv }
})
afterEach(() => {
process.env = originalEnv
})
it("should return API key from environment variable for anthropic", () => {
process.env.ANTHROPIC_API_KEY = "test-anthropic-key"
expect(getApiKeyFromEnv("anthropic")).toBe("test-anthropic-key")
})
it("should return API key from environment variable for openrouter", () => {
process.env.OPENROUTER_API_KEY = "test-openrouter-key"
expect(getApiKeyFromEnv("openrouter")).toBe("test-openrouter-key")
})
it("should return API key from environment variable for openai", () => {
process.env.OPENAI_API_KEY = "test-openai-key"
expect(getApiKeyFromEnv("openai-native")).toBe("test-openai-key")
})
it("should return undefined when API key is not set", () => {
delete process.env.ANTHROPIC_API_KEY
expect(getApiKeyFromEnv("anthropic")).toBeUndefined()
})
})
describe("providerRequiresApiKey", () => {
it("returns false for bedrock (AWS credential chain / profile / direct creds are valid)", () => {
expect(providerRequiresApiKey("bedrock")).toBe(false)
})
it.each(["anthropic", "openai-native", "gemini", "openrouter", "vercel-ai-gateway"] as const)(
"returns true for '%s'",
(provider) => {
expect(providerRequiresApiKey(provider)).toBe(true)
},
)
})
describe("getProviderSettings", () => {
const originalEnv = process.env
beforeEach(() => {
process.env = { ...originalEnv }
for (const key of AWS_ENV_KEYS) {
delete process.env[key]
}
})
afterEach(() => {
process.env = originalEnv
})
it("sets apiProvider for the selected provider", () => {
const config = getProviderSettings("anthropic", undefined, undefined)
expect(config.apiProvider).toBe("anthropic")
})
describe("bedrock authentication modes", () => {
it("mode 1: bearer token / API key sets awsUseApiKey and awsApiKey", () => {
const config = getProviderSettings("bedrock", "bearer-token-123", undefined)
expect(config.apiProvider).toBe("bedrock")
expect(config.awsUseApiKey).toBe(true)
expect(config.awsApiKey).toBe("bearer-token-123")
// API-key mode must not enable profile or direct-credential fields.
expect(config.awsUseProfile).toBeUndefined()
expect(config.awsProfile).toBeUndefined()
expect(config.awsAccessKey).toBeUndefined()
expect(config.awsSecretKey).toBeUndefined()
})
it("mode 2: AWS_PROFILE sets awsUseProfile and awsProfile", () => {
process.env.AWS_PROFILE = "my-sso-profile"
const config = getProviderSettings("bedrock", undefined, undefined)
expect(config.awsUseProfile).toBe(true)
expect(config.awsProfile).toBe("my-sso-profile")
expect(config.awsUseApiKey).toBeUndefined()
expect(config.awsApiKey).toBeUndefined()
expect(config.awsAccessKey).toBeUndefined()
})
it("mode 3: direct credentials map AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY", () => {
process.env.AWS_ACCESS_KEY_ID = "AKIAEXAMPLE"
process.env.AWS_SECRET_ACCESS_KEY = "secret-example"
const config = getProviderSettings("bedrock", undefined, undefined)
expect(config.awsAccessKey).toBe("AKIAEXAMPLE")
expect(config.awsSecretKey).toBe("secret-example")
expect(config.awsSessionToken).toBeUndefined()
expect(config.awsUseApiKey).toBeUndefined()
expect(config.awsUseProfile).toBeUndefined()
})
it("mode 3: direct credentials include AWS_SESSION_TOKEN when present", () => {
process.env.AWS_ACCESS_KEY_ID = "AKIAEXAMPLE"
process.env.AWS_SECRET_ACCESS_KEY = "secret-example"
process.env.AWS_SESSION_TOKEN = "session-token-example"
const config = getProviderSettings("bedrock", undefined, undefined)
expect(config.awsAccessKey).toBe("AKIAEXAMPLE")
expect(config.awsSecretKey).toBe("secret-example")
expect(config.awsSessionToken).toBe("session-token-example")
})
it("mode 4: default credential chain — no creds set does NOT throw and sets no explicit auth fields", () => {
expect(() => getProviderSettings("bedrock", undefined, undefined)).not.toThrow()
const config = getProviderSettings("bedrock", undefined, undefined)
// Region is always resolved, but none of the explicit auth modes engage,
// leaving the AWS SDK to resolve credentials via its default chain.
expect(config.awsUseApiKey).toBeUndefined()
expect(config.awsApiKey).toBeUndefined()
expect(config.awsUseProfile).toBeUndefined()
expect(config.awsProfile).toBeUndefined()
expect(config.awsAccessKey).toBeUndefined()
expect(config.awsSecretKey).toBeUndefined()
})
it("prioritises API key over AWS_PROFILE and direct credentials", () => {
process.env.AWS_PROFILE = "my-profile"
process.env.AWS_ACCESS_KEY_ID = "AKIAEXAMPLE"
process.env.AWS_SECRET_ACCESS_KEY = "secret-example"
const config = getProviderSettings("bedrock", "bearer-token-123", undefined)
expect(config.awsUseApiKey).toBe(true)
expect(config.awsApiKey).toBe("bearer-token-123")
expect(config.awsUseProfile).toBeUndefined()
expect(config.awsAccessKey).toBeUndefined()
})
it("prioritises AWS_PROFILE over direct credentials when no API key is present", () => {
process.env.AWS_PROFILE = "my-profile"
process.env.AWS_ACCESS_KEY_ID = "AKIAEXAMPLE"
process.env.AWS_SECRET_ACCESS_KEY = "secret-example"
const config = getProviderSettings("bedrock", undefined, undefined)
expect(config.awsUseProfile).toBe(true)
expect(config.awsProfile).toBe("my-profile")
expect(config.awsAccessKey).toBeUndefined()
expect(config.awsSecretKey).toBeUndefined()
})
})
describe("bedrock region resolution", () => {
it("defaults awsRegion to us-east-1 when no region env is set", () => {
const config = getProviderSettings("bedrock", undefined, undefined)
expect(config.awsRegion).toBe("us-east-1")
})
it("uses AWS_REGION when set", () => {
process.env.AWS_REGION = "eu-west-1"
const config = getProviderSettings("bedrock", undefined, undefined)
expect(config.awsRegion).toBe("eu-west-1")
})
it("falls back to AWS_DEFAULT_REGION when AWS_REGION is unset", () => {
process.env.AWS_DEFAULT_REGION = "ap-southeast-2"
const config = getProviderSettings("bedrock", undefined, undefined)
expect(config.awsRegion).toBe("ap-southeast-2")
})
it("prefers AWS_REGION over AWS_DEFAULT_REGION", () => {
process.env.AWS_REGION = "eu-west-1"
process.env.AWS_DEFAULT_REGION = "ap-southeast-2"
const config = getProviderSettings("bedrock", undefined, undefined)
expect(config.awsRegion).toBe("eu-west-1")
})
})
describe("bedrock cross-region inference auto-enable", () => {
it.each([
"us.anthropic.claude-3-5-sonnet-20241022-v2:0",
"eu.anthropic.claude-3-5-sonnet",
"apac.amazon.nova-pro",
])("enables awsUseCrossRegionInference for regional-prefixed model '%s'", (model) => {
const config = getProviderSettings("bedrock", undefined, model)
expect(config.apiModelId).toBe(model)
expect(config.awsUseCrossRegionInference).toBe(true)
})
it("does NOT enable cross-region inference for a non-prefixed model", () => {
const model = "anthropic.claude-3-5-sonnet-20241022-v2:0"
const config = getProviderSettings("bedrock", undefined, model)
expect(config.apiModelId).toBe(model)
expect(config.awsUseCrossRegionInference).toBeUndefined()
})
it("does not set apiModelId or cross-region flag when no model is given", () => {
const config = getProviderSettings("bedrock", undefined, undefined)
expect(config.apiModelId).toBeUndefined()
expect(config.awsUseCrossRegionInference).toBeUndefined()
})
})
})