Skip to content
Repos

mologram

C++ 13.5 KB333 lines Opens in site

Directory Raw Download

// M6 message dialogs against the fake service: forward, search, attach, poll, schedule, reactions, link and formatting, bot commands.
// Every dialog is also opened at 40x15, 80x24 and 160x50 (FR-70). SPEC FR-20, FR-23, FR-24, FR-30, FR-60..66.
#include <fstream>

#include "rig.hpp"
#include "mologram/ui/dialogs/ChatDialogs.hpp"

using namespace uitest;

namespace {

/// Opens a dialog at the three standard sizes and checks it exists, fits the terminal and shows `title`.
void atAllSizes(const std::function<void(Rig&)>& open, const std::string& appName, const std::string& title) {
  for (auto sz : {std::make_pair(40, 15), std::make_pair(80, 24), std::make_pair(160, 50)}) {
    Rig r(sz.first, sz.second);
    r.openAlice();
    open(r);
    r.settle();
    ttgui::Window* w = r.window(appName);
    REQUIRE(w != nullptr);
    CHECK(w->bounds.w <= sz.first);
    CHECK(w->bounds.h <= sz.second);
    CHECK(w->bounds.x >= 0);
    CHECK(has(r.screen(), title));
  }
}

}  // namespace

TEST_CASE("every message dialog opens and fits at 40x15, 80x24 and 160x50", "[ui][dialogs][integration]") {
  atAllSizes([](Rig& r) { ForwardDialog::open(r.app.context(), {PeerKind::User, 100}, {1001}); }, "ForwardDialog", "Forward to");
  atAllSizes([](Rig& r) { SearchDialog::open(r.app.context(), nullptr); }, "SearchDialog", "Search");
  atAllSizes([](Rig& r) { AttachDialog::open(r.app.context()); }, "AttachDialog", "Attach file");
  atAllSizes([](Rig& r) { PollDialog::open(r.app.context()); }, "PollDialog", "Create poll");
  atAllSizes([](Rig& r) { ScheduleDialog::open(r.app.context()); }, "ScheduleDialog", "Schedule message");
  atAllSizes([](Rig& r) { ReactionPicker::open(r.app.context(), 1001); }, "ReactionPicker", "Reaction");
  atAllSizes([](Rig& r) { LinkDialog::open(r.app.context()); }, "LinkDialog", "Insert link");
  atAllSizes([](Rig& r) { CommandsDialog::open(r.app.context()); }, "CommandsDialog", "Bot commands");
}

TEST_CASE("forward dialog filters the chats and forwards the chosen messages", "[ui][dialogs][integration]") {
  Rig r;
  r.openAlice();
  long long mid = r.session().target();
  REQUIRE(mid != 0);
  ForwardDialog::open(r.app.context(), {PeerKind::User, 100}, {mid});
  ttgui::Window* w = r.window("ForwardDialog");
  REQUIRE(w != nullptr);
  r.type(r.edit(w), "bob");
  auto lists = r.widgets<ttgui::ListBox>(w);
  REQUIRE(lists.size() == 1);
  REQUIRE(lists[0]->items.size() == 1);
  CHECK_EQ(lists[0]->items[0], std::string("Bob"));
  lists[0]->onActivated.emit(0);
  r.settle();
  CHECK(!r.hasWindow("ForwardDialog"));
  CHECK_EQ(r.svc.history({PeerKind::User, 101}).back().forwardFrom, std::string("Alice"));
  auto all = ForwardDialog::filter(r.app.controller().dialogs(), "");
  CHECK_EQ(all.size(), r.app.controller().dialogs().size());
}

