gix 0.91.0

Interact with git repositories just like git would
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
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
use gix_error::TestResult;

use super::*;

#[test]
fn dependency_classifier_ignores_unrelated_metadata() {
    let mut layout = layout();
    layout.dependencies.push(Dependency {
        path: "/repo/.git/includes/missing/config".into(),
        kind: DependencyKind::Configuration,
    });
    layout.dependencies.push(Dependency {
        path: "/repo/.git/info/exclude".into(),
        kind: DependencyKind::Ignore,
    });
    for path in [
        ".git/config",
        ".git/config.worktree",
        ".git/HEAD",
        ".git/commondir",
        ".git/gitdir",
        ".git/includes",
        ".git/includes/missing/config",
        ".git/info/exclude",
        ".git",
    ] {
        let (changes, refresh) = classify_dependencies(&Path::new("/repo").join(path), &layout, false);
        assert!(refresh, "{path} can change ignore-aware directory coverage");
        assert_eq!(
            changes.worktree,
            Some(Scope::All),
            "{path} invalidates worktree selection"
        );
    }
    for path in [
        "refs",
        "refs/heads/main",
        "rebase-merge",
        "worktrees",
        "worktrees/other/HEAD",
        "info/attributes",
        "logs/HEAD",
        "FETCH_HEAD",
    ] {
        assert_eq!(
            classify_dependencies(&layout.git_dir.join(path), &layout, false),
            (Changes::default(), false),
            "{path} neither publishes metadata nor schedules an unrelated inventory refresh"
        );
    }
}

#[test]
fn all_coverage_classifies_literal_paths_without_inventory() {
    let mut monitor = monitor();
    monitor.options.metadata = false;
    monitor.options.worktree_coverage = Some(WorktreeCoverage::All);
    monitor.layout.common_dir = "/repo/admin".into();
    monitor.layout.index = "/repo/selected-index".into();
    let now = Instant::now();
    let paths = [
        "ignored/deep/file",
        "nested/inside/file",
        "src/.gitignore",
        "src/.gitattributes",
        ".gitmodules",
        "selected-index",
        "selected-index.lock",
    ];
    for path in paths {
        monitor.observe(&modified(Path::new("/repo").join(path)), true, now);
    }
    for path in [
        ".git/index",
        ".git/config",
        "nested/.git/HEAD",
        "nested/.git",
        "admin/refs/main",
    ] {
        monitor.observe(&modified(Path::new("/repo").join(path)), true, now);
    }
    monitor.observe(
        &event("/repo/new-directory", EventKind::Create, PathKind::Directory),
        true,
        now,
    );
    let mut expected: Vec<_> = paths
        .into_iter()
        .map(|path| path_change(path, EventKind::Modify, PathKind::File))
        .collect();
    expected.push(path_change("new-directory", EventKind::Create, PathKind::Directory));
    assert_eq!(
        monitor.pending_worktree,
        Changes {
            worktree: Some(Scope::Paths(expected)),
            ..Changes::default()
        },
        "all coverage includes ignored and nested content but excludes all administrative paths"
    );
    assert!(
        !monitor.inventory_dirty && !monitor.index_dirty && monitor.maintenance.is_none(),
        "recursive native coverage needs no index/ignore inventory for changed files or new directories"
    );
}

#[test]
fn worktree_only_failures_and_manual_refresh_publish_no_metadata() {
    for manual_refresh in [false, true] {
        let now = Instant::now();
        let mut monitor = monitor();
        monitor.options.metadata = false;
        if manual_refresh {
            monitor.rescan();
        } else {
            monitor.coverage_lost(false, now);
            monitor.coverage_lost(true, now);
        }
        let outcome = monitor.service(Instant::now(), || Err(message("unavailable repository").raise()));
        assert_eq!(
            outcome.changes,
            Changes {
                worktree: Some(Scope::All),
                ..Changes::default()
            },
            "loss, manual refresh and recovery errors never publish metadata in worktree-only mode"
        );
        assert_eq!(
            outcome.errors.len(),
            1,
            "filtering metadata does not discard diagnostics"
        );
    }
}

#[test]
fn disabling_worktree_only_releases_dependency_state() -> TestResult {
    let fixture = gix_testtools::scripted_fixture_read_only("make_notify_repo.sh")?;
    let repo = open(&fixture.join("unignored"))?;
    let mut monitor = monitor();
    monitor.options.metadata = false;
    monitor.metadata.watcher = Some(plumbing::Watcher::new(plumbing::Options::default())?);
    monitor.worktree.watcher = Some(plumbing::Watcher::new(plumbing::Options::default())?);
    let options = Options {
        worktree_coverage: None,
        ..monitor.options().clone()
    };
    drop(monitor);
    let mut monitor = repo.monitor(options)?;
    monitor.rescan();
    let now = Instant::now();
    assert!(
        monitor
            .service(now, || panic!("disabled monitoring must not reopen"))
            .changes
            .is_empty(),
        "neither domain publishes when both selections are disabled"
    );
    assert!(
        monitor.metadata.watcher.is_none() && monitor.worktree.watcher.is_none(),
        "disabling worktree-only monitoring also releases internal dependencies"
    );
    assert_eq!(
        monitor.next_timeout(now),
        None,
        "disabled monitoring needs no maintenance wakeups"
    );
    Ok(())
}

#[test]
fn metadata_debounce_has_a_maximum_latency() {
    let now = Instant::now();
    let options = Options::default();
    let mut pending = Pending::default();
    let references = || Changes {
        references: true,
        ..Changes::default()
    };
    pending.add(references(), now, &options, false);
    pending.add(references(), now + Duration::from_millis(90), &options, false);
    pending.add(references(), now + Duration::from_millis(180), &options, false);
    assert!(
        pending.take_due(now + Duration::from_millis(249)).is_empty(),
        "a continuing burst remains pending before its maximum age"
    );
    assert!(
        pending.take_due(now + Duration::from_millis(250)).references,
        "continuous events cannot postpone publication indefinitely"
    );
}

fn layout() -> Layout {
    Layout {
        git_dir: "/repo/.git".into(),
        common_dir: "/repo/.git".into(),
        worktree: Some("/repo".into()),
        index: "/repo/.git/index".into(),
        dependencies: Vec::new(),
    }
}

fn monitor() -> RepositoryMonitor {
    RepositoryMonitor {
        #[cfg(feature = "async-notify")]
        wake: gix_parallel::OwnShared::new(tokio::sync::Notify::new()),
        layout: layout(),
        options: Options::default(),
        metadata: Domain::default(),
        worktree: Domain {
            registered: BTreeMap::from([(PathBuf::from("/repo"), false), (PathBuf::from("/repo/src"), false)]),
            ..Domain::default()
        },
        directories: inventory::directory_lookup([PathBuf::from("/repo"), PathBuf::from("/repo/src")]),
        nested_repositories: HashSet::new(),
        index_fingerprint: None,
        directory_inventory: None,
        changed_directories: HashSet::new(),
        pending_metadata: Pending::default(),
        pending_worktree: Changes::default(),
        pending_statistics: Statistics::default(),
        metadata_dirty: false,
        inventory_dirty: false,
        inventory_verify: false,
        index_dirty: false,
        maintenance: None,
        backlog: false,
        started: false,
        precompose_unicode: false,
        ignore_case: false,
        shared_metadata: false,
        other_worktrees: BTreeMap::new(),
        retired_worktrees: Vec::new(),
        worktree_transition: None,
        maintenance_failed: false,
        undiscovered_worktrees: BTreeMap::new(),
    }
}

fn event(path: impl Into<PathBuf>, kind: EventKind, path_kind: PathKind) -> Event {
    Event {
        paths: vec![path.into()],
        kind,
        path_kind,
    }
}

#[test]
fn additional_paths_keep_component_boundaries_debounce_and_loss_invalidations() {
    let now = Instant::now();
    let expected = Changes {
        operations: true,
        worktree: Some(Scope::All),
        ..Changes::default()
    };
    for (path, ignore_case, matched) in [
        ("/external/state.lock", false, true),
        ("/external/state.lock-other", false, false),
        ("/external", false, true),
        ("/EXTERNAL/STATE.lock", false, false),
        ("/EXTERNAL/STATE.lock", true, true),
    ] {
        let mut monitor = monitor();
        monitor.options.worktree_coverage = None;
        monitor.options.additional_paths = vec![InvalidationPath {
            path: "/external/state.lock".into(),
            changes: expected.clone(),
        }];
        monitor.ignore_case = ignore_case;
        monitor.observe(&event(path, EventKind::Modify, PathKind::File), false, now);
        assert!(
            monitor.take_changes(now).is_empty(),
            "custom metadata respects debounce"
        );
        assert_eq!(
            monitor.take_changes(now + monitor.options.metadata_debounce),
            if matched { expected.clone() } else { Changes::default() },
            "custom paths use native case rules and component boundaries: {path}"
        );
        monitor.coverage_lost(false, now);
        let lost = monitor.take_changes(now);
        assert!(lost.operations, "loss invalidates application operation state");
        assert_eq!(
            lost.worktree, expected.worktree,
            "loss retains custom scopes without built-in checkout coverage"
        );
    }
}

fn modified(path: impl Into<PathBuf>) -> Event {
    event(path, EventKind::Modify, PathKind::File)
}

fn path_change(path: impl Into<BString>, kind: EventKind, path_kind: PathKind) -> PathChange {
    PathChange {
        path: path.into(),
        kind,
        path_kind,
    }
}

fn fixture() -> TestResult<gix_testtools::tempfile::TempDir> {
    let directory = gix_testtools::tempfile::tempdir()?;
    let status = gix_testtools::git_command(directory.path())
        .args(["init", "--quiet", "--initial-branch=main"])
        .status()?;
    assert!(status.success(), "the disposable repository initializes");
    Ok(directory)
}

fn open(path: &Path) -> Result<Repository> {
    crate::open_opts(path, crate::open::Options::isolated())
}

#[test]
fn immediate_metadata_does_not_get_delayed_by_a_later_event() {
    let now = Instant::now();
    let mut pending = Pending::default();
    pending.add(Changes::metadata_all(), now, &Options::default(), true);
    pending.add(
        Changes {
            references: true,
            ..Changes::default()
        },
        now,
        &Options::default(),
        false,
    );
    assert!(
        pending.take_due(now).configuration,
        "a coverage loss is delivered immediately even alongside debounced refs"
    );
}

#[test]
fn metadata_classifier_separates_linked_worktrees_and_ignores_housekeeping() {
    let mut layout = layout();
    layout.git_dir = layout.common_dir.join("worktrees/current");
    layout.index = layout.git_dir.join("index");
    for path in [
        "index",
        "index.lock",
        "logs/HEAD",
        "objects/ab/cdef",
        "description",
        "worktrees/other/index",
        "worktrees/other/logs/HEAD",
        "worktrees/other/refs/worktree/private",
    ] {
        assert!(
            classify_metadata(&layout.common_dir.join(path), &layout, false)
                .0
                .is_empty(),
            "{path} does not invalidate references"
        );
    }
    for path in [
        layout.common_dir.join("refs/heads/main"),
        layout.common_dir.join("worktrees/other/HEAD"),
        layout.git_dir.join("refs/worktree/private"),
    ] {
        assert!(
            classify_metadata(&path, &layout, false).0.references,
            "{} affects reference views",
            path.display()
        );
    }
    let (head, rediscover) = classify_metadata(&layout.git_dir.join("HEAD"), &layout, false);
    assert!(
        head.references && head.configuration && rediscover,
        "HEAD must invalidate and rediscover branch-conditioned configuration"
    );
    let (config, rediscover) = classify_metadata(&layout.git_dir.join("config.worktree"), &layout, false);
    assert!(
        config.configuration && rediscover,
        "worktree configuration has its own invalidation"
    );
    let (operation, rediscover) = classify_metadata(&layout.git_dir.join("rebase-merge"), &layout, false);
    assert!(
        operation.operations && rediscover,
        "new operation directories need recursive registration"
    );
    assert!(
        classify_metadata(&layout.common_dir.join("worktrees/new"), &layout, false)
            .0
            .layout,
        "linked-worktree membership is observed"
    );
}

