Bot & Automation

AI Assistants

/root/hermes-projects/AI Assistants

tests/test_hermes_admin.py text
from pathlib import Path

from apps.whatsapp_control_api.app.api.main import (
    _admin_csrf_token,
    _admin_session_token,
    _admin_session_valid,
)
from apps.whatsapp_control_api.app.core.config import get_settings
from apps.whatsapp_control_api.app.services.hermes_admin import (
    CommandResult,
    install_skill,
    list_installed_skills,
    set_tool_enabled,
)
from apps.whatsapp_control_api.app.services import hermes_admin


def test_admin_session_token_is_stable(monkeypatch) -> None:
    monkeypatch.setenv("META_VERIFY_TOKEN", "test-secret")
    get_settings.cache_clear()
    token = _admin_session_token()
    assert token
    assert _admin_session_valid(token) is True
    assert _admin_csrf_token() != token
    get_settings.cache_clear()


def test_list_installed_skills_reads_frontmatter(tmp_path: Path, monkeypatch) -> None:
    skill_dir = tmp_path / ".hermes" / "skills" / "research" / "sample-skill"
    skill_dir.mkdir(parents=True)
    (skill_dir / "SKILL.md").write_text(
        "---\nname: sample-skill\ndescription: Sample description\nversion: 1.2.3\n---\n# Skill\n",
        encoding="utf-8",
    )
    monkeypatch.setenv("HERMES_WORKSPACE_ROOT", str(tmp_path))
    get_settings.cache_clear()
    skills = list_installed_skills()
    assert skills[0]["name"] == "sample-skill"
    assert skills[0]["category"] == "research"
    assert skills[0]["version"] == "1.2.3"
    get_settings.cache_clear()


def test_install_skill_rejects_shell_characters() -> None:
    result = install_skill("safe-skill; rm -rf /")
    assert isinstance(result, CommandResult)
    assert result.ok is False


def test_tool_name_validation_rejects_shell_characters() -> None:
    result = set_tool_enabled("web; reboot", True)
    assert result.ok is False


def test_install_skill_uses_native_hermes_arguments(monkeypatch) -> None:
    captured: list[str] = []

    def fake_run(arguments: list[str], *, timeout: int) -> CommandResult:
        captured.extend(arguments)
        return CommandResult(True, "installed")

    monkeypatch.setattr(hermes_admin, "run_hermes_command", fake_run)
    result = install_skill("official/research/example-skill")
    assert result.ok is True
    assert captured == ["skills", "install", "official/research/example-skill", "--yes"]