from __future__ import annotations


import sys as _sys
from pathlib import Path as _Path
_sys.path.insert(0, str(_Path(__file__).resolve().parent))
from mutation_profile_fixture import shared_mutation_env  # noqa: E402

import os
import tempfile
import unittest
from pathlib import Path
from unittest.mock import patch

import workflow_skill
import mcp_server


class TestWorkflowSkillModule(unittest.TestCase):
    def test_canonical_names_include_gitea_workflow(self):
        names = workflow_skill.CANONICAL_WORKFLOW_SKILL_NAMES
        self.assertIn("gitea-workflow", names)
        self.assertIn("llm-project-workflow", names)
        self.assertIn("git-pr-workflows", names)

    def test_repo_skill_present_in_this_checkout(self):
        root = str(Path(__file__).resolve().parent.parent)
        result = workflow_skill.assess_workflow_skill_presence(root)
        self.assertTrue(result["repo_skill_present"])
        self.assertTrue(result["repo_alias_present"])
        self.assertTrue(result["workflow_skill_ready"])
        self.assertFalse(result["blocked"])

    def test_missing_repo_skill_blocks(self):
        with tempfile.TemporaryDirectory() as tmp:
            result = workflow_skill.assess_workflow_skill_presence(tmp)
        self.assertTrue(result["blocked"])
        self.assertFalse(result["workflow_skill_ready"])
        self.assertTrue(any("missing" in r for r in result["reasons"]))

    def test_codex_mount_detection(self):
        root = str(Path(__file__).resolve().parent.parent)
        with tempfile.TemporaryDirectory() as tmp:
            skill_dir = Path(tmp) / "gitea-workflow"
            skill_dir.mkdir()
            (skill_dir / "SKILL.md").write_text("# x\n", encoding="utf-8")
            result = workflow_skill.assess_workflow_skill_presence(
                root, codex_skills_dir=tmp
            )
        self.assertTrue(result["codex_skill_mounted"])
        self.assertTrue(result["codex_skill_mounts"]["gitea-workflow"])

    def test_registry_entries_cover_aliases(self):
        entries = workflow_skill.workflow_skill_registry_entries()
        for name in workflow_skill.CANONICAL_WORKFLOW_SKILL_NAMES:
            self.assertIn(name, entries)
            self.assertEqual(entries[name]["status"], "available")


class TestWorkflowSkillMcpTools(unittest.TestCase):
    def test_list_includes_canonical_names(self):
        with patch.dict(
            os.environ,
            {
                "GITEA_PROFILE_NAME": "author-test",
                "GITEA_ALLOWED_OPERATIONS": "gitea.read",
                "GITEA_FORBIDDEN_OPERATIONS": "",
            },
            clear=False,
        ):
            result = mcp_server.mcp_list_project_skills()
        names = {s["name"] for s in result["skills"]}
        for name in workflow_skill.CANONICAL_WORKFLOW_SKILL_NAMES:
            self.assertIn(name, names)

    def test_get_skill_guide_gitea_workflow(self):
        guide = mcp_server.mcp_get_skill_guide("gitea-workflow")
        self.assertTrue(guide["success"])
        self.assertTrue(guide["steps"])
        self.assertIn("workflow", " ".join(guide["steps"]).lower())

    def test_preflight_tool_ok_on_repo(self):
        result = mcp_server.mcp_check_workflow_skill_preflight()
        self.assertTrue(result["success"])
        self.assertTrue(result["workflow_skill_ready"])
        self.assertFalse(result["blocked"])
        self.assertIn("gitea-workflow", result["canonical_names"])


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