#[test]
fn file_scopes_are_literal_and_do_not_reopen_a_repository() {
    let now = Instant::now();
    let mut monitor = monitor();
    monitor.observe(&modified("/repo/src/[literal]*"), true, now);
    monitor.observe(&modified("/repo/.git/HEAD"), true, now);
    monitor.observe(&modified("/outside/file"), true, now);
    let changes = monitor
        .service(now, || panic!("ordinary files do not require repository maintenance"))
        .changes;
    assert_eq!(
        changes.worktree,
        Some(Scope::Paths(vec![path_change(
            "src/[literal]*",
            EventKind::Modify,
            PathKind::File
        )])),
        "scope paths are repository-relative bytes, not pathspecs"
    );
    assert!(
        !monitor.inventory_dirty,
        "ordinary file contents do not change native registration"
    );
    #[cfg(unix)]
    {
        use std::os::unix::ffi::OsStringExt;
        let name = std::ffi::OsString::from_vec(vec![b'n', 0xff]);
        monitor.observe(&modified(Path::new("/repo").join(name)), true, now);
        assert_eq!(
            monitor.pending_worktree.worktree,
            Some(Scope::Paths(vec![path_change(
                vec![b'n', 0xff],
                EventKind::Modify,
                PathKind::File
            )])),
            "non-UTF-8 filenames are preserved"
        );
    }
}

#[test]
fn literal_scopes_preserve_native_hints_and_both_rename_endpoints() {
    let now = Instant::now();
    for coverage in [WorktreeCoverage::IgnoreAware, WorktreeCoverage::All] {
        for kind in [
            EventKind::Create,
            EventKind::Modify,
            EventKind::Remove,
            EventKind::Rename,
        ] {
            for path_kind in [PathKind::File, PathKind::Directory, PathKind::Any] {
                let mut monitor = monitor();
                monitor.options.worktree_coverage = Some(coverage);
                monitor.nested_repositories.insert("/repo/nested".into());
                let mut observed = event("/repo/src/[literal]*", kind, path_kind);
                let mut expected = vec![path_change("src/[literal]*", kind, path_kind)];
                if kind == EventKind::Rename {
                    observed.paths.push("/repo/src/[destination]*".into());
                    expected.insert(0, path_change("src/[destination]*", kind, path_kind));
                }
                monitor.observe(&observed, true, now);
                monitor.observe(&observed, true, now);
                assert_eq!(
                    monitor.take_changes(now).worktree,
                    Some(Scope::Paths(expected)),
                    "{coverage:?}: duplicate {kind:?}/{path_kind:?} observations preserve literal endpoints and hints"
                );
                assert!(
                    monitor.take_changes(now).is_empty(),
                    "publication drains the observed paths"
                );
                monitor.observe(&event("/repo/nested", kind, path_kind), true, now);
                assert_eq!(
                    monitor.take_changes(now).worktree,
                    Some(Scope::Paths(vec![path_change("nested", kind, path_kind)])),
                    "an event on the sentinel itself is not widened and keeps its native hints"
                );
            }
        }
    }
}

#[test]
fn filesystem_directory_inference_preserves_native_hints() -> TestResult {
    let fixture = gix_testtools::scripted_fixture_read_only("make_notify_repo.sh")?;
    let layout = Layout::from_repository(&open(&fixture.join("unignored"))?)?;
    let directory = layout
        .worktree
        .as_ref()
        .expect("fixture has a worktree")
        .join("tracked");
    assert!(
        directory.is_dir(),
        "the read-only fixture supplies filesystem directory evidence"
    );
    let now = Instant::now();
    for kind in [
        EventKind::Create,
        EventKind::Modify,
        EventKind::Remove,
        EventKind::Rename,
    ] {
        for path_kind in [PathKind::File, PathKind::Any] {
            let mut monitor = monitor();
            monitor.layout = layout.clone();
            monitor.directories.clear();
            monitor.observe(&event(&directory, kind, path_kind), true, now);
            assert!(
                !monitor.changed_directories.is_empty(),
                "filesystem evidence still requests directory maintenance"
            );
            assert_eq!(
                monitor.take_changes(now).worktree,
                Some(Scope::Paths(vec![path_change("tracked", kind, path_kind)])),
                "filesystem evidence must not rewrite {kind:?}/{path_kind:?} from the watcher"
            );
        }
    }
    Ok(())
}

#[test]
fn widened_scopes_do_not_claim_native_events_on_the_parent() {
    let now = Instant::now();
    for (path, parent, sentinel) in [
        ("src/.gitignore", "src", false),
        ("nested/.git", "nested", false),
        ("nested/.git", "nested", true),
        ("nested/inside/file", "nested", true),
    ] {
        for kind in [
            EventKind::Create,
            EventKind::Modify,
            EventKind::Remove,
            EventKind::Rename,
        ] {
            for path_kind in [PathKind::File, PathKind::Directory, PathKind::Any] {
                let mut monitor = monitor();
                if sentinel {
                    monitor.nested_repositories.insert("/repo/nested".into());
                }
                monitor.observe(&event(Path::new("/repo").join(path), kind, path_kind), true, now);
                assert_eq!(
                    monitor.take_changes(now).worktree,
                    Some(Scope::Paths(vec![path_change(
                        parent,
                        EventKind::Any,
                        PathKind::Directory
                    )])),
                    "widening {path} to {parent} cannot claim the parent's {kind:?}/{path_kind:?} event happened"
                );
            }
        }
    }
}

#[test]
fn duplicate_path_hints_coalesce_independently_and_stay_unknown() {
    use EventKind::{Create, Modify, Remove};
    use PathKind::{Any, Directory, File};

    let now = Instant::now();
    for (hints, expected_kind, expected_path_kind) in [
        ([(Create, File), (Create, File), (Create, File)], Create, File),
        ([(Create, File), (Modify, File), (Create, File)], EventKind::Any, File),
        ([(Modify, File), (Modify, Directory), (Modify, File)], Modify, Any),
        ([(Modify, Directory), (Modify, File), (Modify, Directory)], Modify, Any),
        ([(Modify, Any), (Modify, File), (Modify, Directory)], Modify, Any),
        ([(Modify, File), (Modify, Any), (Modify, File)], Modify, Any),
        (
            [(Create, File), (Remove, Directory), (Create, File)],
            EventKind::Any,
            Any,
        ),
    ] {
        let mut monitor = monitor();
        for (kind, path_kind) in hints {
            monitor.observe(&event("/repo/file", kind, path_kind), true, now);
        }
        assert_eq!(
            monitor.take_changes(now).worktree,
            Some(Scope::Paths(vec![path_change(
                "file",
                expected_kind,
                expected_path_kind
            )])),
            "{hints:?} coalesces each hint separately without restoring certainty or invalidating all paths"
        );
    }
}

#[test]
fn merging_changes_preserves_equal_hints_and_sticky_conflicts() {
    let initial = Changes {
        worktree: Some(Scope::Paths(vec![
            path_change("equal", EventKind::Create, PathKind::File),
            path_change("event-conflict", EventKind::Create, PathKind::File),
            path_change("path-conflict", EventKind::Modify, PathKind::File),
            path_change("unknown", EventKind::Any, PathKind::Any),
        ])),
        ..Changes::default()
    };
    let incoming = Changes {
        worktree: Some(Scope::Paths(vec![
            path_change("equal", EventKind::Create, PathKind::File),
            path_change("event-conflict", EventKind::Remove, PathKind::File),
            path_change("path-conflict", EventKind::Modify, PathKind::Directory),
            path_change("unknown", EventKind::Modify, PathKind::File),
        ])),
        ..Changes::default()
    };
    let expected = Changes {
        worktree: Some(Scope::Paths(vec![
            path_change("equal", EventKind::Create, PathKind::File),
            path_change("event-conflict", EventKind::Any, PathKind::File),
            path_change("path-conflict", EventKind::Modify, PathKind::Any),
            path_change("unknown", EventKind::Any, PathKind::Any),
        ])),
        ..Changes::default()
    };
    for (mut changes, other) in [(initial.clone(), incoming.clone()), (incoming, initial.clone())] {
        changes.merge(other);
        assert_eq!(changes, expected, "merging hints is independent of batch order");
        changes.merge(initial.clone());
        assert_eq!(
            changes, expected,
            "later concrete hints cannot undo either kind of uncertainty"
        );
    }
}

#[test]
fn published_scopes_are_ordered_by_path_bytes() {
    let now = Instant::now();
    let mut monitor = monitor();
    for (path, kind, path_kind) in [
        ("z", EventKind::Create, PathKind::Directory),
        ("a/[literal]*", EventKind::Modify, PathKind::File),
        ("a", EventKind::Rename, PathKind::Any),
        ("Z", EventKind::Remove, PathKind::File),
    ] {
        monitor.observe(&event(Path::new("/repo").join(path), kind, path_kind), true, now);
    }
    let expected = vec![
        path_change("Z", EventKind::Remove, PathKind::File),
        path_change("a", EventKind::Rename, PathKind::Any),
        path_change("a/[literal]*", EventKind::Modify, PathKind::File),
        path_change("z", EventKind::Create, PathKind::Directory),
    ];
    #[cfg(unix)]
    let expected = {
        use std::os::unix::ffi::OsStringExt;
        let mut expected = expected;
        for byte in [0xff, 0x80] {
            let path = std::ffi::OsString::from_vec(vec![b'n', byte]);
            monitor.observe(&modified(Path::new("/repo").join(path)), true, now);
        }
        expected.insert(3, path_change(vec![b'n', 0x80], EventKind::Modify, PathKind::File));
        expected.insert(4, path_change(vec![b'n', 0xff], EventKind::Modify, PathKind::File));
        expected
    };
    assert_eq!(
        monitor.take_changes(now).worktree,
        Some(Scope::Paths(expected)),
        "publication sorts literal path bytes, not native event order, hints, or decoded Unicode"
    );
}

#[test]
fn special_worktree_files_request_the_correct_refresh() {
    let now = Instant::now();
    for (path, scope, ignores, attributes, inventory) in [
        (
            "src/.gitignore",
            Scope::Paths(vec![path_change("src", EventKind::Any, PathKind::Directory)]),
            true,
            false,
            true,
        ),
        (".gitignore", Scope::All, true, false, true),
        ("src/.gitattributes", Scope::All, false, true, false),
        (".gitmodules", Scope::All, false, true, false),
        (
            "nested/.git",
            Scope::Paths(vec![path_change("nested", EventKind::Any, PathKind::Directory)]),
            false,
            false,
            true,
        ),
    ] {
        let mut monitor = monitor();
        monitor.observe(&modified(Path::new("/repo").join(path)), true, now);
        assert_eq!(
            monitor.pending_worktree.worktree,
            Some(scope),
            "{path} invalidates its required scope"
        );
        assert_eq!(
            monitor.pending_worktree.ignores, ignores,
            "{path} identifies ignore changes"
        );
        assert_eq!(
            monitor.pending_worktree.attributes, attributes,
            "{path} identifies attribute/submodule changes"
        );
        assert_eq!(
            monitor.inventory_dirty, inventory,
            "{path} requests registration maintenance only when necessary"
        );
    }
}

#[test]
fn case_and_unicode_settings_apply_to_event_paths() {
    let now = Instant::now();
    let mut monitor = monitor();
    monitor.ignore_case = true;
    monitor.precompose_unicode = true;
    monitor.observe(&modified("/REPO/src/.GITIGNORE"), true, now);
    assert!(
        monitor.pending_worktree.ignores,
        "case-insensitive filesystems recognize special filenames"
    );
    monitor.pending_worktree = Changes::default();
    monitor.observe(&modified("/repo/a\u{308}"), true, now);
    assert_eq!(
        monitor.pending_worktree.worktree,
        Some(Scope::Paths(vec![path_change("ä", EventKind::Modify, PathKind::File)])),
        "precomposed event paths match Git's index spelling"
    );
    assert!(
        classify_metadata(Path::new("/REPO/.GIT/head"), &monitor.layout, true)
            .0
            .references,
        "metadata comparisons honor ignoreCase too"
    );
    assert!(
        classify_metadata(Path::new("/repo/.git/head"), &monitor.layout, false)
            .0
            .is_empty(),
        "case-sensitive filesystems do not treat lowercase head as HEAD"
    );
}

