feat: add Belichya Nora calculator and VK bot
This commit is contained in:
1 parent
cd6a594ed4
commit
c86d077f9f
20 files changed
+2002
-18
No files matched your search
@@ -0,0 +1,72 @@
|
||||
import unittest
|
||||
import sqlite3
|
||||
from tempfile import TemporaryDirectory
|
||||
|
||||
from app.belki.api import ApiError, calculate_payload, inspect_bulk_payload, resolve_identity_payload
|
||||
from app.belki.history import CalculationStore
|
||||
|
||||
|
||||
class ApiTests(unittest.TestCase):
|
||||
def test_calculate_coaching_payload(self):
|
||||
response = calculate_payload(
|
||||
{
|
||||
"mode": "coaching",
|
||||
"date": "15.08",
|
||||
"skill": "ЛУ",
|
||||
"start": "00:52",
|
||||
"end": "01:52",
|
||||
"escort_name": "Дикая Вишня",
|
||||
"escort_id": 1604614,
|
||||
"kitten_name": "Котёнок",
|
||||
"kitten_id": 1676767,
|
||||
"kitten_independence": "самостоятельно",
|
||||
}
|
||||
)
|
||||
self.assertEqual(response["result"]["minutes"], 60)
|
||||
self.assertIn("Баллы белки: 12", response["message"])
|
||||
|
||||
def test_calculate_rejects_unknown_mode(self):
|
||||
with self.assertRaises(ApiError):
|
||||
calculate_payload({"mode": "unknown"})
|
||||
|
||||
def test_calculate_inspection_payload(self):
|
||||
response = calculate_payload(
|
||||
{
|
||||
"mode": "inspection",
|
||||
"character_name": "Дикая Вишня",
|
||||
"character_id": 1604614,
|
||||
"place": "дупло",
|
||||
"inspections": 6,
|
||||
"resources": [["вьюнок", -2], ["паук", 1]],
|
||||
}
|
||||
)
|
||||
self.assertIn("осмотрел(а) дупло 6 раз", response["message"])
|
||||
|
||||
def test_bulk_inspection_payload_explains_missing_fields(self):
|
||||
response = inspect_bulk_payload({"text": "/лу 15.08 11:00 11:20 | Белка [1] | Котёнок [2]"})
|
||||
self.assertEqual(response["status"], "needs_clarification")
|
||||
self.assertTrue(response["questions"])
|
||||
|
||||
def test_identity_resolution_without_directory_requests_missing_half(self):
|
||||
response = resolve_identity_payload({"character_id": 1617140})
|
||||
self.assertEqual(response["status"], "needs_input")
|
||||
self.assertIn("имя", response["message"].lower())
|
||||
|
||||
def test_calculation_store_persists_full_record(self):
|
||||
with TemporaryDirectory() as directory:
|
||||
path = f"{directory}/history.sqlite3"
|
||||
store = CalculationStore(sqlite_path=path)
|
||||
response = calculate_payload({
|
||||
"mode": "training", "escort_name": "Дикая Вишня", "escort_id": 1604614,
|
||||
"kitten_name": "Серолап", "kitten_id": 1617140,
|
||||
})
|
||||
record_id = store.save(actor_id=700, source="vk", payload={"mode": "training"}, response=response)
|
||||
self.assertGreater(record_id, 0)
|
||||
with sqlite3.connect(path) as connection:
|
||||
row = connection.execute("SELECT actor_id, mode, source, message FROM calculations").fetchone()
|
||||
self.assertEqual(row[:3], (700, "training", "vk"))
|
||||
self.assertIn("25", row[3])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,115 @@
|
||||
import unittest
|
||||
|
||||
from app.belki.domain import (
|
||||
CoachingSession,
|
||||
InspectionReport,
|
||||
TrainingSession,
|
||||
calculate_coaching,
|
||||
calculate_training,
|
||||
format_clock,
|
||||
parse_clock,
|
||||
)
|
||||
from app.belki.reports import render_coaching, render_inspection, render_training
|
||||
|
||||
|
||||
class ClockTests(unittest.TestCase):
|
||||
def test_duration_supports_midnight(self):
|
||||
self.assertEqual(parse_clock("23:55"), 23 * 60 + 55)
|
||||
self.assertEqual(format_clock(parse_clock("23:05")), "23:05")
|
||||
self.assertEqual(
|
||||
CoachingSession.duration_minutes("23:55", "00:10"),
|
||||
15,
|
||||
)
|
||||
|
||||
def test_invalid_clock_is_rejected(self):
|
||||
with self.assertRaises(ValueError):
|
||||
parse_clock("5:5")
|
||||
|
||||
|
||||
class CalculationTests(unittest.TestCase):
|
||||
def test_self_sufficient_kitten(self):
|
||||
result = calculate_coaching(
|
||||
CoachingSession(
|
||||
date="15.08",
|
||||
skill="ЛУ",
|
||||
start="00:52",
|
||||
end="01:52",
|
||||
escort_name="Дикая Вишня",
|
||||
escort_id=1604614,
|
||||
kitten_name="Котёнок",
|
||||
kitten_id=1676767,
|
||||
kitten_independence="самостоятельно",
|
||||
)
|
||||
)
|
||||
self.assertEqual(result.minutes, 60)
|
||||
self.assertEqual(result.escort_points, 12)
|
||||
self.assertEqual(result.kitten_points, 24)
|
||||
|
||||
def test_non_self_kitten_and_extra_companion(self):
|
||||
result = calculate_coaching(
|
||||
CoachingSession(
|
||||
date="15.08",
|
||||
skill="ЗУ",
|
||||
start="11:00",
|
||||
end="12:00",
|
||||
escort_name="Белка",
|
||||
escort_id=1,
|
||||
kitten_name="Первый",
|
||||
kitten_id=2,
|
||||
kitten_independence="несамостоятельно",
|
||||
extra_kittens=(
|
||||
("Второй", 3, "несамостоятельно"),
|
||||
("Третий", 4, "самостоятельно"),
|
||||
),
|
||||
)
|
||||
)
|
||||
self.assertEqual(result.escort_points, 24)
|
||||
self.assertEqual(result.kitten_points, 24)
|
||||
|
||||
def test_training_is_fixed(self):
|
||||
self.assertEqual(calculate_training(TrainingSession("Белка", 1, "Котёнок", 2)).points, 25)
|
||||
|
||||
|
||||
class ReportTests(unittest.TestCase):
|
||||
def test_coaching_report_contains_new_fields_and_reminder(self):
|
||||
session = CoachingSession(
|
||||
date="15.08",
|
||||
skill="ЛУ",
|
||||
start="11:00",
|
||||
end="11:20",
|
||||
escort_name="Дикая Вишня",
|
||||
escort_id=1604614,
|
||||
kitten_name="Серолап",
|
||||
kitten_id=1617140,
|
||||
kitten_independence="несамостоятельно",
|
||||
)
|
||||
text = render_coaching(session, calculate_coaching(session))
|
||||
self.assertIn("15.08 [ЛУ]", text)
|
||||
self.assertIn("Время: 11:00–11:20 [20 минут]", text)
|
||||
self.assertIn("Сопровождающий: Дикая Вишня [1604614]", text)
|
||||
self.assertIn("Баллы белки: 6", text)
|
||||
self.assertIn("Баллы бельчонка: 8", text)
|
||||
self.assertIn("истории", text)
|
||||
|
||||
def test_training_report_is_separate(self):
|
||||
text = render_training(TrainingSession("Белка", 1, "Котёнок", 2))
|
||||
self.assertIn("#урок", text)
|
||||
self.assertIn("25", text)
|
||||
|
||||
def test_inspection_report_lists_zero_and_resources(self):
|
||||
text = render_inspection(
|
||||
InspectionReport(
|
||||
character_name="Дикая Вишня",
|
||||
character_id=1604614,
|
||||
place="дупло",
|
||||
inspections=6,
|
||||
resources=(("вьюнок", -2), ("паук", 1)),
|
||||
)
|
||||
)
|
||||
self.assertIn("осмотрел(а) дупло 6 раз", text)
|
||||
self.assertIn("- 2 вьюнок", text)
|
||||
self.assertIn("+1 паук", text)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,33 @@
|
||||
import unittest
|
||||
|
||||
from app.belki.vk_bot import (
|
||||
HELP_TEXT,
|
||||
build_message_request,
|
||||
parse_text_command,
|
||||
)
|
||||
|
||||
|
||||
class VkBotTests(unittest.TestCase):
|
||||
def test_parse_coaching_command(self):
|
||||
command = parse_text_command(
|
||||
"/лу 15.08 11:00 11:20 | Дикая Вишня [1604614] | Серолап [1617140] | самостоятельно"
|
||||
)
|
||||
self.assertEqual(command["mode"], "coaching")
|
||||
self.assertEqual(command["escort_id"], 1604614)
|
||||
self.assertEqual(command["kitten_id"], 1617140)
|
||||
|
||||
def test_parse_help(self):
|
||||
self.assertEqual(parse_text_command("/help"), {"mode": "help"})
|
||||
self.assertIn("/лу", HELP_TEXT)
|
||||
self.assertIn("Беличья нора", HELP_TEXT)
|
||||
|
||||
def test_message_request_never_omits_random_id(self):
|
||||
request = build_message_request(123, "Готово", "secret", "5.199", random_id=77)
|
||||
self.assertEqual(request["peer_id"], 123)
|
||||
self.assertEqual(request["random_id"], 77)
|
||||
self.assertEqual(request["access_token"], "secret")
|
||||
self.assertEqual(request["v"], "5.199")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -0,0 +1,148 @@
|
||||
import unittest
|
||||
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("15.08 [ЛУ]", 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, "да").lower())
|
||||
|
||||
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())
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user