tributaries 0.1.0

Cross-platform filesystem notification: overlapping watch subscriptions are subsumed into disjoint roots, and every event is attributed back to each subscription covering it.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
use core::{num::NonZeroU64, time::Duration};
use std::{
  ffi::OsString,
  path::{Path, PathBuf},
  time::Instant,
};

use tributary_proto::{Epoch, Location, ScopeId};

use super::Coalescer;
use crate::{
  event::{Event, EventKind},
  options::{Debounce, DebounceConfig},
  subscription::Subscription,
};

/// The delivered-event type this coalescer buffers: the fs `C = OsString`, `V = ()`.
type Ev = Event<OsString, ()>;

/// A subscription with the given non-zero id.
fn sub(id: u64) -> Subscription {
  Subscription::for_test(ScopeId::new(NonZeroU64::new(id).expect("nonzero id")))
}

/// A path's `OsString` components — the located-key form the fs source keys on.
fn key(path: &str) -> Vec<OsString> {
  Path::new(path)
    .components()
    .map(|c| c.as_os_str().to_os_string())
    .collect()
}

/// A synthetic event of `kind` for `s` at `path`, stamped `epoch`. The coalescer only
/// reads `subscription`/`key`/`kind`/`epoch`/`location`, so a synthetic stand-in
/// exercises it without any real source's private event constructor. Its `location`
/// is a fresh (root-anchored) one — the coalescer never keys on `location`.
fn ev(s: Subscription, path: &str, kind: EventKind<OsString>, epoch: u64) -> Ev {
  Event::synthetic(s, key(path), Location::new(), kind, Epoch::new(epoch))
}

fn created(s: Subscription, path: &str, epoch: u64) -> Ev {
  ev(s, path, EventKind::Created, epoch)
}

fn modified(s: Subscription, path: &str, epoch: u64) -> Ev {
  ev(s, path, EventKind::Modified, epoch)
}

fn removed(s: Subscription, path: &str, epoch: u64) -> Ev {
  ev(s, path, EventKind::Removed, epoch)
}

/// A synthetic whole-move fixture: an [`EventKind::Moved`] whose in-kind `from` is the
/// rename source — directly constructible now that the umbrella owns the vocabulary.
/// The coalescer keys on `move_from` (the kind's payload); tests inspect it via
/// [`is_moved`] / `move_from`.
fn moved(s: Subscription, path: &str, from: &str, epoch: u64) -> Ev {
  ev(s, path, EventKind::Moved { from: key(from) }, epoch)
}

/// Whether `ev` is a move — the same wrapper-level detector the coalescer uses.
fn is_moved(ev: &Ev) -> bool {
  ev.move_from().is_some()
}

/// The move source of `ev` as a path — reconstructed from its located source key.
fn move_from_path(ev: &Ev) -> PathBuf {
  PathBuf::from_iter(ev.move_from().expect("a move source"))
}

fn rescan(s: Subscription, path: &str, epoch: u64) -> Ev {
  ev(s, path, EventKind::Rescan, epoch)
}

/// A short settle window and a hold cap a few windows out — round numbers so the
/// deadline arithmetic in the tests is obvious.
fn config() -> DebounceConfig {
  DebounceConfig::new()
    .with_quiet_window(Duration::from_millis(10))
    .with_max_hold(Duration::from_millis(100))
}

/// A fixed clock origin plus a `+ms` helper, so every test uses an explicit manual
/// clock and never reads real time.
struct Clock {
  origin: Instant,
}

impl Clock {
  fn new() -> Self {
    Self {
      origin: Instant::now(),
    }
  }

  /// `origin + ms` milliseconds.
  fn at(&self, ms: u64) -> Instant {
    self.origin + Duration::from_millis(ms)
  }
}

/// Drain everything due at `now` into a fresh `Vec`.
fn drain(coalescer: &mut Coalescer<OsString, ()>, now: Instant) -> Vec<Ev> {
  let mut out = Vec::new();
  coalescer.drain_ready(now, &mut out);
  out
}

// -------------------------------------------------------------------------------
// Collapse table (design §6): one test per named row.
// -------------------------------------------------------------------------------

#[test]
fn created_then_modified_stays_created() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config()));
  let s = sub(1);

  assert!(c.admit(created(s, "/a/f", 1), clk.at(0)).is_none());
  assert!(c.admit(modified(s, "/a/f", 2), clk.at(1)).is_none());

  // Nothing before the settle window; one Created after it.
  assert!(
    drain(&mut c, clk.at(5)).is_empty(),
    "held during the window"
  );
  let out = drain(&mut c, clk.at(11));
  assert_eq!(out.len(), 1, "the burst collapses to a single event");
  assert!(
    out[0].kind().is_created(),
    "a created-then-modified stays Created"
  );
  assert_eq!(out[0].path(), PathBuf::from("/a/f"));
}

#[test]
fn modified_coalesces() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config()));
  let s = sub(1);

  for (n, epoch) in [1, 2, 3, 4].into_iter().enumerate() {
    assert!(
      c.admit(modified(s, "/a/f", epoch), clk.at(n as u64))
        .is_none()
    );
  }
  let out = drain(&mut c, clk.at(20));
  assert_eq!(out.len(), 1, "four Modifieds coalesce to one");
  assert!(out[0].kind().is_modified());
}

#[test]
fn created_then_removed_annihilates() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config()));
  let s = sub(1);

  assert!(c.admit(created(s, "/a/tmp", 1), clk.at(0)).is_none());
  assert!(c.admit(removed(s, "/a/tmp", 2), clk.at(1)).is_none());

  // A file that lived and died inside the window emits NOTHING — and nothing is left
  // to ever emit.
  assert!(
    drain(&mut c, clk.at(50)).is_empty(),
    "the transient annihilates"
  );
  assert_eq!(c.next_deadline(), None, "no entry lingers");
}

#[test]
fn modified_then_removed_is_removed() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config()));
  let s = sub(1);

  assert!(c.admit(modified(s, "/a/f", 1), clk.at(0)).is_none());
  assert!(c.admit(removed(s, "/a/f", 2), clk.at(1)).is_none());

  let out = drain(&mut c, clk.at(20));
  assert_eq!(out.len(), 1);
  assert!(
    out[0].kind().is_removed(),
    "modified-then-removed nets to Removed"
  );
}

#[test]
fn removed_then_created_is_modified() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config()));
  let s = sub(1);

  assert!(c.admit(removed(s, "/a/f", 1), clk.at(0)).is_none());
  assert!(c.admit(created(s, "/a/f", 2), clk.at(1)).is_none());

  let out = drain(&mut c, clk.at(20));
  assert_eq!(out.len(), 1);
  assert!(
    out[0].kind().is_modified(),
    "removed-then-created churn nets to a synthetic Modified"
  );
  assert_eq!(out[0].path(), PathBuf::from("/a/f"));
  assert_eq!(
    out[0].change_id(),
    None,
    "the synthesized Modified carries no change id"
  );
}

