rcom/tests/domain_navigation.rs
2026-12-05 23:30:06 +03:00

117 lines
3.8 KiB
Rust

use std::ffi::{OsStr, OsString};
use std::path::{Path, PathBuf};
use proptest::prelude::*;
use rcom::domain::{
Entry, EntryKind, HostAlias, Location, Selection, prepare_entries, restore_selection,
};
#[test]
fn directories_are_first_and_names_are_deterministic() {
let input = vec![
Entry::new("zeta", EntryKind::File),
Entry::new("beta", EntryKind::Directory),
Entry::new("Alpha", EntryKind::File),
Entry::new("alpha", EntryKind::File),
Entry::new("Gamma", EntryKind::Directory),
];
let first = prepare_entries(input.clone(), true);
let second = prepare_entries(input, true);
let names: Vec<_> = first.iter().map(|entry| entry.name.as_os_str()).collect();
assert_eq!(names, ["beta", "Gamma", "Alpha", "alpha", "zeta"]);
assert_eq!(first, second);
}
#[test]
fn hidden_entries_are_filtered_without_changing_original_names() {
let entries = prepare_entries(
vec![
Entry::new(".config", EntryKind::Directory),
Entry::new("visible", EntryKind::File),
],
false,
);
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].name, "visible");
}
#[cfg(unix)]
#[test]
fn non_utf8_names_survive_sorting_and_filtering() {
use std::os::unix::ffi::OsStringExt;
let raw = OsString::from_vec(vec![b'f', 0x80, b'o']);
let entries = prepare_entries(vec![Entry::new(raw.clone(), EntryKind::File)], true);
assert_eq!(entries[0].name.as_os_str(), raw.as_os_str());
assert_eq!(
entries[0].name.as_os_str().as_encoded_bytes(),
&[b'f', 0x80, b'o']
);
}
#[test]
fn parent_and_child_paths_keep_their_source() {
let local = Location::local("/var/log");
let remote = Location::ssh(HostAlias::new("edge"), "/srv/app");
assert_eq!(local.parent().unwrap().path(), Path::new("/var"));
assert_eq!(remote.parent().unwrap().path(), Path::new("/srv"));
assert_eq!(remote.parent().unwrap().source(), remote.source());
assert_eq!(remote.join("logs").path(), Path::new("/srv/app/logs"));
assert_eq!(local.file_name(), Some(OsStr::new("log")));
assert_eq!(Location::local("/").parent(), None);
assert_eq!(Location::ssh(HostAlias::new("edge"), "/").parent(), None);
}
#[test]
fn selection_is_restored_by_name_then_by_nearest_index() {
let entries = prepare_entries(
vec![
Entry::new("a", EntryKind::File),
Entry::new("b", EntryKind::File),
Entry::new("c", EntryKind::File),
],
true,
);
assert_eq!(
restore_selection(&entries, Some(0), Some(OsStr::new("b")), None),
Some(1)
);
assert_eq!(
restore_selection(&entries, Some(2), Some(OsStr::new("missing")), None),
Some(2)
);
assert_eq!(
restore_selection(&entries, None, None, Some(OsStr::new("a"))),
Some(0)
);
assert_eq!(restore_selection(&[], Some(0), None, None), None);
}
proptest! {
#[test]
fn selection_never_escapes_bounds(len in 0usize..80, steps in prop::collection::vec(-100isize..100, 0..100)) {
let mut selection = Selection::new(len);
for step in steps {
selection.move_by(step, len);
prop_assert!(selection.index().is_none_or(|index| index < len));
}
selection.first(len);
prop_assert!(selection.index().is_none_or(|index| index < len));
selection.last(len);
prop_assert!(selection.index().is_none_or(|index| index < len));
selection.page(len, 7, true);
prop_assert!(selection.index().is_none_or(|index| index < len));
}
#[test]
fn arbitrary_paths_never_panic_on_parent(path in "[/A-Za-z0-9_.-]{0,80}") {
let location = Location { source: rcom::domain::SourceId::Local, path: PathBuf::from(path) };
let _ = location.parent();
}
}