Files
next.orly.dev/pkg/database/query-for-created-at_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

147 lines
3.5 KiB
Go

package database
import (
"testing"
"git.mleku.dev/mleku/nostr/encoders/filter"
"git.mleku.dev/mleku/nostr/encoders/timestamp"
"next.orly.dev/pkg/utils"
)
func TestQueryForCreatedAt(t *testing.T) {
// Use shared database (read-only test)
db, ctx := GetSharedDB(t)
events := GetSharedEvents(t)
if len(events) < 3 {
t.Fatalf("Need at least 3 saved events, got %d", len(events))
}
// Find a timestamp range that should include some events
// Use the timestamp from the middle event as a reference
middleIndex := len(events) / 2
middleEvent := events[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
// Test querying by created_at range
idTsPk, err := db.QueryForIds(
ctx, &filter.F{
Since: sinceTime,
Until: untilTime,
},
)
if err != nil {
t.Fatalf("Failed to query for created_at range: %v", err)
}
// Verify we got results
if len(idTsPk) == 0 {
t.Fatal("did not find any events in the specified time range")
}
// Verify the results exist in our events slice and are within the timestamp range
for i, result := range idTsPk {
// Find the event with this ID
var found bool
for _, ev := range events {
if utils.FastEqual(result.Id, ev.ID) {
found = true
break
}
}
if !found {
t.Fatalf("result %d with ID %x not found in events", i, result.Id)
}
// Verify the timestamp is within the range
if result.Ts < sinceTime.V || result.Ts > untilTime.V {
t.Fatalf(
"result %d with ID %x has timestamp %d outside the range [%d, %d]",
i, result.Id, result.Ts, sinceTime.V, untilTime.V,
)
}
}
// Test with only Since
idTsPk, err = db.QueryForIds(
ctx, &filter.F{
Since: sinceTime,
},
)
if err != nil {
t.Fatalf("Failed to query with Since: %v", err)
}
// Verify we got results
if len(idTsPk) == 0 {
t.Fatal("did not find any events with Since filter")
}
// Verify the results exist in our events slice and are after the Since timestamp
for i, result := range idTsPk {
// Find the event with this ID
var found bool
for _, ev := range events {
if utils.FastEqual(result.Id, ev.ID) {
found = true
break
}
}
if !found {
t.Fatalf("result %d with ID %x not found in events", i, result.Id)
}
// Verify the timestamp is after the Since timestamp
if result.Ts < sinceTime.V {
t.Fatalf(
"result %d with ID %x has timestamp %d before the Since timestamp %d",
i, result.Id, result.Ts, sinceTime.V,
)
}
}
// Test with only Until
idTsPk, err = db.QueryForIds(
ctx, &filter.F{
Until: untilTime,
},
)
if err != nil {
t.Fatalf("Failed to query with Until: %v", err)
}
// Verify we got results
if len(idTsPk) == 0 {
t.Fatal("did not find any events with Until filter")
}
// Verify the results exist in our events slice and are before the Until timestamp
for i, result := range idTsPk {
// Find the event with this ID
var found bool
for _, ev := range events {
if utils.FastEqual(result.Id, ev.ID) {
found = true
break
}
}
if !found {
t.Fatalf("result %d with ID %x not found in events", i, result.Id)
}
// Verify the timestamp is before the Until timestamp
if result.Ts > untilTime.V {
t.Fatalf(
"result %d with ID %x has timestamp %d after the Until timestamp %d",
i, result.Id, result.Ts, untilTime.V,
)
}
}
}