77 lines
3.4 KiB
Python
77 lines
3.4 KiB
Python
import unittest
|
|
import json
|
|
|
|
from app.belki.vk_bot import (
|
|
BelkiDialog,
|
|
HELP_TEXT,
|
|
build_message_request,
|
|
parse_text_command,
|
|
BelkiBot,
|
|
extract_photo_attachments,
|
|
)
|
|
|
|
|
|
class VkBotTests(unittest.TestCase):
|
|
def test_parse_coaching_command(self):
|
|
command = parse_text_command(
|
|
"/лу 15.08 11:00 11:20 | Дикая Вишня [1604614] | Серолап [1617140] | самостоятельно"
|
|
)
|
|
self.assertEqual(command["mode"], "coaching")
|
|
self.assertEqual(command["escort_id"], 1604614)
|
|
self.assertEqual(command["kitten_id"], 1617140)
|
|
|
|
def test_parse_coaching_command_with_additional_wards(self):
|
|
command = parse_text_command(
|
|
"/лу 15.08 11:00 11:20 | Белка [1] | Первый [2] | сам | доп. котята: Второй [3], Третий [4]"
|
|
)
|
|
self.assertEqual(command["extra_kittens"], (("Второй", 3, "самостоятельно"), ("Третий", 4, "самостоятельно")))
|
|
|
|
def test_parse_one_line_command_with_multiple_kittens_and_shared_status(self):
|
|
command = parse_text_command(
|
|
"/зу 15.08 11:00 11:20 | Белка [1] | Первый [2], Второй [3] | несам"
|
|
)
|
|
self.assertEqual(command["kitten_independence"], "несамостоятельно")
|
|
self.assertEqual(command["extra_kittens"], (("Второй", 3, "несамостоятельно"),))
|
|
|
|
def test_parse_help(self):
|
|
self.assertEqual(parse_text_command("/help"), {"mode": "help"})
|
|
self.assertIn("/лу", HELP_TEXT)
|
|
self.assertIn("Беличья нора", HELP_TEXT)
|
|
|
|
def test_message_request_never_omits_random_id(self):
|
|
request = build_message_request(123, "Готово", "secret", "5.199", random_id=77)
|
|
self.assertEqual(request["peer_id"], 123)
|
|
self.assertEqual(request["random_id"], 77)
|
|
self.assertEqual(request["access_token"], "secret")
|
|
self.assertEqual(request["v"], "5.199")
|
|
|
|
def test_message_request_serializes_vk_keyboard(self):
|
|
keyboard = {"one_time": False, "buttons": []}
|
|
request = build_message_request(123, "Меню", "secret", "5.199", random_id=77, keyboard=keyboard)
|
|
self.assertEqual(json.loads(request["keyboard"]), keyboard)
|
|
|
|
def test_button_payload_maps_to_command(self):
|
|
self.assertEqual(BelkiBot._button_text({"payload": "menu:lu"}), "/лу")
|
|
self.assertEqual(BelkiBot._button_text({"payload": '{"command":"confirm:yes"}'}), "да")
|
|
|
|
def test_keyboard_button_payloads_are_valid_json(self):
|
|
keyboard = BelkiDialog().keyboard_for(503)
|
|
for row in keyboard["buttons"]:
|
|
for button in row:
|
|
payload = json.loads(button["action"]["payload"])
|
|
self.assertIsInstance(payload, dict)
|
|
self.assertIn("command", payload)
|
|
|
|
def test_extract_photo_attachment_keeps_largest_size_and_vk_reference(self):
|
|
photos = extract_photo_attachments({"attachments": [{"type": "photo", "photo": {
|
|
"owner_id": -10, "id": 42, "access_key": "secret",
|
|
"sizes": [{"width": 100, "height": 100, "url": "small"},
|
|
{"width": 1000, "height": 800, "url": "large"}],
|
|
}}]})
|
|
self.assertEqual(photos[0]["url"], "large")
|
|
self.assertEqual(photos[0]["attachment"], "photo-10_42_secret")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|