rcom/tests/transfer_connection.rs
2026-12-05 23:30:06 +03:00

93 lines
2.9 KiB
Rust

use std::sync::Arc;
use async_trait::async_trait;
use rcom::domain::{HostAlias, Location, SourceId};
use rcom::fs::{
BackendRegistry, FileOperations, FsError, LocalFileOperations, LocalFs, ReadFileSystem,
};
use rcom::ssh::{ConnectionManager, SshConnector};
use rcom::test_support::MemoryFs;
struct ReadOnlyRemote;
#[async_trait]
impl SshConnector for ReadOnlyRemote {
async fn connect(&self, host: &HostAlias) -> Result<Arc<dyn ReadFileSystem>, FsError> {
let location = Location::ssh(host.clone(), "/home/user");
Ok(Arc::new(MemoryFs::new().with_dir(location)))
}
}
struct MixedRemote;
#[async_trait]
impl SshConnector for MixedRemote {
async fn connect(&self, host: &HostAlias) -> Result<Arc<dyn ReadFileSystem>, FsError> {
ReadOnlyRemote.connect(host).await
}
async fn connect_with_operations(
&self,
host: &HostAlias,
_session_epoch: u64,
) -> Result<(Arc<dyn ReadFileSystem>, Option<Arc<dyn FileOperations>>), FsError> {
let read = self.connect(host).await?;
Ok((read, Some(Arc::new(LocalFileOperations::new()))))
}
}
fn manager(connector: Arc<dyn SshConnector>) -> ConnectionManager {
ConnectionManager::new(connector, BackendRegistry::new(Arc::new(LocalFs::new())))
}
#[tokio::test]
async fn local_resolution_shares_one_typed_read_write_endpoint() {
let connections = manager(Arc::new(ReadOnlyRemote));
let first = connections
.resolve_operations(&Location::local("/"), None)
.await
.unwrap();
let second = connections
.resolve_operations(&Location::local("/tmp"), None)
.await
.unwrap();
assert_eq!(first.source_id(), &SourceId::Local);
assert_eq!(second.source_id(), &SourceId::Local);
assert!(first.transfer.capabilities().regular_files);
}
#[tokio::test]
async fn read_only_remote_is_explicitly_unsupported_for_mutation() {
let host = HostAlias::new("edge");
let connections = manager(Arc::new(ReadOnlyRemote));
let connection = connections.connect(&host).await.unwrap();
let error = match connections
.resolve_operations(&connection.home, Some(connection.epoch))
.await
{
Ok(_) => panic!("read-only remote unexpectedly accepted a mutation adapter"),
Err(error) => error,
};
assert!(matches!(error, FsError::Unsupported(_)));
assert!(
connections
.resolve(&connection.home, Some(connection.epoch))
.await
.is_ok()
);
}
#[tokio::test]
async fn mixed_remote_transfer_adapter_is_rejected_before_it_can_be_cached() {
let host = HostAlias::new("edge");
let connections = manager(Arc::new(MixedRemote));
let error = match connections.connect(&host).await {
Ok(_) => panic!("mixed endpoint connection was cached"),
Err(error) => error,
};
assert!(matches!(error, FsError::Other(_)));
assert!(connections.current(&host).await.is_none());
}