from fxhnt.application.honest_leverage import max_drawdown_at_leverage, leverage_ceiling


def test_drawdown_scales_with_leverage():
    s = {1: 0.05, 2: -0.10, 3: 0.04}
    dd1 = max_drawdown_at_leverage(s, 1.0)
    dd2 = max_drawdown_at_leverage(s, 2.0)
    assert dd1 < 0.0 and dd2 < dd1          # 2x is a deeper (more negative) drawdown


def test_leverage_ceiling_respects_stress_buffer():
    s = {1: 0.02, 2: -0.05, 3: 0.03, 4: -0.02}
    lev = leverage_ceiling(s, dd_limit=-0.35, stress=2.0)
    assert 1.0 <= lev <= 2.0


def test_leverage_ceiling_zero_when_even_1x_breaches():
    s = {1: -0.30, 2: -0.30}                # brutal book: even 1x stressed blows the -0.35 limit
    assert leverage_ceiling(s, dd_limit=-0.35, stress=2.0) == 0.0


def test_leverage_ceiling_zero_on_empty_book():
    # empty / no-drawdown book has no REAL drawdown to anchor to -> 0.0 (not max grid leverage)
    assert leverage_ceiling({}) == 0.0
