"""Tests for judge run formatting.

Judge run execution has moved to the backend (services/judge_orchestrator.py).
These tests cover the SDK-side formatting.
"""

from __future__ import annotations

from bandito.judge.run import format_run_summary


def _result(total=0, passed=0, failed=0, parse_errors=0, fail_traces=None):
    """Build a mock backend response dict."""
    trace_results = []
    for critique in (fail_traces or []):
        trace_results.append({"trace_id": "t", "judge_score": 0, "judge_critique": critique})
    return {
        "total_scored": total,
        "passed": passed,
        "failed": failed,
        "parse_errors": parse_errors,
        "trace_results": trace_results,
    }


class TestFormatRunSummary:
    def test_basic_output(self):
        summary = format_run_summary(_result(total=10, passed=8, failed=2))
        assert "10" in summary
        assert "80%" in summary

    def test_shows_failure_critiques(self):
        summary = format_run_summary(
            _result(total=5, passed=3, failed=2, fail_traces=["Bad answer.", "Hallucinated."])
        )
        assert "Bad answer." in summary
        assert "Hallucinated." in summary

    def test_truncates_long_critiques(self):
        summary = format_run_summary(
            _result(total=1, passed=0, failed=1, fail_traces=["x" * 200])
        )
        assert "..." in summary

    def test_limits_to_five_critiques(self):
        summary = format_run_summary(
            _result(total=8, passed=0, failed=8, fail_traces=[f"critique {i}" for i in range(8)])
        )
        assert "and 3 more" in summary

    def test_shows_parse_errors(self):
        summary = format_run_summary(_result(total=3, passed=3, failed=0, parse_errors=2))
        assert "Parse errors" in summary
        assert "2" in summary

    def test_all_pass(self):
        summary = format_run_summary(_result(total=10, passed=10, failed=0))
        assert "100%" in summary
        assert "Failure" not in summary
