"""Tests for PR validation worktree no-edit verifier (#315)."""
import sys
import unittest
from pathlib import Path

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

from reviewer_validation_worktree_edits import (  # noqa: E402
    assess_validation_worktree_edit_report,
)


def _read_only_report() -> str:
    return "\n".join([
        "Validation worktree path: branches/review-pr280-feat-issue-275-claim",
        "Official PR-head validation on unmodified worktree.",
        "Official validation result: failed (1 failed)",
        "File edits by reviewer: none",
        "Review decision: request_changes",
    ])


def _diagnostic_report() -> str:
    return "\n".join([
        "Validation worktree path: branches/review-pr280-feat-issue-275-claim",
        "Official PR-head validation on unmodified worktree.",
        "Official validation result: failed (1 failed)",
        "Review decision: request_changes",
        "Diagnostic local experiment — not PR-head validation",
        "Diagnostic scratch worktree path: branches/diagnostic-pr280-fix-test",
        "File edits by reviewer: tests/test_agent_temp_artifacts.py (diagnostic only)",
        "Diagnostic result: passed after local fix",
    ])


class TestValidationWorktreeEdits(unittest.TestCase):
    def test_read_only_review_passes(self):
        result = assess_validation_worktree_edit_report(
            _read_only_report(),
            validation_session={
                "validation_worktree_path": (
                    "branches/review-pr280-feat-issue-275-claim"
                ),
            },
        )
        self.assertTrue(result["proven"], result["reasons"])

    def test_validation_worktree_edit_blocks(self):
        result = assess_validation_worktree_edit_report(
            _read_only_report(),
            validation_session={
                "validation_worktree_path": (
                    "branches/review-pr280-feat-issue-275-claim"
                ),
                "validation_worktree_edits": ["tests/test_agent_temp_artifacts.py"],
            },
        )
        self.assertFalse(result["proven"])
        self.assertTrue(result["block"])
        self.assertTrue(any("validation worktree" in r.lower() for r in result["reasons"]))

    def test_file_edits_none_contradiction_blocks(self):
        result = assess_validation_worktree_edit_report(
            _read_only_report(),
            validation_session={
                "validation_worktree_path": (
                    "branches/review-pr280-feat-issue-275-claim"
                ),
            },
            action_log=[
                {
                    "path": "tests/test_agent_temp_artifacts.py",
                    "kind": "file_edit",
                    "performed": True,
                    "worktree_path": "branches/review-pr280-feat-issue-275-claim",
                }
            ],
        )
        self.assertFalse(result["proven"])
        self.assertTrue(any("file edits" in r.lower() for r in result["reasons"]))

    def test_diagnostic_scratch_with_reporting_passes(self):
        result = assess_validation_worktree_edit_report(
            _diagnostic_report(),
            validation_session={
                "validation_worktree_path": (
                    "branches/review-pr280-feat-issue-275-claim"
                ),
                "diagnostic_worktree_path": "branches/diagnostic-pr280-fix-test",
                "diagnostic_edits": ["tests/test_agent_temp_artifacts.py"],
                "diagnostic_experiment": True,
            },
        )
        self.assertTrue(result["proven"], result["reasons"])

    def test_diagnostic_without_scratch_worktree_blocks(self):
        result = assess_validation_worktree_edit_report(
            "File edits by reviewer: tests/test_example.py",
            validation_session={
                "diagnostic_edits": ["tests/test_example.py"],
                "diagnostic_experiment": True,
            },
        )
        self.assertFalse(result["proven"])
        self.assertTrue(any("diagnostic" in r.lower() for r in result["reasons"]))

    def test_post_edit_official_validation_blocks(self):
        report = "\n".join([
            "Validation worktree path: branches/review-pr280-feat-issue-275-claim",
            "Official validation result: passed after local edit",
            "File edits by reviewer: tests/test_agent_temp_artifacts.py",
        ])
        result = assess_validation_worktree_edit_report(
            report,
            validation_session={
                "validation_worktree_path": (
                    "branches/review-pr280-feat-issue-275-claim"
                ),
                "validation_worktree_edits": ["tests/test_agent_temp_artifacts.py"],
                "official_validation_after_edit": True,
            },
        )
        self.assertFalse(result["proven"])


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

        self.assertTrue(callable(exported))


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