@parca/profile 0.19.140 → 0.19.143

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 (182) hide show
  1. package/CHANGELOG.md +9 -1
  2. package/dist/GraphTooltipArrow/Content.js +224 -30
  3. package/dist/GraphTooltipArrow/DockedGraphTooltip/index.js +192 -33
  4. package/dist/GraphTooltipArrow/ExpandOnHoverValue.js +53 -3
  5. package/dist/GraphTooltipArrow/index.d.ts.map +1 -1
  6. package/dist/GraphTooltipArrow/index.js +86 -56
  7. package/dist/GraphTooltipArrow/useGraphTooltip/index.js +37 -37
  8. package/dist/GraphTooltipArrow/useGraphTooltipMetaInfo/index.js +94 -68
  9. package/dist/MatchersInput/SuggestionItem.js +91 -12
  10. package/dist/MatchersInput/SuggestionsList.d.ts +2 -1
  11. package/dist/MatchersInput/SuggestionsList.d.ts.map +1 -1
  12. package/dist/MatchersInput/SuggestionsList.js +371 -157
  13. package/dist/MatchersInput/SuggestionsList.test.d.ts +2 -0
  14. package/dist/MatchersInput/SuggestionsList.test.d.ts.map +1 -0
  15. package/dist/MatchersInput/index.js +308 -115
  16. package/dist/MetricsCircle/index.js +39 -3
  17. package/dist/MetricsGraph/MetricsContextMenu/index.js +119 -19
  18. package/dist/MetricsGraph/MetricsInfoPanel/index.js +81 -20
  19. package/dist/MetricsGraph/MetricsTooltip/index.d.ts.map +1 -1
  20. package/dist/MetricsGraph/MetricsTooltip/index.js +107 -74
  21. package/dist/MetricsGraph/index.js +552 -203
  22. package/dist/MetricsGraph/useMetricsGraphDimensions.js +46 -25
  23. package/dist/MetricsGraph/utils/colorMapping.js +24 -17
  24. package/dist/MetricsSeries/index.js +70 -7
  25. package/dist/PreSelectedMatchers/index.d.ts.map +1 -1
  26. package/dist/PreSelectedMatchers/index.js +249 -102
  27. package/dist/ProfileExplorer/ProfileExplorerCompare.d.ts.map +1 -1
  28. package/dist/ProfileExplorer/ProfileExplorerCompare.js +240 -45
  29. package/dist/ProfileExplorer/ProfileExplorerSingle.js +98 -11
  30. package/dist/ProfileExplorer/index.js +183 -32
  31. package/dist/ProfileFlameChart/SamplesStrips/SamplesGraph/index.js +333 -148
  32. package/dist/ProfileFlameChart/SamplesStrips/SamplesStrips.stories.js +69 -35
  33. package/dist/ProfileFlameChart/SamplesStrips/index.js +645 -134
  34. package/dist/ProfileFlameChart/SamplesStrips/labelSetUtils.js +114 -55
  35. package/dist/ProfileFlameChart/index.js +260 -126
  36. package/dist/ProfileFlameGraph/FlameGraphArrow/ContextMenu.js +283 -85
  37. package/dist/ProfileFlameGraph/FlameGraphArrow/ContextMenuWrapper.js +56 -20
  38. package/dist/ProfileFlameGraph/FlameGraphArrow/FlameGraphNodes.js +211 -140
  39. package/dist/ProfileFlameGraph/FlameGraphArrow/MemoizedTooltip.js +133 -38
  40. package/dist/ProfileFlameGraph/FlameGraphArrow/MiniMap.js +261 -216
  41. package/dist/ProfileFlameGraph/FlameGraphArrow/TextWithEllipsis.d.ts.map +1 -1
  42. package/dist/ProfileFlameGraph/FlameGraphArrow/TextWithEllipsis.js +71 -45
  43. package/dist/ProfileFlameGraph/FlameGraphArrow/TooltipContext.d.ts.map +1 -1
  44. package/dist/ProfileFlameGraph/FlameGraphArrow/TooltipContext.js +58 -28
  45. package/dist/ProfileFlameGraph/FlameGraphArrow/ZoomControls.d.ts.map +1 -1
  46. package/dist/ProfileFlameGraph/FlameGraphArrow/ZoomControls.js +59 -8
  47. package/dist/ProfileFlameGraph/FlameGraphArrow/index.js +396 -179
  48. package/dist/ProfileFlameGraph/FlameGraphArrow/useBatchedRendering.d.ts.map +1 -1
  49. package/dist/ProfileFlameGraph/FlameGraphArrow/useBatchedRendering.js +68 -50
  50. package/dist/ProfileFlameGraph/FlameGraphArrow/useMappingList.js +62 -38
  51. package/dist/ProfileFlameGraph/FlameGraphArrow/useNodeColor.js +14 -6
  52. package/dist/ProfileFlameGraph/FlameGraphArrow/useScrollViewport.js +124 -82
  53. package/dist/ProfileFlameGraph/FlameGraphArrow/useVisibleNodes.js +160 -98
  54. package/dist/ProfileFlameGraph/FlameGraphArrow/useZoom.js +232 -112
  55. package/dist/ProfileFlameGraph/FlameGraphArrow/utils.js +137 -114
  56. package/dist/ProfileFlameGraph/benchmarks/benchdata/populateData.js +85 -0
  57. package/dist/ProfileFlameGraph/index.js +322 -147
  58. package/dist/ProfileMetricsGraph/hooks/useQueryRange.js +140 -32
  59. package/dist/ProfileMetricsGraph/index.js +515 -256
  60. package/dist/ProfileSelector/CompareButton.js +132 -12
  61. package/dist/ProfileSelector/MetricsGraphSection.js +228 -63
  62. package/dist/ProfileSelector/index.d.ts +1 -1
  63. package/dist/ProfileSelector/index.d.ts.map +1 -1
  64. package/dist/ProfileSelector/index.js +734 -142
  65. package/dist/ProfileSelector/useAutoQuerySelector.d.ts +1 -3
  66. package/dist/ProfileSelector/useAutoQuerySelector.d.ts.map +1 -1
  67. package/dist/ProfileSelector/useAutoQuerySelector.js +280 -132
  68. package/dist/ProfileSource.js +230 -163
  69. package/dist/ProfileTypeSelector/index.js +214 -125
  70. package/dist/ProfileView/components/ActionButtons/GroupByDropdown.js +50 -4
  71. package/dist/ProfileView/components/ActionButtons/SortByDropdown.js +137 -32
  72. package/dist/ProfileView/components/ColorStackLegend.js +182 -54
  73. package/dist/ProfileView/components/DashboardItems/index.js +87 -28
  74. package/dist/ProfileView/components/DashboardLayout/index.js +108 -16
  75. package/dist/ProfileView/components/DiffLegend.js +172 -29
  76. package/dist/ProfileView/components/GroupByLabelsDropdown/index.js +199 -55
  77. package/dist/ProfileView/components/InvertCallStack/index.js +97 -9
  78. package/dist/ProfileView/components/ProfileFilters/filterPresets.js +260 -315
  79. package/dist/ProfileView/components/ProfileFilters/index.js +518 -215
  80. package/dist/ProfileView/components/ProfileFilters/useProfileFilters.js +370 -306
  81. package/dist/ProfileView/components/ProfileFilters/useProfileFiltersUrlState.js +191 -118
  82. package/dist/ProfileView/components/ProfileHeader/index.js +105 -11
  83. package/dist/ProfileView/components/ShareButton/ResultBox.js +119 -16
  84. package/dist/ProfileView/components/ShareButton/index.js +352 -62
  85. package/dist/ProfileView/components/Toolbars/MultiLevelDropdown.d.ts.map +1 -1
  86. package/dist/ProfileView/components/Toolbars/MultiLevelDropdown.js +664 -192
  87. package/dist/ProfileView/components/Toolbars/SwitchMenuItem.js +94 -7
  88. package/dist/ProfileView/components/Toolbars/TableColumnsDropdown.js +196 -155
  89. package/dist/ProfileView/components/Toolbars/index.js +441 -21
  90. package/dist/ProfileView/components/ViewSelector/Dropdown.js +233 -22
  91. package/dist/ProfileView/components/ViewSelector/index.js +186 -82
  92. package/dist/ProfileView/components/VisualizationContainer/index.d.ts.map +1 -1
  93. package/dist/ProfileView/components/VisualizationContainer/index.js +52 -7
  94. package/dist/ProfileView/components/VisualizationPanel.js +185 -8
  95. package/dist/ProfileView/context/DashboardContext.js +74 -26
  96. package/dist/ProfileView/context/ProfileViewContext.js +56 -15
  97. package/dist/ProfileView/hooks/useAutoSelectDimension.js +71 -41
  98. package/dist/ProfileView/hooks/useProfileMetadata.js +50 -18
  99. package/dist/ProfileView/hooks/useResetFlameGraphState.js +31 -10
  100. package/dist/ProfileView/hooks/useResetStateOnProfileTypeChange.js +71 -27
  101. package/dist/ProfileView/hooks/useResetStateOnSeriesChange.js +53 -17
  102. package/dist/ProfileView/hooks/useVisualizationState.js +229 -69
  103. package/dist/ProfileView/index.js +383 -45
  104. package/dist/ProfileView/types/visualization.js +1 -13
  105. package/dist/ProfileView/utils/colorUtils.js +8 -7
  106. package/dist/ProfileViewWithData.js +319 -225
  107. package/dist/QueryControls/index.js +418 -47
  108. package/dist/Sandwich/components/CalleesSection.js +54 -4
  109. package/dist/Sandwich/components/CallersSection.js +97 -27
  110. package/dist/Sandwich/components/TableSection.js +77 -4
  111. package/dist/Sandwich/index.js +125 -12
  112. package/dist/Sandwich/utils/processRowData.js +48 -39
  113. package/dist/SelectWithRefresh/index.js +102 -28
  114. package/dist/SimpleMatchers/Select.js +520 -187
  115. package/dist/SimpleMatchers/index.js +590 -288
  116. package/dist/SourceView/Highlighter.js +230 -70
  117. package/dist/SourceView/LineNo.js +72 -17
  118. package/dist/SourceView/index.js +177 -101
  119. package/dist/SourceView/lang-detector/ext-to-lang.json +798 -798
  120. package/dist/SourceView/lang-detector/index.js +28 -14
  121. package/dist/SourceView/useSelectedLineRange.js +72 -20
  122. package/dist/Table/ColorCell.js +42 -1
  123. package/dist/Table/ColumnsVisibility.js +114 -6
  124. package/dist/Table/MoreDropdown.js +107 -21
  125. package/dist/Table/TableContextMenu.js +144 -134
  126. package/dist/Table/TableContextMenuWrapper.js +59 -14
  127. package/dist/Table/hooks/useColorManagement.js +58 -16
  128. package/dist/Table/hooks/useTableConfiguration.d.ts.map +1 -1
  129. package/dist/Table/hooks/useTableConfiguration.js +323 -167
  130. package/dist/Table/index.js +217 -123
  131. package/dist/Table/utils/functions.js +169 -144
  132. package/dist/Table/utils/topAndBottomExpandedRowModel.js +69 -52
  133. package/dist/TimelineGuide/index.js +209 -16
  134. package/dist/TopTable/benchmarks/benchdata/populateData.js +91 -0
  135. package/dist/TopTable/index.js +325 -121
  136. package/dist/contexts/LabelsQueryProvider.js +94 -32
  137. package/dist/contexts/UnifiedLabelsContext.js +114 -49
  138. package/dist/contexts/utils.js +37 -15
  139. package/dist/hooks/urlParsers.js +27 -15
  140. package/dist/hooks/useColorBy.js +47 -10
  141. package/dist/hooks/useCompareModeMeta.js +112 -62
  142. package/dist/hooks/useDashboardItems.js +52 -11
  143. package/dist/hooks/useLabels.js +295 -52
  144. package/dist/hooks/useQueryState.d.ts +1 -1
  145. package/dist/hooks/useQueryState.d.ts.map +1 -1
  146. package/dist/hooks/useQueryState.js +375 -329
  147. package/dist/index.js +11 -6
  148. package/dist/testdata/fg-diff.json +3750 -0
  149. package/dist/testdata/fg-simple.json +1879 -0
  150. package/dist/testdata/link_data.json +56 -0
  151. package/dist/testdata/tabular.json +30 -0
  152. package/dist/testdata/test_flamegraph.json +26846 -0
  153. package/dist/testdata/test_graph.json +53 -0
  154. package/dist/useDelayedLoader.js +32 -18
  155. package/dist/useGrpcQuery/index.js +71 -11
  156. package/dist/useHasProfileData.js +90 -12
  157. package/dist/useQuery.js +205 -64
  158. package/dist/useSumBy.d.ts.map +1 -1
  159. package/dist/useSumBy.js +294 -138
  160. package/dist/utils.js +62 -30
  161. package/package.json +9 -9
  162. package/src/GraphTooltipArrow/index.tsx +3 -0
  163. package/src/MatchersInput/SuggestionsList.test.tsx +70 -0
  164. package/src/MatchersInput/SuggestionsList.tsx +11 -10
  165. package/src/MatchersInput/index.tsx +1 -1
  166. package/src/MetricsGraph/MetricsTooltip/index.tsx +22 -34
  167. package/src/PreSelectedMatchers/index.tsx +3 -0
  168. package/src/ProfileExplorer/ProfileExplorerCompare.tsx +9 -2
  169. package/src/ProfileFlameGraph/FlameGraphArrow/TextWithEllipsis.tsx +3 -0
  170. package/src/ProfileFlameGraph/FlameGraphArrow/TooltipContext.tsx +3 -0
  171. package/src/ProfileFlameGraph/FlameGraphArrow/ZoomControls.tsx +3 -0
  172. package/src/ProfileFlameGraph/FlameGraphArrow/useBatchedRendering.ts +3 -0
  173. package/src/ProfileSelector/index.tsx +31 -9
  174. package/src/ProfileSelector/useAutoQuerySelector.ts +64 -42
  175. package/src/ProfileView/components/Toolbars/MultiLevelDropdown.tsx +3 -0
  176. package/src/ProfileView/components/VisualizationContainer/index.tsx +3 -0
  177. package/src/Table/hooks/useTableConfiguration.tsx +7 -13
  178. package/src/hooks/useQueryState.ts +18 -3
  179. package/src/useDelayedLoader.ts +10 -10
  180. package/src/useSumBy.ts +12 -18
  181. package/dist/ProfileView/components/ProfileFilters/useProfileFiltersUrlState.test.js +0 -455
  182. package/dist/hooks/useQueryState.test.js +0 -868
