From 8dcdc3cbc299d09d868556d3ed526b518c9e292c Mon Sep 17 00:00:00 2001 From: LeonSGP43 Date: Sun, 3 May 2026 19:20:57 +0800 Subject: [PATCH] fix(auth): keep Spotify logout from resetting model config --- hermes_cli/auth.py | 21 ++++++++++--- tests/hermes_cli/test_spotify_auth.py | 45 +++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 5 deletions(-) diff --git a/hermes_cli/auth.py b/hermes_cli/auth.py index f0cbf8c256..3fa726d6a7 100644 --- a/hermes_cli/auth.py +++ b/hermes_cli/auth.py @@ -4231,6 +4231,14 @@ def _config_provider_matches(provider_id: Optional[str]) -> bool: return _get_config_provider() == provider_id.strip().lower() +def _should_reset_config_provider_on_logout(provider_id: Optional[str]) -> bool: + """Return True when logout should reset the model provider config.""" + if not provider_id: + return False + normalized = provider_id.strip().lower() + return normalized in PROVIDER_REGISTRY and _config_provider_matches(normalized) + + def _logout_default_provider_from_config() -> Optional[str]: """Fallback logout target when auth.json has no active provider. @@ -5316,15 +5324,18 @@ def logout_command(args) -> None: print("No provider is currently logged in.") return - config_matches = _config_provider_matches(target) + should_reset_config = _should_reset_config_provider_on_logout(target) provider_name = get_auth_provider_display_name(target) - if clear_provider_auth(target) or config_matches: - _reset_config_provider() + if clear_provider_auth(target) or should_reset_config: + if should_reset_config: + _reset_config_provider() print(f"Logged out of {provider_name}.") - if os.getenv("OPENROUTER_API_KEY"): + if should_reset_config and os.getenv("OPENROUTER_API_KEY"): print("Hermes will use OpenRouter for inference.") - else: + elif should_reset_config: print("Run `hermes model` or configure an API key to use Hermes.") + else: + print("Model provider configuration was unchanged.") else: print(f"No auth state found for {provider_name}.") diff --git a/tests/hermes_cli/test_spotify_auth.py b/tests/hermes_cli/test_spotify_auth.py index ca9c975601..e5cd548d42 100644 --- a/tests/hermes_cli/test_spotify_auth.py +++ b/tests/hermes_cli/test_spotify_auth.py @@ -88,6 +88,51 @@ def test_auth_spotify_status_command_reports_logged_in(capsys, monkeypatch: pyte assert "client_id: spotify-client" in output +def test_spotify_logout_does_not_reset_model_provider( + tmp_path, + monkeypatch: pytest.MonkeyPatch, + capsys, +) -> None: + monkeypatch.setenv("HERMES_HOME", str(tmp_path)) + config_path = tmp_path / "config.yaml" + config_path.write_text( + "model:\n" + " default: gemini-3-flash\n" + " provider: custom:local\n" + " base_url: http://localhost:11434/v1\n" + " api_key: ${LOCAL_API_KEY}\n", + encoding="utf-8", + ) + + with auth_mod._auth_store_lock(): + store = auth_mod._load_auth_store() + auth_mod._store_provider_state( + store, + "spotify", + { + "client_id": "spotify-client", + "access_token": "access-token", + "refresh_token": "refresh-token", + "expires_at": "2099-01-01T00:00:00+00:00", + }, + set_active=False, + ) + auth_mod._save_auth_store(store) + + auth_mod.logout_command(SimpleNamespace(provider="spotify")) + + output = capsys.readouterr().out + assert "Logged out of Spotify." in output + assert "Model provider configuration was unchanged." in output + assert auth_mod.get_provider_auth_state("spotify") is None + assert config_path.read_text(encoding="utf-8") == ( + "model:\n" + " default: gemini-3-flash\n" + " provider: custom:local\n" + " base_url: http://localhost:11434/v1\n" + " api_key: ${LOCAL_API_KEY}\n" + ) + def test_spotify_interactive_setup_persists_client_id( tmp_path,