fix(tg): reviewer fixes — error handling, timeouts, db index

- commands.py: try/except TelegramBadRequest around all Bot API calls (#2);
  /new handles "topics limit" with user-friendly message (#4)
- start.py: isolate _check_and_prune_stale_topics with try/except Exception (#3)
- message.py: asyncio.timeout(30) around stream_message; handle TimeoutError (#6)
- db.py: add idx_chats_user_id index in init_db() (#7)
- settings.py: remove dead active_chat_id variable (#8)
- tests: add test_message.py (stream error/success); add 2 tests in test_commands.py
  (topics limit, /archive in General topic)
This commit is contained in:
Mikhail Putilovskij 2026-04-02 13:44:59 +03:00
parent c95360ce1f
commit 8901e60f6a
7 changed files with 161 additions and 25 deletions

View file

@ -5,6 +5,7 @@ from types import SimpleNamespace
from unittest.mock import AsyncMock, MagicMock
import pytest
from aiogram.exceptions import TelegramBadRequest
@pytest.fixture(autouse=True)
@ -74,3 +75,28 @@ async def test_cmd_rename_updates_db_and_topic(fresh_db, monkeypatch):
chat_id=100, message_thread_id=42, name="Работа"
)
assert fresh_db.get_chat(1, 42)["chat_name"] == "Работа"
async def test_cmd_new_topics_limit(fresh_db):
"""When Telegram returns topics limit error, user gets a friendly message."""
import adapter.telegram.handlers.commands as mod
importlib.reload(mod)
msg = make_message(user_id=1, thread_id=42, chat_id=100)
msg.bot.create_forum_topic = AsyncMock(
side_effect=TelegramBadRequest(method=MagicMock(), message="topics limit exceeded")
)
await mod.cmd_new(msg)
msg.answer.assert_called_once()
assert "лимит" in msg.answer.call_args[0][0]
# No chat should be created
assert fresh_db.count_active_chats(1) == 0
async def test_cmd_archive_general_topic(fresh_db):
"""/archive in General topic (thread_id=None) replies with 'not found'."""
import adapter.telegram.handlers.commands as mod
importlib.reload(mod)
msg = make_message(user_id=1, thread_id=None, chat_id=100)
await mod.cmd_archive(msg)
msg.answer.assert_called_once()
msg.bot.close_forum_topic.assert_not_called()