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

89 lines
3.2 KiB
Rust

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::Command;
use rcom::domain::{HostAlias, Location};
use rcom::fs::{BackendRegistry, FsError, LocalFs, ReadFileSystem};
use rcom::preview::MAX_PREVIEW_BYTES;
use rcom::session::Application;
use rcom::ssh::{ConnectionManager, DiscoveryResult, SshConnector};
use rcom::test_support::{FsOperation, RecordingFs};
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!("acceptance test must not access the network")
}
}
fn manifest(root: &std::path::Path) -> Vec<(String, Vec<u8>)> {
let mut paths: Vec<_> = fs::read_dir(root)
.unwrap()
.map(|entry| entry.unwrap().path())
.collect();
paths.sort();
paths
.into_iter()
.map(|path| {
let name = path.file_name().unwrap().to_string_lossy().into_owned();
let contents = if path.is_dir() {
fs::read_dir(path)
.unwrap()
.map(|entry| entry.unwrap().file_name().to_string_lossy().into_owned())
.collect::<Vec<_>>()
.join("\n")
.into_bytes()
} else {
fs::read(path).unwrap()
};
(name, contents)
})
.collect()
}
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,
}
}
}
#[tokio::test]
async fn navigation_and_preview_do_not_mutate_the_fixture() {
let fixture = TempDir::new().unwrap();
fs::create_dir(fixture.path().join("docs")).unwrap();
fs::write(fixture.path().join("docs/guide.txt"), "guide contents").unwrap();
fs::write(fixture.path().join("readme.txt"), "read-only fixture").unwrap();
let before = manifest(fixture.path());
let recording = Arc::new(RecordingFs::new(Arc::new(LocalFs::new())));
let registry = BackendRegistry::new(recording.clone());
let connections = ConnectionManager::new(Arc::new(NoNetwork), registry);
let (mut application, mut results) = Application::new(
Location::local(fixture.path()),
connections,
DiscoveryResult::default(),
);
application.start();
settle(&mut application, &mut results).await;
application.handle_key(KeyEvent::new(KeyCode::Char('j'), KeyModifiers::NONE), 10);
application.handle_key(KeyEvent::new(KeyCode::F(3), KeyModifiers::NONE), 10);
settle(&mut application, &mut results).await;
let operations = recording.operations();
assert!(!operations.is_empty());
assert!(operations.iter().any(|operation| {
operation.operation == FsOperation::ReadPrefix && operation.limit == Some(MAX_PREVIEW_BYTES)
}));
assert_eq!(manifest(fixture.path()), before);
}