Skip to content
Repos

mologram

C++ 6.2 KB137 lines Opens in site

Directory Raw Download

// Composer suggestions (FR-23): @ people, # hashtags, / bot commands. The word logic is tested directly; the popup through the app.
#include "rig.hpp"
#include "mologram/ui/ComposerSuggest.hpp"
#include "mologram/ui/Suggest.hpp"

using namespace uitest;

TEST_CASE("suggestion word: trigger at the start or after a space, caret inside the word", "[ui][suggest][unit]") {
  auto t = Suggest::tokenAt("hello @al", 9);
  REQUIRE(t.has_value());
  CHECK_EQ(t->trigger, '@');
  CHECK_EQ(t->prefix, std::string("al"));
  CHECK_EQ(t->start, (size_t)6);
  CHECK(Suggest::tokenAt("@", 1).has_value());
  CHECK_EQ(Suggest::tokenAt("#tag", 4)->prefix, std::string("tag"));
  CHECK_EQ(Suggest::tokenAt("see @al and more", 7)->prefix, std::string("al"));  // caret in the middle of a longer line
  CHECK(!Suggest::tokenAt("mail me at a@b.org", 18).has_value());               // an e-mail address is not a mention
  CHECK(!Suggest::tokenAt("http://x.org/#frag", 18).has_value());                 // nor is a URL fragment
  CHECK(Suggest::tokenAt("/start", 6).has_value());
  CHECK(!Suggest::tokenAt("see /start", 10).has_value());                         // commands only open a line
  CHECK(!Suggest::tokenAt("plain words", 11).has_value());
  CHECK(!Suggest::tokenAt("@al ", 4).has_value());                                // the caret left the word
  CHECK_EQ(Suggest::tokenAt("@\xC3\xA9l", 4)->prefix, std::string("\xC3\xA9l"));    // letters of any script count
}

TEST_CASE("suggestions: people with a username, hashtags by use, commands by prefix", "[ui][suggest][unit]") {
  PeerInfo a; a.name = "Alice"; a.username = "alice";
  PeerInfo b; b.name = "Bob"; b.username = "bob";
  PeerInfo n; n.name = "No Handle";
  CHECK_EQ(Suggest::mentions({a, b, n}, "").size(), (size_t)2);          // no username: nothing to insert
  CHECK_EQ(Suggest::mentions({a, b, n}, "AL").size(), (size_t)1);
  CHECK_EQ(Suggest::mentions({a, b, n}, "AL")[0].insert, std::string("@alice"));
  CHECK_EQ(Suggest::mentions({a, b, n}, "ob")[0].insert, std::string("@bob"));  // a name match works too
  auto tags = Suggest::hashtags({"release #news #release", "#release again", "#release", "#news"}, "");
  REQUIRE(tags.size() == 2);
  CHECK_EQ(tags[0].insert, std::string("#release"));                     // most used first
  CHECK_EQ(Suggest::hashtags({"a#b not a tag"}, "").size(), (size_t)0);   // a # inside a word is not a hashtag
  auto cmds = Suggest::commands({{"start", "Begin"}, {"/stop", "End"}}, "st");
  CHECK_EQ(cmds.size(), (size_t)2);
  CHECK_EQ(cmds[1].insert, std::string("/stop"));
  CHECK_EQ(Suggest::commands({{"start", "Begin"}}, "x").size(), (size_t)0);
}

TEST_CASE("accepting a suggestion replaces the word and leaves exactly one space", "[ui][suggest][unit]") {
  std::string line = "hi @al there";
  auto t = Suggest::tokenAt(line, 6);
  REQUIRE(t.has_value());
  size_t caret = 0;
  std::string out = Suggest::apply(line, *t, 6, {"Alice  @alice", "@alice"}, caret);
  CHECK_EQ(out, std::string("hi @alice there"));
  CHECK_EQ(caret, (size_t)10);
  std::string line2 = "@a";
  auto t2 = Suggest::tokenAt(line2, 2);
  CHECK_EQ(Suggest::apply(line2, *t2, 2, {"x", "@alice"}, caret), std::string("@alice "));
}