TEST_CASE("global search finds chats and messages and opens a result; scoped search stays in one chat", "[ui][dialogs][integration]") {
  Rig r;
  SearchDialog::open(r.app.context(), nullptr);
  ttgui::Window* w = r.window("SearchDialog");
  REQUIRE(w != nullptr);
  auto* list = r.widgets<ttgui::ListBox>(w)[0];
  r.type(r.edit(w), "Bob");
  bool chat = false, msg = false;
  for (auto& it : list->items) { if (it == "[chat] Bob") chat = true; if (it.rfind("[msg]", 0) == 0) msg = true; }
  CHECK(chat);
  CHECK(msg);  // messages in Bob's chat contain the name
  int firstMsg = -1;
  for (size_t i = 0; i < list->items.size(); i++) if (list->items[i].rfind("[msg]", 0) == 0) { firstMsg = (int)i; break; }
  REQUIRE(firstMsg >= 0);
  list->onActivated.emit(firstMsg);
  r.settle();
  CHECK(!r.hasWindow("SearchDialog"));
  CHECK(r.app.controller().current().valid());

  Rig s;
  Peer alice{PeerKind::User, 100};
  s.openAlice();
  SearchDialog::open(s.app.context(), &alice);
  ttgui::Window* sw = s.window("SearchDialog");
  REQUIRE(sw != nullptr);
  s.type(s.edit(sw), "Message 12 in");
  auto* sl = s.widgets<ttgui::ListBox>(sw)[0];
  REQUIRE(sl->items.size() == 1);
  CHECK(has(sl->items[0], "Message 12 in Alice"));
  CHECK_EQ(SearchDialog::filterId(0), std::string(""));
  CHECK_EQ(SearchDialog::filterId(4), std::string("link"));
  CHECK_EQ(SearchDialog::filters().size(), (size_t)8);
}

TEST_CASE("attach dialog lists directories, sends the file with its caption, and a pasted path attaches it", "[ui][dialogs][integration]") {
  Rig r;
  r.openAlice();
  std::string file = r.tmp.path + "/hello.txt";
  { std::ofstream f(file); f << "hello world"; }
  auto entries = AttachDialog::listDir(r.tmp.path);
  REQUIRE(!entries.empty());
  CHECK_EQ(entries[0], std::string("../"));
  CHECK(std::find(entries.begin(), entries.end(), "hello.txt") != entries.end());
  std::string norm;
  CHECK(AttachDialog::looksLikePath("  '" + file + "'  ", &norm));
  CHECK_EQ(norm, file);
  CHECK(AttachDialog::looksLikePath("file://" + file, nullptr));
  CHECK(!AttachDialog::looksLikePath(r.tmp.path + "/missing.txt"));
  CHECK(!AttachDialog::looksLikePath("two\nlines"));

  AttachDialog::open(r.app.context(), file);
  ttgui::Window* w = r.window("AttachDialog");
  REQUIRE(w != nullptr);
  r.type(r.edit(w, 1), "my caption");
  r.click(w, "Attach");
  CHECK(!r.hasWindow("AttachDialog"));
  const Message& m = r.svc.history({PeerKind::User, 100}).back();
  CHECK_EQ(m.media.fileName, std::string("hello.txt"));
  CHECK_EQ(m.text, std::string("my caption"));

  // bracketed paste of a path while the composer has focus opens the dialog with that file
  r.app.app().setFocused(&r.composer());
  ttgui::Event e = ttgui::Event::makePaste(file);
  r.app.app().dispatch(e);
  r.settle();
  CHECK(r.hasWindow("SendMediaDialog"));  // a pasted path goes through the media send dialog (several files, album, as document; D-19)
  CHECK(r.composer().editor->getText().empty());  // the path was not typed into the message
}

TEST_CASE("attach with a missing file shows the reason and sends nothing", "[ui][dialogs][integration]") {
  Rig r;
  r.openAlice();
  size_t before = r.svc.history({PeerKind::User, 100}).size();
  AttachDialog::open(r.app.context(), r.tmp.path + "/nope.bin");
  ttgui::Window* w = r.window("AttachDialog");
  REQUIRE(w != nullptr);
  r.click(w, "Attach");
  CHECK(r.hasWindow("AttachDialog"));
  CHECK(has(r.screen(), "Not a file"));
  CHECK_EQ(r.svc.history({PeerKind::User, 100}).size(), before);
}

TEST_CASE("poll dialog creates a poll with the typed options", "[ui][dialogs][integration]") {
  Rig r;
  r.openAlice();
  PollDialog::open(r.app.context());
  ttgui::Window* w = r.window("PollDialog");
  REQUIRE(w != nullptr);
  auto edits = r.widgets<ttgui::EditBox>(w);
  REQUIRE(edits.size() >= 12);
  r.type(edits[0], "Lunch?");
  r.type(edits[1], "Pizza");
  r.type(edits[2], "Sushi");
  r.click(w, "Send");
  const Message& m = r.svc.history({PeerKind::User, 100}).back();
  REQUIRE(m.hasPoll);
  CHECK_EQ(m.poll.question, std::string("Lunch?"));
  REQUIRE(m.poll.options.size() == 2);
  CHECK_EQ(m.poll.options[1].text, std::string("Sushi"));
  bool onScreen = false;
  for (auto& t : r.app.shell().view->msgs) if (t.pollQuestion == "Lunch?") onScreen = true;
  CHECK(onScreen);
}

