From 7a7e5d11189804b1fe9fa13a75a52ccd0017306b Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 26 Mar 2026 23:16:06 +0000 Subject: [PATCH] test: MongoDB normalization + detectAuthMethod coverage MongoDB driver (PR #482) had zero unit tests for config alias normalization (uri, url, authSource, etc.) and auth method detection in telemetry. These tests prevent silent config failures for MongoDB users using common alias field names and ensure telemetry correctly categorizes auth methods for all driver types including the new MongoDB and file-based drivers. Co-Authored-By: Claude Opus 4.6 (1M context) --- .../test/altimate/connections.test.ts | 64 +++++++++++++++++++ .../test/altimate/driver-normalize.test.ts | 49 ++++++++++++++ 2 files changed, 113 insertions(+) diff --git a/packages/opencode/test/altimate/connections.test.ts b/packages/opencode/test/altimate/connections.test.ts index e5facc6db0..ea1e957af6 100644 --- a/packages/opencode/test/altimate/connections.test.ts +++ b/packages/opencode/test/altimate/connections.test.ts @@ -10,6 +10,7 @@ afterAll(() => { delete process.env.ALTIMATE_TELEMETRY_DISABLED }) // --------------------------------------------------------------------------- import * as Registry from "../../src/altimate/native/connections/registry" +import { detectAuthMethod } from "../../src/altimate/native/connections/registry" import * as CredentialStore from "../../src/altimate/native/connections/credential-store" import { parseDbtProfiles } from "../../src/altimate/native/connections/dbt-profiles" import { discoverContainers } from "../../src/altimate/native/connections/docker-discovery" @@ -137,6 +138,69 @@ describe("CredentialStore", () => { }) }) +// --------------------------------------------------------------------------- +// detectAuthMethod +// --------------------------------------------------------------------------- + +describe("detectAuthMethod", () => { + test("returns 'connection_string' for config with connection_string", () => { + expect(detectAuthMethod({ type: "postgres", connection_string: "postgresql://..." } as any)).toBe("connection_string") + }) + + test("returns 'key_pair' for Snowflake private_key_path", () => { + expect(detectAuthMethod({ type: "snowflake", private_key_path: "/path/to/key.p8" } as any)).toBe("key_pair") + }) + + test("returns 'key_pair' for camelCase privateKeyPath", () => { + expect(detectAuthMethod({ type: "snowflake", privateKeyPath: "/path/to/key.p8" } as any)).toBe("key_pair") + }) + + test("returns 'sso' for Snowflake externalbrowser", () => { + expect(detectAuthMethod({ type: "snowflake", authenticator: "EXTERNALBROWSER" } as any)).toBe("sso") + }) + + test("returns 'sso' for Okta URL authenticator", () => { + expect(detectAuthMethod({ type: "snowflake", authenticator: "https://myorg.okta.com" } as any)).toBe("sso") + }) + + test("returns 'oauth' for OAuth authenticator", () => { + expect(detectAuthMethod({ type: "snowflake", authenticator: "OAUTH" } as any)).toBe("oauth") + }) + + test("returns 'token' for access_token", () => { + expect(detectAuthMethod({ type: "databricks", access_token: "dapi..." } as any)).toBe("token") + }) + + test("returns 'password' for config with password", () => { + expect(detectAuthMethod({ type: "postgres", password: "secret" } as any)).toBe("password") + }) + + test("returns 'file' for duckdb", () => { + expect(detectAuthMethod({ type: "duckdb", path: "/data/my.db" } as any)).toBe("file") + }) + + test("returns 'file' for sqlite", () => { + expect(detectAuthMethod({ type: "sqlite", path: "/data/my.sqlite" } as any)).toBe("file") + }) + + test("returns 'connection_string' for mongodb without password", () => { + expect(detectAuthMethod({ type: "mongodb" } as any)).toBe("connection_string") + }) + + test("returns 'password' for mongo with password", () => { + expect(detectAuthMethod({ type: "mongo", password: "secret" } as any)).toBe("password") + }) + + test("returns 'unknown' for null/undefined", () => { + expect(detectAuthMethod(null)).toBe("unknown") + expect(detectAuthMethod(undefined)).toBe("unknown") + }) + + test("returns 'unknown' for empty config with no identifiable auth", () => { + expect(detectAuthMethod({ type: "postgres" } as any)).toBe("unknown") + }) +}) + // --------------------------------------------------------------------------- // dbt profiles parser // --------------------------------------------------------------------------- diff --git a/packages/opencode/test/altimate/driver-normalize.test.ts b/packages/opencode/test/altimate/driver-normalize.test.ts index f437e1187c..5729404418 100644 --- a/packages/opencode/test/altimate/driver-normalize.test.ts +++ b/packages/opencode/test/altimate/driver-normalize.test.ts @@ -652,6 +652,55 @@ describe("normalizeConfig — DuckDB/SQLite passthrough", () => { // normalizeConfig — Snowflake private_key edge cases // --------------------------------------------------------------------------- +// --------------------------------------------------------------------------- +// normalizeConfig — MongoDB aliases +// --------------------------------------------------------------------------- + +describe("normalizeConfig — MongoDB", () => { + test("connectionString → connection_string", () => { + const config = { type: "mongodb", connectionString: "mongodb://localhost:27017/mydb" } + const result = normalizeConfig(config) + expect(result.connection_string).toBe("mongodb://localhost:27017/mydb") + expect(result.connectionString).toBeUndefined() + }) + + test("uri → connection_string", () => { + const config = { type: "mongodb", uri: "mongodb://localhost:27017/mydb" } + const result = normalizeConfig(config) + expect(result.connection_string).toBe("mongodb://localhost:27017/mydb") + expect(result.uri).toBeUndefined() + }) + + test("url → connection_string", () => { + const config = { type: "mongodb", url: "mongodb+srv://user:pass@cluster.mongodb.net/db" } + const result = normalizeConfig(config) + expect(result.connection_string).toBe("mongodb+srv://user:pass@cluster.mongodb.net/db") + expect(result.url).toBeUndefined() + }) + + test("canonical connection_string takes precedence over uri alias", () => { + const config = { type: "mongodb", connection_string: "mongodb://primary", uri: "mongodb://fallback" } + const result = normalizeConfig(config) + expect(result.connection_string).toBe("mongodb://primary") + expect(result.uri).toBeUndefined() + }) + + test("mongo type alias resolves MongoDB-specific aliases", () => { + const config = { type: "mongo", uri: "mongodb://localhost:27017/test", authSource: "admin" } + const result = normalizeConfig(config) + expect(result.connection_string).toBe("mongodb://localhost:27017/test") + expect(result.auth_source).toBe("admin") + expect(result.authSource).toBeUndefined() + }) + + test("authSource → auth_source", () => { + const config = { type: "mongodb", host: "localhost", authSource: "admin" } + const result = normalizeConfig(config) + expect(result.auth_source).toBe("admin") + expect(result.authSource).toBeUndefined() + }) +}) + describe("normalizeConfig — Snowflake private_key edge cases", () => { test("opaque string without path indicators stays as private_key", () => { const result = normalizeConfig({