175 lines
6.3 KiB
Rust
175 lines
6.3 KiB
Rust
use ratatui::Terminal;
|
|
use ratatui::backend::TestBackend;
|
|
use rcom::app::{AppState, Command, HelpCategory, Overlay, PaneId, PaneMode};
|
|
use rcom::domain::{Entry, EntryKind, HostAlias, Location};
|
|
use rcom::preview::{Preview, PreviewKind};
|
|
use rcom::ssh::HostPickerState;
|
|
use rcom::ui::{Theme, render};
|
|
|
|
fn sample_app(mode: PaneMode) -> AppState {
|
|
let mut app = AppState::new(Location::local("/workspace/project"));
|
|
app.panes[0].entries = vec![
|
|
Entry::new("docs", EntryKind::Directory),
|
|
Entry::new("README.md", EntryKind::File),
|
|
];
|
|
app.panes[0].selection.set(Some(1), 2);
|
|
app.panes[0].preview.content = Some(Preview {
|
|
kind: PreviewKind::Text,
|
|
lines: vec![
|
|
"1 │ # rcom".to_owned(),
|
|
"2 │ F3 opens this temporary layer.".to_owned(),
|
|
"3 │ The underlying panes keep their geometry.".to_owned(),
|
|
],
|
|
truncated: false,
|
|
directory_entries: Vec::new(),
|
|
});
|
|
app.panes[1] = app.panes[0].clone();
|
|
app.panes[1].location = Location::ssh(HostAlias::new("edge-prod"), "/srv/releases");
|
|
app.mode = mode;
|
|
app
|
|
}
|
|
|
|
fn draw(app: &AppState, picker: Option<&HostPickerState>, width: u16, height: u16) -> String {
|
|
let backend = TestBackend::new(width, height);
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
terminal
|
|
.draw(|frame| render(frame, app, picker, &Theme::default()))
|
|
.unwrap();
|
|
let buffer = terminal.backend().buffer();
|
|
(0..buffer.area.height)
|
|
.map(|y| {
|
|
let row: String = (0..buffer.area.width)
|
|
.map(|x| buffer.cell((x, y)).unwrap().symbol())
|
|
.collect();
|
|
row.trim_end().to_owned()
|
|
})
|
|
.collect::<Vec<_>>()
|
|
.join("\n")
|
|
}
|
|
|
|
#[test]
|
|
fn help_categories_are_neutral_single_column_and_do_not_mutate_the_workspace() {
|
|
let mut app = sample_app(PaneMode::Dual);
|
|
app.reduce(Command::ToggleHelp);
|
|
let workspace = app.clone();
|
|
for category in HelpCategory::ALL {
|
|
while app.help.category != category {
|
|
app.reduce(Command::SelectHelpCategory(1));
|
|
}
|
|
let before_render = app.clone();
|
|
let output = draw(&app, None, 120, 40);
|
|
assert_eq!(app, before_render);
|
|
assert!(output.contains("HELP"));
|
|
assert!(output.contains(&format!("[{}]", category.label())));
|
|
assert!(!output.contains("yazi + mc"));
|
|
let expected = match category {
|
|
HelpCategory::Navigation => "Workspace: Next entry",
|
|
HelpCategory::Selection => "Space / Insert",
|
|
HelpCategory::Files => "Yank/copy selection to clipboard",
|
|
HelpCategory::Tasks => "Open the task list",
|
|
HelpCategory::Ssh => "Choose an SSH host",
|
|
HelpCategory::General => "Switch active pane",
|
|
};
|
|
assert!(output.contains(expected), "missing {expected:?}\n{output}");
|
|
}
|
|
assert_eq!(app.panes, workspace.panes);
|
|
assert_eq!(app.clipboard, workspace.clipboard);
|
|
}
|
|
|
|
#[test]
|
|
fn host_picker_shows_query_selection_warnings_and_empty_results() {
|
|
let picker = HostPickerState::from_strings(
|
|
vec!["edge-prod".to_owned(), "builder".to_owned()],
|
|
vec!["Include /broken/config was ignored".to_owned()],
|
|
);
|
|
let mut app = sample_app(PaneMode::Single);
|
|
app.overlay = Overlay::HostPicker;
|
|
let output = draw(&app, Some(&picker), 120, 40);
|
|
assert!(output.contains("Filter:"));
|
|
assert!(output.contains("WARNING"));
|
|
assert!(output.contains("broken/config"));
|
|
assert!(output.contains("▸ edge-prod"));
|
|
assert!(output.contains("builder"));
|
|
assert!(output.contains("Enter Connect"));
|
|
|
|
let empty = HostPickerState::from_strings(Vec::new(), Vec::new());
|
|
let output = draw(&app, Some(&empty), 120, 40);
|
|
assert!(output.contains("No SSH hosts found"));
|
|
|
|
let mut no_match = HostPickerState::from_strings(vec!["builder".to_owned()], Vec::new());
|
|
no_match.push_char('z');
|
|
let output = draw(&app, Some(&no_match), 120, 40);
|
|
assert!(output.contains("Filter: z"));
|
|
assert!(output.contains("No hosts match"));
|
|
}
|
|
|
|
#[test]
|
|
fn f3_and_esc_keep_locations_selections_and_dual_mode_intact() {
|
|
let mut app = sample_app(PaneMode::Dual);
|
|
app.active = PaneId::Right;
|
|
let locations = [app.panes[0].location.clone(), app.panes[1].location.clone()];
|
|
let selections = [app.panes[0].selection, app.panes[1].selection];
|
|
|
|
let _effects = app.reduce(Command::OpenPreview);
|
|
assert_eq!(app.overlay, Overlay::Preview);
|
|
let before_render = app.clone();
|
|
let output = draw(&app, None, 160, 40);
|
|
assert_eq!(app, before_render);
|
|
assert!(output.contains("PREVIEW"));
|
|
assert!(output.contains("LOADING PREVIEW"));
|
|
assert!(output.contains("F3 / Esc / q Close"));
|
|
|
|
assert!(app.reduce(Command::CloseOverlay).is_empty());
|
|
assert_eq!(app.overlay, Overlay::None);
|
|
assert_eq!(app.mode, PaneMode::Dual);
|
|
assert_eq!(app.active, PaneId::Right);
|
|
assert_eq!(
|
|
[app.panes[0].location.clone(), app.panes[1].location.clone()],
|
|
locations
|
|
);
|
|
assert_eq!([app.panes[0].selection, app.panes[1].selection], selections);
|
|
}
|
|
|
|
#[test]
|
|
fn preview_overlay_also_works_over_single_without_changing_state() {
|
|
let mut app = sample_app(PaneMode::Single);
|
|
app.overlay = Overlay::Preview;
|
|
let before = app.clone();
|
|
let output = draw(&app, None, 120, 40);
|
|
assert_eq!(app, before);
|
|
assert!(output.contains("PREVIEW"));
|
|
assert!(output.contains("F3 opens this temporary layer"));
|
|
}
|
|
|
|
#[test]
|
|
fn snapshot_help_120x40() {
|
|
let mut app = sample_app(PaneMode::Dual);
|
|
app.overlay = Overlay::Help;
|
|
insta::assert_snapshot!(draw(&app, None, 120, 40));
|
|
}
|
|
|
|
#[test]
|
|
fn snapshot_host_picker_query_and_warning_120x40() {
|
|
let mut app = sample_app(PaneMode::Single);
|
|
app.overlay = Overlay::HostPicker;
|
|
let mut picker = HostPickerState::from_strings(
|
|
vec![
|
|
"edge-prod".to_owned(),
|
|
"edge-stage".to_owned(),
|
|
"builder".to_owned(),
|
|
],
|
|
vec!["Include /broken/config was ignored".to_owned()],
|
|
);
|
|
for character in "edge".chars() {
|
|
picker.push_char(character);
|
|
}
|
|
insta::assert_snapshot!(draw(&app, Some(&picker), 120, 40));
|
|
}
|
|
|
|
#[test]
|
|
fn snapshot_f3_preview_over_dual_160x40() {
|
|
let mut app = sample_app(PaneMode::Dual);
|
|
app.active = PaneId::Right;
|
|
app.overlay = Overlay::Preview;
|
|
insta::assert_snapshot!(draw(&app, None, 160, 40));
|
|
}
|