@@ -11,7 +11,7 @@
11
11
  // See the License for the specific language governing permissions and
12
12
  // limitations under the License.
13
13
 
14
- import {useCallback, useEffect, useMemo, useState} from 'react';
14
+ import {useCallback, useEffect, useMemo, useRef, useState} from 'react';
15
15
 
16
16
  import {useQueryState as useNuqsQueryState, useQueryStates} from 'nuqs';
17
17
 
@@ -50,7 +50,10 @@ interface UseQueryStateReturn {
50
50
  setDraftMatchers: (matchers: string) => void;
51
51
 
52
52
  // Commit function
53
- commitDraft: (refreshedTimeRange?: {from: number; to: number; timeSelection: string}) => void;
53
+ commitDraft: (
54
+ refreshedTimeRange?: {from: number; to: number; timeSelection: string},
55
+ expression?: string
56
+ ) => void;
54
57
 
55
58
  // ProfileSelection state (separate from QuerySelection)
56
59
  profileSelection: ProfileSelection | null;
@@ -229,8 +232,20 @@ export const useQueryState = (options: UseQueryStateOptions = {}): UseQueryState
229
232
 
230
233
  // Sync computed sumBy to URL if URL doesn't already have a value
231
234
  // to ensure the shared URL can always pick it up.
235
+ // Only run once (when sumByParam first becomes available), not on every change,
236
+ // to avoid oscillation with external writers (useViewQueryState).
237
+ const hasSyncedSumByRef = useRef(false);
232
238
  useEffect(() => {
233
- if (sumByParam === null && computedSumByFromURL !== undefined && !sumBySelectionLoading) {
239
+ if (sumByParam !== null) {
240
+ hasSyncedSumByRef.current = true;
241
+ }
242
+ if (
243
+ !hasSyncedSumByRef.current &&
244
+ sumByParam === null &&
245
+ computedSumByFromURL !== undefined &&
246
+ !sumBySelectionLoading
247
+ ) {
248
+ hasSyncedSumByRef.current = true;
234
249
  void setSumByParam(sumByToParam(computedSumByFromURL));
235
250
  }
236
251
  }, [sumByParam, computedSumByFromURL, sumBySelectionLoading, setSumByParam]);
@@ -18,20 +18,20 @@ interface DelayedLoaderOptions {
18
18
  }
19
19
 
20
20
  const useDelayedLoader = (isLoading = false, options?: DelayedLoaderOptions): boolean => {
21
+ 'use no memo';
21
22
  const {delay = 500} = options ?? {};
22
23
  const [isLoaderVisible, setIsLoaderVisible] = useState<boolean>(false);
23
24
  useEffect(() => {
24
- let showLoaderTimeout: ReturnType<typeof setTimeout>;
25
- if (isLoading && !isLoaderVisible) {
26
- // if the request takes longer than half a second, show the loading icon
27
- showLoaderTimeout = setTimeout(() => {
28
- setIsLoaderVisible(true);
29
- }, delay);
30
- } else if (!isLoading && isLoaderVisible) {
25
+ if (!isLoading) return;
26
+ // if the request takes longer than half a second, show the loading icon
27
+ const showLoaderTimeout = setTimeout(() => {
28
+ setIsLoaderVisible(true);
29
+ }, delay);
30
+ return () => {
31
+ clearTimeout(showLoaderTimeout);
31
32
  setIsLoaderVisible(false);
32
- }
33
- return () => clearTimeout(showLoaderTimeout);
34
- }, [isLoading, isLoaderVisible, delay]);
33
+ };
34
+ }, [isLoading, delay]);
35
35
 
36
36
  return isLoaderVisible;
37
37
  };
