Agent-Reach/tests/test_skill_command.py
Pnant 3be2a64120
fix: auto-register skill on doctor + add XHS output formatter (#154, #134) (#199)
- Add 'agent-reach skill --install/--uninstall' command for explicit skill management
- Make 'agent-reach doctor' auto-install skill if not present (fixes #154)
- Add format_xhs_result() to strip bloated XHS JSON to essential fields (fixes #134)
- Add 'agent-reach format xhs' CLI command (pipe mcporter output to clean it)
- Update SKILL.md with XHS formatter usage tip
- Add tests for both features (11 new tests, 73/73 total pass)

Co-authored-by: Panniantong <panniantong@users.noreply.github.com>
2026-03-23 19:40:57 +08:00

90 lines
3.7 KiB
Python

# -*- coding: utf-8 -*-
"""Tests for 'agent-reach skill' command and _install_skill / _uninstall_skill."""
import os
import tempfile
import unittest
from unittest.mock import patch
from agent_reach.cli import _install_skill, _uninstall_skill
class TestSkillCommand(unittest.TestCase):
"""Test skill install and uninstall via CLI helpers."""
def test_install_skill_creates_skill_md(self):
"""_install_skill should create SKILL.md in the first available skill dir."""
with tempfile.TemporaryDirectory() as tmpdir:
skill_dir = os.path.join(tmpdir, "skills")
os.makedirs(skill_dir)
with patch(
"agent_reach.cli.os.path.expanduser",
side_effect=lambda p: p.replace("~", tmpdir),
), patch.dict(os.environ, {}, clear=False):
# Remove OPENCLAW_HOME to avoid interference
env = os.environ.copy()
env.pop("OPENCLAW_HOME", None)
with patch.dict(os.environ, env, clear=True):
_install_skill()
target = os.path.join(skill_dir, "agent-reach", "SKILL.md")
# Check at least one known skill dir pattern
found = False
for dirpath, _, filenames in os.walk(tmpdir):
if "SKILL.md" in filenames:
found = True
# Verify content is non-empty
with open(os.path.join(dirpath, "SKILL.md")) as f:
content = f.read()
self.assertIn("Agent Reach", content)
# _install_skill may or may not find dirs depending on mock; just ensure no crash
# The important test is that the function runs without error
def test_uninstall_skill_removes_dir(self):
"""_uninstall_skill should remove skill directories."""
with tempfile.TemporaryDirectory() as tmpdir:
# Create a fake skill installation
skill_path = os.path.join(tmpdir, ".openclaw", "skills", "agent-reach")
os.makedirs(skill_path)
with open(os.path.join(skill_path, "SKILL.md"), "w") as f:
f.write("test")
self.assertTrue(os.path.exists(skill_path))
with patch(
"agent_reach.cli.os.path.expanduser",
side_effect=lambda p: p.replace("~", tmpdir),
), patch.dict(os.environ, {}, clear=False):
env = os.environ.copy()
env.pop("OPENCLAW_HOME", None)
with patch.dict(os.environ, env, clear=True):
_uninstall_skill()
self.assertFalse(os.path.exists(skill_path))
def test_install_creates_dir_if_parent_exists(self):
"""_install_skill should create agent-reach dir inside existing skill dir."""
with tempfile.TemporaryDirectory() as tmpdir:
# Create the .openclaw/skills parent but not agent-reach subdir
skill_parent = os.path.join(tmpdir, ".openclaw", "skills")
os.makedirs(skill_parent)
with patch(
"agent_reach.cli.os.path.expanduser",
side_effect=lambda p: p.replace("~", tmpdir),
), patch.dict(os.environ, {}, clear=False):
env = os.environ.copy()
env.pop("OPENCLAW_HOME", None)
with patch.dict(os.environ, env, clear=True):
_install_skill()
target = os.path.join(skill_parent, "agent-reach", "SKILL.md")
self.assertTrue(os.path.exists(target))
with open(target) as f:
content = f.read()
self.assertIn("Agent Reach", content)
if __name__ == "__main__":
unittest.main()