forked from nbd-wtf/go-nostr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilter_test.go
284 lines (245 loc) · 8.46 KB
/
filter_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
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package nostr
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestFilterUnmarshal(t *testing.T) {
t.Parallel()
raw := `{"ids": ["abc"],"#e":["zzz"],"#something":[["nothing","bab"]],"since":1644254609,"search":"test"}`
var f Filter
err := json.Unmarshal([]byte(raw), &f)
require.NoError(t, err)
require.Equal(t, "test", f.Search)
require.Nil(t, f.Until)
require.NotNil(t, f.Since)
require.Equal(t, "2022-02-07", f.Since.Time().UTC().Format("2006-01-02"))
require.Len(t, f.Tags, 2)
require.Contains(t, f.Tags, "something")
require.Contains(t, f.Tags, "e")
require.Len(t, f.Tags["something"], 1)
require.Len(t, f.Tags["e"], 1)
require.Equal(t, "nothing", *f.Tags["something"][0][0])
require.Equal(t, "bab", *f.Tags["something"][0][1])
require.Equal(t, "zzz", *f.Tags["e"][0][0])
}
func TestFilterMarshal(t *testing.T) {
t.Parallel()
until := Timestamp(12345678)
filterj, err := json.Marshal(Filter{
Kinds: []int{KindTextNote, KindRecommendServer, KindEncryptedDirectMessage},
Tags: TagMap{}.SetLiterals("fruit", "banana", "mango"),
Until: &until,
})
require.NoError(t, err)
expected := `{"kinds":[1,2,4],"until":12345678,"#fruit":[["banana","mango"]]}`
require.Equal(t, expected, string(filterj))
}
func TestFilterUnmarshalWithLimitZero(t *testing.T) {
t.Parallel()
raw := `{"ids": ["abc"],"#e":["zzz"],"limit":0,"#something":["nothing","bab"],"since":1644254609,"search":"test"}`
var f Filter
err := json.Unmarshal([]byte(raw), &f)
require.NoError(t, err)
require.Equal(t, "test", f.Search)
require.Nil(t, f.Until)
require.NotNil(t, f.Since)
require.Equal(t, "2022-02-07", f.Since.Time().UTC().Format("2006-01-02"))
require.Len(t, f.Tags, 2)
require.Contains(t, f.Tags, "something")
require.Contains(t, f.Tags, "e")
require.True(t, f.LimitZero)
}
func TestFilterMarshalWithLimitZero(t *testing.T) {
t.Parallel()
until := Timestamp(12345678)
filterj, err := json.Marshal(Filter{
Kinds: []int{KindTextNote, KindRecommendServer, KindEncryptedDirectMessage},
Tags: TagMap{}.SetLiterals("fruit", "banana", "mango"),
Until: &until,
LimitZero: true,
})
require.NoError(t, err)
expected := `{"kinds":[1,2,4],"until":12345678,"limit":0,"#fruit":[["banana","mango"]]}`
require.Equal(t, expected, string(filterj))
}
func TestFilterMatchingLive(t *testing.T) {
t.Parallel()
var filter Filter
var event Event
json.Unmarshal([]byte(`{"kinds":[1],"authors":["a8171781fd9e90ede3ea44ddca5d3abf828fe8eedeb0f3abb0dd3e563562e1fc","1d80e5588de010d137a67c42b03717595f5f510e73e42cfc48f31bae91844d59","ed4ca520e9929dfe9efdadf4011b53d30afd0678a09aa026927e60e7a45d9244"],"since":1677033299}`), &filter)
json.Unmarshal([]byte(`{"id":"5a127c9c931f392f6afc7fdb74e8be01c34035314735a6b97d2cf360d13cfb94","pubkey":"1d80e5588de010d137a67c42b03717595f5f510e73e42cfc48f31bae91844d59","created_at":1677033299,"kind":1,"tags":[["t","japan"]],"content":"If you like my art,I'd appreciate a coin or two!!\nZap is welcome!! Thanks.\n\n\n#japan #bitcoin #art #bananaart\nhttps://void.cat/d/CgM1bzDgHUCtiNNwfX9ajY.webp","sig":"828497508487ca1e374f6b4f2bba7487bc09fccd5cc0d1baa82846a944f8c5766918abf5878a580f1e6615de91f5b57a32e34c42ee2747c983aaf47dbf2a0255"}`), &event)
require.True(t, filter.Matches(&event), "live filter should match")
}
func TestFilterEquality(t *testing.T) {
t.Parallel()
require.True(t, FilterEqual(
Filter{Kinds: []int{KindEncryptedDirectMessage, KindDeletion}},
Filter{Kinds: []int{KindEncryptedDirectMessage, KindDeletion}},
), "kinds filters should be equal")
require.True(t, FilterEqual(
Filter{Kinds: []int{KindEncryptedDirectMessage, KindDeletion}, Tags: TagMap{}.SetLiterals("letter", "a", "b")},
Filter{Kinds: []int{KindEncryptedDirectMessage, KindDeletion}, Tags: TagMap{}.SetLiterals("letter", "a", "b")},
), "kind+tags filters should be equal")
tm := Now()
require.True(t, FilterEqual(
Filter{
Kinds: []int{KindEncryptedDirectMessage, KindDeletion},
Tags: TagMap{}.SetLiterals("letter", "a", "b").SetLiterals("fruit", "banana"),
Since: &tm,
IDs: []string{"aaaa", "bbbb"},
},
Filter{
Kinds: []int{KindDeletion, KindEncryptedDirectMessage},
Tags: TagMap{}.SetLiterals("letter", "a", "b").SetLiterals("fruit", "banana"),
Since: &tm,
IDs: []string{"aaaa", "bbbb"},
},
), "kind+2tags+since+ids filters should be equal")
require.False(t, FilterEqual(
Filter{Kinds: []int{KindTextNote, KindEncryptedDirectMessage, KindDeletion}},
Filter{Kinds: []int{KindEncryptedDirectMessage, KindDeletion, KindRepost}},
), "kinds filters shouldn't be equal")
}
func TestFilterClone(t *testing.T) {
t.Parallel()
ts := Now() - 60*60
flt := Filter{
Kinds: []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
Tags: TagMap{}.SetLiterals("letter", "a", "b").SetLiterals("fruit", "banana"),
Since: &ts,
IDs: []string{"9894b4b5cb5166d23ee8899a4151cf0c66aec00bde101982a13b8e8ceb972df9"},
}
clone := flt.Clone()
require.True(t, FilterEqual(flt, clone), "clone is not equal:\n %v !=\n %v", flt, clone)
clone1 := flt.Clone()
clone1.IDs = append(clone1.IDs, "88f0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d")
require.False(t, FilterEqual(flt, clone1), "modifying the clone ids should cause it to not be equal anymore")
clone2 := flt.Clone()
clone2.Tags.Append("letter", ptr("c"))
require.False(t, FilterEqual(flt, clone2), "modifying the clone tag items should cause it to not be equal anymore")
clone3 := flt.Clone()
clone3.Tags.SetLiterals("g", "drt")
require.False(t, FilterEqual(flt, clone3), "modifying the clone tag map should cause it to not be equal anymore")
clone4 := flt.Clone()
*clone4.Since++
require.False(t, FilterEqual(flt, clone4), "modifying the clone since should cause it to not be equal anymore")
}
func TestTheoreticalLimit(t *testing.T) {
require.Equal(t, 6, GetTheoreticalLimit(Filter{IDs: []string{"a", "b", "c", "d", "e", "f"}}))
require.Equal(t, 9, GetTheoreticalLimit(Filter{Authors: []string{"a", "b", "c"}, Kinds: []int{3, 0, 10002}}))
require.Equal(t, 4, GetTheoreticalLimit(Filter{Authors: []string{"a", "b", "c", "d"}, Kinds: []int{10050}}))
require.Equal(t, -1, GetTheoreticalLimit(Filter{Authors: []string{"a", "b", "c", "d"}}))
require.Equal(t, -1, GetTheoreticalLimit(Filter{Kinds: []int{3, 0, 10002}}))
require.Equal(t, 24, GetTheoreticalLimit(Filter{Authors: []string{"a", "b", "c", "d", "e", "f"}, Kinds: []int{30023, 30024}, Tags: TagMap{}.SetLiterals("d", "aaa", "bbb")}))
require.Equal(t, -1, GetTheoreticalLimit(Filter{Authors: []string{"a", "b", "c", "d", "e", "f"}, Kinds: []int{30023, 30024}}))
}
func TestFilterMatches(t *testing.T) {
t.Parallel()
var cases = []struct {
Filter Filter
Event Event
Match bool
}{
{
Filter: Filter{
Tags: TagMap{
"e": nil,
},
},
Event: Event{
Tags: Tags{{"e", "1"}},
},
Match: true,
},
{
Filter: Filter{
Tags: TagMap{
"e": []TagValues{{nil, nil}, {ptr("1")}},
},
},
Event: Event{
Tags: Tags{{"e", "1"}},
},
Match: true,
},
{
Filter: Filter{
Tags: TagMap{
"e": []TagValues{{nil, ptr("2"), nil}, {ptr("1")}},
},
},
Event: Event{
Tags: Tags{{"e", "1", "2", "3"}},
},
Match: true,
},
{
Filter: Filter{
Tags: TagMap{
"e": []TagValues{{nil, ptr("2"), nil}, {ptr("1")}},
},
},
Event: Event{
Tags: Tags{{"e", "0", "2", "3"}},
},
Match: true,
},
{
Filter: Filter{
Tags: TagMap{}.SetLiterals("e", "1", "2", "3", "4"),
},
Event: Event{
Tags: Tags{{"e", "1", "2", "3"}},
},
},
{
Filter: Filter{
Tags: TagMap{
"x": nil,
},
},
Event: Event{},
},
{
Filter: Filter{
Tags: TagMap{}.
Append("k", ptr("1")).
Append("k", ptr("2")),
},
Event: Event{
Tags: Tags{
{"k", "1"},
},
},
Match: true,
},
{
Filter: Filter{
Tags: TagMap{}.
Append("k", ptr("1"), ptr("2")),
},
Event: Event{
Tags: Tags{
{"k", "1"},
},
},
},
}
for _, c := range cases {
t.Run(c.Filter.String(), func(t *testing.T) {
r := c.Filter.Matches(&c.Event)
require.Equalf(t, c.Match, r, "event: %+v", c.Event)
})
}
}
func TestTagMapAll(t *testing.T) {
t.Parallel()
tagMap := TagMap{}.
SetLiterals("fruit", "apple", "banana").
Append("fruit", ptr("orange")).
SetLiterals("color", "red", "yellow").
Append("color", nil, ptr("blue"))
require.ElementsMatch(t, []string{"apple", "banana", "orange"}, tagMap.All("fruit"))
require.ElementsMatch(t, []string{"red", "yellow", "blue"}, tagMap.All("color"))
require.Empty(t, tagMap.All("nonexistent"))
}