411 lines
14 KiB
Rust
411 lines
14 KiB
Rust
use std::collections::BTreeSet;
|
|
|
|
use pretty_assertions::assert_eq;
|
|
use rcom::app::{
|
|
AppState, Command, Effect, ListingRequest, OperationKind, PaneId, PaneMode, PendingOperation,
|
|
};
|
|
use rcom::domain::{Entry, EntryId, EntryKind, Location};
|
|
|
|
fn file(name: &str) -> Entry {
|
|
Entry::new(name, EntryKind::File)
|
|
}
|
|
|
|
fn app_with_files(names: &[&str]) -> AppState {
|
|
let mut app = AppState::new(Location::local("/work"));
|
|
app.active_pane_mut().entries = names.iter().map(|name| file(name)).collect();
|
|
let len = app.active_pane().entries.len();
|
|
app.active_pane_mut()
|
|
.selection
|
|
.set((len > 0).then_some(0), len);
|
|
app
|
|
}
|
|
|
|
fn selected_ids(app: &AppState, pane: PaneId) -> BTreeSet<EntryId> {
|
|
app.pane(pane).selection_set.iter().cloned().collect()
|
|
}
|
|
|
|
fn ids(location: &Location, names: &[&str]) -> BTreeSet<EntryId> {
|
|
names.iter().map(|name| location.entry_id(name)).collect()
|
|
}
|
|
|
|
fn one_listing(effects: Vec<Effect>) -> ListingRequest {
|
|
let effects: [Effect; 1] = effects.try_into().expect("one listing effect");
|
|
let [Effect::LoadListing(request)] = effects else {
|
|
panic!("expected exactly one listing effect");
|
|
};
|
|
request
|
|
}
|
|
|
|
fn finish_listing(app: &mut AppState, request: ListingRequest, entries: Vec<Entry>) {
|
|
let effects = app.reduce(Command::ListingFinished(request.into_result(Ok(entries))));
|
|
assert!(
|
|
effects
|
|
.iter()
|
|
.all(|effect| matches!(effect, Effect::LoadPreview(_))),
|
|
"listing completion may request only a read-only preview"
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn toggle_select_marks_stable_ids_advances_and_stays_at_the_last_row() {
|
|
let mut app = app_with_files(&["a", "b", "c"]);
|
|
let location = app.active_pane().location.clone();
|
|
|
|
app.reduce(Command::ToggleSelect);
|
|
assert_eq!(selected_ids(&app, PaneId::Left), ids(&location, &["a"]));
|
|
assert_eq!(app.active_pane().selection.index(), Some(1));
|
|
|
|
app.reduce(Command::ToggleSelect);
|
|
assert_eq!(
|
|
selected_ids(&app, PaneId::Left),
|
|
ids(&location, &["a", "b"])
|
|
);
|
|
assert_eq!(app.active_pane().selection.index(), Some(2));
|
|
|
|
app.reduce(Command::ToggleSelect);
|
|
assert_eq!(
|
|
selected_ids(&app, PaneId::Left),
|
|
ids(&location, &["a", "b", "c"])
|
|
);
|
|
assert_eq!(app.active_pane().selection.index(), Some(2));
|
|
|
|
// Toggling at the lower boundary still toggles, but cannot move beyond the list.
|
|
app.reduce(Command::ToggleSelect);
|
|
assert_eq!(
|
|
selected_ids(&app, PaneId::Left),
|
|
ids(&location, &["a", "b"])
|
|
);
|
|
assert_eq!(app.active_pane().selection.index(), Some(2));
|
|
|
|
// Repeating a toggle removes only that id, never another row's selection.
|
|
app.reduce(Command::MoveSelection(-1));
|
|
app.reduce(Command::ToggleSelect);
|
|
assert_eq!(selected_ids(&app, PaneId::Left), ids(&location, &["a"]));
|
|
assert_eq!(app.active_pane().selection.index(), Some(2));
|
|
}
|
|
|
|
#[test]
|
|
fn toggle_select_on_an_empty_list_is_a_noop() {
|
|
let mut app = app_with_files(&[]);
|
|
let before = app.clone();
|
|
|
|
assert!(app.reduce(Command::ToggleSelect).is_empty());
|
|
|
|
assert_eq!(app, before);
|
|
}
|
|
|
|
#[test]
|
|
fn visual_mode_selects_the_cursor_path_and_escape_exits_without_clearing() {
|
|
let mut app = app_with_files(&["a", "b", "c", "d"]);
|
|
let location = app.active_pane().location.clone();
|
|
app.reduce(Command::MoveSelection(1));
|
|
|
|
assert!(app.reduce(Command::ToggleVisual).is_empty());
|
|
assert!(app.active_pane().visual_mode);
|
|
assert_eq!(selected_ids(&app, PaneId::Left), ids(&location, &["b"]));
|
|
|
|
app.reduce(Command::MoveSelection(1));
|
|
app.reduce(Command::MoveSelection(1));
|
|
assert_eq!(
|
|
selected_ids(&app, PaneId::Left),
|
|
ids(&location, &["b", "c", "d"])
|
|
);
|
|
|
|
// Moving back over the path does not erase selections already made.
|
|
app.reduce(Command::MoveSelection(-2));
|
|
assert_eq!(
|
|
selected_ids(&app, PaneId::Left),
|
|
ids(&location, &["b", "c", "d"])
|
|
);
|
|
assert!(app.reduce(Command::CloseOverlay).is_empty());
|
|
assert!(!app.active_pane().visual_mode);
|
|
assert_eq!(
|
|
selected_ids(&app, PaneId::Left),
|
|
ids(&location, &["b", "c", "d"])
|
|
);
|
|
|
|
app.reduce(Command::MoveSelection(-1));
|
|
assert_eq!(
|
|
selected_ids(&app, PaneId::Left),
|
|
ids(&location, &["b", "c", "d"])
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn escape_closes_an_overlay_before_exiting_visual_mode() {
|
|
let mut app = app_with_files(&["a", "b"]);
|
|
app.reduce(Command::ToggleVisual);
|
|
let selected = app.active_pane().selection_set.clone();
|
|
|
|
assert!(app.reduce(Command::ToggleHelp).is_empty());
|
|
assert!(app.reduce(Command::CloseOverlay).is_empty());
|
|
assert_eq!(app.overlay, rcom::app::Overlay::None);
|
|
assert!(app.active_pane().visual_mode);
|
|
assert_eq!(app.active_pane().selection_set, selected);
|
|
|
|
assert!(app.reduce(Command::CloseOverlay).is_empty());
|
|
assert!(!app.active_pane().visual_mode);
|
|
assert_eq!(app.active_pane().selection_set, selected);
|
|
}
|
|
|
|
#[test]
|
|
fn select_all_invert_and_clear_use_the_visible_set() {
|
|
let mut app = app_with_files(&["a", "b", "c"]);
|
|
let location = app.active_pane().location.clone();
|
|
app.reduce(Command::ToggleSelect); // Select a and hover b.
|
|
|
|
assert!(app.reduce(Command::SelectAll).is_empty());
|
|
assert_eq!(
|
|
selected_ids(&app, PaneId::Left),
|
|
ids(&location, &["a", "b", "c"])
|
|
);
|
|
|
|
assert!(app.reduce(Command::InvertSelection).is_empty());
|
|
assert!(app.active_pane().selection_set.is_empty());
|
|
|
|
app.reduce(Command::InvertSelection);
|
|
app.active_pane_mut()
|
|
.selection_set
|
|
.remove(&location.entry_id("b"));
|
|
app.reduce(Command::InvertSelection);
|
|
assert_eq!(selected_ids(&app, PaneId::Left), ids(&location, &["b"]));
|
|
|
|
assert!(app.reduce(Command::ClearSelection).is_empty());
|
|
assert!(app.active_pane().selection_set.is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn refresh_retains_existing_ids_across_sorting_hidden_filtering_and_removal() {
|
|
let mut app = AppState::new(Location::local("/work"));
|
|
app.active_pane_mut().show_hidden = true;
|
|
let initial = one_listing(app.reduce(Command::Refresh));
|
|
finish_listing(
|
|
&mut app,
|
|
initial,
|
|
vec![file("z"), file(".hidden"), file("a")],
|
|
);
|
|
app.reduce(Command::SelectAll);
|
|
let location = app.active_pane().location.clone();
|
|
|
|
let reordered = one_listing(app.reduce(Command::Refresh));
|
|
finish_listing(
|
|
&mut app,
|
|
reordered,
|
|
vec![file("new"), file("a"), file("z"), file(".hidden")],
|
|
);
|
|
assert_eq!(
|
|
selected_ids(&app, PaneId::Left),
|
|
ids(&location, &["a", ".hidden", "z"])
|
|
);
|
|
assert!(
|
|
!app.active_pane()
|
|
.selection_set
|
|
.contains(&location.entry_id("new"))
|
|
);
|
|
|
|
let hidden_effects = app.reduce(Command::ToggleHidden);
|
|
assert!(
|
|
hidden_effects
|
|
.iter()
|
|
.all(|effect| matches!(effect, Effect::DebouncePreview(_)))
|
|
);
|
|
assert_eq!(
|
|
app.active_pane()
|
|
.entries
|
|
.iter()
|
|
.map(|entry| entry.name.to_string_lossy())
|
|
.collect::<Vec<_>>(),
|
|
["a", "new", "z"]
|
|
);
|
|
assert!(
|
|
app.active_pane()
|
|
.selection_set
|
|
.contains(&location.entry_id(".hidden")),
|
|
"the complete refresh result retains a hidden but existing id"
|
|
);
|
|
|
|
let removed = one_listing(app.reduce(Command::Refresh));
|
|
finish_listing(
|
|
&mut app,
|
|
removed,
|
|
vec![file("new"), file(".hidden"), file("a")],
|
|
);
|
|
assert_eq!(
|
|
selected_ids(&app, PaneId::Left),
|
|
ids(&location, &["a", ".hidden"])
|
|
);
|
|
assert!(
|
|
!app.active_pane()
|
|
.selection_set
|
|
.contains(&location.entry_id("z"))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn empty_selection_uses_only_the_hovered_entry_for_clipboard_and_f5_f6() {
|
|
for (command, expected_kind, clipboard) in [
|
|
(Command::Yank, OperationKind::Copy, true),
|
|
(Command::Cut, OperationKind::Move, true),
|
|
(Command::Copy, OperationKind::Copy, false),
|
|
(Command::Move, OperationKind::Move, false),
|
|
] {
|
|
let mut app = app_with_files(&["a", "b", "c"]);
|
|
app.reduce(Command::MoveSelection(1));
|
|
let hovered = app.active_pane().location.entry_id("b");
|
|
|
|
let effects = app.reduce(command);
|
|
|
|
assert!(effects.is_empty(), "planning must not emit effects");
|
|
assert!(app.clipboard.is_some() == clipboard);
|
|
if clipboard {
|
|
let clip = app.clipboard.as_ref().unwrap();
|
|
assert_eq!(clip.kind, expected_kind);
|
|
assert_eq!(clip.source_pane, PaneId::Left);
|
|
assert_eq!(clip.sources, vec![hovered.clone()]);
|
|
assert_eq!(app.pending_operation, None);
|
|
} else {
|
|
assert_eq!(
|
|
app.pending_operation,
|
|
Some(PendingOperation {
|
|
kind: expected_kind,
|
|
source_pane: PaneId::Left,
|
|
sources: vec![hovered],
|
|
})
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn nonempty_selection_supplies_every_selected_id_not_the_hovered_row() {
|
|
for (command, expected_kind) in [
|
|
(Command::Yank, OperationKind::Copy),
|
|
(Command::Cut, OperationKind::Move),
|
|
(Command::Copy, OperationKind::Copy),
|
|
(Command::Move, OperationKind::Move),
|
|
] {
|
|
let mut app = app_with_files(&["a", "b", "c"]);
|
|
let location = app.active_pane().location.clone();
|
|
app.active_pane_mut()
|
|
.selection_set
|
|
.insert(location.entry_id("a"));
|
|
app.active_pane_mut()
|
|
.selection_set
|
|
.insert(location.entry_id("c"));
|
|
app.reduce(Command::MoveSelection(1)); // Hover b, which is deliberately not selected.
|
|
let expected = vec![location.entry_id("a"), location.entry_id("c")];
|
|
|
|
assert!(app.reduce(command.clone()).is_empty());
|
|
let actual = match command {
|
|
Command::Yank | Command::Cut => &app.clipboard.as_ref().unwrap().sources,
|
|
Command::Copy | Command::Move => &app.pending_operation.as_ref().unwrap().sources,
|
|
_ => unreachable!(),
|
|
};
|
|
assert_eq!(actual, &expected);
|
|
match command {
|
|
Command::Yank | Command::Cut => {
|
|
assert_eq!(app.clipboard.as_ref().unwrap().kind, expected_kind)
|
|
}
|
|
Command::Copy | Command::Move => {
|
|
assert_eq!(app.pending_operation.as_ref().unwrap().kind, expected_kind)
|
|
}
|
|
_ => unreachable!(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn paste_uses_the_shared_clipboard_kind_and_original_sources_across_panes() {
|
|
let mut app = app_with_files(&["left-a", "left-b"]);
|
|
app.mode = PaneMode::Dual;
|
|
app.panes[1].location = Location::local("/destination");
|
|
app.panes[1].entries = vec![file("right")];
|
|
app.panes[1].selection.set(Some(0), 1);
|
|
let right_before = app.pane(PaneId::Right).clone();
|
|
let left_source = app.active_pane().location.entry_id("left-a");
|
|
app.reduce(Command::ToggleSelect);
|
|
|
|
assert!(app.reduce(Command::Yank).is_empty());
|
|
app.reduce(Command::SwitchPane);
|
|
let effects = app.reduce(Command::Paste);
|
|
assert_eq!(effects.len(), 1);
|
|
assert!(matches!(effects[0], Effect::PlanOperation(_)));
|
|
assert_eq!(
|
|
app.pending_operation,
|
|
Some(PendingOperation {
|
|
kind: OperationKind::Copy,
|
|
source_pane: PaneId::Left,
|
|
sources: vec![left_source.clone()],
|
|
})
|
|
);
|
|
assert_eq!(app.pane(PaneId::Right), &right_before);
|
|
|
|
app.reduce(Command::SwitchPane);
|
|
assert!(app.reduce(Command::Cut).is_empty());
|
|
app.reduce(Command::SwitchPane);
|
|
let effects = app.reduce(Command::Paste);
|
|
assert_eq!(effects.len(), 1);
|
|
assert!(matches!(effects[0], Effect::PlanOperation(_)));
|
|
assert_eq!(
|
|
app.pending_operation.as_ref().unwrap().kind,
|
|
OperationKind::Move
|
|
);
|
|
assert_eq!(
|
|
app.pending_operation.as_ref().unwrap().sources,
|
|
[left_source]
|
|
);
|
|
assert_eq!(app.pane(PaneId::Right), &right_before);
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_clipboard_operations_report_only_in_the_active_pane_without_writes() {
|
|
let mut app = AppState::new(Location::local("/empty"));
|
|
app.mode = PaneMode::Dual;
|
|
app.panes[1].location = Location::local("/other");
|
|
let right_before = app.pane(PaneId::Right).clone();
|
|
|
|
for command in [
|
|
Command::Yank,
|
|
Command::Cut,
|
|
Command::Copy,
|
|
Command::Move,
|
|
Command::Paste,
|
|
] {
|
|
assert!(app.reduce(command).is_empty());
|
|
assert!(app.active_pane().error.is_some());
|
|
assert_eq!(app.pending_operation, None);
|
|
assert_eq!(app.pane(PaneId::Right), &right_before);
|
|
}
|
|
assert_eq!(app.clipboard, None);
|
|
}
|
|
|
|
#[test]
|
|
fn copy_move_and_paste_create_only_pending_intents_before_confirmation() {
|
|
let mut app = app_with_files(&["a", "b"]);
|
|
let before_entries = app.active_pane().entries.clone();
|
|
app.reduce(Command::ToggleSelect);
|
|
let before_selection = app.active_pane().selection_set.clone();
|
|
|
|
assert!(app.reduce(Command::Yank).is_empty());
|
|
assert_eq!(app.pending_operation, None);
|
|
assert!(app.reduce(Command::Copy).is_empty());
|
|
assert_eq!(
|
|
app.pending_operation.as_ref().unwrap().kind,
|
|
OperationKind::Copy
|
|
);
|
|
assert!(app.reduce(Command::Move).is_empty());
|
|
assert_eq!(
|
|
app.pending_operation.as_ref().unwrap().kind,
|
|
OperationKind::Move
|
|
);
|
|
assert!(app.reduce(Command::Cut).is_empty());
|
|
assert!(app.reduce(Command::Paste).is_empty());
|
|
assert_eq!(
|
|
app.pending_operation.as_ref().unwrap().kind,
|
|
OperationKind::Move
|
|
);
|
|
|
|
assert_eq!(app.active_pane().entries, before_entries);
|
|
assert_eq!(app.active_pane().selection_set, before_selection);
|
|
assert!(app.active_pane().error.is_none());
|
|
}
|