// -------------------------------------------------------------------------------
// Invariants that override coalescing (design §6).
// -------------------------------------------------------------------------------

#[test]
fn moved_is_atomic_and_flushes() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config()));
  let s = sub(1);

  // Buffer changes at both endpoints of the coming rename.
  assert!(c.admit(modified(s, "/a/src", 1), clk.at(0)).is_none());
  assert!(c.admit(created(s, "/a/dst", 2), clk.at(1)).is_none());

  // A rename /a/src -> /a/dst. It is atomic: it flushes both buffered endpoints and
  // emits WHOLE, undelayed — never split, never coalesced.
  assert!(
    c.admit(moved(s, "/a/dst", "/a/src", 3), clk.at(2))
      .is_none()
  );

  let out = drain(&mut c, clk.at(2));
  // The two flushed endpoints, then the Moved (FIFO: flushed-before-the-signal).
  assert_eq!(
    out.len(),
    3,
    "both buffered endpoints flush plus the Moved emits"
  );
  let moved_events: Vec<_> = out.iter().filter(|e| is_moved(e)).collect();
  assert_eq!(moved_events.len(), 1, "the Moved emits exactly once, whole");
  let m = moved_events[0];
  assert_eq!(m.path(), PathBuf::from("/a/dst"), "the rename destination");
  assert_eq!(
    move_from_path(m),
    PathBuf::from("/a/src"),
    "the rename source is intact — the Moved was not split"
  );
  assert_eq!(
    c.next_deadline(),
    None,
    "nothing lingers after the atomic flush"
  );
}

#[test]
fn moved_emits_undelayed_not_after_a_settle_window() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config())); // quiet 10ms
  let s = sub(1);

  // A rename with NOTHING else buffered for the subscription: it must be due at its own
  // instant (undelayed), not after the quiet window — unlike a plain lifecycle change,
  // which would settle for `quiet_window` first.
  assert!(
    c.admit(moved(s, "/a/dst", "/a/src", 1), clk.at(0))
      .is_none()
  );

  assert_eq!(
    c.next_deadline(),
    Some(clk.at(0)),
    "a Moved is due immediately (its ready instant), not at now + quiet_window"
  );
  let out = drain(&mut c, clk.at(0));
  assert_eq!(out.len(), 1, "the undelayed Moved is due at once");
  assert!(is_moved(&out[0]));
  assert_eq!(c.next_deadline(), None, "nothing lingers");
}

#[test]
fn debounced_moved_flushes_subscription_buffer_preserving_epoch_order() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config()));
  let s = sub(1);

  // Buffer an older-epoch change at an UNRELATED path (neither the coming rename's
  // source nor its destination). The pre-fix Moved path flushed only its two endpoint
  // paths, so this entry would stay buffered and drain LATER than the immediate Moved —
  // its epoch 1 delivered after the Moved's epoch 2, i.e. epochs going backwards.
  assert!(c.admit(modified(s, "/a/other", 1), clk.at(0)).is_none());

  // A rename at other paths (epoch 2) — immediate. It must flush the whole subscription
  // buffer first, so /a/other's epoch-1 Modified emits BEFORE the Moved.
  assert!(
    c.admit(moved(s, "/a/dst", "/a/src", 2), clk.at(1))
      .is_none()
  );

  let out = drain(&mut c, clk.at(1));
  assert_eq!(
    out.len(),
    2,
    "the unrelated buffered entry is flushed alongside the immediate Moved"
  );
  // FIFO: the flushed older-epoch entry precedes the Moved that flushed it.
  assert!(
    !is_moved(&out[0]),
    "the flushed older-epoch buffered entry emits first"
  );
  assert_eq!(out[0].path(), PathBuf::from("/a/other"));
  assert_eq!(out[0].epoch(), Epoch::new(1), "the older buffered epoch");
  assert!(
    is_moved(&out[1]),
    "the immediate Moved emits after the flush"
  );
  assert_eq!(out[1].epoch(), Epoch::new(2), "the newer Moved epoch");
  // The delivered epochs are monotone-nondecreasing (1 then 2), not backwards.
  assert!(
    out.windows(2).all(|w| w[0].epoch() <= w[1].epoch()),
    "a debounced Moved never delivers an older buffered entry's epoch after itself"
  );
  assert_eq!(
    c.next_deadline(),
    None,
    "the whole subscription was flushed"
  );
}

#[test]
fn rescan_flushes_and_bypasses() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config()));
  let s = sub(1);

  // Buffer a couple of bursts for this subscription.
  assert!(c.admit(modified(s, "/a/one", 1), clk.at(0)).is_none());
  assert!(c.admit(created(s, "/a/two", 2), clk.at(1)).is_none());

  // A Rescan stamped with an umbrella epoch that DOMINATES the buffered stamps.
  assert!(c.admit(rescan(s, "/a", 9), clk.at(2)).is_none());

  let out = drain(&mut c, clk.at(2));
  // Every buffered entry is flushed AND the Rescan emits — all undelayed.
  assert_eq!(
    out.len(),
    3,
    "both buffered entries flush plus the Rescan emits"
  );
  let rescans: Vec<_> = out.iter().filter(|e| e.is_rescan()).collect();
  assert_eq!(rescans.len(), 1, "exactly one Rescan");
  assert_eq!(
    rescans[0].epoch(),
    Epoch::new(9),
    "the Rescan's upstream umbrella epoch stamp is preserved unchanged"
  );
  // The Rescan is emitted last (FIFO: after the entries it flushed), and its stamp
  // dominates every flushed entry.
  let last = out.last().expect("non-empty");
  assert!(
    last.is_rescan(),
    "the Rescan bypasses to the end of the flush"
  );
  for e in &out {
    assert!(
      e.epoch() <= Epoch::new(9),
      "no flushed entry carries an epoch dominating the Rescan"
    );
  }
  assert_eq!(
    c.next_deadline(),
    None,
    "the Rescan drained everything for the sub"
  );
}

#[test]
fn rescan_only_flushes_its_own_subscription() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config()));
  let (s1, s2) = (sub(1), sub(2));

  assert!(c.admit(modified(s1, "/a/f", 1), clk.at(0)).is_none());
  assert!(c.admit(modified(s2, "/b/f", 1), clk.at(0)).is_none());

  // A Rescan for s1 must not disturb s2's buffered burst.
  assert!(c.admit(rescan(s1, "/a", 5), clk.at(1)).is_none());

  let out = drain(&mut c, clk.at(1));
  assert_eq!(out.len(), 2, "s1's flushed burst plus its Rescan");
  assert!(
    out.iter().all(|e| e.subscription() == s1),
    "only s1 is touched"
  );
  // s2's burst still settles on its own timer.
  let out2 = drain(&mut c, clk.at(20));
  assert_eq!(
    out2.len(),
    1,
    "s2's burst is untouched and settles normally"
  );
  assert_eq!(out2[0].subscription(), s2);
}

