# app/tests/unit/market_data/acquisition/validation/test_quote_schema.py

from __future__ import annotations

import pytest

from src.market_data.acquisition.exceptions import QuoteSchemaError
from src.market_data.acquisition.validation.schema import validate_quote_schema


def _document() -> dict[str, object]:
    return {
        "symbol": "BTC/USD_LEVERAGE",
        "lastPrice": "64159.45",
        "bidPrice": "64159.45",
        "askPrice": "64159.55",
        "closeTime": 1783887270312,
        "highPrice": "64261.45",
    }


def test_validate_quote_schema_accepts_real_unwrapped_document() -> None:
    document = _document()

    result = validate_quote_schema(document)

    assert result.is_wrapped is False
    assert result.status is None
    assert result.correlation_id is None
    assert dict(result.payload) == document


def test_validate_quote_schema_accepts_wrapped_document() -> None:
    payload = _document()

    result = validate_quote_schema(
        {
            "status": "OK",
            "correlationId": "quote-1",
            "payload": payload,
        }
    )

    assert result.is_wrapped is True
    assert result.status == "OK"
    assert result.correlation_id == "quote-1"
    assert dict(result.payload) == payload


@pytest.mark.parametrize(
    "missing_key",
    [
        "symbol",
        "lastPrice",
        "bidPrice",
        "askPrice",
        "closeTime",
    ],
)
def test_validate_quote_schema_rejects_missing_required_field(
    missing_key: str,
) -> None:
    document = _document()
    document.pop(missing_key)

    with pytest.raises(QuoteSchemaError, match=missing_key):
        validate_quote_schema(document)


def test_validate_quote_schema_rejects_non_mapping_root() -> None:
    with pytest.raises(QuoteSchemaError, match="JSON-объектом"):
        validate_quote_schema([])
