diff options
| author | bndw <ben@bdw.to> | 2026-02-13 17:41:13 -0800 |
|---|---|---|
| committer | bndw <ben@bdw.to> | 2026-02-13 17:41:13 -0800 |
| commit | a6502c0888613bd0377a25e43de8ae306c4de4d7 (patch) | |
| tree | 7e6b9eaaafbd97d3d0ef5007e392fa7b91e35f6c /internal/storage/events_test.go | |
| parent | af30945803d440d1f803c814f4a37a1890494f1d (diff) | |
feat: add SQLite storage layer with binary-first event persistence
Storage implementation:
- Concrete type with constructor (consumer-side interfaces)
- Event storage: protobuf + zstd-compressed canonical JSON
- Schema: events, deletions, replaceable_events, auth_challenges, rate_limits
- WAL mode, STRICT typing, optimized indexes
- Methods: StoreEvent, GetEvent, GetEventWithCanonical, DeleteEvent
Dependencies:
- modernc.org/sqlite v1.45.0 (pure Go SQLite driver)
- github.com/klauspost/compress v1.18.4 (zstd compression)
366 lines, 10 tests passing
Diffstat (limited to 'internal/storage/events_test.go')
| -rw-r--r-- | internal/storage/events_test.go | 264 |
1 files changed, 264 insertions, 0 deletions
diff --git a/internal/storage/events_test.go b/internal/storage/events_test.go new file mode 100644 index 0000000..4393404 --- /dev/null +++ b/internal/storage/events_test.go | |||
| @@ -0,0 +1,264 @@ | |||
| 1 | package storage | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "context" | ||
| 5 | "testing" | ||
| 6 | |||
| 7 | pb "northwest.io/nostr-grpc/api/nostr/v1" | ||
| 8 | ) | ||
| 9 | |||
| 10 | func TestStoreEvent(t *testing.T) { | ||
| 11 | store, err := New(":memory:") | ||
| 12 | if err != nil { | ||
| 13 | t.Fatalf("failed to create storage: %v", err) | ||
| 14 | } | ||
| 15 | defer store.Close() | ||
| 16 | |||
| 17 | ctx := context.Background() | ||
| 18 | |||
| 19 | // Create test event | ||
| 20 | event := &pb.Event{ | ||
| 21 | Id: "test123", | ||
| 22 | Pubkey: "pubkey123", | ||
| 23 | CreatedAt: 1234567890, | ||
| 24 | Kind: 1, | ||
| 25 | Tags: []*pb.Tag{{Values: []string{"e", "event1"}}}, | ||
| 26 | Content: "Hello, Nostr!", | ||
| 27 | Sig: "sig123", | ||
| 28 | } | ||
| 29 | |||
| 30 | canonicalJSON := []byte(`[0,"pubkey123",1234567890,1,[["e","event1"]],"Hello, Nostr!"]`) | ||
| 31 | |||
| 32 | data := &EventData{ | ||
| 33 | Event: event, | ||
| 34 | CanonicalJSON: canonicalJSON, | ||
| 35 | } | ||
| 36 | |||
| 37 | // Store event | ||
| 38 | err = store.StoreEvent(ctx, data) | ||
| 39 | if err != nil { | ||
| 40 | t.Fatalf("failed to store event: %v", err) | ||
| 41 | } | ||
| 42 | |||
| 43 | // Verify event was stored | ||
| 44 | retrieved, err := store.GetEvent(ctx, "test123") | ||
| 45 | if err != nil { | ||
| 46 | t.Fatalf("failed to retrieve event: %v", err) | ||
| 47 | } | ||
| 48 | |||
| 49 | if retrieved.Id != event.Id { | ||
| 50 | t.Errorf("expected ID %s, got %s", event.Id, retrieved.Id) | ||
| 51 | } | ||
| 52 | if retrieved.Content != event.Content { | ||
| 53 | t.Errorf("expected content %s, got %s", event.Content, retrieved.Content) | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | func TestStoreEventDuplicate(t *testing.T) { | ||
| 58 | store, err := New(":memory:") | ||
| 59 | if err != nil { | ||
| 60 | t.Fatalf("failed to create storage: %v", err) | ||
| 61 | } | ||
| 62 | defer store.Close() | ||
| 63 | |||
| 64 | ctx := context.Background() | ||
| 65 | |||
| 66 | event := &pb.Event{ | ||
| 67 | Id: "duplicate123", | ||
| 68 | Pubkey: "pubkey123", | ||
| 69 | CreatedAt: 1234567890, | ||
| 70 | Kind: 1, | ||
| 71 | Content: "test", | ||
| 72 | Sig: "sig123", | ||
| 73 | } | ||
| 74 | |||
| 75 | data := &EventData{ | ||
| 76 | Event: event, | ||
| 77 | CanonicalJSON: []byte(`[0,"pubkey123",1234567890,1,[],"test"]`), | ||
| 78 | } | ||
| 79 | |||
| 80 | // Store first time | ||
| 81 | err = store.StoreEvent(ctx, data) | ||
| 82 | if err != nil { | ||
| 83 | t.Fatalf("failed to store event first time: %v", err) | ||
| 84 | } | ||
| 85 | |||
| 86 | // Try to store again | ||
| 87 | err = store.StoreEvent(ctx, data) | ||
| 88 | if err != ErrEventExists { | ||
| 89 | t.Errorf("expected ErrEventExists, got %v", err) | ||
| 90 | } | ||
| 91 | } | ||
| 92 | |||
| 93 | func TestGetEvent(t *testing.T) { | ||
| 94 | store, err := New(":memory:") | ||
| 95 | if err != nil { | ||
| 96 | t.Fatalf("failed to create storage: %v", err) | ||
| 97 | } | ||
| 98 | defer store.Close() | ||
| 99 | |||
| 100 | ctx := context.Background() | ||
| 101 | |||
| 102 | // Test non-existent event | ||
| 103 | _, err = store.GetEvent(ctx, "nonexistent") | ||
| 104 | if err != ErrEventNotFound { | ||
| 105 | t.Errorf("expected ErrEventNotFound, got %v", err) | ||
| 106 | } | ||
| 107 | } | ||
| 108 | |||
| 109 | func TestGetEventWithCanonical(t *testing.T) { | ||
| 110 | store, err := New(":memory:") | ||
| 111 | if err != nil { | ||
| 112 | t.Fatalf("failed to create storage: %v", err) | ||
| 113 | } | ||
| 114 | defer store.Close() | ||
| 115 | |||
| 116 | ctx := context.Background() | ||
| 117 | |||
| 118 | canonicalJSON := []byte(`[0,"pubkey123",1234567890,1,[],"test"]`) | ||
| 119 | |||
| 120 | event := &pb.Event{ | ||
| 121 | Id: "canonical123", | ||
| 122 | Pubkey: "pubkey123", | ||
| 123 | CreatedAt: 1234567890, | ||
| 124 | Kind: 1, | ||
| 125 | Content: "test", | ||
| 126 | Sig: "sig123", | ||
| 127 | } | ||
| 128 | |||
| 129 | data := &EventData{ | ||
| 130 | Event: event, | ||
| 131 | CanonicalJSON: canonicalJSON, | ||
| 132 | } | ||
| 133 | |||
| 134 | err = store.StoreEvent(ctx, data) | ||
| 135 | if err != nil { | ||
| 136 | t.Fatalf("failed to store event: %v", err) | ||
| 137 | } | ||
| 138 | |||
| 139 | // Retrieve with canonical JSON | ||
| 140 | retrieved, err := store.GetEventWithCanonical(ctx, "canonical123") | ||
| 141 | if err != nil { | ||
| 142 | t.Fatalf("failed to retrieve event: %v", err) | ||
| 143 | } | ||
| 144 | |||
| 145 | if string(retrieved.CanonicalJson) != string(canonicalJSON) { | ||
| 146 | t.Errorf("canonical JSON mismatch:\nexpected: %s\ngot: %s", | ||
| 147 | canonicalJSON, retrieved.CanonicalJson) | ||
| 148 | } | ||
| 149 | } | ||
| 150 | |||
| 151 | func TestDeleteEvent(t *testing.T) { | ||
| 152 | store, err := New(":memory:") | ||
| 153 | if err != nil { | ||
| 154 | t.Fatalf("failed to create storage: %v", err) | ||
| 155 | } | ||
| 156 | defer store.Close() | ||
| 157 | |||
| 158 | ctx := context.Background() | ||
| 159 | |||
| 160 | event := &pb.Event{ | ||
| 161 | Id: "delete123", | ||
| 162 | Pubkey: "pubkey123", | ||
| 163 | CreatedAt: 1234567890, | ||
| 164 | Kind: 1, | ||
| 165 | Content: "to be deleted", | ||
| 166 | Sig: "sig123", | ||
| 167 | } | ||
| 168 | |||
| 169 | data := &EventData{ | ||
| 170 | Event: event, | ||
| 171 | CanonicalJSON: []byte(`[0,"pubkey123",1234567890,1,[],"to be deleted"]`), | ||
| 172 | } | ||
| 173 | |||
| 174 | // Store event | ||
| 175 | err = store.StoreEvent(ctx, data) | ||
| 176 | if err != nil { | ||
| 177 | t.Fatalf("failed to store event: %v", err) | ||
| 178 | } | ||
| 179 | |||
| 180 | // Delete event | ||
| 181 | err = store.DeleteEvent(ctx, "delete123") | ||
| 182 | if err != nil { | ||
| 183 | t.Fatalf("failed to delete event: %v", err) | ||
| 184 | } | ||
| 185 | |||
| 186 | // Verify event is no longer retrievable | ||
| 187 | _, err = store.GetEvent(ctx, "delete123") | ||
| 188 | if err != ErrEventNotFound { | ||
| 189 | t.Errorf("expected ErrEventNotFound after deletion, got %v", err) | ||
| 190 | } | ||
| 191 | |||
| 192 | // Try deleting non-existent event | ||
| 193 | err = store.DeleteEvent(ctx, "nonexistent") | ||
| 194 | if err != ErrEventNotFound { | ||
| 195 | t.Errorf("expected ErrEventNotFound, got %v", err) | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | func TestCompressDecompressJSON(t *testing.T) { | ||
| 200 | original := []byte(`{"key":"value","array":[1,2,3],"nested":{"a":"b"}}`) | ||
| 201 | |||
| 202 | compressed, err := compressJSON(original) | ||
| 203 | if err != nil { | ||
| 204 | t.Fatalf("compression failed: %v", err) | ||
| 205 | } | ||
| 206 | |||
| 207 | // Verify compression reduces size (for larger data) | ||
| 208 | if len(compressed) >= len(original) { | ||
| 209 | t.Logf("Note: compressed size (%d) >= original (%d) - normal for small data", | ||
| 210 | len(compressed), len(original)) | ||
| 211 | } | ||
| 212 | |||
| 213 | decompressed, err := decompressJSON(compressed) | ||
| 214 | if err != nil { | ||
| 215 | t.Fatalf("decompression failed: %v", err) | ||
| 216 | } | ||
| 217 | |||
| 218 | if string(decompressed) != string(original) { | ||
| 219 | t.Errorf("decompressed data doesn't match original:\nexpected: %s\ngot: %s", | ||
| 220 | original, decompressed) | ||
| 221 | } | ||
| 222 | } | ||
| 223 | |||
| 224 | func TestMarshalTags(t *testing.T) { | ||
| 225 | tests := []struct { | ||
| 226 | name string | ||
| 227 | tags []*pb.Tag | ||
| 228 | expected string | ||
| 229 | }{ | ||
| 230 | { | ||
| 231 | name: "empty tags", | ||
| 232 | tags: nil, | ||
| 233 | expected: "[]", | ||
| 234 | }, | ||
| 235 | { | ||
| 236 | name: "single tag", | ||
| 237 | tags: []*pb.Tag{ | ||
| 238 | {Values: []string{"e", "event123"}}, | ||
| 239 | }, | ||
| 240 | expected: `[["e","event123"]]`, | ||
| 241 | }, | ||
| 242 | { | ||
| 243 | name: "multiple tags", | ||
| 244 | tags: []*pb.Tag{ | ||
| 245 | {Values: []string{"e", "event123", "wss://relay.example.com"}}, | ||
| 246 | {Values: []string{"p", "pubkey456"}}, | ||
| 247 | }, | ||
| 248 | expected: `[["e","event123","wss://relay.example.com"],["p","pubkey456"]]`, | ||
| 249 | }, | ||
| 250 | } | ||
| 251 | |||
| 252 | for _, tt := range tests { | ||
| 253 | t.Run(tt.name, func(t *testing.T) { | ||
| 254 | result, err := marshalTags(tt.tags) | ||
| 255 | if err != nil { | ||
| 256 | t.Fatalf("marshalTags failed: %v", err) | ||
| 257 | } | ||
| 258 | |||
| 259 | if result != tt.expected { | ||
| 260 | t.Errorf("expected %s, got %s", tt.expected, result) | ||
| 261 | } | ||
| 262 | }) | ||
| 263 | } | ||
| 264 | } | ||