TEST_CASE("voting in a poll through the message view reaches the service", "[ui][dialogs][integration]") {
  Rig r;
  r.openAlice();
  js::Object p;
  p["peer"] = js::Value("user:100"); p["question"] = js::Value("Q?"); p["options"] = js::Value(js::Array{js::Value("a"), js::Value("b")});
  r.app.context().client.request("poll", "messages.send_poll", p, [](const RpcResult&) {});
  r.settle();
  Message poll = r.svc.history({PeerKind::User, 100}).back();
  REQUIRE(poll.hasPoll);
  r.open({PeerKind::User, 101});
  r.openAlice();
  r.app.shell().view->onVote.emit(poll.id, std::vector<int>{1});
  r.settle();
  CHECK_EQ(r.svc.history({PeerKind::User, 100}).back().poll.options[1].votes, 1);
}

TEST_CASE("schedule dialog parses the time and keeps the message out of the history", "[ui][dialogs][integration]") {
  std::int64_t t = 0;
  CHECK(ScheduleDialog::parseWhen("2099-01-02 03:04", t));
  CHECK(t > 4000000000LL);
  CHECK(!ScheduleDialog::parseWhen("tomorrow", t));
  CHECK(!ScheduleDialog::parseWhen("2099-13-02 03:04", t));
  CHECK(!ScheduleDialog::parseWhen("2099-01-02 25:04", t));
  CHECK(!ScheduleDialog::parseWhen("2099-01-02 03:04 extra", t));

  Rig r;
  r.openAlice();
  size_t before = r.svc.history({PeerKind::User, 100}).size();
  r.composer().editor->setText("see you later");
  ScheduleDialog::open(r.app.context());
  ttgui::Window* w = r.window("ScheduleDialog");
  REQUIRE(w != nullptr);
  r.type(r.edit(w), "not a time");
  r.click(w, "Send");
  CHECK(r.hasWindow("ScheduleDialog"));  // rejected, stays open with the reason
  CHECK(has(r.screen(), "YYYY-MM-DD"));
  r.type(r.edit(w), "2099-01-02 03:04");
  r.click(w, "Send");
  CHECK(!r.hasWindow("ScheduleDialog"));
  CHECK_EQ(r.svc.history({PeerKind::User, 100}).size(), before);
  CHECK(r.composer().editor->getText().empty());
  js::Object q;
  q["peer"] = js::Value("user:100");
  bool listed = false;
  r.app.context().client.request("scheduled", "messages.scheduled", q, [&](const RpcResult& res) { listed = js::getArr(res.value, "messages")->size() == 1; });
  r.settle();
  CHECK(listed);
}

