|
1 | | -import { describe, expect, test, beforeEach, beforeAll, afterAll } from "bun:test" |
| 1 | +import { describe, expect, test, beforeEach, afterEach, beforeAll, afterAll } from "bun:test" |
2 | 2 | import * as Dispatcher from "../../src/altimate/native/dispatcher" |
3 | 3 |
|
4 | 4 | // Disable telemetry via env var instead of mock.module |
@@ -74,6 +74,70 @@ describe("ConnectionRegistry", () => { |
74 | 74 | }) |
75 | 75 | }) |
76 | 76 |
|
| 77 | +// --------------------------------------------------------------------------- |
| 78 | +// loadFromEnv — env-var-based connection config loading |
| 79 | +// --------------------------------------------------------------------------- |
| 80 | + |
| 81 | +describe("loadFromEnv via Registry.load()", () => { |
| 82 | + const saved: Record<string, string | undefined> = {} |
| 83 | + |
| 84 | + function setEnv(key: string, value: string) { |
| 85 | + saved[key] = process.env[key] |
| 86 | + process.env[key] = value |
| 87 | + } |
| 88 | + |
| 89 | + beforeEach(() => { |
| 90 | + Registry.reset() |
| 91 | + }) |
| 92 | + |
| 93 | + afterEach(() => { |
| 94 | + for (const [key, orig] of Object.entries(saved)) { |
| 95 | + if (orig === undefined) delete process.env[key] |
| 96 | + else process.env[key] = orig |
| 97 | + } |
| 98 | + // Clear saved state for next test |
| 99 | + for (const key of Object.keys(saved)) delete saved[key] |
| 100 | + }) |
| 101 | + |
| 102 | + test("parses valid JSON from ALTIMATE_CODE_CONN_* env vars", () => { |
| 103 | + setEnv("ALTIMATE_CODE_CONN_MYDB", JSON.stringify({ type: "postgres", host: "localhost", port: 5432 })) |
| 104 | + Registry.load() |
| 105 | + const config = Registry.getConfig("mydb") |
| 106 | + expect(config).toBeDefined() |
| 107 | + expect(config?.type).toBe("postgres") |
| 108 | + expect(config?.host).toBe("localhost") |
| 109 | + }) |
| 110 | + |
| 111 | + test("lowercases connection name from env var suffix", () => { |
| 112 | + setEnv("ALTIMATE_CODE_CONN_PROD_DB", JSON.stringify({ type: "snowflake", account: "abc" })) |
| 113 | + Registry.load() |
| 114 | + expect(Registry.getConfig("prod_db")).toBeDefined() |
| 115 | + expect(Registry.getConfig("PROD_DB")).toBeUndefined() |
| 116 | + }) |
| 117 | + |
| 118 | + test("ignores env var with invalid JSON", () => { |
| 119 | + setEnv("ALTIMATE_CODE_CONN_BAD", "not-valid-json{") |
| 120 | + Registry.load() |
| 121 | + expect(Registry.getConfig("bad")).toBeUndefined() |
| 122 | + }) |
| 123 | + |
| 124 | + test("ignores env var config without type field", () => { |
| 125 | + setEnv("ALTIMATE_CODE_CONN_NOTYPE", JSON.stringify({ host: "localhost", port: 5432 })) |
| 126 | + Registry.load() |
| 127 | + expect(Registry.getConfig("notype")).toBeUndefined() |
| 128 | + }) |
| 129 | + |
| 130 | + test("ignores non-object JSON values (string, number, array)", () => { |
| 131 | + setEnv("ALTIMATE_CODE_CONN_STR", JSON.stringify("just a string")) |
| 132 | + setEnv("ALTIMATE_CODE_CONN_NUM", JSON.stringify(42)) |
| 133 | + setEnv("ALTIMATE_CODE_CONN_ARR", JSON.stringify([1, 2, 3])) |
| 134 | + Registry.load() |
| 135 | + expect(Registry.getConfig("str")).toBeUndefined() |
| 136 | + expect(Registry.getConfig("num")).toBeUndefined() |
| 137 | + expect(Registry.getConfig("arr")).toBeUndefined() |
| 138 | + }) |
| 139 | +}) |
| 140 | + |
77 | 141 | // --------------------------------------------------------------------------- |
78 | 142 | // CredentialStore (keytar not available in test environment) |
79 | 143 | // --------------------------------------------------------------------------- |
|
0 commit comments