From 32516645133f0c668588fb4e6a1093a30c604ac3 Mon Sep 17 00:00:00 2001 From: 9qeklajc Date: Wed, 20 May 2026 22:55:35 +0200 Subject: [PATCH] use correct var to report balance info --- routstr/balance.py | 2 +- .../integration/test_insufficient_balance.py | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/routstr/balance.py b/routstr/balance.py index 05e91609..b3487826 100644 --- a/routstr/balance.py +++ b/routstr/balance.py @@ -45,7 +45,7 @@ async def get_balance_info(key: ApiKey, session: AsyncSession) -> dict: billing_key = await get_billing_key(key, session) info = { "api_key": "sk-" + key.hashed_key, - "balance": billing_key.balance, + "balance": billing_key.total_balance, "reserved": billing_key.reserved_balance, "is_child": key.parent_key_hash is not None, "parent_key": "sk-" + key.parent_key_hash if key.parent_key_hash else None, diff --git a/tests/integration/test_insufficient_balance.py b/tests/integration/test_insufficient_balance.py index 489b1dbf..11860327 100644 --- a/tests/integration/test_insufficient_balance.py +++ b/tests/integration/test_insufficient_balance.py @@ -124,6 +124,40 @@ async def test_pay_for_request_raises_402_when_all_balance_reserved( assert key.reserved_balance == 50_000 +@pytest.mark.asyncio +async def test_balance_info_matches_chat_available_balance( + integration_client: AsyncClient, + integration_session: AsyncSession, +) -> None: + """ + Regression for /v1/balance/info showing gross funds while chat admission + rejects with a negative available balance. + """ + from routstr.auth import pay_for_request + + key = _key(balance=4_404_339, reserved=4_410_636) + integration_session.add(key) + await integration_session.commit() + + response = await integration_client.get( + "/v1/balance/info", + headers={"Authorization": f"Bearer sk-{key.hashed_key}"}, + ) + + assert response.status_code == 200 + body = response.json() + assert body["balance"] == -6_297 + assert body["reserved"] == 4_410_636 + + with pytest.raises(HTTPException) as exc_info: + await pay_for_request(key, 1, integration_session) + + assert exc_info.value.status_code == 402 + detail = exc_info.value.detail + assert isinstance(detail, dict) + assert "-6297 available" in detail["error"]["message"] + + # --------------------------------------------------------------------------- # Test 4 — balance just one msat below model cost # ---------------------------------------------------------------------------