use super::*;
#[test]
fn shift_alt_drag_starts_a_column_selection() {
use crossterm::event::{MouseButton, MouseEventKind};
let tmp = tempfile::tempdir().unwrap();
let f = tmp.path().join("t.rs");
std::fs::write(&f, "aaaaaaaa\nbbbbbbbb\ncccccccc\n").unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.open_pinned(&f).unwrap();
let backend = ratatui::backend::TestBackend::new(100, 30);
let mut term = ratatui::Terminal::new(backend).unwrap();
term.draw(|frame| app.render(frame)).unwrap();
let col = app.editor.last_inner.x + 8;
let row = app.editor.last_inner.y;
let mut down = mouse(MouseEventKind::Down(MouseButton::Left), col, row);
down.modifiers = KeyModifiers::ALT | KeyModifiers::SHIFT;
app.handle_mouse(down);
assert!(
app.editor.box_selecting(),
"Shift+Alt press must anchor a column selection"
);
let mut drag = mouse(MouseEventKind::Drag(MouseButton::Left), col + 3, row + 2);
drag.modifiers = KeyModifiers::ALT | KeyModifiers::SHIFT;
app.handle_mouse(drag);
assert!(
app.editor.carets.len() >= 2,
"Shift+Alt+drag must build a column of carets, got {}",
app.editor.carets.len()
);
let head = app
.editor
.selection
.expect("the head row must span columns");
assert_eq!(
head.head.1 - head.anchor.1,
3,
"head row must span 3 columns"
);
for c in &app.editor.carets {
assert_eq!(
(c.anchor.1, c.head.1),
(head.anchor.1, head.head.1),
"every row of a column selection covers the same columns"
);
}
}
#[test]
fn the_at_click_commands_all_refuse_a_non_text_view() {
let tmp = tempfile::tempdir().unwrap();
let bin = tmp.path().join("a.bin");
std::fs::write(&bin, [0u8, 1, 2, 3, 4, 5, 6, 7]).unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.editor.hex = Some(crate::hex::HexView::open(&bin).unwrap());
assert!(
app.editor.has_non_text_view(),
"precondition: the fixture must actually open a non-text view, or \
every assertion below is about the ordinary text path"
);
app.last_mouse_pos = Some((10, 10));
for (cmd, want) in [
(
crate::widgets::command_palette::Command::MouseAddCursorAtClick,
"No cursors in this view",
),
(
crate::widgets::command_palette::Command::MouseGoToDefinitionAtClick,
"No definitions in this view",
),
(
crate::widgets::command_palette::Command::MouseOpenLinkAtClick,
"No links in this view",
),
] {
app.status = String::from("untouched");
app.run_command(cmd);
assert_eq!(
app.status, want,
"{cmd:?} must refuse on a non-text view rather than acting on a \
buffer position the user cannot see"
);
}
}
#[test]
fn a_modified_double_click_can_fire_at_all() {
use crossterm::event::{MouseButton, MouseEventKind};
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.keymap = crate::keymap::Keymap::from_json(
r#"[{"key": "ctrl+double_click", "command": "toggle_word_wrap", "when": "terminal"}]"#,
);
let backend = ratatui::backend::TestBackend::new(120, 40);
let mut term = ratatui::Terminal::new(backend).unwrap();
term.draw(|f| app.render(f)).unwrap();
let before = app.editor.wrap_enabled();
let area = app.terminals[0].last_area;
assert!(
area.width > 4 && area.height > 2,
"the terminal must be laid out, or the click lands nowhere and this \
test passes or fails for reasons unrelated to double-click tracking"
);
let (col, row) = (area.x + 2, area.y + 1);
let mut first = mouse(MouseEventKind::Down(MouseButton::Left), col, row);
first.modifiers = KeyModifiers::CONTROL;
app.handle_mouse(first);
assert_eq!(
app.editor.wrap_enabled(),
before,
"the first click of the pair is not yet a double"
);
let mut second = mouse(MouseEventKind::Down(MouseButton::Left), col, row);
second.modifiers = KeyModifiers::CONTROL;
app.handle_mouse(second);
assert_ne!(
app.editor.wrap_enabled(),
before,
"ctrl+double_click must be reachable, not permanently dead"
);
}
#[test]
fn the_at_click_commands_refuse_a_keyboard_invocation() {
use crate::widgets::command_palette::Command;
let (mut app, _tmp) = tree_app_with_keymap("[]");
for (cmd, want) in [
(
Command::MouseAddCursorAtClick,
"Mouse: Add Cursor at Click needs a mouse binding",
),
(
Command::MouseGoToDefinitionAtClick,
"Mouse: Go to Definition at Click needs a mouse binding",
),
(
Command::MouseOpenLinkAtClick,
"Mouse: Open Link at Click needs a mouse binding",
),
] {
app.last_mouse_pos = None;
app.status.clear();
app.run_command(cmd);
assert_eq!(app.status, want, "{cmd:?} must refuse a keyboard call");
}
}
#[test]
fn an_unmatched_modified_click_does_not_arm_the_tracker() {
use crossterm::event::{MouseButton, MouseEventKind};
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.keymap = crate::keymap::Keymap::from_json(
r#"[{"key": "alt+double_click", "command": "toggle_word_wrap", "when": "terminal"}]"#,
);
let backend = ratatui::backend::TestBackend::new(120, 40);
let mut term = ratatui::Terminal::new(backend).unwrap();
term.draw(|f| app.render(f)).unwrap();
let area = app.terminals[0].last_area;
assert!(
area.width > 4 && area.height > 2,
"terminal must be laid out"
);
app.terminals[0].feed_bytes_for_test(
b"a prompt got here first\r\nbash-5.2$ hello_world_token some other text\r\n",
);
term.draw(|f| app.render(f)).unwrap();
let (col, row) = cell_carrying(&app.terminals[0], "hello_world_token")
.expect("the pane must show THIS test's token, or there is no word to mis-select");
let ctrl_gesture = crate::keymap::Gesture {
kind: crate::keymap::GestureKind::Click,
mods: KeyModifiers::CONTROL,
};
assert!(
app.keymap
.command_for_mouse(ctrl_gesture, crate::keymap::MouseContext::Terminal)
.is_none(),
"ctrl+click must match nothing, or this never reaches the built-in"
);
assert!(
!app.keymap
.is_double_click_prefix(ctrl_gesture, crate::keymap::MouseContext::Terminal),
"ctrl+click must NOT be a double-click prefix here, or the swallow \
branch returns first and the built-in is never reached"
);
let mut ctrl = mouse(MouseEventKind::Down(MouseButton::Left), col, row);
ctrl.modifiers = KeyModifiers::CONTROL;
app.handle_mouse(ctrl);
app.handle_mouse(mouse(MouseEventKind::Down(MouseButton::Left), col + 1, row));
let selected = app.terminal().selection_text();
assert!(
!selected.contains("hello_world_token"),
"a plain click after an unmatched ctrl+click must not select a WORD: \
the ctrl+click must not have armed the double-click tracker, but \
selection was {selected:?}"
);
app.handle_mouse(mouse(MouseEventKind::Down(MouseButton::Left), col, row));
assert_eq!(
app.terminal().selection_text(),
"hello_world_token",
"a PLAIN click pairing with the plain click above must select the \
word: the tracker arms on plain clicks and refuses only modified \
ones, and without this the negative above proves nothing"
);
}
#[test]
fn a_modified_click_that_drags_away_does_not_pair_with_the_click_it_returns_to() {
use crossterm::event::{MouseButton, MouseEventKind};
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.keymap = crate::keymap::Keymap::from_json(
r#"[{"key": "ctrl+double_click", "command": "toggle_word_wrap", "when": "terminal"}]"#,
);
let backend = ratatui::backend::TestBackend::new(120, 40);
let mut term = ratatui::Terminal::new(backend).unwrap();
term.draw(|f| app.render(f)).unwrap();
let area = app.terminals[0].last_area;
assert!(
area.width > 4 && area.height > 2,
"terminal must be laid out"
);
let (col, row) = (area.x + 2, area.y + 1);
let before = app.editor.wrap_enabled();
let mut ctrl = mouse(MouseEventKind::Down(MouseButton::Left), col, row);
ctrl.modifiers = KeyModifiers::CONTROL;
app.handle_mouse(ctrl);
let mut drag = mouse(MouseEventKind::Drag(MouseButton::Left), col + 20, row + 3);
drag.modifiers = KeyModifiers::CONTROL;
app.handle_mouse(drag);
app.handle_mouse(mouse(
MouseEventKind::Up(MouseButton::Left),
col + 20,
row + 3,
));
let mut back = mouse(MouseEventKind::Down(MouseButton::Left), col, row);
back.modifiers = KeyModifiers::CONTROL;
app.handle_mouse(back);
assert_eq!(
app.editor.wrap_enabled(),
before,
"a modified click that dragged away must not pair with the click it \
returns to, but the double fired"
);
}
#[test]
fn cmd_clicking_a_non_web_hyperlink_refuses_instead_of_opening() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
let backend = ratatui::backend::TestBackend::new(120, 40);
let mut term = ratatui::Terminal::new(backend).unwrap();
app.terminal_mut().focused = true;
term.draw(|f| app.render(f)).unwrap();
let script = "printf 'ref \\033]8;;file:///nonexistent-croft-probe\\033\\\\CLICK-ME\\033]8;;\\033\\\\\\n'";
app.terminal_mut()
.write_input(format!("{script}\r").as_bytes());
let linked_cell = |app: &App| -> Option<(usize, usize)> {
(0..40usize).find_map(|r| {
(0..120usize)
.find(|&c| {
app.terminal().hyperlink_at(r, c).as_deref()
== Some("file:///nonexistent-croft-probe")
})
.map(|c| (r, c))
})
};
crate::test_budget::await_spawned(
crate::test_budget::tests::SHELL_PAINT_BASE,
"the shell to paint the linked cell",
|| linked_cell(&app).is_some(),
);
term.draw(|f| app.render(f)).unwrap();
let (row_idx, col) = linked_cell(&app).expect("shell must print the linked text");
let inner = app.terminal().last_inner;
app.handle_mouse(crossterm::event::MouseEvent {
kind: crossterm::event::MouseEventKind::Down(crossterm::event::MouseButton::Left),
column: inner.x + col as u16,
row: inner.y + row_idx as u16,
modifiers: KeyModifiers::SUPER,
});
assert!(
!app.status.starts_with("Opened"),
"a file:// hyperlink must never reach the system opener (status: {})",
app.status
);
assert!(
app.status.contains("file:///nonexistent-croft-probe"),
"the refusal must surface the real destination (status: {})",
app.status
);
}
#[test]
fn keyboard_enhancement_flags_request_disambiguate_escape_codes() {
let f = keyboard_enhancement_flags();
assert!(
f.contains(crossterm::event::KeyboardEnhancementFlags::DISAMBIGUATE_ESCAPE_CODES),
"Cmd / modifier delivery requires DISAMBIGUATE_ESCAPE_CODES"
);
}
#[test]
fn keyboard_enhancement_flags_do_not_force_all_keys_as_escapes() {
let f = keyboard_enhancement_flags();
assert!(
!f.contains(crossterm::event::KeyboardEnhancementFlags::REPORT_ALL_KEYS_AS_ESCAPE_CODES),
"we should not opt into REPORT_ALL_KEYS_AS_ESCAPE_CODES"
);
}
#[test]
fn screencast_mode_shows_the_keys_and_names_what_the_palette_ran() {
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.handle_key(key(KeyCode::F(8), KeyModifiers::SHIFT))
.unwrap();
assert!(app.screencast.is_on(), "{}", app.status);
assert!(run_from_palette(&mut app, "View: Toggle Zen Mode"));
let screen = screen_text_863(&mut app, 120, 30);
assert!(
screen.contains(" View: Toggle Zen Mode Enter View: Toggle Zen Mode"),
"the query typed, then Enter and what it ran:\n{screen}"
);
assert!(
screen.contains(zen_hint_text()),
"the keys sit above the Zen hint, not over it:\n{screen}"
);
let now = std::time::Instant::now();
assert!(!app.screencast.expire(now));
assert!(app.screencast.expire(now + screencast::LINGER));
assert!(!screen_text_863(&mut app, 120, 30).contains("Enter View"));
app.handle_key(key(KeyCode::F(8), KeyModifiers::SHIFT))
.unwrap();
assert!(!app.screencast.is_on());
assert_eq!(app.status, "Screencast Mode off");
assert!(
app.perf.status_span().is_none(),
"Shift+F8 is not the F8 HUD"
);
}
#[test]
fn screencast_mode_leaves_out_typing_into_a_password_field_or_the_shell() {
use crate::widgets::input_prompt::{InputPrompt, InputPurpose};
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.toggle_screencast_mode();
app.input_prompt = Some(InputPrompt::new(InputPurpose::NewWorktreeLane, "Token", "").masked());
for c in "hunter2".chars() {
app.handle_key(key(KeyCode::Char(c), KeyModifiers::NONE))
.unwrap();
}
app.handle_key(key(KeyCode::Char('q'), KeyModifiers::ALT))
.unwrap();
assert_eq!(
app.input_prompt.as_ref().unwrap().value,
"hunter2q",
"the field took the Alt-held character as part of the secret"
);
app.handle_key(key(KeyCode::Esc, KeyModifiers::NONE))
.unwrap();
assert!(app.input_prompt.is_none(), "Esc closed the prompt");
assert_eq!(app.screencast.row_text(200), " Esc ");
app.focus = Pane::Terminal;
for c in "s3cret".chars() {
app.handle_key(key(KeyCode::Char(c), KeyModifiers::NONE))
.unwrap();
}
app.handle_key(key(KeyCode::Char('c'), KeyModifiers::CONTROL))
.unwrap();
assert_eq!(app.screencast.row_text(200), " Esc Ctrl+C ");
let cmd = if cfg!(target_os = "macos") {
"Cmd"
} else {
"Super"
};
app.handle_key(key(KeyCode::Char('k'), KeyModifiers::SUPER))
.unwrap();
app.handle_key(key(KeyCode::Char('?'), KeyModifiers::NONE))
.unwrap();
assert_eq!(
app.screencast.row_text(200),
format!(" Esc Ctrl+C {cmd}+K "),
"the ? went to the shell"
);
app.handle_key(key(KeyCode::Char('k'), KeyModifiers::SUPER))
.unwrap();
app.handle_key(key(KeyCode::Char('k'), KeyModifiers::NONE))
.unwrap();
assert_eq!(
app.screencast.row_text(200),
format!(" Esc Ctrl+C {cmd}+K {cmd}+K K "),
"Cmd+K K cleared the terminal"
);
app.focus = Pane::Editor;
for c in "ok".chars() {
app.handle_key(key(KeyCode::Char(c), KeyModifiers::NONE))
.unwrap();
}
assert!(
app.screencast.row_text(200).ends_with(" ok "),
"typing in the editor shows"
);
}
#[test]
fn screencast_mode_lights_a_clicked_cell_until_it_fades() {
use crossterm::event::MouseButton;
let (mut app, _tmp, col, row) = app_with_open_file_and_editor_cell();
let lit = |app: &mut App| {
let mut term = ratatui::Terminal::new(ratatui::backend::TestBackend::new(100, 30)).unwrap();
term.draw(|f| app.render(f)).unwrap();
term.backend().buffer()[(col, row)].bg == app.theme.accent()
};
press(&mut app, MouseButton::Left, (col, row));
assert!(!lit(&mut app), "only while Screencast Mode is on");
app.toggle_screencast_mode();
press(&mut app, MouseButton::Left, (col, row));
assert!(lit(&mut app));
let now = std::time::Instant::now();
assert!(app.screencast.expire(now + screencast::CLICK_LIT_FOR));
assert!(!lit(&mut app));
}
#[test]
fn a_click_on_the_cell_being_edited_keeps_editing() {
let (_tmp, _p, mut app) = prices_xlsx_app();
app.handle_sheet_key(key(KeyCode::Right, KeyModifiers::NONE));
type_into_sheet(&mut app, "5");
let (x, y) = sheet_cell_on_screen(&app, 0, 1);
left_click(&mut app, x, y);
let view = app.editor.sheet.as_ref().unwrap();
assert!(view.editing.is_some());
assert!(view.cell_edits.is_empty() && !app.editor.dirty);
}
#[test]
fn parse_dropped_paths_accepts_a_single_existing_absolute_path() {
let tmp = tempfile::tempdir().unwrap();
let f = tmp.path().join("dropped.txt");
std::fs::write(&f, "hi").unwrap();
let parsed = parse_dropped_paths(&format!("{}\n", f.display()));
assert_eq!(parsed, vec![f]);
}
#[test]
fn parse_dropped_paths_keeps_user_pngs_that_happen_to_share_a_temp_dir() {
let tmp = tempfile::tempdir().unwrap();
let temp_dir = tmp.path().join("T");
std::fs::create_dir_all(&temp_dir).unwrap();
let user_png = temp_dir.join("my_screenshot.png");
std::fs::write(&user_png, b"\x89PNG\r\n\x1a\n").unwrap();
let parsed = parse_dropped_paths(&format!("{}\n", user_png.display()));
assert_eq!(
parsed,
vec![user_png.clone()],
"a user-named PNG under a temp dir must still be droppable — only the literal iTerm2.<rand>.png pattern is the artefact"
);
}
#[test]
fn parse_dropped_paths_strips_file_url_prefix_and_decodes() {
let tmp = tempfile::tempdir().unwrap();
let f = tmp.path().join("with space.txt");
std::fs::write(&f, "hi").unwrap();
let url = format!("file://{}/with%20space.txt", tmp.path().to_string_lossy());
let parsed = parse_dropped_paths(&url);
assert_eq!(parsed, vec![f]);
}
#[test]
fn parse_dropped_paths_handles_backslash_escaped_spaces() {
let tmp = tempfile::tempdir().unwrap();
let f = tmp.path().join("with space.txt");
std::fs::write(&f, "hi").unwrap();
let escaped = format!("{}/with\\ space.txt", tmp.path().to_string_lossy());
let parsed = parse_dropped_paths(&escaped);
assert_eq!(parsed, vec![f]);
}
#[test]
fn parse_dropped_paths_supports_multi_file_drop_via_newlines() {
let tmp = tempfile::tempdir().unwrap();
let a = tmp.path().join("a.txt");
let b = tmp.path().join("b.txt");
std::fs::write(&a, "1").unwrap();
std::fs::write(&b, "2").unwrap();
let payload = format!("{}\n{}\n", a.display(), b.display());
let parsed = parse_dropped_paths(&payload);
assert_eq!(parsed.len(), 2);
assert!(parsed.contains(&a));
assert!(parsed.contains(&b));
}
#[test]
fn parse_dropped_paths_supports_multi_file_drop_via_unescaped_spaces() {
let tmp = tempfile::tempdir().unwrap();
let a = tmp.path().join("02 Аленький цветочек.mp3");
let b = tmp.path().join("03 Буратино.mp3");
std::fs::write(&a, "x").unwrap();
std::fs::write(&b, "y").unwrap();
let payload = format!(
"{}/02\\ Аленький\\ цветочек.mp3 {}/03\\ Буратино.mp3",
tmp.path().to_string_lossy(),
tmp.path().to_string_lossy(),
);
let parsed = parse_dropped_paths(&payload);
assert_eq!(parsed.len(), 2, "expected two paths, got {parsed:?}");
assert!(parsed.contains(&a));
assert!(parsed.contains(&b));
}
#[test]
fn parse_dropped_paths_keeps_quoted_path_with_space_intact() {
let tmp = tempfile::tempdir().unwrap();
let a = tmp.path().join("with space.txt");
std::fs::write(&a, "x").unwrap();
let payload = format!("\"{}\"", a.display());
let parsed = parse_dropped_paths(&payload);
assert_eq!(parsed, vec![a]);
}
#[test]
fn parse_dropped_paths_handles_mix_of_quoted_and_escaped_paths() {
let tmp = tempfile::tempdir().unwrap();
let a = tmp.path().join("plain.txt");
let b = tmp.path().join("with space.txt");
std::fs::write(&a, "x").unwrap();
std::fs::write(&b, "y").unwrap();
let payload = format!("{} \"{}\"", a.display(), b.display(),);
let parsed = parse_dropped_paths(&payload);
assert_eq!(parsed.len(), 2, "got {parsed:?}");
assert!(parsed.contains(&a));
assert!(parsed.contains(&b));
}
#[test]
fn parse_dropped_paths_returns_empty_for_plain_text() {
let parsed = parse_dropped_paths("hello world this is some text");
assert!(parsed.is_empty());
}
#[test]
fn parse_dropped_paths_skips_nonexistent_paths() {
let parsed = parse_dropped_paths("/this/path/definitely/does/not/exist");
assert!(parsed.is_empty());
}
#[test]
fn finder_drop_into_explorer_moves_file_into_workspace() {
let workspace = tempfile::tempdir().unwrap();
let outside = tempfile::tempdir().unwrap();
let src = outside.path().join("dragged.txt");
std::fs::write(&src, "hello").unwrap();
let mut app = App::new(workspace.path().to_path_buf()).unwrap();
app.focus_pane(Pane::Tree);
app.set_sidebar_view(SidebarView::Explorer);
app.handle_paste(&format!("{}\n", src.display()));
let dest = workspace.path().join("dragged.txt");
assert!(dest.exists(), "file should land in the workspace");
assert!(!src.exists(), "Finder drop must move, not copy");
assert_eq!(std::fs::read_to_string(&dest).unwrap(), "hello");
}
#[test]
fn finder_drop_into_terminal_pastes_path_instead_of_scp_in_remote_view() {
let workspace = tempfile::tempdir().unwrap();
let outside = tempfile::tempdir().unwrap();
let src = outside.path().join("dragged.txt");
std::fs::write(&src, "hello").unwrap();
let mut app = App::new(workspace.path().to_path_buf()).unwrap();
app.remote.targets = vec![crate::remote::RemoteTarget {
alias: String::from("alpha"),
host_name: Some(String::from("alpha.example.com")),
user: Some(String::from("vitali")),
}];
app.remote.selected = 0;
app.set_sidebar_view(SidebarView::Remote);
app.show_terminal = true;
app.focus_pane(Pane::Terminal);
let editor_before = app.editor.lines.clone();
app.handle_paste(&format!("{}\n", src.display()));
assert!(
app.take_pending_scp_uploads().is_empty(),
"terminal-focused drop must not queue an scp upload",
);
assert_eq!(
app.editor.lines, editor_before,
"terminal-focused drop must not leak the path into the editor",
);
assert!(src.exists(), "the drop must not move or delete the source");
}
#[test]
fn finder_drop_into_editor_inserts_path_instead_of_scp_in_remote_view() {
let workspace = tempfile::tempdir().unwrap();
let outside = tempfile::tempdir().unwrap();
let src = outside.path().join("e.txt");
std::fs::write(&src, "x").unwrap();
let mut app = App::new(workspace.path().to_path_buf()).unwrap();
app.remote.targets = vec![crate::remote::RemoteTarget {
alias: String::from("beta"),
host_name: None,
user: None,
}];
app.remote.selected = 0;
app.set_sidebar_view(SidebarView::Remote);
app.focus_pane(Pane::Editor);
app.handle_paste(&format!("{}\n", src.display()));
assert!(
app.take_pending_scp_uploads().is_empty(),
"editor-focused drop must not queue an scp upload",
);
assert!(
app.editor.lines.iter().any(|l| l.contains("e.txt")),
"editor-focused drop should insert the dropped path, was: {:?}",
app.editor.lines,
);
}
#[test]
fn finder_drop_onto_remote_tree_still_queues_scp() {
let workspace = tempfile::tempdir().unwrap();
let outside = tempfile::tempdir().unwrap();
let src = outside.path().join("dragged.txt");
std::fs::write(&src, "hello").unwrap();
let mut app = App::new(workspace.path().to_path_buf()).unwrap();
app.remote.targets = vec![crate::remote::RemoteTarget {
alias: String::from("alpha"),
host_name: Some(String::from("alpha.example.com")),
user: Some(String::from("vitali")),
}];
app.remote.selected = 0;
app.set_sidebar_view(SidebarView::Remote);
app.focus_pane(Pane::Tree);
app.handle_paste(&format!("{}\n", src.display()));
let queued = app.take_pending_scp_uploads();
assert_eq!(queued.len(), 1, "one scp upload should be queued");
assert_eq!(queued[0].alias, "alpha");
assert_eq!(queued[0].src, src);
assert!(src.exists(), "scp must copy, not move");
}
#[test]
fn finder_drop_on_remote_view_with_no_target_reports_clear_status() {
let workspace = tempfile::tempdir().unwrap();
let outside = tempfile::tempdir().unwrap();
let src = outside.path().join("dragged.txt");
std::fs::write(&src, "hi").unwrap();
let mut app = App::new(workspace.path().to_path_buf()).unwrap();
app.remote.targets.clear();
app.remote.selected = 0;
app.set_sidebar_view(SidebarView::Remote);
app.focus_pane(Pane::Tree);
app.handle_paste(&format!("{}\n", src.display()));
assert!(app.take_pending_scp_uploads().is_empty());
assert!(
app.status.contains("no Remote Explorer host"),
"status must explain the failure, was: {:?}",
app.status,
);
assert!(
src.exists(),
"with no host the source file must be left intact",
);
}
#[test]
fn pure_path_payload_distinguishes_finder_drag_from_text_paste() {
assert!(super::is_pure_path_payload("/Users/v/Documents/foo.txt"));
assert!(super::is_pure_path_payload("/Users/v/a.txt /Users/v/b.txt"));
assert!(super::is_pure_path_payload("'/Users/v/with space.txt'"));
assert!(!super::is_pure_path_payload("cat /etc/hosts"));
assert!(!super::is_pure_path_payload("see /Users/v/foo.txt please"));
assert!(!super::is_pure_path_payload("relative/path.txt"));
assert!(!super::is_pure_path_payload(""));
}
#[test]
fn an_esc_that_did_something_is_not_half_of_esc_esc() {
let (_tmp, mut app) = zen_app();
app.toggle_zen_mode();
app.handle_key(key(KeyCode::Right, KeyModifiers::SHIFT))
.unwrap();
assert!(app.editor.selection.is_some(), "precondition: a selection");
app.handle_key(key(KeyCode::Esc, KeyModifiers::NONE))
.unwrap();
assert!(app.editor.selection.is_none(), "the first Esc cleared it");
app.handle_key(key(KeyCode::Esc, KeyModifiers::NONE))
.unwrap();
assert!(
app.zen_mode,
"the Esc that cleared the selection did not count"
);
app.zen_esc_at = None;
app.handle_key(key(KeyCode::Esc, KeyModifiers::NONE))
.unwrap();
app.handle_key(key(KeyCode::Right, KeyModifiers::NONE))
.unwrap();
app.handle_key(key(KeyCode::Esc, KeyModifiers::NONE))
.unwrap();
assert!(app.zen_mode, "a key between the Escapes breaks the pair");
app.zen_esc_at = None;
let t = std::time::Instant::now();
app.note_idle_zen_esc(t);
app.note_idle_zen_esc(t + ZEN_ESC_WINDOW + std::time::Duration::from_millis(1));
assert!(app.zen_mode, "too far apart");
app.zen_esc_at = None;
app.focus = Pane::Terminal;
app.handle_key(key(KeyCode::Esc, KeyModifiers::NONE))
.unwrap();
app.handle_key(key(KeyCode::Esc, KeyModifiers::NONE))
.unwrap();
assert!(app.zen_mode, "Escapes for a terminal program stay its own");
}
#[test]
fn clicking_an_annotated_span_still_clears_the_click_selection() {
use crossterm::event::{MouseButton, MouseEventKind};
let tmp = tempfile::tempdir().unwrap();
let mut app = App::new(tmp.path().to_path_buf()).unwrap();
app.terminals[0] = crate::widgets::terminal::PtyTerminal::new_running(
"/bin/sh",
&[String::from("-c"), String::from("sleep 30")],
tmp.path(),
)
.unwrap();
app.focus_pane(Pane::Terminal);
let backend = ratatui::backend::TestBackend::new(100, 30);
let mut term = ratatui::Terminal::new(backend).unwrap();
term.draw(|f| app.render(f)).unwrap();
app.terminals[0].feed_bytes_for_test(b"annotated words here\r\n");
let line = app.terminals[0]
.find_captured_line("annotated words here")
.expect("the fed text is on the grid");
assert!(
line >= 0,
"with no scrollback the text sits on a viewport row, got line {line}"
);
let clock = app.terminals[0].scroll_clock();
app.terminals[0].add_annotation(line, clock, 0, 15, String::from("the note"));
let inner = app.terminals[0].last_inner;
let (cx, cy) = (inner.x + 2, inner.y + line as u16);
assert!(
app.terminals[0]
.line_text_at(cx, cy)
.is_some_and(|(text, idx)| text.starts_with("annotated words here") && idx == 2),
"the click must land ON the annotated text, or the popup assertion is vacuous"
);
app.handle_mouse(mouse(MouseEventKind::Down(MouseButton::Left), cx, cy));
app.handle_mouse(mouse(MouseEventKind::Up(MouseButton::Left), cx, cy));
assert!(app.hover_popup.is_some(), "the click must pop the note");
assert!(
app.terminals[0].selection().is_none(),
"the zero-area click selection must not stay painted"
);
}
#[test]
fn a_dropped_file_url_with_a_non_ascii_path_decodes_to_utf8() {
use std::path::PathBuf;
assert_eq!(
super::normalise_dropped_token("file:///tmp/caf%C3%A9/%E4%B8%AD%E6%96%87.txt"),
Some(PathBuf::from("/tmp/caf\u{e9}/\u{4e2d}\u{6587}.txt")),
"percent-encoded UTF-8 reassembles into the characters it encodes"
);
assert_eq!(
super::normalise_dropped_token("file:///tmp/caf\u{e9}"),
Some(PathBuf::from("/tmp/caf\u{e9}")),
"a literal non-ASCII character in the URL survives too"
);
assert_eq!(
super::normalise_dropped_token("file:///tmp/a%20b"),
Some(PathBuf::from("/tmp/a b"))
);
assert_eq!(
super::normalise_dropped_token("'/tmp/x y'"),
Some(PathBuf::from("/tmp/x y"))
);
for (url, want) in [
("file:///tmp/a+b", "/tmp/a+b"),
("file:///tmp/a%zzb", "/tmp/a%zzb"),
("file:///tmp/a%2", "/tmp/a%2"),
("file:///tmp/a%", "/tmp/a%"),
] {
assert_eq!(
super::normalise_dropped_token(url),
Some(PathBuf::from(want)),
"{url}"
);
}
}
#[test]
fn a_dropped_file_url_with_invalid_utf8_is_dropped() {
assert_eq!(super::normalise_dropped_token("file:///tmp/%FF"), None);
assert_eq!(
super::normalise_dropped_token("file:///tmp/%C3"),
None,
"a truncated multi-byte lead is not a path either"
);
}
#[test]
fn focus_reports_are_turned_on_at_startup_and_off_on_exit() {
let takeover = String::from_utf8(takeover_mode_seq()).unwrap();
assert!(
takeover.contains("\x1b[?1004h"),
"takeover must enable focus reports: {takeover:?}"
);
let restore = String::from_utf8_lossy(TERMINAL_RESTORE_SEQ);
assert!(
restore.contains("\x1b[?1004l"),
"the restore sequence must disable focus reports"
);
assert!(
!restore.contains("\x1b[?1004h"),
"restoring must never re-enable them"
);
}