|
| 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