2022-02-09 13:40:26 -03:00
package nostr
import (
"encoding/json"
"testing"
2022-11-08 07:15:08 -03:00
"golang.org/x/exp/slices"
2022-02-09 13:40:26 -03:00
)
func TestFilterUnmarshal ( t * testing . T ) {
2023-02-26 21:38:43 +09:00
raw := ` { "ids": ["abc"],"#e":["zzz"],"#something":["nothing","bab"],"since":1644254609,"search":"test"} `
2022-02-09 13:40:26 -03:00
var f Filter
2023-06-11 10:48:46 -03:00
if err := json . Unmarshal ( [ ] byte ( raw ) , & f ) ; err != nil {
2022-11-08 08:44:22 -03:00
t . Errorf ( "failed to parse filter json: %v" , err )
2022-02-09 13:40:26 -03:00
}
2023-04-16 16:12:42 -03:00
if f . Since == nil || f . Since . Time ( ) . UTC ( ) . Format ( "2006-01-02" ) != "2022-02-07" ||
2022-02-09 13:40:26 -03:00
f . Until != nil ||
2023-02-26 21:38:43 +09:00
f . Tags == nil || len ( f . Tags ) != 2 || ! slices . Contains ( f . Tags [ "something" ] , "bab" ) ||
f . Search != "test" {
2022-02-09 13:40:26 -03:00
t . Error ( "failed to parse filter correctly" )
}
}
func TestFilterMarshal ( t * testing . T ) {
2023-04-16 16:12:42 -03:00
until := Timestamp ( 12345678 )
2022-02-09 13:40:26 -03:00
filterj , err := json . Marshal ( Filter {
2022-11-08 07:15:08 -03:00
Kinds : [ ] int { 1 , 2 , 4 } ,
2022-02-09 20:25:20 -03:00
Tags : TagMap { "fruit" : { "banana" , "mango" } } ,
2023-04-16 16:12:42 -03:00
Until : & until ,
2022-02-09 13:40:26 -03:00
} )
if err != nil {
2022-11-08 08:44:22 -03:00
t . Errorf ( "failed to marshal filter json: %v" , err )
2022-02-09 13:40:26 -03:00
}
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 )
}
}
2023-03-18 08:18:08 -03:00
func TestFilterMatchingLive ( t * testing . T ) {
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 )
if ! filter . Matches ( & event ) {
t . Error ( "live filter should match" )
}
}
2022-02-09 13:40:26 -03:00
func TestFilterEquality ( t * testing . T ) {
if ! FilterEqual (
2022-11-08 07:15:08 -03:00
Filter { Kinds : [ ] int { 4 , 5 } } ,
Filter { Kinds : [ ] int { 4 , 5 } } ,
2022-02-09 13:40:26 -03:00
) {
t . Error ( "kinds filters should be equal" )
}
if ! FilterEqual (
2022-11-08 07:15:08 -03:00
Filter { Kinds : [ ] int { 4 , 5 } , Tags : TagMap { "letter" : { "a" , "b" } } } ,
Filter { Kinds : [ ] int { 4 , 5 } , Tags : TagMap { "letter" : { "b" , "a" } } } ,
2022-02-09 13:40:26 -03:00
) {
t . Error ( "kind+tags filters should be equal" )
}
2023-04-16 16:12:42 -03:00
tm := Now ( )
2022-02-09 13:40:26 -03:00
if ! FilterEqual (
Filter {
2022-11-08 07:15:08 -03:00
Kinds : [ ] int { 4 , 5 } ,
2022-02-09 20:25:20 -03:00
Tags : TagMap { "letter" : { "a" , "b" } , "fruit" : { "banana" } } ,
2022-02-09 13:40:26 -03:00
Since : & tm ,
2022-11-08 07:15:08 -03:00
IDs : [ ] string { "aaaa" , "bbbb" } ,
2022-02-09 13:40:26 -03:00
} ,
Filter {
2022-11-08 07:15:08 -03:00
Kinds : [ ] int { 5 , 4 } ,
2022-02-09 20:25:20 -03:00
Tags : TagMap { "letter" : { "a" , "b" } , "fruit" : { "banana" } } ,
2022-02-09 13:40:26 -03:00
Since : & tm ,
2022-11-08 07:15:08 -03:00
IDs : [ ] string { "aaaa" , "bbbb" } ,
2022-02-09 13:40:26 -03:00
} ,
) {
t . Error ( "kind+2tags+since+ids filters should be equal" )
}
if FilterEqual (
2022-11-08 07:15:08 -03:00
Filter { Kinds : [ ] int { 1 , 4 , 5 } } ,
Filter { Kinds : [ ] int { 4 , 5 , 6 } } ,
2022-02-09 13:40:26 -03:00
) {
t . Error ( "kinds filters shouldn't be equal" )
}
}