use std::time::{Duration, Instant}; use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers}; use directories::UserDirs; use pretty_assertions::assert_eq; use rcom::app::{AppState, Command, HelpViewport}; use rcom::domain::{Entry, EntryKind, Location}; use rcom::input::{GG_TIMEOUT, InputOutcome, InputState, PendingKey}; use rcom::ssh::HostPickerState; fn event(code: KeyCode, modifiers: KeyModifiers, kind: KeyEventKind) -> KeyEvent { KeyEvent { code, modifiers, kind, state: KeyEventState::NONE, } } fn press(code: KeyCode) -> KeyEvent { event(code, KeyModifiers::NONE, KeyEventKind::Press) } fn shifted(character: char) -> KeyEvent { event( KeyCode::Char(character), KeyModifiers::SHIFT, KeyEventKind::Press, ) } fn control(character: char) -> KeyEvent { event( KeyCode::Char(character), KeyModifiers::CONTROL, KeyEventKind::Press, ) } fn command(input: &mut InputState, key: KeyEvent, page_size: usize) -> Command { input .handle_key(key, page_size) .into_command() .unwrap_or_else(|| panic!("expected a command for {key:?}")) } fn help_viewport(page_size: usize) -> HelpViewport { HelpViewport { page_size, max_scroll: 100, } } #[test] fn yazi_and_mc_aliases_map_to_the_same_application_commands() { let cases = vec![ (press(KeyCode::Char('j')), Command::MoveSelection(1)), (press(KeyCode::Down), Command::MoveSelection(1)), (press(KeyCode::Char('k')), Command::MoveSelection(-1)), (press(KeyCode::Up), Command::MoveSelection(-1)), (press(KeyCode::Char('h')), Command::Parent), (press(KeyCode::Left), Command::Parent), (press(KeyCode::Char('l')), Command::Enter), (press(KeyCode::Right), Command::Enter), (press(KeyCode::Enter), Command::Enter), (shifted('G'), Command::SelectLast), (press(KeyCode::Home), Command::SelectFirst), (press(KeyCode::End), Command::SelectLast), ( control('b'), Command::PageSelection { page_size: 12, down: false, }, ), ( control('f'), Command::PageSelection { page_size: 12, down: true, }, ), ( control('u'), Command::PageSelection { page_size: 6, down: false, }, ), ( control('d'), Command::PageSelection { page_size: 6, down: true, }, ), ( press(KeyCode::PageUp), Command::PageSelection { page_size: 12, down: false, }, ), ( press(KeyCode::PageDown), Command::PageSelection { page_size: 12, down: true, }, ), (press(KeyCode::Char(' ')), Command::ToggleSelect), (press(KeyCode::Insert), Command::ToggleSelect), (press(KeyCode::Char('v')), Command::ToggleVisual), (control('a'), Command::SelectAll), (shifted('I'), Command::InvertSelection), (shifted('U'), Command::ClearSelection), (press(KeyCode::Char('y')), Command::Yank), (press(KeyCode::Char('x')), Command::Cut), (press(KeyCode::Char('p')), Command::Paste), (press(KeyCode::F(5)), Command::Copy), (press(KeyCode::F(6)), Command::Move), (press(KeyCode::Tab), Command::SwitchPane), ( event(KeyCode::BackTab, KeyModifiers::SHIFT, KeyEventKind::Press), Command::SwitchPane, ), (press(KeyCode::Char('\\')), Command::TogglePaneMode), (press(KeyCode::Char('.')), Command::ToggleHidden), (control('r'), Command::Refresh), (press(KeyCode::Char('s')), Command::OpenHostPicker), (press(KeyCode::Char('m')), Command::OpenMessage), (shifted('?'), Command::ToggleHelp), (press(KeyCode::F(1)), Command::ToggleHelp), (press(KeyCode::Char('q')), Command::Quit), (press(KeyCode::F(10)), Command::Quit), (press(KeyCode::F(3)), Command::OpenPreview), (press(KeyCode::Esc), Command::CloseOverlay), ]; for (key, expected) in cases { let mut input = InputState::new(); assert_eq!(command(&mut input, key, 12), expected, "key: {key:?}"); } } #[test] fn tilde_opens_the_directories_home_location() { let mut input = InputState::new(); let outcome = input.handle_key(shifted('~'), 10); if let Some(directories) = UserDirs::new() { assert_eq!( outcome, InputOutcome::Command(Command::GoHome(Location::local(directories.home_dir()))) ); } else { assert_eq!(outcome, InputOutcome::Ignored); } } #[test] fn entry_action_aliases_map_both_keymaps_without_confusing_uppercase_u() { let cases = [ (press(KeyCode::Char('a')), Command::CreateFile), (shifted('A'), Command::CreateDirectory), (press(KeyCode::F(7)), Command::CreateDirectory), (press(KeyCode::Char('r')), Command::Rename), (press(KeyCode::F(2)), Command::Rename), (press(KeyCode::Char('d')), Command::Trash), (shifted('D'), Command::PermanentDelete), (press(KeyCode::F(8)), Command::PermanentDelete), (press(KeyCode::Char('u')), Command::Undo), (control('z'), Command::Undo), (press(KeyCode::Char('e')), Command::Edit), (press(KeyCode::F(4)), Command::Edit), (shifted('U'), Command::ClearSelection), ]; for (key, expected) in cases { assert_eq!( InputState::new().handle_key(key, 10), InputOutcome::Command(expected) ); } } #[test] fn gg_accepts_a_second_g_within_the_timeout() { let start = Instant::now(); let mut input = InputState::new(); assert_eq!( input.handle_key_at(press(KeyCode::Char('g')), 10, start), InputOutcome::Pending(PendingKey::G) ); assert_eq!(input.pending_key(), Some(PendingKey::G)); assert_eq!( input.handle_key_at( press(KeyCode::Char('g')), 10, start + GG_TIMEOUT - Duration::from_millis(1), ), InputOutcome::Command(Command::SelectFirst) ); assert_eq!(input.pending_key(), None); } #[test] fn gg_expires_at_the_timeout_and_the_next_g_starts_a_new_sequence() { let start = Instant::now(); let mut input = InputState::new(); input.handle_key_at(press(KeyCode::Char('g')), 10, start); assert!(input.expire_pending_at(start + GG_TIMEOUT)); assert_eq!(input.pending_key(), None); assert!(!input.expire_pending_at(start + GG_TIMEOUT)); assert_eq!( input.handle_key_at(press(KeyCode::Char('g')), 10, start), InputOutcome::Pending(PendingKey::G) ); assert_eq!( input.handle_key_at(press(KeyCode::Char('g')), 10, start + GG_TIMEOUT), InputOutcome::Pending(PendingKey::G) ); } #[test] fn escape_cancels_pending_gg_and_non_matching_keys_are_processed_normally() { let start = Instant::now(); let mut input = InputState::new(); input.handle_key_at(press(KeyCode::Char('g')), 10, start); assert_eq!( input.handle_key_at(press(KeyCode::Esc), 10, start), InputOutcome::Command(Command::CloseOverlay) ); assert_eq!(input.pending_key(), None); input.handle_key_at(press(KeyCode::Char('g')), 10, start); assert_eq!( input.handle_key_at(press(KeyCode::Char('j')), 10, start), InputOutcome::Command(Command::MoveSelection(1)) ); assert_eq!(input.pending_key(), None); } #[test] fn repeat_is_limited_to_navigation_and_release_never_emits_commands() { let repeatable = [ event(KeyCode::Char('j'), KeyModifiers::NONE, KeyEventKind::Repeat), event(KeyCode::Down, KeyModifiers::NONE, KeyEventKind::Repeat), event(KeyCode::Char('k'), KeyModifiers::NONE, KeyEventKind::Repeat), event(KeyCode::Up, KeyModifiers::NONE, KeyEventKind::Repeat), event(KeyCode::Home, KeyModifiers::NONE, KeyEventKind::Repeat), event(KeyCode::End, KeyModifiers::NONE, KeyEventKind::Repeat), event(KeyCode::PageDown, KeyModifiers::NONE, KeyEventKind::Repeat), event( KeyCode::Char('d'), KeyModifiers::CONTROL, KeyEventKind::Repeat, ), ]; for key in repeatable { let mut input = InputState::new(); assert!( matches!(input.handle_key(key, 10), InputOutcome::Command(_)), "navigation repeat should be accepted: {key:?}" ); } let non_navigation = [ KeyCode::Char('q'), KeyCode::Char('r'), KeyCode::Char('s'), KeyCode::Char('~'), KeyCode::Char('g'), KeyCode::Char(' '), KeyCode::Char('v'), KeyCode::Char('I'), KeyCode::Char('U'), KeyCode::Char('y'), KeyCode::Char('x'), KeyCode::Char('p'), KeyCode::Char('a'), KeyCode::Char('A'), KeyCode::Char('d'), KeyCode::Char('D'), KeyCode::Char('u'), KeyCode::Char('e'), KeyCode::Insert, KeyCode::F(2), KeyCode::F(3), KeyCode::F(4), KeyCode::F(5), KeyCode::F(6), KeyCode::F(7), KeyCode::F(8), KeyCode::F(10), ]; for code in non_navigation { let mut input = InputState::new(); let key = event(code, KeyModifiers::NONE, KeyEventKind::Repeat); assert_eq!(input.handle_key(key, 10), InputOutcome::Ignored); } let refresh = event( KeyCode::Char('r'), KeyModifiers::CONTROL, KeyEventKind::Repeat, ); assert_eq!( InputState::new().handle_key(refresh, 10), InputOutcome::Ignored ); let select_all = event( KeyCode::Char('a'), KeyModifiers::CONTROL, KeyEventKind::Repeat, ); assert_eq!( InputState::new().handle_key(select_all, 10), InputOutcome::Ignored ); let undo = event( KeyCode::Char('z'), KeyModifiers::CONTROL, KeyEventKind::Repeat, ); assert_eq!( InputState::new().handle_key(undo, 10), InputOutcome::Ignored ); for code in [ KeyCode::Char('j'), KeyCode::Char('q'), KeyCode::Char(' '), KeyCode::Char('v'), KeyCode::Char('y'), KeyCode::Insert, KeyCode::F(2), KeyCode::F(4), KeyCode::F(5), KeyCode::F(6), KeyCode::F(7), KeyCode::F(8), KeyCode::Enter, ] { let mut input = InputState::new(); let key = event(code, KeyModifiers::NONE, KeyEventKind::Release); assert_eq!(input.handle_key(key, 10), InputOutcome::Ignored); } } #[test] fn overlays_capture_selection_clipboard_and_planner_keys() { let selection_keys = [ press(KeyCode::Char(' ')), press(KeyCode::Insert), press(KeyCode::Char('v')), control('a'), shifted('I'), shifted('U'), press(KeyCode::Char('y')), press(KeyCode::Char('x')), press(KeyCode::Char('p')), press(KeyCode::F(5)), press(KeyCode::F(6)), ]; for key in selection_keys { assert!(!matches!( InputState::new().handle_help_key(key, help_viewport(10)), InputOutcome::Command( Command::ToggleSelect | Command::ToggleVisual | Command::SelectAll | Command::InvertSelection | Command::ClearSelection | Command::Yank | Command::Cut | Command::Paste | Command::Copy | Command::Move ) )); assert!(!matches!( InputState::new().handle_message_key(key, 10), InputOutcome::Command( Command::ToggleSelect | Command::ToggleVisual | Command::SelectAll | Command::InvertSelection | Command::ClearSelection | Command::Yank | Command::Cut | Command::Paste | Command::Copy | Command::Move ) )); assert_eq!( InputState::new().handle_preview_key(key), InputOutcome::Ignored ); } let mut picker = HostPickerState::from_strings(vec!["alpha".into()], Vec::new()); for character in ['v', 'y', 'x', 'p', 'I', 'U', ' '] { assert_eq!( InputState::new().handle_host_picker_key(press(KeyCode::Char(character)), &mut picker), InputOutcome::Handled ); } assert_eq!(picker.query(), "vyxpIU "); } #[test] fn entry_prompt_captures_global_aliases_as_text_and_repeats_only_editing() { for character in [ 'q', 'a', 'A', 'r', 'd', 'D', 'u', 'e', 'y', 'x', 'p', 'v', 'U', ' ', ] { let key = event( KeyCode::Char(character), if character.is_uppercase() { KeyModifiers::SHIFT } else { KeyModifiers::NONE }, KeyEventKind::Press, ); assert_eq!( InputState::new().handle_entry_prompt_key(key), InputOutcome::Command(Command::EntryInput(character)) ); } for kind in [KeyEventKind::Press, KeyEventKind::Repeat] { assert_eq!( InputState::new().handle_entry_prompt_key(event( KeyCode::Backspace, KeyModifiers::NONE, kind, )), InputOutcome::Command(Command::EntryBackspace) ); assert_eq!( InputState::new().handle_entry_prompt_key(event( KeyCode::Char('z'), KeyModifiers::NONE, kind, )), InputOutcome::Command(Command::EntryInput('z')) ); } assert_eq!( InputState::new().handle_entry_prompt_key(press(KeyCode::Enter)), InputOutcome::Command(Command::SubmitEntry) ); for key in [ event(KeyCode::Enter, KeyModifiers::NONE, KeyEventKind::Repeat), event( KeyCode::Char('a'), KeyModifiers::NONE, KeyEventKind::Release, ), event( KeyCode::Backspace, KeyModifiers::NONE, KeyEventKind::Release, ), press(KeyCode::F(2)), press(KeyCode::F(7)), ] { assert_eq!( InputState::new().handle_entry_prompt_key(key), InputOutcome::Ignored ); } assert_eq!( InputState::new().handle_entry_prompt_key(press(KeyCode::Esc)), InputOutcome::Command(Command::CloseOverlay) ); } #[test] fn entry_confirmation_and_deferred_overlays_never_repeat_actions() { assert_eq!( InputState::new().handle_entry_confirmation_key(press(KeyCode::Enter)), InputOutcome::Command(Command::ConfirmEntryAction) ); for key in [ event(KeyCode::Enter, KeyModifiers::NONE, KeyEventKind::Repeat), event(KeyCode::Enter, KeyModifiers::NONE, KeyEventKind::Release), event(KeyCode::Char('d'), KeyModifiers::NONE, KeyEventKind::Press), event(KeyCode::F(8), KeyModifiers::NONE, KeyEventKind::Press), ] { assert_eq!( InputState::new().handle_entry_confirmation_key(key), InputOutcome::Ignored ); } for key in [press(KeyCode::Esc), press(KeyCode::Char('q'))] { assert_eq!( InputState::new().handle_entry_confirmation_key(key), InputOutcome::Command(Command::CloseOverlay) ); assert_eq!( InputState::new().handle_deferred_action_key(key), InputOutcome::Command(Command::CloseOverlay) ); } assert_eq!( InputState::new().handle_deferred_action_key(press(KeyCode::Enter)), InputOutcome::Command(Command::ConfirmOperation) ); assert_eq!( InputState::new().handle_deferred_action_key(event( KeyCode::Enter, KeyModifiers::NONE, KeyEventKind::Repeat, )), InputOutcome::Ignored ); } #[test] fn help_overlay_handles_categories_close_quit_and_body_scrolling_aliases() { let cases = [ (press(KeyCode::Esc), Command::CloseOverlay), (press(KeyCode::Char('q')), Command::CloseOverlay), (shifted('?'), Command::ToggleHelp), (press(KeyCode::F(1)), Command::ToggleHelp), (press(KeyCode::F(10)), Command::Quit), (press(KeyCode::Char('h')), Command::SelectHelpCategory(-1)), (press(KeyCode::Left), Command::SelectHelpCategory(-1)), (press(KeyCode::BackTab), Command::SelectHelpCategory(-1)), (press(KeyCode::Char('l')), Command::SelectHelpCategory(1)), (press(KeyCode::Right), Command::SelectHelpCategory(1)), (press(KeyCode::Tab), Command::SelectHelpCategory(1)), ( press(KeyCode::Char('j')), Command::ScrollHelp { delta: 1, max_scroll: 100, }, ), ( press(KeyCode::Down), Command::ScrollHelp { delta: 1, max_scroll: 100, }, ), ( press(KeyCode::Char('k')), Command::ScrollHelp { delta: -1, max_scroll: 100, }, ), ( press(KeyCode::Up), Command::ScrollHelp { delta: -1, max_scroll: 100, }, ), ( press(KeyCode::PageDown), Command::ScrollHelp { delta: 12, max_scroll: 100, }, ), ( press(KeyCode::PageUp), Command::ScrollHelp { delta: -12, max_scroll: 100, }, ), ( control('d'), Command::ScrollHelp { delta: 6, max_scroll: 100, }, ), ( control('u'), Command::ScrollHelp { delta: -6, max_scroll: 100, }, ), ( control('f'), Command::ScrollHelp { delta: 12, max_scroll: 100, }, ), ( control('b'), Command::ScrollHelp { delta: -12, max_scroll: 100, }, ), ]; for (key, expected) in cases { let mut input = InputState::new(); assert_eq!( input.handle_help_key(key, help_viewport(12)), InputOutcome::Command(expected) ); } for key in [ press(KeyCode::Enter), event(KeyCode::Enter, KeyModifiers::NONE, KeyEventKind::Repeat), press(KeyCode::F(3)), ] { assert_eq!( InputState::new().handle_help_key(key, help_viewport(12)), InputOutcome::Ignored ); } } #[test] fn help_repeat_policy_separates_navigation_from_press_only_controls() { for key in [ event(KeyCode::Char('h'), KeyModifiers::NONE, KeyEventKind::Repeat), event(KeyCode::Char('l'), KeyModifiers::NONE, KeyEventKind::Repeat), event(KeyCode::Left, KeyModifiers::NONE, KeyEventKind::Repeat), event(KeyCode::Right, KeyModifiers::NONE, KeyEventKind::Repeat), event(KeyCode::Down, KeyModifiers::NONE, KeyEventKind::Repeat), event(KeyCode::PageDown, KeyModifiers::NONE, KeyEventKind::Repeat), event( KeyCode::Char('f'), KeyModifiers::CONTROL, KeyEventKind::Repeat, ), ] { assert!(matches!( InputState::new().handle_help_key(key, help_viewport(12)), InputOutcome::Command(_) )); } for code in [ KeyCode::Tab, KeyCode::BackTab, KeyCode::Esc, KeyCode::Char('q'), KeyCode::Char('?'), KeyCode::F(1), KeyCode::F(10), ] { assert_eq!( InputState::new().handle_help_key( event(code, KeyModifiers::NONE, KeyEventKind::Repeat), help_viewport(12), ), InputOutcome::Ignored ); assert_eq!( InputState::new().handle_help_key( event(code, KeyModifiers::NONE, KeyEventKind::Release), help_viewport(12), ), InputOutcome::Ignored ); } } #[test] fn message_overlay_uses_the_same_scrolling_model_and_closes_with_escape() { for key in [press(KeyCode::Esc), press(KeyCode::Char('q'))] { assert_eq!( InputState::new().handle_message_key(key, 8), InputOutcome::Command(Command::CloseOverlay) ); } for (key, expected) in [ (press(KeyCode::Char('j')), 1), (press(KeyCode::Char('k')), -1), (press(KeyCode::PageDown), 8), (press(KeyCode::PageUp), -8), (control('d'), 4), (control('u'), -4), ] { assert_eq!( InputState::new().handle_message_key(key, 8), InputOutcome::Command(Command::ScrollOverlay(expected)) ); } } #[test] fn preview_overlay_closes_with_f3_escape_or_q_and_preserves_f10_quit() { for key in [ press(KeyCode::Esc), press(KeyCode::F(3)), press(KeyCode::Char('q')), ] { let mut input = InputState::new(); assert_eq!( input.handle_preview_key(key), InputOutcome::Command(Command::CloseOverlay) ); } assert_eq!( InputState::new().handle_preview_key(press(KeyCode::F(10))), InputOutcome::Command(Command::Quit) ); assert_eq!( InputState::new().handle_preview_key(press(KeyCode::Char('j'))), InputOutcome::Ignored ); } #[test] fn page_half_page_and_boundary_commands_stay_inside_the_list() { let mut app = AppState::new(Location::local("/work")); app.active_pane_mut().entries = (0..5) .map(|index| Entry::new(format!("entry-{index}"), EntryKind::File)) .collect(); app.active_pane_mut().selection.set(Some(0), 5); let mut input = InputState::new(); let page_down = command(&mut input, control('f'), 2); app.reduce(page_down); assert_eq!(app.active_pane().selection.index(), Some(2)); let page_down = command(&mut input, control('f'), 2); app.reduce(page_down); assert_eq!(app.active_pane().selection.index(), Some(4)); let page_down = command(&mut input, press(KeyCode::PageDown), 2); app.reduce(page_down); assert_eq!(app.active_pane().selection.index(), Some(4)); let page_up = command(&mut input, control('b'), 2); app.reduce(page_up); assert_eq!(app.active_pane().selection.index(), Some(2)); let half_down = command(&mut input, control('d'), 4); app.reduce(half_down); assert_eq!(app.active_pane().selection.index(), Some(4)); let half_up = command(&mut input, control('u'), 4); app.reduce(half_up); assert_eq!(app.active_pane().selection.index(), Some(2)); let page_up = command(&mut input, press(KeyCode::PageUp), 20); app.reduce(page_up); assert_eq!(app.active_pane().selection.index(), Some(0)); let page_up = command(&mut input, control('b'), 2); app.reduce(page_up); assert_eq!(app.active_pane().selection.index(), Some(0)); let end = command(&mut input, press(KeyCode::End), 10); app.reduce(end); assert_eq!(app.active_pane().selection.index(), Some(4)); let home = command(&mut input, press(KeyCode::Home), 10); app.reduce(home); assert_eq!(app.active_pane().selection.index(), Some(0)); }