Files
hermes-agent/tests/agent/test_env_loader_applied_homes.py
T
teknium1 d10bb2ab6f test: make tests/ mirror the source tree; drop issue numbers from filenames
`scripts/run_tests.sh tests/<dir>/` is how a change gets its regression
coverage run, so a test filed under the wrong directory is a test nobody
runs when that code changes. Two kinds of drift had accumulated.

Parallel directories for one source package, folded into the mirror:
  tests/acp        -> tests/acp_adapter   (its __init__/conftest move with it)
  tests/cli        -> tests/hermes_cli    (prompt_toolkit fixture merged into
                                           hermes_cli/conftest.py)
  tests/run_agent  -> tests/agent         (backoff fixture becomes
                                           agent/conftest.py)
  tests/relay      -> tests/gateway/relay
  tests/state      -> tests/hermes_state

246 loose files at tests/ root, routed by the package they import/patch:
hermes_cli, hermes_state, agent, gateway, tools, plugins, tui_gateway, cron.
Installer and desktop-update script tests go to tests/scripts/{install,
desktop_update}/. 43 tests of root-level modules (batch_runner, utils,
hermes_constants, packaging) stay at the root.

Filenames drop their issue numbers (95 files: test_89315_x.py -> test_x.py);
the number stays in the module docstring where it has context.

Collisions: test_cli_skin_integration.py existed in both tests/ and tests/cli
with different subsets — merged into one (10 tests, all kept);
run_agent/test_pre_compress_memory_context.py -> agent/..._handoff.py;
tests/test_account_usage.py -> agent/test_account_usage_fetch.py;
tests/test_web_server.py -> hermes_cli/test_web_server_ws_ping.py.
Deleted: test_minisweagent_path.py (empty since PR #2804),
test_model_picker_scroll.py (tested a private copy of the logic, imported
nothing), test_process_loop_event_loop_warning.py (asserted asyncio behaviour,
imported nothing from Hermes).

Repo-root path arithmetic (Path(__file__).parents[N], dirname chains) is
bumped for the 202 files that changed depth and verified by evaluating every
such expression against the new location. classify_changes' desktop-updater
lane prefix, tests-os.yml's ignore glob and every in-tree path comment follow
the moves. tests/test_tests_tree_layout.py keeps the tree from drifting back.
2026-09-13 09:18:02 -07:00

136 lines
4.5 KiB
Python

"""Regression tests for #40597: _APPLIED_HOMES must be marked AFTER a real
fetch attempt, so early failures (malformed config, disabled sources) stay
retryable within the process instead of being permanently skipped."""
from __future__ import annotations
from pathlib import Path
import pytest
from hermes_cli import env_loader
@pytest.fixture(autouse=True)
def _reset():
env_loader.reset_secret_source_cache()
yield
env_loader.reset_secret_source_cache()
from agent.secret_sources import registry
registry._reset_registry_for_tests()
def _write_enabled_config(home: Path):
(home / "config.yaml").write_text(
"secrets:\n"
" bitwarden:\n"
" enabled: true\n"
" project_id: proj\n"
)
def test_malformed_config_does_not_permanently_skip(tmp_path, monkeypatch):
"""Config error on first call → fixed config on second call must apply."""
home = tmp_path / ".hermes"
home.mkdir()
(home / "config.yaml").write_text("secrets: [unclosed") # malformed YAML
env_loader._apply_external_secret_sources(home)
assert str(home.resolve()) not in env_loader._APPLIED_HOMES
# User fixes the config; same process must now attempt the fetch.
_write_enabled_config(home)
monkeypatch.setenv("BWS_ACCESS_TOKEN", "0.t")
import agent.secret_sources.bitwarden as bw
calls = {"n": 0}
def fake_fetch(**kwargs):
calls["n"] += 1
return {"NEW_KEY_40597": "val"}, []
monkeypatch.setattr(bw, "find_bws", lambda install_if_missing=True: home / "bws")
monkeypatch.setattr(bw, "fetch_bitwarden_secrets", fake_fetch)
monkeypatch.delenv("NEW_KEY_40597", raising=False)
from agent.secret_sources import registry
registry._reset_registry_for_tests()
env_loader._apply_external_secret_sources(home)
assert calls["n"] == 1
assert str(home.resolve()) in env_loader._APPLIED_HOMES
monkeypatch.delenv("NEW_KEY_40597", raising=False)
def test_no_secrets_section_does_not_mark_applied(tmp_path):
home = tmp_path / ".hermes"
home.mkdir()
(home / "config.yaml").write_text("model:\n provider: openrouter\n")
env_loader._apply_external_secret_sources(home)
assert str(home.resolve()) not in env_loader._APPLIED_HOMES
def test_disabled_sources_do_not_mark_applied(tmp_path):
home = tmp_path / ".hermes"
home.mkdir()
(home / "config.yaml").write_text(
"secrets:\n bitwarden:\n enabled: false\n project_id: p\n"
)
from agent.secret_sources import registry
registry._reset_registry_for_tests()
env_loader._apply_external_secret_sources(home)
assert str(home.resolve()) not in env_loader._APPLIED_HOMES
def test_fetch_error_still_marks_applied(tmp_path, monkeypatch):
"""A real fetch attempt that FAILS still marks the home — otherwise every
import-time load_hermes_dotenv() would re-fetch and re-print the same
error 3-5x per startup."""
home = tmp_path / ".hermes"
home.mkdir()
_write_enabled_config(home)
monkeypatch.setenv("BWS_ACCESS_TOKEN", "0.dead")
import agent.secret_sources.bitwarden as bw
calls = {"n": 0}
def boom(**kwargs):
calls["n"] += 1
raise RuntimeError("bws exited 1: network unreachable")
monkeypatch.setattr(bw, "find_bws", lambda install_if_missing=True: home / "bws")
monkeypatch.setattr(bw, "fetch_bitwarden_secrets", boom)
from agent.secret_sources import registry
registry._reset_registry_for_tests()
env_loader._apply_external_secret_sources(home)
env_loader._apply_external_secret_sources(home) # second call = no-op
assert calls["n"] == 1
assert str(home.resolve()) in env_loader._APPLIED_HOMES
def test_success_marks_applied_and_second_call_noop(tmp_path, monkeypatch):
home = tmp_path / ".hermes"
home.mkdir()
_write_enabled_config(home)
monkeypatch.setenv("BWS_ACCESS_TOKEN", "0.t")
monkeypatch.delenv("KEY_OK_40597", raising=False)
import agent.secret_sources.bitwarden as bw
calls = {"n": 0}
def fake_fetch(**kwargs):
calls["n"] += 1
return {"KEY_OK_40597": "v"}, []
monkeypatch.setattr(bw, "find_bws", lambda install_if_missing=True: home / "bws")
monkeypatch.setattr(bw, "fetch_bitwarden_secrets", fake_fetch)
from agent.secret_sources import registry
registry._reset_registry_for_tests()
env_loader._apply_external_secret_sources(home)
env_loader._apply_external_secret_sources(home)
assert calls["n"] == 1
monkeypatch.delenv("KEY_OK_40597", raising=False)