|
| 1 | +import { describe, test, expect } from "bun:test" |
| 2 | +import { containerToConfig } from "../../src/altimate/native/connections/docker-discovery" |
| 3 | +import type { DockerContainer } from "../../src/altimate/native/types" |
| 4 | + |
| 5 | +describe("containerToConfig: full container with all fields", () => { |
| 6 | + test("converts a complete DockerContainer to ConnectionConfig", () => { |
| 7 | + const container: DockerContainer = { |
| 8 | + container_id: "abc123def456", |
| 9 | + name: "my-postgres", |
| 10 | + image: "postgres:15-alpine", |
| 11 | + db_type: "postgres", |
| 12 | + host: "127.0.0.1", |
| 13 | + port: 5433, |
| 14 | + user: "myuser", |
| 15 | + password: "secret", |
| 16 | + database: "mydb", |
| 17 | + status: "running", |
| 18 | + } |
| 19 | + |
| 20 | + const config = containerToConfig(container) |
| 21 | + |
| 22 | + expect(config).toEqual({ |
| 23 | + type: "postgres", |
| 24 | + host: "127.0.0.1", |
| 25 | + port: 5433, |
| 26 | + user: "myuser", |
| 27 | + password: "secret", |
| 28 | + database: "mydb", |
| 29 | + }) |
| 30 | + }) |
| 31 | + |
| 32 | + test("omits optional fields when not present on container", () => { |
| 33 | + const container: DockerContainer = { |
| 34 | + container_id: "abc123", |
| 35 | + name: "bare-mysql", |
| 36 | + image: "mysql:8", |
| 37 | + db_type: "mysql", |
| 38 | + host: "127.0.0.1", |
| 39 | + port: 3306, |
| 40 | + status: "running", |
| 41 | + } |
| 42 | + |
| 43 | + const config = containerToConfig(container) |
| 44 | + |
| 45 | + // Should only have type, host, port — no user, password, database |
| 46 | + expect(Object.keys(config).sort()).toEqual(["host", "port", "type"]) |
| 47 | + expect(config.type).toBe("mysql") |
| 48 | + expect(config.host).toBe("127.0.0.1") |
| 49 | + expect(config.port).toBe(3306) |
| 50 | + }) |
| 51 | + |
| 52 | + test("preserves db_type as config.type for all supported databases", () => { |
| 53 | + const dbTypes = ["postgres", "mysql", "sqlserver", "oracle", "duckdb", "sqlite", "mongodb"] |
| 54 | + |
| 55 | + for (const dbType of dbTypes) { |
| 56 | + const container: DockerContainer = { |
| 57 | + container_id: "x", |
| 58 | + name: `test-${dbType}`, |
| 59 | + image: `${dbType}:latest`, |
| 60 | + db_type: dbType, |
| 61 | + host: "127.0.0.1", |
| 62 | + port: 5432, |
| 63 | + status: "running", |
| 64 | + } |
| 65 | + const config = containerToConfig(container) |
| 66 | + expect(config.type).toBe(dbType) |
| 67 | + } |
| 68 | + }) |
| 69 | + |
| 70 | + test("includes user but not password when only user is set", () => { |
| 71 | + const container: DockerContainer = { |
| 72 | + container_id: "x", |
| 73 | + name: "pg-no-pass", |
| 74 | + image: "postgres:15", |
| 75 | + db_type: "postgres", |
| 76 | + host: "127.0.0.1", |
| 77 | + port: 5432, |
| 78 | + user: "postgres", |
| 79 | + status: "running", |
| 80 | + } |
| 81 | + |
| 82 | + const config = containerToConfig(container) |
| 83 | + |
| 84 | + expect(config.user).toBe("postgres") |
| 85 | + expect(config.password).toBeUndefined() |
| 86 | + expect(Object.keys(config).sort()).toEqual(["host", "port", "type", "user"]) |
| 87 | + }) |
| 88 | + |
| 89 | + test("includes database but not user/password when only database is set", () => { |
| 90 | + const container: DockerContainer = { |
| 91 | + container_id: "x", |
| 92 | + name: "pg-db-only", |
| 93 | + image: "postgres:15", |
| 94 | + db_type: "postgres", |
| 95 | + host: "127.0.0.1", |
| 96 | + port: 5432, |
| 97 | + database: "analytics", |
| 98 | + status: "running", |
| 99 | + } |
| 100 | + |
| 101 | + const config = containerToConfig(container) |
| 102 | + |
| 103 | + expect(config.database).toBe("analytics") |
| 104 | + expect(config.user).toBeUndefined() |
| 105 | + expect(config.password).toBeUndefined() |
| 106 | + }) |
| 107 | + |
| 108 | + test("does not include container_id, name, image, or status in config", () => { |
| 109 | + const container: DockerContainer = { |
| 110 | + container_id: "abc123def456", |
| 111 | + name: "my-container", |
| 112 | + image: "postgres:15", |
| 113 | + db_type: "postgres", |
| 114 | + host: "127.0.0.1", |
| 115 | + port: 5432, |
| 116 | + user: "pg", |
| 117 | + password: "pass", |
| 118 | + database: "db", |
| 119 | + status: "running", |
| 120 | + } |
| 121 | + |
| 122 | + const config = containerToConfig(container) |
| 123 | + |
| 124 | + // These Docker-specific fields should NOT leak into the ConnectionConfig |
| 125 | + expect((config as any).container_id).toBeUndefined() |
| 126 | + expect((config as any).name).toBeUndefined() |
| 127 | + expect((config as any).image).toBeUndefined() |
| 128 | + expect((config as any).status).toBeUndefined() |
| 129 | + expect((config as any).db_type).toBeUndefined() |
| 130 | + }) |
| 131 | +}) |
0 commit comments