Files
member-portal/tests/test_coop_spend_totals.py
T
inference-admin 1191bcd521 spend: name the unattributed slice of own spend for posterity
own_all_time_spend already includes SpendLogs rows that join to no team alias,
but the remainder was unnamed, so every reader had to notice it by subtraction.
coop_spend_totals() now reports own_unattributed_spend and both payloads
(/admin/overview totals and admin/money-series totals) carry it: the gateway's
own overhead, historically the master-key audio keepalive pings (~$3.21 over
16,894 rows before the 2026-11-28 key split).

History is labelled, never rewritten: LiteLLM_SpendLogs is the system of record
and the portal keeps read-only access, so no team_id is backfilled.
2026-11-28 14:03:47 +00:00

106 lines
3.5 KiB
Python

#!/usr/bin/env python3
"""Regression test: ONE definition of the co-op's all-time spend.
WHY THIS EXISTS
On 2026-11-13 the panel's card said $941.37 and the member dashboard's said
$900.51 — the same claim, two numbers. The panel used the portal's per-team
query; the dashboard summed the chart series IN THE BROWSER, which disagrees
with the server whenever a row is null, capped or stale. coop_spend_totals()
is now the single source, exposed to the panel's boot payload, the money
payload and the broker's metrics.
The split has to be the same split on every surface, and a failure to compute
it must NEVER look like "$0 spent": the safe wrapper returns {} so callers
omit the key instead of printing a zero that reads as fact.
"""
import pathlib
import sys
ROOT = pathlib.Path(__file__).resolve().parent.parent
sys.path.insert(0, str(ROOT))
import app.main as m # noqa: E402
FAILS = []
def check(cond, msg):
print((" ok " if cond else " FAIL ") + msg)
if not cond:
FAILS.append(msg)
class FakeCursor:
def __init__(self, rows):
self._rows = rows
def execute(self, *a, **k):
pass
def fetchall(self):
return self._rows
def __enter__(self):
return self
def __exit__(self, *a):
return False
class FakeConn:
def __init__(self, rows):
self._rows = rows
def cursor(self):
return FakeCursor(self._rows)
def close(self):
pass
ROWS = [
("member:alice@example.org", 10.005),
("member:bob@example.org", 5.005),
("agent:infrastructure-bot", 1.5),
("sandbox:throwaway", 0.02),
("", 1.07), # spend rows with no team at all
]
real_ready, real_connect = m.litellm_db_ready, m._pg_connect
try:
m.litellm_db_ready = lambda: True
m._pg_connect = lambda: FakeConn(ROWS)
t = m.coop_spend_totals()
print("1. the split adds up and covers every row")
check(t["member_all_time_spend"] == 15.01,
f"member side is the member: teams ($10.005 + $5.005 -> 15.01, got {t['member_all_time_spend']})")
check(t["own_all_time_spend"] == 2.59,
f"own side is everything else: agents, sandboxes, unattributed (got {t['own_all_time_spend']})")
check(t["own_unattributed_spend"] == 1.07,
f"rows that join to no team alias are named, not left as a silent remainder (got {t['own_unattributed_spend']})")
check(t["own_unattributed_spend"] <= t["own_all_time_spend"],
"unattributed is a part of own, never added on top of the total")
check(t["spend_all_time"] == round(t["member_all_time_spend"] + t["own_all_time_spend"], 2),
"total == member + own, so no dollar is counted twice or dropped")
check(t["spend_all_time"] == round(sum(r[1] for r in ROWS), 2),
f"total is every SpendLogs row ({round(sum(r[1] for r in ROWS), 2)}), the same universe the money chart sums")
print("2. a failure to compute must not look like $0")
def boom():
raise RuntimeError("LiteLLM DB credentials not configured")
m._pg_connect = boom
check(m._coop_spend_totals_safe() == {},
"the safe wrapper returns {} so callers omit the key instead of showing a fake zero")
m.litellm_db_ready = lambda: False
check(m._coop_spend_totals_safe() == {},
"and it degrades the same way when the DB is simply not configured")
finally:
m.litellm_db_ready, m._pg_connect = real_ready, real_connect
print()
if FAILS:
print(f"coop spend totals: {len(FAILS)} FAILURE(S)")
sys.exit(1)
print("coop spend totals: OK — one definition, one number, no fake zeros")