/// Regression (forward-compat, design §6): the coalescer coalesces ONLY the known
/// lifecycle kinds (`Created` / `Modified` / `Removed`). The umbrella's own `EventKind` is
/// `#[non_exhaustive]`; a non-lifecycle kind must NOT be folded into the collapse table (whose
/// default arm would relabel or drop it), but flushed-and-emitted immediately — the same path as
/// `Moved`/`Rescan` — so it is delivered in-order and never collapsed.
///
/// LIMITATION: a truly-unknown future `EventKind` cannot be constructed, even from within the
/// defining crate — the enum has no variant beyond the five known ones, and every one of those
/// dispatches to a dedicated `admit` branch (the `matches!` trio, `move_from`, `is_rescan`), so
/// no buildable input reaches the unknown-kind `else` arm today. This test therefore exercises
/// the observable classification the arm generalizes, using a `Moved` (a constructible
/// non-lifecycle kind) as the representative: a buffered lifecycle entry is flushed AHEAD of the
/// immediately-emitted non-lifecycle event, and neither is collapsed — the exact flush-and-emit
/// machinery the `else` arm routes a future variant through. The unknown-kind arm itself is
/// verified by inspection (it mirrors this `Moved`/`Rescan` path).
#[test]
fn a_non_lifecycle_kind_flushes_and_emits_never_coalesced() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config()));
  let s = sub(1);

  // A known lifecycle Modified enters `coalesce` — buffered, nothing ready before its settle.
  assert!(c.admit(modified(s, "/a/f", 1), clk.at(0)).is_none());
  assert!(
    drain(&mut c, clk.at(0)).is_empty(),
    "a lifecycle kind is buffered (enters coalesce), not immediately emitted"
  );

  // A non-lifecycle kind for the same subscription is NOT coalesced onto the buffered entry: it
  // flushes the subscription's buffer and emits immediately, both undelayed and in-order.
  assert!(
    c.admit(moved(s, "/a/dst", "/a/src", 2), clk.at(1))
      .is_none()
  );
  let out = drain(&mut c, clk.at(1));
  assert_eq!(
    out.len(),
    2,
    "the buffered lifecycle entry and the non-lifecycle kind BOTH emit — neither coalesced away"
  );
  assert!(
    out[0].kind().is_modified() && !is_moved(&out[0]),
    "the buffered lifecycle Modified is flushed FIRST (older-epoch, ahead of the immediate emit)"
  );
  assert!(
    is_moved(&out[1]),
    "the non-lifecycle kind emits immediately AFTER the flushed entry"
  );
  assert!(
    out[0].epoch() < out[1].epoch(),
    "epochs stay monotone: the flushed buffered entry precedes the immediate emit (design §8)"
  );
  assert!(
    c.next_deadline().is_none(),
    "nothing lingers buffered — the non-lifecycle kind was not held for a settle window"
  );
}

#[test]
fn max_hold_forces_emission() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config())); // quiet 10ms, max_hold 100ms
  let s = sub(1);

  // Touch the path every 5 ms so the 10 ms settle window NEVER elapses on its own.
  let mut t = 0;
  while t <= 130 {
    assert!(c.admit(modified(s, "/a/f", t / 5 + 1), clk.at(t)).is_none());
    // Before the hold cap (first_seen=0 + 100ms) nothing emits.
    if t < 100 {
      assert!(
        drain(&mut c, clk.at(t)).is_empty(),
        "still held before the max_hold cap at t={t}"
      );
    }
    t += 5;
  }

  // The entry was forced out at first_seen + max_hold = 100 ms regardless of the
  // still-arriving touches — a continuously-touched path cannot settle forever.
  // Drain at the cap: exactly one coalesced Modified.
  let out = drain(&mut c, clk.at(100));
  assert_eq!(
    out.len(),
    1,
    "the bounded hold forces one emission at the cap"
  );
  assert!(out[0].kind().is_modified());
}

// -------------------------------------------------------------------------------
// Epoch preservation (design §8).
// -------------------------------------------------------------------------------

#[test]
fn coalesced_pair_emits_with_the_newest_stamp() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config()));
  let s = sub(1);

  // Monotone stamps within the burst; the emitted event must carry the NEWEST (4).
  assert!(c.admit(created(s, "/a/f", 2), clk.at(0)).is_none());
  assert!(c.admit(modified(s, "/a/f", 3), clk.at(1)).is_none());
  assert!(c.admit(modified(s, "/a/f", 4), clk.at(2)).is_none());

  let out = drain(&mut c, clk.at(20));
  assert_eq!(out.len(), 1);
  assert_eq!(
    out[0].epoch(),
    Epoch::new(4),
    "the coalesced event keeps the newest observation's umbrella stamp"
  );
  assert!(
    out[0].kind().is_created(),
    "…while still reporting the collapsed kind"
  );
}

#[test]
fn become_modified_keeps_the_newest_stamp() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config()));
  let s = sub(1);

  // Removed(5) then Created(7) → synthetic Modified carrying the newest stamp (7).
  assert!(c.admit(removed(s, "/a/f", 5), clk.at(0)).is_none());
  assert!(c.admit(created(s, "/a/f", 7), clk.at(1)).is_none());

  let out = drain(&mut c, clk.at(20));
  assert_eq!(out.len(), 1);
  assert!(out[0].kind().is_modified());
  assert_eq!(
    out[0].epoch(),
    Epoch::new(7),
    "the synthetic Modified carries the newest stamp"
  );
}

#[test]
fn post_rescan_emission_never_carries_a_dominated_epoch() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config()));
  let s = sub(1);

  // A Rescan at umbrella epoch 10, then genuine post-Rescan changes rebased ABOVE it
  // upstream (epoch 11, 12). A conforming consumer must never see a post-Rescan event
  // dominated by the Rescan.
  assert!(c.admit(rescan(s, "/a", 10), clk.at(0)).is_none());
  assert!(c.admit(modified(s, "/a/f", 11), clk.at(1)).is_none());
  assert!(c.admit(modified(s, "/a/f", 12), clk.at(2)).is_none());

  // The Rescan is already ready (undelayed); the burst settles after its window.
  let rescan_out = drain(&mut c, clk.at(2));
  assert_eq!(rescan_out.len(), 1, "the Rescan emits undelayed");
  assert_eq!(rescan_out[0].epoch(), Epoch::new(10));

  let burst_out = drain(&mut c, clk.at(20));
  assert_eq!(burst_out.len(), 1, "the post-Rescan burst settles");
  assert!(
    burst_out[0].epoch() >= Epoch::new(10),
    "the post-Rescan emission is NOT dominated by the Rescan"
  );
  assert_eq!(
    burst_out[0].epoch(),
    Epoch::new(12),
    "…and carries the newest stamp"
  );
}

