from __future__ import annotations

import hashlib

from evals.run_eval import (
    render_prompt,
    render_stage_a_request_prompt,
    render_stage_b_patch_prompt,
)


def _base_evidence(**overrides: object) -> dict:
    base = {
        "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,
    }
    base.update(overrides)
    return base


def _sha256(value: str) -> str:
    return hashlib.sha256(value.encode("utf-8")).hexdigest()


def test_render_prompt_checksum_freeze() -> None:
    prompt = render_prompt(evidence=_base_evidence(), snapshot=None)
    assert _sha256(prompt) == "b2e477f10ca51a4d14af295a1bd96f313dfaac22ec5a4405deb86cdbbfca0c38"


def test_render_stage_a_prompt_checksum_freeze() -> None:
    prompt = render_stage_a_request_prompt(evidence=_base_evidence(), snapshot=None)
    assert _sha256(prompt) == "cca95644ea77f4b2f64f97a5d3f2eaf4474722da06c1314c372ac78823769481"


def test_render_stage_b_prompt_checksum_freeze() -> None:
    prompt = render_stage_b_patch_prompt(
        evidence=_base_evidence(),
        snapshot=None,
        stage_a_context="--- helper.py ---\ndef value():\n    return 2\n",
        include_project_files=False,
    )
    assert _sha256(prompt) == "644fea45258a97fb724067d29221feb014f303c4481d5e1b46e40d946d128b41"