#[test]
fn directory_replacement_rebuilds_native_handles() {
    let now = Instant::now();
    let mut monitor = monitor();
    monitor.metadata.registered.insert("/repo/.git/refs".into(), true);
    monitor.observe(
        &event("/repo/.git/refs", EventKind::Remove, PathKind::Directory),
        false,
        now,
    );
    assert!(
        monitor.metadata.rebuild,
        "recreating the same pathname must not retain a stale native directory handle"
    );
    monitor.observe(&event("/repo/src", EventKind::Rename, PathKind::Any), true, now);
    assert!(
        monitor.worktree.rebuild && !monitor.changed_directories.is_empty(),
        "remembered directory topology survives missing filesystem metadata"
    );
    assert_eq!(
        monitor.pending_worktree.worktree,
        Some(Scope::Paths(vec![path_change("src", EventKind::Rename, PathKind::Any)])),
        "renamed directories invalidate all descendants without replacing the native path-kind hint"
    );
    for (path, ignore_case, rebuild) in [
        ("/repo//./src/", false, true),
        ("/REPO//./SRC/", true, true),
        ("/REPO/src", false, false),
        ("/repo/src/file", true, false),
        ("/repo/src-other", true, false),
    ] {
        let mut monitor = self::monitor();
        monitor.ignore_case = ignore_case;
        monitor.observe(&event(path, EventKind::Remove, PathKind::Any), true, now);
        assert_eq!(
            monitor.worktree.rebuild, rebuild,
            "directory membership respects path components and ignoreCase for {path}"
        );
    }
}

#[test]
fn index_and_metadata_dependencies_are_separate() {
    let now = Instant::now();
    let mut monitor = monitor();
    monitor.observe(&modified("/repo/.git/index"), true, now);
    assert!(
        monitor.index_dirty && !monitor.inventory_dirty,
        "an index event compares topology before deciding to walk"
    );
    assert!(monitor.pending_worktree.index, "index changes are explicitly surfaced");
    monitor.layout.dependencies.push(Dependency {
        path: "/repo/.git/info/exclude".into(),
        kind: DependencyKind::Ignore,
    });
    monitor.observe(&modified("/repo/.git/info/exclude"), false, now);
    assert!(
        monitor.pending_metadata.changes.ignores && monitor.inventory_dirty,
        "repository-local ignore dependencies invalidate status and watches"
    );
    assert_eq!(
        monitor.pending_metadata.changes.worktree,
        Some(Scope::All),
        "repository-local excludes affect the complete worktree"
    );
    monitor.observe(&event("/repo/new", EventKind::Create, PathKind::Directory), true, now);
    assert!(
        monitor.changed_directories.contains(Path::new("/repo/new")),
        "pending ignore reevaluation must not hide a simultaneous directory creation"
    );
}

#[test]
fn unknown_pathless_and_lost_coverage_invalidate_conservatively() {
    let now = Instant::now();
    for event in [
        Event {
            paths: Vec::new(),
            kind: EventKind::Modify,
            path_kind: PathKind::File,
        },
        event("/repo/file", EventKind::Any, PathKind::Any),
        event("/repo/file", EventKind::Any, PathKind::File),
        event("/repo/src", EventKind::Any, PathKind::Directory),
    ] {
        let mut monitor = monitor();
        monitor.observe(&event, true, now);
        assert_eq!(
            monitor.pending_worktree.worktree,
            Some(Scope::All),
            "incomplete native observations cannot retain incremental state"
        );
        assert_eq!(
            monitor.maintenance,
            Some(now),
            "coverage loss immediately requests inventory repair"
        );
    }
    let mut monitor = monitor();
    monitor.coverage_lost(false, now);
    assert!(
        monitor.pending_metadata.take_due(now).configuration,
        "metadata loss invalidates all semantic metadata"
    );
    assert_eq!(
        monitor.pending_worktree.worktree,
        Some(Scope::All),
        "metadata loss can hide changed configuration or ignores"
    );
}

#[test]
fn scope_memory_is_bounded() {
    let mut changes = Changes::default();
    assert_eq!(changes.worktree, None, "default changes have no worktree scope");
    assert!(changes.is_empty(), "default changes have no invalidations");
    changes.merge(Changes {
        worktree: Some(Scope::Paths(Vec::new())),
        ..Changes::default()
    });
    assert_eq!(
        changes.worktree, None,
        "merging no paths does not create a worktree scope"
    );
    assert!(
        changes.is_empty(),
        "merging no paths does not introduce an invalidation"
    );
    for worktree in [
        None,
        Some(Scope::Paths(vec![path_change(
            "file",
            EventKind::Modify,
            PathKind::File,
        )])),
        Some(Scope::All),
    ] {
        let expected = Changes {
            worktree,
            references: true,
            ..Changes::default()
        };
        let mut merged = expected.clone();
        merged.merge(Changes::default());
        assert_eq!(merged, expected, "an absent scope preserves existing invalidations");
        let mut merged = Changes::default();
        merged.merge(expected.clone());
        assert_eq!(merged, expected, "an absent scope accepts incoming invalidations");
    }
    for i in 0..MAX_SCOPES {
        changes.insert_worktree_path(path_change(format!("file-{i}"), EventKind::Modify, PathKind::File));
    }
    changes.insert_worktree_path(path_change("file-0", EventKind::Create, PathKind::Directory));
    assert!(
        matches!(&changes.worktree, Some(Scope::Paths(paths)) if paths.len() == MAX_SCOPES
            && paths[0] == path_change("file-0", EventKind::Any, PathKind::Any)),
        "the exact path-count budget permits duplicates and coalesces their hints without counting them again"
    );
    changes.insert_worktree_path(path_change("one-too-many", EventKind::Create, PathKind::File));
    assert_eq!(
        changes.worktree,
        Some(Scope::All),
        "too many scopes promote to a full invalidation"
    );
    changes.insert_worktree_path(path_change("file-0", EventKind::Modify, PathKind::File));
    changes.merge(Changes {
        worktree: Some(Scope::Paths(vec![path_change(
            "new",
            EventKind::Create,
            PathKind::File,
        )])),
        ..Changes::default()
    });
    assert_eq!(
        changes.worktree,
        Some(Scope::All),
        "insertion and merging cannot narrow a full invalidation"
    );
    let mut partial = Changes::default();
    partial.insert_worktree_path(path_change("file", EventKind::Modify, PathKind::File));
    partial.merge(changes);
    assert_eq!(
        partial.worktree,
        Some(Scope::All),
        "an incoming full invalidation replaces path hints"
    );
    let mut changes = Changes::default();
    changes.insert_worktree_path(path_change(
        vec![b'x'; MAX_SCOPE_BYTES + 1],
        EventKind::Modify,
        PathKind::File,
    ));
    assert_eq!(
        changes.worktree,
        Some(Scope::All),
        "a single long path cannot exceed the scope budget"
    );
    let mut changes = Changes::default();
    for byte in *b"xy" {
        changes.insert_worktree_path(path_change(
            vec![byte; MAX_SCOPE_BYTES / 2],
            EventKind::Modify,
            PathKind::File,
        ));
    }
    changes.merge(Changes {
        worktree: Some(Scope::Paths(vec![path_change(
            vec![b'x'; MAX_SCOPE_BYTES / 2],
            EventKind::Remove,
            PathKind::File,
        )])),
        ..Changes::default()
    });
    assert!(
        matches!(&changes.worktree, Some(Scope::Paths(paths)) if paths.len() == 2
            && paths.iter().map(|change| change.path.len()).sum::<usize>() == MAX_SCOPE_BYTES
            && paths[0].kind == EventKind::Any && paths[0].path_kind == PathKind::File),
        "the byte budget counts only distinct path bytes, not hints or duplicate observations merged at the limit"
    );
    changes.merge(Changes {
        worktree: Some(Scope::Paths(vec![path_change("z", EventKind::Create, PathKind::File)])),
        ..Changes::default()
    });
    assert_eq!(
        changes.worktree,
        Some(Scope::All),
        "one more path byte exceeds the cumulative budget"
    );
}

#[test]
fn pending_diagnostics_survive_metadata_debounce() {
    let now = Instant::now();
    let mut monitor = monitor();
    let event = modified("/repo/.git/refs/heads/main");
    monitor.pending_statistics.received = 1;
    monitor.observe(&event, false, now);
    let initial = monitor.service(now, || panic!("ordinary refs do not change watches"));
    assert!(
        initial.changes.is_empty() && initial.statistics.received == 0,
        "metadata debounce retains its diagnostics"
    );
    let later = monitor.service(now + Duration::from_millis(100), || {
        panic!("ordinary refs do not change watches")
    });
    assert!(later.changes.references, "the due reference invalidation is delivered");
    assert_eq!(
        later.statistics.received, 1,
        "publication retains the original event count"
    );
}

#[test]
fn reopening_failures_keep_invalidations_and_retry_deadlines() {
    let now = Instant::now();
    let mut monitor = monitor();
    monitor.coverage_lost(false, now);
    let outcome = monitor.service(now, || Err(message("temporarily unavailable").raise()));
    assert_eq!(
        outcome.errors.len(),
        1,
        "the repository error is delivered with its context"
    );
    assert!(
        outcome.changes.configuration && outcome.changes.index,
        "failure does not discard the conservative invalidation"
    );
    assert_eq!(
        monitor.next_timeout(now),
        Some(monitor.options.retry_interval),
        "recovery waits for the retry interval"
    );
    monitor.service(now + Duration::from_secs(1), || panic!("retry is not due yet"));
}

#[test]
fn idle_monitor_does_not_refresh_by_elapsed_time() {
    let now = Instant::now();
    let mut monitor = monitor();
    for now in [now, now + Duration::from_secs(3600)] {
        let outcome = monitor.service(now, || panic!("idle monitoring must not reopen the repository"));
        assert!(outcome.changes.is_empty(), "elapsed time does not invalidate caches");
        assert_eq!(
            monitor.next_timeout(now),
            None,
            "idle monitoring has no refresh deadline"
        );
    }
}

#[test]
fn manual_refresh_invalidates_all_caches_even_when_native_recovery_is_delayed() -> TestResult {
    let mut monitor = monitor();
    let mut watcher = plumbing::Watcher::new(plumbing::Options::default())?;
    watcher.set_waker(|| panic!("simulate a queued worktree notification error"));
    monitor.worktree.watcher = Some(watcher);
    monitor.rescan();
    let now = Instant::now();
    let outcome = monitor.service(now, || panic!("native recovery must respect its retry delay"));
    assert_eq!(outcome.errors.len(), 1, "the queued worktree failure is reported");
    let mut expected = Changes::metadata_all();
    expected.merge(Changes::worktree_all());
    assert_eq!(
        outcome.changes, expected,
        "manual refresh invalidates every cache even when a worktree error postpones maintenance"
    );
    assert_eq!(
        monitor.next_timeout(now),
        Some(monitor.options.retry_interval),
        "full invalidation does not bypass native recovery backoff"
    );
    Ok(())
}

