481 lines
18 KiB
Rust
481 lines
18 KiB
Rust
use rcom::app::{AppState, Command, Effect, Overlay, PaneId};
|
|
use rcom::domain::{Entry, EntryKind, Location};
|
|
use rcom::fs::FsError;
|
|
|
|
fn app_with_entries(names: &[&str]) -> AppState {
|
|
let mut app = AppState::new(Location::local("/work"));
|
|
let initial = app.reduce(Command::Refresh);
|
|
let [Effect::LoadListing(request)] = initial.as_slice() else {
|
|
panic!("initial listing")
|
|
};
|
|
let entries = names
|
|
.iter()
|
|
.map(|name| Entry::new(*name, EntryKind::File))
|
|
.collect();
|
|
let effects = app.reduce(Command::ListingFinished(
|
|
request.clone().into_result(Ok(entries)),
|
|
));
|
|
for effect in effects {
|
|
if let Effect::LoadPreview(request) = effect {
|
|
app.reduce(Command::PreviewFinished(
|
|
request.into_result(Ok(rcom::preview::preview_bytes(b"initial", false))),
|
|
));
|
|
}
|
|
}
|
|
app
|
|
}
|
|
|
|
fn names(app: &AppState) -> Vec<String> {
|
|
app.active_pane()
|
|
.entries
|
|
.iter()
|
|
.map(|entry| entry.name.to_string_lossy().into_owned())
|
|
.collect()
|
|
}
|
|
|
|
#[test]
|
|
fn filter_apply_cancel_and_match_navigation_preserve_entry_identity() {
|
|
let mut app = app_with_entries(&["alpha", "alphabet", "beta", "gamma"]);
|
|
let original_id = app.active_pane().location.entry_id("alpha");
|
|
app.reduce(Command::OpenFilter);
|
|
assert_eq!(app.overlay, Overlay::Filter);
|
|
let effects = app.reduce(Command::FilterInput('b'));
|
|
assert!(matches!(effects.as_slice(), [Effect::DebouncePreview(_)]));
|
|
assert_eq!(names(&app), ["alphabet", "beta"]);
|
|
assert_eq!(app.active_pane().selected_entry().unwrap().name, "alphabet");
|
|
assert!(app.active_pane().filter.is_empty(), "draft is not applied");
|
|
|
|
app.reduce(Command::CloseOverlay);
|
|
assert_eq!(app.overlay, Overlay::None);
|
|
assert_eq!(names(&app), ["alpha", "alphabet", "beta", "gamma"]);
|
|
assert_eq!(app.active_pane().selected_entry().unwrap().name, "alpha");
|
|
assert_eq!(app.active_pane().location.entry_id("alpha"), original_id);
|
|
|
|
app.reduce(Command::OpenFilter);
|
|
for character in "alpha".chars() {
|
|
app.reduce(Command::FilterInput(character));
|
|
}
|
|
app.reduce(Command::ApplyFilter);
|
|
assert_eq!(app.active_pane().filter, "alpha");
|
|
assert_eq!(names(&app), ["alpha", "alphabet"]);
|
|
assert_eq!(app.active_pane().selection.index(), Some(0));
|
|
app.reduce(Command::MoveMatch(1));
|
|
assert_eq!(app.active_pane().selection.index(), Some(1));
|
|
app.reduce(Command::MoveMatch(1));
|
|
assert_eq!(app.active_pane().selection.index(), Some(0));
|
|
app.reduce(Command::MoveMatch(-1));
|
|
assert_eq!(app.active_pane().selection.index(), Some(1));
|
|
app.reduce(Command::CloseOverlay);
|
|
assert_eq!(names(&app), ["alpha", "alphabet", "beta", "gamma"]);
|
|
assert_eq!(app.active_pane().selected_entry().unwrap().name, "alphabet");
|
|
}
|
|
|
|
#[test]
|
|
fn no_matches_blocks_source_actions_and_notices_do_not_mutate_listing() {
|
|
let mut app = app_with_entries(&["alpha", "beta"]);
|
|
app.reduce(Command::OpenFilter);
|
|
app.reduce(Command::FilterInput('z'));
|
|
assert!(app.active_pane().entries.is_empty());
|
|
assert_eq!(app.active_pane().selection.index(), None);
|
|
app.reduce(Command::ApplyFilter);
|
|
let before = app.active_pane().clone();
|
|
assert!(app.reduce(Command::Copy).is_empty());
|
|
assert!(app.pending_operation.is_none());
|
|
assert_eq!(app.active_pane().location, before.location);
|
|
app.reduce(Command::MoveMatch(1));
|
|
assert_eq!(
|
|
app.active_pane().filter_notice.as_deref(),
|
|
Some("No matches")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn hidden_marked_sources_stay_complete_and_clipboard_paste_survives_empty_projection() {
|
|
let mut app = app_with_entries(&["alpha", "beta"]);
|
|
app.reduce(Command::SelectAll);
|
|
app.reduce(Command::Yank);
|
|
app.reduce(Command::OpenFilter);
|
|
for character in "alpha".chars() {
|
|
app.reduce(Command::FilterInput(character));
|
|
}
|
|
app.reduce(Command::ApplyFilter);
|
|
app.reduce(Command::TogglePaneMode);
|
|
let effects = app.reduce(Command::Copy);
|
|
assert!(matches!(effects.as_slice(), [Effect::PlanOperation(_)]));
|
|
assert_eq!(app.pending_operation.as_ref().unwrap().sources.len(), 2);
|
|
assert_eq!(app.filter_stats(PaneId::Left).hidden_marked, 1);
|
|
app.reduce(Command::CloseOverlay);
|
|
|
|
app.reduce(Command::OpenFilter);
|
|
app.reduce(Command::ClearFilterInput);
|
|
app.reduce(Command::FilterInput('z'));
|
|
app.reduce(Command::ApplyFilter);
|
|
assert!(app.active_pane().entries.is_empty());
|
|
let paste = app.reduce(Command::Paste);
|
|
assert!(matches!(paste.as_slice(), [Effect::PlanOperation(_)]));
|
|
}
|
|
|
|
#[test]
|
|
fn visual_escape_precedes_filter_clear_and_no_active_match_is_inert() {
|
|
let mut app = app_with_entries(&["alpha", "beta"]);
|
|
app.reduce(Command::MoveMatch(1));
|
|
assert_eq!(
|
|
app.active_pane().filter_notice.as_deref(),
|
|
Some("No active filter")
|
|
);
|
|
app.reduce(Command::ToggleVisual);
|
|
app.reduce(Command::OpenFilter);
|
|
app.reduce(Command::FilterInput('a'));
|
|
app.reduce(Command::ApplyFilter);
|
|
app.reduce(Command::CloseOverlay);
|
|
assert!(!app.active_pane().visual_mode);
|
|
assert_eq!(app.active_pane().filter, "a");
|
|
app.reduce(Command::CloseOverlay);
|
|
assert!(app.active_pane().filter.is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn hidden_policy_and_query_have_separate_marked_counts() {
|
|
let mut app = app_with_entries(&[".secret", "alpha", "beta"]);
|
|
app.reduce(Command::OpenFilter);
|
|
app.reduce(Command::FilterInput('a'));
|
|
app.reduce(Command::ApplyFilter);
|
|
assert_eq!(names(&app), ["alpha", "beta"]);
|
|
app.reduce(Command::ClearSelection);
|
|
app.reduce(Command::ToggleSelect);
|
|
let beta = app.active_pane().location.entry_id("beta");
|
|
app.active_pane_mut().selection_set.insert(beta);
|
|
let secret = app.active_pane().location.entry_id(".secret");
|
|
app.active_pane_mut().selection_set.insert(secret);
|
|
app.reduce(Command::OpenFilter);
|
|
app.reduce(Command::ClearFilterInput);
|
|
for character in "alpha".chars() {
|
|
app.reduce(Command::FilterInput(character));
|
|
}
|
|
app.reduce(Command::ApplyFilter);
|
|
let stats = app.filter_stats(PaneId::Left);
|
|
assert_eq!((stats.visible, stats.total), (1, 2));
|
|
assert_eq!(stats.hidden_marked, 1);
|
|
app.reduce(Command::CloseOverlay);
|
|
assert!(
|
|
app.active_pane()
|
|
.selection_set
|
|
.contains(&app.active_pane().location.entry_id("beta"))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn failed_navigation_preserves_filter_and_accepted_navigation_resets_draft() {
|
|
let mut app = app_with_entries(&["alpha", "beta"]);
|
|
app.reduce(Command::OpenFilter);
|
|
for character in "alpha".chars() {
|
|
app.reduce(Command::FilterInput(character));
|
|
}
|
|
app.reduce(Command::ApplyFilter);
|
|
let target = Location::local("/other");
|
|
let failed_effects = app.reduce(Command::GoHome(target.clone()));
|
|
let [Effect::LoadListing(failed)] = failed_effects.as_slice() else {
|
|
panic!("navigation")
|
|
};
|
|
app.reduce(Command::ListingFinished(
|
|
failed.clone().into_result(Err(FsError::NotFound)),
|
|
));
|
|
assert_eq!(app.active_pane().filter, "alpha");
|
|
assert_eq!(app.active_pane().location, Location::local("/work"));
|
|
|
|
app.reduce(Command::OpenFilter);
|
|
app.reduce(Command::FilterInput('x'));
|
|
let succeeded_effects = app.reduce(Command::GoHome(target.clone()));
|
|
let [Effect::LoadListing(succeeded)] = succeeded_effects.as_slice() else {
|
|
panic!("navigation")
|
|
};
|
|
app.reduce(Command::ListingFinished(
|
|
succeeded
|
|
.clone()
|
|
.into_result(Ok(vec![Entry::new("new", EntryKind::File)])),
|
|
));
|
|
assert_eq!(app.active_pane().location, target);
|
|
assert!(app.active_pane().filter.is_empty());
|
|
assert!(app.filter_editor.is_none());
|
|
assert_eq!(app.overlay, Overlay::None);
|
|
}
|
|
|
|
#[test]
|
|
fn superseded_navigation_followed_by_same_location_refresh_preserves_filter() {
|
|
let mut app = app_with_entries(&["alpha", "beta"]);
|
|
app.reduce(Command::OpenFilter);
|
|
app.reduce(Command::FilterInput('a'));
|
|
app.reduce(Command::ApplyFilter);
|
|
let _superseded = app.reduce(Command::GoHome(Location::local("/other")));
|
|
let refresh = app.reduce(Command::Refresh);
|
|
let [Effect::LoadListing(refresh)] = refresh.as_slice() else {
|
|
panic!("same-location refresh")
|
|
};
|
|
app.reduce(Command::ListingFinished(refresh.clone().into_result(Ok(
|
|
vec![
|
|
Entry::new("alpha", EntryKind::File),
|
|
Entry::new("beta", EntryKind::File),
|
|
],
|
|
))));
|
|
assert_eq!(app.active_pane().location, Location::local("/work"));
|
|
assert_eq!(app.active_pane().filter, "a");
|
|
}
|
|
|
|
#[test]
|
|
fn f10_and_dual_transitions_do_not_leak_draft_or_merge_filters() {
|
|
let mut quit = app_with_entries(&["alpha", "beta"]);
|
|
quit.reduce(Command::OpenFilter);
|
|
quit.reduce(Command::FilterInput('b'));
|
|
quit.reduce(Command::Quit);
|
|
assert!(quit.should_quit);
|
|
assert!(quit.filter_editor.is_none());
|
|
assert!(quit.active_pane().filter.is_empty());
|
|
quit.should_quit = false;
|
|
quit.overlay = Overlay::Quit;
|
|
quit.reduce(Command::DecideQuit(rcom::app::QuitDecision::Stay));
|
|
assert!(quit.filter_editor.is_none());
|
|
assert_eq!(quit.overlay, Overlay::None);
|
|
|
|
let mut dual = app_with_entries(&["alpha", "beta"]);
|
|
dual.reduce(Command::OpenFilter);
|
|
dual.reduce(Command::FilterInput('a'));
|
|
dual.reduce(Command::ApplyFilter);
|
|
dual.reduce(Command::TogglePaneMode);
|
|
dual.reduce(Command::OpenFilter);
|
|
dual.reduce(Command::ClearFilterInput);
|
|
dual.reduce(Command::FilterInput('b'));
|
|
dual.reduce(Command::ApplyFilter);
|
|
assert_eq!(dual.pane(PaneId::Left).filter, "b");
|
|
assert_eq!(dual.pane(PaneId::Right).filter, "a");
|
|
}
|
|
|
|
#[test]
|
|
fn same_home_reconnect_resets_filter_only_after_accepted_new_epoch() {
|
|
let host = rcom::domain::HostAlias::new("edge");
|
|
let home = Location::ssh(host.clone(), "/home/user");
|
|
let mut app = AppState::new(home.clone());
|
|
let effects = app.reduce(Command::ConnectSsh(host.clone()));
|
|
let [Effect::ConnectSsh(connect)] = effects.as_slice() else {
|
|
panic!("connect")
|
|
};
|
|
let listing = app.reduce(Command::ConnectSshFinished(connect.clone().into_result(
|
|
Ok(rcom::app::SshConnection {
|
|
location: home.clone(),
|
|
session_epoch: 1,
|
|
}),
|
|
)));
|
|
let [Effect::LoadListing(listing)] = listing.as_slice() else {
|
|
panic!("home listing")
|
|
};
|
|
app.reduce(Command::ListingFinished(listing.clone().into_result(Ok(
|
|
vec![
|
|
Entry::new("alpha", EntryKind::File),
|
|
Entry::new("beta", EntryKind::File),
|
|
],
|
|
))));
|
|
app.reduce(Command::OpenFilter);
|
|
for character in "alpha".chars() {
|
|
app.reduce(Command::FilterInput(character));
|
|
}
|
|
app.reduce(Command::ApplyFilter);
|
|
app.reduce(Command::TogglePaneMode);
|
|
app.reduce(Command::OpenFilter);
|
|
app.reduce(Command::FilterInput('x'));
|
|
let reconnect = app.reduce(Command::ConnectSsh(host.clone()));
|
|
let [Effect::ConnectSsh(reconnect)] = reconnect.as_slice() else {
|
|
panic!("reconnect")
|
|
};
|
|
app.reduce(Command::SynchronizeSshEpoch {
|
|
host: host.clone(),
|
|
session_epoch: 2,
|
|
});
|
|
let listing = app.reduce(Command::ConnectSshFinished(reconnect.clone().into_result(
|
|
Ok(rcom::app::SshConnection {
|
|
location: home.clone(),
|
|
session_epoch: 2,
|
|
}),
|
|
)));
|
|
let [Effect::LoadListing(listing)] = listing.as_slice() else {
|
|
panic!("new home listing")
|
|
};
|
|
app.reduce(Command::ListingFinished(
|
|
listing
|
|
.clone()
|
|
.into_result(Ok(vec![Entry::new("alpha", EntryKind::File)])),
|
|
));
|
|
assert!(app.active_pane().filter.is_empty());
|
|
assert!(app.pane(PaneId::Right).filter.is_empty());
|
|
assert!(app.filter_editor.is_none());
|
|
assert_eq!(app.overlay, Overlay::None);
|
|
|
|
app.reduce(Command::OpenFilter);
|
|
for character in "alpha".chars() {
|
|
app.reduce(Command::FilterInput(character));
|
|
}
|
|
app.reduce(Command::ApplyFilter);
|
|
app.reduce(Command::OpenFilter);
|
|
app.reduce(Command::FilterInput('x'));
|
|
let failed = app.reduce(Command::ConnectSsh(host.clone()));
|
|
let [Effect::ConnectSsh(failed)] = failed.as_slice() else {
|
|
panic!("failed reconnect")
|
|
};
|
|
app.reduce(Command::ConnectSshFinished(
|
|
failed.clone().into_result(Err(FsError::Disconnected)),
|
|
));
|
|
let refresh = app.reduce(Command::Refresh);
|
|
let [Effect::LoadListing(refresh)] = refresh.as_slice() else {
|
|
panic!("same-location refresh")
|
|
};
|
|
app.reduce(Command::ListingFinished(
|
|
refresh
|
|
.clone()
|
|
.into_result(Ok(vec![Entry::new("alpha", EntryKind::File)])),
|
|
));
|
|
assert_eq!(app.active_pane().filter, "alpha");
|
|
assert_eq!(app.filter_editor.as_ref().unwrap().draft, "alphax");
|
|
app.reduce(Command::CloseOverlay);
|
|
let cached = app.reduce(Command::ConnectSsh(host.clone()));
|
|
let [Effect::ConnectSsh(cached)] = cached.as_slice() else {
|
|
panic!("cached reconnect")
|
|
};
|
|
let listing = app.reduce(Command::ConnectSshFinished(cached.clone().into_result(Ok(
|
|
rcom::app::SshConnection {
|
|
location: home,
|
|
session_epoch: 2,
|
|
},
|
|
))));
|
|
let [Effect::LoadListing(listing)] = listing.as_slice() else {
|
|
panic!("cached home listing")
|
|
};
|
|
app.reduce(Command::ListingFinished(
|
|
listing
|
|
.clone()
|
|
.into_result(Ok(vec![Entry::new("alpha", EntryKind::File)])),
|
|
));
|
|
assert_eq!(app.active_pane().filter, "alpha");
|
|
}
|
|
|
|
#[test]
|
|
fn background_refresh_preserves_draft_or_immediately_reloads_changed_selection() {
|
|
let mut same = app_with_entries(&["alpha", "beta"]);
|
|
same.reduce(Command::OpenFilter);
|
|
same.reduce(Command::FilterInput('b'));
|
|
let effects = same.reduce(Command::RefreshPane(PaneId::Left));
|
|
let [Effect::LoadListing(listing)] = effects.as_slice() else {
|
|
panic!("background listing")
|
|
};
|
|
let result = same.reduce(Command::ListingFinished(listing.clone().into_result(Ok(
|
|
vec![
|
|
Entry::new("alpha", EntryKind::File),
|
|
Entry::new("beta", EntryKind::File),
|
|
],
|
|
))));
|
|
assert!(result.is_empty());
|
|
assert!(same.active_pane().preview.debouncing);
|
|
assert_eq!(same.filter_editor.as_ref().unwrap().draft, "b");
|
|
|
|
let mut changed = app_with_entries(&["alpha", "alphabet"]);
|
|
changed.reduce(Command::OpenFilter);
|
|
for character in "alpha".chars() {
|
|
changed.reduce(Command::FilterInput(character));
|
|
}
|
|
changed.reduce(Command::ApplyFilter);
|
|
let effects = changed.reduce(Command::RefreshPane(PaneId::Left));
|
|
let [Effect::LoadListing(listing)] = effects.as_slice() else {
|
|
panic!("background listing")
|
|
};
|
|
let result = changed.reduce(Command::ListingFinished(
|
|
listing
|
|
.clone()
|
|
.into_result(Ok(vec![Entry::new("alphabet", EntryKind::File)])),
|
|
));
|
|
assert!(matches!(result.as_slice(), [Effect::LoadPreview(_)]));
|
|
assert!(!changed.active_pane().preview.debouncing);
|
|
assert_eq!(
|
|
changed.active_pane().selected_entry().unwrap().name,
|
|
"alphabet"
|
|
);
|
|
|
|
let mut removed = app_with_entries(&["alpha"]);
|
|
removed.reduce(Command::OpenFilter);
|
|
removed.reduce(Command::FilterInput('a'));
|
|
removed.reduce(Command::ApplyFilter);
|
|
let effects = removed.reduce(Command::RefreshPane(PaneId::Left));
|
|
let [Effect::LoadListing(listing)] = effects.as_slice() else {
|
|
panic!("background listing")
|
|
};
|
|
let result = removed.reduce(Command::ListingFinished(
|
|
listing
|
|
.clone()
|
|
.into_result(Ok(vec![Entry::new("0", EntryKind::File)])),
|
|
));
|
|
assert!(result.is_empty());
|
|
assert!(removed.active_pane().entries.is_empty());
|
|
assert_eq!(removed.active_pane().preview.target, None);
|
|
}
|
|
|
|
#[test]
|
|
fn background_removed_match_uses_previous_projected_index_for_nearest_fallback() {
|
|
let mut app = app_with_entries(&["0", "a1", "a2", "a3"]);
|
|
app.reduce(Command::OpenFilter);
|
|
app.reduce(Command::FilterInput('a'));
|
|
app.reduce(Command::ApplyFilter);
|
|
app.reduce(Command::MoveMatch(1));
|
|
assert_eq!(app.active_pane().selected_entry().unwrap().name, "a2");
|
|
let effects = app.reduce(Command::RefreshPane(PaneId::Left));
|
|
let [Effect::LoadListing(listing)] = effects.as_slice() else {
|
|
panic!("background listing")
|
|
};
|
|
let result = app.reduce(Command::ListingFinished(listing.clone().into_result(Ok(
|
|
vec![
|
|
Entry::new("0", EntryKind::File),
|
|
Entry::new("a1", EntryKind::File),
|
|
Entry::new("a3", EntryKind::File),
|
|
],
|
|
))));
|
|
assert_eq!(app.active_pane().selected_entry().unwrap().name, "a3");
|
|
assert_eq!(app.active_pane().selection.index(), Some(1));
|
|
assert!(
|
|
matches!(result.as_slice(), [Effect::LoadPreview(request)] if request.target == Location::local("/work/a3"))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn foreground_removed_match_uses_previous_projected_index_for_nearest_fallback() {
|
|
let mut app = app_with_entries(&["0", "a1", "a2", "a3"]);
|
|
app.reduce(Command::OpenFilter);
|
|
app.reduce(Command::FilterInput('a'));
|
|
app.reduce(Command::ApplyFilter);
|
|
app.reduce(Command::MoveMatch(1));
|
|
let effects = app.reduce(Command::Refresh);
|
|
let [Effect::LoadListing(listing)] = effects.as_slice() else {
|
|
panic!("foreground refresh")
|
|
};
|
|
let result = app.reduce(Command::ListingFinished(listing.clone().into_result(Ok(
|
|
vec![
|
|
Entry::new("0", EntryKind::File),
|
|
Entry::new("a1", EntryKind::File),
|
|
Entry::new("a3", EntryKind::File),
|
|
],
|
|
))));
|
|
assert_eq!(app.active_pane().selected_entry().unwrap().name, "a3");
|
|
assert!(
|
|
matches!(result.as_slice(), [Effect::LoadPreview(request)] if request.target == Location::local("/work/a3"))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn hidden_toggle_uses_canonical_snapshot_without_a_listing_effect() {
|
|
let mut app = app_with_entries(&["visible"]);
|
|
let hidden = Entry::new(".secret", EntryKind::File);
|
|
let initial = app.reduce(Command::Refresh);
|
|
let [Effect::LoadListing(request)] = initial.as_slice() else {
|
|
panic!("refresh")
|
|
};
|
|
app.reduce(Command::ListingFinished(request.clone().into_result(Ok(
|
|
vec![hidden, Entry::new("visible", EntryKind::File)],
|
|
))));
|
|
assert_eq!(names(&app), ["visible"]);
|
|
assert!(app.reduce(Command::ToggleHidden).is_empty());
|
|
assert_eq!(names(&app), [".secret", "visible"]);
|
|
}
|