"""振幅过滤门：永期开仓第一关 / 与期期共用口径。"""

from __future__ import annotations

from app.exchange.candles import AmplitudeHL
from app.strategy.amplitude_gate import evaluate_amplitude_gate


def test_amplitude_gate_off_always_pass() -> None:
    amp = AmplitudeHL(high=2060, low=1940, mid=2000, hours=12, bar_count=12)
    # 6% 超常见上限，但过滤关 → 不拦
    g = evaluate_amplitude_gate(
        filter_enabled=False, amp=amp, max_pct=2.0, hours=12
    )
    assert g["blocked"] is False
    assert g["ok"] is True
    assert g["reason"] is None
    assert g["snapshot"]["filter_enabled"] is False
    assert g["snapshot"]["range_pct"] == amp.range_pct


def test_amplitude_gate_on_blocks_over_max() -> None:
    amp = AmplitudeHL(high=2060, low=1940, mid=2000, hours=12, bar_count=12)
    assert amp.range_pct == 6.0
    g = evaluate_amplitude_gate(
        filter_enabled=True, amp=amp, max_pct=2.0, hours=12
    )
    assert g["blocked"] is True
    assert g["ok"] is False
    assert "振幅未过关" in (g["reason"] or "")
    assert "6.00%" in (g["reason"] or "")
    assert "2%" in (g["reason"] or "")


def test_amplitude_gate_on_pass_within_max() -> None:
    amp = AmplitudeHL(high=2010, low=1990, mid=2000, hours=12, bar_count=12)
    assert amp.range_pct == 1.0
    g = evaluate_amplitude_gate(
        filter_enabled=True, amp=amp, max_pct=2.0, hours=12
    )
    assert g["blocked"] is False
    assert g["ok"] is True
    assert g["reason"] is None


def test_amplitude_gate_on_no_candles_fail_closed() -> None:
    g = evaluate_amplitude_gate(
        filter_enabled=True, amp=None, max_pct=2.0, hours=12
    )
    assert g["blocked"] is True
    assert g["ok"] is False
    assert "无法获取" in (g["reason"] or "")


def test_amplitude_gate_off_no_candles_still_ok() -> None:
    g = evaluate_amplitude_gate(
        filter_enabled=False, amp=None, max_pct=2.0, hours=12
    )
    assert g["blocked"] is False
    assert g["ok"] is True
