Skip to content

Compress tool outputs, logs, files, and RAG chunks before they reach the LLM. 20% fewer tokens for coding agents, 60-95% fewer tokens for JSON, same answers. Library, proxy, MCP server.

3.6 KB
Raw Blame
"""Tests for `headroom wrap hermes` command."""

from __future__ import annotations

from unittest.mock import patch

import pytest
from click.testing import CliRunner

from headroom.cli import wrap as wrap_mod
from headroom.cli.main import main


@pytest.fixture
def runner() -> CliRunner:
    return CliRunner()


def test_wrap_hermes_sets_provider_envs(
    runner: CliRunner,
    tmp_path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """OPENAI_BASE_URL and ANTHROPIC_BASE_URL are set on launch."""
    monkeypatch.chdir(tmp_path)
    captured: dict[str, object] = {}

    def fake_launch_tool(**kwargs):  # noqa: ANN003
        captured.update(kwargs)

    with patch.object(wrap_mod.shutil, "which", return_value="hermes"):
        with patch.object(wrap_mod, "_launch_tool", side_effect=fake_launch_tool):
            result = runner.invoke(
                main, ["wrap", "hermes", "--port", "9000", "--", "--provider", "anthropic"]
            )

    assert result.exit_code == 0, result.output
    env = captured["env"]
    assert isinstance(env, dict)
    assert env["OPENAI_BASE_URL"] == "http://127.0.0.1:9000/v1"
    assert env["ANTHROPIC_BASE_URL"] == "http://127.0.0.1:9000"
    assert captured["tool_label"] == "HERMES"
    assert captured["agent_type"] == "hermes"
    assert captured["args"] == ("--provider", "anthropic")


def test_wrap_hermes_missing_binary_errors_clearly(
    runner: CliRunner,
    tmp_path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """If the hermes binary is missing, fail with a clear error."""
    monkeypatch.chdir(tmp_path)
    with patch.object(wrap_mod.shutil, "which", return_value=None):
        result = runner.invoke(main, ["wrap", "hermes"])

    assert result.exit_code == 1
    assert result.output == (
        "Error: 'hermes' not found in PATH.\n"
        "Install Hermes: https://hermes-agent.nousresearch.com/\n"
    )


def test_wrap_hermes_prepare_only_exits_cleanly(
    runner: CliRunner,
    tmp_path,
    monkeypatch: pytest.MonkeyPatch,
) -> None:
    """--prepare-only must not attempt to launch Hermes."""
    monkeypatch.chdir(tmp_path)
    with patch.object(wrap_mod, "_launch_tool") as launch_tool:
        result = runner.invoke(main, ["wrap", "hermes", "--prepare-only"])

    assert result.exit_code == 0, result.output
    launch_tool.assert_not_called()


@pytest.mark.parametrize(
    "hermes_args",
    [(), ("--provider", "openrouter"), ("--provider=nous",), ("--provider", "custom")],
)
def test_wrap_hermes_rejects_unrouted_provider_before_launch(
    runner: CliRunner,
    tmp_path,
    monkeypatch: pytest.MonkeyPatch,
    hermes_args: tuple[str, ...],
) -> None:
    monkeypatch.chdir(tmp_path)
    with patch.object(wrap_mod.shutil, "which", return_value="hermes"):
        with patch.object(wrap_mod, "_launch_tool") as launch:
            result = runner.invoke(main, ["wrap", "hermes", "--", *hermes_args])

    assert result.exit_code == 1
    assert "cannot route" in result.output
    assert "routed through Headroom" not in result.output
    launch.assert_not_called()


@pytest.mark.parametrize("provider", ["anthropic", "openai"])
def test_wrap_hermes_launches_routed_provider(
    runner: CliRunner,
    tmp_path,
    monkeypatch: pytest.MonkeyPatch,
    provider: str,
) -> None:
    monkeypatch.chdir(tmp_path)
    with patch.object(wrap_mod.shutil, "which", return_value="hermes"):
        with patch.object(wrap_mod, "_launch_tool") as launch:
            result = runner.invoke(main, ["wrap", "hermes", "--", "--provider", provider])

    assert result.exit_code == 0, result.output
    launch.assert_called_once()
    assert launch.call_args.kwargs["args"] == ("--provider", provider)