-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathadd_event_test.go
More file actions
243 lines (228 loc) · 6.06 KB
/
Copy pathadd_event_test.go
File metadata and controls
243 lines (228 loc) · 6.06 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package relayer
import (
"context"
"errors"
"strings"
"testing"
"github.com/nbd-wtf/go-nostr"
)
func TestAddEvent(t *testing.T) {
t.Run("nil event", func(t *testing.T) {
rl := &testRelay{storage: &testStorage{}}
accepted, msg := AddEvent(context.Background(), rl, nil)
if accepted || msg != "" {
t.Errorf("got (%v, %q), want (false, \"\")", accepted, msg)
}
})
t.Run("rejected by relay", func(t *testing.T) {
rl := &testRelay{
storage: &testStorage{},
acceptEvent: func(e *nostr.Event) (bool, string) {
return false, "blocked: custom"
},
}
accepted, msg := AddEvent(context.Background(), rl, &nostr.Event{Kind: 1})
if accepted {
t.Error("expected rejection")
}
if msg != "blocked: custom" {
t.Errorf("got %q", msg)
}
})
t.Run("rejected default message", func(t *testing.T) {
rl := &testRelay{
storage: &testStorage{},
acceptEvent: func(e *nostr.Event) (bool, string) {
return false, ""
},
}
accepted, msg := AddEvent(context.Background(), rl, &nostr.Event{Kind: 1})
if accepted {
t.Error("expected rejection")
}
if msg != "blocked: event blocked by relay" {
t.Errorf("got %q", msg)
}
})
t.Run("ephemeral event not saved", func(t *testing.T) {
clearListeners()
defer clearListeners()
saveCalled := false
rl := &testRelay{
storage: &testStorage{
saveEvent: func(_ context.Context, _ *nostr.Event) error {
saveCalled = true
return nil
},
},
}
accepted, _ := AddEvent(context.Background(), rl, &nostr.Event{Kind: 25000})
if !accepted {
t.Error("expected acceptance")
}
if saveCalled {
t.Error("SaveEvent should not be called for ephemeral events")
}
})
t.Run("ephemeral boundary low", func(t *testing.T) {
clearListeners()
defer clearListeners()
saveCalled := false
rl := &testRelay{
storage: &testStorage{
saveEvent: func(_ context.Context, _ *nostr.Event) error {
saveCalled = true
return nil
},
},
}
AddEvent(context.Background(), rl, &nostr.Event{Kind: 20000})
if saveCalled {
t.Error("kind 20000 should be ephemeral")
}
})
t.Run("ephemeral boundary high", func(t *testing.T) {
clearListeners()
defer clearListeners()
saveCalled := false
rl := &testRelay{
storage: &testStorage{
saveEvent: func(_ context.Context, _ *nostr.Event) error {
saveCalled = true
return nil
},
},
}
AddEvent(context.Background(), rl, &nostr.Event{Kind: 29999})
if saveCalled {
t.Error("kind 29999 should be ephemeral")
}
})
t.Run("non-ephemeral kind 30000", func(t *testing.T) {
clearListeners()
defer clearListeners()
called := false
rl := &testRelay{
storage: &testStorage{
saveEvent: func(_ context.Context, _ *nostr.Event) error {
called = true
return nil
},
replaceEvent: func(_ context.Context, _ *nostr.Event) error {
called = true
return nil
},
},
}
AddEvent(context.Background(), rl, &nostr.Event{Kind: 30000, Tags: nostr.Tags{{"d", ""}}})
if !called {
t.Error("kind 30000 should be saved")
}
})
t.Run("save success", func(t *testing.T) {
clearListeners()
defer clearListeners()
saved := false
rl := &testRelay{
storage: &testStorage{
saveEvent: func(_ context.Context, _ *nostr.Event) error {
saved = true
return nil
},
},
}
accepted, msg := AddEvent(context.Background(), rl, &nostr.Event{Kind: 1})
if !accepted || msg != "" {
t.Errorf("got (%v, %q), want (true, \"\")", accepted, msg)
}
if !saved {
t.Error("SaveEvent not called")
}
})
t.Run("duplicate event via wrapper", func(t *testing.T) {
clearListeners()
defer clearListeners()
// eventstore.RelayWrapper.Publish handles dups silently (returns nil)
rl := &testRelay{
storage: &testStorage{},
}
accepted, msg := AddEvent(context.Background(), rl, &nostr.Event{Kind: 1})
if !accepted {
t.Error("expected acceptance")
}
if msg != "" {
t.Errorf("got %q", msg)
}
})
t.Run("save error", func(t *testing.T) {
rl := &testRelay{
storage: &testStorage{
saveEvent: func(_ context.Context, _ *nostr.Event) error {
return errors.New("db connection failed")
},
},
}
accepted, msg := AddEvent(context.Background(), rl, &nostr.Event{Kind: 1})
if accepted {
t.Error("expected rejection")
}
if !strings.Contains(msg, "db connection failed") {
t.Errorf("expected error containing 'db connection failed', got %q", msg)
}
})
t.Run("replace error", func(t *testing.T) {
rl := &testRelay{
storage: &testStorage{
replaceEvent: func(_ context.Context, _ *nostr.Event) error {
return errors.New("replace failed")
},
},
}
accepted, msg := AddEvent(context.Background(), rl, &nostr.Event{Kind: 30000, Tags: nostr.Tags{{"d", ""}}})
if accepted {
t.Error("expected rejection")
}
if !strings.Contains(msg, "replace failed") {
t.Errorf("expected error containing 'replace failed', got %q", msg)
}
})
t.Run("AdvancedSaver hooks called", func(t *testing.T) {
clearListeners()
defer clearListeners()
var beforeCalled, afterCalled bool
st := &testAdvancedStorage{
testStorage: testStorage{
saveEvent: func(_ context.Context, _ *nostr.Event) error { return nil },
},
beforeSave: func(_ context.Context, _ *nostr.Event) { beforeCalled = true },
afterSave: func(_ *nostr.Event) { afterCalled = true },
}
rl := &testRelay{storage: st}
accepted, _ := AddEvent(context.Background(), rl, &nostr.Event{Kind: 1})
if !accepted {
t.Error("expected acceptance")
}
if !beforeCalled {
t.Error("BeforeSave not called")
}
if !afterCalled {
t.Error("AfterSave not called")
}
})
t.Run("AdvancedSaver AfterSave not called on error", func(t *testing.T) {
afterCalled := false
st := &testAdvancedStorage{
testStorage: testStorage{
saveEvent: func(_ context.Context, _ *nostr.Event) error {
return errors.New("save failed")
},
},
afterSave: func(_ *nostr.Event) { afterCalled = true },
}
rl := &testRelay{storage: st}
AddEvent(context.Background(), rl, &nostr.Event{Kind: 1})
if afterCalled {
t.Error("AfterSave should not be called on save error")
}
})
}