from __future__ import annotations

import pytest

from src.market_data.acquisition.exceptions import QuoteSchemaError
from src.market_data.acquisition.validation.schema import (
    validate_dzengi_websocket_quote_schema,
)


def test_accepts_direct_unwrapped_message() -> None:
    result = validate_dzengi_websocket_quote_schema(
        {"symbol": "BTC/USD", "bid": "10", "ask": "11"}
    )
    assert result.payload["bid"] == "10"


def test_accepts_double_payload_wrapper_and_root_symbol() -> None:
    result = validate_dzengi_websocket_quote_schema(
        {
            "symbol": "BTC/USD",
            "Payload": {"payload": {"bids": [["10", "1"]], "asks": [["11", "1"]]}},
        }
    )
    assert result.root_symbol == "BTC/USD"


def test_accepts_ofr_alias() -> None:
    validate_dzengi_websocket_quote_schema(
        {"symbolName": "BTC/USD", "bid": "10", "ofr": "11"}
    )


@pytest.mark.parametrize(
    "document",
    [
        [],
        {"bid": "10", "ask": "11"},
        {"symbol": "BTC/USD", "bid": "10"},
        {"symbol": "BTC/USD", "bids": [], "asks": [["11"]]},
        {"symbol": "BTC/USD", "bids": [["10"]], "asks": []},
    ],
)
def test_rejects_invalid_structure(document: object) -> None:
    with pytest.raises(QuoteSchemaError):
        validate_dzengi_websocket_quote_schema(document)
