Skip to content
Repos

mologram

C++ 1.8 KB41 lines Opens in site

Directory Raw Download

// Model JSON round trips (SPEC D-14: int64 ids as strings)
#include "tttest.hpp"
#include "mologram/Dialog.hpp"
#include "mologram/Message.hpp"

using namespace mologram;

TEST_CASE("peer key round trip and ordering", "[models][unit]") {
  Peer p{PeerKind::Channel, 9007199254740993LL};  // above 2^53: must survive
  CHECK_EQ(p.key(), std::string("channel:9007199254740993"));
  CHECK(Peer::fromKey(p.key()) == p);
  CHECK(Peer::fromKey("garbage").id == 0);
  CHECK(Peer::fromJson(p.toJson()) == p);
}

TEST_CASE("message round trip keeps ids above 2^53, media, poll, reactions, buttons", "[models][unit]") {
  Message m;
  m.id = 9007199254740993LL; m.peer = {PeerKind::Chat, 5}; m.text = "h\xC3\xA9llo \xF0\x9F\x91\x8D"; m.outgoing = true; m.edited = true;
  m.media.kind = MediaKind::Voice; m.media.duration = 7; m.media.size = 5000000000LL;
  m.hasPoll = true; m.poll.question = "q"; m.poll.options.push_back({"a", "0", 3, true, false});
  m.reactions.push_back({"", 4611686018427387904LL, 2, true});
  m.buttons.push_back({{"ok", "d", ""}});
  Message r = Message::fromJson(m.toJson());
  CHECK_EQ(r.id, m.id);
  CHECK_EQ(r.text, m.text);
  CHECK(r.media.kind == MediaKind::Voice);
  CHECK_EQ(r.media.size, m.media.size);
  REQUIRE(r.hasPoll);
  CHECK_EQ(r.poll.options[0].votes, 3);
  REQUIRE(r.reactions.size() == 1);
  CHECK(r.reactions[0].isCustom());
  CHECK_EQ(r.reactions[0].customEmojiId, 4611686018427387904LL);
  REQUIRE(r.buttons.size() == 1);
  CHECK_EQ(r.buttons[0][0].text, std::string("ok"));
}

TEST_CASE("dialog round trip", "[models][unit]") {
  Dialog d; d.peer = {PeerKind::User, 7}; d.name = "x"; d.unread = 3; d.pinned = true; d.folders = {1, 2};
  Dialog r = Dialog::fromJson(d.toJson());
  CHECK(r.peer == d.peer); CHECK_EQ(r.unread, 3); CHECK(r.pinned); CHECK_EQ((int)r.folders.size(), 2);
}
34.7ms