Skip to content

"Vibe-Trading: Your Personal Trading Agent"

12.1 KB
Raw Blame
"""Tests for the dragon-tiger (龙虎榜) tool.

No request leaves the process: the HTTP boundary
(:func:`backtest.loaders.eastmoney_client.throttled_get_json`) is mocked so the
real client + tool parsing runs offline.
"""

from __future__ import annotations

import json
from typing import Any
from unittest.mock import patch

import pytest

from backtest.loaders import eastmoney_client
from src.tools.dragon_tiger_tool import DragonTigerTool, _bare_code, _compact_date


def _appearance_payload() -> dict[str, Any]:
    """A datacenter appearance payload with two board rows."""
    return {
        "result": {
            "data": [
                {
                    "SECURITY_CODE": "600519",
                    "SECURITY_NAME_ABBR": "贵州茅台",
                    "CLOSE_PRICE": 1700.0,
                    "CHANGE_RATE": 5.2,
                    "BILLBOARD_NET_AMT": 1.2e8,
                    "BILLBOARD_BUY_AMT": 3.0e8,
                    "BILLBOARD_SELL_AMT": 1.8e8,
                    "ACCUM_AMOUNT": 9.0e8,
                    "EXPLANATION": "日涨幅偏离值达7%",
                },
                {
                    "SECURITY_CODE": "000001",
                    "SECURITY_NAME_ABBR": "平安银行",
                    "CLOSE_PRICE": 12.0,
                    "CHANGE_RATE": -4.1,
                    "BILLBOARD_NET_AMT": -5.0e7,
                    "BILLBOARD_BUY_AMT": 1.0e8,
                    "BILLBOARD_SELL_AMT": 1.5e8,
                    "ACCUM_AMOUNT": 4.0e8,
                    "EXPLANATION": "日跌幅偏离值达7%",
                },
            ]
        }
    }


def _seat_payload(side: str) -> dict[str, Any]:
    """A live Eastmoney buy- or sell-side seat payload."""
    return {
        "success": True,
        "result": {
            "data": [
                {
                    "OPERATEDEPT_NAME": "机构专用",
                    "BUY": 2.0e8,
                    "SELL": 0.0,
                    "NET": 2.0e8,
                    "EXPLANATION": "日涨幅偏离值达7%",
                    "TRADE_ID": "trade-1",
                }
            ]
        },
    }


def _rejected_payload() -> dict[str, Any]:
    return {
        "success": False,
        "result": None,
        "code": 9501,
        "message": "报表配置不存在,RPT_BILLBOARD_TRADEDETAIL",
    }


def _empty_payload() -> dict[str, Any]:
    return {"success": False, "result": None, "code": 9201, "message": "返回数据为空"}


class TestHelpers:
    def test_compact_date_normalizes(self) -> None:
        assert _compact_date("20240102") == "2024-01-02"
        assert _compact_date(" 2024-01-02 ") == "2024-01-02"

    def test_compact_date_rejects_garbage(self) -> None:
        with pytest.raises(ValueError):
            _compact_date("not-a-date")

    def test_bare_code_strips_suffix(self) -> None:
        assert _bare_code("600519.SH") == "600519"
        assert _bare_code("000001") == "000001"


class TestToolContract:
    def test_name_and_schema(self) -> None:
        tool = DragonTigerTool()
        assert tool.name == "get_dragon_tiger"
        assert tool.is_readonly is True
        assert tool.parameters["required"] == ["date"]
        assert "code" in tool.parameters["properties"]


