"""Skill 服务测试(CRUD + 关键词触发匹配)。""" import pytest from app.core.errors import AppError from app.services import skills as skills_service async def _create( name: str = "coding", trigger: str = "代码,编程", status: str = "enabled", ) -> None: await skills_service.create_skill( name=name, description="代码辅助", trigger=trigger, prompt_fragment="你是代码助手", tool_bindings=["read_file"], risk_level="L1", status=status, ) @pytest.mark.asyncio async def test_create_and_list() -> None: resp = await skills_service.create_skill( name="coding", trigger="代码", prompt_fragment="你是代码助手", tool_bindings=["read_file"], ) assert resp.name == "coding" assert resp.tool_bindings == ["read_file"] listed = await skills_service.list_skills() assert len(listed) == 1 @pytest.mark.asyncio async def test_create_duplicate_name_conflicts() -> None: await _create("dup") with pytest.raises(AppError): await _create("dup") @pytest.mark.asyncio async def test_update_skill_status() -> None: await _create() listed = await skills_service.list_skills() sid = listed[0].id updated = await skills_service.update_skill(sid, status="disabled") assert updated.status == "disabled" @pytest.mark.asyncio async def test_match_active_skills() -> None: await _create("coding", trigger="代码,编程") await _create("weather", trigger="天气", status="disabled") matched = await skills_service.match_active_skills("帮我写代码") assert len(matched) == 1 assert matched[0].name == "coding" assert matched[0].prompt_fragment == "你是代码助手" @pytest.mark.asyncio async def test_delete_skill() -> None: await _create() listed = await skills_service.list_skills() sid = listed[0].id await skills_service.delete_skill(sid) assert await skills_service.get_skill(sid) is None