//! The pseudo-terminal, driven against `/bin/sh`. //! //! `/bin/sh` rather than either agent: this suite has to prove that a real //! terminal was allocated, that the child sees its size, that typing reaches //! it and that an exit is reported — and none of that needs a language model //! or a person's subscription. Nothing here ever spawns `claude` or `codex`. use std::collections::BTreeMap; use std::time::Duration; use cotton_session_host::pty::{PtySession, SpawnPlan, StopSchedule, StoppedBy}; use tokio::sync::mpsc; fn plan(script: &str, cols: u16, rows: u16) -> SpawnPlan { SpawnPlan { argv: vec![ "/bin/sh".to_string(), "-c".to_string(), script.to_string(), ], // A terminal with no TERM is a terminal a program refuses to draw on. env: BTreeMap::from([("TERM".to_string(), "xterm-256color".to_string())]), cwd: "/".to_string(), cols, rows, } } /// Everything the child wrote, until it hung up or `limit` passed. async fn drain(mut bytes: mpsc::UnboundedReceiver>, limit: Duration) -> String { let mut seen = Vec::new(); let _ = tokio::time::timeout(limit, async { while let Some(chunk) = bytes.recv().await { seen.extend_from_slice(&chunk); } }) .await; String::from_utf8_lossy(&seen).to_string() } #[tokio::test] async fn a_child_writes_into_the_pseudo_terminal_and_the_host_reads_it_back() { let (output, bytes) = mpsc::unbounded_channel(); let session = PtySession::spawn(&plan("echo hello-from-the-pty", 80, 24), output) .expect("a pseudo-terminal opens"); let said = drain(bytes, Duration::from_secs(10)).await; assert!( said.contains("hello-from-the-pty"), "the child's output reached the host — got {said:?}" ); assert_eq!(session.wait().await.code, Some(0)); } #[tokio::test] async fn the_child_is_really_on_a_terminal_and_not_on_a_pipe() { let (output, bytes) = mpsc::unbounded_channel(); // `test -t 0` is the question a program asks before it draws a TUI. On a // pipe this prints `no`, and every agent this host runs would fall back to // its non-interactive mode. let _session = PtySession::spawn( &plan("if [ -t 0 ]; then echo on-a-tty; else echo on-a-pipe; fi", 80, 24), output, ) .expect("a pseudo-terminal opens"); let said = drain(bytes, Duration::from_secs(10)).await; assert!(said.contains("on-a-tty"), "got {said:?}"); assert!(!said.contains("on-a-pipe"), "got {said:?}"); } #[tokio::test] async fn the_child_sees_the_size_it_was_spawned_with() { let (output, bytes) = mpsc::unbounded_channel(); let _session = PtySession::spawn(&plan("stty size", 132, 43), output) .expect("a pseudo-terminal opens"); let said = drain(bytes, Duration::from_secs(10)).await; assert!( said.contains("43 132"), "`stty size` prints rows then columns — got {said:?}" ); } #[tokio::test] async fn a_resize_reaches_the_child_that_was_already_running() { let (output, bytes) = mpsc::unbounded_channel(); // The shell reads a line first, so the resize lands before `stty size` // runs; the write below is what releases it. let session = PtySession::spawn(&plan("read line; stty size", 80, 24), output) .expect("a pseudo-terminal opens"); session.resize(100, 30).await.expect("the window resizes"); session.write(b"go\n").await.expect("the keystrokes land"); let said = drain(bytes, Duration::from_secs(10)).await; assert!( said.contains("30 100"), "the child saw the new window — got {said:?}" ); } #[tokio::test] async fn what_the_host_types_reaches_the_child() { let (output, bytes) = mpsc::unbounded_channel(); let session = PtySession::spawn(&plan("read line; echo \"you-said-$line\"", 80, 24), output) .expect("a pseudo-terminal opens"); session .write(b"cotton\n") .await .expect("the keystrokes land"); let said = drain(bytes, Duration::from_secs(10)).await; assert!(said.contains("you-said-cotton"), "got {said:?}"); } #[tokio::test] async fn an_exit_code_comes_back_and_a_hangup_ends_the_stream_rather_than_erroring() { let (output, bytes) = mpsc::unbounded_channel(); let session = PtySession::spawn(&plan("exit 7", 80, 24), output) .expect("a pseudo-terminal opens"); assert_eq!(session.wait().await.code, Some(7)); // macOS answers EIO on the master once the child has gone; Linux answers // end of file. Either way the reader task ends and the channel closes, // which is what this drain returning proves. let said = drain(bytes, Duration::from_secs(10)).await; assert!(said.len() < 1024, "an exiting shell says almost nothing"); } #[tokio::test] async fn a_stop_ends_a_child_that_takes_the_interrupt() { let (output, _bytes) = mpsc::unbounded_channel(); // `exec`, so the signal lands on the process it was aimed at rather than // on a shell whose answer to SIGINT is its own: `bash` re-raises it and // dies by signal 2, `dash` reports 130 as an exit code. let session = PtySession::spawn(&plan("exec sleep 30", 80, 24), output) .expect("a pseudo-terminal opens"); let stopped = session .stop(StopSchedule { interrupt_grace: Duration::from_secs(5), terminate_grace: Duration::from_secs(5), }) .await .expect("the stop runs"); assert_eq!(stopped, StoppedBy::Interrupt); assert_eq!(session.wait().await.signal, Some(2)); } #[tokio::test] async fn a_stop_reaches_for_the_next_signal_when_the_child_ignores_the_first() { let (output, _bytes) = mpsc::unbounded_channel(); // A shell that traps SIGINT and SIGTERM and keeps going: only SIGKILL // ends it, which is the whole reason the schedule has three steps. let session = PtySession::spawn( &plan("trap '' INT TERM; while true; do sleep 1; done", 80, 24), output, ) .expect("a pseudo-terminal opens"); // Let the shell install the trap before the first signal arrives. tokio::time::sleep(Duration::from_millis(300)).await; let stopped = session .stop(StopSchedule { interrupt_grace: Duration::from_millis(300), terminate_grace: Duration::from_millis(300), }) .await .expect("the stop runs"); assert_eq!(stopped, StoppedBy::Kill); assert_eq!(session.wait().await.signal, Some(9)); } #[tokio::test] async fn stopping_a_child_that_has_already_gone_says_so_rather_than_signalling_a_stranger() { let (output, _bytes) = mpsc::unbounded_channel(); let session = PtySession::spawn(&plan("exit 0", 80, 24), output) .expect("a pseudo-terminal opens"); session.wait().await; assert_eq!( session .stop(StopSchedule::default()) .await .expect("the stop runs"), StoppedBy::AlreadyGone ); } #[tokio::test] async fn every_caller_that_waits_gets_the_same_answer() { let (output, _bytes) = mpsc::unbounded_channel(); let session = PtySession::spawn(&plan("exit 3", 80, 24), output) .expect("a pseudo-terminal opens"); let first = session.wait().await; let second = session.wait().await; assert_eq!(first, second); assert_eq!(first.code, Some(3)); assert_eq!(session.finished(), Some(first)); }