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
66 changes: 65 additions & 1 deletion packages/opencode/test/altimate/connections.test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -74,6 +74,70 @@ describe("ConnectionRegistry", () => {
})
})

// ---------------------------------------------------------------------------
// loadFromEnv — env-var-based connection config loading
// ---------------------------------------------------------------------------

describe("loadFromEnv via Registry.load()", () => {
const saved: Record<string, string | undefined> = {}

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)
// ---------------------------------------------------------------------------
Expand Down
17 changes: 17 additions & 0 deletions packages/opencode/test/altimate/warehouse-telemetry.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
})
Comment on lines +186 to +188
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Precedence test does not currently differentiate the fallback path.

At Line 186, the input has no password, so MongoDB fallback also returns "connection_string". This test won’t catch precedence regressions. Add password to make the fallback outcome different.

✅ Make the assertion branch-distinguishing
 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")
+  expect(
+    Registry.detectAuthMethod({
+      type: "mongodb",
+      password: "secret",
+      connection_string: "mongodb://localhost/test",
+    } as any),
+  ).toBe("connection_string")
 })
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
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")
})
test("prefers explicit connection_string field over mongodb type fallback", () => {
expect(
Registry.detectAuthMethod({
type: "mongodb",
password: "secret",
connection_string: "mongodb://localhost/test",
} as any),
).toBe("connection_string")
})
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@packages/opencode/test/altimate/warehouse-telemetry.test.ts` around lines 186
- 188, The test for Registry.detectAuthMethod currently doesn't distinguish the
explicit connection_string branch from the MongoDB fallback because the input
lacks a password so both return "connection_string"; update the test case used
in the "prefers explicit connection_string field over mongodb type fallback"
test to include a password field (e.g., add password: "x") so that the MongoDB
fallback would produce a different result and the assertion actually verifies
precedence of the connection_string over the mongodb type fallback.

})

// ---------------------------------------------------------------------------
Expand Down
Loading