Files
hermes-agent/tests/agent/test_fallback_dynamic_credential.py
T
Teknium b4464f6fac fix: retain rotating credentials during fallback handoff
Adapt the callable-source diagnosis from snipecoder (#102244) and fallback slice from BGwill-OUTLOOK (#102721), without unrelated reasoning or override changes.

Co-authored-by: CloudWishOS <99405975+snipecoder@users.noreply.github.com>\nCo-authored-by: BGwill-OUTLOOK <bgwillwork@outlook.com>
2026-09-07 06:07:16 -07:00

36 lines
1.4 KiB
Python

"""Fallback rebuilds retain the credential source, not its last token."""
from types import SimpleNamespace
import httpx
import pytest
from openai import OpenAI
from agent.client_lifecycle import _swap_fallback_clients
@pytest.mark.parametrize("mode", ["chat_completions", "codex_responses"])
@pytest.mark.parametrize("dynamic", [False, True])
def test_fallback_rebuild_preserves_rotating_and_static_credentials(mode, dynamic):
token = ["first-token"]
source = (lambda: token[0]) if dynamic else token[0]
captured = []
def respond(request):
captured.append(request.headers.get("Authorization"))
return httpx.Response(200, json={"id": "fixture", "choices": []})
client = OpenAI(api_key=source, base_url="http://localhost:1234/v1")
agent = SimpleNamespace()
_swap_fallback_clients(agent, client, "fixture", "fixture", str(client.base_url), mode)
rebuilt = OpenAI(**agent._client_kwargs, http_client=httpx.Client(transport=httpx.MockTransport(respond)))
try:
for value in ("first-token", "rotated-token"):
token[0] = value
rebuilt.chat.completions.create(model="fixture", messages=[])
expected = ["Bearer first-token", "Bearer rotated-token" if dynamic else "Bearer first-token"]
assert captured == expected
assert agent.api_key is source
finally:
client.close()
rebuilt.close()