relay_test.go raw
1 package bridge
2
3 import (
4 "context"
5 "testing"
6 )
7
8 func TestNewRelayConn(t *testing.T) {
9 rc := NewRelayConn("wss://relay.example.com", nil)
10 if rc == nil {
11 t.Fatal("expected non-nil RelayConn")
12 }
13 if rc.url != "wss://relay.example.com" {
14 t.Errorf("url = %q, want wss://relay.example.com", rc.url)
15 }
16 }
17
18 func TestRelayConn_PublishNotConnected(t *testing.T) {
19 rc := NewRelayConn("wss://relay.example.com", nil)
20 err := rc.Publish(context.Background(), nil)
21 if err == nil {
22 t.Fatal("expected error from Publish without connection")
23 }
24 if err.Error() != "not connected to relay" {
25 t.Errorf("error = %q", err)
26 }
27 }
28
29 func TestRelayConn_SubscribeNotConnected(t *testing.T) {
30 rc := NewRelayConn("wss://relay.example.com", nil)
31 _, err := rc.Subscribe(context.Background(), nil)
32 if err == nil {
33 t.Fatal("expected error from Subscribe without connection")
34 }
35 if err.Error() != "not connected to relay" {
36 t.Errorf("error = %q", err)
37 }
38 }
39
40 func TestRelayConn_CloseWithoutConnect(t *testing.T) {
41 rc := NewRelayConn("wss://relay.example.com", nil)
42 // Should not panic
43 rc.Close()
44 }
45
46 func TestRelayConn_CloseWithCancel(t *testing.T) {
47 rc := NewRelayConn("wss://relay.example.com", nil)
48 rc.ctx, rc.cancel = context.WithCancel(context.Background())
49 // Should cancel context and not panic
50 rc.Close()
51 }
52
53 func TestRelayConn_ConnectInvalidURL(t *testing.T) {
54 rc := NewRelayConn("not-a-valid-url", nil)
55 err := rc.Connect(context.Background())
56 if err == nil {
57 t.Fatal("expected error connecting to invalid URL")
58 }
59 }
60
61 func TestRelayConn_ReconnectCancelledContext(t *testing.T) {
62 rc := NewRelayConn("wss://nonexistent.example.com", nil)
63 ctx, cancel := context.WithCancel(context.Background())
64 rc.ctx = ctx
65 rc.cancel = cancel
66
67 // Cancel immediately so Reconnect exits on first iteration
68 cancel()
69
70 err := rc.Reconnect()
71 if err == nil {
72 t.Fatal("expected error from cancelled context")
73 }
74 }
75