Files
nousresearch--hermes-agent/tests/hermes_cli/test_resolve_provider_openrouter_pool.py
wehub-resource-sync b4fbd6fe9f
Deploy Site / deploy-vercel (push) Has been skipped
Deploy Site / deploy-docs (push) Has been skipped
Build Skills Index / build-index (push) Has been skipped
CI / Deny unrelated histories (push) Has been skipped
CI / Detect affected areas (push) Successful in 27m35s
CI / OSV scan (push) Failing after 4s
CI / Build&Test Docker image (push) Successful in 9s
CI / Supply-chain scan (push) Has been skipped
CI / Lint Docker scripts (push) Failing after 5m13s
CI / Check contributors (push) Failing after 12m8s
CI / Docs Site (push) Failing after 12m8s
CI / TypeScript (push) Failing after 12m8s
CI / Python lints (push) Failing after 12m9s
CI / Python tests (push) Failing after 12m9s
CI / Check uv.lock (push) Failing after 23m22s
CI / CI timing report (push) Has been cancelled
Build Skills Index / trigger-deploy (push) Has been cancelled
CI / All required checks pass (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:56:03 +08:00

77 lines
2.4 KiB
Python

"""Regression tests for issue #42130.
A credential added via `hermes auth add openrouter` lives in the credential
pool, NOT as an OPENROUTER_API_KEY env var. Before the fix, resolve_provider()
auto-detection only checked env vars, so such a credential was invisible:
the provider failed to resolve (AuthError) or resolved without a key, and
requests went out with no Authorization header — OpenRouter's
"HTTP 401: Missing Authentication header".
These tests lock in that auto-detection consults the OpenRouter pool.
"""
import uuid
import pytest
@pytest.fixture(autouse=True)
def _clean_inference_env(monkeypatch):
"""Strip credential-shaped env vars so the pool is the only source."""
for key in (
"OPENROUTER_API_KEY",
"OPENAI_API_KEY",
"ANTHROPIC_API_KEY",
"ANTHROPIC_TOKEN",
"CLAUDE_CODE_OAUTH_TOKEN",
"NOUS_API_KEY",
"HERMES_INFERENCE_PROVIDER",
):
monkeypatch.delenv(key, raising=False)
def _seed_openrouter_pool(token: str = "sk-or-FAKEKEY123") -> None:
"""Mimic `hermes auth add openrouter <token>` — a manual pool entry."""
from agent.credential_pool import (
AUTH_TYPE_API_KEY,
SOURCE_MANUAL,
PooledCredential,
load_pool,
)
pool = load_pool("openrouter")
pool.add_entry(
PooledCredential(
provider="openrouter",
id=uuid.uuid4().hex[:6],
label="api-key-1",
auth_type=AUTH_TYPE_API_KEY,
priority=0,
source=SOURCE_MANUAL,
access_token=token,
base_url="https://openrouter.ai/api/v1",
)
)
def test_auto_detects_openrouter_from_pool(tmp_path, monkeypatch):
"""With only a pool credential (no env var), auto-detection finds it."""
monkeypatch.setenv("HERMES_HOME", str(tmp_path / "hermes"))
(tmp_path / "hermes").mkdir(parents=True, exist_ok=True)
_seed_openrouter_pool()
from hermes_cli.auth import resolve_provider
assert resolve_provider("auto") == "openrouter"
def test_no_credentials_still_raises(tmp_path, monkeypatch):
"""Empty pool + no env var must still fail to resolve — no false positive."""
monkeypatch.setenv("HERMES_HOME", str(tmp_path / "hermes"))
(tmp_path / "hermes").mkdir(parents=True, exist_ok=True)
from hermes_cli.auth import AuthError, resolve_provider
with pytest.raises(AuthError):
resolve_provider("auto")