#[test]
fn inventory_honors_nested_ignores_and_nested_repository_boundaries() -> TestResult {
    let fixture = fixture()?;
    let root = fixture.path();
    for path in [
        "visible/nested",
        "project/fuzz/target/deep/out",
        "project/fuzz/target/visible",
        "nested/inside",
        "ignored/nested",
    ] {
        std::fs::create_dir_all(root.join(path))?;
    }
    std::fs::write(root.join(".gitignore"), "target/\nignored/\n")?;
    std::fs::write(root.join("project/.gitignore"), "!out/\n")?;
    let status = gix_testtools::git_command(root.join("nested"))
        .args(["init", "--quiet"])
        .status()?;
    assert!(status.success(), "a disposable nested repository initializes");
    let repo = open(root)?;
    let index = repo.index_or_empty()?;
    let inventory = inventory::worktree_directories(&repo, &index)?;
    let recursive = inventory::recursive_root_watch(root)?;
    assert_eq!(
        recursive,
        plumbing::Watch {
            path: root.canonicalize()?,
            recursive: true,
        },
        "all coverage uses one native canonical root, not the ignore-aware directory inventory"
    );
    let shallow = inventory.watches_for_backend(false);
    assert_eq!(
        inventory.watches_for_backend(true),
        inventory.watches,
        "native recursive coverage uses the compact directory plan"
    );
    let directories = inventory.watches;
    let root = Layout::from_repository(&repo)?
        .worktree
        .expect("fixture has a worktree");
    assert_eq!(
        shallow,
        ["", "visible", "visible/nested", "project", "project/fuzz", "nested"]
            .into_iter()
            .map(|path| (root.join(path), false))
            .collect(),
        "emulated recursion uses every included directory found by the walk, without ignored or nested repository descendants"
    );
    for (path, recursive) in [
        ("", false),
        ("visible", true),
        ("project", false),
        ("project/fuzz", false),
        ("nested", false),
    ] {
        assert_eq!(
            directories.get(&root.join(path)),
            Some(&recursive),
            "{path} uses recursive coverage only without ignored or repository boundaries"
        );
    }
    for path in [
        "project/fuzz/target",
        "project/fuzz/target/deep/out",
        "ignored",
        "nested/inside",
        ".git",
    ] {
        assert!(
            !directories.contains_key(&root.join(path)),
            "{path} must not be traversed through ignored or repository boundaries"
        );
    }
    let mut monitor = monitor();
    monitor.layout = Layout::from_repository(&repo)?;
    assert!(
        !directories.contains_key(&root.join("visible/nested")),
        "recursive roots cover their descendants without separate registrations"
    );
    monitor.directories = inventory::directory_lookup(directories.into_keys());
    monitor.nested_repositories = inventory.repositories;
    monitor.observe(&modified(root.join("nested/file")), true, Instant::now());
    assert_eq!(
        monitor.pending_worktree.worktree,
        Some(Scope::Paths(vec![path_change(
            "nested",
            EventKind::Any,
            PathKind::Directory
        )])),
        "sentinel events never request traversal into a nested repository or claim it was modified"
    );
    Ok(())
}

#[test]
fn inventory_rechecks_exclusions_and_rediscovers_only_reenabled_subtrees() -> TestResult {
    let fixture = fixture()?;
    let root = fixture.path().canonicalize()?;
    std::fs::create_dir_all(root.join("visible/deep"))?;
    std::fs::create_dir_all(root.join("ignored/deep"))?;
    std::fs::write(root.join("ignored/deep/file"), "tracked later\n")?;
    std::fs::write(root.join(".gitignore"), "ignored/\n")?;
    let repo = open(&root)?;
    let index = repo.index_or_empty()?;
    let mut inventory = inventory::worktree_directories(&repo, &index)?;
    assert!(
        !inventory.watches.contains_key(&root.join("ignored")),
        "fully ignored directories have no watches"
    );

    std::fs::write(root.join(".gitignore"), "# Expose the subtree.\n")?;
    let discover = inventory.recheck(&open(&root)?, &index)?;
    assert_eq!(
        discover,
        HashSet::from([root.join("ignored")]),
        "only never-discovered subtrees need an initial walk"
    );
    assert_eq!(
        inventory.read_dir_calls, 0,
        "rule reevaluation uses the recorded directory inventory"
    );
    inventory = inventory::worktree_directories_reusing(&open(&root)?, &index, Some(&inventory), &discover)?;
    assert_eq!(
        inventory.read_dir_calls, 3,
        "discovery reads the root and newly exposed subtree, skipping visible/"
    );

    std::fs::write(root.join(".gitignore"), "ignored/\n")?;
    assert!(
        inventory.recheck(&open(&root)?, &index)?.is_empty(),
        "excluding a known directory requires no discovery"
    );
    assert_eq!(
        inventory.read_dir_calls, 0,
        "exclusion changes use the directory inventory without a walk"
    );
    std::fs::create_dir_all(root.join("ignored/new/cache/deep"))?;
    std::fs::write(root.join("ignored/new/.gitignore"), "cache/\n")?;
    std::fs::write(root.join(".gitignore"), "# Reenable the subtree.\n")?;
    let discover = inventory.recheck(&open(&root)?, &index)?;
    assert_eq!(
        discover,
        HashSet::from([root.join("ignored")]),
        "changes inside unwatched subtrees require local rediscovery"
    );
    inventory = inventory::worktree_directories_reusing(&open(&root)?, &index, Some(&inventory), &discover)?;
    assert!(
        !inventory.watches.contains_key(&root.join("ignored/new/cache")),
        "rediscovery applies ignore files created while unwatched"
    );
    std::fs::write(root.join(".gitignore"), "ignored/\n")?;
    inventory.recheck(&open(&root)?, &index)?;
    let before = inventory.index_fingerprint(&index, false)?;
    gix_testtools::git(&root, "add -f -- ignored/deep/file")?;
    let repo = open(&root)?;
    let index = repo.index_or_empty()?;
    assert_ne!(
        inventory.index_fingerprint(&index, false)?,
        before,
        "tracking an ignored path changes coverage"
    );
    let discover = inventory.recheck(&repo, &index)?;
    assert_eq!(
        discover,
        HashSet::from([root.join("ignored")]),
        "index changes rediscover only the newly covered ignored subtree"
    );
    inventory = inventory::worktree_directories_reusing(&repo, &index, Some(&inventory), &discover)?;
    assert!(
        inventory.watches.contains_key(&root.join("ignored")),
        "tracked paths retain coverage despite ignores"
    );
    Ok(())
}

#[test]
fn directory_changes_reuse_unaffected_recursive_subtrees() -> TestResult {
    let fixture = fixture()?;
    let root = fixture.path().canonicalize()?;
    std::fs::create_dir_all(root.join("visible/deep"))?;
    std::fs::create_dir_all(root.join("other/deep/deeper"))?;
    let repo = open(&root)?;
    let index = repo.index_or_empty()?;
    let inventory = inventory::worktree_directories(&repo, &index)?;
    std::fs::create_dir_all(root.join("visible/new/a/cache/deep"))?;
    std::fs::write(root.join("visible/new/.gitignore"), "cache/\n")?;
    let refreshed = inventory::worktree_directories_reusing(
        &repo,
        &index,
        Some(&inventory),
        &HashSet::from([root.join("visible/new")]),
    )?;
    assert_eq!(
        refreshed.read_dir_calls, 4,
        "only the changed branch and its containing directories are read"
    );
    assert!(
        !refreshed.watches.contains_key(&root.join("visible/new/a/cache")),
        "new nested ignores prune complete directories"
    );
    assert_eq!(
        refreshed.watches.get(&root.join("other")),
        Some(&true),
        "unaffected subtrees keep their recursive watches"
    );
    assert_eq!(
        refreshed.watches.get(&root.join("visible/deep")),
        Some(&true),
        "splitting a recursive root preserves its known siblings"
    );
    Ok(())
}

#[test]
fn directory_lookup_hashes_components_and_resolves_case_collisions() -> TestResult {
    let paths = [PathBuf::from("/repo/Src"), PathBuf::from("/repo/Ä")].into_iter();
    #[cfg(unix)]
    let paths = paths.chain([gix_path::from_bstr(crate::bstr::BStr::new(b"/repo/\xffDir"))?.into_owned()]);
    let paths: Vec<_> = paths.collect();
    let lookup = inventory::directory_lookup(paths.clone());
    for directory in paths {
        assert!(
            lookup
                .find(inventory::path_hash(&directory), |path| same_path(
                    path, &directory, false
                ))
                .is_some(),
            "native byte paths survive insertion without text conversion"
        );
    }
    for (query, ignore_case, found) in [
        ("/repo//./Src/", false, true),
        ("/REPO/src", true, true),
        ("/repo/src", false, false),
        ("/repo/ä", true, false),
        ("/repo/Src/file", true, false),
    ] {
        let query = Path::new(query);
        assert_eq!(
            lookup
                .find(inventory::path_hash(query), |path| same_path(query, path, ignore_case))
                .is_some(),
            found,
            "hash lookup uses native components and ASCII-only case rules for {query:?}"
        );
    }
    #[cfg(windows)]
    for (stored, query) in [
        (r"\\server\share\dir", r"//server/share/dir"),
        (r"\\.\COM42\dir", r"//./COM42/dir"),
        (r"C:\dir", r"c:/dir"),
    ] {
        let query = Path::new(query);
        let lookup = inventory::directory_lookup([PathBuf::from(stored)]);
        assert!(
            lookup
                .find(inventory::path_hash(query), |path| same_path(query, path, false))
                .is_some(),
            "equivalent Windows prefixes hash equally even when their raw spellings differ"
        );
    }
    Ok(())
}

#[test]
fn index_fingerprint_tracks_only_ignore_and_repository_entries() -> TestResult {
    use gix_index::entry::{Flags, Mode, Stage};

    let project = |entries: &[(&[u8], Mode, Flags)]| {
        let mut index = gix_index::State::new(gix_hash::Kind::Sha1);
        for &(path, mode, flags) in entries {
            index.dangerously_push_entry(
                Default::default(),
                gix_hash::ObjectId::null(gix_hash::Kind::Sha1),
                flags,
                mode,
                path.into(),
            );
        }
        index.sort_entries();
        inventory::index_fingerprint(&index)
    };
    let file = (b"dir/\xff".as_slice(), Mode::FILE, Flags::empty());
    let before = project(&[file])?;
    for (entries, changed, reason) in [
        (
            vec![(b"a".as_slice(), Mode::FILE, Flags::empty()), file],
            false,
            "ordinary file entries do not affect the coverage fingerprint",
        ),
        (
            vec![file, (b"z".as_slice(), Mode::FILE, Flags::empty())],
            false,
            "ordinary file entries do not affect the coverage fingerprint",
        ),
        (
            vec![(b"dir/x".as_slice(), file.1, file.2)],
            false,
            "ordinary file entries do not affect the coverage fingerprint",
        ),
        (
            vec![(file.0, Mode::SYMLINK, file.2)],
            false,
            "ordinary file entries do not affect the coverage fingerprint",
        ),
        (
            vec![(file.0, file.1, Flags::SKIP_WORKTREE)],
            false,
            "ordinary file entries do not affect the coverage fingerprint",
        ),
        (
            vec![(file.0, file.1, Flags::from_stage(Stage::Ours))],
            false,
            "ordinary file entries do not affect the coverage fingerprint",
        ),
        (
            vec![(file.0, file.1, Flags::FSMONITOR_VALID)],
            false,
            "unrelated flags do not affect coverage",
        ),
        (
            vec![(file.0, file.1, Flags::ASSUME_VALID)],
            false,
            "ordinary file entries do not affect the coverage fingerprint",
        ),
        (
            vec![(file.0, file.1, Flags::UPTODATE)],
            false,
            "ordinary file entries do not affect the coverage fingerprint",
        ),
        (
            vec![(b"submodule".as_slice(), Mode::COMMIT, Flags::empty()), file],
            true,
            "root submodule boundaries affect coverage",
        ),
        (
            vec![(b"sparse".as_slice(), Mode::DIR, Flags::SKIP_WORKTREE), file],
            true,
            "root sparse directories affect coverage",
        ),
        (vec![], false, "ordinary file removals do not affect coverage"),
    ] {
        let after = project(&entries)?;
        assert_eq!(before != after, changed, "{reason}");
    }
    let stages = [
        (file.0, Mode::SYMLINK, Flags::from_stage(Stage::Base)),
        (file.0, Mode::FILE, Flags::from_stage(Stage::Ours)),
    ];
    assert_eq!(
        project(&stages)?,
        project(&[stages[1], stages[0]])?,
        "backing storage order is irrelevant, including conflict stages with different modes"
    );
    let entries = [(b"a".as_slice(), file.1, file.2), file];
    assert_eq!(
        project(&entries)?,
        project(&[entries[1], entries[0]])?,
        "path offsets do not contribute to the fingerprint"
    );
    Ok(())
}

