"""Golden-value tests for the margin engine. These assert the exact worked example from the Pricing Analyst playbook (SOP A): break-even $16.76, MAP $19.00, contribution margin ~28% on the microfiber sheet set. If these ever drift, the pricing gate's math has changed — fail loudly. """ from __future__ import annotations import pytest from config.settings import PricingRules from pricing_agent.tools.margin_engine import ( compute_fee_stack, suggest_price_for_floor, worst_case_stack, ) RULES = PricingRules( margin_floor=0.25, margin_target=0.30, returns_reserve_pct=0.02, storage_alloc=0.25, worst_case_referral_pct=0.15, charm_ending=0.99, ) # Playbook worked example inputs. SHEET_SET = dict(selling_price=24.99, landed_cost=8.00, fba_fee=5.50) def test_break_even_matches_playbook(): stack = worst_case_stack(rules=RULES, **SHEET_SET) assert round(stack.break_even_price, 2) == 16.76 def test_map_matches_playbook(): stack = worst_case_stack(rules=RULES, **SHEET_SET) assert round(stack.map_floor, 2) == 19.00 def test_contribution_margin_is_about_28pct(): stack = worst_case_stack(rules=RULES, **SHEET_SET) assert stack.contribution_margin_pct == pytest.approx(0.28, abs=0.005) def test_fee_components_sum_to_total_cost(): stack = worst_case_stack(rules=RULES, **SHEET_SET) recomputed = ( stack.landed_cost + stack.fba_fee + stack.referral_amt + stack.returns_reserve + stack.storage_alloc ) assert stack.total_cost == pytest.approx(recomputed, abs=1e-6) assert stack.profit == pytest.approx(stack.selling_price - stack.total_cost, abs=1e-6) def test_price_below_break_even_is_loss_making(): # At $16.00 (< $16.76 break-even) contribution must be negative. stack = worst_case_stack(selling_price=16.00, landed_cost=8.00, fba_fee=5.50, rules=RULES) assert stack.profit < 0 assert stack.contribution_margin_pct < 0 def test_modeled_referral_beats_worst_case(): # Modeled 12% referral should yield higher margin than worst-case 15%. worst = worst_case_stack(rules=RULES, **SHEET_SET) modeled = compute_fee_stack( referral_pct=0.12, rules=RULES, referral_basis="modeled", **SHEET_SET ) assert modeled.contribution_margin_pct > worst.contribution_margin_pct def test_suggest_price_for_floor_clears_floor(): price = suggest_price_for_floor(landed_cost=8.00, fba_fee=5.50, rules=RULES) stack = worst_case_stack(selling_price=price, landed_cost=8.00, fba_fee=5.50, rules=RULES) assert stack.contribution_margin_pct >= RULES.margin_floor # charm pricing => ends in .99 assert round(price - int(price), 2) == 0.99 def test_invalid_inputs_raise(): with pytest.raises(ValueError): compute_fee_stack( selling_price=0, landed_cost=8, fba_fee=5.5, referral_pct=0.15, rules=RULES ) with pytest.raises(ValueError): compute_fee_stack( selling_price=24.99, landed_cost=8, fba_fee=5.5, referral_pct=1.5, rules=RULES )