"""Relay round-trip tests — events published to the relay come back to a client. Pure WebSocket: publish, subscribe, and read the event back, both from the store and from a live subscription. This is the relay's own end-to-end path (parse → verify → store → filter → deliver) with no browser and no app build. The half that used to share this file — publish an event, check the browser feed renders it — is an assertion about the web app, and lives with the app in ../smesh/test/test_feed_roundtrip.py. """ import json import uuid from nostr_helpers import make_event, TEST_SECKEY, TEST_PUBKEY class TestWSRoundTrip: """Pure WebSocket relay round-trip — no browser needed.""" def test_publish_and_receive(self, relay): """Publish an event, subscribe, and verify it comes back.""" import websocket ws_url = relay["ws"] marker = f"ws-roundtrip-{uuid.uuid4().hex[:8]}" ev = make_event(TEST_SECKEY, marker, kind=1) # Publish. pub = websocket.create_connection(ws_url, timeout=5) pub.send(json.dumps(["EVENT", ev])) ok_raw = pub.recv() pub.close() ok = json.loads(ok_raw) assert ok[0] == "OK", f"expected OK, got {ok}" assert ok[2] is True, f"relay rejected event: {ok}" # Subscribe and fetch. sub = websocket.create_connection(ws_url, timeout=5) sub_id = f"test-{uuid.uuid4().hex[:8]}" sub.send( json.dumps(["REQ", sub_id, {"ids": [ev["id"]]}]) ) found = False for _ in range(20): msg = json.loads(sub.recv()) if msg[0] == "EVENT" and msg[1] == sub_id: assert msg[2]["id"] == ev["id"] assert msg[2]["content"] == marker found = True break if msg[0] == "EOSE": break sub.send(json.dumps(["CLOSE", sub_id])) sub.close() assert found, "event not received back from relay" def test_live_subscription(self, relay): """Subscribe first, then publish — verify live delivery.""" import websocket ws_url = relay["ws"] marker = f"live-{uuid.uuid4().hex[:8]}" sub_id = f"live-{uuid.uuid4().hex[:8]}" # Subscribe to our test pubkey. sub = websocket.create_connection(ws_url, timeout=10) sub.send( json.dumps( ["REQ", sub_id, {"authors": [TEST_PUBKEY], "kinds": [1], "limit": 5}] ) ) # Drain EOSE. while True: msg = json.loads(sub.recv()) if msg[0] == "EOSE": break # Now publish. ev = make_event(TEST_SECKEY, marker, kind=1) pub = websocket.create_connection(ws_url, timeout=5) pub.send(json.dumps(["EVENT", ev])) pub.recv() # OK pub.close() # Should arrive on the subscription. sub.settimeout(5) msg = json.loads(sub.recv()) assert msg[0] == "EVENT" assert msg[2]["content"] == marker sub.send(json.dumps(["CLOSE", sub_id])) sub.close()