@malloy-publisher/server 0.0.235 → 0.0.236

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (301) hide show
  1. package/dist/app/api-doc.yaml +47 -17
  2. package/dist/package_load_worker.mjs +8 -1
  3. package/dist/server.mjs +471 -54
  4. package/package.json +5 -2
  5. package/.eslintrc.json +0 -44
  6. package/.prettierignore +0 -1
  7. package/.prettierrc +0 -4
  8. package/build.ts +0 -97
  9. package/eslint.config.mjs +0 -76
  10. package/k6-tests/README.md +0 -144
  11. package/k6-tests/bun.lock +0 -797
  12. package/k6-tests/load-test/load-test-crud-connections.ts +0 -263
  13. package/k6-tests/load-test/load-test-crud-packages.ts +0 -461
  14. package/k6-tests/load-test/load-test-crud-projects.ts +0 -160
  15. package/k6-tests/load-test/load-test-crud.ts +0 -142
  16. package/k6-tests/load-test/load-test.ts +0 -784
  17. package/k6-tests/package.json +0 -27
  18. package/k6-tests/scripts/run-with-otel.sh +0 -50
  19. package/k6-tests/smoke-test/smoke-test.ts +0 -158
  20. package/k6-tests/tsconfig.json +0 -29
  21. package/k6-tests/utils/client_factory.ts +0 -128
  22. package/k6-tests/utils/common.ts +0 -489
  23. package/k6-tests/utils/logger.ts +0 -62
  24. package/k6-tests/utils/types.d.ts +0 -35
  25. package/malloy_mcp.dxt +0 -0
  26. package/publisher.config.example.bigquery.json +0 -33
  27. package/publisher.config.example.duckdb.json +0 -23
  28. package/publisher.config.json +0 -23
  29. package/scripts/bake-duckdb-extensions.js +0 -110
  30. package/src/config.spec.ts +0 -1535
  31. package/src/config.theme.spec.ts +0 -178
  32. package/src/config.ts +0 -1142
  33. package/src/constants.ts +0 -105
  34. package/src/controller/compile.controller.ts +0 -40
  35. package/src/controller/connection.controller.spec.ts +0 -849
  36. package/src/controller/connection.controller.ts +0 -901
  37. package/src/controller/database.controller.ts +0 -24
  38. package/src/controller/materialization.controller.spec.ts +0 -293
  39. package/src/controller/materialization.controller.ts +0 -319
  40. package/src/controller/model.controller.ts +0 -164
  41. package/src/controller/package.controller.spec.ts +0 -273
  42. package/src/controller/package.controller.ts +0 -306
  43. package/src/controller/query.controller.ts +0 -152
  44. package/src/controller/theme.controller.ts +0 -83
  45. package/src/controller/watch-mode.controller.ts +0 -243
  46. package/src/data_styles.ts +0 -71
  47. package/src/default-publisher.config.json +0 -23
  48. package/src/ducklake_version.spec.ts +0 -163
  49. package/src/ducklake_version.ts +0 -153
  50. package/src/errors.spec.ts +0 -119
  51. package/src/errors.ts +0 -237
  52. package/src/filter_deprecation.spec.ts +0 -64
  53. package/src/filter_deprecation.ts +0 -42
  54. package/src/health.spec.ts +0 -123
  55. package/src/health.ts +0 -233
  56. package/src/heap_check.spec.ts +0 -144
  57. package/src/heap_check.ts +0 -144
  58. package/src/instrumentation.ts +0 -214
  59. package/src/json_utils.spec.ts +0 -51
  60. package/src/json_utils.ts +0 -33
  61. package/src/logger.spec.ts +0 -210
  62. package/src/logger.ts +0 -251
  63. package/src/malloy_pin_prereqs.spec.ts +0 -25
  64. package/src/materialization_metrics.spec.ts +0 -169
  65. package/src/materialization_metrics.ts +0 -346
  66. package/src/mcp/error_messages.spec.ts +0 -35
  67. package/src/mcp/error_messages.ts +0 -183
  68. package/src/mcp/handler_utils.spec.ts +0 -108
  69. package/src/mcp/handler_utils.ts +0 -251
  70. package/src/mcp/mcp_constants.ts +0 -6
  71. package/src/mcp/query_envelope.spec.ts +0 -229
  72. package/src/mcp/query_envelope.ts +0 -240
  73. package/src/mcp/server.protocol.spec.ts +0 -250
  74. package/src/mcp/server.ts +0 -87
  75. package/src/mcp/skills/build_skills_bundle.spec.ts +0 -51
  76. package/src/mcp/skills/build_skills_bundle.ts +0 -176
  77. package/src/mcp/skills/skills_bundle.json +0 -1
  78. package/src/mcp/skills/skills_bundle.spec.ts +0 -171
  79. package/src/mcp/tool_response.spec.ts +0 -108
  80. package/src/mcp/tool_response.ts +0 -138
  81. package/src/mcp/tools/compile_tool.spec.ts +0 -315
  82. package/src/mcp/tools/compile_tool.ts +0 -208
  83. package/src/mcp/tools/docs_search/build_docs_index.ts +0 -132
  84. package/src/mcp/tools/docs_search/malloy_docs_index.json +0 -1
  85. package/src/mcp/tools/docs_search_tool.spec.ts +0 -32
  86. package/src/mcp/tools/docs_search_tool.ts +0 -128
  87. package/src/mcp/tools/embedding_index.spec.ts +0 -1236
  88. package/src/mcp/tools/embedding_index.ts +0 -808
  89. package/src/mcp/tools/execute_query_tool.spec.ts +0 -294
  90. package/src/mcp/tools/execute_query_tool.ts +0 -316
  91. package/src/mcp/tools/get_context_eval.ts +0 -275
  92. package/src/mcp/tools/get_context_tool.spec.ts +0 -592
  93. package/src/mcp/tools/get_context_tool.ts +0 -585
  94. package/src/mcp/tools/reload_package_tool.spec.ts +0 -232
  95. package/src/mcp/tools/reload_package_tool.ts +0 -132
  96. package/src/mcp_config.spec.ts +0 -919
  97. package/src/mcp_config.ts +0 -425
  98. package/src/oom_guards.integration.spec.ts +0 -269
  99. package/src/package_load/package_load_pool.spec.ts +0 -252
  100. package/src/package_load/package_load_pool.ts +0 -931
  101. package/src/package_load/package_load_worker.ts +0 -1034
  102. package/src/package_load/protocol.ts +0 -364
  103. package/src/package_load/rpc_wait_accountant.spec.ts +0 -109
  104. package/src/package_load/rpc_wait_accountant.ts +0 -76
  105. package/src/package_load_metrics.spec.ts +0 -114
  106. package/src/package_load_metrics.ts +0 -127
  107. package/src/path_safety.spec.ts +0 -158
  108. package/src/path_safety.ts +0 -146
  109. package/src/pg_helpers.spec.ts +0 -223
  110. package/src/pg_helpers.ts +0 -52
  111. package/src/query_cap_metrics.spec.ts +0 -89
  112. package/src/query_cap_metrics.ts +0 -116
  113. package/src/query_concurrency.spec.ts +0 -247
  114. package/src/query_concurrency.ts +0 -237
  115. package/src/query_metadata_metrics.ts +0 -49
  116. package/src/query_param_utils.ts +0 -29
  117. package/src/query_timeout.spec.ts +0 -224
  118. package/src/query_timeout.ts +0 -179
  119. package/src/runtime/publisher.js +0 -328
  120. package/src/server-old.ts +0 -988
  121. package/src/server.ts +0 -2059
  122. package/src/service/annotations.spec.ts +0 -118
  123. package/src/service/annotations.ts +0 -91
  124. package/src/service/authorize.spec.ts +0 -154
  125. package/src/service/authorize.ts +0 -498
  126. package/src/service/authorize_integration.spec.ts +0 -2041
  127. package/src/service/build_plan.spec.ts +0 -685
  128. package/src/service/build_plan.ts +0 -797
  129. package/src/service/compile_authorize.spec.ts +0 -85
  130. package/src/service/compile_fragment_techniques.spec.ts +0 -156
  131. package/src/service/connection.spec.ts +0 -2360
  132. package/src/service/connection.ts +0 -2187
  133. package/src/service/connection_config.spec.ts +0 -734
  134. package/src/service/connection_config.ts +0 -899
  135. package/src/service/connection_federation.spec.ts +0 -184
  136. package/src/service/connection_fingerprint.spec.ts +0 -102
  137. package/src/service/connection_service.spec.ts +0 -711
  138. package/src/service/connection_service.ts +0 -291
  139. package/src/service/cron_evaluator.spec.ts +0 -79
  140. package/src/service/cron_evaluator.ts +0 -105
  141. package/src/service/db_utils.spec.ts +0 -1085
  142. package/src/service/db_utils.ts +0 -1502
  143. package/src/service/duckdb_instance_isolation.spec.ts +0 -137
  144. package/src/service/ducklake_lazy_attach.spec.ts +0 -110
  145. package/src/service/embedding_provider.spec.ts +0 -329
  146. package/src/service/embedding_provider.ts +0 -236
  147. package/src/service/environment.ts +0 -2191
  148. package/src/service/environment_admission.spec.ts +0 -344
  149. package/src/service/environment_compile.spec.ts +0 -197
  150. package/src/service/environment_store.spec.ts +0 -2274
  151. package/src/service/environment_store.ts +0 -2434
  152. package/src/service/environment_store_anchoring.spec.ts +0 -107
  153. package/src/service/environment_store_clone.spec.ts +0 -350
  154. package/src/service/explore_visibility.spec.ts +0 -434
  155. package/src/service/exports_probe.spec.ts +0 -107
  156. package/src/service/extension_fetch_policy.spec.ts +0 -256
  157. package/src/service/filter.spec.ts +0 -458
  158. package/src/service/filter.ts +0 -341
  159. package/src/service/filter_bypass.spec.ts +0 -418
  160. package/src/service/filter_integration.spec.ts +0 -1004
  161. package/src/service/freshness.spec.ts +0 -183
  162. package/src/service/freshness.ts +0 -112
  163. package/src/service/gcs_s3_utils.ts +0 -427
  164. package/src/service/given.ts +0 -105
  165. package/src/service/givens_integration.spec.ts +0 -219
  166. package/src/service/manifest_loader.spec.ts +0 -189
  167. package/src/service/manifest_loader.ts +0 -156
  168. package/src/service/materialization_build_session.spec.ts +0 -435
  169. package/src/service/materialization_build_session.ts +0 -681
  170. package/src/service/materialization_config_validation.spec.ts +0 -99
  171. package/src/service/materialization_config_validation.ts +0 -120
  172. package/src/service/materialization_eligibility.spec.ts +0 -158
  173. package/src/service/materialization_eligibility.ts +0 -305
  174. package/src/service/materialization_schedule_surface.spec.ts +0 -256
  175. package/src/service/materialization_scheduler.spec.ts +0 -289
  176. package/src/service/materialization_scheduler.ts +0 -304
  177. package/src/service/materialization_serve_transform.spec.ts +0 -1003
  178. package/src/service/materialization_serve_transform.ts +0 -779
  179. package/src/service/materialization_service.spec.ts +0 -2373
  180. package/src/service/materialization_service.ts +0 -2342
  181. package/src/service/materialization_test_fixtures.ts +0 -249
  182. package/src/service/model.spec.ts +0 -1157
  183. package/src/service/model.ts +0 -3370
  184. package/src/service/model_limits.spec.ts +0 -209
  185. package/src/service/model_limits.ts +0 -131
  186. package/src/service/model_storage_serve.spec.ts +0 -193
  187. package/src/service/model_storage_serve_joins.spec.ts +0 -193
  188. package/src/service/package.spec.ts +0 -612
  189. package/src/service/package.ts +0 -1799
  190. package/src/service/package_manifest.spec.ts +0 -230
  191. package/src/service/package_manifest.ts +0 -241
  192. package/src/service/package_memory_governor.spec.ts +0 -173
  193. package/src/service/package_memory_governor.ts +0 -233
  194. package/src/service/package_quote_bound_tables.spec.ts +0 -171
  195. package/src/service/package_race.spec.ts +0 -208
  196. package/src/service/package_reload_safety.spec.ts +0 -193
  197. package/src/service/package_rollback.spec.ts +0 -190
  198. package/src/service/package_worker_path.spec.ts +0 -645
  199. package/src/service/path_injection.spec.ts +0 -39
  200. package/src/service/persist_annotation_validation.spec.ts +0 -89
  201. package/src/service/persist_annotation_validation.ts +0 -59
  202. package/src/service/persistence_policy.spec.ts +0 -374
  203. package/src/service/proxy.spec.ts +0 -414
  204. package/src/service/proxy.ts +0 -248
  205. package/src/service/query_boundary.spec.ts +0 -470
  206. package/src/service/query_metadata.spec.ts +0 -408
  207. package/src/service/query_metadata.ts +0 -492
  208. package/src/service/query_metadata_identity.spec.ts +0 -149
  209. package/src/service/query_text.spec.ts +0 -79
  210. package/src/service/query_text.ts +0 -43
  211. package/src/service/quoting.spec.ts +0 -135
  212. package/src/service/quoting.ts +0 -78
  213. package/src/service/resolve_environment.ts +0 -15
  214. package/src/service/restricted_mode.spec.ts +0 -299
  215. package/src/service/source_extraction.ts +0 -225
  216. package/src/service/theme_store.ts +0 -199
  217. package/src/storage/DatabaseInterface.ts +0 -198
  218. package/src/storage/StorageManager.ts +0 -113
  219. package/src/storage/duckdb/ConnectionRepository.ts +0 -143
  220. package/src/storage/duckdb/DuckDBConnection.ts +0 -123
  221. package/src/storage/duckdb/DuckDBRepository.ts +0 -208
  222. package/src/storage/duckdb/EnvironmentRepository.ts +0 -148
  223. package/src/storage/duckdb/MaterializationRepository.spec.ts +0 -191
  224. package/src/storage/duckdb/MaterializationRepository.ts +0 -336
  225. package/src/storage/duckdb/PackageRepository.ts +0 -135
  226. package/src/storage/duckdb/schema.ts +0 -229
  227. package/src/stream_helpers.spec.ts +0 -280
  228. package/src/stream_helpers.ts +0 -162
  229. package/src/telemetry.ts +0 -20
  230. package/src/test_helpers/metrics_harness.ts +0 -166
  231. package/src/theme_key_parity.spec.ts +0 -57
  232. package/src/utils.ts +0 -29
  233. package/tests/fixtures/authorize-compile/model.malloy +0 -9
  234. package/tests/fixtures/authorize-compile/publisher.json +0 -4
  235. package/tests/fixtures/html-pages-nopublic/model.malloy +0 -1
  236. package/tests/fixtures/html-pages-nopublic/publisher.json +0 -5
  237. package/tests/fixtures/html-pages-test/data.csv +0 -3
  238. package/tests/fixtures/html-pages-test/public/assets/app.css +0 -3
  239. package/tests/fixtures/html-pages-test/public/barehtml.html +0 -4
  240. package/tests/fixtures/html-pages-test/public/bodymeta.html +0 -8
  241. package/tests/fixtures/html-pages-test/public/data.json +0 -1
  242. package/tests/fixtures/html-pages-test/public/fitcomment.html +0 -11
  243. package/tests/fixtures/html-pages-test/public/index.html +0 -9
  244. package/tests/fixtures/html-pages-test/public/nohead.html +0 -8
  245. package/tests/fixtures/html-pages-test/public/notfit.html +0 -13
  246. package/tests/fixtures/html-pages-test/public/slides.html +0 -12
  247. package/tests/fixtures/html-pages-test/public/sub/page2.html +0 -9
  248. package/tests/fixtures/html-pages-test/public/unterminated.html +0 -10
  249. package/tests/fixtures/html-pages-test/publisher.json +0 -5
  250. package/tests/fixtures/html-pages-test/report.malloy +0 -1
  251. package/tests/fixtures/persist-multi-level/data/orders.csv +0 -5
  252. package/tests/fixtures/persist-multi-level/multi_level.malloy +0 -18
  253. package/tests/fixtures/persist-multi-level/publisher.json +0 -5
  254. package/tests/fixtures/persist-schedule-test/data/orders.csv +0 -5
  255. package/tests/fixtures/persist-schedule-test/persist_schedule_test.malloy +0 -11
  256. package/tests/fixtures/persist-schedule-test/publisher.json +0 -7
  257. package/tests/fixtures/persist-schedule-warn-test/data/orders.csv +0 -5
  258. package/tests/fixtures/persist-schedule-warn-test/persist_warn_test.malloy +0 -11
  259. package/tests/fixtures/persist-schedule-warn-test/publisher.json +0 -6
  260. package/tests/fixtures/persist-test/data/orders.csv +0 -5
  261. package/tests/fixtures/persist-test/persist_test.malloy +0 -11
  262. package/tests/fixtures/persist-test/publisher.json +0 -5
  263. package/tests/fixtures/publisher.config.json +0 -15
  264. package/tests/fixtures/query-givens/data/orders.csv +0 -7
  265. package/tests/fixtures/query-givens/model.malloy +0 -34
  266. package/tests/fixtures/query-givens/publisher.json +0 -5
  267. package/tests/fixtures/xlsx/database.xlsx +0 -0
  268. package/tests/harness/mcp_test_setup.ts +0 -332
  269. package/tests/harness/rest_e2e.ts +0 -68
  270. package/tests/integration/authorize/compile_authorize_http.integration.spec.ts +0 -92
  271. package/tests/integration/concurrent_package/concurrent_package.integration.spec.ts +0 -280
  272. package/tests/integration/duckdb_storage/duckdb_storage.integration.spec.ts +0 -138
  273. package/tests/integration/first_boot/readiness_line.integration.spec.ts +0 -177
  274. package/tests/integration/html_pages/html_pages.integration.spec.ts +0 -440
  275. package/tests/integration/legacy_routes/legacy_routes.integration.spec.ts +0 -259
  276. package/tests/integration/materialization/environment_list.integration.spec.ts +0 -133
  277. package/tests/integration/materialization/freshness_gate.integration.spec.ts +0 -292
  278. package/tests/integration/materialization/manifest_binding.integration.spec.ts +0 -430
  279. package/tests/integration/materialization/materialization_lifecycle.integration.spec.ts +0 -441
  280. package/tests/integration/materialization/orchestrated_rebind.integration.spec.ts +0 -150
  281. package/tests/integration/materialization/reference_manifest.integration.spec.ts +0 -251
  282. package/tests/integration/materialization/schedule_edit.integration.spec.ts +0 -126
  283. package/tests/integration/materialization/scheduler.integration.spec.ts +0 -200
  284. package/tests/integration/materialization/scheduler_recovery.integration.spec.ts +0 -172
  285. package/tests/integration/materialization/scheduler_transitions.integration.spec.ts +0 -256
  286. package/tests/integration/materialization/scheduler_wiring.integration.spec.ts +0 -143
  287. package/tests/integration/mcp/mcp_execute_query_tool.integration.spec.ts +0 -438
  288. package/tests/integration/mcp/mcp_get_context_semantic.integration.spec.ts +0 -235
  289. package/tests/integration/mcp/mcp_transport.integration.spec.ts +0 -98
  290. package/tests/integration/query_givens/query_givens.integration.spec.ts +0 -146
  291. package/tests/integration/query_givens/query_givens_authorize.integration.spec.ts +0 -121
  292. package/tests/integration/sdk_givens/sdk_givens.integration.spec.ts +0 -110
  293. package/tests/integration/watch-mode/watch_mode.integration.spec.ts +0 -421
  294. package/tests/unit/controller/theme.controller.spec.ts +0 -141
  295. package/tests/unit/duckdb/attached_databases.test.ts +0 -1243
  296. package/tests/unit/duckdb/duckdb_connection.test.ts +0 -181
  297. package/tests/unit/duckdb/legacy_schema_migration.test.ts +0 -197
  298. package/tests/unit/duckdb/repositories.test.ts +0 -208
  299. package/tests/unit/ducklake/ducklake.test.ts +0 -952
  300. package/tests/unit/service/theme_store.spec.ts +0 -139
  301. package/tsconfig.json +0 -32
