"""AI 复盘 journal 文本格式化(三所共用)."""
from __future__ import annotations

import sqlite3
import sys
import unittest
from pathlib import Path

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

from lib.ai.ai_review_lib import journal_row_lines_for_ai  # noqa: E402


class TestAiReviewLib(unittest.TestCase):
    def test_journal_row_includes_expect_and_actual_rr(self):
        text = journal_row_lines_for_ai(
            1,
            {
                "coin": "HYPE",
                "tf": "5m",
                "pnl": "10.73",
                "real_rr": "2.1354",
                "expect_rr": "-",
                "entry_reason": "趋势回调",
                "exit_reason": "移动止盈",
                "hold_duration": "1天 3小时",
                "mood_issues": "",
                "post_breakeven_stare": "否",
                "new_trade_while_occupied": "否",
                "note": "测试备注",
            },
        )
        self.assertIn("实际RR:2.1354", text)
        self.assertIn("预期RR:-", text)
        self.assertIn("开仓逻辑:趋势回调", text)
        self.assertIn("备注:测试备注", text)
        self.assertNotIn("开仓类型", text)

    def test_journal_row_accepts_sqlite_row(self):
        conn = sqlite3.connect(":memory:")
        conn.row_factory = sqlite3.Row
        conn.execute(
            """CREATE TABLE journal_entries (
                coin TEXT, tf TEXT, pnl TEXT, real_rr TEXT, expect_rr TEXT,
                entry_reason TEXT, exit_reason TEXT, hold_duration TEXT,
                mood_issues TEXT, mood_score INTEGER, note TEXT
            )"""
        )
        conn.execute(
            """INSERT INTO journal_entries VALUES (?,?,?,?,?,?,?,?,?,?,?)""",
            ("BTC", "15m", "5", "1.2", "2.0", "突破", "止盈", "2小时", "", None, ""),
        )
        row = conn.execute("SELECT * FROM journal_entries").fetchone()
        conn.close()
        text = journal_row_lines_for_ai(1, row)
        self.assertIn("BTC 15m", text)
        self.assertIn("实际RR:1.2", text)
        self.assertIn("开仓逻辑:突破", text)


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