Analysis: docs/analysis/onboarding_funnel.md (56% of users never add a player; drop happens on the first screen; non-ru/en users convert at 25%). Design: docs/design/onboarding.md. - /start: one message. New user: promise + "send a Lichess username, e.g. DrNykterstein" with [❓ Где взять имя] / [🇬🇧 English] (language switch edits the message in place). Returning user: their players + /today /addgamer /help. - /addgamer asks for the name directly (the two-button menu from 2025-12-03 is gone). - Any non-command text in a private chat is a player-name attempt: no awaiting flag, PicklePersistence removed. onboarding.normalise_username accepts lichess.org URLs, @name, quotes, "add Name"; chess.com and non-name text get their own hints; nothing is sent to Lichess for them. - Every reply ends with the next step (success: hourly report is on, /today, send another name). One no_players empty state for /today, /week, /getgamers, /delgamer, /setperiod, /profile, /year. - /help: 18 lines in three groups; /year and /lang aliases; command menu set from code for en (default) and ru at startup (set_my_commands). - /addtoken trap: non-lip_ text gets a hint instead of "invalid token"; any other command inside the conversation ends it and runs; /cancel outside a conversation answers "nothing to cancel". - All echoed input html-escaped. Terminology unified: игрок / имя на Lichess / отчёт (player / Lichess username / report). - events: user_start screen=, addgamer_input kind= normalised=, addgamer_result format|chesscom, lang_set via=, addtoken_result not_a_token; text in_flow= gone. - i18n: 18 dead keys removed, 21 added; docs (README, USER_GUIDE, about, CURRENT_STATE, CHANGELOG Unreleased) updated; 107 bot tests (was 61). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
137 lines
4.3 KiB
Python
137 lines
4.3 KiB
Python
"""
|
|
Minimal stand-ins for python-telegram-bot's Update / Context and for the bot's
|
|
outgoing side, so handler tests (and the onboarding dry run) can call
|
|
LichessBot methods directly and read the exact outgoing text, parse mode,
|
|
preview flag and inline buttons — no network, no Telegram objects to build.
|
|
|
|
Also a LichessBot factory with the network-facing parts replaced: Lichess
|
|
lookups answer from a dict, the admin notifier and the periodic monitor are
|
|
no-ops, and every events.event() call is recorded for assertions.
|
|
"""
|
|
from tests.bot import _env # noqa: F401 (environment before bot imports)
|
|
|
|
import os
|
|
import tempfile
|
|
from types import SimpleNamespace
|
|
|
|
import bot as bot_module
|
|
from bot import LichessBot
|
|
from database import Database
|
|
|
|
|
|
def buttons(kwargs):
|
|
"""[[(label, callback_data), ...], ...] of the reply_markup in a captured call, or None."""
|
|
markup = kwargs.get('reply_markup')
|
|
if markup is None:
|
|
return None
|
|
return [[(b.text, b.callback_data) for b in row] for row in markup.inline_keyboard]
|
|
|
|
|
|
class FakeMessage:
|
|
def __init__(self, outbox, text=None, photo=None):
|
|
self.text = text
|
|
self.photo = photo
|
|
self._outbox = outbox
|
|
|
|
async def reply_text(self, text, **kwargs):
|
|
self._outbox.append(('text', text, kwargs))
|
|
return FakeMessage(self._outbox, text=text)
|
|
|
|
async def reply_photo(self, photo, **kwargs):
|
|
self._outbox.append(('photo', kwargs.get('caption'), kwargs))
|
|
|
|
async def edit_text(self, text, **kwargs):
|
|
self._outbox.append(('edit', text, kwargs))
|
|
|
|
async def delete(self):
|
|
pass
|
|
|
|
|
|
class FakeQuery:
|
|
def __init__(self, outbox, data, user, message):
|
|
self.data = data
|
|
self.from_user = user
|
|
self.message = message
|
|
self._outbox = outbox
|
|
|
|
async def answer(self):
|
|
pass
|
|
|
|
async def edit_message_text(self, text, **kwargs):
|
|
self._outbox.append(('edit', text, kwargs))
|
|
|
|
|
|
def make_user(uid=1, lang_code='ru'):
|
|
return SimpleNamespace(id=uid, username=f"tg{uid}", first_name="Test", last_name=None, language_code=lang_code)
|
|
|
|
|
|
def make_update(outbox, uid=1, text=None, lang_code='ru', callback=None, photo=False):
|
|
"""A text message (text=...), a non-text message (photo=True) or a callback (callback='data')."""
|
|
user = make_user(uid, lang_code)
|
|
chat = SimpleNamespace(id=uid, type='private')
|
|
message = FakeMessage(outbox, text=text, photo=[object()] if photo else None)
|
|
query = FakeQuery(outbox, callback, user, message) if callback else None
|
|
return SimpleNamespace(effective_user=user, effective_chat=chat, message=message, callback_query=query)
|
|
|
|
|
|
def make_context():
|
|
return SimpleNamespace(user_data={}, args=[])
|
|
|
|
|
|
class FakeLichess:
|
|
"""check_user_exists answers from `accounts` (lowercase name -> dict | False | None); tokens never reach Lichess."""
|
|
|
|
def __init__(self, accounts):
|
|
self.accounts = accounts
|
|
self.calls = []
|
|
|
|
async def check_user_exists(self, username):
|
|
self.calls.append(username)
|
|
return self.accounts.get(username.lower(), False)
|
|
|
|
async def get_user_profile(self, token):
|
|
raise AssertionError("get_user_profile must not be called in this test")
|
|
|
|
|
|
class FakeNotifier:
|
|
async def notify_new_user(self, **kwargs):
|
|
pass
|
|
|
|
async def notify_new_player(self, *args, **kwargs):
|
|
pass
|
|
|
|
|
|
class FakeMonitor:
|
|
application = None
|
|
|
|
async def start_periodic_task(self, *args, **kwargs):
|
|
pass
|
|
|
|
async def stop_periodic_task(self, *args, **kwargs):
|
|
pass
|
|
|
|
|
|
def make_bot(accounts=None, db_path=None):
|
|
"""
|
|
A LichessBot on a fresh temporary DB with the network parts replaced.
|
|
Returns (bot, events): `events` collects every events.event() call made by
|
|
bot.py as (name, fields) while this bot is in use (module-level patch —
|
|
restore with restore_events()).
|
|
"""
|
|
if db_path is None:
|
|
db_path = os.path.join(tempfile.mkdtemp(prefix="lichess-handlers-"), "bot.db")
|
|
bot = LichessBot()
|
|
bot.db = Database(db_path)
|
|
bot.lichess_api = FakeLichess(accounts or {})
|
|
bot.notifier = FakeNotifier()
|
|
bot.monitor = FakeMonitor()
|
|
events = []
|
|
bot_module.event = lambda _name, **fields: events.append((_name, fields))
|
|
return bot, events
|
|
|
|
|
|
_ORIGINAL_EVENT = bot_module.event
|
|
|
|
|
|
def restore_events():
|
|
bot_module.event = _ORIGINAL_EVENT
|