Skip to content
Repos

mologram

C++ 18.3 KB414 lines Opens in site

Directory Raw Download

// M7: tear-off into new terminals, floating chat windows in one terminal, live sync between windows, persistence, chat-only mode.
// SPEC FR-72, FR-73, A-02, A-03, NFR-02.
#include "rig.hpp"
#include "mologram/ui/TearOff.hpp"

using namespace uitest;

namespace {

const char* kScriptTerminal = "\"terminal\":{\"command\":\"@TMP@/term.sh {cmd}\"}";

TearOff::Env env(std::string terminal, std::vector<std::string> onPath) {
  TearOff::Env e;
  e.getenv = [terminal](const char* n) { return std::string(n) == "TERMINAL" ? terminal : std::string(); };
  e.inPath = [onPath](const std::string& x) { return std::find(onPath.begin(), onPath.end(), x) != onPath.end(); };
  return e;
}

}  // namespace

// ------------------------------------------------------------------ TearOff command and spawn

TEST_CASE("tear-off command: configured template, $TERMINAL, x-terminal-emulator, gnome-terminal, xterm, none", "[ui][tearoff][unit]") {
  Config plain = Config::fromText("{}");
  auto app = TearOff::appCommand(plain, "/bin/mologram", "user:100", "/run/m.sock");
  CHECK_EQ(app.size(), (size_t)5);
  CHECK_EQ(app[1], std::string("--chat"));
  CHECK_EQ(app[2], std::string("user:100"));
  CHECK_EQ(app[4], std::string("/run/m.sock"));

  auto c1 = TearOff::command(plain, "/bin/mologram", "user:100", "/s", env("foot", {"foot", "xterm"}));
  REQUIRE(c1.size() >= 3);
  CHECK_EQ(c1[0], std::string("foot"));
  CHECK_EQ(c1[1], std::string("-e"));
  CHECK_EQ(c1[2], std::string("/bin/mologram"));
  auto c2 = TearOff::command(plain, "/bin/mologram", "user:100", "/s", env("missing-term", {"x-terminal-emulator"}));
  CHECK_EQ(c2[0], std::string("x-terminal-emulator"));  // $TERMINAL is set but not installed: next candidate
  auto c3 = TearOff::command(plain, "/bin/mologram", "user:100", "/s", env("", {"gnome-terminal", "xterm"}));
  CHECK_EQ(c3[0], std::string("gnome-terminal"));
  CHECK_EQ(c3[1], std::string("--"));
  auto c4 = TearOff::command(plain, "/bin/mologram", "user:100", "/s", env("", {"xterm"}));
  CHECK_EQ(c4[0], std::string("xterm"));
  CHECK_EQ(c4[1], std::string("-e"));
  CHECK(TearOff::command(plain, "/bin/mologram", "user:100", "/s", env("", {})).empty());

  Config tpl = Config::fromText(R"({"terminal":{"command":"kitty --title mologram {cmd}"}})");
  auto c5 = TearOff::command(tpl, "/bin/mologram", "chat:5", "/s", env("", {}));
  REQUIRE(c5.size() > 4);
  CHECK_EQ(c5[0], std::string("kitty"));
  CHECK_EQ(c5[2], std::string("mologram"));
  CHECK_EQ(c5[3], std::string("/bin/mologram"));  // {cmd} expanded to the argv of mologram
  Config noPlaceholder = Config::fromText(R"({"terminal":{"command":"alacritty"}})");
  auto c6 = TearOff::command(noPlaceholder, "/bin/mologram", "chat:5", "/s", env("", {}));
  CHECK_EQ(c6[1], std::string("-e"));
  Config acct = Config::fromText(R"({"account":"work"})");
  auto c7 = TearOff::appCommand(acct, "/bin/mologram", "user:1", "/s");
  CHECK_EQ(c7[c7.size() - 2], std::string("--account"));
  CHECK_EQ(c7.back(), std::string("work"));
}

TEST_CASE("tear-off spawn starts a detached process and reports a missing program", "[ui][tearoff][unit]") {
  TempDir d;
  std::string out = d.path + "/out.txt";
  std::string script = d.path + "/run.sh";
  { std::ofstream f(script); f << "#!/bin/sh\necho \"$1 $2\" > '" << out << "'\n"; }
  ::chmod(script.c_str(), 0755);
  std::string err;
  CHECK(TearOff::spawn({script, "hello", "world"}, err));
  REQUIRE(Rig::waitForFile(out));
  CHECK(has(Rig::readFile(out), "hello world"));
  CHECK(!TearOff::spawn({d.path + "/does-not-exist"}, err));
  CHECK(has(err, "No such file"));
  CHECK(!TearOff::spawn({}, err));
}

