import { describe, expect, it } from 'vitest'; import { isSizeEligible, sizeGuideResolution } from './sizeEligibility'; const variants = [ { variant_id: '2', size_label: 'M', available: true }, { variant_id: '3', size_label: 'L', available: true }, ]; describe( 'the WooCommerce size entry gate', () => { it( 'requires a configured guide with an available matching variation', () => { expect( isSizeEligible( { hasSizeGuide: false }, [ 'shirts' ], '1', variants ) ).toBe( false ); expect( isSizeEligible( { hasSizeGuide: true }, [ 'shirts' ], '1', [ { ...variants[ 0 ], available: false } ] ) ).toBe( false ); expect( isSizeEligible( { hasSizeGuide: true, guides: [] }, [ 'shirts' ], '1', variants ) ).toBe( false ); expect( isSizeEligible( { hasSizeGuide: true, guides: [ { id: 'g', categories: { categories: [] }, labels: [ 'S', 'XL' ] } ], }, [ 'shirts' ], '1', variants ) ).toBe( false ); } ); it( 'honours include and exclude targeting independently from try-on', () => { const base = { hasSizeGuide: true, guides: [ { id: 'g', categories: { categories: [] }, labels: [ 'M', 'L' ] } ], }; expect( isSizeEligible( { ...base, targeting: { mode: 'include', categories: [ 'shirts' ], productIds: [] } }, [ 'shirts' ], '1', variants ) ).toBe( true ); expect( isSizeEligible( { ...base, targeting: { mode: 'exclude', categories: [ 'shirts' ], productIds: [] } }, [ 'shirts' ], '1', variants ) ).toBe( false ); } ); it( 'reports whether the applicable guide came from a collection or the shop default', () => { const size = { hasSizeGuide: true, guides: [ { id: 'g', categories: { categories: [ 'shirts' ] }, labels: [ 'M', 'L' ] } ], }; expect( sizeGuideResolution( size, [ 'shirts' ], variants ) ).toBe( 'collection' ); } ); it.each( [ 'One Size', 'OS', 'Taglia Unica', 'Talla Única' ] )( 'rejects one-size label %s', ( label ) => { expect( isSizeEligible( { hasSizeGuide: true, guides: [ { id: 'g', categories: { categories: [] }, labels: [ label, 'M' ] } ], }, [], '1', [ { variant_id: '1', size_label: label, available: true }, { variant_id: '2', size_label: 'M', available: true }, ] ) ).toBe( false ); } ); it( 'deduplicates colors and ignores sold-out or non-overlapping sizes', () => { const size = { hasSizeGuide: true, guides: [ { id: 'g', categories: { categories: [] }, labels: [ 'S', 'M', 'L' ] } ], }; expect( isSizeEligible( size, [], '1', [ { variant_id: 'red-m', size_label: 'M', available: true }, { variant_id: 'blue-m', size_label: ' m ', available: true }, { variant_id: 'red-l', size_label: 'L', available: false }, ] ) ).toBe( false ); expect( isSizeEligible( size, [], '1', variants ) ).toBe( true ); } ); it( 'fails closed for legacy or malformed guide payloads', () => { expect( isSizeEligible( { hasSizeGuide: true }, [], '1', variants ) ).toBe( false ); expect( isSizeEligible( { hasSizeGuide: true, guides: [] }, [], '1', variants ) ).toBe( false ); } ); } ); it('prefers product assignments without covering neighbors or bypassing One Size', () => { const selected = { hasSizeGuide: true, guides: [{ id: 'special', categories: { categories: [], productIds: ['42', '43'] }, labels: ['M', 'L', 'One Size'] }] }; const variants = [{ variant_id: '1', size_label: 'M', available: true }, { variant_id: '2', size_label: 'L', available: true }]; expect(isSizeEligible(selected, ['shirts'], '42', variants)).toBe(true); expect(isSizeEligible(selected, ['shirts'], '43', variants)).toBe(true); expect(isSizeEligible(selected, ['shirts'], '44', variants)).toBe(false); expect(isSizeEligible(selected, ['shirts'], '42', [{ variant_id: '3', size_label: 'One Size', available: true }])).toBe(false); expect(sizeGuideResolution(selected, [], variants, '42')).toBe('product_id'); }); it('fails closed for malformed product lists and ignores null guides', () => { for (const productIds of ['42', [42]]) { expect(isSizeEligible({ hasSizeGuide: true, guides: [null, { id: 'broken', categories: { categories: [], productIds }, labels: ['M', 'L'] }] } as never, [], '42', variants)).toBe(false); } });