474 lines
18 KiB
Rust
474 lines
18 KiB
Rust
use std::sync::Arc;
|
|
use std::time::{Duration, Instant};
|
|
|
|
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers};
|
|
use ratatui::{Terminal, backend::TestBackend};
|
|
use rcom::app::{Command, ConnectSshRequest, ListingRequest, Overlay, PaneId, PaneMode};
|
|
use rcom::domain::{Entry, EntryKind, HostAlias, Location};
|
|
use rcom::fs::{BackendRegistry, FsError};
|
|
use rcom::preview::PreviewKind;
|
|
use rcom::session::{Application, PREVIEW_DEBOUNCE};
|
|
use rcom::ssh::{ConnectionManager, DiscoveryResult, OpenSshConnector};
|
|
use rcom::test_support::{MemoryFs, RecordingFs};
|
|
use rcom::ui::{Theme, render};
|
|
use tokio::sync::mpsc;
|
|
|
|
fn entries() -> Vec<Entry> {
|
|
vec![
|
|
Entry::new("a.txt", EntryKind::File),
|
|
Entry::new("b.txt", EntryKind::File),
|
|
Entry::new("c.txt", EntryKind::File),
|
|
]
|
|
}
|
|
|
|
fn application() -> (Application, mpsc::Receiver<Command>, Arc<RecordingFs>) {
|
|
let mut memory = MemoryFs::new();
|
|
for name in ["a.txt", "b.txt", "c.txt"] {
|
|
memory = memory.with_file(
|
|
Location::local(format!("/{name}")),
|
|
format!("contents-{name}"),
|
|
);
|
|
}
|
|
let recording = Arc::new(RecordingFs::new(Arc::new(memory)));
|
|
let registry = BackendRegistry::new(recording.clone());
|
|
let connections = ConnectionManager::new(Arc::new(OpenSshConnector::default()), registry);
|
|
let (mut app, results) = Application::new(
|
|
Location::local("/"),
|
|
connections,
|
|
DiscoveryResult::default(),
|
|
);
|
|
let state = app.state_mut_for_test();
|
|
state.panes[0].entries = entries();
|
|
state.panes[0].selection.set(Some(0), 3);
|
|
(app, results, recording)
|
|
}
|
|
|
|
fn draw_buffer(app: &Application) -> ratatui::buffer::Buffer {
|
|
let mut terminal = Terminal::new(TestBackend::new(120, 30)).unwrap();
|
|
terminal
|
|
.draw(|frame| render(frame, app.state(), None, &Theme::default()))
|
|
.unwrap();
|
|
terminal.backend().buffer().clone()
|
|
}
|
|
|
|
fn draw(app: &Application) -> String {
|
|
let buffer = draw_buffer(app);
|
|
(0..30)
|
|
.map(|y| {
|
|
(0..120)
|
|
.map(|x| buffer.cell((x, y)).unwrap().symbol())
|
|
.collect::<String>()
|
|
})
|
|
.collect::<Vec<_>>()
|
|
.join("\n")
|
|
}
|
|
|
|
fn preview_title(buffer: &ratatui::buffer::Buffer) -> String {
|
|
(54..120)
|
|
.map(|x| buffer.cell((x, 1)).unwrap().symbol())
|
|
.collect()
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn rapid_selection_has_no_intermediate_read_or_loading_and_starts_once_at_40ms() {
|
|
let (mut app, mut results, recording) = application();
|
|
let start = Instant::now();
|
|
app.state_mut_for_test().panes[0].preview.content = Some(rcom::preview::Preview {
|
|
kind: PreviewKind::Text,
|
|
lines: vec!["stale-old-preview".into()],
|
|
truncated: false,
|
|
directory_entries: Vec::new(),
|
|
});
|
|
app.dispatch_at(Command::MoveSelection(1), start);
|
|
assert_eq!(app.pending_preview_count(), 1);
|
|
assert!(app.state().panes[0].preview.debouncing);
|
|
assert!(!app.state().panes[0].preview.loading);
|
|
assert!(app.state().panes[0].preview.content.is_none());
|
|
let quiet = draw(&app);
|
|
assert!(!quiet.contains("LOADING PREVIEW"));
|
|
assert!(!quiet.contains("stale-old-preview"));
|
|
assert!(quiet.contains("b.txt"));
|
|
assert!(preview_title(&draw_buffer(&app)).contains("b.txt"));
|
|
|
|
app.expire_pending_at(start + Duration::from_millis(39));
|
|
assert_eq!(app.pending_preview_count(), 1);
|
|
assert!(!app.state().panes[0].preview.loading);
|
|
assert!(recording.operations().is_empty());
|
|
|
|
// A Repeat-style selection replaces the deadline instead of queueing a worker.
|
|
app.handle_key_at(
|
|
KeyEvent::new(KeyCode::Down, KeyModifiers::NONE),
|
|
10,
|
|
start + Duration::from_millis(39),
|
|
);
|
|
assert_eq!(app.pending_preview_count(), 1);
|
|
let quiet = draw(&app);
|
|
assert!(!quiet.contains("LOADING PREVIEW"));
|
|
assert!(preview_title(&draw_buffer(&app)).contains("c.txt"));
|
|
app.expire_pending_at(start + PREVIEW_DEBOUNCE);
|
|
assert_eq!(app.pending_preview_count(), 1);
|
|
app.expire_pending_at(start + Duration::from_millis(39) + PREVIEW_DEBOUNCE);
|
|
assert_eq!(app.pending_preview_count(), 0);
|
|
assert!(app.state().panes[0].preview.loading);
|
|
assert!(!app.state().panes[0].preview.debouncing);
|
|
let running = draw(&app);
|
|
assert!(!running.contains("LOADING PREVIEW"), "{running}");
|
|
assert!(!running.contains("NO PREVIEW"), "{running}");
|
|
assert!(preview_title(&draw_buffer(&app)).contains("c.txt"));
|
|
|
|
let completed = tokio::time::timeout(Duration::from_secs(1), results.recv())
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
let Command::PreviewFinished(result) = completed else {
|
|
panic!("expected one current preview")
|
|
};
|
|
assert_eq!(result.target, Location::local("/c.txt"));
|
|
assert_eq!(recording.operations().len(), 2);
|
|
app.dispatch(Command::PreviewFinished(result));
|
|
}
|
|
|
|
#[test]
|
|
fn initial_f3_and_pure_selection_effects_keep_their_distinct_modes() {
|
|
let mut state = rcom::app::AppState::new(Location::local("/"));
|
|
state.panes[0].entries = entries();
|
|
state.panes[0].selection.set(Some(0), 3);
|
|
let refresh = state.reduce(Command::Refresh);
|
|
let [rcom::app::Effect::LoadListing(listing)] = refresh.as_slice() else {
|
|
panic!("initial listing must be immediate")
|
|
};
|
|
let initial_effects = state.reduce(Command::ListingFinished(
|
|
listing.clone().into_result(Ok(entries())),
|
|
));
|
|
assert!(matches!(
|
|
initial_effects.as_slice(),
|
|
[rcom::app::Effect::LoadPreview(_)]
|
|
));
|
|
let initial = state.reduce(Command::OpenPreview);
|
|
assert!(matches!(
|
|
initial.as_slice(),
|
|
[rcom::app::Effect::LoadPreview(_)]
|
|
));
|
|
assert_eq!(state.overlay, Overlay::Preview);
|
|
let selected = state.reduce(Command::SelectLast);
|
|
assert!(matches!(
|
|
selected.as_slice(),
|
|
[rcom::app::Effect::DebouncePreview(_)]
|
|
));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn same_target_background_completion_preserves_pending_and_running_preview() {
|
|
for complete_before_deadline in [true, false] {
|
|
let (mut app, _results, _recording) = application();
|
|
let start = Instant::now();
|
|
app.dispatch_at(Command::MoveSelection(1), start);
|
|
let generation = app.state().panes[0].preview_generation;
|
|
app.dispatch_at(
|
|
Command::RefreshPane(PaneId::Left),
|
|
start + Duration::from_millis(5),
|
|
);
|
|
let listing = ListingRequest {
|
|
pane: PaneId::Left,
|
|
generation: app.state().panes[0].listing_generation,
|
|
target: Location::local("/"),
|
|
session_epoch: None,
|
|
};
|
|
if complete_before_deadline {
|
|
app.dispatch_at(
|
|
Command::ListingFinished(listing.clone().into_result(Ok(entries()))),
|
|
start + Duration::from_millis(10),
|
|
);
|
|
assert_eq!(app.pending_preview_count(), 1);
|
|
assert!(app.state().panes[0].preview.debouncing);
|
|
assert!(!app.state().panes[0].preview.loading);
|
|
}
|
|
app.expire_pending_at(start + PREVIEW_DEBOUNCE);
|
|
assert!(app.state().panes[0].preview.loading);
|
|
let current_generation = app.state().panes[0].preview_generation;
|
|
assert_eq!(current_generation, generation);
|
|
if !complete_before_deadline {
|
|
app.dispatch_at(
|
|
Command::ListingFinished(listing.into_result(Ok(entries()))),
|
|
start + PREVIEW_DEBOUNCE + Duration::from_millis(1),
|
|
);
|
|
assert_eq!(app.state().panes[0].preview_generation, current_generation);
|
|
assert!(app.state().panes[0].preview.loading);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn background_changed_selection_uses_immediate_current_preview() {
|
|
for after_deadline in [false, true] {
|
|
let (mut app, _results, _recording) = application();
|
|
let start = Instant::now();
|
|
app.dispatch_at(Command::MoveSelection(1), start);
|
|
if after_deadline {
|
|
app.expire_pending_at(start + PREVIEW_DEBOUNCE);
|
|
}
|
|
app.dispatch_at(Command::RefreshPane(PaneId::Left), start);
|
|
let listing = ListingRequest {
|
|
pane: PaneId::Left,
|
|
generation: app.state().panes[0].listing_generation,
|
|
target: Location::local("/"),
|
|
session_epoch: None,
|
|
};
|
|
app.dispatch_at(
|
|
Command::ListingFinished(
|
|
listing.into_result(Ok(vec![Entry::new("z.txt", EntryKind::File)])),
|
|
),
|
|
start + Duration::from_millis(1),
|
|
);
|
|
assert_eq!(app.pending_preview_count(), 0);
|
|
assert!(!app.state().panes[0].preview.debouncing);
|
|
assert!(app.state().panes[0].preview.loading);
|
|
assert_eq!(
|
|
app.state().panes[0].preview.target,
|
|
Some(Location::local("/z.txt"))
|
|
);
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn dual_clone_does_not_inherit_orphan_debounce_and_source_deadline_survives() {
|
|
let (mut app, _results, _recording) = application();
|
|
let start = Instant::now();
|
|
app.dispatch_at(Command::MoveSelection(1), start);
|
|
let deadline = app.next_preview_deadline();
|
|
app.dispatch_at(Command::TogglePaneMode, start + Duration::from_millis(1));
|
|
assert_eq!(app.state().mode, PaneMode::Dual);
|
|
assert!(!app.state().panes[1].preview.debouncing);
|
|
assert!(!app.state().panes[1].preview.loading);
|
|
assert_eq!(app.next_preview_deadline(), deadline);
|
|
app.expire_pending_at(start + PREVIEW_DEBOUNCE);
|
|
assert!(app.state().panes[0].preview.loading);
|
|
assert!(!app.state().panes[1].preview.loading);
|
|
}
|
|
|
|
#[test]
|
|
fn start_preview_rejects_wrong_generation_target_navigation_and_host_at_equal_epoch() {
|
|
let host = HostAlias::new("one");
|
|
let other = HostAlias::new("two");
|
|
let home = Location::ssh(host.clone(), "/home");
|
|
let mut state = rcom::app::AppState::new(home.clone());
|
|
state.reduce(Command::ConnectSsh(host.clone()));
|
|
let connect = ConnectSshRequest {
|
|
pane: PaneId::Left,
|
|
generation: 1,
|
|
host: host.clone(),
|
|
expected_session_epoch: None,
|
|
};
|
|
let effects = state.reduce(Command::ConnectSshFinished(connect.into_result(Ok(
|
|
rcom::app::SshConnection {
|
|
location: home.clone(),
|
|
session_epoch: 9,
|
|
},
|
|
))));
|
|
let [rcom::app::Effect::LoadListing(listing)] = effects.as_slice() else {
|
|
panic!("connect should request listing")
|
|
};
|
|
state.reduce(Command::ListingFinished(
|
|
listing.clone().into_result(Ok(entries())),
|
|
));
|
|
let selected = state.reduce(Command::SelectLast);
|
|
let [rcom::app::Effect::DebouncePreview(request)] = selected.as_slice() else {
|
|
panic!("selection should debounce")
|
|
};
|
|
let request = request.clone();
|
|
for invalid in [
|
|
rcom::app::PreviewRequest {
|
|
generation: request.generation + 1,
|
|
..request.clone()
|
|
},
|
|
rcom::app::PreviewRequest {
|
|
target: home.join("wrong"),
|
|
..request.clone()
|
|
},
|
|
rcom::app::PreviewRequest {
|
|
target: Location::ssh(other, "/home/c.txt"),
|
|
session_epoch: request.session_epoch,
|
|
..request.clone()
|
|
},
|
|
] {
|
|
assert!(
|
|
state
|
|
.reduce(Command::StartPreview {
|
|
navigation_generation: state.active_pane().navigation_generation,
|
|
request: invalid,
|
|
})
|
|
.is_empty()
|
|
);
|
|
}
|
|
assert!(
|
|
state
|
|
.reduce(Command::StartPreview {
|
|
navigation_generation: state.active_pane().navigation_generation + 1,
|
|
request: request.clone(),
|
|
})
|
|
.is_empty()
|
|
);
|
|
assert!(matches!(
|
|
state
|
|
.reduce(Command::StartPreview {
|
|
navigation_generation: state.active_pane().navigation_generation,
|
|
request,
|
|
})
|
|
.as_slice(),
|
|
[rcom::app::Effect::LoadPreview(_)]
|
|
));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn every_selection_alias_and_repeat_coalesces_to_one_read() {
|
|
let press = |code| KeyEvent::new(code, KeyModifiers::NONE);
|
|
let mut keys = vec![
|
|
press(KeyCode::Char('j')),
|
|
press(KeyCode::Down),
|
|
press(KeyCode::Char('k')),
|
|
press(KeyCode::Up),
|
|
press(KeyCode::Home),
|
|
press(KeyCode::End),
|
|
press(KeyCode::PageUp),
|
|
press(KeyCode::PageDown),
|
|
KeyEvent::new(KeyCode::Char('b'), KeyModifiers::CONTROL),
|
|
KeyEvent::new(KeyCode::Char('f'), KeyModifiers::CONTROL),
|
|
KeyEvent::new(KeyCode::Char('u'), KeyModifiers::CONTROL),
|
|
KeyEvent::new(KeyCode::Char('d'), KeyModifiers::CONTROL),
|
|
press(KeyCode::Char(' ')),
|
|
press(KeyCode::Insert),
|
|
KeyEvent {
|
|
code: KeyCode::Char('j'),
|
|
modifiers: KeyModifiers::NONE,
|
|
kind: KeyEventKind::Repeat,
|
|
state: KeyEventState::NONE,
|
|
},
|
|
];
|
|
keys.push(press(KeyCode::Char('G')));
|
|
for key in keys {
|
|
let (mut app, mut results, recording) = application();
|
|
app.state_mut_for_test().panes[0].selection.set(Some(1), 3);
|
|
let start = Instant::now();
|
|
app.handle_key_at(key, 2, start);
|
|
assert_eq!(app.pending_preview_count(), 1, "{key:?}");
|
|
assert!(recording.operations().is_empty(), "{key:?}");
|
|
app.expire_pending_at(start + PREVIEW_DEBOUNCE);
|
|
let result = tokio::time::timeout(Duration::from_secs(1), results.recv())
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
assert!(matches!(result, Command::PreviewFinished(_)), "{key:?}");
|
|
assert_eq!(recording.operations().len(), 2, "{key:?}");
|
|
}
|
|
// `gg` is the only two-key alias and uses the same deadline scheduler.
|
|
let (mut app, mut results, recording) = application();
|
|
let start = Instant::now();
|
|
app.state_mut_for_test().panes[0].selection.set(Some(1), 3);
|
|
app.handle_key_at(press(KeyCode::Char('g')), 2, start);
|
|
app.handle_key_at(
|
|
press(KeyCode::Char('g')),
|
|
2,
|
|
start + Duration::from_millis(1),
|
|
);
|
|
assert_eq!(app.pending_preview_count(), 1);
|
|
app.expire_pending_at(start + Duration::from_millis(1) + PREVIEW_DEBOUNCE);
|
|
assert!(matches!(
|
|
results.recv().await,
|
|
Some(Command::PreviewFinished(_))
|
|
));
|
|
assert_eq!(recording.operations().len(), 2);
|
|
}
|
|
|
|
#[test]
|
|
fn failed_parent_recovers_pending_and_running_selection_preview() {
|
|
for after_deadline in [false, true] {
|
|
let mut state = rcom::app::AppState::new(Location::local("/workspace"));
|
|
state.panes[0].entries = entries();
|
|
state.panes[0].selection.set(Some(0), 3);
|
|
let selected = state.reduce(Command::MoveSelection(1));
|
|
let [rcom::app::Effect::DebouncePreview(request)] = selected.as_slice() else {
|
|
panic!("selection should debounce")
|
|
};
|
|
let old_generation = request.generation;
|
|
if after_deadline {
|
|
let started = state.reduce(Command::StartPreview {
|
|
request: request.clone(),
|
|
navigation_generation: state.active_pane().navigation_generation,
|
|
});
|
|
assert!(matches!(
|
|
started.as_slice(),
|
|
[rcom::app::Effect::LoadPreview(_)]
|
|
));
|
|
}
|
|
let parent = state.reduce(Command::Parent);
|
|
let [rcom::app::Effect::LoadListing(listing)] = parent.as_slice() else {
|
|
panic!("parent should navigate")
|
|
};
|
|
let recovered = state.reduce(Command::ListingFinished(
|
|
listing.clone().into_result(Err(FsError::NotFound)),
|
|
));
|
|
assert!(matches!(
|
|
recovered.as_slice(),
|
|
[rcom::app::Effect::LoadPreview(request)] if request.generation > old_generation
|
|
));
|
|
assert!(!state.active_pane().preview.debouncing);
|
|
}
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn failed_navigation_and_connect_create_immediate_replacement_not_stale_pending() {
|
|
for connect in [false, true] {
|
|
for after_deadline in [false, true] {
|
|
let (mut app, _results, _recording) = application();
|
|
let start = Instant::now();
|
|
app.dispatch_at(Command::MoveSelection(1), start);
|
|
let old_generation = app.state().panes[0].preview_generation;
|
|
if after_deadline {
|
|
app.expire_pending_at(start + PREVIEW_DEBOUNCE);
|
|
assert!(app.state().panes[0].preview.selection_recovery);
|
|
assert!(app.state().panes[0].preview.loading);
|
|
}
|
|
if connect {
|
|
let host = HostAlias::new("other");
|
|
app.dispatch_at(
|
|
Command::ConnectSsh(host.clone()),
|
|
start + Duration::from_millis(1),
|
|
);
|
|
let request = ConnectSshRequest {
|
|
pane: PaneId::Left,
|
|
generation: app.state().panes[0].listing_generation,
|
|
host,
|
|
expected_session_epoch: None,
|
|
};
|
|
app.dispatch_at(
|
|
Command::ConnectSshFinished(request.into_result(Err(FsError::Disconnected))),
|
|
start + Duration::from_millis(2),
|
|
);
|
|
} else {
|
|
app.state_mut_for_test().panes[0].entries =
|
|
vec![Entry::new("dir", EntryKind::Directory)];
|
|
app.state_mut_for_test().panes[0].selection.set(Some(0), 1);
|
|
app.dispatch_at(Command::Enter, start + Duration::from_millis(1));
|
|
let request = ListingRequest {
|
|
pane: PaneId::Left,
|
|
generation: app.state().panes[0].listing_generation,
|
|
target: Location::local("/dir"),
|
|
session_epoch: None,
|
|
};
|
|
app.dispatch_at(
|
|
Command::ListingFinished(request.into_result(Err(FsError::NotFound))),
|
|
start + Duration::from_millis(2),
|
|
);
|
|
}
|
|
assert!(app.state().panes[0].preview.loading, "connect={connect}");
|
|
assert!(
|
|
!app.state().panes[0].preview.debouncing,
|
|
"connect={connect}"
|
|
);
|
|
assert!(app.state().panes[0].preview_generation > old_generation);
|
|
assert_eq!(app.pending_preview_count(), 0);
|
|
app.expire_pending_at(start + PREVIEW_DEBOUNCE);
|
|
assert_eq!(app.pending_preview_count(), 0);
|
|
}
|
|
}
|
|
}
|