Skip to content

The agent that grows with you

test(picker): prove the usage refresh is off-path with an event, not a 5 s clock

Teknium97996be99411
diff --git a/tests/hermes_cli/test_model_options_account_usage.py b/tests/hermes_cli/test_model_options_account_usage.py
index c920d16333d..9ade19743ec 100644
--- a/tests/hermes_cli/test_model_options_account_usage.py
+++ b/tests/hermes_cli/test_model_options_account_usage.py
@@ -5,13 +5,13 @@ refresh in the background: a slow or hung usage endpoint can't be allowed to sta
"""
import threading
-import time
from datetime import datetime, timedelta, timezone, UTC
from agent import account_usage
from agent.account_usage import AccountUsageSnapshot, AccountUsageWindow
from agent.account_usage_cache import cached_account_usage
from hermes_cli.inventory import build_model_options_payload, load_picker_context
+from tests.waiting import wait_until
def _snapshot(used: float) -> AccountUsageSnapshot:
@@ -23,27 +23,29 @@ def _snapshot(used: float) -> AccountUsageSnapshot:
def test_picker_reads_cached_usage_and_refreshes_without_waiting(monkeypatch):
monkeypatch.setenv("OPENROUTER_API_KEY", "test-key-123")
- release, called = threading.Event(), threading.Event()
+ release, called, answered = threading.Event(), threading.Event(), threading.Event()
answers = iter([_snapshot(92.0)])
def slow_usage_api(_base_url, _api_key):
called.set()
- release.wait(10)
+ release.wait(30)
+ answered.set()
return next(answers)
monkeypatch.setitem(account_usage._USAGE_FETCHERS, "openrouter", slow_usage_api)
ctx = load_picker_context()
- started = time.monotonic()
+ # The usage API answers only after ``release``, so a picker that waited on it could not return
+ # before ``answered``. No wall-clock bound: the picker's own cold work (catalogs, credential
+ # probes such as ``gh auth token``) takes seconds on a loaded runner and is not this contract.
first = next(r for r in build_model_options_payload(ctx)["providers"] if r["slug"] == "openrouter")
- assert time.monotonic() - started < 5, "the picker waited on the usage API"
+ assert not answered.is_set(), "the picker waited on the usage API"
assert "usage" not in first, "nothing cached yet, so nothing to show"
- assert called.wait(5), "opening the picker must start a background usage refresh"
+ assert called.wait(30), "opening the picker must start a background usage refresh"
release.set()
- deadline = time.monotonic() + 5
- while cached_account_usage("openrouter") is None and time.monotonic() < deadline:
- time.sleep(0.05)
+ wait_until(lambda: cached_account_usage("openrouter") is not None, timeout=30,
+ what="the background refresh cached the usage snapshot")
row = next(r for r in build_model_options_payload(ctx)["providers"] if r["slug"] == "openrouter")
(window,) = row["usage"]["windows"]