@mhjuma/traverse 0.1.0 → 0.3.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 (29) hide show
  1. package/CHANGELOG.md +61 -0
  2. package/package.json +7 -4
  3. package/src/components/Explorer/Explorer.spec.tsx +122 -3
  4. package/src/components/Explorer/Explorer.tsx +82 -6
  5. package/src/components/ExplorerWorkspace/ExplorerWorkspace.logic.ts +26 -0
  6. package/src/components/ExplorerWorkspace/ExplorerWorkspace.spec.tsx +53 -17
  7. package/src/components/ExplorerWorkspace/ExplorerWorkspace.tsx +78 -74
  8. package/src/components/ExplorerWorkspace/bootstrapKustoLanguage.spec.ts +150 -0
  9. package/src/components/ExplorerWorkspace/bootstrapKustoLanguage.ts +95 -0
  10. package/src/components/ExplorerWorkspace/kustoSchemaQueue.spec.ts +322 -0
  11. package/src/components/ExplorerWorkspace/kustoSchemaQueue.ts +188 -0
  12. package/src/components/ExplorerWorkspace/monacoLoader.spec.ts +82 -0
  13. package/src/components/ExplorerWorkspace/monacoLoader.ts +57 -0
  14. package/src/components/ExplorerWorkspace/monacoWorkers.spec.ts +81 -0
  15. package/src/components/ExplorerWorkspace/monacoWorkers.ts +90 -0
  16. package/src/index.ts +4 -0
  17. package/src/services/schema.spec.ts +107 -28
  18. package/src/services/schema.ts +66 -25
  19. package/src/services/state-service.spec.ts +16 -0
  20. package/src/services/state-service.ts +12 -5
  21. package/src/state/persistence.spec.ts +239 -0
  22. package/src/state/persistence.ts +140 -0
  23. package/src/state/reducer.spec.ts +134 -3
  24. package/src/state/reducer.ts +39 -14
  25. package/src/state/types.ts +6 -0
  26. package/src/test-setup.ts +3 -0
  27. package/vitest.config.ts +7 -0
  28. package/src/components/ExplorerWorkspace/kqlLanguage.spec.ts +0 -321
  29. package/src/components/ExplorerWorkspace/kqlLanguage.ts +0 -155
