use core::num::NonZeroU64;
use std::{ffi::OsString, path::Path};
use tributary_proto::{Epoch, ScopeId};
use super::EpochLedger;
use crate::{route::RoutableEvent, subscription::Subscription};
fn key(path: &str) -> Vec<OsString> {
Path::new(path)
.components()
.map(|c| c.as_os_str().to_os_string())
.collect()
}
struct FakeEvent {
key: Vec<OsString>,
from: Option<Vec<OsString>>,
rescan: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Projection {
Whole,
MoveOut,
MoveIn,
}
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 = (Subscription, Projection);
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) -> (Subscription, Projection) {
(sub, Projection::Whole)
}
fn deliver_move_out(&self, sub: Subscription) -> (Subscription, Projection) {
(sub, Projection::MoveOut)
}
fn deliver_move_in(&self, sub: Subscription) -> (Subscription, Projection) {
(sub, Projection::MoveIn)
}
}
fn sub(id: u64) -> Subscription {
Subscription::for_test(ScopeId::new(NonZeroU64::new(id).expect("nonzero id")))
}
struct Fixture {
subscribers: Vec<Subscription>,
keys: Vec<(Subscription, Vec<OsString>)>,
}
impl Fixture {
fn new(_root_path: &str, subscribers: &[(u64, &str)]) -> Self {
let mut subs = Vec::new();
let mut keys = Vec::new();
for &(id, path) in subscribers {
let s = sub(id);
subs.push(s);
keys.push((s, key(path)));
}
Self {
subscribers: subs,
keys,
}
}
fn canonical_of(&self, s: Subscription) -> Option<&[OsString]> {
self
.keys
.iter()
.find(|(candidate, _)| *candidate == s)
.map(|(_, k)| k.as_slice())
}
fn deliver(
&self,
ledger: &mut EpochLedger,
event: &FakeEvent,
raw: Epoch,
) -> Vec<(Subscription, Epoch)> {
self
.deliver_projected(ledger, event, raw)
.into_iter()
.map(|(s, _projection, stamp)| (s, stamp))
.collect()
}
fn deliver_projected(
&self,
ledger: &mut EpochLedger,
event: &FakeEvent,
raw: Epoch,
) -> Vec<(Subscription, Projection, Epoch)> {
ledger.stamp_and_fan_out(
event,
raw,
&self.subscribers,
|s| self.canonical_of(s),
|_sub, _delivered| true,
|(s, _projection)| *s,
|(s, projection), stamp| (s, projection, stamp),
)
}
fn deliver_gated(
&self,
ledger: &mut EpochLedger,
event: &FakeEvent,
raw: Epoch,
admit: impl Fn(Subscription) -> bool,
) -> Vec<(Subscription, Epoch)> {
ledger.stamp_and_fan_out(
event,
raw,
&self.subscribers,
|s| self.canonical_of(s),
|sub, _delivered| admit(sub),
|(s, _projection)| *s,
|(s, _projection), stamp| (s, stamp),
)
}
}
#[test]
fn stamps_are_monotone_within_a_root() {
let mut ledger = EpochLedger::new();
let fx = Fixture::new("/a", &[(1, "/a")]);
let s = sub(1);
let mut stamps = Vec::new();
for raw in 0..3 {
let delivered = fx.deliver(&mut ledger, &FakeEvent::change("/a/file"), Epoch::new(raw));
assert_eq!(
delivered,
vec![(s, Epoch::new(raw))],
"stamp = base(0) + raw"
);
stamps.push(delivered[0].1);
}
assert!(
stamps.windows(2).all(|w| w[0] <= w[1]),
"stamps are monotone-nondecreasing within one root"
);
}
#[test]
fn post_widen_new_root_events_are_not_dominated_by_the_widen_rescan() {
let mut ledger = EpochLedger::new();
let s = sub(1);
let root_a = Fixture::new("/a/b", &[(1, "/a/b")]);
let mut pre_widen = Vec::new();
for raw in 0..3 {
let delivered = root_a.deliver(&mut ledger, &FakeEvent::change("/a/b/f"), Epoch::new(raw));
assert_eq!(delivered.len(), 1, "the lone covering sub is served");
pre_widen.push(delivered[0].1);
}
assert_eq!(
pre_widen,
vec![Epoch::new(0), Epoch::new(1), Epoch::new(2)],
"pre-widen stamps track the raw fs epochs from base START"
);
let rescan_stamp = ledger.repoint(s);
assert_eq!(
rescan_stamp,
Epoch::new(3),
"the widen Rescan is minted one past the subscription's high-water"
);
let root_b = Fixture::new("/a", &[(1, "/a")]);
let mut post_widen = Vec::new();
for raw in 0..2 {
let delivered = root_b.deliver(&mut ledger, &FakeEvent::change("/a/b/f"), Epoch::new(raw));
assert_eq!(delivered.len(), 1, "the sub still covers events under /a/b");
post_widen.push(delivered[0].1);
}
assert_eq!(
post_widen,
vec![Epoch::new(3), Epoch::new(4)],
"post-widen stamps rebase onto hw.next()+raw, not the raw fs epoch"
);
assert!(
pre_widen.iter().all(|&e| e < rescan_stamp),
"pre-widen events are dominated by the widen Rescan"
);
assert!(
post_widen.iter().all(|&e| e >= rescan_stamp),
"post-widen new-root events are NOT dominated by the widen Rescan (RISK-1)"
);
let whole: Vec<Epoch> = pre_widen
.iter()
.copied()
.chain(core::iter::once(rescan_stamp))
.chain(post_widen.iter().copied())
.collect();
assert!(
whole.windows(2).all(|w| w[0] <= w[1]),
"the whole stamped sequence across the re-point is monotone-nondecreasing"
);
}
#[test]
fn two_subscriptions_onto_one_new_root_rebase_independently() {
let mut ledger = EpochLedger::new();
let (s1, s2) = (sub(1), sub(2));
let root1 = Fixture::new("/a/b", &[(1, "/a/b")]);
for raw in 0..5 {
root1.deliver(&mut ledger, &FakeEvent::change("/a/b/f"), Epoch::new(raw));
}
let root2 = Fixture::new("/a/c", &[(2, "/a/c")]);
for raw in 0..2 {
root2.deliver(&mut ledger, &FakeEvent::change("/a/c/f"), Epoch::new(raw));
}
let r1 = ledger.repoint(s1);
let r2 = ledger.repoint(s2);
assert_eq!(
r1,
Epoch::new(5),
"s1's Rescan is one past its own high-water 4"
);
assert_eq!(
r2,
Epoch::new(2),
"s2's Rescan is one past its own high-water 1"
);
let shared = Fixture::new("/a", &[(1, "/a"), (2, "/a")]);
let delivered = shared.deliver(&mut ledger, &FakeEvent::change("/a/x"), Epoch::new(0));
let stamp_of = |who: Subscription| {
delivered
.iter()
.find(|(s, _)| *s == who)
.map(|(_, e)| *e)
.expect("both subs are covered by /a")
};
assert_eq!(
stamp_of(s1),
Epoch::new(5),
"s1 rebases raw 0 onto its base 5 (independent of s2)"
);
assert_eq!(
stamp_of(s2),
Epoch::new(2),
"s2 rebases raw 0 onto its base 2 (independent of s1)"
);
assert!(stamp_of(s1) >= r1 && stamp_of(s2) >= r2);
}
#[test]
fn fs_rescan_is_stamped_and_dominates_prior_stream() {
let mut ledger = EpochLedger::new();
let fx = Fixture::new("/a", &[(1, "/a"), (2, "/a/b/deep")]);
let (s1, s2) = (sub(1), sub(2));
fx.deliver(&mut ledger, &FakeEvent::change("/a/x"), Epoch::new(0));
let prior = fx.deliver(&mut ledger, &FakeEvent::change("/a/x"), Epoch::new(1));
assert_eq!(
prior,
vec![(s1, Epoch::new(1))],
"plain event only covers /a"
);
let delivered = fx.deliver(&mut ledger, &FakeEvent::rescan("/a/x"), Epoch::new(2));
let stamp_of = |who: Subscription| delivered.iter().find(|(s, _)| *s == who).map(|(_, e)| *e);
assert_eq!(
stamp_of(s1),
Some(Epoch::new(2)),
"the Rescan reaches /a and dominates its prior stamp of 1"
);
assert_eq!(
stamp_of(s2),
Some(Epoch::new(2)),
"the Rescan reaches /a/b/deep despite coverage (loss is never narrowed away)"
);
assert!(
stamp_of(s1).unwrap() > Epoch::new(1),
"the fs Rescan's stamp dominates /a's prior stream"
);
}
#[test]
fn move_decomposition_stamps_each_projection() {
let mut ledger = EpochLedger::new();
let (s1, s2) = (sub(1), sub(2));
let fx = Fixture::new("/a", &[(1, "/a/src"), (2, "/a/dst")]);
fx.deliver(&mut ledger, &FakeEvent::change("/a/src/x"), Epoch::new(3)); fx.deliver(&mut ledger, &FakeEvent::change("/a/dst/x"), Epoch::new(1));
let out = fx.deliver_projected(
&mut ledger,
&FakeEvent::moved("/a/src/f", "/a/dst/f"),
Epoch::new(4),
);
let find = |who: Subscription| out.iter().find(|(s, _, _)| *s == who).copied();
let (_, p1, e1) = find(s1).expect("the source-sub is served");
assert_eq!(
p1,
Projection::MoveOut,
"source-sub gets the move-out Removed"
);
assert_eq!(e1, Epoch::new(4), "…stamped in s1's space (base 0 + raw 4)");
let (_, p2, e2) = find(s2).expect("the dest-sub is served");
assert_eq!(p2, Projection::MoveIn, "dest-sub gets the move-in Created");
assert_eq!(e2, Epoch::new(4), "…stamped in s2's space (base 0 + raw 4)");
}
#[test]
fn shed_rescan_dominates_prior_stream_and_same_root_deltas_sort_at_or_above() {
let mut ledger = EpochLedger::new();
let s = sub(1);
let root = Fixture::new("/a", &[(1, "/a")]);
let mut pre = Vec::new();
for raw in 0..5 {
let delivered = root.deliver(&mut ledger, &FakeEvent::change("/a/f"), Epoch::new(raw));
pre.push(delivered[0].1);
}
assert_eq!(
pre,
vec![
Epoch::new(0),
Epoch::new(1),
Epoch::new(2),
Epoch::new(3),
Epoch::new(4),
],
"pre-shed stamps track the raw fs epochs from base START"
);
let rescan = ledger.shed_rescan(s);
assert_eq!(rescan, Epoch::new(5), "shed_rescan = high_water.next()");
assert!(
pre.iter().all(|&e| e < rescan),
"the shed Rescan strictly dominates every pre-shed delivery"
);
let mut post = Vec::new();
for raw in 5..8 {
let delivered = root.deliver(&mut ledger, &FakeEvent::change("/a/f"), Epoch::new(raw));
post.push(delivered[0].1);
}
assert_eq!(
post,
vec![Epoch::new(5), Epoch::new(6), Epoch::new(7)],
"same-root post-shed deltas stamp base + raw (base unchanged)"
);
assert!(
post.iter().all(|&e| e >= rescan),
"no same-root delta after the shed sorts BELOW the Rescan (the non-rebasing guarantee)"
);
let rescan2 = ledger.shed_rescan(s);
assert_eq!(
rescan2,
Epoch::new(8),
"a second shed mints one past the new high-water (strictly increasing)"
);
assert!(rescan2 > rescan, "sheds are monotone");
}
#[test]
fn post_shed_same_generation_event_is_not_dominated_by_the_shed_rescan() {
let mut ledger = EpochLedger::new();
let s = sub(1);
let root = Fixture::new("/a", &[(1, "/a")]);
for _ in 0..3 {
let delivered = root.deliver(&mut ledger, &FakeEvent::change("/a/f"), Epoch::new(0));
assert_eq!(
delivered[0].1,
Epoch::new(0),
"same-generation events stamp base + 0"
);
}
let rescan = ledger.shed_rescan(s);
assert_eq!(rescan, Epoch::new(1), "shed_rescan = high_water.next()");
let post = root.deliver(&mut ledger, &FakeEvent::change("/a/f"), Epoch::new(0));
assert_eq!(
post[0].1,
Epoch::new(1),
"the post-shed same-generation event is clamped up to tie the shed Rescan, not base + 0 = 0"
);
assert!(
post[0].1 >= rescan,
"no post-shed same-generation event sorts below the shed Rescan (no silent loss)"
);
}
#[test]
fn source_rescan_strictly_dominates_a_post_shed_same_generation_event() {
let mut ledger = EpochLedger::new();
let s = sub(1);
let root = Fixture::new("/a", &[(1, "/a")]);
root.deliver(&mut ledger, &FakeEvent::change("/a/f"), Epoch::new(0));
let shed = ledger.shed_rescan(s);
assert_eq!(shed, Epoch::new(1), "shed_rescan = high_water.next()");
let post = root.deliver(&mut ledger, &FakeEvent::change("/a/f"), Epoch::new(0));
assert_eq!(
post[0].1,
Epoch::new(1),
"the post-shed ordinary event ties the shed Rescan"
);
let src_rescan = root.deliver(&mut ledger, &FakeEvent::rescan("/a/f"), Epoch::new(0));
assert_eq!(
src_rescan[0].1,
Epoch::new(2),
"the source Rescan is stamped strictly above the post-shed event (high_water.next()), not tying it"
);
assert!(
src_rescan[0].1 > post[0].1,
"a source Rescan strictly dominates every prior delivery (coverage-loss signal preserved)"
);
}
#[test]
fn a_filtered_out_delivery_does_not_advance_high_water() {
let mut ledger = EpochLedger::new();
let s = sub(1);
let root = Fixture::new("/a", &[(1, "/a")]);
let d0 = root.deliver(&mut ledger, &FakeEvent::change("/a/f"), Epoch::new(0));
assert_eq!(d0, vec![(s, Epoch::new(0))]);
let rejected = root.deliver_gated(
&mut ledger,
&FakeEvent::change("/a/f"),
Epoch::new(9),
|_| false,
);
assert!(
rejected.is_empty(),
"the filtered-out delivery is not delivered"
);
let d1 = root.deliver(&mut ledger, &FakeEvent::change("/a/f"), Epoch::new(1));
assert_eq!(
d1,
vec![(s, Epoch::new(1))],
"a rejected delivery did not perturb the subscription's epoch high-water"
);
}