fix: keep VK bot alive across long poll failures
This commit is contained in:
1 parent
e33de2114a
commit
2d9aa2126c
8 files changed
+157
-15
No files matched your search
@@ -1,5 +1,7 @@
|
||||
import unittest
|
||||
import json
|
||||
from unittest.mock import patch
|
||||
from urllib.error import URLError
|
||||
|
||||
from app.belki.vk_bot import (
|
||||
BelkiDialog,
|
||||
@@ -13,6 +15,28 @@ from app.belki.vk_bot import (
|
||||
|
||||
|
||||
class VkBotTests(unittest.TestCase):
|
||||
def test_long_poll_reconnects_after_ssl_error(self):
|
||||
class Client:
|
||||
def __init__(self):
|
||||
self.calls = 0
|
||||
|
||||
def call(self, method, params):
|
||||
self.calls += 1
|
||||
return {"server": "https://vk.example/longpoll", "key": "key", "ts": str(self.calls)}
|
||||
|
||||
class Response:
|
||||
def __enter__(self): return self
|
||||
def __exit__(self, *args): return False
|
||||
def __iter__(self): return iter(())
|
||||
def read(self): return b'{"ts":"2","updates":[{"type":"message_new"}]}'
|
||||
|
||||
bot = BelkiBot.__new__(BelkiBot)
|
||||
bot.client = Client()
|
||||
bot.group_id = 1
|
||||
events = bot.events()
|
||||
with patch("app.belki.vk_bot.urlopen", side_effect=[URLError("handshake timeout"), Response()]), patch("app.belki.vk_bot.time.sleep"):
|
||||
self.assertEqual(next(events)["type"], "message_new")
|
||||
self.assertEqual(bot.client.calls, 2)
|
||||
def test_public_chat_messages_are_ignored_but_invite_is_registered(self):
|
||||
class Client:
|
||||
def __init__(self): self.sent = []
|
||||
|
||||
@@ -4,6 +4,7 @@ from tempfile import TemporaryDirectory
|
||||
|
||||
from app.belki.identity import Identity, IdentityDirectory
|
||||
from app.belki.vk_bot import HELP_TEXT, BelkiDialog, inspect_bulk_text, parse_text_command
|
||||
from app.belki.vk_scheduler import VkConversationStore
|
||||
|
||||
|
||||
class VkConversationTests(unittest.TestCase):
|
||||
@@ -223,6 +224,24 @@ class VkConversationTests(unittest.TestCase):
|
||||
self.assertIn("Серолап", confirmation)
|
||||
self.assertIn("подтверд", confirmation.lower())
|
||||
|
||||
def test_dialog_state_and_photo_survive_bot_recreation(self):
|
||||
with TemporaryDirectory() as directory:
|
||||
store = VkConversationStore(sqlite_path=f"{directory}/vk.sqlite3")
|
||||
first = BelkiDialog(state_store=store)
|
||||
first.handle(204, "/осмотр")
|
||||
first.handle(204, "дупло")
|
||||
first.handle(204, "1")
|
||||
first.handle(204, "Серолап [1617140]")
|
||||
first.handle(204, "без ресурсов")
|
||||
first.queue_attachments(204, [{"type": "photo", "url": "https://example.test/history.jpg"}])
|
||||
|
||||
recreated = BelkiDialog(state_store=store)
|
||||
confirmation = recreated.handle(204, "готово")
|
||||
|
||||
self.assertIn("Серолап", confirmation)
|
||||
self.assertIn("подтверд", confirmation.lower())
|
||||
self.assertEqual(recreated._states[204]["attachments"][0]["url"], "https://example.test/history.jpg")
|
||||
|
||||
def test_inspection_character_without_directory_offers_pairing(self):
|
||||
dialog = BelkiDialog(IdentityDirectory())
|
||||
dialog.handle(105, "/осмотр")
|
||||
|
||||
Reference in new issue
Block a user