@mhjuma/traverse 0.1.0

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 (90) hide show
  1. package/LICENSE +21 -0
  2. package/package.json +46 -0
  3. package/src/bootstrap.spec.tsx +290 -0
  4. package/src/bootstrap.tsx +195 -0
  5. package/src/colors.ts +23 -0
  6. package/src/components/ChartPanel/ChartPanel.logic.spec.ts +358 -0
  7. package/src/components/ChartPanel/ChartPanel.logic.ts +318 -0
  8. package/src/components/ChartPanel/ChartPanel.spec.tsx +331 -0
  9. package/src/components/ChartPanel/ChartPanel.styles.ts +14 -0
  10. package/src/components/ChartPanel/ChartPanel.tsx +151 -0
  11. package/src/components/ConnectionDialog/ConnectionDialog.spec.tsx +767 -0
  12. package/src/components/ConnectionDialog/ConnectionDialog.tsx +652 -0
  13. package/src/components/ConnectionDialog/ConnectionRow.spec.tsx +104 -0
  14. package/src/components/ConnectionDialog/ConnectionRow.tsx +177 -0
  15. package/src/components/EditorToolbar/EditorToolbar.spec.tsx +599 -0
  16. package/src/components/EditorToolbar/EditorToolbar.tsx +259 -0
  17. package/src/components/Explorer/Explorer.spec.tsx +615 -0
  18. package/src/components/Explorer/Explorer.tsx +49 -0
  19. package/src/components/ExplorerWorkspace/ExplorerWorkspace.logic.spec.ts +715 -0
  20. package/src/components/ExplorerWorkspace/ExplorerWorkspace.logic.ts +324 -0
  21. package/src/components/ExplorerWorkspace/ExplorerWorkspace.spec.tsx +450 -0
  22. package/src/components/ExplorerWorkspace/ExplorerWorkspace.styles.ts +478 -0
  23. package/src/components/ExplorerWorkspace/ExplorerWorkspace.tsx +648 -0
  24. package/src/components/ExplorerWorkspace/kqlLanguage.spec.ts +321 -0
  25. package/src/components/ExplorerWorkspace/kqlLanguage.ts +155 -0
  26. package/src/components/ResultsPanel/ElapsedTimer.spec.tsx +51 -0
  27. package/src/components/ResultsPanel/ElapsedTimer.tsx +15 -0
  28. package/src/components/ResultsPanel/HistoryItem.spec.tsx +123 -0
  29. package/src/components/ResultsPanel/HistoryItem.tsx +56 -0
  30. package/src/components/ResultsPanel/ResultSetTab.tsx +18 -0
  31. package/src/components/ResultsPanel/ResultsPanel.spec.tsx +1194 -0
  32. package/src/components/ResultsPanel/ResultsPanel.tsx +321 -0
  33. package/src/components/ResultsPanel/TabButton.tsx +11 -0
  34. package/src/components/ResultsTable/ColumnHeader.tsx +42 -0
  35. package/src/components/ResultsTable/ColumnPickerItem.tsx +16 -0
  36. package/src/components/ResultsTable/FilterCell.tsx +24 -0
  37. package/src/components/ResultsTable/ResultsTable.logic.spec.ts +655 -0
  38. package/src/components/ResultsTable/ResultsTable.logic.ts +264 -0
  39. package/src/components/ResultsTable/ResultsTable.spec.tsx +930 -0
  40. package/src/components/ResultsTable/ResultsTable.styles.ts +108 -0
  41. package/src/components/ResultsTable/ResultsTable.tsx +501 -0
  42. package/src/components/ResultsTable/VirtualCell.spec.tsx +55 -0
  43. package/src/components/ResultsTable/VirtualCell.tsx +32 -0
  44. package/src/components/ResultsTable/VirtualRow.tsx +55 -0
  45. package/src/components/SchemaSidebar/FolderRow.tsx +35 -0
  46. package/src/components/SchemaSidebar/SchemaColumnItem.tsx +41 -0
  47. package/src/components/SchemaSidebar/SchemaContextMenu.spec.tsx +148 -0
  48. package/src/components/SchemaSidebar/SchemaContextMenu.tsx +49 -0
  49. package/src/components/SchemaSidebar/SchemaSidebar.logic.spec.ts +230 -0
  50. package/src/components/SchemaSidebar/SchemaSidebar.logic.ts +74 -0
  51. package/src/components/SchemaSidebar/SchemaSidebar.spec.tsx +1111 -0
  52. package/src/components/SchemaSidebar/SchemaSidebar.tsx +244 -0
  53. package/src/components/SchemaSidebar/SchemaTableItem.tsx +77 -0
  54. package/src/components/TabBar/TabBar.logic.spec.ts +127 -0
  55. package/src/components/TabBar/TabBar.logic.ts +35 -0
  56. package/src/components/TabBar/TabBar.spec.tsx +929 -0
  57. package/src/components/TabBar/TabBar.tsx +284 -0
  58. package/src/components/TabBar/TabItem.tsx +166 -0
  59. package/src/components/VegaChart/VegaChart.spec.tsx +126 -0
  60. package/src/components/VegaChart/VegaChart.tsx +224 -0
  61. package/src/config.spec.ts +44 -0
  62. package/src/config.ts +47 -0
  63. package/src/context/ExplorerColorContext.ts +14 -0
  64. package/src/context/ExplorerStateContext.ts +49 -0
  65. package/src/context/KustoClientContext.ts +12 -0
  66. package/src/context/context.spec.tsx +183 -0
  67. package/src/index.ts +14 -0
  68. package/src/services/cache.spec.ts +31 -0
  69. package/src/services/cache.ts +16 -0
  70. package/src/services/concurrency.spec.ts +139 -0
  71. package/src/services/concurrency.ts +46 -0
  72. package/src/services/kusto.spec.ts +919 -0
  73. package/src/services/kusto.ts +472 -0
  74. package/src/services/queryHistory.spec.ts +287 -0
  75. package/src/services/queryHistory.ts +146 -0
  76. package/src/services/schema.spec.ts +618 -0
  77. package/src/services/schema.ts +294 -0
  78. package/src/services/state-service.spec.ts +670 -0
  79. package/src/services/state-service.ts +602 -0
  80. package/src/services/useKustoQuery.spec.ts +234 -0
  81. package/src/services/useKustoQuery.ts +124 -0
  82. package/src/state/actions.ts +50 -0
  83. package/src/state/index.ts +3 -0
  84. package/src/state/reducer.spec.ts +644 -0
  85. package/src/state/reducer.ts +207 -0
  86. package/src/state/types.ts +54 -0
  87. package/tsconfig.json +7 -0
  88. package/tsconfig.lint.json +4 -0
  89. package/tsconfig.spec.json +7 -0
  90. package/vitest.config.ts +34 -0
