184 lines
9.7 KiB
Python
184 lines
9.7 KiB
Python
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):
|
|
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())
|
|
confirmation = dialog.handle(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())
|
|
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())
|
|
confirmation = dialog.handle(102, "Серолап [1617140]")
|
|
self.assertIn("25", confirmation)
|
|
self.assertIn("#урок", dialog.handle(102, "да"))
|
|
|
|
natural = dialog.handle(103, "урок Дикая Вишня 1604614 Серолап 1617140")
|
|
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())
|
|
confirmation = dialog.handle(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, "Серолап")
|
|
confirmation = dialog.handle(105, "без ресурсов")
|
|
self.assertIn("Серолап", confirmation)
|
|
|
|
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)
|
|
confirmation = dialog.handle(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], ["Сам", "Несам"])
|
|
|
|
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()
|