import tempfile import unittest from pathlib import Path from app.belki.applications import ApplicationError, ApplicationStore, validate_application from app.belki.google_sheets_export import GoogleSheetsExportConfig, GoogleSheetsExportService from app.belki.identity import IdentityDirectory from app.belki.vk_bot import ApplicationDialog class ApplicationTests(unittest.TestCase): def test_kitten_requires_activity_screenshot(self) -> None: with self.assertRaisesRegex(ApplicationError, "скриншот активности"): validate_application({"type": "kitten", "name": "Бельчоночек", "character_id": 1676767}) def test_flying_squirrel_requires_previous_practice_comment(self) -> None: with self.assertRaisesRegex(ApplicationError, "прошлой практики"): validate_application({"type": "flying_squirrel", "name": "Муся", "character_id": 1337, "practiced_before": True}) def test_feather_donation_is_saved_and_accepted(self) -> None: with tempfile.TemporaryDirectory() as directory: store = ApplicationStore(sqlite_path=Path(directory) / "applications.sqlite3") application = validate_application({ "type": "feather_donation", "name": "Белка", "character_id": 58775, "feathers": [{"amount": 2, "color": "синие", "source": "личные запасы"}, {"amount": 1, "color": "чёрное", "source": "осмотр дупла"}], }) application_id = store.create(application=application, actor_id=10, reply_peer_id=10, source="vk") self.assertEqual(store.list(status="pending")[0]["application"]["feathers"][0]["color"], "синие") accepted = store.accept(application_id) self.assertEqual(accepted["status"], "accepted") self.assertEqual(store.pending_notifications()[0]["reply_peer_id"], 10) store.mark_notified(application_id) self.assertEqual(store.pending_notifications(), []) def test_export_outbox_collects_create_and_accept_events_while_disabled(self) -> None: with tempfile.TemporaryDirectory() as directory: store = ApplicationStore(sqlite_path=Path(directory) / "applications.sqlite3") application = validate_application({ "type": "feather_donation", "name": "Белка", "character_id": 58775, "feathers": [{"amount": 1, "color": "красное", "source": "личные запасы"}], }) application_id = store.create(application=application, actor_id=None, reply_peer_id=None, source="web") store.accept(application_id) events = store.pending_exports() self.assertEqual([event["event_type"] for event in events], ["application_created", "application_accepted"]) self.assertEqual(events[0]["payload"]["schema_version"], 1) exporter = GoogleSheetsExportService(store, GoogleSheetsExportConfig(enabled=False, url="https://example.test", token="")) self.assertEqual(exporter.sync_pending(), 0) self.assertEqual(len(store.pending_exports()), 2) def test_vk_kitten_application_uses_profile_and_saves(self) -> None: with tempfile.TemporaryDirectory() as directory: store = ApplicationStore(sqlite_path=Path(directory) / "applications.sqlite3") identities = IdentityDirectory(db_path=Path(directory) / "identities.sqlite3") identities.remember_profile(actor_id=55, name="Бельчоночек", character_id=1676767) dialog = ApplicationDialog(identities, store) self.assertIn("скриншот активности", dialog.handle(55, 55, "/бельчонок").lower()) dialog.queue_attachments(55, [{"type": "photo", "url": "https://example.test/activity.jpg"}]) self.assertIn("отправлена", dialog.handle(55, 55, "готово").lower()) self.assertEqual(store.list()[0]["application"]["type"], "kitten") def test_vk_application_uses_keyboard_for_menu_and_branches(self) -> None: with tempfile.TemporaryDirectory() as directory: store = ApplicationStore(sqlite_path=Path(directory) / "applications.sqlite3") identities = IdentityDirectory(db_path=Path(directory) / "identities.sqlite3") identities.remember_profile(actor_id=77, name="Муся", character_id=1337) dialog = ApplicationDialog(identities, store) dialog.handle(77, 77, "/заявка") labels = [button["action"]["label"] for row in dialog.keyboard_for(77)["buttons"] for button in row] self.assertEqual(labels[:4], ["Пожертвовать перо", "Стать бельчонком", "Стать летягой", "Стать куратором"]) dialog.handle(77, 77, "/летяга") labels = [button["action"]["label"] for row in dialog.keyboard_for(77)["buttons"] for button in row] self.assertIn("Нет ссылки VK", labels) dialog.handle(77, 77, "нет") labels = [button["action"]["label"] for row in dialog.keyboard_for(77)["buttons"] for button in row] self.assertEqual(labels[:2], ["Да", "Нет"]) if __name__ == "__main__": unittest.main()