-
Notifications
You must be signed in to change notification settings - Fork 481
Expand file tree
/
Copy pathsource-map-symbolication.test.ts
More file actions
473 lines (419 loc) · 17.1 KB
/
Copy pathsource-map-symbolication.test.ts
File metadata and controls
473 lines (419 loc) · 17.1 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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/* 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/. */
// End-to-end test for the receive-profile -> JS source map symbolication
// pipeline. Drives `loadProfile` with a mocked BrowserConnection that serves
// a real source map and minified bundle, then asserts on:
// - what was fetched (filtering by id / sourceMapURL / WebChannel version),
// - the status reducer transitions,
// - the post-symbolication profile state,
// - the source view selector reading the original-source content.
//
// Worker plumbing: the production worker is bundled by esbuild and replaced
// in jest.config.js with a no-op stub. For these tests we override
// global.Worker with an in-process variant that calls
// runSourceMapSymbolicationCore directly, so the real
// doSourceMapSymbolication action runs its full Redux flow
// (START_SOURCE_MAP_SYMBOLICATION -> BULK_SOURCE_MAP_SYMBOLICATION /
// SOURCE_MAP_SYMBOLICATION_FAILED) and the worker internals are exercised.
import { SourceMapGenerator } from 'source-map';
import { runSourceMapSymbolicationCore } from '../../profile-logic/source-map-symbolication';
import { loadProfile } from '../../actions/receive-profile';
import {
getSourceMapSymbolicationStatus,
getRawProfileSharedData,
} from '../../selectors/profile';
import { getSourceViewCode } from '../../selectors/code';
import { stateFromLocation } from '../../app-logic/url-handling';
import { updateUrlState } from '../../actions/app';
import { blankStore } from '../fixtures/stores';
import { getProfileFromTextSamples } from '../fixtures/profiles/processed-profile';
import type { BrowserConnection } from '../../app-logic/browser-connection';
import type { Profile } from 'firefox-profiler/types';
import type {
WorkerInput,
WorkerOutput,
} from '../../profile-logic/source-map-worker-types';
import type { RawSourceMap } from 'source-map';
// Original source file. Indentation and blank lines matter: the mappings
// below address specific (line, column) positions.
const ORIGINAL_SOURCE = `function greet(name) {
return "Hello, " + name;
}
`;
// Minified single-line bundle. `greet` -> `a`, `name` -> `b`.
const BUNDLE_SOURCE = 'function a(b){return"Hello, "+b}';
const ORIGINAL_FILENAME = 'hello.js';
// Build a source map for BUNDLE_SOURCE referencing ORIGINAL_SOURCE. Includes
// sourcesContent so symbolication can populate sources.content for offline
// source viewing.
function buildSourceMap(bundleFilename: string): RawSourceMap {
const gen = new SourceMapGenerator({ file: bundleFilename });
gen.setSourceContent(ORIGINAL_FILENAME, ORIGINAL_SOURCE);
// bundle 1:0 ('function') -> original 1:0
gen.addMapping({
source: ORIGINAL_FILENAME,
original: { line: 1, column: 0 },
generated: { line: 1, column: 0 },
name: 'greet',
});
// bundle 1:9 ('a' identifier) -> original 1:9 ('greet' identifier)
gen.addMapping({
source: ORIGINAL_FILENAME,
original: { line: 1, column: 9 },
generated: { line: 1, column: 9 },
name: 'greet',
});
// bundle 1:14 ('return') -> original 2:2 ('return' inside the body)
gen.addMapping({
source: ORIGINAL_FILENAME,
original: { line: 2, column: 2 },
generated: { line: 1, column: 14 },
});
return JSON.parse(gen.toString());
}
type SourceDescriptor = {
filename: string;
id: string | null;
sourceMapURL: string | null;
};
// Build a profile with one JS func per source descriptor. Each func is
// positioned at bundle (line 1, col 10) — the start of the identifier `a` in
// BUNDLE_SOURCE — and each frame at (line 1, col 15) — the start of the
// `return` keyword. That way every eligible source produces a successful
// symbolication when paired with buildSourceMap.
function makeProfileWithJsSources(sources: SourceDescriptor[]): Profile {
// One thread per source so each source appears as the funcTable.source of a
// visible thread's stack.
const textSamples = sources.map((s) => `Ajs[file:${s.filename}]`);
const { profile } = getProfileFromTextSamples(...textSamples);
// Skip native symbolication — we only care about JS source map
// symbolication here.
profile.meta.symbolicated = true;
const {
funcTable,
frameTable,
sources: sourceTable,
stringArray,
} = profile.shared;
for (const desc of sources) {
const filenameStrIdx = stringArray.indexOf(desc.filename);
const sourceIndex = sourceTable.filename.findIndex(
(f) => f === filenameStrIdx
);
if (sourceIndex === -1) {
throw new Error(`No source row for ${desc.filename}`);
}
sourceTable.id[sourceIndex] = desc.id;
if (desc.sourceMapURL !== null) {
const urlIdx = stringArray.length;
stringArray.push(desc.sourceMapURL);
sourceTable.sourceMapURL[sourceIndex] = urlIdx;
} else {
sourceTable.sourceMapURL[sourceIndex] = null;
}
}
// Position every func + frame inside the bundle. funcs and frames are
// co-indexed with sources in the order they were added.
for (let i = 0; i < sources.length; i++) {
funcTable.lineNumber[i] = 1;
funcTable.columnNumber[i] = 10;
frameTable.line[i] = 1;
frameTable.column[i] = 15;
}
return profile;
}
type MockBrowserConnection = BrowserConnection & {
getSourceMap: jest.Mock;
getJSSource: jest.Mock;
};
// Build a BrowserConnection that serves the given source map and bundle
// fixtures. WebChannel version 7+ enables source-map fetching in
// finalizeProfileView; v6 disables it.
function makeMockBrowserConnection(opts: {
supportsSourceMapFetching: boolean;
sourceMapsById?: Map<string, RawSourceMap>;
jsSourcesById?: Map<string, string>;
}): MockBrowserConnection {
const sourceMapsById = opts.sourceMapsById ?? new Map();
const jsSourcesById = opts.jsSourcesById ?? new Map();
return {
supportsGetSourceMap: opts.supportsSourceMapFetching,
getSourceMap: jest.fn(async (id: string) => {
const map = sourceMapsById.get(id);
if (!map) {
throw new Error(`No source map fixture for "${id}"`);
}
return map;
}),
getJSSource: jest.fn(async (id: string) => {
const src = jsSourcesById.get(id);
if (src === undefined) {
throw new Error(`No JS source fixture for "${id}"`);
}
return src;
}),
getProfile: jest.fn(),
getExternalMarkers: jest.fn(),
getExternalPowerTracks: jest.fn(),
querySymbolicationApi: jest.fn(),
getSymbolTable: jest.fn(),
getPageFavicons: jest.fn(),
showFunctionInDevtools: jest.fn(),
} as unknown as MockBrowserConnection;
}
// In-process replacement for global.Worker that runs the source-map worker
// core directly. See file-top comment for context.
class InProcessSourceMapWorker {
onmessage: ((event: { data: WorkerOutput }) => void) | null = null;
onerror: ((event: ErrorEvent) => void) | null = null;
postMessage(input: WorkerInput): void {
runSourceMapSymbolicationCore(input, 'ignored-in-node').then((output) => {
if (this.onmessage) {
this.onmessage({ data: output });
}
});
}
terminate(): void {}
}
describe('receive-profile -> JS source map symbolication', function () {
let savedWorker: unknown;
beforeEach(function () {
savedWorker = (global as any).Worker;
(global as any).Worker = InProcessSourceMapWorker;
// The `source-map` library logs a harmless `console.debug` whenever
// `SourceMapConsumer.initialize` runs under Node (it reads the wasm via
// `fs`, making initialization a no-op). Silence console.debug here so it
// doesn't clutter test output.
jest.spyOn(console, 'debug').mockImplementation(() => {});
});
afterEach(function () {
(global as any).Worker = savedWorker;
});
describe('fetching and filtering', function () {
it('fetches source maps and bundle sources for every source with a UUID and sourceMapURL', async function () {
const profile = makeProfileWithJsSources([
{
filename: 'bundle-a.js',
id: 'uuid-a',
sourceMapURL: 'https://example.com/bundle-a.js.map',
},
{
filename: 'bundle-b.js',
id: 'uuid-b',
sourceMapURL: 'https://example.com/bundle-b.js.map',
},
]);
const browserConnection = makeMockBrowserConnection({
supportsSourceMapFetching: true,
sourceMapsById: new Map([
['uuid-a', buildSourceMap('bundle-a.js')],
['uuid-b', buildSourceMap('bundle-b.js')],
]),
jsSourcesById: new Map([
['uuid-a', BUNDLE_SOURCE],
['uuid-b', BUNDLE_SOURCE],
]),
});
const { dispatch, getState } = blankStore();
await dispatch(loadProfile(profile, { browserConnection }));
expect(
browserConnection.getSourceMap.mock.calls.map((c) => c[0])
).toEqual(expect.arrayContaining(['uuid-a', 'uuid-b']));
expect(browserConnection.getJSSource.mock.calls.map((c) => c[0])).toEqual(
expect.arrayContaining(['uuid-a', 'uuid-b'])
);
// Symbolication actually ran: both funcs are now named `greet`.
const { funcTable, stringArray } = getRawProfileSharedData(getState());
expect(stringArray[funcTable.name[0]]).toBe('greet');
expect(stringArray[funcTable.name[1]]).toBe('greet');
});
it('does not fetch source maps when the browser lacks source-map support', async function () {
const profile = makeProfileWithJsSources([
{
filename: 'bundle.js',
id: 'uuid-x',
sourceMapURL: 'https://example.com/bundle.js.map',
},
]);
const browserConnection = makeMockBrowserConnection({
supportsSourceMapFetching: false,
});
const { dispatch, getState } = blankStore();
await dispatch(loadProfile(profile, { browserConnection }));
expect(browserConnection.getSourceMap).not.toHaveBeenCalled();
expect(browserConnection.getJSSource).not.toHaveBeenCalled();
// No symbolication: the bundled name stays as-is.
const { funcTable, stringArray } = getRawProfileSharedData(getState());
expect(stringArray[funcTable.name[0]]).toBe('Ajs');
});
it('does not fetch sources without an id or without a sourceMapURL', async function () {
// - has-id-no-map: id set but no sourceMapURL → skip
// - has-map-no-id: sourceMapURL set but null id → skip
// - both: should be fetched
const profile = makeProfileWithJsSources([
{
filename: 'has-id-no-map.js',
id: 'uuid-1',
sourceMapURL: null,
},
{
filename: 'has-map-no-id.js',
id: null,
sourceMapURL: 'https://example.com/has-map-no-id.js.map',
},
{
filename: 'both.js',
id: 'uuid-3',
sourceMapURL: 'https://example.com/both.js.map',
},
]);
const browserConnection = makeMockBrowserConnection({
supportsSourceMapFetching: true,
sourceMapsById: new Map([['uuid-3', buildSourceMap('both.js')]]),
jsSourcesById: new Map([['uuid-3', BUNDLE_SOURCE]]),
});
const { dispatch, getState } = blankStore();
await dispatch(loadProfile(profile, { browserConnection }));
expect(
browserConnection.getSourceMap.mock.calls.map((c) => c[0])
).toEqual(['uuid-3']);
// Only the eligible source got symbolicated.
const { funcTable, stringArray } = getRawProfileSharedData(getState());
expect(stringArray[funcTable.name[0]]).toBe('Ajs');
expect(stringArray[funcTable.name[1]]).toBe('Ajs');
expect(stringArray[funcTable.name[2]]).toBe('greet');
});
it('silently continues when getSourceMap or getJSSource fails', async function () {
const profile = makeProfileWithJsSources([
{
filename: 'broken.js',
id: 'uuid-broken',
sourceMapURL: 'https://example.com/broken.js.map',
},
]);
// No fixtures registered: getSourceMap and getJSSource throw, but the
// catches in doResolveSourceMaps swallow them.
const browserConnection = makeMockBrowserConnection({
supportsSourceMapFetching: true,
});
// Silence the expected warnings.
jest.spyOn(console, 'warn').mockImplementation(() => {});
const { dispatch, getState } = blankStore();
await dispatch(loadProfile(profile, { browserConnection }));
expect(browserConnection.getSourceMap).toHaveBeenCalledWith(
'uuid-broken'
);
expect(browserConnection.getJSSource).toHaveBeenCalledWith('uuid-broken');
// No symbolication applied — the load completes cleanly with the func
// unchanged.
const { funcTable, stringArray } = getRawProfileSharedData(getState());
expect(stringArray[funcTable.name[0]]).toBe('Ajs');
expect(getSourceMapSymbolicationStatus(getState())).toBe('INACTIVE');
});
});
describe('status transitions', function () {
it('moves through FETCHING and SYMBOLICATING before settling back to INACTIVE', async function () {
const profile = makeProfileWithJsSources([
{
filename: 'bundle.js',
id: 'uuid-x',
sourceMapURL: 'https://example.com/bundle.js.map',
},
]);
const browserConnection = makeMockBrowserConnection({
supportsSourceMapFetching: true,
sourceMapsById: new Map([['uuid-x', buildSourceMap('bundle.js')]]),
jsSourcesById: new Map([['uuid-x', BUNDLE_SOURCE]]),
});
const store = blankStore();
// Record every distinct sourceMapSymbolicationStatus value the store
// passes through.
const statuses: string[] = [
getSourceMapSymbolicationStatus(store.getState()),
];
store.subscribe(() => {
const current = getSourceMapSymbolicationStatus(store.getState());
if (statuses[statuses.length - 1] !== current) {
statuses.push(current);
}
});
await store.dispatch(loadProfile(profile, { browserConnection }));
// FETCHING is set when fetching starts and reverted to INACTIVE on
// DONE_SOURCE_MAP_FETCHING; SYMBOLICATING is set when the worker starts
// and cleared by BULK_SOURCE_MAP_SYMBOLICATION on success.
expect(statuses).toEqual([
'INACTIVE',
'FETCHING',
'INACTIVE',
'SYMBOLICATING',
'INACTIVE',
]);
});
});
describe('post-symbolication profile state', function () {
async function loadAndSymbolicate() {
const profile = makeProfileWithJsSources([
{
filename: 'bundle.js',
id: 'uuid-x',
sourceMapURL: 'https://example.com/bundle.js.map',
},
]);
const browserConnection = makeMockBrowserConnection({
supportsSourceMapFetching: true,
sourceMapsById: new Map([['uuid-x', buildSourceMap('bundle.js')]]),
jsSourcesById: new Map([['uuid-x', BUNDLE_SOURCE]]),
});
const { dispatch, getState } = blankStore();
await dispatch(loadProfile(profile, { browserConnection }));
return { dispatch, getState };
}
it('renames the minified function to its original identifier', async function () {
const { getState } = await loadAndSymbolicate();
const { funcTable, stringArray } = getRawProfileSharedData(getState());
// The bundle defined `function a(b)`. After symbolication, the scope
// tree on the original source recovers the pre-minified name.
expect(stringArray[funcTable.name[0]]).toBe('greet');
});
it('remaps the frame execution position to the original source', async function () {
const { getState } = await loadAndSymbolicate();
const { frameTable, sourceLocationTable, sources, stringArray } =
getRawProfileSharedData(getState());
const frameOriginalLocationIdx = frameTable.originalLocation[0];
expect(frameOriginalLocationIdx).not.toBeNull();
const originalSourceIdx =
sourceLocationTable.source[frameOriginalLocationIdx!];
expect(stringArray[sources.filename[originalSourceIdx]]).toBe(
ORIGINAL_FILENAME
);
// bundle 1:15 ('return') maps to hello.js 2:3 (1-based).
expect(sourceLocationTable.line[frameOriginalLocationIdx!]).toBe(2);
expect(sourceLocationTable.column[frameOriginalLocationIdx!]).toBe(3);
});
it('returns the original source content via the source view selector', async function () {
const { dispatch, getState } = await loadAndSymbolicate();
// The original source was appended to the sources table during
// symbolication. Find its index by filename.
const { sources, stringArray } = getRawProfileSharedData(getState());
const originalSourceIndex = sources.filename.findIndex(
(idx) => stringArray[idx] === ORIGINAL_FILENAME
);
expect(originalSourceIndex).toBeGreaterThanOrEqual(0);
// Point the source view URL state at the original source.
dispatch(
updateUrlState(
stateFromLocation({
pathname: '/public/fakehash/',
search: `?sourceViewIndex=${originalSourceIndex}`,
hash: '',
})
)
);
expect(getSourceViewCode(getState())).toEqual({
type: 'AVAILABLE',
code: ORIGINAL_SOURCE,
});
});
});
});