"""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()