@@ -0,0 +1,239 @@
1
+ import { describe, it, expect, beforeEach, vi } from 'vitest';
2
+
3
+ const mockStore = new Map<string, unknown>();
4
+
5
+ vi.mock('../services/state-service', () => ({
6
+ stateService: {
7
+ get: (_store: string, key: string) => mockStore.get(key) ?? null,
8
+ set: (_store: string, key: string, value: unknown) => { mockStore.set(key, value); },
9
+ delete: (_store: string, key: string) => { mockStore.delete(key); },
10
+ subscribe: () => () => {},
11
+ },
12
+ }));
13
+
14
+ import {
15
+ SNAPSHOT_VERSION,
16
+ loadSnapshot,
17
+ saveSnapshot,
18
+ clearSnapshot,
19
+ extractSnapshot,
20
+ validateAndCleanSnapshot,
21
+ } from './persistence';
22
+ import type { ExplorerTabsSnapshot } from './persistence';
23
+ import type { ExplorerState } from './types';
24
+ import type { KustoConnection, QueryTab } from '../components/ExplorerWorkspace/ExplorerWorkspace.logic';
25
+
26
+ const conn = (id: string): KustoConnection => ({
27
+ id, name: id, clusterUrl: `https://${id}.kusto.windows.net`, database: 'Db', color: '#000',
28
+ });
29
+
30
+ const tab = (id: string, connectionId = 'conn-1', kql = ''): QueryTab => ({
31
+ id, title: `Title ${id}`, kql, connectionId,
32
+ });
33
+
34
+ const baseSnapshot: ExplorerTabsSnapshot = {
35
+ version: SNAPSHOT_VERSION,
36
+ tabs: [tab('tab-1'), tab('tab-2')],
37
+ activeTabId: 'tab-1',
38
+ splitEnabled: false,
39
+ splitDirection: 'vertical',
40
+ splitTabId: null,
41
+ focusedPane: 'primary',
42
+ };
43
+
44
+ describe('persistence', () => {
45
+ beforeEach(() => { mockStore.clear(); });
46
+
47
+ describe('saveSnapshot / loadSnapshot', () => {
48
+ it('round-trips a valid snapshot', () => {
49
+ saveSnapshot(baseSnapshot);
50
+ expect(loadSnapshot()).toEqual(baseSnapshot);
51
+ });
52
+
53
+ it('returns null when no snapshot exists', () => {
54
+ expect(loadSnapshot()).toBeNull();
55
+ });
56
+
57
+ it('returns null for snapshot with wrong version', () => {
58
+ saveSnapshot({ ...baseSnapshot, version: SNAPSHOT_VERSION + 1 });
59
+ expect(loadSnapshot()).toBeNull();
60
+ });
61
+
62
+ it('returns null when the persisted value is malformed', () => {
63
+ mockStore.set('snapshot', { foo: 'bar' });
64
+ expect(loadSnapshot()).toBeNull();
65
+ });
66
+
67
+ it('returns null for snapshot with non-array tabs', () => {
68
+ mockStore.set('snapshot', { ...baseSnapshot, tabs: 'oops' });
69
+ expect(loadSnapshot()).toBeNull();
70
+ });
71
+
72
+ it('returns null when a tab has the wrong shape', () => {
73
+ mockStore.set('snapshot', { ...baseSnapshot, tabs: [{ id: 1, title: 't', kql: '', connectionId: '' }] });
74
+ expect(loadSnapshot()).toBeNull();
75
+ });
76
+
77
+ it('returns null for invalid splitDirection', () => {
78
+ mockStore.set('snapshot', { ...baseSnapshot, splitDirection: 'diagonal' });
79
+ expect(loadSnapshot()).toBeNull();
80
+ });
81
+
82
+ it('returns null for invalid focusedPane', () => {
83
+ mockStore.set('snapshot', { ...baseSnapshot, focusedPane: 'tertiary' });
84
+ expect(loadSnapshot()).toBeNull();
85
+ });
86
+
87
+ it('returns null for non-string activeTabId', () => {
88
+ mockStore.set('snapshot', { ...baseSnapshot, activeTabId: 123 });
89
+ expect(loadSnapshot()).toBeNull();
90
+ });
91
+
92
+ it('returns null for non-boolean splitEnabled', () => {
93
+ mockStore.set('snapshot', { ...baseSnapshot, splitEnabled: 'yes' });
94
+ expect(loadSnapshot()).toBeNull();
95
+ });
96
+
97
+ it('accepts splitTabId as a string', () => {
98
+ const snap = { ...baseSnapshot, splitEnabled: true, splitTabId: 'tab-2' };
99
+ saveSnapshot(snap);
100
+ expect(loadSnapshot()).toEqual(snap);
101
+ });
102
+
103
+ it('rejects splitTabId of the wrong type', () => {
104
+ mockStore.set('snapshot', { ...baseSnapshot, splitTabId: 42 });
105
+ expect(loadSnapshot()).toBeNull();
106
+ });
107
+ });
108
+
109
+ describe('clearSnapshot', () => {
110
+ it('removes a saved snapshot', () => {
111
+ saveSnapshot(baseSnapshot);
112
+ clearSnapshot();
113
+ expect(loadSnapshot()).toBeNull();
114
+ });
115
+ });
116
+
117
+ describe('extractSnapshot', () => {
118
+ it('pulls only the persisted slices from full ExplorerState', () => {
119
+ const state = {
120
+ connections: [conn('conn-1')],
121
+ tabs: [tab('tab-1')],
122
+ activeTabId: 'tab-1',
123
+ splitEnabled: true,
124
+ splitDirection: 'horizontal' as const,
125
+ splitTabId: 'tab-2',
126
+ focusedPane: 'secondary' as const,
127
+ // ...everything else, ignored:
128
+ schema: [], schemaSearch: '', expandedFolders: new Set<string>(),
129
+ expandedTables: new Set<string>(), schemaContextMenu: null,
130
+ loading: false, error: null, queryStartTime: null, runningQueryKey: null,
131
+ rows: null, columns: [], columnTypes: {}, elapsed: null, resultTime: null,
132
+ queryStats: null, resultSets: [], activeResultSet: 0, resultsTab: 'results' as const,
133
+ history: [], editorHeight: 50, hasSelection: false, canRecall: false,
134
+ } satisfies ExplorerState;
135
+
136
+ expect(extractSnapshot(state)).toEqual({
137
+ version: SNAPSHOT_VERSION,
138
+ tabs: [tab('tab-1')],
139
+ activeTabId: 'tab-1',
140
+ splitEnabled: true,
141
+ splitDirection: 'horizontal',
142
+ splitTabId: 'tab-2',
143
+ focusedPane: 'secondary',
144
+ });
145
+ });
146
+ });
147
+
148
+ describe('validateAndCleanSnapshot', () => {
149
+ it('returns null when the snapshot has no tabs', () => {
150
+ const result = validateAndCleanSnapshot({ ...baseSnapshot, tabs: [] }, [conn('conn-1')], 'conn-1');
151
+ expect(result).toBeNull();
152
+ });
153
+
154
+ it('passes through a fully valid snapshot unchanged', () => {
155
+ const conns = [conn('conn-1')];
156
+ const result = validateAndCleanSnapshot(baseSnapshot, conns, 'conn-1');
157
+ expect(result).toEqual(baseSnapshot);
158
+ });
159
+
160
+ it('remaps tabs whose connectionId is missing to the fallback', () => {
161
+ const conns = [conn('conn-1')];
162
+ const snap: ExplorerTabsSnapshot = {
163
+ ...baseSnapshot,
164
+ tabs: [tab('tab-1', 'gone'), tab('tab-2', 'conn-1')],
165
+ };
166
+ const result = validateAndCleanSnapshot(snap, conns, 'conn-1');
167
+ expect(result?.tabs[0].connectionId).toBe('conn-1');
168
+ expect(result?.tabs[1].connectionId).toBe('conn-1');
169
+ });
170
+
171
+ it('preserves kql when remapping a tab to the fallback connection', () => {
172
+ const conns = [conn('conn-1')];
173
+ const snap: ExplorerTabsSnapshot = {
174
+ ...baseSnapshot,
175
+ tabs: [tab('tab-1', 'gone', 'MyTable | take 1')],
176
+ };
177
+ const result = validateAndCleanSnapshot(snap, conns, 'conn-1');
178
+ expect(result?.tabs[0].kql).toBe('MyTable | take 1');
179
+ });
180
+
181
+ it('resets activeTabId to the first tab when it is missing', () => {
182
+ const conns = [conn('conn-1')];
183
+ const snap: ExplorerTabsSnapshot = {
184
+ ...baseSnapshot,
185
+ tabs: [tab('tab-1'), tab('tab-2')],
186
+ activeTabId: 'tab-99',
187
+ };
188
+ const result = validateAndCleanSnapshot(snap, conns, 'conn-1');
189
+ expect(result?.activeTabId).toBe('tab-1');
190
+ });
191
+
192
+ it('resets the split when splitTabId is not in the tab list', () => {
193
+ const conns = [conn('conn-1')];
194
+ const snap: ExplorerTabsSnapshot = {
195
+ ...baseSnapshot,
196
+ splitEnabled: true,
197
+ splitTabId: 'tab-99',
198
+ focusedPane: 'secondary',
199
+ };
200
+ const result = validateAndCleanSnapshot(snap, conns, 'conn-1');
201
+ expect(result?.splitEnabled).toBe(false);
202
+ expect(result?.splitTabId).toBeNull();
203
+ expect(result?.focusedPane).toBe('primary');
204
+ });
205
+
206
+ it('resets the split when splitTabId equals activeTabId', () => {
207
+ const conns = [conn('conn-1')];
208
+ const snap: ExplorerTabsSnapshot = {
209
+ ...baseSnapshot,
210
+ splitEnabled: true,
211
+ splitTabId: 'tab-1',
212
+ activeTabId: 'tab-1',
213
+ focusedPane: 'secondary',
214
+ };
215
+ const result = validateAndCleanSnapshot(snap, conns, 'conn-1');
216
+ expect(result?.splitEnabled).toBe(false);
217
+ expect(result?.splitTabId).toBeNull();
218
+ expect(result?.focusedPane).toBe('primary');
219
+ });
220
+
221
+ it('keeps a valid split untouched', () => {
222
+ const conns = [conn('conn-1')];
223
+ const snap: ExplorerTabsSnapshot = {
224
+ ...baseSnapshot,
225
+ tabs: [tab('tab-1'), tab('tab-2')],
226
+ activeTabId: 'tab-1',
227
+ splitEnabled: true,
228
+ splitTabId: 'tab-2',
229
+ splitDirection: 'horizontal',
230
+ focusedPane: 'secondary',
231
+ };
232
+ const result = validateAndCleanSnapshot(snap, conns, 'conn-1');
233
+ expect(result?.splitEnabled).toBe(true);
234
+ expect(result?.splitTabId).toBe('tab-2');
235
+ expect(result?.splitDirection).toBe('horizontal');
236
+ expect(result?.focusedPane).toBe('secondary');
237
+ });
238
+ });
239
+ });
@@ -0,0 +1,140 @@
1
+ /**
2
+ * Persistence helpers for the Explorer's tab/split state.
3
+ *
4
+ * Snapshots are written to the `explorerTabs` store on the shared
5
+ * StateService. That store is configured with `syncBootstrap: true`,
6
+ * which mirrors writes to localStorage so the snapshot is available
7
+ * synchronously on the next page load (no flash of the default tab).
8
+ *
9
+ * The snapshot is versioned. When the persisted shape changes in a
10
+ * non-backward-compatible way, bump SNAPSHOT_VERSION; older snapshots
11
+ * are then ignored and the Explorer falls back to its default
12
+ * initialization path.
13
+ */
14
+
15
+ import type { QueryTab, KustoConnection } from '../components/ExplorerWorkspace/ExplorerWorkspace.logic';
16
+ import { stateService } from '../services/state-service';
17
+ import type { ExplorerState } from './types';
18
+
19
+ const SNAPSHOT_STORE = 'explorerTabs' as const;
20
+ const SNAPSHOT_KEY = 'snapshot';
21
+ export const SNAPSHOT_VERSION = 1;
22
+
23
+ export interface ExplorerTabsSnapshot {
24
+ version: number;
25
+ tabs: QueryTab[];
26
+ activeTabId: string;
27
+ splitEnabled: boolean;
28
+ splitDirection: 'vertical' | 'horizontal';
29
+ splitTabId: string | null;
30
+ focusedPane: 'primary' | 'secondary';
31
+ }
32
+
33
+ function isQueryTab(value: unknown): value is QueryTab {
34
+ if (!value || typeof value !== 'object') {
35
+ return false;
36
+ }
37
+ const t = value as Record<string, unknown>;
38
+ return typeof t['id'] === 'string'
39
+ && typeof t['title'] === 'string'
40
+ && typeof t['kql'] === 'string'
41
+ && typeof t['connectionId'] === 'string';
42
+ }
43
+
44
+ function isSnapshot(value: unknown): value is ExplorerTabsSnapshot {
45
+ if (!value || typeof value !== 'object') {
46
+ return false;
47
+ }
48
+ const s = value as Record<string, unknown>;
49
+ if (s['version'] !== SNAPSHOT_VERSION) {
50
+ return false;
51
+ }
52
+ if (!Array.isArray(s['tabs']) || !s['tabs'].every(isQueryTab)) {
53
+ return false;
54
+ }
55
+ if (typeof s['activeTabId'] !== 'string') {
56
+ return false;
57
+ }
58
+ if (typeof s['splitEnabled'] !== 'boolean') {
59
+ return false;
60
+ }
61
+ if (s['splitDirection'] !== 'vertical' && s['splitDirection'] !== 'horizontal') {
62
+ return false;
63
+ }
64
+ if (s['splitTabId'] !== null && typeof s['splitTabId'] !== 'string') {
65
+ return false;
66
+ }
67
+ if (s['focusedPane'] !== 'primary' && s['focusedPane'] !== 'secondary') {
68
+ return false;
69
+ }
70
+ return true;
71
+ }
72
+
73
+ /**
74
+ * Read and shape-validate the persisted snapshot.
75
+ * Returns null when no snapshot exists or the shape is invalid/outdated.
76
+ */
77
+ export function loadSnapshot(): ExplorerTabsSnapshot | null {
78
+ const raw = stateService.get<unknown>(SNAPSHOT_STORE, SNAPSHOT_KEY);
79
+ return isSnapshot(raw) ? raw : null;
80
+ }
81
+
82
+ /**
83
+ * Validate the snapshot against the current connection list and clean
84
+ * up any stale references. Returns null when the snapshot has no usable
85
+ * tabs after cleaning.
86
+ *
87
+ * - Tabs whose `connectionId` is missing from `connections` are remapped
88
+ * to `fallbackConnectionId` so the user does not lose their KQL.
89
+ * - `activeTabId` is reset to the first tab if it is missing.
90
+ * - Split state is reset when `splitTabId` does not exist among the
91
+ * restored tabs or matches `activeTabId`.
92
+ */
93
+ export function validateAndCleanSnapshot(
94
+ snapshot: ExplorerTabsSnapshot,
95
+ connections: readonly KustoConnection[],
96
+ fallbackConnectionId: string,
97
+ ): ExplorerTabsSnapshot | null {
98
+ if (snapshot.tabs.length === 0) {
99
+ return null;
100
+ }
101
+ const knownConnIds = new Set(connections.map((c) => c.id));
102
+ const tabs: QueryTab[] = snapshot.tabs.map((t) => (
103
+ knownConnIds.has(t.connectionId) ? t : { ...t, connectionId: fallbackConnectionId }
104
+ ));
105
+
106
+ const tabIds = new Set(tabs.map((t) => t.id));
107
+ const activeTabId = tabIds.has(snapshot.activeTabId) ? snapshot.activeTabId : tabs[0].id;
108
+
109
+ let { splitEnabled, splitDirection, splitTabId, focusedPane } = snapshot;
110
+ if (!splitTabId || !tabIds.has(splitTabId) || splitTabId === activeTabId) {
111
+ splitEnabled = false;
112
+ splitTabId = null;
113
+ focusedPane = 'primary';
114
+ }
115
+
116
+ return { version: SNAPSHOT_VERSION, tabs, activeTabId, splitEnabled, splitDirection, splitTabId, focusedPane };
117
+ }
118
+
119
+ /** Extract the persisted slices from a full ExplorerState. */
120
+ export function extractSnapshot(state: ExplorerState): ExplorerTabsSnapshot {
121
+ return {
122
+ version: SNAPSHOT_VERSION,
123
+ tabs: state.tabs,
124
+ activeTabId: state.activeTabId,
125
+ splitEnabled: state.splitEnabled,
126
+ splitDirection: state.splitDirection,
127
+ splitTabId: state.splitTabId,
128
+ focusedPane: state.focusedPane,
129
+ };
130
+ }
131
+
132
+ /** Persist the snapshot to the StateService (and, transparently, localStorage + IDB). */
133
+ export function saveSnapshot(snapshot: ExplorerTabsSnapshot): void {
134
+ stateService.set(SNAPSHOT_STORE, SNAPSHOT_KEY, snapshot);
135
+ }
136
+
137
+ /** Remove any persisted snapshot. Useful for tests and explicit user "reset". */
138
+ export function clearSnapshot(): void {
139
+ stateService.delete(SNAPSHOT_STORE, SNAPSHOT_KEY);
140
+ }
@@ -1,4 +1,4 @@
1
- import { describe, it, expect, beforeEach, vi } from 'vitest';
1
+ import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
2
2
  import { createInitialState, explorerReducer } from './reducer';