// ------------------------------------------------------------------ opening a conversation in a new terminal

TEST_CASE("Ctrl+T, double click, context menu and edge drag each open a new terminal with --chat and --socket", "[ui][tearoff][integration]") {
  Rig probe;
  std::string out = probe.tmp.path + "/argv.txt";
  auto fresh = [&](Rig& r) { std::filesystem::remove(r.tmp.path + "/argv.txt"); };
  (void)fresh;

  {  // keyboard: Ctrl+T on the open chat
    Rig r(120, 34, kScriptTerminal);
    std::string o = r.tmp.path + "/argv.txt";
    r.fakeTerminal(o);
    r.openAlice();
    r.press("Ctrl+T");
    REQUIRE(Rig::waitForFile(o));
    std::string argv = Rig::readFile(o);
    CHECK(has(argv, "--chat user:100"));
    CHECK(has(argv, "--socket"));
  }
  {  // mouse: double click on a chat row
    Rig r(120, 34, kScriptTerminal);
    std::string o = r.tmp.path + "/argv.txt";
    r.fakeTerminal(o);
    r.app.shell().list->onDoubleClick.emit(2);  // Bob
    REQUIRE(Rig::waitForFile(o));
    CHECK(has(Rig::readFile(o), "--chat user:101"));
  }
  {  // mouse: right click menu "Open in new window"
    Rig r(120, 34, kScriptTerminal);
    std::string o = r.tmp.path + "/argv.txt";
    r.fakeTerminal(o);
    r.app.shell().list->onContextRow.emit(1, ttgui::Point{4, 4});
    bool done = false;
    for (auto& it : r.app.shell().ctx->items) if (it.label == "Open in new window") { it.action(); done = true; }
    REQUIRE(done);
    REQUIRE(Rig::waitForFile(o));
    CHECK(has(Rig::readFile(o), "--chat user:"));
  }
  {  // mouse: drag a chat row to the window edge and release
    Rig r(120, 34, kScriptTerminal);
    std::string o = r.tmp.path + "/argv.txt";
    r.fakeTerminal(o);
    ttgui::Rect a = r.app.shell().list->absoluteBounds();
    int row = 1;
    ttgui::Point start{a.x + 4, a.y + row * 2};
    r.mouse(ttgui::MouseKind::Down, start.x, start.y);
    for (int x = start.x; x > 0; x -= 6) r.mouse(ttgui::MouseKind::Drag, x, start.y);
    r.mouse(ttgui::MouseKind::Drag, 0, start.y);
    r.mouse(ttgui::MouseKind::Up, 0, start.y);
    REQUIRE(Rig::waitForFile(o));
    CHECK(has(Rig::readFile(o), "--chat user:"));
  }
  {  // a drop in the middle of the window does not tear anything off
    Rig r(120, 34, kScriptTerminal);
    std::string o = r.tmp.path + "/argv.txt";
    r.fakeTerminal(o);
    ttgui::Rect a = r.app.shell().list->absoluteBounds();
    r.mouse(ttgui::MouseKind::Down, a.x + 4, a.y + 2);
    r.mouse(ttgui::MouseKind::Drag, a.x + 10, a.y + 4);
    r.mouse(ttgui::MouseKind::Up, 60, 15);
    ::usleep(200000);
    CHECK(!std::filesystem::exists(o));
  }
  {  // mouse: drag the chat header to the edge (the open chat)
    Rig r(160, 40, kScriptTerminal);
    std::string o = r.tmp.path + "/argv.txt";
    r.fakeTerminal(o);
    r.openAlice();
    ttgui::Rect h = r.app.shell().header->absoluteBounds();
    r.mouse(ttgui::MouseKind::Down, h.x + 20, h.y);
    r.mouse(ttgui::MouseKind::Drag, h.x + 60, h.y);
    r.mouse(ttgui::MouseKind::Up, 159, h.y);
    REQUIRE(Rig::waitForFile(o));
    CHECK(has(Rig::readFile(o), "--chat user:100"));
  }
}

TEST_CASE("without a usable terminal the conversation opens as a floating window and says why", "[ui][tearoff][integration]") {
  Rig r(120, 34, "\"terminal\":{\"command\":\"/nonexistent/terminal {cmd}\"}");
  size_t before = r.app.app().notifications.size();
  r.app.shell().list->onDoubleClick.emit(2);
  r.settle();
  CHECK_EQ(r.app.floating().size(), (size_t)1);
  CHECK(r.app.app().notifications.size() > before);
}

