// M6 conversation features through the full app: every action has a handler, drafts, pins, selection, unread divider, stickers, inline // bots, edit-last by key, chat context menu, notifications. SPEC FR-11, FR-20..25, FR-71, FR-75. #include "rig.hpp" #include "mologram/ActionRegistry.hpp" #include "mologram/ui/ActionRouter.hpp" using namespace uitest; namespace { const char* kQuietTerminal = "\"terminal\":{\"command\":\"true {cmd}\"}"; } TEST_CASE("every registered action has a handler and a menu entry", "[ui][actions][integration]") { Rig r; size_t menuItems = 0; for (auto& d : r.app.menuBar()->dropdowns) menuItems += d.size(); CHECK(menuItems >= 30); for (const auto& a : ActionRegistry::all()) { CHECK(r.app.actions().bound(a.id)); size_t slash = a.menu.find('/'); REQUIRE(slash != std::string::npos); std::string label = a.menu.substr(slash + 1); bool inMenu = false; for (auto& d : r.app.menuBar()->dropdowns) for (auto& it : d) if (it.label == label) inMenu = true; CHECK(inMenu); } } TEST_CASE("running every action with a chat open never crashes and leaves no stuck state at 40x15 and 120x34", "[ui][actions][integration]") { for (auto sz : {std::make_pair(40, 15), std::make_pair(120, 34)}) { Rig r(sz.first, sz.second, kQuietTerminal); r.openAlice(); for (const auto& a : ActionRegistry::all()) { if (a.id == "quit") continue; r.app.actions().run(a.id); r.settle(); std::vector extra; for (auto& w : r.app.app().wm.windows) if (w->appName != "MainWindow") extra.push_back(w.get()); for (auto* w : extra) r.app.app().wm.closeWindow(w); r.app.app().wm.processPendingCloses(); r.app.context().keyCapture = nullptr; r.settle(); } CHECK(has(r.screen(), "Chat")); // the menu bar is still there and the screen renders } } TEST_CASE("drafts are saved while typing and show up in the chat list", "[ui][session][integration]") { Rig r; r.openAlice(); r.app.app().setFocused(&r.composer()); r.typeInto(r.composer(), "half a thou"); CHECK_EQ(r.svc.dialogs()[1].draft, std::string("half a thou")); r.typeInto(r.composer(), "ght"); CHECK_EQ(r.svc.dialogs()[1].draft, std::string("half a thought")); bool inList = false; for (auto& row : r.app.shell().allChats) if (row.name == "Alice" && row.preview.find("Draft: half a thought") == 0) inList = true; CHECK(inList); // switching chat restores the draft of the other chat and clears the composer r.open({PeerKind::User, 101}); CHECK(r.composer().editor->getText().empty()); r.openAlice(); CHECK_EQ(r.composer().editor->getText(), std::string("half a thought")); // sending clears the draft on the server r.composer().doSend(); r.settle(); CHECK(r.svc.dialogs()[1].draft.empty()); } TEST_CASE("pin, pinned bar, jump and unpin", "[ui][session][integration]") { Rig r; r.openAlice(); long long mid = r.session().target(); r.action("pin"); CHECK(r.svc.history({PeerKind::User, 100}).back().pinned); REQUIRE(r.app.shell().pinned->pins.size() == 1); CHECK_EQ(r.app.shell().pinned->pins[0].first, mid); r.action("pinned_next"); // jumps to the pinned message (already loaded: scroll only) r.action("pin"); // toggles back CHECK(!r.svc.history({PeerKind::User, 100}).back().pinned); CHECK(r.app.shell().pinned->pins.empty()); // the pinned list is loaded when a chat with pins is opened js::Object p; p["peer"] = js::Value("user:101"); p["id"] = js::id(r.svc.history({PeerKind::User, 101}).front().id); p["on"] = js::Value(true); r.app.context().client.request("pin", "messages.pin", p, [](const RpcResult&) {}); r.settle(); r.open({PeerKind::User, 101}); CHECK_EQ(r.app.shell().pinned->pins.size(), (size_t)1); } TEST_CASE("jumping to a message that is not loaded fetches the page around it", "[ui][session][integration]") { Rig r; r.openAlice(); long long old = r.svc.history({PeerKind::User, 100}).front().id; // far above the newest page of 30 bool loaded = false; for (auto& m : r.app.shell().view->msgs) if (m.id == old) loaded = true; CHECK(!loaded); r.session().jumpTo(old); r.settle(); for (auto& m : r.app.shell().view->msgs) if (m.id == old) loaded = true; CHECK(loaded); CHECK(r.app.shell().view->hasNewer); } TEST_CASE("multi-select then delete removes exactly the selected messages", "[ui][session][integration]") { Rig r; r.openAlice(); const auto& view = r.app.shell().view->msgs; REQUIRE(view.size() >= 3); long long a = view[view.size() - 1].id, b = view[view.size() - 2].id, keep = view[view.size() - 3].id; r.session().toggleSelect(a); r.session().toggleSelect(b); r.session().toggleSelect(a); // toggled off again r.session().toggleSelect(a); CHECK_EQ(r.session().selection().size(), (size_t)2); r.action("delete"); // asks first: for me or for everyone ttgui::Window* dd = r.window("DeleteDialog"); REQUIRE(dd != nullptr); CHECK(has(r.screen(), "Delete 2 messages?")); r.click(dd, "Delete for everyone"); CHECK(r.svc.lastDeleteRevoke()); for (auto& m : r.svc.history({PeerKind::User, 100})) { CHECK(m.id != a); CHECK(m.id != b); } bool kept = false; for (auto& m : r.svc.history({PeerKind::User, 100})) if (m.id == keep) kept = true; CHECK(kept); CHECK(r.session().selection().empty()); } TEST_CASE("forward with a selection opens the forward dialog for all selected messages", "[ui][session][integration]") { Rig r; r.openAlice(); const auto& view = r.app.shell().view->msgs; r.session().toggleSelect(view[view.size() - 1].id); r.session().toggleSelect(view[view.size() - 2].id); r.action("forward"); ttgui::Window* w = r.window("ForwardDialog"); REQUIRE(w != nullptr); r.widgets(w)[0]->onActivated.emit(0); // the first chat in the list r.settle(); size_t forwarded = 0; for (auto& d : r.svc.dialogs()) for (auto& m : r.svc.history(d.peer)) if (m.forwardFrom == "Alice") forwarded++; CHECK_EQ(forwarded, (size_t)2); } TEST_CASE("the first unread message gets a New messages divider", "[ui][session][integration]") { Rig r; r.openAlice(); // the fake seeds two unread messages in this chat CHECK(has(r.screen(), "New messages")); r.open({PeerKind::User, 101}); CHECK(!has(r.screen(), "New messages")); } TEST_CASE("stickers, inline bots, bot buttons and edit-last by the Up key", "[ui][session][integration]") { Rig r; r.openAlice(); r.session().sendSticker("st-hello"); r.settle(); CHECK(r.svc.history({PeerKind::User, 100}).back().media.kind == MediaKind::Sticker); r.session().sendInline("inlinebot", "cats"); r.settle(); CHECK_EQ(r.svc.history({PeerKind::User, 100}).back().viaBot, std::string("inlinebot")); std::string toastBody; r.app.context().toast = [&](const std::string&, const std::string& b) { toastBody = b; }; r.session().toast = r.app.context().toast; r.session().pressButton(r.session().target(), "payload"); r.settle(); CHECK_EQ(toastBody, std::string("clicked payload")); r.session().pressButton(1, "https://example.org"); CHECK_EQ(toastBody, std::string("https://example.org")); r.composer().doSend(); // nothing to send, nothing happens r.composer().editor->setText("my last words"); r.composer().doSend(); r.settle(); r.app.app().setFocused(&r.composer()); r.press("Up"); CHECK_EQ(r.composer().banner, std::string("Editing")); CHECK_EQ(r.composer().editor->getText(), std::string("my last words")); } TEST_CASE("chat row menu: pin, mute, archive, mark read, delete through the controller and the context menu", "[ui][session][integration]") { Rig r; Peer alice{PeerKind::User, 100}; auto labels = r.app.controller().chatMenuFor(alice); CHECK(std::find(labels.begin(), labels.end(), "Pin") != labels.end()); CHECK(std::find(labels.begin(), labels.end(), "Mute") != labels.end()); CHECK(std::find(labels.begin(), labels.end(), "Archive") != labels.end()); CHECK(std::find(labels.begin(), labels.end(), "Mark as read") != labels.end()); // the fake seeds unread messages CHECK(std::find(labels.begin(), labels.end(), "Open in new window") != labels.end()); r.app.controller().chatAction(alice, "Pin"); r.settle(); CHECK(r.app.controller().dialog(alice)->pinned); r.app.controller().chatAction(alice, "Mute"); r.settle(); CHECK(r.app.controller().dialog(alice)->muted); r.app.controller().chatAction(alice, "Mark as read"); r.settle(); CHECK_EQ(r.app.controller().dialog(alice)->unread, 0); r.app.controller().chatAction(alice, "Archive"); r.settle(); CHECK(r.app.controller().dialog(alice)->archived); bool inAll = false; for (auto& row : r.app.shell().shownRows) if (row.name == "Alice") inAll = true; CHECK(!inAll); // the All tab hides archived chats r.app.shell().folders->active = (int)r.app.shell().folders->folders.size() - 1; r.app.shell().applyFilter(); bool inArchive = false; for (auto& row : r.app.shell().shownRows) if (row.name == "Alice") inArchive = true; CHECK(inArchive); r.app.controller().chatAction(alice, "Delete chat"); r.settle(); CHECK(r.app.controller().dialog(alice) == nullptr); // the right-click menu shows the same entries and its items act Rig m; m.app.shell().list->onContextRow.emit(2, ttgui::Point{5, 5}); // Bob REQUIRE(m.app.shell().ctx->open); std::vector shown; for (auto& it : m.app.shell().ctx->items) shown.push_back(it.label); CHECK(std::find(shown.begin(), shown.end(), "Pin") != shown.end()); for (auto& it : m.app.shell().ctx->items) if (it.label == "Pin") it.action(); m.settle(); bool bobPinned = false; for (auto& d : m.svc.dialogs()) if (d.name == "Bob" && d.pinned) bobPinned = true; CHECK(bobPinned); CHECK(!m.app.shell().ctx->open); } TEST_CASE("a mouse click on a chat row opens it", "[ui][session][integration]") { Rig r; r.app.shell().list->onClicked.emit(1); r.settle(); CHECK(r.app.controller().current().valid()); } TEST_CASE("notifications: ring and toast for an unopened unmuted chat, silence for open, muted or disabled", "[ui][session][integration]") { Rig r; size_t before = r.app.app().notifications.size(); r.svc.simulateIncoming({PeerKind::User, 101}, "hello Bob here"); r.settle(); CHECK_EQ(r.app.app().notifications.size(), before + 1); r.openAlice(); size_t afterOpen = r.app.app().notifications.size(); r.svc.simulateIncoming({PeerKind::User, 100}, "visible chat"); r.settle(); CHECK_EQ(r.app.app().notifications.size(), afterOpen); // the open chat is not announced r.app.controller().chatAction({PeerKind::User, 102}, "Mute"); r.settle(); r.svc.simulateIncoming({PeerKind::User, 102}, "muted chat"); r.settle(); CHECK_EQ(r.app.app().notifications.size(), afterOpen); r.app.config().override("notifications.enabled", js::Value(false)); r.svc.simulateIncoming({PeerKind::User, 101}, "disabled"); r.settle(); CHECK_EQ(r.app.app().notifications.size(), afterOpen); } TEST_CASE("Alt letter shortcuts match the way a real terminal sends them (lower case letter)", "[ui][actions][unit]") { CHECK_EQ(ActionRouter::canonicalKey("Alt+c"), std::string("Alt+C")); CHECK_EQ(ActionRouter::canonicalKey("Ctrl+Up"), std::string("Ctrl+Up")); CHECK_EQ(ActionRouter::canonicalKey("F1"), std::string("F1")); Rig r; ttgui::KeyEventData k; k.code = ttgui::KeyCode::Char; k.alt = true; k.text = "c"; // what ESC c decodes to k.ch = U'c'; ttgui::Event e = ttgui::Event::makeKey(k); r.app.app().dispatch(e); r.settle(); CHECK(r.hasWindow("ContactsDialog")); // Alt+C opens the contacts } TEST_CASE("delete for me sends revoke=false; cancel deletes nothing; the context menu asks too", "[ui][session][integration]") { Rig r; r.openAlice(); size_t before = r.svc.history({PeerKind::User, 100}).size(); long long mid = r.session().target(); r.action("delete"); ttgui::Window* dd = r.window("DeleteDialog"); REQUIRE(dd != nullptr); CHECK(has(r.screen(), "Delete 1 message?")); r.click(dd, "Cancel"); CHECK_EQ(r.svc.history({PeerKind::User, 100}).size(), before); r.session().onMenu("Delete", mid); dd = r.window("DeleteDialog"); REQUIRE(dd != nullptr); r.click(dd, "Delete for me"); CHECK(!r.svc.lastDeleteRevoke()); CHECK_EQ(r.svc.history({PeerKind::User, 100}).size(), before - 1); } TEST_CASE("@bot query asks the inline bot; other @ text is sent as it is", "[ui][session][integration]") { Rig r; r.open({PeerKind::User, 101}); // not a bot chat: the bot is addressed from any chat r.settle(); CHECK(r.app.controller().isBotUsername("BuildBot")); CHECK(!r.app.controller().isBotUsername("alice")); r.composer().editor->setText("@buildbot cats"); r.composer().doSend(); r.settle(); CHECK_EQ(r.svc.history({PeerKind::User, 101}).back().viaBot, std::string("inlinebot")); r.composer().editor->setText("@alice hello there"); r.composer().doSend(); r.settle(); CHECK_EQ(r.svc.history({PeerKind::User, 101}).back().text, std::string("@alice hello there")); r.composer().editor->setText("@buildbot"); // no query: plain text r.composer().doSend(); r.settle(); CHECK_EQ(r.svc.history({PeerKind::User, 101}).back().text, std::string("@buildbot")); }