diff --git a/pkg/node/message_test.go b/pkg/node/message_test.go index a519ebd..69363c9 100644 --- a/pkg/node/message_test.go +++ b/pkg/node/message_test.go @@ -89,6 +89,51 @@ func TestMessage_Reply_Good(t *testing.T) { } } +// TestMessage_Reply_Bad verifies that Reply returns an error when the payload cannot be marshalled. +// +// original, _ := NewMessage(MsgPing, "sender", "receiver", nil) +// _, err := original.Reply(MsgPong, make(chan int)) // unmarshalable type +// if err == nil { t.Error("expected error for unmarshalable payload") } +func TestMessage_Reply_Bad(t *testing.T) { + original, err := NewMessage(MsgPing, "sender", "receiver", nil) + if err != nil { + t.Fatalf("failed to create original message: %v", err) + } + + // A channel cannot be marshalled to JSON — Reply must propagate the error. + _, replyErr := original.Reply(MsgPong, make(chan int)) + if replyErr == nil { + t.Error("expected error when payload cannot be marshalled, got nil") + } +} + +// TestMessage_Reply_Ugly verifies that Reply correctly reverses From/To even when both are identical. +// +// original, _ := NewMessage(MsgPing, "self", "self", nil) +// reply, err := original.Reply(MsgPong, nil) +// if reply.From != "self" || reply.To != "self" { t.Error("from/to should both be 'self'") } +func TestMessage_Reply_Ugly(t *testing.T) { + original, err := NewMessage(MsgPing, "self", "self", nil) + if err != nil { + t.Fatalf("failed to create original message: %v", err) + } + + reply, replyErr := original.Reply(MsgPong, nil) + if replyErr != nil { + t.Fatalf("unexpected error: %v", replyErr) + } + + if reply.From != "self" { + t.Errorf("expected From 'self', got '%s'", reply.From) + } + if reply.To != "self" { + t.Errorf("expected To 'self', got '%s'", reply.To) + } + if reply.ReplyTo != original.ID { + t.Errorf("expected ReplyTo '%s', got '%s'", original.ID, reply.ReplyTo) + } +} + func TestMessage_ParsePayload_Good(t *testing.T) { t.Run("ValidPayload", func(t *testing.T) { payload := StartMinerPayload{