TEST_CASE("keep_source=false closes the source pane after a tear-off", "[ui][tearoff][integration]") {
  Rig r(120, 34, std::string(kScriptTerminal) + ",\"tearoff\":{\"keep_source\":false}");
  r.fakeTerminal(r.tmp.path + "/argv.txt");
  FloatingChat* f = r.app.openFloating({PeerKind::User, 101});
  REQUIRE(f != nullptr);
  r.settle();
  std::string err;
  CHECK(r.app.openNewWindow({PeerKind::User, 101}, err));
  r.settle();
  r.app.app().onTickExtra();
  CHECK(r.app.floating().empty());
  r.openAlice();
  CHECK(r.app.openNewWindow({PeerKind::User, 100}, err));
  CHECK(!r.app.shell().showChat);
}

// ------------------------------------------------------------------ floating windows

TEST_CASE("floating chat windows open, show history, and stay in sync with the main window and each other", "[ui][floating][integration]") {
  Rig r;
  r.openAlice();
  FloatingChat* a = r.app.openFloating({PeerKind::User, 100});
  FloatingChat* b = r.app.openFloating({PeerKind::User, 100});
  FloatingChat* other = r.app.openFloating({PeerKind::User, 101});
  REQUIRE(a != nullptr); REQUIRE(b != nullptr); REQUIRE(other != nullptr);
  r.settle();
  CHECK_EQ(r.app.floating().size(), (size_t)3);
  CHECK(r.hasWindow("ChatWindow"));
  CHECK(has(r.screen(), "Message 40 in Alice"));
  auto count = [](ChatSession& s, const std::string& text) { int n = 0; for (auto& m : s.surface().view->msgs) if (m.text == text) n++; return n; };

  // a message sent from one window appears exactly once in all windows of that chat, and not in the other chat
  a->session().send("from window A");
  r.settle();
  CHECK_EQ(count(a->session(), "from window A"), 1);
  CHECK_EQ(count(b->session(), "from window A"), 1);
  CHECK_EQ(count(r.session(), "from window A"), 1);
  CHECK_EQ(count(other->session(), "from window A"), 0);
  // an incoming message reaches every window of the chat
  r.svc.simulateIncoming({PeerKind::User, 100}, "to everybody");
  r.settle();
  CHECK_EQ(count(a->session(), "to everybody"), 1);
  CHECK_EQ(count(b->session(), "to everybody"), 1);
  CHECK_EQ(count(r.session(), "to everybody"), 1);
  CHECK_EQ(r.svc.dialogs()[1].unread, 0);  // visible somewhere: not counted as unread
  // typing, read state and deletion
  js::Object t;
  t["peer"] = js::Value("user:100"); t["name"] = js::Value("Alice"); t["action"] = js::Value("typing");
  r.app.controller().onEvent({"typing", js::Value(t)});
  r.settle();
  CHECK_EQ(a->session().surface().typing->who, std::string("Alice"));
  CHECK_EQ(b->session().surface().typing->who, std::string("Alice"));
  CHECK_EQ(r.app.shell().typing->who, std::string("Alice"));
  CHECK(other->session().surface().typing->who.empty());
  long long mine = 0;
  for (auto& m : a->session().surface().view->msgs) if (m.outgoing) mine = m.id;
  js::Object rd;
  rd["peer"] = js::Value("user:100"); rd["max_id"] = js::id(mine); rd["outgoing"] = js::Value(true);
  r.app.controller().onEvent({"message.read", js::Value(rd)});
  r.settle();
  for (ChatSession* s : {&a->session(), &b->session(), &r.session()})
    for (auto& m : s->surface().view->msgs) if (m.id == mine) CHECK_EQ(m.state, 2);
  a->session().deleteMessage(mine);
  r.settle();
  for (ChatSession* s : {&a->session(), &b->session(), &r.session()})
    for (auto& m : s->surface().view->msgs) CHECK(m.id != mine);
}

TEST_CASE("a draft typed in a floating window appears in the other windows of that chat", "[ui][floating][integration]") {
  Rig r;
  r.openAlice();
  FloatingChat* a = r.app.openFloating({PeerKind::User, 100});
  REQUIRE(a != nullptr);
  r.settle();
  r.app.app().setFocused(a->pane().composer);
  r.typeInto(*a->pane().composer, "typed in the float");
  CHECK_EQ(r.svc.dialogs()[1].draft, std::string("typed in the float"));
  CHECK_EQ(r.composer().editor->getText(), std::string("typed in the float"));  // the unfocused main composer follows
  // and a window opened later starts with the stored draft
  FloatingChat* late = r.app.openFloating({PeerKind::User, 100});
  r.settle();
  CHECK_EQ(late->pane().composer->editor->getText(), std::string("typed in the float"));
}

