// SPDX-FileCopyrightText: 2026 Matvey Shcherbinin // SPDX-License-Identifier: BSD-2-Clause // Tests for the folders the conversation list is split into. // // What is being checked is membership, because that is where the list is // decided: a chat is in exactly one folder, a filed chat stays filed, and the // two built-in folders put a room and a one-to-one on different sides. A chat // in two folders is drawn twice and a chat in none is drawn nowhere, and both // look like the list losing rows rather than like a mistake in it. // // The QML side draws one section per folder and filters the rows through // folderFor(), so the two things worth having spelled out here are that a folder // says what it holds and that a chat lands somewhere. What the rows do with // that is the list's business and there is no window in this test. #include #include #include #include #include "../core/chat/ChatAddress.h" #include "../core/chat/ChatFolders.h" #include "../core/chat/ChatListModel.h" #include "../core/chat/UnreadManager.h" class ChatFoldersTest : public QObject { Q_OBJECT private Q_SLOTS: void initTestCase(); void init(); void cleanup(); void roomAndDirectLandApart(); void countsFollowMembership(); void userFolderWinsOverBuiltIn(); void assignMovesRatherThanCopies(); void assignBackToBuiltInForgetsTheFolder(); void removingAFolderReturnsItsChats(); void aFiledChatStaysFiledWithoutTheChat(); void theBuiltInPairCannotBeRenamedOrDeleted(); void foldersSurviveARestart(); void aFolderStaysInItsMode(); private: // Both are per test rather than per suite: a chat left in the list and a // folder left in the settings are the two ways one test can answer for // another, and the second of them outlives the process. static QString folderNamed(const ChatFolders &folders, const QString &name); static int countIn(const ChatFolders &folders, const QString &folderId); std::unique_ptr m_settingsDir; std::unique_ptr m_unread; std::unique_ptr m_chats; std::unique_ptr m_folders; }; void ChatFoldersTest::initTestCase() { // The folder list is written down with QSettings, and the default is the // user's own configuration file, so a test run would leave folders behind in // the application that starts next. The directory has to be one that outlives // this function: a local QTemporaryDir is deleted on the way out, and every // folder after that would be written to the real file. m_settingsDir = std::make_unique(); QVERIFY(m_settingsDir->isValid()); QSettings::setDefaultFormat(QSettings::IniFormat); QSettings::setPath(QSettings::IniFormat, QSettings::UserScope, m_settingsDir->path()); } void ChatFoldersTest::init() { m_unread = std::make_unique(); m_chats = std::make_unique(); m_chats->setUnreadManagerObj(m_unread.get()); // The folders are per test as well: they keep the ones they were told about // in memory, so a folder made in one test is still in the next one's list. m_folders = std::make_unique(); // Every chat these tests file is a Matrix one, so the folders have to be in // the Matrix mode to count them - the counts follow the mode like the list's // rows do. m_folders->setConnectionMode(1); m_folders->setChatListObj(m_chats.get()); } void ChatFoldersTest::cleanup() { m_folders.reset(); m_chats.reset(); m_unread.reset(); QSettings settings; settings.remove(QStringLiteral("ChatFolders")); settings.sync(); } QString ChatFoldersTest::folderNamed(const ChatFolders &folders, const QString &name) { const QVariantList all = folders.folders(); for (const QVariant &entry : all) { const QVariantMap folder = entry.toMap(); if (folder.value(QStringLiteral("name")).toString() == name) return folder.value(QStringLiteral("id")).toString(); } return {}; } int ChatFoldersTest::countIn(const ChatFolders &folders, const QString &folderId) { const QVariantList all = folders.folders(); for (const QVariant &entry : all) { const QVariantMap folder = entry.toMap(); if (folder.value(QStringLiteral("id")).toString() == folderId) return folder.value(QStringLiteral("count")).toInt(); } return -1; } void ChatFoldersTest::roomAndDirectLandApart() { const QString room = koutnet::chatid::matrixChatId(QStringLiteral("!room:example.org")); const QString peer = koutnet::chatid::matrixChatId(QStringLiteral("!dm:example.org")); m_chats->openChat(room, QStringLiteral("A room")); m_chats->openChat(peer, QStringLiteral("A person")); m_chats->setChatAsRoom(peer, false); // The id prefix is the same for both, so the flag the backend set is the only // thing that tells them apart. QCOMPARE(koutnet::chatid::transportOf(room), koutnet::chatid::Transport::Matrix); QCOMPARE(m_folders->folderFor(room), QStringLiteral("rooms")); QCOMPARE(m_folders->folderFor(peer), QStringLiteral("direct")); } void ChatFoldersTest::countsFollowMembership() { m_chats->openChat(koutnet::chatid::matrixChatId(QStringLiteral("!a:example.org")), QStringLiteral("A")); m_chats->openChat(koutnet::chatid::matrixChatId(QStringLiteral("!b:example.org")), QStringLiteral("B")); m_chats->openChat(koutnet::chatid::matrixChatId(QStringLiteral("!c:example.org")), QStringLiteral("C")); m_chats->setChatAsRoom(koutnet::chatid::matrixChatId(QStringLiteral("!b:example.org")), false); QCOMPARE(m_chats->roomCount(), 2); QCOMPARE(m_chats->directCount(), 1); // The headings read these, so a count that did not follow the list is a // heading that lies about what is under it. QCOMPARE(countIn(*m_folders, QStringLiteral("rooms")), 2); QCOMPARE(countIn(*m_folders, QStringLiteral("direct")), 1); } void ChatFoldersTest::userFolderWinsOverBuiltIn() { const QString room = koutnet::chatid::matrixChatId(QStringLiteral("!room:example.org")); m_chats->openChat(room, QStringLiteral("A room")); const QString made = m_folders->addFolder(QStringLiteral("Work")); QCOMPARE(m_folders->folderFor(room), QStringLiteral("rooms")); m_folders->assign(room, made); // Filing is the point of a folder somebody made, so it outranks the split // that is worked out rather than asked for. QCOMPARE(m_folders->folderFor(room), made); // The chat is in one folder, so it is counted in one folder. QCOMPARE(countIn(*m_folders, QStringLiteral("rooms")), 0); QCOMPARE(countIn(*m_folders, made), 1); } void ChatFoldersTest::assignMovesRatherThanCopies() { const QString chat = koutnet::chatid::matrixChatId(QStringLiteral("!room:example.org")); m_chats->openChat(chat, QStringLiteral("A room")); const QString first = m_folders->addFolder(QStringLiteral("One")); const QString second = m_folders->addFolder(QStringLiteral("Two")); m_folders->assign(chat, first); QCOMPARE(m_folders->folderFor(chat), first); QCOMPARE(countIn(*m_folders, first), 1); m_folders->assign(chat, second); // One folder at a time: the same conversation under two headings is the list // showing it twice. QCOMPARE(m_folders->folderFor(chat), second); QCOMPARE(countIn(*m_folders, first), 0); QCOMPARE(countIn(*m_folders, second), 1); QCOMPARE(countIn(*m_folders, QStringLiteral("rooms")), 0); } void ChatFoldersTest::assignBackToBuiltInForgetsTheFolder() { const QString chat = koutnet::chatid::matrixChatId(QStringLiteral("!room:example.org")); m_chats->openChat(chat, QStringLiteral("A room")); const QString made = m_folders->addFolder(QStringLiteral("Work")); m_folders->assign(chat, made); QCOMPARE(m_folders->folderFor(chat), made); // Putting a chat back under the built-in split only forgets the folder: what // kind of conversation it is was never the folder's to say. So a room sent to // "direct" comes back a room, and the filing is gone rather than waiting to // outrank the split again the next time a folder of that name is made. m_folders->assign(chat, QStringLiteral("direct")); QCOMPARE(m_folders->folderFor(chat), QStringLiteral("rooms")); QCOMPARE(countIn(*m_folders, made), 0); m_folders->assign(chat, QStringLiteral("rooms")); QCOMPARE(m_folders->folderFor(chat), QStringLiteral("rooms")); } void ChatFoldersTest::removingAFolderReturnsItsChats() { const QString room = koutnet::chatid::matrixChatId(QStringLiteral("!room:example.org")); const QString peer = koutnet::chatid::matrixChatId(QStringLiteral("!dm:example.org")); m_chats->openChat(room, QStringLiteral("A room")); m_chats->openChat(peer, QStringLiteral("A person")); m_chats->setChatAsRoom(peer, false); const QString made = m_folders->addFolder(QStringLiteral("Work")); m_folders->assign(room, made); m_folders->assign(peer, made); m_folders->removeFolder(made); // Nowhere to put them but the split they belong to, which is the answer the // two built-in folders knew all along. QCOMPARE(m_folders->folderFor(room), QStringLiteral("rooms")); QCOMPARE(m_folders->folderFor(peer), QStringLiteral("direct")); QCOMPARE(countIn(*m_folders, QStringLiteral("rooms")), 1); QCOMPARE(countIn(*m_folders, QStringLiteral("direct")), 1); QCOMPARE(m_folders->folders().size(), 2); } void ChatFoldersTest::aFiledChatStaysFiledWithoutTheChat() { const QString room = koutnet::chatid::matrixChatId(QStringLiteral("!room:example.org")); m_chats->openChat(room, QStringLiteral("A room")); const QString made = m_folders->addFolder(QStringLiteral("Work")); m_folders->assign(room, made); m_chats->removeChat(room); // A room that has not been synced is not a one-to-one, and a folder that // emptied itself the moment a room fell out of the list would be a folder // that loses what was put in it. QCOMPARE(m_folders->folderFor(room), made); } void ChatFoldersTest::theBuiltInPairCannotBeRenamedOrDeleted() { const QString rooms = QStringLiteral("rooms"); const QString before = folderNamed(*m_folders, QStringLiteral("Group chats")); m_folders->renameFolder(rooms, QStringLiteral("Rooms")); m_folders->removeFolder(rooms); m_folders->removeFolder(QStringLiteral("direct")); // They are worked out from the chats rather than stored, so there is nothing // to write a new name to and nothing to delete: the pair is the answer the // app has for a chat nobody has filed. QCOMPARE(folderNamed(*m_folders, QStringLiteral("Group chats")), before); QCOMPARE(folderNamed(*m_folders, QStringLiteral("Rooms")), QString()); QCOMPARE(m_folders->folders().size(), 2); // A chat nobody has filed is answered for by the split, even one the list has // not heard of: not in the list is not a one-to-one. QCOMPARE(m_folders->folderFor(QStringLiteral("mx:!whatever:example.org")), QStringLiteral("rooms")); } void ChatFoldersTest::foldersSurviveARestart() { const QString chat = koutnet::chatid::matrixChatId(QStringLiteral("!room:example.org")); m_chats->openChat(chat, QStringLiteral("A room")); const QString made = m_folders->addFolder(QStringLiteral("Work")); m_folders->assign(chat, made); // A second pair, the way the next start of the application sees it: the name // and the membership are on disk and nothing else is carried over. ChatListModel chats; chats.setUnreadManagerObj(m_unread.get()); ChatFolders folders; folders.setConnectionMode(1); folders.setChatListObj(&chats); QCOMPARE(folders.folders().size(), 3); QCOMPARE(folderNamed(folders, QStringLiteral("Work")), made); // The chat itself is not in this list, and the folder it was filed in is // still its folder. QCOMPARE(folders.folderFor(chat), made); } void ChatFoldersTest::aFolderStaysInItsMode() { const QString chat = koutnet::chatid::matrixChatId(QStringLiteral("!room:example.org")); m_chats->openChat(chat, QStringLiteral("A room")); const QString made = m_folders->addFolder(QStringLiteral("Work")); m_folders->assign(chat, made); QCOMPARE(countIn(*m_folders, made), 1); // Switching the rail to the LAN mode empties the folder in that mode: the // chats of another protocol are hidden from the rows, and a heading that // still counted them would promise rows the list does not draw. The folder // itself is not offered there either - it was made in the Matrix mode. m_folders->setConnectionMode(0); QCOMPARE(m_folders->folders().size(), 2); QCOMPARE(folderNamed(*m_folders, QStringLiteral("Work")), QString()); // and the chat falls back to the built-in split there. QCOMPARE(m_folders->folderFor(chat), QStringLiteral("rooms")); // Back in the Matrix mode, the folder and its filing are as they were. m_folders->setConnectionMode(1); QCOMPARE(folderNamed(*m_folders, QStringLiteral("Work")), made); QCOMPARE(m_folders->folderFor(chat), made); QCOMPARE(countIn(*m_folders, made), 1); } QTEST_MAIN(ChatFoldersTest) #include "ChatFoldersTest.moc"