// LIVE check of the UI write paths against the real account (NOT part of run_tests). The real UI classes (MologramApp, ChatSession, the // poll and search dialogs) run on the real TapiService; the steps are the ones a user takes: send, edit, react, pin, poll, search, delete. // Only Saved Messages is written to, every message created here is deleted again, and nothing but PASS or FAIL lines is printed (the // chat holds private text). usage (run under flock /tmp/mologram-live.lock): live_ui_write #include #include #include #include #include #include #include #include "mologram/Config.hpp" #include "mologram/tapi/TapiConfig.hpp" #include "mologram/ui/MologramApp.hpp" #include "mologram/ui/dialogs/ChatDialogs.hpp" using namespace mologram; using namespace mologram::ui; namespace { int failures = 0; void check(const std::string& name, bool ok) { std::printf("%s %s\n", ok ? "PASS" : "FAIL", name.c_str()); std::fflush(stdout); if (!ok) failures++; } template bool waitUntil(MologramApp& app, F pred, int ms = 20000) { auto end = std::chrono::steady_clock::now() + std::chrono::milliseconds(ms); while (std::chrono::steady_clock::now() < end) { app.dispatcher().drain(); if (pred()) return true; std::this_thread::sleep_for(std::chrono::milliseconds(25)); } app.dispatcher().drain(); return pred(); } RpcResult callSync(ITelegramService& s, const std::string& m, js::Object p = {}) { auto pr = std::make_shared>(); auto fu = pr->get_future(); s.call(m, js::Value(std::move(p)), [pr](const RpcResult& r) { pr->set_value(r); }); if (fu.wait_for(std::chrono::seconds(60)) != std::future_status::ready) return RpcResult::failure("test", "no reply"); return fu.get(); } template void collect(ttgui::Widget* w, std::vector& out) { if (!w) return; if (auto* t = dynamic_cast(w)) out.push_back(t); for (auto& c : w->children()) collect(c.get(), out); } ttgui::Window* window(MologramApp& app, const std::string& name) { for (auto& w : app.app().wm.windows) if (w->appName == name && !w->closing) return w.get(); return nullptr; } const ttgui::TgMessage* find(MologramApp& app, const std::string& text) { for (auto& m : app.shell().view->msgs) if (m.text == text) return &m; return nullptr; } } // namespace int main(int argc, char** argv) { if (argc < 3) { std::fprintf(stderr, "usage: live_ui_write \n"); return 2; } Config cfg = Config::load(argv[1]); char tmpl[] = "/tmp/mologram-livewrite-XXXXXX"; std::string tmp = mkdtemp(tmpl); TapiService service(tapiOptionsFromConfig(cfg, argv[2])); service.start(); MologramApp::Options opt; opt.prefsPath = tmp + "/prefs.json"; MologramApp app(cfg, service, opt); app.build(120, 34); check("the chat list loads", waitUntil(app, [&] { return !app.controller().dialogs().empty(); }, 40000)); RpcResult prof = callSync(service, "account.profile"); Peer self = prof.ok ? Peer::fromJson(*prof.value.find("peer")) : Peer{}; check("the own account (Saved Messages) is known", self.valid()); if (!self.valid()) return 1; app.controller().openPeer(self); check("Saved Messages opens", waitUntil(app, [&] { return app.controller().session().peer() == self; })); app.shell().composer->editor->setText(""); char stamp[32]; std::snprintf(stamp, sizeof(stamp), "%lld", (long long)std::time(nullptr)); const std::string marker = std::string("mologram live ui ") + stamp; std::vector created; // every id this program makes, deleted at the end auto rememberAll = [&]() { for (auto& m : app.shell().view->msgs) if (m.text.rfind(marker, 0) == 0 || m.pollQuestion.rfind(marker, 0) == 0) if (std::find(created.begin(), created.end(), m.id) == created.end()) created.push_back(m.id); }; // ---- send through the composer app.shell().composer->editor->setText(marker); app.shell().composer->doSend(); bool sent = waitUntil(app, [&] { return find(app, marker) != nullptr; }); check("send: the message reaches Saved Messages and the view", sent); rememberAll(); const ttgui::TgMessage* m1 = find(app, marker); // ---- edit through the composer if (m1) { long long id = m1->id; app.controller().session().editLastOwn(); check("edit: the composer shows the text with the Editing banner", app.shell().composer->banner == "Editing"); app.shell().composer->editor->setText(marker + " edited"); app.shell().composer->doSend(); bool edited = waitUntil(app, [&] { const ttgui::TgMessage* m = find(app, marker + " edited"); return m && m->id == id && m->edited; }); check("edit: the new text and the edited mark are shown", edited); rememberAll(); } else { check("edit: skipped, the send failed", false); } const ttgui::TgMessage* m2 = find(app, marker + " edited"); // ---- react (the account has Premium, a plain reaction needs nothing) if (m2) { long long id = m2->id; Reaction r; r.emoji = "\xF0\x9F\x91\x8D"; app.controller().session().react(id, r, true); bool reacted = waitUntil(app, [&] { RpcResult h = callSync(service, "messages.history", {{"peer", self.toJson()}, {"count", js::Value(15)}}); if (!h.ok) return false; if (const js::Array* a = js::getArr(h.value, "messages")) for (const auto& e : *a) { Message m = Message::fromJson(e); if (m.id == id && !m.reactions.empty()) return true; } return false; }, 15000); check("react: the reaction is stored by Telegram", reacted); } else { check("react: skipped, no message", false); } // ---- pin and unpin if (m2) { long long id = m2->id; app.controller().session().togglePin(id); // messages.pinned (the pinned bar list) is still a pending method in TapiService, so the pin is read from the message itself auto pinState = [&](bool want) { return waitUntil(app, [&] { RpcResult h = callSync(service, "messages.history", {{"peer", self.toJson()}, {"count", js::Value(15)}}); if (!h.ok) return false; if (const js::Array* a = js::getArr(h.value, "messages")) for (const auto& e : *a) { Message mm = Message::fromJson(e); if (mm.id == id) return mm.pinned == want; } return false; }, 15000); }; check("pin: the message is pinned in Telegram", pinState(true)); // the session learns of the pin from the reply of messages.pin; unpinning before that would send a second pin waitUntil(app, [&] { const Message* cm = app.controller().session().message(id); return cm && cm->pinned; }, 10000); app.controller().session().togglePin(id); check("pin: unpin removes it again", pinState(false)); } else { check("pin: skipped, no message", false); } // ---- poll through the poll dialog PollDialog::open(app.context()); if (ttgui::Window* w = window(app, "PollDialog")) { std::vector edits; collect(w, edits); if (edits.size() >= 3) { edits[0]->text = marker + " poll?"; edits[1]->text = "yes"; edits[2]->text = "no"; std::vector buttons; collect(w, buttons); for (auto* b : buttons) if (b->label == "Create" || b->label == "Send") { b->onPressed.emit(); break; } } } bool poll = waitUntil(app, [&] { for (auto& m : app.shell().view->msgs) if (m.pollQuestion == marker + " poll?") return true; return false; }); check("poll: the dialog creates a poll that shows in the chat", poll); rememberAll(); for (auto& m : app.shell().view->msgs) if (m.pollQuestion == marker + " poll?") { long long id = m.id; app.controller().session().vote(id, {0}); check("poll: a vote is accepted", waitUntil(app, [&] { RpcResult h = callSync(service, "messages.history", {{"peer", self.toJson()}, {"count", js::Value(15)}}); if (const js::Array* a = js::getArr(h.value, "messages")) for (const auto& e : *a) { Message mm = Message::fromJson(e); if (mm.id == id && mm.hasPoll && mm.poll.totalVoters >= 1) return true; } return false; }, 15000)); } // ---- search through the search dialog SearchDialog::open(app.context(), &self, marker); bool found = false; waitUntil(app, [&] { ttgui::Window* w = window(app, "SearchDialog"); if (!w) return false; std::vector lists; collect(w, lists); for (auto* l : lists) if (!l->items.empty()) { found = true; return true; } std::vector tables; collect(w, tables); for (auto* t : tables) if (!t->rows.empty()) { found = true; return true; } return false; }, 20000); check("search: the dialog finds the messages by their text", found); // ---- delete everything this program created, through the session (for everyone) rememberAll(); app.controller().session().deleteMessages(created, true); bool gone = waitUntil(app, [&] { RpcResult h = callSync(service, "messages.history", {{"peer", self.toJson()}, {"count", js::Value(30)}}); if (!h.ok) return false; if (const js::Array* a = js::getArr(h.value, "messages")) for (const auto& e : *a) if (std::find(created.begin(), created.end(), (long long)Message::fromJson(e).id) != created.end()) return false; return true; }, 20000); check("delete: every message created here is gone from Telegram", gone); std::printf("created %d messages, %s\n", (int)created.size(), failures ? "SOME STEPS FAILED" : "all steps passed"); std::error_code ec; std::filesystem::remove_all(tmp, ec); return failures ? 1 : 0; }