import { describe, it, expect } from 'vitest'; import { buildDynamicCatalog, resolvePlanCategory } from '../../src/utils/catalog'; import { CURRICULUM_MODULES } from '../../src/constants/modules'; import type { PublicPlan } from '../../src/types'; describe('buildDynamicCatalog', () => { it('falls back to CURRICULUM_MODULES when publicPlans is empty', () => { const result = buildDynamicCatalog([]); expect(result).toEqual(CURRICULUM_MODULES); }); it('maps newly created Module Builder one_time plan to its normalized category with sortOrder', () => { const plans: PublicPlan[] = [ { code: 'MOD_M0044', base_code: 'MOD_M0044', name: 'Pediatric Disorders', description: 'TCM pediatric treatment protocols.', plan_type: 'one_time', price_inr: 600, price_usd: 15, price_eur: 15, is_active: true, modules: [ { id: 'mod-44', code: 'm0044', slug: 'pediatric-disorders', name: 'Pediatric Disorders', description: 'TCM pediatric treatment protocols.', category: 'Therapeutics', sort_order: 40, }, ], }, ]; const result = buildDynamicCatalog(plans); expect(result).toHaveLength(1); expect(result[0]).toMatchObject({ code: 'MOD_M0044', name: 'Pediatric Disorders', category: 'Therapeutics', priceInr: 600, priceUsd: 15, priceEur: 15, sortOrder: 40, }); }); it('expands PATTERN_CORE sub-modules from plan.modules and appends Complete Pack', () => { const plans: PublicPlan[] = [ { code: 'PATTERN_CORE', name: 'Pattern: Complete Pack', description: 'All pattern methodologies.', plan_type: 'bundle', price_inr: 500, price_usd: 10, price_eur: 10, is_active: true, modules: [ { name: 'Zang-Fu Organs', slug: 'zang-fu', category: 'Pattern', sort_order: 2 }, { name: 'Eight Principles', slug: 'eight-principles', category: 'Pattern', sort_order: 1 }, ], }, ]; const result = buildDynamicCatalog(plans); expect(result.map((m) => m.name)).toEqual([ 'Eight Principles', 'Zang-Fu Organs', 'Pattern: Complete Pack', ]); expect(result[2].sortOrder).toBe(Number.MAX_SAFE_INTEGER); }); it('omits Complete Pack row for NUTRITION_CORE when only 1 module exists and keeps POINTS_PACK last', () => { const plans: PublicPlan[] = [ { code: 'NUTRITION_CORE', name: 'Nutrition Module Access (1 Yr)', plan_type: 'one_time', price_inr: 600, price_usd: 15, price_eur: 15, is_active: true, modules: [{ code: 'm0031', name: 'Nutrition', slug: 'nutrition', category: 'Nutrition', sort_order: 99999 }], }, { code: 'POINTS_PACK', name: 'Points Complete Pack (1 Yr)', plan_type: 'bundle', price_inr: 900, price_usd: 15, price_eur: 15, is_active: true, }, { code: 'POINTS_ACU', name: 'Acu Points Module (1 Yr)', plan_type: 'one_time', price_inr: 300, price_usd: 5, price_eur: 5, is_active: true, modules: [{ code: 'm0001', name: 'Acupuncture Points', slug: 'acupuncture-points', category: 'Points', sort_order: 99999 }], }, ]; const result = buildDynamicCatalog(plans); const nutritionItems = result.filter((m) => m.category === 'Nutrition'); expect(nutritionItems).toHaveLength(1); expect(nutritionItems[0].name).toBe('Nutrition'); const pointsItems = result .filter((m) => m.category === 'Points') .sort((a, b) => (a.sortOrder ?? 500) - (b.sortOrder ?? 500)); expect(pointsItems.map((m) => m.code)).toEqual(['POINTS_ACU', 'POINTS_PACK']); }); it('resolves non-standard category or custom bundle cleanly', () => { const unknownCatPlan: PublicPlan = { code: 'MOD_HERB', name: 'Herbal Formulas', plan_type: 'one_time', is_active: true, modules: [{ name: 'Herbal Formulas', category: 'Herbalism', sort_order: 50 }], }; expect(resolvePlanCategory(unknownCatPlan)).toBe('Herbalism'); const bundlePlan: PublicPlan = { code: 'STARTER_BUNDLE', name: 'Starter Bundle', plan_type: 'bundle', price_inr: 1200, price_usd: 25, price_eur: 25, is_active: true, }; const result = buildDynamicCatalog([bundlePlan]); expect(result[0].category).toBe('Bundle'); }); });