| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- from __future__ import annotations
- from types import SimpleNamespace
- from unittest.mock import AsyncMock
- import pytest
- from pyrogram.types import ReplyKeyboardMarkup, ReplyKeyboardRemove
- async def test_enabling_checkin_button_sends_persistent_keyboard(
- app_modules, monkeypatch
- ):
- points = app_modules.load("wbb.utils.dbpoints")
- settings = app_modules.load("wbb.services.point_settings")
- await points.set_point_rules(
- -100, {"enabled": True, "checkin_enabled": True}
- )
- send_message = AsyncMock(return_value=SimpleNamespace(id=1))
- monkeypatch.setattr(settings.app, "send_message", send_message)
- rules = await settings.apply_point_rules(
- -100, {"checkin_button_enabled": True}
- )
- assert rules["checkin_button_enabled"] is True
- send_message.assert_awaited_once()
- chat_id, text = send_message.await_args.args
- reply_markup = send_message.await_args.kwargs["reply_markup"]
- assert chat_id == -100
- assert "签到快捷按钮已开启" in text
- assert isinstance(reply_markup, ReplyKeyboardMarkup)
- assert reply_markup.is_persistent is True
- assert reply_markup.resize_keyboard is True
- assert reply_markup.keyboard[0][0].text == "签到"
- async def test_checkin_button_sync_is_idempotent_and_follows_points_state(
- app_modules, monkeypatch
- ):
- points = app_modules.load("wbb.utils.dbpoints")
- settings = app_modules.load("wbb.services.point_settings")
- await points.set_point_rules(
- -100,
- {
- "enabled": True,
- "checkin_enabled": True,
- "checkin_button_enabled": True,
- },
- )
- send_message = AsyncMock(return_value=SimpleNamespace(id=1))
- monkeypatch.setattr(settings.app, "send_message", send_message)
- await settings.apply_point_rules(-100, {"checkin_button_enabled": True})
- send_message.assert_not_awaited()
- rules = await settings.apply_point_rules(-100, {"enabled": False})
- assert rules["checkin_button_enabled"] is True
- reply_markup = send_message.await_args.kwargs["reply_markup"]
- assert isinstance(reply_markup, ReplyKeyboardRemove)
- async def test_checkin_button_send_failure_rolls_back_rule(app_modules, monkeypatch):
- points = app_modules.load("wbb.utils.dbpoints")
- settings = app_modules.load("wbb.services.point_settings")
- await points.set_point_rules(
- -100, {"enabled": True, "checkin_enabled": True}
- )
- monkeypatch.setattr(
- settings.app,
- "send_message",
- AsyncMock(side_effect=RuntimeError("telegram unavailable")),
- )
- with pytest.raises(points.PointsError, match="无法更新群底部签到按钮"):
- await settings.apply_point_rules(
- -100, {"checkin_button_enabled": True}
- )
- rules = await points.get_point_rules(-100)
- assert rules["checkin_button_enabled"] is False
|