Skip to content
Repos

mologram

C++ 14.9 KB356 lines Opens in site

Directory Raw Download

// Calls, secret chats, Business, gifts, Passport and payments in the UI (SPEC FR-52, FR-60 to FR-66, PG-12, PG-15), against the fake
// service. Premium controls are tested three ways: with Premium, without it, and with Premium lost midway.
#include "rig.hpp"
#include "mologram/ui/CallTracker.hpp"
#include "mologram/ui/ExtrasModule.hpp"
#include "mologram/ui/SecretChats.hpp"
#include "mologram/ui/dialogs/CallPanel.hpp"
#include "mologram/ui/dialogs/ExtrasPanels.hpp"

using namespace uitest;

namespace {

std::string lastCallState(Rig& r) {
  std::string s;
  RpcResult out;
  r.svc.call("calls.list", js::Value(js::Object{}), [&](const RpcResult& x) { out = x; });
  if (const js::Array* a = js::getArr(out.value, "calls")) if (!a->empty()) s = js::getStr(a->back(), "state");
  return s;
}

int quickReplyCount(Rig& r) {
  RpcResult out;
  r.svc.call("business.quick_replies", js::Value(js::Object{}), [&](const RpcResult& x) { out = x; });
  const js::Array* a = js::getArr(out.value, "replies");
  return a ? (int)a->size() : -1;
}

bool inside(ttgui::Window* w, const ttgui::Rect& area) {
  return w && w->bounds.x >= area.x && w->bounds.y >= area.y && w->bounds.right() <= area.right() && w->bounds.bottom() <= area.bottom();
}

}  // namespace

TEST_CASE("call panel text: buttons, status, fingerprint and the honest audio notice", "[ui][calls][unit]") {
  CallModel out;
  out.outgoing = true; out.state = "requesting";
  CHECK_EQ(CallPanel::buttonsFor(out), std::vector<std::string>{"Hang up"});
  CHECK(has(CallPanel::statusText(out), "calling"));
  out.state = "ringing";
  CHECK(has(CallPanel::statusText(out), "ringing"));
  CallModel in;
  in.outgoing = false; in.state = "ringing";
  CHECK_EQ(CallPanel::buttonsFor(in), (std::vector<std::string>{"Accept", "Decline"}));
  CHECK(has(CallPanel::statusText(in), "Incoming call"));
  in.state = "active"; in.fingerprint = 0x1122334455667788LL;
  CHECK(has(CallPanel::fingerprintText(in), "1122 3344 5566 7788"));
  CHECK_EQ(CallPanel::buttonsFor(in), std::vector<std::string>{"Hang up"});
  CallModel fresh;
  fresh.state = "exchanging";
  CHECK(has(CallPanel::fingerprintText(fresh), "not exchanged yet"));
  CHECK(has(CallPanel::notice(in), "Audio is not available"));
  in.media = "audio";
  CHECK(CallPanel::notice(in).empty());
  CallModel done;
  done.state = "ended"; done.endReason = "hangup"; done.duration = 12; done.needRating = true;
  CHECK(has(CallPanel::statusText(done), "ended (hangup), 12 s"));
  auto b = CallPanel::buttonsFor(done);
  CHECK_EQ((int)b.size(), 6);
  CHECK_EQ(b.back(), std::string("Close"));
}

TEST_CASE("outgoing call from the header button and F7: ringing, active with a fingerprint, hang up", "[ui][calls][integration]") {
  Rig r;
  r.openAlice();
  auto actions = r.app.shell().header->actions;
  CHECK(std::find(actions.begin(), actions.end(), "Call") != actions.end());
  r.press("F7");
  REQUIRE(r.hasWindow("CallPanel"));
  CHECK(has(r.screen(), "calling"));
  CHECK(has(r.screen(), "Audio is not available"));  // honest: signaling only
  CHECK(r.button(r.window("CallPanel"), "Hang up") != nullptr);
  CHECK_EQ(lastCallState(r), std::string("requesting"));
  r.click(r.window("CallPanel"), "Hang up");
  CHECK_EQ(lastCallState(r), std::string("ended"));
  REQUIRE(r.hasWindow("CallPanel"));
  CHECK(r.button(r.window("CallPanel"), "Close") != nullptr);
  CHECK(has(r.screen(), "Call ended"));
  r.click(r.window("CallPanel"), "Close");
  CHECK(!r.hasWindow("CallPanel"));
}

