from lib.trade.open_trade_gate_lib import resolve_manual_open_gate


def _base(**kwargs):
    cfg = dict(
        time_allows=True,
        active_count=0,
        max_active_positions=1,
        opens_today=0,
        hard_limit=10,
        risk_status={"can_trade": True, "reason": ""},
        force_close_enabled=False,
        force_close_bj_hour=0,
        now_ms=None,
        reset_hour=8,
    )
    cfg.update(kwargs)
    return resolve_manual_open_gate(**cfg)


def test_open_when_clear():
    g = _base()
    assert g["can_trade"] is True
    assert g["open_block_note"] == ""


def test_block_cooloff_note():
    g = _base(risk_status={"can_trade": False, "reason": "账户冷静期中"})
    assert g["can_trade"] is False
    assert "冷静期" in g["open_block_note"]


def test_block_force_close_window():
    # 2026-07-07 00:02 Asia/Shanghai
    from datetime import datetime
    from zoneinfo import ZoneInfo

    now_ms = int(datetime(2026, 7, 7, 0, 2, tzinfo=ZoneInfo("Asia/Shanghai")).timestamp() * 1000)
    g = _base(force_close_enabled=True, force_close_bj_hour=0, now_ms=now_ms)
    assert g["can_trade"] is False
    assert g["force_close_blocks"] is True
    assert "强制清仓" in g["open_block_note"]
    assert "5 分钟" in g["open_block_note"]
