Websockets instead of polling

This commit is contained in:
2026-06-12 06:19:22 -07:00
parent b72aea9747
commit ba6bf35579
8 changed files with 128 additions and 8 deletions

View File

@@ -1210,3 +1210,39 @@ def test_event_log_pagination(session):
exact, have_more = crud.get_events_page(session, game.id, before=1020.0, limit=20)
assert not have_more
assert [e.message for e in exact] == [f"event {i}" for i in range(20)]
def test_websocket_state_push():
"""A successful POST under /api/game/{gid}/ pings that game's websockets."""
from fastapi.testclient import TestClient
from sqlmodel.pool import StaticPool
from pirats.main import app
from pirats.database import get_session
engine = create_engine(
"sqlite://",
connect_args={"check_same_thread": False},
poolclass=StaticPool,
)
SQLModel.metadata.create_all(engine)
def override_session():
with Session(engine) as s:
yield s
app.dependency_overrides[get_session] = override_session
try:
client = TestClient(app)
gid = client.post("/api/game", data={"crew_name": "Testers"}).json()["id"]
with client.websocket_connect(f"/api/game/{gid}/ws") as ws:
res = client.post(f"/api/game/{gid}/join", data={"name": "Carlos"})
assert res.status_code == 200
assert ws.receive_json() == {"type": "state_changed"}
# A POST for a different game must not ping this socket
other_gid = client.post("/api/game", data={"crew_name": "Others"}).json()["id"]
client.post(f"/api/game/{other_gid}/join", data={"name": "Barry"})
client.post(f"/api/game/{gid}/join", data={"name": "Jaspar"})
assert ws.receive_json() == {"type": "state_changed"}
finally:
app.dependency_overrides.clear()