TEST_CASE("incoming call: panel with Accept and Decline, accept shows the key fingerprint", "[ui][calls][integration]") {
  Rig r;
  r.svc.simulateIncomingCall({PeerKind::User, 100});
  r.settle();
  REQUIRE(r.hasWindow("CallPanel"));
  CHECK(has(r.screen(), "Incoming call"));
  ttgui::Window* w = r.window("CallPanel");
  CHECK(r.button(w, "Accept") != nullptr);
  CHECK(r.button(w, "Decline") != nullptr);
  r.click(w, "Accept");
  CHECK_EQ(lastCallState(r), std::string("active"));
  CHECK(has(r.screen(), "1122 3344 5566 7788"));
  CHECK(has(r.screen(), "keys verified"));
  r.click(r.window("CallPanel"), "Hang up");
  CHECK_EQ(lastCallState(r), std::string("ended"));
}

TEST_CASE("declining an incoming call ends it; a second incoming call while one runs is refused as busy", "[ui][calls][integration]") {
  Rig r;
  r.svc.simulateIncomingCall({PeerKind::User, 100});
  r.settle();
  r.click(r.window("CallPanel"), "Decline");
  CHECK_EQ(lastCallState(r), std::string("ended"));
  r.click(r.window("CallPanel"), "Close");
  r.svc.simulateIncomingCall({PeerKind::User, 101});
  r.settle();
  REQUIRE(r.hasWindow("CallPanel"));
  r.click(r.window("CallPanel"), "Accept");
  r.svc.simulateIncomingCall({PeerKind::User, 102});
  r.settle();
  CHECK_EQ(lastCallState(r), std::string("ended"));  // the third call was refused, the running one is untouched
  REQUIRE(r.app.extras() != nullptr);
  REQUIRE(r.app.extras()->calls().current() != nullptr);
  CHECK_EQ(r.app.extras()->calls().current()->user.id, (std::int64_t)101);
  CHECK_EQ(r.app.extras()->calls().current()->state, std::string("active"));
}

TEST_CASE("the Call button only appears for people, not channels or bots", "[ui][calls][integration]") {
  Rig r;
  r.open({PeerKind::Channel, 300});
  auto a = r.app.shell().header->actions;
  CHECK(std::find(a.begin(), a.end(), "Call") == a.end());
  r.open({PeerKind::User, 103});  // BuildBot
  a = r.app.shell().header->actions;
  CHECK(std::find(a.begin(), a.end(), "Call") == a.end());
  r.openAlice();
  a = r.app.shell().header->actions;
  CHECK(std::find(a.begin(), a.end(), "Call") != a.end());
}

TEST_CASE("secret chats: create, list, converse, timer, fingerprint, discard", "[ui][secret][integration]") {
  Rig r;
  r.action("secret_chat");
  REQUIRE(r.hasWindow("SecretChatsDialog"));
  r.app.extras()->secrets().startWith({PeerKind::User, 100});
  r.settle();
  ttgui::Window* list = r.window("SecretChatsDialog");
  REQUIRE(list != nullptr);
  auto tables = r.widgets<ttgui::Table>(list);
  REQUIRE(!tables.empty());
  REQUIRE(tables[0]->rows.size() == 1);
  CHECK(has(tables[0]->rows[0][1], "ready"));
  tables[0]->selected = 0;
  r.click(list, "Open");
  ttgui::Window* conv = r.window("SecretChatWindow");
  REQUIRE(conv != nullptr);
  CHECK(has(r.screen(), "Key fingerprint: fake-fingerprint"));
  auto edits = r.widgets<ttgui::EditBox>(conv);
  REQUIRE(!edits.empty());
  r.type(edits[0], "psst");
  r.click(conv, "Send");
  auto lists = r.widgets<ttgui::ListBox>(conv);
  REQUIRE(!lists.empty());
  REQUIRE(lists[0]->items.size() == 1);
  CHECK(has(lists[0]->items[0], "You: psst"));
  // a message from the other side arrives as an event
  js::Object m; m["chat"] = js::Value(std::atoi(tables[0]->rows[0][2].c_str())); m["text"] = js::Value("hello back"); m["out"] = js::Value(false); m["ttl"] = js::Value(5);
  js::Object d; d["message"] = js::Value(std::move(m));
  r.app.extras()->secrets().onEvent(Event{"secret.message", js::Value(std::move(d))});
  REQUIRE(lists[0]->items.size() == 2);
  CHECK(has(lists[0]->items[1], "hello back"));
  CHECK(has(lists[0]->items[1], "self-destructs after 5 s"));
  // timer
  auto combos = r.widgets<ttgui::ComboBox>(conv);
  REQUIRE(!combos.empty());
  combos[0]->selected = 2;
  combos[0]->onSelected.emit(2);
  r.settle();
  r.click(list, "Discard");
  RpcResult out;
  r.svc.call("secret.list", js::Value(js::Object{}), [&](const RpcResult& x) { out = x; });
  CHECK_EQ(js::getStr((*js::getArr(out.value, "chats"))[0], "status"), std::string("discarded"));
}

