aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_composewindow.cpp
blob: 68ec4d44f88c3e855180246921c8e77b776f64ce (plain)
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
/*
 * 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 <QtTest>
#include <QComboBox>
#include <QDir>
#include <QFile>
#include <QMenu>
#include <QPlainTextEdit>
#include <QSignalSpy>
#include <QLabel>
#include <QRegularExpression>
#include <QTemporaryDir>
#include <QTimer>
#include <QTextStream>
#include <QToolButton>

#include "composecontext.h"
#include "composewindow.h"
#include "config.h"
#include "signatures.h"

class TestComposeWindow : public QObject
{
    Q_OBJECT

private slots:
    void init();
    void cleanup();

    void aNewMessageSeedsTheComposeSignature();
    void anAccountSignatureOverridesTheComposeOne();
    void aResumedDraftSeedsNoSignature();
    void anUnknownSignatureNameSeedsNothing();
    void theSwitchListsEveryFileAndNone();
    void changingTheAccountFollowsItsSignature();
    void changingTheAccountStopsFollowingOnceTheSwitchIsUsed();
    void aResumedDraftDoesNotReseedOnAnAccountChange();
    void savingADraftEmitsItsPathAndTheReplacedOne();
    void anUnsavedEditIsAnnouncedInBothPlaces();
    void aSavedDraftReportsItAndClearsTheCue();
    void aSentMessageLeavesNoUnsavedCue();
    void onlyTheSetterWritesTheDirtyFlag();
    void theAgeLineFollowsTheClock();

private:
    /// A config pointing at a signatures directory holding \p files, with one
    /// account that can send.
    Config makeConfig(const QList<QPair<QString, QString>> &files,
                      const QString &composeSignature,
                      const QString &accountSignature = {});

    /// QVERIFY cannot appear inside makeConfig(), which returns a value: the
    /// macro expands to a bare `return;` on failure, which is invalid in a
    /// non-void function. A void helper keeps the check and sidesteps that.
    void writeFile(const QString &path, const QString &content);

    /// A config whose one account has a drafts folder, so a save can write.
    Config configWithDrafts();

    QTemporaryDir *m_dir = nullptr;
    QString m_signatureDir;
};

void TestComposeWindow::init()
{
    m_dir = new QTemporaryDir;
    QVERIFY(m_dir->isValid());
    m_signatureDir = m_dir->path() + QStringLiteral("/signatures");
    QVERIFY(QDir().mkpath(m_signatureDir));
}

void TestComposeWindow::cleanup()
{
    delete m_dir;
    m_dir = nullptr;
}

void TestComposeWindow::writeFile(const QString &path, const QString &content)
{
    QFile file(path);
    QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
    QTextStream out(&file);
    out << content;
}

Config TestComposeWindow::makeConfig(
    const QList<QPair<QString, QString>> &files,
    const QString &composeSignature, const QString &accountSignature)
{
    for (const auto &entry : files)
        writeFile(m_signatureDir + QStringLiteral("/") + entry.first, entry.second);

    QString conf;
    {
        QTextStream out(&conf);
        out << "[compose]\n"
            << "signature = " << composeSignature << "\n"
            << "\n"
            << "[account.work]\n"
            << "name = Someone\n"
            << "address = someone@example.org\n"
            << "maildir = work\n"
            << "send_command = /bin/cat\n";
        if (!accountSignature.isEmpty())
            out << "signature = " << accountSignature << "\n";
    }
    const QString path = m_dir->path() + QStringLiteral("/qtmaildir.conf");
    writeFile(path, conf);

    Config config;
    config.load(path);
    return config;
}

void TestComposeWindow::aNewMessageSeedsTheComposeSignature()
{
    const Config config = makeConfig(
        { { QStringLiteral("work.md"), QStringLiteral("Jane Doe") } },
        QStringLiteral("work"));

    ComposeContext context;
    context.kind = ComposeContext::Kind::New;
    context.accountKey = QStringLiteral("work");

    ComposeWindow window(context, config, m_dir->path());
    window.setSignatureDir(m_signatureDir);
    window.seedSignature();

    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(body);
    QVERIFY(body->toPlainText().endsWith(QStringLiteral("-- \nJane Doe")));
}

void TestComposeWindow::anAccountSignatureOverridesTheComposeOne()
{
    const Config config = makeConfig(
        { { QStringLiteral("work.md"), QStringLiteral("Long one") },
          { QStringLiteral("brief.md"), QStringLiteral("Brief") } },
        QStringLiteral("work"), QStringLiteral("brief"));

    ComposeContext context;
    context.kind = ComposeContext::Kind::New;
    context.accountKey = QStringLiteral("work");

    ComposeWindow window(context, config, m_dir->path());
    window.setSignatureDir(m_signatureDir);
    window.seedSignature();

    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(body);
    QVERIFY(body->toPlainText().endsWith(QStringLiteral("-- \nBrief")));
}

void TestComposeWindow::aResumedDraftSeedsNoSignature()
{
    const Config config = makeConfig(
        { { QStringLiteral("work.md"), QStringLiteral("Jane Doe") } },
        QStringLiteral("work"));

    // The saved body already carries whatever signature it was written with.
    // Seeding again would put a SECOND one on a message written once.
    ComposeContext context;
    context.kind = ComposeContext::Kind::Draft;
    context.accountKey = QStringLiteral("work");
    context.body = QStringLiteral("Half a thought.\n\n-- \nJane Doe");
    context.draftPath = m_dir->path() + QStringLiteral("/draft");

    ComposeWindow window(context, config, m_dir->path());
    window.setSignatureDir(m_signatureDir);
    window.seedSignature();

    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(body);
    QCOMPARE(body->toPlainText().count(QStringLiteral("-- \nJane Doe")), 1);
}

void TestComposeWindow::anUnknownSignatureNameSeedsNothing()
{
    const Config config = makeConfig(
        { { QStringLiteral("work.md"), QStringLiteral("Jane Doe") } },
        QStringLiteral("absent"));

    ComposeContext context;
    context.kind = ComposeContext::Kind::New;
    context.accountKey = QStringLiteral("work");

    ComposeWindow window(context, config, m_dir->path());
    window.setSignatureDir(m_signatureDir);
    window.seedSignature();

    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(body);
    // No signature, and the composer still opened rather than refusing.
    QVERIFY(!body->toPlainText().contains(QStringLiteral("-- ")));
}

void TestComposeWindow::theSwitchListsEveryFileAndNone()
{
    const Config config = makeConfig(
        { { QStringLiteral("work.md"), QStringLiteral("Jane Doe") },
          { QStringLiteral("brief.md"), QStringLiteral("Brief") } },
        QString());

    ComposeContext context;
    context.kind = ComposeContext::Kind::New;
    context.accountKey = QStringLiteral("work");

    ComposeWindow window(context, config, m_dir->path());
    window.setSignatureDir(m_signatureDir);
    window.seedSignature();

    auto *button =
        window.findChild<QToolButton *>(QStringLiteral("signatureSwitch"));
    QVERIFY(button);
    QVERIFY(button->menu());
    // "None" plus one per file.
    QCOMPARE(button->menu()->actions().size(), 3);
}

void TestComposeWindow::changingTheAccountFollowsItsSignature()
{
    for (const auto &entry :
         QList<QPair<QString, QString>>{
             { QStringLiteral("work.md"), QStringLiteral("Work sig") },
             { QStringLiteral("home.md"), QStringLiteral("Home sig") } }) {
        QFile file(m_signatureDir + QStringLiteral("/") + entry.first);
        QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
        file.write(entry.second.toUtf8());
        file.close();
    }

    const QString path = m_dir->path() + QStringLiteral("/qtmaildir.conf");
    {
        QFile file(path);
        QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
        QTextStream out(&file);
        out << "[account.work]\n"
            << "name = Someone\naddress = someone@example.org\n"
            << "maildir = work\nsend_command = /bin/cat\n"
            << "signature = work\n"
            << "\n[account.home]\n"
            << "name = Someone\naddress = other@example.org\n"
            << "maildir = home\nsend_command = /bin/cat\n"
            << "signature = home\n";
    }
    Config config;
    config.load(path);

    ComposeContext context;
    context.kind = ComposeContext::Kind::New;
    context.accountKey = QStringLiteral("work");

    ComposeWindow window(context, config, m_dir->path());
    window.setSignatureDir(m_signatureDir);
    window.seedSignature();

    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    auto *from = window.findChild<QComboBox *>(QStringLiteral("from"));
    QVERIFY(body);
    QVERIFY(from);
    QVERIFY(body->toPlainText().contains(QStringLiteral("Work sig")));

    // Select the other account by its key, never by index: the order of the
    // combo is the config's and an index assertion would pass on the wrong one.
    const int home = from->findData(QStringLiteral("home"));
    QVERIFY(home >= 0);
    from->setCurrentIndex(home);

    QVERIFY(body->toPlainText().contains(QStringLiteral("Home sig")));
    QVERIFY(!body->toPlainText().contains(QStringLiteral("Work sig")));
}

void TestComposeWindow::changingTheAccountStopsFollowingOnceTheSwitchIsUsed()
{
    for (const auto &entry :
         QList<QPair<QString, QString>>{
             { QStringLiteral("work.md"), QStringLiteral("Work sig") },
             { QStringLiteral("home.md"), QStringLiteral("Home sig") },
             { QStringLiteral("chosen.md"), QStringLiteral("Chosen sig") } }) {
        QFile file(m_signatureDir + QStringLiteral("/") + entry.first);
        QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
        file.write(entry.second.toUtf8());
        file.close();
    }

    const QString path = m_dir->path() + QStringLiteral("/qtmaildir.conf");
    {
        QFile file(path);
        QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
        QTextStream out(&file);
        out << "[account.work]\n"
            << "name = Someone\naddress = someone@example.org\n"
            << "maildir = work\nsend_command = /bin/cat\n"
            << "signature = work\n"
            << "\n[account.home]\n"
            << "name = Someone\naddress = other@example.org\n"
            << "maildir = home\nsend_command = /bin/cat\n"
            << "signature = home\n";
    }
    Config config;
    config.load(path);

    ComposeContext context;
    context.kind = ComposeContext::Kind::New;
    context.accountKey = QStringLiteral("work");

    ComposeWindow window(context, config, m_dir->path());
    window.setSignatureDir(m_signatureDir);
    window.seedSignature();

    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    auto *from = window.findChild<QComboBox *>(QStringLiteral("from"));
    auto *button =
        window.findChild<QToolButton *>(QStringLiteral("signatureSwitch"));
    QVERIFY(body);
    QVERIFY(from);
    QVERIFY(button);

    // The user picks one deliberately.
    for (QAction *action : button->menu()->actions()) {
        if (action->data().toString() == QStringLiteral("chosen"))
            action->trigger();
    }
    QVERIFY(body->toPlainText().contains(QStringLiteral("Chosen sig")));

    const int home = from->findData(QStringLiteral("home"));
    QVERIFY(home >= 0);
    from->setCurrentIndex(home);

    // The deliberate choice survives the account change. Overwriting it is
    // the one behaviour that can silently discard something the user just did.
    QVERIFY(body->toPlainText().contains(QStringLiteral("Chosen sig")));
    QVERIFY(!body->toPlainText().contains(QStringLiteral("Home sig")));
}

void TestComposeWindow::aResumedDraftDoesNotReseedOnAnAccountChange()
{
    for (const auto &entry :
         QList<QPair<QString, QString>>{
             { QStringLiteral("work.md"), QStringLiteral("Work sig") },
             { QStringLiteral("home.md"), QStringLiteral("Home sig") } }) {
        QFile file(m_signatureDir + QStringLiteral("/") + entry.first);
        QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
        file.write(entry.second.toUtf8());
        file.close();
    }

    const QString path = m_dir->path() + QStringLiteral("/qtmaildir.conf");
    {
        QFile file(path);
        QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
        QTextStream out(&file);
        out << "[account.work]\n"
            << "name = Someone\naddress = someone@example.org\n"
            << "maildir = work\nsend_command = /bin/cat\n"
            << "signature = work\n"
            << "\n[account.home]\n"
            << "name = Someone\naddress = other@example.org\n"
            << "maildir = home\nsend_command = /bin/cat\n"
            << "signature = home\n";
    }
    Config config;
    config.load(path);

    // The saved body already carries its own signature, which does not match
    // any on-disk file. A From: change must not replace it with the new
    // account's: the draft is the message the user wrote, exactly as
    // seedBody() takes its body verbatim.
    ComposeContext context;
    context.kind = ComposeContext::Kind::Draft;
    context.accountKey = QStringLiteral("work");
    context.body = QStringLiteral("Half a thought.\n\n-- \nJane Doe");
    context.draftPath = m_dir->path() + QStringLiteral("/draft");

    ComposeWindow window(context, config, m_dir->path());
    window.setSignatureDir(m_signatureDir);
    window.seedSignature();

    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    auto *from = window.findChild<QComboBox *>(QStringLiteral("from"));
    QVERIFY(body);
    QVERIFY(from);
    QVERIFY(body->toPlainText().contains(QStringLiteral("Jane Doe")));

    const int home = from->findData(QStringLiteral("home"));
    QVERIFY(home >= 0);
    from->setCurrentIndex(home);

    QVERIFY(body->toPlainText().contains(QStringLiteral("Jane Doe")));
    QVERIFY(!body->toPlainText().contains(QStringLiteral("Home sig")));
}

void TestComposeWindow::savingADraftEmitsItsPathAndTheReplacedOne()
{
    // A config whose account has a drafts folder, which makeConfig() does not
    // set, so the save can actually write somewhere.
    const QString confPath = m_dir->path() + QStringLiteral("/qtmaildir.conf");
    {
        QString conf;
        QTextStream out(&conf);
        out << "[account.work]\n"
            << "name = Someone\n"
            << "address = someone@example.org\n"
            << "maildir = work\n"
            << "drafts = Drafts\n"
            << "send_command = /bin/cat\n";
        writeFile(confPath, conf);
    }
    Config config;
    config.load(confPath);

    ComposeContext context;
    context.kind = ComposeContext::Kind::New;
    context.accountKey = QStringLiteral("work");

    ComposeWindow window(context, config, m_dir->path());
    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(body);

    QSignalSpy saved(&window, &ComposeWindow::draftSaved);

    body->setPlainText(QStringLiteral("First revision."));
    QVERIFY(window.saveDraftNow());

    QCOMPARE(saved.size(), 1);
    const QString first = saved.first().at(0).toString();
    const QString firstPrevious = saved.first().at(1).toString();
    QVERIFY(!first.isEmpty());
    QVERIFY(firstPrevious.isEmpty());
    QVERIFY(QFile::exists(first));

    // A rewrite writes a fresh file and unlinks the old; the previous path
    // comes back so the owner can drop the old index entry.
    body->setPlainText(QStringLiteral("Second revision."));
    QVERIFY(window.saveDraftNow());

    QCOMPARE(saved.size(), 2);
    const QString second = saved.at(1).at(0).toString();
    const QString secondPrevious = saved.at(1).at(1).toString();
    QVERIFY(!second.isEmpty());
    QCOMPARE(secondPrevious, first);
    QVERIFY2(second != first, "a rewrite reused the old filename");
}

/// A helper for the status-bar tests: a config whose account has a drafts
/// folder, which makeConfig() deliberately does not set.
Config TestComposeWindow::configWithDrafts()
{
    const QString confPath = m_dir->path() + QStringLiteral("/qtmaildir.conf");
    QString conf;
    {
        QTextStream out(&conf);
        out << "[account.work]\n"
            << "name = Someone\n"
            << "address = someone@example.org\n"
            << "maildir = work\n"
            << "drafts = Drafts\n"
            << "send_command = /bin/cat\n";
    }
    writeFile(confPath, conf);

    Config config;
    config.load(confPath);
    return config;
}

/// Both cues answer the same question and must agree. The status label is
/// what the user reads while typing; the title marker is what they see when
/// the composer is behind another window.
void TestComposeWindow::anUnsavedEditIsAnnouncedInBothPlaces()
{
    const Config config = configWithDrafts();

    ComposeContext context;
    context.kind = ComposeContext::Kind::New;
    context.accountKey = QStringLiteral("work");

    ComposeWindow window(context, config, m_dir->path());

    // Seeding is not an edit: the constructor clears the flag after filling
    // the fields, so a composer nobody has typed into is clean.
    auto *unsaved = window.findChild<QLabel *>(QStringLiteral("unsavedCue"));
    QVERIFY(unsaved);
    QVERIFY2(unsaved->isHidden(), "a freshly opened composer is not dirty");
    QVERIFY2(!window.isWindowModified(),
             "a freshly opened composer must not claim unsaved edits");

    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(body);
    body->setPlainText(QStringLiteral("Something typed."));

    QVERIFY2(!unsaved->isHidden(), "the status cue must appear on an edit");
    QVERIFY2(window.isWindowModified(),
             "the title marker must appear on an edit");
}

/// The gap item 160 exists to close: a successful save said nothing at all.
void TestComposeWindow::aSavedDraftReportsItAndClearsTheCue()
{
    const Config config = configWithDrafts();

    ComposeContext context;
    context.kind = ComposeContext::Kind::New;
    context.accountKey = QStringLiteral("work");

    ComposeWindow window(context, config, m_dir->path());
    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(body);
    auto *unsaved = window.findChild<QLabel *>(QStringLiteral("unsavedCue"));
    auto *age = window.findChild<QLabel *>(QStringLiteral("draftAge"));
    QVERIFY(unsaved);
    QVERIFY(age);

    QVERIFY2(age->text().isEmpty(),
             "nothing has been saved yet, so there is no age to report");

    body->setPlainText(QStringLiteral("First revision."));
    QVERIFY(window.saveDraftNow());

    QVERIFY2(unsaved->isHidden(), "a save must clear the unsaved cue");
    QVERIFY2(!window.isWindowModified(),
             "a save must clear the title marker");
    QVERIFY2(!age->text().isEmpty(), "a save must be reported");
}

/// The send path clears the flag WITHOUT saving a draft, and it is one of the
/// four sites that write it. A cue hung off the save alone would leave a sent
/// message claiming unsaved edits on the way out.
void TestComposeWindow::aSentMessageLeavesNoUnsavedCue()
{
    const Config config = configWithDrafts();

    ComposeContext context;
    context.kind = ComposeContext::Kind::New;
    context.accountKey = QStringLiteral("work");

    ComposeWindow window(context, config, m_dir->path());
    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(body);
    body->setPlainText(QStringLiteral("Outgoing."));
    QVERIFY(window.isWindowModified());

    // What the send handler does on success, without running a real send.
    window.markClean();

    auto *unsaved = window.findChild<QLabel *>(QStringLiteral("unsavedCue"));
    QVERIFY(unsaved);
    QVERIFY2(unsaved->isHidden(), "a sent message has no unsaved edits");
    QVERIFY2(!window.isWindowModified(),
             "a sent message must not claim unsaved edits");
}

/// The test above drives markClean() directly, which proves what the SETTER
/// does and nothing about whether the send path calls it: a mutation putting
/// `m_dirty = false` back into the send handler left the whole suite green,
/// measured. That is CLAUDE.md's "a probe pointed at the wrong object".
///
/// The property that actually matters is structural, so it is asserted
/// structurally: m_dirty has ONE writer. Four of the seven sites that used to
/// assign it clear it, and only two of those are a save, so a cue hung off
/// the save path alone silently missed the constructor and the send.
void TestComposeWindow::onlyTheSetterWritesTheDirtyFlag()
{
    QFile source(QStringLiteral(SOURCE_DIR "/src/composewindow.cpp"));
    QVERIFY2(source.open(QIODevice::ReadOnly | QIODevice::Text),
             qPrintable(source.errorString()));
    const QStringList lines =
        QString::fromUtf8(source.readAll()).split(QLatin1Char('\n'));
    source.close();

    // A guard against the probe itself rotting: if the member is ever
    // renamed, this test must fail rather than quietly verify nothing.
    QVERIFY2(lines.join(QLatin1Char('\n')).contains(QStringLiteral("m_dirty")),
             "m_dirty is gone; this test needs updating, not deleting");

    QStringList offenders;
    for (int i = 0; i < lines.size(); ++i) {
        const QString line = lines.at(i);
        // An assignment, not a read: `m_dirty =` but not `m_dirty ==`.
        static const QRegularExpression assignment(
            QStringLiteral("\\bm_dirty\\s*=[^=]"));
        if (!assignment.match(line).hasMatch())
            continue;
        // The one legitimate writer.
        if (line.contains(QStringLiteral("m_dirty = dirty")))
            continue;
        offenders.append(QStringLiteral("%1: %2").arg(i + 1).arg(line.trimmed()));
    }

    QVERIFY2(offenders.isEmpty(),
             qPrintable(QStringLiteral(
                 "m_dirty must only be written by setDirty(), or the status "
                 "cue and the title marker drift from it. Offending lines:\n%1")
                            .arg(offenders.join(QLatin1Char('\n')))));
}

/// The age changes with no edit to drive it, so it needs a tick of its own.
/// Asserting on the TEXT changing rather than on a wording, which is
/// translated and would pin the test to one locale.
void TestComposeWindow::theAgeLineFollowsTheClock()
{
    const Config config = configWithDrafts();

    ComposeContext context;
    context.kind = ComposeContext::Kind::New;
    context.accountKey = QStringLiteral("work");

    ComposeWindow window(context, config, m_dir->path());
    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(body);
    body->setPlainText(QStringLiteral("First revision."));
    QVERIFY(window.saveDraftNow());

    auto *age = window.findChild<QLabel *>(QStringLiteral("draftAge"));
    QVERIFY(age);
    const QString justSaved = age->text();
    QVERIFY(!justSaved.isEmpty());

    // Driven rather than waited for: a real wait would put seconds into the
    // suite for a label that reads in tens of them.
    auto *tick = window.findChild<QTimer *>(QStringLiteral("draftAgeTick"));
    QVERIFY2(tick, "the age needs a tick of its own; an edit cannot drive it");
    QVERIFY(tick->isActive());

    window.reportDraftAgeFor(90);
    QVERIFY2(age->text() != justSaved,
             "the age line must move as the clock does");
}

QTEST_MAIN(TestComposeWindow)
#include "test_composewindow.moc"