Skip to content

Commit

Permalink
core(fr): filter out manual-only categories (#12367)
Browse files Browse the repository at this point in the history
  • Loading branch information
patrickhulce committed Apr 19, 2021
1 parent 533fe24 commit e2c9e2d
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 9 deletions.
27 changes: 22 additions & 5 deletions lighthouse-core/fraggle-rock/config/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
'use strict';

const Audit = require('../../audits/audit.js');

/**
* Filters an array of artifacts down to the set that supports the specified gather mode.
*
Expand Down Expand Up @@ -47,17 +49,32 @@ function filterAuditsByAvailableArtifacts(audits, availableArtifacts) {
function filterCategoriesByAvailableAudits(categories, availableAudits) {
if (!categories) return categories;

const availableAuditIds = new Set(availableAudits.map(audit => audit.implementation.meta.id));
const availableAuditIdToMeta = new Map(
availableAudits.map(audit => [audit.implementation.meta.id, audit.implementation.meta])
);

return Object.fromEntries(
Object.entries(categories).map(([categoryId, category]) => {
const categoryEntries = Object.entries(categories)
.map(([categoryId, category]) => {
const filteredCategory = {
...category,
auditRefs: category.auditRefs.filter(ref => availableAuditIds.has(ref.id)),
auditRefs: category.auditRefs.filter(ref => availableAuditIdToMeta.has(ref.id)),
};

const didFilter = filteredCategory.auditRefs.length < category.auditRefs.length;
const hasOnlyManualAudits = filteredCategory.auditRefs.every(ref => {
const meta = availableAuditIdToMeta.get(ref.id);
if (!meta) return false;
return meta.scoreDisplayMode === Audit.SCORING_MODES.MANUAL;
});

// If we filtered out audits and the only ones left are manual, remove them too.
if (didFilter && hasOnlyManualAudits) filteredCategory.auditRefs = [];

return [categoryId, filteredCategory];
})
);
.filter(entry => typeof entry[1] === 'object' && entry[1].auditRefs.length);

return Object.fromEntries(categoryEntries);
}

/**
Expand Down
46 changes: 42 additions & 4 deletions lighthouse-core/test/fraggle-rock/config/filters-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ describe('Fraggle Rock Config Filtering', () => {
...auditMeta,
};
}
class ManualAudit extends BaseAudit {
static meta = {
id: 'manual',
scoreDisplayMode: BaseAudit.SCORING_MODES.MANUAL,
requiredArtifacts: /** @type {any} */ ([]),
...auditMeta,
};
}
class TimespanAudit extends BaseAudit {
static meta = {
id: 'timespan',
Expand All @@ -62,7 +70,7 @@ describe('Fraggle Rock Config Filtering', () => {
};
}

const audits = [SnapshotAudit, TimespanAudit, NavigationAudit].map(audit => ({
const audits = [SnapshotAudit, TimespanAudit, NavigationAudit, ManualAudit].map(audit => ({
implementation: audit,
options: {},
}));
Expand All @@ -76,6 +84,7 @@ describe('Fraggle Rock Config Filtering', () => {
const partialArtifacts = [{id: 'Snapshot', gatherer: {instance: snapshotGatherer}}];
expect(filters.filterAuditsByAvailableArtifacts(audits, partialArtifacts)).toEqual([
{implementation: SnapshotAudit, options: {}},
{implementation: ManualAudit, options: {}},
]);
});

Expand All @@ -89,6 +98,7 @@ describe('Fraggle Rock Config Filtering', () => {
snapshot: {title: 'Snapshot', auditRefs: [{id: 'snapshot', weight: 0}]},
timespan: {title: 'Timespan', auditRefs: [{id: 'timespan', weight: 0}]},
navigation: {title: 'Navigation', auditRefs: [{id: 'navigation', weight: 0}]},
manual: {title: 'Manual', auditRefs: [{id: 'manual', weight: 0}]},
mixed: {
title: 'Mixed',
auditRefs: [
Expand All @@ -106,12 +116,36 @@ describe('Fraggle Rock Config Filtering', () => {

it('should filter entire categories', () => {
const partialAudits = [{implementation: SnapshotAudit, options: {}}];
expect(filters.filterCategoriesByAvailableAudits(categories, partialAudits)).toMatchObject({
const filtered = filters.filterCategoriesByAvailableAudits(categories, partialAudits);
expect(filtered).not.toMatchObject({
timespan: {},
navigation: {},
});
expect(filtered).toMatchObject({
snapshot: {},
mixed: {},
});
});

it('should filter entire categories when all remaining audits are manual', () => {
const partialAudits = [
{implementation: SnapshotAudit, options: {}},
{implementation: ManualAudit, options: {}},
];

const filteredCategories = filters.filterCategoriesByAvailableAudits({
snapshot: categories.snapshot,
timespanWithManual: {
title: 'Timespan + Manual',
auditRefs: [
{id: 'timespan', weight: 0},
{id: 'manual', weight: 0},
],
},
}, partialAudits);
expect(filteredCategories).not.toHaveProperty('timespanWithManual');
});

it('should filter audits within categories', () => {
const partialAudits = [{implementation: SnapshotAudit, options: {}}];
const filtered = filters.filterCategoriesByAvailableAudits(categories, partialAudits);
Expand Down Expand Up @@ -139,8 +173,12 @@ describe('Fraggle Rock Config Filtering', () => {
};
expect(filters.filterConfigByGatherMode(config, 'snapshot')).toMatchObject({
artifacts: [{id: 'Snapshot'}],
audits: [{implementation: SnapshotAudit}],
categories: {snapshot: {}, mixed: {auditRefs: [{id: 'snapshot'}]}},
audits: [{implementation: SnapshotAudit}, {implementation: ManualAudit}],
categories: {
snapshot: {},
manual: {},
mixed: {auditRefs: [{id: 'snapshot'}]},
},
});
});
});
Expand Down

0 comments on commit e2c9e2d

Please sign in to comment.