class TestExecuteSuccess:
    def test_full_market_list_no_code(self) -> None:
        tool = DragonTigerTool()
        with patch.object(
            eastmoney_client, "throttled_get_json", return_value=_appearance_payload()
        ) as http:
            out = json.loads(tool.execute(date="2024-01-02"))

        # Only the appearance report is queried when no code is supplied.
        http.assert_called_once()
        _, kwargs = http.call_args
        assert kwargs["host_key"] == "eastmoney"
        assert kwargs["params"]["reportName"] == "RPT_DAILYBILLBOARD_DETAILS"

        assert out["ok"] is True
        assert out["market"] == "a_share"
        assert out["source"] == "eastmoney"
        assert out["data"]["date"] == "2024-01-02"
        assert "seats" not in out["data"]
        assert len(out["data"]["appearances"]) == 2
        assert out["data"]["appearances"][0]["code"] == "600519"
        assert out["data"]["appearances"][0]["net_buy"] == pytest.approx(1.2e8)

    def test_with_code_adds_seats(self) -> None:
        tool = DragonTigerTool()
        payloads = [_appearance_payload(), _seat_payload("BUY"), _seat_payload("SELL")]
        with patch.object(
            eastmoney_client, "throttled_get_json", side_effect=payloads
        ) as http:
            out = json.loads(tool.execute(date="2024-01-02", code="600519.SH"))

        assert http.call_count == 3
        assert out["ok"] is True
        assert out["data"]["code"] == "600519"
        assert len(out["data"]["seats"]) == 2
        assert out["data"]["seats"][0]["side"] == "BUY"
        assert out["data"]["seats"][0]["rank"] == 1
        assert out["data"]["seats"][0]["trade_id"] == "trade-1"
        assert out["data"]["seats"][1]["side"] == "SELL"
        assert out["data"]["seats"][1]["net"] == pytest.approx(2.0e8)

        requests = [call.kwargs["params"] for call in http.call_args_list]
        assert requests[1]["reportName"] == "RPT_BILLBOARD_DAILYDETAILSBUY"
        assert requests[1]["sortColumns"] == "BUY"
        assert requests[2]["reportName"] == "RPT_BILLBOARD_DAILYDETAILSSELL"
        assert requests[2]["sortColumns"] == "SELL"


def _seat(name: str, amount: float, trade_id: str, side: str) -> dict[str, Any]:
    return {
        "OPERATEDEPT_NAME": name,
        "BUY": amount if side == "BUY" else 0.0,
        "SELL": amount if side == "SELL" else 0.0,
        "NET": amount if side == "BUY" else -amount,
        "EXPLANATION": f"reason {trade_id}",
        "TRADE_ID": trade_id,
    }


def _seats_payload(rows: list[dict[str, Any]]) -> dict[str, Any]:
    return {"success": True, "result": {"data": rows}}


class TestSeatsByReason:
    """600721 on 2026-09-21 was listed under three TRADE_IDs; each report
    returns every reason's top 5 in one list sorted by amount."""

    def _run(self, buy: list[dict[str, Any]], sell: list[dict[str, Any]]) -> dict[str, Any]:
        payloads = [_appearance_payload(), _seats_payload(buy), _seats_payload(sell)]
        with patch.object(eastmoney_client, "throttled_get_json", side_effect=payloads):
            return json.loads(DragonTigerTool().execute(date="2026-09-21", code="600721"))["data"]

    def test_rank_restarts_within_each_reason(self) -> None:
        buy = [_seat("A", 9.0, "t1", "BUY"), _seat("B", 8.0, "t2", "BUY"), _seat("C", 7.0, "t1", "BUY")]
        sell = [_seat("D", 6.0, "t2", "SELL"), _seat("E", 5.0, "t1", "SELL")]

        seats = self._run(buy, sell)["seats"]

        ranks = [(s["trade_id"], s["side"], s["seat"], s["rank"]) for s in seats]
        assert ranks == [
            ("t1", "BUY", "A", 1),
            ("t1", "BUY", "C", 2),
            ("t1", "SELL", "E", 1),
            ("t2", "BUY", "B", 1),
            ("t2", "SELL", "D", 1),
        ]

    def test_cap_drops_whole_reasons_not_the_sell_side(self) -> None:
        buy, sell = [], []
        for tid in ("t1", "t2", "t3", "t4"):
            buy += [_seat(f"{tid}-b{i}", 100.0 - i, tid, "BUY") for i in range(5)]
            sell += [_seat(f"{tid}-s{i}", 100.0 - i, tid, "SELL") for i in range(5)]

        data = self._run(buy, sell)

        assert len(data["seats"]) == 30
        assert {s["trade_id"] for s in data["seats"]} == {"t1", "t2", "t3"}
        for tid in ("t1", "t2", "t3"):
            sides = [s["side"] for s in data["seats"] if s["trade_id"] == tid]
            assert sides.count("BUY") == 5 and sides.count("SELL") == 5
        assert data["seat_reasons_omitted"] == 1

    def test_no_omission_marker_when_everything_fits(self) -> None:
        data = self._run([_seat("A", 1.0, "t1", "BUY")], [_seat("B", 1.0, "t1", "SELL")])
        assert "seat_reasons_omitted" not in data