#[test]
fn index_fingerprint_tracks_ignore_blob_ids() -> TestResult {
    for (path, changed) in [
        (".gitignore", true),
        ("dir/.gitignore", true),
        ("dir/.GITIGNORE", true),
        ("dir/ordinary", false),
        ("dir/other.gitignore", false),
    ] {
        let mut index = gix_index::State::new(gix_hash::Kind::Sha1);
        index.dangerously_push_entry(
            Default::default(),
            gix_hash::ObjectId::null(gix_hash::Kind::Sha1),
            gix_index::entry::Flags::SKIP_WORKTREE,
            gix_index::entry::Mode::FILE,
            path.into(),
        );
        let before = inventory::index_fingerprint(&index)?;
        index.entries_mut()[0].id = gix_hash::ObjectId::empty_blob(gix_hash::Kind::Sha1);
        assert_eq!(
            before != inventory::index_fingerprint(&index)?,
            changed,
            "only ignore-rule contents can change coverage for {path}"
        );
    }
    Ok(())
}

#[test]
fn index_fingerprint_ignores_content_and_tracks_directory_topology() -> TestResult {
    let fixture = fixture()?;
    let root = fixture.path();
    std::fs::write(root.join("file"), "one\n")?;
    let stage = |path| -> TestResult {
        let status = gix_testtools::git_command(root).args(["add", path]).status()?;
        assert!(status.success(), "git stages {path} in the fixture");
        Ok(())
    };
    stage("file")?;
    let before = inventory::index_fingerprint(&*open(root)?.index_or_empty()?)?;
    std::fs::write(root.join("file"), "two\n")?;
    stage("file")?;
    let content = inventory::index_fingerprint(&*open(root)?.index_or_empty()?)?;
    assert_eq!(
        before, content,
        "object IDs and stat caches do not change watch topology"
    );
    std::fs::create_dir(root.join("new"))?;
    std::fs::write(root.join("new/file"), "new\n")?;
    stage("new/file")?;
    let directory = inventory::index_fingerprint(&*open(root)?.index_or_empty()?)?;
    assert_eq!(
        content, directory,
        "ordinary staging leaves directory coverage unchanged"
    );
    Ok(())
}

#[test]
fn metadata_watches_exclude_ancestors_and_external_configuration() -> TestResult {
    let fixture = fixture()?;
    let external = gix_testtools::tempfile::tempdir()?;
    let config = external.path().join("config");
    let ignore = external.path().join("ignore");
    let attributes = external.path().join("attributes");
    std::fs::write(&config, "[monitor]\nvalue = before\n")?;
    std::fs::write(&ignore, "ignored\n")?;
    std::fs::write(&attributes, "* text\n")?;
    let open = || {
        let mut options = crate::open::Options::isolated()
            .system_config_path(&config)
            .config_overrides([
                format!("core.excludesFile={}", ignore.display()),
                format!("core.attributesFile={}", attributes.display()),
            ]);
        options.permissions.config.system = true;
        crate::open_opts(fixture.path(), options)
    };
    let repo = open()?;
    let mut layout = Layout::from_repository(&repo)?;
    let root = layout.worktree.clone();
    let is_local = |path: &Path| {
        path.starts_with(&layout.git_dir)
            || path.starts_with(&layout.common_dir)
            || root.as_ref().is_some_and(|root| path.starts_with(root))
    };
    assert!(
        layout
            .dependencies
            .iter()
            .all(|dependency| is_local(&dependency.path) || dependency.kind == DependencyKind::Ignore),
        "only external ignore files become notification dependencies: {:?}",
        layout.dependencies
    );
    let external_root = external.path().canonicalize()?;
    for worktree in [root, None] {
        layout.worktree = worktree;
        let watches = inventory::metadata_watches(&layout, None, false)?;
        assert!(
            watches.keys().all(|path| path == &external_root
                || path.starts_with(&layout.git_dir)
                || path.starts_with(&layout.common_dir)
                || layout.worktree.as_ref().is_some_and(|root| path.starts_with(root))),
            "normal and bare views never watch repository ancestors or external configuration directories: {watches:?}"
        );
        assert!(
            watches.contains_key(&layout.git_dir),
            "local HEAD, configuration and index files remain covered"
        );
        assert!(
            watches.contains_key(&layout.common_dir.join("info")),
            "repository-local ignore and attribute files remain covered"
        );
    }
    std::fs::write(&config, "[monitor]\nvalue = after\n")?;
    assert_eq!(
        open()?
            .config_snapshot()
            .string("monitor.value")
            .map(|value| value.to_vec()),
        Some(b"after".to_vec()),
        "reopening naturally reads external configuration without a watcher"
    );
    drop(repo);
    layout.dependencies.retain(|dependency| {
        layout.git_dir.starts_with(&dependency.path) || dependency.path.starts_with(&layout.git_dir)
    });
    std::fs::remove_dir_all(&layout.git_dir)?;
    assert!(
        inventory::metadata_watches(&layout, None, false)?.is_empty(),
        "a missing repository root does not broaden coverage to its parent"
    );
    Ok(())
}

#[test]
fn metadata_inventory_limits_linked_worktrees_to_administrative_roots() -> TestResult {
    let fixture = fixture()?;
    let repo = open(fixture.path())?;
    let mut layout = Layout::from_repository(&repo)?;
    let worktrees = layout.common_dir.join("worktrees");
    for path in [
        "current/refs/worktree/tix/pins",
        "current/rebase-merge",
        "other/refs/worktree/tix/pins",
        "other/logs/refs/worktree/tix",
        "other/rebase-merge",
        "incomplete",
    ] {
        std::fs::create_dir_all(worktrees.join(path))?;
    }
    for git_dir in [layout.common_dir.clone(), worktrees.join("current")] {
        layout.git_dir = git_dir;
        layout.index = layout.git_dir.join("index");
        let dependencies = inventory::dependency_watches(&layout);
        assert!(
            dependencies.values().all(|recursive| !recursive),
            "ignore-aware dependencies never recursively inventory refs or operation state"
        );
        for directory in [&layout.git_dir, &layout.common_dir] {
            assert!(
                dependencies.contains_key(directory),
                "private and shared roots cover HEAD, configuration and layout dependencies"
            );
        }
        assert!(
            !dependencies.contains_key(&worktrees) && !dependencies.contains_key(&worktrees.join("other")),
            "worktree-only coverage does not enumerate linked-worktree membership"
        );
        let watches = inventory::metadata_watches(&layout, None, false)?;
        assert_eq!(
            watches.get(&worktrees),
            Some(&false),
            "worktree membership needs only immediate directory entries"
        );
        for name in ["current", "other", "incomplete"] {
            let directory = worktrees.join(name);
            assert_eq!(
                watches.get(&directory),
                Some(&false),
                "each administrative root is watched even before its gitdir file exists"
            );
            if directory != layout.git_dir {
                for relative in ["refs", "logs", "rebase-merge"] {
                    let nested = directory.join(relative);
                    assert!(
                        !watches
                            .iter()
                            .any(|(root, recursive)| *recursive && nested.starts_with(root)),
                        "foreign {name}/{relative} must not have recursive coverage"
                    );
                }
            }
        }
        for directory in [&layout.common_dir, &layout.git_dir] {
            assert_eq!(
                watches.get(&directory.join("refs")),
                Some(&true),
                "shared and current-worktree references retain recursive coverage"
            );
        }
        if layout.git_dir != layout.common_dir {
            assert_eq!(
                watches.get(&layout.git_dir.join("rebase-merge")),
                Some(&true),
                "the current worktree's operation state retains recursive coverage"
            );
        }
    }
    Ok(())
}

#[test]
fn linked_worktree_membership_reconciles_shallow_watches() -> TestResult {
    let fixture = fixture()?;
    let root = fixture.path();
    let mut monitor = open(root)?.monitor(Options {
        worktree_coverage: None,
        ..Options::default()
    })?;
    let now = Instant::now();
    for _ in 0..2 {
        let outcome = monitor.service(now, || open(root));
        assert!(
            outcome.errors.is_empty(),
            "startup installs and verifies metadata watches"
        );
    }

    let worktrees = monitor.layout.common_dir.join("worktrees");
    let added = worktrees.join("new");
    std::fs::create_dir_all(&added)?;
    monitor.observe(&event(&worktrees, EventKind::Create, PathKind::Directory), false, now);
    let outcome = monitor.service(now, || open(root));
    assert!(outcome.errors.is_empty(), "first-worktree discovery succeeds");
    assert_eq!(
        monitor.metadata.registered.get(&worktrees),
        Some(&false),
        "the first linked worktree installs shallow membership coverage"
    );
    assert_eq!(
        monitor.metadata.registered.get(&added),
        Some(&false),
        "an incomplete worktree is covered before HEAD and gitdir are written"
    );

    let head = added.join("HEAD");
    std::fs::write(&head, "ref: refs/heads/topic\n")?;
    monitor.observe(&modified(&head), false, now);
    let now = now + Duration::from_millis(250);
    let outcome = monitor.service(now, || open(root));
    assert!(outcome.errors.is_empty(), "completing HEAD preserves native coverage");
    assert!(
        outcome.changes.references && outcome.changes.layout,
        "HEAD changes remain visible after a shallow subscription is installed"
    );

    std::fs::remove_dir_all(&added)?;
    monitor.observe(&event(&added, EventKind::Remove, PathKind::Directory), false, now);
    let outcome = monitor.service(now, || open(root));
    assert!(
        outcome.errors.iter().all(|error| error.is_conflict()
            || error
                .downcast_any_ref::<std::io::Error>()
                .is_some_and(|error| error.kind() == std::io::ErrorKind::NotFound)),
        "removing a native watch can report lost coverage or its missing root: {:?}",
        outcome.errors
    );
    let outcome = monitor.service(now + monitor.options.retry_interval, || open(root));
    assert!(
        outcome.errors.is_empty(),
        "worktree removal restores coverage after any native loss: {:?}",
        outcome.errors
    );
    assert!(
        !monitor.metadata.registered.contains_key(&added),
        "removed administrative directories release their subscriptions"
    );
    assert_eq!(
        monitor.metadata.registered.get(&worktrees),
        Some(&false),
        "membership remains observable after the last linked worktree disappears"
    );
    Ok(())
}

#[test]
fn metadata_inventory_reports_unreadable_worktree_membership() -> TestResult {
    let fixture = fixture()?;
    let repo = open(fixture.path())?;
    let layout = Layout::from_repository(&repo)?;
    std::fs::write(layout.common_dir.join("worktrees"), "not a directory\n")?;
    assert!(
        inventory::dependency_watches(&layout)
            .values()
            .all(|recursive| !recursive),
        "worktree-only dependencies do not inspect unrelated linked-worktree membership"
    );
    assert!(
        inventory::metadata_watches(&layout, None, false).is_err(),
        "an unreadable worktree inventory cannot silently certify incomplete coverage"
    );
    std::fs::write(&layout.index, "invalid index\n")?;
    let mut monitor = monitor();
    monitor.options.metadata = false;
    monitor.options.worktree_coverage = Some(WorktreeCoverage::All);
    let mut statistics = Statistics::default();
    let mut errors = Vec::new();
    monitor.maintain(&repo, Instant::now(), &mut statistics, &mut errors)?;
    assert!(
        errors.is_empty(),
        "all coverage needs neither the index nor metadata inventory"
    );
    assert!(
        monitor.metadata.watcher.is_none()
            && monitor.layout.dependencies.is_empty()
            && monitor.index_fingerprint.is_none(),
        "all coverage retains no ignore/config dependencies or index fingerprint"
    );
    assert_eq!(
        statistics.added, 1,
        "classified all coverage needs only a single recursive root watch"
    );
    Ok(())
}

