use ratatui::backend::TestBackend;
use ratatui::Terminal;
use super::app::App;
pub const DEFAULT_WIDTH: u16 = 200;
pub const DEFAULT_HEIGHT: u16 = 50;
pub fn render_rows(app: &App, width: u16, height: u16) -> Vec<String> {
let backend = TestBackend::new(width, height);
let mut terminal = Terminal::new(backend).expect("TestBackend cannot fail to initialise");
terminal
.draw(|f| super::ui::draw(f, app))
.expect("drawing into an in-memory buffer cannot fail");
let buffer = terminal.backend().buffer();
let mut rows = Vec::with_capacity(height as usize);
for y in 0..height {
let mut row = String::with_capacity(width as usize);
for x in 0..width {
row.push_str(buffer[(x, y)].symbol());
}
rows.push(row.trim_end().to_string());
}
rows
}
pub fn render_frame(app: &App) -> String {
render_rows(app, DEFAULT_WIDTH, DEFAULT_HEIGHT).join("\n")
}
#[cfg(test)]
mod tests {
use super::*;
fn app() -> App {
App::new_fast().expect("the TUI app must be constructible headlessly")
}
#[test]
fn a_frame_renders_and_contains_the_tab_bar() {
let app = app();
let frame = render_frame(&app);
assert!(
!frame.trim().is_empty(),
"the TUI rendered an entirely blank frame"
);
assert!(
frame.contains("Overview"),
"the tab bar is missing from the frame:\n{frame}"
);
}
#[test]
fn the_overview_tab_renders_actual_readings() {
let mut app = app();
app.selected_tab = super::super::app::TAB_OVERVIEW;
let frame = render_frame(&app);
assert!(
frame.contains("CPU") || frame.contains("Memory"),
"the Overview tab shows no subsystem at all:\n{frame}"
);
}
#[test]
fn the_inference_tab_is_first_and_selected_by_default() {
let app = app();
assert_eq!(app.tabs[0], "Inference");
assert_eq!(app.selected_tab, 0);
}
#[test]
fn the_inference_tab_reports_when_no_engine_answered() {
use crate::inference::{
InferencePoller, InferenceSource, Locality, ProviderKind, ProviderSample, SourceError,
};
use std::time::Duration;
struct Refusing;
impl InferenceSource for Refusing {
fn kind(&self) -> ProviderKind {
ProviderKind::IronWorks
}
fn endpoint(&self) -> String {
"http://127.0.0.1:8080".to_string()
}
fn locality(&self) -> Locality {
Locality::Local
}
fn poll(&self, _: Duration) -> Result<ProviderSample, SourceError> {
Err(SourceError::Unreachable("connection refused".to_string()))
}
}
let mut app = app();
app.inference = Some(InferencePoller::spawn(
vec![Box::new(Refusing)],
Duration::from_secs(60),
));
app.await_first_inference_poll(Duration::from_secs(5));
let frame = render_frame(&app);
assert!(
frame.contains("no engine answered at http://127.0.0.1:8080"),
"the Inference tab does not say no engine answered:\n{frame}"
);
}
#[test]
fn typing_into_the_agent_box_does_not_switch_tabs() {
use crossterm::event::{KeyCode, KeyModifiers};
let mut app = app();
assert_eq!(app.selected_tab, super::super::app::TAB_INFERENCE);
app.handle_key(KeyCode::Char('a'), KeyModifiers::empty());
assert!(app.agent_input_mode, "'a' should open the question box");
app.handle_key(KeyCode::Char('2'), KeyModifiers::empty());
app.handle_key(KeyCode::Tab, KeyModifiers::empty());
app.handle_key(KeyCode::Char('q'), KeyModifiers::empty());
assert_eq!(app.selected_tab, super::super::app::TAB_INFERENCE);
assert_eq!(app.agent_input, "2q");
assert!(app.agent_input_mode);
}
#[test]
fn number_keys_follow_the_tab_order() {
use super::super::app::{TAB_AGENT, TAB_INFERENCE, TAB_OVERVIEW, TAB_PROFILES};
use crossterm::event::{KeyCode, KeyModifiers};
let mut app = app();
app.handle_key(KeyCode::Char('2'), KeyModifiers::empty());
assert_eq!(app.selected_tab, TAB_OVERVIEW);
app.handle_key(KeyCode::Char('1'), KeyModifiers::empty());
assert_eq!(app.selected_tab, TAB_INFERENCE);
app.handle_key(KeyCode::Char('9'), KeyModifiers::empty());
assert_eq!(app.selected_tab, TAB_PROFILES);
app.handle_key(KeyCode::Char('0'), KeyModifiers::empty());
assert_eq!(app.selected_tab, TAB_AGENT);
assert_eq!(app.tabs[app.selected_tab], "Agent");
}
#[test]
fn every_tab_renders_without_panicking() {
let mut app = app();
let tab_count = app.tabs.len();
assert!(tab_count > 1, "expected several tabs, found {tab_count}");
for index in 0..tab_count {
app.selected_tab = index;
let frame = render_frame(&app);
assert!(
!frame.trim().is_empty(),
"tab {index} ({}) rendered a blank frame",
app.tabs[index]
);
assert!(
frame.contains("Overview"),
"tab {index} ({}) lost the tab bar, so the layout collapsed:\n{frame}",
app.tabs[index]
);
}
}
#[test]
fn rendered_tab_labels_use_the_ontology_spelling() {
use crate::ontology::labels;
let app = app();
let frame = render_frame(&app);
for tab in &app.tabs {
let lowered = tab.to_ascii_lowercase();
if labels::is_known_domain(&lowered) {
let canonical = labels::domain_label(&lowered);
assert!(
frame.contains(&canonical),
"the TUI renders the domain {lowered:?} but the frame does not \
contain its ontology spelling {canonical:?}:\n{frame}"
);
}
}
}
#[test]
fn frame_geometry_matches_the_requested_size() {
let app = app();
let rows = render_rows(&app, 120, 30);
assert_eq!(rows.len(), 30, "wrong number of rows");
for (i, row) in rows.iter().enumerate() {
assert!(
row.chars().count() <= 120,
"row {i} is {} cells wide, exceeding the requested 120",
row.chars().count()
);
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Step {
Goto(String),
Key(String),
Refresh,
Capture,
Assert(String),
Refute(String),
}
#[derive(Debug, Default)]
pub struct ScriptResult {
pub captures: Vec<String>,
pub failures: Vec<String>,
}
pub fn parse_script(text: &str) -> Result<Vec<Step>, String> {
let mut steps = Vec::new();
for (n, raw) in text.lines().enumerate() {
let line = raw.split('#').next().unwrap_or("").trim();
if line.is_empty() {
continue;
}
let (verb, rest) = match line.split_once(char::is_whitespace) {
Some((v, r)) => (v, r.trim()),
None => (line, ""),
};
let step = match verb.to_ascii_lowercase().as_str() {
"goto" if !rest.is_empty() => Step::Goto(rest.to_string()),
"key" if !rest.is_empty() => Step::Key(rest.to_string()),
"refresh" => Step::Refresh,
"capture" => Step::Capture,
"assert" if !rest.is_empty() => Step::Assert(rest.to_string()),
"refute" if !rest.is_empty() => Step::Refute(rest.to_string()),
other => {
return Err(format!(
"line {}: unknown or incomplete step {other:?}. Steps: goto <tab>, \
key <name>, refresh, capture, assert <text>, refute <text>",
n + 1
))
}
};
steps.push(step);
}
Ok(steps)
}
fn parse_key(name: &str) -> Option<crossterm::event::KeyCode> {
use crossterm::event::KeyCode;
let k = match name.to_ascii_lowercase().as_str() {
"tab" => KeyCode::Tab,
"backtab" | "shift-tab" => KeyCode::BackTab,
"left" => KeyCode::Left,
"right" => KeyCode::Right,
"up" => KeyCode::Up,
"down" => KeyCode::Down,
"enter" => KeyCode::Enter,
"esc" => KeyCode::Esc,
"home" => KeyCode::Home,
"end" => KeyCode::End,
"pageup" => KeyCode::PageUp,
"pagedown" => KeyCode::PageDown,
other => {
let mut chars = other.chars();
match (chars.next(), chars.next()) {
(Some(c), None) => KeyCode::Char(c),
_ => return None,
}
}
};
Some(k)
}
pub fn run_script(app: &mut App, steps: &[Step], width: u16, height: u16) -> ScriptResult {
use crossterm::event::KeyModifiers;
let mut result = ScriptResult::default();
let mut frame = render_rows(app, width, height).join("\n");
for (i, step) in steps.iter().enumerate() {
match step {
Step::Goto(target) => {
let resolved = target
.parse::<usize>()
.ok()
.filter(|n| *n < app.tabs.len())
.or_else(|| app.tabs.iter().position(|t| t.eq_ignore_ascii_case(target)));
match resolved {
Some(index) => app.selected_tab = index,
None => result.failures.push(format!(
"step {}: unknown tab {target:?}; available: {:?}",
i + 1,
app.tabs
)),
}
}
Step::Key(name) => match parse_key(name) {
Some(code) => {
if app.handle_key(code, KeyModifiers::empty()) {
break;
}
}
None => result
.failures
.push(format!("step {}: unknown key {name:?}", i + 1)),
},
Step::Refresh => {
let _ = app.update();
app.sync_snapshot();
}
Step::Capture => result.captures.push(frame.clone()),
Step::Assert(needle) => {
if !frame.contains(needle.as_str()) {
result.failures.push(format!(
"step {}: expected {needle:?} in the frame, not found",
i + 1
));
}
}
Step::Refute(needle) => {
if frame.contains(needle.as_str()) {
result.failures.push(format!(
"step {}: {needle:?} should not appear in the frame, but does",
i + 1
));
}
}
}
if matches!(step, Step::Goto(_) | Step::Key(_) | Step::Refresh) {
frame = render_rows(app, width, height).join("\n");
}
}
result
}
#[cfg(test)]
mod script_tests {
use super::*;
fn app() -> App {
App::new_fast().expect("app must be constructible headlessly")
}
#[test]
fn scripts_parse_with_comments_and_blank_lines() {
let steps = parse_script(
"# navigate\ngoto CPU\n\n key tab # inline comment\ncapture\nassert Overview\n",
)
.expect("should parse");
assert_eq!(
steps,
vec![
Step::Goto("CPU".into()),
Step::Key("tab".into()),
Step::Capture,
Step::Assert("Overview".into()),
]
);
}
#[test]
fn unknown_steps_are_rejected_with_the_available_ones_named() {
let err = parse_script("frobnicate widget").expect_err("should reject");
assert!(err.contains("frobnicate"), "got: {err}");
assert!(
err.contains("goto"),
"the error should list valid steps: {err}"
);
}
#[test]
fn goto_changes_the_selected_tab() {
let mut app = app();
let steps = parse_script("goto Memory\nassert Overview").unwrap();
let result = run_script(&mut app, &steps, 160, 30);
assert!(result.failures.is_empty(), "{:?}", result.failures);
assert_eq!(
app.selected_tab,
app.tabs.iter().position(|t| *t == "Memory").unwrap()
);
}
#[test]
fn key_presses_drive_the_same_handler_as_the_interactive_loop() {
let mut app = app();
let before = app.selected_tab;
let steps = parse_script("key 3").unwrap();
let result = run_script(&mut app, &steps, 160, 30);
assert!(result.failures.is_empty(), "{:?}", result.failures);
assert_eq!(
app.selected_tab,
crate::tui::app::TAB_PROCESSES,
"key '3' should select the Processes tab"
);
assert_ne!(before, app.selected_tab);
}
#[test]
fn assertions_report_failure_rather_than_panicking() {
let mut app = app();
let steps = parse_script("assert definitely-not-on-screen").unwrap();
let result = run_script(&mut app, &steps, 160, 30);
assert_eq!(result.failures.len(), 1, "expected one failure");
assert!(result.failures[0].contains("definitely-not-on-screen"));
}
#[test]
fn refute_catches_text_that_should_be_absent() {
let mut app = app();
let steps = parse_script("refute Overview").unwrap();
let result = run_script(&mut app, &steps, 160, 30);
assert_eq!(result.failures.len(), 1, "refute should have failed");
}
#[test]
fn capture_records_the_frame_at_that_point() {
let mut app = app();
let steps = parse_script("goto CPU\ncapture\ngoto Memory\ncapture").unwrap();
let result = run_script(&mut app, &steps, 160, 30);
assert_eq!(result.captures.len(), 2);
assert!(result.captures.iter().all(|c| c.contains("Overview")));
}
#[test]
fn a_quit_key_stops_the_script() {
let mut app = app();
let steps = parse_script("key q\nassert definitely-not-on-screen").unwrap();
let result = run_script(&mut app, &steps, 160, 30);
assert!(
result.failures.is_empty(),
"steps after a quit should not run: {:?}",
result.failures
);
}
}
#[cfg(test)]
mod agents_tab_tests {
use super::super::app::{App, TAB_AGENTS, TAB_INFERENCE, TAB_OVERVIEW};
use super::render_frame;
use crossterm::event::{KeyCode, KeyModifiers};
fn app() -> App {
App::new_fast().expect("the TUI app must be constructible headlessly")
}
#[test]
fn agents_follows_inference_and_is_reached_by_tab_cycling() {
let mut app = app();
assert_eq!(app.tabs[0], "Inference");
assert_eq!(app.tabs[TAB_AGENTS], "Agents");
assert_eq!(app.selected_tab, TAB_INFERENCE);
app.handle_key(KeyCode::Tab, KeyModifiers::empty());
assert_eq!(app.selected_tab, TAB_AGENTS);
app.handle_key(KeyCode::Tab, KeyModifiers::empty());
assert_eq!(app.selected_tab, TAB_OVERVIEW);
app.handle_key(KeyCode::BackTab, KeyModifiers::empty());
assert_eq!(app.selected_tab, TAB_AGENTS);
}
#[test]
fn digits_still_select_the_tabs_they_always_did() {
let mut app = app();
app.handle_key(KeyCode::Char('2'), KeyModifiers::empty());
assert_eq!(app.selected_tab, TAB_OVERVIEW);
app.handle_key(KeyCode::Char('0'), KeyModifiers::empty());
assert_eq!(app.tabs[app.selected_tab], "Agent");
}
#[test]
fn the_agents_tab_renders_its_sections_and_coverage() {
let mut app = app();
app.selected_tab = TAB_AGENTS;
let frame = render_frame(&app);
for needle in [
"Agents",
"Sessions:",
"Active",
"Recent",
"Workflows",
"Engine requests",
"Coverage",
"Gemini CLI",
] {
assert!(frame.contains(needle), "missing {needle:?} in:\n{frame}");
}
assert!(!frame.contains("Traceback"));
}
#[test]
fn arrow_keys_scroll_the_agents_list_and_nothing_else() {
let mut app = app();
app.selected_tab = TAB_AGENTS;
app.handle_key(KeyCode::PageDown, KeyModifiers::empty());
assert_eq!(app.agents_scroll, 10);
app.handle_key(KeyCode::Up, KeyModifiers::empty());
assert_eq!(app.agents_scroll, 9);
app.handle_key(KeyCode::PageUp, KeyModifiers::empty());
assert_eq!(app.agents_scroll, 0);
app.selected_tab = TAB_INFERENCE;
app.handle_key(KeyCode::PageDown, KeyModifiers::empty());
assert_eq!(app.agents_scroll, 0);
}
}