1package pop3
2
3import (
4 "context"
5 "errors"
6 "testing"
7
8 "github.com/emersion/go-message"
9 "github.com/floatpane/matcha/backend"
10 pop3client "github.com/knadh/go-pop3"
11)
12
13func TestEntityToEmail_ReplyTo(t *testing.T) {
14 tests := []struct {
15 name string
16 replyToHeader string
17 wantReplyTo []string
18 }{
19 {
20 name: "single bare address",
21 replyToHeader: "alice@example.com",
22 wantReplyTo: []string{"alice@example.com"},
23 },
24 {
25 name: "single address with display name",
26 replyToHeader: "Alice Smith <alice@example.com>",
27 wantReplyTo: []string{"alice@example.com"},
28 },
29 {
30 name: "display name with comma",
31 replyToHeader: `"Doe, John" <john@example.com>`,
32 wantReplyTo: []string{"john@example.com"},
33 },
34 {
35 name: "multiple addresses",
36 replyToHeader: "alice@example.com, bob@example.com",
37 wantReplyTo: []string{"alice@example.com", "bob@example.com"},
38 },
39 {
40 name: "multiple addresses with display names containing commas",
41 replyToHeader: `"Doe, John" <john@example.com>, "Smith, Jane" <jane@example.com>`,
42 wantReplyTo: []string{"john@example.com", "jane@example.com"},
43 },
44 {
45 name: "empty reply-to",
46 replyToHeader: "",
47 wantReplyTo: nil,
48 },
49 }
50
51 for _, tt := range tests {
52 t.Run(tt.name, func(t *testing.T) {
53 var header message.Header
54 header.Set("From", "sender@example.com")
55 header.Set("Subject", "Test")
56 if tt.replyToHeader != "" {
57 header.Set("Reply-To", tt.replyToHeader)
58 }
59
60 msgInfo := pop3client.MessageID{ID: 1, UID: "test-uid"}
61 email := entityToEmail(&header, msgInfo, "test-account")
62
63 if len(email.ReplyTo) != len(tt.wantReplyTo) {
64 t.Fatalf("ReplyTo length = %d, want %d; got %v", len(email.ReplyTo), len(tt.wantReplyTo), email.ReplyTo)
65 }
66 for i, want := range tt.wantReplyTo {
67 if email.ReplyTo[i] != want {
68 t.Errorf("ReplyTo[%d] = %q, want %q", i, email.ReplyTo[i], want)
69 }
70 }
71 })
72 }
73}
74
75func TestEntityToEmail_To(t *testing.T) {
76 tests := []struct {
77 name string
78 toHeader string
79 wantTo []string
80 }{
81 {
82 name: "display name with comma",
83 toHeader: `"Doe, John" <john@example.com>`,
84 wantTo: []string{"john@example.com"},
85 },
86 {
87 name: "multiple addresses with display names",
88 toHeader: `"Doe, John" <john@example.com>, Alice <alice@example.com>`,
89 wantTo: []string{"john@example.com", "alice@example.com"},
90 },
91 }
92
93 for _, tt := range tests {
94 t.Run(tt.name, func(t *testing.T) {
95 var header message.Header
96 header.Set("From", "sender@example.com")
97 header.Set("To", tt.toHeader)
98
99 msgInfo := pop3client.MessageID{ID: 1, UID: "test-uid"}
100 email := entityToEmail(&header, msgInfo, "test-account")
101
102 if len(email.To) != len(tt.wantTo) {
103 t.Fatalf("To length = %d, want %d; got %v", len(email.To), len(tt.wantTo), email.To)
104 }
105 for i, want := range tt.wantTo {
106 if email.To[i] != want {
107 t.Errorf("To[%d] = %q, want %q", i, email.To[i], want)
108 }
109 }
110 })
111 }
112}
113
114func TestSearchNotSupported(t *testing.T) {
115 p := &Provider{}
116 _, err := p.Search(context.Background(), "INBOX", backend.SearchQuery{Raw: "subject:test"})
117 if !errors.Is(err, backend.ErrNotSupported) {
118 t.Fatalf("Search error = %v, want ErrNotSupported", err)
119 }
120}