"""Tests for already-landed final-report wording validator (#298).

An already-landed PR is reconciliation-only, never review/merge
eligible, and a head SHA may not be called reviewed unless validation
and diff review actually passed. Final reports and controller handoffs
must use the reconciliation wording, not the legacy eligible/reviewed
fields.
"""
import sys
import unittest
from pathlib import Path

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

from review_proofs import assess_already_landed_report_wording  # noqa: E402


GOOD_ALREADY_LANDED = (
    "Controller Handoff\n"
    "- Oldest open PR requiring action: PR #278\n"
    "- Eligibility class: ALREADY_LANDED_RECONCILE_REQUIRED\n"
    "- Candidate head SHA: 2a544b7\n"
    "- Reviewed head SHA: none\n"
    "- Review worktree used: false\n"
)


class TestAlreadyLandedWording(unittest.TestCase):
    def test_correct_reconciliation_wording_passes(self):
        result = assess_already_landed_report_wording(
            GOOD_ALREADY_LANDED, already_landed=True
        )
        self.assertTrue(result["complete"])
        self.assertFalse(result["downgraded"])
        self.assertEqual(result["reasons"], [])

    def test_oldest_eligible_wording_fails(self):
        report = GOOD_ALREADY_LANDED + "- oldest eligible PR: PR #278\n"
        result = assess_already_landed_report_wording(
            report, already_landed=True
        )
        self.assertFalse(result["complete"])
        self.assertTrue(
            any("oldest eligible pr" in r.lower() for r in result["reasons"])
        )

    def test_next_eligible_wording_fails(self):
        report = GOOD_ALREADY_LANDED + "- next eligible PR: PR #278\n"
        result = assess_already_landed_report_wording(
            report, already_landed=True
        )
        self.assertFalse(result["complete"])

    def test_pinned_reviewed_head_fails(self):
        report = GOOD_ALREADY_LANDED + "- Pinned reviewed head: 2a544b7\n"
        result = assess_already_landed_report_wording(
            report, already_landed=True
        )
        self.assertFalse(result["complete"])
        self.assertTrue(
            any("pinned reviewed head" in r.lower() for r in result["reasons"])
        )

    def test_scratch_worktree_field_fails(self):
        report = GOOD_ALREADY_LANDED + "- Scratch worktree used: False\n"
        result = assess_already_landed_report_wording(
            report, already_landed=True
        )
        self.assertFalse(result["complete"])

    def test_missing_eligibility_class_fails(self):
        report = (
            "Controller Handoff\n"
            "- Oldest open PR requiring action: PR #278\n"
            "- Candidate head SHA: 2a544b7\n"
            "- Reviewed head SHA: none\n"
            "- Review worktree used: false\n"
        )
        result = assess_already_landed_report_wording(
            report, already_landed=True
        )
        self.assertFalse(result["complete"])
        self.assertTrue(
            any("eligibility class" in r.lower() for r in result["reasons"])
        )

    def test_populated_reviewed_head_sha_fails(self):
        report = GOOD_ALREADY_LANDED.replace(
            "- Reviewed head SHA: none\n",
            "- Reviewed head SHA: 2a544b7\n",
        )
        result = assess_already_landed_report_wording(
            report, already_landed=True
        )
        self.assertFalse(result["complete"])
        self.assertTrue(
            any("reviewed head" in r.lower() for r in result["reasons"])
        )


class TestNonAlreadyLandedReports(unittest.TestCase):
    def test_normal_reviewed_report_passes(self):
        report = (
            "- Selected PR: 281\n"
            "- Pinned reviewed head: abc1234\n"
            "- Review decision: approve\n"
        )
        result = assess_already_landed_report_wording(
            report, already_landed=False, review_validated=True
        )
        self.assertTrue(result["complete"])

    def test_blocked_infra_report_with_pinned_head_fails(self):
        report = (
            "- Selected PR: 281\n"
            "- Pinned reviewed head: abc1234\n"
            "- Current status: blocked on infra_stop\n"
        )
        result = assess_already_landed_report_wording(
            report, already_landed=False, review_validated=False
        )
        self.assertFalse(result["complete"])
        self.assertTrue(
            any("before validation" in r.lower() for r in result["reasons"])
        )

    def test_validation_failed_report_with_reviewed_sha_fails(self):
        report = (
            "- Selected PR: 281\n"
            "- Reviewed head SHA: abc1234\n"
            "- Validation: full suite failed\n"
        )
        result = assess_already_landed_report_wording(
            report, already_landed=False, review_validated=False
        )
        self.assertFalse(result["complete"])

    def test_validation_failed_report_with_reviewed_none_passes(self):
        report = (
            "- Selected PR: 281\n"
            "- Reviewed head SHA: none\n"
            "- Validation: full suite failed\n"
        )
        result = assess_already_landed_report_wording(
            report, already_landed=False, review_validated=False
        )
        self.assertTrue(result["complete"])


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