astrbot_plugin_moviepilot/tests/test_notification_service.py

78 行
2.1 KiB
Python

from pathlib import Path
import pytest
from notification_service import (
NotificationPoller,
fixed_notification_targets,
format_notification,
)
class FakeResponse:
def __init__(self, data):
self.data = data
class FakeClient:
def __init__(self, data):
self.data = data
async def get(self, path, *, params=None):
return FakeResponse(self.data)
@pytest.mark.asyncio
async def test_first_poll_seeds_cursor_without_replaying_history(tmp_path: Path) -> None:
sent = []
poller = NotificationPoller(
{"push_group_id": "200", "push_platform_id": "aiocqhttp"},
tmp_path / "cursor.json",
lambda target, text: _append(sent, target, text),
)
poller._client = FakeClient([{"id": 1, "title": "旧通知", "text": "old"}])
assert await poller.poll_once() == []
assert sent == []
assert (tmp_path / "cursor.json").exists()
@pytest.mark.asyncio
async def test_new_notifications_are_sent_once(tmp_path: Path) -> None:
sent = []
poller = NotificationPoller(
{"push_group_id": "200", "push_platform_id": "aiocqhttp"},
tmp_path / "cursor.json",
lambda target, text: _append(sent, target, text),
)
poller._client = FakeClient([{"id": 1, "title": "旧", "text": "old"}])
await poller.poll_once()
poller._client.data = [
{"id": 1, "title": "旧", "text": "old"},
{"id": 2, "title": "新", "text": "done"},
]
assert len(await poller.poll_once()) == 1
assert len(await poller.poll_once()) == 0
assert len(sent) == 1
assert "新" in sent[0][1]
def test_targets_and_formatting() -> None:
targets = fixed_notification_targets(
{
"push_platform_id": "aiocqhttp",
"push_group_id": "200",
"push_private_user_id": "100",
}
)
text = format_notification({"id": 2, "title": "下载完成", "text": "沙丘"})
assert targets == ["aiocqhttp:GroupMessage:200", "aiocqhttp:FriendMessage:100"]
assert "下载完成" in text
assert "通知 ID:2" in text
async def _append(items, target, text):
items.append((target, text))