"""instance_embed_context_lib 顶栏统计."""
from __future__ import annotations

import unittest

from lib.instance.instance_embed_context_lib import (
    options_funding_label,
    profit_loss_ratio_from_averages,
    profit_loss_ratio_from_trades,
    total_funds_usdt,
)


class TestHeaderStatsLib(unittest.TestCase):
    def test_profit_loss_ratio_from_averages(self):
        self.assertEqual(profit_loss_ratio_from_averages(9.0, -3.0), 3.0)
        self.assertIsNone(profit_loss_ratio_from_averages(9.0, 0))

    def test_profit_loss_ratio_from_trades(self):
        trades = [
            {"effective_pnl_amount": 10},
            {"effective_pnl_amount": 8},
            {"effective_pnl_amount": -4},
            {"effective_pnl_amount": -2},
        ]
        self.assertEqual(profit_loss_ratio_from_trades(trades), 3.0)

    def test_total_funds_usdt(self):
        self.assertEqual(total_funds_usdt(100.5, 59.27), 159.77)
        self.assertEqual(total_funds_usdt(None, 10), 10.0)
        self.assertIsNone(total_funds_usdt(None, None))
        self.assertEqual(
            total_funds_usdt(100, 50, options_trading_usdc=0.2, options_trading_usdt=10),
            160.2,
        )

    def test_options_funding_label(self):
        self.assertEqual(options_funding_label(1.5, 10), "1.50 USDC")
        self.assertEqual(options_funding_label(10.19, 0), "10.19 USDC")
        self.assertEqual(options_funding_label(None, 10), "—")
        self.assertEqual(options_funding_label(None, None), "—")

    def test_trading_account_label_coin(self):
        from lib.instance.instance_embed_context_lib import trading_account_label

        self.assertEqual(trading_account_label(100, None, None, margin_mode="usdc"), "100.00U")
        self.assertEqual(
            trading_account_label(100, 0.2, 0.001, margin_mode="coin"),
            "100.00 USDT\n0.2 ETH\n0.001 BTC",
        )
        self.assertEqual(
            trading_account_label(0.02, 0.0, None, margin_mode="coin"),
            "0.02 USDT",
        )
        self.assertEqual(
            trading_account_label(12.5, 0.004321, None, margin_mode="coin"),
            "12.50 USDT\n0.004321 ETH",
        )


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