mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-05-01 01:51:44 +00:00
Rewrite all import statements, patch() targets, sys.modules keys, importlib.import_module() strings, and subprocess -m references to use hermes_agent.* paths. Strip sys.path.insert hacks from production code (rely on editable install). Update COMPONENT_PREFIXES for logger filtering. Fix 3 hardcoded getLogger() calls to use __name__. Update transport and tool registry discovery paths. Update plugin module path strings. Add legacy process-name patterns for gateway PID detection. Add main() to skills_sync for console_script entry point. Fix _get_bundled_dir() path traversal after move. Part of #14182, #14183
86 lines
3.5 KiB
Python
86 lines
3.5 KiB
Python
"""Tests for _parse_env_var and _get_env_config env-var validation."""
|
|
|
|
import json
|
|
from unittest.mock import patch
|
|
|
|
import pytest
|
|
|
|
import sys
|
|
import hermes_agent.tools.terminal # noqa: F401 -- ensure module is loaded
|
|
_tt_mod = sys.modules["hermes_agent.tools.terminal"]
|
|
from hermes_agent.tools.terminal import _parse_env_var
|
|
|
|
|
|
class TestParseEnvVar:
|
|
"""Unit tests for _parse_env_var."""
|
|
|
|
# -- valid values work normally --
|
|
|
|
def test_valid_int(self):
|
|
with patch.dict("os.environ", {"TERMINAL_TIMEOUT": "300"}):
|
|
assert _parse_env_var("TERMINAL_TIMEOUT", "180") == 300
|
|
|
|
def test_valid_float(self):
|
|
with patch.dict("os.environ", {"TERMINAL_CONTAINER_CPU": "2.5"}):
|
|
assert _parse_env_var("TERMINAL_CONTAINER_CPU", "1", float, "number") == 2.5
|
|
|
|
def test_valid_json(self):
|
|
volumes = '["/host:/container"]'
|
|
with patch.dict("os.environ", {"TERMINAL_DOCKER_VOLUMES": volumes}):
|
|
result = _parse_env_var("TERMINAL_DOCKER_VOLUMES", "[]", json.loads, "valid JSON")
|
|
assert result == ["/host:/container"]
|
|
|
|
def test_get_env_config_parses_docker_forward_env_json(self):
|
|
with patch.dict("os.environ", {
|
|
"TERMINAL_ENV": "docker",
|
|
"TERMINAL_DOCKER_FORWARD_ENV": '["GITHUB_TOKEN", "NPM_TOKEN"]',
|
|
}, clear=False):
|
|
config = _tt_mod._get_env_config()
|
|
assert config["docker_forward_env"] == ["GITHUB_TOKEN", "NPM_TOKEN"]
|
|
|
|
def test_create_environment_passes_docker_forward_env(self):
|
|
fake_env = object()
|
|
with patch.object(_tt_mod, "_DockerEnvironment", return_value=fake_env) as mock_docker:
|
|
result = _tt_mod._create_environment(
|
|
"docker",
|
|
image="python:3.11",
|
|
cwd="/root",
|
|
timeout=180,
|
|
container_config={"docker_forward_env": ["GITHUB_TOKEN"]},
|
|
)
|
|
|
|
assert result is fake_env
|
|
assert mock_docker.call_args.kwargs["forward_env"] == ["GITHUB_TOKEN"]
|
|
|
|
def test_falls_back_to_default(self):
|
|
with patch.dict("os.environ", {}, clear=False):
|
|
# Remove the var if it exists, rely on default
|
|
import os
|
|
env = os.environ.copy()
|
|
env.pop("TERMINAL_TIMEOUT", None)
|
|
with patch.dict("os.environ", env, clear=True):
|
|
assert _parse_env_var("TERMINAL_TIMEOUT", "180") == 180
|
|
|
|
# -- invalid int raises ValueError with env var name --
|
|
|
|
def test_invalid_int_raises_with_var_name(self):
|
|
with patch.dict("os.environ", {"TERMINAL_TIMEOUT": "5m"}):
|
|
with pytest.raises(ValueError, match="TERMINAL_TIMEOUT"):
|
|
_parse_env_var("TERMINAL_TIMEOUT", "180")
|
|
|
|
def test_invalid_int_includes_bad_value(self):
|
|
with patch.dict("os.environ", {"TERMINAL_SSH_PORT": "ssh"}):
|
|
with pytest.raises(ValueError, match="ssh"):
|
|
_parse_env_var("TERMINAL_SSH_PORT", "22")
|
|
|
|
# -- invalid JSON raises ValueError with env var name --
|
|
|
|
def test_invalid_json_raises_with_var_name(self):
|
|
with patch.dict("os.environ", {"TERMINAL_DOCKER_VOLUMES": "/host:/container"}):
|
|
with pytest.raises(ValueError, match="TERMINAL_DOCKER_VOLUMES"):
|
|
_parse_env_var("TERMINAL_DOCKER_VOLUMES", "[]", json.loads, "valid JSON")
|
|
|
|
def test_invalid_json_includes_type_label(self):
|
|
with patch.dict("os.environ", {"TERMINAL_DOCKER_VOLUMES": "not json"}):
|
|
with pytest.raises(ValueError, match="valid JSON"):
|
|
_parse_env_var("TERMINAL_DOCKER_VOLUMES", "[]", json.loads, "valid JSON")
|