From d5ec01b9b766dfb8d29559cc9c8652084325cd19 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Mar 2026 22:11:19 +0000 Subject: [PATCH] =?UTF-8?q?test:=20warehouse=20connections=20=E2=80=94=20e?= =?UTF-8?q?nv-var=20loading=20and=20MongoDB=20auth=20detection?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) https://claude.ai/code/session_015egWH6W5HdaVuMysv9tFHq --- .../test/altimate/connections.test.ts | 66 ++++++++++++++++++- .../test/altimate/warehouse-telemetry.test.ts | 17 +++++ 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/packages/opencode/test/altimate/connections.test.ts b/packages/opencode/test/altimate/connections.test.ts index e5facc6db0..0066f329ae 100644 --- a/packages/opencode/test/altimate/connections.test.ts +++ b/packages/opencode/test/altimate/connections.test.ts @@ -1,4 +1,4 @@ -import { describe, expect, test, beforeEach, beforeAll, afterAll } from "bun:test" +import { describe, expect, test, beforeEach, afterEach, beforeAll, afterAll } from "bun:test" import * as Dispatcher from "../../src/altimate/native/dispatcher" // Disable telemetry via env var instead of mock.module @@ -74,6 +74,70 @@ describe("ConnectionRegistry", () => { }) }) +// --------------------------------------------------------------------------- +// loadFromEnv — env-var-based connection config loading +// --------------------------------------------------------------------------- + +describe("loadFromEnv via Registry.load()", () => { + const saved: Record = {} + + function setEnv(key: string, value: string) { + saved[key] = process.env[key] + process.env[key] = value + } + + beforeEach(() => { + Registry.reset() + }) + + afterEach(() => { + for (const [key, orig] of Object.entries(saved)) { + if (orig === undefined) delete process.env[key] + else process.env[key] = orig + } + // Clear saved state for next test + for (const key of Object.keys(saved)) delete saved[key] + }) + + test("parses valid JSON from ALTIMATE_CODE_CONN_* env vars", () => { + 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("lowercases connection name from env var suffix", () => { + setEnv("ALTIMATE_CODE_CONN_PROD_DB", JSON.stringify({ type: "snowflake", account: "abc" })) + Registry.load() + expect(Registry.getConfig("prod_db")).toBeDefined() + expect(Registry.getConfig("PROD_DB")).toBeUndefined() + }) + + test("ignores env var with invalid JSON", () => { + setEnv("ALTIMATE_CODE_CONN_BAD", "not-valid-json{") + Registry.load() + expect(Registry.getConfig("bad")).toBeUndefined() + }) + + test("ignores env var config without type field", () => { + setEnv("ALTIMATE_CODE_CONN_NOTYPE", JSON.stringify({ host: "localhost", port: 5432 })) + Registry.load() + expect(Registry.getConfig("notype")).toBeUndefined() + }) + + test("ignores non-object JSON values (string, number, array)", () => { + setEnv("ALTIMATE_CODE_CONN_STR", JSON.stringify("just a string")) + setEnv("ALTIMATE_CODE_CONN_NUM", JSON.stringify(42)) + setEnv("ALTIMATE_CODE_CONN_ARR", JSON.stringify([1, 2, 3])) + Registry.load() + expect(Registry.getConfig("str")).toBeUndefined() + expect(Registry.getConfig("num")).toBeUndefined() + expect(Registry.getConfig("arr")).toBeUndefined() + }) +}) + // --------------------------------------------------------------------------- // CredentialStore (keytar not available in test environment) // --------------------------------------------------------------------------- diff --git a/packages/opencode/test/altimate/warehouse-telemetry.test.ts b/packages/opencode/test/altimate/warehouse-telemetry.test.ts index ccc7a4c13d..6c566c5f6b 100644 --- a/packages/opencode/test/altimate/warehouse-telemetry.test.ts +++ b/packages/opencode/test/altimate/warehouse-telemetry.test.ts @@ -169,6 +169,23 @@ describe("warehouse telemetry: detectAuthMethod", () => { expect(connectEvent).toBeDefined() expect(connectEvent.auth_method).toBe("unknown") }) + + // MongoDB-specific auth detection (added with MongoDB driver support #482) + test("detects connection_string auth for mongodb without password", () => { + expect(Registry.detectAuthMethod({ type: "mongodb", host: "localhost" } as any)).toBe("connection_string") + }) + + test("detects password auth for mongodb with password", () => { + expect(Registry.detectAuthMethod({ type: "mongodb", host: "localhost", password: "secret" } as any)).toBe("password") + }) + + test("detects connection_string auth for mongo alias without password", () => { + expect(Registry.detectAuthMethod({ type: "mongo", host: "localhost" } as any)).toBe("connection_string") + }) + + test("prefers explicit connection_string field over mongodb type fallback", () => { + expect(Registry.detectAuthMethod({ type: "mongodb", connection_string: "mongodb://localhost/test" } as any)).toBe("connection_string") + }) }) // ---------------------------------------------------------------------------