"""Skill 系统测试。"""

import pytest
from butler.skills.base import SkillBase, SkillContext, SkillResult, SkillRegistry


class MockSkill(SkillBase):
    """测试用 Skill。"""

    name = "mock"
    description = "Mock skill for testing"
    triggers = ["/mock", "/test"]

    async def execute(self, ctx: SkillContext) -> SkillResult:
        return SkillResult(success=True, output=f"Executed with args: {ctx.args}")


class TestSkillBase:
    """Skill 基类测试。"""

    def test_skill_matches_trigger(self):
        """测试触发器匹配。"""
        skill = MockSkill()
        assert skill.matches("/mock arg1") is True
        assert skill.matches("/test") is True
        assert skill.matches("/other") is False

    def test_skill_context_creation(self):
        """测试上下文创建。"""
        ctx = SkillContext(
            session_name="test_session",
            user_id="user123",
            session_state={},
            args=["arg1", "arg2"],
        )
        assert ctx.session_name == "test_session"
        assert ctx.args == ["arg1", "arg2"]

    def test_skill_result_creation(self):
        """测试结果创建。"""
        result = SkillResult(success=True, output="Done", metadata={"key": "value"})
        assert result.success is True
        assert result.output == "Done"


class TestSkillRegistry:
    """Skill 注册表测试。"""

    def test_register_skill(self):
        """测试注册 Skill。"""
        registry = SkillRegistry()
        skill = MockSkill()

        registry.register(skill)

        assert "mock" in registry._skills

    def test_find_matching_skill(self):
        """测试查找匹配的 Skill。"""
        registry = SkillRegistry()
        registry.register(MockSkill())

        found = registry.find_matching("/mock arg1")
        assert found is not None
        assert found.name == "mock"

    def test_find_no_matching_skill(self):
        """测试无匹配时返回 None。"""
        registry = SkillRegistry()
        registry.register(MockSkill())

        found = registry.find_matching("/unknown")
        assert found is None
