"""
Tests for improved error messages in skill_manager_tool (issue #624).
Verifies that error messages include file paths, context, and suggestions.
"""

import pytest
from pathlib import Path
from unittest.mock import patch, MagicMock
from tools.skill_manager_tool import _format_error, _edit_skill, _patch_skill


class TestFormatError:
    """Test the _format_error helper function."""
    
    def test_basic_error(self):
        """Test basic error formatting."""
        result = _format_error("Something went wrong")
        assert result["success"] is False
        assert "Something went wrong" in result["error"]
        assert result["skill_name"] is None
        assert result["file_path"] is None
    
    def test_with_skill_name(self):
        """Test error with skill name."""
        result = _format_error("Failed", skill_name="test-skill")
        assert "test-skill" in result["error"]
        assert result["skill_name"] == "test-skill"
    
    def test_with_file_path(self):
        """Test error with file path."""
        result = _format_error("Failed", file_path="/path/to/SKILL.md")
        assert "/path/to/SKILL.md" in result["error"]
        assert result["file_path"] == "/path/to/SKILL.md"
    
    def test_with_suggestion(self):
        """Test error with suggestion."""
        result = _format_error("Failed", suggestion="Try again")
        assert "Suggestion: Try again" in result["error"]
        assert result["suggestion"] == "Try again"
    
    def test_with_context(self):
        """Test error with context dict."""
        result = _format_error("Failed", context={"line": 5, "found": "x"})
        assert "line: 5" in result["error"]
        assert "found: x" in result["error"]
    
    def test_all_fields(self):
        """Test error with all fields."""
        result = _format_error(
            "Pattern match failed",
            skill_name="my-skill",
            file_path="/skills/my-skill/SKILL.md",
            suggestion="Check whitespace",
            context={"expected": "foo", "found": "bar"}
        )
        assert "Pattern match failed" in result["error"]
        assert "Skill: my-skill" in result["error"]
        assert "File: /skills/my-skill/SKILL.md" in result["error"]
        assert "Suggestion: Check whitespace" in result["error"]
        assert "expected: foo" in result["error"]


class TestEditSkillErrors:
    """Test improved error messages in _edit_skill."""
    
    @patch('tools.skill_manager_tool._find_skill')
    def test_skill_not_found(self, mock_find):
        """Test skill not found error includes suggestion."""
        mock_find.return_value = None
        # Provide valid content with frontmatter so it passes validation
        valid_content = """---
name: test
description: Test skill
---
Body content here.
"""
        result = _edit_skill("nonexistent", valid_content)
        assert result["success"] is False
        assert "nonexistent" in result["error"]
        assert "skills_list()" in result.get("suggestion", "")


class TestPatchSkillErrors:
    """Test improved error messages in _patch_skill."""
    
    def test_old_string_required(self):
        """Test old_string required error includes suggestion."""
        result = _patch_skill("test-skill", None, "new")
        assert result["success"] is False
        assert "old_string is required" in result["error"]
        assert "suggestion" in result
    
    def test_new_string_required(self):
        """Test new_string required error includes suggestion."""
        result = _patch_skill("test-skill", "old", None)
        assert result["success"] is False
        assert "new_string is required" in result["error"]
        assert "suggestion" in result
    
    @patch('tools.skill_manager_tool._find_skill')
    def test_skill_not_found(self, mock_find):
        """Test skill not found error includes suggestion."""
        mock_find.return_value = None
        result = _patch_skill("nonexistent", "old", "new")
        assert result["success"] is False
        assert "nonexistent" in result["error"]
        assert "skills_list()" in result.get("suggestion", "")


if __name__ == "__main__":
    pytest.main([__file__, "-v"])
