From 8dec28ff4f1176b485d9de722756c677be3a4f1c Mon Sep 17 00:00:00 2001 From: "Danilo M." Date: Wed, 26 Aug 2026 16:44:45 +0200 Subject: fix: harden candidate appends and cover the message-row sender roles --- tests/test_businesssenders.cpp | 31 +++++++++++++++++++++++++++++++ tests/test_threadlistmodel.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) (limited to 'tests') diff --git a/tests/test_businesssenders.cpp b/tests/test_businesssenders.cpp index bc6a5f6..fb26e28 100644 --- a/tests/test_businesssenders.cpp +++ b/tests/test_businesssenders.cpp @@ -34,6 +34,7 @@ private slots: void matchingIsCaseInsensitive(); void anAbsentFileMatchesNothing(); void candidatesAreAppendedCommentedOut(); + void appendingDoesNotCorruptALineThatLacksATrailingNewline(); void anAddressAlreadyPresentIsNeverReproposed(); void onlyBulkLookingLocalPartsAreProposed(); void theFirstRunScansEverything(); @@ -126,6 +127,36 @@ void TestBusinessSenders::candidatesAreAppendedCommentedOut() 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 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; diff --git a/tests/test_threadlistmodel.cpp b/tests/test_threadlistmodel.cpp index 25c89b0..5c10b6c 100644 --- a/tests/test_threadlistmodel.cpp +++ b/tests/test_threadlistmodel.cpp @@ -103,6 +103,7 @@ private slots: void flatModeIsOffByDefaultAndReversible(); void recipientsReplaceTheSenderWhenPresent(); void aRowCarriesItsSenderAndAccountAddress(); + void aMessageRowCarriesItsOwnSenderAndAddress(); }; static ThreadSummary makeThread(const QString &id, const QString &subject) @@ -533,6 +534,32 @@ void TestThreadListModel::aRowCarriesItsSenderAndAccountAddress() QStringLiteral("John Doe")); } +void TestThreadListModel::aMessageRowCarriesItsOwnSenderAndAddress() +{ + // Task 8 counterpart of aRowCarriesItsSenderAndAccountAddress: that test + // covers the thread-row branch, and a role added to one branch and not the + // other is silently absent with nothing to flag it. A selected reply's + // avatar reads these, so the row that actually answers must carry them. + ThreadListModel model; + model.appendBatch({ makeThread(QStringLiteral("t1"), + QStringLiteral("A subject")) }); + + MessageNode root = makeNode(QStringLiteral("m0@example.org"), 0); + MessageNode reply = makeNode(QStringLiteral("m1@example.org"), 1, + QStringLiteral("Bob ")); + reply.senderAddress = QStringLiteral("bob@example.org"); + model.setThreadMessages(QStringLiteral("t1"), { root, reply }); + + const QModelIndex replyIndex = + model.index(0, 0, model.index(0, 0, QModelIndex())); + QVERIFY(model.isMessageRow(replyIndex)); + + QCOMPARE(replyIndex.data(ThreadListModel::SenderAddressRole).toString(), + QStringLiteral("bob@example.org")); + QCOMPARE(replyIndex.data(ThreadListModel::SenderNameRole).toString(), + QStringLiteral("Bob ")); +} + void TestThreadListModel::theReplyCountExcludesTheRootMessage() { ThreadListModel model; -- cgit v1.2.3