@parca/profile 0.19.109 → 0.19.110
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/CHANGELOG.md +4 -0
- package/dist/ProfileSelector/index.d.ts.map +1 -1
- package/dist/ProfileSelector/index.js +9 -1
- package/dist/ProfileSelector/useAutoQuerySelector.js +1 -1
- package/dist/ProfileView/components/ProfileFilters/useProfileFilters.d.ts +2 -0
- package/dist/ProfileView/components/ProfileFilters/useProfileFilters.d.ts.map +1 -1
- package/dist/ProfileView/components/ProfileFilters/useProfileFilters.js +3 -1
- package/dist/ProfileView/components/ProfileFilters/useProfileFiltersUrlState.d.ts +1 -0
- package/dist/ProfileView/components/ProfileFilters/useProfileFiltersUrlState.d.ts.map +1 -1
- package/dist/ProfileView/components/ProfileFilters/useProfileFiltersUrlState.js +17 -3
- package/dist/ProfileView/components/ProfileFilters/useProfileFiltersUrlState.test.d.ts +2 -0
- package/dist/ProfileView/components/ProfileFilters/useProfileFiltersUrlState.test.d.ts.map +1 -0
- package/dist/ProfileView/components/ProfileFilters/useProfileFiltersUrlState.test.js +541 -0
- package/dist/QueryControls/index.d.ts.map +1 -1
- package/dist/QueryControls/index.js +1 -3
- package/dist/hooks/useLabels.d.ts.map +1 -1
- package/dist/hooks/useLabels.js +0 -7
- package/dist/hooks/useQueryState.d.ts +4 -0
- package/dist/hooks/useQueryState.d.ts.map +1 -1
- package/dist/hooks/useQueryState.js +29 -5
- package/dist/hooks/useQueryState.test.js +72 -8
- package/dist/index.d.ts +2 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/package.json +3 -3
- package/src/ProfileSelector/index.tsx +14 -1
- package/src/ProfileSelector/useAutoQuerySelector.ts +1 -1
- package/src/ProfileView/components/ProfileFilters/useProfileFilters.ts +5 -1
- package/src/ProfileView/components/ProfileFilters/useProfileFiltersUrlState.test.tsx +663 -0
- package/src/ProfileView/components/ProfileFilters/useProfileFiltersUrlState.ts +24 -3
- package/src/QueryControls/index.tsx +1 -3
- package/src/hooks/useLabels.ts +0 -10
- package/src/hooks/useQueryState.test.tsx +90 -11
- package/src/hooks/useQueryState.ts +36 -4
- package/src/index.tsx +4 -0
|
@@ -0,0 +1,663 @@
|
|
|
1
|
+
// Copyright 2022 The Parca Authors
|
|
2
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
+
// you may not use this file except in compliance with the License.
|
|
4
|
+
// You may obtain a copy of the License at
|
|
5
|
+
//
|
|
6
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
//
|
|
8
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
// See the License for the specific language governing permissions and
|
|
12
|
+
// limitations under the License.
|
|
13
|
+
|
|
14
|
+
import {type ReactNode} from 'react';
|
|
15
|
+
|
|
16
|
+
// eslint-disable-next-line import/named
|
|
17
|
+
import {act, renderHook, waitFor} from '@testing-library/react';
|
|
18
|
+
import {beforeEach, describe, expect, it, vi} from 'vitest';
|
|
19
|
+
|
|
20
|
+
import {URLStateProvider} from '@parca/components';
|
|
21
|
+
|
|
22
|
+
import {type ProfileFilter} from './useProfileFilters';
|
|
23
|
+
import {decodeProfileFilters, useProfileFiltersUrlState} from './useProfileFiltersUrlState';
|
|
24
|
+
|
|
25
|
+
// Mock window.location
|
|
26
|
+
const mockLocation = {
|
|
27
|
+
pathname: '/test',
|
|
28
|
+
search: '',
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
// Mock the navigate function
|
|
32
|
+
const mockNavigateTo = vi.fn((path: string, params: Record<string, string | string[]>) => {
|
|
33
|
+
const searchParams = new URLSearchParams();
|
|
34
|
+
Object.entries(params).forEach(([key, value]) => {
|
|
35
|
+
if (value !== undefined && value !== null) {
|
|
36
|
+
if (Array.isArray(value)) {
|
|
37
|
+
searchParams.set(key, value.join(','));
|
|
38
|
+
} else {
|
|
39
|
+
searchParams.set(key, String(value));
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
mockLocation.search = `?${searchParams.toString()}`;
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
// Mock getQueryParamsFromURL
|
|
47
|
+
vi.mock('@parca/components/src/hooks/URLState/utils', async () => {
|
|
48
|
+
const actual = await vi.importActual('@parca/components/src/hooks/URLState/utils');
|
|
49
|
+
return {
|
|
50
|
+
...actual,
|
|
51
|
+
getQueryParamsFromURL: () => {
|
|
52
|
+
if (mockLocation.search === '') return {};
|
|
53
|
+
const params = new URLSearchParams(mockLocation.search);
|
|
54
|
+
const result: Record<string, string | string[]> = {};
|
|
55
|
+
for (const [key, value] of params.entries()) {
|
|
56
|
+
const decodedValue = decodeURIComponent(value);
|
|
57
|
+
const existing = result[key];
|
|
58
|
+
if (existing !== undefined) {
|
|
59
|
+
result[key] = Array.isArray(existing)
|
|
60
|
+
? [...existing, decodedValue]
|
|
61
|
+
: [existing, decodedValue];
|
|
62
|
+
} else {
|
|
63
|
+
result[key] = decodedValue;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return result;
|
|
67
|
+
},
|
|
68
|
+
};
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
// Helper to create wrapper with URLStateProvider
|
|
72
|
+
const createWrapper = (): (({children}: {children: ReactNode}) => JSX.Element) => {
|
|
73
|
+
const Wrapper = ({children}: {children: ReactNode}): JSX.Element => (
|
|
74
|
+
<URLStateProvider navigateTo={mockNavigateTo}>{children}</URLStateProvider>
|
|
75
|
+
);
|
|
76
|
+
Wrapper.displayName = 'URLStateProviderWrapper';
|
|
77
|
+
return Wrapper;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
describe('useProfileFiltersUrlState', () => {
|
|
81
|
+
beforeEach(() => {
|
|
82
|
+
mockNavigateTo.mockClear();
|
|
83
|
+
Object.defineProperty(window, 'location', {
|
|
84
|
+
value: mockLocation,
|
|
85
|
+
writable: true,
|
|
86
|
+
});
|
|
87
|
+
mockLocation.search = '';
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe('decodeProfileFilters', () => {
|
|
91
|
+
it('should return empty array for empty string', () => {
|
|
92
|
+
expect(decodeProfileFilters('')).toEqual([]);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('should return empty array for undefined', () => {
|
|
96
|
+
expect(decodeProfileFilters(undefined as unknown as string)).toEqual([]);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('should decode stack filter with function_name', () => {
|
|
100
|
+
// Format: type:field:match:value -> s:fn:=:testFunc
|
|
101
|
+
const encoded = 's:fn:=:testFunc';
|
|
102
|
+
const result = decodeProfileFilters(encoded);
|
|
103
|
+
|
|
104
|
+
expect(result).toHaveLength(1);
|
|
105
|
+
expect(result[0]).toMatchObject({
|
|
106
|
+
type: 'stack',
|
|
107
|
+
field: 'function_name',
|
|
108
|
+
matchType: 'equal',
|
|
109
|
+
value: 'testFunc',
|
|
110
|
+
});
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it('should decode frame filter with binary', () => {
|
|
114
|
+
const encoded = 'f:b:!=:libc.so';
|
|
115
|
+
const result = decodeProfileFilters(encoded);
|
|
116
|
+
|
|
117
|
+
expect(result).toHaveLength(1);
|
|
118
|
+
expect(result[0]).toMatchObject({
|
|
119
|
+
type: 'frame',
|
|
120
|
+
field: 'binary',
|
|
121
|
+
matchType: 'not_equal',
|
|
122
|
+
value: 'libc.so',
|
|
123
|
+
});
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('should decode filter with contains match', () => {
|
|
127
|
+
const encoded = 's:fn:~:runtime';
|
|
128
|
+
const result = decodeProfileFilters(encoded);
|
|
129
|
+
|
|
130
|
+
expect(result).toHaveLength(1);
|
|
131
|
+
expect(result[0]).toMatchObject({
|
|
132
|
+
type: 'stack',
|
|
133
|
+
field: 'function_name',
|
|
134
|
+
matchType: 'contains',
|
|
135
|
+
value: 'runtime',
|
|
136
|
+
});
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it('should decode filter with not_contains match', () => {
|
|
140
|
+
const encoded = 'f:b:!~:node';
|
|
141
|
+
const result = decodeProfileFilters(encoded);
|
|
142
|
+
|
|
143
|
+
expect(result).toHaveLength(1);
|
|
144
|
+
expect(result[0]).toMatchObject({
|
|
145
|
+
type: 'frame',
|
|
146
|
+
field: 'binary',
|
|
147
|
+
matchType: 'not_contains',
|
|
148
|
+
value: 'node',
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('should decode filter with starts_with match', () => {
|
|
153
|
+
const encoded = 's:fn:^:std::';
|
|
154
|
+
const result = decodeProfileFilters(encoded);
|
|
155
|
+
|
|
156
|
+
expect(result).toHaveLength(1);
|
|
157
|
+
expect(result[0]).toMatchObject({
|
|
158
|
+
type: 'stack',
|
|
159
|
+
field: 'function_name',
|
|
160
|
+
matchType: 'starts_with',
|
|
161
|
+
value: 'std::',
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('should decode filter with not_starts_with match', () => {
|
|
166
|
+
const encoded = 'f:fn:!^:tokio::';
|
|
167
|
+
const result = decodeProfileFilters(encoded);
|
|
168
|
+
|
|
169
|
+
expect(result).toHaveLength(1);
|
|
170
|
+
expect(result[0]).toMatchObject({
|
|
171
|
+
type: 'frame',
|
|
172
|
+
field: 'function_name',
|
|
173
|
+
matchType: 'not_starts_with',
|
|
174
|
+
value: 'tokio::',
|
|
175
|
+
});
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it('should decode multiple filters', () => {
|
|
179
|
+
const encoded = 's:fn:=:testFunc,f:b:!=:libc.so';
|
|
180
|
+
const result = decodeProfileFilters(encoded);
|
|
181
|
+
|
|
182
|
+
expect(result).toHaveLength(2);
|
|
183
|
+
expect(result[0]).toMatchObject({
|
|
184
|
+
type: 'stack',
|
|
185
|
+
field: 'function_name',
|
|
186
|
+
matchType: 'equal',
|
|
187
|
+
value: 'testFunc',
|
|
188
|
+
});
|
|
189
|
+
expect(result[1]).toMatchObject({
|
|
190
|
+
type: 'frame',
|
|
191
|
+
field: 'binary',
|
|
192
|
+
matchType: 'not_equal',
|
|
193
|
+
value: 'libc.so',
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('should decode preset filter', () => {
|
|
198
|
+
const encoded = 'p:hide_libc:enabled';
|
|
199
|
+
const result = decodeProfileFilters(encoded);
|
|
200
|
+
|
|
201
|
+
expect(result).toHaveLength(1);
|
|
202
|
+
expect(result[0]).toMatchObject({
|
|
203
|
+
type: 'hide_libc',
|
|
204
|
+
value: 'enabled',
|
|
205
|
+
});
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it('should handle values with colons', () => {
|
|
209
|
+
const encoded = 'p:some_preset:value:with:colons';
|
|
210
|
+
const result = decodeProfileFilters(encoded);
|
|
211
|
+
|
|
212
|
+
expect(result).toHaveLength(1);
|
|
213
|
+
expect(result[0]).toMatchObject({
|
|
214
|
+
type: 'some_preset',
|
|
215
|
+
value: 'value:with:colons',
|
|
216
|
+
});
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('should decode all field types', () => {
|
|
220
|
+
const testCases = [
|
|
221
|
+
{encoded: 's:fn:=:test', expectedField: 'function_name'},
|
|
222
|
+
{encoded: 's:b:=:test', expectedField: 'binary'},
|
|
223
|
+
{encoded: 's:sn:=:test', expectedField: 'system_name'},
|
|
224
|
+
{encoded: 's:f:=:test', expectedField: 'filename'},
|
|
225
|
+
{encoded: 's:a:=:test', expectedField: 'address'},
|
|
226
|
+
{encoded: 's:ln:=:test', expectedField: 'line_number'},
|
|
227
|
+
];
|
|
228
|
+
|
|
229
|
+
for (const {encoded, expectedField} of testCases) {
|
|
230
|
+
const result = decodeProfileFilters(encoded);
|
|
231
|
+
expect(result[0].field).toBe(expectedField);
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
it('should return empty array for malformed input', () => {
|
|
236
|
+
// This should not throw - it returns empty array on error
|
|
237
|
+
expect(() => decodeProfileFilters('malformed')).not.toThrow();
|
|
238
|
+
});
|
|
239
|
+
|
|
240
|
+
it('should generate unique IDs for each filter', () => {
|
|
241
|
+
const encoded = 's:fn:=:func1,s:fn:=:func2,s:fn:=:func3';
|
|
242
|
+
const result = decodeProfileFilters(encoded);
|
|
243
|
+
|
|
244
|
+
const ids = result.map(f => f.id);
|
|
245
|
+
const uniqueIds = new Set(ids);
|
|
246
|
+
expect(uniqueIds.size).toBe(ids.length);
|
|
247
|
+
});
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
describe('Basic functionality', () => {
|
|
251
|
+
it('should initialize with empty filters when no URL params', () => {
|
|
252
|
+
const {result} = renderHook(() => useProfileFiltersUrlState(), {wrapper: createWrapper()});
|
|
253
|
+
|
|
254
|
+
expect(result.current.appliedFilters).toEqual([]);
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
it('should read filters from URL', async () => {
|
|
258
|
+
mockLocation.search = '?profile_filters=s:fn:=:testFunc';
|
|
259
|
+
|
|
260
|
+
const {result} = renderHook(() => useProfileFiltersUrlState(), {wrapper: createWrapper()});
|
|
261
|
+
|
|
262
|
+
await waitFor(() => {
|
|
263
|
+
expect(result.current.appliedFilters).toHaveLength(1);
|
|
264
|
+
expect(result.current.appliedFilters[0]).toMatchObject({
|
|
265
|
+
type: 'stack',
|
|
266
|
+
field: 'function_name',
|
|
267
|
+
matchType: 'equal',
|
|
268
|
+
value: 'testFunc',
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
it('should update URL when setting filters', async () => {
|
|
274
|
+
const {result} = renderHook(() => useProfileFiltersUrlState(), {wrapper: createWrapper()});
|
|
275
|
+
|
|
276
|
+
const newFilters: ProfileFilter[] = [
|
|
277
|
+
{
|
|
278
|
+
id: 'test-1',
|
|
279
|
+
type: 'frame',
|
|
280
|
+
field: 'binary',
|
|
281
|
+
matchType: 'not_contains',
|
|
282
|
+
value: 'libc.so',
|
|
283
|
+
},
|
|
284
|
+
];
|
|
285
|
+
|
|
286
|
+
act(() => {
|
|
287
|
+
result.current.setAppliedFilters(newFilters);
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
await waitFor(() => {
|
|
291
|
+
expect(mockNavigateTo).toHaveBeenCalled();
|
|
292
|
+
const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
|
|
293
|
+
expect(params.profile_filters).toBe('f:b:!~:libc.so');
|
|
294
|
+
});
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
it('should clear URL param when setting empty filters', async () => {
|
|
298
|
+
mockLocation.search = '?profile_filters=s:fn:=:testFunc';
|
|
299
|
+
|
|
300
|
+
const {result} = renderHook(() => useProfileFiltersUrlState(), {wrapper: createWrapper()});
|
|
301
|
+
|
|
302
|
+
act(() => {
|
|
303
|
+
result.current.setAppliedFilters([]);
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
await waitFor(() => {
|
|
307
|
+
expect(mockNavigateTo).toHaveBeenCalled();
|
|
308
|
+
const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
|
|
309
|
+
// When filters are empty, the param is either empty string or undefined (removed)
|
|
310
|
+
expect(params.profile_filters === '' || params.profile_filters === undefined).toBe(true);
|
|
311
|
+
});
|
|
312
|
+
});
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
describe('forceApplyFilters', () => {
|
|
316
|
+
it('should provide forceApplyFilters method', () => {
|
|
317
|
+
const {result} = renderHook(() => useProfileFiltersUrlState(), {wrapper: createWrapper()});
|
|
318
|
+
|
|
319
|
+
expect(typeof result.current.forceApplyFilters).toBe('function');
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
it('should force apply filters overwriting existing', async () => {
|
|
323
|
+
mockLocation.search = '?profile_filters=s:fn:=:existingFunc';
|
|
324
|
+
|
|
325
|
+
const {result} = renderHook(() => useProfileFiltersUrlState(), {wrapper: createWrapper()});
|
|
326
|
+
|
|
327
|
+
// Verify existing filter is loaded
|
|
328
|
+
await waitFor(() => {
|
|
329
|
+
expect(result.current.appliedFilters).toHaveLength(1);
|
|
330
|
+
});
|
|
331
|
+
|
|
332
|
+
const newFilters: ProfileFilter[] = [
|
|
333
|
+
{
|
|
334
|
+
id: 'forced-1',
|
|
335
|
+
type: 'frame',
|
|
336
|
+
field: 'binary',
|
|
337
|
+
matchType: 'not_contains',
|
|
338
|
+
value: 'forcedValue',
|
|
339
|
+
},
|
|
340
|
+
];
|
|
341
|
+
|
|
342
|
+
act(() => {
|
|
343
|
+
result.current.forceApplyFilters(newFilters);
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
await waitFor(() => {
|
|
347
|
+
expect(mockNavigateTo).toHaveBeenCalled();
|
|
348
|
+
const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
|
|
349
|
+
expect(params.profile_filters).toBe('f:b:!~:forcedValue');
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
it('should clear filters when force applying empty array', async () => {
|
|
354
|
+
mockLocation.search = '?profile_filters=s:fn:=:existingFunc';
|
|
355
|
+
|
|
356
|
+
const {result} = renderHook(() => useProfileFiltersUrlState(), {wrapper: createWrapper()});
|
|
357
|
+
|
|
358
|
+
act(() => {
|
|
359
|
+
result.current.forceApplyFilters([]);
|
|
360
|
+
});
|
|
361
|
+
|
|
362
|
+
await waitFor(() => {
|
|
363
|
+
expect(mockNavigateTo).toHaveBeenCalled();
|
|
364
|
+
const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
|
|
365
|
+
// When filters are empty, the param is either empty string or undefined (removed)
|
|
366
|
+
expect(params.profile_filters === '' || params.profile_filters === undefined).toBe(true);
|
|
367
|
+
});
|
|
368
|
+
});
|
|
369
|
+
});
|
|
370
|
+
|
|
371
|
+
describe('Preset filter encoding', () => {
|
|
372
|
+
it('should encode preset filters correctly', async () => {
|
|
373
|
+
const {result} = renderHook(() => useProfileFiltersUrlState(), {wrapper: createWrapper()});
|
|
374
|
+
|
|
375
|
+
const presetFilters: ProfileFilter[] = [
|
|
376
|
+
{
|
|
377
|
+
id: 'preset-1',
|
|
378
|
+
type: 'hide_libc',
|
|
379
|
+
value: 'enabled',
|
|
380
|
+
},
|
|
381
|
+
];
|
|
382
|
+
|
|
383
|
+
act(() => {
|
|
384
|
+
result.current.setAppliedFilters(presetFilters);
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
await waitFor(() => {
|
|
388
|
+
expect(mockNavigateTo).toHaveBeenCalled();
|
|
389
|
+
const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
|
|
390
|
+
expect(params.profile_filters).toBe('p:hide_libc:enabled');
|
|
391
|
+
});
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
it('should handle mixed preset and regular filters', async () => {
|
|
395
|
+
const {result} = renderHook(() => useProfileFiltersUrlState(), {wrapper: createWrapper()});
|
|
396
|
+
|
|
397
|
+
const mixedFilters: ProfileFilter[] = [
|
|
398
|
+
{
|
|
399
|
+
id: 'preset-1',
|
|
400
|
+
type: 'hide_libc',
|
|
401
|
+
value: 'enabled',
|
|
402
|
+
},
|
|
403
|
+
{
|
|
404
|
+
id: 'regular-1',
|
|
405
|
+
type: 'frame',
|
|
406
|
+
field: 'binary',
|
|
407
|
+
matchType: 'not_contains',
|
|
408
|
+
value: 'node',
|
|
409
|
+
},
|
|
410
|
+
];
|
|
411
|
+
|
|
412
|
+
act(() => {
|
|
413
|
+
result.current.setAppliedFilters(mixedFilters);
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
await waitFor(() => {
|
|
417
|
+
expect(mockNavigateTo).toHaveBeenCalled();
|
|
418
|
+
const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
|
|
419
|
+
expect(params.profile_filters).toBe('p:hide_libc:enabled,f:b:!~:node');
|
|
420
|
+
});
|
|
421
|
+
});
|
|
422
|
+
});
|
|
423
|
+
|
|
424
|
+
describe('URL encoding edge cases', () => {
|
|
425
|
+
it('should handle special characters in filter values', async () => {
|
|
426
|
+
const {result} = renderHook(() => useProfileFiltersUrlState(), {wrapper: createWrapper()});
|
|
427
|
+
|
|
428
|
+
const filtersWithSpecialChars: ProfileFilter[] = [
|
|
429
|
+
{
|
|
430
|
+
id: 'special-1',
|
|
431
|
+
type: 'stack',
|
|
432
|
+
field: 'function_name',
|
|
433
|
+
matchType: 'contains',
|
|
434
|
+
value: 'std::vector<int>',
|
|
435
|
+
},
|
|
436
|
+
];
|
|
437
|
+
|
|
438
|
+
act(() => {
|
|
439
|
+
result.current.setAppliedFilters(filtersWithSpecialChars);
|
|
440
|
+
});
|
|
441
|
+
|
|
442
|
+
await waitFor(() => {
|
|
443
|
+
expect(mockNavigateTo).toHaveBeenCalled();
|
|
444
|
+
const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
|
|
445
|
+
// Value should be URL encoded
|
|
446
|
+
expect(params.profile_filters).toContain('std%3A%3Avector%3Cint%3E');
|
|
447
|
+
});
|
|
448
|
+
});
|
|
449
|
+
|
|
450
|
+
it('should filter out incomplete filters when encoding', async () => {
|
|
451
|
+
const {result} = renderHook(() => useProfileFiltersUrlState(), {wrapper: createWrapper()});
|
|
452
|
+
|
|
453
|
+
const incompleteFilters: ProfileFilter[] = [
|
|
454
|
+
{
|
|
455
|
+
id: 'complete-1',
|
|
456
|
+
type: 'frame',
|
|
457
|
+
field: 'binary',
|
|
458
|
+
matchType: 'not_contains',
|
|
459
|
+
value: 'valid',
|
|
460
|
+
},
|
|
461
|
+
{
|
|
462
|
+
id: 'incomplete-1',
|
|
463
|
+
type: 'frame',
|
|
464
|
+
// Missing field, matchType
|
|
465
|
+
value: '',
|
|
466
|
+
},
|
|
467
|
+
{
|
|
468
|
+
id: 'incomplete-2',
|
|
469
|
+
type: undefined,
|
|
470
|
+
value: 'value',
|
|
471
|
+
},
|
|
472
|
+
];
|
|
473
|
+
|
|
474
|
+
act(() => {
|
|
475
|
+
result.current.setAppliedFilters(incompleteFilters);
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
await waitFor(() => {
|
|
479
|
+
expect(mockNavigateTo).toHaveBeenCalled();
|
|
480
|
+
const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
|
|
481
|
+
// Only the complete filter should be encoded
|
|
482
|
+
expect(params.profile_filters).toBe('f:b:!~:valid');
|
|
483
|
+
});
|
|
484
|
+
});
|
|
485
|
+
});
|
|
486
|
+
|
|
487
|
+
describe('Memoization', () => {
|
|
488
|
+
it('should return empty array with consistent structure when no filters', () => {
|
|
489
|
+
const {result} = renderHook(() => useProfileFiltersUrlState(), {wrapper: createWrapper()});
|
|
490
|
+
|
|
491
|
+
// Empty filters should be an empty array (not undefined or null)
|
|
492
|
+
expect(Array.isArray(result.current.appliedFilters)).toBe(true);
|
|
493
|
+
expect(result.current.appliedFilters).toHaveLength(0);
|
|
494
|
+
});
|
|
495
|
+
|
|
496
|
+
it('should always return array (never undefined)', () => {
|
|
497
|
+
const {result} = renderHook(() => useProfileFiltersUrlState(), {wrapper: createWrapper()});
|
|
498
|
+
|
|
499
|
+
expect(Array.isArray(result.current.appliedFilters)).toBe(true);
|
|
500
|
+
expect(result.current.appliedFilters).toEqual([]);
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
it('should return correctly structured filters from URL', async () => {
|
|
504
|
+
mockLocation.search = '?profile_filters=s:fn:=:testFunc';
|
|
505
|
+
|
|
506
|
+
const {result} = renderHook(() => useProfileFiltersUrlState(), {wrapper: createWrapper()});
|
|
507
|
+
|
|
508
|
+
await waitFor(() => {
|
|
509
|
+
expect(result.current.appliedFilters).toHaveLength(1);
|
|
510
|
+
});
|
|
511
|
+
|
|
512
|
+
// Verify the filter structure is correct
|
|
513
|
+
const filter = result.current.appliedFilters[0];
|
|
514
|
+
expect(filter).toHaveProperty('id');
|
|
515
|
+
expect(filter).toHaveProperty('type', 'stack');
|
|
516
|
+
expect(filter).toHaveProperty('field', 'function_name');
|
|
517
|
+
expect(filter).toHaveProperty('matchType', 'equal');
|
|
518
|
+
// eslint-disable-next-line jest-dom/prefer-to-have-value
|
|
519
|
+
expect(filter).toHaveProperty('value', 'testFunc');
|
|
520
|
+
});
|
|
521
|
+
});
|
|
522
|
+
|
|
523
|
+
describe('View switching scenarios', () => {
|
|
524
|
+
it('should completely replace filters when switching views using forceApplyFilters', async () => {
|
|
525
|
+
// Start with View A's filters (2 filters)
|
|
526
|
+
mockLocation.search = '?profile_filters=s:fn:=:viewAFunc,f:b:!=:viewABinary';
|
|
527
|
+
|
|
528
|
+
const {result} = renderHook(() => useProfileFiltersUrlState(), {wrapper: createWrapper()});
|
|
529
|
+
|
|
530
|
+
await waitFor(() => {
|
|
531
|
+
expect(result.current.appliedFilters).toHaveLength(2);
|
|
532
|
+
expect(result.current.appliedFilters[0].value).toBe('viewAFunc');
|
|
533
|
+
expect(result.current.appliedFilters[1].value).toBe('viewABinary');
|
|
534
|
+
});
|
|
535
|
+
|
|
536
|
+
// Switch to View B (completely different filter)
|
|
537
|
+
const viewBFilters: ProfileFilter[] = [
|
|
538
|
+
{
|
|
539
|
+
id: 'viewB-1',
|
|
540
|
+
type: 'frame',
|
|
541
|
+
field: 'function_name',
|
|
542
|
+
matchType: 'contains',
|
|
543
|
+
value: 'viewBOnly',
|
|
544
|
+
},
|
|
545
|
+
];
|
|
546
|
+
|
|
547
|
+
act(() => {
|
|
548
|
+
result.current.forceApplyFilters(viewBFilters);
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
await waitFor(() => {
|
|
552
|
+
expect(mockNavigateTo).toHaveBeenCalled();
|
|
553
|
+
const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
|
|
554
|
+
|
|
555
|
+
// View A's filters should be completely gone
|
|
556
|
+
expect(params.profile_filters).not.toContain('viewAFunc');
|
|
557
|
+
expect(params.profile_filters).not.toContain('viewABinary');
|
|
558
|
+
|
|
559
|
+
// Only View B's filter should be present
|
|
560
|
+
expect(params.profile_filters).toBe('f:fn:~:viewBOnly');
|
|
561
|
+
});
|
|
562
|
+
});
|
|
563
|
+
|
|
564
|
+
it('should handle sequential view switches correctly', async () => {
|
|
565
|
+
// Simulate: [default] -> [storage] -> [testing-view]
|
|
566
|
+
const {result} = renderHook(() => useProfileFiltersUrlState(), {wrapper: createWrapper()});
|
|
567
|
+
|
|
568
|
+
// View 1: default view (1 filter)
|
|
569
|
+
const defaultFilters: ProfileFilter[] = [{id: 'd-1', type: 'hide_libc', value: 'enabled'}];
|
|
570
|
+
|
|
571
|
+
act(() => {
|
|
572
|
+
result.current.forceApplyFilters(defaultFilters);
|
|
573
|
+
});
|
|
574
|
+
|
|
575
|
+
await waitFor(() => {
|
|
576
|
+
expect(mockNavigateTo).toHaveBeenCalled();
|
|
577
|
+
const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
|
|
578
|
+
expect(params.profile_filters).toBe('p:hide_libc:enabled');
|
|
579
|
+
});
|
|
580
|
+
|
|
581
|
+
mockNavigateTo.mockClear();
|
|
582
|
+
|
|
583
|
+
// View 2: storage view (3 filters)
|
|
584
|
+
const storageFilters: ProfileFilter[] = [
|
|
585
|
+
{id: 's-1', type: 'stack', field: 'function_name', matchType: 'not_contains', value: 'io'},
|
|
586
|
+
{id: 's-2', type: 'frame', field: 'binary', matchType: 'not_contains', value: 'disk'},
|
|
587
|
+
{id: 's-3', type: 'frame', field: 'function_name', matchType: 'contains', value: 'storage'},
|
|
588
|
+
];
|
|
589
|
+
|
|
590
|
+
act(() => {
|
|
591
|
+
result.current.forceApplyFilters(storageFilters);
|
|
592
|
+
});
|
|
593
|
+
|
|
594
|
+
await waitFor(() => {
|
|
595
|
+
expect(mockNavigateTo).toHaveBeenCalled();
|
|
596
|
+
const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
|
|
597
|
+
// Default view's filter should be gone
|
|
598
|
+
expect(params.profile_filters).not.toContain('hide_libc');
|
|
599
|
+
// Storage view should have 3 filters
|
|
600
|
+
expect(params.profile_filters).toContain('io');
|
|
601
|
+
expect(params.profile_filters).toContain('disk');
|
|
602
|
+
expect(params.profile_filters).toContain('storage');
|
|
603
|
+
});
|
|
604
|
+
|
|
605
|
+
mockNavigateTo.mockClear();
|
|
606
|
+
|
|
607
|
+
// View 3: testing-view (2 filters)
|
|
608
|
+
const testingFilters: ProfileFilter[] = [
|
|
609
|
+
{id: 't-1', type: 'stack', field: 'function_name', matchType: 'equal', value: 'test_main'},
|
|
610
|
+
{id: 't-2', type: 'frame', field: 'binary', matchType: 'contains', value: 'test'},
|
|
611
|
+
];
|
|
612
|
+
|
|
613
|
+
act(() => {
|
|
614
|
+
result.current.forceApplyFilters(testingFilters);
|
|
615
|
+
});
|
|
616
|
+
|
|
617
|
+
await waitFor(() => {
|
|
618
|
+
expect(mockNavigateTo).toHaveBeenCalled();
|
|
619
|
+
const [, params] = mockNavigateTo.mock.calls[mockNavigateTo.mock.calls.length - 1];
|
|
620
|
+
// Storage view's filters should be gone
|
|
621
|
+
expect(params.profile_filters).not.toContain('io');
|
|
622
|
+
expect(params.profile_filters).not.toContain('disk');
|
|
623
|
+
expect(params.profile_filters).not.toContain('storage');
|
|
624
|
+
// Testing view should have its 2 filters
|
|
625
|
+
expect(params.profile_filters).toContain('test_main');
|
|
626
|
+
expect(params.profile_filters).toContain('test');
|
|
627
|
+
});
|
|
628
|
+
});
|
|
629
|
+
|
|
630
|
+
it('should not change filters when clicking the same view tab', async () => {
|
|
631
|
+
// Start with existing filters
|
|
632
|
+
mockLocation.search = '?profile_filters=s:fn:=:existingFilter';
|
|
633
|
+
|
|
634
|
+
const {result} = renderHook(() => useProfileFiltersUrlState(), {wrapper: createWrapper()});
|
|
635
|
+
|
|
636
|
+
await waitFor(() => {
|
|
637
|
+
expect(result.current.appliedFilters).toHaveLength(1);
|
|
638
|
+
});
|
|
639
|
+
|
|
640
|
+
mockNavigateTo.mockClear();
|
|
641
|
+
|
|
642
|
+
// Apply the same filters (simulating clicking the same view tab)
|
|
643
|
+
const sameFilters: ProfileFilter[] = [
|
|
644
|
+
{
|
|
645
|
+
id: 'same-1',
|
|
646
|
+
type: 'stack',
|
|
647
|
+
field: 'function_name',
|
|
648
|
+
matchType: 'equal',
|
|
649
|
+
value: 'existingFilter',
|
|
650
|
+
},
|
|
651
|
+
];
|
|
652
|
+
|
|
653
|
+
act(() => {
|
|
654
|
+
result.current.forceApplyFilters(sameFilters);
|
|
655
|
+
});
|
|
656
|
+
|
|
657
|
+
await waitFor(() => {
|
|
658
|
+
expect(result.current.appliedFilters).toHaveLength(1);
|
|
659
|
+
expect(result.current.appliedFilters[0].value).toBe('existingFilter');
|
|
660
|
+
});
|
|
661
|
+
});
|
|
662
|
+
});
|
|
663
|
+
});
|