mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-31 19:16:29 +00:00
Second, deeper pass over tools/gateway/hermes_cli plus first pass over the trees wave 1 missed (acp, acp_adapter, skills, computer_use, docker, dashboard, conformance, monitoring, secret_sources, hermes_state, providers). Same rubric as wave 1 (AGENTS.md test policy); security, alternation/caching invariants, issue-number regressions, and E2E kept. Real test-quality fixes found and rooted out along the way: - tests/tools/test_command_guards.py made real auxiliary-LLM HTTPS calls (DEFAULT_CONFIG smart-approval leaked in) — pinned approval mode=manual via autouse fixture: 17.4s → 0.4s. - test_model_switch_custom_providers.py / test_user_providers_model_switch.py silently probed live provider catalogs (~2s/test) — stubbed cached_provider_model_ids/provider_model_ids/fetch_api_models. - test_telegram_noise_filter.py: 15-platform copy-paste matrix over shared gateway.run logic → 3 representative platforms (55s → 3.9s). - test_gateway_shutdown.py: stop()'s 5s interrupt-deadline loop spun on MagicMock agents — interrupt.side_effect now clears _running_agents (22s → 1.0s). - test_gateway_inactivity_timeout.py poll-harness timings shrunk 3-5x (24s → 1.1s); test_mcp_stability.py backoff/SIGTERM-grace sleeps patched (15.4s → 2.5s); test_async_delegation.py negative-drain wait 5s → 0.5s. - test_telegram_init_deadline.py: loop-block margin restored to 1.0s with rationale comment — the watchdog-dump assertion needs the loop blocked well past deadline+grace under parallel load (flaked once in the 40-worker verification run at a 0.2s margin). Verification: full hermetic suite via scripts/run_tests.sh — 2,438 files, 21,718 tests passed, 0 failed, 293.9s wall. Suite totals vs original baseline: 46,820 → 19,757 test functions (−57.8%), wall 583.5s → 293.9s (−50%), subprocess CPU 13,564s → 11,623s.
124 lines
4.7 KiB
Python
124 lines
4.7 KiB
Python
"""Tests for ${ENV_VAR} substitution in config.yaml values."""
|
|
|
|
import pytest
|
|
from hermes_cli.config import _expand_env_vars, load_config
|
|
|
|
|
|
class TestExpandEnvVars:
|
|
def test_simple_substitution(self):
|
|
with pytest.MonkeyPatch().context() as mp:
|
|
mp.setenv("MY_KEY", "secret123")
|
|
assert _expand_env_vars("${MY_KEY}") == "secret123"
|
|
|
|
|
|
|
|
|
|
def test_non_string_values_untouched(self):
|
|
assert _expand_env_vars(42) == 42
|
|
assert _expand_env_vars(3.14) == 3.14
|
|
assert _expand_env_vars(True) is True
|
|
assert _expand_env_vars(None) is None
|
|
|
|
|
|
|
|
|
|
class TestLoadConfigExpansion:
|
|
def test_load_config_expands_env_vars(self, tmp_path, monkeypatch):
|
|
config_yaml = (
|
|
"model:\n"
|
|
" api_key: ${GOOGLE_API_KEY}\n"
|
|
"platforms:\n"
|
|
" telegram:\n"
|
|
" token: ${TELEGRAM_BOT_TOKEN}\n"
|
|
"plain: no-substitution\n"
|
|
)
|
|
config_file = tmp_path / "config.yaml"
|
|
config_file.write_text(config_yaml)
|
|
|
|
monkeypatch.setenv("GOOGLE_API_KEY", "gsk-test-key")
|
|
monkeypatch.setenv("TELEGRAM_BOT_TOKEN", "1234567:ABC-token")
|
|
# Patch the imported function's own globals. Other tests may reload
|
|
# hermes_cli.config, making string-target monkeypatches hit a different
|
|
# module object than this collection-time imported load_config().
|
|
monkeypatch.setitem(load_config.__globals__, "get_config_path", lambda: config_file)
|
|
|
|
config = load_config()
|
|
|
|
assert config["model"]["api_key"] == "gsk-test-key"
|
|
assert config["platforms"]["telegram"]["token"] == "1234567:ABC-token"
|
|
assert config["plain"] == "no-substitution"
|
|
|
|
|
|
class TestLoadConfigCacheEnvStaleness:
|
|
"""The load_config() cache must not pin expansions made against a stale
|
|
environment (#58514): a load before load_hermes_dotenv() runs, or an env
|
|
var rotated in-process, must not keep serving the old expansion."""
|
|
|
|
def test_env_var_appearing_after_first_load_invalidates_cache(self, tmp_path, monkeypatch):
|
|
config_yaml = "auxiliary:\n vision:\n api_key: ${LATE_DOTENV_KEY_58514}\n"
|
|
config_file = tmp_path / "config.yaml"
|
|
config_file.write_text(config_yaml)
|
|
|
|
monkeypatch.delenv("LATE_DOTENV_KEY_58514", raising=False)
|
|
monkeypatch.setitem(load_config.__globals__, "get_config_path", lambda: config_file)
|
|
|
|
# First load happens before the var exists (pre-dotenv): literal kept.
|
|
assert load_config()["auxiliary"]["vision"]["api_key"] == "${LATE_DOTENV_KEY_58514}"
|
|
|
|
# .env load brings the var in — same file mtime/size, env changed.
|
|
monkeypatch.setenv("LATE_DOTENV_KEY_58514", "nvapi-real")
|
|
assert load_config()["auxiliary"]["vision"]["api_key"] == "nvapi-real"
|
|
|
|
|
|
def test_unchanged_env_still_serves_cache(self, tmp_path, monkeypatch):
|
|
config_yaml = "providers:\n mistral:\n api_key: ${STABLE_KEY_58514}\n"
|
|
config_file = tmp_path / "config.yaml"
|
|
config_file.write_text(config_yaml)
|
|
|
|
monkeypatch.setenv("STABLE_KEY_58514", "key-stable")
|
|
monkeypatch.setitem(load_config.__globals__, "get_config_path", lambda: config_file)
|
|
|
|
load_config()
|
|
# load_config_readonly() returns the cached object itself, so object
|
|
# identity across calls proves the cache-hit path was taken (a rebuild
|
|
# would produce a fresh dict).
|
|
readonly = load_config.__globals__["load_config_readonly"]
|
|
first = readonly()
|
|
second = readonly()
|
|
|
|
assert first is second
|
|
assert first["providers"]["mistral"]["api_key"] == "key-stable"
|
|
|
|
|
|
class TestLoadCliConfigExpansion:
|
|
"""Verify that load_cli_config() also expands ${VAR} references."""
|
|
|
|
def test_cli_config_ignores_empty_terminal_section(self, tmp_path, monkeypatch):
|
|
config_file = tmp_path / "config.yaml"
|
|
config_file.write_text("terminal:\n")
|
|
|
|
monkeypatch.setattr("cli._hermes_home", tmp_path)
|
|
|
|
from cli import load_cli_config
|
|
config = load_cli_config()
|
|
|
|
assert isinstance(config["terminal"], dict)
|
|
assert config["terminal"]["env_type"] == "local"
|
|
|
|
|
|
def test_cli_config_unresolved_kept_verbatim(self, tmp_path, monkeypatch):
|
|
config_yaml = (
|
|
"auxiliary:\n"
|
|
" vision:\n"
|
|
" api_key: ${UNSET_CLI_VAR_ABC}\n"
|
|
)
|
|
config_file = tmp_path / "config.yaml"
|
|
config_file.write_text(config_yaml)
|
|
|
|
monkeypatch.delenv("UNSET_CLI_VAR_ABC", raising=False)
|
|
monkeypatch.setattr("cli._hermes_home", tmp_path)
|
|
|
|
from cli import load_cli_config
|
|
config = load_cli_config()
|
|
|
|
assert config["auxiliary"]["vision"]["api_key"] == "${UNSET_CLI_VAR_ABC}"
|