|
| 1 | +// altimate_change start — unit tests for ConnectionRegistry loadFromEnv code path |
| 2 | +import { describe, test, expect, beforeEach, afterEach } from "bun:test" |
| 3 | +import * as Registry from "../../src/altimate/native/connections/registry" |
| 4 | + |
| 5 | +// --------------------------------------------------------------------------- |
| 6 | +// loadFromEnv — env-var based connection configuration |
| 7 | +// --------------------------------------------------------------------------- |
| 8 | +// These tests exercise the ALTIMATE_CODE_CONN_* env-var parsing in |
| 9 | +// registry.ts → loadFromEnv(). CI/CD users rely on this to inject |
| 10 | +// warehouse connections without config files on disk. |
| 11 | +// |
| 12 | +// NOTE: Registry.load() also reads from ~/.altimate-code/connections.json |
| 13 | +// and .altimate-code/connections.json. The assertions below only check for |
| 14 | +// specific keys set via env vars, so unrelated entries from disk do not |
| 15 | +// interfere. |
| 16 | +// --------------------------------------------------------------------------- |
| 17 | + |
| 18 | +describe("ConnectionRegistry: loadFromEnv", () => { |
| 19 | + // Track env vars we set so we can clean them up |
| 20 | + const envVarsSet: string[] = [] |
| 21 | + |
| 22 | + function setEnv(key: string, value: string) { |
| 23 | + process.env[key] = value |
| 24 | + envVarsSet.push(key) |
| 25 | + } |
| 26 | + |
| 27 | + beforeEach(() => { |
| 28 | + Registry.reset() |
| 29 | + }) |
| 30 | + |
| 31 | + afterEach(() => { |
| 32 | + for (const key of envVarsSet) { |
| 33 | + delete process.env[key] |
| 34 | + } |
| 35 | + envVarsSet.length = 0 |
| 36 | + }) |
| 37 | + |
| 38 | + test("parses valid ALTIMATE_CODE_CONN_* env var and lowercases the name", () => { |
| 39 | + setEnv("ALTIMATE_CODE_CONN_MYDB", JSON.stringify({ type: "postgres", host: "localhost", port: 5432 })) |
| 40 | + Registry.load() |
| 41 | + |
| 42 | + const config = Registry.getConfig("mydb") |
| 43 | + expect(config).toBeDefined() |
| 44 | + expect(config?.type).toBe("postgres") |
| 45 | + expect(config?.host).toBe("localhost") |
| 46 | + }) |
| 47 | + |
| 48 | + test("ignores malformed JSON in env var without crashing", () => { |
| 49 | + setEnv("ALTIMATE_CODE_CONN_BAD", "not-valid-json{{{") |
| 50 | + Registry.load() |
| 51 | + |
| 52 | + // The malformed env var should be silently skipped |
| 53 | + expect(Registry.getConfig("bad")).toBeUndefined() |
| 54 | + }) |
| 55 | + |
| 56 | + test("ignores env var config objects missing the type field", () => { |
| 57 | + setEnv("ALTIMATE_CODE_CONN_NOTYPE", JSON.stringify({ host: "localhost", port: 5432 })) |
| 58 | + Registry.load() |
| 59 | + |
| 60 | + // Without a type field, the config should not be registered |
| 61 | + expect(Registry.getConfig("notype")).toBeUndefined() |
| 62 | + }) |
| 63 | + |
| 64 | + test("ignores env vars with empty values", () => { |
| 65 | + setEnv("ALTIMATE_CODE_CONN_EMPTY", "") |
| 66 | + Registry.load() |
| 67 | + |
| 68 | + expect(Registry.getConfig("empty")).toBeUndefined() |
| 69 | + }) |
| 70 | + |
| 71 | + test("parses multiple env var connections", () => { |
| 72 | + setEnv("ALTIMATE_CODE_CONN_PG", JSON.stringify({ type: "postgres", host: "pg.example.com" })) |
| 73 | + setEnv("ALTIMATE_CODE_CONN_SF", JSON.stringify({ type: "snowflake", account: "abc123" })) |
| 74 | + Registry.load() |
| 75 | + |
| 76 | + expect(Registry.getConfig("pg")?.type).toBe("postgres") |
| 77 | + expect(Registry.getConfig("sf")?.type).toBe("snowflake") |
| 78 | + }) |
| 79 | +}) |
| 80 | +// altimate_change end |
0 commit comments