use core::num::NonZeroU64;
use std::{collections::HashMap, ffi::OsString, path::Path};
use tributary_proto::ScopeId;
use super::{RoutableEvent, Subscription, fan_out};
fn key(path: &str) -> Vec<OsString> {
Path::new(path)
.components()
.map(|c| c.as_os_str().to_os_string())
.collect()
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Projection {
Whole,
MoveOut,
MoveIn,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct Delivered {
sub: Subscription,
projection: Projection,
}
struct FakeEvent {
key: Vec<OsString>,
from: Option<Vec<OsString>>,
rescan: bool,
}
impl FakeEvent {
fn change(path: &str) -> Self {
Self {
key: key(path),
from: None,
rescan: false,
}
}
fn rescan(path: &str) -> Self {
Self {
key: key(path),
from: None,
rescan: true,
}
}
fn moved(from: &str, to: &str) -> Self {
Self {
key: key(to),
from: Some(key(from)),
rescan: false,
}
}
}
impl RoutableEvent<OsString> for FakeEvent {
type Delivered = Delivered;
fn key(&self) -> &[OsString] {
self.key.as_slice()
}
fn move_from(&self) -> Option<&[OsString]> {
self.from.as_deref()
}
fn is_rescan(&self) -> bool {
self.rescan
}
fn deliver(&self, sub: Subscription) -> Delivered {
Delivered {
sub,
projection: Projection::Whole,
}
}
fn deliver_move_out(&self, sub: Subscription) -> Delivered {
Delivered {
sub,
projection: Projection::MoveOut,
}
}
fn deliver_move_in(&self, sub: Subscription) -> Delivered {
Delivered {
sub,
projection: Projection::MoveIn,
}
}
}
struct Fixture {
subscribers: Vec<Subscription>,
keys: HashMap<Subscription, Vec<OsString>>,
}
impl Fixture {
fn new(_root_path: &str, subscribers: &[(u64, &str)]) -> Self {
let mut subs = Vec::new();
let mut keys = HashMap::new();
for &(id, path) in subscribers {
let sub = Subscription::for_test(ScopeId::new(NonZeroU64::new(id).expect("nonzero id")));
subs.push(sub);
keys.insert(sub, key(path));
}
Self {
subscribers: subs,
keys,
}
}
fn sub(&self, id: u64) -> Subscription {
Subscription::for_test(ScopeId::new(NonZeroU64::new(id).expect("nonzero id")))
}
fn route_full(&self, event: &FakeEvent) -> Vec<Delivered> {
fan_out(
event,
&self.subscribers,
|sub| self.keys.get(&sub).map(Vec::as_slice),
|_sub, _delivered| true,
)
}
fn route(&self, event: &FakeEvent) -> Vec<Subscription> {
self.route_full(event).into_iter().map(|d| d.sub).collect()
}
fn route_filtered(
&self,
event: &FakeEvent,
admits: impl Fn(Subscription, &Delivered) -> bool,
) -> Vec<Subscription> {
fan_out(
event,
&self.subscribers,
|sub| self.keys.get(&sub).map(Vec::as_slice),
admits,
)
.into_iter()
.map(|d| d.sub)
.collect()
}
}
#[test]
fn covers_single_subscriber() {
let fx = Fixture::new("/a", &[(1, "/a")]);
let delivered = fx.route(&FakeEvent::change("/a/file"));
assert_eq!(
delivered,
vec![fx.sub(1)],
"the lone covering sub is served"
);
}
#[test]
fn overlap_fans_to_all_covering() {
let fx = Fixture::new("/a", &[(1, "/a"), (2, "/a/b")]);
let both = fx.route(&FakeEvent::change("/a/b/c"));
assert_eq!(
both,
vec![fx.sub(1), fx.sub(2)],
"an event under /a/b reaches both /a and /a/b"
);
let only_a = fx.route(&FakeEvent::change("/a/x"));
assert_eq!(
only_a,
vec![fx.sub(1)],
"an event under /a/x reaches only /a"
);
}
#[test]
fn coverage_is_component_wise_not_prefix() {
let fx = Fixture::new("/a", &[(1, "/a/b")]);
assert_eq!(
fx.route(&FakeEvent::change("/a/bc")),
Vec::<Subscription>::new(),
"/a/b does not cover the sibling /a/bc"
);
assert_eq!(
fx.route(&FakeEvent::change("/a/b")),
vec![fx.sub(1)],
"/a/b covers itself (ancestor-or-equal)"
);
}
#[test]
fn rescan_reaches_all_subscribers() {
let fx = Fixture::new("/a", &[(1, "/a"), (2, "/a/b/deep")]);
assert_eq!(
fx.route(&FakeEvent::change("/a/x")),
vec![fx.sub(1)],
"a plain event honors coverage narrowing"
);
assert_eq!(
fx.route(&FakeEvent::rescan("/a/x")),
vec![fx.sub(1), fx.sub(2)],
"a Rescan bypasses coverage and reaches all subscribers"
);
}
#[test]
fn filter_narrows_the_covered_set() {
let fx = Fixture::new("/a", &[(1, "/a"), (2, "/a/b")]);
let admitted = fx.route_filtered(&FakeEvent::change("/a/b/c"), |sub, _| sub != fx.sub(2));
assert_eq!(
admitted,
vec![fx.sub(1)],
"the filter excludes the covering subscription that does not admit"
);
let both = fx.route_filtered(&FakeEvent::change("/a/b/c"), |_, _| true);
assert_eq!(both, vec![fx.sub(1), fx.sub(2)], "both cover it");
}
#[test]
fn filter_only_sees_covered_deliveries() {
let fx = Fixture::new("/a", &[(1, "/a"), (2, "/a/b")]);
let admitted = fx.route_filtered(&FakeEvent::change("/a/x"), |sub, _| {
assert_ne!(
sub,
fx.sub(2),
"the filter is only asked about covering subs"
);
true
});
assert_eq!(
admitted,
vec![fx.sub(1)],
"only the covering /a is admitted"
);
}
#[test]
fn rescan_bypasses_the_filter() {
let fx = Fixture::new("/a", &[(1, "/a"), (2, "/a/b")]);
let admitted = fx.route_filtered(&FakeEvent::rescan("/a/x"), |_, _| {
panic!("the filter must not be consulted for a Rescan");
});
assert_eq!(
admitted,
vec![fx.sub(1), fx.sub(2)],
"a Rescan bypasses the filter and reaches every subscriber"
);
}
fn projections(fx: &Fixture, event: &FakeEvent) -> Vec<(u64, Projection)> {
fx.route_full(event)
.into_iter()
.map(|d| (d.sub.id().as_u64(), d.projection))
.collect()
}
#[test]
fn move_out_delivers_removed_to_source_only_sub() {
let fx = Fixture::new("/a", &[(1, "/a/src")]);
let out = projections(&fx, &FakeEvent::moved("/a/src/f", "/a/dst/f"));
assert_eq!(
out,
vec![(1, Projection::MoveOut)],
"a source-only sub gets the move-out Removed(from) — never silently skipped"
);
}
#[test]
fn move_in_delivers_created_to_dest_only_sub() {
let fx = Fixture::new("/a", &[(1, "/a/dst")]);
let out = projections(&fx, &FakeEvent::moved("/a/src/f", "/a/dst/f"));
assert_eq!(
out,
vec![(1, Projection::MoveIn)],
"a destination-only sub gets the move-in Created(to)"
);
}
#[test]
fn move_within_one_sub_delivers_one_moved() {
let fx = Fixture::new("/a", &[(1, "/a")]);
let out = projections(&fx, &FakeEvent::moved("/a/src/f", "/a/dst/f"));
assert_eq!(
out,
vec![(1, Projection::Whole)],
"a both-covering sub gets exactly one whole Moved — dedup, no extra Removed/Created"
);
}
#[test]
fn move_between_sibling_subs() {
let fx = Fixture::new("/a", &[(1, "/a/src"), (2, "/a/dst")]);
let out = projections(&fx, &FakeEvent::moved("/a/src/f", "/a/dst/f"));
assert_eq!(
out,
vec![(1, Projection::MoveOut), (2, Projection::MoveIn)],
"the source-sub gets a Removed, the dest-sub a Created"
);
}
#[test]
fn move_covering_neither_endpoint_delivers_nothing() {
let fx = Fixture::new("/a", &[(1, "/a/other")]);
let out = projections(&fx, &FakeEvent::moved("/a/src/f", "/a/dst/f"));
assert!(
out.is_empty(),
"a sub covering neither endpoint gets nothing"
);
}
#[test]
fn move_dedup_both_covering_sub_gets_exactly_one_moved() {
let fx = Fixture::new("/a", &[(1, "/a"), (2, "/a/src"), (3, "/a/dst")]);
let out = projections(&fx, &FakeEvent::moved("/a/src/f", "/a/dst/f"));
assert_eq!(
out,
vec![
(1, Projection::Whole), (2, Projection::MoveOut), (3, Projection::MoveIn), ],
"the both-covering sub gets exactly one Moved; siblings get their one-sided projection"
);
}
#[test]
fn move_projection_is_gated_by_the_projected_kind() {
let fx = Fixture::new("/a", &[(1, "/a/src"), (2, "/a/dst")]);
let admitted = fx.route_filtered(&FakeEvent::moved("/a/src/f", "/a/dst/f"), |_sub, d| {
d.projection != Projection::MoveOut
});
assert_eq!(
admitted,
vec![fx.sub(2)],
"the gate drops the move-out projection; the move-in still reaches the dest-sub"
);
}