228 lines
8 KiB
Rust
228 lines
8 KiB
Rust
use pretty_assertions::assert_eq;
|
|
use ratatui::Terminal;
|
|
use ratatui::backend::TestBackend;
|
|
use ratatui::buffer::Buffer;
|
|
use rcom::app::{AppState, Command, Effect, PaneId, PaneMode, PreviewRequest};
|
|
use rcom::domain::{Entry, EntryKind, Location};
|
|
use rcom::fs::FsError;
|
|
use rcom::preview::{Preview, PreviewKind};
|
|
use rcom::ui::{Theme, render};
|
|
|
|
fn draw(app: &AppState) -> Buffer {
|
|
draw_size(app, 100, 24)
|
|
}
|
|
|
|
fn draw_size(app: &AppState, width: u16, height: u16) -> Buffer {
|
|
let backend = TestBackend::new(width, height);
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
terminal
|
|
.draw(|frame| render(frame, app, None, &Theme::default()))
|
|
.unwrap();
|
|
terminal.backend().buffer().clone()
|
|
}
|
|
|
|
fn text(buffer: &Buffer) -> String {
|
|
(0..buffer.area.height)
|
|
.map(|y| {
|
|
(0..buffer.area.width)
|
|
.map(|x| buffer.cell((x, y)).unwrap().symbol())
|
|
.collect::<String>()
|
|
})
|
|
.collect::<Vec<_>>()
|
|
.join("\n")
|
|
}
|
|
|
|
fn dual_app() -> AppState {
|
|
let mut app = AppState::new(Location::local("/left"));
|
|
app.mode = PaneMode::Dual;
|
|
app.panes[0].entries = vec![Entry::new("left.txt", EntryKind::File)];
|
|
app.panes[0].selection.set(Some(0), 1);
|
|
app.panes[1].location = Location::local("/right");
|
|
app.panes[1].entries = vec![Entry::new("right.txt", EntryKind::File)];
|
|
app.panes[1].selection.set(Some(0), 1);
|
|
app
|
|
}
|
|
|
|
#[test]
|
|
fn background_dual_poll_keeps_right_pane_and_active_entries_status_identical() {
|
|
let mut app = dual_app();
|
|
let before = draw(&app);
|
|
assert!(text(&before).contains("1 entries"));
|
|
assert!(!text(&before).contains("LOADING"));
|
|
|
|
for pane in [PaneId::Left, PaneId::Right] {
|
|
let effects = app.reduce(Command::RefreshPane(pane));
|
|
assert!(matches!(effects.as_slice(), [Effect::LoadListing(_)]));
|
|
}
|
|
let during = draw(&app);
|
|
assert_eq!(during, before);
|
|
}
|
|
|
|
#[test]
|
|
fn background_poll_preserves_the_confirmed_empty_directory_frame() {
|
|
let mut app = AppState::new(Location::local("/empty"));
|
|
let effects: [Effect; 1] = app.reduce(Command::Refresh).try_into().unwrap();
|
|
let [Effect::LoadListing(initial)] = effects else {
|
|
panic!("expected initial listing");
|
|
};
|
|
app.reduce(Command::ListingFinished(
|
|
initial.into_result(Ok(Vec::new())),
|
|
));
|
|
let before = draw(&app);
|
|
assert!(text(&before).contains("EMPTY DIRECTORY"));
|
|
assert!(text(&before).contains("0 entries"));
|
|
|
|
let effects = app.reduce(Command::RefreshPane(PaneId::Left));
|
|
assert!(matches!(effects.as_slice(), [Effect::LoadListing(_)]));
|
|
let during = draw(&app);
|
|
assert!(text(&during).contains("EMPTY DIRECTORY"));
|
|
assert!(!text(&during).contains("LOADING"));
|
|
assert_eq!(during, before);
|
|
}
|
|
|
|
#[test]
|
|
fn background_response_updates_entries_and_count_without_an_intermediate_loading_frame() {
|
|
let mut app = dual_app();
|
|
let before = draw(&app);
|
|
let effects: [Effect; 1] = app
|
|
.reduce(Command::RefreshPane(PaneId::Left))
|
|
.try_into()
|
|
.unwrap();
|
|
let [Effect::LoadListing(request)] = effects else {
|
|
panic!("expected background listing");
|
|
};
|
|
assert_eq!(draw(&app), before);
|
|
|
|
app.reduce(Command::ListingFinished(request.into_result(Ok(vec![
|
|
Entry::new("left.txt", EntryKind::File),
|
|
Entry::new("new.txt", EntryKind::File),
|
|
]))));
|
|
let after = draw(&app);
|
|
let rendered = text(&after);
|
|
assert!(rendered.contains("left.txt"));
|
|
assert!(rendered.contains("new.txt"));
|
|
assert!(rendered.contains("2 entries"));
|
|
assert!(rendered.contains("Selected 1/2"));
|
|
assert!(!rendered.contains("LOADING"));
|
|
}
|
|
|
|
#[test]
|
|
fn background_recovery_keeps_the_existing_error_visible_until_the_response() {
|
|
let mut app = dual_app();
|
|
app.panes[0].error = Some(FsError::PermissionDenied);
|
|
let before = draw(&app);
|
|
assert!(text(&before).contains("ERROR"));
|
|
|
|
let effects = app.reduce(Command::RefreshPane(PaneId::Left));
|
|
assert!(matches!(effects.as_slice(), [Effect::LoadListing(_)]));
|
|
assert_eq!(app.panes[0].error, Some(FsError::PermissionDenied));
|
|
let during = draw(&app);
|
|
assert!(text(&during).contains("ERROR"));
|
|
assert!(!text(&during).contains("LOADING"));
|
|
assert_eq!(during, before);
|
|
}
|
|
|
|
fn preview_app() -> AppState {
|
|
let root = Location::local("/workspace");
|
|
let mut app = AppState::new(root.clone());
|
|
app.active_pane_mut().entries = vec![Entry::new("note.txt", EntryKind::File)];
|
|
app.active_pane_mut().selection.set(Some(0), 1);
|
|
app.active_pane_mut().preview.target = Some(root.join("note.txt"));
|
|
app.active_pane_mut().preview.content = Some(Preview {
|
|
kind: PreviewKind::Text,
|
|
lines: vec!["1 │ stable preview".into()],
|
|
truncated: false,
|
|
directory_entries: Vec::new(),
|
|
});
|
|
app
|
|
}
|
|
|
|
fn background_preview_request(app: &mut AppState, entries: Vec<Entry>) -> PreviewRequest {
|
|
let effects: [Effect; 1] = app
|
|
.reduce(Command::RefreshPane(PaneId::Left))
|
|
.try_into()
|
|
.unwrap();
|
|
let [Effect::LoadListing(listing)] = effects else {
|
|
panic!("expected background listing");
|
|
};
|
|
let effects: [Effect; 1] = app
|
|
.reduce(Command::ListingFinished(listing.into_result(Ok(entries))))
|
|
.try_into()
|
|
.unwrap();
|
|
let [Effect::LoadPreview(request)] = effects else {
|
|
panic!("expected background preview");
|
|
};
|
|
request
|
|
}
|
|
|
|
#[test]
|
|
fn background_preview_keeps_the_same_file_frame_and_atomically_applies_changes() {
|
|
let mut app = preview_app();
|
|
let before = draw_size(&app, 140, 30);
|
|
let request =
|
|
background_preview_request(&mut app, vec![Entry::new("note.txt", EntryKind::File)]);
|
|
let during = draw_size(&app, 140, 30);
|
|
assert!(text(&during).contains("stable preview"));
|
|
assert!(!text(&during).contains("LOADING PREVIEW"));
|
|
assert_eq!(during, before);
|
|
|
|
app.reduce(Command::PreviewFinished(request.into_result(Ok(Preview {
|
|
kind: PreviewKind::Text,
|
|
lines: vec!["1 │ changed preview".into()],
|
|
truncated: false,
|
|
directory_entries: Vec::new(),
|
|
}))));
|
|
let after = text(&draw_size(&app, 140, 30));
|
|
assert!(after.contains("changed preview"));
|
|
assert!(!after.contains("stable preview"));
|
|
assert!(!after.contains("LOADING PREVIEW"));
|
|
}
|
|
|
|
#[test]
|
|
fn background_preview_recovery_keeps_the_existing_error_frame() {
|
|
let mut app = preview_app();
|
|
app.active_pane_mut().preview.content = None;
|
|
app.active_pane_mut().preview.error = Some(FsError::PermissionDenied);
|
|
let before = draw_size(&app, 140, 30);
|
|
assert!(text(&before).contains("PREVIEW ERROR"));
|
|
|
|
background_preview_request(&mut app, vec![Entry::new("note.txt", EntryKind::File)]);
|
|
let during = draw_size(&app, 140, 30);
|
|
assert!(text(&during).contains("PREVIEW ERROR"));
|
|
assert!(!text(&during).contains("LOADING PREVIEW"));
|
|
assert_eq!(during, before);
|
|
}
|
|
|
|
#[test]
|
|
fn background_listing_with_changed_or_removed_selection_never_shows_the_old_preview() {
|
|
let mut changed = preview_app();
|
|
background_preview_request(&mut changed, vec![Entry::new("other.txt", EntryKind::File)]);
|
|
assert_eq!(
|
|
changed.active_pane().selected_entry().unwrap().name,
|
|
"other.txt"
|
|
);
|
|
assert!(changed.active_pane().preview.content.is_none());
|
|
let loading = text(&draw_size(&changed, 140, 30));
|
|
assert!(loading.contains("LOADING PREVIEW"));
|
|
assert!(!loading.contains("stable preview"));
|
|
|
|
let mut removed = preview_app();
|
|
let effects: [Effect; 1] = removed
|
|
.reduce(Command::RefreshPane(PaneId::Left))
|
|
.try_into()
|
|
.unwrap();
|
|
let [Effect::LoadListing(listing)] = effects else {
|
|
panic!("expected background listing");
|
|
};
|
|
assert!(
|
|
removed
|
|
.reduce(Command::ListingFinished(
|
|
listing.into_result(Ok(Vec::new()))
|
|
))
|
|
.is_empty()
|
|
);
|
|
assert!(removed.active_pane().preview.content.is_none());
|
|
let empty = text(&draw_size(&removed, 140, 30));
|
|
assert!(empty.contains("NO PREVIEW"));
|
|
assert!(!empty.contains("stable preview"));
|
|
}
|