-
Notifications
You must be signed in to change notification settings - Fork 481
Expand file tree
/
Copy pathtracks-sanitization.test.ts
More file actions
312 lines (296 loc) · 9.82 KB
/
Copy pathtracks-sanitization.test.ts
File metadata and controls
312 lines (296 loc) · 9.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import {
computeOldTrackIndexToNewTrackIndexMap,
computeHiddenTracksAfterSanitization,
computeTrackOrderAfterSanitization,
} from '../../profile-logic/tracks';
import type { Track } from 'firefox-profiler/types';
describe('computeOldTrackIndexToNewTrackIndexMap', function () {
it('matches process tracks by pid', function () {
const oldTracks: Track[] = [
{ type: 'process', pid: '1', mainThreadIndex: 0 },
{ type: 'process', pid: '2', mainThreadIndex: 1 },
{ type: 'process', pid: '3', mainThreadIndex: 2 },
];
const newTracks: Track[] = [
{ type: 'process', pid: '1', mainThreadIndex: 0 },
{ type: 'process', pid: '3', mainThreadIndex: 1 },
];
const map = computeOldTrackIndexToNewTrackIndexMap({
oldTracks,
newTracks,
oldThreadIndexToNew: null,
oldCounterIndexToNew: null,
oldStringToNewStringPlusOne: null,
});
expect(map.get(0)).toBe(0);
expect(map.has(1)).toBe(false);
expect(map.get(2)).toBe(1);
});
it('matches screenshot tracks by id', function () {
const oldTracks: Track[] = [
{ type: 'screenshots', id: 'win-A', threadIndex: 0 },
{ type: 'screenshots', id: 'win-B', threadIndex: 0 },
];
const newTracks: Track[] = [
{ type: 'screenshots', id: 'win-B', threadIndex: 0 },
];
const map = computeOldTrackIndexToNewTrackIndexMap({
oldTracks,
newTracks,
oldThreadIndexToNew: null,
oldCounterIndexToNew: null,
oldStringToNewStringPlusOne: null,
});
expect(map.has(0)).toBe(false);
expect(map.get(1)).toBe(0);
});
it('matches visual-progress singletons by type', function () {
const oldTracks: Track[] = [
{ type: 'visual-progress' },
{ type: 'perceptual-visual-progress' },
{ type: 'contentful-visual-progress' },
];
const newTracks: Track[] = [
{ type: 'contentful-visual-progress' },
{ type: 'visual-progress' },
];
const map = computeOldTrackIndexToNewTrackIndexMap({
oldTracks,
newTracks,
oldThreadIndexToNew: null,
oldCounterIndexToNew: null,
oldStringToNewStringPlusOne: null,
});
expect(map.get(0)).toBe(1); // visual-progress
expect(map.has(1)).toBe(false); // perceptual was removed
expect(map.get(2)).toBe(0); // contentful
});
it('translates threadIndex via oldThreadIndexToNew for thread-keyed tracks', function () {
const oldTracks: Track[] = [
{ type: 'thread', threadIndex: 0 },
{ type: 'network', threadIndex: 1 },
{ type: 'ipc', threadIndex: 2 },
{ type: 'event-delay', threadIndex: 3 },
];
// After sanitization thread #1 was removed; the others were renumbered.
const oldThreadIndexToNew = new Map<number, number>([
[0, 0],
[2, 1],
[3, 2],
]);
const newTracks: Track[] = [
{ type: 'thread', threadIndex: 0 },
{ type: 'ipc', threadIndex: 1 },
{ type: 'event-delay', threadIndex: 2 },
];
const map = computeOldTrackIndexToNewTrackIndexMap({
oldTracks,
newTracks,
oldThreadIndexToNew,
oldCounterIndexToNew: null,
oldStringToNewStringPlusOne: null,
});
expect(map.get(0)).toBe(0); // thread:0 → new threadIndex 0
expect(map.has(1)).toBe(false); // network:1 has no surviving thread
expect(map.get(2)).toBe(1); // ipc:2 → new threadIndex 1
expect(map.get(3)).toBe(2); // event-delay:3 → new threadIndex 2
});
it('translates counterIndex via oldCounterIndexToNew for counter tracks', function () {
const oldTracks: Track[] = [
{ type: 'counter', counterIndex: 0 },
{ type: 'counter', counterIndex: 1 },
{ type: 'counter', counterIndex: 2 },
];
// Counter #1 was sanitized away; #2 is now at index 1.
const oldCounterIndexToNew = new Map<number, number>([
[0, 0],
[2, 1],
]);
const newTracks: Track[] = [
{ type: 'counter', counterIndex: 0 },
{ type: 'counter', counterIndex: 1 },
];
const map = computeOldTrackIndexToNewTrackIndexMap({
oldTracks,
newTracks,
oldThreadIndexToNew: null,
oldCounterIndexToNew,
oldStringToNewStringPlusOne: null,
});
expect(map.get(0)).toBe(0);
expect(map.has(1)).toBe(false);
expect(map.get(2)).toBe(1);
});
it('matches marker tracks by (translated threadIndex, schema name, translated markerName)', function () {
const schemaA = { name: 'CustomA' } as any;
const schemaB = { name: 'CustomB' } as any;
const oldTracks: Track[] = [
// Survives: thread translated 0→0, markerName 10→3.
{
type: 'marker',
threadIndex: 0,
markerSchema: schemaA,
markerName: 10,
} as any,
// Sanitized away because its thread is gone.
{
type: 'marker',
threadIndex: 1,
markerSchema: schemaA,
markerName: 11,
} as any,
// Sanitized away because its marker name string was removed.
{
type: 'marker',
threadIndex: 0,
markerSchema: schemaB,
markerName: 12,
} as any,
// Survives: thread 2→1, markerName 13→4.
{
type: 'marker',
threadIndex: 2,
markerSchema: schemaB,
markerName: 13,
} as any,
];
const newTracks: Track[] = [
{
type: 'marker',
threadIndex: 0,
markerSchema: schemaA,
markerName: 3,
} as any,
{
type: 'marker',
threadIndex: 1,
markerSchema: schemaB,
markerName: 4,
} as any,
];
const oldThreadIndexToNew = new Map<number, number>([
[0, 0],
[2, 1],
]);
// Old string indexes 10, 11, 13 survive (mapping to 3, 5, 4); index 12 was
// removed (encoded as 0 in the plusOne array).
const oldStringToNewStringPlusOne = new Int32Array(14);
oldStringToNewStringPlusOne[10] = 4;
oldStringToNewStringPlusOne[11] = 6;
oldStringToNewStringPlusOne[12] = 0;
oldStringToNewStringPlusOne[13] = 5;
const map = computeOldTrackIndexToNewTrackIndexMap({
oldTracks,
newTracks,
oldThreadIndexToNew,
oldCounterIndexToNew: null,
oldStringToNewStringPlusOne,
});
expect(map.get(0)).toBe(0);
expect(map.has(1)).toBe(false);
expect(map.has(2)).toBe(false);
expect(map.get(3)).toBe(1);
});
it('does not match marker tracks whose schema name differs', function () {
const oldTracks: Track[] = [
{
type: 'marker',
threadIndex: 0,
markerSchema: { name: 'CustomA' } as any,
markerName: 5,
} as any,
];
const newTracks: Track[] = [
{
type: 'marker',
threadIndex: 0,
markerSchema: { name: 'CustomB' } as any,
markerName: 5,
} as any,
];
const map = computeOldTrackIndexToNewTrackIndexMap({
oldTracks,
newTracks,
oldThreadIndexToNew: null,
oldCounterIndexToNew: null,
oldStringToNewStringPlusOne: null,
});
expect(map.has(0)).toBe(false);
});
it('handles a mixed track list (process + screenshots + visual-progress)', function () {
const oldTracks: Track[] = [
{ type: 'process', pid: '1', mainThreadIndex: 0 },
{ type: 'screenshots', id: 'win-A', threadIndex: 0 },
{ type: 'process', pid: '2', mainThreadIndex: 1 },
{ type: 'visual-progress' },
];
const newTracks: Track[] = [
{ type: 'process', pid: '1', mainThreadIndex: 0 },
{ type: 'visual-progress' },
];
const map = computeOldTrackIndexToNewTrackIndexMap({
oldTracks,
newTracks,
oldThreadIndexToNew: null,
oldCounterIndexToNew: null,
oldStringToNewStringPlusOne: null,
});
expect(map.get(0)).toBe(0); // process pid 1
expect(map.has(1)).toBe(false); // screenshot win-A removed
expect(map.has(2)).toBe(false); // process pid 2 removed
expect(map.get(3)).toBe(1); // visual-progress preserved
});
});
describe('computeHiddenTracksAfterSanitization', function () {
it('drops hidden indexes whose track was sanitized away', function () {
const oldTrackIndexToNewTrackIndex = new Map<number, number>([
[0, 0],
[2, 1],
[3, 2],
]);
const result = computeHiddenTracksAfterSanitization({
oldHiddenTracks: new Set([0, 1, 3]),
oldTrackIndexToNewTrackIndex,
});
// Old 0 → new 0 (kept); old 1 has no mapping (sanitized away); old 3 → new 2.
expect([...result].sort()).toEqual([0, 2]);
});
it('returns an empty set when no hidden tracks survive', function () {
const result = computeHiddenTracksAfterSanitization({
oldHiddenTracks: new Set([5, 6, 7]),
oldTrackIndexToNewTrackIndex: new Map([[0, 0]]),
});
expect(result.size).toBe(0);
});
it('returns an empty set when no tracks are hidden', function () {
const result = computeHiddenTracksAfterSanitization({
oldHiddenTracks: new Set(),
oldTrackIndexToNewTrackIndex: new Map([[0, 0]]),
});
expect(result.size).toBe(0);
});
});
describe('computeTrackOrderAfterSanitization', function () {
it('preserves relative order, dropping unmapped indexes', function () {
const oldTrackIndexToNewTrackIndex = new Map<number, number>([
[0, 2],
[1, 0],
[3, 1],
]);
const result = computeTrackOrderAfterSanitization({
oldTrackOrder: [3, 0, 1, 2], // index 2 has no mapping → dropped
oldTrackIndexToNewTrackIndex,
});
expect(result).toEqual([1, 2, 0]);
});
it('returns an empty array when no order entries survive', function () {
const result = computeTrackOrderAfterSanitization({
oldTrackOrder: [5, 6, 7],
oldTrackIndexToNewTrackIndex: new Map([[0, 0]]),
});
expect(result).toEqual([]);
});
});