@@ -0,0 +1,618 @@
1
+ import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2
+
3
+ import { parseSchema, loadSchema, reloadSchema, getSchema, getSchemaFolders, listDatabases, getPersistedDatabases, persistDatabases, KQL_FUNCTIONS, KQL_AGGREGATIONS, KQL_SCALAR_FUNCTIONS } from './schema';
4
+ import type { KustoSchemaDb } from './schema';
5
+ import type { KustoClient } from './kusto';
6
+
7
+ const mockStore = new Map<string, unknown>();
8
+ vi.mock('./state-service', () => ({
9
+ stateService: {
10
+ get: (_store: string, key: string) => mockStore.get(key) ?? null,
11
+ set: (_store: string, key: string, value: unknown) => { mockStore.set(key, value); },
12
+ delete: (_store: string, key: string) => { mockStore.delete(key); },
13
+ },
14
+ }));
15
+
16
+ function createMockClient(mgmtResult?: { columns: { ColumnName: string; ColumnType?: string }[]; rows: Record<string, unknown>[] }): KustoClient {
17
+ return {
18
+ queryKusto: vi.fn(),
19
+ queryKustoMgmt: vi.fn().mockResolvedValue(mgmtResult ?? { columns: [], rows: [] }),
20
+ clearQueryCache: vi.fn(),
21
+ getQueryCacheSize: vi.fn().mockReturnValue(0),
22
+ };
23
+ }
24
+
25
+ beforeEach(() => {
26
+ mockStore.clear();
27
+ vi.spyOn(console, 'error').mockImplementation(() => {});
28
+ vi.spyOn(console, 'warn').mockImplementation(() => {});
29
+ });
30
+
31
+ afterEach(() => {
32
+ vi.restoreAllMocks();
33
+ });
34
+
35
+ describe('parseSchema', () => {
36
+ it('parses tables with folder, description, and columns', () => {
37
+ const db: KustoSchemaDb = {
38
+ Tables: {
39
+ MyTable: {
40
+ Folder: 'Analytics',
41
+ DocString: 'A test table',
42
+ OrderedColumns: [
43
+ { Name: 'Id', CslType: 'long' },
44
+ { Name: 'Name', CslType: 'string' },
45
+ ],
46
+ },
47
+ },
48
+ };
49
+
50
+ const result = parseSchema(db);
51
+ expect(result).toHaveLength(1);
52
+ expect(result[0]).toEqual({
53
+ name: 'MyTable',
54
+ folder: 'Analytics',
55
+ description: 'A test table',
56
+ columns: [
57
+ { name: 'Id', type: 'long' },
58
+ { name: 'Name', type: 'string' },
59
+ ],
60
+ kind: 'table',
61
+ });
62
+ });
63
+
64
+ it('parses materialized views', () => {
65
+ const db: KustoSchemaDb = {
66
+ Tables: {},
67
+ MaterializedViews: {
68
+ MyView: {
69
+ Folder: 'Views',
70
+ DocString: 'A materialized view',
71
+ OrderedColumns: [
72
+ { Name: 'Ts', CslType: 'datetime' },
73
+ ],
74
+ },
75
+ },
76
+ };
77
+
78
+ const result = parseSchema(db);
79
+ expect(result).toHaveLength(1);
80
+ expect(result[0]).toEqual({
81
+ name: 'MyView',
82
+ folder: 'Views',
83
+ description: 'A materialized view',
84
+ columns: [{ name: 'Ts', type: 'datetime' }],
85
+ kind: 'materializedView',
86
+ });
87
+ });
88
+
89
+ it('parses functions without columns', () => {
90
+ const db: KustoSchemaDb = {
91
+ Tables: {},
92
+ Functions: {
93
+ MyFunc: {
94
+ Folder: 'Helpers',
95
+ DocString: 'A function',
96
+ },
97
+ },
98
+ };
99
+
100
+ const result = parseSchema(db);
101
+ expect(result).toHaveLength(1);
102
+ expect(result[0]).toEqual({
103
+ name: 'MyFunc',
104
+ folder: 'Helpers',
105
+ description: 'A function',
106
+ columns: [],
107
+ kind: 'function',
108
+ });
109
+ });
110
+
111
+ it('defaults folder to empty string for tables without Folder', () => {
112
+ const db: KustoSchemaDb = {
113
+ Tables: {
114
+ NoFolder: {
115
+ DocString: 'No folder',
116
+ OrderedColumns: [],
117
+ },
118
+ },
119
+ };
120
+
121
+ const result = parseSchema(db);
122
+ expect(result[0].folder).toBe('');
123
+ });
124
+
125
+ it('defaults folder to empty string for materialized views without Folder', () => {
126
+ const db: KustoSchemaDb = {
127
+ Tables: {},
128
+ MaterializedViews: {
129
+ NoFolder: {},
130
+ },
131
+ };
132
+
133
+ const result = parseSchema(db);
134
+ expect(result[0].folder).toBe('');
135
+ });
136
+
137
+ it('defaults folder to empty string for functions without Folder', () => {
138
+ const db: KustoSchemaDb = {
139
+ Tables: {},
140
+ Functions: {
141
+ NoFolder: {},
142
+ },
143
+ };
144
+
145
+ const result = parseSchema(db);
146
+ expect(result[0].folder).toBe('');
147
+ });
148
+
149
+ it('defaults description to empty string for tables', () => {
150
+ const db: KustoSchemaDb = {
151
+ Tables: {
152
+ NoDoc: {},
153
+ },
154
+ };
155
+
156
+ const result = parseSchema(db);
157
+ expect(result[0].description).toBe('');
158
+ });
159
+
160
+ it('defaults description to "Materialized view" for MVs without DocString', () => {
161
+ const db: KustoSchemaDb = {
162
+ Tables: {},
163
+ MaterializedViews: {
164
+ NoDoc: {},
165
+ },
166
+ };
167
+
168
+ const result = parseSchema(db);
169
+ expect(result[0].description).toBe('Materialized view');
170
+ });
171
+
172
+ it('handles missing OrderedColumns', () => {
173
+ const db: KustoSchemaDb = {
174
+ Tables: {
175
+ NoCols: { Folder: 'Test' },
176
+ },
177
+ };
178
+
179
+ const result = parseSchema(db);
180
+ expect(result[0].columns).toEqual([]);
181
+ });
182
+
183
+ it('sorts by folder first, then kind order, then name', () => {
184
+ const db: KustoSchemaDb = {
185
+ Tables: {
186
+ BTable: { Folder: 'Alpha' },
187
+ ATable: { Folder: 'Alpha' },
188
+ ZTable: { Folder: 'Beta' },
189
+ },
190
+ MaterializedViews: {
191
+ AlphaMV: { Folder: 'Alpha' },
192
+ },
193
+ Functions: {
194
+ AlphaFunc: { Folder: 'Alpha' },
195
+ },
196
+ };
197
+
198
+ const result = parseSchema(db);
199
+ const names = result.map((t) => t.name);
200
+
201
+ // Alpha folder items should come first, sorted by kind: function → MV → table
202
+ expect(names.indexOf('AlphaFunc')).toBeLessThan(names.indexOf('AlphaMV'));
203
+ expect(names.indexOf('AlphaMV')).toBeLessThan(names.indexOf('ATable'));
204
+ expect(names.indexOf('ATable')).toBeLessThan(names.indexOf('BTable'));
205
+
206
+ // Beta folder should come after Alpha
207
+ expect(names.indexOf('BTable')).toBeLessThan(names.indexOf('ZTable'));
208
+ });
209
+
210
+ it('sorts lookup tables between MVs and regular tables', () => {
211
+ const db: KustoSchemaDb = {
212
+ Tables: {
213
+ RegularTable: { Folder: 'Same' },
214
+ LookupTable: { Folder: 'Lookup' },
215
+ },
216
+ MaterializedViews: {
217
+ SameMV: { Folder: 'Lookup' },
218
+ },
219
+ };
220
+
221
+ const result = parseSchema(db);
222
+ const lookupItems = result.filter((t) => t.folder === 'Lookup');
223
+ const lookupNames = lookupItems.map((t) => t.name);
224
+
225
+ // Within the Lookup folder: MV → lookup table (kind order 1.5)
226
+ expect(lookupNames.indexOf('SameMV')).toBeLessThan(lookupNames.indexOf('LookupTable'));
227
+ });
228
+
229
+ it('handles empty database', () => {
230
+ const db: KustoSchemaDb = {
231
+ Tables: {},
232
+ };
233
+
234
+ const result = parseSchema(db);
235
+ expect(result).toEqual([]);
236
+ });
237
+
238
+ it('handles database with all sections empty', () => {
239
+ const db: KustoSchemaDb = {
240
+ Tables: {},
241
+ MaterializedViews: {},
242
+ Functions: {},
243
+ };
244
+
245
+ const result = parseSchema(db);
246
+ expect(result).toEqual([]);
247
+ });
248
+
249
+ it('alphabetizes items of the same kind in the same folder', () => {
250
+ const db: KustoSchemaDb = {
251
+ Tables: {
252
+ Zebra: { Folder: 'Zoo' },
253
+ Apple: { Folder: 'Zoo' },
254
+ Mango: { Folder: 'Zoo' },
255
+ },
256
+ };
257
+
258
+ const result = parseSchema(db);
259
+ const names = result.map((t) => t.name);
260
+ expect(names).toEqual(['Apple', 'Mango', 'Zebra']);
261
+ });
262
+ });
263
+
264
+ describe('loadSchema', () => {
265
+ it('loads schema from client.queryKustoMgmt and caches it', async () => {
266
+ const mockDb: KustoSchemaDb = {
267
+ Tables: {
268
+ TestTable: {
269
+ Folder: 'Test',
270
+ DocString: 'A test',
271
+ OrderedColumns: [{ Name: 'Col1', CslType: 'string' }],
272
+ },
273
+ },
274
+ };
275
+ const client = createMockClient({
276
+ rows: [{ DatabaseSchema: JSON.stringify({ Databases: { mydb: mockDb } }) }],
277
+ columns: [],
278
+ });
279
+
280
+ await loadSchema(client);
281
+
282
+ const schema = getSchema();
283
+ expect(schema.length).toBeGreaterThan(0);
284
+ expect(schema.find((t) => t.name === 'TestTable')).toBeDefined();
285
+ });
286
+
287
+ it('does not re-fetch if already cached', async () => {
288
+ const mockDb: KustoSchemaDb = {
289
+ Tables: {
290
+ CachedTable: { Folder: 'Cache', OrderedColumns: [] },
291
+ },
292
+ };
293
+ const client = createMockClient({
294
+ rows: [{ DatabaseSchema: JSON.stringify({ Databases: { db: mockDb } }) }],
295
+ columns: [],
296
+ });
297
+
298
+ // Force a fresh load first
299
+ await reloadSchema(client);
300
+ vi.mocked(client.queryKustoMgmt).mockClear();
301
+
302
+ // Second load should be a no-op (cached)
303
+ await loadSchema(client);
304
+
305
+ expect(client.queryKustoMgmt).toHaveBeenCalledTimes(0);
306
+ });
307
+
308
+ it('handles empty result rows', async () => {
309
+ const client = createMockClient({ rows: [], columns: [] });
310
+
311
+ // Reset default schema cache first
312
+ await reloadSchema(client);
313
+
314
+ const schema = getSchema();
315
+ expect(schema).toEqual([]);
316
+ });
317
+ });
318
+
319
+ describe('reloadSchema', () => {
320
+ it('clears cache and reloads', async () => {
321
+ const mockDb: KustoSchemaDb = {
322
+ Tables: {
323
+ ReloadedTable: { Folder: 'Reload', OrderedColumns: [] },
324
+ },
325
+ };
326
+ const client = createMockClient({
327
+ rows: [{ DatabaseSchema: JSON.stringify({ Databases: { db: mockDb } }) }],
328
+ columns: [],
329
+ });
330
+
331
+ await reloadSchema(client);
332
+
333
+ const schema = getSchema();
334
+ expect(schema.find((t) => t.name === 'ReloadedTable')).toBeDefined();
335
+ });
336
+ });
337
+
338
+ describe('getSchema', () => {
339
+ it('returns empty when no schema is loaded for default target', () => {
340
+ // After previous tests may have loaded something, check non-default target
341
+ const schema = getSchema({ clusterUrl: 'https://unknown.kusto.windows.net', database: 'UnknownDB' });
342
+ expect(schema).toEqual([]);
343
+ });
344
+ });
345
+
346
+ describe('getSchemaFolders', () => {
347
+ it('returns folders from current schema', async () => {
348
+ const mockDb: KustoSchemaDb = {
349
+ Tables: {
350
+ T1: { Folder: 'FolderA', OrderedColumns: [] },
351
+ T2: { Folder: 'FolderB', OrderedColumns: [] },
352
+ },
353
+ };
354
+ const client = createMockClient({
355
+ rows: [{ DatabaseSchema: JSON.stringify({ Databases: { db: mockDb } }) }],
356
+ columns: [],
357
+ });
358
+ await reloadSchema(client);
359
+
360
+ const folders = getSchemaFolders();
361
+ expect(Array.isArray(folders)).toBe(true);
362
+ expect(folders).toContain('FolderA');
363
+ expect(folders).toContain('FolderB');
364
+ });
365
+ });
366
+
367
+ describe('KQL constants', () => {
368
+ it('KQL_FUNCTIONS contains expected keywords', () => {
369
+ expect(KQL_FUNCTIONS).toContain('where');
370
+ expect(KQL_FUNCTIONS).toContain('project');
371
+ expect(KQL_FUNCTIONS).toContain('summarize');
372
+ expect(KQL_FUNCTIONS).toContain('join');
373
+ expect(KQL_FUNCTIONS).toContain('union');
374
+ });
375
+
376
+ it('KQL_AGGREGATIONS contains expected functions', () => {
377
+ expect(KQL_AGGREGATIONS).toContain('count');
378
+ expect(KQL_AGGREGATIONS).toContain('sum');
379
+ expect(KQL_AGGREGATIONS).toContain('avg');
380
+ expect(KQL_AGGREGATIONS).toContain('dcount');
381
+ expect(KQL_AGGREGATIONS).toContain('percentile');
382
+ });
383
+
384
+ it('KQL_SCALAR_FUNCTIONS contains expected functions', () => {
385
+ expect(KQL_SCALAR_FUNCTIONS).toContain('ago');
386
+ expect(KQL_SCALAR_FUNCTIONS).toContain('now');
387
+ expect(KQL_SCALAR_FUNCTIONS).toContain('strlen');
388
+ expect(KQL_SCALAR_FUNCTIONS).toContain('iif');
389
+ expect(KQL_SCALAR_FUNCTIONS).toContain('parse_json');
390
+ });
391
+ });
392
+
393
+ describe('per-cluster schema', () => {
394
+ it('getSchema returns empty for unknown target', () => {
395
+ const schema = getSchema({ clusterUrl: 'https://unknown.kusto.windows.net', database: 'UnknownDB' });
396
+ expect(schema).toEqual([]);
397
+ });
398
+
399
+ it('getSchemaFolders returns empty for unknown target', () => {
400
+ const folders = getSchemaFolders({ clusterUrl: 'https://unknown.kusto.windows.net', database: 'UnknownDB' });
401
+ expect(folders).toEqual([]);
402
+ });
403
+
404
+ it('loadSchema passes target to client.queryKustoMgmt', async () => {
405
+ const target = { clusterUrl: 'https://test.kusto.windows.net', database: 'TestDB' };
406
+ const client = createMockClient({ rows: [], columns: [] });
407
+ await loadSchema(client, target);
408
+ expect(client.queryKustoMgmt).toHaveBeenCalledWith('.show database schema as json', target);
409
+ });
410
+
411
+ it('reloadSchema clears cache and reloads for target', async () => {
412
+ const target = { clusterUrl: 'https://test2.kusto.windows.net', database: 'TestDB2' };
413
+ const client = createMockClient({ rows: [], columns: [] });
414
+ await reloadSchema(client, target);
415
+ expect(client.queryKustoMgmt).toHaveBeenCalledWith('.show database schema as json', target);
416
+ });
417
+ });
418
+
419
+ describe('listDatabases', () => {
420
+ it('returns database names from .show databases query', async () => {
421
+ const client = createMockClient({
422
+ rows: [
423
+ { DatabaseName: 'Telemetry' },
424
+ { DatabaseName: 'Analytics' },
425
+ { DatabaseName: 'Logs' },
426
+ ],
427
+ columns: [],
428
+ });
429
+
430
+ const result = await listDatabases(client, 'https://mycluster.kusto.windows.net');
431
+ expect(result).toEqual(['Analytics', 'Logs', 'Telemetry']);
432
+ expect(client.queryKustoMgmt).toHaveBeenCalledWith(
433
+ '.show databases | project DatabaseName',
434
+ { clusterUrl: 'https://mycluster.kusto.windows.net', database: 'NetDefaultDB' },
435
+ );
436
+ });
437
+
438
+ it('returns empty array on error', async () => {
439
+ const client = createMockClient();
440
+ vi.mocked(client.queryKustoMgmt).mockRejectedValue(new Error('network error'));
441
+
442
+ const result = await listDatabases(client, 'https://badcluster.kusto.windows.net');
443
+ expect(result).toEqual([]);
444
+ });
445
+
446
+ it('filters out empty database names', async () => {
447
+ const client = createMockClient({
448
+ rows: [
449
+ { DatabaseName: 'ValidDB' },
450
+ { DatabaseName: '' },
451
+ ],
452
+ columns: [],
453
+ });
454
+
455
+ const result = await listDatabases(client, 'https://mycluster.kusto.windows.net');
456
+ expect(result).toEqual(['ValidDB']);
457
+ });
458
+ });
459
+
460
+ describe('loadSchema auto-discovery', () => {
461
+ it('auto-discovers database when target has empty database', async () => {
462
+ const mockDb: KustoSchemaDb = {
463
+ Tables: { DiscoveredTable: { Folder: 'Test', OrderedColumns: [] } },
464
+ };
465
+ const client = createMockClient();
466
+ vi.mocked(client.queryKustoMgmt)
467
+ .mockResolvedValueOnce({
468
+ rows: [{ DatabaseName: 'AutoDB' }],
469
+ columns: [],
470
+ resultSets: [],
471
+ })
472
+ .mockResolvedValueOnce({
473
+ rows: [{ DatabaseSchema: JSON.stringify({ Databases: { AutoDB: mockDb } }) }],
474
+ columns: [],
475
+ resultSets: [],
476
+ });
477
+
478
+ const target = { clusterUrl: 'https://auto.kusto.windows.net', database: '' };
479
+ await loadSchema(client, target);
480
+
481
+ expect(vi.mocked(client.queryKustoMgmt).mock.calls[0][0]).toBe('.show databases | project DatabaseName');
482
+ expect(vi.mocked(client.queryKustoMgmt).mock.calls[1][0]).toBe('.show database schema as json');
483
+
484
+ const schema = getSchema({ clusterUrl: 'https://auto.kusto.windows.net', database: 'AutoDB' });
485
+ expect(schema.find((t) => t.name === 'DiscoveredTable')).toBeDefined();
486
+ });
487
+
488
+ it('caches schema under both discovered and original keys', async () => {
489
+ const mockDb: KustoSchemaDb = {
490
+ Tables: { DualCacheTable: { Folder: 'Dual', OrderedColumns: [] } },
491
+ };
492
+ const client = createMockClient();
493
+ vi.mocked(client.queryKustoMgmt)
494
+ .mockResolvedValueOnce({
495
+ rows: [{ DatabaseName: 'FoundDB' }],
496
+ columns: [],
497
+ resultSets: [],
498
+ })
499
+ .mockResolvedValueOnce({
500
+ rows: [{ DatabaseSchema: JSON.stringify({ Databases: { FoundDB: mockDb } }) }],
501
+ columns: [],
502
+ resultSets: [],
503
+ });
504
+
505
+ const target = { clusterUrl: 'https://dual.kusto.windows.net', database: '' };
506
+ await loadSchema(client, target);
507
+
508
+ const schemaOriginal = getSchema(target);
509
+ const schemaDiscovered = getSchema({ clusterUrl: 'https://dual.kusto.windows.net', database: 'FoundDB' });
510
+ expect(schemaOriginal.find((t) => t.name === 'DualCacheTable')).toBeDefined();
511
+ expect(schemaDiscovered.find((t) => t.name === 'DualCacheTable')).toBeDefined();
512
+ });
513
+
514
+ it('returns early if auto-discovery finds no databases', async () => {
515
+ const client = createMockClient({ rows: [], columns: [] });
516
+
517
+ const target = { clusterUrl: 'https://empty.kusto.windows.net', database: '' };
518
+ await loadSchema(client, target);
519
+
520
+ expect(client.queryKustoMgmt).toHaveBeenCalledTimes(1);
521
+ });
522
+ });
523
+
524
+ describe('getSchema for non-default targets', () => {
525
+ it('returns empty array for non-default target without cached data', () => {
526
+ const schema = getSchema({ clusterUrl: 'https://no-cache.kusto.windows.net', database: 'NoDB' });
527
+ expect(schema).toEqual([]);
528
+ });
529
+
530
+ it('getSchemaFolders returns empty for non-default target without cached data', () => {
531
+ const folders = getSchemaFolders({ clusterUrl: 'https://no-cache.kusto.windows.net', database: 'NoDB' });
532
+ expect(folders).toEqual([]);
533
+ });
534
+ });
535
+
536
+ // ---------------------------------------------------------------------------
537
+ // getPersistedDatabases / persistDatabases
538
+ // ---------------------------------------------------------------------------
539
+
540
+ describe('getPersistedDatabases', () => {
541
+ beforeEach(() => {
542
+ mockStore.clear();
543
+ });
544
+
545
+ it('returns null when no databases are cached', () => {
546
+ expect(getPersistedDatabases('https://uncached.kusto.windows.net')).toBeNull();
547
+ });
548
+
549
+ it('returns cached databases after persistDatabases', () => {
550
+ const clusterUrl = 'https://persist-test.kusto.windows.net';
551
+ persistDatabases(clusterUrl, ['DB1', 'DB2']);
552
+ const result = getPersistedDatabases(clusterUrl);
553
+ expect(result).toEqual(['DB1', 'DB2']);
554
+ });
555
+ });
556
+
557
+ describe('persistDatabases', () => {
558
+ beforeEach(() => {
559
+ mockStore.clear();
560
+ });
561
+
562
+ it('stores databases and retrieves them', () => {
563
+ const clusterUrl = 'https://store-test.kusto.windows.net';
564
+ persistDatabases(clusterUrl, ['Alpha', 'Beta']);
565
+ expect(getPersistedDatabases(clusterUrl)).toEqual(['Alpha', 'Beta']);
566
+ });
567
+
568
+ it('overwrites previously persisted databases', () => {
569
+ const clusterUrl = 'https://overwrite.kusto.windows.net';
570
+ persistDatabases(clusterUrl, ['Old']);
571
+ persistDatabases(clusterUrl, ['New']);
572
+ expect(getPersistedDatabases(clusterUrl)).toEqual(['New']);
573
+ });
574
+ });
575
+
576
+ // ---------------------------------------------------------------------------
577
+ // listDatabases with forceRefresh
578
+ // ---------------------------------------------------------------------------
579
+
580
+ describe('listDatabases with forceRefresh', () => {
581
+ beforeEach(() => {
582
+ mockStore.clear();
583
+ });
584
+
585
+ it('uses cached databases when forceRefresh is false', async () => {
586
+ const clusterUrl = 'https://cached-dbs.kusto.windows.net';
587
+ persistDatabases(clusterUrl, ['CachedDB']);
588
+ const client = createMockClient();
589
+
590
+ const result = await listDatabases(client, clusterUrl);
591
+ expect(result).toEqual(['CachedDB']);
592
+ expect(client.queryKustoMgmt).not.toHaveBeenCalled();
593
+ });
594
+
595
+ it('bypasses cache when forceRefresh is true', async () => {
596
+ const clusterUrl = 'https://force-refresh.kusto.windows.net';
597
+ persistDatabases(clusterUrl, ['OldDB']);
598
+ const client = createMockClient({
599
+ rows: [{ DatabaseName: 'FreshDB' }],
600
+ columns: [],
601
+ });
602
+
603
+ const result = await listDatabases(client, clusterUrl, true);
604
+ expect(result).toEqual(['FreshDB']);
605
+ expect(client.queryKustoMgmt).toHaveBeenCalled();
606
+ });
607
+
608
+ it('persists newly fetched databases', async () => {
609
+ const clusterUrl = 'https://persist-after-fetch.kusto.windows.net';
610
+ const client = createMockClient({
611
+ rows: [{ DatabaseName: 'NewDB' }],
612
+ columns: [],
613
+ });
614
+
615
+ await listDatabases(client, clusterUrl, true);
616
+ expect(getPersistedDatabases(clusterUrl)).toEqual(['NewDB']);
617
+ });
618
+ });