@parca/profile 0.19.73 → 0.19.74

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 (71) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/dist/MatchersInput/index.d.ts.map +1 -1
  3. package/dist/MatchersInput/index.js +4 -2
  4. package/dist/ProfileExplorer/ProfileExplorerCompare.d.ts +1 -12
  5. package/dist/ProfileExplorer/ProfileExplorerCompare.d.ts.map +1 -1
  6. package/dist/ProfileExplorer/ProfileExplorerCompare.js +52 -11
  7. package/dist/ProfileExplorer/ProfileExplorerSingle.d.ts +1 -7
  8. package/dist/ProfileExplorer/ProfileExplorerSingle.d.ts.map +1 -1
  9. package/dist/ProfileExplorer/ProfileExplorerSingle.js +4 -2
  10. package/dist/ProfileExplorer/index.d.ts +1 -4
  11. package/dist/ProfileExplorer/index.d.ts.map +1 -1
  12. package/dist/ProfileExplorer/index.js +11 -225
  13. package/dist/ProfileMetricsGraph/index.d.ts +1 -1
  14. package/dist/ProfileMetricsGraph/index.d.ts.map +1 -1
  15. package/dist/ProfileMetricsGraph/index.js +16 -20
  16. package/dist/ProfileSelector/MetricsGraphSection.d.ts +3 -3
  17. package/dist/ProfileSelector/MetricsGraphSection.d.ts.map +1 -1
  18. package/dist/ProfileSelector/MetricsGraphSection.js +10 -6
  19. package/dist/ProfileSelector/index.d.ts +2 -7
  20. package/dist/ProfileSelector/index.d.ts.map +1 -1
  21. package/dist/ProfileSelector/index.js +40 -46
  22. package/dist/ProfileSelector/useAutoQuerySelector.d.ts.map +1 -1
  23. package/dist/ProfileSelector/useAutoQuerySelector.js +19 -4
  24. package/dist/ProfileTypeSelector/index.d.ts.map +1 -1
  25. package/dist/ProfileTypeSelector/index.js +1 -1
  26. package/dist/ProfileView/components/ViewSelector/index.d.ts.map +1 -1
  27. package/dist/ProfileView/components/ViewSelector/index.js +10 -4
  28. package/dist/ProfileView/hooks/useResetStateOnProfileTypeChange.d.ts.map +1 -1
  29. package/dist/ProfileView/hooks/useResetStateOnProfileTypeChange.js +4 -2
  30. package/dist/ProfileView/hooks/useVisualizationState.d.ts.map +1 -1
  31. package/dist/ProfileView/hooks/useVisualizationState.js +20 -13
  32. package/dist/Table/MoreDropdown.d.ts.map +1 -1
  33. package/dist/Table/MoreDropdown.js +7 -3
  34. package/dist/Table/TableContextMenu.d.ts.map +1 -1
  35. package/dist/Table/TableContextMenu.js +9 -5
  36. package/dist/hooks/useCompareModeMeta.d.ts +10 -0
  37. package/dist/hooks/useCompareModeMeta.d.ts.map +1 -0
  38. package/dist/hooks/useCompareModeMeta.js +113 -0
  39. package/dist/hooks/useQueryState.d.ts +32 -0
  40. package/dist/hooks/useQueryState.d.ts.map +1 -0
  41. package/dist/hooks/useQueryState.js +285 -0
  42. package/dist/hooks/useQueryState.test.d.ts +2 -0
  43. package/dist/hooks/useQueryState.test.d.ts.map +1 -0
  44. package/dist/hooks/useQueryState.test.js +910 -0
  45. package/dist/index.d.ts +4 -5
  46. package/dist/index.d.ts.map +1 -1
  47. package/dist/index.js +6 -3
  48. package/dist/useSumBy.d.ts +7 -0
  49. package/dist/useSumBy.d.ts.map +1 -1
  50. package/dist/useSumBy.js +31 -6
  51. package/package.json +6 -6
  52. package/src/MatchersInput/index.tsx +4 -2
  53. package/src/ProfileExplorer/ProfileExplorerCompare.tsx +64 -46
  54. package/src/ProfileExplorer/ProfileExplorerSingle.tsx +7 -19
  55. package/src/ProfileExplorer/index.tsx +11 -339
  56. package/src/ProfileMetricsGraph/index.tsx +16 -20
  57. package/src/ProfileSelector/MetricsGraphSection.tsx +14 -10
  58. package/src/ProfileSelector/index.tsx +65 -83
  59. package/src/ProfileSelector/useAutoQuerySelector.ts +23 -5
  60. package/src/ProfileTypeSelector/index.tsx +3 -1
  61. package/src/ProfileView/components/ViewSelector/index.tsx +9 -4
  62. package/src/ProfileView/hooks/useResetStateOnProfileTypeChange.ts +4 -2
  63. package/src/ProfileView/hooks/useVisualizationState.ts +25 -12
  64. package/src/Table/MoreDropdown.tsx +7 -3
  65. package/src/Table/TableContextMenu.tsx +9 -5
  66. package/src/hooks/useCompareModeMeta.ts +131 -0
  67. package/src/hooks/useQueryState.test.tsx +1202 -0
  68. package/src/hooks/useQueryState.ts +414 -0
  69. package/src/index.tsx +9 -11
  70. package/src/useSumBy.ts +62 -7
  71. package/src/ProfileExplorer/index.test.ts +0 -97
