479 lines
18 KiB
Rust
479 lines
18 KiB
Rust
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
|
|
use async_trait::async_trait;
|
|
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
|
use proptest::prelude::*;
|
|
use rcom::app::{AppState, Command, Effect, OperationKind, Overlay, PaneId, PaneMode};
|
|
use rcom::domain::{Entry, EntryKind, HostAlias, Location};
|
|
use rcom::fs::{BackendRegistry, FsError, LocalFs, ReadFileSystem};
|
|
use rcom::session::Application;
|
|
use rcom::ssh::{ConnectionManager, DiscoveryResult, SshConnector};
|
|
use rcom::test_support::MemoryTransferFs;
|
|
use tempfile::TempDir;
|
|
use tokio::sync::mpsc;
|
|
|
|
struct NoNetwork;
|
|
struct RemoteSessions;
|
|
|
|
fn remote_session(host: &HostAlias, epoch: u64) -> Arc<MemoryTransferFs> {
|
|
let root = Location::ssh(host.clone(), "/work");
|
|
Arc::new(
|
|
MemoryTransferFs::new(rcom::domain::SourceId::Ssh(host.clone()), epoch)
|
|
.with_dir(root.clone())
|
|
.with_file(root.join("file.txt"), b"remote"),
|
|
)
|
|
}
|
|
|
|
#[async_trait]
|
|
impl SshConnector for NoNetwork {
|
|
async fn connect(&self, _host: &HostAlias) -> Result<Arc<dyn ReadFileSystem>, FsError> {
|
|
panic!("clipboard regression is local")
|
|
}
|
|
}
|
|
|
|
#[async_trait]
|
|
impl SshConnector for RemoteSessions {
|
|
async fn connect(&self, host: &HostAlias) -> Result<Arc<dyn ReadFileSystem>, FsError> {
|
|
Ok(remote_session(host, 1))
|
|
}
|
|
|
|
async fn connect_with_operations(
|
|
&self,
|
|
host: &HostAlias,
|
|
epoch: u64,
|
|
) -> Result<
|
|
(
|
|
Arc<dyn ReadFileSystem>,
|
|
Option<Arc<dyn rcom::fs::FileOperations>>,
|
|
),
|
|
FsError,
|
|
> {
|
|
let session = remote_session(host, epoch);
|
|
Ok((session.clone(), Some(session)))
|
|
}
|
|
}
|
|
|
|
fn key(code: KeyCode) -> KeyEvent {
|
|
KeyEvent::new(code, KeyModifiers::NONE)
|
|
}
|
|
|
|
async fn settle(application: &mut Application, results: &mut mpsc::Receiver<Command>) {
|
|
loop {
|
|
match tokio::time::timeout(Duration::from_millis(50), results.recv()).await {
|
|
Ok(Some(command)) => application.dispatch(command),
|
|
Ok(None) => panic!("result channel closed"),
|
|
Err(_) => break,
|
|
}
|
|
}
|
|
}
|
|
|
|
fn fixture() -> (TempDir, Application, mpsc::Receiver<Command>) {
|
|
let fixture = TempDir::new().unwrap();
|
|
std::fs::write(fixture.path().join("file.txt"), b"contents").unwrap();
|
|
std::fs::create_dir(fixture.path().join("destination")).unwrap();
|
|
let connections = ConnectionManager::new(
|
|
Arc::new(NoNetwork),
|
|
BackendRegistry::new(Arc::new(LocalFs::new())),
|
|
);
|
|
let (application, results) = Application::new(
|
|
Location::local(fixture.path()),
|
|
connections,
|
|
DiscoveryResult::default(),
|
|
);
|
|
(fixture, application, results)
|
|
}
|
|
|
|
fn assert_fresh_plan(application: &mut Application) {
|
|
assert_eq!(application.state().overlay, Overlay::ConfirmOperation);
|
|
assert!(
|
|
application.state().planned_operation.is_some(),
|
|
"expected an executable plan; session={:?}, pending={:?}",
|
|
application.state().operation_session,
|
|
application.state().pending_operation
|
|
);
|
|
assert_eq!(application.state().active, PaneId::Left);
|
|
}
|
|
|
|
proptest! {
|
|
#[test]
|
|
fn detached_yank_and_cut_ids_survive_sorted_refresh_navigation_and_tab(
|
|
mask in 1u8..16,
|
|
cut in any::<bool>(),
|
|
reverse in any::<bool>(),
|
|
switch in any::<bool>(),
|
|
) {
|
|
let source = Location::local("/source");
|
|
let destination = Location::local("/destination");
|
|
let names = ["a.txt", "b.txt", "c.txt", "d.txt"];
|
|
let entries: Vec<_> = names
|
|
.iter()
|
|
.map(|name| Entry::new(*name, EntryKind::File))
|
|
.collect();
|
|
let expected: Vec<_> = names
|
|
.iter()
|
|
.enumerate()
|
|
.filter(|(index, _)| mask & (1 << index) != 0)
|
|
.map(|(_, name)| source.entry_id(name))
|
|
.collect();
|
|
let mut app = AppState::new(source.clone());
|
|
app.mode = PaneMode::Dual;
|
|
app.panes[1].location = destination.clone();
|
|
app.active_pane_mut().entries = entries.clone();
|
|
for (index, entry) in entries.iter().enumerate() {
|
|
if mask & (1 << index) != 0 {
|
|
app.active_pane_mut().selection_set.insert(source.entry_id(&entry.name));
|
|
app.active_pane_mut().selection.set(Some(index), entries.len());
|
|
}
|
|
}
|
|
app.reduce(if cut { Command::Cut } else { Command::Yank });
|
|
let clipboard = app.clipboard.clone().unwrap();
|
|
prop_assert_eq!(&clipboard.sources, &expected);
|
|
|
|
let effects: [Effect; 1] = app
|
|
.reduce(Command::RefreshPane(PaneId::Left))
|
|
.try_into()
|
|
.unwrap();
|
|
let [Effect::LoadListing(request)] = effects else {
|
|
panic!("expected source refresh");
|
|
};
|
|
let mut reordered = entries;
|
|
if reverse {
|
|
reordered.reverse();
|
|
} else {
|
|
reordered.rotate_left(1);
|
|
}
|
|
app.reduce(Command::ListingFinished(request.into_result(Ok(reordered))));
|
|
prop_assert_eq!(app.clipboard.as_ref(), Some(&clipboard));
|
|
|
|
let effects: [Effect; 1] = app
|
|
.reduce(Command::GoHome(Location::local("/elsewhere")))
|
|
.try_into()
|
|
.unwrap();
|
|
let [Effect::LoadListing(request)] = effects else {
|
|
panic!("expected source navigation");
|
|
};
|
|
app.reduce(Command::ListingFinished(request.into_result(Ok(Vec::new()))));
|
|
if switch {
|
|
app.reduce(Command::SwitchPane);
|
|
}
|
|
prop_assert_eq!(app.clipboard.as_ref(), Some(&clipboard));
|
|
|
|
let effects: [Effect; 1] = app.reduce(Command::Paste).try_into().unwrap();
|
|
let [Effect::PlanOperation(request)] = effects else {
|
|
panic!("expected a fresh detached plan");
|
|
};
|
|
prop_assert_eq!(&request.sources, &expected);
|
|
prop_assert_eq!(&request.source, &clipboard.source);
|
|
prop_assert_eq!(request.destination, destination);
|
|
prop_assert_eq!(request.kind, if cut { OperationKind::Move } else { OperationKind::Copy });
|
|
prop_assert!(app.task_manager.is_empty());
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn yank_and_paste_keep_fresh_dual_plan_without_switching_panes() {
|
|
let (_fixture, mut application, mut results) = fixture();
|
|
application.start();
|
|
settle(&mut application, &mut results).await;
|
|
application.handle_key(key(KeyCode::Char('\\')), 10);
|
|
application.state_mut_for_test().panes[1].location =
|
|
application.state().panes[0].location.join("destination");
|
|
settle(&mut application, &mut results).await;
|
|
application
|
|
.state_mut_for_test()
|
|
.active_pane_mut()
|
|
.selection
|
|
.set(Some(1), 2);
|
|
application.handle_key(key(KeyCode::Char('y')), 10);
|
|
application.handle_key(key(KeyCode::Char('p')), 10);
|
|
settle(&mut application, &mut results).await;
|
|
assert_fresh_plan(&mut application);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn polling_during_planning_and_confirmation_does_not_stale_a_dual_plan() {
|
|
let (_fixture, mut application, mut results) = fixture();
|
|
let root = application.state().panes[0].location.clone();
|
|
application.start();
|
|
settle(&mut application, &mut results).await;
|
|
application.handle_key(key(KeyCode::Char('\\')), 10);
|
|
application.state_mut_for_test().panes[1].location = root.join("destination");
|
|
settle(&mut application, &mut results).await;
|
|
application
|
|
.state_mut_for_test()
|
|
.active_pane_mut()
|
|
.selection
|
|
.set(Some(1), 2);
|
|
application.handle_key(key(KeyCode::Char('y')), 10);
|
|
application.handle_key(key(KeyCode::Char('p')), 10);
|
|
|
|
// A tick while the planner is running must not replace either captured pane generation.
|
|
application.refresh_visible();
|
|
settle(&mut application, &mut results).await;
|
|
assert_fresh_plan(&mut application);
|
|
let source_generation = application.state().panes[0].listing_generation;
|
|
let destination_generation = application.state().panes[1].listing_generation;
|
|
|
|
// A tick while confirmation is visible must be equally inert.
|
|
application.refresh_visible();
|
|
assert!(
|
|
tokio::time::timeout(Duration::from_millis(60), results.recv())
|
|
.await
|
|
.is_err(),
|
|
"polling must not emit a listing during confirmation"
|
|
);
|
|
assert_eq!(
|
|
application.state().panes[0].listing_generation,
|
|
source_generation
|
|
);
|
|
assert_eq!(
|
|
application.state().panes[1].listing_generation,
|
|
destination_generation
|
|
);
|
|
application.dispatch(Command::ConfirmOperation);
|
|
application.dispatch(Command::ConfirmOperation);
|
|
settle(&mut application, &mut results).await;
|
|
assert_eq!(
|
|
std::fs::read(root.path().join("destination/file.txt")).unwrap(),
|
|
b"contents"
|
|
);
|
|
assert_eq!(application.state().task_manager.len(), 1);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn multi_select_navigate_elsewhere_and_paste_copies_original_sources() {
|
|
let (_fixture, mut application, mut results) = fixture();
|
|
let root = application.state().panes[0].location.clone();
|
|
let root_path = root.path().to_path_buf();
|
|
std::fs::write(root_path.join("a.txt"), b"a").unwrap();
|
|
std::fs::write(root_path.join("b.txt"), b"b").unwrap();
|
|
std::fs::create_dir(root_path.join("other")).unwrap();
|
|
application.start();
|
|
settle(&mut application, &mut results).await;
|
|
application.handle_key(key(KeyCode::Char('\\')), 10);
|
|
application.state_mut_for_test().panes[1].location = root.join("destination");
|
|
settle(&mut application, &mut results).await;
|
|
|
|
let first = application
|
|
.state()
|
|
.active_pane()
|
|
.entries
|
|
.iter()
|
|
.position(|entry| entry.name == "a.txt")
|
|
.unwrap();
|
|
let entry_count = application.state().active_pane().entries.len();
|
|
application
|
|
.state_mut_for_test()
|
|
.active_pane_mut()
|
|
.selection
|
|
.set(Some(first), entry_count);
|
|
application.handle_key(key(KeyCode::Char(' ')), 10);
|
|
application.handle_key(key(KeyCode::Char(' ')), 10);
|
|
application.handle_key(key(KeyCode::Char('y')), 10);
|
|
application.dispatch(Command::GoHome(root.join("other")));
|
|
settle(&mut application, &mut results).await;
|
|
application.handle_key(key(KeyCode::Char('p')), 10);
|
|
settle(&mut application, &mut results).await;
|
|
|
|
let plan = application.state().planned_operation.as_ref().unwrap();
|
|
let sources: Vec<_> = plan
|
|
.plan
|
|
.sources()
|
|
.iter()
|
|
.map(|source| source.location().path.clone())
|
|
.collect();
|
|
assert_eq!(
|
|
sources,
|
|
vec![root_path.join("a.txt"), root_path.join("b.txt")]
|
|
);
|
|
assert_eq!(plan.plan.destination(), &root.join("destination"));
|
|
assert_eq!(
|
|
application.state().active_pane().location,
|
|
root.join("other")
|
|
);
|
|
assert_eq!(application.state().active, PaneId::Left);
|
|
|
|
application.dispatch(Command::ConfirmOperation);
|
|
application.dispatch(Command::ConfirmOperation);
|
|
settle(&mut application, &mut results).await;
|
|
assert_eq!(
|
|
std::fs::read(root_path.join("destination/a.txt")).unwrap(),
|
|
b"a"
|
|
);
|
|
assert_eq!(
|
|
std::fs::read(root_path.join("destination/b.txt")).unwrap(),
|
|
b"b"
|
|
);
|
|
assert_eq!(application.state().task_manager.len(), 1);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn yank_then_switch_to_destination_and_paste_keeps_original_source_snapshot() {
|
|
let (_fixture, mut application, mut results) = fixture();
|
|
application.start();
|
|
settle(&mut application, &mut results).await;
|
|
application.handle_key(key(KeyCode::Char('\\')), 10);
|
|
application.state_mut_for_test().panes[1].location =
|
|
application.state().panes[0].location.join("destination");
|
|
settle(&mut application, &mut results).await;
|
|
application
|
|
.state_mut_for_test()
|
|
.active_pane_mut()
|
|
.selection
|
|
.set(Some(1), 2);
|
|
application.handle_key(key(KeyCode::Char('y')), 10);
|
|
application.handle_key(key(KeyCode::Tab), 10);
|
|
application.handle_key(key(KeyCode::Char('p')), 10);
|
|
settle(&mut application, &mut results).await;
|
|
assert_eq!(application.state().active, PaneId::Right);
|
|
assert_eq!(application.state().mode, PaneMode::Dual);
|
|
assert!(
|
|
application.state().planned_operation.is_some(),
|
|
"expected fresh plan after Tab; session={:?}",
|
|
application.state().operation_session
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn destination_change_requires_fresh_plan_and_never_reuses_the_old_confirmation() {
|
|
let (_fixture, mut application, mut results) = fixture();
|
|
let root = application.state().panes[0].location.clone();
|
|
std::fs::create_dir(root.path().join("other")).unwrap();
|
|
application.start();
|
|
settle(&mut application, &mut results).await;
|
|
application.handle_key(key(KeyCode::Char('\\')), 10);
|
|
application.state_mut_for_test().panes[1].location = root.join("destination");
|
|
settle(&mut application, &mut results).await;
|
|
let selected = application
|
|
.state()
|
|
.active_pane()
|
|
.entries
|
|
.iter()
|
|
.position(|entry| entry.name == "file.txt")
|
|
.unwrap();
|
|
let len = application.state().active_pane().entries.len();
|
|
application
|
|
.state_mut_for_test()
|
|
.active_pane_mut()
|
|
.selection
|
|
.set(Some(selected), len);
|
|
application.handle_key(key(KeyCode::Char('y')), 10);
|
|
application.handle_key(key(KeyCode::Char('p')), 10);
|
|
settle(&mut application, &mut results).await;
|
|
let first = application
|
|
.state()
|
|
.planned_operation
|
|
.as_ref()
|
|
.unwrap()
|
|
.plan
|
|
.id();
|
|
|
|
application.state_mut_for_test().panes[1].location = root.join("other");
|
|
application.dispatch(Command::ConfirmOperation);
|
|
assert!(application.state().task_manager.is_empty());
|
|
application.dispatch(Command::CloseOverlay);
|
|
application.handle_key(key(KeyCode::Char('p')), 10);
|
|
settle(&mut application, &mut results).await;
|
|
let plan = application.state().planned_operation.as_ref().unwrap();
|
|
assert_ne!(plan.plan.id(), first);
|
|
assert_eq!(plan.plan.destination(), &root.join("other"));
|
|
assert!(application.state().task_manager.is_empty());
|
|
assert_eq!(
|
|
std::fs::read_dir(root.path().join("destination"))
|
|
.unwrap()
|
|
.count(),
|
|
0
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn replaced_original_ssh_epoch_is_rejected_after_navigation_to_another_endpoint() {
|
|
let fixture = TempDir::new().unwrap();
|
|
let destination = fixture.path().join("destination");
|
|
let other = fixture.path().join("other");
|
|
std::fs::create_dir(&destination).unwrap();
|
|
std::fs::create_dir(&other).unwrap();
|
|
let host = HostAlias::new("edge");
|
|
let root = Location::ssh(host.clone(), "/work");
|
|
let connections = ConnectionManager::new(
|
|
Arc::new(RemoteSessions),
|
|
BackendRegistry::new(Arc::new(LocalFs::new())),
|
|
);
|
|
let original = connections.connect(&host).await.unwrap();
|
|
let (mut application, mut results) = Application::new(
|
|
root.clone(),
|
|
connections.clone(),
|
|
DiscoveryResult::default(),
|
|
);
|
|
application
|
|
.state_mut_for_test()
|
|
.active_pane_mut()
|
|
.session_epoch = Some(original.epoch);
|
|
application.start();
|
|
settle(&mut application, &mut results).await;
|
|
application.handle_key(key(KeyCode::Char('y')), 10);
|
|
let clipboard = application.state().clipboard.clone().unwrap();
|
|
application.handle_key(key(KeyCode::Char('\\')), 10);
|
|
application.state_mut_for_test().panes[1].location = Location::local(&destination);
|
|
settle(&mut application, &mut results).await;
|
|
application.dispatch(Command::GoHome(Location::local(&other)));
|
|
settle(&mut application, &mut results).await;
|
|
|
|
connections.invalidate_if_epoch(&host, original.epoch).await;
|
|
let replacement = connections.connect(&host).await.unwrap();
|
|
assert_ne!(replacement.epoch, original.epoch);
|
|
application.handle_key(key(KeyCode::Char('p')), 10);
|
|
settle(&mut application, &mut results).await;
|
|
|
|
assert!(application.state().planned_operation.is_none());
|
|
assert!(application.state().task_manager.is_empty());
|
|
assert_eq!(application.state().clipboard.as_ref(), Some(&clipboard));
|
|
assert_eq!(std::fs::read_dir(destination).unwrap().count(), 0);
|
|
}
|
|
|
|
#[test]
|
|
fn changed_source_epoch_rejects_detached_paste_before_planning_or_mutation() {
|
|
let host = HostAlias::new("edge");
|
|
let mut app = AppState::new(Location::ssh(host.clone(), "/work"));
|
|
app.active_pane_mut().session_epoch = Some(10);
|
|
app.active_pane_mut().entries = vec![Entry::new("file", EntryKind::File)];
|
|
app.active_pane_mut().selection.set(Some(0), 1);
|
|
app.reduce(Command::Yank);
|
|
assert_eq!(
|
|
app.clipboard.as_ref().unwrap().source.session_epoch,
|
|
Some(10)
|
|
);
|
|
|
|
app.active_pane_mut().session_epoch = Some(11);
|
|
assert!(app.reduce(Command::Paste).is_empty());
|
|
assert!(app.task_manager.is_empty());
|
|
assert!(app.planned_operation.is_none());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn missing_clipboard_source_fails_read_only_planning_without_writes() {
|
|
let (_fixture, mut application, mut results) = fixture();
|
|
let root = application.state().panes[0].location.clone();
|
|
application.start();
|
|
settle(&mut application, &mut results).await;
|
|
application
|
|
.state_mut_for_test()
|
|
.active_pane_mut()
|
|
.selection
|
|
.set(Some(1), 2);
|
|
application.handle_key(key(KeyCode::Char('y')), 10);
|
|
std::fs::remove_file(root.path().join("file.txt")).unwrap();
|
|
application.handle_key(key(KeyCode::Char('p')), 10);
|
|
settle(&mut application, &mut results).await;
|
|
|
|
assert!(application.state().planned_operation.is_none());
|
|
assert!(application.state().task_manager.is_empty());
|
|
assert!(root.path().join("destination").exists());
|
|
assert_eq!(
|
|
std::fs::read_dir(root.path().join("destination"))
|
|
.unwrap()
|
|
.count(),
|
|
0
|
|
);
|
|
}
|