from __future__ import annotations from decimal import Decimal import pytest from app.features.payments.pricing import PricingEngine, PricingItem, PricingCoupon @pytest.fixture def engine() -> PricingEngine: return PricingEngine() class TestPricingEngineBundleRules: def test_empty_cart(self, engine: PricingEngine) -> None: result = engine.calculate( items=[], owned_categories=[], coupon=None, currency="INR", is_first_purchase=True, ) assert result["base_subtotal"] == 0.0 assert result["grand_total"] == 0.0 assert result["bundle_discount_amount"] == 0.0 assert result["discount_policy"] == "None" def test_first_time_buy_single_unit_no_discount(self, engine: PricingEngine) -> None: items = [ PricingItem(code="MOD_ACU", name="Acupuncture", category="acu", price=Decimal("1000.00")), ] result = engine.calculate( items=items, owned_categories=[], coupon=None, currency="INR", is_first_purchase=True, ) assert result["base_subtotal"] == 1000.0 assert result["bundle_discount_amount"] == 0.0 assert result["grand_total"] == 1000.0 assert result["discount_policy"] == "None" def test_first_time_buy_two_units_ten_percent(self, engine: PricingEngine) -> None: items = [ PricingItem(code="MOD_ACU", name="Acupuncture", category="acu", price=Decimal("1000.00")), PricingItem(code="MOD_MOXA", name="Moxibustion", category="moxa", price=Decimal("1000.00")), ] result = engine.calculate( items=items, owned_categories=[], coupon=None, currency="INR", is_first_purchase=True, ) assert result["base_subtotal"] == 2000.0 assert result["bundle_discount_amount"] == 200.0 # 10% of 2000 assert result["grand_total"] == 1800.0 assert "10% Off" in result["discount_policy"] def test_first_time_buy_three_units_twenty_percent(self, engine: PricingEngine) -> None: items = [ PricingItem(code="MOD_1", name="Mod 1", category="cat1", price=Decimal("1000.00")), PricingItem(code="MOD_2", name="Mod 2", category="cat2", price=Decimal("1000.00")), PricingItem(code="MOD_3", name="Mod 3", category="cat3", price=Decimal("1000.00")), ] result = engine.calculate( items=items, owned_categories=[], coupon=None, currency="INR", is_first_purchase=True, ) assert result["base_subtotal"] == 3000.0 assert result["bundle_discount_amount"] == 600.0 # 20% of 3000 assert result["grand_total"] == 2400.0 assert "20% Off" in result["discount_policy"] def test_first_time_buy_four_or_more_units_thirty_percent(self, engine: PricingEngine) -> None: items = [ PricingItem(code=f"MOD_{i}", name=f"Mod {i}", category=f"cat{i}", price=Decimal("1000.00")) for i in range(1, 5) ] result = engine.calculate( items=items, owned_categories=[], coupon=None, currency="INR", is_first_purchase=True, ) assert result["base_subtotal"] == 4000.0 assert result["bundle_discount_amount"] == 1200.0 # 30% of 4000 assert result["grand_total"] == 2800.0 assert "30% Off" in result["discount_policy"] def test_lifetime_package_overrides_discounts(self, engine: PricingEngine) -> None: items = [ PricingItem(code="LIFETIME_ALL", name="Lifetime Access", category="lifetime", price=Decimal("15000.00")), ] result = engine.calculate( items=items, owned_categories=["cat1", "cat2"], coupon=None, currency="INR", is_first_purchase=False, ) assert result["discount_policy"] == "Lifetime Bulk Package" assert result["bundle_discount_amount"] == 0.0 assert result["grand_total"] == 15000.0 class TestPricingEngineLoyaltyRules: def test_loyalty_discount_scaling(self, engine: PricingEngine) -> None: items = [ PricingItem(code="MOD_1", name="Mod 1", category="cat1", price=Decimal("1000.00")), ] # 1 owned category -> 5% res1 = engine.calculate( items=items, owned_categories=["cat_old1"], coupon=None, currency="INR", is_first_purchase=False, ) assert res1["bundle_discount_amount"] == 50.0 # 5% assert "5% Off" in res1["discount_policy"] # 3 owned categories -> 15% res3 = engine.calculate( items=items, owned_categories=["cat_old1", "cat_old2", "cat_old3"], coupon=None, currency="INR", is_first_purchase=False, ) assert res3["bundle_discount_amount"] == 150.0 # 15% assert "15% Off" in res3["discount_policy"] # 6 owned categories -> capped at 25% res6 = engine.calculate( items=items, owned_categories=[f"cat_old_{i}" for i in range(6)], coupon=None, currency="INR", is_first_purchase=False, ) assert res6["bundle_discount_amount"] == 250.0 # max 25% assert "25% Off" in res6["discount_policy"] class TestPricingEngineCouponRules: def test_flat_coupon_discount(self, engine: PricingEngine) -> None: items = [PricingItem(code="MOD_1", name="Mod 1", category="cat1", price=Decimal("1000.00"))] coupon = PricingCoupon( code="FLAT200", category="general", discount_type="flat", value=Decimal("200.00"), description="Flat Rs 200 off", ) result = engine.calculate( items=items, owned_categories=[], coupon=coupon, currency="INR", is_first_purchase=True, ) assert result["coupon_discount_amount"] == 200.0 assert result["grand_total"] == 800.0 assert "FLAT200" in result["coupon_applied"] def test_percentage_coupon_discount(self, engine: PricingEngine) -> None: items = [PricingItem(code="MOD_1", name="Mod 1", category="cat1", price=Decimal("1000.00"))] coupon = PricingCoupon( code="SAVE15", category="general", discount_type="percentage", value=Decimal("15.00"), ) result = engine.calculate( items=items, owned_categories=[], coupon=coupon, currency="INR", is_first_purchase=True, ) assert result["coupon_discount_amount"] == 150.0 # 15% of 1000 assert result["grand_total"] == 850.0 def test_coupon_max_discount_cap(self, engine: PricingEngine) -> None: items = [PricingItem(code="MOD_1", name="Mod 1", category="cat1", price=Decimal("5000.00"))] coupon = PricingCoupon( code="BIG50", category="general", discount_type="percentage", value=Decimal("50.00"), # 50% would be 2500 max_discount_limit=Decimal("1000.00"), # capped at 1000 ) result = engine.calculate( items=items, owned_categories=[], coupon=coupon, currency="INR", is_first_purchase=True, ) assert result["coupon_discount_amount"] == 1000.0 assert result["grand_total"] == 4000.0 def test_coupon_min_purchase_amount_not_met(self, engine: PricingEngine) -> None: items = [PricingItem(code="MOD_1", name="Mod 1", category="cat1", price=Decimal("500.00"))] coupon = PricingCoupon( code="MIN1000", category="general", discount_type="flat", value=Decimal("100.00"), min_purchase_amount=Decimal("1000.00"), ) result = engine.calculate( items=items, owned_categories=[], coupon=coupon, currency="INR", is_first_purchase=True, ) assert result["coupon_discount_amount"] == 0.0 assert result["grand_total"] == 500.0 assert result["coupon_error"] is not None assert "Minimum cart spend" in result["coupon_error"] def test_plan_scoped_coupon_rejected_on_unmatched_items(self, engine: PricingEngine) -> None: lifetime_plan_id = "11111111-1111-1111-1111-111111111111" yearly_plan_id = "22222222-2222-2222-2222-222222222222" items = [ PricingItem(code="POINTS_1YR", name="Points 1-Year", category="Points", price=Decimal("2000.00"), plan_id=yearly_plan_id) ] coupon = PricingCoupon( code="LIFETIMEONLY", category="specific", discount_type="percentage", value=Decimal("20.00"), applicable_plan_ids=(lifetime_plan_id,), ) result = engine.calculate( items=items, owned_categories=[], coupon=coupon, currency="INR", is_first_purchase=True, ) assert result["coupon_discount_amount"] == 0.0 assert result["coupon_applied"] == "None" assert result["coupon_error"] == "This promo code is not valid for items in your cart." assert result["grand_total"] == 2000.0 def test_plan_scoped_coupon_applied_on_matched_lifetime_item(self, engine: PricingEngine) -> None: lifetime_plan_id = "11111111-1111-1111-1111-111111111111" items = [ PricingItem(code="LIFETIME_ALL", name="Lifetime All", category="Lifetime", price=Decimal("10000.00"), plan_id=lifetime_plan_id) ] coupon = PricingCoupon( code="LIFETIMEONLY", category="specific", discount_type="percentage", value=Decimal("20.00"), applicable_plan_ids=(lifetime_plan_id,), ) result = engine.calculate( items=items, owned_categories=[], coupon=coupon, currency="INR", is_first_purchase=True, ) assert result["coupon_discount_amount"] == 2000.0 assert result["coupon_error"] is None assert "LIFETIMEONLY" in result["coupon_applied"] assert result["grand_total"] == 8000.0 def test_plan_scoped_coupon_mixed_cart_applies_only_to_qualifying_spend(self, engine: PricingEngine) -> None: lifetime_plan_id = "11111111-1111-1111-1111-111111111111" yearly_plan_id = "22222222-2222-2222-2222-222222222222" items = [ PricingItem(code="POINTS_1YR", name="Points 1-Year", category="Points", price=Decimal("2000.00"), plan_id=yearly_plan_id), PricingItem(code="LIFETIME_ALL", name="Lifetime All", category="Lifetime", price=Decimal("10000.00"), plan_id=lifetime_plan_id), ] coupon = PricingCoupon( code="LIFETIMEONLY", category="specific", discount_type="percentage", value=Decimal("10.00"), applicable_plan_ids=(lifetime_plan_id,), ) result = engine.calculate( items=items, owned_categories=[], coupon=coupon, currency="INR", is_first_purchase=True, ) # 10% of 10000 = 1000 discount (does not discount the 2000 1-yr item) assert result["base_subtotal"] == 12000.0 assert result["coupon_discount_amount"] == 1000.0 assert result["grand_total"] == 11000.0 assert result["coupon_error"] is None def test_module_scoped_coupon(self, engine: PricingEngine) -> None: mod_id = "33333333-3333-3333-3333-333333333333" other_mod_id = "44444444-4444-4444-4444-444444444444" items = [ PricingItem(code="PLAN_A", name="Plan A", category="Cat", price=Decimal("1000.00"), module_ids=(other_mod_id,)) ] coupon = PricingCoupon( code="MODONLY", category="specific", discount_type="flat", value=Decimal("200.00"), applicable_module_ids=(mod_id,), ) # Unmatched module res1 = engine.calculate(items=items, owned_categories=[], coupon=coupon, currency="INR", is_first_purchase=True) assert res1["coupon_discount_amount"] == 0.0 assert res1["coupon_error"] == "This promo code is not valid for items in your cart." # Matched module items_match = [ PricingItem(code="PLAN_B", name="Plan B", category="Cat", price=Decimal("1000.00"), module_ids=(mod_id,)) ] res2 = engine.calculate(items=items_match, owned_categories=[], coupon=coupon, currency="INR", is_first_purchase=True) assert res2["coupon_discount_amount"] == 200.0 assert res2["coupon_error"] is None def test_introductory_flat_coupon_disables_unit_bundle_discount(self, engine: PricingEngine) -> None: items = [ PricingItem(code="MOD_1", name="Mod 1", category="cat1", price=Decimal("1000.00")), PricingItem(code="MOD_2", name="Mod 2", category="cat2", price=Decimal("1000.00")), ] coupon = PricingCoupon( code="INTRO100", category="introductory", discount_type="flat", value=Decimal("100.00"), ) result = engine.calculate( items=items, owned_categories=[], coupon=coupon, currency="INR", is_first_purchase=True, ) assert result["discount_policy"] == "None (Overridden by New User Event Coupon)" assert result["bundle_discount_amount"] == 0.0 assert result["coupon_discount_amount"] == 100.0 assert result["grand_total"] == 1900.0 def test_not_first_purchase_without_owned_categories(self, engine: PricingEngine) -> None: items = [ PricingItem(code="MOD_1", name="Mod 1", category="cat1", price=Decimal("1000.00")), ] result = engine.calculate( items=items, owned_categories=[], coupon=None, currency="INR", is_first_purchase=False, ) assert result["bundle_discount_amount"] == 0.0 assert result["discount_policy"] == "None"