astrbot_plugin_imagestudio/tests/test_event_routes.py

64 行
2.5 KiB
Python

2026-07-05 16:20:05 +08:00
from __future__ import annotations
import unittest
from backend.routes.event_routes import get_event_stream
from backend.services.event_stream_service import EventStreamService
from core.event_bus import EventBus
from core.identity import RequestContext
class EventRoutesTests(unittest.TestCase):
def setUp(self):
self.event_bus = EventBus()
self.service = EventStreamService(self.event_bus)
self.ctx = RequestContext.from_astrbot(username="admin", user_id="user_001")
def test_event_stream_returns_sse_payload(self):
event = self.event_bus.publish("task.completed", {"task_id": "task_001"})
self.event_bus.publish("workflow.updated", {"workflow_id": "workflow_001"})
response = get_event_stream(self.ctx, {"limit": 10}, self.service)
self.assertTrue(response["ok"])
data = response["data"]
self.assertEqual(data["content_type"], "text/event-stream")
self.assertEqual(data["count"], 2)
self.assertIn("event: task.completed", data["body"])
self.assertEqual(data["events"][0]["event_id"], event.event_id)
def test_event_stream_filters_event_type(self):
self.event_bus.publish("task.completed", {"task_id": "task_001"})
self.event_bus.publish("workflow.updated", {"workflow_id": "workflow_001"})
response = get_event_stream(self.ctx, {"event_type": "workflow.updated"}, self.service)
self.assertTrue(response["ok"])
self.assertEqual(response["data"]["count"], 1)
self.assertEqual(response["data"]["events"][0]["event_type"], "workflow.updated")
def test_event_stream_limit_is_normalized(self):
for index in range(3):
self.event_bus.publish("task.completed", {"task_id": f"task_{index}"})
bad_limit = get_event_stream(self.ctx, {"limit": "bad"}, self.service)
zero_limit = get_event_stream(self.ctx, {"limit": 0}, self.service)
huge_limit = get_event_stream(self.ctx, {"limit": 999}, self.service)
self.assertTrue(bad_limit["ok"])
self.assertEqual(bad_limit["data"]["count"], 3)
self.assertTrue(zero_limit["ok"])
self.assertEqual(zero_limit["data"]["count"], 1)
self.assertTrue(huge_limit["ok"])
self.assertEqual(huge_limit["data"]["count"], 3)
2026-07-05 16:20:05 +08:00
def test_event_stream_requires_authentication(self):
response = get_event_stream(RequestContext(), {}, self.service)
self.assertFalse(response["ok"])
self.assertEqual(response["error_code"], "UNAUTHORIZED")
if __name__ == "__main__":
unittest.main()