The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
test: verify per-admission API usage on the ordinary daemon
Teknium35d9eb2f7024
diff --git a/tests/gateway/fixtures/api_usage_peer.py b/tests/gateway/fixtures/api_usage_peer.pynew file mode 100644index 00000000000..5baeb77f9ac--- /dev/null+++ b/tests/gateway/fixtures/api_usage_peer.py@@ -0,0 +1,27 @@+"""Loopback peer with explicit SSE usage for per-admission accounting probes."""+import json++from tests.gateway.fixtures.shared_authority_peer import ModelPeer+++class UsageModelPeer(ModelPeer):+ def do_POST(self):+ output = self.wfile++ class StreamUsage:+ def write(self, data):+ if data == b'data: [DONE]\n\n':+ chunk = {'id': 'chatcmpl-local', 'object': 'chat.completion.chunk',+ 'model': 'local-wire-stub', 'created': 1, 'choices': [],+ 'usage': {'prompt_tokens': 10, 'completion_tokens': 5, 'total_tokens': 15}}+ output.write(('data: ' + json.dumps(chunk) + '\n\n').encode())+ return output.write(data)++ def flush(self):+ output.flush()++ self.wfile = StreamUsage()+ try:+ super().do_POST()+ finally:+ self.wfile = outputdiff --git a/tests/gateway/test_api_authority_daemon.py b/tests/gateway/test_api_authority_daemon.pyindex 8a4e50ac0dd..d8a32ba27c9 100644--- a/tests/gateway/test_api_authority_daemon.py+++ b/tests/gateway/test_api_authority_daemon.py@@ -11,7 +11,7 @@ import threadingimport aiohttpfrom tests.gateway.fixtures.local_recovery_probe import daemon, websocket, rpc-from tests.gateway.fixtures.shared_authority_peer import ModelPeer+from tests.gateway.fixtures.api_usage_peer import UsageModelPeerdef test_ordinary_daemon_api_ws_fifo_retry(tmp_path):@@ -19,7 +19,7 @@ def test_ordinary_daemon_api_ws_fifo_retry(tmp_path):home, user = tmp_path / 'state', tmp_path / 'user'home.mkdir(mode=0o700)user.mkdir()- peer = ThreadingHTTPServer(('127.0.0.1', 0), ModelPeer)+ peer = ThreadingHTTPServer(('127.0.0.1', 0), UsageModelPeer)peer.requests, peer.metadata_requests = [], []peer.blocked, peer.release = threading.Event(), threading.Event()threading.Thread(target=peer.serve_forever, daemon=True).start()@@ -66,7 +66,8 @@ def test_ordinary_daemon_api_ws_fifo_retry(tmp_path):while not any(row['request_id'] == 'chat:daemon-retry' and row['status'] == 'queued' for row in rows()):await asyncio.sleep(.02)peer.release.set()- await pending+ completed = json.loads(await pending)+ assert completed['usage'] == {'prompt_tokens': 10, 'completion_tokens': 5, 'total_tokens': 15}, completedbefore = len(peer.requests)streamed = await follow(True)assert 'LOCAL_ACK' in streamed and 'data: [DONE]' in streamed, streamed