@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.
- package/LICENSE +21 -0
- package/package.json +46 -0
- package/src/bootstrap.spec.tsx +290 -0
- package/src/bootstrap.tsx +195 -0
- package/src/colors.ts +23 -0
- package/src/components/ChartPanel/ChartPanel.logic.spec.ts +358 -0
- package/src/components/ChartPanel/ChartPanel.logic.ts +318 -0
- package/src/components/ChartPanel/ChartPanel.spec.tsx +331 -0
- package/src/components/ChartPanel/ChartPanel.styles.ts +14 -0
- package/src/components/ChartPanel/ChartPanel.tsx +151 -0
- package/src/components/ConnectionDialog/ConnectionDialog.spec.tsx +767 -0
- package/src/components/ConnectionDialog/ConnectionDialog.tsx +652 -0
- package/src/components/ConnectionDialog/ConnectionRow.spec.tsx +104 -0
- package/src/components/ConnectionDialog/ConnectionRow.tsx +177 -0
- package/src/components/EditorToolbar/EditorToolbar.spec.tsx +599 -0
- package/src/components/EditorToolbar/EditorToolbar.tsx +259 -0
- package/src/components/Explorer/Explorer.spec.tsx +615 -0
- package/src/components/Explorer/Explorer.tsx +49 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.logic.spec.ts +715 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.logic.ts +324 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.spec.tsx +450 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.styles.ts +478 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.tsx +648 -0
- package/src/components/ExplorerWorkspace/kqlLanguage.spec.ts +321 -0
- package/src/components/ExplorerWorkspace/kqlLanguage.ts +155 -0
- package/src/components/ResultsPanel/ElapsedTimer.spec.tsx +51 -0
- package/src/components/ResultsPanel/ElapsedTimer.tsx +15 -0
- package/src/components/ResultsPanel/HistoryItem.spec.tsx +123 -0
- package/src/components/ResultsPanel/HistoryItem.tsx +56 -0
- package/src/components/ResultsPanel/ResultSetTab.tsx +18 -0
- package/src/components/ResultsPanel/ResultsPanel.spec.tsx +1194 -0
- package/src/components/ResultsPanel/ResultsPanel.tsx +321 -0
- package/src/components/ResultsPanel/TabButton.tsx +11 -0
- package/src/components/ResultsTable/ColumnHeader.tsx +42 -0
- package/src/components/ResultsTable/ColumnPickerItem.tsx +16 -0
- package/src/components/ResultsTable/FilterCell.tsx +24 -0
- package/src/components/ResultsTable/ResultsTable.logic.spec.ts +655 -0
- package/src/components/ResultsTable/ResultsTable.logic.ts +264 -0
- package/src/components/ResultsTable/ResultsTable.spec.tsx +930 -0
- package/src/components/ResultsTable/ResultsTable.styles.ts +108 -0
- package/src/components/ResultsTable/ResultsTable.tsx +501 -0
- package/src/components/ResultsTable/VirtualCell.spec.tsx +55 -0
- package/src/components/ResultsTable/VirtualCell.tsx +32 -0
- package/src/components/ResultsTable/VirtualRow.tsx +55 -0
- package/src/components/SchemaSidebar/FolderRow.tsx +35 -0
- package/src/components/SchemaSidebar/SchemaColumnItem.tsx +41 -0
- package/src/components/SchemaSidebar/SchemaContextMenu.spec.tsx +148 -0
- package/src/components/SchemaSidebar/SchemaContextMenu.tsx +49 -0
- package/src/components/SchemaSidebar/SchemaSidebar.logic.spec.ts +230 -0
- package/src/components/SchemaSidebar/SchemaSidebar.logic.ts +74 -0
- package/src/components/SchemaSidebar/SchemaSidebar.spec.tsx +1111 -0
- package/src/components/SchemaSidebar/SchemaSidebar.tsx +244 -0
- package/src/components/SchemaSidebar/SchemaTableItem.tsx +77 -0
- package/src/components/TabBar/TabBar.logic.spec.ts +127 -0
- package/src/components/TabBar/TabBar.logic.ts +35 -0
- package/src/components/TabBar/TabBar.spec.tsx +929 -0
- package/src/components/TabBar/TabBar.tsx +284 -0
- package/src/components/TabBar/TabItem.tsx +166 -0
- package/src/components/VegaChart/VegaChart.spec.tsx +126 -0
- package/src/components/VegaChart/VegaChart.tsx +224 -0
- package/src/config.spec.ts +44 -0
- package/src/config.ts +47 -0
- package/src/context/ExplorerColorContext.ts +14 -0
- package/src/context/ExplorerStateContext.ts +49 -0
- package/src/context/KustoClientContext.ts +12 -0
- package/src/context/context.spec.tsx +183 -0
- package/src/index.ts +14 -0
- package/src/services/cache.spec.ts +31 -0
- package/src/services/cache.ts +16 -0
- package/src/services/concurrency.spec.ts +139 -0
- package/src/services/concurrency.ts +46 -0
- package/src/services/kusto.spec.ts +919 -0
- package/src/services/kusto.ts +472 -0
- package/src/services/queryHistory.spec.ts +287 -0
- package/src/services/queryHistory.ts +146 -0
- package/src/services/schema.spec.ts +618 -0
- package/src/services/schema.ts +294 -0
- package/src/services/state-service.spec.ts +670 -0
- package/src/services/state-service.ts +602 -0
- package/src/services/useKustoQuery.spec.ts +234 -0
- package/src/services/useKustoQuery.ts +124 -0
- package/src/state/actions.ts +50 -0
- package/src/state/index.ts +3 -0
- package/src/state/reducer.spec.ts +644 -0
- package/src/state/reducer.ts +207 -0
- package/src/state/types.ts +54 -0
- package/tsconfig.json +7 -0
- package/tsconfig.lint.json +4 -0
- package/tsconfig.spec.json +7 -0
- package/vitest.config.ts +34 -0
|
@@ -0,0 +1,715 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
DEFAULT_QUERY,
|
|
5
|
+
KEYBOARD_SHORTCUTS,
|
|
6
|
+
createTab,
|
|
7
|
+
formatBytes,
|
|
8
|
+
formatKql,
|
|
9
|
+
getQueryToRun,
|
|
10
|
+
getQueryToRunFromEditor,
|
|
11
|
+
downloadFile,
|
|
12
|
+
exportCsv,
|
|
13
|
+
exportJson,
|
|
14
|
+
loadConnections,
|
|
15
|
+
saveConnections,
|
|
16
|
+
loadActiveConnectionId,
|
|
17
|
+
saveActiveConnectionId,
|
|
18
|
+
shortenClusterUrl,
|
|
19
|
+
DEFAULT_CONNECTION,
|
|
20
|
+
CONNECTION_COLORS,
|
|
21
|
+
parseResultColumns,
|
|
22
|
+
buildSuccessEntry,
|
|
23
|
+
buildErrorEntry,
|
|
24
|
+
nextQueryTitle,
|
|
25
|
+
getEditorDropTarget,
|
|
26
|
+
getShortcutAction,
|
|
27
|
+
} from './ExplorerWorkspace.logic';
|
|
28
|
+
|
|
29
|
+
const mockConnStore = new Map<string, unknown>();
|
|
30
|
+
vi.mock('../../services/state-service', () => ({
|
|
31
|
+
stateService: {
|
|
32
|
+
get: (_s: string, key: string) => mockConnStore.get(key) ?? null,
|
|
33
|
+
set: (_s: string, key: string, value: unknown) => { mockConnStore.set(key, value); },
|
|
34
|
+
delete: (_s: string, key: string) => { mockConnStore.delete(key); },
|
|
35
|
+
subscribe: () => () => {},
|
|
36
|
+
},
|
|
37
|
+
}));
|
|
38
|
+
|
|
39
|
+
// ---------------------------------------------------------------------------
|
|
40
|
+
// Constants
|
|
41
|
+
// ---------------------------------------------------------------------------
|
|
42
|
+
|
|
43
|
+
describe('DEFAULT_QUERY', () => {
|
|
44
|
+
it('is a non-empty string', () => {
|
|
45
|
+
expect(typeof DEFAULT_QUERY).toBe('string');
|
|
46
|
+
expect(DEFAULT_QUERY.length).toBeGreaterThan(0);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it('contains KQL-like content', () => {
|
|
50
|
+
expect(DEFAULT_QUERY).toContain('|');
|
|
51
|
+
});
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
describe('KEYBOARD_SHORTCUTS', () => {
|
|
55
|
+
it('is a non-empty array', () => {
|
|
56
|
+
expect(Array.isArray(KEYBOARD_SHORTCUTS)).toBe(true);
|
|
57
|
+
expect(KEYBOARD_SHORTCUTS.length).toBeGreaterThan(0);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it('each entry has keys and action', () => {
|
|
61
|
+
for (const shortcut of KEYBOARD_SHORTCUTS) {
|
|
62
|
+
expect(shortcut).toHaveProperty('keys');
|
|
63
|
+
expect(shortcut).toHaveProperty('action');
|
|
64
|
+
expect(typeof shortcut.keys).toBe('string');
|
|
65
|
+
expect(typeof shortcut.action).toBe('string');
|
|
66
|
+
}
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// ---------------------------------------------------------------------------
|
|
71
|
+
// createTab
|
|
72
|
+
// ---------------------------------------------------------------------------
|
|
73
|
+
|
|
74
|
+
describe('createTab', () => {
|
|
75
|
+
it('creates a tab with auto-generated id', () => {
|
|
76
|
+
const tab = createTab();
|
|
77
|
+
expect(tab.id).toMatch(/^tab-\d+$/);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('creates a tab with default title', () => {
|
|
81
|
+
const tab = createTab();
|
|
82
|
+
expect(tab.title).toMatch(/^Query \d+$/);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it('creates a tab with custom title', () => {
|
|
86
|
+
const tab = createTab('', 'My Tab');
|
|
87
|
+
expect(tab.title).toBe('My Tab');
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('creates a tab with provided kql', () => {
|
|
91
|
+
const tab = createTab('StormEvents | take 10');
|
|
92
|
+
expect(tab.kql).toBe('StormEvents | take 10');
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('defaults kql to empty string', () => {
|
|
96
|
+
const tab = createTab();
|
|
97
|
+
expect(tab.kql).toBe('');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('generates unique IDs for each tab', () => {
|
|
101
|
+
const tab1 = createTab();
|
|
102
|
+
const tab2 = createTab();
|
|
103
|
+
expect(tab1.id).not.toBe(tab2.id);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// ---------------------------------------------------------------------------
|
|
108
|
+
// nextQueryTitle
|
|
109
|
+
// ---------------------------------------------------------------------------
|
|
110
|
+
|
|
111
|
+
describe('nextQueryTitle', () => {
|
|
112
|
+
it('returns Query 1 when no tabs exist', () => {
|
|
113
|
+
expect(nextQueryTitle([])).toBe('Query 1');
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('returns next available number', () => {
|
|
117
|
+
const tabs = [createTab('', 'Query 1'), createTab('', 'Query 2')];
|
|
118
|
+
expect(nextQueryTitle(tabs)).toBe('Query 3');
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it('fills gaps in numbering', () => {
|
|
122
|
+
const tabs = [createTab('', 'Query 1'), createTab('', 'Query 3')];
|
|
123
|
+
expect(nextQueryTitle(tabs)).toBe('Query 2');
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('ignores non-default titles', () => {
|
|
127
|
+
const tabs = [createTab('', 'My Custom Tab'), createTab('', 'Query 3')];
|
|
128
|
+
expect(nextQueryTitle(tabs)).toBe('Query 1');
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// ---------------------------------------------------------------------------
|
|
133
|
+
// formatBytes
|
|
134
|
+
// ---------------------------------------------------------------------------
|
|
135
|
+
|
|
136
|
+
describe('formatBytes', () => {
|
|
137
|
+
it('returns "0 B" for zero bytes', () => {
|
|
138
|
+
expect(formatBytes(0)).toBe('0 B');
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('returns bytes for values less than 1024', () => {
|
|
142
|
+
expect(formatBytes(512)).toBe('512 B');
|
|
143
|
+
expect(formatBytes(1)).toBe('1 B');
|
|
144
|
+
expect(formatBytes(1023)).toBe('1023 B');
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it('returns KB for values in KB range', () => {
|
|
148
|
+
expect(formatBytes(1024)).toBe('1.0 KB');
|
|
149
|
+
expect(formatBytes(2048)).toBe('2.0 KB');
|
|
150
|
+
expect(formatBytes(1536)).toBe('1.5 KB');
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it('returns MB for values in MB range', () => {
|
|
154
|
+
expect(formatBytes(1024 * 1024)).toBe('1.0 MB');
|
|
155
|
+
expect(formatBytes(5 * 1024 * 1024)).toBe('5.0 MB');
|
|
156
|
+
expect(formatBytes(1.5 * 1024 * 1024)).toBe('1.5 MB');
|
|
157
|
+
});
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
// ---------------------------------------------------------------------------
|
|
161
|
+
// formatKql
|
|
162
|
+
// ---------------------------------------------------------------------------
|
|
163
|
+
|
|
164
|
+
describe('formatKql', () => {
|
|
165
|
+
it('puts pipes on newlines', () => {
|
|
166
|
+
const result = formatKql('Table | where x > 1 | take 10');
|
|
167
|
+
expect(result).toContain('\n| where x > 1');
|
|
168
|
+
expect(result).toContain('\n| take 10');
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
it('handles let statements', () => {
|
|
172
|
+
const result = formatKql('let x = 5;\n\nTable | take 10');
|
|
173
|
+
expect(result).toContain('let x = 5;');
|
|
174
|
+
expect(result).toContain('Table');
|
|
175
|
+
expect(result).toContain('| take 10');
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('preserves quotes around pipes', () => {
|
|
179
|
+
const result = formatKql("Table | where Name == 'a|b' | take 10");
|
|
180
|
+
expect(result).toContain("'a|b'");
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('handles empty input', () => {
|
|
184
|
+
expect(formatKql('')).toBe('');
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('handles single statement without pipes', () => {
|
|
188
|
+
expect(formatKql('Table')).toBe('Table');
|
|
189
|
+
});
|
|
190
|
+
|
|
191
|
+
it('handles whitespace-only input', () => {
|
|
192
|
+
expect(formatKql(' ')).toBe('');
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('handles double-quoted strings with pipes', () => {
|
|
196
|
+
const result = formatKql('Table | where Name == "a|b" | take 10');
|
|
197
|
+
expect(result).toContain('"a|b"');
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('handles multiple statements separated by blank lines', () => {
|
|
201
|
+
const result = formatKql('let x = 1;\n\nTable | take 10');
|
|
202
|
+
const parts = result.split('\n\n');
|
|
203
|
+
expect(parts.length).toBe(2);
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it('normalizes inner whitespace around pipes', () => {
|
|
207
|
+
const result = formatKql('Table\n | where x > 1\n | take 10');
|
|
208
|
+
expect(result).toContain('| where');
|
|
209
|
+
expect(result).toContain('| take');
|
|
210
|
+
// Each pipe segment starts on its own line
|
|
211
|
+
const lines = result.split('\n');
|
|
212
|
+
expect(lines[0]).toBe('Table');
|
|
213
|
+
expect(lines[1]).toMatch(/^\| where/);
|
|
214
|
+
expect(lines[2]).toMatch(/^\| take/);
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
// ---------------------------------------------------------------------------
|
|
219
|
+
// getQueryToRun
|
|
220
|
+
// ---------------------------------------------------------------------------
|
|
221
|
+
|
|
222
|
+
describe('getQueryToRun', () => {
|
|
223
|
+
it('returns selected text when selection is non-empty', () => {
|
|
224
|
+
const result = getQueryToRun(
|
|
225
|
+
() => ({ isEmpty: () => false, startLineNumber: 1, endLineNumber: 2 }),
|
|
226
|
+
() => 'SELECT * FROM table',
|
|
227
|
+
() => ({ lineNumber: 1 }),
|
|
228
|
+
() => 5,
|
|
229
|
+
() => '',
|
|
230
|
+
);
|
|
231
|
+
expect(result).not.toBeNull();
|
|
232
|
+
expect(result?.query).toBe('SELECT * FROM table');
|
|
233
|
+
expect(result?.range).toEqual({ startLine: 1, endLine: 2 });
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('returns null when selection is non-empty but text is whitespace-only', () => {
|
|
237
|
+
const result = getQueryToRun(
|
|
238
|
+
() => ({ isEmpty: () => false, startLineNumber: 1, endLineNumber: 1 }),
|
|
239
|
+
() => ' ',
|
|
240
|
+
() => ({ lineNumber: 1 }),
|
|
241
|
+
() => 1,
|
|
242
|
+
() => '',
|
|
243
|
+
);
|
|
244
|
+
expect(result).toBeNull();
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
it('finds current statement block around cursor', () => {
|
|
248
|
+
const lines = ['Table', '| where x > 1', '| take 10', '', 'Other'];
|
|
249
|
+
const result = getQueryToRun(
|
|
250
|
+
() => ({ isEmpty: () => true, startLineNumber: 1, endLineNumber: 1 }),
|
|
251
|
+
() => '',
|
|
252
|
+
() => ({ lineNumber: 2 }),
|
|
253
|
+
() => lines.length,
|
|
254
|
+
(line: number) => lines[line - 1],
|
|
255
|
+
);
|
|
256
|
+
expect(result).not.toBeNull();
|
|
257
|
+
expect(result?.query).toContain('Table');
|
|
258
|
+
expect(result?.query).toContain('| where x > 1');
|
|
259
|
+
expect(result?.query).toContain('| take 10');
|
|
260
|
+
expect(result?.range).toEqual({ startLine: 1, endLine: 3 });
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
it('returns null for empty content', () => {
|
|
264
|
+
const result = getQueryToRun(
|
|
265
|
+
() => ({ isEmpty: () => true, startLineNumber: 1, endLineNumber: 1 }),
|
|
266
|
+
() => '',
|
|
267
|
+
() => ({ lineNumber: 1 }),
|
|
268
|
+
() => 1,
|
|
269
|
+
() => '',
|
|
270
|
+
);
|
|
271
|
+
expect(result).toBeNull();
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
it('handles cursor at first line of multi-line block', () => {
|
|
275
|
+
const lines = ['Table', '| take 10'];
|
|
276
|
+
const result = getQueryToRun(
|
|
277
|
+
() => ({ isEmpty: () => true, startLineNumber: 1, endLineNumber: 1 }),
|
|
278
|
+
() => '',
|
|
279
|
+
() => ({ lineNumber: 1 }),
|
|
280
|
+
() => lines.length,
|
|
281
|
+
(line: number) => lines[line - 1],
|
|
282
|
+
);
|
|
283
|
+
expect(result?.range).toEqual({ startLine: 1, endLine: 2 });
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
it('handles cursor at last line of multi-line block', () => {
|
|
287
|
+
const lines = ['Table', '| take 10'];
|
|
288
|
+
const result = getQueryToRun(
|
|
289
|
+
() => ({ isEmpty: () => true, startLineNumber: 1, endLineNumber: 1 }),
|
|
290
|
+
() => '',
|
|
291
|
+
() => ({ lineNumber: 2 }),
|
|
292
|
+
() => lines.length,
|
|
293
|
+
(line: number) => lines[line - 1],
|
|
294
|
+
);
|
|
295
|
+
expect(result?.range).toEqual({ startLine: 1, endLine: 2 });
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it('handles null selection', () => {
|
|
299
|
+
const lines = ['Table'];
|
|
300
|
+
const result = getQueryToRun(
|
|
301
|
+
() => null,
|
|
302
|
+
() => '',
|
|
303
|
+
() => ({ lineNumber: 1 }),
|
|
304
|
+
() => 1,
|
|
305
|
+
(line: number) => lines[line - 1],
|
|
306
|
+
);
|
|
307
|
+
expect(result?.query).toBe('Table');
|
|
308
|
+
});
|
|
309
|
+
|
|
310
|
+
it('defaults to line 1 when getPosition returns null', () => {
|
|
311
|
+
const lines = ['Table', '| take 10', '', 'Other'];
|
|
312
|
+
const result = getQueryToRun(
|
|
313
|
+
() => ({ isEmpty: () => true, startLineNumber: 1, endLineNumber: 1 }),
|
|
314
|
+
() => '',
|
|
315
|
+
() => null,
|
|
316
|
+
() => lines.length,
|
|
317
|
+
(line: number) => lines[line - 1],
|
|
318
|
+
);
|
|
319
|
+
expect(result?.query).toContain('Table');
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
it('isolates blocks separated by blank lines', () => {
|
|
323
|
+
const lines = ['Block1', '', 'Block2', '| take 5'];
|
|
324
|
+
const result = getQueryToRun(
|
|
325
|
+
() => ({ isEmpty: () => true, startLineNumber: 1, endLineNumber: 1 }),
|
|
326
|
+
() => '',
|
|
327
|
+
() => ({ lineNumber: 3 }),
|
|
328
|
+
() => lines.length,
|
|
329
|
+
(line: number) => lines[line - 1],
|
|
330
|
+
);
|
|
331
|
+
expect(result?.query).toBe('Block2\n| take 5');
|
|
332
|
+
expect(result?.range).toEqual({ startLine: 3, endLine: 4 });
|
|
333
|
+
});
|
|
334
|
+
});
|
|
335
|
+
|
|
336
|
+
// ---------------------------------------------------------------------------
|
|
337
|
+
// getQueryToRunFromEditor
|
|
338
|
+
// ---------------------------------------------------------------------------
|
|
339
|
+
|
|
340
|
+
describe('getQueryToRunFromEditor', () => {
|
|
341
|
+
it('returns null when model is null', () => {
|
|
342
|
+
const ed = {
|
|
343
|
+
getSelection: () => null,
|
|
344
|
+
getModel: () => null,
|
|
345
|
+
getPosition: () => ({ lineNumber: 1 }),
|
|
346
|
+
};
|
|
347
|
+
expect(getQueryToRunFromEditor(ed)).toBeNull();
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
it('delegates to getQueryToRun with model accessors', () => {
|
|
351
|
+
const lines = ['Table', '| take 10'];
|
|
352
|
+
const ed = {
|
|
353
|
+
getSelection: () => ({ isEmpty: () => true, startLineNumber: 1, endLineNumber: 1 }),
|
|
354
|
+
getModel: () => ({
|
|
355
|
+
getValueInRange: () => '',
|
|
356
|
+
getLineCount: () => lines.length,
|
|
357
|
+
getLineContent: (line: number) => lines[line - 1],
|
|
358
|
+
}),
|
|
359
|
+
getPosition: () => ({ lineNumber: 1 }),
|
|
360
|
+
};
|
|
361
|
+
const result = getQueryToRunFromEditor(ed);
|
|
362
|
+
expect(result).not.toBeNull();
|
|
363
|
+
expect(result?.query).toContain('Table');
|
|
364
|
+
expect(result?.query).toContain('| take 10');
|
|
365
|
+
});
|
|
366
|
+
|
|
367
|
+
it('uses selection when non-empty', () => {
|
|
368
|
+
const ed = {
|
|
369
|
+
getSelection: () => ({ isEmpty: () => false, startLineNumber: 1, endLineNumber: 1 }),
|
|
370
|
+
getModel: () => ({
|
|
371
|
+
getValueInRange: () => 'selected text',
|
|
372
|
+
getLineCount: () => 1,
|
|
373
|
+
getLineContent: () => 'Table',
|
|
374
|
+
}),
|
|
375
|
+
getPosition: () => ({ lineNumber: 1 }),
|
|
376
|
+
};
|
|
377
|
+
const result = getQueryToRunFromEditor(ed);
|
|
378
|
+
expect(result?.query).toBe('selected text');
|
|
379
|
+
});
|
|
380
|
+
});
|
|
381
|
+
|
|
382
|
+
// ---------------------------------------------------------------------------
|
|
383
|
+
// downloadFile, exportCsv, exportJson
|
|
384
|
+
// ---------------------------------------------------------------------------
|
|
385
|
+
|
|
386
|
+
describe('downloadFile', () => {
|
|
387
|
+
let clickSpy: ReturnType<typeof vi.fn>;
|
|
388
|
+
let revokeObjectURLSpy: ReturnType<typeof vi.fn>;
|
|
389
|
+
let createObjectURLSpy: ReturnType<typeof vi.fn>;
|
|
390
|
+
|
|
391
|
+
beforeEach(() => {
|
|
392
|
+
clickSpy = vi.fn();
|
|
393
|
+
revokeObjectURLSpy = vi.fn();
|
|
394
|
+
createObjectURLSpy = vi.fn(() => 'blob:mock-url');
|
|
395
|
+
|
|
396
|
+
vi.stubGlobal('URL', {
|
|
397
|
+
createObjectURL: createObjectURLSpy,
|
|
398
|
+
revokeObjectURL: revokeObjectURLSpy,
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
vi.spyOn(document, 'createElement').mockReturnValue({
|
|
402
|
+
href: '',
|
|
403
|
+
download: '',
|
|
404
|
+
click: clickSpy,
|
|
405
|
+
} as unknown as HTMLAnchorElement);
|
|
406
|
+
});
|
|
407
|
+
|
|
408
|
+
it('creates a blob, triggers download, and revokes URL', () => {
|
|
409
|
+
downloadFile('content', 'file.txt', 'text/plain');
|
|
410
|
+
expect(createObjectURLSpy).toHaveBeenCalled();
|
|
411
|
+
expect(clickSpy).toHaveBeenCalled();
|
|
412
|
+
expect(revokeObjectURLSpy).toHaveBeenCalledWith('blob:mock-url');
|
|
413
|
+
});
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
describe('exportCsv', () => {
|
|
417
|
+
let clickSpy: ReturnType<typeof vi.fn>;
|
|
418
|
+
|
|
419
|
+
beforeEach(() => {
|
|
420
|
+
clickSpy = vi.fn();
|
|
421
|
+
vi.stubGlobal('URL', {
|
|
422
|
+
createObjectURL: vi.fn(() => 'blob:mock-url'),
|
|
423
|
+
revokeObjectURL: vi.fn(),
|
|
424
|
+
});
|
|
425
|
+
vi.spyOn(document, 'createElement').mockReturnValue({
|
|
426
|
+
href: '',
|
|
427
|
+
download: '',
|
|
428
|
+
click: clickSpy,
|
|
429
|
+
} as unknown as HTMLAnchorElement);
|
|
430
|
+
});
|
|
431
|
+
|
|
432
|
+
it('generates CSV and triggers download', () => {
|
|
433
|
+
const cols = ['Name', 'Age'];
|
|
434
|
+
const rows = [
|
|
435
|
+
{ Name: 'Alice', Age: 30 },
|
|
436
|
+
{ Name: 'Bob', Age: 25 },
|
|
437
|
+
];
|
|
438
|
+
exportCsv(cols, rows);
|
|
439
|
+
expect(clickSpy).toHaveBeenCalled();
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
it('escapes CSV values with commas and quotes', () => {
|
|
443
|
+
const cols = ['Name'];
|
|
444
|
+
const rows = [{ Name: 'O"Brien, Jr.' }];
|
|
445
|
+
exportCsv(cols, rows);
|
|
446
|
+
expect(clickSpy).toHaveBeenCalled();
|
|
447
|
+
});
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
describe('exportJson', () => {
|
|
451
|
+
let clickSpy: ReturnType<typeof vi.fn>;
|
|
452
|
+
|
|
453
|
+
beforeEach(() => {
|
|
454
|
+
clickSpy = vi.fn();
|
|
455
|
+
vi.stubGlobal('URL', {
|
|
456
|
+
createObjectURL: vi.fn(() => 'blob:mock-url'),
|
|
457
|
+
revokeObjectURL: vi.fn(),
|
|
458
|
+
});
|
|
459
|
+
vi.spyOn(document, 'createElement').mockReturnValue({
|
|
460
|
+
href: '',
|
|
461
|
+
download: '',
|
|
462
|
+
click: clickSpy,
|
|
463
|
+
} as unknown as HTMLAnchorElement);
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
it('generates JSON and triggers download', () => {
|
|
467
|
+
const rows = [{ Name: 'Alice' }];
|
|
468
|
+
exportJson(['Name'], rows);
|
|
469
|
+
expect(clickSpy).toHaveBeenCalled();
|
|
470
|
+
});
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
describe('loadConnections', () => {
|
|
474
|
+
const defaultClusters = [{ ...DEFAULT_CONNECTION }];
|
|
475
|
+
|
|
476
|
+
beforeEach(() => {
|
|
477
|
+
vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
478
|
+
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
479
|
+
mockConnStore.clear();
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
it('returns default connection when store is empty', () => {
|
|
483
|
+
const conns = loadConnections(defaultClusters);
|
|
484
|
+
expect(conns.length).toBeGreaterThanOrEqual(1);
|
|
485
|
+
expect(conns[0].id).toBe(DEFAULT_CONNECTION.id);
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
it('returns stored connections from stateService', () => {
|
|
489
|
+
const stored = [{ id: 'c1', name: 'C1', clusterUrl: 'https://c1.kusto.windows.net', database: 'DB1', color: '#aaa' }];
|
|
490
|
+
mockConnStore.set('list', stored);
|
|
491
|
+
const conns = loadConnections(defaultClusters);
|
|
492
|
+
expect(conns.length).toBeGreaterThanOrEqual(1);
|
|
493
|
+
expect(conns[0].id).toBe('c1');
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
it('returns default connection when store has empty array', () => {
|
|
497
|
+
mockConnStore.set('list', []);
|
|
498
|
+
const conns = loadConnections(defaultClusters);
|
|
499
|
+
expect(conns.length).toBeGreaterThanOrEqual(1);
|
|
500
|
+
expect(conns[0].id).toBe(DEFAULT_CONNECTION.id);
|
|
501
|
+
});
|
|
502
|
+
});
|
|
503
|
+
|
|
504
|
+
describe('saveConnections', () => {
|
|
505
|
+
beforeEach(() => {
|
|
506
|
+
vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
507
|
+
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
508
|
+
mockConnStore.clear();
|
|
509
|
+
});
|
|
510
|
+
|
|
511
|
+
it('saves connections to stateService', () => {
|
|
512
|
+
const conns = [{ id: 'c1', name: 'C1', clusterUrl: 'https://c1.kusto.windows.net', database: 'DB1', color: '#aaa' }];
|
|
513
|
+
saveConnections(conns);
|
|
514
|
+
expect(mockConnStore.get('list')).toEqual(conns);
|
|
515
|
+
});
|
|
516
|
+
});
|
|
517
|
+
|
|
518
|
+
describe('loadActiveConnectionId', () => {
|
|
519
|
+
const connections = [
|
|
520
|
+
{ id: 'c1', name: 'C1', clusterUrl: 'https://c1.kusto.windows.net', database: 'DB1', color: '#aaa' },
|
|
521
|
+
{ id: 'c2', name: 'C2', clusterUrl: 'https://c2.kusto.windows.net', database: 'DB2', color: '#bbb' },
|
|
522
|
+
];
|
|
523
|
+
|
|
524
|
+
beforeEach(() => {
|
|
525
|
+
mockConnStore.clear();
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
it('returns the stored active connection when it exists', () => {
|
|
529
|
+
mockConnStore.set('explorer-active-connection', 'c2');
|
|
530
|
+
expect(loadActiveConnectionId(connections)).toBe('c2');
|
|
531
|
+
});
|
|
532
|
+
|
|
533
|
+
it('falls back to the first connection when the stored connection is missing', () => {
|
|
534
|
+
mockConnStore.set('explorer-active-connection', 'missing');
|
|
535
|
+
expect(loadActiveConnectionId(connections)).toBe('c1');
|
|
536
|
+
});
|
|
537
|
+
});
|
|
538
|
+
|
|
539
|
+
describe('saveActiveConnectionId', () => {
|
|
540
|
+
beforeEach(() => {
|
|
541
|
+
mockConnStore.clear();
|
|
542
|
+
});
|
|
543
|
+
|
|
544
|
+
it('persists the active connection id', () => {
|
|
545
|
+
saveActiveConnectionId('c2');
|
|
546
|
+
expect(mockConnStore.get('explorer-active-connection')).toBe('c2');
|
|
547
|
+
});
|
|
548
|
+
});
|
|
549
|
+
|
|
550
|
+
describe('shortenClusterUrl', () => {
|
|
551
|
+
it('removes https:// prefix and .kusto.windows.net suffix', () => {
|
|
552
|
+
expect(shortenClusterUrl('https://mycluster.kusto.windows.net')).toBe('mycluster');
|
|
553
|
+
});
|
|
554
|
+
|
|
555
|
+
it('removes http:// prefix', () => {
|
|
556
|
+
expect(shortenClusterUrl('http://mycluster.kusto.windows.net')).toBe('mycluster');
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
it('handles URL without kusto suffix', () => {
|
|
560
|
+
expect(shortenClusterUrl('https://example.com')).toBe('example.com');
|
|
561
|
+
});
|
|
562
|
+
});
|
|
563
|
+
|
|
564
|
+
describe('CONNECTION_COLORS', () => {
|
|
565
|
+
it('exports an array of color strings', () => {
|
|
566
|
+
expect(Array.isArray(CONNECTION_COLORS)).toBe(true);
|
|
567
|
+
expect(CONNECTION_COLORS.length).toBeGreaterThan(0);
|
|
568
|
+
expect(CONNECTION_COLORS[0]).toMatch(/^#/);
|
|
569
|
+
});
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
describe('parseResultColumns', () => {
|
|
573
|
+
it('extracts column names and types', () => {
|
|
574
|
+
const cols = [
|
|
575
|
+
{ ColumnName: 'Id', ColumnType: 'int' },
|
|
576
|
+
{ ColumnName: 'Name', ColumnType: 'string' },
|
|
577
|
+
];
|
|
578
|
+
const result = parseResultColumns(cols);
|
|
579
|
+
expect(result.columns).toEqual(['Id', 'Name']);
|
|
580
|
+
expect(result.columnTypes).toEqual({ Id: 'int', Name: 'string' });
|
|
581
|
+
});
|
|
582
|
+
|
|
583
|
+
it('handles empty columns', () => {
|
|
584
|
+
const result = parseResultColumns([]);
|
|
585
|
+
expect(result.columns).toEqual([]);
|
|
586
|
+
expect(result.columnTypes).toEqual({});
|
|
587
|
+
});
|
|
588
|
+
});
|
|
589
|
+
|
|
590
|
+
describe('buildSuccessEntry', () => {
|
|
591
|
+
it('creates a success history entry', () => {
|
|
592
|
+
const entry = buildSuccessEntry('test query', 'key1', 42, ['A'], [{ A: 1 }]);
|
|
593
|
+
expect(entry.key).toBe('key1');
|
|
594
|
+
expect(entry.query).toBe('test query');
|
|
595
|
+
expect(entry.elapsed).toBe(42);
|
|
596
|
+
expect(entry.rowCount).toBe(1);
|
|
597
|
+
expect(entry.columnCount).toBe(1);
|
|
598
|
+
expect(entry.status).toBe('success');
|
|
599
|
+
expect(entry.columns).toEqual(['A']);
|
|
600
|
+
expect(entry.rows).toEqual([{ A: 1 }]);
|
|
601
|
+
});
|
|
602
|
+
});
|
|
603
|
+
|
|
604
|
+
describe('buildErrorEntry', () => {
|
|
605
|
+
it('creates an error history entry', () => {
|
|
606
|
+
const entry = buildErrorEntry('bad query', 'key2', 'Syntax error');
|
|
607
|
+
expect(entry.key).toBe('key2');
|
|
608
|
+
expect(entry.status).toBe('error');
|
|
609
|
+
expect(entry.error).toBe('Syntax error');
|
|
610
|
+
expect(entry.elapsed).toBeNull();
|
|
611
|
+
expect(entry.rowCount).toBeNull();
|
|
612
|
+
});
|
|
613
|
+
});
|
|
614
|
+
|
|
615
|
+
// ---------------------------------------------------------------------------
|
|
616
|
+
// getEditorDropTarget
|
|
617
|
+
// ---------------------------------------------------------------------------
|
|
618
|
+
|
|
619
|
+
describe('getEditorDropTarget', () => {
|
|
620
|
+
const rect = { left: 100, top: 100, width: 400, height: 300 } as DOMRect;
|
|
621
|
+
|
|
622
|
+
it('returns "left" when cursor is closest to the left edge', () => {
|
|
623
|
+
// relX=0.05, relY=0.5, distLeft=0.05 is smallest
|
|
624
|
+
expect(getEditorDropTarget(rect, 120, 250)).toBe('left');
|
|
625
|
+
});
|
|
626
|
+
|
|
627
|
+
it('returns "right" when cursor is closest to the right edge', () => {
|
|
628
|
+
// relX=0.95, relY=0.5, distRight=0.05 is smallest
|
|
629
|
+
expect(getEditorDropTarget(rect, 480, 250)).toBe('right');
|
|
630
|
+
});
|
|
631
|
+
|
|
632
|
+
it('returns "top" when cursor is closest to the top edge', () => {
|
|
633
|
+
// relX=0.5, relY=0.05, distTop=0.05 is smallest
|
|
634
|
+
expect(getEditorDropTarget(rect, 300, 115)).toBe('top');
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
it('returns "bottom" when cursor is closest to the bottom edge', () => {
|
|
638
|
+
// relX=0.5, relY=0.95, distBottom=0.05 is smallest
|
|
639
|
+
expect(getEditorDropTarget(rect, 300, 385)).toBe('bottom');
|
|
640
|
+
});
|
|
641
|
+
|
|
642
|
+
it('returns "left" for top-left corner (left wins tie-break)', () => {
|
|
643
|
+
// Near top-left: distLeft and distTop are both small but left is checked first
|
|
644
|
+
expect(getEditorDropTarget(rect, 105, 105)).toBe('left');
|
|
645
|
+
});
|
|
646
|
+
|
|
647
|
+
it('handles cursor at exact center', () => {
|
|
648
|
+
// All distances equal at 0.5 — left wins due to Math.min returning first minimum
|
|
649
|
+
const result = getEditorDropTarget(rect, 300, 250);
|
|
650
|
+
expect(['left', 'right', 'top', 'bottom']).toContain(result);
|
|
651
|
+
});
|
|
652
|
+
});
|
|
653
|
+
|
|
654
|
+
// ---------------------------------------------------------------------------
|
|
655
|
+
// getShortcutAction
|
|
656
|
+
// ---------------------------------------------------------------------------
|
|
657
|
+
|
|
658
|
+
describe('getShortcutAction', () => {
|
|
659
|
+
it('returns "add-tab" for Cmd+T', () => {
|
|
660
|
+
expect(getShortcutAction({ metaKey: true, ctrlKey: false, key: 't' }, 2, '')).toBe('add-tab');
|
|
661
|
+
});
|
|
662
|
+
|
|
663
|
+
it('returns "add-tab" for Ctrl+T', () => {
|
|
664
|
+
expect(getShortcutAction({ metaKey: false, ctrlKey: true, key: 't' }, 2, '')).toBe('add-tab');
|
|
665
|
+
});
|
|
666
|
+
|
|
667
|
+
it('returns "close-tab" for Cmd+W with empty kql and multiple tabs', () => {
|
|
668
|
+
expect(getShortcutAction({ metaKey: true, ctrlKey: false, key: 'w' }, 2, '')).toBe('close-tab');
|
|
669
|
+
});
|
|
670
|
+
|
|
671
|
+
it('returns "close-tab-confirm" for Cmd+W with content', () => {
|
|
672
|
+
expect(getShortcutAction({ metaKey: true, ctrlKey: false, key: 'w' }, 2, 'some kql')).toBe('close-tab-confirm');
|
|
673
|
+
});
|
|
674
|
+
|
|
675
|
+
it('returns "close-tab-confirm" for Cmd+W with whitespace-padded content', () => {
|
|
676
|
+
expect(getShortcutAction({ metaKey: true, ctrlKey: false, key: 'w' }, 2, ' query ')).toBe('close-tab-confirm');
|
|
677
|
+
});
|
|
678
|
+
|
|
679
|
+
it('returns null for Cmd+W with only 1 tab', () => {
|
|
680
|
+
expect(getShortcutAction({ metaKey: true, ctrlKey: false, key: 'w' }, 1, '')).toBeNull();
|
|
681
|
+
});
|
|
682
|
+
|
|
683
|
+
it('returns null for non-shortcut key', () => {
|
|
684
|
+
expect(getShortcutAction({ metaKey: true, ctrlKey: false, key: 'a' }, 2, '')).toBeNull();
|
|
685
|
+
});
|
|
686
|
+
|
|
687
|
+
it('returns null when neither metaKey nor ctrlKey is pressed', () => {
|
|
688
|
+
expect(getShortcutAction({ metaKey: false, ctrlKey: false, key: 't' }, 2, '')).toBeNull();
|
|
689
|
+
});
|
|
690
|
+
|
|
691
|
+
it('returns null for Cmd+W with whitespace-only kql and 1 tab', () => {
|
|
692
|
+
expect(getShortcutAction({ metaKey: true, ctrlKey: false, key: 'w' }, 1, ' ')).toBeNull();
|
|
693
|
+
});
|
|
694
|
+
});
|
|
695
|
+
|
|
696
|
+
// ---------------------------------------------------------------------------
|
|
697
|
+
// nextQueryTitle (additional edge cases)
|
|
698
|
+
// ---------------------------------------------------------------------------
|
|
699
|
+
|
|
700
|
+
describe('nextQueryTitle (edge cases)', () => {
|
|
701
|
+
it('returns Query 1 when all tabs have custom titles', () => {
|
|
702
|
+
const tabs = [createTab('', 'Custom A'), createTab('', 'Custom B')];
|
|
703
|
+
expect(nextQueryTitle(tabs)).toBe('Query 1');
|
|
704
|
+
});
|
|
705
|
+
|
|
706
|
+
it('handles large gap in numbering', () => {
|
|
707
|
+
const tabs = [createTab('', 'Query 100')];
|
|
708
|
+
expect(nextQueryTitle(tabs)).toBe('Query 1');
|
|
709
|
+
});
|
|
710
|
+
|
|
711
|
+
it('handles consecutive numbers starting from 2', () => {
|
|
712
|
+
const tabs = [createTab('', 'Query 2'), createTab('', 'Query 3')];
|
|
713
|
+
expect(nextQueryTitle(tabs)).toBe('Query 1');
|
|
714
|
+
});
|
|
715
|
+
});
|