@@ -29,6 +29,12 @@ function rememberEnv(k: string) { |
29 | 29 | if (!originalEnv.has(k)) originalEnv.set(k, process.env[k]) |
30 | 30 | } |
31 | 31 |
|
| 32 | +const setProcessEnv = (k: string, v: string) => |
| 33 | + Effect.sync(() => { |
| 34 | + rememberEnv(k) |
| 35 | + process.env[k] = v |
| 36 | + }) |
| 37 | + |
32 | 38 | const set = (ctx: InstanceContext, k: string, v: string) => { |
33 | 39 | rememberEnv(k) |
34 | 40 | process.env[k] = v |
@@ -128,160 +134,99 @@ const alphaProviderConfig = { |
128 | 134 | }, |
129 | 135 | } |
130 | 136 |
|
131 | | -test("provider loaded from env variable", async () => { |
132 | | - await using tmp = await tmpdir({ |
133 | | - init: async (dir) => { |
134 | | - await Bun.write( |
135 | | - path.join(dir, "opencode.json"), |
136 | | - JSON.stringify({ |
137 | | - $schema: "https://opencode.ai/config.json", |
138 | | - }), |
139 | | - ) |
140 | | - }, |
141 | | - }) |
142 | | - await withTestInstance({ |
143 | | - directory: tmp.path, |
144 | | - fn: async (ctx) => { |
145 | | - await set(ctx, "ANTHROPIC_API_KEY", "test-api-key") |
146 | | - const providers = await list(ctx) |
147 | | - expect(providers[ProviderID.anthropic]).toBeDefined() |
148 | | - // Provider should retain its connection source even if custom loaders |
149 | | - // merge additional options. |
150 | | - expect(providers[ProviderID.anthropic].source).toBe("env") |
151 | | - expect(providers[ProviderID.anthropic].options.headers["anthropic-beta"]).toBeDefined() |
152 | | - }, |
153 | | - }) |
154 | | -}) |
| 137 | +it.instance("provider loaded from env variable", () => |
| 138 | + Effect.gen(function* () { |
| 139 | + yield* setProcessEnv("ANTHROPIC_API_KEY", "test-api-key") |
| 140 | + const providers = yield* Provider.Service.use((provider) => provider.list()) |
| 141 | + expect(providers[ProviderID.anthropic]).toBeDefined() |
| 142 | + // Provider should retain its connection source even if custom loaders |
| 143 | + // merge additional options. |
| 144 | + expect(providers[ProviderID.anthropic].source).toBe("env") |
| 145 | + expect(providers[ProviderID.anthropic].options.headers["anthropic-beta"]).toBeDefined() |
| 146 | + }), |
| 147 | +) |
155 | 148 |
|
156 | | -test("provider loaded from config with apiKey option", async () => { |
157 | | - await using tmp = await tmpdir({ |
158 | | - init: async (dir) => { |
159 | | - await Bun.write( |
160 | | - path.join(dir, "opencode.json"), |
161 | | - JSON.stringify({ |
162 | | - $schema: "https://opencode.ai/config.json", |
163 | | - provider: { |
164 | | - anthropic: { |
165 | | - options: { |
166 | | - apiKey: "config-api-key", |
167 | | - }, |
168 | | - }, |
| 149 | +it.instance( |
| 150 | + "provider loaded from config with apiKey option", |
| 151 | + Effect.gen(function* () { |
| 152 | + const providers = yield* Provider.Service.use((provider) => provider.list()) |
| 153 | + expect(providers[ProviderID.anthropic]).toBeDefined() |
| 154 | + }), |
| 155 | + { |
| 156 | + config: { |
| 157 | + provider: { |
| 158 | + anthropic: { |
| 159 | + options: { |
| 160 | + apiKey: "config-api-key", |
169 | 161 | }, |
170 | | - }), |
171 | | - ) |
172 | | - }, |
173 | | - }) |
174 | | - await withTestInstance({ |
175 | | - directory: tmp.path, |
176 | | - fn: async (ctx) => { |
177 | | - const providers = await list(ctx) |
178 | | - expect(providers[ProviderID.anthropic]).toBeDefined() |
| 162 | + }, |
| 163 | + }, |
179 | 164 | }, |
180 | | - }) |
181 | | -}) |
| 165 | + }, |
| 166 | +) |
182 | 167 |
|
183 | | -test("disabled_providers excludes provider", async () => { |
184 | | - await using tmp = await tmpdir({ |
185 | | - init: async (dir) => { |
186 | | - await Bun.write( |
187 | | - path.join(dir, "opencode.json"), |
188 | | - JSON.stringify({ |
189 | | - $schema: "https://opencode.ai/config.json", |
190 | | - disabled_providers: ["anthropic"], |
191 | | - }), |
192 | | - ) |
193 | | - }, |
194 | | - }) |
195 | | - await withTestInstance({ |
196 | | - directory: tmp.path, |
197 | | - fn: async (ctx) => { |
198 | | - await set(ctx, "ANTHROPIC_API_KEY", "test-api-key") |
199 | | - const providers = await list(ctx) |
200 | | - expect(providers[ProviderID.anthropic]).toBeUndefined() |
201 | | - }, |
202 | | - }) |
203 | | -}) |
| 168 | +it.instance( |
| 169 | + "disabled_providers excludes provider", |
| 170 | + Effect.gen(function* () { |
| 171 | + yield* setProcessEnv("ANTHROPIC_API_KEY", "test-api-key") |
| 172 | + const providers = yield* Provider.Service.use((provider) => provider.list()) |
| 173 | + expect(providers[ProviderID.anthropic]).toBeUndefined() |
| 174 | + }), |
| 175 | + { config: { disabled_providers: ["anthropic"] } }, |
| 176 | +) |
204 | 177 |
|
205 | | -test("enabled_providers restricts to only listed providers", async () => { |
206 | | - await using tmp = await tmpdir({ |
207 | | - init: async (dir) => { |
208 | | - await Bun.write( |
209 | | - path.join(dir, "opencode.json"), |
210 | | - JSON.stringify({ |
211 | | - $schema: "https://opencode.ai/config.json", |
212 | | - enabled_providers: ["anthropic"], |
213 | | - }), |
214 | | - ) |
215 | | - }, |
216 | | - }) |
217 | | - await withTestInstance({ |
218 | | - directory: tmp.path, |
219 | | - fn: async (ctx) => { |
220 | | - await set(ctx, "ANTHROPIC_API_KEY", "test-api-key") |
221 | | - await set(ctx, "OPENAI_API_KEY", "test-openai-key") |
222 | | - const providers = await list(ctx) |
223 | | - expect(providers[ProviderID.anthropic]).toBeDefined() |
224 | | - expect(providers[ProviderID.openai]).toBeUndefined() |
225 | | - }, |
226 | | - }) |
227 | | -}) |
| 178 | +it.instance( |
| 179 | + "enabled_providers restricts to only listed providers", |
| 180 | + Effect.gen(function* () { |
| 181 | + yield* setProcessEnv("ANTHROPIC_API_KEY", "test-api-key") |
| 182 | + yield* setProcessEnv("OPENAI_API_KEY", "test-openai-key") |
| 183 | + const providers = yield* Provider.Service.use((provider) => provider.list()) |
| 184 | + expect(providers[ProviderID.anthropic]).toBeDefined() |
| 185 | + expect(providers[ProviderID.openai]).toBeUndefined() |
| 186 | + }), |
| 187 | + { config: { enabled_providers: ["anthropic"] } }, |
| 188 | +) |
228 | 189 |
|
229 | | -test("model whitelist filters models for provider", async () => { |
230 | | - await using tmp = await tmpdir({ |
231 | | - init: async (dir) => { |
232 | | - await Bun.write( |
233 | | - path.join(dir, "opencode.json"), |
234 | | - JSON.stringify({ |
235 | | - $schema: "https://opencode.ai/config.json", |
236 | | - provider: { |
237 | | - anthropic: { |
238 | | - whitelist: ["claude-sonnet-4-20250514"], |
239 | | - }, |
240 | | - }, |
241 | | - }), |
242 | | - ) |
243 | | - }, |
244 | | - }) |
245 | | - await withTestInstance({ |
246 | | - directory: tmp.path, |
247 | | - fn: async (ctx) => { |
248 | | - await set(ctx, "ANTHROPIC_API_KEY", "test-api-key") |
249 | | - const providers = await list(ctx) |
250 | | - expect(providers[ProviderID.anthropic]).toBeDefined() |
251 | | - const models = Object.keys(providers[ProviderID.anthropic].models) |
252 | | - expect(models).toContain("claude-sonnet-4-20250514") |
253 | | - expect(models.length).toBe(1) |
| 190 | +it.instance( |
| 191 | + "model whitelist filters models for provider", |
| 192 | + Effect.gen(function* () { |
| 193 | + yield* setProcessEnv("ANTHROPIC_API_KEY", "test-api-key") |
| 194 | + const providers = yield* Provider.Service.use((provider) => provider.list()) |
| 195 | + expect(providers[ProviderID.anthropic]).toBeDefined() |
| 196 | + const models = Object.keys(providers[ProviderID.anthropic].models) |
| 197 | + expect(models).toContain("claude-sonnet-4-20250514") |
| 198 | + expect(models.length).toBe(1) |
| 199 | + }), |
| 200 | + { |
| 201 | + config: { |
| 202 | + provider: { |
| 203 | + anthropic: { |
| 204 | + whitelist: ["claude-sonnet-4-20250514"], |
| 205 | + }, |
| 206 | + }, |
254 | 207 | }, |
255 | | - }) |
256 | | -}) |
| 208 | + }, |
| 209 | +) |
257 | 210 |
|
258 | | -test("model blacklist excludes specific models", async () => { |
259 | | - await using tmp = await tmpdir({ |
260 | | - init: async (dir) => { |
261 | | - await Bun.write( |
262 | | - path.join(dir, "opencode.json"), |
263 | | - JSON.stringify({ |
264 | | - $schema: "https://opencode.ai/config.json", |
265 | | - provider: { |
266 | | - anthropic: { |
267 | | - blacklist: ["claude-sonnet-4-20250514"], |
268 | | - }, |
269 | | - }, |
270 | | - }), |
271 | | - ) |
272 | | - }, |
273 | | - }) |
274 | | - await withTestInstance({ |
275 | | - directory: tmp.path, |
276 | | - fn: async (ctx) => { |
277 | | - await set(ctx, "ANTHROPIC_API_KEY", "test-api-key") |
278 | | - const providers = await list(ctx) |
279 | | - expect(providers[ProviderID.anthropic]).toBeDefined() |
280 | | - const models = Object.keys(providers[ProviderID.anthropic].models) |
281 | | - expect(models).not.toContain("claude-sonnet-4-20250514") |
| 211 | +it.instance( |
| 212 | + "model blacklist excludes specific models", |
| 213 | + Effect.gen(function* () { |
| 214 | + yield* setProcessEnv("ANTHROPIC_API_KEY", "test-api-key") |
| 215 | + const providers = yield* Provider.Service.use((provider) => provider.list()) |
| 216 | + expect(providers[ProviderID.anthropic]).toBeDefined() |
| 217 | + const models = Object.keys(providers[ProviderID.anthropic].models) |
| 218 | + expect(models).not.toContain("claude-sonnet-4-20250514") |
| 219 | + }), |
| 220 | + { |
| 221 | + config: { |
| 222 | + provider: { |
| 223 | + anthropic: { |
| 224 | + blacklist: ["claude-sonnet-4-20250514"], |
| 225 | + }, |
| 226 | + }, |
282 | 227 | }, |
283 | | - }) |
284 | | -}) |
| 228 | + }, |
| 229 | +) |
285 | 230 |
|
286 | 231 | test("custom model alias via config", async () => { |
287 | 232 | await using tmp = await tmpdir({ |
|