clone

a self-hosted multitenant nostr relay with sites, file storage, git hosting, and inbox and outbox delivery

main
tinyrelay / internal / gitstore / store_test.go
store_test.go 1.6K
1package gitstore23import (4 "context"5 "errors"6 "path/filepath"7 "strings"8 "testing"910 "github.com/FelineStateMachine/tinyrelay/protocol/nostr"11)1213func TestStorePreservesDurableMetadataAndReadVisibility(t *testing.T) {14 ctx := context.Background()15 store, err := Open(ctx, filepath.Join(t.TempDir(), "events.db"))16 if err != nil {17 t.Fatal(err)18 }19 t.Cleanup(func() { _ = store.Close() })20 e := nostr.Event{Kind: 30618, CreatedAt: 1, Tags: [][]string{{"d", "repo"}}}21 if err := nostr.Sign(&e, strings.Repeat("0", 63)+"1"); err != nil {22 t.Fatal(err)23 }24 if err := store.Save(ctx, e, 1); err != nil {25 t.Fatal(err)26 }27 if err := store.Save(ctx, e, 1); !errors.Is(err, ErrDuplicate) {28 t.Fatalf("duplicate save: %v", err)29 }30 if found, err := store.Exists(ctx, e.ID, 30618); err != nil || !found {31 t.Fatalf("exists=%v, error=%v", found, err)32 }33 if _, err := store.store.DB().ExecContext(ctx, `INSERT INTO pending_events(id,reason) VALUES(?,'git')`, e.ID); err != nil {34 t.Fatal(err)35 }36 rows, err := store.Query(ctx, nostr.Filter{Kinds: []int{30618}}, 1, 0)37 if err != nil || len(rows) != 0 {38 t.Fatalf("pending query=%v, error=%v", rows, err)39 }40 latest, found, err := store.Latest(ctx, 30618, e.PubKey, "repo")41 if err != nil || !found || latest.ID != e.ID {42 t.Fatalf("latest=%v, found=%v, error=%v", latest, found, err)43 }44 if _, err := store.store.DB().ExecContext(ctx, `UPDATE events SET raw='{}' WHERE id=?`, e.ID); err != nil {45 t.Fatal(err)46 }47 if _, _, err := store.Latest(ctx, 30618, e.PubKey, "repo"); err == nil {48 t.Fatal("accepted corrupt stored metadata")49 }50}