LichessStatTgWeb/tests/bot/test_periodic_decisions.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

87 lines
4.6 KiB
Python

"""PeriodicMonitor's pure decision helpers: _decide_skip, _skip_window_end, _is_stale_backlog."""
from tests.bot import _env # noqa: F401 (environment before bot imports)
import unittest
from datetime import datetime, timedelta
from periodic import PeriodicMonitor
NOW = 1_800_000_000.0
SINCE = NOW - 3600 # a one-hour window that ends about now
def status(**overrides):
base = {'disabled': False, 'not_found': False, 'seen_at': None,
'games_total': 100, 'checked_at': NOW - 120, 'missing_count': 0}
base.update(overrides)
return base
class DecideSkipTests(unittest.TestCase):
def check(self, expected, st, pair_state=None, has_token=False, since_ts=SINCE):
if pair_state is None:
pair_state = {'last_games_total': 100, 'closed_notice_sent': False}
self.assertEqual(expected, PeriodicMonitor._decide_skip(st, pair_state, NOW, has_token, since_ts))
def test_table(self):
stale_age = PeriodicMonitor.STATUS_MAX_AGE_SECONDS + 1
margin = PeriodicMonitor.STATUS_SAFETY_MARGIN_SECONDS
cases = [
# (expected, status, pair_state, has_token, since_ts)
('request', None, None, False, SINCE), # never refreshed
('request', status(checked_at=None), None, False, SINCE), # no checked_at
('request', status(checked_at=NOW - stale_age), None, False, SINCE), # stale status
# status refreshed at/just after the start of a 20-minute window
('request', status(checked_at=NOW - 1200 + margin), None, False, NOW - 1200), # predates the window
('skip_unchanged', status(checked_at=NOW - 1200 + margin + 1), None, False, NOW - 1200),
('skip_closed', status(disabled=True), None, False, SINCE), # closed account
('skip_closed', status(not_found=True), None, False, SINCE), # account gone
('skip_closed', status(disabled=True), None, True, SINCE), # closed beats token
('skip_closed', status(not_found=True, games_total=None), None, False, SINCE),
('request', status(), None, True, SINCE), # token pair: puzzles
('request', status(), {'last_games_total': None, 'closed_notice_sent': False}, False, SINCE), # no baseline
('request', status(), {}, False, SINCE), # empty pair state
('request', status(games_total=None), None, False, SINCE), # no total in status
('skip_unchanged', status(games_total=100), None, False, SINCE), # unchanged
('request', status(games_total=101), None, False, SINCE), # played
('skip_unchanged', status(), None, False, None), # since unknown: no window check
]
for expected, st, pair_state, has_token, since_ts in cases:
with self.subTest(expected=expected, status=st, pair_state=pair_state, token=has_token):
self.check(expected, st, pair_state, has_token, since_ts)
class SkipWindowEndTests(unittest.TestCase):
def test_capped_at_status_coverage(self):
checked_at = NOW - 600
st = status(checked_at=checked_at)
covered = datetime.fromtimestamp(checked_at - PeriodicMonitor.STATUS_SAFETY_MARGIN_SECONDS)
later = datetime.fromtimestamp(NOW)
earlier = datetime.fromtimestamp(NOW - 3000)
self.assertEqual(covered, PeriodicMonitor._skip_window_end(later, st))
self.assertEqual(earlier, PeriodicMonitor._skip_window_end(earlier, st))
class StaleBacklogTests(unittest.TestCase):
def test_table(self):
h = timedelta(hours=1)
cases = [
# (window, period_minutes, expected)
(h, 60, False), # on schedule
(3 * h, 60, False), # exactly period + 2h: not stale
(3 * h + timedelta(seconds=1), 60, True), # just over
(timedelta(minutes=15), 15, False),
(timedelta(minutes=15) + 2 * h, 15, False),
(timedelta(minutes=16) + 2 * h, 15, True),
(24 * h, 1440, False), # daily pair, normal window
(26 * h, 1440, False),
(26 * h + timedelta(minutes=1), 1440, True),
(timedelta(days=14), 60, True), # weeks of downtime
]
for window, period, expected in cases:
with self.subTest(window=window, period=period):
self.assertEqual(expected, PeriodicMonitor._is_stale_backlog(window, period))
if __name__ == '__main__':
unittest.main()