"""
Binance Spot WebSocket API

OpenAPI Specifications for the Binance Spot WebSocket API

API documents:
  - [Github web-socket-api documentation file](https://github.com/binance/binance-spot-api-docs/blob/master/web-socket-api.md)
  - [General API information for web-socket-api on website](https://developers.binance.com/docs/binance-spot-api-docs/web-socket-api/general-api-information)

The version of the OpenAPI document: 1.0.0
Generated by OpenAPI Generator (https://openapi-generator.tech)

Do not edit the class manually.
"""

import pytest

from unittest.mock import AsyncMock, MagicMock

from binance_common.models import WebsocketApiResponse
from binance_common.utils import parse_ws_rate_limit_headers
from binance_common.errors import RequiredError
from binance_sdk_spot.websocket_api.api import TradeApi


from binance_sdk_spot.websocket_api.models import OrderCancelCancelRestrictionsEnum
from binance_sdk_spot.websocket_api.models import (
    OrderCancelReplaceCancelReplaceModeEnum,
)
from binance_sdk_spot.websocket_api.models import OrderCancelReplaceSideEnum
from binance_sdk_spot.websocket_api.models import OrderCancelReplaceTypeEnum
from binance_sdk_spot.websocket_api.models import OrderCancelReplaceTimeInForceEnum
from binance_sdk_spot.websocket_api.models import OrderCancelReplaceNewOrderRespTypeEnum
from binance_sdk_spot.websocket_api.models import (
    OrderCancelReplaceSelfTradePreventionModeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderCancelReplaceCancelRestrictionsEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderCancelReplaceOrderRateLimitExceededModeEnum,
)
from binance_sdk_spot.websocket_api.models import OrderCancelReplacePegPriceTypeEnum
from binance_sdk_spot.websocket_api.models import OrderCancelReplacePegOffsetTypeEnum
from binance_sdk_spot.websocket_api.models import OrderListPlaceSideEnum
from binance_sdk_spot.websocket_api.models import OrderListPlaceStopLimitTimeInForceEnum
from binance_sdk_spot.websocket_api.models import OrderListPlaceNewOrderRespTypeEnum
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceSelfTradePreventionModeEnum,
)
from binance_sdk_spot.websocket_api.models import OrderListPlaceOcoSideEnum
from binance_sdk_spot.websocket_api.models import OrderListPlaceOcoAboveTypeEnum
from binance_sdk_spot.websocket_api.models import OrderListPlaceOcoBelowTypeEnum
from binance_sdk_spot.websocket_api.models import OrderListPlaceOcoAboveTimeInForceEnum
from binance_sdk_spot.websocket_api.models import OrderListPlaceOcoAbovePegPriceTypeEnum
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOcoAbovePegOffsetTypeEnum,
)
from binance_sdk_spot.websocket_api.models import OrderListPlaceOcoBelowTimeInForceEnum
from binance_sdk_spot.websocket_api.models import OrderListPlaceOcoBelowPegPriceTypeEnum
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOcoBelowPegOffsetTypeEnum,
)
from binance_sdk_spot.websocket_api.models import OrderListPlaceOcoNewOrderRespTypeEnum
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOcoSelfTradePreventionModeEnum,
)
from binance_sdk_spot.websocket_api.models import OrderListPlaceOpoWorkingTypeEnum
from binance_sdk_spot.websocket_api.models import OrderListPlaceOpoWorkingSideEnum
from binance_sdk_spot.websocket_api.models import OrderListPlaceOpoPendingTypeEnum
from binance_sdk_spot.websocket_api.models import OrderListPlaceOpoPendingSideEnum
from binance_sdk_spot.websocket_api.models import OrderListPlaceOpoNewOrderRespTypeEnum
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOpoSelfTradePreventionModeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOpoWorkingTimeInForceEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOpoWorkingPegPriceTypeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOpoWorkingPegOffsetTypeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOpoPendingTimeInForceEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOpoPendingPegPriceTypeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOpoPendingPegOffsetTypeEnum,
)
from binance_sdk_spot.websocket_api.models import OrderListPlaceOpocoWorkingTypeEnum
from binance_sdk_spot.websocket_api.models import OrderListPlaceOpocoWorkingSideEnum
from binance_sdk_spot.websocket_api.models import OrderListPlaceOpocoPendingSideEnum
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOpocoPendingAboveTypeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOpocoNewOrderRespTypeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOpocoSelfTradePreventionModeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOpocoWorkingTimeInForceEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOpocoWorkingPegPriceTypeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOpocoWorkingPegOffsetTypeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOpocoPendingAboveTimeInForceEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOpocoPendingAbovePegPriceTypeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOpocoPendingAbovePegOffsetTypeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOpocoPendingBelowTypeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOpocoPendingBelowTimeInForceEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOpocoPendingBelowPegPriceTypeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOpocoPendingBelowPegOffsetTypeEnum,
)
from binance_sdk_spot.websocket_api.models import OrderListPlaceOtoWorkingTypeEnum
from binance_sdk_spot.websocket_api.models import OrderListPlaceOtoWorkingSideEnum
from binance_sdk_spot.websocket_api.models import OrderListPlaceOtoPendingTypeEnum
from binance_sdk_spot.websocket_api.models import OrderListPlaceOtoPendingSideEnum
from binance_sdk_spot.websocket_api.models import OrderListPlaceOtoNewOrderRespTypeEnum
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOtoSelfTradePreventionModeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOtoWorkingTimeInForceEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOtoWorkingPegPriceTypeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOtoWorkingPegOffsetTypeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOtoPendingTimeInForceEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOtoPendingPegOffsetTypeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOtoPendingPegPriceTypeEnum,
)
from binance_sdk_spot.websocket_api.models import OrderListPlaceOtocoWorkingTypeEnum
from binance_sdk_spot.websocket_api.models import OrderListPlaceOtocoWorkingSideEnum
from binance_sdk_spot.websocket_api.models import OrderListPlaceOtocoPendingSideEnum
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOtocoPendingAboveTypeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOtocoNewOrderRespTypeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOtocoSelfTradePreventionModeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOtocoWorkingTimeInForceEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOtocoWorkingPegPriceTypeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOtocoWorkingPegOffsetTypeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOtocoPendingAboveTimeInForceEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOtocoPendingAbovePegPriceTypeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOtocoPendingAbovePegOffsetTypeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOtocoPendingBelowTypeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOtocoPendingBelowTimeInForceEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOtocoPendingBelowPegPriceTypeEnum,
)
from binance_sdk_spot.websocket_api.models import (
    OrderListPlaceOtocoPendingBelowPegOffsetTypeEnum,
)
from binance_sdk_spot.websocket_api.models import OrderPlaceSideEnum
from binance_sdk_spot.websocket_api.models import OrderPlaceTypeEnum
from binance_sdk_spot.websocket_api.models import OrderPlaceTimeInForceEnum
from binance_sdk_spot.websocket_api.models import OrderPlaceNewOrderRespTypeEnum
from binance_sdk_spot.websocket_api.models import OrderPlaceSelfTradePreventionModeEnum
from binance_sdk_spot.websocket_api.models import OrderPlacePegPriceTypeEnum
from binance_sdk_spot.websocket_api.models import OrderPlacePegOffsetTypeEnum
from binance_sdk_spot.websocket_api.models import OrderTestSideEnum
from binance_sdk_spot.websocket_api.models import OrderTestTypeEnum
from binance_sdk_spot.websocket_api.models import OrderTestTimeInForceEnum
from binance_sdk_spot.websocket_api.models import OrderTestNewOrderRespTypeEnum
from binance_sdk_spot.websocket_api.models import OrderTestSelfTradePreventionModeEnum
from binance_sdk_spot.websocket_api.models import OrderTestPegPriceTypeEnum
from binance_sdk_spot.websocket_api.models import OrderTestPegOffsetTypeEnum
from binance_sdk_spot.websocket_api.models import SorOrderPlaceSideEnum
from binance_sdk_spot.websocket_api.models import SorOrderPlaceTypeEnum
from binance_sdk_spot.websocket_api.models import SorOrderPlaceTimeInForceEnum
from binance_sdk_spot.websocket_api.models import SorOrderPlaceNewOrderRespTypeEnum
from binance_sdk_spot.websocket_api.models import (
    SorOrderPlaceSelfTradePreventionModeEnum,
)
from binance_sdk_spot.websocket_api.models import SorOrderTestSideEnum
from binance_sdk_spot.websocket_api.models import SorOrderTestTypeEnum
from binance_sdk_spot.websocket_api.models import SorOrderTestTimeInForceEnum
from binance_sdk_spot.websocket_api.models import SorOrderTestNewOrderRespTypeEnum
from binance_sdk_spot.websocket_api.models import (
    SorOrderTestSelfTradePreventionModeEnum,
)
from binance_sdk_spot.websocket_api.models import OpenOrdersCancelAllResponse
from binance_sdk_spot.websocket_api.models import OrderAmendKeepPriorityResponse
from binance_sdk_spot.websocket_api.models import OrderCancelResponse
from binance_sdk_spot.websocket_api.models import OrderCancelReplaceResponse
from binance_sdk_spot.websocket_api.models import OrderListCancelResponse
from binance_sdk_spot.websocket_api.models import OrderListPlaceResponse
from binance_sdk_spot.websocket_api.models import OrderListPlaceOcoResponse
from binance_sdk_spot.websocket_api.models import OrderListPlaceOpoResponse
from binance_sdk_spot.websocket_api.models import OrderListPlaceOpocoResponse
from binance_sdk_spot.websocket_api.models import OrderListPlaceOtoResponse
from binance_sdk_spot.websocket_api.models import OrderListPlaceOtocoResponse
from binance_sdk_spot.websocket_api.models import OrderPlaceResponse
from binance_sdk_spot.websocket_api.models import OrderTestResponse
from binance_sdk_spot.websocket_api.models import SorOrderPlaceResponse
from binance_sdk_spot.websocket_api.models import SorOrderTestResponse