TEST_CASE("closing a floating window releases its session; events afterwards are harmless", "[ui][floating][integration]") {
  Rig r;
  FloatingChat* a = r.app.openFloating({PeerKind::User, 100});
  REQUIRE(a != nullptr);
  r.settle();
  ttgui::Window* w = a->window();
  r.app.app().wm.closeWindow(w);  // the user clicks the close box
  r.app.app().wm.processPendingCloses();
  r.app.app().onTickExtra();
  CHECK(r.app.floating().empty());
  r.svc.simulateIncoming({PeerKind::User, 100}, "nobody listens");
  r.settle();
  FloatingChat* b = r.app.openFloating({PeerKind::User, 101});
  r.app.closeFloating(b);
  r.app.app().wm.processPendingCloses();
  r.app.app().onTickExtra();
  CHECK(r.app.floating().empty());
  r.svc.simulateIncoming({PeerKind::User, 101}, "still nobody");
  r.settle();
  CHECK(has(r.screen(), "nobody listens"));  // the visual bell may be flashing the menu row; the chat list shows the new preview
}

TEST_CASE("floating windows move, resize, snap and appear in the task list", "[ui][floating][integration]") {
  Rig r;
  FloatingChat* a = r.app.openFloating({PeerKind::User, 100}, nullptr);
  FloatingChat* b = r.app.openFloating({PeerKind::User, 101}, nullptr);
  REQUIRE(a != nullptr); REQUIRE(b != nullptr);
  auto tasks = r.app.app().wm.tasklist();
  int chat = 0;
  for (auto* t : tasks) if (t->appName == "ChatWindow") chat++;
  CHECK_EQ(chat, 2);
  ttgui::Window* w = a->window();
  ttgui::Rect before = w->bounds;
  r.app.app().wm.startDrag(w, ttgui::Point{before.x + 5, before.y});
  r.app.app().wm.updateDrag(ttgui::Point{before.x + 25, before.y + 3}, false);
  r.app.app().wm.endDrag();
  CHECK_EQ(w->bounds.x, before.x + 20);
  CHECK_EQ(w->bounds.y, before.y + 3);
  ttgui::Rect sized = w->bounds;
  r.app.app().wm.startResize(w, ttgui::Point{sized.right() - 1, sized.bottom() - 1});
  r.app.app().wm.updateDrag(ttgui::Point{sized.right() + 9, sized.bottom() + 3}, false);
  r.app.app().wm.endDrag();
  CHECK_EQ(w->bounds.w, sized.w + 10);
  CHECK_EQ(w->bounds.h, sized.h + 4);
  // snapping to the left edge of the work area
  r.app.app().wm.startDrag(w, ttgui::Point{w->bounds.x + 3, w->bounds.y});
  r.app.app().wm.updateDrag(ttgui::Point{2, w->bounds.y}, true);
  r.app.app().wm.endDrag();
  CHECK_EQ(w->bounds.x, r.app.app().wm.workArea.x);
  // raising by focus
  r.app.app().wm.focusWindow(b->window());
  CHECK(r.app.app().wm.windows.back().get() == b->window());
}

TEST_CASE("floating windows are restored after a restart with their size and place", "[ui][floating][integration]") {
  Rig r;
  ttgui::Rect where{6, 4, 52, 16};
  FloatingChat* a = r.app.openFloating({PeerKind::User, 100}, &where);
  FloatingChat* b = r.app.openFloating({PeerKind::Chat, 200}, nullptr);
  REQUIRE(a != nullptr); REQUIRE(b != nullptr);
  r.app.saveWindows();
  MologramApp second(r.cfg, r.svc, Rig::opts(r.tmp.path, ""));
  second.build(120, 34);
  for (int i = 0; i < 6; i++) second.dispatcher().drain();
  REQUIRE(second.floating().size() == 2);
  ttgui::Window* w = second.floating()[0]->window();
  CHECK_EQ(w->bounds.x, 6);
  CHECK_EQ(w->bounds.y, 4);
  CHECK_EQ(w->bounds.w, 52);
  CHECK_EQ(w->bounds.h, 16);
  CHECK_EQ(second.floating()[1]->peer().key(), std::string("chat:200"));
  // a closed window is not restored
  r.app.closeFloating(a);
  r.app.app().wm.processPendingCloses();
  MologramApp third(r.cfg, r.svc, Rig::opts(r.tmp.path, ""));
  third.build(120, 34);
  CHECK_EQ(third.floating().size(), (size_t)1);
}

