feat(applications): add keyboard flow and export outbox

This commit is contained in:
nyan404 committed 2026-08-21 13:25:33 +03:00
1 parent cfeee5a814
commit 27bcc291db
11 files changed
+255 -10

No files matched your search

+33
View File
@@ -3,6 +3,7 @@ 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
@@ -31,6 +32,22 @@ class ApplicationTests(unittest.TestCase):
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")
@@ -42,6 +59,22 @@ class ApplicationTests(unittest.TestCase):
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()
+2
View File
@@ -53,6 +53,8 @@ class VkBotTests(unittest.TestCase):
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"}'}), "да")
self.assertEqual(BelkiBot._button_text({"payload": '{"command":"application:flying"}'}), "/летяга")
self.assertEqual(BelkiBot._button_text({"payload": '{"command":"application:practice-no"}'}), "нет")
def test_keyboard_button_payloads_are_valid_json(self):
keyboard = BelkiDialog().keyboard_for(503)