TEST_CASE("typing @ in a group offers its people; Enter inserts and does not send", "[ui][suggest][integration]") {
  Rig r;
  r.open({PeerKind::Chat, 200});  // Family
  r.app.app().setFocused(&r.composer());
  r.typeInto(r.composer(), "hi @a");
  r.settle();
  REQUIRE(r.session().suggest != nullptr);
  REQUIRE(r.session().suggest->visible());
  CHECK(r.hasWindow("SuggestPopup"));
  bool alice = false;
  for (auto& s : r.session().suggest->items()) if (s.insert == "@alice") alice = true;
  CHECK(alice);
  int sends = r.rec.count("messages.send");
  r.press("Enter");
  CHECK(!r.session().suggest->visible());
  CHECK_EQ(r.composer().editor->getText(), std::string("hi @alice "));
  CHECK_EQ(r.rec.count("messages.send"), sends);  // Enter chose the suggestion, it did not send the message
  r.press("Enter");
  CHECK_EQ(r.rec.count("messages.send"), sends + 1);  // with no popup Enter sends as usual
}

TEST_CASE("Escape closes the popup and keeps the text; Down moves the choice", "[ui][suggest][integration]") {
  Rig r;
  r.open({PeerKind::Chat, 200});
  r.app.app().setFocused(&r.composer());
  r.typeInto(r.composer(), "@");
  r.settle();
  REQUIRE(r.session().suggest->visible());
  CHECK_EQ(r.session().suggest->selected(), 0);
  r.press("Down");
  CHECK_EQ(r.session().suggest->selected(), 1);
  r.press("Escape");
  CHECK(!r.session().suggest->visible());
  CHECK(!r.hasWindow("SuggestPopup"));
  CHECK_EQ(r.composer().editor->getText(), std::string("@"));
}

TEST_CASE("typing # offers hashtags seen in the chat, most used first", "[ui][suggest][integration]") {
  Rig r;
  r.svc.simulateIncoming({PeerKind::Chat, 200}, "#release notes");
  r.svc.simulateIncoming({PeerKind::Chat, 200}, "again #release and #news");
  r.open({PeerKind::Chat, 200});
  r.app.app().setFocused(&r.composer());
  r.typeInto(r.composer(), "#re");
  r.settle();
  REQUIRE(r.session().suggest->visible());
  CHECK_EQ(r.session().suggest->items()[0].insert, std::string("#release"));
  r.typeInto(r.composer(), "x");  // "#rex": nothing matches any more
  CHECK(!r.session().suggest->visible());
}

TEST_CASE("typing / in a bot chat offers its commands; elsewhere nothing opens", "[ui][suggest][integration]") {
  Rig r;
  r.open({PeerKind::User, 103});  // BuildBot
  r.app.app().setFocused(&r.composer());
  r.typeInto(r.composer(), "/");
  r.settle();
  CHECK_EQ(r.session().suggest->visible(), !r.session().botCommands().empty());
  r.press("Escape");
  r.composer().editor->setText("");
  r.open({PeerKind::User, 100});  // a person: no commands
  r.app.app().setFocused(&r.composer());
  r.typeInto(r.composer(), "/");
  r.settle();
  CHECK(!r.session().suggest->visible());
}

TEST_CASE("the popup stays inside the work area at 40x15", "[ui][suggest][integration]") {
  Rig r(40, 15);
  r.open({PeerKind::Chat, 200});
  r.app.app().setFocused(&r.composer());
  r.typeInto(r.composer(), "@");
  r.settle();
  ttgui::Window* w = r.window("SuggestPopup");
  REQUIRE(w != nullptr);
  ttgui::Rect area = r.app.app().wm.workArea;
  CHECK(w->bounds.x >= area.x);
  CHECK(w->bounds.y >= area.y);
  CHECK(w->bounds.right() <= area.right());
  CHECK(w->bounds.bottom() <= area.bottom());
}
47.0ms