Files
hermes-agent/tests/agent/test_usage_nous_section.py
Siddharth Balyan b2c6bc4e1e fix(usage): provider limits first, Nous balance apart
Squashed onto main (#151819). The session.usage conflict with #141859 keeps the
branch side: _usage_nous_fields already reads the Nous fields inside the
session profile scope.

main's #141859 test now stubs both Portal reads: a session off a Nous route reads
with the stored token only (stored_token_account_info).

agent.account_usage keeps the names that moved to agent.account_usage_nous (and
BP_FULL): plugin_api.json publishes the whole module, and catalog plugins import
build_credits_view and build_nous_credits_snapshot from it.
2026-11-15 08:20:26 +05:30

52 lines
2.4 KiB
Python

"""``/usage`` shows the active provider's block, then the Nous balance whenever Nous is signed in.
On a Claude session the Nous balance printed inside the Claude block with "/topup to add to your
balance", which read as a Claude balance. Hiding it lost a balance the user holds, so the Nous
section now always follows a signed-in profile's provider block: off a Nous route it has its own
heading, no top-up nudge, and is read with the stored token only (a /usage read never refreshes
the Nous credential the session is not running on).
"""
from __future__ import annotations
import time
from datetime import UTC, datetime, timedelta
import pytest
from agent.account_usage_nous import NOUS_OTHER_ROUTE_HEADING, nous_usage_lines
from tests.hermes_cli.test_nous_account import _account_payload, _jwt
_PAYLOAD = _account_payload(allowed=True, subscription=None, subscription_credits=0, purchased_credits=7.75)
@pytest.fixture
def portal(monkeypatch):
"""A signed-in profile whose portal answers ``_PAYLOAD``; records each account fetch's token and any refresh."""
seen = {"tokens": [], "refreshed": 0}
state = {"access_token": _jwt({"sub": "u", "exp": int(time.time()) + 900}), "client_id": "hermes-cli",
"portal_base_url": "https://portal.example.test",
"expires_at": (datetime.now(UTC) + timedelta(minutes=15)).isoformat()}
def refresh(**kw):
seen["refreshed"] += 1
return state["access_token"]
def fetch(token, base=None):
seen["tokens"].append(token)
return _PAYLOAD
monkeypatch.setattr("hermes_cli.auth.get_provider_auth_state", lambda provider: state if seen.get("signed_in", True) else {})
monkeypatch.setattr("hermes_cli.auth.resolve_nous_access_token", refresh)
monkeypatch.setattr("hermes_cli.nous_account._fetch_nous_account_info", fetch)
monkeypatch.delenv("HERMES_DEV_CREDITS_FIXTURE", raising=False)
return seen, state
def test_another_providers_session_gets_the_labelled_nous_balance_without_topup_actions(portal):
seen, state = portal
lines = nous_usage_lines(nous_route=False)
assert lines[0] == NOUS_OTHER_ROUTE_HEADING and "Top-up credits: $7.75" in lines
assert not any("/topup" in line or line.startswith("Top up:") for line in lines), lines
assert seen == {"tokens": [state["access_token"]], "refreshed": 0} # the stored token, never refreshed
assert "(or run /topup)" in nous_usage_lines(nous_route=True)