from __future__ import annotations from unittest.mock import AsyncMock, MagicMock import pytest from yuxi.channel.interfaces.sse.endpoint import SSEEndpoint class TestSSEEndpoint: @pytest.fixture def endpoint(self): return SSEEndpoint() @pytest.mark.asyncio async def test_start(self, endpoint): await endpoint.start() assert endpoint.is_running is True @pytest.mark.asyncio async def test_stop(self, endpoint): await endpoint.start() await endpoint.stop() assert endpoint.is_running is False @pytest.mark.asyncio async def test_subscribe(self, endpoint): await endpoint.start() queue = await endpoint.subscribe("client1") assert "client1" in endpoint._clients @pytest.mark.asyncio async def test_unsubscribe(self, endpoint): await endpoint.start() queue = await endpoint.subscribe("client1") await endpoint.unsubscribe("client1") assert "client1" not in endpoint._clients @pytest.mark.asyncio async def test_broadcast(self, endpoint): await endpoint.start() queue = await endpoint.subscribe("client1") await endpoint.broadcast({"type": "message", "data": "test"}) @pytest.mark.asyncio async def test_broadcast_shutdown(self, endpoint): await endpoint.start() queue = await endpoint.subscribe("client1") await endpoint.broadcast_shutdown() @pytest.mark.asyncio async def test_send_to_client(self, endpoint): await endpoint.start() queue = await endpoint.subscribe("client1") await endpoint.send_to_client("client1", {"type": "message", "data": "test"}) @pytest.mark.asyncio async def test_send_to_nonexistent_client(self, endpoint): await endpoint.start() await endpoint.send_to_client("nonexistent", {"type": "message", "data": "test"}) @pytest.mark.asyncio async def test_get_client_queue(self, endpoint): await endpoint.start() queue = await endpoint.subscribe("client1") result = endpoint.get_client_queue("client1") assert result is not None def test_get_client_queue_not_found(self, endpoint): result = endpoint.get_client_queue("nonexistent") assert result is None