Expand description
§ratatui_testlib
A Rust library for integration testing of terminal user interface (TUI) applications.
§Overview
ratatui_testlib provides a comprehensive testing framework for TUI applications built with
libraries like Ratatui, with first-class support for:
- PTY-based testing: Real terminal emulation with pseudo-terminal support
- Sixel graphics testing: Position verification and bounds checking for Sixel images
- Bevy ECS integration: Test Bevy-based TUI applications with
bevy_ratatui - Async runtime support: Full Tokio async/await support
- Snapshot testing: Integration with
instafor visual regression testing - Headless CI/CD: Works without X11/Wayland for GitHub Actions
§Quick Start
use ratatui_testlib::{TuiTestHarness, Result};
use portable_pty::CommandBuilder;
#[test]
fn test_my_tui_app() -> Result<()> {
// Create a test harness with 80x24 terminal
let mut harness = TuiTestHarness::new(80, 24)?;
// Spawn your TUI application
let mut cmd = CommandBuilder::new("./my-tui-app");
harness.spawn(cmd)?;
// Wait for initial render
harness.wait_for(|state| {
state.contents().contains("Welcome")
})?;
// Send keyboard input
harness.send_text("hello")?;
// Capture screen state
let contents = harness.screen_contents();
assert!(contents.contains("hello"));
Ok(())
}§Testing Sixel Graphics
With the sixel feature enabled, you can verify Sixel graphics positioning:
use ratatui_testlib::TuiTestHarness;
let mut harness = TuiTestHarness::new(80, 24)?;
// ... spawn your app and trigger Sixel rendering ...
// Verify all Sixel graphics are within the preview area
let preview_area = (5, 5, 30, 15); // row, col, width, height
let sixel_regions = harness.state().sixel_regions();
for region in sixel_regions {
assert!(region.start_row >= preview_area.0);
assert!(region.start_col >= preview_area.1);
}§Feature Flags
async-tokio: Enable Tokio async runtime supportbevy: Enable Bevy ECS integrationbevy-ratatui: Enable bevy_ratatui plugin supportratatui-helpers: Enable Ratatui-specific test helperssixel: Enable Sixel graphics position tracking and testingsnapshot-insta: Enable snapshot testing withinstamvp: Enable all MVP features (recommended for dgx-pixels)
§Architecture
The library is organized into several layers:
- PTY Management (
pty): Pseudo-terminal creation and lifecycle - Terminal Emulation (
screen): VT100 parsing and screen state - Test Harness (
harness): High-level testing API - Sixel Support (
sixel): Graphics protocol testing (MVP requirement) - Bevy Integration (
bevy): ECS testing support (MVP requirement)
§Primary Use Case: dgx-pixels
This library was built to support comprehensive integration testing for the dgx-pixels project, with focus on:
- Sixel graphics positioning within designated preview areas
- Sixel clearing on screen transitions
- Bevy ECS entity and component testing
- Text input and cursor position verification
- Async Tokio runtime compatibility
Re-exports§
Modules§
- events
- Keyboard event types and escape sequence encoding.
Structs§
- Cell
- Represents a single terminal cell with character and attributes.
- Command
Builder CommandBuilderis used to prepare a command to be spawned into a pty. The interface is intentionally similar to that ofstd::process::Command.- Screen
State - Represents the current state of the terminal screen.
- Sixel
Region - Represents a Sixel graphics region in the terminal.
- Test
Terminal - A test terminal backed by a pseudo-terminal (PTY).
- TuiTest
Harness - High-level test harness for TUI applications.
Enums§
- Term
Test Error - Errors that can occur during TUI testing.
Type Aliases§
- Result
- Result type alias for ratatui_testlib operations.