test: add snapshot tests for MessageBuilder (#5598)

This commit is contained in:
nerix
2024-10-13 12:38:10 +02:00
committed by GitHub
parent 3e2116629a
commit d9453313b3
90 changed files with 14661 additions and 218 deletions
+13
View File
@@ -1,5 +1,6 @@
#include "util/Helpers.hpp"
#include "Application.hpp"
#include "providers/twitch/TwitchCommon.hpp"
#include <QDirIterator>
@@ -301,4 +302,16 @@ QString unescapeZeroWidthJoiner(QString escaped)
return escaped;
}
QLocale getSystemLocale()
{
#ifdef CHATTERINO_WITH_TESTS
if (getApp()->isTest())
{
return {QLocale::English};
}
#endif
return QLocale::system();
}
} // namespace chatterino
+2
View File
@@ -189,4 +189,6 @@ constexpr std::optional<std::decay_t<T>> makeConditionedOptional(bool condition,
/// a ZWJ. See also: https://github.com/Chatterino/chatterino2/issues/3384.
QString unescapeZeroWidthJoiner(QString escaped);
QLocale getSystemLocale();
} // namespace chatterino
+72
View File
@@ -0,0 +1,72 @@
#include "util/IrcHelpers.hpp"
#include "Application.hpp"
namespace {
using namespace chatterino;
QDateTime calculateMessageTimeBase(const Communi::IrcMessage *message)
{
// Check if message is from recent-messages API
if (message->tags().contains("historical"))
{
bool customReceived = false;
auto ts =
message->tags().value("rm-received-ts").toLongLong(&customReceived);
if (!customReceived)
{
ts = message->tags().value("tmi-sent-ts").toLongLong();
}
return QDateTime::fromMSecsSinceEpoch(ts);
}
// If present, handle tmi-sent-ts tag and use it as timestamp
if (message->tags().contains("tmi-sent-ts"))
{
auto ts = message->tags().value("tmi-sent-ts").toLongLong();
return QDateTime::fromMSecsSinceEpoch(ts);
}
// Some IRC Servers might have server-time tag containing UTC date in ISO format, use it as timestamp
// See: https://ircv3.net/irc/#server-time
if (message->tags().contains("time"))
{
QString timedate = message->tags().value("time").toString();
auto date = QDateTime::fromString(timedate, Qt::ISODate);
date.setTimeZone(QTimeZone::utc());
return date.toLocalTime();
}
// Fallback to current time
#ifdef CHATTERINO_WITH_TESTS
if (getApp()->isTest())
{
return QDateTime::fromMSecsSinceEpoch(0, QTimeZone::utc());
}
#endif
return QDateTime::currentDateTime();
}
} // namespace
namespace chatterino {
QDateTime calculateMessageTime(const Communi::IrcMessage *message)
{
auto dt = calculateMessageTimeBase(message);
#ifdef CHATTERINO_WITH_TESTS
if (getApp()->isTest())
{
return dt.toUTC();
}
#endif
return dt;
}
} // namespace chatterino
+1 -37
View File
@@ -59,43 +59,7 @@ inline QString parseTagString(const QString &input)
return output;
}
inline QDateTime calculateMessageTime(const Communi::IrcMessage *message)
{
// Check if message is from recent-messages API
if (message->tags().contains("historical"))
{
bool customReceived = false;
auto ts =
message->tags().value("rm-received-ts").toLongLong(&customReceived);
if (!customReceived)
{
ts = message->tags().value("tmi-sent-ts").toLongLong();
}
return QDateTime::fromMSecsSinceEpoch(ts);
}
// If present, handle tmi-sent-ts tag and use it as timestamp
if (message->tags().contains("tmi-sent-ts"))
{
auto ts = message->tags().value("tmi-sent-ts").toLongLong();
return QDateTime::fromMSecsSinceEpoch(ts);
}
// Some IRC Servers might have server-time tag containing UTC date in ISO format, use it as timestamp
// See: https://ircv3.net/irc/#server-time
if (message->tags().contains("time"))
{
QString timedate = message->tags().value("time").toString();
auto date = QDateTime::fromString(timedate, Qt::ISODate);
date.setTimeZone(QTimeZone::utc());
return date.toLocalTime();
}
// Fallback to current time
return QDateTime::currentDateTime();
}
QDateTime calculateMessageTime(const Communi::IrcMessage *message);
// "foo/bar/baz,tri/hard" can be a valid badge-info tag
// In that case, valid map content should be 'split by slash' only once: