sender_test.go

 1package sender
 2
 3import (
 4	"strings"
 5	"testing"
 6)
 7
 8// TestGenerateMessageID ensures the Message-ID has the correct format.
 9func TestGenerateMessageID(t *testing.T) {
10	from := "test@example.com"
11	msgID := generateMessageID(from)
12
13	// Check if the message ID is enclosed in angle brackets.
14	if !strings.HasPrefix(msgID, "<") || !strings.HasSuffix(msgID, ">") {
15		t.Errorf("Message-ID should be enclosed in angle brackets, got %s", msgID)
16	}
17
18	// Check if the 'from' address is part of the message ID.
19	if !strings.Contains(msgID, from) {
20		t.Errorf("Message-ID should contain the from address, got %s", msgID)
21	}
22
23	// The original check was too simple and failed because the 'from' address itself contains an '@'.
24	// A Message-ID is generally <unique-part@domain>. The current implementation uses the full 'from' address as the domain part.
25	// This revised check validates that structure correctly.
26	unwrappedID := strings.Trim(msgID, "<>")
27
28	// Ensure there's at least one '@' symbol.
29	if !strings.Contains(unwrappedID, "@") {
30		t.Errorf("Message-ID should contain an '@' symbol, got %s", msgID)
31	}
32
33	// Check that the ID ends with the full 'from' address, preceded by an '@'.
34	// This confirms the structure is <random_part>@<from_address>.
35	expectedSuffix := "@" + from
36	if !strings.HasSuffix(unwrappedID, expectedSuffix) {
37		t.Errorf("Message-ID should end with '@' + from address. Got %s, expected suffix %s", unwrappedID, expectedSuffix)
38	}
39
40	// Check that the part before the suffix is not empty.
41	randomPart := strings.TrimSuffix(unwrappedID, expectedSuffix)
42	if randomPart == "" {
43		t.Errorf("Message-ID has an empty random part, got %s", msgID)
44	}
45}