Skip to content
Repos

DevPlaceApp

C++ 4.1 KB67 lines Opens in site

Directory Raw Download

#include "TestHarness.hpp"
#include "model/ChatSocketCodec.hpp"

using namespace dp;

DP_TEST(chat_encode_is_byte_compatible) {
  CHECK_EQ(ChatSocketCodec::encodeSend("u2", "hi", {}, "c1"),
           std::string(R"({"type":"send","receiver_uid":"u2","content":"hi","attachment_uids":[],"client_id":"c1"})"));
  CHECK_EQ(ChatSocketCodec::encodeSend("u2", "a/b \"q\" \\ \n\t", {"x", "y"}, "c"),
           std::string(R"({"type":"send","receiver_uid":"u2","content":"a\/b \"q\" \\ \n\t","attachment_uids":["x","y"],"client_id":"c"})"));
  CHECK_EQ(ChatSocketCodec::encodeTyping("u9"), std::string(R"({"type":"typing","receiver_uid":"u9"})"));
  CHECK_EQ(ChatSocketCodec::encodeRead("u9"), std::string(R"({"type":"read","with_uid":"u9"})"));
  CHECK_EQ(ChatSocketCodec::quote("caf\xC3\xA9 \xF0\x9F\x98\x84"), std::string("\"caf\xC3\xA9 \xF0\x9F\x98\x84\""));
  CHECK_EQ(ChatSocketCodec::quote(std::string("\x01\x1f", 2)), std::string("\"\\u0001\\u001f\""));
}

DP_TEST(chat_decode_all_frames) {
  auto ready = ChatSocketCodec::decode(R"({"type":"ready","user_uid":"u1"})");
  CHECK(ready && ready->type == ChatSocketEvent::Type::Ready && ready->userUid == "u1");
  auto typing = ChatSocketCodec::decode(R"({"type":"typing","from_uid":"u2"})");
  CHECK(typing && typing->type == ChatSocketEvent::Type::Typing && typing->fromUid == "u2");
  auto read = ChatSocketCodec::decode(R"({"type":"read","by_uid":"u2"})");
  CHECK(read && read->type == ChatSocketEvent::Type::Read && read->byUid == "u2");
  auto err = ChatSocketCodec::decode(R"({"type":"error","text":"bad","client_id":"c1"})");
  CHECK(err && err->type == ChatSocketEvent::Type::Failed && err->text == "bad" && err->clientId == std::optional<std::string>("c1"));
  auto err2 = ChatSocketCodec::decode(R"({"type":"error","text":"bad"})");
  CHECK(err2 && !err2->clientId.has_value());
}

DP_TEST(chat_decode_message_with_lenient_attachments) {
  auto m = ChatSocketCodec::decode(
      R"({"type":"message","uid":"m1","sender_uid":"a","receiver_uid":"b","content":"yo","created_at":"2026-11-29T08:00:00.123Z",)"
      R"("client_id":"c9","attachments":[{"uid":"a1","url":"https://x/y"},{"uid":"a2","url":"u","is_image":true,"size":5,"created_at":"2026-11-29T08:00:00Z"}]})");
  CHECK(m && m->type == ChatSocketEvent::Type::Message && m->message);
  CHECK_EQ(m->message->content, std::string("yo"));
  CHECK_EQ(*m->message->clientId, std::string("c9"));
  CHECK_EQ(m->message->attachments.size(), static_cast<std::size_t>(2));
  CHECK(!m->message->attachments[0].isImage);
  CHECK_EQ(m->message->attachments[0].mimeType, std::string(""));
  CHECK(m->message->attachments[1].isImage);
  CHECK_EQ(*m->message->attachments[1].size, 5);
  auto none = ChatSocketCodec::decode(
      R"({"type":"message","uid":"m1","sender_uid":"a","receiver_uid":"b","content":"yo","created_at":"2026-11-29T08:00:00Z","attachments":null})");
  CHECK(none && none->message && none->message->attachments.empty() && !none->message->clientId);
}

DP_TEST(chat_decode_drops_bad_frames) {
  CHECK(!ChatSocketCodec::decode("not json"));
  CHECK(!ChatSocketCodec::decode("[]"));
  CHECK(!ChatSocketCodec::decode(R"({"type":5})"));
  CHECK(!ChatSocketCodec::decode(R"({"type":"unknown"})"));
  CHECK(!ChatSocketCodec::decode(R"({"type":"ready"})"));
  CHECK(!ChatSocketCodec::decode(R"({"type":"message","uid":"m","sender_uid":"a","receiver_uid":"b","content":"c","created_at":"nope"})"));
  CHECK(!ChatSocketCodec::decode(
      R"({"type":"message","uid":"m","sender_uid":"a","receiver_uid":"b","content":"c","created_at":"2026-11-29T08:00:00Z","attachments":[{"url":"u"}]})"));
}

DP_TEST(chat_encode_then_decode_round_trip_of_server_shapes) {
  // The client frames are valid JSON that nlohmann reads back to the same values.
  const std::string frame = ChatSocketCodec::encodeSend("r/1", "line1\nline2 \"x\"", {"a"}, "cid");
  Json j = Json::parse(frame);
  CHECK_EQ(j["type"].get<std::string>(), std::string("send"));
  CHECK_EQ(j["receiver_uid"].get<std::string>(), std::string("r/1"));
  CHECK_EQ(j["content"].get<std::string>(), std::string("line1\nline2 \"x\""));
  CHECK_EQ(j["attachment_uids"].size(), static_cast<std::size_t>(1));
  CHECK_EQ(j["client_id"].get<std::string>(), std::string("cid"));
}
37.0ms