Skip to content

Commit 1908892

Browse files
committed
feat(plugin-lighthouse): convert treemap-data details to trees
1 parent 18d4230 commit 1908892

File tree

5 files changed

+246
-11
lines changed

5 files changed

+246
-11
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`parseTreemapDataToBasicTrees > should convert root nodes to basic trees 1`] = `
4+
[
5+
{
6+
"root": {
7+
"children": [
8+
{
9+
"children": [
10+
{
11+
"name": "failbot/failbot-error.ts",
12+
"values": {
13+
"resourceBytes": "237 B",
14+
"unusedBytes": "113 B",
15+
},
16+
},
17+
{
18+
"name": "remove-child-patch/remove-child-patch.ts",
19+
"values": {
20+
"resourceBytes": "268 B",
21+
},
22+
},
23+
{
24+
"name": "fetch-overrides/fetch-overrides.ts",
25+
"values": {
26+
"resourceBytes": "376 B",
27+
"unusedBytes": "263 B",
28+
},
29+
},
30+
],
31+
"name": "ui/packages",
32+
"values": {
33+
"resourceBytes": "881 B",
34+
"unusedBytes": "376 B",
35+
},
36+
},
37+
{
38+
"name": "app/assets/modules/environment.ts",
39+
"values": {
40+
"resourceBytes": "42 B",
41+
},
42+
},
43+
{
44+
"name": "(unmapped)",
45+
"values": {
46+
"resourceBytes": "515 B",
47+
},
48+
},
49+
],
50+
"name": "https://github.githubassets.com/assets/environment-7b93e0f0c8ff.js",
51+
"values": {
52+
"resourceBytes": "1.4 kB",
53+
"unusedBytes": "376 B",
54+
},
55+
},
56+
"type": "basic",
57+
},
58+
{
59+
"root": {
60+
"children": [
61+
{
62+
"children": [
63+
{
64+
"name": "commenting/edit.ts",
65+
"values": {
66+
"resourceBytes": "9.07 kB",
67+
"unusedBytes": "8.03 kB",
68+
},
69+
},
70+
{
71+
"name": "html-validation.ts",
72+
"values": {
73+
"duplicatedNormalizedModuleName": "app/assets/modules/github/behaviors/html-validation.ts",
74+
"resourceBytes": "2.03 kB",
75+
"unusedBytes": "2.03 kB",
76+
},
77+
},
78+
],
79+
"name": "app/assets/modules/github/behaviors",
80+
"values": {
81+
"resourceBytes": "11.1 kB",
82+
"unusedBytes": "10.06 kB",
83+
},
84+
},
85+
{
86+
"name": "(unmapped)",
87+
"values": {
88+
"resourceBytes": "534 B",
89+
"unusedBytes": "65 B",
90+
},
91+
},
92+
],
93+
"name": "https://github.githubassets.com/assets/app_assets_modules_github_behaviors_commenting_edit_ts-app_assets_modules_github_behaviors_ht-83c235-fb43816ab83c.js",
94+
"values": {
95+
"resourceBytes": "11.62 kB",
96+
"unusedBytes": "10.12 kB",
97+
},
98+
},
99+
"type": "basic",
100+
},
101+
]
102+
`;

packages/plugin-lighthouse/src/lib/runner/details/details.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@ import { bold, yellow } from 'ansis';
22
import type { FormattedIcu } from 'lighthouse';
33
import type Details from 'lighthouse/types/lhr/audit-details';
44
import type { Result } from 'lighthouse/types/lhr/audit-result';
5-
import type { AuditDetails, Table } from '@code-pushup/models';
5+
import type { AuditDetails } from '@code-pushup/models';
66
import { ui } from '@code-pushup/utils';
77
import { PLUGIN_SLUG } from '../constants.js';
88
import { parseCriticalRequestChainToAuditDetails } from './critical-request-chain.type.js';
99
import { parseOpportunityToAuditDetailsTable } from './opportunity.type.js';
1010
import { parseTableToAuditDetailsTable } from './table.type.js';
11+
import { parseTreemapDataToBasicTrees } from './treemap-data.type.js';
1112

