"""OKX USDT/USDC 现货市价兑换参数."""
from __future__ import annotations

import unittest
from unittest.mock import MagicMock

from lib.exchange.okx_options_lib import spot_market_swap_usdt_usdc


class TestOkxSpotSwap(unittest.TestCase):
    def test_usdt_to_usdc_uses_quote_ccy(self):
        ex = MagicMock()
        ex.private_post_trade_order.return_value = {
            "data": [{"sCode": "0", "ordId": "1"}],
        }
        result = spot_market_swap_usdt_usdc(ex, direction="usdt_to_usdc", amount=10)
        self.assertTrue(result["ok"])
        body = ex.private_post_trade_order.call_args[0][0]
        self.assertEqual(body["tgtCcy"], "quote_ccy")
        self.assertEqual(body["side"], "buy")

    def test_usdc_to_usdt_uses_base_ccy(self):
        ex = MagicMock()
        ex.private_post_trade_order.return_value = {
            "data": [{"sCode": "0", "ordId": "2"}],
        }
        result = spot_market_swap_usdt_usdc(ex, direction="usdc_to_usdt", amount=5)
        self.assertTrue(result["ok"])
        body = ex.private_post_trade_order.call_args[0][0]
        self.assertEqual(body["tgtCcy"], "base_ccy")
        self.assertEqual(body["side"], "sell")

    def test_insufficient_balance_returns_chinese_message(self):
        ex = MagicMock()
        ex.private_post_trade_order.side_effect = Exception(
            'okx {"code":"1","data":[{"sCode":"51008","sMsg":"Order failed. Your available USDT balance is insufficient."}]}'
        )
        result = spot_market_swap_usdt_usdc(ex, direction="usdt_to_usdc", amount=20)
        self.assertFalse(result["ok"])
        self.assertEqual(result["msg"], "资金账户 USDT 可用余额不足")
        self.assertNotIn("{", result["msg"])


if __name__ == "__main__":
    unittest.main()
