// MologramApp headless: real data from FakeTelegramService reaches the widgets; send, receive, reply, edit, delete; Premium
// panel; keys and menus (SPEC FR-10..25, FR-61..66, FR-70, FR-71, FR-75).
#include "rig.hpp"
using namespace mologram;
using namespace mologram::ui;
using uitest::Rig;
using uitest::has;
namespace {
} // namespace
TEST_CASE("app shows chats, folders and status from the service", "[ui][integration]") {
Rig r;
std::string s = r.screen();
CHECK(has(s, "Saved Messages"));
CHECK(has(s, "Alice"));
const auto& tabs = r.app.shell().folders->folders; // tabs collapse to a dropdown at this width, the list is what matters
CHECK(std::find(tabs.begin(), tabs.end(), "Archive") != tabs.end());
CHECK(std::find(tabs.begin(), tabs.end(), "Personal") != tabs.end());
CHECK_EQ(tabs.front(), std::string("All"));
CHECK(has(s, "No Premium")); // fake starts without Premium
CHECK(has(s, "Fake Account"));
CHECK(has(s, "Select a chat"));
}
TEST_CASE("opening a chat loads its history and header", "[ui][integration]") {
Rig r;
r.app.controller().openPeer({PeerKind::User, 100});
r.settle();
std::string s = r.screen();
CHECK(has(s, "Message 40 in Alice"));
CHECK(has(s, "online"));
CHECK_EQ(r.app.controller().current().id, (std::int64_t)100);
CHECK_EQ(r.svc.dialogs()[1].unread, 0);
}
TEST_CASE("send from the composer reaches the service and the view exactly once", "[ui][integration]") {
Rig r;
r.app.controller().openPeer({PeerKind::User, 100});
r.settle();
r.app.shell().composer->editor->setText("hello from the test");
r.app.shell().composer->doSend();
r.settle();
const auto& h = r.svc.history({PeerKind::User, 100});
CHECK_EQ(h.back().text, std::string("hello from the test"));
int count = 0;
for (auto& m : r.app.shell().view->msgs) if (m.text == "hello from the test") count++;
CHECK_EQ(count, 1); // the send reply and the message.new event do not duplicate it
CHECK(has(r.screen(), "hello from the test"));
}
TEST_CASE("incoming message: appended in the open chat, counted as unread elsewhere", "[ui][integration]") {
Rig r;
r.app.controller().openPeer({PeerKind::User, 100});
r.settle();
r.svc.simulateIncoming({PeerKind::User, 100}, "ping open");
r.svc.simulateIncoming({PeerKind::User, 101}, "ping other");
r.settle();
std::string s = r.screen();
CHECK(has(s, "ping open"));
bool badge = false;
for (auto& row : r.app.shell().allChats) if (row.name == "Bob" && row.unread >= 1) badge = true;
CHECK(badge);
CHECK_EQ(r.svc.dialogs()[1].unread, 0); // the open chat is marked read
}
TEST_CASE("reply, edit and delete through the controller", "[ui][integration]") {
Rig r;
r.app.controller().openPeer({PeerKind::User, 100});
r.settle();
long long foreign = 0, own = 0;
for (auto& m : r.app.shell().view->msgs) { if (!m.outgoing) foreign = m.id; else own = m.id; }
REQUIRE(foreign != 0); REQUIRE(own != 0);
r.app.controller().replyTo(foreign);
CHECK(has(r.app.shell().composer->banner, "Reply to"));
r.app.shell().composer->editor->setText("an answer");
r.app.shell().composer->doSend();
r.settle();
CHECK_EQ(r.svc.history({PeerKind::User, 100}).back().replyTo, (std::int64_t)foreign);
r.app.controller().editMessage(own);
CHECK_EQ(r.app.shell().composer->banner, std::string("Editing"));
r.app.shell().composer->editor->setText("edited text");
r.app.shell().composer->doSend();
r.settle();
bool edited = false;
for (auto& m : r.svc.history({PeerKind::User, 100})) if (m.id == own && m.edited && m.text == "edited text") edited = true;
CHECK(edited);
r.app.controller().deleteMessage(own);
r.settle();
for (auto& m : r.app.shell().view->msgs) CHECK(m.id != own);
}
TEST_CASE("premium: missing Premium shows the calm panel, state flips live, loss midway keeps the draft", "[ui][premium][integration]") {
Rig r;
CHECK(!r.app.premium().has());
bool ran = false;
CHECK(!r.app.premium().use("translate", [&] { ran = true; }));
CHECK(!ran);
CHECK(r.hasWindow("PremiumPanel"));
r.svc.setPremium(PremiumState::Premium);
r.settle();
CHECK(r.app.premium().has());
CHECK(has(r.screen(), "Premium"));
r.app.controller().openPeer({PeerKind::User, 100});
r.settle();
r.app.shell().composer->editor->setText("my unsent draft");
r.svc.revokePremiumSilently(); // server stops honouring Premium while we still believe it
js::Object p; p["peer"] = js::Value("user:100"); p["id"] = js::id(r.svc.history({PeerKind::User, 100}).back().id);
js::Array rs; Reaction a; a.emoji = "A"; Reaction b; b.emoji = "B"; rs.push_back(a.toJson()); rs.push_back(b.toJson());
p["reactions"] = js::Value(rs);
ServiceClient client(r.svc, r.app.dispatcher());
client.onError = [&](const std::string&, const RpcError& e) { if (e.code == ErrorCode::PremiumRequired) r.app.premium().refused("extra_reactions"); };
client.request("react", "messages.react", p, [](const RpcResult&) {});
r.settle();
CHECK(!r.app.premium().has());
CHECK(r.hasWindow("PremiumPanel"));
CHECK_EQ(r.app.shell().composer->editor->getText(), std::string("my unsent draft")); // nothing lost
CHECK_EQ(r.app.controller().current().id, (std::int64_t)100);
}
TEST_CASE("keys: F1 opens help listing the actions, Ctrl+Q quits, Ctrl+Down opens the next chat", "[ui][integration]") {
Rig r;
r.press("F1");
CHECK(r.hasWindow("HelpOverlay"));
CHECK(has(r.screen(), "Message/Send"));
Rig q;
q.app.app().running = true;
q.press("Ctrl+Q");
CHECK(!q.app.app().running);
Rig n;
n.press("Ctrl+Down");
CHECK(n.app.controller().current().valid());
}
TEST_CASE("menu bar has one entry per action menu path", "[ui][unit]") {
Rig r;
auto* bar = r.app.menuBar();
REQUIRE(bar != nullptr);
std::set<std::string> tops(bar->menus.begin(), bar->menus.end());
for (const char* m : {"Chat", "Go", "Message", "Format", "View", "Window", "App", "Help"}) CHECK(tops.count(m) == 1);
size_t items = 0;
for (auto& d : bar->dropdowns) items += d.size();
CHECK(items >= 20);
}
TEST_CASE("narrow terminal 40x15 renders without overlap or crash and one pane at a time", "[ui][integration]") {
Rig r(40, 15);
std::string list = r.screen();
CHECK(has(list, "Saved Messages"));
r.app.controller().openPeer({PeerKind::User, 100});
r.settle();
std::string chat = r.screen();
CHECK(has(chat, "Message 40 in Alice"));
CHECK(!has(chat, "Saved Messages")); // the list collapsed behind the conversation
}
TEST_CASE("wide terminal 160x50 shows list, conversation and info at once", "[ui][integration]") {
Rig r(160, 50);
r.app.controller().openPeer({PeerKind::User, 100});
r.settle();
std::string s = r.screen();
CHECK(has(s, "Saved Messages"));
CHECK(has(s, "Message 40 in Alice"));
CHECK(has(s, "Username")); // info pane
}