rcom/tests/workspace_panes.rs

359 lines
13 KiB
Rust

use pretty_assertions::assert_eq;
use rcom::app::{
AppState, Command, ConnectSshRequest, Effect, ListingRequest, PaneId, PaneMode, SshConnection,
};
use rcom::domain::{Entry, EntryKind, HostAlias, Location};
use rcom::fs::{FsError, ReadFileSystem};
use rcom::test_support::MemoryFs;
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 one_connect(effects: Vec<Effect>) -> ConnectSshRequest {
let effects: [Effect; 1] = effects.try_into().expect("one connect effect");
let [Effect::ConnectSsh(request)] = effects else {
panic!("expected exactly one connect effect");
};
request
}
fn file(name: &str) -> Entry {
Entry::new(name, EntryKind::File)
}
fn directory(name: &str) -> Entry {
Entry::new(name, EntryKind::Directory)
}
fn finish_listing(app: &mut AppState, request: ListingRequest, entries: Vec<Entry>) -> Vec<Effect> {
app.reduce(Command::ListingFinished(request.into_result(Ok(entries))))
}
fn names(app: &AppState, pane: PaneId) -> Vec<String> {
app.pane(pane)
.entries
.iter()
.map(|entry| entry.name.to_string_lossy().into_owned())
.collect()
}
async fn finish_from_memory(
fs: &MemoryFs,
app: &mut AppState,
request: ListingRequest,
) -> Vec<Effect> {
let result = fs.list(&request.target).await;
app.reduce(Command::ListingFinished(request.into_result(result)))
}
#[test]
fn default_workspace_is_single_local_home() {
let home = Location::local("/home/user");
let app = AppState::new(home.clone());
assert_eq!(app.mode, PaneMode::Single);
assert_eq!(app.active, PaneId::Left);
assert_eq!(app.active_pane().location, home);
assert_eq!(
app.active_pane().location.source,
rcom::domain::SourceId::Local
);
assert!(app.active_pane().entries.is_empty());
assert!(!app.active_pane().loading);
assert!(!app.should_quit);
}
#[test]
fn opening_second_pane_copies_location_then_separates_state() {
let mut app = AppState::new(Location::local("/workspace"));
let initial = one_listing(app.reduce(Command::Refresh));
finish_listing(
&mut app,
initial,
vec![directory("docs"), file("readme.txt")],
);
app.reduce(Command::MoveSelection(1));
let left_selection = app.pane(PaneId::Left).selection;
assert!(app.reduce(Command::TogglePaneMode).is_empty());
assert_eq!(app.mode, PaneMode::Dual);
assert_eq!(
app.pane(PaneId::Right).location,
app.pane(PaneId::Left).location
);
assert_eq!(
app.pane(PaneId::Right).entries,
app.pane(PaneId::Left).entries
);
assert_eq!(app.pane(PaneId::Right).selection, left_selection);
app.reduce(Command::SwitchPane);
app.reduce(Command::SelectFirst);
assert_eq!(app.pane(PaneId::Right).selection.index(), Some(0));
assert_eq!(app.pane(PaneId::Left).selection, left_selection);
}
#[test]
fn enabling_dual_during_initial_listing_schedules_second_pane_loading() {
let mut app = AppState::new(Location::local("/workspace"));
let left_request = one_listing(app.reduce(Command::Refresh));
let right_request = one_listing(app.reduce(Command::TogglePaneMode));
assert_eq!(app.mode, PaneMode::Dual);
assert_eq!(right_request.pane, PaneId::Right);
assert_eq!(right_request.target, left_request.target);
assert!(app.pane(PaneId::Right).loading);
assert!(app.pane(PaneId::Right).entries.is_empty());
finish_listing(
&mut app,
left_request,
vec![directory("docs"), file("readme.txt")],
);
finish_listing(
&mut app,
right_request,
vec![directory("docs"), file("readme.txt")],
);
assert!(!app.pane(PaneId::Right).loading);
assert_eq!(names(&app, PaneId::Right), ["docs", "readme.txt"]);
}
#[test]
fn closing_second_pane_preserves_active_location_and_selection() {
let mut app = AppState::new(Location::local("/left"));
let initial = one_listing(app.reduce(Command::Refresh));
finish_listing(&mut app, initial, vec![file("left-a"), file("left-b")]);
app.reduce(Command::TogglePaneMode);
app.reduce(Command::SwitchPane);
let target = Location::local("/right");
let request = one_listing(app.reduce(Command::GoHome(target.clone())));
finish_listing(&mut app, request, vec![file("right-a"), file("right-b")]);
app.reduce(Command::SelectLast);
app.reduce(Command::TogglePaneMode);
assert_eq!(app.mode, PaneMode::Single);
assert_eq!(app.active, PaneId::Right);
assert_eq!(app.active_pane().location, target);
assert_eq!(app.active_pane().selection.index(), Some(1));
}
#[test]
fn reopening_restores_hidden_pane_state() {
let mut app = AppState::new(Location::local("/left"));
app.reduce(Command::TogglePaneMode);
app.reduce(Command::SwitchPane);
let request = one_listing(app.reduce(Command::GoHome(Location::local("/right"))));
finish_listing(&mut app, request, vec![file("a"), file("b")]);
app.reduce(Command::SelectLast);
let restored = app.pane(PaneId::Right).clone();
let left = app.pane(PaneId::Left).clone();
app.reduce(Command::TogglePaneMode);
app.reduce(Command::SwitchPane); // Single mode deliberately keeps focus unchanged.
app.reduce(Command::TogglePaneMode);
assert_eq!(app.mode, PaneMode::Dual);
assert_eq!(app.active, PaneId::Right);
assert_eq!(app.pane(PaneId::Right), &restored);
assert_eq!(app.pane(PaneId::Left), &left);
}
#[test]
fn tab_switches_the_single_active_pane() {
let mut app = AppState::new(Location::local("/workspace"));
app.reduce(Command::SwitchPane);
assert_eq!(app.active, PaneId::Left);
app.reduce(Command::TogglePaneMode);
app.reduce(Command::SwitchPane);
assert_eq!(app.active, PaneId::Right);
app.reduce(Command::SwitchPane);
assert_eq!(app.active, PaneId::Left);
}
#[tokio::test]
async fn hidden_mode_is_independent_and_survives_navigation() {
let fs = MemoryFs::new()
.with_dir(Location::local("/hidden/child"))
.with_file(Location::local("/hidden/.secret"), "secret")
.with_file(Location::local("/hidden/visible"), "visible")
.with_file(Location::local("/hidden/child/.child-secret"), "secret")
.with_file(Location::local("/hidden/child/child-visible"), "visible");
let mut app = AppState::new(Location::local("/hidden"));
let initial = one_listing(app.reduce(Command::Refresh));
finish_from_memory(&fs, &mut app, initial).await;
app.reduce(Command::TogglePaneMode);
let show_hidden = app.reduce(Command::ToggleHidden);
assert!(
show_hidden
.iter()
.all(|effect| matches!(effect, Effect::DebouncePreview(_)))
);
assert!(app.pane(PaneId::Left).show_hidden);
assert!(!app.pane(PaneId::Right).show_hidden);
assert_eq!(names(&app, PaneId::Left), ["child", ".secret", "visible"]);
assert_eq!(names(&app, PaneId::Right), ["child", "visible"]);
let enter = one_listing(app.reduce(Command::Enter));
finish_from_memory(&fs, &mut app, enter).await;
assert!(app.pane(PaneId::Left).show_hidden);
assert_eq!(
names(&app, PaneId::Left),
[".child-secret", "child-visible"]
);
assert!(!app.pane(PaneId::Right).show_hidden);
assert_eq!(app.pane(PaneId::Right).location, Location::local("/hidden"));
}
#[test]
fn navigation_does_not_change_inactive_pane() {
let mut app = AppState::new(Location::local("/left"));
let initial = one_listing(app.reduce(Command::Refresh));
finish_listing(&mut app, initial, vec![file("left")]);
app.reduce(Command::TogglePaneMode);
let left = app.pane(PaneId::Left).clone();
app.reduce(Command::SwitchPane);
let request = one_listing(app.reduce(Command::GoHome(Location::local("/right"))));
finish_listing(&mut app, request, vec![file("right-a"), file("right-b")]);
app.reduce(Command::SelectLast);
assert_eq!(app.pane(PaneId::Left), &left);
assert_eq!(app.pane(PaneId::Right).location, Location::local("/right"));
assert_eq!(app.pane(PaneId::Right).selection.index(), Some(1));
}
#[test]
fn local_and_remote_panes_keep_independent_locations() {
let host = HostAlias::new("edge");
let mut app = AppState::new(Location::local("/local"));
let initial = one_listing(app.reduce(Command::Refresh));
finish_listing(&mut app, initial, vec![file("local")]);
app.reduce(Command::TogglePaneMode);
let local = app.pane(PaneId::Right).clone();
let connect = one_connect(app.reduce(Command::ConnectSsh(host.clone())));
let home = Location::ssh(host.clone(), "/home/user");
let listing = one_listing(
app.reduce(Command::ConnectSshFinished(connect.into_result(Ok(
SshConnection {
location: home.clone(),
session_epoch: 11,
},
)))),
);
finish_listing(&mut app, listing, vec![file("remote")]);
assert_eq!(app.pane(PaneId::Left).location, home);
assert_eq!(app.pane(PaneId::Left).session_epoch, Some(11));
assert_eq!(app.pane(PaneId::Right), &local);
}
#[test]
fn one_pane_error_does_not_block_the_other() {
let mut app = AppState::new(Location::local("/workspace"));
let initial = one_listing(app.reduce(Command::Refresh));
finish_listing(&mut app, initial, vec![file("original")]);
app.reduce(Command::TogglePaneMode);
let failed = one_listing(app.reduce(Command::GoHome(Location::local("/denied"))));
app.reduce(Command::SwitchPane);
let succeeded = one_listing(app.reduce(Command::GoHome(Location::local("/available"))));
assert!(
app.reduce(Command::ListingFinished(
failed.into_result(Err(FsError::PermissionDenied))
))
.is_empty()
);
assert_eq!(
app.pane(PaneId::Left).error,
Some(FsError::PermissionDenied)
);
assert_eq!(
app.pane(PaneId::Left).location,
Location::local("/workspace")
);
assert!(app.pane(PaneId::Right).loading);
assert_eq!(app.pane(PaneId::Right).error, None);
finish_listing(&mut app, succeeded, vec![file("new")]);
assert_eq!(
app.pane(PaneId::Right).location,
Location::local("/available")
);
assert_eq!(app.pane(PaneId::Right).error, None);
assert!(!app.pane(PaneId::Right).loading);
}
#[test]
fn reversed_listing_completion_keeps_latest_location() {
let mut app = AppState::new(Location::local("/start"));
let first = one_listing(app.reduce(Command::GoHome(Location::local("/first"))));
let second = one_listing(app.reduce(Command::GoHome(Location::local("/second"))));
finish_listing(&mut app, second.clone(), vec![file("newest")]);
assert_eq!(app.active_pane().location, Location::local("/second"));
assert_eq!(names(&app, PaneId::Left), ["newest"]);
let stale_effects = app.reduce(Command::ListingFinished(
first.into_result(Ok(vec![file("stale")])),
));
assert!(stale_effects.is_empty());
assert_eq!(app.active_pane().location, Location::local("/second"));
assert_eq!(names(&app, PaneId::Left), ["newest"]);
assert_eq!(app.active_pane().listing_generation, second.generation);
}
#[test]
fn confirmed_location_change_clears_only_that_panes_multi_selection() {
let mut app = AppState::new(Location::local("/left"));
let initial = one_listing(app.reduce(Command::Refresh));
finish_listing(&mut app, initial, vec![file("left-a"), file("left-b")]);
app.reduce(Command::SelectAll);
app.reduce(Command::ToggleVisual);
app.reduce(Command::TogglePaneMode);
app.reduce(Command::SwitchPane);
app.reduce(Command::ToggleSelect);
let right_selection = app.pane(PaneId::Right).selection_set.clone();
let right_location = app.pane(PaneId::Right).location.clone();
app.reduce(Command::SwitchPane);
let request = one_listing(app.reduce(Command::GoHome(Location::local("/new-left"))));
finish_listing(&mut app, request, vec![file("new")]);
assert!(app.pane(PaneId::Left).selection_set.is_empty());
assert!(!app.pane(PaneId::Left).visual_mode);
assert_eq!(app.pane(PaneId::Right).selection_set, right_selection);
assert_eq!(app.pane(PaneId::Right).location, right_location);
}
#[test]
fn listing_response_updates_origin_pane_only() {
let mut app = AppState::new(Location::local("/start"));
app.reduce(Command::TogglePaneMode);
let request = one_listing(app.reduce(Command::GoHome(Location::local("/left-result"))));
app.reduce(Command::SwitchPane);
let right = app.pane(PaneId::Right).clone();
finish_listing(&mut app, request, vec![file("left")]);
assert_eq!(app.active, PaneId::Right);
assert_eq!(app.pane(PaneId::Right), &right);
assert_eq!(
app.pane(PaneId::Left).location,
Location::local("/left-result")
);
assert_eq!(names(&app, PaneId::Left), ["left"]);
}