feat: add WebSocket announce manager
Made-with: Cursor
This commit is contained in:
45
tests/test_websocket.py
Normal file
45
tests/test_websocket.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import pytest
|
||||
from ntr_fetcher.websocket import AnnounceManager
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def manager():
|
||||
return AnnounceManager()
|
||||
|
||||
|
||||
def test_no_subscribers_initially(manager):
|
||||
assert manager.subscriber_count == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_subscribe_and_broadcast(manager):
|
||||
received = []
|
||||
|
||||
class FakeWS:
|
||||
async def send_json(self, data):
|
||||
received.append(data)
|
||||
|
||||
ws = FakeWS()
|
||||
manager.add_subscriber(ws)
|
||||
assert manager.subscriber_count == 1
|
||||
|
||||
await manager.broadcast({"type": "announce", "message": "Now Playing: Song #1"})
|
||||
assert len(received) == 1
|
||||
assert received[0]["message"] == "Now Playing: Song #1"
|
||||
|
||||
manager.remove_subscriber(ws)
|
||||
assert manager.subscriber_count == 0
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_broadcast_skips_dead_connections(manager):
|
||||
class DeadWS:
|
||||
async def send_json(self, data):
|
||||
raise Exception("connection closed")
|
||||
|
||||
ws = DeadWS()
|
||||
manager.add_subscriber(ws)
|
||||
assert manager.subscriber_count == 1
|
||||
|
||||
await manager.broadcast({"type": "announce", "message": "test"})
|
||||
assert manager.subscriber_count == 0
|
||||
Reference in New Issue
Block a user