// Real processes: `mologram --chat KEY` children in pseudo-terminals attached to one in-process daemon (FakeTelegramService behind a // unix socket). Proves tear-off windows stay live and in sync across processes, and that the terminal is restored when the child // ends by quit, SIGTERM or a crash. SPEC FR-73, NFR-02, A-02. #include #include #include #include #include #include #include #include #include "tttest.hpp" #include "ptychild.hpp" #include "mologram/Daemon.hpp" #include "mologram/FakeTelegramService.hpp" using namespace mologram; using ptytest::PtyChild; using ptytest::binDir; using ptytest::restored; namespace { struct TempDir { std::string path; TempDir() { char t[] = "/tmp/mologram-pty-XXXXXX"; path = mkdtemp(t); } ~TempDir() { std::error_code ec; std::filesystem::remove_all(path, ec); } }; struct DaemonRun { FakeTelegramService svc; Daemon daemon; std::thread th; explicit DaemonRun(const std::string& sock) : daemon(svc, sock) {} bool start() { if (!daemon.start().empty()) return false; th = std::thread([this]() { daemon.run(); }); return true; } ~DaemonRun() { daemon.stop(); if (th.joinable()) th.join(); } }; std::string writeConfig(const std::string& dir) { std::string path = dir + "/cfg.json"; std::ofstream f(path); f << "{\"api_id\":1,\"api_hash\":\"x\",\"phone\":\"+1\",\"data_dir\":\"" << dir << "\"}"; return path; } std::vector childArgs(const std::string& dir, const std::string& sock, const std::string& chat) { return {binDir() + "/mologram", "--chat", chat, "--socket", sock, "--config", writeConfig(dir)}; } } // namespace TEST_CASE("a torn-off process shows its conversation, follows live events and restores the terminal on quit", "[process][tearoff][integration]") { TempDir d; std::string sock = d.path + "/m.sock"; DaemonRun run(sock); REQUIRE(run.start()); PtyChild c; REQUIRE(c.start(childArgs(d.path, sock, "user:100"))); REQUIRE(c.waitFor("Message 40 in Alice")); CHECK(c.out.find("\x1b[?1049h") != std::string::npos); // alternate screen entered run.svc.simulateIncoming({PeerKind::User, 100}, "arrived in the child"); CHECK(c.waitFor("arrived in the child")); c.write("\x11"); // Ctrl+Q int status = c.wait(); REQUIRE(status != -1); CHECK(WIFEXITED(status)); CHECK_EQ(WEXITSTATUS(status), 0); CHECK(restored(c.out)); } TEST_CASE("two terminals on one daemon stay in sync: a message typed in one shows up in the other and in the service", "[process][tearoff][integration]") { TempDir d; std::string sock = d.path + "/m.sock"; DaemonRun run(sock); REQUIRE(run.start()); PtyChild a, b; REQUIRE(a.start(childArgs(d.path, sock, "user:100"))); REQUIRE(b.start(childArgs(d.path, sock, "user:100"))); REQUIRE(a.waitFor("Message 40 in Alice")); REQUIRE(b.waitFor("Message 40 in Alice")); a.write("from terminal one\r"); bool shownInA = a.waitFor("from terminal one"); bool shownInB = b.waitFor("from terminal one"); CHECK(shownInA); CHECK(shownInB); if (!shownInA || !shownInB) { std::fprintf(stderr, "---- terminal A ----\n%s\n---- terminal B ----\n%s\n", a.screen().c_str(), b.screen().c_str()); } bool inService = false; for (int i = 0; i < 100 && !inService; i++) { for (auto& m : run.svc.history({PeerKind::User, 100})) if (m.text == "from terminal one") inService = true; if (!inService) { a.pump(20); } } CHECK(inService); // a message from the other side reaches both run.svc.simulateIncoming({PeerKind::User, 100}, "to both terminals"); CHECK(a.waitFor("to both terminals")); CHECK(b.waitFor("to both terminals")); a.write("\x11"); b.write("\x11"); CHECK(a.wait() != -1); CHECK(b.wait() != -1); CHECK(restored(a.out)); CHECK(restored(b.out)); } TEST_CASE("SIGTERM and SIGHUP end a torn-off process normally with the terminal restored", "[process][tearoff][integration]") { for (int sig : {SIGTERM, SIGHUP}) { TempDir d; std::string sock = d.path + "/m.sock"; DaemonRun run(sock); REQUIRE(run.start()); PtyChild c; REQUIRE(c.start(childArgs(d.path, sock, "user:100"))); REQUIRE(c.waitFor("Message 40 in Alice")); ::kill(c.pid, sig); int status = c.wait(); REQUIRE(status != -1); CHECK(WIFEXITED(status)); CHECK(restored(c.out)); } } TEST_CASE("a crash of a torn-off process still resets its terminal and dies with the crash signal", "[process][tearoff][integration]") { TempDir d; std::string sock = d.path + "/m.sock"; DaemonRun run(sock); REQUIRE(run.start()); PtyChild c; REQUIRE(c.start(childArgs(d.path, sock, "user:100"))); REQUIRE(c.waitFor("Message 40 in Alice")); ::kill(c.pid, SIGSEGV); int status = c.wait(); REQUIRE(status != -1); CHECK(WIFSIGNALED(status)); CHECK_EQ(WTERMSIG(status), (int)SIGSEGV); CHECK(restored(c.out)); // the guard wrote the reset sequence before dying } TEST_CASE("the daemon keeps serving when a torn-off process dies", "[process][tearoff][integration]") { TempDir d; std::string sock = d.path + "/m.sock"; DaemonRun run(sock); REQUIRE(run.start()); PtyChild a, b; REQUIRE(a.start(childArgs(d.path, sock, "user:100"))); REQUIRE(b.start(childArgs(d.path, sock, "user:101"))); REQUIRE(a.waitFor("Message 40 in Alice")); REQUIRE(b.waitFor("Message 40 in Bob")); ::kill(a.pid, SIGKILL); // no chance to clean up a.wait(); run.svc.simulateIncoming({PeerKind::User, 101}, "bob still hears me"); CHECK(b.waitFor("bob still hears me")); b.write("\x11"); CHECK(b.wait() != -1); }