Debian Codegraph @

worker/notmuch/message_test.go

raw
1 //go:build notmuch
2 // +build notmuch
3
4 package notmuch
5
6 import (
7 "testing"
8
9 "git.sr.ht/~rjarry/aerc/worker/types"
10 "github.com/emersion/go-maildir"
11 )
12
13 func TestFilterForStrategy(t *testing.T) {
14 tests := []struct {
15 filenames []string
16 strategy types.MultiFileStrategy
17 curDir string
18 expectedAct []string
19 expectedDel []string
20 expectedErr bool
21 }{
22 // if there's only one file, always act on it
23 {
24 filenames: []string{"/h/j/m/A/cur/a.b.c:2,"},
25 strategy: types.Refuse,
26 curDir: "/h/j/m/B",
27 expectedAct: []string{"/h/j/m/A/cur/a.b.c:2,"},
28 expectedDel: []string{},
29 },
30 {
31 filenames: []string{"/h/j/m/A/cur/a.b.c:2,"},
32 strategy: types.ActAll,
33 curDir: "/h/j/m/B",
34 expectedAct: []string{"/h/j/m/A/cur/a.b.c:2,"},
35 expectedDel: []string{},
36 },
37 {
38 filenames: []string{"/h/j/m/A/cur/a.b.c:2,"},
39 strategy: types.ActOne,
40 curDir: "/h/j/m/B",
41 expectedAct: []string{"/h/j/m/A/cur/a.b.c:2,"},
42 expectedDel: []string{},
43 },
44 {
45 filenames: []string{"/h/j/m/A/cur/a.b.c:2,"},
46 strategy: types.ActOneDelRest,
47 curDir: "/h/j/m/B",
48 expectedAct: []string{"/h/j/m/A/cur/a.b.c:2,"},
49 expectedDel: []string{},
50 },
51 {
52 filenames: []string{"/h/j/m/A/cur/a.b.c:2,"},
53 strategy: types.ActDir,
54 curDir: "/h/j/m/B",
55 expectedAct: []string{"/h/j/m/A/cur/a.b.c:2,"},
56 expectedDel: []string{},
57 },
58 {
59 filenames: []string{"/h/j/m/A/cur/a.b.c:2,"},
60 strategy: types.ActDirDelRest,
61 curDir: "/h/j/m/B",
62 expectedAct: []string{"/h/j/m/A/cur/a.b.c:2,"},
63 expectedDel: []string{},
64 },
65
66 // follow strategy for multiple files
67 {
68 filenames: []string{
69 "/h/j/m/A/cur/a.b.c:2,",
70 "/h/j/m/B/new/b.c.d",
71 "/h/j/m/B/cur/c.d.e:2,S",
72 "/h/j/m/C/new/d.e.f",
73 },
74 strategy: types.Refuse,
75 curDir: "/h/j/m/B",
76 expectedErr: true,
77 },
78 {
79 filenames: []string{
80 "/h/j/m/A/cur/a.b.c:2,",
81 "/h/j/m/B/new/b.c.d",
82 "/h/j/m/B/cur/c.d.e:2,S",
83 "/h/j/m/C/new/d.e.f",
84 },
85 strategy: types.ActAll,
86 curDir: "/h/j/m/B",
87 expectedAct: []string{
88 "/h/j/m/A/cur/a.b.c:2,",
89 "/h/j/m/B/new/b.c.d",
90 "/h/j/m/B/cur/c.d.e:2,S",
91 "/h/j/m/C/new/d.e.f",
92 },
93 expectedDel: []string{},
94 },
95 {
96 filenames: []string{
97 "/h/j/m/A/cur/a.b.c:2,",
98 "/h/j/m/B/new/b.c.d",
99 "/h/j/m/B/cur/c.d.e:2,S",
100 "/h/j/m/C/new/d.e.f",
101 },
102 strategy: types.ActOne,
103 curDir: "/h/j/m/B",
104 expectedAct: []string{"/h/j/m/A/cur/a.b.c:2,"},
105 expectedDel: []string{},
106 },
107 {
108 filenames: []string{
109 "/h/j/m/A/cur/a.b.c:2,",
110 "/h/j/m/B/new/b.c.d",
111 "/h/j/m/B/cur/c.d.e:2,S",
112 "/h/j/m/C/new/d.e.f",
113 },
114 strategy: types.ActOneDelRest,
115 curDir: "/h/j/m/B",
116 expectedAct: []string{"/h/j/m/A/cur/a.b.c:2,"},
117 expectedDel: []string{
118 "/h/j/m/B/new/b.c.d",
119 "/h/j/m/B/cur/c.d.e:2,S",
120 "/h/j/m/C/new/d.e.f",
121 },
122 },
123 {
124 filenames: []string{
125 "/h/j/m/A/cur/a.b.c:2,",
126 "/h/j/m/B/new/b.c.d",
127 "/h/j/m/B/cur/c.d.e:2,S",
128 "/h/j/m/C/new/d.e.f",
129 },
130 strategy: types.ActDir,
131 curDir: "/h/j/m/B",
132 expectedAct: []string{
133 "/h/j/m/B/new/b.c.d",
134 "/h/j/m/B/cur/c.d.e:2,S",
135 },
136 expectedDel: []string{},
137 },
138 {
139 filenames: []string{
140 "/h/j/m/A/cur/a.b.c:2,",
141 "/h/j/m/B/new/b.c.d",
142 "/h/j/m/B/cur/c.d.e:2,S",
143 "/h/j/m/C/new/d.e.f",
144 },
145 strategy: types.ActDirDelRest,
146 curDir: "/h/j/m/B",
147 expectedAct: []string{
148 "/h/j/m/B/new/b.c.d",
149 "/h/j/m/B/cur/c.d.e:2,S",
150 },
151 expectedDel: []string{
152 "/h/j/m/A/cur/a.b.c:2,",
153 "/h/j/m/C/new/d.e.f",
154 },
155 },
156
157 // refuse to act on multiple files for ActDir and friends if
158 // no current dir is provided
159 {
160 filenames: []string{
161 "/h/j/m/A/cur/a.b.c:2,",
162 "/h/j/m/B/new/b.c.d",
163 "/h/j/m/B/cur/c.d.e:2,S",
164 "/h/j/m/C/new/d.e.f",
165 },
166 strategy: types.ActDir,
167 curDir: "",
168 expectedErr: true,
169 },
170 {
171 filenames: []string{
172 "/h/j/m/A/cur/a.b.c:2,",
173 "/h/j/m/B/new/b.c.d",
174 "/h/j/m/B/cur/c.d.e:2,S",
175 "/h/j/m/C/new/d.e.f",
176 },
177 strategy: types.ActDirDelRest,
178 curDir: "",
179 expectedErr: true,
180 },
181
182 // act on multiple files w/o current dir for other strategies
183 {
184 filenames: []string{
185 "/h/j/m/A/cur/a.b.c:2,",
186 "/h/j/m/B/new/b.c.d",
187 "/h/j/m/B/cur/c.d.e:2,S",
188 "/h/j/m/C/new/d.e.f",
189 },
190 strategy: types.ActAll,
191 curDir: "",
192 expectedAct: []string{
193 "/h/j/m/A/cur/a.b.c:2,",
194 "/h/j/m/B/new/b.c.d",
195 "/h/j/m/B/cur/c.d.e:2,S",
196 "/h/j/m/C/new/d.e.f",
197 },
198 expectedDel: []string{},
199 },
200 {
201 filenames: []string{
202 "/h/j/m/A/cur/a.b.c:2,",
203 "/h/j/m/B/new/b.c.d",
204 "/h/j/m/B/cur/c.d.e:2,S",
205 "/h/j/m/C/new/d.e.f",
206 },
207 strategy: types.ActOne,
208 curDir: "",
209 expectedAct: []string{"/h/j/m/A/cur/a.b.c:2,"},
210 expectedDel: []string{},
211 },
212 {
213 filenames: []string{
214 "/h/j/m/A/cur/a.b.c:2,",
215 "/h/j/m/B/new/b.c.d",
216 "/h/j/m/B/cur/c.d.e:2,S",
217 "/h/j/m/C/new/d.e.f",
218 },
219 strategy: types.ActOneDelRest,
220 curDir: "",
221 expectedAct: []string{"/h/j/m/A/cur/a.b.c:2,"},
222 expectedDel: []string{
223 "/h/j/m/B/new/b.c.d",
224 "/h/j/m/B/cur/c.d.e:2,S",
225 "/h/j/m/C/new/d.e.f",
226 },
227 },
228 }
229
230 for i, test := range tests {
231 act, del, err := filterForStrategy(test.filenames, test.strategy,
232 maildir.Dir(test.curDir))
233
234 if test.expectedErr && err == nil {
235 t.Errorf("[test %d] got nil, expected error", i)
236 }
237
238 if !test.expectedErr && err != nil {
239 t.Errorf("[test %d] got %v, expected nil", i, err)
240 }
241
242 if !arrEq(act, test.expectedAct) {
243 t.Errorf("[test %d] got %v, expected %v", i, act, test.expectedAct)
244 }
245
246 if !arrEq(del, test.expectedDel) {
247 t.Errorf("[test %d] got %v, expected %v", i, del, test.expectedDel)
248 }
249 }
250 }
251
252 func arrEq(a, b []string) bool {
253 if len(a) != len(b) {
254 return false
255 }
256
257 for i := range a {
258 if a[i] != b[i] {
259 return false
260 }
261 }
262
263 return true
264 }