query-for-kinds_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  )
  10  
  11  func TestQueryForKinds(t *testing.T) {
  12  	// Use shared database (read-only test)
  13  	db, ctx := GetSharedDB(t)
  14  	events := GetSharedEvents(t)
  15  
  16  	if len(events) == 0 {
  17  		t.Fatal("Need at least 1 saved event")
  18  	}
  19  
  20  	// Test querying by kind
  21  	// Find an event with a specific kind
  22  	testKind := kind.New(1) // Kind 1 is typically text notes
  23  	kindFilter := kind.NewS(testKind)
  24  
  25  	idTsPk, err := db.QueryForIds(
  26  		ctx, &filter.F{
  27  			Kinds: kindFilter,
  28  		},
  29  	)
  30  	if err != nil {
  31  		t.Fatalf("Failed to query for kinds: %v", err)
  32  	}
  33  
  34  	// Verify we got results
  35  	if len(idTsPk) == 0 {
  36  		t.Fatal("did not find any events with the specified kind")
  37  	}
  38  
  39  	// Verify the results have the correct kind
  40  	for i, result := range idTsPk {
  41  		// Find the event with this ID
  42  		var found bool
  43  		for _, ev := range events {
  44  			if bytes.Equal(result.Id[:], ev.ID[:]) {
  45  				found = true
  46  				if ev.Kind != testKind.K {
  47  					t.Fatalf(
  48  						"result %d has incorrect kind, got %d, expected %d",
  49  						i, ev.Kind, testKind.K,
  50  					)
  51  				}
  52  				break
  53  			}
  54  		}
  55  		if !found {
  56  			t.Fatalf("result %d with ID %x not found in events", i, result.Id)
  57  		}
  58  	}
  59  }
  60