aboutsummaryrefslogtreecommitdiffstats
path: root/worker/imap/extensions/xgmext/search_test.go
blob: a2a2791e2f99b876ff64f34ee310c6a014f9a995 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package xgmext_test

import (
	"bytes"
	"testing"

	"git.sr.ht/~rjarry/aerc/worker/imap/extensions/xgmext"
	"github.com/emersion/go-imap"
)

func TestXGMEXT_ThreadIDSearch(t *testing.T) {
	tests := []struct {
		name string
		ids  []string
		want string
	}{
		{
			name: "search for single id",
			ids:  []string{"1234"},
			want: "* SEARCH CHARSET UTF-8 X-GM-THRID 1234\r\n",
		},
		{
			name: "search for multiple id",
			ids:  []string{"1234", "5678", "2345"},
			want: "* SEARCH CHARSET UTF-8 OR OR X-GM-THRID 1234 X-GM-THRID 5678 X-GM-THRID 2345\r\n",
		},
	}
	for _, test := range tests {
		cmd := xgmext.NewThreadIDSearch(test.ids).Command()
		var buf bytes.Buffer
		err := cmd.WriteTo(imap.NewWriter(&buf))
		if err != nil {
			t.Errorf("failed to write command: %v", err)
		}
		if got := buf.String(); got != test.want {
			t.Errorf("test '%s' failed: got: '%s', but wanted: '%s'",
				test.name, got, test.want)
		}
	}
}

func TestXGMEXT_RawSearch(t *testing.T) {
	tests := []struct {
		name   string
		search string
		want   string
	}{
		{
			name:   "search messages from mailing list",
			search: "list:info@example.com",
			want:   "* SEARCH CHARSET UTF-8 X-GM-RAW list:info@example.com\r\n",
		},
		{
			name:   "search for an exact phrase",
			search: "\"good morning\"",
			want:   "* SEARCH CHARSET UTF-8 X-GM-RAW \"good morning\"\r\n",
		},
		{
			name:   "group multiple search terms together",
			search: "subject:(dinner movie)",
			want:   "* SEARCH CHARSET UTF-8 X-GM-RAW subject:(dinner movie)\r\n",
		},
	}
	for _, test := range tests {
		cmd := xgmext.NewRawSearch(test.search).Command()
		var buf bytes.Buffer
		err := cmd.WriteTo(imap.NewWriter(&buf))
		if err != nil {
			t.Errorf("failed to write command: %v", err)
		}
		if got := buf.String(); got != test.want {
			t.Errorf("test '%s' failed: got: '%s', but wanted: '%s'",
				test.name, got, test.want)
		}
	}
}