feat: add sent report history lookup

This commit is contained in:
nyan404 committed 2026-10-18 18:56:23 +03:00
1 parent 546fa37fce
commit 3ebab303a5
12 files changed
+393 -18

No files matched your search

+98
View File
@@ -0,0 +1,98 @@
import http.client
import json
import tempfile
import threading
import unittest
from pathlib import Path
from urllib.parse import urlencode
from unittest.mock import patch
from app.belki.history import CalculationStore
from app.main import BelkiHTTPServer, Handler
class ReportLookupTests(unittest.TestCase):
def make_store(self, path: Path) -> CalculationStore:
store = CalculationStore(sqlite_path=path)
entries = [
(1, "vk", "coaching", {"escort_id": 123, "kitten_id": 201, "extra_kittens": [["Дополнительный", 301, "самостоятельно"]]}, "ЛУ [123] → [201]"),
(2, "web", "training", {"escort_id": 401, "kitten_id": 123}, "[123] — тренировка"),
(1, "vk", "inspection", {"character_id": 123}, "Осмотр [123]"),
(1, "vk", "goal", {"kitten_count": 1}, "Расчёт цели"),
(2, "vk", "coaching", {"escort_id": 402, "kitten_id": 403}, "Другой кот"),
]
for actor_id, source, mode, payload, message in entries:
store.save(actor_id=actor_id, source=source, payload={"mode": mode, **payload}, response={"mode": mode, "message": message, "result": {}})
return store
def test_vk_history_filters_sender_source_and_report_modes(self):
with tempfile.TemporaryDirectory() as directory:
store = self.make_store(Path(directory) / "history.sqlite3")
rows = store.list_vk_reports(1)
self.assertEqual({row["message"] for row in rows}, {"ЛУ [123] → [201]", "Осмотр [123]"})
self.assertTrue(all(row["actor_id"] == 1 and row["source"] == "vk" for row in rows))
def test_character_history_matches_every_participant_shape_and_source(self):
with tempfile.TemporaryDirectory() as directory:
store = self.make_store(Path(directory) / "history.sqlite3")
rows = store.list_character_reports(123)
self.assertEqual({row["message"] for row in rows}, {"ЛУ [123] → [201]", "[123] — тренировка", "Осмотр [123]"})
self.assertEqual([row["message"] for row in store.list_character_reports(301)], ["ЛУ [123] → [201]"])
self.assertEqual(store.list_character_reports(999), [])
def test_character_history_limit_is_applied_after_matching(self):
with tempfile.TemporaryDirectory() as directory:
store = self.make_store(Path(directory) / "history.sqlite3")
for index in range(15):
store.save(actor_id=None, source="web", payload={"mode": "inspection", "character_id": 123}, response={"mode": "inspection", "message": str(index), "result": {}})
rows = store.list_character_reports(123, limit=4)
self.assertEqual(len(rows), 4)
self.assertEqual([row["message"] for row in rows], ["14", "13", "12", "11"])
def test_public_character_lookup_returns_no_vk_sender_id(self):
with tempfile.TemporaryDirectory() as directory:
store = self.make_store(Path(directory) / "history.sqlite3")
server = BelkiHTTPServer(("127.0.0.1", 0), Handler)
worker = threading.Thread(target=server.serve_forever, daemon=True)
worker.start()
try:
with patch("app.main.history_store", return_value=store):
connection = http.client.HTTPConnection("127.0.0.1", server.server_port)
connection.request("GET", f"/api/reports?{urlencode({'character_id': '123'})}")
response = connection.getresponse()
payload = json.loads(response.read())
connection.close()
self.assertEqual(response.status, 200)
self.assertEqual(len(payload["items"]), 3)
self.assertTrue(all("actor_id" not in item for item in payload["items"]))
finally:
server.shutdown()
server.server_close()
worker.join(timeout=2)
def test_public_character_lookup_rejects_invalid_id(self):
server = BelkiHTTPServer(("127.0.0.1", 0), Handler)
worker = threading.Thread(target=server.serve_forever, daemon=True)
worker.start()
try:
connection = http.client.HTTPConnection("127.0.0.1", server.server_port)
connection.request("GET", "/api/reports?character_id=0")
response = connection.getresponse()
payload = json.loads(response.read())
connection.close()
self.assertEqual(response.status, 400)
self.assertIn("ID кота", payload["error"])
finally:
server.shutdown()
server.server_close()
worker.join(timeout=2)
if __name__ == "__main__":
unittest.main()
+23
View File
@@ -144,6 +144,7 @@ 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":"menu:my_reports"}'}), "/мои-отчёты")
self.assertEqual(BelkiBot._button_text({"payload": '{"command":"points:coaching"}'}), "прокачка")
self.assertEqual(BelkiBot._button_text({"payload": '{"command":"points:training"}'}), "тренировка")
self.assertEqual(BelkiBot._button_text({"payload": '{"command":"points:inspection"}'}), "осмотр")
@@ -176,6 +177,28 @@ class VkBotTests(unittest.TestCase):
self.assertIsInstance(payload, dict)
self.assertIn("command", payload)
def test_my_reports_menu_shows_only_current_vk_sender_history(self):
class Store:
def list_vk_reports(self, actor_id, *, limit=100):
self.actor_id = actor_id
return [{"id": 7, "mode": "training", "message": "Малыш [123] — тренировка.", "created_at": "2026-10-18T12:00:00+00:00"}]
store = Store()
dialog = BelkiDialog(calculation_store=store)
response = dialog.handle(900, "/мои-отчёты", user_id=501)
self.assertEqual(store.actor_id, 501)
self.assertIn("Малыш [123]", response)
self.assertIn("№7", response)
def test_my_reports_button_is_in_main_menu(self):
keyboard = BelkiDialog().keyboard_for(503)
commands = [
json.loads(button["action"]["payload"])["command"]
for row in keyboard["buttons"] for button in row
]
self.assertIn("menu:my_reports", commands)
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",