"""Tests for precise mutation category verifier (#319)."""
import sys
import unittest
from pathlib import Path

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

from reviewer_mutation_categories import assess_mutation_categories_report  # noqa: E402


def _precise_handoff() -> str:
    return "\n".join([
        "Controller Handoff",
        "File edits by reviewer: none",
        "Worktree/index mutations: git worktree add branches/review-pr1",
        "Git ref mutations: git fetch prgs master",
        "MCP/Gitea mutations: gitea_view_pr",
        "Review mutations: gitea_review_pr request_changes",
        "Read-only diagnostics: git status, git diff",
    ])


class TestMutationCategories(unittest.TestCase):
    def test_precise_categories_pass(self):
        result = assess_mutation_categories_report(_precise_handoff())
        self.assertTrue(result["proven"], result["reasons"])

    def test_legacy_workspace_mutations_blocks(self):
        report = "\n".join([
            "Controller Handoff",
            "Workspace mutations: none (no local file changes)",
            "File edits by reviewer: none",
            "Worktree/index mutations: none",
            "Git ref mutations: none",
        ])
        result = assess_mutation_categories_report(report)
        self.assertFalse(result["proven"])
        self.assertTrue(any("workspace mutations" in r.lower() for r in result["reasons"]))

    def test_workspace_none_with_worktree_command_blocks(self):
        report = "\n".join([
            "Controller Handoff",
            "Workspace mutations: none (no local file changes)",
            "File edits by reviewer: none",
            "Worktree/index mutations: none",
            "Git ref mutations: none",
        ])
        result = assess_mutation_categories_report(
            report,
            observed_commands=["git reset --hard FETCH_HEAD"],
        )
        self.assertFalse(result["proven"])

    def test_file_edits_none_with_worktree_mutation_passes(self):
        report = "\n".join([
            "Controller Handoff",
            "File edits by reviewer: none",
            "Worktree/index mutations: git reset --hard FETCH_HEAD",
            "Git ref mutations: git fetch prgs",
        ])
        result = assess_mutation_categories_report(
            report,
            observed_commands=["git reset --hard FETCH_HEAD", "git fetch prgs"],
        )
        self.assertTrue(result["proven"], result["reasons"])

    def test_missing_required_categories_blocks(self):
        report = "Controller Handoff\nFile edits by reviewer: none\n"
        result = assess_mutation_categories_report(report)
        self.assertFalse(result["proven"])
        self.assertTrue(any("missing" in r.lower() for r in result["reasons"]))

    def test_non_controller_handoff_skips(self):
        result = assess_mutation_categories_report(
            "Workspace mutations: none\n",
        )
        self.assertTrue(result["proven"])


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

        self.assertTrue(callable(exported))


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