test_point_settings.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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_hideable_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 False
  27. assert reply_markup.resize_keyboard is True
  28. assert reply_markup.one_time_keyboard is True
  29. assert reply_markup.keyboard[0][0].text == "签到"
  30. async def test_checkin_button_sync_is_idempotent_and_follows_points_state(
  31. app_modules, monkeypatch
  32. ):
  33. points = app_modules.load("wbb.utils.dbpoints")
  34. settings = app_modules.load("wbb.services.point_settings")
  35. await points.set_point_rules(
  36. -100,
  37. {
  38. "enabled": True,
  39. "checkin_enabled": True,
  40. "checkin_button_enabled": True,
  41. },
  42. )
  43. send_message = AsyncMock(return_value=SimpleNamespace(id=1))
  44. monkeypatch.setattr(settings.app, "send_message", send_message)
  45. await settings.apply_point_rules(-100, {"checkin_button_enabled": True})
  46. send_message.assert_not_awaited()
  47. rules = await settings.apply_point_rules(-100, {"enabled": False})
  48. assert rules["checkin_button_enabled"] is True
  49. reply_markup = send_message.await_args.kwargs["reply_markup"]
  50. assert isinstance(reply_markup, ReplyKeyboardRemove)
  51. async def test_checkin_button_send_failure_rolls_back_rule(app_modules, monkeypatch):
  52. points = app_modules.load("wbb.utils.dbpoints")
  53. settings = app_modules.load("wbb.services.point_settings")
  54. await points.set_point_rules(
  55. -100, {"enabled": True, "checkin_enabled": True}
  56. )
  57. monkeypatch.setattr(
  58. settings.app,
  59. "send_message",
  60. AsyncMock(side_effect=RuntimeError("telegram unavailable")),
  61. )
  62. with pytest.raises(points.PointsError, match="无法更新群底部签到按钮"):
  63. await settings.apply_point_rules(
  64. -100, {"checkin_button_enabled": True}
  65. )
  66. rules = await points.get_point_rules(-100)
  67. assert rules["checkin_button_enabled"] is False