438 lines
15 KiB
Rust
438 lines
15 KiB
Rust
use std::collections::BTreeMap;
|
|
|
|
use crossterm::event::{KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers};
|
|
use pretty_assertions::assert_eq;
|
|
use ratatui::Terminal;
|
|
use ratatui::backend::TestBackend;
|
|
use ratatui::layout::Rect;
|
|
use rcom::app::{AppState, Command, HelpCategory, HelpState, Overlay, PaneMode, help_entries};
|
|
use rcom::domain::{Entry, EntryKind, Location};
|
|
use rcom::fs::FsError;
|
|
use rcom::input::{InputOutcome, InputState};
|
|
use rcom::ui::help::help_view_model;
|
|
use rcom::ui::text::wrap;
|
|
use rcom::ui::{IconMode, Theme, render};
|
|
|
|
fn sample_app() -> AppState {
|
|
let mut app = AppState::new(Location::local("/workspace/project"));
|
|
app.mode = PaneMode::Dual;
|
|
app.active_pane_mut().entries = vec![
|
|
Entry::new("docs", EntryKind::Directory),
|
|
Entry::new("README.md", EntryKind::File),
|
|
];
|
|
app.active_pane_mut().selection.set(Some(1), 2);
|
|
app
|
|
}
|
|
|
|
fn press(code: KeyCode) -> KeyEvent {
|
|
KeyEvent {
|
|
code,
|
|
modifiers: KeyModifiers::NONE,
|
|
kind: KeyEventKind::Press,
|
|
state: KeyEventState::NONE,
|
|
}
|
|
}
|
|
|
|
fn draw(app: &AppState, width: u16, height: u16) -> String {
|
|
let backend = TestBackend::new(width, height);
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
terminal
|
|
.draw(|frame| render(frame, app, None, &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")
|
|
}
|
|
|
|
fn dispatch(outcome: InputOutcome, app: &mut AppState) {
|
|
if let InputOutcome::Command(command) = outcome {
|
|
assert!(app.reduce(command).is_empty());
|
|
}
|
|
}
|
|
|
|
fn help_key(app: &mut AppState, input: &mut InputState, key: KeyEvent, width: u16, height: u16) {
|
|
let model = help_view_model(Rect::new(0, 0, width, height), &app.help);
|
|
dispatch(input.handle_help_key(key, model.viewport()), app);
|
|
}
|
|
|
|
fn draw_help_body(app: &AppState, width: u16, height: u16) -> String {
|
|
let model = help_view_model(Rect::new(0, 0, width, height), &app.help);
|
|
let backend = TestBackend::new(width, height);
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
terminal
|
|
.draw(|frame| render(frame, app, None, &Theme::default()))
|
|
.unwrap();
|
|
let buffer = terminal.backend().buffer();
|
|
(model.body.y..model.body.y + model.body.height)
|
|
.map(|y| {
|
|
(model.body.x..model.body.x + model.body.width)
|
|
.map(|x| buffer.cell((x, y)).unwrap().symbol())
|
|
.collect::<String>()
|
|
})
|
|
.collect::<Vec<_>>()
|
|
.join("\n")
|
|
}
|
|
|
|
fn compact(input: &str) -> String {
|
|
input
|
|
.chars()
|
|
.filter(|character| !character.is_whitespace())
|
|
.collect()
|
|
}
|
|
|
|
#[test]
|
|
fn intermediate_help_footer_uses_measured_compact_controls_without_truncation() {
|
|
for theme in [Theme::default(), Theme::ascii()] {
|
|
let mut app = sample_app();
|
|
app.reduce(Command::ToggleHelp);
|
|
let model = help_view_model(Rect::new(0, 0, 62, 24), &app.help);
|
|
for expected in ["1-11/11", "h/l", "j/k/Pg", "Esc/q/?/F1"] {
|
|
assert!(
|
|
model.footer_text.contains(expected),
|
|
"{}",
|
|
model.footer_text
|
|
);
|
|
}
|
|
assert!(!model.footer_text.contains('…'));
|
|
let mut terminal = Terminal::new(TestBackend::new(62, 24)).unwrap();
|
|
terminal
|
|
.draw(|frame| render(frame, &app, None, &theme))
|
|
.unwrap();
|
|
let buffer = terminal.backend().buffer();
|
|
let footer_area = model.footer.unwrap();
|
|
let footer = (footer_area.x..footer_area.x + footer_area.width)
|
|
.map(|x| buffer.cell((x, footer_area.y)).unwrap().symbol())
|
|
.collect::<String>();
|
|
assert!(footer.contains("j/k/Pg"), "{footer}");
|
|
assert!(footer.contains("Esc/q/?/F1"), "{footer}");
|
|
assert!(!footer.contains('…'), "{footer}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn every_help_category_alias_and_context_is_reachable_at_wide_and_minimum_sizes() {
|
|
for (width, height) in [(40, 8), (40, 10), (80, 24), (120, 40)] {
|
|
let before = sample_app();
|
|
let mut app = before.clone();
|
|
let mut input = InputState::new();
|
|
assert!(app.reduce(Command::ToggleHelp).is_empty());
|
|
|
|
for category in HelpCategory::ALL {
|
|
assert_eq!(app.help.category, category);
|
|
assert_eq!(app.help.scroll, 0);
|
|
let initial = draw(&app, width, height);
|
|
if width == 40 {
|
|
assert!(
|
|
initial.contains(&format!(
|
|
"< {}/6 {} >",
|
|
category.index() + 1,
|
|
category.label()
|
|
)),
|
|
"{width}x{height}\n{initial}"
|
|
);
|
|
} else {
|
|
assert!(initial.contains(&format!("[{}]", category.label())));
|
|
}
|
|
assert!(initial.contains("Esc/q/?/F1"), "{initial}");
|
|
assert!(!initial.contains("yazi"));
|
|
assert!(!initial.contains("mc"));
|
|
|
|
let mut bodies = BTreeMap::new();
|
|
loop {
|
|
let model = help_view_model(Rect::new(0, 0, width, height), &app.help);
|
|
assert_eq!(model.offset, app.help.scroll);
|
|
assert!(model.viewport().page_size > 0);
|
|
assert!(model.footer_text.len() <= usize::from(model.inner.width));
|
|
for (row, line) in draw_help_body(&app, width, height).lines().enumerate() {
|
|
bodies.insert(model.offset + row, line.to_owned());
|
|
}
|
|
if app.help.scroll == model.max_scroll {
|
|
break;
|
|
}
|
|
help_key(
|
|
&mut app,
|
|
&mut input,
|
|
press(KeyCode::PageDown),
|
|
width,
|
|
height,
|
|
);
|
|
assert!(app.help.scroll <= model.max_scroll);
|
|
}
|
|
let all_body = compact(
|
|
&bodies
|
|
.values()
|
|
.map(String::as_str)
|
|
.collect::<Vec<_>>()
|
|
.join("\n"),
|
|
);
|
|
for entry in help_entries(category) {
|
|
let expected = compact(&format!(
|
|
"{}{}: {}",
|
|
entry.aliases,
|
|
entry.context.label(),
|
|
entry.description
|
|
));
|
|
assert!(
|
|
all_body.contains(&expected),
|
|
"missing {} ({expected}) at {width}x{height}\n{}",
|
|
entry.id,
|
|
bodies
|
|
.values()
|
|
.map(String::as_str)
|
|
.collect::<Vec<_>>()
|
|
.join("\n")
|
|
);
|
|
}
|
|
|
|
let max_scroll = help_view_model(Rect::new(0, 0, width, height), &app.help).max_scroll;
|
|
for _ in 0..3 {
|
|
help_key(
|
|
&mut app,
|
|
&mut input,
|
|
press(KeyCode::PageDown),
|
|
width,
|
|
height,
|
|
);
|
|
assert_eq!(app.help.scroll, max_scroll);
|
|
}
|
|
if max_scroll > 0 {
|
|
help_key(
|
|
&mut app,
|
|
&mut input,
|
|
press(KeyCode::Char('k')),
|
|
width,
|
|
height,
|
|
);
|
|
assert_eq!(app.help.scroll, max_scroll - 1);
|
|
}
|
|
help_key(
|
|
&mut app,
|
|
&mut input,
|
|
press(KeyCode::Char('l')),
|
|
width,
|
|
height,
|
|
);
|
|
assert_eq!(app.help.scroll, 0);
|
|
}
|
|
assert_eq!(app.help.category, HelpCategory::Navigation);
|
|
assert_eq!(app.panes, before.panes);
|
|
help_key(&mut app, &mut input, press(KeyCode::Esc), width, height);
|
|
assert_eq!(app, before);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn help_category_aliases_wrap_reset_scroll_and_preserve_the_workspace() {
|
|
let previous = [
|
|
press(KeyCode::Char('h')),
|
|
press(KeyCode::Left),
|
|
KeyEvent::new(KeyCode::BackTab, KeyModifiers::SHIFT),
|
|
];
|
|
let next = [
|
|
press(KeyCode::Char('l')),
|
|
press(KeyCode::Right),
|
|
press(KeyCode::Tab),
|
|
];
|
|
for (keys, initial, expected) in [
|
|
(previous, HelpCategory::Navigation, HelpCategory::General),
|
|
(next, HelpCategory::General, HelpCategory::Navigation),
|
|
] {
|
|
for key in keys {
|
|
let before = sample_app();
|
|
let mut app = before.clone();
|
|
let mut input = InputState::new();
|
|
app.reduce(Command::ToggleHelp);
|
|
app.reduce(Command::SelectHelpCategory(
|
|
initial.index() as isize - HelpCategory::Navigation.index() as isize,
|
|
));
|
|
app.help.scroll = 7;
|
|
help_key(&mut app, &mut input, key, 40, 8);
|
|
assert_eq!(app.help.category, expected);
|
|
assert_eq!(app.help.scroll, 0);
|
|
assert_eq!(app.panes, before.panes);
|
|
assert_eq!(app.clipboard, before.clipboard);
|
|
assert_eq!(app.mode, before.mode);
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn help_close_aliases_and_f10_keep_workspace_state() {
|
|
for key in [
|
|
press(KeyCode::Esc),
|
|
press(KeyCode::Char('q')),
|
|
press(KeyCode::Char('?')),
|
|
press(KeyCode::F(1)),
|
|
] {
|
|
let before = sample_app();
|
|
let mut app = before.clone();
|
|
let mut input = InputState::new();
|
|
app.reduce(Command::ToggleHelp);
|
|
app.reduce(Command::SelectHelpCategory(3));
|
|
app.help.scroll = 4;
|
|
help_key(&mut app, &mut input, key, 80, 24);
|
|
assert_eq!(app, before);
|
|
}
|
|
|
|
let before = sample_app();
|
|
let mut app = before.clone();
|
|
let mut input = InputState::new();
|
|
app.reduce(Command::ToggleHelp);
|
|
help_key(&mut app, &mut input, press(KeyCode::F(10)), 80, 24);
|
|
assert!(app.should_quit);
|
|
assert_eq!(app.panes, before.panes);
|
|
assert_eq!(app.mode, before.mode);
|
|
assert_eq!(app.clipboard, before.clipboard);
|
|
}
|
|
|
|
#[test]
|
|
fn help_scroll_is_normalized_on_resize_without_render_order_dependency() {
|
|
for category in HelpCategory::ALL {
|
|
for (width, height) in [(40, 8), (80, 24), (120, 40)] {
|
|
let area = Rect::new(0, 0, width, height);
|
|
let max_scroll = help_view_model(
|
|
area,
|
|
&HelpState {
|
|
category,
|
|
scroll: 0,
|
|
},
|
|
)
|
|
.max_scroll;
|
|
for raw in [0, max_scroll / 2, max_scroll, usize::MAX] {
|
|
let mut app = sample_app();
|
|
app.reduce(Command::ToggleHelp);
|
|
app.help = HelpState {
|
|
category,
|
|
scroll: raw,
|
|
};
|
|
let mut rendered_first = app.clone();
|
|
let _ = draw(&rendered_first, width, height);
|
|
app.reduce(Command::NormalizeHelpScroll(max_scroll));
|
|
rendered_first.reduce(Command::NormalizeHelpScroll(max_scroll));
|
|
assert_eq!(app, rendered_first);
|
|
assert_eq!(app.help.scroll, raw.min(max_scroll));
|
|
assert_eq!(help_view_model(area, &app.help).offset, app.help.scroll);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn help_monochrome_ascii_retains_non_color_category_and_reachability() {
|
|
let mut app = sample_app();
|
|
app.theme = Theme::with_capabilities(false, true).with_icon_mode(IconMode::None);
|
|
app.reduce(Command::ToggleHelp);
|
|
let output = {
|
|
let backend = TestBackend::new(40, 8);
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
terminal
|
|
.draw(|frame| render(frame, &app, None, &app.theme))
|
|
.unwrap();
|
|
let buffer = terminal.backend().buffer();
|
|
(0..8)
|
|
.map(|y| {
|
|
(0..40)
|
|
.map(|x| buffer.cell((x, y)).unwrap().symbol())
|
|
.collect::<String>()
|
|
})
|
|
.collect::<Vec<_>>()
|
|
.join("\n")
|
|
};
|
|
assert!(output.contains("< 1/6 Navigation >"));
|
|
assert!(output.contains("1-4/"));
|
|
assert!(
|
|
!output
|
|
.chars()
|
|
.any(|character| { ('\u{e000}'..='\u{f8ff}').contains(&character) })
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn long_unicode_error_is_fully_scrollable_and_escape_restores_the_workspace() {
|
|
let detail = format!(
|
|
"{}終了🦀",
|
|
(0..30)
|
|
.map(|index| format!("χ{index:02}-"))
|
|
.collect::<String>()
|
|
);
|
|
let mut app = sample_app();
|
|
app.active_pane_mut().error = Some(FsError::Other(detail));
|
|
let before = app.clone();
|
|
let mut input = InputState::new();
|
|
|
|
assert!(app.reduce(Command::OpenMessage).is_empty());
|
|
assert_eq!(app.overlay, Overlay::Message);
|
|
assert_eq!(app.overlay_scroll, 0);
|
|
let initial = draw(&app, 40, 8);
|
|
assert!(initial.contains("ERROR DETAILS"));
|
|
assert!(initial.contains("TARGET"));
|
|
assert!(initial.contains("LOCAL · /workspace/project"));
|
|
assert!(initial.contains("MESSAGE"));
|
|
assert!(!initial.contains("終了"));
|
|
|
|
let mut frames = vec![initial];
|
|
for _ in 0..50 {
|
|
dispatch(
|
|
input.handle_message_key(press(KeyCode::PageDown), 3),
|
|
&mut app,
|
|
);
|
|
frames.push(draw(&app, 40, 8));
|
|
}
|
|
let expected_message = before.active_pane().error.as_ref().unwrap().user_message();
|
|
let wrapped = wrap(&expected_message, 38);
|
|
assert_eq!(wrapped.concat(), expected_message);
|
|
let compact_frames = frames
|
|
.iter()
|
|
.map(|frame| frame.replace(' ', ""))
|
|
.collect::<Vec<_>>();
|
|
for part in wrapped {
|
|
let part = part.trim_end().replace(' ', "");
|
|
assert!(
|
|
compact_frames.iter().any(|frame| frame.contains(&part)),
|
|
"message part was not rendered: {part:?}"
|
|
);
|
|
}
|
|
assert!(compact_frames.last().unwrap().contains("終了🦀"));
|
|
assert_eq!(app.panes, before.panes);
|
|
|
|
let render_before = app.clone();
|
|
let _ = draw(&app, 40, 10);
|
|
assert_eq!(app, render_before);
|
|
dispatch(input.handle_message_key(press(KeyCode::Esc), 3), &mut app);
|
|
assert_eq!(app, before);
|
|
}
|
|
|
|
#[test]
|
|
fn message_command_is_unavailable_without_an_active_pane_error() {
|
|
let mut app = sample_app();
|
|
let before = app.clone();
|
|
assert!(app.reduce(Command::OpenMessage).is_empty());
|
|
assert_eq!(app, before);
|
|
}
|
|
|
|
#[test]
|
|
fn snapshot_help_scrolled_40x8() {
|
|
let mut app = sample_app();
|
|
app.reduce(Command::ToggleHelp);
|
|
app.reduce(Command::SelectHelpCategory(2));
|
|
app.help.scroll = 11;
|
|
insta::assert_snapshot!(draw(&app, 40, 8));
|
|
}
|
|
|
|
#[test]
|
|
fn snapshot_message_details_40x10() {
|
|
let mut app = sample_app();
|
|
app.active_pane_mut().error = Some(FsError::Other(
|
|
"A detailed Unicode failure: 開始 🦀 — the complete message remains available and scrollable at every viewport".into(),
|
|
));
|
|
assert!(app.reduce(Command::OpenMessage).is_empty());
|
|
app.reduce(Command::ScrollOverlay(2));
|
|
insta::assert_snapshot!(draw(&app, 40, 10));
|
|
}
|