"""Tests for validation cwd/HEAD proof verifier (#398)."""
import sys
import unittest
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent.parent))

from final_report_validator import assess_final_report_validator  # noqa: E402
from reviewer_validation_cwd_proof import assess_validation_cwd_proof_report  # noqa: E402

ROOT = "/Users/jasonwalker/Development/Gitea-Tools"
WORKTREE = f"{ROOT}/branches/review-feat-issue-398"
HEAD = "f5953549aad5e822f14f52d3ea3c6d7990109384"


def _proof_backed_report() -> str:
    return "\n".join([
        f"Candidate head SHA: {HEAD}",
        f"pwd: {WORKTREE}",
        f"git rev-parse HEAD: {HEAD}",
        "git status --short --branch: ## feat/issue-398...prgs/master",
        f"Validation command: cd {WORKTREE} && venv/bin/python -m pytest tests/ -q",
        "Result: 1497 passed, 6 skipped",
    ])


class TestValidationCwdProof(unittest.TestCase):
    def test_no_validation_claim_passes(self):
        result = assess_validation_cwd_proof_report("Review decision: approve")
        self.assertTrue(result["proven"])

    def test_missing_cwd_proof_fails(self):
        result = assess_validation_cwd_proof_report(
            f"Validation command: pytest tests/\nCandidate head SHA: {HEAD}",
            validation_session={"validation_ran": True, "expected_head_sha": HEAD},
        )
        self.assertFalse(result["proven"])
        self.assertTrue(result["block"])

    def test_main_checkout_cwd_blocks(self):
        result = assess_validation_cwd_proof_report(
            "\n".join([
                f"Candidate head SHA: {HEAD}",
                f"pwd: {ROOT}",
                f"git rev-parse HEAD: {HEAD}",
                "git status --short --branch: ## master",
                "Validation command: pytest tests/ -q",
            ]),
            validation_session={"validation_ran": True, "expected_head_sha": HEAD},
            project_root=ROOT,
        )
        self.assertFalse(result["proven"])
        self.assertTrue(result["violations"])

    def test_wrong_head_blocks(self):
        wrong = "a" * 40
        result = assess_validation_cwd_proof_report(
            "\n".join([
                f"Candidate head SHA: {HEAD}",
                f"pwd: {WORKTREE}",
                f"git rev-parse HEAD: {wrong}",
                "git status --short --branch: clean",
                f"Validation command: cd {WORKTREE} && pytest -q",
            ]),
            validation_session={"validation_ran": True, "expected_head_sha": HEAD},
            project_root=ROOT,
        )
        self.assertFalse(result["proven"])
        self.assertTrue(result["violations"])

    def test_fully_proof_backed_passes(self):
        result = assess_validation_cwd_proof_report(
            _proof_backed_report(),
            validation_session={"validation_ran": True, "expected_head_sha": HEAD},
            project_root=ROOT,
        )
        self.assertTrue(result["proven"], result["reasons"])

    def test_baseline_without_cwd_fails(self):
        result = assess_validation_cwd_proof_report(
            "\n".join([
                _proof_backed_report(),
                "Baseline validation command: pytest tests/ -q",
            ]),
            validation_session={
                "validation_ran": True,
                "expected_head_sha": HEAD,
                "baseline_validation_ran": True,
            },
            project_root=ROOT,
        )
        self.assertFalse(result["proven"])
        self.assertTrue(
            any("baseline" in r.lower() for r in result["reasons"])
        )

    def test_baseline_with_full_proof_passes(self):
        result = assess_validation_cwd_proof_report(
            "\n".join([
                _proof_backed_report(),
                f"Baseline worktree: {ROOT}/branches/baseline-master-pr376",
                f"Baseline target SHA: {HEAD}",
                f"Baseline validation command: cd {ROOT}/branches/baseline-master-pr376 && pytest -q",
            ]),
            validation_session={
                "validation_ran": True,
                "expected_head_sha": HEAD,
                "baseline_validation_ran": True,
            },
            project_root=ROOT,
        )
        self.assertTrue(result["proven"], result["reasons"])

    def test_final_report_validator_integration(self):
        result = assess_final_report_validator(
            "Validation command: pytest tests/ -q",
            "review_pr",
            validation_session={"validation_ran": True},
        )
        self.assertTrue(result["blocked"] or result["downgraded"])
        self.assertTrue(
            any(
                f.get("rule_id") == "reviewer.validation_cwd_proof"
                for f in result.get("findings") or []
            )
        )

    def test_exported_from_review_proofs(self):
        from review_proofs import assess_validation_cwd_proof_report as exported

        self.assertTrue(callable(exported))


if __name__ == "__main__":
    unittest.main()