jmap_test.go

  1package jmap
  2
  3import (
  4	"testing"
  5
  6	"git.sr.ht/~rockorager/go-jmap/mail"
  7	"git.sr.ht/~rockorager/go-jmap/mail/email"
  8)
  9
 10func TestJmapEmailToBackend_ReplyTo(t *testing.T) {
 11	tests := []struct {
 12		name        string
 13		replyTo     []*mail.Address
 14		wantReplyTo []string
 15	}{
 16		{
 17			name:        "single bare address",
 18			replyTo:     []*mail.Address{{Email: "alice@example.com"}},
 19			wantReplyTo: []string{"alice@example.com"},
 20		},
 21		{
 22			name:        "address with display name returns only email",
 23			replyTo:     []*mail.Address{{Name: "Alice Smith", Email: "alice@example.com"}},
 24			wantReplyTo: []string{"alice@example.com"},
 25		},
 26		{
 27			name: "display name with comma returns only email",
 28			replyTo: []*mail.Address{
 29				{Name: "Doe, John", Email: "john@example.com"},
 30			},
 31			wantReplyTo: []string{"john@example.com"},
 32		},
 33		{
 34			name: "multiple addresses with display names",
 35			replyTo: []*mail.Address{
 36				{Name: "Doe, John", Email: "john@example.com"},
 37				{Name: "Smith, Jane", Email: "jane@example.com"},
 38			},
 39			wantReplyTo: []string{"john@example.com", "jane@example.com"},
 40		},
 41		{
 42			name:        "empty reply-to",
 43			replyTo:     nil,
 44			wantReplyTo: nil,
 45		},
 46	}
 47
 48	for _, tt := range tests {
 49		t.Run(tt.name, func(t *testing.T) {
 50			eml := &email.Email{
 51				ReplyTo: tt.replyTo,
 52			}
 53			result := jmapEmailToBackend(eml, 1, "test-account")
 54
 55			if len(result.ReplyTo) != len(tt.wantReplyTo) {
 56				t.Fatalf("ReplyTo length = %d, want %d; got %v", len(result.ReplyTo), len(tt.wantReplyTo), result.ReplyTo)
 57			}
 58			for i, want := range tt.wantReplyTo {
 59				if result.ReplyTo[i] != want {
 60					t.Errorf("ReplyTo[%d] = %q, want %q", i, result.ReplyTo[i], want)
 61				}
 62			}
 63		})
 64	}
 65}
 66
 67func TestJmapEmailToBackend_To(t *testing.T) {
 68	tests := []struct {
 69		name   string
 70		to     []*mail.Address
 71		wantTo []string
 72	}{
 73		{
 74			name:   "address with display name returns only email",
 75			to:     []*mail.Address{{Name: "Alice Smith", Email: "alice@example.com"}},
 76			wantTo: []string{"alice@example.com"},
 77		},
 78		{
 79			name: "multiple addresses return only emails",
 80			to: []*mail.Address{
 81				{Name: "Doe, John", Email: "john@example.com"},
 82				{Name: "Alice", Email: "alice@example.com"},
 83			},
 84			wantTo: []string{"john@example.com", "alice@example.com"},
 85		},
 86	}
 87
 88	for _, tt := range tests {
 89		t.Run(tt.name, func(t *testing.T) {
 90			eml := &email.Email{
 91				To: tt.to,
 92			}
 93			result := jmapEmailToBackend(eml, 1, "test-account")
 94
 95			if len(result.To) != len(tt.wantTo) {
 96				t.Fatalf("To length = %d, want %d; got %v", len(result.To), len(tt.wantTo), result.To)
 97			}
 98			for i, want := range tt.wantTo {
 99				if result.To[i] != want {
100					t.Errorf("To[%d] = %q, want %q", i, result.To[i], want)
101				}
102			}
103		})
104	}
105}
106
107func TestJmapEmailToBackend_From(t *testing.T) {
108	tests := []struct {
109		name     string
110		from     []*mail.Address
111		wantFrom string
112	}{
113		{
114			name:     "from with display name includes name",
115			from:     []*mail.Address{{Name: "Alice Smith", Email: "alice@example.com"}},
116			wantFrom: "Alice Smith <alice@example.com>",
117		},
118		{
119			name:     "from without display name returns bare email",
120			from:     []*mail.Address{{Email: "alice@example.com"}},
121			wantFrom: "alice@example.com",
122		},
123		{
124			name:     "empty from",
125			from:     nil,
126			wantFrom: "",
127		},
128	}
129
130	for _, tt := range tests {
131		t.Run(tt.name, func(t *testing.T) {
132			eml := &email.Email{
133				From: tt.from,
134			}
135			result := jmapEmailToBackend(eml, 1, "test-account")
136
137			if result.From != tt.wantFrom {
138				t.Errorf("From = %q, want %q", result.From, tt.wantFrom)
139			}
140		})
141	}
142}