"""Pure unlock-short domain: supply shock, inverse-vol cap, squeeze stop."""
from fxhnt.domain.unlock_shorts import inverse_vol_capped_weights, short_leg_pnl, supply_shock


def test_supply_shock_is_notional_over_volume():
    assert supply_shock(1_000_000, 2.0, 1_000_000.0) == 2.0   # $2M unlock / $1M ADV = 2 days
    assert supply_shock(0, 2.0, 1e6) is None
    assert supply_shock(1e6, 2.0, 0) is None


def test_inverse_vol_weights_favor_low_vol_and_cap():
    w = inverse_vol_capped_weights({"calm": 0.02, "wild": 0.10}, max_name_weight=0.9)
    assert w["calm"] > w["wild"]                  # low-vol gets more
    assert abs(sum(w.values()) - 1.0) < 1e-9      # gross 1


def test_cap_prevents_single_name_domination():
    w = inverse_vol_capped_weights({"a": 0.001, "b": 0.10, "c": 0.10}, max_name_weight=0.4)
    assert max(w.values()) <= 0.4 + 1e-9          # 'a' would dominate uncapped; cap holds


def test_squeeze_stop_floors_the_loss():
    # name moons +200% -> naked short loses ~2.0; stop floors it at -daily_stop
    stopped = short_leg_pnl(name_ret=2.0, btc_ret=0.0, funding=0.0, daily_stop=0.25)
    assert stopped == -0.25
    # normal day passes through (short profits when name falls)
    ok = short_leg_pnl(name_ret=-0.05, btc_ret=0.0, funding=0.0, daily_stop=0.25)
    assert abs(ok - 0.05) < 1e-9


def test_funding_and_hedge_enter_pnl():
    # short receives +funding; long-BTC hedge adds btc_ret
    p = short_leg_pnl(name_ret=0.0, btc_ret=0.01, funding=0.002, daily_stop=0.25)
    assert abs(p - 0.012) < 1e-9
