|
| 1 | +/** |
| 2 | + * Adversarial Telemetry Safety Tests |
| 3 | + * |
| 4 | + * Verifies telemetry failures NEVER break driver operations. |
| 5 | + * Uses direct function calls (not Dispatcher) to test the exact |
| 6 | + * code paths where telemetry is added. |
| 7 | + * |
| 8 | + * Background: bad telemetry code previously broke drivers. |
| 9 | + */ |
| 10 | + |
| 11 | +import { describe, expect, test, beforeEach, mock } from "bun:test" |
| 12 | + |
| 13 | +// Track all telemetry calls for verification |
| 14 | +const telemetryCalls: Array<{ type: string; threw: boolean }> = [] |
| 15 | +let shouldThrow = false |
| 16 | + |
| 17 | +// Mock Telemetry — can toggle between working and throwing mode |
| 18 | +mock.module("../../src/altimate/telemetry", () => ({ |
| 19 | + Telemetry: { |
| 20 | + track: (event: any) => { |
| 21 | + telemetryCalls.push({ type: event?.type ?? "unknown", threw: shouldThrow }) |
| 22 | + if (shouldThrow) { |
| 23 | + throw new Error(`TELEMETRY EXPLOSION: ${event?.type}`) |
| 24 | + } |
| 25 | + }, |
| 26 | + getContext: () => { |
| 27 | + if (shouldThrow) throw new Error("getContext EXPLOSION") |
| 28 | + return { sessionId: "test-session" } |
| 29 | + }, |
| 30 | + }, |
| 31 | +})) |
| 32 | + |
| 33 | +mock.module("../../src/util/log", () => ({ |
| 34 | + Log: { Default: { warn: () => {}, error: () => {}, info: () => {}, debug: () => {} } }, |
| 35 | +})) |
| 36 | + |
| 37 | +// Import modules under test |
| 38 | +import * as Registry from "../../src/altimate/native/connections/registry" |
| 39 | +import { detectAuthMethod, categorizeConnectionError } from "../../src/altimate/native/connections/registry" |
| 40 | +import { detectQueryType, categorizeQueryError } from "../../src/altimate/native/connections/register" |
| 41 | + |
| 42 | +describe("Telemetry Safety: Helper functions never throw", () => { |
| 43 | + test("detectAuthMethod handles all config shapes", () => { |
| 44 | + expect(detectAuthMethod({ type: "postgres", connection_string: "pg://..." })).toBe("connection_string") |
| 45 | + expect(detectAuthMethod({ type: "snowflake", private_key_path: "/key.p8" })).toBe("key_pair") |
| 46 | + expect(detectAuthMethod({ type: "databricks", access_token: "dapi..." })).toBe("token") |
| 47 | + expect(detectAuthMethod({ type: "postgres", password: "secret" })).toBe("password") |
| 48 | + expect(detectAuthMethod({ type: "duckdb" })).toBe("file") |
| 49 | + expect(detectAuthMethod({ type: "sqlite" })).toBe("file") |
| 50 | + expect(detectAuthMethod({ type: "postgres" })).toBe("unknown") |
| 51 | + // Edge cases |
| 52 | + expect(detectAuthMethod({} as any)).toBe("unknown") |
| 53 | + expect(detectAuthMethod({ type: "" })).toBe("unknown") |
| 54 | + expect(detectAuthMethod(null as any)).toBe("unknown") |
| 55 | + }) |
| 56 | + |
| 57 | + test("detectAuthMethod does not throw on bizarre input", () => { |
| 58 | + expect(() => detectAuthMethod(undefined as any)).not.toThrow() |
| 59 | + expect(() => detectAuthMethod(null as any)).not.toThrow() |
| 60 | + expect(() => detectAuthMethod({} as any)).not.toThrow() |
| 61 | + expect(() => detectAuthMethod({ type: 123 } as any)).not.toThrow() |
| 62 | + }) |
| 63 | + |
| 64 | + test("categorizeConnectionError categorizes all error types", () => { |
| 65 | + expect(categorizeConnectionError(new Error("not installed"))).toBe("driver_missing") |
| 66 | + expect(categorizeConnectionError(new Error("Cannot find module"))).toBe("driver_missing") |
| 67 | + expect(categorizeConnectionError(new Error("Incorrect password"))).toBe("auth_failed") |
| 68 | + expect(categorizeConnectionError(new Error("authentication failed"))).toBe("auth_failed") |
| 69 | + expect(categorizeConnectionError(new Error("JWT token invalid"))).toBe("auth_failed") |
| 70 | + expect(categorizeConnectionError(new Error("connection timed out"))).toBe("timeout") |
| 71 | + expect(categorizeConnectionError(new Error("ECONNREFUSED"))).toBe("network_error") |
| 72 | + expect(categorizeConnectionError(new Error("ENOTFOUND host"))).toBe("network_error") |
| 73 | + expect(categorizeConnectionError(new Error("Connection not found"))).toBe("config_error") |
| 74 | + expect(categorizeConnectionError(new Error("something random"))).toBe("other") |
| 75 | + // Edge cases |
| 76 | + expect(categorizeConnectionError(null)).toBe("other") |
| 77 | + expect(categorizeConnectionError(undefined)).toBe("other") |
| 78 | + expect(categorizeConnectionError(42)).toBe("other") |
| 79 | + expect(categorizeConnectionError("string error")).toBe("other") |
| 80 | + }) |
| 81 | + |
| 82 | + test("detectQueryType classifies all SQL types", () => { |
| 83 | + expect(detectQueryType("SELECT 1")).toBe("SELECT") |
| 84 | + expect(detectQueryType(" select * from t")).toBe("SELECT") |
| 85 | + expect(detectQueryType("WITH cte AS (SELECT 1) SELECT * FROM cte")).toBe("SELECT") |
| 86 | + expect(detectQueryType("INSERT INTO t VALUES (1)")).toBe("INSERT") |
| 87 | + expect(detectQueryType("UPDATE t SET x = 1")).toBe("UPDATE") |
| 88 | + expect(detectQueryType("DELETE FROM t")).toBe("DELETE") |
| 89 | + expect(detectQueryType("CREATE TABLE t (id INT)")).toBe("DDL") |
| 90 | + expect(detectQueryType("ALTER TABLE t ADD col INT")).toBe("DDL") |
| 91 | + expect(detectQueryType("DROP TABLE t")).toBe("DDL") |
| 92 | + expect(detectQueryType("SHOW TABLES")).toBe("SHOW") |
| 93 | + expect(detectQueryType("DESCRIBE TABLE t")).toBe("SHOW") |
| 94 | + expect(detectQueryType("EXPLAIN SELECT 1")).toBe("SHOW") |
| 95 | + expect(detectQueryType("GRANT SELECT ON t TO user")).toBe("OTHER") |
| 96 | + expect(detectQueryType("")).toBe("OTHER") |
| 97 | + }) |
| 98 | + |
| 99 | + test("detectQueryType does not throw on bizarre input", () => { |
| 100 | + expect(() => detectQueryType("")).not.toThrow() |
| 101 | + expect(() => detectQueryType(null as any)).not.toThrow() |
| 102 | + expect(() => detectQueryType(undefined as any)).not.toThrow() |
| 103 | + expect(() => detectQueryType(123 as any)).not.toThrow() |
| 104 | + }) |
| 105 | + |
| 106 | + test("categorizeQueryError categorizes all error types", () => { |
| 107 | + expect(categorizeQueryError(new Error("syntax error at position 5"))).toBe("syntax_error") |
| 108 | + expect(categorizeQueryError(new Error("permission denied for table"))).toBe("permission_denied") |
| 109 | + expect(categorizeQueryError(new Error("access denied"))).toBe("permission_denied") |
| 110 | + expect(categorizeQueryError(new Error("query timeout after 30s"))).toBe("timeout") |
| 111 | + expect(categorizeQueryError(new Error("connection closed unexpectedly"))).toBe("connection_lost") |
| 112 | + expect(categorizeQueryError(new Error("connection terminated"))).toBe("connection_lost") |
| 113 | + expect(categorizeQueryError(new Error("random error"))).toBe("other") |
| 114 | + expect(categorizeQueryError(null)).toBe("other") |
| 115 | + expect(categorizeQueryError(undefined)).toBe("other") |
| 116 | + }) |
| 117 | +}) |
| 118 | + |
| 119 | +describe("Telemetry Safety: Registry operations survive telemetry explosions", () => { |
| 120 | + beforeEach(() => { |
| 121 | + Registry.reset() |
| 122 | + telemetryCalls.length = 0 |
| 123 | + shouldThrow = true // ALL telemetry will throw |
| 124 | + }) |
| 125 | + |
| 126 | + test("list() returns correct data when telemetry (census) throws", () => { |
| 127 | + Registry.setConfigs({ |
| 128 | + pg: { type: "postgres", host: "localhost" }, |
| 129 | + sf: { type: "snowflake", account: "test" }, |
| 130 | + }) |
| 131 | + |
| 132 | + const result = Registry.list() |
| 133 | + expect(result.warehouses.length).toBe(2) |
| 134 | + expect(result.warehouses.map((w: any) => w.name).sort()).toEqual(["pg", "sf"]) |
| 135 | + // Census telemetry was attempted and threw — but list worked fine |
| 136 | + }) |
| 137 | + |
| 138 | + test("list() called 10 times in a row with throwing telemetry", () => { |
| 139 | + Registry.setConfigs({ |
| 140 | + db: { type: "duckdb", path: ":memory:" }, |
| 141 | + }) |
| 142 | + |
| 143 | + for (let i = 0; i < 10; i++) { |
| 144 | + const r = Registry.list() |
| 145 | + expect(r.warehouses.length).toBe(1) |
| 146 | + } |
| 147 | + }) |
| 148 | + |
| 149 | + test("getConfig() works regardless of telemetry state", () => { |
| 150 | + Registry.setConfigs({ |
| 151 | + pg: { type: "postgres", host: "myhost", database: "mydb" }, |
| 152 | + }) |
| 153 | + |
| 154 | + const config = Registry.getConfig("pg") |
| 155 | + expect(config?.type).toBe("postgres") |
| 156 | + expect(config?.host).toBe("myhost") |
| 157 | + }) |
| 158 | + |
| 159 | + test("add() succeeds when telemetry throws", async () => { |
| 160 | + const result = await Registry.add("test_add", { |
| 161 | + type: "duckdb", |
| 162 | + path: ":memory:", |
| 163 | + }) |
| 164 | + expect(result.success).toBe(true) |
| 165 | + expect(result.name).toBe("test_add") |
| 166 | + }) |
| 167 | + |
| 168 | + test("remove() succeeds when telemetry throws", async () => { |
| 169 | + Registry.setConfigs({ |
| 170 | + to_remove: { type: "duckdb", path: ":memory:" }, |
| 171 | + }) |
| 172 | + const result = await Registry.remove("to_remove") |
| 173 | + expect(result.success).toBe(true) |
| 174 | + }) |
| 175 | + |
| 176 | + test("test() returns error for bad connection without crashing", async () => { |
| 177 | + Registry.setConfigs({ |
| 178 | + bad: { type: "postgres", host: "nonexistent.invalid" }, |
| 179 | + }) |
| 180 | + const result = await Registry.test("bad") |
| 181 | + expect(result.connected).toBe(false) |
| 182 | + expect(typeof result.error).toBe("string") |
| 183 | + }) |
| 184 | +}) |
| 185 | + |
| 186 | +describe("Telemetry Safety: Telemetry calls are attempted but swallowed", () => { |
| 187 | + beforeEach(() => { |
| 188 | + Registry.reset() |
| 189 | + telemetryCalls.length = 0 |
| 190 | + }) |
| 191 | + |
| 192 | + test("working telemetry: events are tracked", () => { |
| 193 | + shouldThrow = false |
| 194 | + Registry.setConfigs({ |
| 195 | + pg: { type: "postgres", host: "localhost" }, |
| 196 | + }) |
| 197 | + |
| 198 | + Registry.list() |
| 199 | + const censusEvents = telemetryCalls.filter((c) => c.type === "warehouse_census") |
| 200 | + expect(censusEvents.length).toBeGreaterThanOrEqual(1) |
| 201 | + expect(censusEvents[0].threw).toBe(false) |
| 202 | + }) |
| 203 | + |
| 204 | + test("throwing telemetry: list still works when census throws", () => { |
| 205 | + shouldThrow = true |
| 206 | + Registry.setConfigs({ |
| 207 | + pg: { type: "postgres", host: "localhost" }, |
| 208 | + }) |
| 209 | + |
| 210 | + // This should NOT throw even though telemetry is exploding |
| 211 | + const result = Registry.list() |
| 212 | + expect(result.warehouses.length).toBe(1) |
| 213 | + expect(result.warehouses[0].name).toBe("pg") |
| 214 | + }) |
| 215 | +}) |
0 commit comments