rcom/tests/ui_layout.rs

286 lines
8.6 KiB
Rust

use std::time::{Duration, UNIX_EPOCH};
use pretty_assertions::assert_eq;
use proptest::prelude::*;
use ratatui::Terminal;
use ratatui::backend::TestBackend;
use ratatui::layout::Rect;
use rcom::app::{AppState, Overlay, PaneId, PaneMode};
use rcom::domain::{Entry, EntryKind, HostAlias, Location};
use rcom::preview::{Preview, PreviewKind};
use rcom::ssh::HostPickerState;
use rcom::ui::layout::{ColumnRole, columns, frame_layout};
use rcom::ui::{Theme, render};
fn sample_app(mode: PaneMode) -> AppState {
let mut app = AppState::new(Location::local("/workspace/project"));
let modified = UNIX_EPOCH + Duration::from_secs(1_700_000_000);
app.panes[0].entries = vec![
Entry::new("docs", EntryKind::Directory).with_metadata(Some(4096), Some(modified), None),
Entry::new("README.md", EntryKind::File).with_metadata(Some(12_345), Some(modified), None),
Entry::new("データー-αβγ.txt", EntryKind::File).with_metadata(
Some(768),
Some(modified),
None,
),
Entry::new("latest", EntryKind::Symlink).with_metadata(None, Some(modified), None),
];
app.panes[0]
.selection
.set(Some(1), app.panes[0].entries.len());
app.panes[0].preview.content = Some(Preview {
kind: PreviewKind::Text,
lines: vec![
"1 │ # rcom".to_owned(),
"2 │ Dual-pane local and SSH file manager".to_owned(),
"3 │ Safe, bounded text preview.".to_owned(),
],
truncated: false,
});
app.panes[1] = app.panes[0].clone();
app.panes[1].location = Location::ssh(HostAlias::new("edge-prod"), "/srv/releases");
app.panes[1]
.selection
.set(Some(0), app.panes[1].entries.len());
app.mode = mode;
app
}
fn draw(
app: &AppState,
picker: Option<&HostPickerState>,
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, picker, &theme))
.unwrap();
buffer_text(terminal.backend())
}
fn buffer_text(backend: &TestBackend) -> String {
let buffer = 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 single_miller_columns_are_20_40_40_and_dual_is_exactly_half() {
let single = columns(Rect::new(0, 0, 150, 30), PaneMode::Single, PaneId::Left);
assert_eq!(
single
.iter()
.map(|column| column.area.width)
.collect::<Vec<_>>(),
[30, 60, 60]
);
assert_eq!(
single.iter().map(|column| column.role).collect::<Vec<_>>(),
[
ColumnRole::Parent,
ColumnRole::Pane(PaneId::Left),
ColumnRole::Preview
]
);
let medium = columns(Rect::new(0, 0, 149, 30), PaneMode::Single, PaneId::Right);
assert_eq!(
medium
.iter()
.map(|column| column.area.width)
.collect::<Vec<_>>(),
[67, 82]
);
assert_eq!(medium[0].role, ColumnRole::Pane(PaneId::Right));
let preview_start = columns(Rect::new(0, 0, 100, 30), PaneMode::Single, PaneId::Left);
assert_eq!(
preview_start
.iter()
.map(|column| column.area.width)
.collect::<Vec<_>>(),
[45, 55]
);
let list_only = columns(Rect::new(0, 0, 99, 30), PaneMode::Single, PaneId::Left);
assert_eq!(list_only.len(), 1);
assert_eq!(list_only[0].area.width, 99);
for width in [72, 99, 100, 149, 150, 151] {
let dual = columns(Rect::new(0, 0, width, 30), PaneMode::Dual, PaneId::Right);
assert_eq!(dual.len(), 2);
assert_eq!(dual[0].area.width + dual[1].area.width, width);
assert_eq!(dual[0].area.width, width / 2);
assert!(
dual.iter()
.all(|column| !matches!(column.role, ColumnRole::Preview))
);
}
}
#[test]
fn narrow_dual_shows_only_the_active_pane_with_a_non_color_indicator() {
let mut app = sample_app(PaneMode::Dual);
app.active = PaneId::Right;
let output = draw(&app, None, 71, 24, Theme::no_color());
assert!(output.contains("PANE 2/2"));
assert!(output.contains("ACTIVE"));
assert!(output.contains("SSH edge-prod"));
assert!(!output.contains("LOCAL · /workspace/project"));
assert!(!output.contains("PREVIEW"));
}
#[test]
fn small_sizes_show_a_warning_instead_of_a_damaged_workspace() {
for (width, height) in [(24, 40), (39, 24), (40, 7), (100, 0)] {
let output = draw(
&sample_app(PaneMode::Dual),
None,
width,
height,
Theme::default(),
);
if width > 0 && height > 0 {
assert!(
output.contains("TERMINAL TOO SMALL"),
"{width}x{height}: {output}"
);
}
assert!(!output.contains("README.md"));
}
}
#[test]
fn hints_row_is_gone_and_help_affordance_never_consumes_status_or_workspace() {
for mode in [PaneMode::Single, PaneMode::Dual] {
for (width, height) in [(40, 8), (40, 10), (80, 24), (120, 40)] {
let area = Rect::new(0, 0, width, height);
let layout = frame_layout(area, mode, PaneId::Left);
assert!(!layout.too_small);
assert_eq!(layout.workspace.height, height - 2);
assert_eq!(layout.status.unwrap().y, height - 1);
let output = draw(&sample_app(mode), None, width, height, Theme::no_color());
assert!(
output.lines().next().unwrap().contains("? Help"),
"{output}"
);
assert!(!output.contains("Space Select · y/x/p Yank/Cut/Paste"));
assert!(output.contains("Dotfiles hidden"), "{output}");
}
}
}
#[test]
fn unicode_interface_does_not_require_nerd_font_private_use_glyphs() {
let output = draw(
&sample_app(PaneMode::Single),
None,
160,
40,
Theme::default(),
);
assert!(
!output
.chars()
.any(|character| ('\u{e000}'..='\u{f8ff}').contains(&character))
);
assert!(output.contains("LOCAL"));
assert!(output.contains("ACTIVE"));
assert!(output.contains("NAME"));
assert!(output.contains("SIZE"));
assert!(output.contains("MODIFIED"));
}
#[test]
fn snapshot_single_miller_160x40() {
insta::assert_snapshot!(draw(
&sample_app(PaneMode::Single),
None,
160,
40,
Theme::default()
));
}
#[test]
fn snapshot_single_preview_120x40() {
insta::assert_snapshot!(draw(
&sample_app(PaneMode::Single),
None,
120,
40,
Theme::default()
));
}
#[test]
fn snapshot_single_list_80x24() {
insta::assert_snapshot!(draw(
&sample_app(PaneMode::Single),
None,
80,
24,
Theme::default()
));
}
#[test]
fn snapshot_single_compact_40x10() {
insta::assert_snapshot!(draw(
&sample_app(PaneMode::Single),
None,
40,
10,
Theme::default()
));
}
#[test]
fn snapshot_dual_160x40_has_no_persistent_preview() {
let output = draw(&sample_app(PaneMode::Dual), None, 160, 40, Theme::default());
assert!(!output.contains("PREVIEW"));
insta::assert_snapshot!(output);
}
proptest! {
#[test]
fn responsive_breakpoints_and_overlays_never_panic(
width_index in 0usize..8,
height_index in 0usize..8,
dual in any::<bool>(),
right_active in any::<bool>(),
overlay_index in 0usize..4,
) {
const WIDTHS: [u16; 8] = [24, 40, 71, 72, 99, 100, 149, 150];
const HEIGHTS: [u16; 8] = [0, 1, 7, 8, 9, 10, 24, 40];
let mut app = sample_app(if dual { PaneMode::Dual } else { PaneMode::Single });
app.active = if right_active { PaneId::Right } else { PaneId::Left };
app.overlay = match overlay_index {
1 => Overlay::Help,
2 => Overlay::HostPicker,
3 => Overlay::Preview,
_ => Overlay::None,
};
let picker = HostPickerState::from_strings(
vec!["edge".to_owned(), "builder".to_owned()],
vec!["ignored broken include".to_owned()],
);
let output = draw(
&app,
Some(&picker),
WIDTHS[width_index],
HEIGHTS[height_index],
Theme::default(),
);
prop_assert!(output.len() < 2_000_000);
}
}