|
| 1 | +import { describe, test, expect, afterEach } from "bun:test" |
| 2 | +import path from "path" |
| 3 | +import fs from "fs/promises" |
| 4 | + |
| 5 | +import { tmpdir } from "../fixture/fixture" |
| 6 | +import { Instance } from "../../src/project/instance" |
| 7 | +import { Provider } from "../../src/provider/provider" |
| 8 | +import { Auth } from "../../src/auth" |
| 9 | +import { ProviderID } from "../../src/provider/schema" |
| 10 | + |
| 11 | +const PROVIDER_ID = ProviderID.make("altimate-backend") |
| 12 | + |
| 13 | +afterEach(async () => { |
| 14 | + await Auth.remove(PROVIDER_ID).catch(() => {}) |
| 15 | +}) |
| 16 | + |
| 17 | +describe("altimate-backend provider loader", () => { |
| 18 | + test("loads from ~/.altimate/altimate.json when it exists", async () => { |
| 19 | + await using tmp = await tmpdir({ |
| 20 | + init: async (dir) => { |
| 21 | + await Bun.write( |
| 22 | + path.join(dir, "opencode.json"), |
| 23 | + JSON.stringify({ $schema: "https://altimate.ai/config.json" }), |
| 24 | + ) |
| 25 | + }, |
| 26 | + }) |
| 27 | + const originalHome = process.env.OPENCODE_TEST_HOME |
| 28 | + process.env.OPENCODE_TEST_HOME = tmp.path |
| 29 | + |
| 30 | + // Write fake credentials file |
| 31 | + const altimateDir = path.join(tmp.path, ".altimate") |
| 32 | + await fs.mkdir(altimateDir, { recursive: true }) |
| 33 | + await Bun.write( |
| 34 | + path.join(altimateDir, "altimate.json"), |
| 35 | + JSON.stringify({ |
| 36 | + altimateUrl: "https://api.getaltimate.com", |
| 37 | + altimateInstanceName: "mycompany", |
| 38 | + altimateApiKey: "test-key-123", |
| 39 | + }), |
| 40 | + ) |
| 41 | + |
| 42 | + try { |
| 43 | + await Instance.provide({ |
| 44 | + directory: tmp.path, |
| 45 | + fn: async () => { |
| 46 | + const providers = await Provider.list() |
| 47 | + const p = providers["altimate-backend"] |
| 48 | + expect(p).toBeDefined() |
| 49 | + expect(p.options.baseURL).toBe("https://api.getaltimate.com/agents/v1") |
| 50 | + expect(p.options.apiKey).toBe("test-key-123") |
| 51 | + expect(p.options.headers["x-tenant"]).toBe("mycompany") |
| 52 | + }, |
| 53 | + }) |
| 54 | + } finally { |
| 55 | + if (originalHome !== undefined) { |
| 56 | + process.env.OPENCODE_TEST_HOME = originalHome |
| 57 | + } else { |
| 58 | + delete process.env.OPENCODE_TEST_HOME |
| 59 | + } |
| 60 | + } |
| 61 | + }) |
| 62 | + |
| 63 | + test("strips trailing slashes from altimateUrl in baseURL", async () => { |
| 64 | + await using tmp = await tmpdir({ |
| 65 | + init: async (dir) => { |
| 66 | + await Bun.write( |
| 67 | + path.join(dir, "opencode.json"), |
| 68 | + JSON.stringify({ $schema: "https://altimate.ai/config.json" }), |
| 69 | + ) |
| 70 | + }, |
| 71 | + }) |
| 72 | + const originalHome = process.env.OPENCODE_TEST_HOME |
| 73 | + process.env.OPENCODE_TEST_HOME = tmp.path |
| 74 | + |
| 75 | + const altimateDir = path.join(tmp.path, ".altimate") |
| 76 | + await fs.mkdir(altimateDir, { recursive: true }) |
| 77 | + await Bun.write( |
| 78 | + path.join(altimateDir, "altimate.json"), |
| 79 | + JSON.stringify({ |
| 80 | + altimateUrl: "https://api.getaltimate.com///", |
| 81 | + altimateInstanceName: "tenant", |
| 82 | + altimateApiKey: "key", |
| 83 | + }), |
| 84 | + ) |
| 85 | + |
| 86 | + try { |
| 87 | + await Instance.provide({ |
| 88 | + directory: tmp.path, |
| 89 | + fn: async () => { |
| 90 | + const providers = await Provider.list() |
| 91 | + const p = providers["altimate-backend"] |
| 92 | + expect(p).toBeDefined() |
| 93 | + // Trailing slashes should be stripped before appending /agents/v1 |
| 94 | + expect(p.options.baseURL).toBe("https://api.getaltimate.com/agents/v1") |
| 95 | + }, |
| 96 | + }) |
| 97 | + } finally { |
| 98 | + if (originalHome !== undefined) { |
| 99 | + process.env.OPENCODE_TEST_HOME = originalHome |
| 100 | + } else { |
| 101 | + delete process.env.OPENCODE_TEST_HOME |
| 102 | + } |
| 103 | + } |
| 104 | + }) |
| 105 | + |
| 106 | + test("falls back to auth store when config file is absent", async () => { |
| 107 | + await using tmp = await tmpdir({ |
| 108 | + init: async (dir) => { |
| 109 | + await Bun.write( |
| 110 | + path.join(dir, "opencode.json"), |
| 111 | + JSON.stringify({ $schema: "https://altimate.ai/config.json" }), |
| 112 | + ) |
| 113 | + }, |
| 114 | + }) |
| 115 | + const originalHome = process.env.OPENCODE_TEST_HOME |
| 116 | + process.env.OPENCODE_TEST_HOME = tmp.path |
| 117 | + |
| 118 | + try { |
| 119 | + await Instance.provide({ |
| 120 | + directory: tmp.path, |
| 121 | + fn: async () => { |
| 122 | + // No ~/.altimate/altimate.json exists — populate auth store instead |
| 123 | + await Auth.set(PROVIDER_ID, { |
| 124 | + type: "api", |
| 125 | + key: "https://api.getaltimate.com::mycompany::auth-key-456", |
| 126 | + }) |
| 127 | + |
| 128 | + const providers = await Provider.list() |
| 129 | + const p = providers["altimate-backend"] |
| 130 | + expect(p).toBeDefined() |
| 131 | + expect(p.options.baseURL).toBe("https://api.getaltimate.com/agents/v1") |
| 132 | + expect(p.options.apiKey).toBe("auth-key-456") |
| 133 | + expect(p.options.headers["x-tenant"]).toBe("mycompany") |
| 134 | + }, |
| 135 | + }) |
| 136 | + } finally { |
| 137 | + if (originalHome !== undefined) { |
| 138 | + process.env.OPENCODE_TEST_HOME = originalHome |
| 139 | + } else { |
| 140 | + delete process.env.OPENCODE_TEST_HOME |
| 141 | + } |
| 142 | + } |
| 143 | + }) |
| 144 | + |
| 145 | + test("cleans up stale auth when key format is invalid", async () => { |
| 146 | + await using tmp = await tmpdir({ |
| 147 | + init: async (dir) => { |
| 148 | + await Bun.write( |
| 149 | + path.join(dir, "opencode.json"), |
| 150 | + JSON.stringify({ $schema: "https://altimate.ai/config.json" }), |
| 151 | + ) |
| 152 | + }, |
| 153 | + }) |
| 154 | + const originalHome = process.env.OPENCODE_TEST_HOME |
| 155 | + process.env.OPENCODE_TEST_HOME = tmp.path |
| 156 | + |
| 157 | + try { |
| 158 | + await Instance.provide({ |
| 159 | + directory: tmp.path, |
| 160 | + fn: async () => { |
| 161 | + // Set an invalid key format (missing :: separators) |
| 162 | + await Auth.set(PROVIDER_ID, { |
| 163 | + type: "api", |
| 164 | + key: "not-a-valid-altimate-key", |
| 165 | + }) |
| 166 | + |
| 167 | + // First call triggers cleanup of stale auth entry |
| 168 | + await Provider.list() |
| 169 | + |
| 170 | + // After the loader cleaned up the invalid auth, subsequent calls |
| 171 | + // should not find the stale entry |
| 172 | + const auth = await Auth.get(PROVIDER_ID) |
| 173 | + expect(auth).toBeUndefined() |
| 174 | + }, |
| 175 | + }) |
| 176 | + } finally { |
| 177 | + if (originalHome !== undefined) { |
| 178 | + process.env.OPENCODE_TEST_HOME = originalHome |
| 179 | + } else { |
| 180 | + delete process.env.OPENCODE_TEST_HOME |
| 181 | + } |
| 182 | + } |
| 183 | + }) |
| 184 | + |
| 185 | + test("returns autoload:false when neither credentials file nor auth store exists", async () => { |
| 186 | + await using tmp = await tmpdir({ |
| 187 | + init: async (dir) => { |
| 188 | + await Bun.write( |
| 189 | + path.join(dir, "opencode.json"), |
| 190 | + JSON.stringify({ $schema: "https://altimate.ai/config.json" }), |
| 191 | + ) |
| 192 | + }, |
| 193 | + }) |
| 194 | + const originalHome = process.env.OPENCODE_TEST_HOME |
| 195 | + process.env.OPENCODE_TEST_HOME = tmp.path |
| 196 | + |
| 197 | + try { |
| 198 | + await Instance.provide({ |
| 199 | + directory: tmp.path, |
| 200 | + fn: async () => { |
| 201 | + const providers = await Provider.list() |
| 202 | + // Without any credentials, the provider should not autoload |
| 203 | + expect(providers["altimate-backend"]).toBeUndefined() |
| 204 | + }, |
| 205 | + }) |
| 206 | + } finally { |
| 207 | + if (originalHome !== undefined) { |
| 208 | + process.env.OPENCODE_TEST_HOME = originalHome |
| 209 | + } else { |
| 210 | + delete process.env.OPENCODE_TEST_HOME |
| 211 | + } |
| 212 | + } |
| 213 | + }) |
| 214 | + |
| 215 | + test("altimate-backend model registration includes correct defaults", async () => { |
| 216 | + await using tmp = await tmpdir({ |
| 217 | + init: async (dir) => { |
| 218 | + await Bun.write( |
| 219 | + path.join(dir, "opencode.json"), |
| 220 | + JSON.stringify({ $schema: "https://altimate.ai/config.json" }), |
| 221 | + ) |
| 222 | + }, |
| 223 | + }) |
| 224 | + const originalHome = process.env.OPENCODE_TEST_HOME |
| 225 | + process.env.OPENCODE_TEST_HOME = tmp.path |
| 226 | + |
| 227 | + const altimateDir = path.join(tmp.path, ".altimate") |
| 228 | + await fs.mkdir(altimateDir, { recursive: true }) |
| 229 | + await Bun.write( |
| 230 | + path.join(altimateDir, "altimate.json"), |
| 231 | + JSON.stringify({ |
| 232 | + altimateUrl: "https://api.getaltimate.com", |
| 233 | + altimateInstanceName: "tenant", |
| 234 | + altimateApiKey: "key", |
| 235 | + }), |
| 236 | + ) |
| 237 | + |
| 238 | + try { |
| 239 | + await Instance.provide({ |
| 240 | + directory: tmp.path, |
| 241 | + fn: async () => { |
| 242 | + const providers = await Provider.list() |
| 243 | + const p = providers["altimate-backend"] |
| 244 | + expect(p).toBeDefined() |
| 245 | + expect(p.name).toBe("Altimate") |
| 246 | + expect(p.source).toBe("custom") |
| 247 | + // Should have the altimate-default model |
| 248 | + expect(p.models["altimate-default"]).toBeDefined() |
| 249 | + expect(p.models["altimate-default"].name).toBe("Altimate AI") |
| 250 | + expect(p.models["altimate-default"].capabilities.toolcall).toBe(true) |
| 251 | + }, |
| 252 | + }) |
| 253 | + } finally { |
| 254 | + if (originalHome !== undefined) { |
| 255 | + process.env.OPENCODE_TEST_HOME = originalHome |
| 256 | + } else { |
| 257 | + delete process.env.OPENCODE_TEST_HOME |
| 258 | + } |
| 259 | + } |
| 260 | + }) |
| 261 | +}) |
0 commit comments