@@ -1,1085 +0,0 @@
1
- import { afterEach, describe, expect, it, mock } from "bun:test";
2
-
3
- // Stub the missing optional dependency so db_utils.ts can be imported
4
- mock.module("@azure/identity", () => ({
5
- ClientSecretCredential: class {},
6
- }));
7
- mock.module("@azure/storage-blob", () => ({
8
- ContainerClient: class {},
9
- }));
10
- // Records bigquery.dataset() calls so tests can assert how a (possibly
11
- // project-qualified) schema name is split into datasetId + projectId.
12
- const bqDatasetCalls: Array<{ id: string; options?: unknown }> = [];
13
- mock.module("@google-cloud/bigquery", () => ({
14
- BigQuery: class {
15
- dataset(id: string, options?: unknown) {
16
- bqDatasetCalls.push({ id, options });
17
- return { getTables: async () => [[]] };
18
- }
19
- },
20
- }));
21
-
22
- import { Connection } from "@malloydata/malloy";
23
- import { normalizeQueryArray } from "../query_param_utils";
24
- import {
25
- extractErrorDataFromError,
26
- getSchemasForConnection,
27
- listTablesForSchema,
28
- sqlInFilter,
29
- } from "./db_utils";
30
- import { components } from "../api";
31
-
32
- type ApiConnection = components["schemas"]["Connection"];
33
-
34
- /**
35
- * Minimal mock Connection whose runSQL captures the SQL string
36
- * and returns configurable rows.
37
- */
38
- function mockConnection(rows: unknown[] = []) {
39
- let lastSQL = "";
40
- return {
41
- get lastSQL() {
42
- return lastSQL;
43
- },
44
- conn: {
45
- runSQL: async (sql: string) => {
46
- lastSQL = sql;
47
- return { rows };
48
- },
49
- } as unknown as Connection,
50
- };
51
- }
52
-
53
- // ---------------------------------------------------------------------------
54
- // sqlInFilter
55
- // ---------------------------------------------------------------------------
56
- describe("sqlInFilter", () => {
57
- it("returns empty string for undefined", () => {
58
- expect(sqlInFilter("col", undefined)).toBe("");
59
- });
60
-
61
- it("returns empty string for empty array", () => {
62
- expect(sqlInFilter("col", [])).toBe("");
63
- });
64
-
65
- it("builds single-value IN clause", () => {
66
- expect(sqlInFilter("TABLE_NAME", ["orders"])).toBe(
67
- "AND TABLE_NAME IN ('orders')",
68
- );
69
- });
70
-
71
- it("builds multi-value IN clause", () => {
72
- expect(sqlInFilter("t", ["a", "b", "c"])).toBe(
73
- "AND t IN ('a', 'b', 'c')",
74
- );
75
- });
76
-
77
- it("escapes single quotes in values", () => {
78
- expect(sqlInFilter("t", ["it's", "a'b"])).toBe(
79
- "AND t IN ('it''s', 'a''b')",
80
- );
81
- });
82
- });
83
-
84
- // ---------------------------------------------------------------------------
85
- // normalizeQueryArray
86
- // ---------------------------------------------------------------------------
87
- describe("normalizeQueryArray", () => {
88
- it("returns undefined for undefined", () => {
89
- expect(normalizeQueryArray(undefined)).toBeUndefined();
90
- });
91
-
92
- it("returns undefined for null", () => {
93
- expect(normalizeQueryArray(null)).toBeUndefined();
94
- });
95
-
96
- it("wraps a single string in an array", () => {
97
- expect(normalizeQueryArray("table1")).toEqual(["table1"]);
98
- });
99
-
100
- it("passes through an array of strings", () => {
101
- expect(normalizeQueryArray(["a", "b"])).toEqual(["a", "b"]);
102
- });
103
-
104
- it("converts non-string array elements to strings", () => {
105
- expect(normalizeQueryArray([1, true])).toEqual(["1", "true"]);
106
- });
107
-
108
- it("converts a numeric value to a string array", () => {
109
- expect(normalizeQueryArray(42)).toEqual(["42"]);
110
- });
111
- });
112
-
113
- // ---------------------------------------------------------------------------
114
- // listTablesForSchema – SQL generation & result grouping
115
- // ---------------------------------------------------------------------------
116
- describe("listTablesForSchema", () => {
117
- const columnRows = [
118
- { TABLE_NAME: "orders", COLUMN_NAME: "id", DATA_TYPE: "INTEGER" },
119
- { TABLE_NAME: "orders", COLUMN_NAME: "total", DATA_TYPE: "DECIMAL" },
120
- { TABLE_NAME: "customers", COLUMN_NAME: "id", DATA_TYPE: "INTEGER" },
121
- { TABLE_NAME: "customers", COLUMN_NAME: "name", DATA_TYPE: "VARCHAR" },
122
- ];
123
-
124
- describe("mysql", () => {
125
- const conn: ApiConnection = {
126
- name: "test",
127
- type: "mysql",
128
- mysqlConnection: {
129
- host: "localhost",
130
- port: 3306,
131
- user: "root",
132
- password: "",
133
- database: "testdb",
134
- },
135
- };
136
-
137
- it("queries INFORMATION_SCHEMA.COLUMNS and groups into ApiTable[]", async () => {
138
- const m = mockConnection(columnRows);
139
- const tables = await listTablesForSchema(conn, "testdb", m.conn);
140
-
141
- expect(m.lastSQL).toContain("information_schema.columns");
142
- expect(m.lastSQL).toContain("table_schema = 'testdb'");
143
- expect(tables).toHaveLength(2);
144
- expect(tables[0].resource).toBe("testdb.orders");
145
- expect(tables[0].columns).toEqual([
146
- { name: "id", type: "integer" },
147
- { name: "total", type: "decimal" },
148
- ]);
149
- expect(tables[1].resource).toBe("testdb.customers");
150
- });
151
-
152
- it("includes IN filter when tableNames provided", async () => {
153
- const m = mockConnection(columnRows.slice(0, 2));
154
- await listTablesForSchema(conn, "testdb", m.conn, ["orders"]);
155
- expect(m.lastSQL).toContain("AND TABLE_NAME IN ('orders')");
156
- });
157
-
158
- it("omits IN filter when tableNames is undefined", async () => {
159
- const m = mockConnection(columnRows);
160
- await listTablesForSchema(conn, "testdb", m.conn);
161
- expect(m.lastSQL).not.toContain("IN (");
162
- });
163
- });
164
-
165
- describe("postgres", () => {
166
- const conn: ApiConnection = {
167
- name: "test",
168
- type: "postgres",
169
- postgresConnection: {
170
- host: "localhost",
171
- port: 5432,
172
- userName: "postgres",
173
- password: "",
174
- databaseName: "testdb",
175
- },
176
- };
177
-
178
- it("queries information_schema.columns wrapped in row_to_json", async () => {
179
- const m = mockConnection(columnRows);
180
- const tables = await listTablesForSchema(conn, "public", m.conn);
181
-
182
- expect(m.lastSQL).toContain("row_to_json");
183
- expect(m.lastSQL).toContain("information_schema.columns");
184
- expect(m.lastSQL).toContain("table_schema = 'public'");
185
- expect(tables).toHaveLength(2);
186
- expect(tables[0].resource).toBe("public.orders");
187
- });
188
-
189
- it("includes IN filter when tableNames provided", async () => {
190
- const m = mockConnection([]);
191
- await listTablesForSchema(conn, "public", m.conn, ["orders"]);
192
- expect(m.lastSQL).toContain("AND table_name IN ('orders')");
193
- });
194
- });
195
-
196
- describe("snowflake", () => {
197
- const conn: ApiConnection = {
198
- name: "test",
199
- type: "snowflake",
200
- snowflakeConnection: {
201
- account: "test_account",
202
- username: "user",
203
- password: "pass",
204
- database: "MY_DB",
205
- schema: "PUBLIC",
206
- },
207
- };
208
-
209
- it("queries DATABASE.INFORMATION_SCHEMA.COLUMNS", async () => {
210
- const m = mockConnection(columnRows);
211
- const tables = await listTablesForSchema(conn, "MY_DB.PUBLIC", m.conn);
212
-
213
- expect(m.lastSQL).toContain("MY_DB.INFORMATION_SCHEMA.COLUMNS");
214
- expect(m.lastSQL).toContain("TABLE_SCHEMA = 'PUBLIC'");
215
- expect(tables).toHaveLength(2);
216
- expect(tables[0].resource).toBe("MY_DB.PUBLIC.orders");
217
- });
218
-
219
- it("falls back to connection database when schema is unqualified", async () => {
220
- const m = mockConnection(columnRows);
221
- const tables = await listTablesForSchema(conn, "PUBLIC", m.conn);
222
-
223
- expect(m.lastSQL).toContain("MY_DB.INFORMATION_SCHEMA.COLUMNS");
224
- expect(tables[0].resource).toBe("MY_DB.PUBLIC.orders");
225
- });
226
-
227
- it("includes IN filter when tableNames provided", async () => {
228
- const m = mockConnection([]);
229
- await listTablesForSchema(conn, "MY_DB.PUBLIC", m.conn, [
230
- "orders",
231
- "customers",
232
- ]);
233
- expect(m.lastSQL).toContain(
234
- "AND TABLE_NAME IN ('orders', 'customers')",
235
- );
236
- });
237
- });
238
-
239
- describe("trino", () => {
240
- it("uses catalog-prefixed information_schema.columns", async () => {
241
- const conn: ApiConnection = {
242
- name: "test",
243
- type: "trino",
244
- trinoConnection: {
245
- server: "localhost",
246
- port: 8080,
247
- catalog: "hive",
248
- },
249
- };
250
- const m = mockConnection(columnRows);
251
- const tables = await listTablesForSchema(conn, "default", m.conn);
252
-
253
- expect(m.lastSQL).toContain("hive.information_schema.columns");
254
- expect(m.lastSQL).toContain("table_schema = 'default'");
255
- expect(tables[0].resource).toBe("hive.default.orders");
256
- });
257
-
258
- it("extracts catalog from schemaName when no explicit catalog", async () => {
259
- const conn: ApiConnection = {
260
- name: "test",
261
- type: "trino",
262
- trinoConnection: { server: "localhost", port: 8080 },
263
- };
264
- const m = mockConnection(columnRows);
265
- const tables = await listTablesForSchema(conn, "hive.default", m.conn);
266
-
267
- expect(m.lastSQL).toContain("hive.information_schema.columns");
268
- expect(m.lastSQL).toContain("table_schema = 'default'");
269
- expect(tables[0].resource).toBe("hive.default.orders");
270
- });
271
- });
272
-
273
- describe("databricks", () => {
274
- it("uses defaultCatalog-prefixed information_schema.columns", async () => {
275
- const conn: ApiConnection = {
276
- name: "test",
277
- type: "databricks",
278
- databricksConnection: {
279
- host: "dbc.cloud.databricks.com",
280
- path: "/sql/1.0/warehouses/abc",
281
- token: "dapi",
282
- defaultCatalog: "main",
283
- },
284
- };
285
- const m = mockConnection(columnRows);
286
- const tables = await listTablesForSchema(conn, "default", m.conn);
287
-
288
- expect(m.lastSQL).toContain("main.information_schema.columns");
289
- expect(m.lastSQL).toContain("table_schema = 'default'");
290
- expect(tables[0].resource).toBe("main.default.orders");
291
- });
292
-
293
- it("extracts catalog from schemaName when no defaultCatalog", async () => {
294
- const conn: ApiConnection = {
295
- name: "test",
296
- type: "databricks",
297
- databricksConnection: {
298
- host: "dbc.cloud.databricks.com",
299
- path: "/sql/1.0/warehouses/abc",
300
- token: "dapi",
301
- },
302
- };
303
- const m = mockConnection(columnRows);
304
- const tables = await listTablesForSchema(conn, "main.default", m.conn);
305
-
306
- expect(m.lastSQL).toContain("main.information_schema.columns");
307
- expect(m.lastSQL).toContain("table_schema = 'default'");
308
- expect(tables[0].resource).toBe("main.default.orders");
309
- });
310
-
311
- it("includes IN filter when tableNames provided", async () => {
312
- const conn: ApiConnection = {
313
- name: "test",
314
- type: "databricks",
315
- databricksConnection: {
316
- host: "dbc.cloud.databricks.com",
317
- path: "/sql/1.0/warehouses/abc",
318
- token: "dapi",
319
- defaultCatalog: "main",
320
- },
321
- };
322
- const m = mockConnection(columnRows);
323
- await listTablesForSchema(conn, "default", m.conn, ["orders"]);
324
- expect(m.lastSQL).toContain("table_name IN ('orders')");
325
- });
326
- });
327
-
328
- describe("duckdb", () => {
329
- const conn: ApiConnection = {
330
- name: "test",
331
- type: "duckdb",
332
- duckdbConnection: {},
333
- };
334
-
335
- it("queries information_schema.columns with catalog and schema", async () => {
336
- const rows = columnRows.map((r) => ({
337
- table_name: r.TABLE_NAME,
338
- column_name: r.COLUMN_NAME,
339
- data_type: r.DATA_TYPE,
340
- }));
341
- const m = mockConnection(rows);
342
- const tables = await listTablesForSchema(conn, "memory.main", m.conn);
343
-
344
- expect(m.lastSQL).toContain("information_schema.columns");
345
- expect(m.lastSQL).toContain("table_schema = 'main'");
346
- expect(m.lastSQL).toContain("table_catalog = 'memory'");
347
- expect(tables).toHaveLength(2);
348
- expect(tables[0].resource).toBe("memory.main.orders");
349
- });
350
- });
351
-
352
- describe("motherduck", () => {
353
- const conn: ApiConnection = {
354
- name: "test",
355
- type: "motherduck",
356
- motherduckConnection: { accessToken: "fake" },
357
- };
358
-
359
- it("queries information_schema.columns", async () => {
360
- const m = mockConnection(columnRows);
361
- const tables = await listTablesForSchema(conn, "main", m.conn);
362
-
363
- expect(m.lastSQL).toContain("information_schema.columns");
364
- expect(m.lastSQL).toContain("table_schema = 'main'");
365
- expect(tables).toHaveLength(2);
366
- expect(tables[0].resource).toBe("main.orders");
367
- });
368
- });
369
-
370
- describe("ducklake", () => {
371
- const conn: ApiConnection = {
372
- name: "test",
373
- type: "ducklake",
374
- ducklakeConnection: {
375
- catalog: {
376
- postgresConnection: {
377
- host: "localhost",
378
- port: 5432,
379
- userName: "postgres",
380
- password: "",
381
- databaseName: "testdb",
382
- },
383
- },
384
- storage: { bucketUrl: "s3://bucket" },
385
- },
386
- };
387
-
388
- it("queries information_schema.columns with catalog and schema", async () => {
389
- const m = mockConnection(columnRows);
390
- const tables = await listTablesForSchema(
391
- conn,
392
- "mycat.myschema",
393
- m.conn,
394
- );
395
-
396
- expect(m.lastSQL).toContain("information_schema.columns");
397
- expect(m.lastSQL).toContain("table_schema = 'myschema'");
398
- expect(m.lastSQL).toContain("table_catalog = 'mycat'");
399
- expect(tables[0].resource).toBe("mycat.myschema.orders");
400
- });
401
- });
402
-
403
- describe("column grouping", () => {
404
- it("lowercases data types", async () => {
405
- const conn: ApiConnection = {
406
- name: "test",
407
- type: "mysql",
408
- mysqlConnection: {
409
- host: "localhost",
410
- port: 3306,
411
- user: "root",
412
- password: "",
413
- database: "testdb",
414
- },
415
- };
416
- const m = mockConnection([
417
- {
418
- TABLE_NAME: "t",
419
- COLUMN_NAME: "col",
420
- DATA_TYPE: "VARCHAR(255)",
421
- },
422
- ]);
423
- const tables = await listTablesForSchema(conn, "testdb", m.conn);
424
- expect(tables[0]?.columns?.[0]?.type).toBe("varchar(255)");
425
- });
426
-
427
- it("returns empty array when no rows", async () => {
428
- const conn: ApiConnection = {
429
- name: "test",
430
- type: "postgres",
431
- postgresConnection: {
432
- host: "localhost",
433
- port: 5432,
434
- userName: "postgres",
435
- password: "",
436
- databaseName: "testdb",
437
- },
438
- };
439
- const m = mockConnection([]);
440
- const tables = await listTablesForSchema(conn, "public", m.conn);
441
- expect(tables).toEqual([]);
442
- });
443
- });
444
-
445
- describe("error handling", () => {
446
- it("throws for unsupported connection type", async () => {
447
- const conn = {
448
- name: "test",
449
- type: "unsupported",
450
- } as unknown as ApiConnection;
451
- const m = mockConnection();
452
- await expect(
453
- listTablesForSchema(conn, "schema", m.conn),
454
- ).rejects.toThrow("Unsupported connection type");
455
- });
456
-
457
- it("throws when duckdb schema is not qualified", async () => {
458
- const conn: ApiConnection = {
459
- name: "test",
460
- type: "duckdb",
461
- duckdbConnection: {},
462
- };
463
- const m = mockConnection();
464
- await expect(
465
- listTablesForSchema(conn, "main", m.conn),
466
- ).rejects.toThrow('must be qualified as "catalog.schema"');
467
- });
468
-
469
- it("throws when snowflake schema is unqualified and no database configured", async () => {
470
- const conn: ApiConnection = {
471
- name: "test",
472
- type: "snowflake",
473
- snowflakeConnection: {
474
- account: "test_account",
475
- username: "user",
476
- password: "pass",
477
- },
478
- };
479
- const m = mockConnection();
480
- await expect(
481
- listTablesForSchema(conn, "PUBLIC", m.conn),
482
- ).rejects.toThrow("Cannot resolve database");
483
- });
484
- });
485
-
486
- describe("ducklake schema prefixing", () => {
487
- const conn: ApiConnection = {
488
- name: "myconn",
489
- type: "ducklake",
490
- ducklakeConnection: {
491
- catalog: {
492
- postgresConnection: {
493
- host: "localhost",
494
- port: 5432,
495
- userName: "postgres",
496
- password: "",
497
- databaseName: "testdb",
498
- },
499
- },
500
- storage: { bucketUrl: "s3://bucket" },
501
- },
502
- };
503
-
504
- it("prefixes bare schema name with connection name", async () => {
505
- const m = mockConnection([]);
506
- await listTablesForSchema(conn, "main", m.conn);
507
- expect(m.lastSQL).toContain("table_catalog = 'myconn'");
508
- expect(m.lastSQL).toContain("table_schema = 'main'");
509
- });
510
-
511
- it("uses provided catalog when schema is already qualified", async () => {
512
- const m = mockConnection([]);
513
- await listTablesForSchema(conn, "othercat.myschema", m.conn);
514
- expect(m.lastSQL).toContain("table_catalog = 'othercat'");
515
- expect(m.lastSQL).toContain("table_schema = 'myschema'");
516
- });
517
- });
518
- });
519
-
520
- // ---------------------------------------------------------------------------
521
- // getSchemasForConnection – schema listing
522
- // ---------------------------------------------------------------------------
523
- describe("getSchemasForConnection", () => {
524
- describe("postgres", () => {
525
- const conn: ApiConnection = {
526
- name: "test",
527
- type: "postgres",
528
- postgresConnection: {
529
- host: "localhost",
530
- port: 5432,
531
- userName: "postgres",
532
- password: "",
533
- databaseName: "testdb",
534
- },
535
- };
536
-
537
- it("queries information_schema.schemata wrapped in row_to_json", async () => {
538
- const rows = [
539
- { schema_name: "public" },
540
- { schema_name: "information_schema" },
541
- { schema_name: "pg_catalog" },
542
- { schema_name: "app" },
543
- ];
544
- const m = mockConnection(rows);
545
- const schemas = await getSchemasForConnection(conn, m.conn);
546
-
547
- expect(m.lastSQL).toContain("row_to_json");
548
- expect(m.lastSQL).toContain("information_schema.schemata");
549
- expect(schemas).toHaveLength(4);
550
- expect(schemas.find((s) => s.name === "public")?.isDefault).toBe(true);
551
- expect(
552
- schemas.find((s) => s.name === "information_schema")?.isHidden,
553
- ).toBe(true);
554
- expect(schemas.find((s) => s.name === "pg_catalog")?.isHidden).toBe(
555
- true,
556
- );
557
- expect(schemas.find((s) => s.name === "app")?.isHidden).toBe(false);
558
- });
559
- });
560
-
561
- describe("mysql", () => {
562
- it("returns a single schema from the connection database", async () => {
563
- const conn: ApiConnection = {
564
- name: "test",
565
- type: "mysql",
566
- mysqlConnection: {
567
- host: "localhost",
568
- port: 3306,
569
- user: "root",
570
- password: "",
571
- database: "mydb",
572
- },
573
- };
574
- const m = mockConnection();
575
- const schemas = await getSchemasForConnection(conn, m.conn);
576
-
577
- expect(schemas).toHaveLength(1);
578
- expect(schemas[0].name).toBe("mydb");
579
- expect(schemas[0].isDefault).toBe(true);
580
- });
581
- });
582
-
583
- describe("snowflake", () => {
584
- it("queries INFORMATION_SCHEMA.SCHEMATA with database filter", async () => {
585
- const conn: ApiConnection = {
586
- name: "test",
587
- type: "snowflake",
588
- snowflakeConnection: {
589
- account: "test_account",
590
- username: "user",
591
- password: "pass",
592
- database: "MY_DB",
593
- schema: "PUBLIC",
594
- },
595
- };
596
- const rows = [
597
- {
598
- CATALOG_NAME: "MY_DB",
599
- SCHEMA_NAME: "PUBLIC",
600
- SCHEMA_OWNER: "SYSADMIN",
601
- },
602
- {
603
- CATALOG_NAME: "MY_DB",
604
- SCHEMA_NAME: "INFORMATION_SCHEMA",
605
- SCHEMA_OWNER: "",
606
- },
607
- ];
608
- const m = mockConnection(rows);
609
- const schemas = await getSchemasForConnection(conn, m.conn);
610
-
611
- expect(m.lastSQL).toContain("INFORMATION_SCHEMA.SCHEMATA");
612
- expect(m.lastSQL).toContain("CATALOG_NAME = 'MY_DB'");
613
- expect(schemas).toHaveLength(2);
614
- expect(schemas.find((s) => s.name === "MY_DB.PUBLIC")?.isDefault).toBe(
615
- true,
616
- );
617
- expect(
618
- schemas.find((s) => s.name === "MY_DB.INFORMATION_SCHEMA")
619
- ?.isHidden,
620
- ).toBe(true);
621
- });
622
- });
623
-
624
- describe("duckdb", () => {
625
- it("queries information_schema.schemata and hides system schemas", async () => {
626
- const conn: ApiConnection = {
627
- name: "test",
628
- type: "duckdb",
629
- duckdbConnection: {},
630
- };
631
- const rows = [
632
- { catalog_name: "main", schema_name: "main" },
633
- { catalog_name: "main", schema_name: "information_schema" },
634
- { catalog_name: "system", schema_name: "main" },
635
- ];
636
- const m = mockConnection(rows);
637
- const schemas = await getSchemasForConnection(conn, m.conn);
638
-
639
- expect(schemas).toHaveLength(3);
640
- const mainMain = schemas.find((s) => s.name === "main.main");
641
- expect(mainMain?.isDefault).toBe(true);
642
- expect(mainMain?.isHidden).toBe(false);
643
- expect(
644
- schemas.find((s) => s.name === "main.information_schema")?.isHidden,
645
- ).toBe(true);
646
- expect(schemas.find((s) => s.name === "system.main")?.isHidden).toBe(
647
- true,
648
- );
649
- });
650
- });
651
-
652
- describe("motherduck", () => {
653
- it("queries information_schema.schemata with optional database filter", async () => {
654
- const conn: ApiConnection = {
655
- name: "test",
656
- type: "motherduck",
657
- motherduckConnection: { accessToken: "fake", database: "mydb" },
658
- };
659
- const rows = [
660
- { schema_name: "main" },
661
- { schema_name: "information_schema" },
662
- ];
663
- const m = mockConnection(rows);
664
- const schemas = await getSchemasForConnection(conn, m.conn);
665
-
666
- expect(m.lastSQL).toContain("catalog_name = 'mydb'");
667
- expect(schemas).toHaveLength(2);
668
- expect(schemas.find((s) => s.name === "main")?.isDefault).toBe(true);
669
- expect(
670
- schemas.find((s) => s.name === "information_schema")?.isHidden,
671
- ).toBe(true);
672
- });
673
- });
674
-
675
- describe("ducklake", () => {
676
- it("queries information_schema.schemata filtered by connection name", async () => {
677
- const conn: ApiConnection = {
678
- name: "myconn",
679
- type: "ducklake",
680
- ducklakeConnection: {
681
- catalog: {
682
- postgresConnection: {
683
- host: "localhost",
684
- port: 5432,
685
- userName: "postgres",
686
- password: "",
687
- databaseName: "testdb",
688
- },
689
- },
690
- storage: { bucketUrl: "s3://bucket" },
691
- },
692
- };
693
- const rows = [
694
- { schema_name: "main" },
695
- { schema_name: "public" },
696
- { schema_name: "internal" },
697
- ];
698
- const m = mockConnection(rows);
699
- const schemas = await getSchemasForConnection(conn, m.conn);
700
-
701
- expect(m.lastSQL).toContain("catalog_name = 'myconn'");
702
- expect(schemas).toHaveLength(3);
703
- expect(schemas.find((s) => s.name === "main")?.isHidden).toBe(false);
704
- expect(schemas.find((s) => s.name === "public")?.isHidden).toBe(false);
705
- expect(schemas.find((s) => s.name === "internal")?.isHidden).toBe(
706
- true,
707
- );
708
- });
709
- });
710
-
711
- describe("trino", () => {
712
- it("queries catalog.information_schema.schemata when catalog is set", async () => {
713
- const conn: ApiConnection = {
714
- name: "test",
715
- type: "trino",
716
- trinoConnection: {
717
- server: "localhost",
718
- port: 8080,
719
- catalog: "hive",
720
- },
721
- };
722
- const rows = [
723
- { schema_name: "default" },
724
- { schema_name: "information_schema" },
725
- ];
726
- const m = mockConnection(rows);
727
- const schemas = await getSchemasForConnection(conn, m.conn);
728
-
729
- expect(m.lastSQL).toContain("hive.information_schema.schemata");
730
- expect(schemas).toHaveLength(2);
731
- expect(schemas.find((s) => s.name === "default")?.isHidden).toBe(
732
- false,
733
- );
734
- expect(
735
- schemas.find((s) => s.name === "information_schema")?.isHidden,
736
- ).toBe(true);
737
- });
738
- });
739
-
740
- describe("databricks", () => {
741
- it("queries catalog.information_schema.schemata when defaultCatalog is set", async () => {
742
- const conn: ApiConnection = {
743
- name: "test",
744
- type: "databricks",
745
- databricksConnection: {
746
- host: "dbc.cloud.databricks.com",
747
- path: "/sql/1.0/warehouses/abc",
748
- token: "dapi",
749
- defaultCatalog: "main",
750
- defaultSchema: "default",
751
- },
752
- };
753
- const rows = [
754
- { schema_name: "default" },
755
- { schema_name: "information_schema" },
756
- ];
757
- const m = mockConnection(rows);
758
- const schemas = await getSchemasForConnection(conn, m.conn);
759
-
760
- expect(m.lastSQL).toContain("main.information_schema.schemata");
761
- expect(schemas).toHaveLength(2);
762
- expect(schemas.find((s) => s.name === "default")?.isDefault).toBe(
763
- true,
764
- );
765
- expect(
766
- schemas.find((s) => s.name === "information_schema")?.isHidden,
767
- ).toBe(true);
768
- });
769
-
770
- it("falls back to SHOW CATALOGS when defaultCatalog is unset", async () => {
771
- const conn: ApiConnection = {
772
- name: "test",
773
- type: "databricks",
774
- databricksConnection: {
775
- host: "dbc.cloud.databricks.com",
776
- path: "/sql/1.0/warehouses/abc",
777
- token: "dapi",
778
- },
779
- };
780
- // First runSQL returns catalog list; subsequent runSQL calls (one
781
- // per catalog) return schema rows. We use a dedicated mock so we
782
- // can switch behavior across calls.
783
- let callIndex = 0;
784
- const calls: string[] = [];
785
- const fakeConn = {
786
- runSQL: async (sql: string) => {
787
- calls.push(sql);
788
- if (callIndex++ === 0) {
789
- return {
790
- rows: [{ catalog: "main" }, { catalog: "samples" }],
791
- };
792
- }
793
- return { rows: [{ schema_name: "default" }] };
794
- },
795
- } as unknown as Connection;
796
-
797
- const schemas = await getSchemasForConnection(conn, fakeConn);
798
-
799
- expect(calls[0]).toContain("SHOW CATALOGS");
800
- expect(
801
- calls.some((c) => c.includes("main.information_schema.schemata")),
802
- ).toBe(true);
803
- expect(
804
- calls.some((c) =>
805
- c.includes("samples.information_schema.schemata"),
806
- ),
807
- ).toBe(true);
808
- // Two catalogs each contribute one schema → catalog-qualified names.
809
- expect(schemas.map((s) => s.name)).toEqual([
810
- "main.default",
811
- "samples.default",
812
- ]);
813
- });
814
-
815
- it("warns and continues when a catalog rejects information_schema", async () => {
816
- const conn: ApiConnection = {
817
- name: "test",
818
- type: "databricks",
819
- databricksConnection: {
820
- host: "dbc.cloud.databricks.com",
821
- path: "/sql/1.0/warehouses/abc",
822
- token: "dapi",
823
- },
824
- };
825
- let callIndex = 0;
826
- const fakeConn = {
827
- runSQL: async (sql: string) => {
828
- if (callIndex++ === 0) {
829
- return { rows: [{ catalog: "denied" }, { catalog: "ok" }] };
830
- }
831
- if (sql.includes("denied")) {
832
- throw new Error("USE CATALOG denied");
833
- }
834
- return { rows: [{ schema_name: "default" }] };
835
- },
836
- } as unknown as Connection;
837
-
838
- const schemas = await getSchemasForConnection(conn, fakeConn);
839
-
840
- // Denied catalog is skipped, ok catalog contributes its schema.
841
- expect(schemas).toHaveLength(1);
842
- expect(schemas[0].name).toBe("ok.default");
843
- });
844
- });
845
-
846
- it("throws for unsupported connection type", async () => {
847
- const conn = {
848
- name: "test",
849
- type: "unsupported",
850
- } as unknown as ApiConnection;
851
- const m = mockConnection();
852
- await expect(getSchemasForConnection(conn, m.conn)).rejects.toThrow(
853
- "Unsupported connection type",
854
- );
855
- });
856
- });
857
-
858
- // ---------------------------------------------------------------------------
859
- // extractErrorDataFromError
860
- // ---------------------------------------------------------------------------
861
- describe("extractErrorDataFromError", () => {
862
- it("extracts message from Error instance", () => {
863
- const result = extractErrorDataFromError(new Error("boom"));
864
- expect(result.error).toBe("boom");
865
- });
866
-
867
- it("converts string errors", () => {
868
- const result = extractErrorDataFromError("something went wrong");
869
- expect(result.error).toBe("something went wrong");
870
- });
871
-
872
- it("converts non-string non-Error values", () => {
873
- const result = extractErrorDataFromError(42);
874
- expect(result.error).toBe("42");
875
- });
876
-
877
- it("extracts task property when present", () => {
878
- const err = Object.assign(new Error("fail"), { task: { id: 1 } });
879
- const result = extractErrorDataFromError(err);
880
- expect(result.error).toBe("fail");
881
- expect(result.task).toEqual({ id: 1 });
882
- });
883
- });
884
-
885
- // ---------------------------------------------------------------------------
886
- // publisher proxy introspection
887
- //
888
- // The remote dataplane is the oracle for introspection correctness and is not
889
- // simulated here. These tests cover only this server's own logic: the request
890
- // URL it builds, the auth header, the bare-name table filter, and how it
891
- // surfaces a non-ok / unreachable dataplane (credentials redacted from errors).
892
- // ---------------------------------------------------------------------------
893
- describe("publisher proxy introspection", () => {
894
- const realFetch = globalThis.fetch;
895
- let lastRequest:
896
- | { url: string; headers: Record<string, string> }
897
- | undefined;
898
-
899
- function stubFetch(
900
- payload: unknown,
901
- init: { ok?: boolean; status?: number; body?: string } = {},
902
- ) {
903
- lastRequest = undefined;
904
- const ok = init.ok ?? true;
905
- globalThis.fetch = (async (url: unknown, opts?: RequestInit) => {
906
- lastRequest = {
907
- url: String(url),
908
- headers: (opts?.headers as Record<string, string>) ?? {},
909
- };
910
- return {
911
- ok,
912
- status: init.status ?? (ok ? 200 : 500),
913
- json: async () => payload,
914
- text: async () => init.body ?? "",
915
- } as Response;
916
- }) as typeof fetch;
917
- }
918
-
919
- afterEach(() => {
920
- globalThis.fetch = realFetch;
921
- });
922
-
923
- const conn: ApiConnection = {
924
- name: "analytics",
925
- type: "publisher",
926
- publisherConnection: {
927
- connectionUri:
928
- "https://org.data.example.com/api/v0/environments/proj/connections/analytics",
929
- accessToken: "jwt-token",
930
- },
931
- };
932
- // No malloyConnection is used for the publisher path.
933
- const noConn = undefined as unknown as Connection;
934
-
935
- it("forwards GET /schemas to the connectionUri with a bearer token", async () => {
936
- const schemas = [{ name: "main", isHidden: false, isDefault: true }];
937
- stubFetch(schemas);
938
- const result = await getSchemasForConnection(conn, noConn);
939
-
940
- expect(lastRequest?.url).toBe(
941
- "https://org.data.example.com/api/v0/environments/proj/connections/analytics/schemas",
942
- );
943
- expect(lastRequest?.headers["Authorization"]).toBe("Bearer jwt-token");
944
- expect(result).toEqual(schemas);
945
- });
946
-
947
- it("omits the Authorization header when no accessToken is configured", async () => {
948
- const tokenless: ApiConnection = {
949
- name: "analytics",
950
- type: "publisher",
951
- publisherConnection: {
952
- connectionUri:
953
- "https://org.data.example.com/api/v0/environments/proj/connections/analytics",
954
- },
955
- };
956
- stubFetch([]);
957
- await getSchemasForConnection(tokenless, noConn);
958
- expect(lastRequest?.headers["Authorization"]).toBeUndefined();
959
- });
960
-
961
- it("forwards GET /schemas/<schema>/tables and returns all tables when unfiltered", async () => {
962
- const tables = [
963
- { resource: "main.orders" },
964
- { resource: "main.customers" },
965
- ];
966
- stubFetch(tables);
967
- const result = await listTablesForSchema(conn, "main", noConn);
968
-
969
- expect(lastRequest?.url).toBe(
970
- "https://org.data.example.com/api/v0/environments/proj/connections/analytics/schemas/main/tables",
971
- );
972
- expect(result).toEqual(tables);
973
- });
974
-
975
- it("URL-encodes the schema name in the tables path", async () => {
976
- stubFetch([]);
977
- await listTablesForSchema(conn, "weird schema/name", noConn);
978
- expect(lastRequest?.url).toContain(
979
- "/schemas/weird%20schema%2Fname/tables",
980
- );
981
- });
982
-
983
- it("filters by bare table name across schema- and catalog-qualified resources", async () => {
984
- const tables = [
985
- { resource: "main.orders" }, // "<schema>.<table>"
986
- { resource: "cat.main.customers" }, // "<catalog>.<schema>.<table>"
987
- { resource: "main.events" },
988
- ];
989
- stubFetch(tables);
990
- const result = await listTablesForSchema(conn, "main", noConn, [
991
- "orders",
992
- "customers",
993
- ]);
994
- expect(result.map((t) => t.resource)).toEqual([
995
- "main.orders",
996
- "cat.main.customers",
997
- ]);
998
- });
999
-
1000
- it("returns an empty list when the filter matches nothing (not all rows)", async () => {
1001
- stubFetch([{ resource: "main.orders" }]);
1002
- const result = await listTablesForSchema(conn, "main", noConn, [
1003
- "nonexistent",
1004
- ]);
1005
- expect(result).toEqual([]);
1006
- });
1007
-
1008
- it("throws an actionable error on a non-ok dataplane response", async () => {
1009
- stubFetch(null, { ok: false, status: 403, body: "forbidden" });
1010
- await expect(getSchemasForConnection(conn, noConn)).rejects.toThrow(
1011
- "failed (403): forbidden",
1012
- );
1013
- });
1014
-
1015
- it("redacts URL-embedded credentials from error messages", async () => {
1016
- const withCreds: ApiConnection = {
1017
- name: "analytics",
1018
- type: "publisher",
1019
- publisherConnection: {
1020
- connectionUri:
1021
- "https://user:secret@org.data.example.com/api/v0/environments/proj/connections/analytics",
1022
- },
1023
- };
1024
- stubFetch(null, { ok: false, status: 500, body: "boom" });
1025
- await expect(getSchemasForConnection(withCreds, noConn)).rejects.toThrow(
1026
- /Publisher dataplane request to https:\/\/org\.data\.example\.com.*failed \(500\)/,
1027
- );
1028
- await expect(
1029
- getSchemasForConnection(withCreds, noConn),
1030
- ).rejects.not.toThrow(/secret/);
1031
- });
1032
-
1033
- it("surfaces a network-level failure with the connection name and no token", async () => {
1034
- globalThis.fetch = (async () => {
1035
- throw new Error("ECONNREFUSED");
1036
- }) as unknown as typeof fetch;
1037
- await expect(getSchemasForConnection(conn, noConn)).rejects.toThrow(
1038
- 'failed for connection "analytics": ECONNREFUSED',
1039
- );
1040
- });
1041
- });
1042
-
1043
- // ---------------------------------------------------------------------------
1044
- // listTablesForSchema — bigquery (project-qualified dataset)
1045
- // ---------------------------------------------------------------------------
1046
- describe("listTablesForSchema bigquery", () => {
1047
- const bqConnection = {
1048
- name: "bq",
1049
- type: "bigquery",
1050
- bigqueryConnection: {
1051
- defaultProjectId: "default-proj",
1052
- serviceAccountKeyJson: JSON.stringify({ project_id: "default-proj" }),
1053
- },
1054
- } as unknown as ApiConnection;
1055
- const malloyConn = {} as unknown as Connection;
1056
-
1057
- it("splits a project-qualified schema into a bare dataset id + projectId", async () => {
1058
- bqDatasetCalls.length = 0;
1059
- await listTablesForSchema(bqConnection, "myproj.myds", malloyConn);
1060
- expect(bqDatasetCalls).toHaveLength(1);
1061
- expect(bqDatasetCalls[0]?.id).toBe("myds");
1062
- expect(bqDatasetCalls[0]?.options).toEqual({ projectId: "myproj" });
1063
- });
1064
-
1065
- it("splits on the last dot so domain-scoped project ids survive", async () => {
1066
- bqDatasetCalls.length = 0;
1067
- await listTablesForSchema(
1068
- bqConnection,
1069
- "domain.com:proj.myds",
1070
- malloyConn,
1071
- );
1072
- expect(bqDatasetCalls[0]?.id).toBe("myds");
1073
- expect(bqDatasetCalls[0]?.options).toEqual({
1074
- projectId: "domain.com:proj",
1075
- });
1076
- });
1077
-
1078
- it("passes a bare dataset name through unqualified", async () => {
1079
- bqDatasetCalls.length = 0;
1080
- await listTablesForSchema(bqConnection, "myds", malloyConn);
1081
- expect(bqDatasetCalls).toHaveLength(1);
1082
- expect(bqDatasetCalls[0]?.id).toBe("myds");
1083
- expect(bqDatasetCalls[0]?.options).toBeUndefined();
1084
- });
1085
- });