From 25338420a4c921cd43d7c8b3f1db0920742cde6c Mon Sep 17 00:00:00 2001 From: Hakula Chen Date: Sun, 6 Dec 2026 17:27:27 +0800 Subject: [PATCH] refactor(tui): deduplicate ox-tui test helpers into test_support Consolidate duplicated test-only helpers in ox-tui into a crate-internal #[cfg(test)] pub(crate) mod test_support module and remove one-line pass-through wrappers on test backends. - Extract buffer_text, row_text, line_text, key, and type_text into crates/ox-tui/src/test_support.rs. - Remove QuitOnRenderBackend::buffer_text, QuitOnRenderBackend::draw_count, and SnapshotBackend::buffer_text in runner/tests.rs so callers use buffer_text and struct fields directly. - Replace inline key-typing loops in render/ui/tests/fullscreen.rs and app/tests/input.rs with type_text and key. --- crates/ox-tui/src/app/tests.rs | 12 +---- crates/ox-tui/src/app/tests/input.rs | 4 +- crates/ox-tui/src/input/overlay.rs | 5 +- crates/ox-tui/src/lib.rs | 2 + crates/ox-tui/src/render/markdown/tests.rs | 6 +-- crates/ox-tui/src/render/statusline/tests.rs | 6 +-- crates/ox-tui/src/render/ui/hint_bar.rs | 6 +-- crates/ox-tui/src/render/ui/tests.rs | 27 +--------- .../ox-tui/src/render/ui/tests/fullscreen.rs | 52 +++++++------------ crates/ox-tui/src/render/ui/tests/mouse.rs | 14 ++--- crates/ox-tui/src/render/ui/tests/overlays.rs | 14 ++--- crates/ox-tui/src/render/wrapping.rs | 5 +- crates/ox-tui/src/runner/tests.rs | 30 ++--------- crates/ox-tui/src/runner/tests/loop_events.rs | 9 ++-- crates/ox-tui/src/test_support.rs | 36 +++++++++++++ 15 files changed, 87 insertions(+), 141 deletions(-) create mode 100644 crates/ox-tui/src/test_support.rs diff --git a/crates/ox-tui/src/app/tests.rs b/crates/ox-tui/src/app/tests.rs index 6de67a818..59ebee90c 100644 --- a/crates/ox-tui/src/app/tests.rs +++ b/crates/ox-tui/src/app/tests.rs @@ -1,6 +1,6 @@ use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; -use super::App; +use crate::test_support::{key, type_text}; mod commands; mod events; @@ -9,16 +9,6 @@ mod overlays; mod resume; mod rewind; -fn key(code: KeyCode) -> KeyEvent { - KeyEvent::new(code, KeyModifiers::NONE) -} - fn ctrl(ch: char) -> KeyEvent { KeyEvent::new(KeyCode::Char(ch), KeyModifiers::CONTROL) } - -fn type_text(app: &mut App, text: &str) { - for ch in text.chars() { - app.handle_key(key(KeyCode::Char(ch))); - } -} diff --git a/crates/ox-tui/src/app/tests/input.rs b/crates/ox-tui/src/app/tests/input.rs index 3ac1c9687..ab056915b 100644 --- a/crates/ox-tui/src/app/tests/input.rs +++ b/crates/ox-tui/src/app/tests/input.rs @@ -744,9 +744,7 @@ fn esc_closes_overlay_and_trigger_before_dismissing_active_hint() { assert!(app.active_hint().is_some()); // Activate a completion trigger: Esc closes the trigger without dismissing the active hint. - for ch in "/he".chars() { - app.handle_key(KeyEvent::new(KeyCode::Char(ch), KeyModifiers::NONE)); - } + type_text(&mut app, "/he"); assert!(app.active_trigger().is_some()); assert_eq!( app.handle_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)), diff --git a/crates/ox-tui/src/input/overlay.rs b/crates/ox-tui/src/input/overlay.rs index 6a0eca9ad..d3406b22f 100644 --- a/crates/ox-tui/src/input/overlay.rs +++ b/crates/ox-tui/src/input/overlay.rs @@ -516,10 +516,7 @@ mod tests { use super::{Overlay, OverlayOutcome, PaletteEntry, PaletteTarget}; use crate::input::keymap::CommandId; - - fn key(code: KeyCode) -> KeyEvent { - KeyEvent::new(code, KeyModifiers::NONE) - } + use crate::test_support::key; #[test] fn select_filters_navigates_and_picks_item() { diff --git a/crates/ox-tui/src/lib.rs b/crates/ox-tui/src/lib.rs index e9351aab3..e40aea4ed 100644 --- a/crates/ox-tui/src/lib.rs +++ b/crates/ox-tui/src/lib.rs @@ -7,6 +7,8 @@ mod input; mod render; mod runner; mod style; +#[cfg(test)] +pub(crate) mod test_support; pub mod transcript; pub use app::{ diff --git a/crates/ox-tui/src/render/markdown/tests.rs b/crates/ox-tui/src/render/markdown/tests.rs index f7645535b..56a9071b6 100644 --- a/crates/ox-tui/src/render/markdown/tests.rs +++ b/crates/ox-tui/src/render/markdown/tests.rs @@ -8,12 +8,10 @@ use super::{ render_markdown_incremental, }; use crate::style::theme::Theme; +use crate::test_support::line_text; fn rendered_lines(lines: &[Line<'_>]) -> Vec { - lines - .iter() - .map(|line| line.spans.iter().map(|s| s.content.as_ref()).collect()) - .collect() + lines.iter().map(line_text).collect() } #[test] diff --git a/crates/ox-tui/src/render/statusline/tests.rs b/crates/ox-tui/src/render/statusline/tests.rs index d5ba80842..1117c5f31 100644 --- a/crates/ox-tui/src/render/statusline/tests.rs +++ b/crates/ox-tui/src/render/statusline/tests.rs @@ -6,7 +6,6 @@ use ratatui::backend::TestBackend; use ratatui::buffer::Buffer; use ratatui::layout::Rect; use ratatui::style::{Color, Modifier, Style}; -use ratatui::text::Line; use super::{ ContextInput, ContextThresholds, MAX_EXTERNAL_STATUSLINE_LINES, ModelInput, StatusContext, @@ -20,6 +19,7 @@ use crate::style::config::{ TuiConfig, TuiGlyphsConfig, load_builtin_tui_preset, resolve_tui_config, }; use crate::style::theme::{RenderCtx, Theme}; +use crate::test_support::line_text; fn render_ctx(theme: &Theme, expanded: bool) -> RenderCtx<'_> { RenderCtx { @@ -44,10 +44,6 @@ fn render_statusline_buf(app: &App, theme: &Theme, width: u16) -> Buffer { term.backend().buffer().clone() } -fn line_text(line: &Line<'_>) -> String { - line.spans.iter().map(|s| s.content.as_ref()).collect() -} - fn context_span_text_and_style(app: &App, theme: &Theme) -> (String, Style) { let line = build_statusline(app, render_ctx(theme, false), 80); let span = line diff --git a/crates/ox-tui/src/render/ui/hint_bar.rs b/crates/ox-tui/src/render/ui/hint_bar.rs index e888fd56c..57492dbd6 100644 --- a/crates/ox-tui/src/render/ui/hint_bar.rs +++ b/crates/ox-tui/src/render/ui/hint_bar.rs @@ -116,14 +116,10 @@ pub(super) fn fit_hint_bar_with_rects( #[cfg(test)] mod tests { use ratatui::style::Style; - use ratatui::text::Line; use unicode_width::UnicodeWidthStr as _; use super::{HintItem, fit_hint_bar}; - - fn line_text(line: &Line<'_>) -> String { - line.spans.iter().map(|s| s.content.as_ref()).collect() - } + use crate::test_support::line_text; #[test] fn fit_hint_bar_drops_lower_priority_items_and_preserves_display_order() { diff --git a/crates/ox-tui/src/render/ui/tests.rs b/crates/ox-tui/src/render/ui/tests.rs index 47c89ec9d..a0dc58a92 100644 --- a/crates/ox-tui/src/render/ui/tests.rs +++ b/crates/ox-tui/src/render/ui/tests.rs @@ -1,4 +1,3 @@ -use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; use ratatui::Terminal; use ratatui::backend::TestBackend; use ratatui::buffer::Buffer; @@ -6,6 +5,7 @@ use ratatui::layout::Position; use super::render; use crate::app::App; +use crate::test_support::{buffer_text, key, row_text, type_text}; mod fullscreen; mod layout; @@ -14,16 +14,6 @@ mod narrow; mod overlays; mod transcript; -fn key(code: KeyCode) -> KeyEvent { - KeyEvent::new(code, KeyModifiers::NONE) -} - -fn type_text(app: &mut App, text: &str) { - for ch in text.chars() { - app.handle_key(key(KeyCode::Char(ch))); - } -} - fn render_buffer(app: &App, width: u16, height: u16) -> Buffer { let backend = TestBackend::new(width, height); let mut terminal = Terminal::new(backend).expect("create test terminal"); @@ -33,12 +23,6 @@ fn render_buffer(app: &App, width: u16, height: u16) -> Buffer { terminal.backend().buffer().clone() } -fn row_string(buf: &Buffer, row: u16) -> String { - (0..buf.area.width) - .map(|x| buf[(x, row)].symbol()) - .collect() -} - fn render_to_string(app: &App, width: u16, height: u16) -> String { let (screen, _) = render_with_cursor(app, width, height); screen @@ -51,13 +35,6 @@ fn render_with_cursor(app: &App, width: u16, height: u16) -> (String, Position) .draw(|frame| render(app, frame)) .expect("draw frame"); let cursor = terminal.get_cursor_position().expect("cursor position"); - let buf = terminal.backend().buffer(); - let mut out = String::new(); - for y in 0..height { - for x in 0..width { - out.push_str(buf[(x, y)].symbol()); - } - out.push('\n'); - } + let out = buffer_text(terminal.backend().buffer()); (out, cursor) } diff --git a/crates/ox-tui/src/render/ui/tests/fullscreen.rs b/crates/ox-tui/src/render/ui/tests/fullscreen.rs index 2fc52ed53..47924b275 100644 --- a/crates/ox-tui/src/render/ui/tests/fullscreen.rs +++ b/crates/ox-tui/src/render/ui/tests/fullscreen.rs @@ -1,6 +1,6 @@ use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; -use super::render_to_string; +use super::{key, render_to_string, type_text}; use crate::app::App; use crate::style::config::TuiConfig; @@ -13,10 +13,8 @@ fn render_fullscreen_rewind_shows_transcript_and_footer() { ox_core::message::Message::assistant("second answer"), ]); - for ch in "/rewind".chars() { - app.handle_key(KeyEvent::new(KeyCode::Char(ch), KeyModifiers::NONE)); - } - app.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)); + type_text(&mut app, "/rewind"); + app.handle_key(key(KeyCode::Enter)); let rendered = render_to_string(&app, 104, 16); assert!(rendered.contains("first prompt")); @@ -29,7 +27,7 @@ fn render_fullscreen_rewind_shows_transcript_and_footer() { assert!(rendered.contains("Esc cancel")); assert!(!rendered.contains("gemini-4-hydrogen")); - app.handle_key(KeyEvent::new(KeyCode::Up, KeyModifiers::NONE)); + app.handle_key(key(KeyCode::Up)); let rendered_prev = render_to_string(&app, 104, 16); assert!(rendered_prev.contains("Rewind [turn 1/2]")); } @@ -63,10 +61,8 @@ fn render_fullscreen_resume_wide_split_and_narrow_layouts() { .with_session_id("session-1") .with_session_store(store); - for ch in "/resume".chars() { - app.handle_key(KeyEvent::new(KeyCode::Char(ch), KeyModifiers::NONE)); - } - app.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)); + type_text(&mut app, "/resume"); + app.handle_key(key(KeyCode::Enter)); // Wide terminal (>= 100 cols): shows both list and preview side by side. let wide = render_to_string(&app, 110, 16); @@ -119,10 +115,8 @@ fn render_fullscreen_resume_footers_empty_list_and_preview_states() { .with_session_id("s-active") .with_session_store(store); - for ch in "/resume".chars() { - app.handle_key(KeyEvent::new(KeyCode::Char(ch), KeyModifiers::NONE)); - } - app.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)); + type_text(&mut app, "/resume"); + app.handle_key(key(KeyCode::Enter)); // Wide layout with ASCII border, untitled session, fork badge, and shortened path. let wide = render_to_string(&app, 110, 16); @@ -132,9 +126,7 @@ fn render_fullscreen_resume_footers_empty_list_and_preview_states() { assert!(wide.contains("…/c/ox")); // Filter to s-empty so it is deterministically selected for empty-preview, rename, and delete. - for ch in "s-empty".chars() { - app.handle_key(KeyEvent::new(KeyCode::Char(ch), KeyModifiers::NONE)); - } + type_text(&mut app, "s-empty"); let wide_empty = render_to_string(&app, 110, 16); assert!(wide_empty.contains("No messages in this session yet.")); @@ -150,27 +142,23 @@ fn render_fullscreen_resume_footers_empty_list_and_preview_states() { let rename_screen = render_to_string(&app, 100, 16); assert!(rename_screen.contains("Rename: Empty Session_")); assert!(rename_screen.contains("(Enter save · Esc cancel)")); - app.handle_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)); + app.handle_key(key(KeyCode::Esc)); // Delete-confirm footer (on non-active session). app.handle_key(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::CONTROL)); let delete_screen = render_to_string(&app, 100, 16); assert!(delete_screen.contains("Delete session s-empty? [y/N]")); - app.handle_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)); + app.handle_key(key(KeyCode::Esc)); // Clear query, then filter to s-active for the notice footer. - app.handle_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)); - for ch in "s-active".chars() { - app.handle_key(KeyEvent::new(KeyCode::Char(ch), KeyModifiers::NONE)); - } + app.handle_key(key(KeyCode::Esc)); + type_text(&mut app, "s-active"); app.handle_key(KeyEvent::new(KeyCode::Char('d'), KeyModifiers::CONTROL)); let notice_screen = render_to_string(&app, 100, 16); assert!(notice_screen.contains("Cannot delete the active session.")); // Empty filtered list in both narrow and wide layouts. - for ch in "zzz-no-match".chars() { - app.handle_key(KeyEvent::new(KeyCode::Char(ch), KeyModifiers::NONE)); - } + type_text(&mut app, "zzz-no-match"); let empty_wide = render_to_string(&app, 110, 16); assert!(empty_wide.contains("No matching sessions.")); assert!(empty_wide.contains("Press Tab to search all projects.")); @@ -201,10 +189,8 @@ fn render_fullscreen_resume_updates_list_page_size_for_page_navigation() { .with_cwd("/work/ox") .with_session_store(store); - for ch in "/resume".chars() { - app.handle_key(KeyEvent::new(KeyCode::Char(ch), KeyModifiers::NONE)); - } - app.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)); + type_text(&mut app, "/resume"); + app.handle_key(key(KeyCode::Enter)); _ = render_to_string(&app, 100, 8); let short_page = match app.fullscreen() { @@ -213,7 +199,7 @@ fn render_fullscreen_resume_updates_list_page_size_for_page_navigation() { }; assert_eq!(short_page, 3); - app.handle_key(KeyEvent::new(KeyCode::PageDown, KeyModifiers::NONE)); + app.handle_key(key(KeyCode::PageDown)); let selected_after_short = match app.fullscreen() { Some(crate::app::FullscreenMode::Resume(state)) => state.selected, _ => panic!("expected FullscreenMode::Resume"), @@ -227,14 +213,14 @@ fn render_fullscreen_resume_updates_list_page_size_for_page_navigation() { }; assert_eq!(tall_page, 9); - app.handle_key(KeyEvent::new(KeyCode::PageDown, KeyModifiers::NONE)); + app.handle_key(key(KeyCode::PageDown)); let selected_after_tall = match app.fullscreen() { Some(crate::app::FullscreenMode::Resume(state)) => state.selected, _ => panic!("expected FullscreenMode::Resume"), }; assert_eq!(selected_after_tall, 12); - app.handle_key(KeyEvent::new(KeyCode::PageUp, KeyModifiers::NONE)); + app.handle_key(key(KeyCode::PageUp)); let selected_after_up = match app.fullscreen() { Some(crate::app::FullscreenMode::Resume(state)) => state.selected, _ => panic!("expected FullscreenMode::Resume"), diff --git a/crates/ox-tui/src/render/ui/tests/mouse.rs b/crates/ox-tui/src/render/ui/tests/mouse.rs index 30b80c1fc..b6bc07215 100644 --- a/crates/ox-tui/src/render/ui/tests/mouse.rs +++ b/crates/ox-tui/src/render/ui/tests/mouse.rs @@ -9,11 +9,12 @@ use ox_core::session::SessionStore; use ratatui::buffer::Buffer; use serde_json::json; -use super::{key, render_buffer, row_string, type_text}; +use super::{key, render_buffer, type_text}; use crate::app::{App, AppAction, FullscreenMode, ResumeState}; use crate::input::overlay::{Overlay, OverlayOutcome}; use crate::render::statusline::{StatusLineConfig, StatusLineStyle}; use crate::style::config::{ToolStyle, TuiConfig}; +use crate::test_support::buffer_text; fn resume_state(app: &App) -> &ResumeState { match app.fullscreen() { @@ -40,13 +41,6 @@ fn wheel(kind: MouseEventKind, col: u16, row: u16) -> MouseEvent { } } -fn buffer_to_string(buf: &Buffer) -> String { - (0..buf.area.height) - .map(|y| row_string(buf, y)) - .collect::>() - .join("\n") -} - fn try_find_text_in_row(buf: &Buffer, row: u16, needle: &str) -> Option { let mut text = String::new(); let mut col_starts = Vec::with_capacity(usize::from(buf.area.width)); @@ -75,7 +69,7 @@ fn find_text(buf: &Buffer, needle: &str) -> (u16, u16) { try_find_text(buf, needle).unwrap_or_else(|| { panic!( "text {needle:?} not found in rendered buffer:\n{}", - buffer_to_string(buf) + buffer_text(buf) ) }) } @@ -928,7 +922,7 @@ fn statusline_expanded_indicator_toggles_across_styles_and_noop_when_dropped_at_ assert!( try_find_text(&narrow_buf, "expanded").is_none(), "{label}: narrow width should drop expanded segment:\n{}", - buffer_to_string(&narrow_buf) + buffer_text(&narrow_buf) ); let status_row = narrow_buf.area.height - 1; for col in 0..narrow_buf.area.width { diff --git a/crates/ox-tui/src/render/ui/tests/overlays.rs b/crates/ox-tui/src/render/ui/tests/overlays.rs index 1cb398f59..e03ef80e1 100644 --- a/crates/ox-tui/src/render/ui/tests/overlays.rs +++ b/crates/ox-tui/src/render/ui/tests/overlays.rs @@ -1,7 +1,7 @@ use crossterm::event::KeyCode; use unicode_width::UnicodeWidthStr as _; -use super::{key, render_buffer, render_to_string, row_string, type_text}; +use super::{key, render_buffer, render_to_string, row_text, type_text}; use crate::app::App; use crate::input::overlay::Overlay; use crate::input::trigger::{ActiveTrigger, TriggerCandidate}; @@ -61,9 +61,9 @@ fn render_confirm_overlay_renders_markdown_inline_code_and_wraps() { let buf = render_buffer(&app, 90, 16); let msg_y = (0..buf.area.height) - .find(|&y| row_string(&buf, y).contains("Allow bash to run")) + .find(|&y| row_text(&buf, y).contains("Allow bash to run")) .expect("rendered confirm message row"); - let msg_line = row_string(&buf, msg_y); + let msg_line = row_text(&buf, msg_y); assert!( !msg_line.contains('`'), "confirm message should not show raw backticks:\n{msg_line}" @@ -314,9 +314,9 @@ fn render_completion_popup_highlights_selected_row_source_tag() { let muted_style = app.theme().chrome.muted; let selected_y = (0..buf.area.height) - .find(|&y| row_string(&buf, y).contains("> [cmd]")) + .find(|&y| row_text(&buf, y).contains("> [cmd]")) .expect("selected completion row"); - let selected_line = row_string(&buf, selected_y); + let selected_line = row_text(&buf, selected_y); let tag = "[cmd]"; let tag_x = u16::try_from(selected_line.find(tag).expect("source tag on selected row")) .expect("column fits in u16"); @@ -339,11 +339,11 @@ fn render_completion_popup_highlights_selected_row_source_tag() { let unselected_y = (0..buf.area.height) .find(|&y| { - let line = row_string(&buf, y); + let line = row_text(&buf, y); line.contains(" [skill]") && !line.contains("> [skill]") }) .expect("unselected completion row"); - let unselected_line = row_string(&buf, unselected_y); + let unselected_line = row_text(&buf, unselected_y); let unselected_tag_x = u16::try_from( unselected_line .find("[skill]") diff --git a/crates/ox-tui/src/render/wrapping.rs b/crates/ox-tui/src/render/wrapping.rs index eb193517f..4f77b7994 100644 --- a/crates/ox-tui/src/render/wrapping.rs +++ b/crates/ox-tui/src/render/wrapping.rs @@ -235,10 +235,7 @@ mod tests { use ratatui::text::{Line, Span}; use super::{WrapOptions, truncate_to_width, wrap_line, wrap_lines, wrap_raw_line_ranges}; - - fn line_text(line: &Line<'_>) -> String { - line.spans.iter().map(|s| s.content.as_ref()).collect() - } + use crate::test_support::line_text; #[test] fn wrap_raw_line_ranges_handles_ascii_words_and_mid_word_breaks() { diff --git a/crates/ox-tui/src/runner/tests.rs b/crates/ox-tui/src/runner/tests.rs index 6628d8c7c..5607cdd91 100644 --- a/crates/ox-tui/src/runner/tests.rs +++ b/crates/ox-tui/src/runner/tests.rs @@ -10,6 +10,8 @@ use ox_core::client::{ use ratatui::backend::{Backend, TestBackend}; use tokio::sync::mpsc; +use crate::test_support::buffer_text; + macro_rules! delegate_test_backend_methods { () => { type Error = std::io::Error; @@ -69,18 +71,6 @@ mod sessions; const RUN_LOOP_TEST_TIMEOUT: Duration = Duration::from_secs(5); -fn buffer_text(backend: &TestBackend) -> String { - let buf = backend.buffer(); - let mut out = String::new(); - for y in 0..buf.area.height { - for x in 0..buf.area.width { - out.push_str(buf[(x, y)].symbol()); - } - out.push('\n'); - } - out -} - /// [`TestBackend`] wrapper that sends `Ctrl+C` once the rendered buffer satisfies `predicate`. struct QuitOnRenderBackend

