@malloy-publisher/server 0.0.176 → 0.0.177

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.
@@ -0,0 +1,712 @@
1
+ import { 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
+ mock.module("@google-cloud/bigquery", () => ({
11
+ BigQuery: class {},
12
+ }));
13
+
14
+ import { Connection } from "@malloydata/malloy";
15
+ import { normalizeQueryArray } from "../server";
16
+ import {
17
+ extractErrorDataFromError,
18
+ getSchemasForConnection,
19
+ listTablesForSchema,
20
+ sqlInFilter,
21
+ } from "./db_utils";
22
+ import { components } from "../api";
23
+
24
+ type ApiConnection = components["schemas"]["Connection"];
25
+
26
+ /**
27
+ * Minimal mock Connection whose runSQL captures the SQL string
28
+ * and returns configurable rows.
29
+ */
30
+ function mockConnection(rows: unknown[] = []) {
31
+ let lastSQL = "";
32
+ return {
33
+ get lastSQL() {
34
+ return lastSQL;
35
+ },
36
+ conn: {
37
+ runSQL: async (sql: string) => {
38
+ lastSQL = sql;
39
+ return { rows };
40
+ },
41
+ } as unknown as Connection,
42
+ };
43
+ }
44
+
45
+ // ---------------------------------------------------------------------------
46
+ // sqlInFilter
47
+ // ---------------------------------------------------------------------------
48
+ describe("sqlInFilter", () => {
49
+ it("returns empty string for undefined", () => {
50
+ expect(sqlInFilter("col", undefined)).toBe("");
51
+ });
52
+
53
+ it("returns empty string for empty array", () => {
54
+ expect(sqlInFilter("col", [])).toBe("");
55
+ });
56
+
57
+ it("builds single-value IN clause", () => {
58
+ expect(sqlInFilter("TABLE_NAME", ["orders"])).toBe(
59
+ "AND TABLE_NAME IN ('orders')",
60
+ );
61
+ });
62
+
63
+ it("builds multi-value IN clause", () => {
64
+ expect(sqlInFilter("t", ["a", "b", "c"])).toBe(
65
+ "AND t IN ('a', 'b', 'c')",
66
+ );
67
+ });
68
+
69
+ it("escapes single quotes in values", () => {
70
+ expect(sqlInFilter("t", ["it's", "a'b"])).toBe(
71
+ "AND t IN ('it''s', 'a''b')",
72
+ );
73
+ });
74
+ });
75
+
76
+ // ---------------------------------------------------------------------------
77
+ // normalizeQueryArray
78
+ // ---------------------------------------------------------------------------
79
+ describe("normalizeQueryArray", () => {
80
+ it("returns undefined for undefined", () => {
81
+ expect(normalizeQueryArray(undefined)).toBeUndefined();
82
+ });
83
+
84
+ it("returns undefined for null", () => {
85
+ expect(normalizeQueryArray(null)).toBeUndefined();
86
+ });
87
+
88
+ it("wraps a single string in an array", () => {
89
+ expect(normalizeQueryArray("table1")).toEqual(["table1"]);
90
+ });
91
+
92
+ it("passes through an array of strings", () => {
93
+ expect(normalizeQueryArray(["a", "b"])).toEqual(["a", "b"]);
94
+ });
95
+
96
+ it("converts non-string array elements to strings", () => {
97
+ expect(normalizeQueryArray([1, true])).toEqual(["1", "true"]);
98
+ });
99
+
100
+ it("converts a numeric value to a string array", () => {
101
+ expect(normalizeQueryArray(42)).toEqual(["42"]);
102
+ });
103
+ });
104
+
105
+ // ---------------------------------------------------------------------------
106
+ // listTablesForSchema – SQL generation & result grouping
107
+ // ---------------------------------------------------------------------------
108
+ describe("listTablesForSchema", () => {
109
+ const columnRows = [
110
+ { TABLE_NAME: "orders", COLUMN_NAME: "id", DATA_TYPE: "INTEGER" },
111
+ { TABLE_NAME: "orders", COLUMN_NAME: "total", DATA_TYPE: "DECIMAL" },
112
+ { TABLE_NAME: "customers", COLUMN_NAME: "id", DATA_TYPE: "INTEGER" },
113
+ { TABLE_NAME: "customers", COLUMN_NAME: "name", DATA_TYPE: "VARCHAR" },
114
+ ];
115
+
116
+ describe("mysql", () => {
117
+ const conn: ApiConnection = {
118
+ name: "test",
119
+ type: "mysql",
120
+ mysqlConnection: {
121
+ host: "localhost",
122
+ port: 3306,
123
+ user: "root",
124
+ password: "",
125
+ database: "testdb",
126
+ },
127
+ };
128
+
129
+ it("queries INFORMATION_SCHEMA.COLUMNS and groups into ApiTable[]", async () => {
130
+ const m = mockConnection(columnRows);
131
+ const tables = await listTablesForSchema(conn, "testdb", m.conn);
132
+
133
+ expect(m.lastSQL).toContain("information_schema.columns");
134
+ expect(m.lastSQL).toContain("table_schema = 'testdb'");
135
+ expect(tables).toHaveLength(2);
136
+ expect(tables[0].resource).toBe("testdb.orders");
137
+ expect(tables[0].columns).toEqual([
138
+ { name: "id", type: "integer" },
139
+ { name: "total", type: "decimal" },
140
+ ]);
141
+ expect(tables[1].resource).toBe("testdb.customers");
142
+ });
143
+
144
+ it("includes IN filter when tableNames provided", async () => {
145
+ const m = mockConnection(columnRows.slice(0, 2));
146
+ await listTablesForSchema(conn, "testdb", m.conn, ["orders"]);
147
+ expect(m.lastSQL).toContain("AND TABLE_NAME IN ('orders')");
148
+ });
149
+
150
+ it("omits IN filter when tableNames is undefined", async () => {
151
+ const m = mockConnection(columnRows);
152
+ await listTablesForSchema(conn, "testdb", m.conn);
153
+ expect(m.lastSQL).not.toContain("IN (");
154
+ });
155
+ });
156
+
157
+ describe("postgres", () => {
158
+ const conn: ApiConnection = {
159
+ name: "test",
160
+ type: "postgres",
161
+ postgresConnection: {
162
+ host: "localhost",
163
+ port: 5432,
164
+ userName: "postgres",
165
+ password: "",
166
+ databaseName: "testdb",
167
+ },
168
+ };
169
+
170
+ it("queries information_schema.columns with correct schema", async () => {
171
+ const m = mockConnection(columnRows);
172
+ const tables = await listTablesForSchema(conn, "public", m.conn);
173
+
174
+ expect(m.lastSQL).toContain("information_schema.columns");
175
+ expect(m.lastSQL).toContain("table_schema = 'public'");
176
+ expect(tables).toHaveLength(2);
177
+ expect(tables[0].resource).toBe("public.orders");
178
+ });
179
+
180
+ it("includes IN filter when tableNames provided", async () => {
181
+ const m = mockConnection([]);
182
+ await listTablesForSchema(conn, "public", m.conn, ["orders"]);
183
+ expect(m.lastSQL).toContain("AND table_name IN ('orders')");
184
+ });
185
+ });
186
+
187
+ describe("snowflake", () => {
188
+ const conn: ApiConnection = {
189
+ name: "test",
190
+ type: "snowflake",
191
+ snowflakeConnection: {
192
+ account: "test_account",
193
+ username: "user",
194
+ password: "pass",
195
+ database: "MY_DB",
196
+ schema: "PUBLIC",
197
+ },
198
+ };
199
+
200
+ it("queries DATABASE.INFORMATION_SCHEMA.COLUMNS", async () => {
201
+ const m = mockConnection(columnRows);
202
+ const tables = await listTablesForSchema(conn, "MY_DB.PUBLIC", m.conn);
203
+
204
+ expect(m.lastSQL).toContain("MY_DB.INFORMATION_SCHEMA.COLUMNS");
205
+ expect(m.lastSQL).toContain("TABLE_SCHEMA = 'PUBLIC'");
206
+ expect(tables).toHaveLength(2);
207
+ expect(tables[0].resource).toBe("MY_DB.PUBLIC.orders");
208
+ });
209
+
210
+ it("falls back to connection database when schema is unqualified", async () => {
211
+ const m = mockConnection(columnRows);
212
+ const tables = await listTablesForSchema(conn, "PUBLIC", m.conn);
213
+
214
+ expect(m.lastSQL).toContain("MY_DB.INFORMATION_SCHEMA.COLUMNS");
215
+ expect(tables[0].resource).toBe("MY_DB.PUBLIC.orders");
216
+ });
217
+
218
+ it("includes IN filter when tableNames provided", async () => {
219
+ const m = mockConnection([]);
220
+ await listTablesForSchema(conn, "MY_DB.PUBLIC", m.conn, [
221
+ "orders",
222
+ "customers",
223
+ ]);
224
+ expect(m.lastSQL).toContain(
225
+ "AND TABLE_NAME IN ('orders', 'customers')",
226
+ );
227
+ });
228
+ });
229
+
230
+ describe("trino", () => {
231
+ it("uses catalog-prefixed information_schema.columns", async () => {
232
+ const conn: ApiConnection = {
233
+ name: "test",
234
+ type: "trino",
235
+ trinoConnection: {
236
+ server: "localhost",
237
+ port: 8080,
238
+ catalog: "hive",
239
+ },
240
+ };
241
+ const m = mockConnection(columnRows);
242
+ const tables = await listTablesForSchema(conn, "default", m.conn);
243
+
244
+ expect(m.lastSQL).toContain("hive.information_schema.columns");
245
+ expect(m.lastSQL).toContain("table_schema = 'default'");
246
+ expect(tables[0].resource).toBe("hive.default.orders");
247
+ });
248
+
249
+ it("extracts catalog from schemaName when no explicit catalog", async () => {
250
+ const conn: ApiConnection = {
251
+ name: "test",
252
+ type: "trino",
253
+ trinoConnection: { server: "localhost", port: 8080 },
254
+ };
255
+ const m = mockConnection(columnRows);
256
+ const tables = await listTablesForSchema(conn, "hive.default", m.conn);
257
+
258
+ expect(m.lastSQL).toContain("hive.information_schema.columns");
259
+ expect(m.lastSQL).toContain("table_schema = 'default'");
260
+ expect(tables[0].resource).toBe("hive.default.orders");
261
+ });
262
+ });
263
+
264
+ describe("duckdb", () => {
265
+ const conn: ApiConnection = {
266
+ name: "test",
267
+ type: "duckdb",
268
+ duckdbConnection: {},
269
+ };
270
+
271
+ it("queries information_schema.columns with catalog and schema", async () => {
272
+ const rows = columnRows.map((r) => ({
273
+ table_name: r.TABLE_NAME,
274
+ column_name: r.COLUMN_NAME,
275
+ data_type: r.DATA_TYPE,
276
+ }));
277
+ const m = mockConnection(rows);
278
+ const tables = await listTablesForSchema(conn, "memory.main", m.conn);
279
+
280
+ expect(m.lastSQL).toContain("information_schema.columns");
281
+ expect(m.lastSQL).toContain("table_schema = 'main'");
282
+ expect(m.lastSQL).toContain("table_catalog = 'memory'");
283
+ expect(tables).toHaveLength(2);
284
+ expect(tables[0].resource).toBe("memory.main.orders");
285
+ });
286
+ });
287
+
288
+ describe("motherduck", () => {
289
+ const conn: ApiConnection = {
290
+ name: "test",
291
+ type: "motherduck",
292
+ motherduckConnection: { accessToken: "fake" },
293
+ };
294
+
295
+ it("queries information_schema.columns", async () => {
296
+ const m = mockConnection(columnRows);
297
+ const tables = await listTablesForSchema(conn, "main", m.conn);
298
+
299
+ expect(m.lastSQL).toContain("information_schema.columns");
300
+ expect(m.lastSQL).toContain("table_schema = 'main'");
301
+ expect(tables).toHaveLength(2);
302
+ expect(tables[0].resource).toBe("main.orders");
303
+ });
304
+ });
305
+
306
+ describe("ducklake", () => {
307
+ const conn: ApiConnection = {
308
+ name: "test",
309
+ type: "ducklake",
310
+ ducklakeConnection: {
311
+ catalog: {
312
+ postgresConnection: {
313
+ host: "localhost",
314
+ port: 5432,
315
+ userName: "postgres",
316
+ password: "",
317
+ databaseName: "testdb",
318
+ },
319
+ },
320
+ storage: { bucketUrl: "s3://bucket" },
321
+ },
322
+ };
323
+
324
+ it("queries information_schema.columns with catalog and schema", async () => {
325
+ const m = mockConnection(columnRows);
326
+ const tables = await listTablesForSchema(
327
+ conn,
328
+ "mycat.myschema",
329
+ m.conn,
330
+ );
331
+
332
+ expect(m.lastSQL).toContain("information_schema.columns");
333
+ expect(m.lastSQL).toContain("table_schema = 'myschema'");
334
+ expect(m.lastSQL).toContain("table_catalog = 'mycat'");
335
+ expect(tables[0].resource).toBe("mycat.myschema.orders");
336
+ });
337
+ });
338
+
339
+ describe("column grouping", () => {
340
+ it("lowercases data types", async () => {
341
+ const conn: ApiConnection = {
342
+ name: "test",
343
+ type: "mysql",
344
+ mysqlConnection: {
345
+ host: "localhost",
346
+ port: 3306,
347
+ user: "root",
348
+ password: "",
349
+ database: "testdb",
350
+ },
351
+ };
352
+ const m = mockConnection([
353
+ {
354
+ TABLE_NAME: "t",
355
+ COLUMN_NAME: "col",
356
+ DATA_TYPE: "VARCHAR(255)",
357
+ },
358
+ ]);
359
+ const tables = await listTablesForSchema(conn, "testdb", m.conn);
360
+ expect(tables[0]?.columns?.[0]?.type).toBe("varchar(255)");
361
+ });
362
+
363
+ it("returns empty array when no rows", async () => {
364
+ const conn: ApiConnection = {
365
+ name: "test",
366
+ type: "postgres",
367
+ postgresConnection: {
368
+ host: "localhost",
369
+ port: 5432,
370
+ userName: "postgres",
371
+ password: "",
372
+ databaseName: "testdb",
373
+ },
374
+ };
375
+ const m = mockConnection([]);
376
+ const tables = await listTablesForSchema(conn, "public", m.conn);
377
+ expect(tables).toEqual([]);
378
+ });
379
+ });
380
+
381
+ describe("error handling", () => {
382
+ it("throws for unsupported connection type", async () => {
383
+ const conn = {
384
+ name: "test",
385
+ type: "unsupported",
386
+ } as unknown as ApiConnection;
387
+ const m = mockConnection();
388
+ await expect(
389
+ listTablesForSchema(conn, "schema", m.conn),
390
+ ).rejects.toThrow("Unsupported connection type");
391
+ });
392
+
393
+ it("throws when duckdb schema is not qualified", async () => {
394
+ const conn: ApiConnection = {
395
+ name: "test",
396
+ type: "duckdb",
397
+ duckdbConnection: {},
398
+ };
399
+ const m = mockConnection();
400
+ await expect(
401
+ listTablesForSchema(conn, "main", m.conn),
402
+ ).rejects.toThrow('must be qualified as "catalog.schema"');
403
+ });
404
+
405
+ it("throws when snowflake schema is unqualified and no database configured", async () => {
406
+ const conn: ApiConnection = {
407
+ name: "test",
408
+ type: "snowflake",
409
+ snowflakeConnection: {
410
+ account: "test_account",
411
+ username: "user",
412
+ password: "pass",
413
+ },
414
+ };
415
+ const m = mockConnection();
416
+ await expect(
417
+ listTablesForSchema(conn, "PUBLIC", m.conn),
418
+ ).rejects.toThrow("Cannot resolve database");
419
+ });
420
+ });
421
+
422
+ describe("ducklake schema prefixing", () => {
423
+ const conn: ApiConnection = {
424
+ name: "myconn",
425
+ type: "ducklake",
426
+ ducklakeConnection: {
427
+ catalog: {
428
+ postgresConnection: {
429
+ host: "localhost",
430
+ port: 5432,
431
+ userName: "postgres",
432
+ password: "",
433
+ databaseName: "testdb",
434
+ },
435
+ },
436
+ storage: { bucketUrl: "s3://bucket" },
437
+ },
438
+ };
439
+
440
+ it("prefixes bare schema name with connection name", async () => {
441
+ const m = mockConnection([]);
442
+ await listTablesForSchema(conn, "main", m.conn);
443
+ expect(m.lastSQL).toContain("table_catalog = 'myconn'");
444
+ expect(m.lastSQL).toContain("table_schema = 'main'");
445
+ });
446
+
447
+ it("uses provided catalog when schema is already qualified", async () => {
448
+ const m = mockConnection([]);
449
+ await listTablesForSchema(conn, "othercat.myschema", m.conn);
450
+ expect(m.lastSQL).toContain("table_catalog = 'othercat'");
451
+ expect(m.lastSQL).toContain("table_schema = 'myschema'");
452
+ });
453
+ });
454
+ });
455
+
456
+ // ---------------------------------------------------------------------------
457
+ // getSchemasForConnection – schema listing
458
+ // ---------------------------------------------------------------------------
459
+ describe("getSchemasForConnection", () => {
460
+ describe("postgres", () => {
461
+ const conn: ApiConnection = {
462
+ name: "test",
463
+ type: "postgres",
464
+ postgresConnection: {
465
+ host: "localhost",
466
+ port: 5432,
467
+ userName: "postgres",
468
+ password: "",
469
+ databaseName: "testdb",
470
+ },
471
+ };
472
+
473
+ it("queries information_schema.schemata", async () => {
474
+ const rows = [
475
+ { schema_name: "public" },
476
+ { schema_name: "information_schema" },
477
+ { schema_name: "pg_catalog" },
478
+ { schema_name: "app" },
479
+ ];
480
+ const m = mockConnection(rows);
481
+ const schemas = await getSchemasForConnection(conn, m.conn);
482
+
483
+ expect(m.lastSQL).toContain("information_schema.schemata");
484
+ expect(schemas).toHaveLength(4);
485
+ expect(schemas.find((s) => s.name === "public")?.isDefault).toBe(true);
486
+ expect(
487
+ schemas.find((s) => s.name === "information_schema")?.isHidden,
488
+ ).toBe(true);
489
+ expect(schemas.find((s) => s.name === "pg_catalog")?.isHidden).toBe(
490
+ true,
491
+ );
492
+ expect(schemas.find((s) => s.name === "app")?.isHidden).toBe(false);
493
+ });
494
+ });
495
+
496
+ describe("mysql", () => {
497
+ it("returns a single schema from the connection database", async () => {
498
+ const conn: ApiConnection = {
499
+ name: "test",
500
+ type: "mysql",
501
+ mysqlConnection: {
502
+ host: "localhost",
503
+ port: 3306,
504
+ user: "root",
505
+ password: "",
506
+ database: "mydb",
507
+ },
508
+ };
509
+ const m = mockConnection();
510
+ const schemas = await getSchemasForConnection(conn, m.conn);
511
+
512
+ expect(schemas).toHaveLength(1);
513
+ expect(schemas[0].name).toBe("mydb");
514
+ expect(schemas[0].isDefault).toBe(true);
515
+ });
516
+ });
517
+
518
+ describe("snowflake", () => {
519
+ it("queries INFORMATION_SCHEMA.SCHEMATA with database filter", async () => {
520
+ const conn: ApiConnection = {
521
+ name: "test",
522
+ type: "snowflake",
523
+ snowflakeConnection: {
524
+ account: "test_account",
525
+ username: "user",
526
+ password: "pass",
527
+ database: "MY_DB",
528
+ schema: "PUBLIC",
529
+ },
530
+ };
531
+ const rows = [
532
+ {
533
+ CATALOG_NAME: "MY_DB",
534
+ SCHEMA_NAME: "PUBLIC",
535
+ SCHEMA_OWNER: "SYSADMIN",
536
+ },
537
+ {
538
+ CATALOG_NAME: "MY_DB",
539
+ SCHEMA_NAME: "INFORMATION_SCHEMA",
540
+ SCHEMA_OWNER: "",
541
+ },
542
+ ];
543
+ const m = mockConnection(rows);
544
+ const schemas = await getSchemasForConnection(conn, m.conn);
545
+
546
+ expect(m.lastSQL).toContain("INFORMATION_SCHEMA.SCHEMATA");
547
+ expect(m.lastSQL).toContain("CATALOG_NAME = 'MY_DB'");
548
+ expect(schemas).toHaveLength(2);
549
+ expect(schemas.find((s) => s.name === "MY_DB.PUBLIC")?.isDefault).toBe(
550
+ true,
551
+ );
552
+ expect(
553
+ schemas.find((s) => s.name === "MY_DB.INFORMATION_SCHEMA")
554
+ ?.isHidden,
555
+ ).toBe(true);
556
+ });
557
+ });
558
+
559
+ describe("duckdb", () => {
560
+ it("queries information_schema.schemata and hides system schemas", async () => {
561
+ const conn: ApiConnection = {
562
+ name: "test",
563
+ type: "duckdb",
564
+ duckdbConnection: {},
565
+ };
566
+ const rows = [
567
+ { catalog_name: "main", schema_name: "main" },
568
+ { catalog_name: "main", schema_name: "information_schema" },
569
+ { catalog_name: "system", schema_name: "main" },
570
+ ];
571
+ const m = mockConnection(rows);
572
+ const schemas = await getSchemasForConnection(conn, m.conn);
573
+
574
+ expect(schemas).toHaveLength(3);
575
+ const mainMain = schemas.find((s) => s.name === "main.main");
576
+ expect(mainMain?.isDefault).toBe(true);
577
+ expect(mainMain?.isHidden).toBe(false);
578
+ expect(
579
+ schemas.find((s) => s.name === "main.information_schema")?.isHidden,
580
+ ).toBe(true);
581
+ expect(schemas.find((s) => s.name === "system.main")?.isHidden).toBe(
582
+ true,
583
+ );
584
+ });
585
+ });
586
+
587
+ describe("motherduck", () => {
588
+ it("queries information_schema.schemata with optional database filter", async () => {
589
+ const conn: ApiConnection = {
590
+ name: "test",
591
+ type: "motherduck",
592
+ motherduckConnection: { accessToken: "fake", database: "mydb" },
593
+ };
594
+ const rows = [
595
+ { schema_name: "main" },
596
+ { schema_name: "information_schema" },
597
+ ];
598
+ const m = mockConnection(rows);
599
+ const schemas = await getSchemasForConnection(conn, m.conn);
600
+
601
+ expect(m.lastSQL).toContain("catalog_name = 'mydb'");
602
+ expect(schemas).toHaveLength(2);
603
+ expect(schemas.find((s) => s.name === "main")?.isDefault).toBe(true);
604
+ expect(
605
+ schemas.find((s) => s.name === "information_schema")?.isHidden,
606
+ ).toBe(true);
607
+ });
608
+ });
609
+
610
+ describe("ducklake", () => {
611
+ it("queries information_schema.schemata filtered by connection name", async () => {
612
+ const conn: ApiConnection = {
613
+ name: "myconn",
614
+ type: "ducklake",
615
+ ducklakeConnection: {
616
+ catalog: {
617
+ postgresConnection: {
618
+ host: "localhost",
619
+ port: 5432,
620
+ userName: "postgres",
621
+ password: "",
622
+ databaseName: "testdb",
623
+ },
624
+ },
625
+ storage: { bucketUrl: "s3://bucket" },
626
+ },
627
+ };
628
+ const rows = [
629
+ { schema_name: "main" },
630
+ { schema_name: "public" },
631
+ { schema_name: "internal" },
632
+ ];
633
+ const m = mockConnection(rows);
634
+ const schemas = await getSchemasForConnection(conn, m.conn);
635
+
636
+ expect(m.lastSQL).toContain("catalog_name = 'myconn'");
637
+ expect(schemas).toHaveLength(3);
638
+ expect(schemas.find((s) => s.name === "main")?.isHidden).toBe(false);
639
+ expect(schemas.find((s) => s.name === "public")?.isHidden).toBe(false);
640
+ expect(schemas.find((s) => s.name === "internal")?.isHidden).toBe(
641
+ true,
642
+ );
643
+ });
644
+ });
645
+
646
+ describe("trino", () => {
647
+ it("queries catalog.information_schema.schemata when catalog is set", async () => {
648
+ const conn: ApiConnection = {
649
+ name: "test",
650
+ type: "trino",
651
+ trinoConnection: {
652
+ server: "localhost",
653
+ port: 8080,
654
+ catalog: "hive",
655
+ },
656
+ };
657
+ const rows = [
658
+ { schema_name: "default" },
659
+ { schema_name: "information_schema" },
660
+ ];
661
+ const m = mockConnection(rows);
662
+ const schemas = await getSchemasForConnection(conn, m.conn);
663
+
664
+ expect(m.lastSQL).toContain("hive.information_schema.schemata");
665
+ expect(schemas).toHaveLength(2);
666
+ expect(schemas.find((s) => s.name === "default")?.isHidden).toBe(
667
+ false,
668
+ );
669
+ expect(
670
+ schemas.find((s) => s.name === "information_schema")?.isHidden,
671
+ ).toBe(true);
672
+ });
673
+ });
674
+
675
+ it("throws for unsupported connection type", async () => {
676
+ const conn = {
677
+ name: "test",
678
+ type: "unsupported",
679
+ } as unknown as ApiConnection;
680
+ const m = mockConnection();
681
+ await expect(getSchemasForConnection(conn, m.conn)).rejects.toThrow(
682
+ "Unsupported connection type",
683
+ );
684
+ });
685
+ });
686
+
687
+ // ---------------------------------------------------------------------------
688
+ // extractErrorDataFromError
689
+ // ---------------------------------------------------------------------------
690
+ describe("extractErrorDataFromError", () => {
691
+ it("extracts message from Error instance", () => {
692
+ const result = extractErrorDataFromError(new Error("boom"));
693
+ expect(result.error).toBe("boom");
694
+ });
695
+
696
+ it("converts string errors", () => {
697
+ const result = extractErrorDataFromError("something went wrong");
698
+ expect(result.error).toBe("something went wrong");
699
+ });
700
+
701
+ it("converts non-string non-Error values", () => {
702
+ const result = extractErrorDataFromError(42);
703
+ expect(result.error).toBe("42");
704
+ });
705
+
706
+ it("extracts task property when present", () => {
707
+ const err = Object.assign(new Error("fail"), { task: { id: 1 } });
708
+ const result = extractErrorDataFromError(err);
709
+ expect(result.error).toBe("fail");
710
+ expect(result.task).toEqual({ id: 1 });
711
+ });
712
+ });