// -------------------------------------------------------------------------------
// next_deadline / drain ordering.
// -------------------------------------------------------------------------------

#[test]
fn next_deadline_is_the_earliest_pending() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config()));
  let s = sub(1);

  assert_eq!(c.next_deadline(), None, "empty: no deadline");

  assert!(c.admit(modified(s, "/a/f", 1), clk.at(0)).is_none());
  assert_eq!(
    c.next_deadline(),
    Some(clk.at(10)),
    "one buffered entry: due at now + quiet_window"
  );

  // A Rescan makes something immediately ready — the deadline drops to that ready
  // instant (in the past relative to the burst's), so the driver drains at once.
  assert!(c.admit(rescan(s, "/b", 3), clk.at(1)).is_none());
  assert_eq!(
    c.next_deadline(),
    Some(clk.at(1)),
    "a ready item pulls the deadline to its (earlier) ready instant"
  );
}

#[test]
fn flush_all_drains_everything_for_stream_close() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config()));
  let s = sub(1);

  assert!(c.admit(modified(s, "/a/f", 1), clk.at(0)).is_none());
  assert!(c.admit(created(s, "/b/g", 2), clk.at(0)).is_none());

  // On stream close no further change can settle the bursts — flush the tail.
  let mut out = Vec::new();
  c.flush_all(&mut out);
  assert_eq!(
    out.len(),
    2,
    "every still-settling entry is force-emitted on close"
  );
  assert_eq!(c.next_deadline(), None, "the coalescer is left empty");
}

/// `drop_subscription` (the driver's parked-overflow-`Rescan` path, design backpressure
/// doc) discards every buffered AND ready entry for one subscription — now dominated by the
/// parked `Rescan` — while leaving every other subscription untouched. Unlike the inline
/// `Rescan`/`Moved` flush, it drops rather than enqueues (the parked `Rescan` cannot be
/// delivered inline, so re-emitting the suspect deltas would deliver a stale epoch after
/// it).
#[test]
fn drop_subscription_discards_only_that_subscriptions_entries() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config()));
  let (s1, s2) = (sub(1), sub(2));

  // A buffered entry for s1, then a Rescan for s1 flushes it into the ready queue and
  // enqueues itself — so s1 now holds entries in the READY queue…
  assert!(c.admit(modified(s1, "/a/f", 1), clk.at(0)).is_none());
  assert!(c.admit(rescan(s1, "/a", 2), clk.at(0)).is_none());
  // …and a fresh buffered entry after the flush — so s1 also holds a BUFFER entry.
  assert!(c.admit(modified(s1, "/a/h", 3), clk.at(0)).is_none());
  // An unrelated subscription's buffered entry, which the drop must not touch.
  assert!(c.admit(modified(s2, "/b/f", 4), clk.at(0)).is_none());
  assert!(
    c.next_deadline().is_some(),
    "entries are pending before the drop"
  );

  // Drop s1: its buffered (/a/h) AND ready (/a/f, the Rescan) entries all vanish.
  c.drop_subscription(s1);

  let out = drain(&mut c, clk.at(1000));
  assert_eq!(out.len(), 1, "only s2's entry survives the drop of s1");
  assert_eq!(
    out[0].subscription(),
    s2,
    "the survivor belongs to the un-dropped subscription"
  );
  assert_eq!(out[0].path(), Path::new("/b/f"));
  assert_eq!(
    c.next_deadline(),
    None,
    "no s1 entry lingers in the buffer or ready queue after the drop"
  );
}

// -------------------------------------------------------------------------------
// Per-subscription policy resolution (design §6): Inherit / Off / Custom,
// the per-sub max_buffered cap + its counter, and the forget-vs-drop split.
// -------------------------------------------------------------------------------

/// An `Off` override passes its events through undelayed and uncollapsed, in admission
/// order, while an inheriting sibling's burst keeps settling under the global default.
#[test]
fn off_override_passes_through_while_inherit_sibling_settles() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config())); // global quiet 10ms
  let (raw, settled) = (sub(1), sub(2));
  c.set_policy(raw, Debounce::Off);

  // Interleave: the settling sibling first, then three raw events — two to the SAME key,
  // which a settling policy would collapse.
  assert!(c.admit(modified(settled, "/s/f", 1), clk.at(0)).is_none());
  assert!(c.admit(modified(raw, "/r/a", 1), clk.at(0)).is_none());
  assert!(c.admit(modified(raw, "/r/a", 2), clk.at(1)).is_none());
  assert!(c.admit(created(raw, "/r/b", 3), clk.at(1)).is_none());

  // The raw events are due at their own admission instants — not at now + quiet_window.
  assert_eq!(
    c.next_deadline(),
    Some(clk.at(0)),
    "a pass-through event is ready the instant it is admitted"
  );
  let out = drain(&mut c, clk.at(1));
  assert_eq!(out.len(), 3, "every raw event emits — none collapsed");
  assert!(
    out.iter().all(|e| e.subscription() == raw),
    "only the Off subscription's events ride through"
  );
  assert_eq!(
    (out[0].epoch(), out[1].epoch(), out[2].epoch()),
    (Epoch::new(1), Epoch::new(2), Epoch::new(3)),
    "pass-through preserves per-subscription admission (= epoch) order"
  );

  // The inheriting sibling still settles under the global window.
  assert!(
    drain(&mut c, clk.at(5)).is_empty(),
    "the settling sibling is still held"
  );
  let out = drain(&mut c, clk.at(11));
  assert_eq!(out.len(), 1, "the sibling's burst settles normally");
  assert_eq!(out[0].subscription(), settled);
}

/// A `Custom` override settles under its OWN windows, not the watcher-global default's.
#[test]
fn custom_override_settles_under_its_own_windows() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config())); // global quiet 10ms
  let (custom, inherit) = (sub(1), sub(2));
  c.set_policy(
    custom,
    Debounce::Custom(config().with_quiet_window(Duration::from_millis(40))),
  );

  assert!(c.admit(modified(custom, "/c/f", 1), clk.at(0)).is_none());
  assert!(c.admit(modified(inherit, "/i/f", 1), clk.at(0)).is_none());

  // The inheriting entry is due at the global 10ms; the custom one at its own 40ms.
  let out = drain(&mut c, clk.at(11));
  assert_eq!(out.len(), 1, "only the inheriting entry is due at 10ms");
  assert_eq!(out[0].subscription(), inherit);
  assert!(
    drain(&mut c, clk.at(39)).is_empty(),
    "the custom entry still settles under its longer window"
  );
  let out = drain(&mut c, clk.at(41));
  assert_eq!(out.len(), 1, "the custom entry emits at its own deadline");
  assert_eq!(out[0].subscription(), custom);
}

