"""Tests for benchmark tools."""

from unittest.mock import MagicMock, patch

import pytest

from tests.conftest import MockMCP


@pytest.fixture
def mcp():
    m = MockMCP()
    from kaggle_mcp import benchmarks
    benchmarks.register(m)
    return m


# ---------------------------------------------------------------------------
# benchmark_leaderboard
# ---------------------------------------------------------------------------

def test_benchmark_leaderboard_returns_formatted(mcp):
    with patch("kaggle_mcp.benchmarks.get_client") as mock_gc:
        task = MagicMock()
        task.benchmark_task_name = "Text Classification"
        task.task_version = 1
        task.result = MagicMock()
        task.result.__str__ = lambda self: "0.95"

        row = MagicMock()
        row.model_version_name = "BERT-base"
        row.model_version_slug = "bert-base"
        row.task_results = [task]

        resp = MagicMock()
        resp.rows = [row]
        mock_gc.return_value.benchmarks.benchmarks_api_client.get_benchmark_leaderboard.return_value = resp

        result = mcp.tools["benchmark_leaderboard"]("google", "glue")
        assert "BERT-base" in result
        assert "Text Classification" in result


def test_benchmark_leaderboard_empty(mcp):
    with patch("kaggle_mcp.benchmarks.get_client") as mock_gc:
        resp = MagicMock()
        resp.rows = []
        mock_gc.return_value.benchmarks.benchmarks_api_client.get_benchmark_leaderboard.return_value = resp

        result = mcp.tools["benchmark_leaderboard"]("google", "glue")
        assert "No leaderboard data found." in result


def test_benchmark_leaderboard_with_version(mcp):
    with patch("kaggle_mcp.benchmarks.get_client") as mock_gc:
        row = MagicMock()
        row.model_version_name = "T5-large"
        row.model_version_slug = "t5-large"
        row.task_results = []

        resp = MagicMock()
        resp.rows = [row]
        mock_gc.return_value.benchmarks.benchmarks_api_client.get_benchmark_leaderboard.return_value = resp

        result = mcp.tools["benchmark_leaderboard"]("google", "glue", version_number=2)
        assert "T5-large" in result
        assert "v2" in result


def test_benchmark_leaderboard_error(mcp):
    with patch("kaggle_mcp.benchmarks.get_client") as mock_gc:
        mock_gc.return_value.benchmarks.benchmarks_api_client.get_benchmark_leaderboard.side_effect = Exception("not found")
        result = mcp.tools["benchmark_leaderboard"]("bad", "slug")
        assert "Error" in result
