Skip to content

Commit 1d84cbf

Browse files
committed
test: ClickHouse env detection and Altimate provider resolver
Cover two untested code paths added this week: ClickHouse warehouse auto-detection via env vars (PR #574) and the altimate-backend provider credential resolver (PR #606). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> https://claude.ai/code/session_01JpvxVa5gFNv3Y7hQrtjo8U
1 parent 0d34855 commit 1d84cbf

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

packages/opencode/test/provider/provider.test.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { test, expect } from "bun:test"
22
import path from "path"
3+
import fsp from "fs/promises"
34

45
import { tmpdir } from "../fixture/fixture"
56
import { Instance } from "../../src/project/instance"
@@ -2331,3 +2332,57 @@ test("github-copilot is excluded when CODESPACES=true and only GITHUB_TOKEN is s
23312332
})
23322333
})
23332334
// altimate_change end
2335+
2336+
// altimate_change start — test altimate-backend provider resolver
2337+
test("altimate-backend provider loaded from credential file", async () => {
2338+
await using tmp = await tmpdir({ config: {} })
2339+
const savedTestHome = process.env.OPENCODE_TEST_HOME
2340+
try {
2341+
process.env.OPENCODE_TEST_HOME = tmp.path
2342+
const credsDir = path.join(tmp.path, ".altimate")
2343+
await fsp.mkdir(credsDir, { recursive: true })
2344+
await fsp.writeFile(
2345+
path.join(credsDir, "altimate.json"),
2346+
JSON.stringify({
2347+
altimateUrl: "https://api.getaltimate.com/",
2348+
altimateInstanceName: "testco",
2349+
altimateApiKey: "test-key-123",
2350+
}),
2351+
)
2352+
await Instance.provide({
2353+
directory: tmp.path,
2354+
fn: async () => {
2355+
const providers = await Provider.list()
2356+
const ab = providers["altimate-backend"]
2357+
expect(ab).toBeDefined()
2358+
// getCredentials() strips trailing slash, then resolver appends /agents/v1
2359+
expect(ab.options.baseURL).toBe("https://api.getaltimate.com/agents/v1")
2360+
expect(ab.options.apiKey).toBe("test-key-123")
2361+
expect(ab.options.headers["x-tenant"]).toBe("testco")
2362+
},
2363+
})
2364+
} finally {
2365+
if (savedTestHome === undefined) delete process.env.OPENCODE_TEST_HOME
2366+
else process.env.OPENCODE_TEST_HOME = savedTestHome
2367+
}
2368+
})
2369+
2370+
test("altimate-backend provider not loaded when no credentials exist", async () => {
2371+
await using tmp = await tmpdir({ config: {} })
2372+
const savedTestHome = process.env.OPENCODE_TEST_HOME
2373+
try {
2374+
// Point to empty temp dir — no .altimate/altimate.json exists
2375+
process.env.OPENCODE_TEST_HOME = tmp.path
2376+
await Instance.provide({
2377+
directory: tmp.path,
2378+
fn: async () => {
2379+
const providers = await Provider.list()
2380+
expect(providers["altimate-backend"]).toBeUndefined()
2381+
},
2382+
})
2383+
} finally {
2384+
if (savedTestHome === undefined) delete process.env.OPENCODE_TEST_HOME
2385+
else process.env.OPENCODE_TEST_HOME = savedTestHome
2386+
}
2387+
})
2388+
// altimate_change end

packages/opencode/test/tool/project-scan.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,8 @@ describe("detectEnvVars", () => {
312312
"PGHOST", "PGPORT", "PGDATABASE", "PGUSER", "PGPASSWORD", "DATABASE_URL",
313313
"MYSQL_HOST", "MYSQL_TCP_PORT", "MYSQL_DATABASE", "MYSQL_USER", "MYSQL_PASSWORD",
314314
"REDSHIFT_HOST", "REDSHIFT_PORT", "REDSHIFT_DATABASE", "REDSHIFT_USER", "REDSHIFT_PASSWORD",
315+
"CLICKHOUSE_HOST", "CLICKHOUSE_URL", "CLICKHOUSE_PORT", "CLICKHOUSE_DB",
316+
"CLICKHOUSE_DATABASE", "CLICKHOUSE_USER", "CLICKHOUSE_USERNAME", "CLICKHOUSE_PASSWORD",
315317
]
316318
for (const v of vars) {
317319
delete process.env[v]
@@ -554,6 +556,49 @@ describe("detectEnvVars", () => {
554556
expect(conn.name).toBe(`env_${conn.type}`)
555557
}
556558
})
559+
560+
test("detects ClickHouse via CLICKHOUSE_HOST", async () => {
561+
clearWarehouseEnvVars()
562+
process.env.CLICKHOUSE_HOST = "ch.example.com"
563+
process.env.CLICKHOUSE_PORT = "8123"
564+
process.env.CLICKHOUSE_DATABASE = "analytics"
565+
process.env.CLICKHOUSE_USER = "default"
566+
567+
const result = await detectEnvVars()
568+
const ch = result.find((r) => r.type === "clickhouse")
569+
expect(ch).toBeDefined()
570+
expect(ch!.name).toBe("env_clickhouse")
571+
expect(ch!.source).toBe("env-var")
572+
expect(ch!.signal).toBe("CLICKHOUSE_HOST")
573+
expect(ch!.config.host).toBe("ch.example.com")
574+
expect(ch!.config.port).toBe("8123")
575+
expect(ch!.config.database).toBe("analytics")
576+
expect(ch!.config.user).toBe("default")
577+
})
578+
579+
test("detects ClickHouse via CLICKHOUSE_URL", async () => {
580+
clearWarehouseEnvVars()
581+
process.env.CLICKHOUSE_URL = "https://ch.example.com:8443"
582+
583+
const result = await detectEnvVars()
584+
const ch = result.find((r) => r.type === "clickhouse")
585+
expect(ch).toBeDefined()
586+
expect(ch!.name).toBe("env_clickhouse")
587+
expect(ch!.source).toBe("env-var")
588+
expect(ch!.signal).toBe("CLICKHOUSE_URL")
589+
expect(ch!.config.connection_string).toBe("***")
590+
})
591+
592+
test("detects ClickHouse via DATABASE_URL with clickhouse scheme", async () => {
593+
clearWarehouseEnvVars()
594+
process.env.DATABASE_URL = "clickhouse://user:pass@host:9000/db"
595+
596+
const result = await detectEnvVars()
597+
const ch = result.find((r) => r.type === "clickhouse")
598+
expect(ch).toBeDefined()
599+
expect(ch!.signal).toBe("DATABASE_URL")
600+
expect(ch!.config.connection_string).toBe("***")
601+
})
557602
})
558603

559604
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)