TEST_CASE("secret chats: accepting a request needs the request state, otherwise the reason is shown", "[ui][secret][unit]") {
  CHECK_EQ(SecretChats::statusText("requested"), std::string("wants a secret chat with you"));
  CHECK_EQ(SecretChats::ttlLabels().size(), SecretChats::ttlSeconds().size());
  js::Object m; m["text"] = js::Value("x"); m["ttl"] = js::Value(0);
  CHECK_EQ(SecretChats::lineFor("Bob", js::Value(m)), std::string("Bob: x"));
}

TEST_CASE("Business three ways: without Premium the calm panel opens and nothing is written", "[ui][business][premium][integration]") {
  Rig r;
  CHECK(!r.app.premium().has());
  r.action("business");
  ttgui::Window* w = r.window("BusinessDialog");
  REQUIRE(w != nullptr);
  CHECK(has(r.screen(), "needs Premium"));
  r.type(r.edit(w, 0), "hi");
  r.type(r.edit(w, 1), "Thanks for writing!");
  ttgui::PushButton* add = nullptr;
  for (auto* b : r.widgets<ttgui::PushButton>(w)) if (b->label.rfind("Add", 0) == 0) add = b;
  REQUIRE(add != nullptr);
  CHECK(has(add->label, "\xF0\x9F\x94\x92"));  // lock mark
  add->onPressed.emit();
  r.settle();
  CHECK(r.hasWindow("PremiumPanel"));
  CHECK_EQ(quickReplyCount(r), 0);
}

TEST_CASE("Business with Premium: quick replies, greeting, hours, location, intro, chatbots, links all write through", "[ui][business][premium][integration]") {
  Rig r;
  r.svc.setPremium(PremiumState::Premium);
  r.settle();
  r.action("business");
  ttgui::Window* w = r.window("BusinessDialog");
  REQUIRE(w != nullptr);
  r.type(r.edit(w, 0), "hi");
  r.type(r.edit(w, 1), "Thanks for writing!");
  r.click(w, "Add");
  CHECK_EQ(quickReplyCount(r), 1);
  CHECK(!r.hasWindow("PremiumPanel"));
  auto combos = r.widgets<ttgui::ComboBox>(w);
  REQUIRE(!combos.empty());
  auto showSection = [&](int i) { combos[0]->selected = i; combos[0]->onSelected.emit(i); r.settle(); };
  auto get = [&](const char* key) { RpcResult o; r.svc.call("business.get", js::Value(js::Object{}), [&](const RpcResult& x) { o = x; }); const js::Value* v = o.value.find(key); return v ? *v : js::Value(); };
  showSection(1);  // greeting message uses the quick reply
  r.click(w, "Save");
  CHECK(!get("greeting").isNull());
  CHECK_EQ(js::getInt(get("greeting"), "no_activity_days"), 7);
  showSection(3);  // work hours
  r.type(r.edit(w, 0), "Europe/Amsterdam");
  r.click(w, "Save");
  CHECK_EQ(js::getStr(get("hours"), "timezone_id"), std::string("Europe/Amsterdam"));
  CHECK_EQ((int)js::getArr(get("hours"), "weekly")->size(), 5);
  showSection(4);  // location
  r.type(r.edit(w, 0), "Dam 1, Amsterdam");
  r.click(w, "Save");
  CHECK_EQ(js::getStr(get("location"), "address"), std::string("Dam 1, Amsterdam"));
  showSection(5);  // intro
  r.type(r.edit(w, 0), "Welcome");
  r.type(r.edit(w, 1), "Ask me anything");
  r.click(w, "Save");
  CHECK_EQ(js::getStr(get("intro"), "title"), std::string("Welcome"));
  r.click(w, "Clear");
  CHECK(get("intro").isNull());
  showSection(6);  // chatbots
  r.type(r.edit(w, 0), "helperbot");
  r.click(w, "Connect");
  RpcResult bots;
  r.svc.call("business.bots", js::Value(js::Object{}), [&](const RpcResult& x) { bots = x; });
  CHECK_EQ((int)js::getArr(bots.value, "bots")->size(), 1);
  showSection(7);  // chat links
  r.type(r.edit(w, 0), "Hello from my link");
  r.click(w, "Create");
  RpcResult links;
  r.svc.call("business.chat_links", js::Value(js::Object{}), [&](const RpcResult& x) { links = x; });
  CHECK_EQ((int)js::getArr(links.value, "links")->size(), 1);
}

TEST_CASE("Business: Premium lost midway flips the state, explains, keeps the typed text", "[ui][business][premium][integration]") {
  Rig r;
  r.svc.setPremium(PremiumState::Premium);
  r.settle();
  r.action("business");
  ttgui::Window* w = r.window("BusinessDialog");
  REQUIRE(w != nullptr);
  r.type(r.edit(w, 0), "hi");
  r.type(r.edit(w, 1), "Thanks for writing!");
  r.svc.revokePremiumSilently();  // the server stops honouring Premium while the UI still believes it
  r.click(w, "Add");
  CHECK(!r.app.premium().has());
  CHECK(r.hasWindow("PremiumPanel"));
  CHECK_EQ(r.edit(w, 1)->text, std::string("Thanks for writing!"));  // nothing typed was lost
  CHECK_EQ(quickReplyCount(r), 0);
}

