From d5cf9aa9f5f4c594d11b7254e60e5defadb40e55 Mon Sep 17 00:00:00 2001 From: Phil Weir Date: Sun, 29 Dec 2024 12:33:08 +0000 Subject: [PATCH] tests(inspector): add height test to do basic verification that this is constructed as expected --- .../src/command/client/search/inspector.rs | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/crates/atuin/src/command/client/search/inspector.rs b/crates/atuin/src/command/client/search/inspector.rs index 2fc8a67ae54..1bfd6550c08 100644 --- a/crates/atuin/src/command/client/search/inspector.rs +++ b/crates/atuin/src/command/client/search/inspector.rs @@ -353,3 +353,89 @@ pub fn input( _ => InputAction::Continue, } } + +#[cfg(test)] +mod tests { + use ratatui::{backend::TestBackend, prelude::*}; + use atuin_client::{ + history::{History, HistoryStats, HistoryId}, + theme::ThemeManager + }; + use time::OffsetDateTime; + use super::draw_ultracompact; + + #[test] + fn correct_height_for_ultracompact() { + let backend = TestBackend::new(22, 5); + let mut terminal = Terminal::new(backend).expect("Could not create terminal"); + let chunk = Rect::new(0, 0, 22, 5); + let history = History { + id: HistoryId::from("test1".to_string()), + timestamp: OffsetDateTime::now_utc(), + duration: 3, + exit: 0, + command: "/bin/cmd".to_string(), + cwd: "/toot".to_string(), + session: "sesh1".to_string(), + hostname: "hostn".to_string(), + deleted_at: None + }; + let next = History { + id: HistoryId::from("test2".to_string()), + timestamp: OffsetDateTime::now_utc(), + duration: 2, + exit: 0, + command: "/bin/cmd -os".to_string(), + cwd: "/toot".to_string(), + session: "sesh1".to_string(), + hostname: "hostn".to_string(), + deleted_at: None + }; + let prev = History { + id: HistoryId::from("test3".to_string()), + timestamp: OffsetDateTime::now_utc(), + duration: 1, + exit: 0, + command: "/bin/cmd -a".to_string(), + cwd: "/toot".to_string(), + session: "sesh1".to_string(), + hostname: "hostn".to_string(), + deleted_at: None + }; + let stats = HistoryStats { + next: Some(next.clone()), + previous: Some(prev.clone()), + total: 2, + average_duration: 3, + exits: Vec::new(), + day_of_week: Vec::new(), + duration_over_time: Vec::new(), + }; + let mut manager = ThemeManager::new(Some(true), Some("".to_string())); + let theme = manager.load_theme("default", None); + let _ = terminal.draw(|f| draw_ultracompact( + f, + chunk, + &history, + &stats, + &theme, + )); + let mut lines = [" "; 5].map(|l| Line::from(l)); + let mut l = lines[0].to_string(); + l.replace_range(0..prev.command.len(), &prev.command); + lines[0] = Line::styled(l, Color::DarkGray); + + // Line one highlights just the history command. + let l = lines[1].to_string(); + lines[1] = Line::from(vec![ + Span::styled(&history.command, Style::new().fg(Color::White).add_modifier(Modifier::BOLD)), + Span::styled(&l[history.command.len()..], Color::Reset), + ]); + + let mut l = lines[2].to_string(); + l.replace_range(0..next.command.len(), &next.command); + lines[2] = Line::styled(l, Color::DarkGray); + + terminal.backend().assert_buffer_lines(lines); + } +}