1 package database
2 3 import (
4 "bytes"
5 "testing"
6 7 "next.orly.dev/pkg/nostr/encoders/filter"
8 "next.orly.dev/pkg/nostr/encoders/timestamp"
9 )
10 11 func TestQueryForCreatedAt(t *testing.T) {
12 // Use shared database (read-only test)
13 db, ctx := GetSharedDB(t)
14 events := GetSharedEvents(t)
15 16 if len(events) < 3 {
17 t.Fatalf("Need at least 3 saved events, got %d", len(events))
18 }
19 20 // Find a timestamp range that should include some events
21 // Use the timestamp from the middle event as a reference
22 middleIndex := len(events) / 2
23 middleEvent := events[middleIndex]
24 25 // Create a timestamp range that includes events before and after the middle event
26 sinceTime := new(timestamp.T)
27 sinceTime.V = middleEvent.CreatedAt - 3600 // 1 hour before middle event
28 29 untilTime := new(timestamp.T)
30 untilTime.V = middleEvent.CreatedAt + 3600 // 1 hour after middle event
31 32 // Test querying by created_at range
33 idTsPk, err := db.QueryForIds(
34 ctx, &filter.F{
35 Since: sinceTime,
36 Until: untilTime,
37 },
38 )
39 if err != nil {
40 t.Fatalf("Failed to query for created_at range: %v", err)
41 }
42 43 // Verify we got results
44 if len(idTsPk) == 0 {
45 t.Fatal("did not find any events in the specified time range")
46 }
47 48 // Verify the results exist in our events slice and are within the timestamp range
49 for i, result := range idTsPk {
50 // Find the event with this ID
51 var found bool
52 for _, ev := range events {
53 if bytes.Equal(result.Id[:], ev.ID[:]) {
54 found = true
55 break
56 }
57 }
58 if !found {
59 t.Fatalf("result %d with ID %x not found in events", i, result.Id)
60 }
61 62 // Verify the timestamp is within the range
63 if result.Ts < sinceTime.V || result.Ts > untilTime.V {
64 t.Fatalf(
65 "result %d with ID %x has timestamp %d outside the range [%d, %d]",
66 i, result.Id, result.Ts, sinceTime.V, untilTime.V,
67 )
68 }
69 }
70 71 // Test with only Since
72 idTsPk, err = db.QueryForIds(
73 ctx, &filter.F{
74 Since: sinceTime,
75 },
76 )
77 if err != nil {
78 t.Fatalf("Failed to query with Since: %v", err)
79 }
80 81 // Verify we got results
82 if len(idTsPk) == 0 {
83 t.Fatal("did not find any events with Since filter")
84 }
85 86 // Verify the results exist in our events slice and are after the Since timestamp
87 for i, result := range idTsPk {
88 // Find the event with this ID
89 var found bool
90 for _, ev := range events {
91 if bytes.Equal(result.Id[:], ev.ID[:]) {
92 found = true
93 break
94 }
95 }
96 if !found {
97 t.Fatalf("result %d with ID %x not found in events", i, result.Id)
98 }
99 100 // Verify the timestamp is after the Since timestamp
101 if result.Ts < sinceTime.V {
102 t.Fatalf(
103 "result %d with ID %x has timestamp %d before the Since timestamp %d",
104 i, result.Id, result.Ts, sinceTime.V,
105 )
106 }
107 }
108 109 // Test with only Until
110 idTsPk, err = db.QueryForIds(
111 ctx, &filter.F{
112 Until: untilTime,
113 },
114 )
115 if err != nil {
116 t.Fatalf("Failed to query with Until: %v", err)
117 }
118 119 // Verify we got results
120 if len(idTsPk) == 0 {
121 t.Fatal("did not find any events with Until filter")
122 }
123 124 // Verify the results exist in our events slice and are before the Until timestamp
125 for i, result := range idTsPk {
126 // Find the event with this ID
127 var found bool
128 for _, ev := range events {
129 if bytes.Equal(result.Id[:], ev.ID[:]) {
130 found = true
131 break
132 }
133 }
134 if !found {
135 t.Fatalf("result %d with ID %x not found in events", i, result.Id)
136 }
137 138 // Verify the timestamp is before the Until timestamp
139 if result.Ts > untilTime.V {
140 t.Fatalf(
141 "result %d with ID %x has timestamp %d after the Until timestamp %d",
142 i, result.Id, result.Ts, untilTime.V,
143 )
144 }
145 }
146 }
147