test_relay_roundtrip.py raw

   1  """Relay round-trip tests — events published to the relay come back to a client.
   2  
   3  Pure WebSocket: publish, subscribe, and read the event back, both from the
   4  store and from a live subscription. This is the relay's own end-to-end path
   5  (parse → verify → store → filter → deliver) with no browser and no app build.
   6  
   7  The half that used to share this file — publish an event, check the browser
   8  feed renders it — is an assertion about the web app, and lives with the app in
   9  ../smesh/test/test_feed_roundtrip.py.
  10  """
  11  
  12  import json
  13  import uuid
  14  
  15  from nostr_helpers import make_event, TEST_SECKEY, TEST_PUBKEY
  16  
  17  
  18  class TestWSRoundTrip:
  19      """Pure WebSocket relay round-trip — no browser needed."""
  20  
  21      def test_publish_and_receive(self, relay):
  22          """Publish an event, subscribe, and verify it comes back."""
  23          import websocket
  24  
  25          ws_url = relay["ws"]
  26          marker = f"ws-roundtrip-{uuid.uuid4().hex[:8]}"
  27          ev = make_event(TEST_SECKEY, marker, kind=1)
  28  
  29          # Publish.
  30          pub = websocket.create_connection(ws_url, timeout=5)
  31          pub.send(json.dumps(["EVENT", ev]))
  32          ok_raw = pub.recv()
  33          pub.close()
  34          ok = json.loads(ok_raw)
  35          assert ok[0] == "OK", f"expected OK, got {ok}"
  36          assert ok[2] is True, f"relay rejected event: {ok}"
  37  
  38          # Subscribe and fetch.
  39          sub = websocket.create_connection(ws_url, timeout=5)
  40          sub_id = f"test-{uuid.uuid4().hex[:8]}"
  41          sub.send(
  42              json.dumps(["REQ", sub_id, {"ids": [ev["id"]]}])
  43          )
  44  
  45          found = False
  46          for _ in range(20):
  47              msg = json.loads(sub.recv())
  48              if msg[0] == "EVENT" and msg[1] == sub_id:
  49                  assert msg[2]["id"] == ev["id"]
  50                  assert msg[2]["content"] == marker
  51                  found = True
  52                  break
  53              if msg[0] == "EOSE":
  54                  break
  55  
  56          sub.send(json.dumps(["CLOSE", sub_id]))
  57          sub.close()
  58          assert found, "event not received back from relay"
  59  
  60      def test_live_subscription(self, relay):
  61          """Subscribe first, then publish — verify live delivery."""
  62          import websocket
  63  
  64          ws_url = relay["ws"]
  65          marker = f"live-{uuid.uuid4().hex[:8]}"
  66          sub_id = f"live-{uuid.uuid4().hex[:8]}"
  67  
  68          # Subscribe to our test pubkey.
  69          sub = websocket.create_connection(ws_url, timeout=10)
  70          sub.send(
  71              json.dumps(
  72                  ["REQ", sub_id, {"authors": [TEST_PUBKEY], "kinds": [1], "limit": 5}]
  73              )
  74          )
  75          # Drain EOSE.
  76          while True:
  77              msg = json.loads(sub.recv())
  78              if msg[0] == "EOSE":
  79                  break
  80  
  81          # Now publish.
  82          ev = make_event(TEST_SECKEY, marker, kind=1)
  83          pub = websocket.create_connection(ws_url, timeout=5)
  84          pub.send(json.dumps(["EVENT", ev]))
  85          pub.recv()  # OK
  86          pub.close()
  87  
  88          # Should arrive on the subscription.
  89          sub.settimeout(5)
  90          msg = json.loads(sub.recv())
  91          assert msg[0] == "EVENT"
  92          assert msg[2]["content"] == marker
  93  
  94          sub.send(json.dumps(["CLOSE", sub_id]))
  95          sub.close()
  96