#[test]
fn missing_dependencies_follow_the_nearest_existing_ancestor() -> TestResult {
    let fixture = fixture()?;
    let repo = open(fixture.path())?;
    let mut layout = Layout::from_repository(&repo)?;
    let root = layout.worktree.clone().expect("fixture has a worktree");
    let dependency = root.join("missing/deeper/config");
    layout.dependencies.push(Dependency {
        path: dependency.clone(),
        kind: DependencyKind::Configuration,
    });
    assert!(
        inventory::metadata_watches(&layout, None, false)?.contains_key(&root),
        "missing parents are observed through an existing ancestor"
    );
    std::fs::create_dir_all(root.join("missing/deeper"))?;
    let (changes, refresh) = classify_metadata(&root.join("missing"), &layout, false);
    assert!(
        changes.configuration && refresh,
        "creating an ancestor rediscovers the dependency's parent"
    );
    assert!(
        inventory::metadata_watches(&layout, None, false)?
            .contains_key(dependency.parent().expect("dependency has parent")),
        "registration advances as missing ancestors appear"
    );
    Ok(())
}

#[cfg(unix)]
#[test]
fn physical_roots_and_config_symlink_replacements_are_both_observed() -> TestResult {
    let fixture = fixture()?;
    let root = fixture.path();
    let target = root.join("actual-config");
    std::fs::rename(root.join(".git/config"), &target)?;
    std::os::unix::fs::symlink(&target, root.join(".git/config"))?;
    let repo = open(root)?;
    let layout = Layout::from_repository(&repo)?;
    let physical_root = root.canonicalize()?;
    assert_eq!(
        layout.worktree.as_ref(),
        Some(&physical_root),
        "native registrations use the physical root spelling"
    );
    for path in [physical_root.join(".git/config"), physical_root.join("actual-config")] {
        assert!(
            layout.dependencies.iter().any(|dependency| dependency.path == path),
            "{} remains a configuration dependency",
            path.display()
        );
        assert!(
            classify_metadata(&path, &layout, false).0.configuration,
            "both symlink replacement and target edits invalidate configuration"
        );
    }
    let relative = absolute_path(Path::new("missing/config"), &physical_root, false)?;
    assert_eq!(
        relative,
        physical_root.join("missing/config"),
        "relative missing paths are made absolute"
    );
    Ok(())
}

#[test]
fn startup_verifies_registration_once_and_unchanged_sets_are_stable() -> TestResult {
    let fixture = fixture()?;
    let root = fixture.path();
    let mut monitor = open(root)?.monitor(Options::default())?;
    let now = Instant::now();
    let mut opens = 0;
    let mut opener = || {
        opens += 1;
        open(root)
    };
    let initial = monitor.service(now, &mut opener);
    assert!(
        initial.errors.is_empty(),
        "initial registration succeeds: {:?}",
        initial.errors
    );
    assert!(
        initial.changes.references && initial.changes.index,
        "installing watches requests a complete baseline"
    );
    assert_eq!(
        monitor.next_timeout(now),
        Some(Duration::ZERO),
        "installation races require a later inventory verification"
    );
    let verified = monitor.service(now, &mut opener);
    assert!(
        verified.errors.is_empty(),
        "verification succeeds: {:?}",
        verified.errors
    );
    assert_eq!(opens, 2, "each service call opens at most once");
    assert!(
        monitor.maintenance.is_none(),
        "an unchanged inventory does not keep scheduling scans"
    );
    assert_eq!(
        (verified.statistics.added, verified.statistics.removed),
        (0, 0),
        "verification does not churn native registrations"
    );
    Ok(())
}

#[test]
fn head_changes_rediscover_branch_conditioned_dependencies() -> TestResult {
    let fixture = fixture()?;
    let root = fixture.path();
    let config = root.join(".git/config");
    let mut contents = std::fs::read_to_string(&config)?;
    contents.push_str(
        "\n[includeIf \"onbranch:main\"]\npath = main-config\n[includeIf \"onbranch:other\"]\npath = other-config\n",
    );
    std::fs::write(config, contents)?;
    let open = || {
        let mut options = crate::open::Options::isolated();
        options.permissions.config.includes = true;
        crate::open_opts(root, options)
    };
    let mut monitor = open()?.monitor(Options::default())?;
    assert!(
        monitor
            .layout
            .dependencies
            .iter()
            .any(|dependency| dependency.path.ends_with("main-config")),
        "initial dependencies reflect the current branch"
    );
    std::fs::write(root.join(".git/HEAD"), "ref: refs/heads/other\n")?;
    let now = Instant::now();
    monitor.observe(&modified(monitor.layout.git_dir.join("HEAD")), false, now);
    let outcome = monitor.service(now, open);
    assert!(
        outcome.errors.is_empty(),
        "branch dependency rediscovery succeeds: {:?}",
        outcome.errors
    );
    assert!(
        outcome.changes.configuration,
        "switching conditional includes invalidates loaded configuration"
    );
    assert!(
        monitor
            .layout
            .dependencies
            .iter()
            .any(|dependency| dependency.path.ends_with("other-config")),
        "the newly active missing include is observed"
    );
    assert!(
        !monitor
            .layout
            .dependencies
            .iter()
            .any(|dependency| dependency.path.ends_with("main-config")),
        "inactive include paths are removed"
    );
    Ok(())
}

