|
16 | 16 | import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest'; |
17 | 17 | import { join } from 'path'; |
18 | 18 | import fs from 'fs-extra'; |
19 | | -import { buildTargetRegistry, transformTargets } from './target-utils'; |
| 19 | +import { buildTargetRegistry, transformTargets, collectUITargetIds, validateTargetRegistry } from './target-utils'; |
20 | 20 | import { logger } from '../utils/logger'; |
21 | 21 |
|
22 | 22 | describe('target-utils', () => { |
@@ -630,4 +630,182 @@ describe('target-utils', () => { |
630 | 630 | expect(result).not.toContain('UITargetProviders'); |
631 | 631 | }); |
632 | 632 | }); |
| 633 | + |
| 634 | + describe('collectUITargetIds', () => { |
| 635 | + const testDir = join(__dirname, '__test-collect-targets__'); |
| 636 | + |
| 637 | + beforeEach(() => { |
| 638 | + fs.ensureDirSync(testDir); |
| 639 | + }); |
| 640 | + |
| 641 | + afterEach(() => { |
| 642 | + fs.removeSync(testDir); |
| 643 | + }); |
| 644 | + |
| 645 | + it('should collect targetIds from tsx files', () => { |
| 646 | + fs.ensureDirSync(join(testDir, 'components')); |
| 647 | + fs.writeFileSync( |
| 648 | + join(testDir, 'components', 'page.tsx'), |
| 649 | + `<UITarget targetId="sfcc.header.before.cart" />\n<UITarget targetId="sfcc.footer.start" />` |
| 650 | + ); |
| 651 | + |
| 652 | + const ids = collectUITargetIds(testDir); |
| 653 | + expect(ids).toContain('sfcc.header.before.cart'); |
| 654 | + expect(ids).toContain('sfcc.footer.start'); |
| 655 | + expect(ids.size).toBe(2); |
| 656 | + }); |
| 657 | + |
| 658 | + it('should exclude the extensions directory', () => { |
| 659 | + fs.ensureDirSync(join(testDir, 'extensions', 'my-ext')); |
| 660 | + fs.writeFileSync( |
| 661 | + join(testDir, 'extensions', 'my-ext', 'comp.tsx'), |
| 662 | + `<UITarget targetId="should.not.be.found" />` |
| 663 | + ); |
| 664 | + fs.ensureDirSync(join(testDir, 'components')); |
| 665 | + fs.writeFileSync(join(testDir, 'components', 'page.tsx'), `<UITarget targetId="valid.target" />`); |
| 666 | + |
| 667 | + const ids = collectUITargetIds(testDir); |
| 668 | + expect(ids).not.toContain('should.not.be.found'); |
| 669 | + expect(ids).toContain('valid.target'); |
| 670 | + }); |
| 671 | + |
| 672 | + it('should exclude ui-target-dev-mode and ui-target-smoke-test directories', () => { |
| 673 | + fs.ensureDirSync(join(testDir, 'ui-target-dev-mode')); |
| 674 | + fs.writeFileSync( |
| 675 | + join(testDir, 'ui-target-dev-mode', 'tool.tsx'), |
| 676 | + `<UITarget targetId="dev.only.target" />` |
| 677 | + ); |
| 678 | + fs.ensureDirSync(join(testDir, 'ui-target-smoke-test')); |
| 679 | + fs.writeFileSync( |
| 680 | + join(testDir, 'ui-target-smoke-test', 'test.tsx'), |
| 681 | + `<UITarget targetId="smoke.test.target" />` |
| 682 | + ); |
| 683 | + |
| 684 | + const ids = collectUITargetIds(testDir); |
| 685 | + expect(ids).not.toContain('dev.only.target'); |
| 686 | + expect(ids).not.toContain('smoke.test.target'); |
| 687 | + }); |
| 688 | + |
| 689 | + it('should return empty set when no targets found', () => { |
| 690 | + fs.writeFileSync(join(testDir, 'page.tsx'), `export default function Page() { return <div />; }`); |
| 691 | + |
| 692 | + const ids = collectUITargetIds(testDir); |
| 693 | + expect(ids.size).toBe(0); |
| 694 | + }); |
| 695 | + |
| 696 | + it('should match single-quoted targetId attributes', () => { |
| 697 | + fs.ensureDirSync(join(testDir, 'components')); |
| 698 | + fs.writeFileSync( |
| 699 | + join(testDir, 'components', 'page.tsx'), |
| 700 | + `<UITarget targetId='sfcc.header.single.quote' />` |
| 701 | + ); |
| 702 | + |
| 703 | + const ids = collectUITargetIds(testDir); |
| 704 | + expect(ids).toContain('sfcc.header.single.quote'); |
| 705 | + }); |
| 706 | + |
| 707 | + it('should not match targetId on non-UITarget components', () => { |
| 708 | + fs.ensureDirSync(join(testDir, 'components')); |
| 709 | + fs.writeFileSync( |
| 710 | + join(testDir, 'components', 'page.tsx'), |
| 711 | + `<SomeOther targetId="not.a.real.target" />\n<UITarget targetId="real.target" />` |
| 712 | + ); |
| 713 | + |
| 714 | + const ids = collectUITargetIds(testDir); |
| 715 | + expect(ids).not.toContain('not.a.real.target'); |
| 716 | + expect(ids).toContain('real.target'); |
| 717 | + expect(ids.size).toBe(1); |
| 718 | + }); |
| 719 | + |
| 720 | + it('should exclude test files', () => { |
| 721 | + fs.ensureDirSync(join(testDir, 'components')); |
| 722 | + fs.writeFileSync(join(testDir, 'components', 'page.tsx'), `<UITarget targetId="real.target" />`); |
| 723 | + fs.writeFileSync(join(testDir, 'components', 'page.test.tsx'), `<UITarget targetId="test.only.target" />`); |
| 724 | + |
| 725 | + const ids = collectUITargetIds(testDir); |
| 726 | + expect(ids).toContain('real.target'); |
| 727 | + expect(ids).not.toContain('test.only.target'); |
| 728 | + expect(ids.size).toBe(1); |
| 729 | + }); |
| 730 | + }); |
| 731 | + |
| 732 | + describe('validateTargetRegistry', () => { |
| 733 | + it('should return empty array when all targetIds are declared', () => { |
| 734 | + const registry = { |
| 735 | + 'sfcc.header.before.cart': [ |
| 736 | + { |
| 737 | + targetId: 'sfcc.header.before.cart', |
| 738 | + path: 'extensions/foo/comp.tsx', |
| 739 | + namespace: 'Foo', |
| 740 | + componentName: 'Foo_Comp', |
| 741 | + order: 0, |
| 742 | + }, |
| 743 | + ], |
| 744 | + }; |
| 745 | + const declared = new Set(['sfcc.header.before.cart', 'sfcc.footer.start']); |
| 746 | + |
| 747 | + const orphaned = validateTargetRegistry(registry, declared); |
| 748 | + expect(orphaned).toHaveLength(0); |
| 749 | + }); |
| 750 | + |
| 751 | + it('should return orphaned entries for undeclared targetIds', () => { |
| 752 | + const registry = { |
| 753 | + 'sfcc.header.before.cart': [ |
| 754 | + { |
| 755 | + targetId: 'sfcc.header.before.cart', |
| 756 | + path: 'extensions/foo/comp.tsx', |
| 757 | + namespace: 'Foo', |
| 758 | + componentName: 'Foo_Comp', |
| 759 | + order: 0, |
| 760 | + }, |
| 761 | + ], |
| 762 | + 'sfcc.nonexistent.target': [ |
| 763 | + { |
| 764 | + targetId: 'sfcc.nonexistent.target', |
| 765 | + path: 'extensions/bar/widget.tsx', |
| 766 | + namespace: 'Bar', |
| 767 | + componentName: 'Bar_Widget', |
| 768 | + order: 0, |
| 769 | + }, |
| 770 | + ], |
| 771 | + }; |
| 772 | + const declared = new Set(['sfcc.header.before.cart']); |
| 773 | + |
| 774 | + const orphaned = validateTargetRegistry(registry, declared); |
| 775 | + expect(orphaned).toHaveLength(1); |
| 776 | + expect(orphaned[0].targetId).toBe('sfcc.nonexistent.target'); |
| 777 | + expect(orphaned[0].extension).toBe('Bar'); |
| 778 | + expect(orphaned[0].componentPath).toBe('extensions/bar/widget.tsx'); |
| 779 | + }); |
| 780 | + |
| 781 | + it('should report all components for a single orphaned targetId', () => { |
| 782 | + const registry = { |
| 783 | + 'missing.target': [ |
| 784 | + { |
| 785 | + targetId: 'missing.target', |
| 786 | + path: 'extensions/a/comp.tsx', |
| 787 | + namespace: 'A', |
| 788 | + componentName: 'A_Comp', |
| 789 | + order: 0, |
| 790 | + }, |
| 791 | + { |
| 792 | + targetId: 'missing.target', |
| 793 | + path: 'extensions/b/comp.tsx', |
| 794 | + namespace: 'B', |
| 795 | + componentName: 'B_Comp', |
| 796 | + order: 1, |
| 797 | + }, |
| 798 | + ], |
| 799 | + }; |
| 800 | + const declared = new Set<string>(); |
| 801 | + |
| 802 | + const orphaned = validateTargetRegistry(registry, declared); |
| 803 | + expect(orphaned).toHaveLength(2); |
| 804 | + }); |
| 805 | + |
| 806 | + it('should return empty array for empty registry', () => { |
| 807 | + const orphaned = validateTargetRegistry({}, new Set(['some.target'])); |
| 808 | + expect(orphaned).toHaveLength(0); |
| 809 | + }); |
| 810 | + }); |
633 | 811 | }); |
0 commit comments