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

283 lines
9.3 KiB
Rust

use std::collections::BTreeSet;
use std::ffi::OsString;
use std::path::{Path, PathBuf};
use proptest::prelude::*;
use rcom::domain::{
Entry, EntryId, EntryKind, HostAlias, Location, SelectionSet, SourceId, prepare_entries,
};
fn unicode_name() -> impl Strategy<Value = String> {
prop::collection::vec(
any::<char>().prop_filter("valid file name character", |character| {
*character != '/' && *character != '\0'
}),
0..32,
)
.prop_map(|characters| characters.into_iter().collect())
}
fn unicode_names() -> impl Strategy<Value = Vec<String>> {
prop::collection::vec(unicode_name(), 0..32)
}
fn entry_ids(location: &Location, names: &[String]) -> BTreeSet<EntryId> {
names.iter().map(|name| location.entry_id(name)).collect()
}
fn selected_ids(selection: &SelectionSet) -> BTreeSet<EntryId> {
selection.iter().cloned().collect()
}
#[cfg(unix)]
fn unix_entry_id(location: &Location, bytes: &[u8]) -> EntryId {
use std::os::unix::ffi::OsStringExt;
location.entry_id(OsString::from_vec(bytes.to_vec()))
}
#[test]
fn entry_id_contains_source_and_full_child_path() {
let local = Location::local("/var/log");
let remote = Location::ssh(HostAlias::new("edge"), "/srv/app");
assert_eq!(
local.entry_id("app.log"),
EntryId {
source: SourceId::Local,
path: PathBuf::from("/var/log/app.log"),
}
);
assert_eq!(
remote.entry_id("данные"),
EntryId {
source: SourceId::Ssh(HostAlias::new("edge")),
path: PathBuf::from("/srv/app/данные"),
}
);
assert_ne!(local.entry_id("same"), remote.entry_id("same"));
}
#[test]
fn selection_set_supports_mutation_iteration_and_visible_inversion() {
let location = Location::local("/work");
let first = location.entry_id("a");
let second = location.entry_id("b");
let hidden = location.entry_id(".hidden");
let mut selection = SelectionSet::new();
assert!(selection.is_empty());
assert!(selection.insert(first.clone()));
assert!(!selection.insert(first.clone()));
assert!(selection.insert(hidden.clone()));
assert!(selection.contains(&first));
assert_eq!(selection.len(), 2);
assert_eq!(
selection.iter().cloned().collect::<Vec<_>>(),
vec![hidden.clone(), first.clone()]
);
selection.invert_visible([first.clone(), second.clone(), second.clone()]);
assert!(!selection.contains(&first));
assert!(selection.contains(&second));
assert!(selection.contains(&hidden));
assert!(!selection.toggle(second.clone()));
assert!(selection.toggle(second.clone()));
assert!(selection.remove(&second));
assert!(!selection.remove(&second));
selection.clear();
assert!(selection.is_empty());
}
#[test]
fn vanished_entry_does_not_transfer_selection_to_replacement_name() {
let location = Location::local("/work");
let old = location.entry_id("old");
let replacement = location.entry_id("new");
let mut selection = SelectionSet::new();
selection.insert(old);
selection.retain_existing([replacement]);
assert!(selection.is_empty());
}
proptest! {
#[test]
fn iteration_is_sorted_and_duplicate_inserts_are_idempotent(names in unicode_names()) {
let location = Location::local("/unicode/⌘");
let expected = entry_ids(&location, &names);
let mut selection = SelectionSet::new();
for id in expected.iter().rev().chain(expected.iter()).cloned() {
selection.insert(id);
}
let actual: Vec<_> = selection.iter().cloned().collect();
prop_assert_eq!(actual, expected.iter().cloned().collect::<Vec<_>>());
prop_assert_eq!(selection.len(), expected.len());
}
#[test]
fn sorting_and_refresh_keep_the_same_entry_ids(names in unicode_names()) {
let location = Location::ssh(HostAlias::new("хост"), "/каталог");
let entries: Vec<_> = names
.iter()
.map(|name| Entry::new(name.clone(), EntryKind::File))
.collect();
let expected = entry_ids(&location, &names);
let mut selection = SelectionSet::new();
for id in &expected {
selection.insert(id.clone());
}
let sorted = prepare_entries(entries.clone(), true);
let refreshed = prepare_entries(entries.into_iter().rev().collect(), true);
let sorted_ids: Vec<_> = sorted.iter().map(|entry| location.entry_id(&entry.name)).collect();
let refreshed_ids: Vec<_> = refreshed
.iter()
.map(|entry| location.entry_id(&entry.name))
.collect();
prop_assert_eq!(&sorted_ids, &refreshed_ids);
selection.retain_existing(refreshed_ids.iter().rev());
prop_assert_eq!(selected_ids(&selection), expected);
}
#[test]
fn hidden_filtering_does_not_change_existing_selection(names in unicode_names()) {
let location = Location::local("/hidden");
let existing: Vec<_> = names
.iter()
.map(|name| Entry::new(name.clone(), EntryKind::File))
.collect();
let expected = entry_ids(&location, &names);
let mut selection = SelectionSet::new();
for id in &expected {
selection.insert(id.clone());
}
let visible = prepare_entries(existing.clone(), false);
let refreshed = prepare_entries(existing.into_iter().rev().collect(), false);
let visible_ids: BTreeSet<_> = visible
.iter()
.map(|entry| location.entry_id(&entry.name))
.collect();
let refreshed_ids: BTreeSet<_> = refreshed
.iter()
.map(|entry| location.entry_id(&entry.name))
.collect();
prop_assert_eq!(visible_ids, refreshed_ids);
// Refresh receives all entries which still exist, not only currently visible entries.
selection.retain_existing(entry_ids(&location, &names));
prop_assert_eq!(selected_ids(&selection), expected);
}
#[test]
fn refresh_drops_disappeared_ids_without_transferring_selection(
before in unicode_names(),
after in unicode_names(),
) {
let location = Location::local("/refresh");
let old_ids = entry_ids(&location, &before);
let existing = entry_ids(&location, &after);
let expected = old_ids.intersection(&existing).cloned().collect::<BTreeSet<_>>();
let mut selection = SelectionSet::new();
for id in &old_ids {
selection.insert(id.clone());
}
selection.retain_existing(existing.iter());
prop_assert_eq!(selected_ids(&selection), expected);
for id in selection.iter() {
prop_assert!(existing.contains(id));
}
}
#[test]
fn inversion_matches_a_set_model(
initially_selected in unicode_names(),
visible in unicode_names(),
) {
let location = Location::local("/invert");
let selected = entry_ids(&location, &initially_selected);
let visible = entry_ids(&location, &visible);
let mut expected = selected.clone();
for id in &visible {
if !expected.remove(id) {
expected.insert(id.clone());
}
}
let mut selection = SelectionSet::new();
for id in selected {
selection.insert(id);
}
selection.invert_visible(visible.iter());
prop_assert_eq!(selected_ids(&selection), expected);
}
}
#[cfg(unix)]
proptest! {
#[test]
fn arbitrary_non_utf8_names_keep_their_identity_across_refresh(
before in prop::collection::vec(
prop::collection::vec(
any::<u8>().prop_filter("valid Unix file name byte", |byte| {
*byte != 0 && *byte != b'/'
}),
0..40,
),
0..24,
),
after in prop::collection::vec(
prop::collection::vec(
any::<u8>().prop_filter("valid Unix file name byte", |byte| {
*byte != 0 && *byte != b'/'
}),
0..40,
),
0..24,
),
) {
let location = Location::local("/bytes");
let old_ids: BTreeSet<_> = before
.iter()
.map(|bytes| unix_entry_id(&location, bytes))
.collect();
let existing: BTreeSet<_> = after
.iter()
.map(|bytes| unix_entry_id(&location, bytes))
.collect();
let expected = old_ids.intersection(&existing).cloned().collect::<BTreeSet<_>>();
let mut selection = SelectionSet::new();
for id in &old_ids {
selection.insert(id.clone());
}
selection.retain_existing(existing.iter());
prop_assert_eq!(selected_ids(&selection), expected);
}
#[test]
fn invalid_utf8_name_round_trips_through_entry_id(bytes in prop::collection::vec(0x80u8..=0xff, 1..40)) {
use std::os::unix::ffi::{OsStrExt, OsStringExt};
let location = Location::local(Path::new("/bytes"));
let name = OsString::from_vec(bytes.clone());
let id = location.entry_id(name);
prop_assert_eq!(
&id.path,
&Path::new("/bytes").join(OsString::from_vec(bytes.clone()))
);
prop_assert_eq!(id.path.file_name().map(OsStrExt::as_bytes), Some(bytes.as_slice()));
}
}