Skip to content

Commit d5ec01b

Browse files
committed
test: warehouse connections — env-var loading and MongoDB auth detection
Cover two untested code paths in the connection registry: 1. ALTIMATE_CODE_CONN_* env var parsing (CI/Docker users) 2. MongoDB-specific detectAuthMethod branches added in #482 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> https://claude.ai/code/session_015egWH6W5HdaVuMysv9tFHq
1 parent abcaa1d commit d5ec01b

2 files changed

Lines changed: 82 additions & 1 deletion

File tree

packages/opencode/test/altimate/connections.test.ts

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, expect, test, beforeEach, beforeAll, afterAll } from "bun:test"
1+
import { describe, expect, test, beforeEach, afterEach, beforeAll, afterAll } from "bun:test"
22
import * as Dispatcher from "../../src/altimate/native/dispatcher"
33

44
// Disable telemetry via env var instead of mock.module
@@ -74,6 +74,70 @@ describe("ConnectionRegistry", () => {
7474
})
7575
})
7676

77+
// ---------------------------------------------------------------------------
78+
// loadFromEnv — env-var-based connection config loading
79+
// ---------------------------------------------------------------------------
80+
81+
describe("loadFromEnv via Registry.load()", () => {
82+
const saved: Record<string, string | undefined> = {}
83+
84+
function setEnv(key: string, value: string) {
85+
saved[key] = process.env[key]
86+
process.env[key] = value
87+
}
88+
89+
beforeEach(() => {
90+
Registry.reset()
91+
})
92+
93+
afterEach(() => {
94+
for (const [key, orig] of Object.entries(saved)) {
95+
if (orig === undefined) delete process.env[key]
96+
else process.env[key] = orig
97+
}
98+
// Clear saved state for next test
99+
for (const key of Object.keys(saved)) delete saved[key]
100+
})
101+
102+
test("parses valid JSON from ALTIMATE_CODE_CONN_* env vars", () => {
103+
setEnv("ALTIMATE_CODE_CONN_MYDB", JSON.stringify({ type: "postgres", host: "localhost", port: 5432 }))
104+
Registry.load()
105+
const config = Registry.getConfig("mydb")
106+
expect(config).toBeDefined()
107+
expect(config?.type).toBe("postgres")
108+
expect(config?.host).toBe("localhost")
109+
})
110+
111+
test("lowercases connection name from env var suffix", () => {
112+
setEnv("ALTIMATE_CODE_CONN_PROD_DB", JSON.stringify({ type: "snowflake", account: "abc" }))
113+
Registry.load()
114+
expect(Registry.getConfig("prod_db")).toBeDefined()
115+
expect(Registry.getConfig("PROD_DB")).toBeUndefined()
116+
})
117+
118+
test("ignores env var with invalid JSON", () => {
119+
setEnv("ALTIMATE_CODE_CONN_BAD", "not-valid-json{")
120+
Registry.load()
121+
expect(Registry.getConfig("bad")).toBeUndefined()
122+
})
123+
124+
test("ignores env var config without type field", () => {
125+
setEnv("ALTIMATE_CODE_CONN_NOTYPE", JSON.stringify({ host: "localhost", port: 5432 }))
126+
Registry.load()
127+
expect(Registry.getConfig("notype")).toBeUndefined()
128+
})
129+
130+
test("ignores non-object JSON values (string, number, array)", () => {
131+
setEnv("ALTIMATE_CODE_CONN_STR", JSON.stringify("just a string"))
132+
setEnv("ALTIMATE_CODE_CONN_NUM", JSON.stringify(42))
133+
setEnv("ALTIMATE_CODE_CONN_ARR", JSON.stringify([1, 2, 3]))
134+
Registry.load()
135+
expect(Registry.getConfig("str")).toBeUndefined()
136+
expect(Registry.getConfig("num")).toBeUndefined()
137+
expect(Registry.getConfig("arr")).toBeUndefined()
138+
})
139+
})
140+
77141
// ---------------------------------------------------------------------------
78142
// CredentialStore (keytar not available in test environment)
79143
// ---------------------------------------------------------------------------

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,23 @@ describe("warehouse telemetry: detectAuthMethod", () => {
169169
expect(connectEvent).toBeDefined()
170170
expect(connectEvent.auth_method).toBe("unknown")
171171
})
172+
173+
// MongoDB-specific auth detection (added with MongoDB driver support #482)
174+
test("detects connection_string auth for mongodb without password", () => {
175+
expect(Registry.detectAuthMethod({ type: "mongodb", host: "localhost" } as any)).toBe("connection_string")
176+
})
177+
178+
test("detects password auth for mongodb with password", () => {
179+
expect(Registry.detectAuthMethod({ type: "mongodb", host: "localhost", password: "secret" } as any)).toBe("password")
180+
})
181+
182+
test("detects connection_string auth for mongo alias without password", () => {
183+
expect(Registry.detectAuthMethod({ type: "mongo", host: "localhost" } as any)).toBe("connection_string")
184+
})
185+
186+
test("prefers explicit connection_string field over mongodb type fallback", () => {
187+
expect(Registry.detectAuthMethod({ type: "mongodb", connection_string: "mongodb://localhost/test" } as any)).toBe("connection_string")
188+
})
172189
})
173190

174191
// ---------------------------------------------------------------------------

0 commit comments

Comments
 (0)