3
3
  import type { ExplorerState, ExplorerAction } from './index';
4
4
 
@@ -13,6 +13,12 @@ vi.mock('../services/state-service', () => ({
13
13
 
14
14
  const mockStore = new Map<string, unknown>();
15
15
 
16
+ // Clear the mock store for every test (in this file). The reducer hydrates
17
+ // from stateService on createInitialState, so leftover entries from one
18
+ // test would leak into the next.
19
+ beforeEach(() => { mockStore.clear(); });
20
+ afterEach(() => { mockStore.clear(); });
21
+
16
22
 
17
23
 
18
24
  // ---------------------------------------------------------------------------
@@ -92,6 +98,129 @@ describe('createInitialState', () => {
92
98
 
93
99
  expect(state.tabs[0].connectionId).toBe('conn-1');
94
100
  });
101
+
102
+ describe('snapshot hydration', () => {
103
+ const conns = [
104
+ { id: 'conn-1', name: 'Cluster A', clusterUrl: 'https://a.kusto.windows.net', database: 'DbA', color: '#909d63' },
105
+ { id: 'conn-2', name: 'Cluster B', clusterUrl: 'https://b.kusto.windows.net', database: 'DbB', color: '#6a8799' },
106
+ ];
107
+
108
+ function setSnapshot(partial: Record<string, unknown>) {
109
+ mockStore.set('snapshot', {
110
+ version: 1,
111
+ tabs: [{ id: 'tab-7', title: 'My Tab', kql: 'A | take 1', connectionId: 'conn-1' }],
112
+ activeTabId: 'tab-7',
113
+ splitEnabled: false,
114
+ splitDirection: 'vertical',
115
+ splitTabId: null,
116
+ focusedPane: 'primary',
117
+ ...partial,
118
+ });
119
+ }
120
+
121
+ it('restores tabs and split state from a valid snapshot', () => {
122
+ mockStore.set('list', conns);
123
+ setSnapshot({
124
+ tabs: [
125
+ { id: 'tab-1', title: 'Q1', kql: 'A', connectionId: 'conn-1' },
126
+ { id: 'tab-2', title: 'Q2', kql: 'B', connectionId: 'conn-2' },
127
+ ],
128
+ activeTabId: 'tab-2',
129
+ splitEnabled: true,
130
+ splitDirection: 'horizontal',
131
+ splitTabId: 'tab-1',
132
+ focusedPane: 'secondary',
133
+ });
134
+
135
+ const state = createInitialState({ initialQuery: 'IGNORED', defaultClusters: [] });
136
+
137
+ expect(state.tabs).toHaveLength(2);
138
+ expect(state.tabs[0].kql).toBe('A');
139
+ expect(state.tabs[1].kql).toBe('B');
140
+ expect(state.activeTabId).toBe('tab-2');
141
+ expect(state.splitEnabled).toBe(true);
142
+ expect(state.splitDirection).toBe('horizontal');
143
+ expect(state.splitTabId).toBe('tab-1');
144
+ expect(state.focusedPane).toBe('secondary');
145
+ });
146
+
147
+ it('uses initialQuery when no snapshot exists', () => {
148
+ mockStore.set('list', conns);
149
+ const state = createInitialState({ initialQuery: 'MyTable | take 5', defaultClusters: [] });
150
+ expect(state.tabs).toHaveLength(1);
151
+ expect(state.tabs[0].kql).toBe('MyTable | take 5');
152
+ });
153
+
154
+ it('uses initialQuery when skipSnapshot is true (URL deep-link)', () => {
155
+ mockStore.set('list', conns);
156
+ setSnapshot({});
157
+ const state = createInitialState({ initialQuery: 'FromUrl | take 1', defaultClusters: [], skipSnapshot: true });
158
+ expect(state.tabs).toHaveLength(1);
159
+ expect(state.tabs[0].kql).toBe('FromUrl | take 1');
160
+ });
161
+
162
+ it('falls back to initialQuery when snapshot is invalid (wrong version)', () => {
163
+ mockStore.set('list', conns);
164
+ mockStore.set('snapshot', { version: 99, tabs: [] });
165
+ const state = createInitialState({ initialQuery: 'fallback', defaultClusters: [] });
166
+ expect(state.tabs[0].kql).toBe('fallback');
167
+ });
168
+
169
+ it('falls back to initialQuery when snapshot has no tabs', () => {
170
+ mockStore.set('list', conns);
171
+ setSnapshot({ tabs: [] });
172
+ const state = createInitialState({ initialQuery: 'fallback', defaultClusters: [] });
173
+ expect(state.tabs[0].kql).toBe('fallback');
174
+ });
175
+
176
+ it('remaps stale tab connectionId to the active connection', () => {
177
+ mockStore.set('list', conns);
178
+ mockStore.set('explorer-active-connection', 'conn-2');
179
+ setSnapshot({
180
+ tabs: [{ id: 'tab-1', title: 'Q', kql: 'X', connectionId: 'gone' }],
181
+ activeTabId: 'tab-1',
182
+ });
183
+ const state = createInitialState({ initialQuery: '', defaultClusters: [] });
184
+ expect(state.tabs[0].connectionId).toBe('conn-2');
185
+ expect(state.tabs[0].kql).toBe('X');
186
+ });
187
+
188
+ it('resets the split when splitTabId is missing from restored tabs', () => {
189
+ mockStore.set('list', conns);
190
+ setSnapshot({
191
+ tabs: [{ id: 'tab-1', title: 'Q', kql: '', connectionId: 'conn-1' }],
192
+ activeTabId: 'tab-1',
193
+ splitEnabled: true,
194
+ splitTabId: 'tab-99',
195
+ focusedPane: 'secondary',
196
+ });
197
+ const state = createInitialState({ initialQuery: '', defaultClusters: [] });
198
+ expect(state.splitEnabled).toBe(false);
199
+ expect(state.splitTabId).toBeNull();
200
+ expect(state.focusedPane).toBe('primary');
201
+ });
202
+
203
+ it('bumps tabCounter past restored ids so subsequent ADD_TAB does not collide', () => {
204
+ mockStore.set('list', conns);
205
+ setSnapshot({
206
+ tabs: [
207
+ { id: 'tab-5', title: 'A', kql: '', connectionId: 'conn-1' },
208
+ { id: 'tab-9', title: 'B', kql: '', connectionId: 'conn-1' },
209
+ ],
210
+ activeTabId: 'tab-5',
211
+ });
212
+ const state = createInitialState({ initialQuery: '', defaultClusters: [] });
213
+
214
+ const next = explorerReducer(state, { type: 'ADD_TAB' });
215
+ const newId = next.tabs[next.tabs.length - 1].id;
216
+ expect(newId).not.toBe('tab-5');
217
+ expect(newId).not.toBe('tab-9');
218
+ // New id should sit past tab-9
219
+ const m = /^tab-(\d+)$/.exec(newId);
220
+ expect(m).not.toBeNull();
221
+ expect(Number(m![1])).toBeGreaterThan(9);
222
+ });
223
+ });
95
224
  });
96
225
 
97
226
  // ---------------------------------------------------------------------------
@@ -630,14 +759,16 @@ describe('explorerReducer', () => {
630
759
  });
631
760
 
632
761
  describe('createInitialState with connections', () => {
762
+ const cluster = { id: 'conn-1', name: 'Cluster A', clusterUrl: 'https://a.kusto.windows.net', database: 'DbA', color: '#909d63' };
763
+
633
764
  it('loads connections', () => {
634
- const state = createInitialState({ initialQuery: '', defaultClusters: [] });
765
+ const state = createInitialState({ initialQuery: '', defaultClusters: [cluster] });
635
766
  expect(state.connections.length).toBeGreaterThanOrEqual(1);
636
767
  expect(state.connections[0].id).toBeTruthy();
637
768
  });
638
769
 
639
770
  it('first tab has a connectionId', () => {
640
- const state = createInitialState({ initialQuery: '', defaultClusters: [] });
771
+ const state = createInitialState({ initialQuery: '', defaultClusters: [cluster] });
641
772
  expect(state.tabs[0].connectionId).toBeTruthy();
642
773
  });
643
774
  });
@@ -5,28 +5,23 @@
5
5
 
6
6
  import type { ExplorerState, InitialStateArgs } from './types';
7
7
  import type { ExplorerAction } from './actions';
8
- import { createTab, nextQueryTitle, loadConnections, loadActiveConnectionId } from '../components/ExplorerWorkspace/ExplorerWorkspace.logic';
8
+ import { createTab, nextQueryTitle, loadConnections, loadActiveConnectionId, bumpTabCounterPast } from '../components/ExplorerWorkspace/ExplorerWorkspace.logic';
9
9
  import { SUPPORTED_RENDER_TYPES } from '../components/ChartPanel/ChartPanel.logic';
10
+ import { loadSnapshot, validateAndCleanSnapshot } from './persistence';
10
11
 
11
- export function createInitialState({ initialQuery, defaultClusters }: InitialStateArgs): ExplorerState {
12
+ export function createInitialState({ initialQuery, defaultClusters, skipSnapshot = false }: InitialStateArgs): ExplorerState {
12
13
  const connections = loadConnections(defaultClusters);
13
- const firstTab = createTab(initialQuery, 'Query 1', loadActiveConnectionId(connections));
14
- return {
14
+ const activeConnectionId = loadActiveConnectionId(connections);
15
+
16
+ const baseDefaults = {
15
17
  connections,
16
18
 
17
19
  schema: [],
18
20
  schemaSearch: '',
19
- expandedFolders: new Set(),
20
- expandedTables: new Set(),
21
+ expandedFolders: new Set<string>(),
22
+ expandedTables: new Set<string>(),
21
23
  schemaContextMenu: null,
22
24
 
23
- tabs: [firstTab],
24
- activeTabId: firstTab.id,
25
- splitEnabled: false,
26
- splitDirection: 'vertical',
27
- splitTabId: null,
28
- focusedPane: 'primary',
29
-
30
25
  loading: false,
31
26
  error: null,
32
27
  queryStartTime: null,
@@ -40,13 +35,43 @@ export function createInitialState({ initialQuery, defaultClusters }: InitialSta
40
35
  queryStats: null,
41
36
  resultSets: [],
42
37
  activeResultSet: 0,
43
- resultsTab: 'results',
38
+ resultsTab: 'results' as const,
44
39
 
45
40
  history: [],
46
41
 
47
42
  editorHeight: 50,
48
43
  hasSelection: false,
49
44
  canRecall: false,
45
+ } satisfies Partial<ExplorerState>;
46
+
47
+ if (!skipSnapshot) {
48
+ const raw = loadSnapshot();
49
+ if (raw) {
50
+ const snap = validateAndCleanSnapshot(raw, connections, activeConnectionId);
51
+ if (snap) {
52
+ bumpTabCounterPast(snap.tabs);
53
+ return {
54
+ ...baseDefaults,
55
+ tabs: snap.tabs,
56
+ activeTabId: snap.activeTabId,
57
+ splitEnabled: snap.splitEnabled,
58
+ splitDirection: snap.splitDirection,
59
+ splitTabId: snap.splitTabId,
60
+ focusedPane: snap.focusedPane,
61
+ };
62
+ }
63
+ }
64
+ }
65
+
66
+ const firstTab = createTab(initialQuery, 'Query 1', activeConnectionId);
67
+ return {
68
+ ...baseDefaults,
69
+ tabs: [firstTab],
70
+ activeTabId: firstTab.id,
71
+ splitEnabled: false,
72
+ splitDirection: 'vertical',
73
+ splitTabId: null,
74
+ focusedPane: 'primary',
50
75
  };
51
76
  }
52
77
 
@@ -51,4 +51,10 @@ export interface ExplorerState {
51
51
  export interface InitialStateArgs {
52
52
  initialQuery: string;
53
53
  defaultClusters: KustoConnection[];
54
+ /**
55
+ * When true, ignore any persisted tab snapshot and start fresh with
56
+ * `initialQuery` in a single tab. The Explorer sets this when the
57
+ * URL contains a `?query=` parameter so deep-links always win.
58
+ */
59
+ skipSnapshot?: boolean;
54
60
  }
@@ -0,0 +1,3 @@
1
+ import { vi } from 'vitest';
2
+
3
+ vi.mock('monaco-editor', () => ({}));
package/vitest.config.ts CHANGED
@@ -1,6 +1,12 @@
1
1
  import { defineConfig } from 'vitest/config';
2
2
 
3
3
  export default defineConfig({
4
+ optimizeDeps: {
5
+ include: ['monaco-editor'],
6
+ },
7
+ resolve: {
8
+ mainFields: ['module', 'jsnext:main', 'jsnext', 'main', 'browser'],
9
+ },
4
10
  test: {
5
11
  globals: false,
6
12
  environment: 'jsdom',
@@ -8,6 +14,7 @@ export default defineConfig({
8
14
  exclude: ['**/node_modules/**', '**/.{idea,git,cache,output,temp}/**'],
9
15
  passWithNoTests: false,
10
16
  retry: 0,
17
+ setupFiles: ['./src/test-setup.ts'],
11
18
  typecheck: {
12
19
  enabled: true,
13
20
  tsconfig: 'tsconfig.spec.json',