#include "SocialTestSupport.hpp"
#include "screens/conversation/ConversationModel.hpp"
using namespace dp;
using namespace dp::test;
namespace {
struct Fixture {
SignedInMock mock;
AlertLog log;
FakeSocket* socket = nullptr;
double clock = 100;
ConversationModel model;
Fixture()
: model(MockFixtures::alice(), mock.api(), log.sink(),
[this](const std::string&) {
auto s = std::make_unique<FakeSocket>();
socket = s.get();
return std::unique_ptr<MessagesSocket>(std::move(s));
},
[this] { return clock; }) {}
void connect() {
model.startRealtime();
socket->emit(ChatSocketEvent::ready(AppState::instance().currentUser()->id));
}
void loadAndWait() {
bool done = false;
model.load([&] { done = true; });
CHECK(pumpUntil([&] { return done; }));
}
};
ChatSocketIncomingMessage incoming(const std::string& uid, const std::string& from, const std::string& to,
const std::string& text, std::optional<std::string> clientId = std::nullopt) {
ChatSocketIncomingMessage m;
m.uid = uid;
m.senderUid = from;
m.receiverUid = to;
m.content = text;
m.createdAt = DateCodec::now();
m.clientId = std::move(clientId);
return m;
}
} // namespace
DP_TEST(social_conversation_draft_validation_counts_utf16_units) {
Fixture f;
CHECK(!f.model.canSend()); // empty draft needs at least 1 unit
f.model.setDraft("hi");
CHECK(f.model.canSend());
f.model.setDraft("\xF0\x9F\x98\x80"); // one emoji = 2 UTF-16 units
CHECK_EQ(f.model.draftCount(), 2);
f.model.setDraft(std::string(2000, 'a'));
CHECK(f.model.canSend());
f.model.setDraft(std::string(2001, 'a'));
CHECK(!f.model.canSend());
std::string emojis;
for (int i = 0; i < 1001; ++i) emojis += "\xF0\x9F\x98\x80"; // 2002 units
f.model.setDraft(emojis);
CHECK_EQ(f.model.draftCount(), 2002);
CHECK(!f.model.canSend());
f.model.setDraft(" "); // whitespace only is allowed (count based)
CHECK(f.model.canSend());
}
DP_TEST(social_conversation_attachments_lower_the_minimum) {
Fixture f;
CHECK_EQ(f.model.minCharacterCount(), 1);
UploadResponse up;
up.id = "up1";
up.url = "https://x/y.png";
up.isImage = true;
f.model.setAttachments({up});
CHECK_EQ(f.model.minCharacterCount(), 0);
CHECK(f.model.canSend()); // empty text with an attachment
}
DP_TEST(social_conversation_socket_send_is_optimistic_and_echo_replaces) {
Fixture f;
f.loadAndWait();
const std::size_t before = f.model.messages().size();
f.connect();
CHECK(f.model.isSocketConnected());
CHECK_EQ(f.socket->reads.size(), static_cast<std::size_t>(1)); // ready sends a read frame
f.model.setDraft("hello there");
bool ok = false;
f.model.send([&](bool r) { ok = r; });
CHECK(ok);
CHECK_EQ(f.model.draft(), std::string());
CHECK_EQ(f.model.messages().size(), before + 1);
CHECK_EQ(f.socket->sent.size(), static_cast<std::size_t>(1));
const std::string clientId = f.socket->sent[0].clientId;
CHECK_EQ(clientId.size(), static_cast<std::size_t>(36));
const Message& optimistic = f.model.messages().back();
CHECK_EQ(optimistic.data.id, clientId);
CHECK(optimistic.isMine && !optimistic.data.read);
// Server echo with the client id replaces the optimistic bubble.
const std::string me = AppState::instance().currentUser()->id;
f.socket->emit(ChatSocketEvent::incoming(incoming("srv1", me, f.model.otherUser().id, "hello there", clientId)));
CHECK_EQ(f.model.messages().size(), before + 1);
CHECK_EQ(f.model.messages().back().data.id, std::string("srv1"));
CHECK(f.model.messages().back().isMine);
}
DP_TEST(social_conversation_rest_fallback_when_socket_not_connected) {
Fixture f;
f.loadAndWait();
f.model.setDraft("via rest");
bool finished = false, ok = false;
f.model.send([&](bool r) { finished = true; ok = r; });
CHECK(f.model.isSending());
CHECK(f.model.messages().back().data.content == "via rest");
CHECK(pumpUntil([&] { return finished; }, 5.0));
CHECK(ok);
CHECK(!f.model.isSending());
for (const auto& m : f.model.messages()) CHECK(m.data.content != "via rest"); // optimistic removed, server list reseeded
CHECK(f.log.alerts.empty());
}
DP_TEST(social_conversation_typing_sender_is_throttled_to_1_5_seconds) {
Fixture f;
f.connect();
f.model.setDraft("a");
CHECK_EQ(f.socket->typings, 1);
f.clock += 1.0;
f.model.setDraft("ab");
CHECK_EQ(f.socket->typings, 1);
f.clock += 0.6; // 1.6 s since the first frame
f.model.setDraft("abc");
CHECK_EQ(f.socket->typings, 2);
}
DP_TEST(social_conversation_typing_not_sent_without_connected_socket) {
Fixture f;
f.model.setDraft("a");
CHECK(f.socket == nullptr);
f.model.startRealtime();
f.model.setDraft("ab"); // socket exists but no ready yet
CHECK_EQ(f.socket->typings, 0);
}
DP_TEST(social_conversation_incoming_message_sends_read_and_ignores_strangers) {
Fixture f;
f.connect();
const std::string other = f.model.otherUser().id;
const std::string me = AppState::instance().currentUser()->id;
const std::size_t before = f.model.messages().size();
f.socket->emit(ChatSocketEvent::incoming(incoming("m9", other, me, "ping")));
CHECK_EQ(f.model.messages().size(), before + 1);
CHECK(!f.model.messages().back().isMine);
CHECK_EQ(f.socket->reads.size(), static_cast<std::size_t>(2)); // ready + incoming
f.socket->emit(ChatSocketEvent::incoming(incoming("m10", "stranger", me, "spam")));
CHECK_EQ(f.model.messages().size(), before + 1);
// The same uid again replaces instead of appending.
f.socket->emit(ChatSocketEvent::incoming(incoming("m9", other, me, "ping edited")));
CHECK_EQ(f.model.messages().size(), before + 1);
CHECK_EQ(f.model.messages().back().data.content, std::string("ping edited"));
}
DP_TEST(social_conversation_read_event_marks_only_own_messages) {
Fixture f;
f.connect();
const std::string other = f.model.otherUser().id;
const std::string me = AppState::instance().currentUser()->id;
f.socket->emit(ChatSocketEvent::incoming(incoming("a1", me, other, "mine")));
f.socket->emit(ChatSocketEvent::incoming(incoming("a2", other, me, "theirs")));
f.socket->emit(ChatSocketEvent::read("somebody-else"));
for (const auto& m : f.model.messages()) CHECK(!m.data.read);
f.socket->emit(ChatSocketEvent::read(other));
for (const auto& m : f.model.messages()) CHECK_EQ(m.data.read, m.isMine);
}
DP_TEST(social_conversation_typing_event_sets_indicator_for_other_user_only) {
Fixture f;
f.connect();
f.socket->emit(ChatSocketEvent::typing("stranger"));
CHECK(!f.model.isOtherTyping());
f.socket->emit(ChatSocketEvent::typing(f.model.otherUser().id));
CHECK(f.model.isOtherTyping());
f.model.stopRealtime();
CHECK(!f.model.isOtherTyping());
CHECK(!f.model.isSocketConnected());
}
DP_TEST(social_conversation_failed_frame_removes_bubble_and_alerts) {
Fixture f;
f.connect();
f.model.setDraft("will fail");
f.model.send();
const std::string clientId = f.socket->sent[0].clientId;
const std::size_t withBubble = f.model.messages().size();
f.socket->emit(ChatSocketEvent::failed(clientId, "Message too long"));
CHECK_EQ(f.model.messages().size(), withBubble - 1);
CHECK_EQ(f.log.alerts.size(), static_cast<std::size_t>(1));
CHECK_EQ(f.log.alerts[0].message, std::string("Message too long"));
CHECK_EQ(f.log.alerts[0].title, std::string("Error"));
CHECK_EQ(f.log.alerts[0].buttonText, std::string("OK"));
// Swift quirk kept: the draft is not restored because removeOptimistic cleared the pending entry first.
CHECK_EQ(f.model.draft(), std::string());
}
DP_TEST(social_conversation_reload_keeps_pending_optimistic_bubble) {
Fixture f;
f.loadAndWait();
f.connect();
f.model.setDraft("in flight");
f.model.send();
bool done = false;
f.model.reload();
CHECK(f.model.isReloading());
CHECK(pumpUntil([&] { return !f.model.isReloading(); }));
done = true;
CHECK(done);
bool found = false;
for (const auto& m : f.model.messages()) found = found || m.data.content == "in flight";
CHECK(found);
}
DP_TEST(social_conversation_unsent_attachments_are_cleared_on_delete) {
Fixture f;
UploadResponse up;
up.id = "u-1";
f.model.setAttachments({up});
f.model.deleteUnsubmittedAttachments();
CHECK(f.model.attachments().empty());
}
DP_TEST(social_conversation_start_is_idempotent_and_stop_disconnects) {
Fixture f;
f.model.startRealtime();
FakeSocket* first = f.socket;
f.model.startRealtime();
CHECK(f.socket == first);
CHECK_EQ(first->connects, 1);
f.model.stopRealtime();
CHECK(!f.model.hasSocket());
}