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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
/*
* qtmaildir - a Qt6 mail client for notmuch-indexed Maildirs
* Copyright (C) 2026 Danilo M. <danix@danix.xyz>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <QFileInfo>
#include <QTemporaryDir>
#include <QTest>
#include "businesssenders.h"
class TestBusinessSenders : public QObject
{
Q_OBJECT
private slots:
void anExactAddressMatches();
void aDomainEntryMatchesEveryAddressUnderIt();
void commentsAndBlankLinesAreIgnored();
void whitespaceAroundAnEntryIsIgnored();
void matchingIsCaseInsensitive();
void anAbsentFileMatchesNothing();
void candidatesAreAppendedCommentedOut();
void appendingDoesNotCorruptALineThatLacksATrailingNewline();
void anAddressAlreadyPresentIsNeverReproposed();
void onlyBulkLookingLocalPartsAreProposed();
void theFirstRunScansEverything();
void alaterRunScansOnlyRecentMail();
};
void TestBusinessSenders::anExactAddressMatches()
{
const BusinessSenders::List list = BusinessSenders::parse(
QStringLiteral("noreply@cofidis.it\n"));
QVERIFY(BusinessSenders::contains(list,
QStringLiteral("noreply@cofidis.it")));
QVERIFY(!BusinessSenders::contains(list,
QStringLiteral("someone@cofidis.it")));
}
void TestBusinessSenders::aDomainEntryMatchesEveryAddressUnderIt()
{
const BusinessSenders::List list =
BusinessSenders::parse(QStringLiteral("@cofidis.it\n"));
QVERIFY(BusinessSenders::contains(list,
QStringLiteral("noreply@cofidis.it")));
QVERIFY(BusinessSenders::contains(list,
QStringLiteral("billing@cofidis.it")));
QVERIFY(!BusinessSenders::contains(list,
QStringLiteral("a@example.org")));
}
void TestBusinessSenders::commentsAndBlankLinesAreIgnored()
{
// A commented entry is the REJECT gesture: present in the file, not
// applied. This is the property the whole file format rests on.
const BusinessSenders::List list = BusinessSenders::parse(
QStringLiteral("# noreply@cofidis.it (47 messages)\n"
"\n"
" \n"
"billing@example.org\n"));
QVERIFY(!BusinessSenders::contains(list,
QStringLiteral("noreply@cofidis.it")));
QVERIFY(BusinessSenders::contains(list,
QStringLiteral("billing@example.org")));
}
void TestBusinessSenders::whitespaceAroundAnEntryIsIgnored()
{
const BusinessSenders::List list =
BusinessSenders::parse(QStringLiteral(" billing@example.org \n"));
QVERIFY(BusinessSenders::contains(list,
QStringLiteral("billing@example.org")));
}
void TestBusinessSenders::matchingIsCaseInsensitive()
{
// Addresses arrive from headers in whatever case the sender used, so a
// list entry that matched only one casing would look broken at random.
const BusinessSenders::List list =
BusinessSenders::parse(QStringLiteral("NoReply@Cofidis.IT\n"));
QVERIFY(BusinessSenders::contains(list,
QStringLiteral("noreply@cofidis.it")));
}
void TestBusinessSenders::anAbsentFileMatchesNothing()
{
QTemporaryDir dir;
const BusinessSenders::List list =
BusinessSenders::load(dir.filePath(QStringLiteral("does-not-exist")));
QVERIFY(!BusinessSenders::contains(list, QStringLiteral("a@example.org")));
}
void TestBusinessSenders::candidatesAreAppendedCommentedOut()
{
QTemporaryDir dir;
const QString path = dir.filePath(QStringLiteral("business-senders"));
QHash<QString, int> counts;
counts.insert(QStringLiteral("noreply@cofidis.it"), 47);
BusinessSenders::appendCandidates(path, counts);
QFile file(path);
QVERIFY(file.open(QIODevice::ReadOnly | QIODevice::Text));
const QString written = QString::fromUtf8(file.readAll());
// Commented, and carrying the count so the user can judge it.
QVERIFY(written.contains(QStringLiteral("# noreply@cofidis.it")));
QVERIFY(written.contains(QStringLiteral("47")));
// Nothing it wrote may take effect on its own.
const BusinessSenders::List list = BusinessSenders::load(path);
QVERIFY(!BusinessSenders::contains(list,
QStringLiteral("noreply@cofidis.it")));
}
void TestBusinessSenders::appendingDoesNotCorruptALineThatLacksATrailingNewline()
{
// Hand-editing, the documented workflow, can leave the file without a
// trailing newline. Appending then glued the first candidate onto the last
// existing line, silently breaking the user's own active entry so it
// stopped matching. The guard writes a newline before the additions.
QTemporaryDir dir;
const QString path = dir.filePath(QStringLiteral("business-senders"));
QFile seed(path);
QVERIFY(seed.open(QIODevice::WriteOnly | QIODevice::Text));
seed.write("billing@example.org"); // deliberately no trailing newline
seed.close();
QHash<QString, int> counts;
counts.insert(QStringLiteral("noreply@a.org"), 3);
BusinessSenders::appendCandidates(path, counts);
// The original entry is intact and still matches.
const BusinessSenders::List list = BusinessSenders::load(path);
QVERIFY(BusinessSenders::contains(list,
QStringLiteral("billing@example.org")));
// ...and the candidate sits on its own commented line, not glued onto it.
QFile file(path);
QVERIFY(file.open(QIODevice::ReadOnly | QIODevice::Text));
const QString written = QString::fromUtf8(file.readAll());
QVERIFY(written.contains(QStringLiteral(
"billing@example.org\n# noreply@a.org (3 messages)")));
}
void TestBusinessSenders::anAddressAlreadyPresentIsNeverReproposed()
{
QTemporaryDir dir;
const QString path = dir.filePath(QStringLiteral("business-senders"));
// Both forms count as present: an active entry and a rejected one. The
// rejected case is the one that matters, since re-proposing it would undo
// the user's decision every ten minutes with no explanation.
QFile seed(path);
QVERIFY(seed.open(QIODevice::WriteOnly | QIODevice::Text));
seed.write("billing@example.org\n# noreply@cofidis.it (47 messages)\n");
seed.close();
const qint64 sizeBefore = QFileInfo(path).size();
QHash<QString, int> counts;
counts.insert(QStringLiteral("noreply@cofidis.it"), 51);
counts.insert(QStringLiteral("billing@example.org"), 12);
BusinessSenders::appendCandidates(path, counts);
QCOMPARE(QFileInfo(path).size(), sizeBefore);
}
void TestBusinessSenders::onlyBulkLookingLocalPartsAreProposed()
{
QTemporaryDir dir;
const QString path = dir.filePath(QStringLiteral("business-senders"));
QHash<QString, int> counts;
counts.insert(QStringLiteral("noreply@a.org"), 3);
counts.insert(QStringLiteral("john.doe@b.org"), 3);
BusinessSenders::appendCandidates(path, counts);
QFile file(path);
QVERIFY(file.open(QIODevice::ReadOnly | QIODevice::Text));
const QString written = QString::fromUtf8(file.readAll());
QVERIFY(written.contains(QStringLiteral("noreply@a.org")));
QVERIFY(!written.contains(QStringLiteral("john.doe@b.org")));
}
void TestBusinessSenders::theFirstRunScansEverything()
{
QTemporaryDir dir;
const QString missing = dir.filePath(QStringLiteral("business-senders"));
// No file at all: a week of mail would propose almost nothing and the
// list would take months to become useful, so the first run pays for a
// full scan once.
QCOMPARE(BusinessSenders::scanQuery(missing), QStringLiteral("*"));
// A file holding ONLY rejected candidates is still a first run: nothing
// has been accepted yet. Rescanning re-proposes none of them, since
// appendCandidates skips anything already mentioned.
QFile rejected(missing);
QVERIFY(rejected.open(QIODevice::WriteOnly | QIODevice::Text));
rejected.write("# noreply@cofidis.it (47 messages)\n");
rejected.close();
QCOMPARE(BusinessSenders::scanQuery(missing), QStringLiteral("*"));
}
void TestBusinessSenders::alaterRunScansOnlyRecentMail()
{
QTemporaryDir dir;
const QString path = dir.filePath(QStringLiteral("business-senders"));
QFile file(path);
QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
file.write("billing@example.org\n");
file.close();
QCOMPARE(BusinessSenders::scanQuery(path), QStringLiteral("date:1week.."));
}
QTEST_MAIN(TestBusinessSenders)
#include "test_businesssenders.moc"
|