"""
测试 skills_tools.py —— SKILL.md 解析、discover_skills（无 yaml）
"""
import sys
import tempfile
from pathlib import Path

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


class TestSkillsParser:
    """SKILL.md 解析 + 自动发现"""

    def test_parse_skill_md_basic(self):
        from aacode.tools.skills_tools import SkillsManager
        sm = SkillsManager(Path("aacode"))

        with tempfile.TemporaryDirectory() as td:
            skill_dir = Path(td)
            md_file = skill_dir / "SKILL.md"
            md_file.write_text("""## Description
这是一句话描述

## Parameters
- param1: 参数1描述
- param2: 参数2描述

## Example
run_skill("test", {"param1": "value1"})
""")
            info = sm._parse_skill_md(md_file)

            assert info["description"] == "这是一句话描述"
            assert info["parameters_desc"] == {"param1": "参数1描述", "param2": "参数2描述"}
            assert "Example" in info["examples_text"] or "run_skill" in info["examples_text"]
            assert "full_content" in info

    def test_parse_skill_md_no_keywords(self):
        """新格式无关键词段，不影响解析"""
        from aacode.tools.skills_tools import SkillsManager
        sm = SkillsManager(Path("aacode"))

        with tempfile.TemporaryDirectory() as td:
            skill_dir = Path(td)
            md_file = skill_dir / "SKILL.md"
            md_file.write_text("""## Description
简单描述

## Example
run_skill("x", {})
""")
            info = sm._parse_skill_md(md_file)

            assert info["description"] == "简单描述"
            assert info["parameters_desc"] == {}

    def test_parse_skill_md_english_keywords(self):
        from aacode.tools.skills_tools import SkillsManager
        sm = SkillsManager(Path("aacode"))

        with tempfile.TemporaryDirectory() as td:
            skill_dir = Path(td)
            md_file = skill_dir / "SKILL.md"
            md_file.write_text("""## Description
Call REST APIs

## Parameters
- url: Target URL
- method: HTTP method

## Example
run_skill("api_client", {"url": "https://api.example.com", "method": "GET"})
""")
            info = sm._parse_skill_md(md_file)

            assert info["description"] == "Call REST APIs"
            assert info["parameters_desc"] == {"url": "Target URL", "method": "HTTP method"}

    def test_discover_skills_no_yaml(self):
        """验证 discover_skills 不依赖 yaml"""
        from aacode.tools.skills_tools import SkillsManager
        sm = SkillsManager(Path("aacode"))
        # 调用 discover_skills —— 内部不应引用 skills_metadata（yaml）
        discovered = sm.discover_skills()
        assert len(discovered) >= 3  # playwright, pandas, numpy
        for name, info in discovered.items():
            assert info.description, f"{name}: missing description"
            assert info.metadata_loaded, f"{name}: metadata_loaded 应为 True"

    def test_load_skill_extracts_display_name(self):
        from aacode.tools.skills_tools import SkillsManager
        sm = SkillsManager(Path("aacode"))
        discovered = sm.discover_skills()

        pandas = discovered.get("pandas")
        assert pandas is not None
        assert pandas.display_name == "Pandas"
        assert "pandas" in pandas.description.lower() or "数据" in pandas.description

    def test_load_skill_parameters_desc_from_md(self):
        """SKILL.md 的 Parameters 段落应补全函数参数描述"""
        from aacode.tools.skills_tools import SkillsManager
        sm = SkillsManager(Path("aacode"))
        discovered = sm.discover_skills()

        playwright = discovered.get("playwright")
        assert playwright is not None
        # playwright 的 SKILL.md 有 Parameters 段落，参数 url 应有描述
        ba_params = playwright.functions.get("browser_automation", {}).get("parameters", {})
        url_desc = ba_params.get("url", {}).get("description", "")
        assert len(url_desc) > 0
