Files
next.orly.dev/pkg/database/fetch-event-by-serial_test.go
mleku 24383ef1f4
Some checks failed
Go / build-and-release (push) Has been cancelled
Decompose handle-event.go into DDD domain services (v0.36.15)
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>
2025-12-25 05:30:07 +01:00

99 lines
2.5 KiB
Go

package database
import (
"testing"
"git.mleku.dev/mleku/nostr/encoders/filter"
"git.mleku.dev/mleku/nostr/encoders/tag"
"next.orly.dev/pkg/database/indexes/types"
"next.orly.dev/pkg/utils"
)
func TestFetchEventBySerial(t *testing.T) {
// Use shared database (skips in short mode)
db, ctx := GetSharedDB(t)
savedEvents := GetSharedEvents(t)
if len(savedEvents) < 4 {
t.Fatalf("Need at least 4 saved events, got %d", len(savedEvents))
}
testEvent := savedEvents[3]
// Use QueryForIds to get the serial for this event
sers, err := db.QueryForSerials(
ctx, &filter.F{
Ids: tag.NewFromBytesSlice(testEvent.ID),
},
)
if err != nil {
t.Fatalf("Failed to query for Ids: %v", err)
}
// Verify we got exactly one result
if len(sers) != 1 {
t.Fatalf("Expected 1 serial, got %d", len(sers))
}
// Fetch the event by serial
fetchedEvent, err := db.FetchEventBySerial(sers[0])
if err != nil {
t.Fatalf("Failed to fetch event by serial: %v", err)
}
// Verify the fetched event is not nil
if fetchedEvent == nil {
t.Fatal("Expected fetched event to be non-nil, but got nil")
}
// Verify the fetched event has the same ID as the original event
if !utils.FastEqual(fetchedEvent.ID, testEvent.ID) {
t.Fatalf(
"Fetched event ID doesn't match original event ID. Got %x, expected %x",
fetchedEvent.ID, testEvent.ID,
)
}
// Verify other event properties match
if fetchedEvent.Kind != testEvent.Kind {
t.Fatalf(
"Fetched event kind doesn't match. Got %d, expected %d",
fetchedEvent.Kind, testEvent.Kind,
)
}
if !utils.FastEqual(fetchedEvent.Pubkey, testEvent.Pubkey) {
t.Fatalf(
"Fetched event pubkey doesn't match. Got %x, expected %x",
fetchedEvent.Pubkey, testEvent.Pubkey,
)
}
if fetchedEvent.CreatedAt != testEvent.CreatedAt {
t.Fatalf(
"Fetched event created_at doesn't match. Got %d, expected %d",
fetchedEvent.CreatedAt, testEvent.CreatedAt,
)
}
// Test with a non-existent serial
nonExistentSerial := new(types.Uint40)
err = nonExistentSerial.Set(uint64(0xFFFFFFFFFF)) // Max value
if err != nil {
t.Fatalf("Failed to create non-existent serial: %v", err)
}
// This should return an error since the serial doesn't exist
fetchedEvent, err = db.FetchEventBySerial(nonExistentSerial)
if err == nil {
t.Fatal("Expected error for non-existent serial, but got nil")
}
// The fetched event should be nil
if fetchedEvent != nil {
t.Fatalf(
"Expected nil event for non-existent serial, but got: %v",
fetchedEvent,
)
}
}