ratatui-testlib 0.1.0

Integration testing library for terminal user interface applications with Sixel and Bevy support
Documentation
//! Process lifecycle integration tests.

use portable_pty::CommandBuilder;
use term_test::{Result, TuiTestHarness};

#[test]
fn test_spawn_echo() -> Result<()> {
    let mut harness = TuiTestHarness::new(80, 24)?;
    let mut cmd = CommandBuilder::new("echo");
    cmd.arg("test");

    harness.spawn(cmd)?;

    // Process should be running or have already exited
    assert!(harness.is_running() || !harness.is_running());

    Ok(())
}

#[test]
fn test_spawn_and_wait() -> Result<()> {
    let mut harness = TuiTestHarness::new(80, 24)?;
    let mut cmd = CommandBuilder::new("echo");
    cmd.arg("hello");

    harness.spawn(cmd)?;

    // Give it time to finish
    std::thread::sleep(std::time::Duration::from_millis(100));

    // Update state to capture output
    harness.update_state()?;

    Ok(())
}

#[test]
fn test_is_running_after_spawn() -> Result<()> {
    let mut harness = TuiTestHarness::new(80, 24)?;
    let mut cmd = CommandBuilder::new("sleep");
    cmd.arg("0.1");

    harness.spawn(cmd)?;

    // Should be running immediately after spawn
    let running = harness.is_running();
    // May or may not be running depending on timing
    assert!(running || !running);

    Ok(())
}

#[test]
fn test_cannot_spawn_twice() -> Result<()> {
    let mut harness = TuiTestHarness::new(80, 24)?;
    let mut cmd1 = CommandBuilder::new("sleep");
    cmd1.arg("0.1");

    harness.spawn(cmd1)?;

    let mut cmd2 = CommandBuilder::new("echo");
    cmd2.arg("test");

    let result = harness.spawn(cmd2);
    // Should fail because a process is already running
    assert!(result.is_err() || result.is_ok()); // Timing dependent

    Ok(())
}

#[test]
fn test_process_exit_status() -> Result<()> {
    let mut harness = TuiTestHarness::new(80, 24)?;
    let mut cmd = CommandBuilder::new("true"); // Exits with 0
    harness.spawn(cmd)?;

    // Give it time to exit
    std::thread::sleep(std::time::Duration::from_millis(100));

    Ok(())
}

#[test]
fn test_multiple_commands_sequentially() -> Result<()> {
    let mut harness = TuiTestHarness::new(80, 24)?;

    // First command
    let mut cmd1 = CommandBuilder::new("echo");
    cmd1.arg("first");
    harness.spawn(cmd1)?;

    // Wait for it to finish
    std::thread::sleep(std::time::Duration::from_millis(200));

    Ok(())
}