|
1 | 1 | import { test, expect } from "bun:test" |
2 | 2 | import path from "path" |
| 3 | +import fs from "fs/promises" |
3 | 4 |
|
4 | 5 | import { tmpdir } from "../fixture/fixture" |
5 | 6 | import { Instance } from "../../src/project/instance" |
6 | 7 | import { Provider } from "../../src/provider/provider" |
7 | 8 | import { ProviderID, ModelID } from "../../src/provider/schema" |
8 | 9 | import { Env } from "../../src/env" |
| 10 | +import { Global } from "../../src/global" |
9 | 11 |
|
10 | 12 | test("provider loaded from env variable", async () => { |
11 | 13 | await using tmp = await tmpdir({ |
@@ -2330,4 +2332,155 @@ test("github-copilot is excluded when CODESPACES=true and only GITHUB_TOKEN is s |
2330 | 2332 | }, |
2331 | 2333 | }) |
2332 | 2334 | }) |
| 2335 | + |
| 2336 | +// altimate_change start — tests for altimate-backend default model preference |
| 2337 | +test("defaultModel returns altimate-backend when altimate credentials exist and no model configured", async () => { |
| 2338 | + await using tmp = await tmpdir({ |
| 2339 | + init: async (dir) => { |
| 2340 | + await Bun.write( |
| 2341 | + path.join(dir, "opencode.json"), |
| 2342 | + JSON.stringify({ |
| 2343 | + $schema: "https://altimate.ai/config.json", |
| 2344 | + }), |
| 2345 | + ) |
| 2346 | + }, |
| 2347 | + }) |
| 2348 | + const originalHome = process.env.OPENCODE_TEST_HOME |
| 2349 | + process.env.OPENCODE_TEST_HOME = tmp.path |
| 2350 | + const altimateDir = path.join(tmp.path, ".altimate") |
| 2351 | + await fs.mkdir(altimateDir, { recursive: true }) |
| 2352 | + await Bun.write( |
| 2353 | + path.join(altimateDir, "altimate.json"), |
| 2354 | + JSON.stringify({ |
| 2355 | + altimateUrl: "https://test.altimate.ai", |
| 2356 | + altimateInstanceName: "test-instance", |
| 2357 | + altimateApiKey: "test-api-key", |
| 2358 | + }), |
| 2359 | + ) |
| 2360 | + try { |
| 2361 | + await Instance.provide({ |
| 2362 | + directory: tmp.path, |
| 2363 | + fn: async () => { |
| 2364 | + const model = await Provider.defaultModel() |
| 2365 | + expect(String(model.providerID)).toBe("altimate-backend") |
| 2366 | + expect(String(model.modelID)).toBe("altimate-default") |
| 2367 | + }, |
| 2368 | + }) |
| 2369 | + } finally { |
| 2370 | + process.env.OPENCODE_TEST_HOME = originalHome |
| 2371 | + } |
| 2372 | +}) |
| 2373 | + |
| 2374 | +test("defaultModel prefers altimate-backend over other providers when altimate is configured", async () => { |
| 2375 | + await using tmp = await tmpdir({ |
| 2376 | + init: async (dir) => { |
| 2377 | + await Bun.write( |
| 2378 | + path.join(dir, "opencode.json"), |
| 2379 | + JSON.stringify({ |
| 2380 | + $schema: "https://altimate.ai/config.json", |
| 2381 | + }), |
| 2382 | + ) |
| 2383 | + }, |
| 2384 | + }) |
| 2385 | + const originalHome = process.env.OPENCODE_TEST_HOME |
| 2386 | + process.env.OPENCODE_TEST_HOME = tmp.path |
| 2387 | + const altimateDir = path.join(tmp.path, ".altimate") |
| 2388 | + await fs.mkdir(altimateDir, { recursive: true }) |
| 2389 | + await Bun.write( |
| 2390 | + path.join(altimateDir, "altimate.json"), |
| 2391 | + JSON.stringify({ |
| 2392 | + altimateUrl: "https://test.altimate.ai", |
| 2393 | + altimateInstanceName: "test-instance", |
| 2394 | + altimateApiKey: "test-api-key", |
| 2395 | + }), |
| 2396 | + ) |
| 2397 | + try { |
| 2398 | + await Instance.provide({ |
| 2399 | + directory: tmp.path, |
| 2400 | + init: async () => { |
| 2401 | + Env.set("ANTHROPIC_API_KEY", "test-api-key") |
| 2402 | + }, |
| 2403 | + fn: async () => { |
| 2404 | + const providers = await Provider.list() |
| 2405 | + // Both providers should be available |
| 2406 | + expect(providers["anthropic"]).toBeDefined() |
| 2407 | + expect(providers["altimate-backend"]).toBeDefined() |
| 2408 | + // But defaultModel should prefer altimate-backend |
| 2409 | + const model = await Provider.defaultModel() |
| 2410 | + expect(String(model.providerID)).toBe("altimate-backend") |
| 2411 | + expect(String(model.modelID)).toBe("altimate-default") |
| 2412 | + }, |
| 2413 | + }) |
| 2414 | + } finally { |
| 2415 | + process.env.OPENCODE_TEST_HOME = originalHome |
| 2416 | + } |
| 2417 | +}) |
| 2418 | + |
| 2419 | +test("defaultModel respects explicit config model over altimate-backend", async () => { |
| 2420 | + await using tmp = await tmpdir({ |
| 2421 | + init: async (dir) => { |
| 2422 | + await Bun.write( |
| 2423 | + path.join(dir, "opencode.json"), |
| 2424 | + JSON.stringify({ |
| 2425 | + $schema: "https://altimate.ai/config.json", |
| 2426 | + model: "anthropic/claude-sonnet-4-20250514", |
| 2427 | + }), |
| 2428 | + ) |
| 2429 | + }, |
| 2430 | + }) |
| 2431 | + const originalHome = process.env.OPENCODE_TEST_HOME |
| 2432 | + process.env.OPENCODE_TEST_HOME = tmp.path |
| 2433 | + const altimateDir = path.join(tmp.path, ".altimate") |
| 2434 | + await fs.mkdir(altimateDir, { recursive: true }) |
| 2435 | + await Bun.write( |
| 2436 | + path.join(altimateDir, "altimate.json"), |
| 2437 | + JSON.stringify({ |
| 2438 | + altimateUrl: "https://test.altimate.ai", |
| 2439 | + altimateInstanceName: "test-instance", |
| 2440 | + altimateApiKey: "test-api-key", |
| 2441 | + }), |
| 2442 | + ) |
| 2443 | + try { |
| 2444 | + await Instance.provide({ |
| 2445 | + directory: tmp.path, |
| 2446 | + init: async () => { |
| 2447 | + Env.set("ANTHROPIC_API_KEY", "test-api-key") |
| 2448 | + }, |
| 2449 | + fn: async () => { |
| 2450 | + const model = await Provider.defaultModel() |
| 2451 | + expect(String(model.providerID)).toBe("anthropic") |
| 2452 | + expect(String(model.modelID)).toBe("claude-sonnet-4-20250514") |
| 2453 | + }, |
| 2454 | + }) |
| 2455 | + } finally { |
| 2456 | + process.env.OPENCODE_TEST_HOME = originalHome |
| 2457 | + } |
| 2458 | +}) |
| 2459 | + |
| 2460 | +test("defaultModel falls through to other providers when altimate is not configured", async () => { |
| 2461 | + await using tmp = await tmpdir({ |
| 2462 | + init: async (dir) => { |
| 2463 | + await Bun.write( |
| 2464 | + path.join(dir, "opencode.json"), |
| 2465 | + JSON.stringify({ |
| 2466 | + $schema: "https://altimate.ai/config.json", |
| 2467 | + }), |
| 2468 | + ) |
| 2469 | + }, |
| 2470 | + }) |
| 2471 | + await Instance.provide({ |
| 2472 | + directory: tmp.path, |
| 2473 | + init: async () => { |
| 2474 | + Env.set("ANTHROPIC_API_KEY", "test-api-key") |
| 2475 | + }, |
| 2476 | + fn: async () => { |
| 2477 | + const providers = await Provider.list() |
| 2478 | + // altimate-backend should NOT be available (no credentials file) |
| 2479 | + expect(providers["altimate-backend"]).toBeUndefined() |
| 2480 | + const model = await Provider.defaultModel() |
| 2481 | + // Should fall through to anthropic |
| 2482 | + expect(String(model.providerID)).toBe("anthropic") |
| 2483 | + }, |
| 2484 | + }) |
| 2485 | +}) |
2333 | 2486 | // altimate_change end |
0 commit comments