Open History to see the last commit
1import { afterEach, describe, expect, it, vi } from 'vitest'
2import { resolveBedrockAuth, withBedrockDefaults } from '../src/utils/client'
3
4afterEach(() => {
5 vi.unstubAllEnvs()
6})
7
8describe('withBedrockDefaults', () => {
9 it('builds the runtime URL by default', () => {
10 const out = withBedrockDefaults({ apiKey: 'k', region: 'us-east-1' })
11 expect(out.baseURL).toBe(
12 'https://bedrock-runtime.us-east-1.amazonaws.com/openai/v1',
13 )
14 })
15
16 it('defaults region to us-east-1', () => {
17 const out = withBedrockDefaults({ apiKey: 'k' })
18 expect(out.baseURL).toBe(
19 'https://bedrock-runtime.us-east-1.amazonaws.com/openai/v1',
20 )
21 })
22
23 it('builds the mantle URL when endpoint is mantle', () => {
24 const out = withBedrockDefaults({
25 apiKey: 'k',
26 region: 'eu-west-1',
27 endpoint: 'mantle',
28 })
29 expect(out.baseURL).toBe('https://bedrock-mantle.eu-west-1.api.aws/v1')
30 })
31
32 it('uses config mantlePath /openai/v1 for google.gemma-4-31b (#925)', () => {
33 const out = withBedrockDefaults(
34 { apiKey: 'k', region: 'eu-central-1', endpoint: 'mantle' },
35 undefined,
36 'google.gemma-4-31b',
37 )
38 expect(out.baseURL).toBe(
39 'https://bedrock-mantle.eu-central-1.api.aws/openai/v1',
40 )
41 })
42
43 it('keeps /v1 on mantle for google.gemma-3-*', () => {
44 const out = withBedrockDefaults(
45 { apiKey: 'k', region: 'eu-central-1', endpoint: 'mantle' },
46 undefined,
47 'google.gemma-3-12b-it',
48 )
49 expect(out.baseURL).toBe('https://bedrock-mantle.eu-central-1.api.aws/v1')
50 })
51
52 it('keeps /v1 on mantle for other models', () => {
53 const out = withBedrockDefaults(
54 { apiKey: 'k', region: 'eu-central-1', endpoint: 'mantle' },
55 undefined,
56 'openai.gpt-oss-120b-1:0',
57 )
58 expect(out.baseURL).toBe('https://bedrock-mantle.eu-central-1.api.aws/v1')
59 })
60
61 it('uses /v1 on mantle for an id that matches no compatibility rule', () => {
62 const out = withBedrockDefaults(
63 { apiKey: 'k', region: 'eu-central-1', endpoint: 'mantle' },
64 undefined,
65 'acme.unknown-model-v1:0',
66 )
67 expect(out.baseURL).toBe('https://bedrock-mantle.eu-central-1.api.aws/v1')
68 })
69
70 it('uses config mantlePath for a Gemma 4 id that is not in the catalog', () => {
71 const out = withBedrockDefaults(
72 { apiKey: 'k', region: 'eu-central-1', endpoint: 'mantle' },
73 undefined,
74 'google.gemma-4-future',
75 )
76 expect(out.baseURL).toBe(
77 'https://bedrock-mantle.eu-central-1.api.aws/openai/v1',
78 )
79 })
80
81 it('uses config mantlePath when forced to mantle', () => {
82 const out = withBedrockDefaults(
83 { apiKey: 'k', region: 'us-west-2', endpoint: 'runtime' },
84 'mantle',
85 'google.gemma-4-31b',
86 )
87 expect(out.baseURL).toBe(
88 'https://bedrock-mantle.us-west-2.api.aws/openai/v1',
89 )
90 })
91
92 it('leaves the runtime path unchanged for Gemma', () => {
93 const out = withBedrockDefaults(
94 { apiKey: 'k', region: 'us-east-1', endpoint: 'runtime' },
95 undefined,
96 'google.gemma-4-31b',
97 )
98 expect(out.baseURL).toBe(
99 'https://bedrock-runtime.us-east-1.amazonaws.com/openai/v1',
100 )
101 })
102
103 it('explicit baseURL still wins for a Gemma model', () => {
104 const out = withBedrockDefaults(
105 {
106 apiKey: 'k',
107 endpoint: 'mantle',
108 baseURL: 'http://127.0.0.1:4010/v1',
109 },
110 undefined,
111 'google.gemma-4-31b',
112 )
113 expect(out.baseURL).toBe('http://127.0.0.1:4010/v1')
114 })
115
116 it('forces mantle when the `forced` arg is mantle, ignoring config.endpoint', () => {
117 const out = withBedrockDefaults(
118 { apiKey: 'k', region: 'us-west-2', endpoint: 'runtime' },
119 'mantle',
120 )
121 expect(out.baseURL).toBe('https://bedrock-mantle.us-west-2.api.aws/v1')
122 })
123
124 it('honors an explicit baseURL override', () => {
125 const out = withBedrockDefaults({
126 apiKey: 'k',
127 baseURL: 'http://127.0.0.1:4010/v1',
128 })
129 expect(out.baseURL).toBe('http://127.0.0.1:4010/v1')
130 })
131
132 it('does not leak region/endpoint/auth into the OpenAI ClientOptions', () => {
133 const out = withBedrockDefaults({
134 apiKey: 'k',
135 region: 'us-east-1',
136 endpoint: 'runtime',
137 auth: 'apikey',
138 })
139 expect('region' in out).toBe(false)
140 expect('endpoint' in out).toBe(false)
141 expect('auth' in out).toBe(false)
142 })
143
144 it('explicit baseURL survives the SigV4 path and signer is attached', () => {
145 const out = withBedrockDefaults({
146 baseURL: 'http://127.0.0.1:4010/v1',
147 auth: 'sigv4',
148 region: 'us-east-1',
149 })
150 expect(out.baseURL).toBe('http://127.0.0.1:4010/v1')
151 expect(typeof out.fetch).toBe('function')
152 })
153
154 it('user-supplied fetch wins over the SigV4 signer', () => {
155 const userFetch: NonNullable<
156 import('openai').ClientOptions['fetch']
157 > = async () => new Response()
158 const out = withBedrockDefaults({
159 auth: 'sigv4',
160 region: 'us-east-1',
161 fetch: userFetch,
162 })
163 expect(out.fetch).toBe(userFetch)
164 })
165})
166
167describe('resolveBedrockAuth', () => {
168 it('uses an explicit apiKey — returns bearer', () => {
169 const r = resolveBedrockAuth({ apiKey: 'explicit' }, 'runtime')
170 expect(r).toEqual({ kind: 'bearer', token: 'explicit' })
171 })
172
173 it('falls back to BEDROCK_API_KEY — returns bearer', () => {
174 vi.stubEnv('BEDROCK_API_KEY', 'from-bedrock-env')
175 const r = resolveBedrockAuth({}, 'runtime')
176 expect(r).toEqual({ kind: 'bearer', token: 'from-bedrock-env' })
177 })
178
179 it('falls back to AWS_BEARER_TOKEN_BEDROCK — returns bearer', () => {
180 vi.stubEnv('AWS_BEARER_TOKEN_BEDROCK', 'from-aws-env')
181 const r = resolveBedrockAuth({}, 'runtime')
182 expect(r).toEqual({ kind: 'bearer', token: 'from-aws-env' })
183 })
184
185 it('resolves precedence apiKey > BEDROCK_API_KEY > AWS_BEARER_TOKEN_BEDROCK', () => {
186 vi.stubEnv('BEDROCK_API_KEY', 'bedrock-env')
187 vi.stubEnv('AWS_BEARER_TOKEN_BEDROCK', 'aws-env')
188 // Explicit apiKey wins over both env vars.
189 expect(resolveBedrockAuth({ apiKey: 'explicit' }, 'runtime')).toEqual({
190 kind: 'bearer',
191 token: 'explicit',
192 })
193 // With no apiKey, BEDROCK_API_KEY wins over AWS_BEARER_TOKEN_BEDROCK.
194 expect(resolveBedrockAuth({}, 'runtime')).toEqual({
195 kind: 'bearer',
196 token: 'bedrock-env',
197 })
198 })
199
200 it('treats a present-but-blank BEDROCK_API_KEY as absent', () => {
201 vi.stubEnv('BEDROCK_API_KEY', ' ')
202 vi.stubEnv('AWS_BEARER_TOKEN_BEDROCK', 'aws-env')
203 expect(resolveBedrockAuth({}, 'runtime')).toEqual({
204 kind: 'bearer',
205 token: 'aws-env',
206 })
207 })
208
209 it("uses service 'bedrock-mantle' for the mantle endpoint under sigv4", () => {
210 const r = resolveBedrockAuth(
211 { auth: 'sigv4', region: 'us-west-2' },
212 'mantle',
213 )
214 expect(r.kind).toBe('sigv4')
215 if (r.kind === 'sigv4') {
216 expect(r.region).toBe('us-west-2')
217 expect(r.service).toBe('bedrock-mantle')
218 }
219 })
220
221 it("auth: 'apikey' with no key throws an actionable error", () => {
222 vi.stubEnv('BEDROCK_API_KEY', '')
223 vi.stubEnv('AWS_BEARER_TOKEN_BEDROCK', '')
224 expect(() =>
225 resolveBedrockAuth({ auth: 'apikey' }, 'runtime'),
226 ).toThrowError(/No Bedrock API key/)
227 })
228
229 it("auth: 'sigv4' returns kind:'sigv4' with region and service", () => {
230 const r = resolveBedrockAuth(
231 { auth: 'sigv4', region: 'us-east-1' },
232 'runtime',
233 )
234 expect(r.kind).toBe('sigv4')
235 if (r.kind === 'sigv4') {
236 expect(r.region).toBe('us-east-1')
237 expect(r.service).toBe('bedrock')
238 }
239 })
240
241 it("'auto' with no key falls through to SigV4 — returns kind:'sigv4'", () => {
242 vi.stubEnv('BEDROCK_API_KEY', '')
243 vi.stubEnv('AWS_BEARER_TOKEN_BEDROCK', '')
244 const r = resolveBedrockAuth({ region: 'us-east-1' }, 'runtime')
245 expect(r.kind).toBe('sigv4')
246 })
247})