from fxhnt.application.allocation_engine import apply_venue_caps


def test_no_cap_at_default_is_identity():
    d = {"a": 60000.0, "b": -30000.0}
    v = {"a": "bybit-perp", "b": "ibkr-equity"}
    assert apply_venue_caps(dict(d), v, 1.0, 100000.0) == d


def test_single_venue_over_cap_scales_down_sign_preserving():
    # both on bybit-perp, gross 75k, cap 0.6*100k=60k -> scale 0.8
    d = {"a": 50000.0, "b": -25000.0}
    v = {"a": "bybit-perp", "b": "bybit-perp"}
    out = apply_venue_caps(dict(d), v, 0.6, 100000.0)
    assert round(out["a"], 2) == 40000.0 and round(out["b"], 2) == -20000.0


def test_under_cap_venue_untouched():
    d = {"a": 40000.0, "b": 40000.0}
    v = {"a": "bybit-perp", "b": "ibkr-equity"}   # each venue 40k < 60k cap
    assert apply_venue_caps(dict(d), v, 0.6, 100000.0) == d


def test_missing_venue_treated_as_own_bucket():
    d = {"a": 80000.0}
    v = {}                                          # sid with no venue -> its own ("") bucket
    out = apply_venue_caps(dict(d), v, 0.6, 100000.0)
    assert round(out["a"], 2) == 60000.0
