-
Notifications
You must be signed in to change notification settings - Fork 16
feat(plugin-axe): aggregate categories #1182
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| import { axePlugin } from './lib/axe-plugin.js'; | ||
|
|
||
| export default axePlugin; | ||
| export type { AxePluginOptions } from './lib/config.js'; | ||
| export type { AxePreset } from './lib/config.js'; | ||
|
|
||
| export type { AxePluginOptions, AxePreset } from './lib/config.js'; | ||
| export type { AxeGroupSlug } from './lib/groups.js'; | ||
|
|
||
| export { axeAuditRef, axeGroupRef } from './lib/utils.js'; | ||
| export { axeCategories } from './lib/categories.js'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| import type { CategoryConfig, Group, PluginConfig } from '@code-pushup/models'; | ||
| import { | ||
| type PluginUrlContext, | ||
| createCategoryRefs, | ||
| expandCategoryRefs, | ||
| removeIndex, | ||
| shouldExpandForUrls, | ||
| validateUrlContext, | ||
| } from '@code-pushup/utils'; | ||
| import { AXE_PLUGIN_SLUG } from './constants.js'; | ||
| import { type AxeCategoryGroupSlug, isAxeGroupSlug } from './groups.js'; | ||
|
|
||
| /** | ||
| * Creates categories for the Axe plugin. | ||
| * | ||
| * @public | ||
| * @param plugin - {@link PluginConfig} object with groups and context | ||
| * @param categories - {@link CategoryConfig} optional user-defined categories | ||
| * @returns {CategoryConfig[]} - expanded and aggregated categories | ||
| * | ||
| * @example | ||
| * const axe = await axePlugin(urls); | ||
| * const axeCoreConfig = { | ||
| * plugins: [axe], | ||
| * categories: axeCategories(axe), | ||
| * }; | ||
| */ | ||
| export function axeCategories( | ||
| plugin: Pick<PluginConfig, 'groups' | 'context'>, | ||
| categories?: CategoryConfig[], | ||
| ): CategoryConfig[] { | ||
| if (!plugin.groups || plugin.groups.length === 0) { | ||
| return categories ?? []; | ||
| } | ||
| validateUrlContext(plugin.context); | ||
| if (!categories) { | ||
| return createCategories(plugin.groups, plugin.context); | ||
| } | ||
| return expandCategories(categories, plugin.context); | ||
| } | ||
|
|
||
| function createCategories( | ||
| groups: Group[], | ||
| context: PluginUrlContext, | ||
| ): CategoryConfig[] { | ||
| return [createAggregatedCategory(groups, context)]; | ||
| } | ||
|
|
||
| function expandCategories( | ||
| categories: CategoryConfig[], | ||
| context: PluginUrlContext, | ||
| ): CategoryConfig[] { | ||
| if (!shouldExpandForUrls(context.urlCount)) { | ||
| return categories; | ||
| } | ||
| return categories.map(category => | ||
| expandAggregatedCategory(category, context), | ||
| ); | ||
| } | ||
|
|
||
| export function createAggregatedCategory( | ||
| groups: Group[], | ||
| context: PluginUrlContext, | ||
| ): CategoryConfig { | ||
| const refs = extractGroupSlugs(groups).flatMap(slug => | ||
| createCategoryRefs(slug, AXE_PLUGIN_SLUG, context), | ||
| ); | ||
| return { | ||
| slug: 'axe-a11y', | ||
| title: 'Axe Accessibility', | ||
| refs, | ||
| }; | ||
| } | ||
|
|
||
| export function expandAggregatedCategory( | ||
| category: CategoryConfig, | ||
| context: PluginUrlContext, | ||
| ): CategoryConfig { | ||
| return { | ||
| ...category, | ||
| refs: category.refs.flatMap(ref => | ||
| ref.plugin === AXE_PLUGIN_SLUG ? expandCategoryRefs(ref, context) : [ref], | ||
| ), | ||
| }; | ||
| } | ||
|
|
||
| export function extractGroupSlugs(groups: Group[]): AxeCategoryGroupSlug[] { | ||
| const slugs = groups.map(({ slug }) => removeIndex(slug)); | ||
| return [...new Set(slugs)].filter(isAxeGroupSlug); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,144 @@ | ||
| import { describe, expect, it } from 'vitest'; | ||
| import type { CategoryConfig, PluginConfig } from '@code-pushup/models'; | ||
| import { axeCategories, extractGroupSlugs } from './categories.js'; | ||
| import { AXE_PLUGIN_SLUG } from './constants.js'; | ||
| import { axeGroupRef } from './utils.js'; | ||
|
|
||
| describe('axeCategories', () => { | ||
| const createMockPlugin = ( | ||
| overrides: Partial<Pick<PluginConfig, 'groups' | 'context'>> = {}, | ||
| ): Pick<PluginConfig, 'groups' | 'context'> => ({ | ||
| groups: [ | ||
| { slug: 'aria', title: 'ARIA', refs: [] }, | ||
| { slug: 'color', title: 'Color & Contrast', refs: [] }, | ||
| ], | ||
| context: { urlCount: 1, weights: { 1: 1 } }, | ||
| ...overrides, | ||
| }); | ||
|
|
||
| it('should create accessibility category with all groups', () => { | ||
| expect(axeCategories(createMockPlugin())).toEqual([ | ||
| { | ||
| slug: 'axe-a11y', | ||
| title: 'Axe Accessibility', | ||
| refs: [ | ||
| { plugin: AXE_PLUGIN_SLUG, slug: 'aria', type: 'group', weight: 1 }, | ||
| { | ||
| plugin: AXE_PLUGIN_SLUG, | ||
| slug: 'color', | ||
| type: 'group', | ||
| weight: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('should expand refs for multi-URL', () => { | ||
| const plugin = createMockPlugin({ | ||
| groups: [ | ||
| { slug: 'aria-1', title: 'ARIA 1', refs: [] }, | ||
| { slug: 'aria-2', title: 'ARIA 2', refs: [] }, | ||
| ], | ||
| context: { urlCount: 2, weights: { 1: 1, 2: 1 } }, | ||
| }); | ||
|
|
||
| expect(axeCategories(plugin)).toEqual([ | ||
| { | ||
| slug: 'axe-a11y', | ||
| title: 'Axe Accessibility', | ||
| refs: [ | ||
| { | ||
| plugin: AXE_PLUGIN_SLUG, | ||
| slug: 'aria-1', | ||
| type: 'group', | ||
| weight: 1, | ||
| }, | ||
| { | ||
| plugin: AXE_PLUGIN_SLUG, | ||
| slug: 'aria-2', | ||
| type: 'group', | ||
| weight: 1, | ||
| }, | ||
| ], | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('should return empty array if plugin has no groups', () => { | ||
| expect(axeCategories(createMockPlugin({ groups: [] }))).toEqual([]); | ||
| }); | ||
|
|
||
| it('should return categories unchanged for single URL', () => { | ||
| const categories: CategoryConfig[] = [ | ||
| { | ||
| slug: 'axe-a11y', | ||
| title: 'Axe Accessibility', | ||
| refs: [axeGroupRef('aria')], | ||
| }, | ||
| ]; | ||
|
|
||
| expect(axeCategories(createMockPlugin(), categories)).toEqual(categories); | ||
| }); | ||
|
|
||
| it('should expand Axe refs and preserve non-Axe refs for multi-URL', () => { | ||
| const categories: CategoryConfig[] = [ | ||
| { | ||
| slug: 'axe-a11y', | ||
| title: 'Axe Accessibility', | ||
| refs: [ | ||
| axeGroupRef('aria'), | ||
| { plugin: 'lighthouse', type: 'group', slug: 'seo', weight: 1 }, | ||
| ], | ||
| }, | ||
| ]; | ||
|
|
||
| expect( | ||
| axeCategories( | ||
| createMockPlugin({ context: { urlCount: 2, weights: { 1: 1, 2: 1 } } }), | ||
| categories, | ||
| ), | ||
| ).toEqual([ | ||
| { | ||
| slug: 'axe-a11y', | ||
| title: 'Axe Accessibility', | ||
| refs: [ | ||
| { plugin: AXE_PLUGIN_SLUG, slug: 'aria-1', type: 'group', weight: 1 }, | ||
| { plugin: AXE_PLUGIN_SLUG, slug: 'aria-2', type: 'group', weight: 1 }, | ||
| { plugin: 'lighthouse', type: 'group', slug: 'seo', weight: 1 }, | ||
| ], | ||
| }, | ||
| ]); | ||
| }); | ||
|
|
||
| it('should throw for invalid context', () => { | ||
| const plugin = createMockPlugin({ | ||
| context: { urlCount: 2, weights: { 1: 1 } }, | ||
| }); | ||
|
|
||
| expect(() => axeCategories(plugin)).toThrow( | ||
| 'Invalid plugin context: weights count must match urlCount', | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe('extractGroupSlugs', () => { | ||
| it('should extract unique base slugs from groups', () => { | ||
| expect( | ||
| extractGroupSlugs([ | ||
| { slug: 'aria-1', title: 'ARIA 1', refs: [] }, | ||
| { slug: 'aria-2', title: 'ARIA 2', refs: [] }, | ||
| { slug: 'color', title: 'Color & Contrast', refs: [] }, | ||
| ]), | ||
| ).toEqual(['aria', 'color']); | ||
| }); | ||
|
|
||
| it('should filter out invalid group slugs', () => { | ||
| expect( | ||
| extractGroupSlugs([ | ||
| { slug: 'aria', title: 'ARIA', refs: [] }, | ||
| { slug: 'invalid-group', title: 'Invalid', refs: [] }, | ||
| ]), | ||
| ).toEqual(['aria']); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import type { CategoryRef } from '@code-pushup/models'; | ||
| import { AXE_PLUGIN_SLUG } from './constants.js'; | ||
| import type { AxeGroupSlug } from './groups.js'; | ||
|
|
||
| export function axeGroupRef(groupSlug: AxeGroupSlug, weight = 1): CategoryRef { | ||
| return { | ||
| plugin: AXE_PLUGIN_SLUG, | ||
| slug: groupSlug, | ||
| type: 'group', | ||
| weight, | ||
| }; | ||
| } | ||
|
|
||
| export function axeAuditRef(auditSlug: string, weight = 1): CategoryRef { | ||
| return { | ||
| plugin: AXE_PLUGIN_SLUG, | ||
| slug: auditSlug, | ||
| type: 'audit', | ||
| weight, | ||
| }; | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.