From aec04f0085bd9c566bd033b8bb628389ff22bdf6 Mon Sep 17 00:00:00 2001 From: Fringg Date: Mon, 23 Mar 2026 06:42:57 +0300 Subject: [PATCH] fix: correctly price unlimited traffic (0 GB) in classic subscription mode _calculate_traffic_price treated unlimited traffic as free because base_gb=0 triggered the `if base_gb > 0 else 0` guard, skipping the price lookup. Added early return for total_gb==0 to use the configured unlimited tier price. --- app/services/pricing_engine.py | 5 +++++ tests/test_pricing_engine.py | 13 ++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/app/services/pricing_engine.py b/app/services/pricing_engine.py index 9174a228..8e72267c 100644 --- a/app/services/pricing_engine.py +++ b/app/services/pricing_engine.py @@ -479,6 +479,11 @@ class PricingEngine: Prevents purchased top-ups from inflating the tier lookup.""" total_gb = traffic_limit_gb or 0 purchased_gb = purchased_traffic_gb or 0 + + # 0 = unlimited traffic — has its own price tier, return directly + if total_gb == 0: + return settings.get_traffic_price(0) + base_gb = max(0, total_gb - purchased_gb) base_price = settings.get_traffic_price(base_gb) if base_gb > 0 else 0 diff --git a/tests/test_pricing_engine.py b/tests/test_pricing_engine.py index aa7f27d5..b7a9c20e 100644 --- a/tests/test_pricing_engine.py +++ b/tests/test_pricing_engine.py @@ -232,12 +232,19 @@ class TestCalculateTrafficPrice: price = engine._calculate_traffic_price(traffic_limit_gb=125, purchased_traffic_gb=100) assert price == 11000 # NOT 12000 - def test_zero_traffic(self): + def test_unlimited_traffic_has_price(self): engine = PricingEngine() with patch('app.services.pricing_engine.settings') as ms: - ms.get_traffic_price.return_value = 0 + ms.get_traffic_price.side_effect = lambda gb: {0: 20000, 5: 2000}.get(gb, 0) price = engine._calculate_traffic_price(traffic_limit_gb=0, purchased_traffic_gb=0) - assert price == 0 + assert price == 20000 # 0 GB = unlimited, charged at unlimited tier + + def test_unlimited_traffic_ignores_purchased(self): + engine = PricingEngine() + with patch('app.services.pricing_engine.settings') as ms: + ms.get_traffic_price.side_effect = lambda gb: {0: 20000, 50: 5000}.get(gb, 0) + price = engine._calculate_traffic_price(traffic_limit_gb=0, purchased_traffic_gb=50) + assert price == 20000 # unlimited tier, purchased ignored def test_purchased_exceeds_total(self): engine = PricingEngine()