|
1 | | -import { test, expect } from "bun:test" |
| 1 | +import { test, expect, mock } from "bun:test" |
2 | 2 | import path from "path" |
3 | | -import { tmpdir } from "../fixture/fixture" |
4 | | -import { Instance } from "../../src/project/instance" |
5 | | -import { Provider } from "../../src/provider/provider" |
6 | | -import { Env } from "../../src/env" |
7 | | -import { Auth } from "../../src/auth" |
8 | | -import { Global } from "../../src/global" |
| 3 | + |
| 4 | +// === Mocks === |
| 5 | +// These mocks are required because Provider.list() triggers: |
| 6 | +// 1. BunProc.install("@aws-sdk/credential-providers") - in bedrock custom loader |
| 7 | +// 2. Plugin.list() which calls BunProc.install() for default plugins |
| 8 | +// Without mocks, these would attempt real package installations that timeout in tests. |
| 9 | + |
| 10 | +mock.module("../../src/bun/index", () => ({ |
| 11 | + BunProc: { |
| 12 | + install: async (pkg: string) => pkg, |
| 13 | + run: async () => { |
| 14 | + throw new Error("BunProc.run should not be called in tests") |
| 15 | + }, |
| 16 | + which: () => process.execPath, |
| 17 | + InstallFailedError: class extends Error {}, |
| 18 | + }, |
| 19 | +})) |
| 20 | + |
| 21 | +mock.module("@aws-sdk/credential-providers", () => ({ |
| 22 | + fromNodeProviderChain: () => async () => ({ |
| 23 | + accessKeyId: "mock-access-key-id", |
| 24 | + secretAccessKey: "mock-secret-access-key", |
| 25 | + }), |
| 26 | +})) |
| 27 | + |
| 28 | +const mockPlugin = () => ({}) |
| 29 | +mock.module("opencode-copilot-auth", () => ({ default: mockPlugin })) |
| 30 | +mock.module("opencode-anthropic-auth", () => ({ default: mockPlugin })) |
| 31 | + |
| 32 | +// Import after mocks are set up |
| 33 | +const { tmpdir } = await import("../fixture/fixture") |
| 34 | +const { Instance } = await import("../../src/project/instance") |
| 35 | +const { Provider } = await import("../../src/provider/provider") |
| 36 | +const { Env } = await import("../../src/env") |
| 37 | +const { Global } = await import("../../src/global") |
9 | 38 |
|
10 | 39 | test("Bedrock: config region takes precedence over AWS_REGION env var", async () => { |
11 | 40 | await using tmp = await tmpdir({ |
@@ -34,13 +63,12 @@ test("Bedrock: config region takes precedence over AWS_REGION env var", async () |
34 | 63 | fn: async () => { |
35 | 64 | const providers = await Provider.list() |
36 | 65 | expect(providers["amazon-bedrock"]).toBeDefined() |
37 | | - // Region from config should be used (not env var) |
38 | 66 | expect(providers["amazon-bedrock"].options?.region).toBe("eu-west-1") |
39 | 67 | }, |
40 | 68 | }) |
41 | 69 | }) |
42 | 70 |
|
43 | | -test("Bedrock: falls back to AWS_REGION env var when no config", async () => { |
| 71 | +test("Bedrock: falls back to AWS_REGION env var when no config region", async () => { |
44 | 72 | await using tmp = await tmpdir({ |
45 | 73 | init: async (dir) => { |
46 | 74 | await Bun.write( |
@@ -65,34 +93,7 @@ test("Bedrock: falls back to AWS_REGION env var when no config", async () => { |
65 | 93 | }) |
66 | 94 | }) |
67 | 95 |
|
68 | | -test("Bedrock: without explicit region config, uses AWS_REGION env or defaults", async () => { |
69 | | - await using tmp = await tmpdir({ |
70 | | - init: async (dir) => { |
71 | | - await Bun.write( |
72 | | - path.join(dir, "opencode.json"), |
73 | | - JSON.stringify({ |
74 | | - $schema: "https://opencode.ai/config.json", |
75 | | - }), |
76 | | - ) |
77 | | - }, |
78 | | - }) |
79 | | - await Instance.provide({ |
80 | | - directory: tmp.path, |
81 | | - init: async () => { |
82 | | - Env.set("AWS_PROFILE", "default") |
83 | | - // AWS_REGION might be set in the environment, use that or default |
84 | | - }, |
85 | | - fn: async () => { |
86 | | - const providers = await Provider.list() |
87 | | - expect(providers["amazon-bedrock"]).toBeDefined() |
88 | | - // Should have some region set (either from env or default) |
89 | | - expect(providers["amazon-bedrock"].options?.region).toBeDefined() |
90 | | - expect(typeof providers["amazon-bedrock"].options?.region).toBe("string") |
91 | | - }, |
92 | | - }) |
93 | | -}) |
94 | | - |
95 | | -test("Bedrock: uses config region in provider options", async () => { |
| 96 | +test("Bedrock: loads when bearer token from auth.json is present", async () => { |
96 | 97 | await using tmp = await tmpdir({ |
97 | 98 | init: async (dir) => { |
98 | 99 | await Bun.write( |
@@ -102,62 +103,43 @@ test("Bedrock: uses config region in provider options", async () => { |
102 | 103 | provider: { |
103 | 104 | "amazon-bedrock": { |
104 | 105 | options: { |
105 | | - region: "eu-north-1", |
| 106 | + region: "eu-west-1", |
106 | 107 | }, |
107 | 108 | }, |
108 | 109 | }, |
109 | 110 | }), |
110 | 111 | ) |
111 | 112 | }, |
112 | 113 | }) |
| 114 | + |
| 115 | + const authPath = path.join(Global.Path.data, "auth.json") |
| 116 | + await Bun.write( |
| 117 | + authPath, |
| 118 | + JSON.stringify({ |
| 119 | + "amazon-bedrock": { |
| 120 | + type: "api", |
| 121 | + key: "test-bearer-token", |
| 122 | + }, |
| 123 | + }), |
| 124 | + ) |
| 125 | + |
113 | 126 | await Instance.provide({ |
114 | 127 | directory: tmp.path, |
115 | 128 | init: async () => { |
116 | | - Env.set("AWS_PROFILE", "default") |
| 129 | + Env.set("AWS_PROFILE", "") |
| 130 | + Env.set("AWS_ACCESS_KEY_ID", "") |
| 131 | + Env.set("AWS_BEARER_TOKEN_BEDROCK", "") |
117 | 132 | }, |
118 | 133 | fn: async () => { |
119 | 134 | const providers = await Provider.list() |
120 | | - const bedrockProvider = providers["amazon-bedrock"] |
121 | | - expect(bedrockProvider).toBeDefined() |
122 | | - expect(bedrockProvider.options?.region).toBe("eu-north-1") |
| 135 | + expect(providers["amazon-bedrock"]).toBeDefined() |
| 136 | + expect(providers["amazon-bedrock"].options?.region).toBe("eu-west-1") |
123 | 137 | }, |
124 | 138 | }) |
125 | 139 | }) |
126 | 140 |
|
127 | | -test("Bedrock: respects config region for different instances", async () => { |
128 | | - // First instance with EU config |
129 | | - await using tmp1 = await tmpdir({ |
130 | | - init: async (dir) => { |
131 | | - await Bun.write( |
132 | | - path.join(dir, "opencode.json"), |
133 | | - JSON.stringify({ |
134 | | - $schema: "https://opencode.ai/config.json", |
135 | | - provider: { |
136 | | - "amazon-bedrock": { |
137 | | - options: { |
138 | | - region: "eu-west-1", |
139 | | - }, |
140 | | - }, |
141 | | - }, |
142 | | - }), |
143 | | - ) |
144 | | - }, |
145 | | - }) |
146 | | - |
147 | | - await Instance.provide({ |
148 | | - directory: tmp1.path, |
149 | | - init: async () => { |
150 | | - Env.set("AWS_PROFILE", "default") |
151 | | - Env.set("AWS_REGION", "us-east-1") |
152 | | - }, |
153 | | - fn: async () => { |
154 | | - const providers1 = await Provider.list() |
155 | | - expect(providers1["amazon-bedrock"].options?.region).toBe("eu-west-1") |
156 | | - }, |
157 | | - }) |
158 | | - |
159 | | - // Second instance with US config |
160 | | - await using tmp2 = await tmpdir({ |
| 141 | +test("Bedrock: config profile takes precedence over AWS_PROFILE env var", async () => { |
| 142 | + await using tmp = await tmpdir({ |
161 | 143 | init: async (dir) => { |
162 | 144 | await Bun.write( |
163 | 145 | path.join(dir, "opencode.json"), |
@@ -166,29 +148,30 @@ test("Bedrock: respects config region for different instances", async () => { |
166 | 148 | provider: { |
167 | 149 | "amazon-bedrock": { |
168 | 150 | options: { |
169 | | - region: "us-west-2", |
| 151 | + profile: "my-custom-profile", |
| 152 | + region: "us-east-1", |
170 | 153 | }, |
171 | 154 | }, |
172 | 155 | }, |
173 | 156 | }), |
174 | 157 | ) |
175 | 158 | }, |
176 | 159 | }) |
177 | | - |
178 | 160 | await Instance.provide({ |
179 | | - directory: tmp2.path, |
| 161 | + directory: tmp.path, |
180 | 162 | init: async () => { |
181 | 163 | Env.set("AWS_PROFILE", "default") |
182 | | - Env.set("AWS_REGION", "eu-west-1") |
| 164 | + Env.set("AWS_ACCESS_KEY_ID", "test-key-id") |
183 | 165 | }, |
184 | 166 | fn: async () => { |
185 | | - const providers2 = await Provider.list() |
186 | | - expect(providers2["amazon-bedrock"].options?.region).toBe("us-west-2") |
| 167 | + const providers = await Provider.list() |
| 168 | + expect(providers["amazon-bedrock"]).toBeDefined() |
| 169 | + expect(providers["amazon-bedrock"].options?.region).toBe("us-east-1") |
187 | 170 | }, |
188 | 171 | }) |
189 | 172 | }) |
190 | 173 |
|
191 | | -test("Bedrock: loads when bearer token from auth.json is present", async () => { |
| 174 | +test("Bedrock: includes custom endpoint in options when specified", async () => { |
192 | 175 | await using tmp = await tmpdir({ |
193 | 176 | init: async (dir) => { |
194 | 177 | await Bun.write( |
@@ -198,39 +181,25 @@ test("Bedrock: loads when bearer token from auth.json is present", async () => { |
198 | 181 | provider: { |
199 | 182 | "amazon-bedrock": { |
200 | 183 | options: { |
201 | | - region: "eu-west-1", |
| 184 | + endpoint: "https://bedrock-runtime.us-east-1.vpce-xxxxx.amazonaws.com", |
202 | 185 | }, |
203 | 186 | }, |
204 | 187 | }, |
205 | 188 | }), |
206 | 189 | ) |
207 | 190 | }, |
208 | 191 | }) |
209 | | - |
210 | | - // Setup auth.json with bearer token for amazon-bedrock |
211 | | - const authPath = path.join(Global.Path.data, "auth.json") |
212 | | - await Bun.write( |
213 | | - authPath, |
214 | | - JSON.stringify({ |
215 | | - "amazon-bedrock": { |
216 | | - type: "api", |
217 | | - key: "test-bearer-token", |
218 | | - }, |
219 | | - }), |
220 | | - ) |
221 | | - |
222 | 192 | await Instance.provide({ |
223 | 193 | directory: tmp.path, |
224 | 194 | init: async () => { |
225 | | - // Clear env vars so only auth.json should trigger autoload |
226 | | - Env.set("AWS_PROFILE", "") |
227 | | - Env.set("AWS_ACCESS_KEY_ID", "") |
228 | | - Env.set("AWS_BEARER_TOKEN_BEDROCK", "") |
| 195 | + Env.set("AWS_PROFILE", "default") |
229 | 196 | }, |
230 | 197 | fn: async () => { |
231 | 198 | const providers = await Provider.list() |
232 | 199 | expect(providers["amazon-bedrock"]).toBeDefined() |
233 | | - expect(providers["amazon-bedrock"].options?.region).toBe("eu-west-1") |
| 200 | + expect(providers["amazon-bedrock"].options?.endpoint).toBe( |
| 201 | + "https://bedrock-runtime.us-east-1.vpce-xxxxx.amazonaws.com", |
| 202 | + ) |
234 | 203 | }, |
235 | 204 | }) |
236 | 205 | }) |