{ inner: TestBackend, @@ -103,14 +93,6 @@ impl bool> QuitOnRenderBackend

{ draw_count: 0, } } - - const fn draw_count(&self) -> usize { - self.draw_count - } - - fn buffer_text(&self) -> String { - buffer_text(&self.inner) - } } impl bool> Backend for QuitOnRenderBackend

{ @@ -120,7 +102,7 @@ impl bool> Backend for QuitOnRenderBackend

{ let Ok(()) = self.inner.flush(); self.draw_count += 1; if self.quit_tx.is_some() - && (self.predicate)(&self.buffer_text()) + && (self.predicate)(&buffer_text(self.inner.buffer())) && let Some(tx) = self.quit_tx.take() { _ = tx.send(Ok(Event::Key(KeyEvent::new( @@ -147,10 +129,6 @@ impl SnapshotBackend { frames_tx, } } - - fn buffer_text(&self) -> String { - buffer_text(&self.inner) - } } impl Backend for SnapshotBackend { @@ -158,7 +136,7 @@ impl Backend for SnapshotBackend { fn flush(&mut self) -> Result<(), Self::Error> { let Ok(()) = self.inner.flush(); - _ = self.frames_tx.send(self.buffer_text()); + _ = self.frames_tx.send(buffer_text(self.inner.buffer())); Ok(()) } } diff --git a/crates/ox-tui/src/runner/tests/loop_events.rs b/crates/ox-tui/src/runner/tests/loop_events.rs index 53d527058..a54c3c1a8 100644 --- a/crates/ox-tui/src/runner/tests/loop_events.rs +++ b/crates/ox-tui/src/runner/tests/loop_events.rs @@ -21,6 +21,7 @@ use super::super::{EditorOutcome, FRAME_INTERVAL, SyncOutputBackend, draw_synchr use super::{QuitOnRenderBackend, RUN_LOOP_TEST_TIMEOUT, SnapshotBackend, StaticWire}; use crate::app::{App, AppStatus, SubagentState, SubagentStatus, TranscriptEntry}; use crate::render::statusline::{StatusLineConfig, StatusSegment}; +use crate::test_support::buffer_text; #[tokio::test] async fn run_loop_submits_prompt_streams_response_and_quits() { @@ -782,13 +783,13 @@ async fn run_loop_coalesces_burst_streaming_events_and_renders_final_state() { ] ); assert!( - terminal.backend().buffer_text().contains(&expected), + buffer_text(terminal.backend().inner.buffer()).contains(&expected), "final rendered buffer must contain full coalesced response" ); assert!( - terminal.backend().draw_count() <= 10, + terminal.backend().draw_count <= 10, "burst of 64 streaming deltas should coalesce into far fewer draws, got {}", - terminal.backend().draw_count() + terminal.backend().draw_count ); } @@ -923,7 +924,7 @@ where drop(agent_tx); loop_res.expect("run_loop should exit cleanly on EOF"); - terminal.backend().buffer_text() + buffer_text(terminal.backend().inner.buffer()) } #[tokio::test(start_paused = true)] diff --git a/crates/ox-tui/src/test_support.rs b/crates/ox-tui/src/test_support.rs new file mode 100644 index 000000000..2196f19c1 --- /dev/null +++ b/crates/ox-tui/src/test_support.rs @@ -0,0 +1,36 @@ +use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; +use ratatui::buffer::Buffer; +use ratatui::text::Line; + +use crate::app::App; + +pub(crate) fn key(code: KeyCode) -> KeyEvent { + KeyEvent::new(code, KeyModifiers::NONE) +} + +pub(crate) fn type_text(app: &mut App, text: &str) { + for ch in text.chars() { + app.handle_key(key(KeyCode::Char(ch))); + } +} + +pub(crate) fn line_text(line: &Line<'_>) -> String { + line.spans.iter().map(|s| s.content.as_ref()).collect() +} + +pub(crate) fn row_text(buf: &Buffer, row: u16) -> String { + (0..buf.area.width) + .map(|x| buf[(x, row)].symbol()) + .collect() +} + +pub(crate) fn buffer_text(buf: &Buffer) -> String { + let mut out = String::new(); + for y in 0..buf.area.height { + for x in 0..buf.area.width { + out.push_str(buf[(x, y)].symbol()); + } + out.push('\n'); + } + out +}