Files
hermes-agent/tests/hermes_cli/test_cli_first_run_setup.py
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

253 lines
8.2 KiB
Python

"""First-run onboarding routing for a completely unconfigured install.
Regression tests for the "keyless first run boots into a broken chat" bug:
a fresh install with zero providers accepted a message, spun for ~30s, then
failed with a provider-specific error ("Set OPENROUTER_API_KEY") the user
never chose, and never offered setup.
Covers:
- ``_runtime_credentials_ready()`` silent probe semantics
- ``_offer_first_run_setup()`` routing into the shared provider picker
- the provider-aware (non-OpenRouter-specific) empty-key error message
"""
import importlib
import sys
import types
import pytest
from hermes_cli.auth import AuthError
def _reset_modules(prefixes: tuple[str, ...]):
for name in list(sys.modules):
if any(name == p or name.startswith(p + ".") for p in prefixes):
sys.modules.pop(name, None)
@pytest.fixture(autouse=True)
def _restore_cli_and_tool_modules():
prefixes = ("tools", "cli", "run_agent")
original_modules = {
name: module
for name, module in sys.modules.items()
if any(name == p or name.startswith(p + ".") for p in prefixes)
}
try:
yield
finally:
_reset_modules(prefixes)
sys.modules.update(original_modules)
def _import_cli():
for name in list(sys.modules):
if name == "cli" or name == "run_agent" or name == "tools" or name.startswith("tools."):
sys.modules.pop(name, None)
if "firecrawl" not in sys.modules:
sys.modules["firecrawl"] = types.SimpleNamespace(Firecrawl=object)
return importlib.import_module("cli")
def _make_shell(cli, monkeypatch):
shell = cli.HermesCLI(compact=True, max_turns=1)
return shell
# ---------------------------------------------------------------------------
# _runtime_credentials_ready
# ---------------------------------------------------------------------------
def test_credentials_ready_false_when_no_provider(monkeypatch):
cli = _import_cli()
def _raise(**kwargs):
raise AuthError("No inference provider configured.", code="no_provider_configured")
monkeypatch.setattr("hermes_cli.runtime_provider.resolve_runtime_provider", _raise)
shell = _make_shell(cli, monkeypatch)
assert shell._runtime_credentials_ready() is False
def test_credentials_ready_false_on_empty_openrouter_key(monkeypatch):
"""The exact broken-chat state: provider resolves but api_key is empty."""
cli = _import_cli()
monkeypatch.setattr(
"hermes_cli.runtime_provider.resolve_runtime_provider",
lambda **kw: {
"provider": "openrouter",
"api_key": "",
"base_url": "https://openrouter.ai/api/v1",
"source": "env/config",
},
)
shell = _make_shell(cli, monkeypatch)
assert shell._runtime_credentials_ready() is False
def test_credentials_ready_true_with_key(monkeypatch):
cli = _import_cli()
monkeypatch.setattr(
"hermes_cli.runtime_provider.resolve_runtime_provider",
lambda **kw: {
"provider": "openrouter",
"api_key": "sk-test",
"base_url": "https://openrouter.ai/api/v1",
"source": "env/config",
},
)
shell = _make_shell(cli, monkeypatch)
assert shell._runtime_credentials_ready() is True
def test_credentials_ready_true_for_keyless_local_endpoint(monkeypatch):
"""ollama/llama.cpp-style custom endpoints need no key."""
cli = _import_cli()
monkeypatch.setattr(
"hermes_cli.runtime_provider.resolve_runtime_provider",
lambda **kw: {
"provider": "custom",
"api_key": "",
"base_url": "http://localhost:11434/v1",
"source": "custom_provider",
},
)
shell = _make_shell(cli, monkeypatch)
assert shell._runtime_credentials_ready() is True
def test_credentials_ready_true_for_callable_bearer_provider(monkeypatch):
cli = _import_cli()
monkeypatch.setattr(
"hermes_cli.runtime_provider.resolve_runtime_provider",
lambda **kw: {
"provider": "azure-foundry",
"api_key": lambda: "tok",
"base_url": "https://foundry.example/v1",
"source": "entra",
},
)
shell = _make_shell(cli, monkeypatch)
assert shell._runtime_credentials_ready() is True
def test_credentials_ready_never_prints(monkeypatch, capsys):
cli = _import_cli()
def _raise(**kwargs):
raise AuthError("No inference provider configured.", code="no_provider_configured")
monkeypatch.setattr("hermes_cli.runtime_provider.resolve_runtime_provider", _raise)
shell = _make_shell(cli, monkeypatch)
capsys.readouterr() # drain construction output
shell._runtime_credentials_ready()
out = capsys.readouterr()
assert out.out == ""
# ---------------------------------------------------------------------------
# _offer_first_run_setup
# ---------------------------------------------------------------------------
def test_offer_first_run_setup_routes_into_shared_picker(monkeypatch):
cli = _import_cli()
shell = _make_shell(cli, monkeypatch)
picker_calls = {"count": 0}
def _fake_picker():
picker_calls["count"] += 1
monkeypatch.setattr("hermes_cli.main.select_provider_and_model", _fake_picker)
monkeypatch.setattr("builtins.input", lambda *a, **k: "y")
# After the picker "runs", config has a provider and creds resolve.
monkeypatch.setattr(
"hermes_cli.config.load_config",
lambda: {"model": {"provider": "nous", "default": "hermes-4-405b"}},
)
monkeypatch.setattr(
"hermes_cli.runtime_provider.resolve_runtime_provider",
lambda **kw: {
"provider": "nous",
"api_key": "portal-token",
"base_url": "https://inference-api.nousresearch.com/v1",
"source": "oauth",
},
)
assert shell._offer_first_run_setup() is True
assert picker_calls["count"] == 1
assert shell.requested_provider == "nous"
assert shell.model == "hermes-4-405b"
# Agent must be rebuilt with the new credentials on next use.
assert shell.agent is None
def test_offer_first_run_setup_declined(monkeypatch):
cli = _import_cli()
shell = _make_shell(cli, monkeypatch)
def _fail_picker():
raise AssertionError("picker must not run when declined")
monkeypatch.setattr("hermes_cli.main.select_provider_and_model", _fail_picker)
monkeypatch.setattr("builtins.input", lambda *a, **k: "n")
assert shell._offer_first_run_setup() is False
def test_offer_first_run_setup_picker_cancel_is_graceful(monkeypatch):
cli = _import_cli()
shell = _make_shell(cli, monkeypatch)
def _cancel_picker():
raise KeyboardInterrupt()
monkeypatch.setattr("hermes_cli.main.select_provider_and_model", _cancel_picker)
monkeypatch.setattr("builtins.input", lambda *a, **k: "")
# Empty answer defaults to yes -> picker runs -> cancels -> False, no raise.
assert shell._offer_first_run_setup() is False
# ---------------------------------------------------------------------------
# Provider-aware empty-key error (replaces the OpenRouter-specific one)
# ---------------------------------------------------------------------------
def test_empty_key_error_names_actual_provider(monkeypatch, capsys):
cli = _import_cli()
monkeypatch.setattr(
"hermes_cli.runtime_provider.resolve_runtime_provider",
lambda **kw: {
"provider": "fireworks",
"api_key": "",
"base_url": "https://api.fireworks.ai/inference/v1/extra",
"source": "env/config",
},
)
shell = _make_shell(cli, monkeypatch)
# A custom base_url would get the no-key placeholder; force the
# openrouter-shaped branch by pointing base_url at openrouter.
monkeypatch.setattr(
"hermes_cli.runtime_provider.resolve_runtime_provider",
lambda **kw: {
"provider": "fireworks",
"api_key": "",
"base_url": "https://openrouter.ai/api/v1",
"source": "env/config",
},
)
capsys.readouterr()
assert shell._ensure_runtime_credentials() is False
out = capsys.readouterr().out
assert "fireworks" in out
assert "OPENROUTER_API_KEY" not in out
assert "hermes model" in out or "hermes setup" in out