Skip to content

Commit c6f4150

Browse files
authored
fix: avoid recording cron sends without a matching platform (#10468)
1 parent b01285a commit c6f4150

2 files changed

Lines changed: 106 additions & 1 deletion

File tree

‎astrbot/core/cron/events.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ def __init__(
5656
async def send(self, message: MessageChain) -> None:
5757
if message is None:
5858
return
59-
await self.context_obj.send_message(self.session, message)
59+
if not await self.context_obj.send_message(self.session, message):
60+
return
6061
await super().send(message)
6162

6263
async def send_streaming(self, generator, use_fallback: bool = False) -> None:

‎tests/unit/test_cron_events.py‎

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
"""Tests for synthetic cron event send accounting."""
2+
3+
import asyncio
4+
from types import SimpleNamespace
5+
from unittest.mock import AsyncMock
6+
7+
import pytest
8+
9+
from astrbot.core.cron.events import CronMessageEvent
10+
from astrbot.core.message.message_event_result import MessageChain
11+
from astrbot.core.platform.message_session import MessageSession
12+
from astrbot.core.star.context import Context
13+
14+
15+
@pytest.fixture
16+
def cron_event(monkeypatch):
17+
"""Create a real event with controlled transport and metric upload.
18+
19+
Args:
20+
monkeypatch: Replaces network metric upload.
21+
22+
Returns:
23+
Event, transport mock and metric mock.
24+
"""
25+
send = AsyncMock(return_value=True)
26+
metric = AsyncMock()
27+
monkeypatch.setattr("astrbot.core.platform.astr_message_event.Metric.upload", metric)
28+
event = CronMessageEvent(
29+
context=SimpleNamespace(send_message=send),
30+
session=MessageSession.from_str("test:FriendMessage:session-1"),
31+
message="scheduled task",
32+
)
33+
return event, send, metric
34+
35+
36+
@pytest.mark.asyncio
37+
@pytest.mark.parametrize(
38+
"outcomes", [[False], [True], [False, True], [True, False], [False, False]]
39+
)
40+
@pytest.mark.parametrize("streaming", [False, True])
41+
async def test_send_accounting_respects_context_result(cron_event, outcomes, streaming):
42+
event, send, metric = cron_event
43+
send.side_effect = outcomes
44+
45+
async def chunks():
46+
for _ in outcomes:
47+
yield MessageChain().message("result")
48+
49+
if streaming:
50+
await event.send_streaming(chunks())
51+
else:
52+
sent = False
53+
for outcome in outcomes:
54+
assert await event.send(MessageChain().message("result")) is None
55+
sent = sent or outcome
56+
assert event._has_send_oper is sent
57+
await asyncio.sleep(0)
58+
59+
assert event._has_send_oper is any(outcomes)
60+
assert send.await_count == len(outcomes)
61+
assert metric.await_count == sum(outcomes)
62+
63+
64+
@pytest.mark.asyncio
65+
@pytest.mark.parametrize("prior_send", [False, True])
66+
async def test_send_exception_preserves_accounting(cron_event, prior_send):
67+
event, send, metric = cron_event
68+
event._has_send_oper = prior_send
69+
error = OSError("controlled transport error")
70+
send.side_effect = error
71+
72+
with pytest.raises(OSError) as caught:
73+
await event.send(MessageChain().message("result"))
74+
await asyncio.sleep(0)
75+
76+
assert caught.value is error
77+
assert event._has_send_oper is prior_send
78+
metric.assert_not_awaited()
79+
80+
81+
@pytest.mark.asyncio
82+
async def test_none_message_does_not_send(cron_event):
83+
event, send, metric = cron_event
84+
await event.send(None)
85+
await asyncio.sleep(0)
86+
87+
assert event._has_send_oper is False
88+
send.assert_not_awaited()
89+
metric.assert_not_awaited()
90+
91+
92+
@pytest.mark.asyncio
93+
async def test_missing_platform_does_not_record_send(cron_event, caplog):
94+
event, _, metric = cron_event
95+
context = object.__new__(Context)
96+
context.platform_manager = SimpleNamespace(platform_insts=[])
97+
event.context_obj = context
98+
99+
await event.send(MessageChain().message("result"))
100+
await asyncio.sleep(0)
101+
102+
assert event._has_send_oper is False
103+
metric.assert_not_awaited()
104+
assert "cannot find platform" in caplog.text

0 commit comments

Comments
 (0)