Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core(fr): filter out manual-only categories #12367

Merged
merged 2 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
});

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that line is empty of course it's covered...

go home codecov you're drunk 😆

image

(I'm assuming this is some source mapping issue because line 66 can't be covered, it's a make TS happy check that's impossible, EDIT: I take it back the line is obviously covered it's just the branch that can't. dunno what's going on)

// 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: {},
});
Comment on lines +120 to +123
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice way of enforcing this

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