"""Tests for proof-backed reviewer handoff verifier (#395)."""
import sys
import unittest
from pathlib import Path

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

from reviewer_proof_backed_handoff import (  # noqa: E402
    assess_proof_backed_handoff_report,
)


def _good_report() -> str:
    return "\n".join([
        "Inventory pagination proof: is_final_page: true, has_more: false, total_count: 11",
        "Earlier PRs skipped: #372 non-mergeable — merge simulation conflicts in review_proofs.py",
        "Baseline worktree path: branches/baseline-master-pr383",
        "Baseline target SHA: 4243b60ca32d79f1ee5e6b0f10d7570e9dea2937",
        "Baseline dirty before validation: false",
        "Baseline validation command: venv/bin/python -m pytest tests/ -q",
        "Baseline validation result: 1 failed (test_clean_reviewer_report_passes)",
        "Baseline dirty after validation: false",
        "Cleanup mutations: git worktree list showed session worktrees removed",
        "## Controller Handoff",
        "- Inventory pagination proof: inventory_complete: true, total_count: 11",
        "- Earlier PRs skipped: #372 conflict proof via merge simulation",
        "- Baseline worktree used: true",
        "- Baseline worktree path: branches/baseline-master-pr383",
        "- Cleanup mutations: git worktree remove branches/review-pr383",
    ])


class TestProofBackedHandoff(unittest.TestCase):
    def test_fully_proof_backed_report_passes(self):
        result = assess_proof_backed_handoff_report(
            _good_report(),
            action_log=[{"command": "gitea_list_prs", "result": "inventory_complete=true"}],
            baseline_session={
                "complete": True,
                "clean_before": True,
                "clean_after": True,
                "command": "pytest",
                "result": "passed",
            },
        )
        self.assertTrue(result["proven"], result["reasons"])

    def test_inventory_page_size_assumption_blocks(self):
        report = (
            "Inventory is complete because 13 results are less than page size 50."
        )
        result = assess_proof_backed_handoff_report(report)
        self.assertFalse(result["proven"])
        self.assertTrue(any("page-size" in r.lower() for r in result["reasons"]))

    def test_skip_without_conflict_proof_blocks(self):
        report = "\n".join([
            "Earlier PRs skipped: #372, #374",
            "## Controller Handoff",
            "- Earlier PRs skipped: #372, #374",
        ])
        result = assess_proof_backed_handoff_report(report)
        self.assertFalse(result["proven"])
        self.assertTrue(any("skip" in r.lower() for r in result["reasons"]))

    def test_baseline_without_command_blocks(self):
        report = "Baseline validation passed; same as master failures."
        result = assess_proof_backed_handoff_report(report)
        self.assertFalse(result["proven"])
        self.assertTrue(any("baseline" in r.lower() for r in result["reasons"]))

    def test_cleanup_without_worktree_list_blocks(self):
        report = "Worktrees were cleaned after merge."
        result = assess_proof_backed_handoff_report(report)
        self.assertFalse(result["proven"])
        self.assertTrue(any("cleanup" in r.lower() for r in result["reasons"]))

    def test_cleanup_with_worktree_list_passes(self):
        report = "Cleanup: git worktree list confirmed removal."
        result = assess_proof_backed_handoff_report(
            report,
            action_log=[{"command": "git worktree list"}],
        )
        self.assertTrue(result["proven"], result["reasons"])

    def test_master_integration_requires_command(self):
        report = "Merged master into the review worktree before validation."
        result = assess_proof_backed_handoff_report(report)
        self.assertFalse(result["proven"])
        self.assertTrue(any("master integration" in r.lower() for r in result["reasons"]))

    def test_master_integration_with_action_log_passes(self):
        report = "Merged master into review worktree; merge_exit=0"
        result = assess_proof_backed_handoff_report(
            report,
            action_log=[{"command": "git merge --no-commit prgs/master"}],
        )
        self.assertTrue(result["proven"], result["reasons"])

    def test_live_proof_requires_source_classification(self):
        report = "Live proof shows inventory complete."
        result = assess_proof_backed_handoff_report(report)
        self.assertFalse(result["proven"])
        self.assertTrue(any("proof source" in r.lower() for r in result["reasons"]))


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

        self.assertTrue(callable(exported))
        result = exported(_good_report(), inventory_session={"inventory_complete": True})
        self.assertTrue(result["proven"], result["reasons"])


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