/
assertions.js
389 lines (337 loc) · 13.3 KB
/
assertions.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
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
/**
* @license Copyright 2019 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
'use strict';
const _ = require('./lodash.js');
const {computeRepresentativeRuns} = require('./representative-runs.js');
/** @typedef {keyof StrictOmit<LHCI.AssertCommand.AssertionOptions, 'aggregationMethod'>|'auditRan'} AssertionType */
/**
* @typedef AssertionResult
* @property {string} url
* @property {AssertionType} name
* @property {string} operator
* @property {number} expected
* @property {number} actual
* @property {number[]} values
* @property {LHCI.AssertCommand.AssertionFailureLevel} [level]
* @property {string} [auditId]
* @property {string|undefined} [auditProperty]
*/
/** @typedef {StrictOmit<AssertionResult, 'url'>} AssertionResultNoURL */
/** @type {Record<AssertionType, (result: LH.AuditResult) => number | undefined>} */
const AUDIT_TYPE_VALUE_GETTERS = {
auditRan: result => (result === undefined ? 0 : 1),
minScore: result => {
if (typeof result.score === 'number') return result.score;
if (result.scoreDisplayMode === 'notApplicable') return 1;
if (result.scoreDisplayMode === 'informative') return 0;
return undefined;
},
maxLength: result => result.details && result.details.items && result.details.items.length,
maxNumericValue: result => result.numericValue,
};
/** @type {Record<AssertionType, {operator: string, passesFn(actual: number, expected: number): boolean}>} */
const AUDIT_TYPE_OPERATORS = {
auditRan: {operator: '==', passesFn: (actual, expected) => actual === expected},
minScore: {operator: '>=', passesFn: (actual, expected) => actual >= expected},
maxLength: {operator: '<=', passesFn: (actual, expected) => actual <= expected},
maxNumericValue: {operator: '<=', passesFn: (actual, expected) => actual <= expected},
};
/**
* @param {any} x
* @return {x is number}
*/
const isFiniteNumber = x => typeof x === 'number' && Number.isFinite(x);
/**
* @param {LHCI.AssertCommand.AssertionFailureLevel | [LHCI.AssertCommand.AssertionFailureLevel, LHCI.AssertCommand.AssertionOptions] | undefined} assertion
* @return {[LHCI.AssertCommand.AssertionFailureLevel, LHCI.AssertCommand.AssertionOptions]}
*/
function normalizeAssertion(assertion) {
if (!assertion) return ['off', {}];
if (typeof assertion === 'string') return [assertion, {}];
return assertion;
}
/**
* @param {number[]} values
* @param {LHCI.AssertCommand.AssertionAggregationMethod} aggregationMethod
* @param {AssertionType} assertionType
* @return {number}
*/
function getValueForAggregationMethod(values, aggregationMethod, assertionType) {
if (aggregationMethod === 'median') {
const medianIndex = Math.floor((values.length - 1) / 2);
const sorted = values.slice().sort((a, b) => a - b);
if (values.length % 2 === 1) return sorted[medianIndex];
return (sorted[medianIndex] + sorted[medianIndex + 1]) / 2;
}
const useMin =
(aggregationMethod === 'optimistic' && assertionType.startsWith('max')) ||
(aggregationMethod === 'pessimistic' && assertionType.startsWith('min'));
return useMin ? Math.min(...values) : Math.max(...values);
}
/**
* @param {LH.AuditResult[]} auditResults
* @param {LHCI.AssertCommand.AssertionAggregationMethod} aggregationMethod
* @param {AssertionType} assertionType
* @param {number} expectedValue
* @return {AssertionResultNoURL[]}
*/
function getAssertionResult(auditResults, aggregationMethod, assertionType, expectedValue) {
const values = auditResults.map(AUDIT_TYPE_VALUE_GETTERS[assertionType]);
const filteredValues = values.filter(isFiniteNumber);
if (
(!filteredValues.length && aggregationMethod !== 'pessimistic') ||
(filteredValues.length !== values.length && aggregationMethod === 'pessimistic')
) {
const didRun = values.map(value => (isFiniteNumber(value) ? 1 : 0));
return [{name: 'auditRan', expected: 1, actual: 0, values: didRun, operator: '=='}];
}
const {operator, passesFn} = AUDIT_TYPE_OPERATORS[assertionType];
const actualValue = getValueForAggregationMethod(
filteredValues,
aggregationMethod,
assertionType
);
if (passesFn(actualValue, expectedValue)) return [];
return [
{
name: assertionType,
expected: expectedValue,
actual: actualValue,
values: filteredValues,
operator,
},
];
}
/**
* @param {Array<LH.AuditResult|undefined>} possibleAuditResults
* @param {LHCI.AssertCommand.AssertionOptions} options
* @return {AssertionResultNoURL[]}
*/
function getAssertionResults(possibleAuditResults, options) {
const {minScore, maxLength, maxNumericValue, aggregationMethod = 'optimistic'} = options;
if (possibleAuditResults.some(result => result === undefined)) {
return [
{
name: 'auditRan',
expected: 1,
actual: 0,
values: possibleAuditResults.map(result => (result === undefined ? 0 : 1)),
operator: '>=',
},
];
}
// We just checked that all of them are defined, so redefine for easier tsc.
const auditResults = /** @type {Array<LH.AuditResult>} */ (possibleAuditResults);
/** @type {AssertionResultNoURL[]} */
const results = [];
// Keep track of if we had a manual assertion so we know whether or not to automatically create a
// default minScore assertion.
let hadManualAssertion = false;
if (maxLength !== undefined) {
hadManualAssertion = true;
results.push(...getAssertionResult(auditResults, aggregationMethod, 'maxLength', maxLength));
}
if (maxNumericValue !== undefined) {
hadManualAssertion = true;
results.push(
...getAssertionResult(auditResults, aggregationMethod, 'maxNumericValue', maxNumericValue)
);
}
const realMinScore = minScore === undefined && !hadManualAssertion ? 1 : minScore;
if (realMinScore !== undefined) {
results.push(...getAssertionResult(auditResults, aggregationMethod, 'minScore', realMinScore));
}
return results;
}
/**
* @param {string} key
* @param {number} actual
* @param {number} expected
* @return {AssertionResultNoURL[]}
*/
function getAssertionResultsForBudgetRow(key, actual, expected) {
return getAssertionResult(
[{score: 0, numericValue: actual}],
'pessimistic',
'maxNumericValue',
expected
).map(assertion => {
return {...assertion, auditProperty: key};
});
}
/**
* Budgets are somewhat unique in that they are already asserted at collection time by Lighthouse.
* We won't use any of our fancy logic here and we just want to pass on whatever Lighthouse found
* by creating fake individual audit results to assert against for each individual table row
* (de-duped by "<resource type>.<property>").
*
* @param {LH.AuditResult[]} auditResults
* @return {AssertionResultNoURL[]}
*/
function getBudgetAssertionResults(auditResults) {
/** @type {AssertionResultNoURL[]} */
const results = [];
/** @type {Set<string>} */
const resultsKeys = new Set();
for (const auditResult of auditResults) {
if (!auditResult.details || !auditResult.details.items) continue;
for (const budgetRow of auditResult.details.items) {
const sizeKey = `${budgetRow.resourceType}.size`;
const countKey = `${budgetRow.resourceType}.count`;
if (budgetRow.sizeOverBudget && !resultsKeys.has(sizeKey)) {
const actual = budgetRow.size;
const expected = actual - budgetRow.sizeOverBudget;
results.push(...getAssertionResultsForBudgetRow(sizeKey, actual, expected));
resultsKeys.add(sizeKey);
}
if (budgetRow.countOverBudget && !resultsKeys.has(countKey)) {
const actual = budgetRow.requestCount;
const overBudgetMatch = budgetRow.countOverBudget.match(/\d+/);
if (!overBudgetMatch) continue;
const overBudget = Number(overBudgetMatch[0]) || 0;
const expected = actual - overBudget;
results.push(...getAssertionResultsForBudgetRow(countKey, actual, expected));
resultsKeys.add(countKey);
}
}
}
return results;
}
/**
* @param {string} pattern
* @param {LH.Result} lhr
* @return {boolean}
*/
function doesLHRMatchPattern(pattern, lhr) {
return new RegExp(pattern).test(lhr.finalUrl);
}
/**
* Gets the assertion results for a particular audit. This method delegates some of the unique
* handling for budgets and auditProperty assertions as necessary.
*
* @param {string} auditId
* @param {Array<string>|undefined} auditProperty
* @param {Array<LH.AuditResult>} auditResults
* @param {LHCI.AssertCommand.AssertionOptions} assertionOptions
* @return {AssertionResultNoURL[]}
*/
function getAssertionResultsForAudit(auditId, auditProperty, auditResults, assertionOptions) {
if (auditId === 'performance-budget') {
return getBudgetAssertionResults(auditResults);
} else if (auditId === 'resource-summary' && auditProperty) {
if (auditProperty.length !== 2 || !['size', 'count'].includes(auditProperty[1])) {
throw new Error(`Invalid resource-summary assertion "${auditProperty}"`);
}
const psuedoAuditResults = auditResults.map(result => {
if (!result || !result.details || !result.details.items) return;
const itemKey = auditProperty[1] === 'count' ? 'requestCount' : 'size';
const item = result.details.items.find(item => item.resourceType === auditProperty[0]);
if (!item) return;
return {...result, numericValue: item[itemKey]};
});
return getAssertionResults(psuedoAuditResults, assertionOptions).map(result => ({
...result,
auditProperty: auditProperty.join('.'),
}));
} else {
return getAssertionResults(auditResults, assertionOptions);
}
}
/**
* @param {LHCI.AssertCommand.BaseOptions} baseOptions
* @param {LH.Result[]} unfilteredLhrs
*/
function resolveAssertionOptionsAndLhrs(baseOptions, unfilteredLhrs) {
const {preset = '', ...optionOverrides} = baseOptions;
let optionsToUse = optionOverrides;
const presetMatch = preset.match(/lighthouse:(.*)$/);
if (presetMatch) {
const presetData = require(`./presets/${presetMatch[1]}.js`);
optionsToUse = _.merge(_.cloneDeep(presetData), optionsToUse);
}
const {assertions = {}, matchingUrlPattern: urlPattern} = optionsToUse;
const lhrs = urlPattern
? unfilteredLhrs.filter(lhr => doesLHRMatchPattern(urlPattern, lhr))
: unfilteredLhrs;
// Double-check we've only got one URL to look at that should have been pre-grouped in `getAllAssertionResults`.
const uniqueURLs = new Set(lhrs.map(lhr => lhr.finalUrl));
if (uniqueURLs.size > 1) throw new Error('Can only assert one URL at a time!');
const medianLhrs = computeRepresentativeRuns([
lhrs.map(lhr => /** @type {[LH.Result, LH.Result]} */ ([lhr, lhr])),
]);
const auditsToAssert = [...new Set(Object.keys(assertions).map(_.kebabCase))].map(
assertionKey => {
const [auditId, ...rest] = assertionKey.split('.');
if (!rest.length) return {assertionKey, auditId};
return {assertionKey, auditId, auditProperty: rest};
}
);
return {
assertions,
auditsToAssert,
medianLhrs,
lhrs: lhrs,
url: (lhrs[0] && lhrs[0].finalUrl) || '',
};
}
/**
* @param {LHCI.AssertCommand.BaseOptions} baseOptions
* @param {LH.Result[]} unfilteredLhrs
* @return {AssertionResult[]}
*/
function getAllFilteredAssertionResults(baseOptions, unfilteredLhrs) {
const {assertions, auditsToAssert, medianLhrs, lhrs, url} = resolveAssertionOptionsAndLhrs(
baseOptions,
unfilteredLhrs
);
// If we don't have any data, just return early.
if (!lhrs.length) return [];
/** @type {AssertionResult[]} */
const results = [];
for (const {assertionKey, auditId, auditProperty} of auditsToAssert) {
const [level, assertionOptions] = normalizeAssertion(assertions[assertionKey]);
if (level === 'off') continue;
const lhrsToUseForAudit =
assertionOptions.aggregationMethod === 'median-run' ? medianLhrs : lhrs;
const auditResults = lhrsToUseForAudit.map(lhr => lhr.audits[auditId]);
const assertionResults = getAssertionResultsForAudit(
auditId,
auditProperty,
auditResults,
assertionOptions
);
for (const result of assertionResults) {
results.push({...result, auditId, level, url});
}
}
return results;
}
/**
* @param {LHCI.AssertCommand.Options} options
* @param {LH.Result[]} lhrs
* @return {AssertionResult[]}
*/
function getAllAssertionResults(options, lhrs) {
const groupedByURL = _.groupBy(lhrs, lhr => lhr.finalUrl);
/** @type {LHCI.AssertCommand.BaseOptions[]} */
let arrayOfOptions = [options];
if (options.assertMatrix) {
const {assertMatrix, ...restOptions} = options;
if (Object.keys(restOptions).length) {
throw new Error('Cannot use assertMatrix with other options');
}
arrayOfOptions = assertMatrix;
}
/** @type {AssertionResult[]} */
const results = [];
for (const lhrSet of groupedByURL) {
for (const baseOptions of arrayOfOptions) {
results.push(...getAllFilteredAssertionResults(baseOptions, lhrSet));
}
}
return results;
}
module.exports = {getAllAssertionResults};