-
Notifications
You must be signed in to change notification settings - Fork 9.5k
/
Copy pathproto-preprocessor-test.js
253 lines (222 loc) · 6.28 KB
/
proto-preprocessor-test.js
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
/**
* @license
* Copyright 2018 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import {getProtoRoundTrip, readJson} from '../test-utils.js';
import {processForProto} from '../../lib/proto-preprocessor.js';
const sampleJson = readJson('../results/sample_v2.json', import.meta);
const {describeIfProtoExists, sampleResultsRoundtripStr} = getProtoRoundTrip();
const roundTripJson = sampleResultsRoundtripStr && JSON.parse(sampleResultsRoundtripStr);
describe('processing for proto', () => {
it('doesn\'t modify the input object', () => {
const input = JSON.parse(JSON.stringify(sampleJson));
processForProto(input);
expect(input).toEqual(sampleJson);
});
it('keeps only necessary configSettings', () => {
const samplejson = JSON.parse(JSON.stringify(sampleJson));
const {configSettings} = samplejson;
const input = {
configSettings,
};
const expectation = {
'configSettings': {
'formFactor': 'mobile',
'locale': 'en-US',
'onlyCategories': null,
},
};
const output = processForProto(input);
expect(output).toMatchObject(expectation);
expect(Object.keys(output.configSettings)).toMatchInlineSnapshot(`
Array [
"output",
"maxWaitForFcp",
"maxWaitForLoad",
"pauseAfterFcpMs",
"pauseAfterLoadMs",
"networkQuietThresholdMs",
"cpuQuietThresholdMs",
"formFactor",
"throttling",
"throttlingMethod",
"screenEmulation",
"emulatedUserAgent",
"auditMode",
"gatherMode",
"clearStorageTypes",
"disableStorageReset",
"debugNavigation",
"channel",
"usePassiveGathering",
"disableFullPageScreenshot",
"skipAboutBlank",
"blankPage",
"ignoreStatusCode",
"budgets",
"locale",
"blockedUrlPatterns",
"additionalTraceCategories",
"extraHeaders",
"precomputedLanternData",
"onlyAudits",
"onlyCategories",
"skipAudits",
]
`);
// This must be correctly populated for appropriate report metablock rendering
expect(output.configSettings.screenEmulation).toMatchInlineSnapshot(`
Object {
"deviceScaleFactor": 1.75,
"disabled": false,
"height": 823,
"mobile": true,
"width": 412,
}
`);
});
it('cleans up default runtimeErrors', () => {
const input = {
'runtimeError': {
'code': 'NO_ERROR',
},
};
const output = processForProto(input);
expect(output).not.toHaveProperty('runtimeError');
});
it('non-default runtimeErrors are untouched', () => {
const input = {
'runtimeError': {
'code': 'ERROR_NO_DOCUMENT_REQUEST',
},
};
const output = processForProto(input);
expect(output).toMatchObject(input);
});
it('cleans up audits', () => {
const input = {
'audits': {
'critical-request-chains': {
'scoreDisplayMode': 'not-applicable',
'numericValue': 14.3,
'displayValue': ['hello %d', 123],
},
},
};
const expectation = {
'audits': {
'critical-request-chains': {
'scoreDisplayMode': 'notApplicable',
'displayValue': 'hello %d | 123',
},
},
};
const output = processForProto(input);
expect(output).toMatchObject(expectation);
});
it('removes i18n icuMessagePaths', () => {
const input = {
'i18n': {
'icuMessagePaths': {
'content': 'paths',
},
},
};
const expectation = {
'i18n': {},
};
const output = processForProto(input);
expect(output).toMatchObject(expectation);
});
it('removes empty strings', () => {
const input = {
'audits': {
'critical-request-chains': {
'details': {
'chains': {
'1': '',
},
},
},
},
'i18n': {
'icuMessagePaths': {
'content': 'paths',
},
'2': '',
'3': [
{
'hello': 'world',
'4': '',
},
],
},
};
const expectation = {
'audits': {
'critical-request-chains': {
'details': {
'chains': {},
},
},
},
'i18n': {
'3': [
{'hello': 'world'},
],
},
};
const output = processForProto(input);
expect(output).toMatchObject(expectation);
});
});
describeIfProtoExists('round trip JSON comparison subsets', () => {
let processedLHR;
beforeEach(() => {
processedLHR = processForProto(sampleJson);
});
it('has the same audit results and details (if applicable)', () => {
for (const auditId of Object.keys(processedLHR.audits)) {
expect(roundTripJson.audits[auditId]).toEqual(processedLHR.audits[auditId]);
}
});
it('has the same i18n rendererFormattedStrings', () => {
expect(roundTripJson.i18n).toMatchObject(processedLHR.i18n);
});
it('has the same top level values', () => {
// Don't test all top level properties that are objects.
Object.keys(processedLHR).forEach(audit => {
if (typeof processedLHR[audit] === 'object' && !Array.isArray(processedLHR[audit])) {
delete processedLHR[audit];
}
});
// Properties set to their type's default value will be omitted in the roundTripJson.
// For an explicit list of properties, remove sampleJson values if set to a default.
if (Array.isArray(processedLHR.stackPacks) && processedLHR.stackPacks.length === 0) {
delete processedLHR.stackPacks;
}
expect(roundTripJson).toMatchObject(processedLHR);
});
it('has the same config values', () => {
// Config settings from proto round trip should be a subset of the actual settings.
expect(processedLHR.configSettings).toMatchObject(roundTripJson.configSettings);
});
});
describeIfProtoExists('round trip JSON comparison to everything', () => {
let processedLHR;
beforeEach(() => {
processedLHR = processForProto(sampleJson);
// Proto conversion turns empty summaries into null. This is OK,
// and is handled in the PSI roundtrip just fine, but messes up the easy
// jest sub-object matcher. So, we put the empty object back in its place.
for (const audit of Object.values(roundTripJson.audits)) {
if (audit.details && audit.details.summary === null) {
audit.details.summary = {};
}
}
});
it('has the same JSON overall', () => {
expect(processedLHR).toMatchObject(roundTripJson);
});
});