refactor: trim down PubSub implementation (#6158)

This commit is contained in:
nerix
2025-04-19 14:31:22 +02:00
committed by GitHub
parent 52067d0af7
commit 9d557146b4
24 changed files with 20 additions and 3014 deletions
+9 -181
View File
@@ -1,8 +1,6 @@
#include "mocks/BaseApplication.hpp"
#include "providers/twitch/PubSubActions.hpp"
#include "providers/twitch/PubSubClient.hpp"
#include "providers/twitch/PubSubManager.hpp"
#include "providers/twitch/pubsubmessages/AutoMod.hpp"
#include "providers/twitch/TwitchAccount.hpp"
#include "Test.hpp"
@@ -20,11 +18,7 @@ using namespace std::chrono_literals;
* Server randomly disconnects us, we should reconnect (COMPLETE)
* Client listens to more than 50 topics, so it opens 2 connections (COMPLETE)
* Server sends RECONNECT message to us, we should reconnect (INCOMPLETE, leaving for now since if we just ignore it and Twitch disconnects us we should already handle it properly)
* Listen that required authentication, but authentication is missing (COMPLETE)
* Listen that required authentication, but authentication is wrong (COMPLETE)
* Incoming AutoMod message
* Incoming ChannelPoints message
* Incoming ChatModeratorAction message (COMPLETE)
**/
#define RUN_PUBSUB_TESTS
@@ -85,23 +79,18 @@ const QString TEST_SETTINGS = R"(
class FTest : public PubSub
{
public:
explicit FTest(const char *path, std::chrono::seconds pingInterval,
QString token = "token")
explicit FTest(const char *path, std::chrono::seconds pingInterval)
: PubSub(QString("wss://127.0.0.1:9050%1").arg(path), pingInterval)
{
auto account = std::make_shared<TwitchAccount>("testaccount_420", token,
"clientid", "123456");
this->setAccount(account);
}
};
class MockApplication : public mock::BaseApplication
{
public:
MockApplication(const char *path, std::chrono::seconds pingInterval,
QString token = "token")
MockApplication(const char *path, std::chrono::seconds pingInterval)
: mock::BaseApplication(TEST_SETTINGS)
, pubSub(path, pingInterval, token)
, pubSub(path, pingInterval)
{
}
@@ -127,7 +116,7 @@ TEST(TwitchPubSubClient, ServerRespondsToPings)
ASSERT_EQ(pubSub.diag.connectionsFailed, 0);
ASSERT_EQ(pubSub.diag.messagesReceived, 0);
pubSub.listenToChannelModerationActions("123456");
pubSub.listenToChannelPointRewards("123456");
std::this_thread::sleep_for(150ms);
@@ -159,7 +148,7 @@ TEST(TwitchPubSubClient, ServerDoesntRespondToPings)
auto &pubSub = a.pubSub;
pubSub.start();
pubSub.listenToChannelModerationActions("123456");
pubSub.listenToChannelPointRewards("123456");
std::this_thread::sleep_for(750ms);
@@ -198,7 +187,7 @@ TEST(TwitchPubSubClient, DisconnectedAfter1s)
ASSERT_EQ(pubSub.diag.messagesReceived, 0);
ASSERT_EQ(pubSub.diag.listenResponses, 0);
pubSub.listenToChannelModerationActions("123456");
pubSub.listenToChannelPointRewards("123456");
std::this_thread::sleep_for(500ms);
@@ -233,7 +222,7 @@ TEST(TwitchPubSubClient, ExceedTopicLimit)
for (auto i = 0; i < PubSubClient::MAX_LISTENS; ++i)
{
pubSub.listenToChannelModerationActions(QString("1%1").arg(i));
pubSub.listenToChannelPointRewards(QString("1%1").arg(i));
}
std::this_thread::sleep_for(50ms);
@@ -244,7 +233,7 @@ TEST(TwitchPubSubClient, ExceedTopicLimit)
for (auto i = 0; i < PubSubClient::MAX_LISTENS; ++i)
{
pubSub.listenToChannelModerationActions(QString("2%1").arg(i));
pubSub.listenToChannelPointRewards(QString("2%1").arg(i));
}
std::this_thread::sleep_for(50ms);
@@ -274,7 +263,7 @@ TEST(TwitchPubSubClient, ExceedTopicLimitSingleStep)
for (auto i = 0; i < PubSubClient::MAX_LISTENS * 2; ++i)
{
pubSub.listenToChannelModerationActions("123456");
pubSub.listenToChannelPointRewards("123456");
}
std::this_thread::sleep_for(150ms);
@@ -290,167 +279,6 @@ TEST(TwitchPubSubClient, ExceedTopicLimitSingleStep)
ASSERT_EQ(pubSub.diag.connectionsFailed, 0);
}
TEST(TwitchPubSubClient, ModeratorActionsUserBanned)
{
MockApplication a("/moderator-actions-user-banned", 1s);
auto &pubSub = a.pubSub;
pubSub.start();
ReceivedMessage<BanAction> received;
std::ignore =
pubSub.moderation.userBanned.connect([&received](const auto &action) {
received = action;
});
ASSERT_EQ(pubSub.diag.listenResponses, 0);
pubSub.listenToChannelModerationActions("123456");
std::this_thread::sleep_for(50ms);
ASSERT_EQ(pubSub.diag.connectionsOpened, 1);
ASSERT_EQ(pubSub.diag.connectionsClosed, 0);
ASSERT_EQ(pubSub.diag.connectionsFailed, 0);
ASSERT_EQ(pubSub.diag.messagesReceived, 3);
ASSERT_EQ(pubSub.diag.listenResponses, 1);
ASSERT_TRUE(received);
ActionUser expectedTarget{"140114344", "1xelerate", "", QColor()};
ActionUser expectedSource{"117691339", "mm2pl", "", QColor()};
ASSERT_EQ(received->reason, QString());
ASSERT_EQ(received->duration, 0);
ASSERT_EQ(received->target, expectedTarget);
ASSERT_EQ(received->source, expectedSource);
pubSub.stop();
ASSERT_EQ(pubSub.diag.connectionsOpened, 1);
ASSERT_EQ(pubSub.diag.connectionsClosed, 1);
ASSERT_EQ(pubSub.diag.connectionsFailed, 0);
}
TEST(TwitchPubSubClient, MissingToken)
{
// The token that's required is "xD"
MockApplication a("/authentication-required", 1s, "");
auto &pubSub = a.pubSub;
pubSub.start();
pubSub.listenToChannelModerationActions("123456");
std::this_thread::sleep_for(150ms);
ASSERT_EQ(pubSub.diag.connectionsOpened, 1);
ASSERT_EQ(pubSub.diag.connectionsClosed, 0);
ASSERT_EQ(pubSub.diag.connectionsFailed, 0);
ASSERT_EQ(pubSub.diag.messagesReceived, 2);
ASSERT_EQ(pubSub.diag.listenResponses, 0);
ASSERT_EQ(pubSub.diag.failedListenResponses, 1);
pubSub.stop();
ASSERT_EQ(pubSub.diag.connectionsOpened, 1);
ASSERT_EQ(pubSub.diag.connectionsClosed, 1);
ASSERT_EQ(pubSub.diag.connectionsFailed, 0);
}
TEST(TwitchPubSubClient, WrongToken)
{
// The token that's required is "xD"
MockApplication a("/authentication-required", 1s);
auto &pubSub = a.pubSub;
pubSub.start();
pubSub.listenToChannelModerationActions("123456");
std::this_thread::sleep_for(50ms);
ASSERT_EQ(pubSub.diag.connectionsOpened, 1);
ASSERT_EQ(pubSub.diag.connectionsClosed, 0);
ASSERT_EQ(pubSub.diag.connectionsFailed, 0);
ASSERT_EQ(pubSub.diag.messagesReceived, 2);
ASSERT_EQ(pubSub.diag.listenResponses, 0);
ASSERT_EQ(pubSub.diag.failedListenResponses, 1);
pubSub.stop();
ASSERT_EQ(pubSub.diag.connectionsOpened, 1);
ASSERT_EQ(pubSub.diag.connectionsClosed, 1);
ASSERT_EQ(pubSub.diag.connectionsFailed, 0);
}
TEST(TwitchPubSubClient, CorrectToken)
{
// The token that's required is "xD"
MockApplication a("/authentication-required", 1s, "xD");
auto &pubSub = a.pubSub;
pubSub.start();
pubSub.listenToChannelModerationActions("123456");
std::this_thread::sleep_for(50ms);
ASSERT_EQ(pubSub.diag.connectionsOpened, 1);
ASSERT_EQ(pubSub.diag.connectionsClosed, 0);
ASSERT_EQ(pubSub.diag.connectionsFailed, 0);
ASSERT_EQ(pubSub.diag.messagesReceived, 2);
ASSERT_EQ(pubSub.diag.listenResponses, 1);
ASSERT_EQ(pubSub.diag.failedListenResponses, 0);
pubSub.stop();
ASSERT_EQ(pubSub.diag.connectionsOpened, 1);
ASSERT_EQ(pubSub.diag.connectionsClosed, 1);
ASSERT_EQ(pubSub.diag.connectionsFailed, 0);
}
TEST(TwitchPubSubClient, AutoModMessageHeld)
{
MockApplication a("/automod-held", 1s);
auto &pubSub = a.pubSub;
pubSub.start();
ReceivedMessage<PubSubAutoModQueueMessage> received;
ReceivedMessage<QString> channelID;
std::ignore = pubSub.moderation.autoModMessageCaught.connect(
[&](const auto &msg, const QString &incomingChannelID) {
received = msg;
channelID = incomingChannelID;
});
pubSub.listenToAutomod("117166826");
std::this_thread::sleep_for(50ms);
ASSERT_EQ(pubSub.diag.connectionsOpened, 1);
ASSERT_EQ(pubSub.diag.connectionsClosed, 0);
ASSERT_EQ(pubSub.diag.connectionsFailed, 0);
ASSERT_EQ(pubSub.diag.messagesReceived, 3);
ASSERT_EQ(pubSub.diag.listenResponses, 1);
ASSERT_EQ(pubSub.diag.failedListenResponses, 0);
ASSERT_TRUE(received);
ASSERT_TRUE(channelID);
ASSERT_EQ(channelID, "117166826");
ASSERT_EQ(received->messageText, "kurwa");
pubSub.stop();
ASSERT_EQ(pubSub.diag.connectionsOpened, 1);
ASSERT_EQ(pubSub.diag.connectionsClosed, 1);
ASSERT_EQ(pubSub.diag.connectionsFailed, 0);
}
} // namespace chatterino
#endif