"""Unit tests for the COSMOS mapping layer (no network). Uses a fake client that returns a captured real take-home response, so we verify the FeeQuote mapping and the stack assembly without hitting the API. """ from __future__ import annotations from config.settings import PricingRules from pricing_agent.cosmos.service import CosmosPricingService from pricing_agent.tools.margin_engine import stack_from_quote # A real captured take-home response for UBMICROFIBERGUSSETPILLOWWHITEQUEEN @ $24.99. REAL_TAKEHOME = { "itemPrice": 24.99, "referralFee": 3.75, "fbaFee": 8.97, "lowInventoryFee": 0.0, "inboundPlacementFee": 0.11, "returnFee": 0.5, "eprFee": 0, "vat": 0.0, "costSubtotal": -12.72, "marginImpact": 37.71, "logisticsCost": 0.0, "dutyAmount": 0.0, "costPerUnit": 8.59, "orderHandling": 0, "weightHandling": 0, "warehouseExpense": 0, "costBreakdown": {"purchasePrice": 6.28, "freight": 1.27, "duty": 1.03}, "takeHome": 3.07, } RULES = PricingRules(margin_floor=0.25, worst_case_referral_pct=0.15) class FakeClient: def get(self, path, params=None): assert "takehome-calculator" in path return REAL_TAKEHOME def test_fee_quote_maps_takehome(): svc = CosmosPricingService(FakeClient()) q = svc.fee_quote("UBTEST", 24.99) assert q.selling_price == 24.99 assert q.landed_cost == 8.59 assert q.fba_fee == 8.97 assert q.referral_amt == 3.75 assert q.returns_reserve == 0.5 assert q.other_fees == 0.11 # inbound placement only assert q.net_takehome == 3.07 assert q.source == "cosmos" def test_stack_from_quote_matches_cosmos_net(): svc = CosmosPricingService(FakeClient()) q = svc.fee_quote("UBTEST", 24.99) stack = stack_from_quote(q, RULES) # Net profit must equal COSMOS take-home. assert round(stack.profit, 2) == 3.07 # CM% = takeHome / price. assert round(stack.contribution_margin_pct, 4) == round(3.07 / 24.99, 4) # Break-even uses the fixed cost base and the derived referral %. cost_base = 8.59 + 8.97 + 0.5 + 0.11 assert round(stack.break_even_price, 2) == round(cost_base / (1 - 0.15), 2) assert round(stack.map_floor, 2) == round(cost_base / (1 - 0.25), 2)