-
Notifications
You must be signed in to change notification settings - Fork 113
/
Copy pathrelay_js_test.go
77 lines (62 loc) · 1.58 KB
/
relay_js_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
//go:build js
package nostr
import (
"context"
"os"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var testRelayURL = func() string {
url := os.Getenv("TEST_RELAY_URL")
if url != "" {
return url
}
return "wss://nos.lol"
}()
func TestConnectContext(t *testing.T) {
// relay client
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
r, err := RelayConnect(ctx, testRelayURL)
assert.NoError(t, err)
defer r.Close()
}
func TestConnectContextCanceled(t *testing.T) {
// relay client
ctx, cancel := context.WithCancel(context.Background())
cancel() // make ctx expired
_, err := RelayConnect(ctx, testRelayURL)
assert.ErrorIs(t, err, context.Canceled)
}
func TestPublish(t *testing.T) {
// test note to be sent over websocket
priv, pub := makeKeyPair(t)
textNote := Event{
Kind: KindTextNote,
Content: "hello",
CreatedAt: Timestamp(1672068534), // random fixed timestamp
Tags: Tags{[]string{"foo", "bar"}},
PubKey: pub,
}
err := textNote.Sign(priv)
assert.NoError(t, err)
// connect a client and send the text note
rl := mustRelayConnect(t, testRelayURL)
err = rl.Publish(context.Background(), textNote)
assert.NoError(t, err)
}
func makeKeyPair(t *testing.T) (priv, pub string) {
t.Helper()
privkey := GeneratePrivateKey()
pubkey, err := GetPublicKey(privkey)
assert.NoError(t, err)
return privkey, pubkey
}
func mustRelayConnect(t *testing.T, url string) *Relay {
t.Helper()
rl, err := RelayConnect(context.Background(), url)
require.NoError(t, err)
return rl
}