// test_pty.cpp - ยง13.3.10 PTY tier [pty][slow] (skips cleanly if PTY unavailable)
#include "../tttest.hpp"
#include "../ttgui.hpp"
using namespace ttgui;
static bool waitFor(TerminalWidget& t,const std::string& needle,int ms=3000){
for(int elapsed=0; elapsed<ms; elapsed+=20){ t.pollInput();
std::string all; for(auto& row: t.vt.grid){ for(auto&c: row) all+=c.ch; all+="\n"; }
if(all.find(needle)!=std::string::npos) return true;
usleep(20000); }
return false;
}
TEST_CASE("pty: spawn /bin/sh echo round-trip", "[pty][slow][terminal]"){
TerminalWidget t(40,10);
if(!t.start("/bin/sh")) SKIP_TEST("PTY unavailable (openpty failed)");
t.sendStr("echo rgui-pty-ok\n");
CHECK(waitFor(t,"rgui-pty-ok"));
t.sendStr("exit\n");
}
TEST_CASE("pty: child resize reflects widget size, exit state", "[pty][slow][terminal]"){
TerminalWidget t(50,12);
if(!t.start("/bin/sh")) SKIP_TEST("PTY unavailable");
t.vt.resize(33,9); t.resizePTY();
t.sendStr("stty size\n");
bool ok=waitFor(t,"9 33",3000);
CHECK(ok);
t.sendStr("exit 3\n");
for(int i=0;i<100 && !t.exited;i++){ t.pollInput(); usleep(20000); }
CHECK(t.exited);
}
TEST_CASE("pty: two terminals concurrently not cross-talking", "[pty][slow][terminal]"){
TerminalWidget a(40,10), b(40,10);
if(!a.start("/bin/sh") || !b.start("/bin/sh")) SKIP_TEST("PTY unavailable");
a.sendStr("echo only-in-a\n"); b.sendStr("echo only-in-b\n");
CHECK(waitFor(a,"only-in-a")); CHECK(waitFor(b,"only-in-b"));
std::string btext; for(auto& row: b.vt.grid){ for(auto&c: row) btext+=c.ch; }
CHECK(btext.find("only-in-a")==std::string::npos);
a.sendStr("exit\n"); b.sendStr("exit\n");
}