Some checks failed
Go / build-and-release (push) Has been cancelled
Major refactoring of event handling into clean, testable domain services: - Add pkg/event/validation: JSON hex validation, signature verification, timestamp bounds, NIP-70 protected tag validation - Add pkg/event/authorization: Policy and ACL authorization decisions, auth challenge handling, access level determination - Add pkg/event/routing: Event router registry with ephemeral and delete handlers, kind-based dispatch - Add pkg/event/processing: Event persistence, delivery to subscribers, and post-save hooks (ACL reconfig, sync, relay groups) - Reduce handle-event.go from 783 to 296 lines (62% reduction) - Add comprehensive unit tests for all new domain services - Refactor database tests to use shared TestMain setup - Fix blossom URL test expectations (missing "/" separator) - Add go-memory-optimization skill and analysis documentation - Update DDD_ANALYSIS.md to reflect completed decomposition Files modified: - app/handle-event.go: Slim orchestrator using domain services - app/server.go: Service initialization and interface wrappers - app/handle-event-types.go: Shared types (OkHelper, result types) - pkg/event/validation/*: New validation service package - pkg/event/authorization/*: New authorization service package - pkg/event/routing/*: New routing service package - pkg/event/processing/*: New processing service package - pkg/database/*_test.go: Refactored to shared TestMain - pkg/blossom/http_test.go: Fixed URL format expectations 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
165 lines
4.4 KiB
Go
165 lines
4.4 KiB
Go
package database
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"git.mleku.dev/mleku/nostr/encoders/filter"
|
|
"git.mleku.dev/mleku/nostr/encoders/kind"
|
|
"git.mleku.dev/mleku/nostr/encoders/tag"
|
|
"git.mleku.dev/mleku/nostr/encoders/timestamp"
|
|
"next.orly.dev/pkg/utils"
|
|
)
|
|
|
|
func TestGetSerialsByRange(t *testing.T) {
|
|
// Use shared database (skips in short mode)
|
|
db, _ := GetSharedDB(t)
|
|
savedEvents := GetSharedEvents(t)
|
|
|
|
if len(savedEvents) < 10 {
|
|
t.Fatalf("Need at least 10 saved events, got %d", len(savedEvents))
|
|
}
|
|
|
|
// Test GetSerialsByRange with a time range filter
|
|
// Use the timestamp from the middle event as a reference
|
|
middleIndex := len(savedEvents) / 2
|
|
middleEvent := savedEvents[middleIndex]
|
|
|
|
// Create a timestamp range that includes events before and after the middle event
|
|
sinceTime := new(timestamp.T)
|
|
sinceTime.V = middleEvent.CreatedAt - 3600 // 1 hour before middle event
|
|
|
|
untilTime := new(timestamp.T)
|
|
untilTime.V = middleEvent.CreatedAt + 3600 // 1 hour after middle event
|
|
|
|
// Create a filter with the time range
|
|
timeFilter := &filter.F{
|
|
Since: sinceTime,
|
|
Until: untilTime,
|
|
}
|
|
|
|
// Get the indexes from the filter
|
|
ranges, err := GetIndexesFromFilter(timeFilter)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get indexes from filter: %v", err)
|
|
}
|
|
|
|
// Verify we got at least one range
|
|
if len(ranges) == 0 {
|
|
t.Fatal("Expected at least one range from filter, but got none")
|
|
}
|
|
|
|
// Test GetSerialsByRange with the first range
|
|
serials, err := db.GetSerialsByRange(ranges[0])
|
|
if err != nil {
|
|
t.Fatalf("Failed to get serials by range: %v", err)
|
|
}
|
|
|
|
// Verify we got results
|
|
if len(serials) == 0 {
|
|
t.Fatal("Expected serials for events in time range, but got none")
|
|
}
|
|
|
|
// Verify the serials correspond to events within the time range
|
|
for i, serial := range serials {
|
|
// Fetch the event using the serial
|
|
ev, err := db.FetchEventBySerial(serial)
|
|
if err != nil {
|
|
t.Fatalf("Failed to fetch event for serial %d: %v", i, err)
|
|
}
|
|
|
|
if ev.CreatedAt < sinceTime.V || ev.CreatedAt > untilTime.V {
|
|
t.Fatalf(
|
|
"Event %d is outside the time range. Got %d, expected between %d and %d",
|
|
i, ev.CreatedAt, sinceTime.V, untilTime.V,
|
|
)
|
|
}
|
|
}
|
|
|
|
// Test GetSerialsByRange with a kind filter
|
|
testKind := kind.New(1) // Kind 1 is typically text notes
|
|
kindFilter := &filter.F{
|
|
Kinds: kind.NewS(testKind),
|
|
}
|
|
|
|
// Get the indexes from the filter
|
|
ranges, err = GetIndexesFromFilter(kindFilter)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get indexes from filter: %v", err)
|
|
}
|
|
|
|
// Verify we got at least one range
|
|
if len(ranges) == 0 {
|
|
t.Fatal("Expected at least one range from filter, but got none")
|
|
}
|
|
|
|
// Test GetSerialsByRange with the first range
|
|
serials, err = db.GetSerialsByRange(ranges[0])
|
|
if err != nil {
|
|
t.Fatalf("Failed to get serials by range: %v", err)
|
|
}
|
|
|
|
// Verify we got results
|
|
if len(serials) == 0 {
|
|
t.Fatal("Expected serials for events with kind 1, but got none")
|
|
}
|
|
|
|
// Verify the serials correspond to events with the correct kind
|
|
for i, serial := range serials {
|
|
// Fetch the event using the serial
|
|
ev, err := db.FetchEventBySerial(serial)
|
|
if err != nil {
|
|
t.Fatalf("Failed to fetch event for serial %d: %v", i, err)
|
|
}
|
|
|
|
if ev.Kind != testKind.K {
|
|
t.Fatalf(
|
|
"Event %d has incorrect kind. Got %d, expected %d",
|
|
i, ev.Kind, testKind.K,
|
|
)
|
|
}
|
|
}
|
|
|
|
// Test GetSerialsByRange with an author filter
|
|
authorFilter := &filter.F{
|
|
Authors: tag.NewFromBytesSlice(savedEvents[1].Pubkey),
|
|
}
|
|
|
|
// Get the indexes from the filter
|
|
ranges, err = GetIndexesFromFilter(authorFilter)
|
|
if err != nil {
|
|
t.Fatalf("Failed to get indexes from filter: %v", err)
|
|
}
|
|
|
|
// Verify we got at least one range
|
|
if len(ranges) == 0 {
|
|
t.Fatal("Expected at least one range from filter, but got none")
|
|
}
|
|
|
|
// Test GetSerialsByRange with the first range
|
|
serials, err = db.GetSerialsByRange(ranges[0])
|
|
if err != nil {
|
|
t.Fatalf("Failed to get serials by range: %v", err)
|
|
}
|
|
|
|
// Verify we got results
|
|
if len(serials) == 0 {
|
|
t.Fatal("Expected serials for events from author, but got none")
|
|
}
|
|
|
|
// Verify the serials correspond to events with the correct author
|
|
for i, serial := range serials {
|
|
// Fetch the event using the serial
|
|
ev, err := db.FetchEventBySerial(serial)
|
|
if err != nil {
|
|
t.Fatalf("Failed to fetch event for serial %d: %v", i, err)
|
|
}
|
|
|
|
if !utils.FastEqual(ev.Pubkey, savedEvents[1].Pubkey) {
|
|
t.Fatalf(
|
|
"Event %d has incorrect author. Got %x, expected %x",
|
|
i, ev.Pubkey, savedEvents[1].Pubkey,
|
|
)
|
|
}
|
|
}
|
|
}
|