@@ -0,0 +1,910 @@
1
+ import { jsx as _jsx } from "react/jsx-runtime";
2
+ // eslint-disable-next-line import/named
3
+ import { act, renderHook, waitFor } from '@testing-library/react';
4
+ import { beforeEach, describe, expect, it, vi } from 'vitest';
5
+ import { URLStateProvider } from '@parca/components';
6
+ import { useQueryState } from './useQueryState';
7
+ // Mock window.location
8
+ const mockLocation = {
9
+ pathname: '/test',
10
+ search: '',
11
+ };
12
+ // Mock the navigate function that actually updates the mock location
13
+ const mockNavigateTo = vi.fn((path, params) => {
14
+ // Convert params object to query string
15
+ const searchParams = new URLSearchParams();
16
+ Object.entries(params).forEach(([key, value]) => {
17
+ if (value !== undefined && value !== null) {
18
+ if (Array.isArray(value)) {
19
+ // For arrays, join with commas
20
+ searchParams.set(key, value.join(','));
21
+ }
22
+ else {
23
+ searchParams.set(key, String(value));
24
+ }
25
+ }
26
+ });
27
+ mockLocation.search = `?${searchParams.toString()}`;
28
+ });
29
+ // Mock the getQueryParamsFromURL function
30
+ vi.mock('@parca/components/src/hooks/URLState/utils', async () => {
31
+ const actual = await vi.importActual('@parca/components/src/hooks/URLState/utils');
32
+ return {
33
+ ...actual,
34
+ getQueryParamsFromURL: () => {
35
+ if (mockLocation.search === '')
36
+ return {};
37
+ const params = new URLSearchParams(mockLocation.search);
38
+ const result = {};
39
+ for (const [key, value] of params.entries()) {
40
+ const decodedValue = decodeURIComponent(value);
41
+ const existing = result[key];
42
+ if (existing !== undefined) {
43
+ result[key] = Array.isArray(existing)
44
+ ? [...existing, decodedValue]
45
+ : [existing, decodedValue];
46
+ }
47
+ else {
48
+ result[key] = decodedValue;
49
+ }
50
+ }
51
+ return result;
52
+ },
53
+ };
54
+ });
55
+ // Mock useSumBy to return the sumBy from URL params or undefined
56
+ vi.mock('../useSumBy', async () => {
57
+ const actual = await vi.importActual('../useSumBy');
58
+ return {
59
+ ...actual,
60
+ useSumBy: (_queryClient, _profileType, _timeRange, defaultValue) => ({
61
+ sumBy: defaultValue,
62
+ setSumBy: vi.fn(),
63
+ isLoading: false,
64
+ }),
65
+ };
66
+ });
67
+ // Helper to create wrapper with URLStateProvider
68
+ const createWrapper = (paramPreferences = {}) => {
69
+ const Wrapper = ({ children }) => (_jsx(URLStateProvider, { navigateTo: mockNavigateTo, paramPreferences: paramPreferences, children: children }));
70
+ Wrapper.displayName = 'URLStateProviderWrapper';
71
+ return Wrapper;
72
+ };
73
+ describe('useQueryState', () => {
74
+ beforeEach(() => {
75
+ mockNavigateTo.mockClear();
76
+ Object.defineProperty(window, 'location', {
77
+ value: mockLocation,
78
+ writable: true,
79
+ });
80
+ mockLocation.search = '';
81
+ });
82
+ describe('Basic functionality', () => {
83
+ it('should initialize with default values', () => {
84
+ const { result } = renderHook(() => useQueryState({
85
+ defaultExpression: 'process_cpu{}',
86
+ defaultTimeSelection: 'relative:hour|1',
87
+ defaultFrom: 1000,
88
+ defaultTo: 2000,
89
+ }), { wrapper: createWrapper() });
90
+ const { querySelection } = result.current;
91
+ expect(querySelection.expression).toBe('process_cpu{}');
92
+ expect(querySelection.timeSelection).toBe('relative:hour|1');
93
+ // From/to should be calculated from the range
94
+ expect(querySelection.from).toBeDefined();
95
+ expect(querySelection.to).toBeDefined();
96
+ });
97
+ it('should handle suffix for comparison mode', () => {
98
+ mockLocation.search =
99
+ '?expression_a=process_cpu:cpu:nanoseconds:cpu:nanoseconds:delta{}&from_a=1000&to_a=2000';
100
+ const { result } = renderHook(() => useQueryState({ suffix: '_a' }), { wrapper: createWrapper() });
101
+ const { querySelection } = result.current;
102
+ expect(querySelection.expression).toBe('process_cpu:cpu:nanoseconds:cpu:nanoseconds:delta{}');
103
+ expect(querySelection.from).toBe(1000);
104
+ expect(querySelection.to).toBe(2000);
105
+ });
106
+ });
107
+ describe('Individual setters', () => {
108
+ it('should update expression and handle delta profiles', async () => {
109
+ const { result } = renderHook(() => useQueryState({
110
+ defaultFrom: 1000,
111
+ defaultTo: 2000,
112
+ }), { wrapper: createWrapper() });
113
+ act(() => {
114
+ result.current.setDraftExpression('memory:alloc_objects:count:space:bytes:delta{}');
115
+ });
116
+ // Draft should be updated but not committed
117
+ expect(result.current.draftSelection.expression).toBe('memory:alloc_objects:count:space:bytes:delta{}');
118
+ // Delta profile should auto-calculate merge params in draft
119
+ expect(result.current.draftSelection.mergeFrom).toBe('1000000000');
120
+ expect(result.current.draftSelection.mergeTo).toBe('2000000000');
121
+ act(() => {
122
+ result.current.commitDraft();
123
+ });
124
+ await waitFor(() => {
125
+ expect(mockNavigateTo).toHaveBeenCalled();
126
+ const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
127
+ expect(params.expression).toBe('memory:alloc_objects:count:space:bytes:delta{}');
128
+ // Should set merge parameters for delta profile
129
+ expect(params).toHaveProperty('merge_from');
130
+ expect(params).toHaveProperty('merge_to');
131
+ expect(params.merge_from).toBe('1000000000');
132
+ expect(params.merge_to).toBe('2000000000');
133
+ });
134
+ });
135
+ it('should update time range', async () => {
136
+ const { result } = renderHook(() => useQueryState(), { wrapper: createWrapper() });
137
+ act(() => {
138
+ result.current.setDraftTimeRange(3000, 4000, 'relative:minute|5');
139
+ });
140
+ // Draft should be updated
141
+ expect(result.current.draftSelection.from).toBe(3000);
142
+ expect(result.current.draftSelection.to).toBe(4000);
143
+ expect(result.current.draftSelection.timeSelection).toBe('relative:minute|5');
144
+ act(() => {
145
+ result.current.commitDraft();
146
+ });
147
+ await waitFor(() => {
148
+ expect(mockNavigateTo).toHaveBeenCalled();
149
+ const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
150
+ expect(params.from).toBe('3000');
151
+ expect(params.to).toBe('4000');
152
+ expect(params.time_selection).toBe('relative:minute|5');
153
+ });
154
+ });
155
+ it('should update sumBy', async () => {
156
+ const { result } = renderHook(() => useQueryState(), { wrapper: createWrapper() });
157
+ act(() => {
158
+ result.current.setDraftSumBy(['namespace', 'container']);
159
+ });
160
+ // Draft should be updated
161
+ expect(result.current.draftSelection.sumBy).toEqual(['namespace', 'container']);
162
+ act(() => {
163
+ result.current.commitDraft();
164
+ });
165
+ await waitFor(() => {
166
+ expect(mockNavigateTo).toHaveBeenCalled();
167
+ const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
168
+ expect(params.sum_by).toBe('namespace,container');
169
+ });
170
+ });
171
+ it('should auto-calculate merge range for delta profiles', async () => {
172
+ const { result } = renderHook(() => useQueryState(), { wrapper: createWrapper() });
173
+ // Set a delta profile expression
174
+ act(() => {
175
+ result.current.setDraftExpression('memory:alloc_space:bytes:space:bytes:delta{}');
176
+ result.current.setDraftTimeRange(5000, 6000, 'relative:minute|5');
177
+ });
178
+ // Merge range should be auto-calculated in draft
179
+ expect(result.current.draftSelection.mergeFrom).toBe('5000000000');
180
+ expect(result.current.draftSelection.mergeTo).toBe('6000000000');
181
+ act(() => {
182
+ result.current.commitDraft();
183
+ });
184
+ await waitFor(() => {
185
+ expect(mockNavigateTo).toHaveBeenCalled();
186
+ const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
187
+ expect(params.merge_from).toBe('5000000000');
188
+ expect(params.merge_to).toBe('6000000000');
189
+ });
190
+ });
191
+ });
192
+ describe('Batch updates', () => {
193
+ it('should batch multiple updates into single navigation', async () => {
194
+ const { result } = renderHook(() => useQueryState(), { wrapper: createWrapper() });
195
+ act(() => {
196
+ // Update multiple draft values
197
+ result.current.setDraftExpression('memory:inuse_space:bytes:space:bytes{}');
198
+ result.current.setDraftTimeRange(7000, 8000, 'relative:minute|30');
199
+ result.current.setDraftSumBy(['pod', 'node']);
200
+ });
201
+ // All drafts should be updated
202
+ expect(result.current.draftSelection.expression).toBe('memory:inuse_space:bytes:space:bytes{}');
203
+ expect(result.current.draftSelection.from).toBe(7000);
204
+ expect(result.current.draftSelection.to).toBe(8000);
205
+ expect(result.current.draftSelection.sumBy).toEqual(['pod', 'node']);
206
+ act(() => {
207
+ result.current.commitDraft();
208
+ });
209
+ await waitFor(() => {
210
+ // Should only navigate once for all updates
211
+ expect(mockNavigateTo).toHaveBeenCalledTimes(1);
212
+ const [, params] = mockNavigateTo.mock.calls[0];
213
+ expect(params.expression).toBe('memory:inuse_space:bytes:space:bytes{}');
214
+ expect(params.from).toBe('7000');
215
+ expect(params.to).toBe('8000');
216
+ expect(params.time_selection).toBe('relative:minute|30');
217
+ expect(params.sum_by).toBe('pod,node');
218
+ });
219
+ });
220
+ it('should handle partial updates', async () => {
221
+ mockLocation.search =
222
+ '?expression=process_cpu:cpu:nanoseconds:cpu:nanoseconds:delta{}&from=1000&to=2000&time_selection=relative:hour|1';
223
+ const { result } = renderHook(() => useQueryState(), { wrapper: createWrapper() });
224
+ act(() => {
225
+ // Only update expression, other values should remain
226
+ result.current.setDraftExpression('memory:inuse_space:bytes:space:bytes{}');
227
+ });
228
+ expect(result.current.draftSelection.expression).toBe('memory:inuse_space:bytes:space:bytes{}');
229
+ // Other values should be from URL
230
+ expect(result.current.draftSelection.from).toBe(1000);
231
+ expect(result.current.draftSelection.to).toBe(2000);
232
+ act(() => {
233
+ result.current.commitDraft();
234
+ });
235
+ await waitFor(() => {
236
+ expect(mockNavigateTo).toHaveBeenCalled();
237
+ const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
238
+ expect(params.expression).toBe('memory:inuse_space:bytes:space:bytes{}');
239
+ expect(params.from).toBe('1000');
240
+ expect(params.to).toBe('2000');
241
+ expect(params.time_selection).toBe('relative:hour|1');
242
+ });
243
+ });
244
+ it('should auto-calculate merge params for delta profiles in batch update', async () => {
245
+ const { result } = renderHook(() => useQueryState(), { wrapper: createWrapper() });
246
+ act(() => {
247
+ result.current.setDraftExpression('memory:alloc_space:bytes:space:bytes:delta{}');
248
+ result.current.setDraftTimeRange(9000, 10000, 'relative:minute|5');
249
+ });
250
+ // Merge params should be auto-calculated in draft
251
+ expect(result.current.draftSelection.mergeFrom).toBe('9000000000');
252
+ expect(result.current.draftSelection.mergeTo).toBe('10000000000');
253
+ act(() => {
254
+ result.current.commitDraft();
255
+ });
256
+ await waitFor(() => {
257
+ expect(mockNavigateTo).toHaveBeenCalled();
258
+ const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
259
+ expect(params.expression).toBe('memory:alloc_space:bytes:space:bytes:delta{}');
260
+ expect(params.merge_from).toBe('9000000000');
261
+ expect(params.merge_to).toBe('10000000000');
262
+ });
263
+ });
264
+ });
265
+ describe('Helper functions', () => {
266
+ it('should set profile name correctly', async () => {
267
+ mockLocation.search =
268
+ '?expression=process_cpu:cpu:nanoseconds:cpu:nanoseconds:delta{job="parca"}';
269
+ const { result } = renderHook(() => useQueryState(), { wrapper: createWrapper() });
270
+ act(() => {
271
+ result.current.setDraftProfileName('memory:inuse_space:bytes:space:bytes');
272
+ });
273
+ // Draft should be updated
274
+ expect(result.current.draftSelection.expression).toBe('memory:inuse_space:bytes:space:bytes{job="parca"}');
275
+ act(() => {
276
+ result.current.commitDraft();
277
+ });
278
+ await waitFor(() => {
279
+ expect(mockNavigateTo).toHaveBeenCalled();
280
+ const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
281
+ expect(params.expression).toBe('memory:inuse_space:bytes:space:bytes{job="parca"}');
282
+ });
283
+ });
284
+ it('should set matchers correctly using draft', async () => {
285
+ mockLocation.search = '?expression=process_cpu:cpu:nanoseconds:cpu:nanoseconds:delta{}';
286
+ const { result } = renderHook(() => useQueryState(), { wrapper: createWrapper() });
287
+ act(() => {
288
+ result.current.setDraftMatchers('namespace="default",pod="my-pod"');
289
+ });
290
+ // Draft should be updated but not URL yet
291
+ expect(result.current.draftSelection.expression).toBe('process_cpu:cpu:nanoseconds:cpu:nanoseconds:delta{namespace="default",pod="my-pod"}');
292
+ expect(mockNavigateTo).not.toHaveBeenCalled();
293
+ // Commit the draft
294
+ act(() => {
295
+ result.current.commitDraft();
296
+ });
297
+ await waitFor(() => {
298
+ expect(mockNavigateTo).toHaveBeenCalled();
299
+ const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
300
+ expect(params.expression).toBe('process_cpu:cpu:nanoseconds:cpu:nanoseconds:delta{namespace="default",pod="my-pod"}');
301
+ });
302
+ });
303
+ });
304
+ describe('Comparison mode', () => {
305
+ it('should handle _a suffix correctly', async () => {
306
+ const { result } = renderHook(() => useQueryState({ suffix: '_a' }), { wrapper: createWrapper() });
307
+ // Update draft state
308
+ act(() => {
309
+ result.current.setDraftExpression('process_cpu:cpu:nanoseconds:cpu:nanoseconds:delta{}');
310
+ result.current.setDraftTimeRange(1111, 2222, 'relative:hour|1');
311
+ result.current.setDraftSumBy(['label_a']);
312
+ });
313
+ // Commit draft
314
+ act(() => {
315
+ result.current.commitDraft();
316
+ });
317
+ await waitFor(() => {
318
+ expect(mockNavigateTo).toHaveBeenCalled();
319
+ const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
320
+ expect(params.expression_a).toBe('process_cpu:cpu:nanoseconds:cpu:nanoseconds:delta{}');
321
+ expect(params.from_a).toBe('1111');
322
+ expect(params.to_a).toBe('2222');
323
+ expect(params.sum_by_a).toBe('label_a');
324
+ });
325
+ });
326
+ it('should handle _b suffix correctly', async () => {
327
+ const { result } = renderHook(() => useQueryState({ suffix: '_b' }), { wrapper: createWrapper() });
328
+ // Update draft state
329
+ act(() => {
330
+ result.current.setDraftExpression('memory:inuse_space:bytes:space:bytes{}');
331
+ result.current.setDraftTimeRange(3333, 4444, 'relative:hour|2');
332
+ result.current.setDraftSumBy(['label_b']);
333
+ });
334
+ // Commit draft
335
+ act(() => {
336
+ result.current.commitDraft();
337
+ });
338
+ await waitFor(() => {
339
+ expect(mockNavigateTo).toHaveBeenCalled();
340
+ const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
341
+ expect(params.expression_b).toBe('memory:inuse_space:bytes:space:bytes{}');
342
+ expect(params.from_b).toBe('3333');
343
+ expect(params.to_b).toBe('4444');
344
+ expect(params.sum_by_b).toBe('label_b');
345
+ });
346
+ });
347
+ });
348
+ describe('Draft state pattern', () => {
349
+ it('should not update URL until commit', async () => {
350
+ const { result } = renderHook(() => useQueryState(), { wrapper: createWrapper() });
351
+ // Make multiple draft changes
352
+ act(() => {
353
+ result.current.setDraftExpression('memory:inuse_space:bytes:space:bytes{}');
354
+ result.current.setDraftTimeRange(5000, 6000, 'relative:hour|3');
355
+ result.current.setDraftSumBy(['namespace', 'pod']);
356
+ });
357
+ // URL should not be updated yet
358
+ expect(mockNavigateTo).not.toHaveBeenCalled();
359
+ // Commit all changes at once
360
+ act(() => {
361
+ result.current.commitDraft();
362
+ });
363
+ // Now URL should be updated exactly once with all changes
364
+ await waitFor(() => {
365
+ expect(mockNavigateTo).toHaveBeenCalledTimes(1);
366
+ const [, params] = mockNavigateTo.mock.calls[0];
367
+ expect(params.expression).toBe('memory:inuse_space:bytes:space:bytes{}');
368
+ expect(params.from).toBe('5000');
369
+ expect(params.to).toBe('6000');
370
+ expect(params.sum_by).toBe('namespace,pod');
371
+ });
372
+ });
373
+ it('should handle draft profile name changes', () => {
374
+ mockLocation.search =
375
+ '?expression=process_cpu:cpu:nanoseconds:cpu:nanoseconds:delta{job="test"}';
376
+ const { result } = renderHook(() => useQueryState(), { wrapper: createWrapper() });
377
+ // Change profile name in draft
378
+ act(() => {
379
+ result.current.setDraftProfileName('memory:inuse_space:bytes:space:bytes');
380
+ });
381
+ // Draft should be updated
382
+ expect(result.current.draftSelection.expression).toBe('memory:inuse_space:bytes:space:bytes{job="test"}');
383
+ // URL should not be updated yet
384
+ expect(mockNavigateTo).not.toHaveBeenCalled();
385
+ });
386
+ });
387
+ describe('Edge cases', () => {
388
+ it('should handle invalid expression gracefully', () => {
389
+ const { result } = renderHook(() => useQueryState({
390
+ defaultExpression: 'invalid{{}expression',
391
+ }), { wrapper: createWrapper() });
392
+ // Should not throw error
393
+ expect(() => result.current.querySelection).not.toThrow();
394
+ });
395
+ it('should clear merge params for non-delta profiles', async () => {
396
+ mockLocation.search =
397
+ '?expression=memory:alloc_objects:count:space:bytes:delta{}&merge_from=1000000000&merge_to=2000000000';
398
+ const { result } = renderHook(() => useQueryState(), { wrapper: createWrapper() });
399
+ // Switch to non-delta profile (without :delta suffix) using draft
400
+ act(() => {
401
+ result.current.setDraftExpression('memory:inuse_space:bytes:space:bytes{}');
402
+ });
403
+ // Commit the draft
404
+ act(() => {
405
+ result.current.commitDraft();
406
+ });
407
+ await waitFor(() => {
408
+ expect(mockNavigateTo).toHaveBeenCalled();
409
+ const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
410
+ expect(params.expression).toBe('memory:inuse_space:bytes:space:bytes{}');
411
+ expect(params).not.toHaveProperty('merge_from');
412
+ expect(params).not.toHaveProperty('merge_to');
413
+ });
414
+ });
415
+ it('should preserve other URL parameters when updating', async () => {
416
+ mockLocation.search =
417
+ '?expression=process_cpu:cpu:nanoseconds:cpu:nanoseconds:delta{}&other_param=value&unrelated=test';
418
+ const { result } = renderHook(() => useQueryState(), { wrapper: createWrapper() });
419
+ // Update draft and commit
420
+ act(() => {
421
+ result.current.setDraftExpression('memory:inuse_space:bytes:space:bytes{}');
422
+ });
423
+ act(() => {
424
+ result.current.commitDraft();
425
+ });
426
+ await waitFor(() => {
427
+ expect(mockNavigateTo).toHaveBeenCalled();
428
+ const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
429
+ expect(params.expression).toBe('memory:inuse_space:bytes:space:bytes{}');
430
+ expect(params.other_param).toBe('value');
431
+ expect(params.unrelated).toBe('test');
432
+ });
433
+ });
434
+ });
435
+ describe('Commit with refreshed time range (time range re-evaluation)', () => {
436
+ it('should use refreshed time range values instead of draft state when provided', async () => {
437
+ mockLocation.search =
438
+ '?expression=process_cpu:cpu:nanoseconds:cpu:nanoseconds{}&from=1000&to=2000&time_selection=relative:minute|15';
439
+ const { result } = renderHook(() => useQueryState(), { wrapper: createWrapper() });
440
+ // Draft state has original values
441
+ expect(result.current.draftSelection.from).toBe(1000);
442
+ expect(result.current.draftSelection.to).toBe(2000);
443
+ expect(result.current.draftSelection.timeSelection).toBe('relative:minute|15');
444
+ // Commit with refreshed time range (simulating re-evaluated time range)
445
+ act(() => {
446
+ result.current.commitDraft({
447
+ from: 5000,
448
+ to: 6000,
449
+ timeSelection: 'relative:minute|15',
450
+ });
451
+ });
452
+ await waitFor(() => {
453
+ expect(mockNavigateTo).toHaveBeenCalled();
454
+ const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
455
+ // Should use refreshed time range values, not draft values
456
+ expect(params.from).toBe('5000');
457
+ expect(params.to).toBe('6000');
458
+ expect(params.time_selection).toBe('relative:minute|15');
459
+ });
460
+ });
461
+ it('should update draft state with refreshed time range after commit', async () => {
462
+ const { result } = renderHook(() => useQueryState({
463
+ defaultExpression: 'process_cpu:cpu:nanoseconds:cpu:nanoseconds:delta{}',
464
+ defaultFrom: 1000,
465
+ defaultTo: 2000,
466
+ defaultTimeSelection: 'relative:minute|5',
467
+ }), { wrapper: createWrapper() });
468
+ // Commit with refreshed time values
469
+ act(() => {
470
+ result.current.commitDraft({
471
+ from: 3000,
472
+ to: 4000,
473
+ timeSelection: 'relative:minute|5',
474
+ });
475
+ });
476
+ await waitFor(() => {
477
+ expect(mockNavigateTo).toHaveBeenCalled();
478
+ });
479
+ // Draft state should be updated with the refreshed time range
480
+ expect(result.current.draftSelection.from).toBe(3000);
481
+ expect(result.current.draftSelection.to).toBe(4000);
482
+ });
483
+ it('should trigger navigation even when expression unchanged (time re-evaluation)', async () => {
484
+ mockLocation.search =
485
+ '?expression=process_cpu:cpu:nanoseconds:cpu:nanoseconds{}&from=1000&to=2000&time_selection=relative:minute|5';
486
+ const { result } = renderHook(() => useQueryState(), { wrapper: createWrapper() });
487
+ mockNavigateTo.mockClear();
488
+ // First commit with new time values
489
+ act(() => {
490
+ result.current.commitDraft({
491
+ from: 5000,
492
+ to: 6000,
493
+ timeSelection: 'relative:minute|5',
494
+ });
495
+ });
496
+ await waitFor(() => {
497
+ expect(mockNavigateTo).toHaveBeenCalledTimes(1);
498
+ });
499
+ const firstCallParams = mockNavigateTo.mock.calls[0][1];
500
+ expect(firstCallParams.from).toBe('5000');
501
+ expect(firstCallParams.to).toBe('6000');
502
+ mockNavigateTo.mockClear();
503
+ // Second commit with different time values (simulating clicking Search again)
504
+ act(() => {
505
+ result.current.commitDraft({
506
+ from: 7000,
507
+ to: 8000,
508
+ timeSelection: 'relative:minute|5',
509
+ });
510
+ });
511
+ await waitFor(() => {
512
+ expect(mockNavigateTo).toHaveBeenCalledTimes(1);
513
+ });
514
+ const secondCallParams = mockNavigateTo.mock.calls[0][1];
515
+ expect(secondCallParams.from).toBe('7000');
516
+ expect(secondCallParams.to).toBe('8000');
517
+ // Verify that navigation was called both times despite expression being unchanged
518
+ expect(firstCallParams.from).not.toBe(secondCallParams.from);
519
+ });
520
+ it('should auto-calculate merge params for delta profiles when using refreshed time range', async () => {
521
+ const { result } = renderHook(() => useQueryState({
522
+ defaultExpression: 'process_cpu:cpu:nanoseconds:cpu:nanoseconds:delta{}',
523
+ defaultFrom: 1000,
524
+ defaultTo: 2000,
525
+ }), { wrapper: createWrapper() });
526
+ // Commit with refreshed time range for delta profile
527
+ act(() => {
528
+ result.current.commitDraft({
529
+ from: 5000,
530
+ to: 6000,
531
+ timeSelection: 'relative:minute|5',
532
+ });
533
+ });
534
+ await waitFor(() => {
535
+ expect(mockNavigateTo).toHaveBeenCalled();
536
+ const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
537
+ // Verify merge params are calculated from refreshed time range
538
+ expect(params.merge_from).toBe('5000000000'); // 5000ms * 1_000_000
539
+ expect(params.merge_to).toBe('6000000000'); // 6000ms * 1_000_000
540
+ });
541
+ });
542
+ it('should use draft values when refreshedTimeRange is not provided', async () => {
543
+ mockLocation.search =
544
+ '?expression=memory:inuse_space:bytes:space:bytes{}&from=1000&to=2000&time_selection=relative:hour|1';
545
+ const { result } = renderHook(() => useQueryState(), { wrapper: createWrapper() });
546
+ // Change draft values
547
+ act(() => {
548
+ result.current.setDraftTimeRange(3000, 4000, 'relative:minute|30');
549
+ });
550
+ // Commit without refreshedTimeRange - should use draft values
551
+ act(() => {
552
+ result.current.commitDraft();
553
+ });
554
+ await waitFor(() => {
555
+ expect(mockNavigateTo).toHaveBeenCalled();
556
+ const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
557
+ // Should use updated draft values
558
+ expect(params.from).toBe('3000');
559
+ expect(params.to).toBe('4000');
560
+ expect(params.time_selection).toBe('relative:minute|30');
561
+ });
562
+ });
563
+ });
564
+ describe('State persistence after page reload', () => {
565
+ it('should retain committed values after page reload simulation', async () => {
566
+ // Initial state
567
+ mockLocation.search =
568
+ '?expression=process_cpu:cpu:nanoseconds:cpu:nanoseconds{}&from=1000&to=2000';
569
+ const { result: result1, unmount } = renderHook(() => useQueryState(), {
570
+ wrapper: createWrapper(),
571
+ });
572
+ // User makes changes to draft
573
+ act(() => {
574
+ result1.current.setDraftExpression('memory:inuse_space:bytes:space:bytes{}');
575
+ result1.current.setDraftTimeRange(5000, 6000, 'relative:minute|15');
576
+ result1.current.setDraftSumBy(['namespace', 'pod']);
577
+ });
578
+ // User clicks Search to commit
579
+ act(() => {
580
+ result1.current.commitDraft();
581
+ });
582
+ await waitFor(() => {
583
+ expect(mockNavigateTo).toHaveBeenCalled();
584
+ });
585
+ // Get the params that were committed to URL
586
+ const committedParams = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1][1];
587
+ // Simulate page reload by updating mockLocation.search with committed values
588
+ const queryString = new URLSearchParams({
589
+ expression: committedParams.expression,
590
+ from: committedParams.from,
591
+ to: committedParams.to,
592
+ time_selection: committedParams.time_selection,
593
+ sum_by: committedParams.sum_by,
594
+ }).toString();
595
+ mockLocation.search = `?${queryString}`;
596
+ // Unmount the old hook instance
597
+ unmount();
598
+ // Clear navigation mock to verify no new navigation on reload
599
+ mockNavigateTo.mockClear();
600
+ // Create new hook instance (simulating page reload)
601
+ const { result: result2 } = renderHook(() => useQueryState(), { wrapper: createWrapper() });
602
+ // Verify state is loaded from URL after "reload"
603
+ expect(result2.current.querySelection.expression).toBe('memory:inuse_space:bytes:space:bytes{}');
604
+ expect(result2.current.querySelection.from).toBe(5000);
605
+ expect(result2.current.querySelection.to).toBe(6000);
606
+ expect(result2.current.querySelection.timeSelection).toBe('relative:minute|15');
607
+ expect(result2.current.querySelection.sumBy).toEqual(['namespace', 'pod']);
608
+ // Draft should be synced with URL state on page load
609
+ expect(result2.current.draftSelection.expression).toBe('memory:inuse_space:bytes:space:bytes{}');
610
+ expect(result2.current.draftSelection.from).toBe(5000);
611
+ expect(result2.current.draftSelection.to).toBe(6000);
612
+ expect(result2.current.draftSelection.sumBy).toEqual(['namespace', 'pod']);
613
+ // No navigation should occur on page load
614
+ expect(mockNavigateTo).not.toHaveBeenCalled();
615
+ });
616
+ it('should preserve delta profile merge params after reload', async () => {
617
+ // Initial state with delta profile
618
+ mockLocation.search =
619
+ '?expression=process_cpu:cpu:nanoseconds:cpu:nanoseconds:delta{}&from=1000&to=2000';
620
+ const { result: result1, unmount } = renderHook(() => useQueryState(), {
621
+ wrapper: createWrapper(),
622
+ });
623
+ // Commit with time override
624
+ act(() => {
625
+ result1.current.commitDraft({
626
+ from: 5000,
627
+ to: 6000,
628
+ timeSelection: 'relative:minute|5',
629
+ });
630
+ });
631
+ await waitFor(() => {
632
+ expect(mockNavigateTo).toHaveBeenCalled();
633
+ });
634
+ const committedParams = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1][1];
635
+ // Verify merge params were set
636
+ expect(committedParams.merge_from).toBe('5000000000');
637
+ expect(committedParams.merge_to).toBe('6000000000');
638
+ // Simulate page reload with all params including merge params
639
+ const queryString = new URLSearchParams({
640
+ expression: committedParams.expression,
641
+ from: committedParams.from,
642
+ to: committedParams.to,
643
+ time_selection: committedParams.time_selection,
644
+ merge_from: committedParams.merge_from,
645
+ merge_to: committedParams.merge_to,
646
+ }).toString();
647
+ mockLocation.search = `?${queryString}`;
648
+ unmount();
649
+ mockNavigateTo.mockClear();
650
+ // Create new hook instance
651
+ const { result: result2 } = renderHook(() => useQueryState(), { wrapper: createWrapper() });
652
+ // Verify merge params are preserved
653
+ expect(result2.current.querySelection.mergeFrom).toBe('5000000000');
654
+ expect(result2.current.querySelection.mergeTo).toBe('6000000000');
655
+ // Draft should also have merge params
656
+ expect(result2.current.draftSelection.mergeFrom).toBe('5000000000');
657
+ expect(result2.current.draftSelection.mergeTo).toBe('6000000000');
658
+ });
659
+ });
660
+ describe('ProfileSelection state management', () => {
661
+ it('should initialize with null ProfileSelection when no URL params exist', () => {
662
+ const { result } = renderHook(() => useQueryState({ suffix: '_a' }), { wrapper: createWrapper() });
663
+ expect(result.current.profileSelection).toBeNull();
664
+ });
665
+ it('should compute ProfileSelection from URL params', () => {
666
+ // Set URL with ProfileSelection params - using valid profile type
667
+ mockLocation.search =
668
+ '?merge_from_a=1234567890&merge_to_a=9876543210&selection_a=process_cpu:cpu:nanoseconds:cpu:nanoseconds:delta{pod="test"}';
669
+ const { result } = renderHook(() => useQueryState({ suffix: '_a' }), { wrapper: createWrapper() });
670
+ const { profileSelection } = result.current;
671
+ expect(profileSelection).not.toBeNull();
672
+ // Test using the interface methods
673
+ expect(profileSelection?.Type()).toBe('merge');
674
+ expect(profileSelection?.ProfileName()).toBe('process_cpu:cpu:nanoseconds:cpu:nanoseconds:delta');
675
+ // Test HistoryParams which should return merge params
676
+ const historyParams = profileSelection?.HistoryParams();
677
+ expect(historyParams?.merge_from).toBe('1234567890');
678
+ expect(historyParams?.merge_to).toBe('9876543210');
679
+ expect(historyParams?.selection).toBe('process_cpu:cpu:nanoseconds:cpu:nanoseconds:delta{pod="test"}');
680
+ });
681
+ it('should auto-commit ProfileSelection to URL when setProfileSelection called', async () => {
682
+ const { result } = renderHook(() => useQueryState({ suffix: '_a' }), { wrapper: createWrapper() });
683
+ const mergeFrom = BigInt(5000000000);
684
+ const mergeTo = BigInt(6000000000);
685
+ // Create a mock Query object - in real code, this would be Query.parse()
686
+ const mockQuery = {
687
+ toString: () => 'memory:inuse_space:bytes:space:bytes{namespace="default"}',
688
+ profileType: () => ({ delta: false }),
689
+ };
690
+ act(() => {
691
+ result.current.setProfileSelection(mergeFrom, mergeTo, mockQuery);
692
+ });
693
+ await waitFor(() => {
694
+ expect(mockNavigateTo).toHaveBeenCalled();
695
+ const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
696
+ expect(params.selection_a).toBe('memory:inuse_space:bytes:space:bytes{namespace="default"}');
697
+ expect(params.merge_from_a).toBe('5000000000');
698
+ expect(params.merge_to_a).toBe('6000000000');
699
+ });
700
+ });
701
+ it('should use correct suffix for ProfileSelection in comparison mode', async () => {
702
+ const { result: resultB } = renderHook(() => useQueryState({ suffix: '_b' }), {
703
+ wrapper: createWrapper(),
704
+ });
705
+ const mergeFrom = BigInt(7000000000);
706
+ const mergeTo = BigInt(8000000000);
707
+ const mockQuery = {
708
+ toString: () => 'process_cpu:cpu:nanoseconds:cpu:nanoseconds{job="test"}',
709
+ profileType: () => ({ delta: false }),
710
+ };
711
+ act(() => {
712
+ resultB.current.setProfileSelection(mergeFrom, mergeTo, mockQuery);
713
+ });
714
+ await waitFor(() => {
715
+ expect(mockNavigateTo).toHaveBeenCalled();
716
+ const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
717
+ expect(params.selection_b).toBe('process_cpu:cpu:nanoseconds:cpu:nanoseconds{job="test"}');
718
+ expect(params.merge_from_b).toBe('7000000000');
719
+ expect(params.merge_to_b).toBe('8000000000');
720
+ });
721
+ });
722
+ it('should clear ProfileSelection when commitDraft is called', async () => {
723
+ // Start with a ProfileSelection in URL - using valid profile type
724
+ mockLocation.search =
725
+ '?expression_a=process_cpu:cpu:nanoseconds:cpu:nanoseconds{}&merge_from_a=1000000000&merge_to_a=2000000000&selection_a=process_cpu:cpu:nanoseconds:cpu:nanoseconds{pod="test"}';
726
+ const { result } = renderHook(() => useQueryState({ suffix: '_a' }), { wrapper: createWrapper() });
727
+ // Verify ProfileSelection exists
728
+ expect(result.current.profileSelection).not.toBeNull();
729
+ // Make a change to trigger commit
730
+ act(() => {
731
+ result.current.setDraftExpression('memory:inuse_space:bytes:space:bytes{}');
732
+ });
733
+ // Commit the draft (this should clear ProfileSelection as per design decision 4.B)
734
+ act(() => {
735
+ result.current.commitDraft();
736
+ });
737
+ await waitFor(() => {
738
+ expect(mockNavigateTo).toHaveBeenCalled();
739
+ const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
740
+ // ProfileSelection params should be cleared
741
+ expect(params).not.toHaveProperty('selection_a');
742
+ // But QuerySelection params should still be present
743
+ expect(params.expression_a).toBe('memory:inuse_space:bytes:space:bytes{}');
744
+ });
745
+ });
746
+ it('should handle ProfileSelection with delta profiles correctly', () => {
747
+ mockLocation.search =
748
+ '?merge_from_a=1000000000&merge_to_a=2000000000&selection_a=process_cpu:cpu:nanoseconds:cpu:nanoseconds:delta{node="worker"}';
749
+ const { result } = renderHook(() => useQueryState({ suffix: '_a' }), { wrapper: createWrapper() });
750
+ const { profileSelection } = result.current;
751
+ expect(profileSelection).not.toBeNull();
752
+ // Test that ProfileSelection recognizes delta profile type
753
+ expect(profileSelection?.ProfileName()).toBe('process_cpu:cpu:nanoseconds:cpu:nanoseconds:delta');
754
+ // Test HistoryParams
755
+ const historyParams = profileSelection?.HistoryParams();
756
+ expect(historyParams?.merge_from).toBe('1000000000');
757
+ expect(historyParams?.merge_to).toBe('2000000000');
758
+ });
759
+ it('should persist ProfileSelection across page reloads', async () => {
760
+ // Initial state - user clicks on metrics graph point
761
+ const { result: result1, unmount } = renderHook(() => useQueryState({ suffix: '_a' }), {
762
+ wrapper: createWrapper(),
763
+ });
764
+ const mergeFrom = BigInt(3000000000);
765
+ const mergeTo = BigInt(4000000000);
766
+ const mockQuery = {
767
+ toString: () => 'memory:alloc_objects:count:space:bytes{pod="test"}',
768
+ profileType: () => ({ delta: false }),
769
+ };
770
+ // Set ProfileSelection
771
+ act(() => {
772
+ result1.current.setProfileSelection(mergeFrom, mergeTo, mockQuery);
773
+ });
774
+ await waitFor(() => {
775
+ expect(mockNavigateTo).toHaveBeenCalled();
776
+ });
777
+ const committedParams = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1][1];
778
+ // Simulate page reload by updating mockLocation.search
779
+ const selectionA = String(committedParams.selection_a ?? '');
780
+ const mergeFromA = String(committedParams.merge_from_a ?? '');
781
+ const mergeToA = String(committedParams.merge_to_a ?? '');
782
+ mockLocation.search = `?selection_a=${encodeURIComponent(selectionA)}&merge_from_a=${mergeFromA}&merge_to_a=${mergeToA}`;
783
+ unmount();
784
+ mockNavigateTo.mockClear();
785
+ // Create new hook instance (simulating page reload)
786
+ const { result: result2 } = renderHook(() => useQueryState({ suffix: '_a' }), {
787
+ wrapper: createWrapper(),
788
+ });
789
+ // Verify ProfileSelection is loaded from URL after reload
790
+ const profileSelection = result2.current.profileSelection;
791
+ expect(profileSelection).not.toBeNull();
792
+ // Use interface methods to test
793
+ expect(profileSelection?.Type()).toBe('merge');
794
+ const historyParams = profileSelection?.HistoryParams();
795
+ expect(historyParams?.merge_from).toBe('3000000000');
796
+ expect(historyParams?.merge_to).toBe('4000000000');
797
+ expect(historyParams?.selection).toBe('memory:alloc_objects:count:space:bytes{pod="test"}');
798
+ // No navigation should occur on page load
799
+ expect(mockNavigateTo).not.toHaveBeenCalled();
800
+ });
801
+ it('should handle independent ProfileSelection for both sides in comparison mode', async () => {
802
+ // Test component using both hooks with the same URLStateProvider (real-world scenario)
803
+ // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
804
+ const TestComponent = () => {
805
+ const stateA = useQueryState({ suffix: '_a' });
806
+ const stateB = useQueryState({ suffix: '_b' });
807
+ return { stateA, stateB };
808
+ };
809
+ const { result } = renderHook(() => TestComponent(), {
810
+ wrapper: createWrapper(),
811
+ });
812
+ const mockQueryA = {
813
+ toString: () => 'process_cpu:cpu:nanoseconds:cpu:nanoseconds{pod="app-a"}',
814
+ profileType: () => ({ delta: false }),
815
+ };
816
+ const mockQueryB = {
817
+ toString: () => 'process_cpu:cpu:nanoseconds:cpu:nanoseconds{pod="app-b"}',
818
+ profileType: () => ({ delta: false }),
819
+ };
820
+ // Set ProfileSelection for side A
821
+ act(() => {
822
+ result.current.stateA.setProfileSelection(BigInt(1000000000), BigInt(2000000000), mockQueryA);
823
+ });
824
+ await waitFor(() => {
825
+ expect(mockNavigateTo).toHaveBeenCalled();
826
+ });
827
+ mockNavigateTo.mockClear();
828
+ // Set ProfileSelection for side B
829
+ act(() => {
830
+ result.current.stateB.setProfileSelection(BigInt(3000000000), BigInt(4000000000), mockQueryB);
831
+ });
832
+ await waitFor(() => {
833
+ expect(mockNavigateTo).toHaveBeenCalled();
834
+ const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
835
+ // Both selections should be in URL with different suffixes
836
+ expect(params.selection_a).toBe('process_cpu:cpu:nanoseconds:cpu:nanoseconds{pod="app-a"}');
837
+ expect(params.selection_b).toBe('process_cpu:cpu:nanoseconds:cpu:nanoseconds{pod="app-b"}');
838
+ expect(params.merge_from_a).toBe('1000000000');
839
+ expect(params.merge_from_b).toBe('3000000000');
840
+ });
841
+ // The mockNavigateTo automatically updates mockLocation.search, so the URL change
842
+ // should propagate to the hooks automatically. Verify both ProfileSelections exist.
843
+ await waitFor(() => {
844
+ expect(result.current.stateA.profileSelection).not.toBeNull();
845
+ expect(result.current.stateB.profileSelection).not.toBeNull();
846
+ });
847
+ });
848
+ it('should return null ProfileSelection when only partial params exist', () => {
849
+ // Missing selection param
850
+ mockLocation.search = '?merge_from_a=1000000000&merge_to_a=2000000000';
851
+ const { result } = renderHook(() => useQueryState({ suffix: '_a' }), { wrapper: createWrapper() });
852
+ expect(result.current.profileSelection).toBeNull();
853
+ });
854
+ it('should handle ProfileSelection with complex query expressions', async () => {
855
+ const { result } = renderHook(() => useQueryState({ suffix: '_a' }), { wrapper: createWrapper() });
856
+ const mockQuery = {
857
+ toString: () => 'memory:alloc_objects:count:space:bytes:delta{namespace="default",pod="app-1",container="main"}',
858
+ profileType: () => ({ delta: true }),
859
+ };
860
+ act(() => {
861
+ result.current.setProfileSelection(BigInt(5000000000), BigInt(6000000000), mockQuery);
862
+ });
863
+ await waitFor(() => {
864
+ expect(mockNavigateTo).toHaveBeenCalled();
865
+ const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
866
+ expect(params.selection_a).toBe('memory:alloc_objects:count:space:bytes:delta{namespace="default",pod="app-1",container="main"}');
867
+ });
868
+ });
869
+ it('should batch ProfileSelection update with other URL state changes', async () => {
870
+ const { result } = renderHook(() => useQueryState({ suffix: '_a' }), { wrapper: createWrapper() });
871
+ const mockQuery = {
872
+ toString: () => 'process_cpu:cpu:nanoseconds:cpu:nanoseconds{job="test"}',
873
+ profileType: () => ({ delta: false }),
874
+ };
875
+ // The batchUpdates is used internally by setProfileSelection
876
+ act(() => {
877
+ result.current.setProfileSelection(BigInt(1000000000), BigInt(2000000000), mockQuery);
878
+ });
879
+ await waitFor(() => {
880
+ // Should only navigate once despite setting 3 params (selection, merge_from, merge_to)
881
+ expect(mockNavigateTo).toHaveBeenCalledTimes(1);
882
+ const [, params] = mockNavigateTo.mock.calls[0];
883
+ expect(params.selection_a).toBe('process_cpu:cpu:nanoseconds:cpu:nanoseconds{job="test"}');
884
+ expect(params.merge_from_a).toBe('1000000000');
885
+ expect(params.merge_to_a).toBe('2000000000');
886
+ });
887
+ });
888
+ it('should preserve other URL params when setting ProfileSelection', async () => {
889
+ mockLocation.search = '?expression_a=process_cpu{}&other_param=value&unrelated=test';
890
+ const { result } = renderHook(() => useQueryState({ suffix: '_a' }), { wrapper: createWrapper() });
891
+ const mockQuery = {
892
+ toString: () => 'process_cpu:cpu:nanoseconds:cpu:nanoseconds{pod="test"}',
893
+ profileType: () => ({ delta: false }),
894
+ };
895
+ act(() => {
896
+ result.current.setProfileSelection(BigInt(1000000000), BigInt(2000000000), mockQuery);
897
+ });
898
+ await waitFor(() => {
899
+ expect(mockNavigateTo).toHaveBeenCalled();
900
+ const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
901
+ // ProfileSelection params should be set
902
+ expect(params.selection_a).toBe('process_cpu:cpu:nanoseconds:cpu:nanoseconds{pod="test"}');
903
+ // Other params should be preserved
904
+ expect(params.expression_a).toBe('process_cpu{}');
905
+ expect(params.other_param).toBe('value');
906
+ expect(params.unrelated).toBe('test');
907
+ });
908
+ });
909
+ });
910
+ });