aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_messagebuilder.cpp
diff options
context:
space:
mode:
authorDanilo M. <danix@danix.xyz>2026-09-06 15:26:31 +0200
committerDanilo M. <danix@danix.xyz>2026-09-06 15:26:31 +0200
commit51b8fd5708d23108d532be1cf38e4bb619eeb777 (patch)
tree1f98bee2bf6da1950ae8958eaff737bfbf8d317b /tests/test_messagebuilder.cpp
parentc896eaf84b8b784622fd21380cf8d7f6d51028b2 (diff)
downloadqtmaildir-51b8fd5708d23108d532be1cf38e4bb619eeb777.tar.gz
qtmaildir-51b8fd5708d23108d532be1cf38e4bb619eeb777.zip
fix: keep one Message-ID across a draft's revisions
Every autosave called MessageBuilder::build(), which generated a fresh Message-ID unconditionally, so each revision of a draft was a different message rather than a new version of one. The entry called this invisible while the file is replaced correctly, and that turned out to be wrong: mbsync uploads each revision to the drafts folder before the next save removes the local file, so the server keeps one message per revision and syncs them all back down. Measured on real mail as four independent messages for a single reply, all four carrying a ,U= infix, threading into the conversation and putting a draft tag on a Sent row. Deleting a local file does not retract an uploaded one, which is why the local cleanup, which is correct, could never fix it. The user chose a stable id while drafting, discarded at send: the sent copy is a different item from the draft, and the draft is deleted once the message goes out, which the code already did. Three links, none of which existed. OutgoingMessage::messageId is the field, where empty means generate, so the send path is unchanged by construction rather than by remembering to clear it. MessageBuilder::build() uses a supplied id when there is one. ComposeWindow::m_draftMessageId holds the identity between revisions, assigned from built.messageId so the first save adopts the id GMime just generated, and ComposeContext::draftMessageId carries it across a reopen, read in forDraft() from ParsedMessage::messageId, which the parser already provided and nothing had ever used. Five tests, because the property spans three objects and a test at any one of them passes while another link is broken. Two are the safety constraint rather than the feature: a field defaulting to a fixed value would satisfy the reuse test and make two sent messages share an id, which is far worse than the defect this fixes. One comment is corrected rather than left: the autosave's dirty check justified comparing the message rather than the built bytes with "GMime is given a fresh Date and Message-ID on every build". Half of that is no longer true. The Date still is, so the conclusion stands. Revisions already on the server are not touched by this; the four found on real mail were deleted by hand. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Jq9gXquUo9W4KXDagJXMmn
Diffstat (limited to 'tests/test_messagebuilder.cpp')
-rw-r--r--tests/test_messagebuilder.cpp43
1 files changed, 43 insertions, 0 deletions
diff --git a/tests/test_messagebuilder.cpp b/tests/test_messagebuilder.cpp
index 2ea14f0..56cf6a9 100644
--- a/tests/test_messagebuilder.cpp
+++ b/tests/test_messagebuilder.cpp
@@ -56,6 +56,8 @@ private slots:
void aDirectoryAttachmentFailsRatherThanHangingTheProcess();
void anUnparseableRecipientFailsRatherThanVanishing();
void everyMessageCarriesADateAndMessageId();
+ void aSuppliedMessageIdIsUsedRatherThanAFreshOne();
+ void twoBuildsWithNoSuppliedIdStillDiffer();
void aForwardSendsOnePartChosenByTheHtmlToggle();
void recipientsAppearInTheirOwnHeaders();
void anAccountWithNoAddressFailsRatherThanBuildingHeaderlessMail();
@@ -419,6 +421,47 @@ void TestMessageBuilder::everyMessageCarriesADateAndMessageId()
QVERIFY(!r.messageId.isEmpty());
}
+/// Item 165. A draft keeps ONE identity across its revisions, so an autosave
+/// replaces the message it wrote last time rather than adding another. Without
+/// this every save minted a new Message-ID, and mbsync uploaded each revision
+/// to the drafts folder before the next save removed the local file: measured
+/// on real mail as four distinct messages on the server for one reply.
+void TestMessageBuilder::aSuppliedMessageIdIsUsedRatherThanAFreshOne()
+{
+ OutgoingMessage message = baseMessage();
+ message.messageId = QStringLiteral("kept-across-revisions@example.org");
+
+ const MessageBuilder::Result r = MessageBuilder::build(message, m_account);
+ QVERIFY2(r.ok(), qPrintable(r.error));
+
+ QCOMPARE(r.messageId, QStringLiteral("kept-across-revisions@example.org"));
+ QVERIFY2(QString::fromUtf8(r.bytes).contains(
+ QStringLiteral("<kept-across-revisions@example.org>")),
+ "the supplied id did not reach the headers");
+
+ // Twice, because the point is that a SECOND save keeps it. A test building
+ // once cannot tell a reused id from a freshly generated one.
+ const MessageBuilder::Result again = MessageBuilder::build(message, m_account);
+ QVERIFY2(again.ok(), qPrintable(again.error));
+ QCOMPARE(again.messageId, r.messageId);
+}
+
+/// The other half, and the constraint that makes the change safe: the SEND
+/// path supplies no id, and two sent messages must never share one. A field
+/// that defaulted to some fixed value would pass the test above and break
+/// this.
+void TestMessageBuilder::twoBuildsWithNoSuppliedIdStillDiffer()
+{
+ const MessageBuilder::Result first = MessageBuilder::build(baseMessage(), m_account);
+ const MessageBuilder::Result second = MessageBuilder::build(baseMessage(), m_account);
+ QVERIFY2(first.ok() && second.ok(), "a build failed");
+
+ QVERIFY(!first.messageId.isEmpty());
+ QVERIFY(!second.messageId.isEmpty());
+ QVERIFY2(first.messageId != second.messageId,
+ "two messages built with no supplied id share a Message-ID");
+}
+
/// Bcc must be PRESENT in the bytes. The documented send command is `msmtp -t`,
/// which reads its recipients FROM the headers and strips Bcc itself before
/// transmission. Removing it here would mean blind recipients never receive the