forked from nbd-wtf/go-nostr
-
Notifications
You must be signed in to change notification settings - Fork 1
/
filter_test.go
107 lines (93 loc) · 2.45 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
package nostr
import (
"encoding/json"
"testing"
"time"
"golang.org/x/exp/slices"
)
func TestFilterUnmarshal(t *testing.T) {
raw := `{"ids": ["abc"],"#e":["zzz"],"#something":["nothing","bab"],"since":1644254609}`
var f Filter
err := json.Unmarshal([]byte(raw), &f)
if err != nil {
t.Errorf("failed to parse filter json: %v", err)
}
if f.Since == nil || f.Since.Format("2006-01-02") != "2022-02-07" ||
f.Until != nil ||
f.Tags == nil || len(f.Tags) != 2 || !slices.Contains(f.Tags["something"], "bab") {
t.Error("failed to parse filter correctly")
}
}
func TestFilterMarshal(t *testing.T) {
tm := time.Unix(12345678, 0)
filterj, err := json.Marshal(Filter{
Kinds: []int{1, 2, 4},
Tags: TagMap{"fruit": {"banana", "mango"}},
Until: &tm,
})
if err != nil {
t.Errorf("failed to marshal filter json: %v", err)
}
expected := `{"kinds":[1,2,4],"until":12345678,"#fruit":["banana","mango"]}`
if string(filterj) != expected {
t.Errorf("filter json was wrong: %s != %s", string(filterj), expected)
}
}
func TestFilterMatching(t *testing.T) {
if (Filter{Kinds: []int{4, 5}}).Matches(&Event{Kind: 6}) {
t.Error("matched event that shouldn't have matched")
}
if !(Filter{Kinds: []int{4, 5}}).Matches(&Event{Kind: 4}) {
t.Error("failed to match event by kind")
}
if !(Filter{
Kinds: []int{4, 5},
Tags: TagMap{
"p": {"ooo"},
},
IDs: []string{"prefix"},
}).Matches(&Event{
Kind: 4,
Tags: Tags{{"p", "ooo", ",x,x,"}, {"m", "yywyw", "xxx"}},
ID: "prefix123",
}) {
t.Error("failed to match event by kind+tags+id prefix")
}
}
func TestFilterEquality(t *testing.T) {
if !FilterEqual(
Filter{Kinds: []int{4, 5}},
Filter{Kinds: []int{4, 5}},
) {
t.Error("kinds filters should be equal")
}
if !FilterEqual(
Filter{Kinds: []int{4, 5}, Tags: TagMap{"letter": {"a", "b"}}},
Filter{Kinds: []int{4, 5}, Tags: TagMap{"letter": {"b", "a"}}},
) {
t.Error("kind+tags filters should be equal")
}
tm := time.Now()
if !FilterEqual(
Filter{
Kinds: []int{4, 5},
Tags: TagMap{"letter": {"a", "b"}, "fruit": {"banana"}},
Since: &tm,
IDs: []string{"aaaa", "bbbb"},
},
Filter{
Kinds: []int{5, 4},
Tags: TagMap{"letter": {"a", "b"}, "fruit": {"banana"}},
Since: &tm,
IDs: []string{"aaaa", "bbbb"},
},
) {
t.Error("kind+2tags+since+ids filters should be equal")
}
if FilterEqual(
Filter{Kinds: []int{1, 4, 5}},
Filter{Kinds: []int{4, 5, 6}},
) {
t.Error("kinds filters shouldn't be equal")
}
}