rcom/tests/ui_hardening.rs

225 lines
7.4 KiB
Rust

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use pretty_assertions::assert_eq;
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use ratatui::layout::Rect;
use rcom::app::{AppState, Command, Overlay, PaneId, PaneMode};
use rcom::domain::{Entry, EntryKind, HostAlias, Location};
use rcom::input::InputState;
use rcom::preview::{Preview, PreviewKind, TRUNCATION_MARKER};
use rcom::ui::layout::active_page_size;
use rcom::ui::{Theme, render};
fn draw(app: &AppState, width: u16, height: u16) -> String {
draw_with_theme(app, width, height, Theme::default())
}
fn draw_with_theme(app: &AppState, width: u16, height: u16, theme: Theme) -> String {
let backend = TestBackend::new(width, height);
let mut terminal = Terminal::new(backend).unwrap();
terminal
.draw(|frame| render(frame, app, None, &theme))
.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 preview_app(kind: PreviewKind) -> AppState {
let mut app = AppState::new(Location::local("/workspace/project"));
app.active_pane_mut().entries = vec![Entry::new("item", EntryKind::File)];
app.active_pane_mut().selection.set(Some(0), 1);
let mut lines: Vec<String> = (0..80)
.map(|index| format!("preview line {index}"))
.collect();
lines.push(TRUNCATION_MARKER.to_owned());
app.active_pane_mut().preview.content = Some(Preview {
kind,
lines,
truncated: true,
directory_entries: Vec::new(),
});
app
}
#[test]
fn text_and_directory_truncation_markers_stay_on_the_last_visible_preview_row() {
for kind in [PreviewKind::Text, PreviewKind::Directory] {
for overlay in [false, true] {
let mut app = preview_app(kind);
app.overlay = if overlay {
Overlay::Preview
} else {
Overlay::None
};
let output = draw(&app, 120, 12);
let rows: Vec<&str> = output.lines().collect();
let marker_row = if overlay { 10 } else { 9 };
assert!(
rows[marker_row].contains(TRUNCATION_MARKER),
"kind={kind:?} overlay={overlay}: marker is not on row {marker_row}\n{output}"
);
assert_eq!(output.matches(TRUNCATION_MARKER).count(), 1);
assert!(!output.contains("preview line 79"));
}
}
}
#[test]
fn page_and_half_page_steps_come_from_the_active_panes_visible_rows() {
for mode in [PaneMode::Single, PaneMode::Dual] {
for (width, height, page_size) in [(120, 40, 35), (80, 24, 19), (40, 10, 5)] {
let area = Rect::new(0, 0, width, height);
let active = if width == 40 {
PaneId::Right
} else {
PaneId::Left
};
assert_eq!(active_page_size(area, mode, active), page_size);
let page = InputState::new()
.handle_key(
KeyEvent::new(KeyCode::PageDown, KeyModifiers::NONE),
page_size,
)
.into_command()
.unwrap();
assert_eq!(
page,
Command::PageSelection {
page_size,
down: true,
}
);
let half = InputState::new()
.handle_key(
KeyEvent::new(KeyCode::Char('d'), KeyModifiers::CONTROL),
page_size,
)
.into_command()
.unwrap();
assert_eq!(
half,
Command::PageSelection {
page_size: page_size / 2,
down: true,
}
);
}
}
}
fn dual_app() -> AppState {
let mut app = AppState::new(Location::local(
"/workspace/project/with/a/very/long/path/that/needs/truncation",
));
app.mode = PaneMode::Dual;
app.active_pane_mut().entries = vec![Entry::new("local.txt", EntryKind::File)];
app.active_pane_mut().selection.set(Some(0), 1);
app.panes[1] = app.panes[0].clone();
app.panes[1].location = Location::ssh(
HostAlias::new("edge-prod"),
"/srv/releases/with/a/very/long/path/that/needs/truncation",
);
app.panes[1].entries = vec![Entry::new("stale.txt", EntryKind::File)];
app.panes[1].selection.set(Some(0), 1);
app
}
#[test]
fn medium_dual_titles_preserve_state_source_and_only_truncate_paths() {
for width in [72, 80, 99] {
let output = draw(&dual_app(), width, 24);
let titles = output.lines().nth(1).unwrap();
for expected in [
"ACTIVE",
"PANE 1/2",
"PANE 2/2",
"LOCAL",
"SSH edge-prod",
"/w",
"/s",
] {
assert!(
titles.contains(expected),
"{width}: missing {expected:?} in {titles:?}"
);
}
assert_eq!(titles.matches('…').count(), 2, "{width}: {titles}");
}
}
#[test]
fn active_ssh_dual_title_keeps_source_and_state_on_medium_widths() {
for width in [72, 80, 99] {
let mut app = dual_app();
app.active = PaneId::Right;
let output = draw(&app, width, 24);
let titles = output.lines().nth(1).unwrap();
for expected in ["ACTIVE", "2/2", "SSH edge-prod", "/s"] {
assert!(
titles.contains(expected),
"{width}: missing {expected:?} in {titles:?}"
);
}
}
let mut app = dual_app();
app.active = PaneId::Right;
app.panes[1].location = Location::ssh(
HostAlias::new("edge-prod-eu-central-with-a-very-long-alias"),
"/srv/releases",
);
let titles = draw(&app, 72, 24).lines().nth(1).unwrap().to_owned();
assert!(titles.contains("ACTIVE"), "{titles}");
assert!(titles.contains("SSH"), "{titles}");
assert!(titles.contains('…'), "{titles}");
}
#[test]
fn active_ssh_compact_title_respects_ascii_fallback() {
let mut app = dual_app();
app.active = PaneId::Right;
let titles = draw_with_theme(&app, 72, 24, Theme::ascii())
.lines()
.nth(1)
.unwrap()
.to_owned();
assert!(titles.contains("ACTIVE"), "{titles}");
assert!(titles.contains("2/2"), "{titles}");
assert!(titles.contains("SSH edge-prod"), "{titles}");
assert!(titles.contains("/s"), "{titles}");
assert!(!titles.contains('·'), "{titles}");
}
#[test]
fn delayed_inactive_dual_pane_shows_loading_with_stale_entries_and_ssh_connect_target() {
let mut app = dual_app();
app.panes[1].loading = true;
app.panes[1].pending_target = Some(Location::ssh(HostAlias::new("edge-prod"), "."));
let stale = draw(&app, 80, 24);
assert!(stale.contains("PANE 2/2"));
assert!(stale.contains("SSH edge-prod"));
assert!(stale.contains("◌ LOADING"), "{stale}");
assert!(stale.contains("stale.txt"));
assert_eq!(stale.matches("◌ LOADING").count(), 1);
app.panes[1].entries.clear();
app.panes[1].selection.set(None, 0);
let connecting = draw(&app, 80, 24);
assert!(connecting.contains("Connect SSH edge-prod"), "{connecting}");
assert!(!connecting.contains("Reading ."));
}