230 lines
8.4 KiB
Rust
230 lines
8.4 KiB
Rust
use std::time::{Duration, UNIX_EPOCH};
|
|
|
|
use ratatui::Terminal;
|
|
use ratatui::backend::TestBackend;
|
|
use ratatui::style::{Color, Modifier};
|
|
use rcom::app::{AppState, PaneMode};
|
|
use rcom::domain::{Entry, EntryKind, Location};
|
|
use rcom::fs::FsError;
|
|
use rcom::preview::{Preview, PreviewKind};
|
|
use rcom::ui::{Theme, render};
|
|
|
|
fn app_with_entries() -> AppState {
|
|
let mut app = AppState::new(Location::local("/workspace/project"));
|
|
app.panes[0].entries = vec![
|
|
Entry::new("docs", EntryKind::Directory).with_metadata(
|
|
Some(4096),
|
|
Some(UNIX_EPOCH + Duration::from_secs(1_700_000_000)),
|
|
None,
|
|
),
|
|
Entry::new("README.md", EntryKind::File).with_metadata(
|
|
Some(12_345),
|
|
Some(UNIX_EPOCH + Duration::from_secs(1_700_000_000)),
|
|
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 │ Read-only workspace".to_owned(),
|
|
],
|
|
truncated: false,
|
|
directory_entries: Vec::new(),
|
|
});
|
|
app
|
|
}
|
|
|
|
fn draw(app: &AppState, width: u16, height: u16, theme: Theme) -> (String, Terminal<TestBackend>) {
|
|
let backend = TestBackend::new(width, height);
|
|
let mut terminal = Terminal::new(backend).unwrap();
|
|
terminal
|
|
.draw(|frame| render(frame, app, None, &theme))
|
|
.unwrap();
|
|
(buffer_text(terminal.backend()), terminal)
|
|
}
|
|
|
|
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 loading_empty_error_and_stale_warning_are_distinct_states() {
|
|
let mut loading = AppState::new(Location::local("/work"));
|
|
loading.panes[0].loading = true;
|
|
loading.panes[0].pending_target = Some(Location::local("/work/slow"));
|
|
let loading_output = draw(&loading, 120, 40, Theme::default()).0;
|
|
assert!(loading_output.contains("LOADING"));
|
|
assert!(loading_output.contains("Reading /work/slow"));
|
|
assert!(!loading_output.contains("EMPTY DIRECTORY"));
|
|
|
|
let empty = AppState::new(Location::local("/work/empty"));
|
|
let empty_output = draw(&empty, 120, 40, Theme::default()).0;
|
|
assert!(empty_output.contains("EMPTY DIRECTORY"));
|
|
assert!(empty_output.contains("No visible entries"));
|
|
assert!(!empty_output.contains("LOADING"));
|
|
|
|
let mut error = AppState::new(Location::local("/work/private"));
|
|
error.panes[0].error = Some(FsError::PermissionDenied);
|
|
let error_output = draw(&error, 120, 40, Theme::default()).0;
|
|
assert!(error_output.contains("LISTING ERROR"));
|
|
assert!(error_output.contains("Permission denied"));
|
|
assert!(error_output.contains("ERROR"));
|
|
|
|
let mut stale = app_with_entries();
|
|
stale.panes[0].loading = true;
|
|
let stale_output = draw(&stale, 120, 40, Theme::default()).0;
|
|
assert!(stale_output.contains("WARNING") || stale_output.contains('⚠'));
|
|
assert!(stale_output.contains("previous listing may be stale"));
|
|
assert!(stale_output.contains("README.md"));
|
|
}
|
|
|
|
#[test]
|
|
fn absent_selection_and_unavailable_preview_are_not_empty_success() {
|
|
let app = AppState::new(Location::local("/work"));
|
|
let output = draw(&app, 120, 40, Theme::default()).0;
|
|
assert!(output.contains("NO PREVIEW"));
|
|
assert!(output.contains("Select a file or directory"));
|
|
|
|
let mut unavailable = app_with_entries();
|
|
unavailable.panes[0].preview.content = None;
|
|
let output = draw(&unavailable, 120, 40, Theme::default()).0;
|
|
assert!(output.contains("NO PREVIEW"));
|
|
assert!(output.contains("unavailable"));
|
|
}
|
|
|
|
#[test]
|
|
fn hostile_names_are_escaped_and_unicode_truncation_stays_within_the_frame() {
|
|
let mut app = AppState::new(Location::local("/work"));
|
|
app.panes[0].entries = vec![Entry::new(
|
|
"p\u{1b}[2J\u{202e}-界界界界界界界界界界界界-suffix\nforged",
|
|
EntryKind::File,
|
|
)];
|
|
app.panes[0].selection.set(Some(0), 1);
|
|
let (output, _) = draw(&app, 80, 24, Theme::default());
|
|
|
|
assert!(output.contains("\\x1b[2J"));
|
|
assert!(output.contains("\\u{202e}"), "{output}");
|
|
assert!(output.contains("\\nforged"));
|
|
assert!(!output.contains('\u{1b}'));
|
|
assert!(!output.contains('\u{202e}'));
|
|
assert!(!output.contains("forged\nTYPE"));
|
|
}
|
|
|
|
#[test]
|
|
fn no_color_preserves_text_geometry_and_uses_reversed_selection() {
|
|
let app = app_with_entries();
|
|
let colored = draw(&app, 120, 40, Theme::default()).0;
|
|
let (plain, backend) = draw(&app, 120, 40, Theme::no_color());
|
|
assert_eq!(colored, plain);
|
|
assert!(plain.contains("▸ ACTIVE"));
|
|
assert!(plain.contains("📖"));
|
|
assert!(plain.contains("README.md"));
|
|
|
|
for cell in backend.backend().buffer().content() {
|
|
assert_eq!(cell.fg, Color::Reset);
|
|
assert_eq!(cell.bg, Color::Reset);
|
|
}
|
|
let selected = (0..backend.backend().buffer().area.height)
|
|
.flat_map(|y| (0..backend.backend().buffer().area.width).map(move |x| (x, y)))
|
|
.map(|position| backend.backend().buffer().cell(position).unwrap())
|
|
.find(|cell| cell.symbol() == "▸" && cell.modifier.contains(Modifier::REVERSED));
|
|
assert!(selected.is_some());
|
|
}
|
|
|
|
#[test]
|
|
fn ascii_fallback_replaces_structural_unicode_but_keeps_names() {
|
|
let app = app_with_entries();
|
|
let output = draw(&app, 120, 40, Theme::ascii()).0;
|
|
assert!(output.contains("> ACTIVE"));
|
|
assert!(output.contains("i README.md"));
|
|
assert!(!output.contains('▸'));
|
|
assert!(!output.contains('╭'));
|
|
assert!(!output.contains('·'));
|
|
}
|
|
|
|
#[test]
|
|
fn multi_selection_has_a_star_separate_from_cursor_and_a_status_count() {
|
|
let mut app = app_with_entries();
|
|
let docs = app.active_pane().location.entry_id("docs");
|
|
app.active_pane_mut().selection_set.insert(docs);
|
|
|
|
let (output, backend) = draw(&app, 120, 40, Theme::no_color());
|
|
let docs_row = output
|
|
.lines()
|
|
.find(|line| line.contains("docs/"))
|
|
.expect("docs row");
|
|
let cursor_row = output
|
|
.lines()
|
|
.find(|line| line.contains("README.md") && line.contains("│▸"))
|
|
.expect("cursor row");
|
|
assert!(docs_row.contains(" *"), "{docs_row}");
|
|
assert!(!docs_row.contains('▸'), "{docs_row}");
|
|
assert!(cursor_row.contains("📖"), "{cursor_row}");
|
|
assert!(!cursor_row.contains('*'), "{cursor_row}");
|
|
assert!(output.contains("Marked 1"), "{output}");
|
|
|
|
for cell in backend.backend().buffer().content() {
|
|
assert_eq!(cell.fg, Color::Reset);
|
|
assert_eq!(cell.bg, Color::Reset);
|
|
}
|
|
|
|
let readme = app.active_pane().location.entry_id("README.md");
|
|
app.active_pane_mut().selection_set.insert(readme);
|
|
let output = draw(&app, 120, 40, Theme::no_color()).0;
|
|
let cursor_row = output
|
|
.lines()
|
|
.find(|line| line.contains("README.md") && line.contains("│▸"))
|
|
.expect("selected cursor row");
|
|
assert!(cursor_row.contains("▸*"), "{cursor_row}");
|
|
assert!(cursor_row.contains("📖"), "{cursor_row}");
|
|
assert!(output.contains("Marked 2"), "{output}");
|
|
}
|
|
|
|
#[test]
|
|
fn dual_mode_preview_is_never_persistent_even_with_preview_content() {
|
|
let mut app = app_with_entries();
|
|
app.mode = PaneMode::Dual;
|
|
app.panes[1] = app.panes[0].clone();
|
|
let output = draw(&app, 160, 40, Theme::default()).0;
|
|
assert!(!output.contains("PREVIEW"));
|
|
assert!(!output.contains("1 │ # rcom"));
|
|
}
|
|
|
|
#[test]
|
|
fn snapshot_loading_120x40() {
|
|
let mut app = AppState::new(Location::local("/work"));
|
|
app.panes[0].loading = true;
|
|
app.panes[0].pending_target = Some(Location::local("/work/slow-remote"));
|
|
insta::assert_snapshot!(draw(&app, 120, 40, Theme::default()).0);
|
|
}
|
|
|
|
#[test]
|
|
fn snapshot_error_120x40() {
|
|
let mut app = AppState::new(Location::local("/work/private"));
|
|
app.panes[0].error = Some(FsError::PermissionDenied);
|
|
insta::assert_snapshot!(draw(&app, 120, 40, Theme::default()).0);
|
|
}
|
|
|
|
#[test]
|
|
fn snapshot_no_preview_120x40() {
|
|
let app = AppState::new(Location::local("/work/empty"));
|
|
insta::assert_snapshot!(draw(&app, 120, 40, Theme::default()).0);
|
|
}
|
|
|
|
#[test]
|
|
fn snapshot_no_color_120x40() {
|
|
insta::assert_snapshot!(draw(&app_with_entries(), 120, 40, Theme::no_color()).0);
|
|
}
|