1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
package nostr
import (
"encoding/json"
"testing"
)
func TestEventSerialize(t *testing.T) {
event := &Event{
PubKey: "7e7e9c42a91bfef19fa929e5fda1b72e0ebc1a4c1141673e2794234d86addf4e",
CreatedAt: 1704067200,
Kind: 1,
Tags: Tags{{"e", "abc123"}, {"p", "def456"}},
Content: "Hello, Nostr!",
}
serialized := event.Serialize()
// Parse the JSON to verify structure
var arr []interface{}
if err := json.Unmarshal(serialized, &arr); err != nil {
t.Fatalf("Serialize() produced invalid JSON: %v", err)
}
if len(arr) != 6 {
t.Fatalf("Serialized array has %d elements, want 6", len(arr))
}
// Check each element
if arr[0].(float64) != 0 {
t.Errorf("arr[0] = %v, want 0", arr[0])
}
if arr[1].(string) != event.PubKey {
t.Errorf("arr[1] = %v, want %s", arr[1], event.PubKey)
}
if int64(arr[2].(float64)) != event.CreatedAt {
t.Errorf("arr[2] = %v, want %d", arr[2], event.CreatedAt)
}
if int(arr[3].(float64)) != event.Kind {
t.Errorf("arr[3] = %v, want %d", arr[3], event.Kind)
}
if arr[5].(string) != event.Content {
t.Errorf("arr[5] = %v, want %s", arr[5], event.Content)
}
}
func TestEventComputeID(t *testing.T) {
// Test with a known event (you can verify with other implementations)
event := &Event{
PubKey: "7e7e9c42a91bfef19fa929e5fda1b72e0ebc1a4c1141673e2794234d86addf4e",
CreatedAt: 1704067200,
Kind: 1,
Tags: Tags{},
Content: "Hello, Nostr!",
}
id := event.ComputeID()
// ID should be 64 hex characters
if len(id) != 64 {
t.Errorf("ComputeID() returned ID of length %d, want 64", len(id))
}
// Verify it's valid hex
for _, c := range id {
if !((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f')) {
t.Errorf("ComputeID() returned invalid hex character: %c", c)
}
}
// Verify consistency
id2 := event.ComputeID()
if id != id2 {
t.Errorf("ComputeID() is not consistent: %s != %s", id, id2)
}
}
func TestEventSetID(t *testing.T) {
event := &Event{
PubKey: "7e7e9c42a91bfef19fa929e5fda1b72e0ebc1a4c1141673e2794234d86addf4e",
CreatedAt: 1704067200,
Kind: 1,
Tags: Tags{},
Content: "Test",
}
event.SetID()
if event.ID == "" {
t.Error("SetID() did not set ID")
}
if !event.CheckID() {
t.Error("CheckID() returned false after SetID()")
}
}
func TestEventCheckID(t *testing.T) {
event := &Event{
PubKey: "7e7e9c42a91bfef19fa929e5fda1b72e0ebc1a4c1141673e2794234d86addf4e",
CreatedAt: 1704067200,
Kind: 1,
Tags: Tags{},
Content: "Test",
}
event.SetID()
if !event.CheckID() {
t.Error("CheckID() returned false for valid ID")
}
// Corrupt the ID
event.ID = "0000000000000000000000000000000000000000000000000000000000000000"
if event.CheckID() {
t.Error("CheckID() returned true for invalid ID")
}
}
func TestEventMarshalJSON(t *testing.T) {
event := Event{
ID: "abc123",
PubKey: "def456",
CreatedAt: 1704067200,
Kind: 1,
Tags: nil, // nil tags
Content: "Test",
Sig: "sig789",
}
data, err := json.Marshal(event)
if err != nil {
t.Fatalf("MarshalJSON() error = %v", err)
}
// Verify tags is [] not null
var m map[string]interface{}
if err := json.Unmarshal(data, &m); err != nil {
t.Fatalf("Failed to unmarshal: %v", err)
}
tags, ok := m["tags"]
if !ok {
t.Error("tags field missing from JSON")
}
if tags == nil {
t.Error("tags is null, want []")
}
if arr, ok := tags.([]interface{}); !ok || len(arr) != 0 {
t.Errorf("tags = %v, want []", tags)
}
}
func TestEventJSONRoundTrip(t *testing.T) {
original := Event{
ID: "abc123def456",
PubKey: "pubkey123",
CreatedAt: 1704067200,
Kind: 1,
Tags: Tags{{"e", "event1"}, {"p", "pubkey1", "relay"}},
Content: "Hello with \"quotes\" and \n newlines",
Sig: "signature123",
}
data, err := json.Marshal(original)
if err != nil {
t.Fatalf("Marshal error: %v", err)
}
var decoded Event
if err := json.Unmarshal(data, &decoded); err != nil {
t.Fatalf("Unmarshal error: %v", err)
}
if decoded.ID != original.ID {
t.Errorf("ID mismatch: %s != %s", decoded.ID, original.ID)
}
if decoded.PubKey != original.PubKey {
t.Errorf("PubKey mismatch: %s != %s", decoded.PubKey, original.PubKey)
}
if decoded.CreatedAt != original.CreatedAt {
t.Errorf("CreatedAt mismatch: %d != %d", decoded.CreatedAt, original.CreatedAt)
}
if decoded.Kind != original.Kind {
t.Errorf("Kind mismatch: %d != %d", decoded.Kind, original.Kind)
}
if decoded.Content != original.Content {
t.Errorf("Content mismatch: %s != %s", decoded.Content, original.Content)
}
if decoded.Sig != original.Sig {
t.Errorf("Sig mismatch: %s != %s", decoded.Sig, original.Sig)
}
if len(decoded.Tags) != len(original.Tags) {
t.Errorf("Tags length mismatch: %d != %d", len(decoded.Tags), len(original.Tags))
}
}
|