mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-30 19:09:28 +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.
168 lines
5.7 KiB
Python
168 lines
5.7 KiB
Python
from __future__ import annotations
|
|
|
|
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from hermes_cli import auth as auth_mod
|
|
from hermes_cli.auth import AuthError, resolve_spotify_runtime_credentials
|
|
|
|
|
|
|
|
|
|
def test_resolve_spotify_runtime_credentials_refreshes_without_changing_active_provider(
|
|
tmp_path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
|
|
|
|
with auth_mod._auth_store_lock():
|
|
store = auth_mod._load_auth_store()
|
|
store["active_provider"] = "nous"
|
|
auth_mod._store_provider_state(
|
|
store,
|
|
"spotify",
|
|
{
|
|
"client_id": "spotify-client",
|
|
"redirect_uri": "http://127.0.0.1:43827/spotify/callback",
|
|
"api_base_url": auth_mod.DEFAULT_SPOTIFY_API_BASE_URL,
|
|
"accounts_base_url": auth_mod.DEFAULT_SPOTIFY_ACCOUNTS_BASE_URL,
|
|
"scope": auth_mod.DEFAULT_SPOTIFY_SCOPE,
|
|
"access_token": "expired-token",
|
|
"refresh_token": "refresh-token",
|
|
"token_type": "Bearer",
|
|
"expires_at": "2000-01-01T00:00:00+00:00",
|
|
},
|
|
set_active=False,
|
|
)
|
|
auth_mod._save_auth_store(store)
|
|
|
|
monkeypatch.setattr(
|
|
auth_mod,
|
|
"_refresh_spotify_oauth_state",
|
|
lambda state, timeout_seconds=20.0: {
|
|
**state,
|
|
"access_token": "fresh-token",
|
|
"expires_at": "2099-01-01T00:00:00+00:00",
|
|
},
|
|
)
|
|
|
|
creds = auth_mod.resolve_spotify_runtime_credentials()
|
|
|
|
assert creds["access_token"] == "fresh-token"
|
|
persisted = auth_mod.get_provider_auth_state("spotify")
|
|
assert persisted is not None
|
|
assert persisted["access_token"] == "fresh-token"
|
|
assert auth_mod.get_active_provider() == "nous"
|
|
|
|
|
|
def test_auth_spotify_status_command_reports_logged_in(capsys, monkeypatch: pytest.MonkeyPatch) -> None:
|
|
monkeypatch.setattr(
|
|
auth_mod,
|
|
"get_auth_status",
|
|
lambda provider=None: {
|
|
"logged_in": True,
|
|
"auth_type": "oauth_pkce",
|
|
"client_id": "spotify-client",
|
|
"redirect_uri": "http://127.0.0.1:43827/spotify/callback",
|
|
"scope": "user-library-read",
|
|
},
|
|
)
|
|
|
|
from hermes_cli.auth_commands import auth_status_command
|
|
|
|
auth_status_command(SimpleNamespace(provider="spotify"))
|
|
output = capsys.readouterr().out
|
|
assert "spotify: logged in" in output
|
|
assert "client_id: spotify-client" in output
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Quarantine: terminal refresh failure clears dead tokens (#28139)
|
|
# ---------------------------------------------------------------------------
|
|
|
|
_STALE_SPOTIFY_STATE = {
|
|
"client_id": "test-client",
|
|
"redirect_uri": "http://127.0.0.1:43827/spotify/callback",
|
|
"api_base_url": auth_mod.DEFAULT_SPOTIFY_API_BASE_URL,
|
|
"accounts_base_url": auth_mod.DEFAULT_SPOTIFY_ACCOUNTS_BASE_URL,
|
|
"scope": auth_mod.DEFAULT_SPOTIFY_SCOPE,
|
|
"granted_scope": auth_mod.DEFAULT_SPOTIFY_SCOPE,
|
|
"token_type": "Bearer",
|
|
"access_token": "dead-access-token",
|
|
"refresh_token": "dead-refresh-token",
|
|
"expires_at": "2000-01-01T00:00:00+00:00",
|
|
"expires_in": 3600,
|
|
"obtained_at": "2000-01-01T00:00:00+00:00",
|
|
"auth_type": "oauth_pkce",
|
|
}
|
|
|
|
|
|
def _seed_spotify_state(tmp_path, state: dict) -> None:
|
|
with auth_mod._auth_store_lock():
|
|
store = auth_mod._load_auth_store()
|
|
store["active_provider"] = "nous"
|
|
auth_mod._store_provider_state(store, "spotify", state, set_active=False)
|
|
auth_mod._save_auth_store(store)
|
|
|
|
|
|
def test_resolve_credentials_quarantines_dead_tokens_on_terminal_refresh_failure(
|
|
tmp_path,
|
|
monkeypatch: pytest.MonkeyPatch,
|
|
) -> None:
|
|
"""Terminal refresh failure (relogin_required=True + refresh_token present)
|
|
must clear access_token/refresh_token/expires_* from auth.json and write a
|
|
last_auth_error marker so subsequent calls fail fast without a network retry.
|
|
Mirrors Nous / xAI-OAuth / Codex-OAuth / MiniMax quarantine pattern.
|
|
"""
|
|
monkeypatch.setenv("HERMES_HOME", str(tmp_path))
|
|
_seed_spotify_state(tmp_path, dict(_STALE_SPOTIFY_STATE))
|
|
|
|
def _terminal_refresh(_state, **_kw):
|
|
raise AuthError(
|
|
"Spotify token refresh failed. Run `hermes auth spotify` again.",
|
|
provider="spotify",
|
|
code="spotify_refresh_failed",
|
|
relogin_required=True,
|
|
)
|
|
|
|
monkeypatch.setattr(auth_mod, "_refresh_spotify_oauth_state", _terminal_refresh)
|
|
|
|
with pytest.raises(AuthError) as exc_info:
|
|
resolve_spotify_runtime_credentials(force_refresh=True)
|
|
|
|
assert exc_info.value.code == "spotify_refresh_failed"
|
|
assert exc_info.value.relogin_required is True
|
|
|
|
persisted = auth_mod.get_provider_auth_state("spotify")
|
|
assert persisted is not None
|
|
|
|
# Dead OAuth fields must be cleared.
|
|
assert "access_token" not in persisted
|
|
assert "refresh_token" not in persisted
|
|
assert "expires_at" not in persisted
|
|
assert "expires_in" not in persisted
|
|
assert "obtained_at" not in persisted
|
|
|
|
# Non-credential metadata must be preserved.
|
|
assert persisted["client_id"] == "test-client"
|
|
assert persisted["api_base_url"] == auth_mod.DEFAULT_SPOTIFY_API_BASE_URL
|
|
assert persisted["accounts_base_url"] == auth_mod.DEFAULT_SPOTIFY_ACCOUNTS_BASE_URL
|
|
|
|
# Structured diagnostic blob must be written.
|
|
err = persisted.get("last_auth_error")
|
|
assert isinstance(err, dict)
|
|
assert err["provider"] == "spotify"
|
|
assert err["code"] == "spotify_refresh_failed"
|
|
assert err["reason"] == "runtime_refresh_failure"
|
|
assert err["relogin_required"] is True
|
|
assert "at" in err
|
|
|
|
# Active provider must be unchanged.
|
|
assert auth_mod.get_active_provider() == "nous"
|
|
|
|
|