|
| 1 | +import { describe, test, expect, beforeAll, afterAll } from "bun:test" |
| 2 | + |
| 3 | +// Disable telemetry to avoid side-effects |
| 4 | +beforeAll(() => { process.env.ALTIMATE_TELEMETRY_DISABLED = "true" }) |
| 5 | +afterAll(() => { delete process.env.ALTIMATE_TELEMETRY_DISABLED }) |
| 6 | + |
| 7 | +import { extractSshConfig, closeTunnel, getActiveTunnel } from "../../src/altimate/native/connections/ssh-tunnel" |
| 8 | +import { detectAuthMethod } from "../../src/altimate/native/connections/registry" |
| 9 | + |
| 10 | +// --------------------------------------------------------------------------- |
| 11 | +// extractSshConfig — pure function that extracts SSH tunnel config |
| 12 | +// --------------------------------------------------------------------------- |
| 13 | + |
| 14 | +describe("extractSshConfig", () => { |
| 15 | + test("returns null when no ssh_host is present", () => { |
| 16 | + const result = extractSshConfig({ type: "postgres", host: "db.example.com", port: 5432 }) |
| 17 | + expect(result).toBeNull() |
| 18 | + }) |
| 19 | + |
| 20 | + test("extracts full SSH config with all fields", () => { |
| 21 | + const result = extractSshConfig({ |
| 22 | + type: "postgres", |
| 23 | + host: "db.internal", |
| 24 | + port: 5433, |
| 25 | + ssh_host: "bastion.example.com", |
| 26 | + ssh_port: 2222, |
| 27 | + ssh_user: "deployer", |
| 28 | + ssh_password: "secret", |
| 29 | + }) |
| 30 | + expect(result).toEqual({ |
| 31 | + ssh_host: "bastion.example.com", |
| 32 | + ssh_port: 2222, |
| 33 | + ssh_user: "deployer", |
| 34 | + ssh_password: "secret", |
| 35 | + ssh_private_key: undefined, |
| 36 | + host: "db.internal", |
| 37 | + port: 5433, |
| 38 | + }) |
| 39 | + }) |
| 40 | + |
| 41 | + test("applies defaults for ssh_port, ssh_user, host, port", () => { |
| 42 | + const result = extractSshConfig({ |
| 43 | + type: "postgres", |
| 44 | + ssh_host: "bastion.example.com", |
| 45 | + }) |
| 46 | + expect(result).not.toBeNull() |
| 47 | + expect(result!.ssh_port).toBe(22) |
| 48 | + expect(result!.ssh_user).toBe("root") |
| 49 | + expect(result!.host).toBe("127.0.0.1") |
| 50 | + expect(result!.port).toBe(5432) |
| 51 | + }) |
| 52 | + |
| 53 | + test("throws when connection_string is used with SSH tunnel", () => { |
| 54 | + expect(() => extractSshConfig({ |
| 55 | + type: "postgres", |
| 56 | + ssh_host: "bastion.example.com", |
| 57 | + connection_string: "postgresql://user:pass@host:5432/db", |
| 58 | + })).toThrow("Cannot use SSH tunnel with connection_string") |
| 59 | + }) |
| 60 | + |
| 61 | + test("supports private key authentication", () => { |
| 62 | + const result = extractSshConfig({ |
| 63 | + type: "snowflake", |
| 64 | + host: "db.internal", |
| 65 | + port: 443, |
| 66 | + ssh_host: "bastion.example.com", |
| 67 | + ssh_private_key: "-----BEGIN OPENSSH PRIVATE KEY-----\nAAA...", |
| 68 | + }) |
| 69 | + expect(result).not.toBeNull() |
| 70 | + expect(result!.ssh_private_key).toBe("-----BEGIN OPENSSH PRIVATE KEY-----\nAAA...") |
| 71 | + expect(result!.ssh_password).toBeUndefined() |
| 72 | + }) |
| 73 | +}) |
| 74 | + |
| 75 | +// --------------------------------------------------------------------------- |
| 76 | +// closeTunnel / getActiveTunnel — idempotent operations on empty state |
| 77 | +// --------------------------------------------------------------------------- |
| 78 | + |
| 79 | +describe("SSH tunnel state management", () => { |
| 80 | + test("closeTunnel is a no-op for non-existent tunnel and does not corrupt state", () => { |
| 81 | + closeTunnel("nonexistent-tunnel-name") |
| 82 | + expect(getActiveTunnel("nonexistent-tunnel-name")).toBeUndefined() |
| 83 | + }) |
| 84 | + |
| 85 | + test("getActiveTunnel returns undefined for non-existent tunnel", () => { |
| 86 | + expect(getActiveTunnel("nonexistent")).toBeUndefined() |
| 87 | + }) |
| 88 | +}) |
| 89 | + |
| 90 | +// --------------------------------------------------------------------------- |
| 91 | +// detectAuthMethod — MongoDB-specific fallback paths (added in commit abcaa1d) |
| 92 | +// |
| 93 | +// Note: config.password triggers the generic "password" branch (line 226) |
| 94 | +// BEFORE the type-specific MongoDB branch (line 229). These tests document |
| 95 | +// the actual precedence behavior, not the MongoDB branch in isolation. |
| 96 | +// --------------------------------------------------------------------------- |
| 97 | + |
| 98 | +describe("detectAuthMethod: MongoDB", () => { |
| 99 | + test("mongodb without password falls through to MongoDB-specific branch returning 'connection_string'", () => { |
| 100 | + expect(detectAuthMethod({ type: "mongodb" })).toBe("connection_string") |
| 101 | + }) |
| 102 | + |
| 103 | + test("mongo alias without password falls through to MongoDB-specific branch returning 'connection_string'", () => { |
| 104 | + expect(detectAuthMethod({ type: "mongo" })).toBe("connection_string") |
| 105 | + }) |
| 106 | + |
| 107 | + test("mongodb with password is caught by the generic password check (precedence test)", () => { |
| 108 | + // The generic `if (config.password)` fires before the MongoDB branch |
| 109 | + expect(detectAuthMethod({ type: "mongodb", password: "secret" })).toBe("password") |
| 110 | + }) |
| 111 | + |
| 112 | + test("mongodb with connection_string is caught by the generic connection_string check (precedence test)", () => { |
| 113 | + expect(detectAuthMethod({ type: "mongodb", connection_string: "mongodb://localhost:27017" })).toBe("connection_string") |
| 114 | + }) |
| 115 | +}) |
0 commit comments