diff --git a/pkg/node/message_test.go b/pkg/node/message_test.go index 69363c9..4c0b4a6 100644 --- a/pkg/node/message_test.go +++ b/pkg/node/message_test.go @@ -60,6 +60,37 @@ func TestMessage_NewMessage_Good(t *testing.T) { }) } +// TestMessage_NewMessage_Bad verifies that NewMessage returns an error when the payload cannot be marshalled. +// +// _, err := NewMessage(MsgPing, "sender", "receiver", make(chan int)) +// if err == nil { t.Error("expected error for unmarshalable payload") } +func TestMessage_NewMessage_Bad(t *testing.T) { + // A channel cannot be marshalled to JSON — NewMessage must propagate the error. + _, err := NewMessage(MsgPing, "sender-id", "receiver-id", make(chan int)) + if err == nil { + t.Error("expected error when payload cannot be marshalled, got nil") + } +} + +// TestMessage_NewMessage_Ugly verifies that NewMessage succeeds with empty From/To fields. +// +// msg, err := NewMessage(MsgPing, "", "", nil) +// if err != nil { t.Fatalf("unexpected error: %v", err) } +// if msg.ID == "" { t.Error("message ID should not be empty even with empty from/to") } +func TestMessage_NewMessage_Ugly(t *testing.T) { + // Empty From/To should not cause an error — callers control routing. + msg, err := NewMessage(MsgPing, "", "", nil) + if err != nil { + t.Fatalf("unexpected error with empty from/to: %v", err) + } + if msg.ID == "" { + t.Error("message ID should not be empty even with empty from/to") + } + if msg.Type != MsgPing { + t.Errorf("expected type MsgPing, got %s", msg.Type) + } +} + func TestMessage_Reply_Good(t *testing.T) { original, _ := NewMessage(MsgPing, "sender", "receiver", PingPayload{SentAt: 12345})