Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions packages/opencode/test/altimate/registry-env.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// altimate_change start — unit tests for ConnectionRegistry loadFromEnv code path
import { describe, test, expect, beforeEach, afterEach } from "bun:test"
import * as Registry from "../../src/altimate/native/connections/registry"

// ---------------------------------------------------------------------------
// loadFromEnv — env-var based connection configuration
// ---------------------------------------------------------------------------
// These tests exercise the ALTIMATE_CODE_CONN_* env-var parsing in
// registry.ts → loadFromEnv(). CI/CD users rely on this to inject
// warehouse connections without config files on disk.
//
// NOTE: Registry.load() also reads from ~/.altimate-code/connections.json
// and .altimate-code/connections.json. The assertions below only check for
// specific keys set via env vars, so unrelated entries from disk do not
// interfere.
// ---------------------------------------------------------------------------

describe("ConnectionRegistry: loadFromEnv", () => {
// Track env vars we set so we can clean them up
const envVarsSet: string[] = []

function setEnv(key: string, value: string) {
process.env[key] = value
envVarsSet.push(key)
}

beforeEach(() => {
Registry.reset()
})

afterEach(() => {
for (const key of envVarsSet) {
delete process.env[key]
}
envVarsSet.length = 0
})

test("parses valid ALTIMATE_CODE_CONN_* env var and lowercases the name", () => {
setEnv("ALTIMATE_CODE_CONN_MYDB", JSON.stringify({ type: "postgres", host: "localhost", port: 5432 }))
Registry.load()

const config = Registry.getConfig("mydb")
expect(config).toBeDefined()
expect(config?.type).toBe("postgres")
expect(config?.host).toBe("localhost")
})

test("ignores malformed JSON in env var without crashing", () => {
setEnv("ALTIMATE_CODE_CONN_BAD", "not-valid-json{{{")
Registry.load()

// The malformed env var should be silently skipped
expect(Registry.getConfig("bad")).toBeUndefined()
})

test("ignores env var config objects missing the type field", () => {
setEnv("ALTIMATE_CODE_CONN_NOTYPE", JSON.stringify({ host: "localhost", port: 5432 }))
Registry.load()

// Without a type field, the config should not be registered
expect(Registry.getConfig("notype")).toBeUndefined()
})

test("ignores env vars with empty values", () => {
setEnv("ALTIMATE_CODE_CONN_EMPTY", "")
Registry.load()

expect(Registry.getConfig("empty")).toBeUndefined()
})

test("parses multiple env var connections", () => {
setEnv("ALTIMATE_CODE_CONN_PG", JSON.stringify({ type: "postgres", host: "pg.example.com" }))
setEnv("ALTIMATE_CODE_CONN_SF", JSON.stringify({ type: "snowflake", account: "abc123" }))
Registry.load()

expect(Registry.getConfig("pg")?.type).toBe("postgres")
expect(Registry.getConfig("sf")?.type).toBe("snowflake")
})
})
// altimate_change end
18 changes: 18 additions & 0 deletions packages/opencode/test/altimate/warehouse-telemetry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,24 @@ describe("warehouse telemetry: detectAuthMethod", () => {
expect(connectEvent.auth_method).toBe("file")
})

// altimate_change start — MongoDB detectAuthMethod coverage (PR #482)
// These call detectAuthMethod directly rather than going through Registry.get(),
// because the MongoDB driver import + connect would time out in test environments.
test("detects connection_string auth for mongodb without password", () => {
expect(Registry.detectAuthMethod({ type: "mongodb", host: "localhost" })).toBe("connection_string")
})

test("detects connection_string auth for mongo alias without password", () => {
expect(Registry.detectAuthMethod({ type: "mongo", host: "localhost" })).toBe("connection_string")
})

test("detects password auth for mongodb with password", () => {
// Note: the generic password check (line 226) fires before the mongo branch,
// but the result is the same — "password". This verifies the overall behavior.
expect(Registry.detectAuthMethod({ type: "mongodb", password: "secret" })).toBe("password")
})
// altimate_change end

test("returns unknown for unrecognized auth", async () => {
Registry.setConfigs({
mystery: { type: "unsupported_db_type", host: "localhost" },
Expand Down
Loading