TEST_CASE("reaction picker: a free reaction works, custom emoji need Premium (three ways)", "[ui][dialogs][premium][integration]") {
  Rig r;
  r.openAlice();
  long long mid = r.session().target();
  ReactionPicker::open(r.app.context(), mid);
  r.settle();
  ttgui::Window* w = r.window("ReactionPicker");
  REQUIRE(w != nullptr);
  auto* list = r.widgets<ttgui::ListBox>(w)[0];
  REQUIRE(list->items.size() > 9);
  CHECK(has(list->items[8], "custom"));
  CHECK(has(list->items[8], "\xF0\x9F\x94\x92") || has(list->items[8], "[P]"));  // lock mark without Premium
  list->onActivated.emit(0);
  r.settle();
  const Message* shown = nullptr;
  for (auto& m : r.svc.history({PeerKind::User, 100})) if (m.id == mid) shown = &m;
  REQUIRE(shown != nullptr);
  REQUIRE(shown->reactions.size() == 1);
  CHECK_EQ(shown->reactions[0].emoji, std::string("\xF0\x9F\x91\x8D"));

  // NotPremium: the custom entry explains instead of sending
  r.svc.setPremium(mologram::PremiumState::NotPremium);
  r.settle();
  ReactionPicker::open(r.app.context(), mid);
  r.settle();
  ttgui::Window* w2 = r.window("ReactionPicker");
  REQUIRE(w2 != nullptr);
  r.widgets<ttgui::ListBox>(w2)[0]->onActivated.emit(8);
  r.settle();
  CHECK(r.hasWindow("PremiumPanel"));

  // Premium: allowed
  r.svc.setPremium(mologram::PremiumState::Premium);
  r.settle();
  ReactionPicker::open(r.app.context(), mid);
  r.settle();
  ttgui::Window* w3 = r.window("ReactionPicker");
  REQUIRE(w3 != nullptr);
  auto* l3 = r.widgets<ttgui::ListBox>(w3)[0];
  CHECK(!has(l3->items[8], "[P]"));
  l3->onActivated.emit(8);
  r.settle();
  bool custom = false;
  for (auto& m : r.svc.history({PeerKind::User, 100})) if (m.id == mid) for (auto& rc : m.reactions) if (rc.isCustom()) custom = true;
  CHECK(custom);

  // Premium lost midway: the server refuses, the state flips, nothing else is lost
  r.composer().editor->setText("my draft");
  r.svc.revokePremiumSilently();
  ReactionPicker::open(r.app.context(), mid);
  r.settle();
  ttgui::Window* w4 = r.window("ReactionPicker");
  REQUIRE(w4 != nullptr);
  r.widgets<ttgui::ListBox>(w4)[0]->onActivated.emit(8);
  r.settle();
  CHECK(!r.app.premium().has());
  CHECK_EQ(r.composer().editor->getText(), std::string("my draft"));
}

TEST_CASE("formatting actions insert tags with the caret between them and the message is sent as formatted", "[ui][dialogs][integration]") {
  Rig r;
  r.openAlice();
  r.action("fmt_bold");
  CHECK_EQ(r.composer().editor->getText(), std::string("<b></b>"));
  CHECK_EQ(r.composer().editor->cx, 3);
  r.session().insertText("hi");
  CHECK_EQ(r.composer().editor->getText(), std::string("<b>hi</b>"));
  r.composer().doSend();
  r.settle();
  CHECK_EQ(r.svc.history({PeerKind::User, 100}).back().text, std::string("<b>hi</b>"));
  for (const char* id : {"fmt_italic", "fmt_underline", "fmt_code", "fmt_spoiler"}) {
    r.composer().editor->setText("");
    r.action(id);
    CHECK(has(r.composer().editor->getText(), "</"));
  }
  // text without formatting is sent untouched, even when it contains angle brackets
  r.composer().editor->setText("a < b");
  r.composer().doSend();
  r.settle();
  CHECK_EQ(r.svc.history({PeerKind::User, 100}).back().text, std::string("a < b"));
  // formatted text escapes characters that are not our tags
  r.composer().editor->setText("");
  r.action("fmt_bold");
  r.session().insertText("1 < 2 & 3");
  r.composer().doSend();
  r.settle();
  CHECK_EQ(r.svc.history({PeerKind::User, 100}).back().text, std::string("<b>1 &lt; 2 &amp; 3</b>"));
}

TEST_CASE("link dialog inserts an anchor tag", "[ui][dialogs][integration]") {
  Rig r;
  r.openAlice();
  r.action("fmt_link");
  ttgui::Window* w = r.window("LinkDialog");
  REQUIRE(w != nullptr);
  r.type(r.edit(w, 0), "docs");
  r.type(r.edit(w, 1), "https://example.org");
  r.click(w, "OK");
  CHECK_EQ(r.composer().editor->getText(), std::string("<a href=\"https://example.org\">docs</a>"));
}

TEST_CASE("bot commands dialog lists the commands of a bot chat and inserts the chosen one", "[ui][dialogs][integration]") {
  Rig r;
  r.open({PeerKind::User, 103});
  REQUIRE(!r.session().botCommands().empty());
  r.app.shell().header->onAction.emit("Commands");
  r.settle();
  ttgui::Window* w = r.window("CommandsDialog");
  REQUIRE(w != nullptr);
  auto* list = r.widgets<ttgui::ListBox>(w)[0];
  REQUIRE(!list->items.empty());
  CHECK(has(list->items[0], "/start"));
  list->onActivated.emit(0);
  r.settle();
  CHECK_EQ(r.composer().editor->getText(), std::string("/start "));
  // a person chat has no commands and no Commands header button
  r.openAlice();
  CHECK(r.session().botCommands().empty());
}
36.2ms