package/src/useSumBy.ts CHANGED
@@ -11,7 +11,7 @@
11
11
  // See the License for the specific language governing permissions and
12
12
  // limitations under the License.
13
13
 
14
- import {useCallback, useEffect, useMemo, useRef, useState} from 'react';
14
+ import {useCallback, useMemo, useState} from 'react';
15
15
 
16
16
  import {QueryServiceClient} from '@parca/client';
17
17
  import {DateTimeRange} from '@parca/components';
@@ -70,14 +70,19 @@ export const useSumBySelection = (
70
70
  );
71
71
 
72
72
  // Update userSelectedSumBy when defaultValue changes (e.g., during navigation)
73
- useEffect(() => {
73
+ const [prevProfileType, setPrevProfileType] = useState(profileType);
74
+ const [prevDefaultValue, setPrevDefaultValue] = useState(defaultValue);
75
+
76
+ if (prevProfileType !== profileType || prevDefaultValue !== defaultValue) {
77
+ setPrevProfileType(profileType);
78
+ setPrevDefaultValue(defaultValue);
74
79
  if (profileType != null && defaultValue !== undefined) {
75
80
  setUserSelectedSumBy(prev => ({
76
81
  ...prev,
77
82
  [profileType.toString()]: defaultValue,
78
83
  }));
79
84
  }
80
- }, [profileType, defaultValue]);
85
+ }
81
86
 