TEST_CASE("float_chat opens the active conversation in a floating window by key and by menu", "[ui][floating][integration]") {
  Rig r;
  r.openAlice();
  r.press("Alt+T");
  REQUIRE(r.app.floating().size() == 1);
  CHECK_EQ(r.app.floating()[0]->peer().id, (std::int64_t)100);
  r.app.shell().list->onContextRow.emit(2, ttgui::Point{3, 3});
  for (auto& it : r.app.shell().ctx->items) if (it.label == "Open floating window") it.action();
  r.settle();
  CHECK_EQ(r.app.floating().size(), (size_t)2);
  // actions apply to the focused floating window's conversation
  r.app.app().wm.focusWindow(r.app.floating()[1]->window());
  r.app.app().setFocused(r.app.floating()[1]->pane().composer);
  CHECK_EQ(r.app.context().activeSession()->peer().id, (std::int64_t)101);
  r.action("close_window");
  r.app.app().wm.processPendingCloses();
  r.app.app().onTickExtra();
  CHECK_EQ(r.app.floating().size(), (size_t)1);
}

TEST_CASE("Premium three ways inside a floating window: a custom emoji reaction", "[ui][floating][premium][integration]") {
  Rig r;
  FloatingChat* f = r.app.openFloating({PeerKind::User, 100});
  r.settle();
  long long mid = f->session().target();
  REQUIRE(mid != 0);
  Reaction custom;
  custom.customEmojiId = 5001;
  r.svc.setPremium(mologram::PremiumState::NotPremium);
  r.settle();
  f->session().react(mid, custom, true);
  r.settle();
  CHECK(r.hasWindow("PremiumPanel"));  // explained, nothing sent
  r.svc.setPremium(mologram::PremiumState::Premium);
  r.settle();
  f->session().react(mid, custom, true);
  r.settle();
  bool has5001 = false;
  for (auto& m : r.svc.history({PeerKind::User, 100})) if (m.id == mid) for (auto& rc : m.reactions) if (rc.customEmojiId == 5001) has5001 = true;
  CHECK(has5001);
}

// ------------------------------------------------------------------ chat-only mode (the torn-off process)

TEST_CASE("chat-only mode shows one conversation without the chat list and follows live events", "[ui][tearoff][integration]") {
  for (auto sz : {std::make_pair(40, 15), std::make_pair(80, 24), std::make_pair(160, 50)}) {
    Rig r(sz.first, sz.second, "", "user:100");
    REQUIRE(r.app.chatOnly());
    REQUIRE(r.app.floating().size() == 1);
    std::string s = r.screen();
    CHECK(has(s, "Message 40 in Alice"));
    CHECK(!has(s, "Saved Messages"));
    CHECK(!has(s, "Bob"));
    r.svc.simulateIncoming({PeerKind::User, 100}, "live in the child");
    r.settle();
    CHECK(has(r.screen(), "live in the child"));
  }
  Rig r(100, 30, "", "user:100");
  r.action("close_window");  // the single window of a torn-off process is not closable by the action
  r.app.app().wm.processPendingCloses();
  CHECK_EQ(r.app.floating().size(), (size_t)1);
  r.app.saveWindows();
  PrefsStore prefs(r.tmp.path + "/prefs.json");
  std::string err;
  prefs.load(err);
  CHECK(prefs.windows().empty());  // a torn-off process never overwrites the main window's layout
  r.app.app().running = true;
  r.press("Ctrl+Q");
  CHECK(!r.app.app().running);
}

TEST_CASE("a conversation opened before the daemon connection was up loads when it connects", "[ui][tearoff][integration]") {
  Rig r(100, 30, "", "user:100");  // the torn-off process: its first requests may fail while the socket connects
  REQUIRE(r.app.floating().size() == 1);
  auto& msgs = r.app.floating()[0]->session().surface().view->msgs;
  msgs.clear();                    // as if the history requests had been refused
  r.app.floating()[0]->session().surface().composer->editor->setText("typed meanwhile");
  js::Object d;
  d["state"] = js::Value("connected");
  r.app.controller().onEvent({"connection.state", js::Value(d)});
  r.settle();
  CHECK(!r.app.floating()[0]->session().surface().view->msgs.empty());
  CHECK_EQ(r.app.floating()[0]->session().surface().composer->editor->getText(), std::string("typed meanwhile"));
  Rig m;
  m.openAlice();
  m.session().surface().view->msgs.clear();
  m.app.controller().onEvent({"connection.state", js::Value(d)});
  m.settle();
  CHECK(!m.session().surface().view->msgs.empty());
}
30.5ms