TEST_CASE("Settings has the Business, Gifts and Passport sections", "[ui][business][integration]") {
  Rig r;
  r.action("settings");
  ttgui::Window* w = r.window("SettingsDialog");
  REQUIRE(w != nullptr);
  auto combos = r.widgets<ttgui::ComboBox>(w);
  REQUIRE(!combos.empty());
  CHECK(std::find(combos[0]->items.begin(), combos[0]->items.end(), "Business") != combos[0]->items.end());
  for (int i : {7, 8, 9}) { combos[0]->selected = i; combos[0]->onSelected.emit(i); r.settle(); }
  CHECK(has(r.screen(), "Passport"));
}

TEST_CASE("gifts: browsing is free, sending is a Premium action that shows the price and charges nothing before the confirm", "[ui][gifts][premium][integration]") {
  Rig r;
  r.action("gifts");
  ttgui::Window* w = r.window("GiftsDialog");
  REQUIRE(w != nullptr);
  auto tables = r.widgets<ttgui::Table>(w);
  REQUIRE(!tables.empty());
  REQUIRE(tables[0]->rows.size() == 3);   // the catalogue loads without Premium
  tables[0]->selected = 0;
  ttgui::PushButton* send = nullptr;
  for (auto* b : r.widgets<ttgui::PushButton>(w)) if (b->label.rfind("Send to a person", 0) == 0) send = b;
  REQUIRE(send != nullptr);
  send->onPressed.emit();
  r.settle();
  CHECK(r.hasWindow("PremiumPanel"));     // without Premium: the calm panel, no picker
  CHECK(!r.hasWindow("ContactsPicker"));
}

TEST_CASE("gifts with Premium: pick a person, see the price, Stars are spent only by the explicit button", "[ui][gifts][premium][integration]") {
  Rig r;
  r.svc.setPremium(PremiumState::Premium);
  r.settle();
  js::Object add; add["user"] = js::Value("user:100"); add["name"] = js::Value("Alice");
  r.svc.call("contacts.add", js::Value(add), [](const RpcResult&) {});
  r.action("gifts");
  ttgui::Window* w = r.window("GiftsDialog");
  REQUIRE(w != nullptr);
  r.widgets<ttgui::Table>(w)[0]->selected = 1;
  r.click(w, "Send to a person");
  ttgui::Window* pick = r.window("ContactsPicker");
  REQUIRE(pick != nullptr);
  auto lists = r.widgets<ttgui::ListBox>(pick);
  REQUIRE(!lists.empty());
  REQUIRE(!lists[0]->items.empty());
  lists[0]->onActivated.emit(0);
  r.settle();
  ttgui::Window* confirm = r.window("GiftConfirm");
  REQUIRE(confirm != nullptr);
  CHECK(has(r.screen(), "spends 50 Stars"));
  r.click(confirm, "Cancel");
  CHECK(!r.hasWindow("GiftConfirm"));
}

TEST_CASE("Passport and payments show data read only and report a bad invoice id", "[ui][gifts][integration]") {
  Rig r;
  ttgui::Window* w = PassportPanel::open(r.app.context());
  r.settle();
  REQUIRE(w != nullptr);
  CHECK(has(r.screen(), "No documents stored."));
  r.type(r.edit(w, 0), "user:100");
  r.type(r.edit(w, 1), "12345");
  r.click(w, "Show form");  // the fake answers MESSAGE_ID_INVALID: shown as a notification, nothing charged
  CHECK(!r.app.app().notifications.empty());
}

TEST_CASE("new dialogs fit at 40x15, 80x24 and 160x50", "[ui][calls][business][integration]") {
  for (auto [w, h] : {std::pair<int, int>{40, 15}, {80, 24}, {160, 50}}) {
    Rig r(w, h);
    r.svc.setPremium(PremiumState::Premium);
    r.settle();
    ttgui::Rect area = r.app.app().wm.workArea;
    r.action("business");
    CHECK(inside(r.window("BusinessDialog"), area));
    r.action("gifts");
    CHECK(inside(r.window("GiftsDialog"), area));
    r.action("secret_chat");
    CHECK(inside(r.window("SecretChatsDialog"), area));
    PassportPanel::open(r.app.context());
    r.settle();
    CHECK(inside(r.window("PassportDialog"), area));
    r.svc.simulateIncomingCall({PeerKind::User, 100});
    r.settle();
    CHECK(inside(r.window("CallPanel"), area));
  }
}
39.5ms