use std::collections::HashMap;
use std::io::{Read, Write};
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use std::thread::{self, JoinHandle};
use std::time::{Duration, Instant};
use portable_pty::{native_pty_system, Child, CommandBuilder, MasterPty, PtySize};
use crate::terminal::cast::{ActivityClock, CastRecorder};
use crate::terminal::error::SessionError;
use crate::terminal::screen::TerminalScreen;
use crate::terminal::session::Session;
const EOT: u8 = 0x04;
const CLOSE_GRACE: Duration = Duration::from_millis(500);
const KEY_MAP: &[(&str, &str)] = &[
("enter", "\r"),
("escape", "\x1b"),
("tab", "\t"),
("backspace", "\x7f"),
("up", "\x1b[A"),
("down", "\x1b[B"),
("right", "\x1b[C"),
("left", "\x1b[D"),
];
#[derive(Debug, Clone)]
pub struct PtyConfig {
pub argv: Vec<String>,
pub cwd: Option<PathBuf>,
pub env: HashMap<String, String>,
pub cast_path: Option<PathBuf>,
}
impl PtyConfig {
pub fn new(argv: Vec<String>) -> Self {
Self {
argv,
cwd: None,
env: HashMap::new(),
cast_path: None,
}
}
#[must_use]
pub fn with_cwd(mut self, cwd: impl Into<PathBuf>) -> Self {
self.cwd = Some(cwd.into());
self
}
#[must_use]
pub fn with_env(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
self.env.insert(key.into(), value.into());
self
}
#[must_use]
pub fn with_cast_path(mut self, path: impl Into<PathBuf>) -> Self {
self.cast_path = Some(path.into());
self
}
}
fn launch_dir(config: &PtyConfig) -> Option<PathBuf> {
if let Some(dir) = config.cwd.as_deref().filter(|d| d.is_dir()) {
return Some(dir.to_path_buf());
}
config
.env
.get("HOME")
.map(PathBuf::from)
.or_else(|| std::env::var_os("HOME").map(PathBuf::from))
}
#[derive(Debug)]
pub enum PtyError {
EmptyArgv,
SpawnFailed(String),
Io(String),
NotStarted,
UnknownKey(String),
}
impl std::fmt::Display for PtyError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::EmptyArgv => write!(f, "argv must not be empty"),
Self::SpawnFailed(msg) => write!(f, "failed to spawn pty child: {msg}"),
Self::Io(msg) => write!(f, "pty I/O error: {msg}"),
Self::NotStarted => write!(f, "pty session has not been started"),
Self::UnknownKey(k) => write!(f, "unknown key: {k}"),
}
}
}
impl std::error::Error for PtyError {}
pub struct PtySession {
config: PtyConfig,
cols: u16,
rows: u16,
raw_output: Arc<Mutex<Vec<u8>>>,
screen: Arc<Mutex<TerminalScreen>>,
activity: Arc<Mutex<ActivityClock>>,
cast: Option<Arc<Mutex<CastRecorder>>>,
master: Option<Box<dyn MasterPty + Send>>,
writer: Option<Box<dyn Write + Send>>,
child: Option<Box<dyn Child + Send + Sync>>,
reader_handle: Option<JoinHandle<()>>,
exit_code: Option<i32>,
exit_signal: Option<String>,
screen_snapshot: String,
raw_snapshot: String,
cast_path_snapshot: PathBuf,
launched_in: Option<PathBuf>,
}
impl std::fmt::Debug for PtySession {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("PtySession")
.field("argv", &self.config.argv)
.field("cols", &self.cols)
.field("rows", &self.rows)
.field("exit_code", &self.exit_code)
.finish()
}
}
impl PtySession {
pub fn new(config: PtyConfig, cols: u16, rows: u16) -> Result<Self, PtyError> {
if config.argv.is_empty() {
return Err(PtyError::EmptyArgv);
}
if cols == 0 || rows == 0 {
return Err(PtyError::SpawnFailed("cols and rows must be > 0".into()));
}
let screen = TerminalScreen::new(cols, rows);
let cast_path_snapshot = config.cast_path.clone().unwrap_or_default();
Ok(Self {
config,
cols,
rows,
raw_output: Arc::new(Mutex::new(Vec::new())),
screen: Arc::new(Mutex::new(screen)),
activity: Arc::new(Mutex::new(ActivityClock::new())),
cast: None,
master: None,
writer: None,
child: None,
reader_handle: None,
exit_code: None,
exit_signal: None,
screen_snapshot: String::new(),
raw_snapshot: String::new(),
cast_path_snapshot,
launched_in: None,
})
}
pub fn from_parts(
argv: Vec<String>,
cwd: Option<PathBuf>,
env: HashMap<String, String>,
cols: u16,
rows: u16,
) -> Result<Self, PtyError> {
Self::new(
PtyConfig {
argv,
cwd,
env,
cast_path: None,
},
cols,
rows,
)
}
pub fn spawn(&mut self) -> Result<(), PtyError> {
if self.child.is_some() {
return Ok(());
}
if let Some(path) = self.config.cast_path.clone() {
if let Ok(rec) = CastRecorder::new(path, self.cols, self.rows, self.config.argv.clone())
{
self.cast = Some(Arc::new(Mutex::new(rec)));
}
}
let pair = native_pty_system()
.openpty(PtySize {
rows: self.rows,
cols: self.cols,
pixel_width: 0,
pixel_height: 0,
})
.map_err(|e| PtyError::SpawnFailed(e.to_string()))?;
let mut cmd = CommandBuilder::new(&self.config.argv[0]);
cmd.args(&self.config.argv[1..]);
if let Some(cwd) = &self.config.cwd {
cmd.cwd(cwd);
}
for (k, v) in &self.config.env {
cmd.env(k, v);
}
if std::env::var("TERM").unwrap_or_default().is_empty()
&& !self.config.env.contains_key("TERM")
{
cmd.env("TERM", "xterm-256color");
}
let child = pair
.slave
.spawn_command(cmd)
.map_err(|e| PtyError::SpawnFailed(e.to_string()))?;
drop(pair.slave);
let reader = pair
.master
.try_clone_reader()
.map_err(|e| PtyError::Io(e.to_string()))?;
let writer = pair
.master
.take_writer()
.map_err(|e| PtyError::Io(e.to_string()))?;
let raw_out = Arc::clone(&self.raw_output);
let screen_out = Arc::clone(&self.screen);
let activity_out = Arc::clone(&self.activity);
let cast_out = self.cast.clone();
let reader_handle = thread::spawn(move || {
let mut reader = reader;
let mut buf = [0u8; 4096];
loop {
match reader.read(&mut buf) {
Ok(0) | Err(_) => break,
Ok(n) => {
let chunk = &buf[..n];
if let Ok(mut r) = raw_out.lock() {
r.extend_from_slice(chunk);
}
if let Ok(mut s) = screen_out.lock() {
s.feed_bytes(chunk);
}
if let Ok(mut a) = activity_out.lock() {
a.mark();
}
if let Some(c) = cast_out.as_ref() {
if let Ok(mut rec) = c.lock() {
rec.record_output(&String::from_utf8_lossy(chunk));
}
}
}
}
}
});
self.writer = Some(writer);
self.master = Some(pair.master);
self.child = Some(child);
self.reader_handle = Some(reader_handle);
self.launched_in = launch_dir(&self.config);
Ok(())
}
pub fn raw_output(&self) -> Vec<u8> {
self.raw_output
.lock()
.map(|r| r.clone())
.unwrap_or_default()
}
pub fn raw_output_len(&self) -> usize {
self.raw_output.lock().map(|r| r.len()).unwrap_or_default()
}
pub fn raw_output_str(&self) -> String {
String::from_utf8_lossy(&self.raw_output()).to_string()
}
pub fn screen_text(&self) -> String {
self.screen.lock().map(|s| s.contents()).unwrap_or_default()
}
pub fn screen(&self) -> String {
self.screen_text()
}
pub fn exit_code(&self) -> Option<i32> {
self.exit_code
}
pub fn exit_signal(&self) -> Option<&str> {
self.exit_signal.as_deref()
}
pub fn process_id(&self) -> Option<u32> {
self.child.as_ref().and_then(|c| c.process_id())
}
pub fn is_alive(&mut self) -> bool {
let status = match self.child.as_mut() {
Some(child) => child.try_wait(),
None => return false,
};
match status {
Ok(None) => true,
Ok(Some(status)) => {
if self.exit_code.is_none() {
self.exit_code = Some(i32::try_from(status.exit_code()).unwrap_or(-1));
self.exit_signal = status.signal().map(str::to_string);
}
false
}
Err(_) => false,
}
}
pub fn send_bytes(&mut self, data: &[u8]) -> Result<(), PtyError> {
let writer = self.writer.as_mut().ok_or(PtyError::NotStarted)?;
writer
.write_all(data)
.map_err(|e| PtyError::Io(e.to_string()))?;
writer.flush().map_err(|e| PtyError::Io(e.to_string()))?;
if let Some(c) = self.cast.as_ref() {
if let Ok(mut rec) = c.lock() {
rec.record_input(&String::from_utf8_lossy(data));
}
}
Ok(())
}
pub fn send_text(&mut self, text: &str) -> Result<(), PtyError> {
self.send_bytes(text.as_bytes())
}
pub fn send_line(&mut self, text: &str) -> Result<(), PtyError> {
let mut buf = text.as_bytes().to_vec();
buf.push(b'\r');
self.send_bytes(&buf)
}
pub fn press(&mut self, key: &str) -> Result<(), PtyError> {
let normalized = key.to_ascii_lowercase();
if let Some(stripped) = normalized.strip_prefix("ctrl-") {
if let Some(ch) = stripped.chars().next() {
let lower = ch.to_ascii_lowercase() as u8;
if lower.is_ascii_lowercase() {
let ctrl = lower - b'a' + 1;
return self.send_bytes(&[ctrl]);
}
return Err(PtyError::UnknownKey(key.to_string()));
}
return Err(PtyError::UnknownKey(key.to_string()));
}
if let Some((_, seq)) = KEY_MAP.iter().find(|(k, _)| *k == normalized) {
return self.send_bytes(seq.as_bytes());
}
Err(PtyError::UnknownKey(key.to_string()))
}
pub fn send_eof(&mut self) -> Result<(), PtyError> {
if self.writer.is_some() {
self.send_bytes(&[EOT])?;
self.writer.take();
}
Ok(())
}
pub fn set_echo(&mut self, _enabled: bool) -> Result<(), PtyError> {
Ok(())
}
pub fn resize(&mut self, cols: u16, rows: u16) -> Result<(), PtyError> {
if cols == 0 || rows == 0 {
return Err(PtyError::SpawnFailed("cols and rows must be > 0".into()));
}
self.cols = cols;
self.rows = rows;
if let Some(master) = self.master.as_ref() {
master
.resize(PtySize {
rows,
cols,
pixel_width: 0,
pixel_height: 0,
})
.map_err(|e| PtyError::Io(e.to_string()))?;
}
if let Ok(mut s) = self.screen.lock() {
s.resize(cols, rows);
}
Ok(())
}
pub fn read_available(&mut self, timeout: Duration) {
if timeout.is_zero() {
thread::sleep(Duration::from_millis(5));
} else {
thread::sleep(timeout);
}
}
pub fn wait_for_text(&mut self, text: &str, timeout: Duration) -> Result<bool, PtyError> {
if self.child.is_none() {
return Err(PtyError::NotStarted);
}
let deadline = Instant::now() + timeout;
while Instant::now() < deadline {
self.read_available(Duration::from_millis(50));
let screen = self.screen_text();
let raw = self.raw_output_str();
if screen.contains(text) || raw.contains(text) {
return Ok(true);
}
if !self.is_alive() {
self.read_available(Duration::from_millis(20));
let screen = self.screen_text();
let raw = self.raw_output_str();
return Ok(screen.contains(text) || raw.contains(text));
}
}
Ok(false)
}
pub fn wait_for_idle(&mut self, stable: Duration, timeout: Duration) -> Result<bool, PtyError> {
if self.child.is_none() {
return Err(PtyError::NotStarted);
}
let deadline = Instant::now() + timeout;
let mut last_screen = self.screen_text();
let mut last_raw_len = self.raw_output_len();
let mut stable_since = Instant::now();
while Instant::now() < deadline {
self.read_available(Duration::from_millis(50));
let current = self.screen_text();
let current_raw_len = self.raw_output_len();
if current != last_screen || current_raw_len != last_raw_len {
last_screen = current;
last_raw_len = current_raw_len;
stable_since = Instant::now();
}
if stable_since.elapsed() >= stable {
return Ok(true);
}
if !self.is_alive() {
self.read_available(Duration::from_millis(20));
return Ok(true);
}
}
Ok(false)
}
pub fn wait_for_exit(&mut self, timeout: Duration) -> Result<Option<i32>, PtyError> {
if self.child.is_none() {
return Err(PtyError::NotStarted);
}
let deadline = Instant::now() + timeout;
while Instant::now() < deadline {
self.read_available(Duration::from_millis(50));
if !self.is_alive() {
return Ok(self.exit_code);
}
}
Ok(self.exit_code)
}
pub fn terminate(&mut self) -> Result<(), PtyError> {
if let Some(child) = self.child.as_mut() {
let _ = child.kill();
}
Ok(())
}
pub fn close(&mut self) {
if self.is_alive() {
let _ = self.terminate();
}
let deadline = Instant::now() + CLOSE_GRACE;
while self.is_alive() && Instant::now() < deadline {
thread::sleep(Duration::from_millis(10));
}
self.writer.take();
if let Some(handle) = self.reader_handle.take() {
let join_deadline = Instant::now() + CLOSE_GRACE;
while !handle.is_finished() && Instant::now() < join_deadline {
thread::sleep(Duration::from_millis(5));
}
if handle.is_finished() {
let _ = handle.join();
}
}
self.master.take();
self.child.take();
if let Some(c) = self.cast.take() {
if let Ok(mut rec) = c.lock() {
let _ = rec.finish();
}
}
}
fn sync_snapshot(&mut self) {
self.screen_snapshot = self.screen_text();
self.raw_snapshot = self.raw_output_str();
}
pub fn dimensions(&self) -> (u16, u16) {
(self.cols, self.rows)
}
pub fn cols(&self) -> u16 {
self.cols
}
pub fn rows(&self) -> u16 {
self.rows
}
}
impl From<PtyError> for SessionError {
fn from(err: PtyError) -> Self {
match err {
PtyError::EmptyArgv | PtyError::SpawnFailed(_) | PtyError::UnknownKey(_) => {
SessionError::Config(err.to_string())
}
PtyError::Io(msg) => SessionError::Io(msg),
PtyError::NotStarted => SessionError::NotStarted,
}
}
}
impl Session for PtySession {
fn send_text(&mut self, text: &str) -> Result<(), SessionError> {
PtySession::send_text(self, text)?;
self.sync_snapshot();
Ok(())
}
fn send_line(&mut self, text: &str) -> Result<(), SessionError> {
PtySession::send_line(self, text)?;
self.sync_snapshot();
Ok(())
}
fn press(&mut self, key: &str) -> Result<(), SessionError> {
PtySession::press(self, key)?;
self.sync_snapshot();
Ok(())
}
fn wait_for_text(&mut self, text: &str, timeout: Duration) -> Result<bool, SessionError> {
let found = PtySession::wait_for_text(self, text, timeout)?;
self.sync_snapshot();
Ok(found)
}
fn wait_for_idle(&mut self, stable: Duration, timeout: Duration) -> Result<bool, SessionError> {
let idle = PtySession::wait_for_idle(self, stable, timeout)?;
self.sync_snapshot();
Ok(idle)
}
fn wait_for_exit(&mut self, timeout: Duration) -> Result<Option<i32>, SessionError> {
let code = PtySession::wait_for_exit(self, timeout)?;
self.sync_snapshot();
Ok(code)
}
fn read_available(&mut self, timeout: Duration) -> Result<(), SessionError> {
PtySession::read_available(self, timeout);
self.sync_snapshot();
Ok(())
}
fn is_alive(&mut self) -> bool {
PtySession::is_alive(self)
}
fn close(&mut self) -> Result<(), SessionError> {
PtySession::close(self);
self.sync_snapshot();
Ok(())
}
fn screen(&self) -> &str {
&self.screen_snapshot
}
fn screen_attributed(&mut self) -> Option<crate::terminal::attributed::AttributedScreen> {
self.screen.lock().ok().map(|s| s.attributed())
}
fn raw_output(&self) -> &str {
&self.raw_snapshot
}
fn exit_code(&self) -> Option<i32> {
self.exit_code
}
fn cols(&self) -> u16 {
self.cols
}
fn rows(&self) -> u16 {
self.rows
}
fn argv(&self) -> &[String] {
&self.config.argv
}
fn cwd(&self) -> Option<&std::path::Path> {
self.launched_in.as_deref()
}
fn cast_path(&self) -> &std::path::Path {
&self.cast_path_snapshot
}
}
impl Drop for PtySession {
fn drop(&mut self) {
self.close();
}
}
#[cfg(test)]
mod tests {
use super::*;
use std::process::{Command, Stdio};
use std::time::Duration;
#[test]
fn spawns_and_captures_text() {
let config = PtyConfig::new(vec!["sh".into(), "-c".into(), "echo hello".into()]);
let mut sess = PtySession::new(config, 80, 24).expect("new");
sess.spawn().expect("spawn");
let found = sess
.wait_for_text("hello", Duration::from_secs(2))
.expect("wait");
assert!(found, "hello not found, screen={:?}", sess.screen_text());
sess.close();
assert!(sess.exit_code().is_some());
}
#[test]
fn unicode_is_preserved() {
let config = PtyConfig::new(vec![
"sh".into(),
"-c".into(),
"printf 'héllo 🌍\\n'".into(),
]);
let mut sess = PtySession::new(config, 80, 24).expect("new");
sess.spawn().expect("spawn");
let found = sess
.wait_for_text("héllo", Duration::from_secs(2))
.expect("wait");
assert!(found, "unicode not found: {:?}", sess.screen_text());
sess.close();
}
#[test]
fn resize_changes_dimensions() {
let config = PtyConfig::new(vec!["sh".into(), "-c".into(), "sleep 0.5".into()]);
let mut sess = PtySession::new(config, 80, 24).expect("new");
sess.spawn().expect("spawn");
sess.resize(40, 10).expect("resize");
assert_eq!(sess.dimensions(), (40, 10));
sess.resize(100, 30).expect("resize2");
assert_eq!(sess.dimensions(), (100, 30));
sess.close();
}
#[test]
fn send_text_and_wait() {
let config = PtyConfig::new(vec!["cat".into()]);
let mut sess = PtySession::new(config, 80, 24).expect("new");
sess.spawn().expect("spawn");
thread::sleep(Duration::from_millis(100));
sess.send_line("hello pty").expect("send");
let found = sess
.wait_for_text("hello pty", Duration::from_secs(2))
.expect("wait");
assert!(found, "echo not found: {:?}", sess.screen_text());
sess.close();
}
#[test]
fn wait_for_idle_returns_on_stable_screen() {
let config = PtyConfig::new(vec![
"sh".into(),
"-c".into(),
"echo hi; sleep 0.1; echo done".into(),
]);
let mut sess = PtySession::new(config, 80, 24).expect("new");
sess.spawn().expect("spawn");
let idle = sess
.wait_for_idle(Duration::from_millis(150), Duration::from_secs(2))
.expect("idle");
assert!(idle);
sess.close();
}
#[test]
fn press_unknown_key_is_error() {
let config = PtyConfig::new(vec!["sh".into(), "-c".into(), "sleep 0.1".into()]);
let mut sess = PtySession::new(config, 80, 24).expect("new");
sess.spawn().expect("spawn");
let err = sess.press("f13").unwrap_err();
assert!(matches!(err, PtyError::UnknownKey(_)));
sess.close();
}
#[test]
fn child_gets_a_controlling_tty() {
let config = PtyConfig::new(vec![
"sh".into(),
"-c".into(),
"test -t 0 && test -t 1 && echo HAS_TTY || echo NO_TTY".into(),
]);
let mut sess = PtySession::new(config, 80, 24).expect("new");
sess.spawn().expect("spawn");
let found = sess
.wait_for_text("HAS_TTY", Duration::from_secs(5))
.expect("wait");
assert!(found, "child had no tty: {:?}", sess.raw_output_str());
sess.close();
}
#[test]
fn resize_reaches_the_kernel() {
let config = PtyConfig::new(vec![
"sh".into(),
"-c".into(),
"sleep 0.4; stty size".into(),
]);
let mut sess = PtySession::new(config, 80, 24).expect("new");
sess.spawn().expect("spawn");
sess.resize(40, 10).expect("resize");
let found = sess
.wait_for_text("10 40", Duration::from_secs(5))
.expect("wait");
assert!(
found,
"child did not see the new size: {:?}",
sess.raw_output_str()
);
sess.close();
}
#[test]
fn idle_accounts_for_output_that_does_not_change_the_screen() {
let config = PtyConfig::new(vec![
"sh".into(),
"-c".into(),
"printf hello; i=0; while [ $i -lt 20 ]; do printf '\\033[1;1Hhello'; \
sleep 0.05; i=$((i+1)); done; sleep 5"
.into(),
]);
let mut sess = PtySession::new(config, 80, 24).expect("new");
sess.spawn().expect("spawn");
let start = Instant::now();
let idle = sess
.wait_for_idle(Duration::from_millis(300), Duration::from_secs(10))
.expect("wait");
let elapsed = start.elapsed();
sess.close();
assert!(idle, "never reported idle");
assert!(
elapsed >= Duration::from_millis(800),
"reported idle after {elapsed:?} while the child was still \
repainting; screen-only comparison missed the raw output"
);
}
#[test]
fn stderr_is_merged_into_the_terminal_stream() {
let config = PtyConfig::new(vec![
"sh".into(),
"-c".into(),
"echo out; echo err 1>&2".into(),
]);
let mut sess = PtySession::new(config, 80, 24).expect("new");
sess.spawn().expect("spawn");
assert!(sess
.wait_for_text("err", Duration::from_secs(5))
.expect("wait"));
sess.close();
let screen = sess.screen_text();
assert!(screen.contains("out"), "stdout missing: {screen:?}");
assert!(screen.contains("err"), "stderr missing: {screen:?}");
}
#[cfg(unix)]
#[test]
fn drop_terminates_the_child_promptly() {
let config = PtyConfig::new(vec![
"sh".into(),
"-c".into(),
"echo READY; sleep 30".into(),
]);
let mut sess = PtySession::new(config, 80, 24).expect("new");
sess.spawn().expect("spawn");
assert!(sess
.wait_for_text("READY", Duration::from_secs(5))
.expect("wait"));
let pid = sess.process_id().expect("pid");
let start = Instant::now();
drop(sess);
let elapsed = start.elapsed();
assert!(
elapsed < Duration::from_secs(5),
"Drop blocked for {elapsed:?}"
);
thread::sleep(Duration::from_millis(200));
let alive = Command::new("kill")
.args(["-0", &pid.to_string()])
.stdout(Stdio::null())
.stderr(Stdio::null())
.status()
.map(|s| s.success())
.unwrap_or(false);
assert!(!alive, "child {pid} survived Drop");
}
#[cfg(unix)]
#[test]
fn a_signalled_child_reports_the_signal_by_name() {
let config = PtyConfig::new(vec![
"sh".into(),
"-c".into(),
"echo READY; sleep 30".into(),
]);
let mut sess = PtySession::new(config, 80, 24).expect("new");
sess.spawn().expect("spawn");
assert!(sess
.wait_for_text("READY", Duration::from_secs(5))
.expect("wait"));
sess.terminate().expect("terminate");
let deadline = Instant::now() + Duration::from_secs(5);
while sess.is_alive() && Instant::now() < deadline {
thread::sleep(Duration::from_millis(10));
}
let signal = sess.exit_signal();
assert!(
signal.is_some_and(|s| !s.is_empty()),
"child was killed by a signal but reported none; exit_code={:?}",
sess.exit_code()
);
sess.close();
}
#[test]
fn cast_is_written_when_path_given() {
let dir = std::env::temp_dir().join(format!("termproof-pty-cast-{}", std::process::id()));
let _ = std::fs::create_dir_all(&dir);
let path = dir.join("pty.cast");
let _ = std::fs::remove_file(&path);
let config = PtyConfig::new(vec!["sh".into(), "-c".into(), "echo cast_hello".into()])
.with_cast_path(path.clone());
let mut sess = PtySession::new(config, 80, 24).expect("new");
sess.spawn().expect("spawn");
let _ = sess
.wait_for_text("cast_hello", Duration::from_secs(2))
.expect("wait");
sess.close();
let content = std::fs::read_to_string(&path).expect("read cast");
let mut lines = content.lines();
let header: serde_json::Value =
serde_json::from_str(lines.next().expect("header")).expect("header json");
assert_eq!(header["version"], 2);
let events: Vec<String> = lines.map(|s| s.to_string()).collect();
assert!(!events.is_empty(), "no events");
let joined = events.join("\n");
assert!(
joined.contains("cast_hello"),
"cast missing hello: {joined:?}"
);
let _ = std::fs::remove_file(&path);
}
}