mirror of
https://github.com/NousResearch/hermes-agent.git
synced 2026-07-08 13:12:08 +00:00
153 lines
4.5 KiB
Python
153 lines
4.5 KiB
Python
from types import SimpleNamespace
|
|
|
|
import pytest
|
|
|
|
from agent import account_usage
|
|
|
|
|
|
class _FakeResponse:
|
|
def __init__(self, payload):
|
|
self._payload = payload
|
|
|
|
def raise_for_status(self):
|
|
return None
|
|
|
|
def json(self):
|
|
return self._payload
|
|
|
|
|
|
class _FakeClient:
|
|
def __init__(self, calls, payload):
|
|
self.calls = calls
|
|
self.payload = payload
|
|
|
|
def __enter__(self):
|
|
return self
|
|
|
|
def __exit__(self, *exc):
|
|
return False
|
|
|
|
def get(self, url, headers):
|
|
self.calls.append({"url": url, "headers": headers})
|
|
return _FakeResponse(self.payload)
|
|
|
|
|
|
@pytest.fixture
|
|
def codex_usage_payload():
|
|
return {
|
|
"plan_type": "plus",
|
|
"rate_limit": {
|
|
"primary_window": {
|
|
"used_percent": 21,
|
|
"reset_at": 1779846359,
|
|
},
|
|
"secondary_window": {
|
|
"used_percent": 4,
|
|
"reset_at": 1780230796,
|
|
},
|
|
},
|
|
"credits": {"has_credits": False},
|
|
}
|
|
|
|
|
|
def test_codex_usage_prefers_explicit_live_agent_credentials(monkeypatch, codex_usage_payload):
|
|
calls = []
|
|
monkeypatch.setattr(
|
|
account_usage.httpx,
|
|
"Client",
|
|
lambda timeout: _FakeClient(calls, codex_usage_payload),
|
|
)
|
|
monkeypatch.setattr(
|
|
account_usage,
|
|
"resolve_codex_runtime_credentials",
|
|
lambda **kwargs: (_ for _ in ()).throw(AssertionError("legacy auth should not be used")),
|
|
)
|
|
|
|
snapshot = account_usage.fetch_account_usage(
|
|
"openai-codex",
|
|
base_url="https://chatgpt.com/backend-api/codex",
|
|
api_key="live-agent-token",
|
|
)
|
|
|
|
assert snapshot is not None
|
|
assert snapshot.provider == "openai-codex"
|
|
assert snapshot.plan == "Plus"
|
|
assert [w.label for w in snapshot.windows] == ["Session", "Weekly"]
|
|
assert snapshot.windows[0].used_percent == 21
|
|
assert calls[0]["url"] == "https://chatgpt.com/backend-api/wham/usage"
|
|
assert calls[0]["headers"]["Authorization"] == "Bearer live-agent-token"
|
|
|
|
|
|
def test_codex_usage_falls_back_to_native_credential_pool(monkeypatch, codex_usage_payload):
|
|
calls = []
|
|
monkeypatch.setattr(
|
|
account_usage.httpx,
|
|
"Client",
|
|
lambda timeout: _FakeClient(calls, codex_usage_payload),
|
|
)
|
|
monkeypatch.setattr(
|
|
account_usage,
|
|
"resolve_codex_runtime_credentials",
|
|
lambda **kwargs: (_ for _ in ()).throw(RuntimeError("no singleton auth")),
|
|
)
|
|
|
|
pool_entry = SimpleNamespace(
|
|
runtime_api_key="pooled-token",
|
|
runtime_base_url="https://chatgpt.com/backend-api/codex",
|
|
)
|
|
pool = SimpleNamespace(select=lambda: pool_entry)
|
|
|
|
import agent.credential_pool as credential_pool
|
|
|
|
monkeypatch.setattr(credential_pool, "load_pool", lambda provider: pool)
|
|
|
|
snapshot = account_usage.fetch_account_usage("openai-codex")
|
|
|
|
assert snapshot is not None
|
|
assert snapshot.windows[0].label == "Session"
|
|
assert snapshot.windows[1].label == "Weekly"
|
|
assert calls[0]["url"] == "https://chatgpt.com/backend-api/wham/usage"
|
|
assert calls[0]["headers"]["Authorization"] == "Bearer pooled-token"
|
|
|
|
|
|
def test_codex_usage_treats_wham_used_percent_as_used_not_remaining(monkeypatch):
|
|
"""ChatGPT UI says "left"; /wham/usage.used_percent is already used."""
|
|
payload = {
|
|
"plan_type": "plus",
|
|
"rate_limit": {
|
|
"primary_window": {
|
|
"used_percent": 85,
|
|
"reset_at": 1779846359,
|
|
},
|
|
"secondary_window": {
|
|
"used_percent": 14,
|
|
"reset_at": 1780230796,
|
|
},
|
|
},
|
|
"credits": {"has_credits": False},
|
|
}
|
|
calls = []
|
|
monkeypatch.setattr(
|
|
account_usage.httpx,
|
|
"Client",
|
|
lambda timeout: _FakeClient(calls, payload),
|
|
)
|
|
monkeypatch.setattr(
|
|
account_usage,
|
|
"resolve_codex_runtime_credentials",
|
|
lambda **kwargs: (_ for _ in ()).throw(AssertionError("explicit auth should be used")),
|
|
)
|
|
|
|
snapshot = account_usage.fetch_account_usage(
|
|
"openai-codex",
|
|
base_url="https://chatgpt.com/backend-api/codex",
|
|
api_key="live-agent-token",
|
|
)
|
|
|
|
assert snapshot is not None
|
|
assert [window.used_percent for window in snapshot.windows] == [85, 14]
|
|
rendered = "\n".join(account_usage.render_account_usage_lines(snapshot, markdown=True))
|
|
assert "85% used" in rendered
|
|
assert "14% used" in rendered
|
|
assert "15% used" not in rendered
|
|
assert "86% used" not in rendered
|