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