/// A `Custom` override ENABLES settling on a coalescer whose global default is `None`
/// (the lazy override-only instantiation), while a sibling inheriting nothing passes
/// through raw.
#[test]
fn custom_override_enables_settling_when_global_is_off() {
  let clk = Clock::new();
  let mut c = Coalescer::new(None);
  let (custom, inherit) = (sub(1), sub(2));
  c.set_policy(custom, Debounce::Custom(config()));

  // The custom subscription's burst buffers and collapses…
  assert!(c.admit(modified(custom, "/c/f", 1), clk.at(0)).is_none());
  assert!(c.admit(modified(custom, "/c/f", 2), clk.at(1)).is_none());
  // …while the inheriting sibling resolves to the disabled default: pass-through.
  assert!(c.admit(modified(inherit, "/i/f", 1), clk.at(1)).is_none());

  let out = drain(&mut c, clk.at(1));
  assert_eq!(out.len(), 1, "inherit-of-nothing rides through undelayed");
  assert_eq!(out[0].subscription(), inherit);

  let out = drain(&mut c, clk.at(20));
  assert_eq!(out.len(), 1, "the custom burst settled to one emission");
  assert_eq!(out[0].subscription(), custom);
  assert_eq!(out[0].epoch(), Epoch::new(2), "carrying the newest stamp");
}

/// `set_policy(Inherit)` removes a previously-registered override: the subscription
/// falls back to the watcher-global default.
#[test]
fn set_policy_inherit_removes_the_override() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config()));
  let s = sub(1);

  c.set_policy(s, Debounce::Off);
  assert!(c.admit(modified(s, "/a/f", 1), clk.at(0)).is_none());
  assert_eq!(
    drain(&mut c, clk.at(0)).len(),
    1,
    "the Off override passes through"
  );

  c.set_policy(s, Debounce::Inherit);
  assert!(!c.has_policy(s), "Inherit removes the override entry");
  assert!(c.admit(modified(s, "/a/g", 2), clk.at(1)).is_none());
  assert!(
    drain(&mut c, clk.at(1)).is_empty(),
    "back under the global default: the event settles"
  );
  assert_eq!(drain(&mut c, clk.at(20)).len(), 1);
}

/// A `Custom` policy's `max_buffered` caps THAT subscription's own fresh entries: the
/// overflow signals the shed, and the driver's purge (`drop_subscription`) zeroes the
/// counter so the cap re-arms from zero.
#[test]
fn per_sub_cap_sheds_and_counter_zeroes_after_the_purge() {
  let clk = Clock::new();
  let mut c = Coalescer::new(None); // structural cap = DEFAULT_MAX_BUFFERED, not in play
  let s = sub(1);
  c.set_policy(s, Debounce::Custom(config().with_max_buffered(2)));

  assert!(c.admit(modified(s, "/k0", 1), clk.at(0)).is_none());
  assert!(c.admit(modified(s, "/k1", 2), clk.at(0)).is_none());
  assert_eq!(
    c.per_sub_len.get(&s).copied(),
    Some(2),
    "two fresh entries counted"
  );
  assert_eq!(
    c.admit(modified(s, "/k2", 3), clk.at(0)),
    Some(s),
    "the third fresh entry overflows the per-sub cap and sheds the subscription"
  );

  // The driver's park path purges the shed subscription — the counter zeroes with it.
  c.drop_subscription(s);
  assert!(
    !c.per_sub_len.contains_key(&s),
    "the purge zeroes the fresh-entry counter"
  );
  assert!(c.buffer.is_empty(), "…and frees the buffered entries");

  // The cap re-arms from zero: two fresh admissions fit again before the next shed.
  assert!(c.admit(modified(s, "/k3", 4), clk.at(1)).is_none());
  assert!(c.admit(modified(s, "/k4", 5), clk.at(1)).is_none());
  assert_eq!(c.admit(modified(s, "/k5", 6), clk.at(1)), Some(s));
}

/// Collapsing onto an already-buffered entry is exempt from the per-sub cap, exactly as
/// it is from the structural cap.
#[test]
fn per_sub_cap_exempts_collapses() {
  let clk = Clock::new();
  let mut c = Coalescer::new(None);
  let s = sub(1);
  c.set_policy(s, Debounce::Custom(config().with_max_buffered(2)));

  assert!(c.admit(modified(s, "/k0", 1), clk.at(0)).is_none());
  assert!(c.admit(modified(s, "/k1", 2), clk.at(0)).is_none());
  assert!(
    c.admit(modified(s, "/k0", 3), clk.at(0)).is_none(),
    "a collapse onto a buffered entry is admitted at the cap"
  );
  assert_eq!(
    c.per_sub_len.get(&s).copied(),
    Some(2),
    "a collapse never grows the fresh-entry count"
  );
}

/// The fresh-entry counter tracks EVERY buffer-removal path: the annihilating collapse,
/// the settle-timer drain, the `Rescan` flush, and the teardown `flush_all`.
#[test]
fn per_sub_counter_tracks_every_removal_path() {
  let clk = Clock::new();
  let mut c = Coalescer::new(None);
  let s = sub(1);
  c.set_policy(s, Debounce::Custom(config().with_max_buffered(2)));

  // Annihilate: created-then-removed frees its slot.
  assert!(c.admit(created(s, "/a", 1), clk.at(0)).is_none());
  assert!(c.admit(removed(s, "/a", 2), clk.at(0)).is_none());
  assert!(
    !c.per_sub_len.contains_key(&s),
    "the annihilated transient freed its counted slot"
  );

  // Settle drain: a due entry decrements as it emits.
  assert!(c.admit(modified(s, "/b", 3), clk.at(0)).is_none());
  assert_eq!(c.per_sub_len.get(&s).copied(), Some(1));
  assert_eq!(drain(&mut c, clk.at(20)).len(), 1);
  assert!(
    !c.per_sub_len.contains_key(&s),
    "the drained entry decremented its count"
  );

  // Rescan flush: the flushed entries reconcile the count to zero.
  assert!(c.admit(modified(s, "/c", 4), clk.at(21)).is_none());
  assert!(c.admit(modified(s, "/d", 5), clk.at(21)).is_none());
  assert!(c.admit(rescan(s, "/", 6), clk.at(21)).is_none());
  assert!(
    !c.per_sub_len.contains_key(&s),
    "the Rescan flush zeroed the count"
  );
  assert_eq!(drain(&mut c, clk.at(21)).len(), 3);

  // flush_all: the teardown flush clears every count.
  assert!(c.admit(modified(s, "/e", 7), clk.at(22)).is_none());
  let mut out = Vec::new();
  c.flush_all(&mut out);
  assert_eq!(out.len(), 1);
  assert!(
    c.per_sub_len.is_empty(),
    "the teardown flush cleared the counter map"
  );
}