class TestExecuteError:
    def test_missing_date_returns_error_envelope(self) -> None:
        out = json.loads(DragonTigerTool().execute())
        assert out["ok"] is False
        assert "date" in out["error"]

    def test_bad_date_returns_error_envelope(self) -> None:
        out = json.loads(DragonTigerTool().execute(date="nope"))
        assert out["ok"] is False
        assert "invalid date" in out["error"]

    def test_http_failure_returns_error_envelope(self) -> None:
        tool = DragonTigerTool()
        with (
            patch.object(
                eastmoney_client,
                "throttled_get_json",
                side_effect=RuntimeError("eastmoney banned"),
            ),
            patch(
                "src.tools.dragon_tiger_tool.tushare_fallbacks.fetch_dragon_tiger",
                side_effect=RuntimeError("no fallback"),
            ),
        ):
            out = json.loads(tool.execute(date="2024-01-02"))

        assert out["ok"] is False
        assert "eastmoney banned" in out["error"]

    def test_http_failure_uses_tushare_fallback_when_available(self) -> None:
        fallback = {
            "date": "2024-01-02",
            "count": 1,
            "appearances": [{"code": "600519", "net_buy": 1.0}],
        }
        tool = DragonTigerTool()
        with (
            patch.object(
                eastmoney_client,
                "throttled_get_json",
                side_effect=RuntimeError("eastmoney banned"),
            ),
            patch(
                "src.tools.dragon_tiger_tool.tushare_fallbacks.fetch_dragon_tiger",
                return_value=fallback,
            ) as fallback_fetch,
        ):
            out = json.loads(tool.execute(date="2024-01-02", code="600519.SH"))

        fallback_fetch.assert_called_once_with("2024-01-02", "600519")
        assert out["ok"] is True
        assert out["source"] == "tushare"
        assert out["data"]["appearances"][0]["code"] == "600519"
        assert "used tushare fallback" in out["warnings"][0]

    def test_rejected_report_uses_tushare_fallback(self) -> None:
        fallback = {
            "date": "2024-01-02",
            "count": 1,
            "appearances": [{"code": "600519", "net_buy": 1.0}],
        }
        tool = DragonTigerTool()
        with (
            patch.object(
                eastmoney_client,
                "throttled_get_json",
                side_effect=[_appearance_payload(), _rejected_payload()],
            ),
            patch(
                "src.tools.dragon_tiger_tool.tushare_fallbacks.fetch_dragon_tiger",
                return_value=fallback,
            ) as fallback_fetch,
        ):
            out = json.loads(tool.execute(date="2024-01-02", code="600519.SH"))

        fallback_fetch.assert_called_once_with("2024-01-02", "600519")
        assert out["ok"] is True
        assert out["source"] == "tushare"
        assert "9501" in out["warnings"][0]

    def test_empty_report_remains_empty(self) -> None:
        tool = DragonTigerTool()
        with patch.object(
            eastmoney_client,
            "throttled_get_json",
            side_effect=[_appearance_payload(), _empty_payload(), _empty_payload()],
        ):
            out = json.loads(tool.execute(date="2024-01-02", code="600519.SH"))

        assert out["ok"] is True
        assert out["source"] == "eastmoney"
        assert out["data"]["seats"] == []

    def test_sell_report_rejection_uses_tushare_fallback(self) -> None:
        fallback = {
            "date": "2024-01-02",
            "count": 1,
            "appearances": [{"code": "600519", "net_buy": 1.0}],
        }
        tool = DragonTigerTool()
        with (
            patch.object(
                eastmoney_client,
                "throttled_get_json",
                side_effect=[
                    _appearance_payload(),
                    _seat_payload("BUY"),
                    _rejected_payload(),
                ],
            ),
            patch(
                "src.tools.dragon_tiger_tool.tushare_fallbacks.fetch_dragon_tiger",
                return_value=fallback,
            ) as fallback_fetch,
        ):
            out = json.loads(tool.execute(date="2024-01-02", code="600519.SH"))

        fallback_fetch.assert_called_once_with("2024-01-02", "600519")
        assert out["ok"] is True
        assert out["source"] == "tushare"
        assert "9501" in out["warnings"][0]