107 lines
3.7 KiB
Rust
107 lines
3.7 KiB
Rust
use ratatui::{Terminal, backend::TestBackend, buffer::Buffer};
|
|
use rcom::app::{AppState, Command, Overlay};
|
|
use rcom::domain::{Entry, EntryKind, Location};
|
|
use rcom::ui::{IconMode, Theme, render};
|
|
|
|
fn app() -> AppState {
|
|
let mut app = AppState::new(Location::local("/work"));
|
|
app.active_pane_mut().entries = ["alpha", "beta", "gamma"]
|
|
.into_iter()
|
|
.map(|name| Entry::new(name, EntryKind::File))
|
|
.collect();
|
|
app.active_pane_mut().selection.set(Some(0), 3);
|
|
app
|
|
}
|
|
|
|
fn buffer(app: &AppState, width: u16, height: u16, theme: &Theme) -> Buffer {
|
|
let mut terminal = Terminal::new(TestBackend::new(width, height)).unwrap();
|
|
terminal
|
|
.draw(|frame| render(frame, app, None, theme))
|
|
.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")
|
|
}
|
|
|
|
#[test]
|
|
fn filter_editor_and_applied_status_fit_every_supported_layout() {
|
|
for (width, height) in [(40, 8), (80, 24), (120, 40)] {
|
|
let mut app = app();
|
|
app.reduce(Command::OpenFilter);
|
|
for character in "alpha".chars() {
|
|
app.reduce(Command::FilterInput(character));
|
|
}
|
|
let output = text(&buffer(&app, width, height, &Theme::default()));
|
|
assert!(
|
|
output.contains("FILTER> alpha"),
|
|
"{width}x{height}\n{output}"
|
|
);
|
|
assert!(output.contains("Enter Apply"), "{output}");
|
|
assert!(output.contains("Esc Cancel"), "{output}");
|
|
assert!(output.contains("alpha"), "{output}");
|
|
app.reduce(Command::ApplyFilter);
|
|
assert_eq!(app.overlay, Overlay::None);
|
|
let applied = text(&buffer(&app, width, height, &Theme::default()));
|
|
assert!(applied.contains("FILTER alpha"), "{applied}");
|
|
assert!(applied.contains("1/3"), "{applied}");
|
|
assert!(!applied.contains("Enter Apply"), "{applied}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn no_matches_and_hidden_marked_are_textual_in_monochrome_and_icons_off() {
|
|
let mut app = app();
|
|
app.reduce(Command::SelectAll);
|
|
app.reduce(Command::OpenFilter);
|
|
for _ in 0..30 {
|
|
app.reduce(Command::FilterInput('ж'));
|
|
}
|
|
app.reduce(Command::ApplyFilter);
|
|
for theme in [
|
|
Theme::no_color(),
|
|
Theme::ascii().with_icon_mode(IconMode::None),
|
|
] {
|
|
let output = text(&buffer(&app, 40, 8, &theme));
|
|
assert!(output.contains("NO MATCHES"), "{output}");
|
|
assert!(output.contains("0/3"), "{output}");
|
|
assert!(output.contains("Hidden marked 3"), "{output}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn cancel_restores_workspace_geometry_and_unicode_input_is_sanitized() {
|
|
let mut app = app();
|
|
let normal = rcom::ui::layout::frame_layout(
|
|
ratatui::layout::Rect::new(0, 0, 40, 8),
|
|
app.mode,
|
|
app.active,
|
|
);
|
|
app.reduce(Command::OpenFilter);
|
|
app.reduce(Command::FilterInput('\u{2028}'));
|
|
let editing = rcom::ui::layout::frame_layout_with_filter(
|
|
ratatui::layout::Rect::new(0, 0, 40, 8),
|
|
app.mode,
|
|
app.active,
|
|
true,
|
|
);
|
|
assert_eq!(normal.workspace.height, editing.workspace.height + 2);
|
|
let output = text(&buffer(&app, 40, 8, &Theme::default()));
|
|
assert!(!output.contains('\u{2028}'));
|
|
app.reduce(Command::CloseOverlay);
|
|
let restored = rcom::ui::layout::frame_layout_with_filter(
|
|
ratatui::layout::Rect::new(0, 0, 40, 8),
|
|
app.mode,
|
|
app.active,
|
|
app.overlay == Overlay::Filter,
|
|
);
|
|
assert_eq!(normal.workspace, restored.workspace);
|
|
}
|