class TestWebSocketTradeApi:
    @pytest.fixture(autouse=True)
    def setup_method(self):
        self.mock_websocket_api = MagicMock()
        self.websocket_api = TradeApi(websocket_api=self.mock_websocket_api)

    @pytest.mark.asyncio
    async def test_open_orders_cancel_all_success(self):
        """Test open_orders_cancel_all() successfully with required parameters only."""

        params = {
            "symbol": "BNBUSDT",
        }

        expected_response = {
            "id": "778f938f-9041-4b88-9914-efbf64eeacc8",
            "status": 200,
            "result": [
                {
                    "orderListId": 19431,
                    "contingencyType": "OCO",
                    "listStatusType": "ALL_DONE",
                    "listOrderStatus": "ALL_DONE",
                    "listClientOrderId": "iuVNVJYYrByz6C4yGOPPK0",
                    "transactionTime": 1660803702431,
                    "symbol": "BTCUSDT",
                    "orders": [
                        {
                            "symbol": "BTCUSDT",
                            "orderId": 12569099454,
                            "clientOrderId": "Tnu2IP0J5Y4mxw3IATBfmW",
                        },
                        {
                            "symbol": "BTCUSDT",
                            "orderId": 12569099453,
                            "clientOrderId": "bX5wROblo6YeDwa9iTLeyY",
                        },
                    ],
                    "orderReports": [
                        {
                            "symbol": "BTCUSDT",
                            "origClientOrderId": "Tnu2IP0J5Y4mxw3IATBfmW",
                            "orderId": 12569099454,
                            "orderListId": 19431,
                            "clientOrderId": "OFFXQtxVFZ6Nbcg4PgE2DA",
                            "transactTime": 1684804350068,
                            "price": "23400.00000000",
                            "origQty": "0.00850000",
                            "executedQty": "0.00000000",
                            "origQuoteOrderQty": "0.000000",
                            "cummulativeQuoteQty": "0.00000000",
                            "status": "CANCELED",
                            "timeInForce": "GTC",
                            "type": "LIMIT_MAKER",
                            "side": "BUY",
                            "selfTradePreventionMode": "NONE",
                        },
                        {
                            "symbol": "BTCUSDT",
                            "origClientOrderId": "bX5wROblo6YeDwa9iTLeyY",
                            "orderId": 12569099453,
                            "orderListId": 19431,
                            "clientOrderId": "OFFXQtxVFZ6Nbcg4PgE2DA",
                            "transactTime": 1684804350068,
                            "price": "23450.50000000",
                            "origQty": "0.00850000",
                            "executedQty": "0.00000000",
                            "origQuoteOrderQty": "0.000000",
                            "cummulativeQuoteQty": "0.00000000",
                            "status": "CANCELED",
                            "timeInForce": "GTC",
                            "type": "STOP_LOSS_LIMIT",
                            "side": "BUY",
                            "stopPrice": "23430.00000000",
                            "selfTradePreventionMode": "NONE",
                        },
                    ],
                },
                {
                    "symbol": "BTCUSDT",
                    "origClientOrderId": "4d96324ff9d44481926157",
                    "orderId": 12569099453,
                    "orderListId": -1,
                    "clientOrderId": "91fe37ce9e69c90d6358c0",
                    "transactTime": 1684804350068,
                    "price": "23416.10000000",
                    "origQty": "0.00847000",
                    "executedQty": "0.00001000",
                    "origQuoteOrderQty": "0.000000",
                    "cummulativeQuoteQty": "0.23416100",
                    "status": "CANCELED",
                    "timeInForce": "GTC",
                    "type": "LIMIT",
                    "side": "SELL",
                    "stopPrice": "0.00000000",
                    "trailingDelta": 0,
                    "trailingTime": -1,
                    "icebergQty": "0.00000000",
                    "strategyId": 37463720,
                    "strategyType": 1000000,
                    "selfTradePreventionMode": "NONE",
                },
            ],
            "rateLimits": [
                {
                    "rateLimitType": "REQUEST_WEIGHT",
                    "interval": "MINUTE",
                    "intervalNum": 1,
                    "limit": 6000,
                    "count": 1,
                }
            ],
        }

        self.mock_websocket_api.send_signed_message = AsyncMock(
            return_value=WebsocketApiResponse(
                data_function=lambda: expected_response,
                rate_limits=(
                    parse_ws_rate_limit_headers(expected_response["rateLimits"])
                    if "rateLimits" in expected_response
                    else None
                ),
            )
        )
        result = await self.websocket_api.open_orders_cancel_all(**params)

        actual_call_args = self.mock_websocket_api.send_signed_message.call_args
        request_kwargs = actual_call_args.kwargs

        assert "payload" in request_kwargs
        assert "method" in request_kwargs["payload"]
        assert request_kwargs["payload"]["method"] == "/openOrders.cancelAll".replace(
            "/", "", 1
        )

        assert "params" in request_kwargs["payload"]
        params = request_kwargs["payload"]["params"]
        assert params["symbol"] == "BNBUSDT"

        assert result is not None
        assert result.data() == expected_response
        self.mock_websocket_api.send_signed_message.assert_called_once_with(
            payload={
                "method": "/openOrders.cancelAll".replace("/", "", 1),
                "params": params,
            },
            response_model=OpenOrdersCancelAllResponse,
            signer=None,
        )

    @pytest.mark.asyncio
    async def test_open_orders_cancel_all_success_with_optional_params(self):
        """Test open_orders_cancel_all() successfully with optional parameters."""

        params = {
            "symbol": "BNBUSDT",
            "id": "e9d6b4349871b40611412680b3445fac",
            "recv_window": 5000.0,
        }

        expected_response = {
            "id": "778f938f-9041-4b88-9914-efbf64eeacc8",
            "status": 200,
            "result": [
                {
                    "orderListId": 19431,
                    "contingencyType": "OCO",
                    "listStatusType": "ALL_DONE",
                    "listOrderStatus": "ALL_DONE",
                    "listClientOrderId": "iuVNVJYYrByz6C4yGOPPK0",
                    "transactionTime": 1660803702431,
                    "symbol": "BTCUSDT",
                    "orders": [
                        {
                            "symbol": "BTCUSDT",
                            "orderId": 12569099454,
                            "clientOrderId": "Tnu2IP0J5Y4mxw3IATBfmW",
                        },
                        {
                            "symbol": "BTCUSDT",
                            "orderId": 12569099453,
                            "clientOrderId": "bX5wROblo6YeDwa9iTLeyY",
                        },
                    ],
                    "orderReports": [
                        {
                            "symbol": "BTCUSDT",
                            "origClientOrderId": "Tnu2IP0J5Y4mxw3IATBfmW",
                            "orderId": 12569099454,
                            "orderListId": 19431,
                            "clientOrderId": "OFFXQtxVFZ6Nbcg4PgE2DA",
                            "transactTime": 1684804350068,
                            "price": "23400.00000000",
                            "origQty": "0.00850000",
                            "executedQty": "0.00000000",
                            "origQuoteOrderQty": "0.000000",
                            "cummulativeQuoteQty": "0.00000000",
                            "status": "CANCELED",
                            "timeInForce": "GTC",
                            "type": "LIMIT_MAKER",
                            "side": "BUY",
                            "selfTradePreventionMode": "NONE",
                        },
                        {
                            "symbol": "BTCUSDT",
                            "origClientOrderId": "bX5wROblo6YeDwa9iTLeyY",
                            "orderId": 12569099453,
                            "orderListId": 19431,
                            "clientOrderId": "OFFXQtxVFZ6Nbcg4PgE2DA",
                            "transactTime": 1684804350068,
                            "price": "23450.50000000",
                            "origQty": "0.00850000",
                            "executedQty": "0.00000000",
                            "origQuoteOrderQty": "0.000000",
                            "cummulativeQuoteQty": "0.00000000",
                            "status": "CANCELED",
                            "timeInForce": "GTC",
                            "type": "STOP_LOSS_LIMIT",
                            "side": "BUY",
                            "stopPrice": "23430.00000000",
                            "selfTradePreventionMode": "NONE",
                        },
                    ],
                },
                {
                    "symbol": "BTCUSDT",
                    "origClientOrderId": "4d96324ff9d44481926157",
                    "orderId": 12569099453,
                    "orderListId": -1,
                    "clientOrderId": "91fe37ce9e69c90d6358c0",
                    "transactTime": 1684804350068,
                    "price": "23416.10000000",
                    "origQty": "0.00847000",
                    "executedQty": "0.00001000",
                    "origQuoteOrderQty": "0.000000",
                    "cummulativeQuoteQty": "0.23416100",
                    "status": "CANCELED",
                    "timeInForce": "GTC",
                    "type": "LIMIT",
                    "side": "SELL",
                    "stopPrice": "0.00000000",
                    "trailingDelta": 0,
                    "trailingTime": -1,
                    "icebergQty": "0.00000000",
                    "strategyId": 37463720,
                    "strategyType": 1000000,
                    "selfTradePreventionMode": "NONE",
                },
            ],
            "rateLimits": [
                {
                    "rateLimitType": "REQUEST_WEIGHT",
                    "interval": "MINUTE",
                    "intervalNum": 1,
                    "limit": 6000,
                    "count": 1,
                }
            ],
        }

        self.mock_websocket_api.send_signed_message = AsyncMock(
            return_value=WebsocketApiResponse(
                data_function=lambda: expected_response,
                rate_limits=(
                    parse_ws_rate_limit_headers(expected_response["rateLimits"])
                    if "rateLimits" in expected_response
                    else None
                ),
            )
        )

        result = await self.websocket_api.open_orders_cancel_all(**params)

        actual_call_args = self.mock_websocket_api.send_signed_message.call_args
        request_kwargs = actual_call_args.kwargs

        assert "payload" in request_kwargs
        assert "method" in request_kwargs["payload"]
        assert request_kwargs["payload"]["method"] == "/openOrders.cancelAll".replace(
            "/", "", 1
        )
        assert params["symbol"] == "BNBUSDT"
        assert params["id"] == "e9d6b4349871b40611412680b3445fac"
        assert params["recv_window"] == 5000.0

        assert result is not None
        assert result.data() == expected_response
        self.mock_websocket_api.send_signed_message.assert_called_once_with(
            payload={
                "method": "/openOrders.cancelAll".replace("/", "", 1),
                "params": params,
            },
            response_model=OpenOrdersCancelAllResponse,
            signer=None,
        )

    @pytest.mark.asyncio
    async def test_open_orders_cancel_all_missing_required_param_symbol(self):
        """Test that open_orders_cancel_all() raises RequiredError when 'symbol' is missing."""

        params = {
            "symbol": "BNBUSDT",
            "id": "e9d6b4349871b40611412680b3445fac",
            "recv_window": 5000.0,
        }
        params["symbol"] = None

        with pytest.raises(RequiredError, match="Missing required parameter 'symbol'"):
            await self.websocket_api.open_orders_cancel_all(**params)

    @pytest.mark.asyncio
    async def test_open_orders_cancel_all_server_error(self):
        """Test that open_orders_cancel_all() raises an error when the server returns an error."""

        params = {
            "symbol": "BNBUSDT",
        }

        mock_error = Exception("ResponseError")
        self.mock_websocket_api.send_signed_message.side_effect = mock_error

        with pytest.raises(Exception, match="ResponseError"):
            await self.websocket_api.open_orders_cancel_all(**params)

    @pytest.mark.asyncio
    async def test_order_amend_keep_priority_success(self):
        """Test order_amend_keep_priority() successfully with required parameters only."""

        params = {
            "symbol": "BNBUSDT",
            "new_qty": 1.0,
        }

        expected_response = {
            "id": "56374b46-3061-486b-a311-89ee972eb648",
            "status": 200,
            "result": {
                "transactTime": 1741924229819,
                "executionId": 60,
                "amendedOrder": {
                    "symbol": "BTUCSDT",
                    "orderId": 23,
                    "orderListId": 4,
                    "origClientOrderId": "my_pending_order",
                    "clientOrderId": "xbxXh5SSwaHS7oUEOCI88B",
                    "price": "1.00000000",
                    "qty": "5.00000000",
                    "executedQty": "0.00000000",
                    "preventedQty": "0.00000000",
                    "quoteOrderQty": "0.00000000",
                    "cumulativeQuoteQty": "0.00000000",
                    "status": "NEW",
                    "timeInForce": "GTC",
                    "type": "LIMIT",
                    "side": "BUY",
                    "workingTime": 1741924204920,
                    "selfTradePreventionMode": "NONE",
                },
                "listStatus": {
                    "orderListId": 4,
                    "contingencyType": "OTO",
                    "listOrderStatus": "EXECUTING",
                    "listClientOrderId": "8nOGLLawudj1QoOiwbroRH",
                    "symbol": "BTCUSDT",
                    "orders": [
                        {
                            "symbol": "BTCUSDT",
                            "orderId": 23,
                            "clientOrderId": "xbxXh5SSwaHS7oUEOCI88B",
                        },
                        {
                            "symbol": "BTCUSDT",
                            "orderId": 22,
                            "clientOrderId": "g04EWsjaackzedjC9wRkWD",
                        },
                        {
                            "symbol": "BTCUSDT",
                            "orderId": 23,
                            "clientOrderId": "xbxXh5SSwaHS7oUEOCI88B",
                        },
                        {
                            "symbol": "BTCUSDT",
                            "orderId": 22,
                            "clientOrderId": "g04EWsjaackzedjC9wRkWD",
                        },
                    ],
                },
            },
            "rateLimits": [
                {
                    "rateLimitType": "REQUEST_WEIGHT",
                    "interval": "MINUTE",
                    "intervalNum": 1,
                    "limit": 6000,
                    "count": 1,
                }
            ],
        }

        self.mock_websocket_api.send_signed_message = AsyncMock(
            return_value=WebsocketApiResponse(
                data_function=lambda: expected_response,
                rate_limits=(
                    parse_ws_rate_limit_headers(expected_response["rateLimits"])
                    if "rateLimits" in expected_response
                    else None
                ),
            )
        )
        result = await self.websocket_api.order_amend_keep_priority(**params)

        actual_call_args = self.mock_websocket_api.send_signed_message.call_args
        request_kwargs = actual_call_args.kwargs

        assert "payload" in request_kwargs
        assert "method" in request_kwargs["payload"]
        assert request_kwargs["payload"][
            "method"
        ] == "/order.amend.keepPriority".replace("/", "", 1)

        assert params["symbol"] == "BNBUSDT"

        assert params["new_qty"] == 1.0

        assert result is not None
        assert result.data() == expected_response
        self.mock_websocket_api.send_signed_message.assert_called_once_with(
            payload={
                "method": "/order.amend.keepPriority".replace("/", "", 1),
                "params": params,
            },
            response_model=OrderAmendKeepPriorityResponse,
            signer=None,
        )

    @pytest.mark.asyncio
    async def test_order_amend_keep_priority_success_with_optional_params(self):
        """Test order_amend_keep_priority() successfully with optional parameters."""

        params = {
            "symbol": "BNBUSDT",
            "new_qty": 1.0,
            "id": "e9d6b4349871b40611412680b3445fac",
            "order_id": 1,
            "orig_client_order_id": "orig_client_order_id_example",
            "new_client_order_id": "new_client_order_id_example",
            "recv_window": 5000.0,
        }

        expected_response = {
            "id": "56374b46-3061-486b-a311-89ee972eb648",
            "status": 200,
            "result": {
                "transactTime": 1741924229819,
                "executionId": 60,
                "amendedOrder": {
                    "symbol": "BTUCSDT",
                    "orderId": 23,
                    "orderListId": 4,
                    "origClientOrderId": "my_pending_order",
                    "clientOrderId": "xbxXh5SSwaHS7oUEOCI88B",
                    "price": "1.00000000",
                    "qty": "5.00000000",
                    "executedQty": "0.00000000",
                    "preventedQty": "0.00000000",
                    "quoteOrderQty": "0.00000000",
                    "cumulativeQuoteQty": "0.00000000",
                    "status": "NEW",
                    "timeInForce": "GTC",
                    "type": "LIMIT",
                    "side": "BUY",
                    "workingTime": 1741924204920,
                    "selfTradePreventionMode": "NONE",
                },
                "listStatus": {
                    "orderListId": 4,
                    "contingencyType": "OTO",
                    "listOrderStatus": "EXECUTING",
                    "listClientOrderId": "8nOGLLawudj1QoOiwbroRH",
                    "symbol": "BTCUSDT",
                    "orders": [
                        {
                            "symbol": "BTCUSDT",
                            "orderId": 23,
                            "clientOrderId": "xbxXh5SSwaHS7oUEOCI88B",
                        },
                        {
                            "symbol": "BTCUSDT",
                            "orderId": 22,
                            "clientOrderId": "g04EWsjaackzedjC9wRkWD",
                        },
                        {
                            "symbol": "BTCUSDT",
                            "orderId": 23,
                            "clientOrderId": "xbxXh5SSwaHS7oUEOCI88B",
                        },
                        {
                            "symbol": "BTCUSDT",
                            "orderId": 22,
                            "clientOrderId": "g04EWsjaackzedjC9wRkWD",
                        },
                    ],
                },
            },
            "rateLimits": [
                {
                    "rateLimitType": "REQUEST_WEIGHT",
                    "interval": "MINUTE",
                    "intervalNum": 1,
                    "limit": 6000,
                    "count": 1,
                }
            ],
        }

        self.mock_websocket_api.send_signed_message = AsyncMock(
            return_value=WebsocketApiResponse(
                data_function=lambda: expected_response,
                rate_limits=(
                    parse_ws_rate_limit_headers(expected_response["rateLimits"])
                    if "rateLimits" in expected_response
                    else None
