"""Tests for workspace-vs-worktree mutation consistency verifier (#313).

Final reports must not claim ``Workspace mutations: none`` when worktree
mutations (reset --hard, checkout, clean, worktree add/remove, ...)
occurred, and must report observed worktree / git ref mutations under the
precise category fields.
"""
import sys
import unittest
from pathlib import Path

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

from review_proofs import assess_workspace_mutation_consistency  # noqa: E402


class TestWorkspaceNoneContradiction(unittest.TestCase):
    def test_reset_hard_with_workspace_none_fails(self):
        report = (
            "Controller Handoff\n"
            "- Workspace mutations: none (no local file edits)\n"
        )
        result = assess_workspace_mutation_consistency(
            report,
            observed_commands=["git reset --hard FETCH_HEAD"],
        )
        self.assertFalse(result["complete"])
        self.assertTrue(result["downgraded"])
        self.assertTrue(
            any("workspace mutations" in r.lower() for r in result["reasons"])
        )

    def test_checkout_with_workspace_none_fails(self):
        report = "- Workspace mutations: none\n"
        result = assess_workspace_mutation_consistency(
            report,
            observed_commands=["git checkout feat/some-branch"],
        )
        self.assertFalse(result["complete"])

    def test_worktree_add_with_workspace_none_fails(self):
        report = "- Workspace mutations: none\n"
        result = assess_workspace_mutation_consistency(
            report,
            observed_commands=["git worktree add /tmp/review-pr1 abc123"],
        )
        self.assertFalse(result["complete"])

    def test_clean_with_workspace_none_fails(self):
        report = "- Workspace mutations: none\n"
        result = assess_workspace_mutation_consistency(
            report,
            observed_commands=["git clean -fd"],
        )
        self.assertFalse(result["complete"])

    def test_self_reported_worktree_mutation_contradicts_workspace_none(self):
        # No observed command log; the report itself carries the
        # contradiction (#313 observed behavior).
        report = (
            "- Worktree mutations: git reset --hard FETCH_HEAD\n"
            "- Workspace mutations: none (no local file edits)\n"
        )
        result = assess_workspace_mutation_consistency(report)
        self.assertFalse(result["complete"])
        self.assertTrue(
            any("workspace mutations" in r.lower() for r in result["reasons"])
        )


class TestPreciseCategoriesPass(unittest.TestCase):
    def test_file_edits_none_with_worktree_mutation_listed_passes(self):
        report = (
            "- File edits by reviewer: none\n"
            "- Worktree mutations: git reset --hard FETCH_HEAD\n"
            "- Git ref mutations: git fetch prgs\n"
        )
        result = assess_workspace_mutation_consistency(
            report,
            observed_commands=[
                "git fetch prgs",
                "git reset --hard FETCH_HEAD",
            ],
        )
        self.assertTrue(result["complete"])
        self.assertFalse(result["downgraded"])
        self.assertEqual(result["reasons"], [])

    def test_noop_report_passes(self):
        report = (
            "- File edits by reviewer: none\n"
            "- Worktree mutations: none\n"
            "- Git ref mutations: none\n"
        )
        result = assess_workspace_mutation_consistency(
            report, observed_commands=[]
        )
        self.assertTrue(result["complete"])
        self.assertEqual(result["reasons"], [])

    def test_fetch_only_with_ref_mutation_reported_passes(self):
        # Fetch is a git ref mutation, not a worktree mutation; a
        # workspace-none claim is not contradicted by fetch alone.
        report = (
            "- Workspace mutations: none\n"
            "- Git ref mutations: git fetch prgs master\n"
        )
        result = assess_workspace_mutation_consistency(
            report,
            observed_commands=["git fetch prgs master"],
        )
        self.assertTrue(result["complete"])


class TestObservedMutationsMustBeReported(unittest.TestCase):
    def test_reset_without_worktree_mutation_field_fails(self):
        report = "- File edits by reviewer: none\n"
        result = assess_workspace_mutation_consistency(
            report,
            observed_commands=["git reset --hard FETCH_HEAD"],
        )
        self.assertFalse(result["complete"])
        self.assertTrue(
            any("worktree mutation" in r.lower() for r in result["reasons"])
        )

    def test_reset_with_worktree_mutations_none_fails(self):
        report = (
            "- File edits by reviewer: none\n"
            "- Worktree mutations: none\n"
        )
        result = assess_workspace_mutation_consistency(
            report,
            observed_commands=["git reset --hard FETCH_HEAD"],
        )
        self.assertFalse(result["complete"])

    def test_fetch_without_ref_mutation_field_fails(self):
        report = (
            "- File edits by reviewer: none\n"
            "- Worktree mutations: none\n"
        )
        result = assess_workspace_mutation_consistency(
            report,
            observed_commands=["git fetch prgs master"],
        )
        self.assertFalse(result["complete"])
        self.assertTrue(
            any("git ref mutation" in r.lower() for r in result["reasons"])
        )

    def test_fetch_with_ref_mutations_none_fails(self):
        report = (
            "- Worktree mutations: none\n"
            "- Git ref mutations: none\n"
        )
        result = assess_workspace_mutation_consistency(
            report,
            observed_commands=["git fetch prgs master"],
        )
        self.assertFalse(result["complete"])


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