"""Tests for calibration report formatting.

Calibration execution and metrics computation have moved to the backend
(services/judge_orchestrator.py). These tests cover the SDK-side formatting.
"""

from __future__ import annotations

from bandito.judge.calibrate import format_calibration_report


class TestFormatCalibrationReport:
    def test_well_calibrated(self):
        report = format_calibration_report({
            "total": 20, "tp": 10, "tn": 9, "fp": 1, "fn": 0,
            "tpr": 1.0, "tnr": 0.9, "agreement": 0.95, "kappa": 0.9,
            "trace_results": [],
        })
        assert "well-calibrated" in report
        assert "100%" in report
        assert "90%" in report

    def test_too_lenient(self):
        report = format_calibration_report({
            "total": 20, "tp": 10, "tn": 5, "fp": 5, "fn": 0,
            "tpr": 1.0, "tnr": 0.5, "agreement": 0.75, "kappa": 0.5,
            "trace_results": [],
        })
        assert "too lenient" in report

    def test_too_strict(self):
        report = format_calibration_report({
            "total": 20, "tp": 5, "tn": 10, "fp": 0, "fn": 5,
            "tpr": 0.5, "tnr": 1.0, "agreement": 0.75, "kappa": 0.5,
            "trace_results": [],
        })
        assert "too strict" in report

    def test_shows_disagreements(self):
        traces_with_grades = [
            ({"trace_id": "t-1", "input": "q1", "output": "a1"}, 0),
            ({"trace_id": "t-2", "input": "q2", "output": "a2"}, 0),
        ]
        report = format_calibration_report(
            {
                "total": 10, "tp": 5, "tn": 3, "fp": 2, "fn": 0,
                "tpr": 1.0, "tnr": 0.6, "agreement": 0.8, "kappa": 0.6,
                "trace_results": [
                    {"trace_id": "t-1", "human_score": 0, "judge_score": 1,
                     "judge_critique": "Looked fine to me.", "agreed": False, "kind": "fp"},
                    {"trace_id": "t-2", "human_score": 0, "judge_score": 1,
                     "judge_critique": "Seemed correct.", "agreed": False, "kind": "fp"},
                ],
            },
            traces_with_grades=traces_with_grades,
        )
        assert "t-1" in report
        assert "Looked fine to me." in report
        assert "false positive" in report.lower()

    def test_shows_confusion_matrix(self):
        report = format_calibration_report({
            "total": 20, "tp": 8, "tn": 7, "fp": 3, "fn": 2,
            "tpr": 0.8, "tnr": 0.7, "agreement": 0.75, "kappa": 0.5,
            "trace_results": [],
        })
        assert "Judge=1" in report
        assert "Human=1" in report
