"""趋势回调中控 enrich:补仓次数与加仓价."""
from __future__ import annotations

import json
import sys
import unittest
from pathlib import Path
from unittest.mock import MagicMock

ROOT = Path(__file__).resolve().parents[1]
sys.path.insert(0, str(ROOT))

from lib.strategy.strategy_trend_register import _trend_add_leg_fields  # noqa: E402


class TestTrendHubEnrich(unittest.TestCase):
    def test_add_count_and_prices(self):
        mock_ex = MagicMock()
        mock_ex.price_to_precision = lambda sym, px: f"{float(px):.4f}"
        app_mod = MagicMock()
        app_mod.exchange = mock_ex
        app_mod.ensure_markets_loaded = MagicMock()
        app_mod.normalize_exchange_symbol = lambda s: s
        cfg = {"app_module": app_mod}
        raw = {
            "symbol": "ETH/USDT",
            "exchange_symbol": "ETH/USDT:USDT",
            "legs_done": 2,
            "dca_legs": 5,
            "grid_prices_json": json.dumps([1800.1, 1750.2, 1700.3]),
            "stop_loss": 1600,
            "take_profit": 2000,
            "avg_entry_price": 1820.5,
        }
        out = _trend_add_leg_fields(cfg, raw)
        self.assertEqual(out["add_count"], 2)
        self.assertEqual(out["add_count_total"], 5)
        self.assertEqual(out["add_prices"], [1800.1, 1750.2])
        self.assertEqual(len(out["add_prices_display"]), 2)
        self.assertEqual(out["stop_loss_display"], "1600.0000")


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