2022-02-09 13:40:26 -03:00
package nostr
import (
2024-02-08 16:33:39 -03:00
"slices"
2024-03-29 08:06:29 -03:00
"testing"
2024-09-09 13:50:56 +03:30
"github.com/stretchr/testify/assert"
2024-11-03 15:54:31 -03:00
"github.com/stretchr/testify/require"
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
2024-09-09 13:50:56 +03:30
err := json . Unmarshal ( [ ] byte ( raw ) , & f )
assert . NoError ( t , err )
assert . Condition ( t , func ( ) ( success bool ) {
if f . Since == nil || f . Since . Time ( ) . UTC ( ) . Format ( "2006-01-02" ) != "2022-02-07" ||
f . Until != nil ||
f . Tags == nil || len ( f . Tags ) != 2 || ! slices . Contains ( f . Tags [ "something" ] , "bab" ) ||
f . Search != "test" {
return false
}
return true
} , "failed to parse filter correctly" )
2022-02-09 13:40:26 -03:00
}
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 {
2023-09-06 21:00:05 -03:00
Kinds : [ ] int { KindTextNote , KindRecommendServer , KindEncryptedDirectMessage } ,
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
} )
2024-09-09 13:50:56 +03:30
assert . NoError ( t , err )
2022-02-09 13:40:26 -03:00
expected := ` { "kinds":[1,2,4],"until":12345678,"#fruit":["banana","mango"]} `
2024-09-09 13:50:56 +03:30
assert . Equal ( t , expected , string ( filterj ) )
2022-02-09 13:40:26 -03:00
}
2024-03-29 08:06:29 -03:00
func TestFilterUnmarshalWithLimitZero ( t * testing . T ) {
raw := ` { "ids": ["abc"],"#e":["zzz"],"limit":0,"#something":["nothing","bab"],"since":1644254609,"search":"test"} `
var f Filter
2024-09-09 13:50:56 +03:30
err := json . Unmarshal ( [ ] byte ( raw ) , & f )
assert . NoError ( t , err )
assert . Condition ( t , func ( ) ( success bool ) {
if f . Since == nil ||
f . Since . Time ( ) . UTC ( ) . Format ( "2006-01-02" ) != "2022-02-07" ||
f . Until != nil ||
f . Tags == nil || len ( f . Tags ) != 2 || ! slices . Contains ( f . Tags [ "something" ] , "bab" ) ||
f . Search != "test" ||
f . LimitZero == false {
return false
}
return true
} , "failed to parse filter correctly" )
2024-03-29 08:06:29 -03:00
}
func TestFilterMarshalWithLimitZero ( t * testing . T ) {
until := Timestamp ( 12345678 )
filterj , err := json . Marshal ( Filter {
Kinds : [ ] int { KindTextNote , KindRecommendServer , KindEncryptedDirectMessage } ,
Tags : TagMap { "fruit" : { "banana" , "mango" } } ,
Until : & until ,
LimitZero : true ,
} )
2024-09-09 13:50:56 +03:30
assert . NoError ( t , err )
2024-03-29 08:06:29 -03:00
expected := ` { "kinds":[1,2,4],"until":12345678,"limit":0,"#fruit":["banana","mango"]} `
2024-09-09 13:50:56 +03:30
assert . Equal ( t , expected , string ( filterj ) )
2024-03-29 08:06:29 -03:00
}
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 )
2024-09-09 13:50:56 +03:30
assert . True ( t , filter . Matches ( & event ) , "live filter should match" )
2023-03-18 08:18:08 -03:00
}
2022-02-09 13:40:26 -03:00
func TestFilterEquality ( t * testing . T ) {
2024-09-09 13:50:56 +03:30
assert . True ( t , FilterEqual (
2023-09-06 21:00:05 -03:00
Filter { Kinds : [ ] int { KindEncryptedDirectMessage , KindDeletion } } ,
Filter { Kinds : [ ] int { KindEncryptedDirectMessage , KindDeletion } } ,
2024-09-09 13:50:56 +03:30
) , "kinds filters should be equal" )
2022-02-09 13:40:26 -03:00
2024-09-09 13:50:56 +03:30
assert . True ( t , FilterEqual (
2023-09-06 21:00:05 -03:00
Filter { Kinds : [ ] int { KindEncryptedDirectMessage , KindDeletion } , Tags : TagMap { "letter" : { "a" , "b" } } } ,
Filter { Kinds : [ ] int { KindEncryptedDirectMessage , KindDeletion } , Tags : TagMap { "letter" : { "b" , "a" } } } ,
2024-09-09 13:50:56 +03:30
) , "kind+tags filters should be equal" )
2022-02-09 13:40:26 -03:00
2023-04-16 16:12:42 -03:00
tm := Now ( )
2024-09-09 13:50:56 +03:30
assert . True ( t , FilterEqual (
2022-02-09 13:40:26 -03:00
Filter {
2023-09-06 21:00:05 -03:00
Kinds : [ ] int { KindEncryptedDirectMessage , KindDeletion } ,
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 {
2023-09-06 21:00:05 -03:00
Kinds : [ ] int { KindDeletion , KindEncryptedDirectMessage } ,
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
} ,
2024-09-09 13:50:56 +03:30
) , "kind+2tags+since+ids filters should be equal" )
2022-02-09 13:40:26 -03:00
2024-09-09 13:50:56 +03:30
assert . False ( t , FilterEqual (
2023-09-06 21:00:05 -03:00
Filter { Kinds : [ ] int { KindTextNote , KindEncryptedDirectMessage , KindDeletion } } ,
Filter { Kinds : [ ] int { KindEncryptedDirectMessage , KindDeletion , KindRepost } } ,
2024-09-09 13:50:56 +03:30
) , "kinds filters shouldn't be equal" )
2022-02-09 13:40:26 -03:00
}
2023-11-06 18:35:11 -03:00
func TestFilterClone ( t * testing . T ) {
ts := Now ( ) - 60 * 60
flt := Filter {
Kinds : [ ] int { 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ,
Tags : TagMap { "letter" : { "a" , "b" } , "fruit" : { "banana" } } ,
Since : & ts ,
IDs : [ ] string { "9894b4b5cb5166d23ee8899a4151cf0c66aec00bde101982a13b8e8ceb972df9" } ,
}
clone := flt . Clone ( )
2024-09-09 13:50:56 +03:30
assert . True ( t , FilterEqual ( flt , clone ) , "clone is not equal:\n %v !=\n %v" , flt , clone )
2023-11-06 18:35:11 -03:00
clone1 := flt . Clone ( )
clone1 . IDs = append ( clone1 . IDs , "88f0c63fcb93463407af97a5e5ee64fa883d107ef9e558472c4eb9aaaefa459d" )
2024-09-09 13:50:56 +03:30
assert . False ( t , FilterEqual ( flt , clone1 ) , "modifying the clone ids should cause it to not be equal anymore" )
2023-11-06 18:35:11 -03:00
clone2 := flt . Clone ( )
clone2 . Tags [ "letter" ] = append ( clone2 . Tags [ "letter" ] , "c" )
2024-09-09 13:50:56 +03:30
assert . False ( t , FilterEqual ( flt , clone2 ) , "modifying the clone tag items should cause it to not be equal anymore" )
2023-11-06 18:35:11 -03:00
clone3 := flt . Clone ( )
clone3 . Tags [ "g" ] = [ ] string { "drt" }
2024-09-09 13:50:56 +03:30
assert . False ( t , FilterEqual ( flt , clone3 ) , "modifying the clone tag map should cause it to not be equal anymore" )
2023-11-06 18:35:11 -03:00
clone4 := flt . Clone ( )
* clone4 . Since ++
2024-09-09 13:50:56 +03:30
assert . False ( t , FilterEqual ( flt , clone4 ) , "modifying the clone since should cause it to not be equal anymore" )
2023-11-06 18:35:11 -03:00
}
2024-11-03 15:54:31 -03:00
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 { "d" : [ ] string { "aaa" , "bbb" } } } ) )
require . Equal ( t , - 1 , GetTheoreticalLimit ( Filter { Authors : [ ] string { "a" , "b" , "c" , "d" , "e" , "f" } , Kinds : [ ] int { 30023 , 30024 } } ) )
}