"""hub_ai:期权持仓进教练上下文 + 执行手册摘要."""
from __future__ import annotations

import unittest

from hub_ai.context import (
    format_chat_context_for_chat,
    format_chat_context_slim,
    format_chat_position_overview,
)
from hub_ai.playbook_brief import format_playbook_brief_for_chat
from hub_ai.prompts import build_chat_user_prompt


def _sample_payload():
    return {
        "totals": {
            "trading_day": "2026-07-22",
            "total_pnl_u": 0,
            "closed_count": 0,
            "win_count": 0,
            "loss_count": 0,
            "float_pnl_u": 1.25,
            "open_position_count": 1,
            "options_open_position_count": 1,
            "perpetual_open_position_count": 0,
        },
        "accounts": [
            {
                "name": "OKX_趋势",
                "key": "okx",
                "status": "已监控",
                "open_position_count": 1,
                "options_open_position_count": 1,
                "float_pnl_u": 1.25,
                "funding_usdt": 100,
                "trading_usdt": 50,
                "trade_stats": {"total_pnl_u": 0, "closed_count": 0, "win_count": 0, "loss_count": 0},
                "positions": [],
                "trades": [],
                "monitor_lines": {},
                "options_snapshot": {
                    "ok": True,
                    "enabled": True,
                    "positions": [
                        {
                            "inst_id": "ETH-USD-260723-3500-C",
                            "opt_type": "C",
                            "pos": 1,
                            "premium_paid": 8.5,
                            "source_label": "纯期权",
                            "source": "option",
                            "upl": 1.25,
                            "target_monitor_text": "目标 3600",
                        }
                    ],
                },
            }
        ],
    }


class HubAiOptionsPlaybookTests(unittest.TestCase):
    def test_chat_slim_includes_options_line(self):
        text = format_chat_context_slim(_sample_payload())
        self.assertIn("期权 ETH-USD-260723-3500-C Call", text)
        self.assertIn("永续0/期权1", text)
        self.assertIn("权利金8.5U", text)

    def test_overview_lists_options(self):
        text = format_chat_position_overview(_sample_payload())
        self.assertIn("期权1", text)
        self.assertIn("ETH-USD-260723-3500-C", text)

    def test_chat_bundle_keeps_options(self):
        text = format_chat_context_for_chat(_sample_payload(), max_chars=8000)
        self.assertIn("ETH-USD-260723-3500-C", text)
        self.assertIn("期权", text)

    def test_playbook_brief_injected(self):
        brief = format_playbook_brief_for_chat()
        self.assertIn("执行手册", brief)
        self.assertIn("OKX", brief)
        self.assertIn("不手动平仓", brief)
        prompt = build_chat_user_prompt(
            context_text="快照",
            trading_day="2026-07-22",
            summary_excerpt="",
            user_message="今天怎么样",
            playbook_brief=brief,
        )
        self.assertIn("用户策略执行手册", prompt)
        self.assertIn("不手动平仓", prompt)


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