11import type { CategoryConfig , Group , PluginConfig } from '@code-pushup/models' ;
2+ import {
3+ type PluginUrlContext ,
4+ createCategoryRefs ,
5+ expandCategoryRefs ,
6+ removeIndex ,
7+ shouldExpandForUrls ,
8+ validateUrlContext ,
9+ } from '@code-pushup/utils' ;
210import { LIGHTHOUSE_GROUP_SLUGS , LIGHTHOUSE_PLUGIN_SLUG } from './constants.js' ;
3- import { orderSlug , shouldExpandForUrls } from './processing.js' ;
411import { LIGHTHOUSE_GROUPS } from './runner/constants.js' ;
5- import type { LighthouseContext , LighthouseGroupSlug } from './types.js' ;
12+ import type { LighthouseGroupSlug } from './types.js' ;
613import { isLighthouseGroupSlug } from './utils.js' ;
714
815/**
@@ -31,7 +38,7 @@ export function mergeLighthouseCategories(
3138 if ( ! plugin . groups || plugin . groups . length === 0 ) {
3239 return categories ?? [ ] ;
3340 }
34- validateContext ( plugin . context ) ;
41+ validateUrlContext ( plugin . context ) ;
3542 if ( ! categories ) {
3643 return createCategories ( plugin . groups , plugin . context ) ;
3744 }
@@ -40,7 +47,7 @@ export function mergeLighthouseCategories(
4047
4148function createCategories (
4249 groups : Group [ ] ,
43- context : LighthouseContext ,
50+ context : PluginUrlContext ,
4451) : CategoryConfig [ ] {
4552 if ( ! shouldExpandForUrls ( context . urlCount ) ) {
4653 return [ ] ;
@@ -52,7 +59,7 @@ function createCategories(
5259
5360function expandCategories (
5461 categories : CategoryConfig [ ] ,
55- context : LighthouseContext ,
62+ context : PluginUrlContext ,
5663) : CategoryConfig [ ] {
5764 if ( ! shouldExpandForUrls ( context . urlCount ) ) {
5865 return categories ;
@@ -68,7 +75,7 @@ function expandCategories(
6875 */
6976export function createAggregatedCategory (
7077 groupSlug : LighthouseGroupSlug ,
71- context : LighthouseContext ,
78+ context : PluginUrlContext ,
7279) : CategoryConfig {
7380 const group = LIGHTHOUSE_GROUPS . find ( ( { slug } ) => slug === groupSlug ) ;
7481 if ( ! group ) {
@@ -81,14 +88,7 @@ export function createAggregatedCategory(
8188 slug : group . slug ,
8289 title : group . title ,
8390 ...( group . description && { description : group . description } ) ,
84- refs : Array . from ( { length : context . urlCount } , ( _ , i ) => ( {
85- plugin : LIGHTHOUSE_PLUGIN_SLUG ,
86- slug : shouldExpandForUrls ( context . urlCount )
87- ? orderSlug ( group . slug , i )
88- : group . slug ,
89- type : 'group' ,
90- weight : resolveWeight ( context . weights , i ) ,
91- } ) ) ,
91+ refs : createCategoryRefs ( group . slug , LIGHTHOUSE_PLUGIN_SLUG , context ) ,
9292 } ;
9393}
9494
@@ -98,22 +98,15 @@ export function createAggregatedCategory(
9898 */
9999export function expandAggregatedCategory (
100100 category : CategoryConfig ,
101- context : LighthouseContext ,
101+ context : PluginUrlContext ,
102102) : CategoryConfig {
103103 return {
104104 ...category ,
105- refs : category . refs . flatMap ( ref => {
106- if ( ref . plugin === LIGHTHOUSE_PLUGIN_SLUG ) {
107- return Array . from ( { length : context . urlCount } , ( _ , i ) => ( {
108- ...ref ,
109- slug : shouldExpandForUrls ( context . urlCount )
110- ? orderSlug ( ref . slug , i )
111- : ref . slug ,
112- weight : resolveWeight ( context . weights , i , ref . weight ) ,
113- } ) ) ;
114- }
115- return [ ref ] ;
116- } ) ,
105+ refs : category . refs . flatMap ( ref =>
106+ ref . plugin === LIGHTHOUSE_PLUGIN_SLUG
107+ ? expandCategoryRefs ( ref , context )
108+ : [ ref ] ,
109+ ) ,
117110 } ;
118111}
119112
@@ -122,38 +115,6 @@ export function expandAggregatedCategory(
122115 * Useful for deduplicating and normalizing group slugs when generating categories.
123116 */
124117export function extractGroupSlugs ( groups : Group [ ] ) : LighthouseGroupSlug [ ] {
125- const slugs = groups . map ( ( { slug } ) => slug . replace ( / - \d + $ / , '' ) ) ;
118+ const slugs = groups . map ( ( { slug } ) => removeIndex ( slug ) ) ;
126119 return [ ...new Set ( slugs ) ] . filter ( isLighthouseGroupSlug ) ;
127120}
128-
129- export class ContextValidationError extends Error {
130- constructor ( message : string ) {
131- super ( `Invalid Lighthouse context: ${ message } ` ) ;
132- }
133- }
134-
135- export function validateContext (
136- context : PluginConfig [ 'context' ] ,
137- ) : asserts context is LighthouseContext {
138- if ( ! context || typeof context !== 'object' ) {
139- throw new ContextValidationError ( 'must be an object' ) ;
140- }
141- const { urlCount, weights } = context ;
142- if ( typeof urlCount !== 'number' || urlCount < 0 ) {
143- throw new ContextValidationError ( 'urlCount must be a non-negative number' ) ;
144- }
145- if ( ! weights || typeof weights !== 'object' ) {
146- throw new ContextValidationError ( 'weights must be an object' ) ;
147- }
148- if ( Object . keys ( weights ) . length !== urlCount ) {
149- throw new ContextValidationError ( 'weights count must match urlCount' ) ;
150- }
151- }
152-
153- function resolveWeight (
154- weights : LighthouseContext [ 'weights' ] ,
155- index : number ,
156- userDefinedWeight ?: number ,
157- ) : number {
158- return weights [ index + 1 ] ?? userDefinedWeight ?? 1 ;
159- }
0 commit comments