116 lines
4 KiB
Rust
116 lines
4 KiB
Rust
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
|
|
use async_trait::async_trait;
|
|
use rcom::app::{AppState, Command, Overlay};
|
|
use rcom::domain::{HostAlias, Location};
|
|
use rcom::fs::{BackendRegistry, FsError, LocalFs, ReadFileSystem};
|
|
use rcom::operations::{OperationEventKind, TaskStatus};
|
|
use rcom::runtime::EffectRunner;
|
|
use rcom::ssh::{ConnectionManager, SshConnector};
|
|
use tempfile::TempDir;
|
|
use tokio::sync::mpsc;
|
|
|
|
struct NoNetwork;
|
|
|
|
#[async_trait]
|
|
impl SshConnector for NoNetwork {
|
|
async fn connect(&self, _host: &HostAlias) -> Result<Arc<dyn ReadFileSystem>, FsError> {
|
|
panic!("entry actions must remain local")
|
|
}
|
|
}
|
|
|
|
fn dispatch(app: &mut AppState, runner: &mut EffectRunner, command: Command) {
|
|
for effect in app.reduce(command) {
|
|
runner.execute(effect);
|
|
}
|
|
}
|
|
|
|
async fn run_to_completion(
|
|
app: &mut AppState,
|
|
runner: &mut EffectRunner,
|
|
results: &mut mpsc::Receiver<Command>,
|
|
) {
|
|
loop {
|
|
let command = tokio::time::timeout(Duration::from_secs(5), results.recv())
|
|
.await
|
|
.expect("entry action timed out")
|
|
.expect("runtime channel closed");
|
|
let terminal = matches!(
|
|
&command,
|
|
Command::OperationEvent(event)
|
|
if matches!(event.kind, OperationEventKind::Completed | OperationEventKind::CancellationAcknowledged)
|
|
);
|
|
dispatch(app, runner, command);
|
|
if terminal {
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
fn input_name(app: &mut AppState, runner: &mut EffectRunner, name: &str) {
|
|
for character in name.chars() {
|
|
dispatch(app, runner, Command::EntryInput(character));
|
|
}
|
|
dispatch(app, runner, Command::SubmitEntry);
|
|
assert_eq!(app.overlay, Overlay::ConfirmEntryAction);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn create_file_directory_and_rename_flow_through_the_shared_runtime_queue() {
|
|
let fixture = TempDir::new().unwrap();
|
|
let old = fixture.path().join("old.txt");
|
|
std::fs::write(&old, b"contents").unwrap();
|
|
let old_entry = LocalFs::new()
|
|
.metadata(&Location::local(&old))
|
|
.await
|
|
.unwrap();
|
|
let connections = ConnectionManager::new(
|
|
Arc::new(NoNetwork),
|
|
BackendRegistry::new(Arc::new(LocalFs::new())),
|
|
);
|
|
let (mut runner, mut results) = EffectRunner::new(connections);
|
|
let mut app = AppState::new(Location::local(fixture.path()));
|
|
app.active_pane_mut().entries = vec![old_entry];
|
|
app.active_pane_mut().selection.set(Some(0), 1);
|
|
|
|
dispatch(&mut app, &mut runner, Command::CreateFile);
|
|
assert_eq!(app.overlay, Overlay::EntryPrompt);
|
|
assert!(!fixture.path().join("new.txt").exists());
|
|
input_name(&mut app, &mut runner, "new.txt");
|
|
assert!(!fixture.path().join("new.txt").exists());
|
|
dispatch(&mut app, &mut runner, Command::ConfirmEntryAction);
|
|
dispatch(&mut app, &mut runner, Command::ConfirmEntryAction);
|
|
run_to_completion(&mut app, &mut runner, &mut results).await;
|
|
assert_eq!(std::fs::read(fixture.path().join("new.txt")).unwrap(), b"");
|
|
assert_eq!(
|
|
app.task_manager.len(),
|
|
1,
|
|
"double confirmation must not duplicate work"
|
|
);
|
|
|
|
dispatch(&mut app, &mut runner, Command::CreateDirectory);
|
|
input_name(&mut app, &mut runner, "new-dir");
|
|
dispatch(&mut app, &mut runner, Command::ConfirmEntryAction);
|
|
run_to_completion(&mut app, &mut runner, &mut results).await;
|
|
assert!(fixture.path().join("new-dir").is_dir());
|
|
|
|
dispatch(&mut app, &mut runner, Command::Rename);
|
|
for _ in "old.txt".chars() {
|
|
dispatch(&mut app, &mut runner, Command::EntryBackspace);
|
|
}
|
|
input_name(&mut app, &mut runner, "renamed.txt");
|
|
dispatch(&mut app, &mut runner, Command::ConfirmEntryAction);
|
|
run_to_completion(&mut app, &mut runner, &mut results).await;
|
|
assert!(!old.exists());
|
|
assert_eq!(
|
|
std::fs::read(fixture.path().join("renamed.txt")).unwrap(),
|
|
b"contents"
|
|
);
|
|
assert_eq!(app.task_manager.len(), 3);
|
|
assert!(
|
|
app.task_manager
|
|
.tasks()
|
|
.all(|task| task.status() == TaskStatus::Succeeded)
|
|
);
|
|
}
|