fix(tg): /archive tries delete_forum_topic, falls back with explanation if API rejects

This commit is contained in:
Mikhail Putilovskij 2026-04-02 15:16:39 +03:00
parent dd5745bf51
commit 8a00d5ac54
2 changed files with 32 additions and 7 deletions

View file

@ -30,6 +30,7 @@ def make_message(*, user_id=1, thread_id=42, chat_id=100, text="/new"):
return_value=SimpleNamespace(message_thread_id=200)
)
m.bot.close_forum_topic = AsyncMock()
m.bot.delete_forum_topic = AsyncMock()
m.bot.edit_forum_topic = AsyncMock()
m.bot.send_message = AsyncMock()
return m
@ -47,14 +48,28 @@ async def test_cmd_new_creates_topic(fresh_db, monkeypatch):
assert fresh_db.get_chat(1, 200) is not None
async def test_cmd_archive_closes_and_archives(fresh_db, monkeypatch):
async def test_cmd_archive_deletes_topic_when_possible(fresh_db, monkeypatch):
"""When delete_forum_topic succeeds (user-created topic), no answer is sent."""
import adapter.telegram.handlers.commands as mod
importlib.reload(mod)
fresh_db.create_chat(1, 42, "Чат #1")
msg = make_message(user_id=1, thread_id=42, chat_id=100)
await mod.cmd_archive(msg)
# close_forum_topic is NOT called — unsupported in Threaded Mode personal chats
msg.bot.close_forum_topic.assert_not_called()
msg.bot.delete_forum_topic.assert_called_once_with(chat_id=100, message_thread_id=42)
assert fresh_db.get_chat(1, 42)["archived_at"] is not None
msg.answer.assert_not_called()
async def test_cmd_archive_fallback_message_when_delete_fails(fresh_db, monkeypatch):
"""When delete_forum_topic fails (bot-created topic), user gets explanation."""
import adapter.telegram.handlers.commands as mod
importlib.reload(mod)
fresh_db.create_chat(1, 42, "Чат #1")
msg = make_message(user_id=1, thread_id=42, chat_id=100)
msg.bot.delete_forum_topic = AsyncMock(
side_effect=TelegramBadRequest(method=MagicMock(), message="not a supergroup forum")
)
await mod.cmd_archive(msg)
assert fresh_db.get_chat(1, 42)["archived_at"] is not None
msg.answer.assert_called_once()
assert "архивирован" in msg.answer.call_args[0][0]