Skip to content

Commit bc7c85b

Browse files
committed
test: connection registry — MongoDB auth detection and env var loading
Cover untested pure functions in the connection registry: detectAuthMethod MongoDB paths (added in #482) and loadFromEnv parsing via public API. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com> https://claude.ai/code/session_016v7ah9WzhYe5veNDx1hh8W
1 parent abcaa1d commit bc7c85b

1 file changed

Lines changed: 170 additions & 0 deletions

File tree

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
// altimate_change start — unit tests for connection registry pure functions
2+
import { describe, test, expect, beforeEach, afterEach } from "bun:test"
3+
import {
4+
detectAuthMethod,
5+
categorizeConnectionError,
6+
reset,
7+
load,
8+
getConfig,
9+
list,
10+
setConfigs,
11+
} from "../../src/altimate/native/connections/registry"
12+
13+
// ---------------------------------------------------------------------------
14+
// 1. detectAuthMethod — MongoDB paths (added in #482, zero coverage)
15+
// ---------------------------------------------------------------------------
16+
17+
describe("detectAuthMethod: MongoDB", () => {
18+
test('returns "password" when mongodb config has a password', () => {
19+
// Note: this actually hits the generic `config.password` check (line 226)
20+
// before the MongoDB-specific branch, but the behavior is still correct
21+
// and worth pinning.
22+
expect(detectAuthMethod({ type: "mongodb", password: "secret" })).toBe("password")
23+
})
24+
25+
test('returns "connection_string" when mongodb config has no password', () => {
26+
// This is the MongoDB-specific branch at line 229 of registry.ts
27+
expect(detectAuthMethod({ type: "mongodb" })).toBe("connection_string")
28+
})
29+
30+
test('returns "connection_string" when mongo (alias) has no password', () => {
31+
expect(detectAuthMethod({ type: "mongo" })).toBe("connection_string")
32+
})
33+
34+
test('returns "password" when mongo (alias) has a password', () => {
35+
expect(detectAuthMethod({ type: "mongo", password: "secret" })).toBe("password")
36+
})
37+
38+
test('returns "connection_string" when mongodb has connection_string field', () => {
39+
// connection_string check (line 216) fires BEFORE the type check,
40+
// so this should return "connection_string" — verify the priority is correct
41+
expect(detectAuthMethod({ type: "mongodb", connection_string: "mongodb://localhost" })).toBe("connection_string")
42+
})
43+
44+
test('returns "token" when mongodb has access_token', () => {
45+
// Token check fires before MongoDB type check
46+
expect(detectAuthMethod({ type: "mongodb", access_token: "tok" })).toBe("token")
47+
})
48+
})
49+
50+
// ---------------------------------------------------------------------------
51+
// 2. loadFromEnv via public API (reset + load + getConfig)
52+
// ---------------------------------------------------------------------------
53+
54+
describe("loadFromEnv via public API", () => {
55+
const envVars: string[] = []
56+
57+
function setEnv(key: string, value: string) {
58+
process.env[key] = value
59+
envVars.push(key)
60+
}
61+
62+
beforeEach(() => {
63+
reset()
64+
})
65+
66+
afterEach(() => {
67+
for (const key of envVars) {
68+
delete process.env[key]
69+
}
70+
envVars.length = 0
71+
reset()
72+
})
73+
74+
test("parses valid ALTIMATE_CODE_CONN_* env var into a connection config", () => {
75+
setEnv("ALTIMATE_CODE_CONN_MYDB", JSON.stringify({ type: "postgres", host: "localhost", port: 5432 }))
76+
load()
77+
const config = getConfig("mydb")
78+
expect(config).toBeDefined()
79+
expect(config!.type).toBe("postgres")
80+
expect(config!.host).toBe("localhost")
81+
})
82+
83+
test("lowercases the connection name from env var suffix", () => {
84+
setEnv("ALTIMATE_CODE_CONN_MYUPPERDB", JSON.stringify({ type: "postgres" }))
85+
load()
86+
// The name should be lowercased
87+
expect(getConfig("myupperdb")).toBeDefined()
88+
// Original case should not exist as a separate entry
89+
expect(getConfig("MYUPPERDB")).toBeUndefined()
90+
})
91+
92+
test("ignores env var with malformed JSON", () => {
93+
setEnv("ALTIMATE_CODE_CONN_BAD", "not valid json {{{")
94+
load()
95+
expect(getConfig("bad")).toBeUndefined()
96+
})
97+
98+
test("ignores env var with missing type field", () => {
99+
setEnv("ALTIMATE_CODE_CONN_NOTYPE", JSON.stringify({ host: "localhost" }))
100+
load()
101+
expect(getConfig("notype")).toBeUndefined()
102+
})
103+
104+
test("ignores env var with null JSON value", () => {
105+
setEnv("ALTIMATE_CODE_CONN_NULLVAL", "null")
106+
load()
107+
expect(getConfig("nullval")).toBeUndefined()
108+
})
109+
110+
test("ignores env var with empty string value", () => {
111+
// Empty string is falsy, so the `if (!value) continue` guard should skip it
112+
process.env["ALTIMATE_CODE_CONN_EMPTY"] = ""
113+
envVars.push("ALTIMATE_CODE_CONN_EMPTY")
114+
load()
115+
expect(getConfig("empty")).toBeUndefined()
116+
})
117+
118+
test("env vars are included in list() output", () => {
119+
setEnv("ALTIMATE_CODE_CONN_ENVDB", JSON.stringify({ type: "duckdb", database: "test.db" }))
120+
load()
121+
const { warehouses } = list()
122+
const found = warehouses.find((w) => w.name === "envdb")
123+
expect(found).toBeDefined()
124+
expect(found!.type).toBe("duckdb")
125+
})
126+
})
127+
128+
// ---------------------------------------------------------------------------
129+
// 3. setConfigs + list round-trip (public API sanity)
130+
// ---------------------------------------------------------------------------
131+
132+
describe("setConfigs and list", () => {
133+
beforeEach(() => {
134+
reset()
135+
})
136+
137+
afterEach(() => {
138+
reset()
139+
})
140+
141+
test("setConfigs populates configs readable via getConfig", () => {
142+
setConfigs({
143+
prod: { type: "postgres", host: "prod.example.com" },
144+
staging: { type: "snowflake", account: "acme" },
145+
})
146+
expect(getConfig("prod")).toBeDefined()
147+
expect(getConfig("prod")!.type).toBe("postgres")
148+
expect(getConfig("staging")).toBeDefined()
149+
expect(getConfig("staging")!.type).toBe("snowflake")
150+
})
151+
152+
test("list returns all configured warehouses", () => {
153+
setConfigs({
154+
a: { type: "postgres" },
155+
b: { type: "mongodb", database: "mydb" },
156+
})
157+
const { warehouses } = list()
158+
expect(warehouses).toHaveLength(2)
159+
const names = warehouses.map((w) => w.name).sort()
160+
expect(names).toEqual(["a", "b"])
161+
})
162+
163+
test("setConfigs clears previous configs", () => {
164+
setConfigs({ old: { type: "postgres" } })
165+
setConfigs({ new: { type: "duckdb" } })
166+
expect(getConfig("old")).toBeUndefined()
167+
expect(getConfig("new")).toBeDefined()
168+
})
169+
})
170+
// altimate_change end

0 commit comments

Comments
 (0)