|
| 1 | +import { describe, test, expect } from "bun:test" |
| 2 | +import { |
| 3 | + extractSshConfig, |
| 4 | + closeTunnel, |
| 5 | + closeAllTunnels, |
| 6 | + getActiveTunnel, |
| 7 | +} from "../../src/altimate/native/connections/ssh-tunnel" |
| 8 | + |
| 9 | +describe("SSH tunnel: extractSshConfig", () => { |
| 10 | + test("returns null when no ssh_host present", () => { |
| 11 | + const result = extractSshConfig({ type: "postgres", host: "db.example.com", port: 5432 }) |
| 12 | + expect(result).toBeNull() |
| 13 | + }) |
| 14 | + |
| 15 | + test("extracts SSH config with all fields", () => { |
| 16 | + const config = { |
| 17 | + type: "postgres", |
| 18 | + host: "db.internal", |
| 19 | + port: 5432, |
| 20 | + ssh_host: "bastion.example.com", |
| 21 | + ssh_port: 2222, |
| 22 | + ssh_user: "deploy", |
| 23 | + ssh_password: "secret", |
| 24 | + } |
| 25 | + const result = extractSshConfig(config) |
| 26 | + expect(result).toEqual({ |
| 27 | + ssh_host: "bastion.example.com", |
| 28 | + ssh_port: 2222, |
| 29 | + ssh_user: "deploy", |
| 30 | + ssh_password: "secret", |
| 31 | + ssh_private_key: undefined, |
| 32 | + host: "db.internal", |
| 33 | + port: 5432, |
| 34 | + }) |
| 35 | + }) |
| 36 | + |
| 37 | + test("uses defaults for optional ssh_port and ssh_user", () => { |
| 38 | + const result = extractSshConfig({ |
| 39 | + type: "postgres", |
| 40 | + host: "db.internal", |
| 41 | + port: 5432, |
| 42 | + ssh_host: "bastion.example.com", |
| 43 | + }) |
| 44 | + expect(result!.ssh_port).toBe(22) |
| 45 | + expect(result!.ssh_user).toBe("root") |
| 46 | + }) |
| 47 | + |
| 48 | + test("uses default host and port when not specified", () => { |
| 49 | + const result = extractSshConfig({ |
| 50 | + type: "postgres", |
| 51 | + ssh_host: "bastion.example.com", |
| 52 | + }) |
| 53 | + expect(result!.host).toBe("127.0.0.1") |
| 54 | + expect(result!.port).toBe(5432) |
| 55 | + }) |
| 56 | + |
| 57 | + test("throws when ssh_host used with connection_string", () => { |
| 58 | + expect(() => |
| 59 | + extractSshConfig({ |
| 60 | + type: "postgres", |
| 61 | + ssh_host: "bastion.example.com", |
| 62 | + connection_string: "postgresql://user:pass@host/db", |
| 63 | + }), |
| 64 | + ).toThrow("Cannot use SSH tunnel with connection_string") |
| 65 | + }) |
| 66 | + |
| 67 | + test("passes through both ssh_private_key and ssh_password", () => { |
| 68 | + const result = extractSshConfig({ |
| 69 | + type: "postgres", |
| 70 | + host: "db.internal", |
| 71 | + port: 5432, |
| 72 | + ssh_host: "bastion.example.com", |
| 73 | + ssh_private_key: "-----BEGIN RSA PRIVATE KEY-----\nfake\n-----END RSA PRIVATE KEY-----", |
| 74 | + ssh_password: "also-provided", |
| 75 | + }) |
| 76 | + expect(result!.ssh_private_key).toContain("BEGIN RSA PRIVATE KEY") |
| 77 | + expect(result!.ssh_password).toBe("also-provided") |
| 78 | + }) |
| 79 | +}) |
| 80 | + |
| 81 | +describe("SSH tunnel: lifecycle helpers (no real SSH)", () => { |
| 82 | + test("closeTunnel is a no-op for unknown name", () => { |
| 83 | + closeTunnel("nonexistent-tunnel-name") |
| 84 | + expect(getActiveTunnel("nonexistent-tunnel-name")).toBeUndefined() |
| 85 | + }) |
| 86 | + |
| 87 | + test("closeAllTunnels is safe when no tunnels exist", () => { |
| 88 | + closeAllTunnels() |
| 89 | + expect(getActiveTunnel("any")).toBeUndefined() |
| 90 | + }) |
| 91 | +}) |
0 commit comments