"""Tests for REQUEST_CHANGES override proof before approval (#326)."""
import sys
import unittest
from pathlib import Path

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

from review_proofs import assess_request_changes_approval_proof  # noqa: E402

HEAD_A = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
HEAD_B = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"


def _feedback(**overrides):
    base = {
        "success": True,
        "current_head_sha": HEAD_A,
        "has_blocking_change_requests": False,
        "author_pushed_after_request_changes": False,
        "reviews": [],
    }
    base.update(overrides)
    return base


def _blocking_review(**overrides):
    entry = {
        "reviewer": "reviewer-a",
        "verdict": "REQUEST_CHANGES",
        "body": "Tests fail on pinned head; fix test_commit_files_gate.",
        "submitted_at": "2026-07-07T01:00:00-05:00",
        "reviewed_head_sha": HEAD_A,
        "dismissed": False,
        "stale": False,
    }
    entry.update(overrides)
    return entry


class TestRequestChangesApprovalProof(unittest.TestCase):
    def test_missing_feedback_blocks_approval(self):
        result = assess_request_changes_approval_proof(None)
        self.assertFalse(result["approve_allowed"])
        self.assertTrue(result["block"])

    def test_no_prior_request_changes_allows_approval(self):
        feedback = _feedback(
            reviews=[{
                "reviewer": "sysadmin",
                "verdict": "APPROVED",
                "body": "LGTM",
                "submitted_at": "2026-07-07T02:00:00-05:00",
                "reviewed_head_sha": HEAD_A,
                "dismissed": False,
            }]
        )
        result = assess_request_changes_approval_proof(feedback)
        self.assertTrue(result["approve_allowed"])
        self.assertIsNone(result["blocking_review"])

    def test_changed_head_since_blocker_allows_approval(self):
        feedback = _feedback(
            current_head_sha=HEAD_B,
            author_pushed_after_request_changes=True,
            has_blocking_change_requests=True,
            reviews=[_blocking_review(reviewed_head_sha=HEAD_A)],
        )
        result = assess_request_changes_approval_proof(feedback)
        self.assertTrue(result["approve_allowed"])
        self.assertTrue(result["head_changed_since_blocker"])

    def test_unchanged_head_without_override_blocks_approval(self):
        feedback = _feedback(
            has_blocking_change_requests=True,
            reviews=[_blocking_review()],
        )
        result = assess_request_changes_approval_proof(feedback)
        self.assertFalse(result["approve_allowed"])
        self.assertIn("override_reason", " ".join(result["reasons"]))

    def test_unchanged_head_with_valid_override_allows_approval(self):
        blocker = _blocking_review()
        feedback = _feedback(
            has_blocking_change_requests=True,
            reviews=[blocker],
        )
        report = (
            "Blocker text: Tests fail on pinned head; fix test_commit_files_gate.\n"
            "Override: wrong_validation_environment — CI used stale worktree."
        )
        result = assess_request_changes_approval_proof(
            feedback,
            override_reason="wrong_validation_environment",
            override_explanation="CI used stale worktree; local rerun passed.",
            report_text=report,
        )
        self.assertTrue(result["approve_allowed"])
        self.assertEqual(
            result["blocking_review"]["blocking_reviewer"], "reviewer-a"
        )


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