/// The forget-vs-drop split: `drop_subscription` purges buffers but RETAINS the policy
/// (the subscription is still live — a widen re-point / overflow park), while
/// `forget_subscription` retires the policy with the buffers (unwatch / terminal
/// retirement).
#[test]
fn forget_clears_the_policy_drop_retains_it() {
  let clk = Clock::new();
  let mut c = Coalescer::new(None);
  let s = sub(1);
  c.set_policy(s, Debounce::Custom(config()));

  assert!(c.admit(modified(s, "/a/f", 1), clk.at(0)).is_none());
  c.drop_subscription(s);
  assert!(
    c.has_policy(s),
    "drop_subscription keeps the policy — the subscription is still live"
  );
  // Post-drop events still settle under the retained Custom policy.
  assert!(c.admit(modified(s, "/a/g", 2), clk.at(1)).is_none());
  assert!(
    drain(&mut c, clk.at(1)).is_empty(),
    "the retained policy still buffers"
  );
  assert_eq!(drain(&mut c, clk.at(20)).len(), 1);

  c.forget_subscription(s);
  assert!(!c.has_policy(s), "forget_subscription retires the policy");
  // Back to inheriting the (disabled) default: pass-through.
  assert!(c.admit(modified(s, "/a/h", 3), clk.at(21)).is_none());
  assert_eq!(
    drain(&mut c, clk.at(21)).len(),
    1,
    "without the policy the subscription inherits the disabled default"
  );
}

/// With no global default, the coalescer-wide structural bound falls back to
/// [`DebounceConfig::DEFAULT_MAX_BUFFERED`] — a `Custom` override cannot widen it.
#[test]
fn structural_cap_defaults_when_override_only() {
  let clk = Clock::new();
  let mut c = Coalescer::new(None);
  let s = sub(1);
  // A per-sub cap far beyond the structural bound: the structural bound must still shed.
  c.set_policy(s, Debounce::Custom(config().with_max_buffered(usize::MAX)));

  for i in 0..DebounceConfig::DEFAULT_MAX_BUFFERED {
    assert!(
      c.admit(modified(s, &format!("/k{i}"), i as u64 + 1), clk.at(0))
        .is_none(),
      "admission {i} sits under the structural bound"
    );
  }
  assert_eq!(
    c.admit(modified(s, "/overflow", u64::MAX), clk.at(0)),
    Some(s),
    "the coalescer-wide structural bound sheds past DEFAULT_MAX_BUFFERED"
  );
  assert_eq!(c.buffer.len(), DebounceConfig::DEFAULT_MAX_BUFFERED);
}

// -------------------------------------------------------------------------------
// Per-subscription admission-order drains (design §8).
//
// Every multi-entry drain must emit a subscription's entries in admission (= epoch) order,
// NOT BTreeMap path-key order. Each test uses the same fixture: two paths whose lexical
// order OPPOSES their epoch order — `/z` admitted earlier at the LOWER epoch 1, `/a`
// admitted later at the HIGHER epoch 2 (`/a` < `/z` lexically, but epoch 2 > 1). The
// pre-fix path-key drain emitted `/a` (epoch 2) before `/z` (epoch 1) — a subscription's
// epochs going backwards, which a monotone-epoch consumer silently drops. The fix orders
// by the admission sequence, so `/z` (epoch 1) precedes `/a` (epoch 2).
// -------------------------------------------------------------------------------

#[test]
fn drain_ready_emits_buffered_in_epoch_order_not_path_order() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config()));
  let s = sub(1);

  // `/z` admitted FIRST at the LOWER epoch; `/a` admitted SECOND at the HIGHER epoch.
  assert!(c.admit(modified(s, "/z", 1), clk.at(0)).is_none());
  assert!(c.admit(modified(s, "/a", 2), clk.at(1)).is_none());

  // Both settle by t=20; the settle-timer drain releases them together.
  let out = drain(&mut c, clk.at(20));
  assert_eq!(
    out.len(),
    2,
    "both distinct paths emit (no cross-path collapse)"
  );
  assert_eq!(
    out[0].path(),
    PathBuf::from("/z"),
    "the earlier-admitted lower-epoch path emits first — NOT the lexically-first /a"
  );
  assert_eq!(out[0].epoch(), Epoch::new(1));
  assert_eq!(
    out[1].path(),
    PathBuf::from("/a"),
    "the later-admitted higher-epoch path emits second"
  );
  assert_eq!(out[1].epoch(), Epoch::new(2));
  assert!(
    out.windows(2).all(|w| w[0].epoch() <= w[1].epoch()),
    "a subscription's buffered epochs never emit backwards"
  );
}

#[test]
fn flush_all_emits_buffered_in_epoch_order_not_path_order() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config()));
  let s = sub(1);

  assert!(c.admit(modified(s, "/z", 1), clk.at(0)).is_none());
  assert!(c.admit(modified(s, "/a", 2), clk.at(1)).is_none());

  // Force-emit the still-settling tail (as a stream close would), regardless of deadline.
  let mut out = Vec::new();
  c.flush_all(&mut out);
  assert_eq!(out.len(), 2);
  assert_eq!(
    out[0].path(),
    PathBuf::from("/z"),
    "lower-epoch tail entry first"
  );
  assert_eq!(out[0].epoch(), Epoch::new(1));
  assert_eq!(
    out[1].path(),
    PathBuf::from("/a"),
    "higher-epoch tail entry second"
  );
  assert_eq!(out[1].epoch(), Epoch::new(2));
  assert!(
    out.windows(2).all(|w| w[0].epoch() <= w[1].epoch()),
    "the teardown flush never delivers a subscription's epochs out of order"
  );
}

#[test]
fn rescan_flush_emits_buffered_in_epoch_order_not_path_order() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config()));
  let s = sub(1);

  assert!(c.admit(modified(s, "/z", 1), clk.at(0)).is_none());
  assert!(c.admit(modified(s, "/a", 2), clk.at(1)).is_none());

  // A Rescan (epoch 3) flushes the whole subscription buffer into the ready queue ahead of
  // itself — in admission (seq) order, so the FIFO replay is lowest-epoch-first.
  assert!(c.admit(rescan(s, "/root", 3), clk.at(2)).is_none());

  let out = drain(&mut c, clk.at(2));
  assert_eq!(out.len(), 3, "both flushed entries plus the Rescan");
  assert_eq!(
    out[0].path(),
    PathBuf::from("/z"),
    "lower-epoch buffered entry flushed first"
  );
  assert_eq!(out[0].epoch(), Epoch::new(1));
  assert_eq!(
    out[1].path(),
    PathBuf::from("/a"),
    "higher-epoch buffered entry flushed second"
  );
  assert_eq!(out[1].epoch(), Epoch::new(2));
  assert!(
    out[2].is_rescan(),
    "the Rescan bypasses to the end of the flush"
  );
  assert_eq!(out[2].epoch(), Epoch::new(3));
  assert!(
    out.windows(2).all(|w| w[0].epoch() <= w[1].epoch()),
    "flushed epochs stay monotone: 1, 2, then the Rescan's 3"
  );
}

