Skip to content
Repos

mologram

C++ 6.7 KB150 lines Opens in site

Directory Raw Download

// LIVE check of the terminal UI (NOT part of run_tests). Runs the real `mologram` binary in a pty and walks the read-only paths:
// connect, Premium state, settings (all sections), contacts, search, emoji, tear-off child, quit. It prints PASS or FAIL per step and
// never prints screen contents (they hold private chat data). With --fake the same flow runs against the in-memory service.
//   usage: live_ui --fake
//          live_ui <env.json> <auth.tsv>        (run under: flock /tmp/mologram-live.lock live_ui ...)
// The real run only reads. It opens no chat (opening marks messages read), sends nothing and changes no setting.
#include <signal.h>
#include <sys/ioctl.h>

#include <cstdio>
#include <cstring>
#include <string>
#include <vector>

#include "ptychild.hpp"

using ptytest::PtyChild;

static int failures = 0;
static 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++;
}

static const std::string kF2 = "\x1bOQ", kF1 = "\x1bOP", kDown = "\x1b[B", kEsc = "\x1b", kCtrlQ = "\x11", kCtrlK = "\x0b", kAltC = "\x1b" "c";

// Row r (0 based) of the child's screen.
static std::string rowText(const PtyChild& c, int r) {
  if (r < 0 || r >= (int)c.vt->grid.size()) return "";
  std::string line;
  for (auto& cell : c.vt->grid[(size_t)r]) line += cell.ch;
  return line;
}

// Layout check at several terminal sizes, including a live resize: menu bar on the first row, status bar on the last, chat list rows in
// between, no broken characters, and the dialogs fit. Counts only; no screen content is printed.
static int sizesCheck(const std::vector<std::string>& args) {
  struct Size { int cols, rows; };
  const Size sizes[] = {{40, 15}, {80, 24}, {160, 50}};
  PtyChild c;
  if (!c.start(args, 120, 36)) { std::fprintf(stderr, "cannot start mologram\n"); return 2; }
  check("start 120x36: connected", c.waitFor("connected", 25000, false));
  for (const Size& sz : sizes) {
    winsize ws{};
    ws.ws_col = (unsigned short)sz.cols;
    ws.ws_row = (unsigned short)sz.rows;
    ::ioctl(c.fd, TIOCSWINSZ, &ws);
    c.vt = std::make_unique<ttgui::VtParser>(sz.cols, sz.rows);
    ::kill(c.pid, SIGWINCH);
    c.pump(1500);
    std::string tag = std::to_string(sz.cols) + "x" + std::to_string(sz.rows);
    check(tag + ": menu bar on the first row", rowText(c, 0).find("Chat") != std::string::npos);
    check(tag + ": status bar on the last row", rowText(c, sz.rows - 1).find("onnect") != std::string::npos);
    int filled = 0;
    for (int r = 1; r < sz.rows - 1; r++) { std::string t = rowText(c, r); if (t.find_first_not_of(' ') != std::string::npos) filled++; }
    check(tag + ": the chat list fills rows between the bars", filled >= std::min(4, sz.rows - 4));
    check(tag + ": no broken characters", c.screen().find("\xEF\xBF\xBD") == std::string::npos);
    c.write(kF2);
    c.pump(900);
    check(tag + ": settings fits below the menu bar", c.has("Settings") && rowText(c, 0).find("Chat") != std::string::npos);
    c.write(kEsc);
    c.pump(500);
    c.write(kF1);
    c.pump(700);
    check(tag + ": help overlay fits", c.has("Message/Send") || c.has("Action"));
    c.write(kEsc);
    c.pump(500);
  }
  c.write(kCtrlQ);
  int status = c.wait(10000);
  check("quits with exit code 0", status != -1 && WIFEXITED(status) && WEXITSTATUS(status) == 0);
  check("terminal restored", ptytest::restored(c.out));
  std::printf("RESULT failures=%d\n", failures);
  return failures ? 1 : 0;
}

