Skip to content

Commit 7a69d5b

Browse files
committed
test: connections — env-var loading and MongoDB auth detection
Cover untested CI/CD code path (ALTIMATE_CODE_CONN_* env vars) and new MongoDB detectAuthMethod branches added in PR #482. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> https://claude.ai/code/session_01Uf1H6t7768cFYAT9AbKSzr
1 parent abcaa1d commit 7a69d5b

2 files changed

Lines changed: 98 additions & 0 deletions

File tree

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// altimate_change start — unit tests for ConnectionRegistry loadFromEnv code path
2+
import { describe, test, expect, beforeEach, afterEach } from "bun:test"
3+
import * as Registry from "../../src/altimate/native/connections/registry"
4+
5+
// ---------------------------------------------------------------------------
6+
// loadFromEnv — env-var based connection configuration
7+
// ---------------------------------------------------------------------------
8+
// These tests exercise the ALTIMATE_CODE_CONN_* env-var parsing in
9+
// registry.ts → loadFromEnv(). CI/CD users rely on this to inject
10+
// warehouse connections without config files on disk.
11+
//
12+
// NOTE: Registry.load() also reads from ~/.altimate-code/connections.json
13+
// and .altimate-code/connections.json. The assertions below only check for
14+
// specific keys set via env vars, so unrelated entries from disk do not
15+
// interfere.
16+
// ---------------------------------------------------------------------------
17+
18+
describe("ConnectionRegistry: loadFromEnv", () => {
19+
// Track env vars we set so we can clean them up
20+
const envVarsSet: string[] = []
21+
22+
function setEnv(key: string, value: string) {
23+
process.env[key] = value
24+
envVarsSet.push(key)
25+
}
26+
27+
beforeEach(() => {
28+
Registry.reset()
29+
})
30+
31+
afterEach(() => {
32+
for (const key of envVarsSet) {
33+
delete process.env[key]
34+
}
35+
envVarsSet.length = 0
36+
})
37+
38+
test("parses valid ALTIMATE_CODE_CONN_* env var and lowercases the name", () => {
39+
setEnv("ALTIMATE_CODE_CONN_MYDB", JSON.stringify({ type: "postgres", host: "localhost", port: 5432 }))
40+
Registry.load()
41+
42+
const config = Registry.getConfig("mydb")
43+
expect(config).toBeDefined()
44+
expect(config?.type).toBe("postgres")
45+
expect(config?.host).toBe("localhost")
46+
})
47+
48+
test("ignores malformed JSON in env var without crashing", () => {
49+
setEnv("ALTIMATE_CODE_CONN_BAD", "not-valid-json{{{")
50+
Registry.load()
51+
52+
// The malformed env var should be silently skipped
53+
expect(Registry.getConfig("bad")).toBeUndefined()
54+
})
55+
56+
test("ignores env var config objects missing the type field", () => {
57+
setEnv("ALTIMATE_CODE_CONN_NOTYPE", JSON.stringify({ host: "localhost", port: 5432 }))
58+
Registry.load()
59+
60+
// Without a type field, the config should not be registered
61+
expect(Registry.getConfig("notype")).toBeUndefined()
62+
})
63+
64+
test("ignores env vars with empty values", () => {
65+
setEnv("ALTIMATE_CODE_CONN_EMPTY", "")
66+
Registry.load()
67+
68+
expect(Registry.getConfig("empty")).toBeUndefined()
69+
})
70+
71+
test("parses multiple env var connections", () => {
72+
setEnv("ALTIMATE_CODE_CONN_PG", JSON.stringify({ type: "postgres", host: "pg.example.com" }))
73+
setEnv("ALTIMATE_CODE_CONN_SF", JSON.stringify({ type: "snowflake", account: "abc123" }))
74+
Registry.load()
75+
76+
expect(Registry.getConfig("pg")?.type).toBe("postgres")
77+
expect(Registry.getConfig("sf")?.type).toBe("snowflake")
78+
})
79+
})
80+
// altimate_change end

packages/opencode/test/altimate/warehouse-telemetry.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,24 @@ describe("warehouse telemetry: detectAuthMethod", () => {
158158
expect(connectEvent.auth_method).toBe("file")
159159
})
160160

161+
// altimate_change start — MongoDB detectAuthMethod coverage (PR #482)
162+
// These call detectAuthMethod directly rather than going through Registry.get(),
163+
// because the MongoDB driver import + connect would time out in test environments.
164+
test("detects connection_string auth for mongodb without password", () => {
165+
expect(Registry.detectAuthMethod({ type: "mongodb", host: "localhost" })).toBe("connection_string")
166+
})
167+
168+
test("detects connection_string auth for mongo alias without password", () => {
169+
expect(Registry.detectAuthMethod({ type: "mongo", host: "localhost" })).toBe("connection_string")
170+
})
171+
172+
test("detects password auth for mongodb with password", () => {
173+
// Note: the generic password check (line 226) fires before the mongo branch,
174+
// but the result is the same — "password". This verifies the overall behavior.
175+
expect(Registry.detectAuthMethod({ type: "mongodb", password: "secret" })).toBe("password")
176+
})
177+
// altimate_change end
178+
161179
test("returns unknown for unrecognized auth", async () => {
162180
Registry.setConfigs({
163181
mystery: { type: "unsupported_db_type", host: "localhost" },

0 commit comments

Comments
 (0)