import unittest from datetime import datetime from tempfile import TemporaryDirectory from app.belki.identity import Identity, IdentityDirectory from app.belki.vk_bot import HELP_TEXT, BelkiDialog, inspect_bulk_text class VkConversationTests(unittest.TestCase): @staticmethod def add_screenshot(dialog, actor_id): dialog.queue_attachments(actor_id, [{"type": "photo", "url": "https://example.test/screenshot.jpg"}]) return dialog.handle(actor_id, "готово") def test_help_is_a_neutral_faq(self): self.assertIn("Беличья нора — помощь", HELP_TEXT) self.assertIn("Как отправить данные", HELP_TEXT) self.assertIn("/отмена", HELP_TEXT) self.assertNotIn("Для детей", HELP_TEXT) def test_step_by_step_coaching_dialog(self): dialog = BelkiDialog() self.assertIn("дат", dialog.handle(77, "/лу").lower()) self.assertIn("начал", dialog.handle(77, "15.08").lower()) self.assertIn("конец", dialog.handle(77, "11:00").lower()) self.assertIn("сопровождающ", dialog.handle(77, "11:20").lower()) self.assertIn("котён", dialog.handle(77, "Дикая Вишня [1604614]").lower()) self.assertIn("самостоятель", dialog.handle(77, "Серолап [1617140]").lower()) self.assertIn("скриншот", dialog.handle(77, "самостоятельно").lower()) confirmation = self.add_screenshot(dialog, 77) self.assertIn("подтверд", confirmation.lower()) report = dialog.handle(77, "да") self.assertIn("Баллы белки: 4 баллы.", report) def test_bulk_input_is_detected_and_confirmed(self): dialog = BelkiDialog() confirmation = dialog.handle( 88, "/лу 15.08 11:00 11:20 | Дикая Вишня [1604614] | Серолап [1617140] | самостоятельно", ) self.assertIn("скриншот", confirmation.lower()) confirmation = self.add_screenshot(dialog, 88) self.assertIn("распознал", confirmation.lower()) self.assertIn("11:00", confirmation) self.assertIn("подтверд", confirmation.lower()) report = dialog.handle(88, "да") self.assertIn("#лазание", report) def test_bulk_without_skill_requests_clarification(self): dialog = BelkiDialog() response = dialog.handle(99, "15.08 11:00 11:20 | Белка [1] | Котёнок [2]") self.assertIn("ЛУ", response) self.assertIn("ЗУ", response) def test_cancel_clears_dialog(self): dialog = BelkiDialog() dialog.handle(1, "/лу") self.assertIn("отмен", dialog.handle(1, "/отмена").lower()) def test_bulk_inspector_returns_fields_and_missing_questions(self): inspected = inspect_bulk_text("/лу 15.08 11:00 11:20 | Белка [1] | Котёнок [2]") self.assertEqual(inspected["status"], "needs_clarification") self.assertIn("skill", inspected["fields"]) self.assertIn("самостоятель", " ".join(inspected["questions"]).lower()) def test_bulk_inspector_detects_plain_text_without_skill(self): inspected = inspect_bulk_text("15.08 11:00 11:20 | Белка [1] | Котёнок [2]") self.assertEqual(inspected["status"], "needs_clarification") self.assertIn("ЛУ", " ".join(inspected["questions"])) def test_natural_bulk_input_needs_no_pipes(self): dialog = BelkiDialog() response = dialog.handle(101, "15.08 11:00 11:20 Дикая Вишня 1604614 Серолап 1617140") self.assertIn("ЛУ", response) self.assertIn("ЗУ", response) response = dialog.handle(101, "ЛУ") self.assertIn("самостоятель", response.lower()) response = dialog.handle(101, "самостоятельно") self.assertIn("скриншот", response.lower()) def test_training_supports_stepwise_and_natural_bulk(self): dialog = BelkiDialog() self.assertIn("сопровождающ", dialog.handle(102, "/урок").lower()) self.assertIn("котён", dialog.handle(102, "Дикая Вишня [1604614]").lower()) self.assertIn("скриншот", dialog.handle(102, "Серолап [1617140]").lower()) confirmation = self.add_screenshot(dialog, 102) self.assertIn("25", confirmation) self.assertIn("#урок", dialog.handle(102, "да")) natural = dialog.handle(103, "урок Дикая Вишня 1604614 Серолап 1617140") self.assertIn("скриншот", natural.lower()) natural = self.add_screenshot(dialog, 103) self.assertIn("25", natural) self.assertIn("подтверд", natural.lower()) def test_inspection_supports_stepwise_and_id_only_character(self): directory = IdentityDirectory([Identity("Серолап", 1617140)]) dialog = BelkiDialog(directory) self.assertIn("место", dialog.handle(104, "/осмотр").lower()) self.assertIn("сколько", dialog.handle(104, "дупло").lower()) self.assertIn("персонаж", dialog.handle(104, "6").lower()) self.assertIn("ресурс", dialog.handle(104, "1617140").lower()) self.assertIn("скриншот", dialog.handle(104, "без ресурсов").lower()) confirmation = self.add_screenshot(dialog, 104) self.assertIn("Серолап", confirmation) self.assertIn("подтверд", confirmation.lower()) def test_inspection_character_without_directory_offers_pairing(self): dialog = BelkiDialog(IdentityDirectory()) dialog.handle(105, "/осмотр") dialog.handle(105, "дупло") dialog.handle(105, "1") response = dialog.handle(105, "1617140") self.assertIn("связк", response.lower()) dialog.handle(105, "Серолап") dialog.handle(105, "без ресурсов") confirmation = self.add_screenshot(dialog, 105) self.assertIn("Серолап", confirmation) def test_inspection_resources_use_signed_amount_before_name(self): dialog = BelkiDialog() dialog.handle(106, "/осмотр") dialog.handle(106, "дупло") dialog.handle(106, "6") dialog.handle(106, "Серолап [1617140]") self.assertIn("скриншот", dialog.handle(106, "-1 вьюнок, + 3 паук").lower()) confirmation = self.add_screenshot(dialog, 106) self.assertIn("вьюнок=-1", confirmation) self.assertIn("паук=3", confirmation) def test_queued_photo_is_saved_in_confirmed_state(self): dialog = BelkiDialog() dialog.queue_attachments(107, [{"type": "photo", "url": "https://example.test/photo.jpg"}]) dialog.handle(107, "/урок") dialog.handle(107, "Белка [1]") self.assertIn("скриншот", dialog.handle(107, "Котёнок [2]").lower()) confirmation = self.add_screenshot(dialog, 107) self.assertIn("подтверд", confirmation.lower()) self.assertEqual(dialog._states[107]["attachments"][0]["url"], "https://example.test/photo.jpg") def test_photo_is_mirrored_before_history_save(self): mirrored = [] def mirror(photo): mirrored.append(photo["url"]) return "https://img.nyan404.ru/photo/abc.jpg" dialog = BelkiDialog(attachment_mirror=mirror) dialog.queue_attachments(108, [{"type": "photo", "url": "https://vk.example/photo.jpg"}]) dialog.handle(108, "/урок") dialog.handle(108, "Белка [1]") dialog.handle(108, "Котёнок [2]") self.add_screenshot(dialog, 108) dialog.handle(108, "да") self.assertEqual(mirrored[0], "https://vk.example/photo.jpg") def test_natural_inspection_accepts_character_id_without_name(self): inspected = inspect_bulk_text("осмотр дупло 6 1617140 без ресурсов") self.assertEqual(inspected["fields"]["character_id"], 1617140) self.assertIn("имени", " ".join(inspected["questions"]).lower()) def test_identity_directory_persists_pairs_in_sqlite(self): with TemporaryDirectory() as directory: path = f"{directory}/identities.sqlite3" first = IdentityDirectory(db_path=path) first.remember(name="Серолап", character_id=1617140) second = IdentityDirectory(db_path=path) self.assertEqual(second.resolve(character_id=1617140).name, "Серолап") self.assertEqual(second.resolve(name="Серолап").character_id, 1617140) def test_onboarding_remembers_author_and_greets_by_name(self): with TemporaryDirectory() as directory: identities = IdentityDirectory(db_path=f"{directory}/identities.sqlite3") dialog = BelkiDialog(identities, onboarding=True) self.assertIn("представься", dialog.handle(500, "/лу", user_id=700).lower()) welcome = dialog.handle(500, "Дикая Вишня [1604614]", user_id=700) self.assertIn("Привет, Дикая Вишня", welcome) self.assertIn("/осмотр", welcome) self.assertIn("дат", dialog.handle(500, "/лу", user_id=700).lower()) restarted = BelkiDialog(IdentityDirectory(db_path=f"{directory}/identities.sqlite3"), onboarding=True) self.assertIn("дат", restarted.handle(500, "/лу", user_id=700).lower()) def test_independence_accepts_adjectival_forms(self): dialog = BelkiDialog() dialog.handle(501, "/лу") for answer in ("15.08", "11:00", "11:20", "Дикая Вишня [1604614]", "Серолап [1617140]"): dialog.handle(501, answer) self.assertIn("скриншот", dialog.handle(501, "Самостоятельный").lower()) confirmation = self.add_screenshot(dialog, 501) self.assertIn("самостоятельно", confirmation.lower()) dialog.handle(502, "/лу") for answer in ("15.08", "11:00", "11:20", "Дикая Вишня [1604614]", "Серолап [1617140]"): dialog.handle(502, answer) self.assertIn("скриншот", dialog.handle(502, "несам").lower()) def test_keyboard_changes_with_dialog_state(self): dialog = BelkiDialog() menu = dialog.keyboard_for(503) labels = [button["action"]["label"] for row in menu["buttons"] for button in row] self.assertIn("Прокачка ЛУ", labels) dialog.handle(503, "/лу") for answer in ("15.08", "11:00", "11:20", "Дикая Вишня [1604614]", "Серолап [1617140]"): dialog.handle(503, answer) labels = [button["action"]["label"] for row in dialog.keyboard_for(503)["buttons"] for button in row] self.assertEqual(labels[:2], ["Сам", "Несам"]) dialog.handle(503, "сам") labels = [button["action"]["label"] for row in dialog.keyboard_for(503)["buttons"] for button in row] self.assertIn("Готово", labels) def test_quick_buttons_cover_safe_choices(self): dialog = BelkiDialog() dialog.handle(504, "15.08 11:00 11:20 Белка 1 Котёнок 2") labels = [button["action"]["label"] for row in dialog.keyboard_for(504)["buttons"] for button in row] self.assertIn("ЛУ", labels) dialog = BelkiDialog() dialog.handle(504, "/лу") labels = [button["action"]["label"] for row in dialog.keyboard_for(504)["buttons"] for button in row] self.assertIn("Сегодня", labels) response = dialog.handle(504, "__today__") self.assertIn("начал", response.lower()) self.assertEqual(dialog._states[504]["date"], datetime.now().strftime("%d.%m")) inspection = BelkiDialog() inspection.handle(505, "/осмотр") labels = [button["action"]["label"] for row in inspection.keyboard_for(505)["buttons"] for button in row] self.assertEqual(labels[:2], ["Дупло", "Расщелина"]) inspection.handle(505, "дупло") inspection.handle(505, "0") inspection.handle(505, "Серолап [2]") labels = [button["action"]["label"] for row in inspection.keyboard_for(505)["buttons"] for button in row] self.assertIn("Без ресурсов", labels) if __name__ == "__main__": unittest.main()