383 lines
14 KiB
Rust
383 lines
14 KiB
Rust
use std::collections::BTreeMap;
|
|
use std::fs;
|
|
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
|
|
use async_trait::async_trait;
|
|
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
|
use rcom::app::{
|
|
AppState, Command, DestinationFingerprint, Effect, Overlay, PaneId, PaneMode,
|
|
PlanOperationOutput,
|
|
};
|
|
use rcom::domain::{Entry, EntryKind, HostAlias, Location, SourceId};
|
|
use rcom::fs::{BackendRegistry, FileOperations, FsError, ReadFileSystem};
|
|
use rcom::operations::{ItemId, OperationPlan, SourceItem};
|
|
use rcom::session::{Application, initial_location};
|
|
use rcom::ssh::{ConnectionManager, DiscoveryResult, SshConnector};
|
|
use rcom::test_support::{MemoryFs, MemoryTransferFs};
|
|
use tempfile::TempDir;
|
|
use tokio::sync::mpsc;
|
|
|
|
struct UnusedConnector;
|
|
|
|
#[async_trait]
|
|
impl SshConnector for UnusedConnector {
|
|
async fn connect(&self, _host: &HostAlias) -> Result<Arc<dyn ReadFileSystem>, FsError> {
|
|
Err(FsError::Disconnected)
|
|
}
|
|
}
|
|
|
|
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 unexpectedly"),
|
|
Err(_) => break,
|
|
}
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn cli_initial_directory_is_opened() {
|
|
let fixture = TempDir::new().unwrap();
|
|
let nested = fixture.path().join("nested");
|
|
fs::create_dir(&nested).unwrap();
|
|
|
|
assert_eq!(
|
|
initial_location(Some(nested.clone())).await.unwrap(),
|
|
Location::local(fs::canonicalize(nested).unwrap())
|
|
);
|
|
let file = fixture.path().join("file");
|
|
fs::write(&file, "not a directory").unwrap();
|
|
assert_eq!(
|
|
initial_location(Some(file)).await.unwrap_err(),
|
|
FsError::NotADirectory
|
|
);
|
|
}
|
|
|
|
#[cfg(unix)]
|
|
#[tokio::test]
|
|
async fn cli_canonicalizes_symlinks_before_checking_the_directory_type() {
|
|
let fixture = TempDir::new().unwrap();
|
|
let directory = fixture.path().join("directory");
|
|
let file = fixture.path().join("file.txt");
|
|
let directory_link = fixture.path().join("directory-link");
|
|
let file_link = fixture.path().join("file-link");
|
|
fs::create_dir(&directory).unwrap();
|
|
fs::write(&file, "not a directory").unwrap();
|
|
std::os::unix::fs::symlink(&directory, &directory_link).unwrap();
|
|
std::os::unix::fs::symlink(&file, &file_link).unwrap();
|
|
|
|
assert_eq!(
|
|
initial_location(Some(directory_link)).await.unwrap(),
|
|
Location::local(fs::canonicalize(&directory).unwrap())
|
|
);
|
|
assert_eq!(
|
|
initial_location(Some(file_link)).await.unwrap_err(),
|
|
FsError::NotADirectory
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn local_navigation_dual_independence_and_quit_form_a_headless_e2e() {
|
|
let fixture = TempDir::new().unwrap();
|
|
fs::create_dir(fixture.path().join("docs")).unwrap();
|
|
fs::write(fixture.path().join("docs/a.txt"), "alpha").unwrap();
|
|
fs::write(fixture.path().join("docs/b.txt"), "beta").unwrap();
|
|
fs::write(fixture.path().join("README"), "read me").unwrap();
|
|
let registry = BackendRegistry::new(Arc::new(rcom::fs::LocalFs::new()));
|
|
let connections = ConnectionManager::new(Arc::new(UnusedConnector), registry);
|
|
let (mut application, mut results) = Application::new(
|
|
Location::local(fixture.path()),
|
|
connections,
|
|
DiscoveryResult::default(),
|
|
);
|
|
|
|
application.start();
|
|
settle(&mut application, &mut results).await;
|
|
assert_eq!(
|
|
application.state().active_pane().location.path(),
|
|
fixture.path()
|
|
);
|
|
assert!(!application.state().active_pane().entries.is_empty());
|
|
|
|
application.handle_key(key(KeyCode::Char('l')), 10);
|
|
settle(&mut application, &mut results).await;
|
|
assert_eq!(
|
|
application.state().active_pane().location.path(),
|
|
fixture.path().join("docs")
|
|
);
|
|
|
|
application.handle_key(key(KeyCode::Char('\\')), 10);
|
|
assert_eq!(application.state().mode, PaneMode::Dual);
|
|
assert_eq!(
|
|
application.state().pane(PaneId::Right).selection.index(),
|
|
Some(0)
|
|
);
|
|
|
|
application.handle_key(key(KeyCode::Char('j')), 10);
|
|
assert_eq!(
|
|
application.state().pane(PaneId::Left).selection.index(),
|
|
Some(1)
|
|
);
|
|
assert_eq!(
|
|
application.state().pane(PaneId::Right).selection.index(),
|
|
Some(0)
|
|
);
|
|
|
|
application.handle_key(key(KeyCode::Tab), 10);
|
|
assert_eq!(application.state().active, PaneId::Right);
|
|
application.handle_key(key(KeyCode::Char('j')), 10);
|
|
assert_eq!(
|
|
application.state().pane(PaneId::Left).selection.index(),
|
|
Some(1)
|
|
);
|
|
assert_eq!(
|
|
application.state().pane(PaneId::Right).selection.index(),
|
|
Some(1)
|
|
);
|
|
|
|
application.handle_key(key(KeyCode::F(10)), 10);
|
|
assert!(application.should_quit());
|
|
}
|
|
|
|
#[test]
|
|
fn dual_selection_plan_confirm_and_clipboard_move_form_a_write_free_headless_flow() {
|
|
let source = Location::local("/source");
|
|
let destination = Location::local("/destination");
|
|
let mut app = AppState::new(source.clone());
|
|
app.mode = PaneMode::Dual;
|
|
app.active_pane_mut().entries = vec![
|
|
Entry::new("a.txt", EntryKind::File),
|
|
Entry::new("b.txt", EntryKind::File),
|
|
];
|
|
app.active_pane_mut().selection.set(Some(0), 2);
|
|
app.panes[1].location = destination.clone();
|
|
app.reduce(Command::ToggleSelect);
|
|
app.reduce(Command::ToggleSelect);
|
|
assert_eq!(app.active_pane().selection_set.len(), 2);
|
|
|
|
let effects: [Effect; 1] = app.reduce(Command::Copy).try_into().unwrap();
|
|
let [Effect::PlanOperation(request)] = effects else {
|
|
panic!("copy should request read-only planning");
|
|
};
|
|
assert_eq!(request.destination, destination);
|
|
assert_eq!(request.source.pane, PaneId::Left);
|
|
assert_eq!(
|
|
request.destination_pane.as_ref().unwrap().pane,
|
|
PaneId::Right
|
|
);
|
|
assert_eq!(app.active, PaneId::Left);
|
|
assert!(app.task_manager.is_empty());
|
|
let sources = request
|
|
.sources
|
|
.iter()
|
|
.enumerate()
|
|
.map(|(index, id)| {
|
|
SourceItem::new(
|
|
ItemId::new(index as u64 + 1),
|
|
Location {
|
|
source: id.source.clone(),
|
|
path: id.path.clone(),
|
|
},
|
|
)
|
|
})
|
|
.collect();
|
|
let output = PlanOperationOutput {
|
|
prepared: None,
|
|
plan: OperationPlan::new(
|
|
request.plan_id,
|
|
request.generation,
|
|
request.kind,
|
|
sources,
|
|
request.destination.clone(),
|
|
request.policy,
|
|
Some(2),
|
|
None,
|
|
BTreeMap::new(),
|
|
)
|
|
.unwrap(),
|
|
destination_fingerprint: DestinationFingerprint::new("headless"),
|
|
collisions: Vec::new(),
|
|
item_diagnostics: Vec::new(),
|
|
};
|
|
app.reduce(Command::PlanFinished(request.into_result(Ok(output))));
|
|
let effects: [Effect; 1] = app.reduce(Command::ConfirmOperation).try_into().unwrap();
|
|
let [Effect::ExecuteOperation(execution)] = effects else {
|
|
panic!("confirmation should start exactly one active execution");
|
|
};
|
|
assert_eq!(execution.operation.plan.expected_items(), Some(2));
|
|
assert!(app.reduce(Command::ConfirmOperation).is_empty());
|
|
|
|
// Cut only replaces the shared clipboard; it does not delete or modify the source selection.
|
|
app.reduce(Command::Cut);
|
|
assert_eq!(app.clipboard.as_ref().unwrap().sources.len(), 2);
|
|
assert_eq!(app.active_pane().entries.len(), 2);
|
|
assert_eq!(app.task_manager.len(), 1);
|
|
}
|
|
|
|
#[derive(Clone, Default)]
|
|
struct HeadlessRemote {
|
|
transfer: Arc<std::sync::Mutex<Option<Arc<MemoryTransferFs>>>>,
|
|
}
|
|
|
|
#[async_trait]
|
|
impl SshConnector for HeadlessRemote {
|
|
async fn connect(&self, host: &HostAlias) -> Result<Arc<dyn ReadFileSystem>, FsError> {
|
|
Ok(Arc::new(
|
|
MemoryFs::new()
|
|
.with_dir(Location::ssh(host.clone(), "/work"))
|
|
.with_dir(Location::ssh(host.clone(), "/destination")),
|
|
))
|
|
}
|
|
|
|
async fn connect_with_operations(
|
|
&self,
|
|
host: &HostAlias,
|
|
epoch: u64,
|
|
) -> Result<(Arc<dyn ReadFileSystem>, Option<Arc<dyn FileOperations>>), FsError> {
|
|
let transfer = Arc::new(
|
|
MemoryTransferFs::new(SourceId::Ssh(host.clone()), epoch)
|
|
.with_dir(Location::ssh(host.clone(), "/work"))
|
|
.with_dir(Location::ssh(host.clone(), "/destination")),
|
|
);
|
|
let read: Arc<dyn ReadFileSystem> = transfer.clone();
|
|
*self.transfer.lock().unwrap() = Some(transfer.clone());
|
|
Ok((read, Some(transfer)))
|
|
}
|
|
}
|
|
|
|
fn confirm_once(application: &mut Application, command: Command) {
|
|
application.dispatch(command.clone());
|
|
let tasks = application.state().task_manager.len();
|
|
application.dispatch(command);
|
|
assert_eq!(application.state().task_manager.len(), tasks);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn create_rename_trash_undo_move_and_undo_form_one_fake_ssh_headless_flow() {
|
|
let host = HostAlias::new("edge");
|
|
let connector = HeadlessRemote::default();
|
|
let connections = ConnectionManager::new(
|
|
Arc::new(connector.clone()),
|
|
BackendRegistry::new(Arc::new(rcom::fs::LocalFs::new())),
|
|
);
|
|
let connection = connections.connect(&host).await.unwrap();
|
|
let work = Location::ssh(host.clone(), "/work");
|
|
let (mut application, mut results) =
|
|
Application::new(work.clone(), connections, DiscoveryResult::default());
|
|
application.state_mut_for_test().mode = PaneMode::Dual;
|
|
application.state_mut_for_test().panes[0].session_epoch = Some(connection.epoch);
|
|
application.state_mut_for_test().panes[1].location =
|
|
Location::ssh(host.clone(), "/destination");
|
|
application.state_mut_for_test().panes[1].session_epoch = Some(connection.epoch);
|
|
application.start();
|
|
settle(&mut application, &mut results).await;
|
|
|
|
application.handle_key(key(KeyCode::Char('a')), 10);
|
|
for character in "file.txt".chars() {
|
|
application.handle_key(key(KeyCode::Char(character)), 10);
|
|
}
|
|
application.handle_key(key(KeyCode::Enter), 10);
|
|
confirm_once(&mut application, Command::ConfirmEntryAction);
|
|
settle(&mut application, &mut results).await;
|
|
assert!(
|
|
application
|
|
.state()
|
|
.task_manager
|
|
.tasks()
|
|
.all(|task| task.status() == rcom::operations::TaskStatus::Succeeded),
|
|
"create failed: {:?}; diagnostics {:?}",
|
|
application.state().task_manager.tasks().collect::<Vec<_>>(),
|
|
application.state().task_diagnostics
|
|
);
|
|
assert_eq!(application.state().undo_journal.len(), 1);
|
|
|
|
assert_eq!(
|
|
application
|
|
.state()
|
|
.active_pane()
|
|
.selected_entry()
|
|
.map(|entry| entry.name.clone()),
|
|
Some("file.txt".into()),
|
|
"state before rename: entries={:?}, overlay={:?}",
|
|
application.state().active_pane().entries,
|
|
application.state().overlay
|
|
);
|
|
application.handle_key(key(KeyCode::Char('r')), 10);
|
|
assert_eq!(application.state().overlay, Overlay::EntryPrompt);
|
|
for _ in "file.txt".chars() {
|
|
application.handle_key(key(KeyCode::Backspace), 10);
|
|
}
|
|
for character in "renamed.txt".chars() {
|
|
application.handle_key(key(KeyCode::Char(character)), 10);
|
|
}
|
|
application.handle_key(key(KeyCode::Enter), 10);
|
|
confirm_once(&mut application, Command::ConfirmEntryAction);
|
|
settle(&mut application, &mut results).await;
|
|
assert!(
|
|
application
|
|
.state()
|
|
.task_manager
|
|
.tasks()
|
|
.all(|task| task.status() == rcom::operations::TaskStatus::Succeeded),
|
|
"rename failed: {:?}; diagnostics {:?}",
|
|
application.state().task_manager.tasks().collect::<Vec<_>>(),
|
|
application.state().task_diagnostics
|
|
);
|
|
assert_eq!(
|
|
application.state().task_manager.len(),
|
|
2,
|
|
"rename was not queued; overlay={:?}, action={:?}",
|
|
application.state().overlay,
|
|
application.state().entry_action
|
|
);
|
|
assert_eq!(
|
|
application.state().undo_journal.len(),
|
|
2,
|
|
"rename recording diagnostics: {:?}",
|
|
application.state().task_diagnostics
|
|
);
|
|
|
|
application.handle_key(key(KeyCode::Char('d')), 10);
|
|
settle(&mut application, &mut results).await;
|
|
confirm_once(&mut application, Command::ConfirmEntryAction);
|
|
settle(&mut application, &mut results).await;
|
|
assert_eq!(application.state().undo_journal.len(), 3);
|
|
|
|
application.handle_key(key(KeyCode::Char('u')), 10);
|
|
settle(&mut application, &mut results).await;
|
|
confirm_once(&mut application, Command::ConfirmOperation);
|
|
settle(&mut application, &mut results).await;
|
|
assert_eq!(application.state().undo_journal.len(), 2);
|
|
|
|
application.handle_key(key(KeyCode::F(6)), 10);
|
|
settle(&mut application, &mut results).await;
|
|
confirm_once(&mut application, Command::ConfirmOperation);
|
|
settle(&mut application, &mut results).await;
|
|
assert_eq!(application.state().undo_journal.len(), 3);
|
|
|
|
application.handle_key(key(KeyCode::Char('u')), 10);
|
|
settle(&mut application, &mut results).await;
|
|
confirm_once(&mut application, Command::ConfirmOperation);
|
|
settle(&mut application, &mut results).await;
|
|
assert_eq!(application.state().undo_journal.len(), 2);
|
|
assert_eq!(application.state().active, PaneId::Left);
|
|
|
|
let transfer = connector.transfer.lock().unwrap().clone().unwrap();
|
|
assert_eq!(
|
|
transfer.file_contents(&work.join("renamed.txt")).unwrap(),
|
|
b""
|
|
);
|
|
assert!(
|
|
application
|
|
.state()
|
|
.task_manager
|
|
.tasks()
|
|
.all(|task| { task.status() == rcom::operations::TaskStatus::Succeeded })
|
|
);
|
|
}
|