test_point_settings.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. from __future__ import annotations
  2. from types import SimpleNamespace
  3. from unittest.mock import AsyncMock
  4. import pytest
  5. from pyrogram.types import ReplyKeyboardMarkup, ReplyKeyboardRemove
  6. async def test_enabling_checkin_button_sends_persistent_keyboard(
  7. app_modules, monkeypatch
  8. ):
  9. points = app_modules.load("wbb.utils.dbpoints")
  10. settings = app_modules.load("wbb.services.point_settings")
  11. await points.set_point_rules(
  12. -100, {"enabled": True, "checkin_enabled": True}
  13. )
  14. send_message = AsyncMock(return_value=SimpleNamespace(id=1))
  15. monkeypatch.setattr(settings.app, "send_message", send_message)
  16. rules = await settings.apply_point_rules(
  17. -100, {"checkin_button_enabled": True}
  18. )
  19. assert rules["checkin_button_enabled"] is True
  20. send_message.assert_awaited_once()
  21. chat_id, text = send_message.await_args.args
  22. reply_markup = send_message.await_args.kwargs["reply_markup"]
  23. assert chat_id == -100
  24. assert "签到快捷按钮已开启" in text
  25. assert isinstance(reply_markup, ReplyKeyboardMarkup)
  26. assert reply_markup.is_persistent is True
  27. assert reply_markup.resize_keyboard is True
  28. assert reply_markup.keyboard[0][0].text == "签到"
  29. async def test_checkin_button_sync_is_idempotent_and_follows_points_state(
  30. app_modules, monkeypatch
  31. ):
  32. points = app_modules.load("wbb.utils.dbpoints")
  33. settings = app_modules.load("wbb.services.point_settings")
  34. await points.set_point_rules(
  35. -100,
  36. {
  37. "enabled": True,
  38. "checkin_enabled": True,
  39. "checkin_button_enabled": True,
  40. },
  41. )
  42. send_message = AsyncMock(return_value=SimpleNamespace(id=1))
  43. monkeypatch.setattr(settings.app, "send_message", send_message)
  44. await settings.apply_point_rules(-100, {"checkin_button_enabled": True})
  45. send_message.assert_not_awaited()
  46. rules = await settings.apply_point_rules(-100, {"enabled": False})
  47. assert rules["checkin_button_enabled"] is True
  48. reply_markup = send_message.await_args.kwargs["reply_markup"]
  49. assert isinstance(reply_markup, ReplyKeyboardRemove)
  50. async def test_checkin_button_send_failure_rolls_back_rule(app_modules, monkeypatch):
  51. points = app_modules.load("wbb.utils.dbpoints")
  52. settings = app_modules.load("wbb.services.point_settings")
  53. await points.set_point_rules(
  54. -100, {"enabled": True, "checkin_enabled": True}
  55. )
  56. monkeypatch.setattr(
  57. settings.app,
  58. "send_message",
  59. AsyncMock(side_effect=RuntimeError("telegram unavailable")),
  60. )
  61. with pytest.raises(points.PointsError, match="无法更新群底部签到按钮"):
  62. await settings.apply_point_rules(
  63. -100, {"checkin_button_enabled": True}
  64. )
  65. rules = await points.get_point_rules(-100)
  66. assert rules["checkin_button_enabled"] is False