#[test]
fn moved_flush_emits_buffered_in_epoch_order_not_path_order() {
  let clk = Clock::new();
  let mut c = Coalescer::new(Some(config()));
  let s = sub(1);

  assert!(c.admit(modified(s, "/z", 1), clk.at(0)).is_none());
  assert!(c.admit(modified(s, "/a", 2), clk.at(1)).is_none());

  // A Moved (epoch 3) at unrelated endpoints flushes the WHOLE subscription buffer ahead of
  // itself — the same seq-ordered flush machinery a Rescan uses.
  assert!(c.admit(moved(s, "/dst", "/src", 3), clk.at(2)).is_none());

  let out = drain(&mut c, clk.at(2));
  assert_eq!(out.len(), 3, "both flushed entries plus the Moved");
  assert_eq!(
    out[0].path(),
    PathBuf::from("/z"),
    "lower-epoch buffered entry flushed first"
  );
  assert_eq!(out[0].epoch(), Epoch::new(1));
  assert_eq!(
    out[1].path(),
    PathBuf::from("/a"),
    "higher-epoch buffered entry flushed second"
  );
  assert_eq!(out[1].epoch(), Epoch::new(2));
  assert!(is_moved(&out[2]), "the Moved emits whole after the flush");
  assert_eq!(out[2].epoch(), Epoch::new(3));
  assert!(
    out.windows(2).all(|w| w[0].epoch() <= w[1].epoch()),
    "flushed epochs stay monotone: 1, 2, then the Moved's 3"
  );
}

// -------------------------------------------------------------------------------
// Property tests (design §10): no silent loss, determinism, no dominated post-Rescan.
// -------------------------------------------------------------------------------

mod proptests {
  use super::*;
  use proptest::prelude::*;

  /// A scripted admission: a kind selector (0..5), a small path index, an epoch delta,
  /// and a time delta (ms). Epochs are made monotone-nondecreasing before feeding the
  /// coalescer (the umbrella stamps are monotone per subscription), so a coalescing
  /// pair never straddles a dominance boundary — the invariant the coalescer relies on.
  #[derive(Debug, Clone)]
  struct Step {
    kind: u8,
    path: u8,
    epoch_delta: u16,
    time_delta: u16,
  }

  fn step_strategy() -> impl Strategy<Value = Step> {
    (0u8..5, 0u8..4, 0u16..3, 0u16..30).prop_map(|(kind, path, epoch_delta, time_delta)| Step {
      kind,
      path,
      epoch_delta,
      time_delta,
    })
  }

  /// Build one event for `step` for subscription `s`, at monotone epoch `epoch`.
  /// One fixed subscription keeps the property focused on the collapse/flush logic.
  fn event_for(s: Subscription, step: &Step, epoch: u64) -> Ev {
    let path = format!("/root/p{}", step.path);
    match step.kind {
      0 => created(s, &path, epoch),
      1 => modified(s, &path, epoch),
      2 => removed(s, &path, epoch),
      3 => moved(s, &path, "/root/src", epoch),
      _ => rescan(s, &path, epoch),
    }
  }

  /// Case budget: a full sweep natively, a handful under miri (where each case runs
  /// the interpreter, so the native count would blow the time budget).
  const CASES: u32 = if cfg!(miri) { 4 } else { 200 };

  proptest! {
    // No failure-persistence file: keeps miri from calling getcwd, and keeps the run
    // hermetic.
    #![proptest_config(ProptestConfig { cases: CASES, failure_persistence: None, ..ProptestConfig::default() })]

    /// Every admitted event is accounted for: it is emitted, folded into an emitted
    /// entry, or intentionally annihilated per the create-then-remove row — never
    /// silently dropped. We assert the strong, checkable half: at least one event is
    /// emitted whenever a non-annihilating event was admitted, and the count of
    /// distinct emitted `(path, kind-class)` is bounded by what was admitted.
    #[test]
    fn no_admitted_event_is_silently_lost(steps in prop::collection::vec(step_strategy(), 1..40)) {
      let clk = Clock::new();
      let mut c = Coalescer::new(Some(config()));
      let s = sub(1);

      let mut epoch = 0u64;
      let mut admitted = 0usize;
      let mut emitted = Vec::new();
      let mut t = 0u64;
      for step in &steps {
        epoch += u64::from(step.epoch_delta); // monotone-nondecreasing
        t += u64::from(step.time_delta);
        assert!(c.admit(event_for(s, step, epoch), clk.at(t)).is_none());
        admitted += 1;
        // Drain whatever became ready as time advanced.
        c.drain_ready(clk.at(t), &mut emitted);
      }
      // Flush the settling tail (as a stream close would).
      c.flush_all(&mut emitted);

      // Every emitted event is a real, well-formed delivery for our subscription.
      for e in &emitted {
        prop_assert_eq!(e.subscription(), s);
      }
      // A Moved or Rescan is NEVER coalesced away: each admitted one must appear.
      let admitted_atomic = steps.iter().filter(|st| st.kind >= 3).count();
      let emitted_atomic = emitted.iter().filter(|e| is_moved(e) || e.is_rescan()).count();
      prop_assert_eq!(
        emitted_atomic,
        admitted_atomic,
        "every Moved/Rescan is emitted whole, none coalesced or dropped"
      );
      // The coalescer is fully drained.
      prop_assert_eq!(c.next_deadline(), None);
      let _ = admitted;
    }

    /// Determinism: the same script fed twice yields byte-identical output (no
    /// HashMap-iteration nondeterminism in the drain order).
    #[test]
    fn determinism(steps in prop::collection::vec(step_strategy(), 1..40)) {
      fn run(steps: &[Step]) -> Vec<(PathBuf, &'static str, Epoch)> {
        let clk = Clock::new();
        let mut c = Coalescer::new(Some(config()));
        let s = sub(1);
        let mut epoch = 0u64;
        let mut t = 0u64;
        let mut out = Vec::new();
        for step in steps {
          epoch += u64::from(step.epoch_delta);
          t += u64::from(step.time_delta);
          assert!(c.admit(event_for(s, step, epoch), clk.at(t)).is_none());
          c.drain_ready(clk.at(t), &mut out);
        }
        c.flush_all(&mut out);
        out
          .into_iter()
          .map(|e| (e.path().to_path_buf(), e.kind().as_str(), e.epoch()))
          .collect()
      }
      prop_assert_eq!(run(&steps), run(&steps));
    }

    /// No emission after an immediate (undelayed) one ever carries an epoch it
    /// dominated: once an immediate emission at epoch `r` has drained — a Rescan OR a
    /// Moved, since BOTH now flush the whole subscription buffer before emitting — every
    /// later emission has epoch >= r. This is the per-subscription monotone-epoch
    /// contract across ALL immediate emissions, not just Rescan (the pre-fix Moved
    /// flushed only its two endpoint paths, so an older buffered entry for another path
    /// could drain after the Moved and go backwards).
    #[test]
    fn no_post_immediate_dominated_epoch(steps in prop::collection::vec(step_strategy(), 1..40)) {
      let clk = Clock::new();
      let mut c = Coalescer::new(Some(config()));
      let s = sub(1);
      let mut epoch = 0u64;
      let mut t = 0u64;
      let mut emitted = Vec::new();
      for step in &steps {
        epoch += u64::from(step.epoch_delta);
        t += u64::from(step.time_delta);
        assert!(c.admit(event_for(s, step, epoch), clk.at(t)).is_none());
        c.drain_ready(clk.at(t), &mut emitted);
      }
      c.flush_all(&mut emitted);

      // Walk the emission order; track the highest epoch of any immediate emission
      // (Rescan or Moved) seen so far. Because each flushes its subscription's whole
      // buffer, everything it dominated is emitted BEFORE it — so no LATER emission may
      // carry an epoch < that immediate emission's.
      let mut immediate_hw: Option<Epoch> = None;
      for e in &emitted {
        if let Some(r) = immediate_hw {
          prop_assert!(
            e.epoch() >= r,
            "an emission after an immediate one is dominated by it: {:?} < {:?}",
            e.epoch(),
            r
          );
        }
        if e.is_rescan() || is_moved(e) {
          immediate_hw = Some(immediate_hw.map_or(e.epoch(), |r| r.max(e.epoch())));
        }
      }
    }
  }
}

