LichessStatTgWeb/tests/bot/test_player_status.py
vrubelroman a7571f13c1
All checks were successful
CI/CD Pipeline / build-and-deploy (push) Successful in 16s
v1.5.1: not-found players paused, tests in repo + CI, log-based daily summary, docs pruned
- players absent from two consecutive bulk answers (deleted/renamed on Lichess) are paused
  like closed accounts with their own notice; ❓ mark; stats service answers 404 instead
  of "0 games" for unknown users
- /getgamers table stays unmarked, a legend line under it lists closed / not-found players
- daily SUMMARY is computed from events.log (survives restarts); lichess_429 comes from the
  stats service /health, which now exposes lichess_429_24h / lichess_429_total
- admin /status reads its numbers through Database.get_admin_stats
- tests/: 36 bot + 15 service unittest cases, run_tests.sh runs them inside the real images,
  CI runs them after the build and before pushing/deploying
- docs: USER_GUIDE/about/API_DOCUMENTATION rewritten for the current code, ARCHITECTURE.md and
  two sub-READMEs removed, 7 obsolete helper scripts removed, CURRENT_STATE updated
- .claude/agents/designer.md: Telegram-aware designer role

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-21 02:48:00 +00:00

92 lines
3.9 KiB
Python

"""PlayerStatusRefresher.refresh_once against a fake stats-service client: present / absent / not_found."""
from tests.bot import _env # noqa: F401 (environment before bot imports)
import asyncio
import os
import tempfile
import unittest
from database import Database
from player_status import PlayerStatusRefresher
class FakeLichessAPI:
"""get_users_status answers from `self.answers` (list per call), None = failure."""
def __init__(self):
self.answers = []
self.calls = []
async def get_users_status(self, usernames):
self.calls.append(list(usernames))
return self.answers.pop(0)
def player(name, games_total=10, disabled=False):
return {'username': name, 'id': name.lower(), 'disabled': disabled, 'seen_at': None, 'games_total': games_total}
class RefreshOnceTests(unittest.TestCase):
def setUp(self):
self.db = Database(os.path.join(tempfile.mkdtemp(), "t.db"))
self.db.add_or_get_telegram_user(1, 'alice', 'Alice', None, 'ru')
self.alpha = self.db.add_gamer('Alpha')
self.gone = self.db.add_gamer('Gone')
for gid in (self.alpha, self.gone):
self.db.add_user_gamer(1, gid)
self.db.set_user_gamer_period(1, gid, 60)
self.api = FakeLichessAPI()
self.refresher = PlayerStatusRefresher(self.db, self.api, notifier=None)
def refresh(self):
return asyncio.run(self.refresher.refresh_once())
def test_absent_twice_flags_not_found_and_presence_clears_it(self):
# 1st refresh: Lichess answers for Alpha only (canonical spelling differs in case)
self.api.answers.append([player('ALPHA', 10)])
result = self.refresh()
self.assertEqual({'tracked': 2, 'answered': 1, 'closed': 0, 'not_found': 0}, {k: result[k] for k in ('tracked', 'answered', 'closed', 'not_found')})
self.assertEqual(sorted(['Alpha', 'Gone']), sorted(self.api.calls[0]))
alpha = self.db.get_gamer_status(self.alpha)
gone = self.db.get_gamer_status(self.gone)
self.assertEqual((10, 0, False), (alpha['games_total'], alpha['missing_count'], alpha['not_found']))
self.assertEqual((None, 1, False), (gone['games_total'], gone['missing_count'], gone['not_found']))
# 2nd refresh: still absent -> not_found
self.api.answers.append([player('Alpha', 11)])
result = self.refresh()
self.assertEqual(1, result['not_found'])
gone = self.db.get_gamer_status(self.gone)
self.assertEqual((2, True, False), (gone['missing_count'], gone['not_found'], gone['disabled']))
self.assertEqual(11, self.db.get_gamer_status(self.alpha)['games_total'])
# 3rd refresh: the name is back (renamed back / Lichess hiccup) -> cleared
self.api.answers.append([player('Alpha', 11), player('Gone', 3)])
result = self.refresh()
self.assertEqual(0, result['not_found'])
gone = self.db.get_gamer_status(self.gone)
self.assertEqual((3, 0, False), (gone['games_total'], gone['missing_count'], gone['not_found']))
def test_failed_chunk_marks_nothing(self):
self.api.answers.append(None)
with self.assertRaises(RuntimeError):
self.refresh()
self.assertIsNone(self.db.get_gamer_status(self.alpha))
self.assertIsNone(self.db.get_gamer_status(self.gone))
def test_closed_account_is_counted_and_found(self):
self.api.answers.append([player('Alpha', 10, disabled=True), player('Gone', 1)])
result = self.refresh()
self.assertEqual((1, 0), (result['closed'], result['not_found']))
alpha = self.db.get_gamer_status(self.alpha)
self.assertTrue(alpha['disabled'])
self.assertFalse(alpha['not_found'])
def test_nothing_tracked(self):
for gid in (self.alpha, self.gone):
self.db.set_user_gamer_period(1, gid, 0)
self.assertEqual(0, self.refresh()['tracked'])
self.assertEqual([], self.api.calls)
if __name__ == '__main__':
unittest.main()