"""Agent Skills (agentskills.io) discovery and frontmatter validation.""" from pathlib import Path import pytest from app.skills.agent_skills import SkillFormatError, discover_skills, read_skill def write_skill( parent: Path, dirname: str, frontmatter: str, body: str = "# Instructions\n" ) -> Path: skill_dir = parent / dirname skill_dir.mkdir(parents=True) (skill_dir / "SKILL.md").write_text( f"---\n{frontmatter}\n---\n\n{body}", encoding="utf-8" ) return skill_dir def test_reads_valid_skill_with_optional_fields(tmp_path): skill_dir = write_skill( tmp_path, "pdf-processing", "name: pdf-processing\n" "description: >-\n Extract PDF text. Use when handling PDFs.\n" "license: Apache-2.0\n" "compatibility: Requires poppler\n" "metadata:\n category: Documents\n" "allowed-tools: Read", ) skill = read_skill(skill_dir) assert skill.name == "pdf-processing" assert skill.description == "Extract PDF text. Use when handling PDFs." assert skill.path == skill_dir @pytest.mark.parametrize( ("dirname", "frontmatter", "message"), [ ("PDF-Processing", "name: PDF-Processing\ndescription: d", "lowercase"), ("-pdf", "name: -pdf\ndescription: d", "lowercase"), ("pdf-", "name: pdf-\ndescription: d", "lowercase"), ("pdf--processing", "name: pdf--processing\ndescription: d", "lowercase"), ("pdf_processing", "name: pdf_processing\ndescription: d", "lowercase"), ("a" * 65, f"name: {'a' * 65}\ndescription: d", "64"), ("other-dir", "name: pdf-processing\ndescription: d", "directory"), ("pdf", "name: pdf", "description"), ("pdf", "name: pdf\ndescription: ''", "description"), ("pdf", f"name: pdf\ndescription: {'x' * 1025}", "1024"), ("pdf", f"name: pdf\ndescription: d\ncompatibility: {'x' * 501}", "500"), ("pdf", "name: pdf\ndescription: d\nmetadata:\n version: 2", "metadata"), ("pdf", "name: pdf\ndescription: d\nphase: design", "phase"), ], ) def test_rejects_invalid_frontmatter(tmp_path, dirname, frontmatter, message): skill_dir = write_skill(tmp_path, dirname, frontmatter) with pytest.raises(SkillFormatError, match=message): read_skill(skill_dir) def test_rejects_skill_without_frontmatter(tmp_path): skill_dir = tmp_path / "pdf" skill_dir.mkdir() (skill_dir / "SKILL.md").write_text("# No frontmatter\n", encoding="utf-8") with pytest.raises(SkillFormatError, match="frontmatter"): read_skill(skill_dir) def test_discovers_immediate_skill_directories_only(tmp_path): root = tmp_path / "skills" write_skill(root, "alpha", "name: alpha\ndescription: first") write_skill(root / "vendor" / "google", "beta", "name: beta\ndescription: nested") (root / "notes").mkdir() skills = discover_skills([root, root / "vendor" / "google"]) assert sorted(skills) == ["alpha", "beta"] def test_rejects_duplicate_skill_names_across_directories(tmp_path): write_skill(tmp_path / "one", "alpha", "name: alpha\ndescription: first") write_skill(tmp_path / "two", "alpha", "name: alpha\ndescription: second") with pytest.raises(SkillFormatError, match="alpha"): discover_skills([tmp_path / "one", tmp_path / "two"])