-
Notifications
You must be signed in to change notification settings - Fork 470
Expand file tree
/
Copy pathwindow-console.test.ts
More file actions
289 lines (273 loc) · 8.92 KB
/
window-console.test.ts
File metadata and controls
289 lines (273 loc) · 8.92 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
/* 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 { stripIndent } from 'common-tags';
import type { ExtraPropertiesOnWindowForConsole } from '../../utils/window-console';
import {
addDataToWindowObject,
logFriendlyPreamble,
} from '../../utils/window-console';
import { storeWithSimpleProfile, storeWithProfile } from '../fixtures/stores';
import { getProfileWithMarkers } from '../fixtures/profiles/processed-profile';
import type { MixedObject } from 'firefox-profiler/types';
describe('console-accessible values on the window object', function () {
// Coerce the window into a generic object, as these values aren't defined
// in the flow type definition.
it('does not have the values initially', function () {
expect((window as any).profile).toBeUndefined();
expect((window as any).filteredProfile).toBeUndefined();
expect((window as any).callTree).toBeUndefined();
});
it('adds values to the console', function () {
const store = storeWithSimpleProfile();
const targetWin: Partial<ExtraPropertiesOnWindowForConsole> = {};
const target = addDataToWindowObject(
store.getState,
store.dispatch,
targetWin
);
expect(target.profile).toBeTruthy();
expect(target.filteredThread).toBeTruthy();
expect(target.callTree).toBeTruthy();
expect(target.shortenUrl).toBeTruthy();
});
it('logs a friendly message', function () {
const log = console.log;
const mockedLog = jest.fn();
(console as any).log = mockedLog;
logFriendlyPreamble();
expect(mockedLog.mock.calls.length).toEqual(2);
expect(mockedLog.mock.calls).toMatchSnapshot();
(console as any).log = log;
});
it('can extract gecko logs', function () {
const profile = getProfileWithMarkers([
[
'LogMessages',
170,
null,
{
type: 'Log',
module: 'nsHttp',
name: 'ParentChannelListener::ParentChannelListener [this=7fb5e19b98d0, next=7fb5f48f2320]',
},
],
[
'LogMessages',
190,
null,
{
type: 'Log',
name: 'nsJARChannel::nsJARChannel [this=0x87f1ec80]\n',
module: 'nsJarProtocol',
},
],
]);
const store = storeWithProfile(profile);
const target: MixedObject = {};
addDataToWindowObject(store.getState, store.dispatch, target);
const result = (target as any).extractGeckoLogs();
expect(result).toBe(stripIndent`
1970-01-01 00:00:00.170000000 UTC - [Unknown Process 0: Empty]: D/nsHttp ParentChannelListener::ParentChannelListener [this=7fb5e19b98d0, next=7fb5f48f2320]
1970-01-01 00:00:00.190000000 UTC - [Unknown Process 0: Empty]: D/nsJarProtocol nsJARChannel::nsJARChannel [this=0x87f1ec80]
`);
});
it('can extract gecko logs with log level already in module', function () {
const profile = getProfileWithMarkers([
[
'LogMessages',
170,
null,
{
type: 'Log',
module: 'D/nsHttp',
name: 'ParentChannelListener::ParentChannelListener [this=7fb5e19b98d0, next=7fb5f48f2320]',
},
],
[
'LogMessages',
190,
null,
{
type: 'Log',
name: 'nsJARChannel::nsJARChannel [this=0x87f1ec80]\n',
module: 'D/nsJarProtocol',
},
],
]);
const store = storeWithProfile(profile);
const target: MixedObject = {};
addDataToWindowObject(store.getState, store.dispatch, target);
const result = (target as any).extractGeckoLogs();
expect(result).toBe(stripIndent`
1970-01-01 00:00:00.170000000 UTC - [Unknown Process 0: Empty]: D/nsHttp ParentChannelListener::ParentChannelListener [this=7fb5e19b98d0, next=7fb5f48f2320]
1970-01-01 00:00:00.190000000 UTC - [Unknown Process 0: Empty]: D/nsJarProtocol nsJARChannel::nsJARChannel [this=0x87f1ec80]
`);
});
it('can extract gecko logs in new structured format', function () {
const profile = getProfileWithMarkers([
[
'nsHttp',
170,
null,
{
type: 'Log',
level: 'Error',
message:
'ParentChannelListener::ParentChannelListener [this=7fb5e19b98d0]',
},
],
[
'nsJarProtocol',
190,
null,
{
type: 'Log',
level: 'Warning',
message: 'nsJARChannel::nsJARChannel [this=0x87f1ec80]\n',
},
],
[
'cubeb',
200,
null,
{ type: 'Log', level: 'Info', message: 'cubeb_init' },
],
[
'AudioStream',
210,
null,
{ type: 'Log', level: 'Debug', message: 'AudioStream init\n' },
],
[
'VideoSink',
220,
null,
{ type: 'Log', level: 'Verbose', message: 'VideoSink::VideoSink' },
],
]);
const store = storeWithProfile(profile);
const target: MixedObject = {};
addDataToWindowObject(store.getState, store.dispatch, target);
const result = (target as any).extractGeckoLogs();
expect(result).toBe(stripIndent`
1970-01-01 00:00:00.170000000 UTC - [Unknown Process 0: Empty]: E/nsHttp ParentChannelListener::ParentChannelListener [this=7fb5e19b98d0]
1970-01-01 00:00:00.190000000 UTC - [Unknown Process 0: Empty]: W/nsJarProtocol nsJARChannel::nsJARChannel [this=0x87f1ec80]
1970-01-01 00:00:00.200000000 UTC - [Unknown Process 0: Empty]: I/cubeb cubeb_init
1970-01-01 00:00:00.210000000 UTC - [Unknown Process 0: Empty]: D/AudioStream AudioStream init
1970-01-01 00:00:00.220000000 UTC - [Unknown Process 0: Empty]: V/VideoSink VideoSink::VideoSink
`);
});
describe('totalMarkerDuration', function () {
function setup(): ExtraPropertiesOnWindowForConsole {
jest.spyOn(console, 'log').mockImplementation(() => {});
const store = storeWithSimpleProfile();
const targetWin: Partial<ExtraPropertiesOnWindowForConsole> = {};
return addDataToWindowObject(store.getState, store.dispatch, targetWin);
}
beforeEach(function () {});
it('returns 0 for empty array', function () {
const target = setup();
const result = target.totalMarkerDuration([]);
expect(result).toBe(0);
});
it('returns 0 and logs error for non-array input', function () {
const target = setup();
const consoleErrorSpy = jest
.spyOn(console, 'error')
.mockImplementation(() => {});
const result = target.totalMarkerDuration('not an array');
expect(result).toBe(0);
expect(consoleErrorSpy).toHaveBeenCalledWith(
'totalMarkerDuration expects an array of markers'
);
consoleErrorSpy.mockRestore();
});
it('calculates duration for interval markers', function () {
const target = setup();
const markers = [
{
start: 100,
end: 200,
name: 'marker1',
category: 0,
data: null,
},
{
start: 150,
end: 250,
name: 'marker2',
category: 0,
data: null,
},
];
const result = target.totalMarkerDuration(markers);
expect(result).toBe(200); // (200-100) + (250-150) = 100 + 100 = 200
// Make sure that we print a formatted log for the duration.
expect(console.log).toHaveBeenCalledWith('Total marker duration: 200ms');
});
it('skips instant markers with null end times', function () {
const target = setup();
const markers = [
{
start: 100,
end: 200,
name: 'interval',
category: 0,
threadId: null,
data: null,
},
{
start: 150,
end: null,
name: 'instant',
category: 0,
threadId: null,
data: null,
},
{
start: 300,
end: 400,
name: 'interval2',
category: 0,
threadId: null,
data: null,
},
];
const result = target.totalMarkerDuration(markers);
expect(result).toBe(200); // (200-100) + (400-300) = 100 + 100 = 200
});
it('handles mixed valid and invalid markers', function () {
const target = setup();
const markers = [
{
start: 100,
end: 200,
name: 'valid',
category: 0,
threadId: null,
data: null,
},
null,
{
start: 'invalid',
end: 300,
name: 'invalid',
category: 0,
threadId: null,
data: null,
},
{
start: 400,
end: 500,
name: 'valid2',
category: 0,
threadId: null,
data: null,
},
];
const result = target.totalMarkerDuration(markers);
expect(result).toBe(200); // (200-100) + (500-400) = 100 + 100 = 200
});
});
});