hermes-agent/tests/gateway/test_notice_rendering.py
Siddharth Balyan 7ba5df0d52
feat(billing): /credits command — balance + portal top-up handoff (#44776)
* feat(billing): /usage → portal top-up browser handoff

Add the terminal side of the billing slice (phase 2a): start a top-up by
throwing the user to the portal billing page with the top-up modal open. The
terminal does not confirm, poll, or track payment — checkout completes in the
browser and the next /usage shows the new balance.

- nous_account.py: parse organisation.slug/name from /api/oauth/account into
  NousPortalAccountInfo; add nous_portal_topup_url() building the org-pinned
  {base}/orgs/{slug}/billing?topup=open with a null-slug fallback to the legacy
  {base}/billing?topup=open (never /orgs/None/...).
- portal_cli.py: 'hermes portal topup' — fresh account fetch, identity line
  (Topping up as <email> / org <name>), browser open with printed-URL fallback,
  no-wait closing copy. No polling/confirmation (deferred to 2b).
- account_usage.py: the shared /usage credits block now links the org-pinned
  top-up URL (auto-opens the modal) + points to the command.

Depends on NAS #409 (organisation.slug/name + ?topup=open). Do not merge until
that is live on the target env; until then /api/oauth/account returns
organisation: { id } only and the URL falls back to legacy.

* feat(billing): /credits command for balance + top-up handoff

Replace the standalone `hermes portal topup` subcommand with an in-session
/credits slash command — a focused money surface (balance in, top-up out) that
works in the CLI, TUI, and every messaging platform from one registry entry.

- commands.py: register /credits (Info category). Slack is at its 50-slash cap,
  so /credits is routed via /hermes credits on Slack only (new
  _SLACK_VIA_HERMES_ONLY set) to avoid clamping a canonical command off the
  native list and breaking Telegram parity; native everywhere else.
- account_usage.py: build_credits_view() — one portal fetch → balance lines +
  identity line + org-pinned top-up URL + depleted flag, consumed by all
  surfaces. Reuses the same snapshot/URL builder as /usage so numbers match.
- cli.py: _show_credits() — balance block + identity line + 3-button panel
  (Open top-up / Copy link / Cancel) via the existing prompt_toolkit modal.
  ASK, never auto-launch; headless falls back to printing the URL.
- gateway/slash_commands.py: _handle_credits_command() — renders the block +
  tappable top-up URL + no-wait copy; works on button and plain-text platforms.
- /usage credits line now points to /credits.
- Retire `hermes portal topup` (portal_cli.py back to baseline); the engine
  (slug/name parse + nous_portal_topup_url) stays as the shared core.

No polling, no payment confirmation (billing phase 2a). Depends on NAS #409.

* fix(credits): /credits works in the TUI slash-worker (non-interactive)

In the TUI, /credits runs in the slash-worker subprocess where there is no
live prompt_toolkit app and stdin is the JSON-RPC pipe. _show_credits called
the 3-button modal unconditionally, which fell back to reading stdin →
exception → slash.exec rejected → the command produced no output (only the
pre-existing 'Credit access paused' banner showed).

- _show_credits: when self._app is None (TUI worker / piped / non-interactive),
  render the text variant — balance block + tappable top-up URL + no-wait line,
  same affordance as the messaging surfaces — and skip the modal entirely. The
  3-button panel still renders in the interactive CLI.
- Depleted banner copy: 'run /usage for balance' → 'run /credits to top up'
  now that /credits is the dedicated money surface (+ tests).
- Regression tests: _show_credits with self._app=None renders text and never
  invokes the modal; logged-out path.

* feat(tui): credits.view RPC for the /credits tappable top-up button

Add a credits.view JSON-RPC method returning the structured CreditsView
(logged_in, balance_lines, identity_line, topup_url, depleted) so the TUI can
render a clickable <Link> top-up button instead of plain text. Account-
independent (portal fetch gated on a logged-in Nous account), fail-open to
{logged_in: false} on any hiccup. Mirrors session.usage's credits-block pattern.

Frontend (TUI-local /credits command + Ink component) lands separately.

* feat(tui): /credits command with keyboard-driven top-up confirm

TUI-local /credits: fetches the structured balance via the credits.view RPC,
prints the balance + identity + top-up URL, then arms the EXISTING confirm
overlay (Enter = open top-up in browser via openExternalUrl, Esc = cancel).
Reuses ConfirmReq — no new overlay component/state/input handler. Headless
(openExternalUrl returns false) falls back to printing the URL.

- gatewayTypes.ts: CreditsViewResponse.
- commands/credits.ts: the command (mirrors /status's rpc+guarded pattern).
- registry.ts: register creditsCommands.
- test: balance+overlay armed, headless fallback, no-url, logged-out (4 cases).

Matches the CLI /credits 'Enter to open' affordance. Phase 2a: no polling.
2026-06-12 08:51:10 +00:00

174 lines
6.9 KiB
Python

"""Unit tests for messaging-gateway credit-notice rendering.
Covers render_notice_line — the pure helper that turns an AgentNotice into the
single plaintext line pushed standalone over a messaging platform (no status
bar, unlike the TUI). Behavior contracts, not data snapshots.
"""
from agent.credits_tracker import AgentNotice
from gateway.run import render_notice_line
class TestRenderNoticeLine:
"""render_notice_line emits the notice text VERBATIM.
The notice policy already bakes the level glyph (⚠ / • / ✕ / ✓) into the
text, and the TUI + CLI REPL render it as-is — so messaging must NOT add a
second glyph, which would double it ("⚠ ⚠ Credits 90% used", "⛔ ✕ Credit
access paused").
"""
def test_returns_text_verbatim_with_its_baked_glyph(self):
assert (
render_notice_line(AgentNotice(text="⚠ Credits 90% used · $20.00 cap", level="warn"))
== "⚠ Credits 90% used · $20.00 cap"
)
assert (
render_notice_line(AgentNotice(text="• Grant spent · $5.00 top-up left", level="info"))
== "• Grant spent · $5.00 top-up left"
)
assert (
render_notice_line(
AgentNotice(text="✕ Credit access paused · run /credits to top up", level="error")
)
== "✕ Credit access paused · run /credits to top up"
)
def test_does_not_prepend_a_second_glyph(self):
# Regression: the text already carries its glyph; the level must not add
# another (the bug produced "⚠ ⚠ …" / "⛔ ✕ …").
line = render_notice_line(AgentNotice(text="⚠ Credits 90% used", level="warn"))
assert line == "⚠ Credits 90% used"
assert "⚠ ⚠" not in line
def test_text_is_stripped(self):
assert render_notice_line(AgentNotice(text=" ⚠ padded ", level="warn")) == "⚠ padded"
def test_empty_text_returns_empty_string(self):
# Empty/whitespace → "" → the callback suppresses the push. Fail-soft.
assert render_notice_line(AgentNotice(text="", level="warn")) == ""
assert render_notice_line(AgentNotice(text=" ", level="warn")) == ""
def test_malformed_notice_does_not_raise(self):
# Duck-typed: a stand-in lacking the expected attrs degrades to "".
class _Bare:
pass
assert render_notice_line(_Bare()) == ""
def test_real_policy_notices_render_without_doubling():
"""End-to-end regression: every notice evaluate_credits_notices emits already
carries its glyph, so render_notice_line must return it unchanged (no second
glyph prepended) for the messaging push."""
from agent.credits_tracker import CreditsState, evaluate_credits_notices
def _emitted(uf=None, paid=True, purchased=0):
latch = {"active": set(), "seen_below_90": True, "usage_band": None}
if uf is None:
st = CreditsState(
subscription_limit_micros=None, subscription_micros=0,
denominator_kind="none", paid_access=paid,
purchased_micros=purchased, purchased_usd="%.2f" % (purchased / 1e6),
)
else:
lim = 20_000_000
st = CreditsState(
subscription_limit_micros=lim, subscription_limit_usd="20.00",
subscription_micros=int(lim * (1 - uf)), denominator_kind="subscription_cap",
paid_access=paid, purchased_micros=purchased,
purchased_usd="%.2f" % (purchased / 1e6),
)
show, _ = evaluate_credits_notices(st, latch)
return show
notices = (
_emitted(uf=0.9) # band 90 (warn)
+ _emitted(uf=0.5) # band 50 (info)
+ _emitted(uf=1.0, purchased=5_000_000) # band 90 + grant_spent
+ _emitted(uf=None, paid=False) # depleted
)
assert notices, "policy produced no notices to check"
for n in notices:
assert render_notice_line(n) == n.text # verbatim — no prepended glyph
# ── Delivery seam: a rendered notice line goes out via _deliver_platform_notice ──
import threading
from unittest.mock import AsyncMock, MagicMock
import pytest
def _make_source(platform_value="telegram", chat_id="555", user_id="u1"):
src = MagicMock()
plat = MagicMock()
plat.value = platform_value
src.platform = plat
src.chat_id = chat_id
src.user_id = user_id
return src
def _make_runner_with_adapter(source, adapter):
from gateway.run import GatewayRunner
runner = object.__new__(GatewayRunner)
runner.adapters = {source.platform: adapter}
runner.config = MagicMock()
runner.config.get_notice_delivery = MagicMock(return_value="public")
runner._thread_metadata_for_source = MagicMock(return_value={"thread": "t"})
return runner
class TestDeliverNoticeLine:
"""The seam between render_notice_line and the platform adapter.
Proves a rendered credit-notice line reaches adapter.send (public) /
send_private_notice (private) through the shared _deliver_platform_notice
rail — the path the gateway notice_callback schedules onto the loop.
"""
@pytest.mark.asyncio
async def test_public_delivery_sends_rendered_line(self):
source = _make_source()
adapter = MagicMock()
adapter.send = AsyncMock(return_value=MagicMock(success=True))
runner = _make_runner_with_adapter(source, adapter)
line = render_notice_line(
AgentNotice(text="⚠ Credits 90% used · $20.00 cap", level="warn")
)
await runner._deliver_platform_notice(source, line)
adapter.send.assert_awaited_once()
args, kwargs = adapter.send.call_args
assert args[0] == "555"
# Delivered verbatim — the policy's single glyph, not a doubled one.
assert args[1] == "⚠ Credits 90% used · $20.00 cap"
@pytest.mark.asyncio
async def test_private_delivery_prefers_private_notice(self):
source = _make_source()
adapter = MagicMock()
adapter.send = AsyncMock(return_value=MagicMock(success=True))
adapter.send_private_notice = AsyncMock(return_value=MagicMock(success=True))
runner = _make_runner_with_adapter(source, adapter)
runner.config.get_notice_delivery = MagicMock(return_value="private")
line = render_notice_line(
AgentNotice(text="✓ Credit access restored", level="success")
)
await runner._deliver_platform_notice(source, line)
adapter.send_private_notice.assert_awaited_once()
adapter.send.assert_not_awaited()
@pytest.mark.asyncio
async def test_no_adapter_is_a_noop(self):
source = _make_source()
runner = object.__new__(__import__("gateway.run", fromlist=["GatewayRunner"]).GatewayRunner)
runner.adapters = {}
# Must not raise when the platform has no registered adapter.
await runner._deliver_platform_notice(source, "• anything")