from __future__ import annotations

from crypto_spot_bot.market_data import _closed_candles, _is_closed_kline_row
from crypto_spot_bot.models import Candle


def test_closed_candles_excludes_current_open_interval() -> None:
    candles = [
        Candle(0, 100, 101, 99, 100, 10),
        Candle(3_600_000, 101, 102, 100, 101, 10),
        Candle(7_200_000, 102, 103, 101, 102, 10),
    ]

    closed = _closed_candles(candles, "60", now_ms=7_200_000 + 1_000)

    assert [candle.timestamp for candle in closed] == [0, 3_600_000]


def test_websocket_kline_requires_confirmed_candle() -> None:
    assert _is_closed_kline_row({"start": 7_200_000, "confirm": False}, "60") is False
    assert _is_closed_kline_row({"start": 7_200_000, "confirm": True}, "60") is True
