@ -20,8 +20,9 @@ func main() {
var err error
var err error
url := flag . String ( "url" , "ws://127.0.0.1:3334" , "relay websocket URL" )
url := flag . String ( "url" , "ws://127.0.0.1:3334" , "relay websocket URL" )
timeout := flag . Duration ( "timeout" , 20 * time . Second , "operation timeout" )
timeout := flag . Duration ( "timeout" , 20 * time . Second , "operation timeout" )
testType := flag . String ( "type" , "event" , "test type: 'event' for write control, 'req' for read control, 'both' for both" )
testType := flag . String ( "type" , "event" , "test type: 'event' for write control, 'req' for read control, 'both' for both, 'publish-and-query' for full test " )
eventKind := flag . Int ( "kind" , 4678 , "event kind to test" )
eventKind := flag . Int ( "kind" , 4678 , "event kind to test" )
numEvents := flag . Int ( "count" , 2 , "number of events to publish (for publish-and-query)" )
flag . Parse ( )
flag . Parse ( )
// Connect to relay
// Connect to relay
@ -54,8 +55,10 @@ func main() {
testEventWrite ( rl , signer , * eventKind , * timeout )
testEventWrite ( rl , signer , * eventKind , * timeout )
log . I . Ln ( "\nTesting REQ (read control)..." )
log . I . Ln ( "\nTesting REQ (read control)..." )
testReqRead ( rl , signer , * eventKind , * timeout )
testReqRead ( rl , signer , * eventKind , * timeout )
case "publish-and-query" :
testPublishAndQuery ( rl , signer , * eventKind , * numEvents , * timeout )
default :
default :
log . E . F ( "invalid test type: %s (must be 'event', 'req', or 'both')" , * testType )
log . E . F ( "invalid test type: %s (must be 'event', 'req', 'both', or 'publish-and-query ')" , * testType )
}
}
}
}
@ -133,3 +136,94 @@ func testReqRead(rl *ws.Client, signer *p8k.Signer, eventKind int, timeout time.
fmt . Printf ( "REQ ACCEPT: %d events returned\n" , len ( events ) )
fmt . Printf ( "REQ ACCEPT: %d events returned\n" , len ( events ) )
log . I . F ( "REQ result: %d events of kind %d returned" , len ( events ) , eventKind )
log . I . F ( "REQ result: %d events of kind %d returned" , len ( events ) , eventKind )
}
}
func testPublishAndQuery ( rl * ws . Client , signer * p8k . Signer , eventKind int , numEvents int , timeout time . Duration ) {
log . I . F ( "Publishing %d events of kind %d..." , numEvents , eventKind )
publishedIDs := make ( [ ] [ ] byte , 0 , numEvents )
acceptedCount := 0
rejectedCount := 0
// Publish multiple events
for i := 0 ; i < numEvents ; i ++ {
ev := & event . E {
CreatedAt : time . Now ( ) . Unix ( ) + int64 ( i ) , // Slightly different timestamps
Kind : uint16 ( eventKind ) ,
Tags : tag . NewS ( ) ,
Content : [ ] byte ( fmt . Sprintf ( "policy test event %d/%d" , i + 1 , numEvents ) ) ,
}
if err := ev . Sign ( signer ) ; chk . E ( err ) {
log . E . F ( "sign error for event %d: %v" , i + 1 , err )
continue
}
ctx , cancel := context . WithTimeout ( context . Background ( ) , timeout )
err := rl . Publish ( ctx , ev )
cancel ( )
if err != nil {
log . W . F ( "Event %d/%d rejected: %v" , i + 1 , numEvents , err )
rejectedCount ++
} else {
log . I . F ( "Event %d/%d published successfully (id: %x...)" , i + 1 , numEvents , ev . ID [ : 8 ] )
publishedIDs = append ( publishedIDs , ev . ID )
acceptedCount ++
}
}
fmt . Printf ( "PUBLISH: %d accepted, %d rejected out of %d total\n" , acceptedCount , rejectedCount , numEvents )
if acceptedCount == 0 {
fmt . Println ( "No events were accepted, skipping query test" )
return
}
// Wait a moment for events to be stored
time . Sleep ( 500 * time . Millisecond )
// Now query for events of this kind
log . I . F ( "Querying for events of kind %d..." , eventKind )
limit := uint ( 100 )
f := & filter . F {
Kinds : kind . FromIntSlice ( [ ] int { eventKind } ) ,
Limit : & limit ,
}
ctx , cancel := context . WithTimeout ( context . Background ( ) , timeout )
defer cancel ( )
events , err := rl . QuerySync ( ctx , f )
if chk . E ( err ) {
log . E . F ( "query error: %v" , err )
fmt . Println ( "QUERY ERROR:" , err )
return
}
log . I . F ( "Query returned %d events" , len ( events ) )
// Check if we got our published events back
foundCount := 0
for _ , pubID := range publishedIDs {
found := false
for _ , ev := range events {
if string ( ev . ID ) == string ( pubID ) {
found = true
break
}
}
if found {
foundCount ++
}
}
fmt . Printf ( "QUERY: found %d/%d published events (total returned: %d)\n" , foundCount , len ( publishedIDs ) , len ( events ) )
if foundCount == len ( publishedIDs ) {
fmt . Println ( "SUCCESS: All published events were retrieved" )
} else if foundCount > 0 {
fmt . Printf ( "PARTIAL: Only %d/%d events retrieved (some filtered by read policy?)\n" , foundCount , len ( publishedIDs ) )
} else {
fmt . Println ( "FAILURE: None of the published events were retrieved (read policy blocked?)" )
}
}