query-for-kinds-authors_test.go raw

   1  package database
   2  
   3  import (
   4  	"bytes"
   5  	"testing"
   6  
   7  	"next.orly.dev/pkg/nostr/encoders/filter"
   8  	"next.orly.dev/pkg/nostr/encoders/kind"
   9  	"next.orly.dev/pkg/nostr/encoders/tag"
  10  )
  11  
  12  func TestQueryForKindsAuthors(t *testing.T) {
  13  	// Use shared database (read-only test)
  14  	db, ctx := GetSharedDB(t)
  15  	events := GetSharedEvents(t)
  16  
  17  	if len(events) < 2 {
  18  		t.Fatalf("Need at least 2 saved events, got %d", len(events))
  19  	}
  20  
  21  	// Test querying by kind and author
  22  	// Find an event with a specific kind and author
  23  	testKind := kind.New(1) // Kind 1 is typically text notes
  24  	kindFilter := kind.NewS(testKind)
  25  
  26  	// Use the author from events[1]
  27  	authorFilter := tag.NewFromBytesSlice(events[1].Pubkey)
  28  
  29  	idTsPk, err := db.QueryForIds(
  30  		ctx, &filter.F{
  31  			Kinds:   kindFilter,
  32  			Authors: authorFilter,
  33  		},
  34  	)
  35  	if err != nil {
  36  		t.Fatalf("Failed to query for kinds and authors: %v", err)
  37  	}
  38  
  39  	// Verify we got results
  40  	if len(idTsPk) == 0 {
  41  		t.Fatal("did not find any events with the specified kind and author")
  42  	}
  43  
  44  	// Verify the results have the correct kind and author
  45  	for i, result := range idTsPk {
  46  		// Find the event with this ID
  47  		var found bool
  48  		for _, ev := range events {
  49  			if bytes.Equal(result.Id[:], ev.ID[:]) {
  50  				found = true
  51  				if ev.Kind != testKind.K {
  52  					t.Fatalf(
  53  						"result %d has incorrect kind, got %d, expected %d",
  54  						i, ev.Kind, testKind.K,
  55  					)
  56  				}
  57  				if !bytes.Equal(ev.Pubkey[:], events[1].Pubkey[:]) {
  58  					t.Fatalf(
  59  						"result %d has incorrect author, got %x, expected %x",
  60  						i, ev.Pubkey, events[1].Pubkey,
  61  					)
  62  				}
  63  				break
  64  			}
  65  		}
  66  		if !found {
  67  			t.Fatalf("result %d with ID %x not found in events", i, result.Id)
  68  		}
  69  	}
  70  }
  71