from __future__ import annotations

from evals.run_eval import render_prompt


def _base_evidence() -> dict:
    return {
        "case_id": "case_1",
        "condition": "traceback_only",
        "attempt": 2,
        "repro": ["python", "-m", "pytest", "-q"],
        "stdout": "E   AssertionError: expected 2 got 1\n",
        "stderr": "",
        "failure_signature": "AssertionError:abc123",
        "previous_failure_signature": "AssertionError:def456",
        "failure_signature_repeat": False,
        "previous_attempt_diff": {
            "attempt_prev": "attempt_01",
            "attempt_curr": "attempt_02",
            "failure_signature_same": False,
            "evidence_delta": {"new_frames": [], "dropped_frames": [], "message_changed": True},
            "change_delta": {"touched_files": ["buggy.py"], "touched_functions": ["f"]},
            "hypothesis_delta": "unknown",
            "verification_delta": {
                "old_status": "unknown",
                "new_status": "unknown",
                "status_changed": False,
            },
        },
        "file_index": "buggy.py\ntest_buggy.py",
        "files": "",
        "snapshot_available": False,
        "snapshot_error": None,
    }


def test_render_prompt_contains_rca_contract_sections() -> None:
    prompt = render_prompt(evidence=_base_evidence(), snapshot=None)
    assert "RCA checklist (internal reasoning only, do not print these sections)" in prompt
    assert "1) Observed failure with concrete evidence" in prompt
    assert "root-cause hypothesis" in prompt.lower()
    assert "Structured diff vs previous failed attempt" in prompt
    assert "Return ONLY a single unified diff" in prompt


def test_render_prompt_contains_loop_breaker_when_signature_repeats() -> None:
    evidence = _base_evidence()
    evidence["failure_signature_repeat"] = True
    prompt = render_prompt(evidence=evidence, snapshot=None)
    assert "Loop breaker" in prompt
    assert "signature repeated" in prompt


def test_render_prompt_embeds_snapshot_summary_when_present() -> None:
    snapshot = {
        "exception": {"type": "ValueError", "message": "bad input"},
        "frames": [{"file_rel": "buggy.py", "line": 3, "function": "f"}],
    }
    prompt = render_prompt(evidence=_base_evidence(), snapshot=snapshot)
    assert "llmdebug snapshot summary" in prompt
    assert "ValueError" in prompt
