forked from sass/tipibot
Adds economy.get_economy_stats() (single-scan total coins, house balance, player-held coins, player count) and a "Rahavaru" field to /status, so admins can see whether the sinks are keeping pace with minted income. Also removed a duplicate bot_admin_check import. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_013VbAVsrZuYesea99mPMmPT
78 lines
3.0 KiB
Python
78 lines
3.0 KiB
Python
"""Tests for leaderboard queries, incl. the single-scan combined builder."""
|
|
|
|
from core import economy, pb_client
|
|
|
|
from conftest import run
|
|
|
|
|
|
def _seed(fake_pb, n: int) -> None:
|
|
for i in range(1, n + 1):
|
|
run(economy.get_user(1000 + i))
|
|
rec = fake_pb.record_for(1000 + i)
|
|
rec["balance"] = i * 100
|
|
rec["exp"] = i * 50
|
|
rec["season_total_exp"] = i * 10
|
|
rec["prestige_level"] = i % 3
|
|
rec["prestige_points"] = i
|
|
rec["total_wagered"] = i * 7
|
|
rec["total_fish_caught"] = n - i # inverse order, to catch sort mistakes
|
|
|
|
|
|
class TestGetAllLeaderboards:
|
|
def test_matches_individual_queries(self, fake_pb):
|
|
_seed(fake_pb, 5)
|
|
combined = run(economy.get_all_leaderboards())
|
|
assert combined["coins"] == run(economy.get_leaderboard(top_n=None))
|
|
assert combined["exp"] == run(economy.get_leaderboard_exp(top_n=None))
|
|
assert combined["season"] == run(economy.get_leaderboard_season_exp(top_n=None))
|
|
assert combined["prestige"] == run(economy.get_leaderboard_prestige(top_n=None))
|
|
assert combined["wagered"] == run(economy.get_leaderboard_wagered(top_n=None))
|
|
assert combined["fish"] == run(economy.get_leaderboard_fish(top_n=None))
|
|
|
|
def test_scans_collection_once(self, fake_pb, monkeypatch):
|
|
_seed(fake_pb, 3)
|
|
calls = {"n": 0}
|
|
real = pb_client.list_all_records
|
|
|
|
async def counting():
|
|
calls["n"] += 1
|
|
return await real()
|
|
|
|
monkeypatch.setattr(pb_client, "list_all_records", counting)
|
|
run(economy.get_all_leaderboards())
|
|
assert calls["n"] == 1 # not six
|
|
|
|
def test_fish_sorted_descending(self, fake_pb):
|
|
_seed(fake_pb, 4)
|
|
fish = run(economy.get_all_leaderboards())["fish"]
|
|
counts = [c for _, c in fish]
|
|
assert counts == sorted(counts, reverse=True)
|
|
|
|
|
|
class TestEconomyStats:
|
|
def test_totals_split_house_from_players(self, fake_pb, monkeypatch):
|
|
monkeypatch.setattr(economy.house, "HOUSE_ID", 999)
|
|
monkeypatch.setattr(economy.house, "_house_pb_id", None)
|
|
# three players + the house
|
|
for uid, bal in [(1, 100), (2, 250), (3, 0)]:
|
|
run(economy.get_user(uid))
|
|
fake_pb.record_for(uid)["balance"] = bal
|
|
run(economy.get_user(999))
|
|
fake_pb.record_for(999)["balance"] = 5000
|
|
|
|
stats = run(economy.get_economy_stats())
|
|
assert stats["total_coins"] == 100 + 250 + 0 + 5000
|
|
assert stats["house_balance"] == 5000
|
|
assert stats["player_coins"] == 350
|
|
assert stats["player_count"] == 3 # house excluded
|
|
|
|
def test_no_house_configured(self, fake_pb, monkeypatch):
|
|
monkeypatch.setattr(economy.house, "HOUSE_ID", None)
|
|
run(economy.get_user(1))
|
|
fake_pb.record_for(1)["balance"] = 42
|
|
stats = run(economy.get_economy_stats())
|
|
assert stats["total_coins"] == 42
|
|
assert stats["house_balance"] == 0
|
|
assert stats["player_coins"] == 42
|
|
assert stats["player_count"] == 1
|