Lines
98.59 %
Functions
100 %
Branches
use super::*;
use crate::event::{Intent, apply};
use crate::modal::Modal;
use crate::view::Tab;
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use ratatui::buffer::Cell;
use ratatui::style::{Color, Style};
use sqlx::types::Uuid;
fn buffer_text(terminal: &Terminal<TestBackend>) -> String {
terminal
.backend()
.buffer()
.content()
.iter()
.map(|cell| cell.symbol())
.collect()
}
fn render_console(app: &App) -> String {
render_console_at(app, 80)
fn render_console_at(app: &App, width: u16) -> String {
let backend = TestBackend::new(width, 24);
let mut terminal = Terminal::new(backend).expect("test terminal");
.draw(|frame| draw(frame, app, &mut crate::hits::Hits::default()))
.expect("draw must not panic");
buffer_text(&terminal)
fn console_app() -> App {
let mut app = crate::test_support::app_for(Uuid::nil());
app.switch_tab(Tab::Console);
app
#[test]
fn transaction_modal_renders_split_row_details() {
use crate::form::Form;
use crate::widgets::splits::{COL_FROM, COL_VALUE};
use crate::widgets::{FocusedSubWidget, SelectOption, Widget};
let mut form = Form::transaction_create();
if let Widget::Splits(ref mut sw) = form.fields[3].widget {
sw.set_account_options(vec![
SelectOption {
id: "acc-0".to_string(),
label: "Cash".to_string(),
},
id: "acc-1".to_string(),
label: "Food".to_string(),
]);
sw.col_focus = COL_VALUE;
if let Some(FocusedSubWidget::Amount(aw)) = sw.focused_subwidget_mut() {
aw.insert_char('5');
aw.insert_char('0');
sw.col_focus = COL_FROM;
app.overlays.push(Modal::Form(form));
let backend = TestBackend::new(120, 40);
.draw(|frame| draw(frame, &app, &mut crate::hits::Hits::default()))
let text = buffer_text(&terminal);
assert!(
text.contains("Cash"),
"the selected from-account label must render"
);
assert!(text.contains("50"), "typed amount must render: not found");
fn console_tab_renders_scrollback_and_prompt() {
let mut app = console_app();
app.console_mut().push_scrollback("(:id 0 :value 42)");
app.console_mut().input.insert_char('(');
let text = render_console(&app);
assert!(text.contains("(:id 0 :value 42)"), "scrollback missing");
assert!(text.contains("nms>"), "prompt missing");
fn console_prompt_shows_continuation_marker_when_pending() {
for c in "(list".chars() {
app.console_mut().input.insert_char(c);
app.console_mut().take_complete_form();
assert!(!app.console().pending.is_empty(), "form must be pending");
assert!(text.contains("...>"), "continuation marker missing");
assert!(!text.contains("nms>"), "primary prompt should be hidden");
fn console_hint_switches_with_focus() {
let unfocused = render_console(&app);
assert!(unfocused.contains("i Focus"), "blurred hint missing");
app.focus_console();
let focused = render_console(&app);
assert!(focused.contains("Esc Leave"), "focused hint missing");
let wide = render_console_at(&app, 120);
assert!(wide.contains("C-c Interrupt"), "interrupt hint missing");
fn console_prompt_uses_the_prompt_role_only_when_focused() {
let theme = crate::test_support::dn_theme();
let app = console_app();
let backend = TestBackend::new(80, 24);
let prompt_cell = prompt_marker_cell(&terminal);
shows(&prompt_cell, theme.style(Role::PanelDim)),
"blurred prompt must use the dim role"
let mut focused = console_app();
focused.focus_console();
.draw(|frame| draw(frame, &focused, &mut crate::hits::Hits::default()))
shows(&prompt_cell, theme.style(Role::Prompt)),
"focused prompt must use the prompt role"
fn shows(cell: &Cell, style: Style) -> bool {
Some(cell.fg) == style.fg && Some(cell.bg) == style.bg && cell.modifier == style.add_modifier
fn prompt_marker_cell(terminal: &Terminal<TestBackend>) -> ratatui::buffer::Cell {
let buffer = terminal.backend().buffer();
for y in 0..buffer.area.height {
if buffer[(1, y)].symbol() == "n" && buffer[(2, y)].symbol() == "m" {
return buffer[(1, y)].clone();
panic!("nms> prompt marker not found in rendered buffer");
fn form_focused_field_uses_the_focused_input_role_unfocused_the_input_role() {
let form = Form::config_set();
assert_eq!(form.focus, 0, "focus must start on first field");
let cursor = terminal.get_cursor_position().expect("the cursor position");
shows(
&buffer[(cursor.x, cursor.y)],
theme.style(Role::InputFocused)
),
"focused field must use the focused input role"
shows(&buffer[(cursor.x, cursor.y + 1)], theme.style(Role::Input)),
"unfocused field must use the input role"
fn scrollback_shows_only_the_tail_when_longer_than_viewport() {
for i in 0..100 {
app.console_mut().push_scrollback(format!("line-{i:03}"));
text.contains(&format!("line-{:03}", 99)),
"newest line must be visible"
!text.contains("line-000"),
"oldest line must be scrolled out of the viewport"
fn render_app(app: &App) -> String {
fn help_window_contains_command_palette_key() {
let mut app = crate::test_support::app();
crate::event::apply(&mut app, crate::event::Intent::OpenHelp);
let text = render_app(&app);
assert!(text.contains(':'), "help must mention the ':' key");
fn help_window_contains_list_view_keys() {
assert!(text.contains('d'), "help must mention 'd' for delete");
assert!(text.contains('e'), "help must mention 'e' for edit/tag");
fn help_modal_names_the_window_keys() {
let app = crate::test_support::app();
let text = crate::keymap::help_lines(&app).join("\n");
text.contains("F6"),
"help must mention F6 for the next window"
fn screen_rows(app: &App) -> Vec<String> {
let mut terminal = Terminal::new(TestBackend::new(80, 24)).expect("test terminal");
.expect("draw");
buffer
.chunks(usize::from(buffer.area.width))
.map(|row| row.iter().map(|cell| cell.symbol()).collect())
fn help_shows_its_top_lines_on_80x24_and_scrolls_to_the_rest() {
let Some(help) = app.front::<crate::viewer::Viewer>() else {
panic!("help is open");
};
let lines = help.lines.clone();
let top = screen_rows(&app);
assert!(top.iter().any(|row| row.contains(lines[0].trim_end())));
let last = lines.last().expect("help has lines").trim_end();
assert!(!top.iter().any(|row| row.contains(last)), "{last:?}");
for _ in 0..lines.len() {
crate::event::apply(&mut app, crate::event::Intent::ScrollPageDown);
let bottom = screen_rows(&app);
assert!(bottom.iter().any(|row| row.contains(last)), "{last:?}");
fn cmdline_active_with_completions_shows_them_in_status() {
apply(&mut app, Intent::OpenCommandLine);
for c in "account ".chars() {
apply(&mut app, Intent::InsertChar(c));
apply(&mut app, Intent::CompleteCommandLine);
text.contains("create"),
"completion candidates must appear in the status area"
fn cmdline_active_without_completions_shows_the_typed_text() {
for c in "ver".chars() {
assert!(text.contains(":ver"), "the typed text must be shown");
assert!(!text.contains("cursor"), "no debug cursor position");
fn assert_no_default_background(app: &App) {
for x in 0..buffer.area.width {
assert_ne!(buffer[(x, y)].bg, Color::Reset, "({x}, {y})");
fn the_whole_default_screen_is_filled() {
assert_no_default_background(&crate::test_support::app());
fn the_whole_screen_is_filled_under_a_modal() {
crate::test_support::open_overlay(&mut app);
assert_no_default_background(&app);
fn every_tab_fills_the_screen() {
for tab in Tab::ALL {
app.switch_tab(tab);
fn bottom_row(app: &App, width: u16) -> String {
let mut terminal = Terminal::new(TestBackend::new(width, 24)).expect("test terminal");
(0..width).map(|x| buffer[(x, 23)].symbol()).collect()
fn the_status_bar_shows_the_hints_and_the_keymap_indicator() {
let cua = bottom_row(&crate::test_support::app(), 80);
assert!(cua.contains("F1 Help M-x Quit"), "{cua:?}");
assert!(cua.trim_end().ends_with("[cua]"), "{cua:?}");
let emacs = bottom_row(&crate::test_support::emacs_app(), 120);
assert!(emacs.contains("q Close/Quit ? Help"), "{emacs:?}");
assert!(emacs.trim_end().ends_with("[emacs]"), "{emacs:?}");
fn a_status_message_takes_the_whole_row() {
app.status = "x".repeat(200);
let row = bottom_row(&app, 80);
assert!(row.starts_with("[cua] xxx"), "{row:?}");
assert!(!row.contains("F1 Help"), "{row:?}");
fn the_status_hotkeys_use_the_hotkey_role() {
let row: String = (0..80)
.map(|x| terminal.backend().buffer()[(x, 23)].symbol())
.collect();
let at = row.find("F1").expect("the F1 hint is shown");
let cell = &terminal.backend().buffer()[(u16::try_from(at).expect("fits"), 23)];
assert_eq!(cell.fg, app.theme.style(Role::StatusHotkey).fg.expect("fg"));