|
1 | | -import type { CoreConfig, PluginConfig } from '@code-pushup/models'; |
2 | | -import { mergeConfigs } from './merge-configs.js'; |
| 1 | +import type { |
| 2 | + CategoryConfig, |
| 3 | + CoreConfig, |
| 4 | + PluginConfig, |
| 5 | +} from '@code-pushup/models'; |
| 6 | +import { mergeCategoriesBySlug, mergeConfigs } from './merge-configs.js'; |
3 | 7 |
|
4 | 8 | const MOCK_CONFIG_PERSIST = { |
5 | 9 | persist: { |
@@ -328,3 +332,142 @@ describe('mergeObjects', () => { |
328 | 332 | }); |
329 | 333 | }); |
330 | 334 | }); |
| 335 | + |
| 336 | +describe('mergeCategoriesBySlug', () => { |
| 337 | + it('should return categories unchanged when no duplicates', () => { |
| 338 | + const categories: CategoryConfig[] = [ |
| 339 | + { slug: 'bug-prevention', title: 'Bug prevention', refs: [] }, |
| 340 | + { slug: 'code-style', title: 'Code style', refs: [] }, |
| 341 | + ]; |
| 342 | + expect(mergeCategoriesBySlug(categories)).toEqual(categories); |
| 343 | + }); |
| 344 | + |
| 345 | + it('should merge duplicate slugs — first title wins, refs concatenated', () => { |
| 346 | + expect( |
| 347 | + mergeCategoriesBySlug([ |
| 348 | + { |
| 349 | + slug: 'bug-prevention', |
| 350 | + title: 'Bug prevention', |
| 351 | + refs: [ |
| 352 | + { type: 'group', plugin: 'eslint', slug: 'problems', weight: 1 }, |
| 353 | + ], |
| 354 | + }, |
| 355 | + { |
| 356 | + slug: 'bug-prevention', |
| 357 | + title: 'Bug detection', |
| 358 | + refs: [ |
| 359 | + { |
| 360 | + type: 'group', |
| 361 | + plugin: 'basic-plugin', |
| 362 | + slug: 'problems', |
| 363 | + weight: 1, |
| 364 | + }, |
| 365 | + ], |
| 366 | + }, |
| 367 | + ]), |
| 368 | + ).toEqual([ |
| 369 | + { |
| 370 | + slug: 'bug-prevention', |
| 371 | + title: 'Bug prevention', |
| 372 | + refs: [ |
| 373 | + { type: 'group', plugin: 'eslint', slug: 'problems', weight: 1 }, |
| 374 | + { |
| 375 | + type: 'group', |
| 376 | + plugin: 'basic-plugin', |
| 377 | + slug: 'problems', |
| 378 | + weight: 1, |
| 379 | + }, |
| 380 | + ], |
| 381 | + }, |
| 382 | + ]); |
| 383 | + }); |
| 384 | + |
| 385 | + it('should join different descriptions as sentences', () => { |
| 386 | + expect( |
| 387 | + mergeCategoriesBySlug([ |
| 388 | + { |
| 389 | + slug: 'bug-prevention', |
| 390 | + title: 'Bug prevention', |
| 391 | + description: 'Catches common bugs', |
| 392 | + refs: [], |
| 393 | + }, |
| 394 | + { |
| 395 | + slug: 'bug-prevention', |
| 396 | + title: 'Bug prevention', |
| 397 | + description: 'Enforces type safety.', |
| 398 | + refs: [], |
| 399 | + }, |
| 400 | + ]), |
| 401 | + ).toContainEqual( |
| 402 | + expect.objectContaining({ |
| 403 | + description: 'Catches common bugs. Enforces type safety.', |
| 404 | + }), |
| 405 | + ); |
| 406 | + }); |
| 407 | + |
| 408 | + it('should not duplicate identical descriptions', () => { |
| 409 | + expect( |
| 410 | + mergeCategoriesBySlug([ |
| 411 | + { |
| 412 | + slug: 'code-style', |
| 413 | + title: 'Code style', |
| 414 | + description: 'Consistent formatting.', |
| 415 | + refs: [], |
| 416 | + }, |
| 417 | + { |
| 418 | + slug: 'code-style', |
| 419 | + title: 'Code style', |
| 420 | + description: 'Consistent formatting.', |
| 421 | + refs: [], |
| 422 | + }, |
| 423 | + ]), |
| 424 | + ).toContainEqual( |
| 425 | + expect.objectContaining({ description: 'Consistent formatting.' }), |
| 426 | + ); |
| 427 | + }); |
| 428 | + |
| 429 | + it('should use first non-empty docsUrl', () => { |
| 430 | + expect( |
| 431 | + mergeCategoriesBySlug([ |
| 432 | + { |
| 433 | + slug: 'bug-prevention', |
| 434 | + title: 'Bug prevention', |
| 435 | + docsUrl: 'https://eslint.org/rules', |
| 436 | + refs: [], |
| 437 | + }, |
| 438 | + { |
| 439 | + slug: 'bug-prevention', |
| 440 | + title: 'Bug prevention', |
| 441 | + docsUrl: 'https://typescript-eslint.io/rules', |
| 442 | + refs: [], |
| 443 | + }, |
| 444 | + ]), |
| 445 | + ).toContainEqual( |
| 446 | + expect.objectContaining({ docsUrl: 'https://eslint.org/rules' }), |
| 447 | + ); |
| 448 | + }); |
| 449 | + |
| 450 | + it('should fall back to second value when first is missing', () => { |
| 451 | + expect( |
| 452 | + mergeCategoriesBySlug([ |
| 453 | + { slug: 'code-style', title: 'Code style', refs: [] }, |
| 454 | + { |
| 455 | + slug: 'code-style', |
| 456 | + title: 'Code style', |
| 457 | + docsUrl: 'https://eslint.org/rules', |
| 458 | + description: 'Consistent formatting.', |
| 459 | + refs: [], |
| 460 | + }, |
| 461 | + ]), |
| 462 | + ).toContainEqual( |
| 463 | + expect.objectContaining({ |
| 464 | + docsUrl: 'https://eslint.org/rules', |
| 465 | + description: 'Consistent formatting.', |
| 466 | + }), |
| 467 | + ); |
| 468 | + }); |
| 469 | + |
| 470 | + it('should return empty array for empty input', () => { |
| 471 | + expect(mergeCategoriesBySlug([])).toEqual([]); |
| 472 | + }); |
| 473 | +}); |
0 commit comments