From 7a69d5b658258ec79ebf31f7ff8338a01bae117c Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Mar 2026 08:21:47 +0000 Subject: [PATCH] =?UTF-8?q?test:=20connections=20=E2=80=94=20env-var=20loa?= =?UTF-8?q?ding=20and=20MongoDB=20auth=20detection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) https://claude.ai/code/session_01Uf1H6t7768cFYAT9AbKSzr --- .../test/altimate/registry-env.test.ts | 80 +++++++++++++++++++ .../test/altimate/warehouse-telemetry.test.ts | 18 +++++ 2 files changed, 98 insertions(+) create mode 100644 packages/opencode/test/altimate/registry-env.test.ts diff --git a/packages/opencode/test/altimate/registry-env.test.ts b/packages/opencode/test/altimate/registry-env.test.ts new file mode 100644 index 0000000000..a8a053fadd --- /dev/null +++ b/packages/opencode/test/altimate/registry-env.test.ts @@ -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 diff --git a/packages/opencode/test/altimate/warehouse-telemetry.test.ts b/packages/opencode/test/altimate/warehouse-telemetry.test.ts index ccc7a4c13d..995c076705 100644 --- a/packages/opencode/test/altimate/warehouse-telemetry.test.ts +++ b/packages/opencode/test/altimate/warehouse-telemetry.test.ts @@ -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" },