// -------------------------------------------------------------------------------
// Runtime-touching: the settle timer under paused tokio time (design §6). This is the
// only test that touches a runtime; it drives a `Coalescer` through the exact clock
// bridge the driver uses (`R::now()` → std `Instant`, `sleep_until`/`timeout_at`).
// -------------------------------------------------------------------------------

#[cfg(feature = "tokio")]
mod tokio_timer {
  use super::*;
  use agnostic_lite::{LocalRuntimeLite, RuntimeLite};

  type Rt = agnostic_lite::tokio::TokioRuntime;

  /// A burst of Modifieds to one path collapses to a single delivered event after the
  /// quiet window; a Rescan admitted mid-burst jumps the queue — it drains before the
  /// buffered burst's deadline. Paused time makes the timing exact and instant.
  #[tokio::test(start_paused = true)]
  async fn burst_coalesces_and_rescan_jumps_the_queue() {
    let cfg = config(); // quiet 10ms, max_hold 100ms
    let mut c = Coalescer::new(Some(cfg));
    let s = sub(1);

    // Admit a burst of three Modifieds to one path at the current (paused) instant.
    let start = Rt::now();
    for epoch in 1..=3 {
      assert!(c.admit(modified(s, "/a/f", epoch), start.into()).is_none());
    }

    // The burst is not yet due: draining now yields nothing.
    let mut out = Vec::new();
    c.drain_ready(Rt::now().into(), &mut out);
    assert!(out.is_empty(), "the burst is still settling");

    // A Rescan mid-burst is immediately ready — it must NOT wait for the burst's
    // deadline. Sleep until the coalescer's next deadline (now the ready Rescan) and
    // drain: the flushed burst + the Rescan come out well before quiet_window elapses.
    assert!(c.admit(rescan(s, "/a", 9), Rt::now().into()).is_none());
    let deadline = c.next_deadline().expect("the ready Rescan sets a deadline");
    Rt::sleep_until(deadline.into()).await;
    c.drain_ready(Rt::now().into(), &mut out);

    assert!(
      Rt::now().duration_since(start) < cfg.quiet_window(),
      "the Rescan jumped the queue: drained before the burst's settle window elapsed"
    );
    // The flushed burst collapsed to one Modified, plus the Rescan — the Rescan last.
    assert_eq!(out.len(), 2, "the coalesced burst plus the Rescan");
    assert!(out[0].kind().is_modified(), "the flushed, coalesced burst");
    assert_eq!(out[0].epoch(), Epoch::new(3), "…carrying the newest stamp");
    assert!(
      out[1].is_rescan(),
      "the Rescan, jumped ahead of the burst's own deadline"
    );
    assert_eq!(
      out[1].epoch(),
      Epoch::new(9),
      "its umbrella stamp preserved"
    );
    assert_eq!(c.next_deadline(), None, "everything drained");
  }
}

/// the buffered-entry cap is the structural memory bound in FRONT of the
/// bounded event channel. A high-cardinality burst (10,000 distinct keys) against a
/// cap of 64 never grows the buffer past the cap; every admission past it signals the
/// overflowed subscription (owed a dominating parked Rescan by the driver), and
/// collapses onto existing entries stay exempt at the cap.
#[test]
fn buffered_entry_cap_bounds_high_cardinality_bursts() {
  let now = Instant::now();
  let mut coalescer = Coalescer::new(Some(
    crate::options::DebounceConfig::new().with_max_buffered(64),
  ));
  let s = sub(1);

  let mut overflows = 0usize;
  for i in 0..10_000u32 {
    let outcome = coalescer.admit(created(s, &format!("/k{i}"), u64::from(i) + 1), now);
    assert!(
      coalescer.buffer.len() <= 64,
      "the buffer NEVER exceeds the cap (got {} at admission {i})",
      coalescer.buffer.len()
    );
    if let Some(overflowed) = outcome {
      assert_eq!(overflowed, s, "the shed names the admitting subscription");
      overflows += 1;
    }
  }
  assert_eq!(
    overflows,
    10_000 - 64,
    "every admission past the cap signalled the shed — none was silently dropped"
  );

  // Collapsing onto an ALREADY-buffered entry never counts against the cap: a second
  // observation of a buffered key is admitted even while the buffer sits at the cap.
  assert!(
    coalescer.admit(modified(s, "/k0", 20_000), now).is_none(),
    "a collapse onto an existing entry is exempt at the cap"
  );
  assert_eq!(coalescer.buffer.len(), 64, "still exactly at the cap");
}