from __future__ import annotations

from config import BotConfig
from restart_state import recover_position_state


def test_recover_position_state_uses_persisted_paper_metadata() -> None:
    cfg = BotConfig(dead_fish_time_limit_mins=10)
    pos = {
        "contracts": 100.0,
        "entryPrice": 0.30,
        "entryTs": 1_700_000_000.0,
        "highPrice": 0.34,
        "atrSlPx": 0.285,
        "beActive": True,
        "trailingActive": True,
    }

    state, reconstructed = recover_position_state(pos, now_ts=1_700_000_600.0, cfg=cfg, atr=0.01)

    assert reconstructed is False
    assert state.entry_ts == 1_700_000_000.0
    assert state.high_px == 0.34
    assert state.atr_sl_px == 0.285
    assert state.be_active is True
    assert state.trailing_active is True


def test_recover_position_state_without_metadata_is_conservative() -> None:
    cfg = BotConfig(dead_fish_time_limit_mins=10, atr_sl_multiplier=1.5)
    pos = {"contracts": 100.0, "entryPrice": 0.30}

    state, reconstructed = recover_position_state(pos, now_ts=1_700_000_600.0, cfg=cfg, atr=0.01)

    assert reconstructed is True
    assert state.entry_ts == 1_700_000_000.0
    assert state.high_px == 0.30
    assert state.atr_sl_px == 0.285
