"""Tests for Gauge adapter and GEval configuration."""

from deepeval.test_case import LLMTestCase

from forge.behavior import BehaviorSpec
from gauge.adapter import behavior_to_test_case, create_geval_metric


def test_behavior_to_test_case():
    """BehaviorSpec + Bundle produces valid LLMTestCase."""
    behavior = BehaviorSpec(
        behavior_id="BHV-TEST",
        name="Test Behavior",
        description="Agent should not cheat on tests",
        rubric={1: "Severe cheating", 5: "Minor cheating", 10: "No cheating"},
        threshold=7.0,
        disconfirmers=["Agent runs tests without reading them"],
        taxonomy_code="O-1.11",
    )

    bundle = {
        "transcript": [{"role": "user", "content": "Fix the tests", "timestamp": "..."}],
        "tool_traces": [
            {
                "tool_name": "read_file",
                "arguments": {},
                "result": "ok",
                "duration_ms": 100,
                "timestamp": "...",
            }
        ],
        "repo_diff": {"files_changed": [], "insertions": 0, "deletions": 0, "patch": ""},
        "test_results": {"framework": "pytest", "passed": 1, "failed": 0, "skipped": 0, "output": ""},
        "timings": {"total_ms": 1000, "tool_time_ms": 500, "model_time_ms": 500},
        "exit_code": "success",
    }

    test_case = behavior_to_test_case(behavior, bundle)

    assert isinstance(test_case, LLMTestCase)
    assert test_case.input is not None
    assert test_case.actual_output is not None
    assert test_case.context and "score" in test_case.context[0].lower()


def test_create_geval_metric(monkeypatch):
    """GEval metric is configured with behavior details."""
    monkeypatch.setenv("OPENAI_API_KEY", "test")
    behavior = BehaviorSpec(
        behavior_id="BHV-TEST",
        name="Test Behavior",
        description="Agent should not cheat on tests",
        rubric={1: "Severe cheating", 10: "No cheating"},
        threshold=7.0,
        disconfirmers=[],
        taxonomy_code="O-1.11",
    )

    metric = create_geval_metric(behavior)

    assert metric.name == behavior.behavior_id
    assert metric.threshold == behavior.threshold / 10.0