82
87
  const setSumBy = useCallback(
83
88
  (sumBy: string[]) => {
@@ -97,19 +102,11 @@ export const useSumBySelection = (
97
102
 
98
103
  const {defaultSumBy} = useDefaultSumBy(profileType, labelNamesLoading, labels);
99
104
 
100
- // Store the last valid sumBy value to return during loading
101
- const lastValidSumByRef = useRef<string[]>(DEFAULT_EMPTY_SUM_BY);
102
-
103
105
  const sumBy = useMemo(() => {
104
- if (labelNamesLoading) {
105
- // For smoother UX, return draftSumBy first if available during loading
106
- // as this must be recently computed with the draft time range labels.
107
- if (draftSumBy !== undefined) {
108
- return draftSumBy;
109
- }
110
- if (lastValidSumByRef.current == null) {
111
- return lastValidSumByRef.current;
112
- }
106
+ // For smoother UX, return draftSumBy first if available during loading
107
+ // as this must be recently computed with the draft time range labels.
108
+ if (labelNamesLoading && draftSumBy !== undefined) {
109
+ return draftSumBy;
113
110
  }
114
111
 
115
112
  // Prefer non-empty URL default over auto-computed default to avoid a
@@ -125,9 +122,6 @@ export const useSumBySelection = (
125
122
  result = DEFAULT_EMPTY_SUM_BY;
126
123
  }
127
124
 
128
- // Store the computed value for next loading state
129
- lastValidSumByRef.current = result;
130
-
131
125
  return result;
132
126
  }, [userSelectedSumBy, profileType, defaultSumBy, labelNamesLoading, draftSumBy, defaultValue]);
133
127
 
@@ -1,455 +0,0 @@
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
- // eslint-disable-next-line import/no-unresolved
5
- import { NuqsTestingAdapter } from 'nuqs/adapters/testing';
6
- import { describe, expect, it, vi } from 'vitest';
7
- import { decodeProfileFilters, useProfileFiltersUrlState } from './useProfileFiltersUrlState';
8
- // Helper to create wrapper with NuqsTestingAdapter
9
- const createWrapper = (searchParams = {}, onUrlUpdate) => {
10
- const Wrapper = ({ children }) => (_jsx(NuqsTestingAdapter, { searchParams: searchParams, onUrlUpdate: onUrlUpdate, hasMemory: true, children: children }));
11
- Wrapper.displayName = 'NuqsTestingWrapper';
12
- return Wrapper;
13
- };
14
- describe('useProfileFiltersUrlState', () => {
15
- describe('decodeProfileFilters', () => {
16
- it('should return empty array for empty string', () => {
17
- expect(decodeProfileFilters('')).toEqual([]);
18
- });
19
- it('should return empty array for undefined', () => {
20
- expect(decodeProfileFilters(undefined)).toEqual([]);
21
- });
22
- it('should decode stack filter with function_name', () => {
23
- // Format: type:field:match:value -> s:fn:=:testFunc
24
- const encoded = 's:fn:=:testFunc';
25
- const result = decodeProfileFilters(encoded);
26
- expect(result).toHaveLength(1);
27
- expect(result[0]).toMatchObject({
28
- type: 'stack',
29
- field: 'function_name',
30
- matchType: 'equal',
31
- value: 'testFunc',
32
- });
33
- });
34
- it('should decode frame filter with binary', () => {
35
- const encoded = 'f:b:!=:libc.so';
36
- const result = decodeProfileFilters(encoded);
37
- expect(result).toHaveLength(1);
38
- expect(result[0]).toMatchObject({
39
- type: 'frame',
40
- field: 'binary',
41
- matchType: 'not_equal',
42
- value: 'libc.so',
43
- });
44
- });
45
- it('should decode filter with contains match', () => {
46
- const encoded = 's:fn:~:runtime';
47
- const result = decodeProfileFilters(encoded);
48
- expect(result).toHaveLength(1);
49
- expect(result[0]).toMatchObject({
50
- type: 'stack',
51
- field: 'function_name',
52
- matchType: 'contains',
53
- value: 'runtime',
54
- });
55
- });
56
- it('should decode filter with not_contains match', () => {
57
- const encoded = 'f:b:!~:node';
58
- const result = decodeProfileFilters(encoded);
59
- expect(result).toHaveLength(1);
60
- expect(result[0]).toMatchObject({
61
- type: 'frame',
62
- field: 'binary',
63
- matchType: 'not_contains',
64
- value: 'node',
65
- });
66
- });
67
- it('should decode filter with starts_with match', () => {
68
- const encoded = 's:fn:^:std::';
69
- const result = decodeProfileFilters(encoded);
70
- expect(result).toHaveLength(1);
71
- expect(result[0]).toMatchObject({
72
- type: 'stack',
73
- field: 'function_name',
74
- matchType: 'starts_with',
75
- value: 'std::',
76
- });
77
- });
78
- it('should decode filter with not_starts_with match', () => {
79
- const encoded = 'f:fn:!^:tokio::';
80
- const result = decodeProfileFilters(encoded);
81
- expect(result).toHaveLength(1);
82
- expect(result[0]).toMatchObject({
83
- type: 'frame',
84
- field: 'function_name',
85
- matchType: 'not_starts_with',
86
- value: 'tokio::',
87
- });
88
- });
89
- it('should decode multiple filters', () => {
90
- const encoded = 's:fn:=:testFunc,f:b:!=:libc.so';
91
- const result = decodeProfileFilters(encoded);
92
- expect(result).toHaveLength(2);
93
- expect(result[0]).toMatchObject({
94
- type: 'stack',
95
- field: 'function_name',
96
- matchType: 'equal',
97
- value: 'testFunc',
98
- });
99
- expect(result[1]).toMatchObject({
100
- type: 'frame',
101
- field: 'binary',
102
- matchType: 'not_equal',
103
- value: 'libc.so',
104
- });
105
- });
106
- it('should decode preset filter', () => {
107
- const encoded = 'p:hide_libc:enabled';
108
- const result = decodeProfileFilters(encoded);
109
- expect(result).toHaveLength(1);
110
- expect(result[0]).toMatchObject({
111
- type: 'hide_libc',
112
- value: 'enabled',
113
- });
114
- });
115
- it('should handle values with colons', () => {
116
- const encoded = 'p:some_preset:value:with:colons';
117
- const result = decodeProfileFilters(encoded);
118
- expect(result).toHaveLength(1);
119
- expect(result[0]).toMatchObject({
120
- type: 'some_preset',
121
- value: 'value:with:colons',
122
- });
123
- });
124
- it('should decode all field types', () => {
125
- const testCases = [
126
- { encoded: 's:fn:=:test', expectedField: 'function_name' },
127
- { encoded: 's:b:=:test', expectedField: 'binary' },
128
- { encoded: 's:sn:=:test', expectedField: 'system_name' },
129
- { encoded: 's:f:=:test', expectedField: 'filename' },
130
- { encoded: 's:a:=:test', expectedField: 'address' },
131
- { encoded: 's:ln:=:test', expectedField: 'line_number' },
132
- ];
133
- for (const { encoded, expectedField } of testCases) {
134
- const result = decodeProfileFilters(encoded);
135
- expect(result[0].field).toBe(expectedField);
136
- }
137
- });
138
- it('should return empty array for malformed input', () => {
139
- // This should not throw - it returns empty array on error
140
- expect(() => decodeProfileFilters('malformed')).not.toThrow();
141
- });
142
- it('should generate unique IDs for each filter', () => {
143
- const encoded = 's:fn:=:func1,s:fn:=:func2,s:fn:=:func3';
144
- const result = decodeProfileFilters(encoded);
145
- const ids = result.map(f => f.id);
146
- const uniqueIds = new Set(ids);
147
- expect(uniqueIds.size).toBe(ids.length);
148
- });
149
- });
150
- describe('Basic functionality', () => {
151
- it('should initialize with empty filters when no URL params', () => {
152
- const { result } = renderHook(() => useProfileFiltersUrlState(), { wrapper: createWrapper() });
153
- expect(result.current.appliedFilters).toEqual([]);
154
- });
155
- it('should read filters from URL', async () => {
156
- const { result } = renderHook(() => useProfileFiltersUrlState(), {
157
- wrapper: createWrapper({ profile_filters: 's:fn:=:testFunc' }),
158
- });
159
- await waitFor(() => {
160
- expect(result.current.appliedFilters).toHaveLength(1);
161
- expect(result.current.appliedFilters[0]).toMatchObject({
162
- type: 'stack',
163
- field: 'function_name',
164
- matchType: 'equal',
165
- value: 'testFunc',
166
- });
167
- });
168
- });
169
- it('should update URL when setting filters', async () => {
170
- const onUrlUpdate = vi.fn();
171
- const { result } = renderHook(() => useProfileFiltersUrlState(), {
172
- wrapper: createWrapper({}, onUrlUpdate),
173
- });
174
- const newFilters = [
175
- {
176
- id: 'test-1',
177
- type: 'frame',
178
- field: 'binary',
179
- matchType: 'not_contains',
180
- value: 'libc.so',
181
- },
182
- ];
183
- act(() => {
184
- result.current.setAppliedFilters(newFilters);
185
- });
186
- await waitFor(() => {
187
- expect(onUrlUpdate).toHaveBeenCalled();
188
- const lastCall = onUrlUpdate.mock.calls[onUrlUpdate.mock.calls.length - 1][0];
189
- expect(lastCall.searchParams.get('profile_filters')).toBe('f:b:!~:libc.so');
190
- });
191
- });
192
- it('should clear URL param when setting empty filters', async () => {
193
- const onUrlUpdate = vi.fn();
194
- const { result } = renderHook(() => useProfileFiltersUrlState(), {
195
- wrapper: createWrapper({ profile_filters: 's:fn:=:testFunc' }, onUrlUpdate),
196
- });
197
- act(() => {
198
- result.current.setAppliedFilters([]);
199
- });
200
- await waitFor(() => {
201
- expect(onUrlUpdate).toHaveBeenCalled();
202
- const lastCall = onUrlUpdate.mock.calls[onUrlUpdate.mock.calls.length - 1][0];
203
- expect(lastCall.searchParams.has('profile_filters')).toBe(false);
204
- });
205
- });
206
- });
207
- describe('forceApplyFilters', () => {
208
- it('should provide forceApplyFilters method', () => {
209
- const { result } = renderHook(() => useProfileFiltersUrlState(), { wrapper: createWrapper() });
210
- expect(typeof result.current.forceApplyFilters).toBe('function');
211
- });
212
- it('should force apply filters overwriting existing', async () => {
213
- const onUrlUpdate = vi.fn();
214
- const { result } = renderHook(() => useProfileFiltersUrlState(), {
215
- wrapper: createWrapper({ profile_filters: 's:fn:=:existingFunc' }, onUrlUpdate),
216
- });
217
- // Verify existing filter is loaded
218
- await waitFor(() => {
219
- expect(result.current.appliedFilters).toHaveLength(1);
220
- });
221
- const newFilters = [
222
- {
223
- id: 'forced-1',
224
- type: 'frame',
225
- field: 'binary',
226
- matchType: 'not_contains',
227
- value: 'forcedValue',
228
- },
229
- ];
230
- act(() => {
231
- result.current.forceApplyFilters(newFilters);
232
- });
233
- await waitFor(() => {
234
- expect(onUrlUpdate).toHaveBeenCalled();
235
- const lastCall = onUrlUpdate.mock.calls[onUrlUpdate.mock.calls.length - 1][0];
236
- expect(lastCall.searchParams.get('profile_filters')).toBe('f:b:!~:forcedValue');
237
- });
238
- });
239
- it('should clear filters when force applying empty array', async () => {
240
- const onUrlUpdate = vi.fn();
241
- const { result } = renderHook(() => useProfileFiltersUrlState(), {
242
- wrapper: createWrapper({ profile_filters: 's:fn:=:existingFunc' }, onUrlUpdate),
243
- });
244
- act(() => {
245
- result.current.forceApplyFilters([]);
246
- });
247
- await waitFor(() => {
248
- expect(onUrlUpdate).toHaveBeenCalled();
249
- const lastCall = onUrlUpdate.mock.calls[onUrlUpdate.mock.calls.length - 1][0];
250
- expect(lastCall.searchParams.has('profile_filters')).toBe(false);
251
- });
252
- });
253
- });
254
- describe('Preset filter encoding', () => {
255
- it('should encode preset filters correctly', async () => {
256
- const onUrlUpdate = vi.fn();
257
- const { result } = renderHook(() => useProfileFiltersUrlState(), {
258
- wrapper: createWrapper({}, onUrlUpdate),
259
- });
260
- const presetFilters = [
261
- {
262
- id: 'preset-1',
263
- type: 'hide_libc',
264
- value: 'enabled',
265
- },
266
- ];
267
- act(() => {
268
- result.current.setAppliedFilters(presetFilters);
269
- });
270
- await waitFor(() => {
271
- expect(onUrlUpdate).toHaveBeenCalled();
272
- const lastCall = onUrlUpdate.mock.calls[onUrlUpdate.mock.calls.length - 1][0];
273
- expect(lastCall.searchParams.get('profile_filters')).toBe('p:hide_libc:enabled');
274
- });
275
- });
276
- it('should handle mixed preset and regular filters', async () => {
277
- const onUrlUpdate = vi.fn();
278
- const { result } = renderHook(() => useProfileFiltersUrlState(), {
279
- wrapper: createWrapper({}, onUrlUpdate),
280
- });
281
- const mixedFilters = [
282
- {
283
- id: 'preset-1',
284
- type: 'hide_libc',
285
- value: 'enabled',
286
- },
287
- {
288
- id: 'regular-1',
289
- type: 'frame',
290
- field: 'binary',
291
- matchType: 'not_contains',
292
- value: 'node',
293
- },
294
- ];
295
- act(() => {
296
- result.current.setAppliedFilters(mixedFilters);
297
- });
298
- await waitFor(() => {
299
- expect(onUrlUpdate).toHaveBeenCalled();
300
- const lastCall = onUrlUpdate.mock.calls[onUrlUpdate.mock.calls.length - 1][0];
301
- expect(lastCall.searchParams.get('profile_filters')).toBe('p:hide_libc:enabled,f:b:!~:node');
302
- });
303
- });
304
- });
305
- describe('URL encoding edge cases', () => {
306
- it('should handle special characters in filter values', async () => {
307
- const onUrlUpdate = vi.fn();
308
- const { result } = renderHook(() => useProfileFiltersUrlState(), {
309
- wrapper: createWrapper({}, onUrlUpdate),
310
- });
311
- const filtersWithSpecialChars = [
312
- {
313
- id: 'special-1',
314
- type: 'stack',
315
- field: 'function_name',
316
- matchType: 'contains',
317
- value: 'std::vector<int>',
318
- },
319
- ];
320
- act(() => {
321
- result.current.setAppliedFilters(filtersWithSpecialChars);
322
- });
323
- await waitFor(() => {
324
- expect(onUrlUpdate).toHaveBeenCalled();
325
- const lastCall = onUrlUpdate.mock.calls[onUrlUpdate.mock.calls.length - 1][0];
326
- const filterValue = lastCall.searchParams.get('profile_filters');
327
- // The value should contain the encoded special characters
328
- expect(filterValue).toContain('std%3A%3Avector%3Cint%3E');
329
- });
330
- });
331
- it('should filter out incomplete filters when encoding', async () => {
332
- const onUrlUpdate = vi.fn();
333
- const { result } = renderHook(() => useProfileFiltersUrlState(), {
334
- wrapper: createWrapper({}, onUrlUpdate),
335
- });
336
- const incompleteFilters = [
337
- {
338
- id: 'complete-1',
339
- type: 'frame',
340
- field: 'binary',
341
- matchType: 'not_contains',
342
- value: 'valid',
343
- },
344
- {
345
- id: 'incomplete-1',
346
- type: 'frame',
347
- // Missing field, matchType
348
- value: '',
349
- },
350
- {
351
- id: 'incomplete-2',
352
- type: undefined,
353
- value: 'value',
354
- },
355
- ];
356
- act(() => {
357
- result.current.setAppliedFilters(incompleteFilters);
358
- });
359
- await waitFor(() => {
360
- expect(onUrlUpdate).toHaveBeenCalled();
361
- const lastCall = onUrlUpdate.mock.calls[onUrlUpdate.mock.calls.length - 1][0];
362
- // Only the complete filter should be encoded
363
- expect(lastCall.searchParams.get('profile_filters')).toBe('f:b:!~:valid');
364
- });
365
- });
366
- });
367
- describe('Memoization', () => {
368
- it('should return empty array with consistent structure when no filters', () => {
369
- const { result } = renderHook(() => useProfileFiltersUrlState(), { wrapper: createWrapper() });
370
- // Empty filters should be an empty array (not undefined or null)
371
- expect(Array.isArray(result.current.appliedFilters)).toBe(true);
372
- expect(result.current.appliedFilters).toHaveLength(0);
373
- });
374
- it('should always return array (never undefined)', () => {
375
- const { result } = renderHook(() => useProfileFiltersUrlState(), { wrapper: createWrapper() });
376
- expect(Array.isArray(result.current.appliedFilters)).toBe(true);
377
- expect(result.current.appliedFilters).toEqual([]);
378
- });
379
- it('should return correctly structured filters from URL', async () => {
380
- const { result } = renderHook(() => useProfileFiltersUrlState(), {
381
- wrapper: createWrapper({ profile_filters: 's:fn:=:testFunc' }),
382
- });
383
- await waitFor(() => {
384
- expect(result.current.appliedFilters).toHaveLength(1);
385
- });
386
- // Verify the filter structure is correct
387
- const filter = result.current.appliedFilters[0];
388
- expect(filter).toHaveProperty('id');
389
- expect(filter).toHaveProperty('type', 'stack');
390
- expect(filter).toHaveProperty('field', 'function_name');
391
- expect(filter).toHaveProperty('matchType', 'equal');
392
- // eslint-disable-next-line jest-dom/prefer-to-have-value
393
- expect(filter).toHaveProperty('value', 'testFunc');
394
- });
395
- });
396
- describe('View switching scenarios', () => {
397
- it('should completely replace filters when switching views using forceApplyFilters', async () => {
398
- const onUrlUpdate = vi.fn();
399
- const { result } = renderHook(() => useProfileFiltersUrlState(), {
400
- wrapper: createWrapper({ profile_filters: 's:fn:=:viewAFunc,f:b:!=:viewABinary' }, onUrlUpdate),
401
- });
402
- await waitFor(() => {
403
- expect(result.current.appliedFilters).toHaveLength(2);
404
- });
405
- // Switch to View B (completely different filter)
406
- const viewBFilters = [
407
- {
408
- id: 'viewB-1',
409
- type: 'frame',
410
- field: 'function_name',
411
- matchType: 'contains',
412
- value: 'viewBOnly',
413
- },
414
- ];
415
- act(() => {
416
- result.current.forceApplyFilters(viewBFilters);
417
- });
418
- await waitFor(() => {
419
- expect(onUrlUpdate).toHaveBeenCalled();
420
- const lastCall = onUrlUpdate.mock.calls[onUrlUpdate.mock.calls.length - 1][0];
421
- const filterValue = lastCall.searchParams.get('profile_filters');
422
- // View A's filters should be completely gone
423
- expect(filterValue).not.toContain('viewAFunc');
424
- expect(filterValue).not.toContain('viewABinary');
425
- // Only View B's filter should be present
426
- expect(filterValue).toBe('f:fn:~:viewBOnly');
427
- });
428
- });
429
- it('should not change filters when clicking the same view tab', async () => {
430
- const { result } = renderHook(() => useProfileFiltersUrlState(), {
431
- wrapper: createWrapper({ profile_filters: 's:fn:=:existingFilter' }),
432
- });
433
- await waitFor(() => {
434
- expect(result.current.appliedFilters).toHaveLength(1);
435
- });
436
- // Apply the same filters (simulating clicking the same view tab)
437
- const sameFilters = [
438
- {
439
- id: 'same-1',
440
- type: 'stack',
441
- field: 'function_name',
442
- matchType: 'equal',
443
- value: 'existingFilter',
444
- },
445
- ];
446
- act(() => {
447
- result.current.forceApplyFilters(sameFilters);
448
- });
449
- await waitFor(() => {
450
- expect(result.current.appliedFilters).toHaveLength(1);
451
- expect(result.current.appliedFilters[0].value).toBe('existingFilter');
452
- });
453
- });
454
- });
455
- });