"""Tests for reconciliation linked-issue live proof verifier (#300)."""
import sys
import unittest
from pathlib import Path

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

from reviewer_reconcile_linked_issue import assess_reconcile_linked_issue_report  # noqa: E402


def _good_handoff() -> str:
    return "\n".join([
        "## PR reconciliation summary",
        "Fetched linked issue #263 live with gitea_view_issue in this session.",
        "",
        "## Controller Handoff",
        "- Selected PR: #278",
        "- Eligibility class: ALREADY_LANDED_RECONCILE_REQUIRED",
        "- Linked issue: #263",
        "- Linked issue live status: Issue #263 (open)",
        "- Safe next action: reconcile open PR",
    ])


class TestReconcileLinkedIssueLiveProof(unittest.TestCase):
    def test_live_fetch_with_open_status_passes(self):
        result = assess_reconcile_linked_issue_report(
            _good_handoff(),
            reconcile_session={"live_fetched": True, "linked_issue_number": 263},
        )
        self.assertTrue(result["proven"], result["reasons"])
        self.assertEqual(result["linked_issue_number"], 263)

    def test_no_linked_issue_passes(self):
        report = "\n".join([
            "## Controller Handoff",
            "- Selected PR: #99",
            "- Linked issue: none",
            "- Linked issue live status: not applicable",
        ])
        result = assess_reconcile_linked_issue_report(report)
        self.assertTrue(result["proven"])
        self.assertIsNone(result["linked_issue_number"])

    def test_closed_status_without_live_proof_blocks(self):
        report = "\n".join([
            "## Controller Handoff",
            "- Linked issue: #263",
            "- Linked issue live status: Issue #263 (closed)",
        ])
        result = assess_reconcile_linked_issue_report(report)
        self.assertFalse(result["proven"])
        self.assertTrue(result["block"])
        self.assertTrue(any("live" in r.lower() for r in result["reasons"]))

    def test_not_verified_wording_passes_without_live_proof(self):
        report = "\n".join([
            "## Controller Handoff",
            "- Linked issue: #263",
            "- Linked issue live status: not verified in this session",
        ])
        result = assess_reconcile_linked_issue_report(report)
        self.assertTrue(result["proven"], result["reasons"])

    def test_missing_issue_requires_not_verified(self):
        report = "\n".join([
            "## Controller Handoff",
            "- Linked issue: #999",
            "- Linked issue live status: Issue #999 (open)",
        ])
        result = assess_reconcile_linked_issue_report(
            report,
            reconcile_session={"issue_missing": True, "linked_issue_number": 999},
        )
        self.assertFalse(result["proven"])
        self.assertTrue(any("not verified" in r.lower() for r in result["reasons"]))

    def test_text_live_proof_without_session_lock_passes(self):
        report = _good_handoff()
        result = assess_reconcile_linked_issue_report(report)
        self.assertTrue(result["proven"], result["reasons"])
        self.assertTrue(result["live_proof_present"])

    def test_issue_action_recommendation_requires_capability_proof(self):
        report = "\n".join([
            _good_handoff(),
            "Recommended: close linked issue #263 after PR reconciliation.",
        ])
        result = assess_reconcile_linked_issue_report(
            report,
            reconcile_session={"live_fetched": True},
        )
        self.assertFalse(result["proven"])
        self.assertTrue(any("capability" in r.lower() for r in result["reasons"]))

    def test_issue_action_with_capability_proof_passes(self):
        report = "\n".join([
            _good_handoff(),
            "Recommended: close linked issue #263.",
            "Capability proof: gitea_close_issue allowed for prgs-author.",
        ])
        result = assess_reconcile_linked_issue_report(
            report,
            reconcile_session={"live_fetched": True},
        )
        self.assertTrue(result["proven"], result["reasons"])


class TestExport(unittest.TestCase):
    def test_review_proofs_reexport(self):
        from review_proofs import assess_reconcile_linked_issue_report as exported

        self.assertTrue(callable(exported))


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