test: 完善模拟赛马测试中的消息服务模拟

为 _NoopMessageService 添加基本消息记录和撤回功能,以支持 race_update 消息的防刷屏机制。这使得测试环境能更真实地模拟生产代码的行为,确保在模拟密集更新时不会因消息过多而影响测试观察。
This commit is contained in:
2026-04-07 20:29:43 +08:00
parent e445878ed8
commit 9a00d3d731

View File

@@ -196,14 +196,42 @@ class _InMemoryPointsService:
class _NoopMessageService: class _NoopMessageService:
def __init__(self):
self.last_messages: dict[str, dict[str, str]] = {}
def clear_pending_recalls(self, scope: str): def clear_pending_recalls(self, scope: str):
return if scope in self.last_messages:
del self.last_messages[scope]
async def send_with_recall(self, bot, scope, message_type, message): async def send_with_recall(self, bot, scope, message_type, message):
# Support basic recall for race_update to avoid flooding during simulation
if message_type == "race_update":
await self.recall_previous_of_type(bot, scope, "race_update")
is_group = scope.startswith("group_")
result = await bot.send_msg(
message_type="group" if is_group else "private",
group_id=int(scope.split("_", 1)[1]) if is_group else None,
user_id=int(scope.split("_", 1)[1]) if not is_group else None,
message=message,
)
if scope not in self.last_messages:
self.last_messages[scope] = {}
if isinstance(result, dict) and "message_id" in result:
self.last_messages[scope][message_type] = result["message_id"]
return "fake_msg_id" return "fake_msg_id"
async def recall_previous_of_type(self, bot, scope, message_type): async def recall_previous_of_type(self, bot, scope, message_type):
return if scope in self.last_messages and message_type in self.last_messages[scope]:
msg_id = self.last_messages[scope][message_type]
try:
await bot.delete_msg(message_id=msg_id)
except Exception:
pass
del self.last_messages[scope][message_type]
@test_simulate_race_cmd.handle() @test_simulate_race_cmd.handle()