#[test]
fn head_changes_refresh_coverage_with_unchanged_dependency_paths() -> TestResult {
    let fixture = fixture()?;
    let root = fixture.path();
    let config = root.join(".git/config");
    let mut contents = std::fs::read_to_string(&config)?;
    contents.push_str(
        "
[include]
    path = case-config
[core]
    ignoreCase = true
[includeIf \"onbranch:other\"]
    path = case-config
",
    );
    std::fs::write(config, contents)?;
    std::fs::write(
        root.join(".git/case-config"),
        "[core]
    ignoreCase = false
",
    )?;
    std::fs::write(root.join(".gitignore"), "*.ignored\n")?;
    std::fs::create_dir(root.join("FILE.IGNORED"))?;
    let open = || {
        let mut options = crate::open::Options::isolated();
        options.permissions.config.includes = true;
        crate::open_opts(root, options)
    };
    let mut monitor = open()?.monitor(Options::default())?;
    let now = Instant::now();
    for _ in 0..2 {
        let out = monitor.service(now, open);
        assert!(out.errors.is_empty(), "initial coverage succeeds: {:?}", out.errors);
    }
    let dependencies = monitor.layout.dependencies.clone();
    assert!(
        !monitor.worktree.registered.contains_key(&root.join("FILE.IGNORED")),
        "the initial effective case setting excludes the uppercase directory"
    );

    std::fs::write(root.join(".git/HEAD"), "ref: refs/heads/other\n")?;
    let repo = open()?;
    assert!(
        !repo.filesystem_options()?.ignore_case,
        "the repeated conditional include changes effective configuration without adding a source path"
    );
    monitor.observe(&modified(monitor.layout.git_dir.join("HEAD")), false, now);
    monitor.maintain(&repo, now, &mut Statistics::default(), &mut Vec::new())?;
    assert_eq!(
        monitor.layout.dependencies, dependencies,
        "deduplicated dependency paths cannot detect changed include precedence"
    );
    assert_eq!(
        monitor.worktree.registered.get(
            &monitor
                .layout
                .worktree
                .as_ref()
                .expect("fixture has a worktree")
                .join("FILE.IGNORED")
        ),
        Some(&plumbing::Watcher::supports_native_recursive_watches()),
        "HEAD refreshes backend-appropriate coverage even when configuration dependency paths are unchanged"
    );
    let changes = monitor.take_changes(now + monitor.options.metadata_debounce);
    assert!(
        changes.configuration && changes.worktree == Some(Scope::All),
        "consumers must re-evaluate configuration and worktree state after branch-conditioned settings change"
    );
    Ok(())
}

#[test]
fn all_worktrees_share_metadata_and_settle_without_reopening() -> TestResult {
    // Writable fixture copies emit FSEvents on their source; isolate this synthetic timeline.
    let (fixture, ()) = gix_testtools::scripted_fixture_read_only_with_post("make_notify_worktrees.sh", 3, |_| Ok(()))?;
    let root = fixture.join("main");
    let mut monitor = open(&root)?.monitor(Options {
        all_worktrees: true,
        ..Options::default()
    })?;
    let now = Instant::now();
    let initial = monitor.service(now, || open(&root));
    assert!(
        initial.errors.is_empty(),
        "initial union registration succeeds: {:?}",
        initial.errors
    );
    assert_eq!(
        monitor.other_worktrees.len(),
        2,
        "both sibling checkouts are discovered"
    );
    assert_eq!(
        initial.other_worktrees.len(),
        2,
        "each discovered checkout has one baseline"
    );
    assert!(
        initial
            .other_worktrees
            .iter()
            .all(|change| matches!(change.kind, WorktreeChange::Added)),
        "Added establishes the baseline without a redundant startup Changed event"
    );
    let registrations = monitor.metadata.registered.len()
        + monitor.worktree.registered.len()
        + monitor
            .other_worktrees
            .values()
            .map(|other| other.worktree.registered.len())
            .sum::<usize>();
    assert_eq!(
        initial.statistics.added, registrations,
        "only the owner registers shared metadata"
    );
    assert_eq!(
        monitor.metadata.registered.get(&monitor.layout.common_dir.join("refs")),
        Some(&true),
        "shared refs have one recursive registration"
    );
    for other in monitor.other_worktrees.values() {
        assert!(
            other.metadata.watcher.is_none(),
            "additional checkouts never allocate a metadata watcher"
        );
        assert!(
            other
                .worktree
                .registered
                .keys()
                .all(|path| path.starts_with(other.layout.worktree.as_ref().expect("checkout"))),
            "private index subscriptions belong to the union, not checkout streams"
        );
    }
    let verified = monitor.service(now, || open(&root));
    assert!(
        verified.errors.is_empty(),
        "inventory verification succeeds: {:?}",
        verified.errors
    );
    assert_eq!(
        (verified.statistics.added, verified.statistics.removed),
        (0, 0),
        "verification leaves registrations unchanged"
    );
    assert!(
        monitor.maintenance.is_none()
            && monitor
                .other_worktrees
                .values()
                .all(|other| other.maintenance.is_none()),
        "every inventory settles after verification"
    );
    monitor.service(now, || panic!("idle monitors must not reopen repositories"));
    monitor.coverage_lost(false, now);
    assert!(
        monitor
            .other_worktrees
            .values()
            .all(|other| other.pending_worktree.worktree == Some(Scope::All)),
        "union loss invalidates every checkout baseline"
    );
    let options = Options {
        worktree_coverage: None,
        ..monitor.options().clone()
    };
    drop(monitor);
    monitor = open(&root)?.monitor(options)?;
    assert!(
        monitor.other_worktrees.is_empty(),
        "disabling checkout coverage drops all additional checkout streams"
    );
    let disabled = monitor.service(Instant::now(), || open(&root));
    assert!(
        disabled.errors.is_empty() && disabled.other_worktrees.is_empty(),
        "metadata-only service does not rediscover checkout streams"
    );
    let options = Options {
        worktree_coverage: Some(WorktreeCoverage::IgnoreAware),
        ..monitor.options().clone()
    };
    drop(monitor);
    monitor = open(&root)?.monitor(options)?;
    let enabled = monitor.service(Instant::now(), || open(&root));
    assert!(
        enabled.errors.is_empty()
            && enabled.other_worktrees.len() == 2
            && enabled
                .other_worktrees
                .iter()
                .all(|change| matches!(change.kind, WorktreeChange::Added)),
        "enabling coverage establishes fresh Added baselines"
    );
    Ok(())
}

#[test]
fn all_worktrees_retry_backoff_covers_overdue_child_maintenance() -> TestResult {
    let fixture = gix_testtools::scripted_fixture_read_only("make_notify_worktrees.sh")?;
    let root = fixture.join("main");
    let mut monitor = open(&root)?.monitor(Options {
        all_worktrees: true,
        ..Options::default()
    })?;
    let now = Instant::now();
    for _ in 0..2 {
        let out = monitor.service(now, || open(&root));
        assert!(out.errors.is_empty(), "startup succeeds: {:?}", out.errors);
    }
    monitor
        .other_worktrees
        .values_mut()
        .next()
        .expect("linked checkout")
        .maintenance = Some(now);
    let out = monitor.service(now, || {
        Err(message("temporarily unavailable selected repository").raise())
    });
    assert_eq!(out.errors.len(), 1, "opening failure is reported once");
    assert!(
        monitor.next_timeout(now).is_some_and(|timeout| !timeout.is_zero()),
        "overdue children cannot bypass the coordinator's retry interval"
    );
    monitor.service(now, || panic!("failed maintenance must wait for retry backoff"));
    Ok(())
}

#[test]
fn all_worktrees_retry_backoff_covers_overdue_undiscovered_worktrees() -> TestResult {
    let fixture = gix_testtools::scripted_fixture_read_only("make_notify_worktrees.sh")?;
    let root = fixture.join("main");
    let mut monitor = open(&root)?.monitor(Options {
        all_worktrees: true,
        ..Options::default()
    })?;
    let mut now = Instant::now();
    for _ in 0..2 {
        let out = monitor.service(now, || open(&root));
        assert!(out.errors.is_empty(), "startup succeeds: {:?}", out.errors);
    }
    let git_dir = monitor.layout.common_dir.join("worktrees/linked-a");
    monitor
        .other_worktrees
        .remove(&git_dir)
        .expect("discovered linked checkout");
    monitor.undiscovered_worktrees.insert(git_dir.clone(), now);
    monitor.maintenance = Some(now);
    for _ in 0..2 {
        let out = monitor.service(now, || {
            Err(message("temporarily unavailable selected repository").raise())
        });
        assert_eq!(out.errors.len(), 1, "the coordinator reports its opening failure once");
        let retry = now + monitor.options.retry_interval;
        assert_eq!(
            monitor.undiscovered_worktrees.get(&git_dir),
            Some(&retry),
            "coordinator failure postpones overdue undiscovered entries just like known children"
        );
        assert_eq!(
            monitor.maintenance,
            Some(retry),
            "the old map deadline cannot override retry backoff"
        );
        assert!(
            monitor.next_timeout(now).is_some_and(|timeout| !timeout.is_zero()),
            "undiscovered entries cannot cause a busy loop after coordinator failure"
        );
        let idle = monitor.service(now, || panic!("failed discovery must wait for retry backoff"));
        assert!(
            idle.errors.is_empty(),
            "service before the retry deadline does not repeat the error"
        );
        now = retry;
    }
    Ok(())
}

#[test]
fn bare_main_without_checkout_recovers_undiscovered_registration() -> TestResult {
    // Writable fixture copies emit FSEvents on their source; isolate this synthetic timeline.
    let (fixture, ()) = gix_testtools::scripted_fixture_read_only_with_post("make_notify_worktrees.sh", 3, |_| Ok(()))?;
    let root = fixture.join("bare-a");
    let mut monitor = open(&root)?.monitor(Options {
        all_worktrees: true,
        ..Options::default()
    })?;
    let now = Instant::now();
    let main_git_dir = monitor.layout.common_dir.clone();
    for _ in 0..2 {
        let out = monitor.service(now, || open(&root));
        assert!(out.errors.is_empty(), "startup succeeds: {:?}", out.errors);
        assert!(
            out.other_worktrees.iter().all(|change| change.git_dir != main_git_dir),
            "a bare main has no checkout baseline or lifecycle event"
        );
    }
    monitor.undiscovered_worktrees.insert(main_git_dir.clone(), now);
    monitor.maintenance = Some(now);
    let recovered = monitor.service(now, || open(&root));
    assert!(
        recovered.errors.is_empty(),
        "rediscovery of the bare main succeeds: {:?}",
        recovered.errors
    );
    assert!(
        monitor.undiscovered_worktrees.is_empty(),
        "successfully discovering that the main is bare clears its previous failure deadline"
    );
    assert!(
        recovered.changes.is_empty() && recovered.other_worktrees.is_empty(),
        "an absent bare-main checkout neither publishes Added nor invalidates healthy checkouts"
    );
    assert!(
        !monitor.other_worktrees.contains_key(&main_git_dir),
        "a bare main never becomes a checkout monitor"
    );
    assert!(
        monitor.maintenance.is_none()
            && monitor
                .other_worktrees
                .values()
                .all(|other| other.maintenance.is_none()),
        "all inventories settle after rediscovering the bare main"
    );
    let idle = monitor.service(now + monitor.options.retry_interval, || {
        panic!("a recovered bare main must not schedule endless rediscovery")
    });
    assert!(
        idle.errors.is_empty() && idle.changes.is_empty() && idle.other_worktrees.is_empty(),
        "the old retry deadline remains cleared without generating a bare-main baseline"
    );
    Ok(())
}

#[test]
fn disabled_all_worktree_coverage_routes_metadata_as_selected_only() -> TestResult {
    let fixture = gix_testtools::scripted_fixture_read_only("make_notify_worktrees.sh")?;
    let root = fixture.join("linked-a");
    for coverage in [None, Some(WorktreeCoverage::All), Some(WorktreeCoverage::IgnoreAware)] {
        let mut monitor = open(&root)?.monitor(Options {
            all_worktrees: true,
            worktree_coverage: coverage,
            metadata_debounce: Duration::ZERO,
            ..Options::default()
        })?;
        let mut now = Instant::now();
        for _ in 0..2 {
            let out = monitor.service(now, || open(&root));
            assert!(out.errors.is_empty(), "startup succeeds: {:?}", out.errors);
        }
        if coverage.is_some() {
            let options = Options {
                worktree_coverage: None,
                ..monitor.options().clone()
            };
            drop(monitor);
            monitor = open(&root)?.monitor(options)?;
            now = Instant::now();
            for _ in 0..2 {
                let out = monitor.service(now, || open(&root));
                assert!(
                    out.errors.is_empty(),
                    "disabling checkout coverage succeeds: {:?}",
                    out.errors
                );
            }
        }
        monitor.observe(&modified(monitor.layout.common_dir.join("MERGE_MSG")), false, now);
        let out = monitor.service(now, || panic!("an operation-state file does not change watch coverage"));
        assert_eq!(
            out.changes,
            Changes {
                operations: true,
                ..Changes::default()
            },
            "{coverage:?}: with checkout coverage disabled, main-private metadata belongs to the selected-only view"
        );
        assert!(
            out.other_worktrees.is_empty() && monitor.other_worktrees.is_empty(),
            "disabled all-worktree coverage has no child to receive main-private operation changes"
        );
        assert!(
            !monitor.metadata_is_shared(),
            "all_worktrees alone does not select shared metadata routing"
        );
    }
    Ok(())
}

#[test]
fn worktree_replace_failure_invalidates_coverage_despite_latched_maintenance_failure() -> TestResult {
    use gix_parallel::{Mutable, OwnShared, lock};

    for coverage in [WorktreeCoverage::All, WorktreeCoverage::IgnoreAware] {
        let fixture = gix_testtools::scripted_fixture_writable("make_notify_worktrees.sh")?;
        let root = fixture.path().join("linked-a");
        let mut monitor = open(&root)?.monitor(Options {
            metadata: false,
            worktree_coverage: Some(coverage),
            ..Options::default()
        })?;
        let now = Instant::now();
        for _ in 0..2 {
            let out = monitor.service(now, || open(&root));
            assert!(out.errors.is_empty(), "startup succeeds: {:?}", out.errors);
        }
        monitor.maintenance_failed(now);
        let failed = monitor.service(now, || panic!("the first failure schedules a future retry"));
        assert_eq!(
            failed.changes.worktree,
            Some(Scope::All),
            "the initial failure publishes its baseline invalidation"
        );
        assert!(
            monitor.maintenance_failed,
            "the failure latch remains set before native replacement"
        );

        // An empty watcher has no backend thread. Arm its waker only after set_waker's initial
        // callback: replace wakes synchronously after losing coverage, before native registration.
        // Removing the checkout there deterministically fails registration, not inventory discovery.
        let remove_on_replace = OwnShared::new(Mutable::new(None::<PathBuf>));
        let mut watcher = plumbing::Watcher::new(plumbing::Options::default())?;
        watcher.set_waker({
            let remove_on_replace = remove_on_replace.clone();
            move || {
                if let Some(root) = lock(&remove_on_replace).take() {
                    std::fs::remove_dir_all(root).expect("the disposable checkout can be removed before registration");
                }
            }
        });
        *lock(&remove_on_replace) = Some(root.clone());
        monitor.worktree = Domain {
            watcher: Some(watcher),
            ..Domain::default()
        };
        monitor.inventory_dirty = true;
        let retry = now + monitor.options.retry_interval;
        let out = monitor.service(retry, || open(&root));
        assert!(!root.try_exists()?, "the waker removed the checkout during replacement");
        assert!(
            lock(&remove_on_replace).is_none(),
            "the one-shot registration failure was injected"
        );
        assert_eq!(
            out.errors.len(),
            1,
            "{coverage:?}: native registration reports the missing checkout: {:?}",
            out.errors
        );
        assert!(
            monitor.worktree.rebuild,
            "failed native replacement requires rebuilding coverage"
        );
        assert_eq!(
            out.changes,
            Changes {
                worktree: Some(Scope::All),
                ..Changes::default()
            },
            "{coverage:?}: new native coverage loss cannot be suppressed by an already-set maintenance failure latch"
        );
        assert_eq!(
            monitor.maintenance,
            Some(retry + monitor.options.retry_interval),
            "native registration failure still backs off"
        );
    }
    Ok(())
}

#[test]
fn all_worktrees_require_a_new_monitor_for_a_different_primary() -> TestResult {
    let fixture = gix_testtools::scripted_fixture_read_only("make_notify_worktrees.sh")?;
    let root = fixture.join("linked-a");
    let mut monitor = open(&root)?.monitor(Options {
        all_worktrees: true,
        ..Options::default()
    })?;
    let now = Instant::now();
    for _ in 0..2 {
        let out = monitor.service(now, || open(&root));
        assert!(out.errors.is_empty(), "startup succeeds: {:?}", out.errors);
    }
    let primary = monitor.layout.git_dir.clone();
    monitor.rescan();
    let out = monitor.service(Instant::now(), || open(&fixture.join("main")));
    assert!(
        out.errors.iter().any(gix_error::Error::is_validation),
        "an implicit primary switch must not duplicate the main checkout"
    );
    assert_eq!(monitor.layout.git_dir, primary, "the existing primary remains selected");
    let options = monitor.options().clone();
    drop(monitor);
    monitor = open(&fixture.join("main"))?.monitor(options)?;
    let out = monitor.service(Instant::now(), || open(&fixture.join("main")));
    assert!(
        out.errors.is_empty() && out.other_worktrees.len() == 2,
        "dropping the old monitor and constructing a new one safely selects another primary"
    );
    Ok(())
}

#[test]
fn native_errors_count_discarded_registrations() -> TestResult {
    let fixture = gix_testtools::scripted_fixture_read_only("make_notify_repo.sh")?;
    let root = fixture.join("unignored");
    let mut monitor = open(&root)?.monitor(Options {
        metadata: false,
        worktree_coverage: Some(WorktreeCoverage::All),
        ..Options::default()
    })?;
    let now = Instant::now();
    let initial = monitor.service(now, || panic!("all-worktree-only coverage does not reopen"));
    assert!(initial.errors.is_empty(), "startup succeeds: {:?}", initial.errors);
    let registered = monitor.worktree.registered.len();
    assert!(registered > 0, "the checkout has native coverage");
    // A failing callback queues a native error without filesystem mutation or timing races.
    monitor
        .worktree
        .watcher
        .as_mut()
        .expect("startup installed the watcher")
        .set_waker(|| panic!("deliberate wake callback failure"));
    let mut out = Outcome::default();
    monitor.poll(now, &mut out);
    assert!(!out.errors.is_empty(), "the callback failure is reported");
    assert!(monitor.worktree.registered.is_empty(), "failed coverage is discarded");
    assert_eq!(
        out.statistics.removed, registered,
        "discarded registrations count as removed"
    );
    let quiet = monitor.service(now, || panic!("native recovery must respect backoff"));
    assert!(
        quiet.errors.is_empty() && quiet.recovery_pending,
        "quiet native retry preserves degraded coverage"
    );
    let recovered = monitor.service(now + monitor.options.retry_interval, || {
        panic!("all coverage does not reopen")
    });
    assert!(
        recovered.errors.is_empty() && !recovered.recovery_pending,
        "native registration repairs coverage"
    );
    Ok(())
}

#[test]
fn incomplete_worktrees_do_not_poison_the_shared_metadata_inventory() -> TestResult {
    let fixture = gix_testtools::scripted_fixture_writable("make_notify_worktrees.sh")?;
    let root = fixture.path().join("main");
    let mut monitor = open(&root)?.monitor(Options {
        all_worktrees: true,
        worktree_coverage: Some(WorktreeCoverage::All),
        ..Options::default()
    })?;
    let now = Instant::now();
    for _ in 0..2 {
        let out = monitor.service(now, || open(&root));
        assert!(out.errors.is_empty(), "startup succeeds: {:?}", out.errors);
    }
    let key = monitor.other_worktrees.keys().next().expect("linked checkout").clone();
    let checkout = monitor.other_worktrees[&key]
        .layout
        .worktree
        .clone()
        .expect("checkout path");
    std::fs::remove_dir_all(&checkout)?;
    std::fs::remove_file(key.join("gitdir"))?;
    monitor
        .other_worktrees
        .get_mut(&key)
        .expect("retained child")
        .maintenance = Some(now);
    monitor.service(now, || open(&root));
    // Native root loss can defer maintenance until the retry deadline.
    let out = monitor.service(now + monitor.options.retry_interval, || open(&root));
    assert!(
        !out.errors.is_empty(),
        "incomplete registration reports degraded coverage"
    );
    assert!(
        !monitor.metadata.rebuild,
        "a degraded child must not prevent rebuilding the metadata union"
    );
    assert!(
        monitor.metadata.registered.keys().all(|path| path.is_dir()),
        "the union does not retain registrations for missing checkout roots"
    );
    assert!(
        monitor.other_worktrees.contains_key(&key)
            && out
                .other_worktrees
                .iter()
                .all(|change| !matches!(change.kind, WorktreeChange::Removed)),
        "inaccessible checkouts are retained, not declared removed"
    );
    Ok(())
}

#[test]
fn stale_worktree_registration_retries_do_not_invalidate_healthy_checkouts() -> TestResult {
    let (fixture, ()) =
        gix_testtools::scripted_fixture_read_only_with_post("make_notify_worktrees.sh", 1, |fixture| {
            if let gix_testtools::FixtureState::Uninitialized(path) = fixture {
                // Keep HEAD, commondir, index and gitdir: Git still lists this complete registration,
                // but opening its missing checkout must fail during every coverage retry.
                std::fs::remove_dir_all(path.join("linked-a"))?;
            }
            Ok(())
        })?;
    let root = fixture.join("main");
    for coverage in [WorktreeCoverage::All, WorktreeCoverage::IgnoreAware] {
        let mut monitor = open(&root)?.monitor(Options {
            all_worktrees: true,
            worktree_coverage: Some(coverage),
            metadata_debounce: Duration::ZERO,
            ..Options::default()
        })?;
        let stale_git_dir = monitor.layout.common_dir.join("worktrees/linked-a");
        let healthy_git_dir = monitor.layout.common_dir.join("worktrees/linked-b");
        let mut now = Instant::now();
        let initial = monitor.service(now, || open(&root));
        assert_eq!(
            initial.errors.len(),
            1,
            "{coverage:?}: the complete-but-stale registration reports its missing checkout: {:?}",
            initial.errors
        );
        assert_eq!(initial.other_worktrees.len(), 2, "both registrations are retained");
        assert!(
            initial
                .other_worktrees
                .iter()
                .all(|change| matches!(change.kind, WorktreeChange::Added)),
            "startup establishes Added baselines, not Changed events: {:?}",
            initial.other_worktrees
        );
        assert!(
            monitor.other_worktrees.contains_key(&stale_git_dir),
            "failed checkout coverage must not retire a complete registration"
        );
        assert!(
            !monitor.metadata.rebuild && monitor.metadata.registered.keys().all(|path| path.is_dir()),
            "a missing checkout does not poison initial union registration"
        );
        let union = monitor.metadata.registered.clone();
        let retry_interval = monitor.options.retry_interval;
        for retry in 0..3 {
            now += retry_interval;
            let mut expected_primary = Changes::default();
            let mut expected_other = Vec::new();
            if retry == 1 {
                // Inject ordinary observations instead of mutating this read-only fixture or
                // forcing a rescan, which would legitimately invalidate the entire shared stream.
                let path = monitor
                    .layout
                    .worktree
                    .as_ref()
                    .expect("primary checkout")
                    .join("tracked/file");
                monitor.observe(&modified(path), true, now);
                monitor.observe(&modified(monitor.layout.common_dir.join("refs/heads/main")), false, now);
                expected_primary.references = true;
                expected_primary.worktree = Some(Scope::Paths(vec![path_change(
                    "tracked/file",
                    EventKind::Modify,
                    PathKind::File,
                )]));
                let other = monitor
                    .other_worktrees
                    .get_mut(&healthy_git_dir)
                    .expect("healthy child");
                let path = other
                    .layout
                    .worktree
                    .as_ref()
                    .expect("healthy checkout")
                    .join("tracked/file");
                other.observe(&modified(path), true, now);
                expected_other.push(healthy_git_dir.clone());
            }
            let out = monitor.service(now, || open(&root));
            assert_eq!(
                out.errors.len(),
                1,
                "{coverage:?}: retry {retry} still reports the child failure: {:?}",
                out.errors
            );
            assert_eq!(
                out.changes, expected_primary,
                "{coverage:?}: retry {retry} neither invents primary changes nor discards healthy observations"
            );
            assert_eq!(
                out.other_worktrees
                    .iter()
                    .map(|change| change.git_dir.clone())
                    .collect::<Vec<_>>(),
                expected_other,
                "{coverage:?}: unchanged child failures do not republish Changed or lifecycle events"
            );
            if let Some(change) = out.other_worktrees.first() {
                assert!(
                    matches!(&change.kind, WorktreeChange::Changed(changes) if *changes == Changes {
                        worktree: Some(Scope::Paths(vec![path_change("tracked/file", EventKind::Modify, PathKind::File)])),
                        ..Changes::default()
                    }),
                    "a healthy sibling's actual path observation survives another child's failed retry: {change:?}"
                );
            }
            assert_eq!(
                monitor.metadata.registered, union,
                "failed retries keep the settled union unchanged"
            );
            assert_eq!(
                (out.statistics.added, out.statistics.removed),
                (0, 0),
                "retries do not churn native registrations"
            );
            assert_eq!(
                monitor.other_worktrees[&stale_git_dir].maintenance,
                Some(now + retry_interval),
                "the failed child waits for its next retry deadline"
            );
            assert!(
                monitor.other_worktrees[&healthy_git_dir].maintenance.is_none(),
                "the healthy sibling settles despite failed retries elsewhere"
            );
            let idle = monitor.service(now, || panic!("a failed child must respect retry backoff"));
            assert!(
                idle.errors.is_empty() && idle.changes.is_empty() && idle.other_worktrees.is_empty(),
                "service before the next retry has neither changes nor repeated diagnostics"
            );
        }
    }
    Ok(())
}

#[test]
fn child_maintenance_failure_and_same_layout_recovery_invalidate_only_that_child() -> TestResult {
    // Copying a read-only fixture for writable tests emits FSEvents on the source files too.
    // A dedicated cache identity keeps native copy notifications out of this synthetic timeline.
    let (fixture, ()) = gix_testtools::scripted_fixture_read_only_with_post("make_notify_worktrees.sh", 2, |_| Ok(()))?;
    let root = fixture.join("main");
    for coverage in [WorktreeCoverage::All, WorktreeCoverage::IgnoreAware] {
        let mut monitor = open(&root)?.monitor(Options {
            all_worktrees: true,
            worktree_coverage: Some(coverage),
            ..Options::default()
        })?;
        let mut now = Instant::now();
        for _ in 0..2 {
            let out = monitor.service(now, || open(&root));
            assert!(out.errors.is_empty(), "startup succeeds: {:?}", out.errors);
        }
        let git_dir = monitor.layout.common_dir.join("worktrees/linked-a");
        let layout = monitor.other_worktrees[&git_dir].layout.clone();
        let union = monitor.metadata.registered.clone();
        let watches = monitor.other_worktrees[&git_dir].worktree.registered.clone();
        let mut full = Changes::metadata_all();
        full.merge(Changes::worktree_all());
        for _ in 0..2 {
            // Model a transient maintenance error without changing any watched path. Filesystem
            // deletion/restoration would change the union and mask a missing recovery invalidation.
            monitor
                .other_worktrees
                .get_mut(&git_dir)
                .expect("retained child")
                .maintenance_failed(now);
            let failed = monitor.service(now, || panic!("the injected failure has a future retry deadline"));
            assert!(failed.recovery_pending, "the parent reports a degraded linked checkout");
            assert!(
                failed.errors.is_empty(),
                "the state transition does not synthesize diagnostics"
            );
            assert_only_child_change(&failed, &layout, &full);

            monitor
                .other_worktrees
                .get_mut(&git_dir)
                .expect("retained child")
                .maintenance_failed(now);
            let repeated = monitor.service(now, || panic!("repeated failures still respect backoff"));
            assert!(repeated.recovery_pending, "quiet child backoff remains degraded");
            assert!(
                repeated.changes.is_empty() && repeated.other_worktrees.is_empty(),
                "{coverage:?}: the first failure invalidates once, not on every retry"
            );

            now += monitor.options.retry_interval;
            let mut opens = 0;
            let recovered = monitor.service(now, || {
                opens += 1;
                open(&root)
            });
            assert_eq!(opens, 1, "the due child recovery uses one freshly opened repository");
            assert!(recovered.errors.is_empty(), "recovery succeeds: {:?}", recovered.errors);
            assert!(
                !recovered.recovery_pending,
                "repaired child coverage clears the parent recovery state"
            );
            assert_only_child_change(&recovered, &layout, &full);
            assert_eq!(
                monitor.other_worktrees[&git_dir].layout, layout,
                "recovery retains the same layout"
            );
            assert_eq!(
                monitor.metadata.registered, union,
                "recovery does not alter the shared union"
            );
            assert_eq!(
                monitor.other_worktrees[&git_dir].worktree.registered, watches,
                "recovery retains the same child watches"
            );
            assert_eq!(
                (recovered.statistics.added, recovered.statistics.removed),
                (0, 0),
                "recovery invalidates even without watch replacement"
            );
            let idle = monitor.service(now, || panic!("unchanged recovery needs no further inventory work"));
            assert!(
                idle.changes.is_empty() && idle.other_worktrees.is_empty(),
                "recovery publishes one full invalidation and re-arms the failure latch"
            );
        }
    }
    Ok(())
}

fn assert_only_child_change(out: &Outcome, layout: &Layout, expected: &Changes) {
    assert!(
        out.changes.is_empty(),
        "a child-only failure/recovery must not invalidate the primary: {:?}",
        out.changes
    );
    assert_eq!(
        out.other_worktrees.len(),
        1,
        "only the affected child publishes changes: {:?}",
        out.other_worktrees
    );
    let change = &out.other_worktrees[0];
    assert_eq!(
        change.git_dir, layout.git_dir,
        "the private Git directory owns the failure/recovery"
    );
    assert_eq!(
        Some(&change.worktree),
        layout.worktree.as_ref(),
        "the unchanged checkout keeps its identity"
    );
    assert!(
        matches!(&change.kind, WorktreeChange::Changed(changes) if changes == expected),
        "an existing child's failure/recovery is a full Changed invalidation, not a new baseline: {change:?}"
    );
}