use core::num::NonZeroU64;
use std::{ffi::OsString, path::Path};
use tributary_proto::{Epoch, Location, ScopeId};
use super::{Filter, FilterInput};
use crate::{
event::{Event, EventKind},
subscription::Subscription,
};
fn sub(id: u64) -> Subscription {
Subscription::for_test(ScopeId::new(NonZeroU64::new(id).expect("nonzero id")))
}
fn key(path: &str) -> Vec<OsString> {
Path::new(path)
.components()
.map(|c| c.as_os_str().to_os_string())
.collect()
}
fn admits(f: &Filter<OsString>, path: &str, kind: EventKind<OsString>) -> bool {
let ev: Event<OsString, ()> =
Event::synthetic(sub(1), key(path), Location::new(), kind, Epoch::new(1));
f.admits(&FilterInput::new(ev.key(), ev.kind(), ev.location()))
}
#[test]
fn all_admits_everything() {
let f = Filter::all();
assert!(admits(&f, "/a/f.rs", EventKind::Created));
assert!(admits(&f, "/a/f.txt", EventKind::Removed));
assert!(admits(&f, "/a", EventKind::Rescan));
}
#[test]
fn filter_excludes_non_matching() {
let f = Filter::new(|input| input.path().extension().is_some_and(|ext| ext == "rs"));
assert!(
admits(&f, "/a/lib.rs", EventKind::Modified),
"a .rs is admitted"
);
assert!(
!admits(&f, "/a/notes.txt", EventKind::Modified),
"a non-matching path is excluded"
);
}
#[test]
fn filter_gates_on_the_projected_kind() {
let f = Filter::new(|input: &FilterInput<'_, OsString>| input.kind().is_removed());
assert!(
admits(&f, "/a/gone", EventKind::Removed),
"a Removed is admitted"
);
assert!(
!admits(&f, "/a/new", EventKind::Created),
"a Created is excluded by a removals-only filter"
);
}
#[test]
fn admits_is_a_pure_predicate_not_a_rescan_special_case() {
let reject_all = Filter::new(|_| false);
assert!(
!admits(&reject_all, "/a", EventKind::Rescan),
"admits is pure: the Rescan bypass is enforced in fan_out, not here"
);
}
#[test]
fn live_swap_takes_effect() {
let f = Filter::all();
assert!(
admits(&f, "/a/notes.txt", EventKind::Modified),
"the initial filter admits the .txt"
);
f.swap(|input| input.path().extension().is_some_and(|ext| ext == "rs"));
assert!(
!admits(&f, "/a/notes.txt", EventKind::Modified),
"after the swap the .txt is excluded — the new predicate is live"
);
assert!(
admits(&f, "/a/lib.rs", EventKind::Modified),
"…and the new predicate still admits a .rs"
);
}
#[test]
fn a_clone_shares_the_swappable_slot() {
let held_by_driver = Filter::all();
let held_by_caller = held_by_driver.clone();
assert!(
admits(&held_by_driver, "/a/notes.txt", EventKind::Modified),
"both start admitting"
);
held_by_caller.swap(|input| input.path().extension().is_some_and(|ext| ext == "rs"));
assert!(
!admits(&held_by_driver, "/a/notes.txt", EventKind::Modified),
"the driver's handle observes the caller's swap (shared slot)"
);
}