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)) } }