int main(int argc, char** argv) {
  bool sizes = false;
  for (int i = 1; i < argc; i++) if (std::string(argv[i]) == "--sizes") sizes = true;
  if (sizes) {  // live_ui --sizes --fake   |   live_ui --sizes <env.json> <auth.tsv>
    std::vector<std::string> rest;
    for (int i = 1; i < argc; i++) if (std::string(argv[i]) != "--sizes") rest.push_back(argv[i]);
    bool fk = !rest.empty() && rest[0] == "--fake";
    if (!fk && rest.size() < 2) { std::fprintf(stderr, "usage: live_ui --sizes --fake | live_ui --sizes <env.json> <auth.tsv>\n"); return 2; }
    char t[] = "/tmp/mologram-livesizes-XXXXXX";
    std::string tmp = mkdtemp(t);
    std::vector<std::string> a{ptytest::binDir() + "/mologram"};
    if (fk) a.push_back("--fake"); else a.insert(a.end(), {"--socket", tmp + "/live.sock", "--config", rest[0], "--auth-tsv", rest[1]});
    return sizesCheck(a);
  }
  bool fake = argc >= 2 && std::string(argv[1]) == "--fake";
  if (!fake && argc < 3) { std::fprintf(stderr, "usage: live_ui --fake | live_ui <env.json> <auth.tsv>\n"); return 2; }
  std::string dir = ptytest::binDir();
  char tmpl[] = "/tmp/mologram-liveui-XXXXXX";
  std::string tmp = mkdtemp(tmpl);
  std::vector<std::string> args{dir + "/mologram"};
  if (fake) {
    args.push_back("--fake");
  } else {
    args.insert(args.end(), {"--socket", tmp + "/live.sock", "--config", argv[1], "--auth-tsv", argv[2]});
  }
  PtyChild c;
  if (!c.start(args, 120, 36)) { std::fprintf(stderr, "cannot start mologram\n"); return 2; }

  check("UI draws the menu bar", c.waitFor("Message", 15000, false));
  check("status bar reaches connected", c.waitFor("connected", 20000, false));
  if (!fake) check("Premium detected", c.waitFor("Premium", 5000, false) && !c.has("No Premium"));
  check("chat folders are shown", c.waitFor("All", 10000, false));

  // settings: F2, then every section by Down on the focused section combo
  c.write(kF2);
  c.pump(800);
  check("settings opens", c.waitFor("Settings", 5000, false));
  check("profile section loads", c.waitFor("First name", 8000, false) || c.has("Save"));
  struct Step { const char* section; const char* marker; };
  const Step steps[] = {{"Privacy", "Last seen"}, {"Sessions", "Device"}, {"Notifications", "Mute"}, {"Appearance", "Theme"}, {"Keys", "Action"}, {"Premium", "Telegram Premium"}};
  for (const auto& st : steps) {
    c.write(kDown + kDown + "\r");  // the combo: Down opens the list, Down moves, Enter commits
    c.pump(500);
    check(std::string("settings section ") + st.section, c.waitFor(st.marker, 6000, false));
  }
  c.write(kEsc);
  c.pump(500);
  check("Escape closes settings", !c.has("Telegram Premium: ") && !c.has("Device  "));

  // contacts and search
  c.write(kAltC);
  c.pump(800);
  check("contacts dialog opens", c.waitFor("Contacts", 5000, false));
  c.write(kEsc);
  c.pump(400);
  c.write(kCtrlK);
  c.pump(800);
  check("search dialog opens", c.waitFor("Search", 5000, false));
  c.write(kEsc);
  c.pump(400);
  c.write(kF1);
  c.pump(600);
  check("help overlay lists the keys", c.waitFor("Message/Send", 5000, false));
  c.write(kEsc);
  c.pump(400);

  c.write(kCtrlQ);
  int status = c.wait(10000);
  check("quits with exit code 0", status != -1 && WIFEXITED(status) && WEXITSTATUS(status) == 0);
  check("terminal restored", ptytest::restored(c.out));
  std::printf("RESULT failures=%d\n", failures);
  return failures ? 1 : 0;
}
32.8ms