1213
export function toAuditDetails<T extends FormattedIcu<Details>>(
1314
details: T | undefined,
@@ -20,15 +21,16 @@ export function toAuditDetails<T extends FormattedIcu<Details>>(
2021

2122
switch (type) {
2223
case 'table':
23-
const table: Table | undefined = parseTableToAuditDetailsTable(details);
24+
const table = parseTableToAuditDetailsTable(details);
2425
return table ? { table } : {};
2526
case 'opportunity':
26-
const opportunity: Table | undefined =
27-
parseOpportunityToAuditDetailsTable(details);
27+
const opportunity = parseOpportunityToAuditDetailsTable(details);
2828
return opportunity ? { table: opportunity } : {};
2929
case 'criticalrequestchain':
3030
return parseCriticalRequestChainToAuditDetails(details);
31-
case 'treemap-data': // TODO: implement
31+
case 'treemap-data':
32+
const trees = parseTreemapDataToBasicTrees(details);
33+
return { trees };
3234

3335
// TODO: add 'list' once array of tables supported in audit details
3436

@@ -38,7 +40,8 @@ export function toAuditDetails<T extends FormattedIcu<Details>>(
3840
}
3941

4042
// @TODO implement all details
41-
export const unsupportedDetailTypes = new Set([
43+
export const unsupportedDetailTypes = new Set<Details['type']>([
44+
'list',
4245
'debugdata',
4346
'screenshot',
4447
'filmstrip',
@@ -48,8 +51,8 @@ export function logUnsupportedDetails(lhrAudits: Result[]) {
4851
const slugsWithDetailParsingErrors = [
4952
...new Set(
5053
lhrAudits
51-
.filter(({ details }) =>
52-
unsupportedDetailTypes.has(details?.type as string),
54+
.filter(
55+
({ details }) => details && unsupportedDetailTypes.has(details.type),
5356
)
5457
.map(({ details }) => details?.type),
5558
),

packages/plugin-lighthouse/src/lib/runner/details/details.unit.test.ts

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,13 +304,32 @@ describe('toAuditDetails', () => {
304304
expect(outputs).toStrictEqual({});
305305
});
306306

307-
it('should ignore unsupported detail type treemap yet', () => {
307+
it('should render audit details of type treemap', () => {
308308
const outputs = toAuditDetails({
309309
type: 'treemap-data',
310-
nodes: [],
310+
nodes: [
311+
{
312+
name: 'http://127.0.0.1:8000/js/index.js',
313+
resourceBytes: 1_212_311,
314+
unusedBytes: 634_854,
315+
},
316+
],
311317
});
312318

313-
expect(outputs).toStrictEqual({});
319+
expect(outputs).toStrictEqual<AuditDetails>({
320+
trees: [
321+
{
322+
type: 'basic',
323+
root: {
324+
name: 'http://127.0.0.1:8000/js/index.js',
325+
values: {
326+
resourceBytes: '1.16 MB',
327+
unusedBytes: '619.97 kB',
328+
},
329+
},
330+
},
331+
],
332+
});
314333
});
315334

316335
it('should render audit details of type criticalrequestchain', () => {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type { Treemap } from 'lighthouse';
2+
import type Details from 'lighthouse/types/lhr/audit-details';
3+
import type { BasicTree, BasicTreeNode } from '@code-pushup/models';
4+
import { formatBytes } from '@code-pushup/utils';
5+
6+
export function parseTreemapDataToBasicTrees(
7+
details: Details.TreemapData,
8+
): BasicTree[] {
9+
return details.nodes.map(node => ({
10+
type: 'basic',
11+
root: treemapNodeToBasicTreeNode(node),
12+
}));
13+
}
14+
15+
function treemapNodeToBasicTreeNode(node: Treemap.Node): BasicTreeNode {
16+
return {
17+
name: node.name,
18+
values: {
19+
resourceBytes: formatBytes(node.resourceBytes),
20+
...(node.unusedBytes != null && {
21+
unusedBytes: formatBytes(node.unusedBytes),
22+
}),
23+
...(node.duplicatedNormalizedModuleName && {
24+
duplicatedNormalizedModuleName: node.duplicatedNormalizedModuleName,
25+
}),
26+
},
27+
...(node.children && {
28+
children: node.children.map(treemapNodeToBasicTreeNode),
29+
}),
30+
};
31+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import { parseTreemapDataToBasicTrees } from './treemap-data.type.js';
2+
3+
describe('parseTreemapDataToBasicTrees', () => {
4+
it('should convert root nodes to basic trees', () => {
5+
expect(
6+
parseTreemapDataToBasicTrees({
7+
type: 'treemap-data',
8+
nodes: [
9+
{
10+
name: 'https://github.githubassets.com/assets/environment-7b93e0f0c8ff.js',
11+
resourceBytes: 1438,
12+
unusedBytes: 376,
13+
children: [
14+
{
15+
name: 'ui/packages',
16+
resourceBytes: 881,
17+
unusedBytes: 376,
18+
children: [
19+
{
20+
name: 'failbot/failbot-error.ts',
21+
resourceBytes: 237,
22+
unusedBytes: 113,
23+
},
24+
{
25+
name: 'remove-child-patch/remove-child-patch.ts',
26+
resourceBytes: 268,
27+
},
28+
{
29+
name: 'fetch-overrides/fetch-overrides.ts',
30+
resourceBytes: 376,
31+
unusedBytes: 263,
32+
},
33+
],
34+
},
35+
{
36+
name: 'app/assets/modules/environment.ts',
37+
resourceBytes: 42,
38+
},
39+
{
40+
name: '(unmapped)',
41+
resourceBytes: 515,
42+
},
43+
],
44+
},
45+
{
46+
name: 'https://github.githubassets.com/assets/app_assets_modules_github_behaviors_commenting_edit_ts-app_assets_modules_github_behaviors_ht-83c235-fb43816ab83c.js',
47+
resourceBytes: 11_903,
48+
unusedBytes: 10_362,
49+
children: [
50+
{
51+
name: 'app/assets/modules/github/behaviors',
52+
resourceBytes: 11_369,
53+
unusedBytes: 10_297,
54+
children: [
55+
{
56+
name: 'commenting/edit.ts',
57+
resourceBytes: 9290,
58+
unusedBytes: 8218,
59+
},
60+
{
61+
name: 'html-validation.ts',
62+
resourceBytes: 2079,
63+
unusedBytes: 2079,
64+
duplicatedNormalizedModuleName:
65+
'app/assets/modules/github/behaviors/html-validation.ts',
66+
},
67+
],
68+
},
69+
{
70+
name: '(unmapped)',
71+
resourceBytes: 534,
72+
unusedBytes: 65,
73+
},
74+
],
75+
},
76+
],
77+
}),
78+
).toMatchSnapshot();
79+
});
80+
});

0 commit comments

Comments
 (0)