use super::*;
#[test]
fn a_late_location_picker_reply_does_not_clobber_an_open_menu() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let items = vec![(
String::from("a.rs:1"),
MenuAction::GoToLocation {
path: tmp.path().join("a.rs"),
line: 0,
col: 0,
},
)];
app.context_menu = Some(ContextMenu::flat(
(1, 1),
items.clone(),
tmp.path().to_path_buf(),
));
let menu_before = app.context_menu.as_ref().map(|m| m.items.len());
app.open_location_picker(vec![(tmp.path().join("a.rs"), 0, 0)], "references");
assert_eq!(
app.context_menu.as_ref().map(|m| m.items.len()),
menu_before,
"an open menu survives a late reply"
);
assert_eq!(app.status, "Locations ready, but another menu is open");
app.context_menu = None;
app.open_location_picker(vec![(tmp.path().join("a.rs"), 0, 0)], "references");
assert!(
app.context_menu.is_some(),
"a free slot presents the picker"
);
assert_eq!(app.status, "1 references");
}
#[test]
fn ctrl_minus_and_the_palette_both_navigate_back() {
let tmp = tempfile::tempdir().unwrap();
let a = tmp.path().join("a.rs");
let b = tmp.path().join("b.rs");
std::fs::write(&a, "fn main() {}\nlet x = helper();\n").unwrap();
std::fs::write(&b, "fn helper() {}\n").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open_pinned(&a).unwrap();
app.editor.cursor_row = 1;
app.focus_pane(Pane::Editor);
app.go_to_definition(b.clone(), 0, 3);
assert_eq!(app.editor.path.as_deref(), Some(b.as_path()));
app.handle_key(key(KeyCode::Char('-'), KeyModifiers::CONTROL))
.unwrap();
assert_eq!(
app.editor.path.as_deref(),
Some(a.as_path()),
"Ctrl+- goes back"
);
app.go_to_definition(b.clone(), 0, 3);
app.handle_key(key(KeyCode::Char('_'), KeyModifiers::CONTROL))
.unwrap();
assert_eq!(
app.editor.path.as_deref(),
Some(a.as_path()),
"the legacy Ctrl+- spelling goes back too"
);
app.go_to_definition(b.clone(), 0, 3);
app.run_command(crate::widgets::command_palette::Command::NavigateBack);
assert_eq!(
app.editor.path.as_deref(),
Some(a.as_path()),
"the Go Back palette command goes back"
);
}
#[test]
fn the_palette_goes_to_implementations_at_the_caret() {
let tmp = tempfile::tempdir().unwrap();
let mut app = app_with_open_file(tmp.path(), "lib.rs", "trait Shape {}\n");
app.editor.cursor_col = 7;
assert!(app.implementation_request_id.is_none());
assert!(run_palette_row_titled(&mut app, "Go to Implementations"));
assert!(
app.implementation_request_id.is_some(),
"the request went to the language servers"
);
}
#[test]
fn the_palette_opens_the_zoxide_jump_from_any_pane() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.focus_pane(Pane::Editor);
assert!(run_palette_row_titled(
&mut app,
"Explorer: Jump to Directory (zoxide)"
));
assert!(app.zoxide_jump.is_some());
}
#[test]
fn the_palette_focuses_the_left_and_right_editor_groups() {
let tmp = tempfile::tempdir().unwrap();
let mut app = app_with_open_file(tmp.path(), "a.txt", "left");
app.split_editor();
assert_eq!(app.editor_layout.active_dfs_index(), 1);
app.focus_pane(Pane::Tree);
assert!(run_palette_row_titled(
&mut app,
"View: Focus Left Editor Group"
));
assert_eq!(app.editor_layout.active_dfs_index(), 0);
assert!(app.focus == Pane::Editor);
assert!(run_palette_row_titled(
&mut app,
"View: Focus Right Editor Group"
));
assert_eq!(app.editor_layout.active_dfs_index(), 1);
}
#[test]
fn the_palette_focuses_the_next_and_previous_terminal() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.split_terminal().unwrap();
app.split_terminal().unwrap();
app.active_terminal = 0;
app.focus_pane(Pane::Editor);
assert!(run_palette_row_titled(
&mut app,
"Terminal: Focus Next Terminal"
));
assert_eq!(app.active_terminal, 1);
assert!(app.focus == Pane::Terminal);
assert!(run_palette_row_titled(
&mut app,
"Terminal: Focus Previous Terminal"
));
assert_eq!(app.active_terminal, 0);
assert!(run_palette_row_titled(
&mut app,
"Terminal: Focus Previous Terminal"
));
assert_eq!(app.active_terminal, 2, "it wraps like Cmd+[");
}
#[test]
fn the_palette_lists_every_terminal_action() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
for title in [
"Terminal: Clear",
"Terminal: Rename",
"Terminal: Kill the Active Terminal Instance",
"Terminal: Split Terminal",
"Terminal: Create New Terminal (With Profile)",
"Terminal: Quick Select",
"Terminal: Enter Copy Mode",
"Terminal: Show Command History",
"Terminal: Open Scrollback in Editor",
] {
app.handle_key(key(
KeyCode::Char('p'),
KeyModifiers::SUPER | KeyModifiers::SHIFT,
))
.unwrap();
for c in title.chars() {
app.handle_key(key(KeyCode::Char(c), KeyModifiers::NONE))
.unwrap();
}
let palette = app.command_palette.as_ref().expect("the palette opened");
assert!(
palette.results.iter().any(|i| i.title() == title),
"{title} is in the palette"
);
app.handle_key(key(KeyCode::Esc, KeyModifiers::NONE))
.unwrap();
}
}
#[test]
fn the_palette_selects_the_whole_buffer() {
let tmp = tempfile::tempdir().unwrap();
let mut app = app_with_open_file(tmp.path(), "notes.txt", "one\ntwo");
app.focus_pane(Pane::Tree);
assert!(run_palette_row_titled(&mut app, "Select All"));
assert!(app.focus == Pane::Editor);
assert_eq!(app.editor.selection_text(), "one\ntwo");
}
#[test]
fn select_all_is_one_palette_command() {
use crate::widgets::command_palette::{ALL_COMMANDS, Command};
assert_eq!(Command::from_id("select_all"), Some(Command::SelectAll));
assert_eq!(Command::SelectAll.title(), "Select All");
assert_eq!(Command::SelectAll.keybinding_hint(), "Cmd+A");
assert_eq!(
ALL_COMMANDS
.iter()
.filter(|c| c.title() == "Select All")
.count(),
1
);
}
#[test]
fn the_palette_runs_the_markdown_code_block_at_the_caret() {
let tmp = tempfile::tempdir().unwrap();
let mut app = markdown_fence_app(tmp.path());
let panes = app.terminals.len();
assert!(run_palette_row_titled(
&mut app,
"Markdown: Run Code Block at Cursor"
));
let pending = app.pending_run_block.as_ref().expect("it confirms first");
assert_eq!(pending.pane_name, "R.md:1");
app.handle_key(key(KeyCode::Char('n'), KeyModifiers::NONE))
.unwrap();
assert!(app.pending_run_block.is_none());
assert_eq!(app.terminals.len(), panes, "N runs nothing");
}
#[test]
fn insert_line_below_and_above_are_palette_commands() {
use crate::widgets::command_palette::Command;
assert_eq!(
Command::from_id("insert_line_below"),
Some(Command::InsertLineBelow)
);
assert_eq!(
Command::from_id("insert_line_above"),
Some(Command::InsertLineAbove)
);
let tmp = tempfile::tempdir().unwrap();
let mut app = app_with_open_file(tmp.path(), "i.py", "a = 1\nb = 2\n");
app.editor.cursor_col = 2;
assert!(run_palette_row_titled(&mut app, "Insert Line Below"));
assert_eq!(app.editor.lines, ["a = 1", "", "b = 2"]);
app.editor.cursor_row = 2;
assert!(run_palette_row_titled(&mut app, "Insert Line Above"));
assert_eq!(app.editor.lines, ["a = 1", "", "", "b = 2"]);
assert_eq!(app.editor.cursor_row, 2);
}
#[test]
fn go_to_next_change_is_a_palette_command() {
use crate::widgets::command_palette::Command;
let (mut app, _t) = app_with_change_bars();
app.editor.cursor_row = 0;
app.run_command(Command::from_id("next_change").expect("next_change id"));
assert_eq!(app.editor.cursor_row, 4);
app.run_command(Command::from_id("previous_change").expect("previous_change id"));
assert_eq!(app.editor.cursor_row, 8, "wraps back to the last change");
let mut report = crate::import_vscode::Report::default();
crate::import_vscode::convert_keybindings(
&serde_json::json!([
{ "key": "alt+f5", "command": "workbench.action.editor.nextChange" }
]),
&mut report,
);
assert!(
report
.keybindings
.iter()
.any(|(_, c, _)| c == "next_change"),
"{:?}",
report.keybindings
);
}
#[test]
fn cmd_p_opens_the_file_finder_modal_for_quick_open() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("alpha.rs"), "").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.handle_key(key(KeyCode::Char('p'), KeyModifiers::SUPER))
.unwrap();
assert!(
app.file_finder.is_some(),
"Cmd+P must open the Quick Open file finder modal so the user can type a name and jump to a file the way VS Code does"
);
}
#[test]
fn ctrl_p_opens_the_file_finder_modal_on_systems_without_super() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("alpha.rs"), "").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.handle_key(key(KeyCode::Char('p'), KeyModifiers::CONTROL))
.unwrap();
assert!(
app.file_finder.is_some(),
"Ctrl+P must also open the Quick Open finder so the chord works on Linux / iTerm2 sessions that report CONTROL rather than SUPER"
);
}
#[test]
fn plain_p_keystroke_does_not_open_the_file_finder() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("alpha.rs"), "").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.handle_key(key(KeyCode::Char('p'), KeyModifiers::NONE))
.unwrap();
assert!(
app.file_finder.is_none(),
"an unmodified 'p' must reach the editor / tree typeahead and never trigger the modal"
);
}
#[test]
fn esc_closes_the_file_finder_modal() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("alpha.rs"), "").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.handle_key(key(KeyCode::Char('p'), KeyModifiers::SUPER))
.unwrap();
app.handle_key(key(KeyCode::Esc, KeyModifiers::NONE))
.unwrap();
assert!(
app.file_finder.is_none(),
"Esc must dismiss the file finder"
);
}
#[test]
fn the_palette_owns_the_caret_while_it_is_open() {
let tmp = tempfile::tempdir().unwrap();
let a = tmp.path().join("a.txt");
std::fs::write(&a, "alpha\nbeta\n").unwrap();
for (theme, loch) in [
(crate::theme::Theme::from_id("loch"), true),
(crate::theme::Theme::BLACK, false),
] {
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.apply_theme(theme);
app.editor.open_in_new_tab(&a).unwrap();
app.focus_pane(Pane::Editor);
app.screen_reader = true;
app.open_command_palette();
for c in "tog".chars() {
app.handle_key(key(KeyCode::Char(c), KeyModifiers::NONE))
.unwrap();
}
let mut term = ratatui::Terminal::new(ratatui::backend::TestBackend::new(120, 40)).unwrap();
term.draw(|f| app.render(f)).unwrap();
let palette = app.command_palette.as_ref().unwrap();
let rect = palette.last_rect;
if loch {
assert_eq!(
app.quick_input_caret(),
Some((rect.x + 7, rect.y + 1)),
"after `tog` on the input row"
);
assert!(app.cursor_should_be_visible());
} else {
assert_eq!(app.quick_input_caret(), None, "a painted caret block");
assert!(!app.quick_input_owns_caret(), "boxed caret rules as before");
}
}
}
#[test]
fn a_screen_reader_keeps_a_visible_caret_in_the_loch_palette_query() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.screen_reader = true;
app.open_command_palette();
for c in "tog".chars() {
app.handle_key(key(KeyCode::Char(c), KeyModifiers::NONE))
.unwrap();
}
let mut term = ratatui::Terminal::new(ratatui::backend::TestBackend::new(120, 40)).unwrap();
term.draw(|f| app.render(f)).unwrap();
let (cx, cy) = app
.quick_input_caret()
.expect("Loch hands the query a caret");
let buf = term.backend().buffer();
assert!(
buf[(cx, cy)].modifier.contains(Modifier::REVERSED),
"the query's caret is painted while the announcement has the terminal's"
);
let parked = term.get_cursor_position().unwrap();
assert_eq!(
Some((parked.x, parked.y)),
app.announce_pos,
"the terminal's caret stays on the announcement"
);
}
#[test]
fn the_quick_input_scrim_is_withheld_where_inline_images_draw() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("alpha.rs"), "fn main() {}\n").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.open_file_finder();
let draw = |app: &mut App| {
let mut term = ratatui::Terminal::new(ratatui::backend::TestBackend::new(120, 40)).unwrap();
term.draw(|f| app.render(f)).unwrap();
term.backend().buffer().clone()
};
let dimmed = draw(&mut app);
app.inline_protocol = crate::iterm2_inline::InlineImageProtocol::Kitty;
let lit = draw(&mut app);
let card = app.file_finder.as_ref().unwrap().last_rect;
let luma = |c: ratatui::style::Color| match c {
ratatui::style::Color::Rgb(r, g, b) => u32::from(r) + u32::from(g) + u32::from(b),
_ => 0,
};
let status = 39;
assert!(status > card.bottom(), "the status bar is off the card");
let darker = (0..120).any(|x| luma(dimmed[(x, status)].fg) < luma(lit[(x, status)].fg));
assert!(darker, "the scrim sinks the status bar");
for x in 0..120 {
if matches!(lit[(x, status)].fg, ratatui::style::Color::Rgb(..)) {
assert!(
luma(dimmed[(x, status)].fg) <= luma(lit[(x, status)].fg),
"nothing behind the card gets brighter ({x})"
);
}
}
}
#[test]
fn quick_open_popup_follows_the_light_theme() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("alpha.rs"), "").unwrap();
std::fs::write(tmp.path().join("beta.rs"), "").unwrap();
for (theme, want_bg, dark_text) in [
(
crate::theme::Theme::DARK_BLUE,
ratatui::style::Color::Rgb(0x16, 0x18, 0x1f),
false,
),
(
crate::theme::Theme::from_id("light"),
ratatui::style::Color::Rgb(0xf8, 0xf8, 0xf8),
true,
),
] {
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.apply_theme(theme);
app.handle_key(key(KeyCode::Char('p'), KeyModifiers::SUPER))
.unwrap();
let backend = ratatui::backend::TestBackend::new(120, 40);
let mut term = ratatui::Terminal::new(backend).unwrap();
term.draw(|frame| app.render(frame)).unwrap();
let finder = app.file_finder.as_ref().unwrap();
let (x, y) = (finder.last_rect.x + 4, finder.last_rect.y + 4);
let cell = &term.backend().buffer()[(x, y)];
assert_eq!(
cell.bg,
want_bg,
"{}: the finder body must wear the theme's popup fill",
theme.id()
);
if dark_text {
let ratatui::style::Color::Rgb(r, g, b) = cell.fg else {
panic!("light finder text must be an Rgb color, got {:?}", cell.fg)
};
let luma = 0.299 * f32::from(r) + 0.587 * f32::from(g) + 0.114 * f32::from(b);
assert!(
luma < 128.0,
"light finder text must be dark on the white popup, got {:?}",
cell.fg
);
}
}
}
#[test]
fn clicking_a_file_finder_row_opens_that_file() {
use crossterm::event::{MouseButton, MouseEventKind};
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("alpha.rs"), "").unwrap();
std::fs::write(tmp.path().join("beta.rs"), "").unwrap();
std::fs::write(tmp.path().join("gamma.rs"), "").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.handle_key(key(KeyCode::Char('p'), KeyModifiers::SUPER))
.unwrap();
let backend = ratatui::backend::TestBackend::new(120, 40);
let mut term = ratatui::Terminal::new(backend).unwrap();
term.draw(|frame| app.render(frame)).unwrap();
let finder = app.file_finder.as_ref().unwrap();
assert!(
finder.results.len() >= 3,
"the three seeded files must all be indexed"
);
let col = finder.last_rect.x + 4;
let row = finder.last_rect.y + 3 + 1;
let expected = finder.results[1].entry.path.clone();
app.handle_mouse(mouse(MouseEventKind::Down(MouseButton::Left), col, row));
assert!(
app.file_finder.is_none(),
"a click on a result row must confirm the pick and dismiss the finder"
);
assert_eq!(
app.editor.path.as_deref(),
Some(expected.as_path()),
"the clicked row's file must be the one that opens"
);
}
#[test]
fn clicking_the_file_finder_prompt_line_is_inert() {
use crossterm::event::{MouseButton, MouseEventKind};
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("alpha.rs"), "").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.handle_key(key(KeyCode::Char('p'), KeyModifiers::SUPER))
.unwrap();
let backend = ratatui::backend::TestBackend::new(120, 40);
let mut term = ratatui::Terminal::new(backend).unwrap();
term.draw(|frame| app.render(frame)).unwrap();
let finder = app.file_finder.as_ref().unwrap();
let col = finder.last_rect.x + 4;
let prompt_row = finder.last_rect.y + 1;
app.handle_mouse(mouse(
MouseEventKind::Down(MouseButton::Left),
col,
prompt_row,
));
assert!(
app.file_finder.is_some(),
"a click on the query line must not dismiss the finder"
);
assert_eq!(
app.editor.path, None,
"a click on the query line must not open anything"
);
}
#[test]
fn clicking_below_the_last_file_finder_result_is_inert() {
use crossterm::event::{MouseButton, MouseEventKind};
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("alpha.rs"), "").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.handle_key(key(KeyCode::Char('p'), KeyModifiers::SUPER))
.unwrap();
let backend = ratatui::backend::TestBackend::new(120, 40);
let mut term = ratatui::Terminal::new(backend).unwrap();
term.draw(|frame| app.render(frame)).unwrap();
let finder = app.file_finder.as_ref().unwrap();
let results = finder.results.len();
let col = finder.last_rect.x + 4;
let row = finder.last_rect.y + 3 + results as u16;
assert!(
row < finder.last_rect.y + finder.last_rect.height - 1,
"the empty row must land inside the popup for this test to mean anything"
);
app.handle_mouse(mouse(MouseEventKind::Down(MouseButton::Left), col, row));
assert!(
app.file_finder.is_some(),
"a click on an empty list row must not dismiss the finder"
);
assert_eq!(
app.editor.path, None,
"a click on an empty list row must not open anything"
);
}
#[test]
fn clicking_a_command_palette_row_runs_that_command() {
use crossterm::event::{MouseButton, MouseEventKind};
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.handle_key(key(
KeyCode::Char('p'),
KeyModifiers::SUPER | KeyModifiers::SHIFT,
))
.unwrap();
assert!(
app.command_palette.is_some(),
"Cmd+Shift+P must open the palette"
);
for c in "keyboard shortcuts reference".chars() {
app.handle_key(key(KeyCode::Char(c), KeyModifiers::NONE))
.unwrap();
}
let backend = ratatui::backend::TestBackend::new(120, 40);
let mut term = ratatui::Terminal::new(backend).unwrap();
term.draw(|frame| app.render(frame)).unwrap();
let palette = app.command_palette.as_ref().unwrap();
assert!(
!palette.results.is_empty(),
"the query must keep the Help: Keyboard Shortcuts Reference command"
);
let col = palette.last_rect.x + 4;
let row = palette.last_rect.y + 3;
app.handle_mouse(mouse(MouseEventKind::Down(MouseButton::Left), col, row));
assert!(
app.command_palette.is_none(),
"a click on a palette row must run the command and dismiss the palette"
);
assert!(
app.shortcuts_modal.is_some(),
"the clicked command (Help: Keyboard Shortcuts Reference) must actually run"
);
}
#[test]
fn typing_in_file_finder_fuzzy_filters_the_result_list() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("alpha.rs"), "").unwrap();
std::fs::create_dir(tmp.path().join("sub")).unwrap();
std::fs::write(tmp.path().join("sub").join("beta.rs"), "").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.handle_key(key(KeyCode::Char('p'), KeyModifiers::SUPER))
.unwrap();
app.handle_key(key(KeyCode::Char('b'), KeyModifiers::NONE))
.unwrap();
app.handle_key(key(KeyCode::Char('e'), KeyModifiers::NONE))
.unwrap();
let finder = app
.file_finder
.as_ref()
.expect("finder must still be open while typing");
let names: Vec<String> = finder
.visible_results()
.iter()
.map(|r| r.entry.rel.clone())
.collect();
assert!(
names.iter().any(|n| n == "sub/beta.rs"),
"typing 'be' must keep sub/beta.rs in the result list — got {names:?}"
);
assert!(
!names.iter().any(|n| n == "alpha.rs"),
"typing 'be' must drop alpha.rs because 'be' is not a subsequence of 'alpha.rs' — got {names:?}"
);
}
#[test]
fn enter_in_file_finder_opens_the_selected_file_and_closes_the_modal() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("alpha.rs"), "fn main() {}").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.handle_key(key(KeyCode::Char('p'), KeyModifiers::SUPER))
.unwrap();
app.handle_key(key(KeyCode::Char('a'), KeyModifiers::NONE))
.unwrap();
app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
.unwrap();
assert!(
app.file_finder.is_none(),
"Enter must close the modal after opening the file"
);
let opened = app
.editor
.path
.as_ref()
.expect("the active tab must now have a path");
assert_eq!(
opened.file_name().and_then(|n| n.to_str()),
Some("alpha.rs"),
"Enter must open the highlighted entry (alpha.rs) into the active editor tab"
);
}
#[test]
fn quick_open_with_a_line_range_lands_on_the_line_and_selects_the_range() {
let tmp = tempfile::tempdir().unwrap();
let body: String = (1..=300).map(|i| format!("line {i}\n")).collect();
std::fs::write(tmp.path().join("alpha.rs"), &body).unwrap();
std::fs::write(tmp.path().join("beta.rs"), "fn b() {}\n").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let type_and_enter = |app: &mut App, text: &str| {
app.handle_key(key(KeyCode::Char('p'), KeyModifiers::SUPER))
.unwrap();
type_keys(app, text);
app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
.unwrap();
};
type_and_enter(&mut app, "alpha:236-239");
assert!(app.file_finder.is_none(), "Enter closes the modal");
assert_eq!(
app.editor
.path
.as_deref()
.and_then(|p| p.file_name())
.and_then(|n| n.to_str()),
Some("alpha.rs"),
"the file part of the query is what gets opened"
);
let sel = app
.editor
.selection
.expect("the range is selected so it reads as highlighted");
let ((r0, c0), (r1, _)) = sel.normalised();
assert_eq!(
(r0, c0),
(235, 0),
"the selection starts at the top of line 236"
);
assert_eq!(r1, 238, "and reaches line 239");
assert_eq!(
(app.editor.cursor_row, app.editor.cursor_col),
sel.head,
"the cursor is at the head of the range"
);
assert_eq!(
app.editor.cursor_row, 235,
"and the head is the landing line"
);
assert!(
app.status.ends_with("alpha.rs:236-239"),
"the status names the range: {:?}",
app.status
);
app.handle_key(key(KeyCode::Up, KeyModifiers::SHIFT))
.unwrap();
let ((r0, _), (r1, _)) = app.editor.selection.expect("still selected").normalised();
assert_eq!(
(r0, r1),
(234, 238),
"Shift+Up extends the range from the landing line"
);
let page = app.editor.page_size().max(1);
assert!(
app.editor.scroll <= 235 && 235 < app.editor.scroll + page,
"the landing line is on screen: scroll={} page={page}",
app.editor.scroll
);
let extended = app.editor.selection;
type_and_enter(&mut app, "alpha");
assert_eq!(
app.editor.selection, extended,
"a range the user extended survives an unhinted re-pick"
);
type_and_enter(&mut app, "alpha:236-239");
type_and_enter(&mut app, "alpha");
assert!(
app.editor.selection.is_none(),
"an unhinted pick clears the range a hinted pick left"
);
type_and_enter(&mut app, "alpha:99999999999999999999");
assert_eq!(
app.editor.cursor_row, 299,
"a saturated line lands on the last line"
);
assert!(
app.status.ends_with("alpha.rs:300"),
"the status names the landed line, not the typed one: {:?}",
app.status
);
type_and_enter(&mut app, "alpha:280-99999999999999999999");
assert!(
app.status.ends_with("alpha.rs:280-300"),
"both ends of the range are clamped to the file: {:?}",
app.status
);
let mine = crate::widgets::editor::EditorSelection {
anchor: (10, 0),
head: (11, 3),
};
app.editor.selection = Some(mine);
type_and_enter(&mut app, "alpha");
assert_eq!(
app.editor.selection,
Some(mine),
"an unhinted re-pick keeps a selection the user made"
);
app.editor.cursor_row = 5;
app.editor.add_cursor_below();
assert!(
app.editor.has_multi_cursor(),
"fixture: a second caret exists"
);
type_and_enter(&mut app, "alpha:236-239");
assert!(
!app.editor.has_multi_cursor(),
"a pick collapses the carets along with the selection"
);
type_and_enter(&mut app, "alpha:236:0");
assert_eq!(
(app.editor.cursor_row, app.editor.cursor_col),
(235, 0),
"`:236:0` lands on line 236 at the start"
);
type_and_enter(&mut app, "alpha:12");
assert_eq!(
app.editor.cursor_row, 11,
"a single line lands on that line"
);
assert!(
app.editor.selection.is_none(),
"a single line is a place to land, not a range to select"
);
assert!(
app.status.ends_with("alpha.rs:12"),
"the status names the line: {:?}",
app.status
);
type_and_enter(&mut app, "alpha:5000");
assert_eq!(
app.editor.cursor_row, 299,
"a line past the end lands on the last line"
);
let backend = ratatui::backend::TestBackend::new(120, 40);
let mut term = ratatui::Terminal::new(backend).unwrap();
term.draw(|f| app.render(f)).unwrap();
type_and_enter(&mut app, "alpha:100-200");
term.draw(|f| app.render(f)).unwrap();
let page = app.editor.page_size().max(1);
assert!(
app.editor.scroll <= 99 && 99 < app.editor.scroll + page,
"line 100 is on screen after the paint: scroll={} page={page}",
app.editor.scroll
);
let ((r0, _), (r1, _)) = app
.editor
.selection
.expect("the tall range is selected")
.normalised();
assert_eq!((r0, r1), (99, 199));
type_and_enter(&mut app, "alpha:236:7");
assert_eq!(
(app.editor.cursor_row, app.editor.cursor_col),
(235, 6),
"`:line:col` lands on the column too"
);
use crossterm::event::{MouseButton, MouseEventKind};
app.handle_key(key(KeyCode::Char('p'), KeyModifiers::SUPER))
.unwrap();
for c in "alpha:40".chars() {
app.handle_key(key(KeyCode::Char(c), KeyModifiers::NONE))
.unwrap();
}
let backend = ratatui::backend::TestBackend::new(120, 40);
let mut term = ratatui::Terminal::new(backend).unwrap();
term.draw(|frame| app.render(frame)).unwrap();
let finder = app.file_finder.as_ref().unwrap();
assert_eq!(
finder.results.len(),
1,
"the file part alone matches alpha.rs"
);
let (col, row) = (finder.last_rect.x + 4, finder.last_rect.y + 3);
app.handle_mouse(mouse(MouseEventKind::Down(MouseButton::Left), col, row));
assert!(app.file_finder.is_none(), "the click confirms the pick");
assert_eq!(
app.editor.cursor_row, 39,
"a clicked row lands on the hinted line"
);
}
#[test]
fn down_arrow_moves_selection_in_the_file_finder() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("a1.rs"), "").unwrap();
std::fs::write(tmp.path().join("a2.rs"), "").unwrap();
std::fs::write(tmp.path().join("a3.rs"), "").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.handle_key(key(KeyCode::Char('p'), KeyModifiers::SUPER))
.unwrap();
let before = app.file_finder.as_ref().unwrap().selected_index();
app.handle_key(key(KeyCode::Down, KeyModifiers::NONE))
.unwrap();
let after = app.file_finder.as_ref().unwrap().selected_index();
assert_eq!(
after,
before + 1,
"Down arrow must move the selection cursor one row"
);
}
#[test]
fn backspace_removes_a_character_from_the_finder_query() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("alpha.rs"), "").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.handle_key(key(KeyCode::Char('p'), KeyModifiers::SUPER))
.unwrap();
app.handle_key(key(KeyCode::Char('a'), KeyModifiers::NONE))
.unwrap();
app.handle_key(key(KeyCode::Char('b'), KeyModifiers::NONE))
.unwrap();
app.handle_key(key(KeyCode::Backspace, KeyModifiers::NONE))
.unwrap();
assert_eq!(app.file_finder.as_ref().unwrap().query, "a");
}
#[test]
fn opening_the_file_finder_arms_one_image_clear_so_iterm_evicts_cached_osc_1337_cells() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.handle_key(key(KeyCode::Char('p'), KeyModifiers::SUPER))
.unwrap();
assert!(
app.consume_popup_image_clear(),
"Cmd+P must arm terminal.clear() so iTerm2's image cache (activity bar icons, welcome wordmark, editor preview) is wiped before the modal paints, otherwise those cached image cells bleed through the file list"
);
assert!(
!app.consume_popup_image_clear(),
"the flag is one-shot — a second consume call must return false so the driver does not re-clear every frame"
);
}
#[test]
fn bracketed_paste_into_open_file_finder_appends_to_the_query_not_the_editor() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("alpha.rs"), "").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.focus_pane(Pane::Editor);
app.editor.lines = vec![String::from("hello")];
app.handle_key(key(KeyCode::Char('p'), KeyModifiers::SUPER))
.unwrap();
app.handle_paste("alpha");
assert_eq!(
app.file_finder.as_ref().unwrap().query,
"alpha",
"an iTerm2 bracketed paste while the finder is open must land in the finder query so the user can Cmd+P then Cmd+V a filename"
);
assert_eq!(
app.editor.lines,
vec!["hello"],
"the editor pane behind the modal must not receive the pasted text"
);
}
#[test]
fn cmd_v_keystroke_in_file_finder_reads_clipboard_into_the_query() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("alpha.rs"), "").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.clipboard_reader = || Some(String::from("alpha"));
app.handle_key(key(KeyCode::Char('p'), KeyModifiers::SUPER))
.unwrap();
app.handle_key(key(KeyCode::Char('v'), KeyModifiers::SUPER))
.unwrap();
assert_eq!(
app.file_finder.as_ref().unwrap().query,
"alpha",
"Cmd+V inside the finder must read the system clipboard and append every char to the query — this covers terminals that disable bracketed paste or route Cmd+V via CSI-u"
);
}
#[test]
fn file_finder_paste_strips_control_chars_so_a_pasted_newline_does_not_submit_garbage() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("alpha.rs"), "").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.handle_key(key(KeyCode::Char('p'), KeyModifiers::SUPER))
.unwrap();
app.handle_paste("al\nph\ta");
assert_eq!(
app.file_finder.as_ref().unwrap().query,
"alpha",
"control chars in pasted text must be filtered out — a stray newline must not end the line and a tab must not navigate"
);
}
#[test]
fn file_finder_modal_swallows_typed_characters_so_editor_does_not_receive_them() {
let tmp = tempfile::tempdir().unwrap();
std::fs::write(tmp.path().join("alpha.rs"), "").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.focus_pane(Pane::Editor);
app.editor.lines = vec![String::from("hello")];
app.editor.cursor_col = 5;
app.handle_key(key(KeyCode::Char('p'), KeyModifiers::SUPER))
.unwrap();
app.handle_key(key(KeyCode::Char('x'), KeyModifiers::NONE))
.unwrap();
assert_eq!(
app.editor.lines,
vec!["hello"],
"while the file finder is open, typed letters must not leak into the editor buffer"
);
assert_eq!(
app.file_finder.as_ref().unwrap().query,
"x",
"typed letters must go into the finder query instead"
);
}
#[test]
fn cmd_p_index_drops_a_file_deleted_off_disk() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir(tmp.path().join(".git")).unwrap();
std::fs::write(tmp.path().join("alpha.rs"), b"").unwrap();
let target = tmp.path().join("doomed.rs");
std::fs::write(&target, b"").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let mut waited = 0u32;
loop {
app.poll_file_finder_index();
if let Some(idx) = app.file_finder_index.as_ref()
&& idx.iter().any(|e| e.rel == "doomed.rs")
{
break;
}
if waited >= 5000 {
panic!("initial index must contain doomed.rs within 5s");
}
std::thread::sleep(std::time::Duration::from_millis(20));
waited += 20;
}
for _ in 0..20 {
let _ = app.drain_fs_events();
std::thread::sleep(std::time::Duration::from_millis(10));
}
std::fs::remove_file(&target).unwrap();
let mut gone = false;
for _ in 0..400 {
let _ = app.drain_fs_events();
app.poll_file_finder_index();
if let Some(idx) = app.file_finder_index.as_ref()
&& !idx.iter().any(|e| e.rel == "doomed.rs")
{
gone = true;
break;
}
std::thread::sleep(std::time::Duration::from_millis(10));
}
assert!(
gone,
"deleted file must drop out of the Cmd+P index after the FS-event-driven rebuild lands"
);
}
#[test]
fn open_cmd_p_finder_re_ranks_in_place_when_the_index_swaps() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir(tmp.path().join(".git")).unwrap();
std::fs::write(tmp.path().join("alpha.rs"), b"").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let mut waited = 0u32;
loop {
app.poll_file_finder_index();
if app.file_finder_index.is_some() {
break;
}
if waited >= 5000 {
panic!("initial index must land within 5s");
}
std::thread::sleep(std::time::Duration::from_millis(20));
waited += 20;
}
app.open_file_finder();
for c in "resolver.ts".chars() {
app.file_finder.as_mut().unwrap().push_char(c);
}
assert!(
app.file_finder
.as_ref()
.unwrap()
.visible_results()
.is_empty(),
"resolver.ts is not on disk yet; finder must show no matches before the file is created"
);
let new_dir = tmp
.path()
.join("packages/ant-ts-lib-noggin/src/entities/files");
std::fs::create_dir_all(&new_dir).unwrap();
std::fs::write(new_dir.join("resolver.ts"), b"").unwrap();
let mut surfaced = false;
for _ in 0..400 {
let _ = app.drain_fs_events();
app.poll_file_finder_index();
let finder = app.file_finder.as_ref().unwrap();
if finder
.visible_results()
.iter()
.any(|r| r.entry.rel == "packages/ant-ts-lib-noggin/src/entities/files/resolver.ts")
{
surfaced = true;
break;
}
std::thread::sleep(std::time::Duration::from_millis(10));
}
assert!(
surfaced,
"an open Cmd+P finder must re-rank against the rebuilt index without the user closing and reopening it"
);
}
#[test]
fn the_profile_picker_anchors_under_the_pane_that_owns_the_caret() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.split_terminal().unwrap();
app.split_terminal().unwrap();
app.terminal_pane_maximized = true;
assert_eq!(
app.active_terminal, 2,
"the newest pane is the maximized one"
);
let backend = ratatui::backend::TestBackend::new(180, 40);
let mut term = ratatui::Terminal::new(backend).unwrap();
term.draw(|f| app.render(f)).unwrap();
let caret = app.terminal_profile_buttons[2];
assert!(caret.width > 0, "the maximized pane paints a profile caret");
assert_eq!(
app.terminal_profile_buttons[0],
ratatui::layout::Rect::default(),
"the hidden pane holds a placeholder, which is what `.first()` found"
);
assert_eq!(
app.terminal_caret_origin(2),
(caret.x, caret.y + 1),
"the dropdown anchors under the caret that is actually on screen"
);
app.terminal_pane_maximized = false;
term.draw(|f| app.render(f)).unwrap();
let rightmost = app.terminal_profile_buttons[2];
assert!(
rightmost.x > app.terminal_profile_buttons[0].x,
"the panes must really be side by side for this to prove anything"
);
assert_eq!(
app.terminal_caret_origin(2),
(rightmost.x, rightmost.y + 1),
"each pane's dropdown anchors under its OWN caret"
);
}
#[test]
fn the_palette_has_a_zoxide_jump_command_that_works_from_any_pane() {
use crate::widgets::command_palette::ALL_COMMANDS;
let cmd = ALL_COMMANDS
.iter()
.copied()
.find(|c| c.title().to_lowercase().contains("zoxide"))
.expect("a palette command for the zoxide jump");
assert_eq!(cmd.title(), "Explorer: Jump to Directory (zoxide)");
for pane in [Pane::Editor, Pane::Terminal, Pane::Tree] {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.focus = pane;
app.run_command(cmd);
assert!(app.zoxide_jump.is_some(), "{}", app.status);
}
}
#[test]
fn zoxide_jump_popup_anchors_over_the_explorer_column_not_centered() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.handle_explorer_shortcut(key(KeyCode::Char('z'), KeyModifiers::SUPER));
assert!(app.zoxide_jump.is_some(), "precondition: popup is open");
let backend = ratatui::backend::TestBackend::new(120, 40);
let mut term = ratatui::Terminal::new(backend).unwrap();
term.draw(|f| app.render(f)).unwrap();
let popup = app.zoxide_jump.as_ref().unwrap().last_rect;
assert_eq!(
popup.x, app.tree.last_area.x,
"jump popup must be left-anchored to the Explorer column, got x={} vs sidebar x={}",
popup.x, app.tree.last_area.x
);
assert!(
popup.y <= app.tree.last_area.y,
"jump popup must drop from the top of the Explorer, not be vertically centered"
);
}
#[test]
fn a_zoxide_row_re_roots_under_the_same_key_as_the_add_branch() {
let tmp = tempfile::tempdir().unwrap();
let real = tmp.path().canonicalize().unwrap().join("real");
std::fs::create_dir(&real).unwrap();
let link = tmp.path().canonicalize().unwrap().join("link");
std::os::unix::fs::symlink(&real, &link).unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let mut jump = crate::widgets::zoxide_jump::ZoxideJump::new();
jump.set_results(Some(vec![link.clone()]));
app.zoxide_jump = Some(jump);
app.zoxide_add_folder = false;
app.apply_zoxide_jump_selection();
let jumped = app.roots.primary().to_path_buf();
let mut app2 = App::new(tmp.path().to_path_buf()).unwrap();
let mut jump2 = crate::widgets::zoxide_jump::ZoxideJump::new();
jump2.set_results(Some(vec![link.clone()]));
app2.zoxide_jump = Some(jump2);
app2.zoxide_add_folder = true;
app2.apply_zoxide_jump_selection();
let added = app2
.roots
.iter()
.find(|r| r.ends_with("real") || r.ends_with("link"))
.expect("the folder was added")
.to_path_buf();
assert_eq!(
jumped, added,
"both branches of one gesture must store the same spelling"
);
assert_eq!(
jumped,
link.canonicalize().unwrap(),
"and it is the canonical one, so the folder store keys agree"
);
}
#[test]
fn the_palette_and_vim_recorders_are_one_recording_not_two() {
let (mut app, tmp) = vim_app("one\ntwo\n");
app.macros_path = tmp.path().join("macros.json");
type_keys(&mut app, "qa");
assert!(app.macro_recording.is_some());
app.run_command(crate::widgets::command_palette::Command::MacroStartStopRecording);
assert!(app.macro_recording.is_none(), "the palette stopped it");
type_keys(&mut app, "qb");
assert_eq!(
app.macro_recording.as_ref().map(|r| r.register),
Some(Some('b')),
"vim sees the real state and opens register b"
);
app.run_command(crate::widgets::command_palette::Command::MacroStartStopRecording);
app.run_command(crate::widgets::command_palette::Command::MacroStartStopRecording);
assert!(app.macro_recording.is_some(), "palette started one");
type_char(&mut app, 'q');
assert!(
app.macro_recording.is_none(),
"a bare q stops what the palette started"
);
}
#[test]
fn a_macro_stopped_from_the_palette_replays_only_what_was_recorded() {
let tmp = tempfile::tempdir().unwrap();
let mut app = app_with_open_file(tmp.path(), "names.txt", "alice\nbob\ncarol\ndave\neve\n");
app.macros_path = tmp.path().join("macros.json");
let palette = key(
KeyCode::Char('p'),
KeyModifiers::SUPER | KeyModifiers::SHIFT,
);
let none = |code| key(code, KeyModifiers::NONE);
let run_palette_row_titled = |app: &mut App, query: &str| {
app.handle_key(palette).unwrap();
type_keys(app, query);
app.handle_key(none(KeyCode::Enter)).unwrap();
};
run_palette_row_titled(&mut app, "Macro: Start/Stop Recording");
assert!(app.macro_recording.is_some(), "the palette started it");
app.handle_key(none(KeyCode::Home)).unwrap();
type_keys(&mut app, "- ");
run_palette_row_titled(&mut app, "Transform to Uppercase");
app.handle_key(none(KeyCode::Down)).unwrap();
run_palette_row_titled(&mut app, "Macro: Start/Stop Recording");
assert!(app.macro_recording.is_none(), "the palette stopped it");
assert_eq!(app.editor.lines[0], "- ALICE");
app.replay_last_macro(4);
assert!(
app.command_palette.is_none(),
"replay ends with no palette open"
);
assert_eq!(
app.editor.lines[..5],
["- ALICE", "- BOB", "- CAROL", "- DAVE", "- EVE"]
);
}
#[test]
fn a_palette_command_that_leaves_the_editor_is_not_recorded() {
let tmp = tempfile::tempdir().unwrap();
let mut app = app_with_open_file(tmp.path(), "names.txt", "alice\nbob\n");
app.macros_path = tmp.path().join("macros.json");
let palette = key(
KeyCode::Char('p'),
KeyModifiers::SUPER | KeyModifiers::SHIFT,
);
let run_palette_row_titled = |app: &mut App, query: &str| {
app.handle_key(palette).unwrap();
type_keys(app, query);
app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
.unwrap();
};
run_palette_row_titled(&mut app, "Macro: Start/Stop Recording");
type_keys(&mut app, "x");
run_palette_row_titled(&mut app, "Terminal: Focus Terminal");
assert!(app.focus != Pane::Editor, "the command left the editor");
app.focus_pane(Pane::Editor);
type_keys(&mut app, "y");
run_palette_row_titled(&mut app, "Macro: Start/Stop Recording");
app.replay_last_macro(1);
assert!(app.focus == Pane::Editor, "replay stays in the editor");
assert!(
app.editor.lines[0].starts_with("xyxy"),
"{:?}",
app.editor.lines
);
}
#[test]
fn a_palette_closed_by_a_click_leaves_nothing_for_the_macro() {
let tmp = tempfile::tempdir().unwrap();
let mut app = app_with_open_file(tmp.path(), "names.txt", "alice\n");
app.run_command(crate::widgets::command_palette::Command::MacroStartStopRecording);
app.handle_key(key(
KeyCode::Char('p'),
KeyModifiers::SUPER | KeyModifiers::SHIFT,
))
.unwrap();
type_keys(&mut app, "delete line");
let held = |app: &App| app.macro_recording.as_ref().unwrap().palette_keys.len();
assert!(held(&app) > 0, "an open palette session is held");
app.command_palette = None;
app.handle_mouse(crossterm::event::MouseEvent {
kind: crossterm::event::MouseEventKind::Moved,
column: 0,
row: 0,
modifiers: KeyModifiers::NONE,
});
assert_eq!(held(&app), 0);
assert!(app.macro_recording.as_ref().unwrap().recorded.is_empty());
}
#[test]
fn quick_open_skips_excluded_files() {
let (mut app, _t) = app_with_vscode_excludes();
app.open_file_finder();
let rels: Vec<String> = app
.file_finder
.as_ref()
.unwrap()
.entries
.iter()
.map(|e| e.rel.clone())
.collect();
assert!(rels.contains(&String::from("src/orders.py")), "{rels:?}");
assert!(
!rels
.iter()
.any(|r| r.starts_with("generated/") || r.starts_with("tests/")),
"{rels:?}"
);
}
#[test]
fn cmd_shift_p_opens_command_palette() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
assert!(app.command_palette.is_none());
app.handle_key(key(
KeyCode::Char('p'),
KeyModifiers::SUPER | KeyModifiers::SHIFT,
))
.unwrap();
assert!(
app.command_palette.is_some(),
"Cmd+Shift+P must open the command palette"
);
app.handle_key(key(KeyCode::Esc, KeyModifiers::NONE))
.unwrap();
assert!(app.command_palette.is_none());
}
#[test]
fn occurrence_commands_with_nothing_to_act_on_change_nothing() {
use crate::widgets::command_palette::Command;
assert_eq!(
Command::from_id("select_all_occurrences"),
Some(Command::SelectAllOccurrences)
);
assert_eq!(
Command::from_id("add_cursors_to_line_ends"),
Some(Command::AddCursorsToLineEnds)
);
let tmp = tempfile::tempdir().unwrap();
let mut app = occurrences_app(tmp.path());
app.editor.cursor_col = 4;
cmd_shift_l(&mut app);
assert!(!app.editor.has_multi_cursor());
app.handle_key(key(
KeyCode::Char('I'),
KeyModifiers::SHIFT | KeyModifiers::ALT,
))
.unwrap();
assert!(!app.editor.has_multi_cursor());
select(&mut app, (0, 4), (1, 3));
cmd_shift_l(&mut app);
assert!(!app.editor.has_multi_cursor());
assert_eq!(app.editor.selection.unwrap().normalised(), ((0, 4), (1, 3)));
assert_eq!(app.editor.lines[0], "foo = 1");
}
#[test]
fn the_palette_clear_bookmarks_command_leaves_a_preview_alone() {
use crate::widgets::command_palette::Command;
let tmp = tempfile::tempdir().unwrap();
let mut app = app_with_open_file(tmp.path(), "notes.md", "# a\n\nb\n");
app.editor.cursor_row = 2;
app.editor.toggle_bookmark();
assert!(app.editor.toggle_markdown_preview());
app.run_command(Command::ClearBookmarks);
assert_eq!(
app.editor.bookmarked_lines(),
vec![3],
"the preview hides them"
);
assert!(app.editor.toggle_markdown_preview());
app.run_command(Command::ClearBookmarks);
assert!(app.editor.bookmarked_lines().is_empty());
}
#[test]
fn the_bookmark_picker_filters_as_you_type() {
use crate::widgets::command_palette::Command;
let tmp = tempfile::tempdir().unwrap();
let a = tmp.path().join("a.txt");
let b = tmp.path().join("b.txt");
std::fs::write(&a, "alpha\nbeta\ngamma\n").unwrap();
std::fs::write(&b, "one\ntwo\nthree\n").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&a).unwrap();
app.editor.cursor_row = 1;
app.toggle_bookmark();
app.editor.open_in_new_tab(&b).unwrap();
app.editor.cursor_row = 2;
app.toggle_bookmark();
app.run_command(Command::ViewBookmarks);
for c in "b.txt".chars() {
app.handle_key(key(KeyCode::Char(c), KeyModifiers::NONE))
.unwrap();
}
let picker = app.list_picker.as_ref().expect("the picker stays open");
assert_eq!(picker.visible_count(), 1);
assert_eq!(
picker.selected_row().map(|r| r.label.as_str()),
Some("b.txt:3 three")
);
}
#[test]
fn palette_merge_accept_incoming_resolves_at_cursor() {
let tmp = tempfile::tempdir().unwrap();
let mut app = app_with_open_file(tmp.path(), "a.rs", CONFLICTED_BODY);
app.editor.cursor_row = 1;
app.run_command(crate::widgets::command_palette::Command::MergeAcceptIncoming);
assert_eq!(app.editor.lines, vec!["fn main() {", " theirs();", "}"]);
assert_eq!(
app.status,
"Accepted incoming change — all conflicts resolved; \"Merge: Complete Merge\" stages the file"
);
}
#[test]
fn palette_merge_accept_both_keeps_ours_then_theirs() {
let tmp = tempfile::tempdir().unwrap();
let mut app = app_with_open_file(tmp.path(), "a.rs", CONFLICTED_BODY);
app.editor.cursor_row = 3;
app.run_command(crate::widgets::command_palette::Command::MergeAcceptBoth);
assert_eq!(
app.editor.lines,
vec!["fn main() {", " ours();", " theirs();", "}"]
);
}
fn both_added_conflict(root: &std::path::Path) {
let base = "fn a() {\n x();\n}\n\nfn tail() {\n z();\n}\n";
let add = |f: &str| {
base.replace(
"fn tail",
&format!("fn {f}() {{\n {f}();\n}}\n\nfn tail"),
)
};
git_init(root);
git_ok(root, &["config", "user.name", "t"]);
git_ok(root, &["config", "user.email", "t@t"]);
git_ok(root, &["config", "merge.conflictStyle", "merge"]);
std::fs::write(root.join("a.rs"), base).unwrap();
git_ok(root, &["add", "-A"]);
git_ok(root, &["commit", "-qm", "base"]);
git_ok(root, &["checkout", "-qb", "feature"]);
std::fs::write(root.join("a.rs"), add("from_branch")).unwrap();
git_ok(root, &["commit", "-qam", "branch"]);
git_ok(root, &["checkout", "-q", "main"]);
std::fs::write(root.join("a.rs"), add("from_main")).unwrap();
git_ok(root, &["commit", "-qam", "main"]);
assert!(
!git_succeeds(root, &["merge", "-q", "feature"]),
"the merge must conflict"
);
}
fn shared_brace_footer(app: &App) -> usize {
let footer = app
.editor
.lines
.iter()
.position(|l| l.starts_with(">>>>>>>"))
.unwrap();
assert_eq!(
app.editor.lines[footer + 1],
"}",
"git wrote the shared `}}` below the markers"
);
footer
}
const BOTH_ADDED_RESOLVED: &str = "fn a() {\n x();\n}\n\nfn from_main() {\n from_main();\n}\n\n\
fn from_branch() {\n from_branch();\n}\n\nfn tail() {\n z();\n}";
#[test]
fn palette_merge_accept_both_keeps_both_added_functions_closed() {
let tmp = tempfile::tempdir().unwrap();
let root = tmp.path();
both_added_conflict(root);
let mut app = App::new(root.to_path_buf()).unwrap();
app.editor.open_pinned(&root.join("a.rs")).unwrap();
app.focus_pane(Pane::Editor);
app.editor.cursor_row = shared_brace_footer(&app) - 1;
app.run_command(crate::widgets::command_palette::Command::MergeAcceptBoth);
assert_eq!(app.editor.lines.join("\n"), BOTH_ADDED_RESOLVED);
assert_eq!(
app.status,
"Accepted both changes — all conflicts resolved; \"Merge: Complete Merge\" stages the file"
);
assert!(app.editor.undo(), "resolution is one undo step");
assert!(app.editor.lines.iter().any(|l| l.starts_with("<<<<<<<")));
}
#[test]
fn accept_both_reads_the_stages_of_the_files_own_repo() {
let tmp = tempfile::tempdir().unwrap();
let repo = tmp.path().join("repo");
let other = tmp.path().join("other");
std::fs::create_dir(&repo).unwrap();
std::fs::create_dir(&other).unwrap();
git_init(&other);
both_added_conflict(&repo);
let mut app = App::new(repo.clone()).unwrap();
app.add_workspace_folder(other.clone());
app.editor.open_pinned(&repo.join("a.rs")).unwrap();
app.focus_pane(Pane::Editor);
app.active_scm_root = other;
app.editor.cursor_row = shared_brace_footer(&app) - 1;
app.run_command(crate::widgets::command_palette::Command::MergeAcceptBoth);
assert_eq!(app.editor.lines.join("\n"), BOTH_ADDED_RESOLVED);
}
#[test]
fn hash_prefix_in_quick_open_switches_to_workspace_symbols() {
let tmp = tempfile::tempdir().unwrap();
let mut app = app_with_open_file(tmp.path(), "a.rs", "fn main() {}");
app.handle_key(key(KeyCode::Char('p'), KeyModifiers::SUPER))
.unwrap();
assert!(app.file_finder.is_some(), "Cmd+P opens Quick Open");
app.handle_key(key(KeyCode::Char('#'), KeyModifiers::NONE))
.unwrap();
assert!(
app.file_finder.is_none(),
"typing # hands Quick Open over to the workspace symbol picker"
);
let picker = app
.workspace_symbols
.as_ref()
.expect("the workspace symbol picker must open");
assert_eq!(picker.query, "", "the # itself is not part of the query");
}
#[test]
fn angle_prefix_in_quick_open_opens_the_command_palette() {
let tmp = tempfile::tempdir().unwrap();
let mut app = app_with_open_file(tmp.path(), "a.py", "x = 1\n");
quick_open_typing(&mut app, ">");
assert!(app.file_finder.is_none(), "Quick Open hands over");
let palette = app.command_palette.as_ref().expect("the palette opens");
assert_eq!(palette.query, "", "the > itself is not part of the query");
}
#[test]
fn a_pasted_prefixed_query_carries_into_the_picker_it_opens() {
let tmp = tempfile::tempdir().unwrap();
let mut app = app_with_open_file(tmp.path(), "a.py", "x = 1\n");
quick_open_typing(&mut app, "");
app.handle_paste(">toggle word wrap");
let palette = app.command_palette.as_ref().expect("the palette opens");
assert_eq!(palette.query, "toggle word wrap");
app.command_palette = None;
quick_open_typing(&mut app, "");
app.handle_paste("#main");
let picker = app.workspace_symbols.as_ref().expect("symbols open");
assert_eq!(picker.query, "main");
}
#[test]
fn at_prefix_in_quick_open_opens_go_to_symbol_in_editor() {
let tmp = tempfile::tempdir().unwrap();
let mut app = app_with_open_file(tmp.path(), "a.py", "def f():\n pass\n");
quick_open_typing(&mut app, "@");
assert!(app.file_finder.is_none(), "Quick Open hands over");
let picker = app.go_to_symbol.as_ref().expect("Go to Symbol opens");
assert_eq!(picker.query, "");
}
#[test]
fn colon_line_in_quick_open_goes_to_that_line() {
let tmp = tempfile::tempdir().unwrap();
let body: String = (1..=50).map(|i| format!("x = {i}\n")).collect();
let mut app = app_with_open_file(tmp.path(), "a.py", &body);
quick_open_typing(&mut app, ":12");
assert!(app.file_finder.is_none(), "Quick Open hands over");
app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
.unwrap();
assert_eq!(app.editor.cursor_row, 11, "{}", app.status);
}
#[test]
fn palette_go_to_symbol_in_workspace_opens_the_picker() {
let tmp = tempfile::tempdir().unwrap();
let mut app = app_with_open_file(tmp.path(), "a.rs", "fn main() {}");
app.run_command(crate::widgets::command_palette::Command::GoToWorkspaceSymbol);
assert!(app.workspace_symbols.is_some());
}
#[test]
fn typing_in_the_picker_without_a_language_server_reports_unsupported() {
let tmp = tempfile::tempdir().unwrap();
let mut app = app_with_open_file(tmp.path(), "a.rs", "fn main() {}");
app.lsp = None;
app.open_workspace_symbols("");
app.handle_key(key(KeyCode::Char('m'), KeyModifiers::NONE))
.unwrap();
std::thread::sleep(std::time::Duration::from_millis(250));
app.workspace_symbols_dispatch_due();
let picker = app.workspace_symbols.as_ref().unwrap();
assert!(
picker.unsupported,
"with no language server the picker must say so instead of spinning"
);
}
#[test]
fn finished_commands_land_in_durable_history_and_the_popup_types_them() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.command_history =
crate::command_history::CommandHistory::load(&tmp.path().join("hist.jsonl"));
let script = "printf '\\033]133;A\\007$ \\033]133;B\\007kubectl get pods\\n\\033]133;C\\007'; sleep 1; printf 'out\\n\\033]133;D;0\\007'; read x; echo typed-$x; sleep 30";
let mut entry = None;
for attempt in 0..3 {
app.command_history.entries.clear();
app.terminals[0] = crate::widgets::terminal::PtyTerminal::new_running(
"/bin/sh",
&[String::from("-c"), String::from(script)],
tmp.path(),
)
.unwrap();
crate::test_budget::await_spawned(
crate::test_budget::tests::SHELL_PAINT_BASE,
"the finished command to reach the history store",
|| {
app.drain_terminal_bells();
!app.command_history.is_empty()
},
);
let e = app.command_history.entries[0].clone();
assert_eq!(
e.cmd, "kubectl get pods",
"the typed text travels with the completion"
);
assert_eq!(e.exit, Some(0));
if e.dur_ms >= 500 {
entry = Some(e);
break;
}
assert!(
attempt < 2,
"duration must cover the C→D gap on some attempt; every run measured under 500ms (last: {})",
e.dur_ms
);
}
let e = entry.expect("an attempt produced a full-gap measurement");
let _ = e;
app.focus_pane(Pane::Terminal);
app.handle_terminal_key(key(
KeyCode::Char('h'),
KeyModifiers::CONTROL | KeyModifiers::SHIFT,
));
assert!(
app.command_history_popup.is_some(),
"Ctrl+Shift+H must open the history popup"
);
for c in "pods".chars() {
app.handle_key(key(KeyCode::Char(c), KeyModifiers::NONE))
.unwrap();
}
let picked = app
.command_history_popup
.as_ref()
.and_then(|p| p.selected_entry())
.map(|e| e.cmd.clone());
assert_eq!(picked.as_deref(), Some("kubectl get pods"));
app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
.unwrap();
assert!(
app.command_history_popup.is_none(),
"Enter closes the popup"
);
app.terminals[0].write_input(b"\n");
crate::test_budget::await_spawned(
crate::test_budget::tests::SHELL_PAINT_BASE,
"the typed command to reach the shell",
|| {
app.terminals[0]
.grid_lines()
.0
.iter()
.any(|l| l.contains("typed-kubectl get pods"))
},
);
}
#[test]
fn palette_carries_the_navigator_commands() {
use crate::widgets::command_palette::{ALL_COMMANDS, Command};
for cmd in [
Command::AskNavigator,
Command::YieldToNavigator,
Command::ToggleNavigator,
Command::ClearNavigatorNotes,
] {
assert!(ALL_COMMANDS.contains(&cmd), "{cmd:?} missing from palette");
}
assert_eq!(Command::AskNavigator.keybinding_hint(), "Cmd+K Q");
assert_eq!(Command::YieldToNavigator.keybinding_hint(), "Cmd+K Y");
let tmp = tempfile::tempdir().unwrap();
let file = tmp.path().join("f.txt");
std::fs::write(&file, "a\nb").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.pair_record_path = tmp.path().join("x.pair.json");
app.open_file_at_launch(&file);
app.run_command(crate::widgets::command_palette::Command::AskNavigator);
assert!(app.status.to_lowercase().contains("not active"));
app.run_command(crate::widgets::command_palette::Command::YieldToNavigator);
assert!(app.status.to_lowercase().contains("not active"));
app.navigator_notes
.insert("f.txt".into(), vec![(1, 0, "n".into())]);
app.editor.comment_focus = Some(crate::widgets::editor::CommentFocus {
id: 1,
reply: String::new(),
cursor: 0,
});
app.run_command(crate::widgets::command_palette::Command::ClearNavigatorNotes);
assert!(app.navigator_notes.is_empty());
assert!(app.editor.comment_focus.is_none());
app.run_command(crate::widgets::command_palette::Command::ToggleNavigator);
let record =
crate::session::read_pair_record(&app.pair_record_path).expect("toggle writes the record");
assert!(record.enabled);
app.run_command(crate::widgets::command_palette::Command::ToggleNavigator);
let record = crate::session::read_pair_record(&app.pair_record_path).unwrap();
assert!(!record.enabled);
}
#[test]
fn the_picker_lists_compounds_and_selecting_one_reports_why_it_cannot_launch() {
let tmp = tempfile::tempdir().unwrap();
std::fs::create_dir_all(tmp.path().join(".vscode")).unwrap();
std::fs::write(
tmp.path().join(".vscode/launch.json"),
r#"{ "configurations": [
{ "name": "Server", "type": "python", "request": "launch", "program": "s.py" },
{ "name": "Client", "type": "python", "request": "launch", "program": "c.py" }
],
"compounds": [
{ "name": "Full Stack", "configurations": ["Server", "Client"] },
{ "name": "Broken", "configurations": ["Server", "Missing"] }
]}"#,
)
.unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.open_debug_config_picker();
let picker = app.list_picker.as_ref().expect("picker open");
assert_eq!(
picker.rows.len(),
6,
"zero-config entry + two configurations + two compounds + Add Configuration"
);
let compound_row = picker
.rows
.iter()
.find(|r| r.label.starts_with("Full Stack"))
.expect("the compound is listed");
assert!(
compound_row.label.contains("Server"),
"the row names its members: {}",
compound_row.label
);
assert!(
compound_row.id.starts_with("compound:"),
"compound rows carry their own id space so they cannot collide with \
a configuration index: {}",
compound_row.id
);
let idx = picker
.rows
.iter()
.position(|r| r.label.starts_with("Full Stack"))
.unwrap();
app.list_picker.as_mut().unwrap().selected = idx;
app.confirm_list_picker();
assert!(
app.status.contains("started no sessions") || app.status.starts_with("Debugging compound"),
"the compound went down the launch path rather than being refused: {}",
app.status
);
assert!(
!app.status.contains("#310"),
"and does not cite a limit that is gone: {}",
app.status
);
app.open_debug_config_picker();
let bad = app
.list_picker
.as_ref()
.unwrap()
.rows
.iter()
.position(|r| r.label.starts_with("Broken"))
.expect("the second compound is listed");
app.list_picker.as_mut().unwrap().selected = bad;
app.confirm_list_picker();
assert!(
app.status.contains("Missing"),
"the error names the member that does not exist: {}",
app.status
);
}
#[test]
fn the_profiles_picker_offers_the_default_and_a_new_profile() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.run_command(crate::widgets::command_palette::Command::SwitchProfile);
let picker = app.list_picker.as_ref().expect("picker open");
assert!(matches!(
picker.purpose,
crate::widgets::list_picker::ListPurpose::Profiles
));
let ids: Vec<&str> = picker.rows.iter().map(|r| r.id.as_str()).collect();
assert_eq!(ids.first(), Some(&"profile:"), "Default comes first");
assert!(ids.contains(&"new"));
assert!(
!ids.contains(&"workspace-clear"),
"no workspace choice to clear in a fresh workspace"
);
}
#[test]
fn session_detach_outside_a_session_says_there_is_nothing_to_detach_from() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.session_channel = None;
app.run_command(crate::widgets::command_palette::Command::SessionDetach);
assert_eq!(
app.status,
"Not attached to a persistent session: nothing to detach from"
);
}
#[test]
fn esc_in_a_palette_the_user_opened_keeps_the_tour() {
let _guard = relay_test_lock().lock().unwrap_or_else(|e| e.into_inner());
let home = tempfile::tempdir().unwrap();
with_relay_home(home.path(), || {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let _settings = own_settings(&mut app);
app.start_demo();
assert!(
app.command_palette.is_none(),
"fixture: step 1 opens no modal"
);
app.open_command_palette();
app.handle_key(key(KeyCode::Esc, KeyModifiers::NONE))
.unwrap();
assert!(app.command_palette.is_none());
assert!(app.tour.is_some(), "the tour goes on");
app.finish_tour();
});
}
#[test]
fn the_palette_turns_the_startup_tab_into_untitled_1() {
use crate::widgets::command_palette::Command;
assert_eq!(
Command::from_id("new_untitled_file"),
Some(Command::NewUntitledFile)
);
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
assert!(app.editor.is_blank_initial());
assert!(run_palette_row_titled(
&mut app,
"File: New Untitled Text File"
));
assert_eq!(app.editor.tab_count(), 1);
assert_eq!(app.editor.tab_display_label(0), "Untitled-1");
assert!(!app.editor.is_blank_initial(), "no welcome screen over it");
assert!(app.focus == Pane::Editor);
}
#[test]
fn select_all_from_the_palette_selects_the_active_buffer() {
let tmp = tempfile::tempdir().unwrap();
let mut app = app_with_open_file(tmp.path(), "a.txt", "one\ntwo");
app.focus_pane(Pane::Tree);
app.run_command(crate::widgets::command_palette::Command::SelectAll);
assert_eq!(app.editor.selection_text(), "one\ntwo");
assert!(app.focus == Pane::Editor);
}
#[test]
fn palette_new_file_with_no_file_open_uses_the_explorer_target() {
use crate::widgets::command_palette::Command;
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
assert!(app.editor.path.is_none());
app.focus_pane(Pane::Editor);
let explorer_dir = app.explorer_create_target_dir();
app.run_command(Command::NewFile);
match app.prompt.as_ref() {
Some(Prompt {
kind: PromptKind::Create(CreateKind::File),
target_dir,
..
}) => assert_eq!(target_dir, &explorer_dir),
_ => panic!("File: New File… must open the New File prompt"),
}
}
#[test]
fn palette_query_layout_lists_the_customize_layout_commands() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
open_palette_852(&mut app);
type_keys(&mut app, "layout");
let titles: Vec<String> = app
.command_palette
.as_ref()
.unwrap()
.results
.iter()
.map(|r| r.title().to_string())
.collect();
for want in [
"View: Customize Layout…",
"View: Toggle Activity Bar Visibility",
"View: Toggle Status Bar Visibility",
"View: Toggle Primary Side Bar Position",
"View: Set Panel Alignment…",
"View: Set Quick Input Position…",
] {
assert!(
titles.iter().any(|t| t == want),
"\"layout\" must find {want:?}; got {titles:?}"
);
}
}
#[test]
fn palette_toggles_the_activity_bar_status_bar_and_side_bar_position() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.activity_bar_visible = true;
app.status_bar_visible = true;
app.side_bar_position = SideBarPosition::Left;
open_palette_852(&mut app);
type_keys(&mut app, "toggle status bar visibility");
app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
.unwrap();
assert!(!app.status_bar_visible, "the status bar hides");
open_palette_852(&mut app);
type_keys(&mut app, "toggle activity bar visibility");
app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
.unwrap();
assert!(!app.activity_bar_visible, "the activity bar hides");
open_palette_852(&mut app);
type_keys(&mut app, "toggle primary side bar position");
app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
.unwrap();
assert_eq!(app.side_bar_position, SideBarPosition::Right);
}
#[test]
fn palette_sets_panel_alignment_and_quick_input_position_by_key() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.panel_alignment = PanelAlignment::Center;
app.quick_input_position = QuickInputPosition::Top;
open_palette_852(&mut app);
type_keys(&mut app, "set panel alignment");
app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
.unwrap();
assert!(app.list_picker.is_some(), "the alignment picker opens");
type_keys(&mut app, "justify");
app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
.unwrap();
assert_eq!(app.panel_alignment, PanelAlignment::Justify);
open_palette_852(&mut app);
type_keys(&mut app, "set quick input position");
app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
.unwrap();
assert!(app.list_picker.is_some(), "the position picker opens");
type_keys(&mut app, "center");
app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
.unwrap();
assert_eq!(app.quick_input_position, QuickInputPosition::Center);
}
#[test]
fn palette_layout_commands_leave_the_customize_layout_popup_closed() {
use crate::widgets::command_palette::Command;
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
for cmd in [
Command::ToggleActivityBar,
Command::ToggleStatusBar,
Command::ToggleSideBarPosition,
] {
app.run_command(cmd);
assert!(
app.context_menu.is_none(),
"{cmd:?} must not open the Customize Layout popup"
);
}
let fresh = App::new(tmp.path().to_path_buf()).unwrap();
for cmd in [
Command::ToggleActivityBar,
Command::ToggleStatusBar,
Command::ToggleSideBarPosition,
] {
app.run_command(cmd);
}
assert_eq!(app.activity_bar_visible, fresh.activity_bar_visible);
assert_eq!(app.status_bar_visible, fresh.status_bar_visible);
assert_eq!(app.side_bar_position, fresh.side_bar_position);
}
fn palette_rows_852(app: &mut App, query: &str) -> Vec<String> {
open_palette_852(app);
type_keys(app, query);
let rows = app
.command_palette
.as_ref()
.map(|p| p.results.iter().map(|i| i.title().to_string()).collect())
.unwrap_or_default();
app.handle_key(key(KeyCode::Esc, KeyModifiers::NONE))
.unwrap();
rows
}
#[test]
fn palette_close_other_editors_keeps_only_the_active_tab() {
let (mut app, _tmp) = tabs_app_852(&["a.rs", "b.rs", "c.rs", "d.rs"]);
app.editor.select(1);
let ran = run_palette_top_match(&mut app, "View: Close Other Editors in Group");
assert_eq!(
tab_names_852(&app.editor),
["b.rs"],
"the palette ran {ran:?}"
);
assert_eq!(app.status, "Closed 3 other tabs");
}
#[test]
fn palette_close_editors_to_the_right_closes_only_the_tabs_right_of_the_active_one() {
let (mut app, _tmp) = tabs_app_852(&["a.rs", "b.rs", "c.rs", "d.rs"]);
app.editor.select(1);
let ran = run_palette_top_match(&mut app, "View: Close Editors to the Right in Group");
assert_eq!(
tab_names_852(&app.editor),
["a.rs", "b.rs"],
"the palette ran {ran:?}"
);
assert_eq!(app.editor.active_index(), 1, "b.rs stays active");
assert_eq!(app.status, "Closed 2 tabs to the right");
}
#[test]
fn palette_close_saved_editors_closes_the_saved_tabs_and_keeps_the_dirty_one() {
let (mut app, tmp) = tabs_app_852(&["a.rs", "b.rs", "c.rs"]);
app.editor.select(1);
type_into_active_tab_852(&mut app);
let ran = run_palette_top_match(&mut app, "View: Close Saved Editors in Group");
assert_eq!(
tab_names_852(&app.editor),
["b.rs"],
"the palette ran {ran:?}"
);
assert!(app.editor.dirty, "the unsaved edit is kept");
assert_eq!(
std::fs::read_to_string(tmp.path().join("b.rs")).unwrap(),
"x\n",
"and not written"
);
assert_eq!(app.status, "Closed 2 saved tabs");
}
#[test]
fn palette_close_all_editors_closes_every_tab() {
let (mut app, _tmp) = tabs_app_852(&["a.rs", "b.rs", "c.rs"]);
let ran = run_palette_top_match(&mut app, "View: Close All Editors");
assert!(
app.editor.is_blank_initial(),
"the palette ran {ran:?}; tabs left: {:?}",
tab_names_852(&app.editor)
);
assert_eq!(app.status, "Closed 3 tabs");
}
#[test]
fn palette_pin_editor_and_unpin_editor_pin_and_unpin_the_active_tab() {
let (mut app, _tmp) = tabs_app_852(&["a.rs", "b.rs", "c.rs"]);
let ran = run_palette_top_match(&mut app, "View: Pin Editor");
assert!(app.editor.pinned, "the palette ran {ran:?}");
assert_eq!(
tab_names_852(&app.editor),
["c.rs", "a.rs", "b.rs"],
"a pinned tab moves leftmost"
);
assert_eq!(app.status, "Pinned tab");
let ran = run_palette_top_match(&mut app, "View: Unpin Editor");
assert!(!app.editor.pinned, "the palette ran {ran:?}");
assert_eq!(
app.editor.path.as_deref().and_then(Path::file_name),
Some(std::ffi::OsStr::new("c.rs"))
);
assert_eq!(app.status, "Unpinned tab");
}
#[test]
fn the_palette_offers_pin_editor_or_unpin_editor_by_the_active_tab() {
let (mut app, tmp) = tabs_app_852(&["a.rs", "b.rs"]);
let rows = palette_rows_852(&mut app, "pin editor");
assert!(rows.iter().any(|t| t == "View: Pin Editor"), "{rows:?}");
assert!(
!rows.iter().any(|t| t == "View: Unpin Editor"),
"an unpinned tab has nothing to unpin: {rows:?}"
);
let idx = app.editor.active_index();
app.dispatch_menu_action(MenuAction::ToggleTabPin(idx), tmp.path().to_path_buf());
assert!(app.editor.pinned, "fixture: the tab menu pinned it");
let rows = palette_rows_852(&mut app, "pin editor");
assert!(rows.iter().any(|t| t == "View: Unpin Editor"), "{rows:?}");
assert!(
!rows.iter().any(|t| t == "View: Pin Editor"),
"a pinned tab is already pinned: {rows:?}"
);
}
#[test]
fn palette_keep_editor_keeps_the_preview_tab_open() {
let (mut app, tmp) = tabs_app_852(&["a.rs"]);
let p = tmp.path().join("p.rs");
std::fs::write(&p, "x\n").unwrap();
app.editor.open_preview(&p).unwrap();
assert!(app.editor.preview, "fixture: p.rs is the preview tab");
let ran = run_palette_top_match(&mut app, "View: Keep Editor");
assert!(!app.editor.preview, "the palette ran {ran:?}");
assert_eq!(app.status, "Kept tab open");
}
#[test]
fn palette_close_other_editors_keeps_the_active_and_the_pinned_tabs() {
let (mut app, tmp) = tabs_app_852(&["a.rs", "b.rs", "c.rs", "d.rs"]);
app.dispatch_menu_action(MenuAction::ToggleTabPin(0), tmp.path().to_path_buf());
app.editor.select(2);
run_palette_top_match(&mut app, "View: Close Other Editors in Group");
assert_eq!(tab_names_852(&app.editor), ["a.rs", "c.rs"]);
assert!(app.editor.editors[0].pinned, "the pinned a.rs survives");
assert_eq!(app.editor.active_index(), 1, "c.rs stays active");
let reopen: Vec<String> = app
.closed_tabs
.iter()
.map(|t| t.path.file_name().unwrap().to_string_lossy().into_owned())
.collect();
assert_eq!(reopen, ["b.rs", "d.rs"]);
assert_eq!(app.status, "Closed 2 other tabs");
}
#[test]
fn palette_close_editors_to_the_right_on_the_last_tab_closes_nothing() {
let (mut app, _tmp) = tabs_app_852(&["a.rs", "b.rs", "c.rs"]);
run_palette_top_match(&mut app, "View: Close Editors to the Right in Group");
assert_eq!(tab_names_852(&app.editor), ["a.rs", "b.rs", "c.rs"]);
assert_eq!(app.editor.active_index(), 2);
assert!(app.closed_tabs.is_empty(), "nothing to reopen");
assert_eq!(app.status, "No tabs to the right to close");
}
#[test]
fn palette_tab_commands_leave_the_other_split_alone() {
for (title, active) in [
("View: Close Other Editors in Group", "c.rs"),
("View: Close Editors to the Right in Group", "c.rs"),
("View: Close Saved Editors in Group", "c.rs"),
("View: Pin Editor", "c.rs"),
("View: Unpin Editor", "b.rs"),
("View: Keep Editor", "p.rs"),
] {
let (mut app, _tmp) = split_tabs_app_852(active);
let before = tabs_snapshot_852(&app);
let ran = run_palette_top_match(&mut app, title);
assert_eq!(ran.as_deref(), Some(title));
let after = tabs_snapshot_852(&app);
assert!(after.2, "{title}: still split");
assert_ne!(after.0[0], before.0[0], "{title} changed the focused group");
assert_eq!(
after.0[1..],
before.0[1..],
"{title} must leave the other split alone"
);
}
}
#[test]
fn the_share_commands_are_in_the_palette_and_the_editor_menu() {
use crate::widgets::command_palette::{ALL_COMMANDS, Command};
for cmd in [
Command::GitCopyPermalink,
Command::GitCopyHeadLink,
Command::GitOpenOnRemote,
] {
assert!(ALL_COMMANDS.contains(&cmd), "{cmd:?}");
assert!(cmd.keyword().contains("github"), "{cmd:?}");
assert_eq!(Command::from_id(cmd.id()), Some(cmd));
}
let (mut app, _tmp, _) = app_on_a_pushed_github_file();
draw(&mut app, 100, 30);
let x = app.editor.last_inner.x + app.editor.last_gutter_width + 2;
let y = app.editor.last_inner.y + 1;
app.handle_mouse(mouse(MouseEventKind::Down(MouseButton::Right), x, y));
let labels = menu_labels(&app.context_menu.as_ref().unwrap().items);
assert!(labels.contains(&"Copy Permalink"), "{labels:?}");
assert!(labels.contains(&"Open on Remote"), "{labels:?}");
}
#[test]
fn the_palette_offers_the_move_editor_commands() {
let (mut app, _tmp) = three_tab_app_1565();
let rows = palette_rows_852(&mut app, "move editor");
for title in [
"View: Move Editor Left",
"View: Move Editor Right",
"View: Move Editor into Next Group",
"View: Move Editor into Previous Group",
] {
assert!(
rows.iter().any(|r| r == title),
"{title} missing from {rows:?}"
);
}
assert_eq!(
run_palette_top_match(&mut app, "View: Move Editor Left").as_deref(),
Some("View: Move Editor Left")
);
assert_eq!(tab_names_852(&app.editor), ["a.py", "c.py", "b.py"]);
}
#[test]
fn the_bookmark_picker_lists_every_tabs_marks_and_jumps_to_one() {
use crate::widgets::command_palette::Command;
use crate::widgets::list_picker::ListPurpose;
let tmp = tempfile::tempdir().unwrap();
let a = tmp.path().join("a.txt");
let b = tmp.path().join("b.txt");
std::fs::write(&a, "alpha\nbeta\ngamma\n").unwrap();
std::fs::write(&b, "one\ntwo\nthree\n").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.run_command(Command::ViewBookmarks);
assert!(app.list_picker.is_none(), "no bookmarks, no picker");
assert!(app.status.contains("No bookmarks"), "was {:?}", app.status);
app.editor.open(&a).unwrap();
app.editor.cursor_row = 1;
app.editor.toggle_bookmark();
app.editor.open_in_new_tab(&b).unwrap();
app.editor.cursor_row = 2;
app.editor.toggle_bookmark();
app.run_command(Command::ViewBookmarks);
let picker = app.list_picker.as_ref().expect("the picker opens");
assert_eq!(picker.purpose, ListPurpose::Bookmark);
let labels: Vec<&str> = picker.rows.iter().map(|r| r.label.as_str()).collect();
assert_eq!(labels, ["a.txt:2 beta", "b.txt:3 three"]);
app.handle_key(key(KeyCode::End, KeyModifiers::NONE))
.unwrap();
assert_eq!(app.list_picker.as_ref().unwrap().selected, 1);
app.handle_key(key(KeyCode::Home, KeyModifiers::NONE))
.unwrap();
assert_eq!(app.list_picker.as_ref().unwrap().selected, 0);
app.list_picker.as_mut().unwrap().selected = 0;
app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
.unwrap();
assert_eq!(app.editor.path.as_deref(), Some(a.as_path()));
assert_eq!(app.editor.cursor_row, 1);
}
#[test]
fn the_bookmark_picker_leaves_out_a_file_no_tab_shows() {
use crate::widgets::command_palette::Command;
let tmp = tempfile::tempdir().unwrap();
let a = tmp.path().join("a.txt");
let b = tmp.path().join("b.txt");
std::fs::write(&a, "alpha\nbeta\n").unwrap();
std::fs::write(&b, "one\ntwo\n").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open_preview(&a).unwrap();
app.editor.cursor_row = 1;
app.editor.toggle_bookmark();
app.editor.open_preview(&b).unwrap();
assert!(
!app.editor
.editors
.iter()
.any(|e| e.path.as_deref() == Some(a.as_path())),
"the preview slot moved on to b.txt"
);
assert!(
app.editor
.editors
.iter()
.any(|e| e.bookmarks.contains_key(&a)),
"a tab still holds a.txt's mark"
);
app.run_command(Command::ViewBookmarks);
assert!(app.list_picker.is_none(), "a.txt is not open");
assert!(app.status.contains("No bookmarks"), "was {:?}", app.status);
}
#[test]
fn the_bookmark_picker_lists_another_groups_marks_and_jumps_into_it() {
use crate::widgets::command_palette::Command;
let tmp = tempfile::tempdir().unwrap();
let a = tmp.path().join("a.txt");
let b = tmp.path().join("b.txt");
std::fs::write(&a, "alpha\nbeta\ngamma\n").unwrap();
std::fs::write(&b, "one\ntwo\nthree\n").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&a).unwrap();
app.editor.cursor_row = 1;
app.editor.toggle_bookmark();
app.split_editor();
app.editor.open_preview(&b).unwrap();
app.editor.cursor_row = 2;
app.editor.toggle_bookmark();
let focused = app.editor_layout.active_dfs_index();
app.run_command(Command::ViewBookmarks);
let picker = app.list_picker.as_ref().expect("the picker opens");
let labels: Vec<&str> = picker.rows.iter().map(|r| r.label.as_str()).collect();
assert_eq!(labels, ["a.txt:2 beta", "b.txt:3 three"]);
app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
.unwrap();
assert_ne!(
app.editor_layout.active_dfs_index(),
focused,
"focus moved to the group holding a.txt's mark"
);
assert_eq!(app.editor.path.as_deref(), Some(a.as_path()));
assert_eq!(app.editor.cursor_row, 1);
assert!(
app.editor.is_bookmarked(1),
"the tab with the mark is in front"
);
assert_eq!(app.status, "Bookmark at line 2");
}
#[test]
fn the_bookmark_picker_lists_a_mark_two_groups_share_once() {
use crate::widgets::command_palette::Command;
let tmp = tempfile::tempdir().unwrap();
let a = tmp.path().join("a.txt");
std::fs::write(&a, "alpha\nbeta\n").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&a).unwrap();
app.editor.cursor_row = 1;
app.editor.toggle_bookmark();
app.split_editor();
app.editor.cursor_row = 1;
app.editor.toggle_bookmark();
assert!(app.editor.is_bookmarked(1));
app.run_command(Command::ViewBookmarks);
let picker = app.list_picker.as_ref().expect("the picker opens");
let labels: Vec<&str> = picker.rows.iter().map(|r| r.label.as_str()).collect();
assert_eq!(labels, ["a.txt:2 beta"]);
}
#[cfg(target_os = "linux")]
#[test]
fn the_bookmark_picker_opens_a_file_whose_name_is_not_utf8() {
use crate::widgets::command_palette::Command;
use std::os::unix::ffi::OsStrExt;
let tmp = tempfile::tempdir().unwrap();
let odd = tmp.path().join(std::ffi::OsStr::from_bytes(b"caf\xe9.txt"));
let other = tmp.path().join("other.txt");
std::fs::write(&odd, "one\ntwo\n").unwrap();
std::fs::write(&other, "x\n").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&odd).unwrap();
app.editor.cursor_row = 1;
app.editor.toggle_bookmark();
app.editor.open_in_new_tab(&other).unwrap();
app.run_command(Command::ViewBookmarks);
assert!(app.list_picker.is_some(), "the picker opens");
app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
.unwrap();
assert_eq!(app.status, "Bookmark at line 2");
assert_eq!(app.editor.path.as_deref(), Some(odd.as_path()));
assert_eq!(app.editor.cursor_row, 1);
}
#[test]
fn the_bookmark_picker_jumps_into_the_tab_that_holds_the_mark() {
use crate::widgets::command_palette::Command;
let tmp = tempfile::tempdir().unwrap();
let a = tmp.path().join("a.py");
std::fs::write(&a, "def f():\n return 1\n\ndef g():\n return 2\n").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&a).unwrap();
app.open_symbol_tab_for("g".into(), 3, 4);
assert!(
app.editor.symbol_view.is_some(),
"the symbol tab is in front"
);
let symbol_tab = app.editor.active_index();
app.editor.cursor_row = 4;
app.editor.toggle_bookmark();
let file_tab = app
.editor
.editors
.iter()
.position(|e| e.symbol_view.is_none() && e.path.as_deref() == Some(a.as_path()))
.expect("the file's own tab");
app.editor.select(file_tab);
assert!(!app.editor.is_bookmarked(4), "the file's tab has no mark");
app.run_command(Command::ViewBookmarks);
let picker = app.list_picker.as_ref().expect("the picker opens");
let labels: Vec<&str> = picker.rows.iter().map(|r| r.label.as_str()).collect();
assert_eq!(labels, ["a.py:5 return 2"]);
app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
.unwrap();
assert_eq!(app.editor.active_index(), symbol_tab);
assert_eq!(app.editor.cursor_row, 4);
assert!(app.editor.is_bookmarked(4));
}
#[test]
fn a_bookmark_jump_drops_the_destination_tabs_selection() {
use crate::widgets::command_palette::Command;
let tmp = tempfile::tempdir().unwrap();
let a = tmp.path().join("a.txt");
std::fs::write(&a, "alpha\nbeta\ngamma\n").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&a).unwrap();
app.editor.cursor_row = 1;
app.editor.toggle_bookmark();
app.editor.select_all();
assert!(app.editor.selection.is_some());
app.run_command(Command::ViewBookmarks);
app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
.unwrap();
assert_eq!(app.editor.cursor_row, 1);
assert!(
app.editor.selection.is_none(),
"no selection left to type over"
);
}
#[test]
fn a_bookmark_jump_drops_the_destination_tabs_extra_carets() {
use crate::widgets::command_palette::Command;
let tmp = tempfile::tempdir().unwrap();
let a = tmp.path().join("a.txt");
std::fs::write(&a, "alpha\nbeta\ngamma\n").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open(&a).unwrap();
app.editor.cursor_row = 1;
app.editor.toggle_bookmark();
app.editor.cursor_row = 0;
app.editor.add_cursor_below();
assert!(!app.editor.carets.is_empty(), "a second caret is set");
app.run_command(Command::ViewBookmarks);
app.handle_key(key(KeyCode::Enter, KeyModifiers::NONE))
.unwrap();
assert_eq!(app.editor.cursor_row, 1);
assert!(
app.editor.carets.is_empty(),
"no extra caret left to type at"
);
}
#[test]
fn the_editor_caret_is_hidden_while_a_list_picker_is_open() {
use crate::widgets::command_palette::Command;
let tmp = tempfile::tempdir().unwrap();
let a = tmp.path().join("a.txt");
std::fs::write(&a, "alpha\nbeta\n").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open_in_new_tab(&a).unwrap();
app.focus_pane(Pane::Editor);
app.editor.toggle_bookmark();
let mut term = ratatui::Terminal::new(ratatui::backend::TestBackend::new(100, 30)).unwrap();
term.draw(|f| app.render(f)).unwrap();
app.screen_reader = true;
assert!(
app.cursor_should_be_visible(),
"the caret shows in the editor"
);
let editor_caret = app.editor.cursor_screen_pos();
app.run_command(Command::ViewBookmarks);
assert!(app.list_picker.is_some());
assert!(
!app.cursor_should_be_visible(),
"the caret stays hidden behind View Bookmarks"
);
term.draw(|f| app.render(f)).unwrap();
let picker = app.list_picker.as_ref().unwrap();
let caret = app
.quick_input_caret()
.expect("Loch hands the query its caret");
assert_eq!(Some(caret), picker.last_caret);
assert_eq!(caret.1, picker.last_rect.y + 1, "on the picker's input row");
assert_ne!(Some(caret), editor_caret, "not the editor's");
assert!(app.cursor_should_be_visible());
app.dismiss_list_picker();
assert!(
app.cursor_should_be_visible(),
"and returns with the picker closed"
);
}
#[test]
fn quick_open_line_hint_counts_the_buffer_a_pick_opens_as_it_is_now() {
let tmp = tempfile::tempdir().unwrap();
let numbered = |n: usize| -> Vec<String> { (1..=n).map(|i| format!("line {i}")).collect() };
for (rel, n) in [("web/a.ts", 50), ("api/a.ts", 400)] {
let path = tmp.path().join(rel);
std::fs::create_dir_all(path.parent().unwrap()).unwrap();
std::fs::write(&path, numbered(n).join("\n") + "\n").unwrap();
}
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor
.open_preview(&tmp.path().join("web/a.ts"))
.unwrap();
let listed = |app: &mut App| -> Vec<String> {
let finder = app.file_finder.as_mut().expect("Quick Open is open");
finder.finish_line_check();
let mut rels: Vec<String> = finder
.visible_results()
.iter()
.map(|r| r.entry.rel.clone())
.collect();
rels.sort_unstable();
rels
};
let quick_open = |app: &mut App| {
app.handle_key(key(KeyCode::Char('p'), KeyModifiers::SUPER))
.unwrap();
type_keys(app, "a.ts:318");
};
quick_open(&mut app);
assert_eq!(listed(&mut app), ["api/a.ts"], "the open web/a.ts is short");
app.editor.replace_all_lines(numbered(400));
assert!(
app.sync_file_finder_open_lines(),
"the grown buffer is recounted"
);
assert_eq!(listed(&mut app), ["api/a.ts", "web/a.ts"]);
assert!(!app.sync_file_finder_open_lines(), "nothing changed since");
app.close_file_finder();
app.split_editor();
assert!(
app.editor.lines.len() < 318,
"the split opened the saved file"
);
quick_open(&mut app);
assert_eq!(
listed(&mut app),
["api/a.ts"],
"a pick opens the active group's short copy"
);
}
#[cfg(target_os = "linux")]
#[test]
fn every_command_matches_the_saved_registry() {
use crate::command_registry::{ALL_COMMANDS, LAYOUT_COMMANDS, hint_for_platform};
use std::fmt::Write as _;
let mut text = String::from(
"# Every palette command: src/app/tests/palette.rs. Rewrite with CROFT_BLESS=1 after a change meant to move one.\n\
# palette place | variant | id | title | keyword | macOS hint | Linux hint | groups\n",
);
for (place, cmd) in ALL_COMMANDS.iter().enumerate() {
let hint = cmd.keybinding_hint();
let groups: Vec<&str> = [
(LAYOUT_COMMANDS.contains(cmd), "layout"),
(cmd.skips_the_shell(), "skips-the-shell"),
(cmd.is_codeql(), "codeql"),
]
.into_iter()
.filter_map(|(member, group)| member.then_some(group))
.collect();
let row = format!(
"{place} | {cmd:?} | {} | {} | {} | {} | {} | {}",
cmd.id(),
cmd.title(),
cmd.keyword(),
hint_for_platform(hint, true),
hint_for_platform(hint, false),
groups.join(" "),
);
writeln!(text, "{}", row.trim_end()).unwrap();
}
text.push_str("# Customize Layout, in the popup's order\n");
for cmd in LAYOUT_COMMANDS {
writeln!(text, "layout | {cmd:?}").unwrap();
}
super::golden::check_text("commands", &text);
}
#[test]
fn no_pane_caret_blinks_through_a_popup() {
use crate::app::popups::Popup;
let draw = |app: &mut App| {
let backend = super::CaretBackend {
inner: ratatui::backend::TestBackend::new(120, 40),
caret_shown: false,
};
let mut term = ratatui::Terminal::new(backend).unwrap();
app.cursor_blink.poke();
term.draw(|f| app.render(f)).unwrap();
let shown = term.backend().caret_shown;
let at = ratatui::backend::Backend::get_cursor_position(term.backend_mut()).unwrap();
(shown, (at.x, at.y))
};
let mut wrong = Vec::new();
for theme in [
crate::theme::Theme::BLACK,
crate::theme::Theme::from_id("loch"),
] {
for p in Popup::ALL {
let tmp = tempfile::tempdir().unwrap();
let file = tmp.path().join("a.txt");
std::fs::write(&file, "one\ntwo\n").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.apply_theme(theme);
app.editor.open_in_new_tab(&file).unwrap();
app.focus_pane(Pane::Editor);
let (shown, editor_caret) = draw(&mut app);
assert!(shown, "the editor's caret shows with no pop-up open");
let _hook = super::popups::open(&mut app, p);
let (shown, at) = draw(&mut app);
if shown && at == editor_caret {
wrong.push(format!("{p:?} on {}", theme.id()));
}
assert_eq!(
app.cursor_should_be_visible(),
shown,
"{p:?} on {}: the pictures' caret rule must agree with the frame's",
theme.id()
);
}
}
assert!(
wrong.is_empty(),
"the editor's caret blinked through these pop-ups: {}",
wrong.join(", ")
);
}
#[test]
fn the_connect_dialog_keeps_its_own_caret() {
let tmp = tempfile::tempdir().unwrap();
let file = tmp.path().join("a.txt");
std::fs::write(&file, "one\n").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open_in_new_tab(&file).unwrap();
app.focus_pane(Pane::Editor);
let mut dialog = crate::widgets::connect_dialog::ConnectDialog::new(String::from("devbox"));
dialog.set_prompt(
crate::remote_connect::PromptKind::Password,
String::from("devbox's password:"),
);
app.connect_dialog = Some(dialog);
let backend = super::CaretBackend {
inner: ratatui::backend::TestBackend::new(120, 40),
caret_shown: false,
};
let mut term = ratatui::Terminal::new(backend).unwrap();
app.cursor_blink.poke();
term.draw(|f| app.render(f)).unwrap();
let field = app
.connect_dialog
.as_ref()
.and_then(|d| d.cursor_screen_pos())
.expect("the dialog places its caret in its field");
assert!(
term.backend().caret_shown,
"the frame shows the dialog's caret"
);
let at = ratatui::backend::Backend::get_cursor_position(term.backend_mut()).unwrap();
assert_eq!(
(at.x, at.y),
field,
"at the field, not at the editor's caret"
);
assert!(
app.cursor_should_be_visible(),
"the pictures put the dialog's caret back after drawing"
);
}