test-r-core 8.0.1

Test framework for Rust (core module)
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
use rand::prelude::{SliceRandom, StdRng};
use rand::SeedableRng;
use std::any::Any;
use std::collections::{HashMap, HashSet, VecDeque};
use std::fmt::{Debug, Formatter};
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::Arc;
use topological_sort::TopologicalSort;

use crate::args::Arguments;
use crate::internal::{
    apply_suite_props_to_tests, filter_registered_tests, DepScope, DependencyConstructor,
    DependencyView, HostedRpcOwnerCell, RegisteredDependency, RegisteredTest,
    RegisteredTestSuiteProperty,
};

/// Wire bytes for a single Cloneable / Hosted dependency, keyed by its
/// fully-qualified id (`{crate}::{module}::{name}`).
pub type DepWireBytes = (String, Vec<u8>);

/// Parent-held owner value (used only for `Hosted` deps — the parent keeps
/// the owner alive for the suite's duration).
pub type HostedOwner = Arc<dyn Any + Send + Sync>;

#[cfg(feature = "tokio")]
type ParentSharedDependenciesFuture<'a> = std::pin::Pin<
    Box<dyn std::future::Future<Output = HashMap<String, Arc<dyn Any + Send + Sync>>> + 'a>,
>;

#[cfg(test)]
/// Result of [`TestSuiteExecution::collect_hosted_descriptor_bytes_sync`] /
/// [`TestSuiteExecution::collect_hosted_descriptor_bytes_async`]: the
/// descriptor bytes that get shipped to workers, plus the parent-held owner
/// values that must outlive every worker.
pub type HostedDescriptorCollection = (Vec<DepWireBytes>, Vec<HostedOwner>);

/// Parent-side materialisation output for dependency scopes whose worker-side
/// value is derived from parent-owned state instead of by rerunning the user
/// constructor in each worker process.
pub struct ParentSharedDependencies {
    pub cloneable_wire_bytes: Vec<DepWireBytes>,
    /// Parent-constructed `Cloneable` values keyed by fully-qualified dep id.
    /// In **no-spawn-workers** mode (e.g. `--nocapture`) the runner installs
    /// these directly into the execution tree via
    /// [`TestSuiteExecution::provide_cloneable_value`] so tests see the
    /// parent's value without re-running the constructor in
    /// `materialize_deps`. For Cloneable, the round-trip
    /// `from_wire(to_wire(value))` is by contract semantics-preserving, so
    /// reusing the parent value directly is equivalent to round-tripping
    /// while avoiding the duplicate constructor run that historically
    /// occurred on the no-spawn-workers code path.
    ///
    /// In spawn-workers mode this list is unused — workers receive
    /// `cloneable_wire_bytes` over IPC instead.
    pub cloneable_local_values: Vec<(String, Arc<dyn Any + Send + Sync>)>,
    pub hosted_descriptor_bytes: Vec<DepWireBytes>,
    pub hosted_owners: Vec<HostedOwner>,
    pub hosted_rpc_owner_cells: Vec<(String, Arc<HostedRpcOwnerCell>)>,
}

impl ParentSharedDependencies {
    fn new() -> Self {
        Self {
            cloneable_wire_bytes: Vec::new(),
            cloneable_local_values: Vec::new(),
            hosted_descriptor_bytes: Vec::new(),
            hosted_owners: Vec::new(),
            hosted_rpc_owner_cells: Vec::new(),
        }
    }
}

pub(crate) struct TestSuiteExecution {
    crate_and_module: String,
    dependencies: Vec<RegisteredDependency>,
    tests: Vec<RegisteredTest>,
    props: Vec<RegisteredTestSuiteProperty>,
    inner: Vec<TestSuiteExecution>,
    materialized_dependencies: HashMap<String, Arc<dyn Any + Send + Sync>>,
    sequential_lock: SequentialExecutionLock,
    remaining_count: usize,
    idx: usize,
    is_sequential: bool,
    skip_creating_dependencies: bool,
    in_progress: Arc<AtomicUsize>,
}

impl TestSuiteExecution {
    pub fn construct(
        arguments: &Arguments,
        dependencies: &[RegisteredDependency],
        tests: &[RegisteredTest],
        props: &[RegisteredTestSuiteProperty],
    ) -> (Self, Vec<RegisteredTest>) {
        let tests_with_props = apply_suite_props_to_tests(tests, props);
        let mut filtered_tests = filter_registered_tests(arguments, &tests_with_props);
        Self::shuffle(arguments, &mut filtered_tests);
        filtered_tests.reverse();

        if filtered_tests.is_empty() {
            (
                Self::root(
                    dependencies
                        .iter()
                        .filter(|dep| dep.crate_name.is_empty() && dep.module_path.is_empty())
                        .cloned()
                        .collect::<Vec<_>>(),
                    Vec::new(),
                    props
                        .iter()
                        .filter(|dep| dep.crate_name().is_empty() && dep.module_path().is_empty())
                        .cloned()
                        .collect::<Vec<_>>(),
                ),
                Vec::new(),
            )
        } else {
            let mut root = Self::root(Vec::new(), Vec::new(), Vec::new());

            for prop in props {
                root.add_prop(prop.clone());
            }

            for dep in dependencies {
                root.add_dependency(dep.clone());
            }

            for test in filtered_tests.clone() {
                root.add_test(test.clone());
            }

            root.propagate_sequential(None);
            root.prune_unused_deps();

            (root, filtered_tests)
        }
    }

    fn shuffle(arguments: &Arguments, tests: &mut [RegisteredTest]) {
        if let Some(seed) = arguments.shuffle_seed {
            let mut rng = StdRng::seed_from_u64(seed);
            tests.shuffle(&mut rng);
        }
    }

    /// Disables creating dependencies when picking the next test. This is useful when the execution plan
    /// is only used to drive spawned workers instead of actually running the tests.
    pub fn skip_creating_dependencies(&mut self) {
        self.skip_creating_dependencies = true;
        for inner in &mut self.inner {
            inner.skip_creating_dependencies();
        }
    }

    pub fn remaining(&self) -> usize {
        self.remaining_count
    }

    pub fn is_empty(&self) -> bool {
        self.tests.is_empty() && self.inner.is_empty()
    }

    pub fn is_done(&self) -> bool {
        self.remaining_count == 0
    }

    /// Returns true if either this level, or any of the inner levels have dependencies
    #[allow(dead_code)]
    pub fn has_dependencies(&self) -> bool {
        !self.dependencies.is_empty() || self.inner.iter().any(|inner| inner.has_dependencies())
    }

    /// Returns true if any dependency in this subtree uses `DepScope::Shared`
    /// — those force single-threaded execution when output capture is on,
    /// because the materialised value cannot cross the parent/worker boundary.
    pub fn has_shared_dependencies(&self) -> bool {
        self.dependencies
            .iter()
            .any(|d| d.scope == DepScope::Shared)
            || self
                .inner
                .iter()
                .any(|inner| inner.has_shared_dependencies())
    }

    /// Returns true if any dependency in this subtree uses `DepScope::Cloneable`.
    #[allow(dead_code)]
    pub fn has_cloneable_dependencies(&self) -> bool {
        self.dependencies
            .iter()
            .any(|d| d.scope == DepScope::Cloneable)
            || self
                .inner
                .iter()
                .any(|inner| inner.has_cloneable_dependencies())
    }

    /// Returns true if any dependency in this subtree uses `DepScope::Hosted`.
    /// The parent keeps Hosted owners alive for the duration of the suite
    /// while shipping descriptors to workers.
    #[allow(dead_code)]
    pub fn has_hosted_dependencies(&self) -> bool {
        self.dependencies
            .iter()
            .any(|d| d.scope == DepScope::Hosted)
            || self
                .inner
                .iter()
                .any(|inner| inner.has_hosted_dependencies())
    }

    /// Returns true if any dependency in this subtree uses `DepScope::HostedRpc`.
    /// The parent keeps owner cells alive for the suite and routes
    /// worker-initiated IPC calls to those cells.
    #[allow(dead_code)]
    pub fn has_hosted_rpc_dependencies(&self) -> bool {
        self.dependencies
            .iter()
            .any(|d| d.scope == DepScope::HostedRpc)
            || self
                .inner
                .iter()
                .any(|inner| inner.has_hosted_rpc_dependencies())
    }

    /// Collects every Cloneable dependency in this subtree (depth-first).
    #[allow(dead_code)]
    pub fn collect_cloneable_dependencies(&self) -> Vec<RegisteredDependency> {
        let mut out = Vec::new();
        self.collect_cloneable_dependencies_into(&mut out);
        out
    }

    #[allow(dead_code)]
    fn collect_cloneable_dependencies_into(&self, out: &mut Vec<RegisteredDependency>) {
        for dep in &self.dependencies {
            if dep.scope == DepScope::Cloneable {
                out.push(dep.clone());
            }
        }
        for inner in &self.inner {
            inner.collect_cloneable_dependencies_into(out);
        }
    }

    /// Walks the subtree, materialising dependencies in dependency order and
    /// collecting the parent-side wire/state needed by Cloneable, Hosted, and
    /// HostedRpc scopes. Constructor dependencies are resolved in this parent
    /// context, but workers still receive these shared scopes as dependency-free
    /// leaves: Cloneable/Hosted values are reconstructed from bytes, and
    /// HostedRpc values are stubs backed by a channel.
    pub fn collect_parent_shared_dependencies_sync(&self) -> ParentSharedDependencies {
        let mut out = ParentSharedDependencies::new();
        let parent_map = HashMap::new();
        self.collect_parent_shared_dependencies_into_sync(&parent_map, &mut out);
        out
    }

    fn collect_parent_shared_dependencies_into_sync(
        &self,
        parent_map: &HashMap<String, Arc<dyn Any + Send + Sync>>,
        out: &mut ParentSharedDependencies,
    ) -> HashMap<String, Arc<dyn Any + Send + Sync>> {
        let mut dependency_map = parent_map.clone();
        let sorted_dependencies = self.sorted_dependencies();

        for dep in sorted_dependencies {
            if dependency_map.contains_key(&dep.name) {
                continue;
            }

            let value = Self::construct_dependency_sync(dep, &dependency_map);
            match dep.scope {
                DepScope::Cloneable => {
                    let codec = dep.cloneable_codec.as_ref().unwrap_or_else(|| {
                        panic!("Cloneable dep '{}' missing CloneableCodec", dep.name)
                    });
                    out.cloneable_wire_bytes
                        .push((dep.qualified_id(), (codec.to_wire)(value.clone())));
                    // Keep the parent-constructed value too, for the
                    // no-spawn-workers code path that installs Cloneable
                    // values directly into the execution tree (instead of
                    // re-running the constructor inside `materialize_deps`).
                    out.cloneable_local_values
                        .push((dep.qualified_id(), value.clone()));
                }
                DepScope::Hosted => {
                    let codec = dep.hosted_codec.as_ref().unwrap_or_else(|| {
                        panic!("Hosted dep '{}' missing hosted codec", dep.name)
                    });
                    out.hosted_descriptor_bytes
                        .push((dep.qualified_id(), (codec.to_wire)(value.clone())));
                    out.hosted_owners.push(value.clone());
                }
                DepScope::HostedRpc => {
                    let factory = dep.rpc_factory.as_ref().unwrap_or_else(|| {
                        panic!("HostedRpc dep '{}' missing RpcFactory", dep.name)
                    });
                    let cell = (factory.owner_into_cell)(value.clone());
                    out.hosted_rpc_owner_cells.push((dep.qualified_id(), cell));
                }
                DepScope::Shared | DepScope::PerWorker => {}
            }

            dependency_map.insert(dep.name.clone(), value);
        }

        for inner in &self.inner {
            inner.collect_parent_shared_dependencies_into_sync(&dependency_map, out);
        }

        dependency_map
    }

    fn construct_dependency_sync(
        dep: &RegisteredDependency,
        dependency_map: &HashMap<String, Arc<dyn Any + Send + Sync>>,
    ) -> Arc<dyn Any + Send + Sync> {
        match &dep.constructor {
            DependencyConstructor::Sync(cons) => cons(Arc::new(dependency_map.clone())),
            DependencyConstructor::Async(cons) => {
                futures::executor::block_on(cons(Arc::new(dependency_map.clone())))
            }
        }
    }

    /// Collects only Cloneable wire bytes. The runner uses
    /// [`Self::collect_parent_shared_dependencies_sync`] to collect all shared
    /// parent-side values in one pass; this narrower helper remains for unit
    /// tests and focused callers.
    #[cfg(test)]
    pub fn collect_cloneable_wire_bytes_sync(&self) -> Vec<(String, Vec<u8>)> {
        self.collect_parent_shared_dependencies_sync()
            .cloneable_wire_bytes
    }

    /// Parent-side materialisation for `Hosted` dependencies.
    ///
    /// The returned descriptor bytes are keyed by fully-qualified dep id and
    /// the returned owner values must be kept alive for the duration of the
    /// suite. Unlike Cloneable, Hosted owners may hold resources (TCP
    /// listeners, Docker containers, gRPC clients, etc.) that workers'
    /// reconstructed handles depend on.
    #[cfg(test)]
    pub fn collect_hosted_descriptor_bytes_sync(&self) -> HostedDescriptorCollection {
        let collected = self.collect_parent_shared_dependencies_sync();
        (collected.hosted_descriptor_bytes, collected.hosted_owners)
    }

    /// Parent-side materialisation for `HostedRpc` dependencies.
    ///
    /// Returns `(qualified_id, cell)` pairs that the runtime keeps alive for
    /// the suite's lifetime and uses to dispatch worker-initiated RPC calls.
    #[cfg(test)]
    pub fn collect_hosted_rpc_owner_cells_sync(&self) -> Vec<(String, Arc<HostedRpcOwnerCell>)> {
        self.collect_parent_shared_dependencies_sync()
            .hosted_rpc_owner_cells
    }

    /// Async counterpart of [`Self::collect_parent_shared_dependencies_sync`].
    /// Async constructors are awaited on the parent before workers receive
    /// wire bytes, descriptors, or RPC stubs.
    #[cfg(feature = "tokio")]
    pub async fn collect_parent_shared_dependencies_async(&self) -> ParentSharedDependencies {
        let mut out = ParentSharedDependencies::new();
        let parent_map = HashMap::new();
        self.collect_parent_shared_dependencies_into_async(&parent_map, &mut out)
            .await;
        out
    }

    #[cfg(feature = "tokio")]
    fn collect_parent_shared_dependencies_into_async<'a>(
        &'a self,
        parent_map: &'a HashMap<String, Arc<dyn Any + Send + Sync>>,
        out: &'a mut ParentSharedDependencies,
    ) -> ParentSharedDependenciesFuture<'a> {
        Box::pin(async move {
            let mut dependency_map = parent_map.clone();
            let sorted_dependencies = self.sorted_dependencies();

            for dep in sorted_dependencies {
                if dependency_map.contains_key(&dep.name) {
                    continue;
                }

                let value = match &dep.constructor {
                    DependencyConstructor::Sync(cons) => cons(Arc::new(dependency_map.clone())),
                    DependencyConstructor::Async(cons) => {
                        cons(Arc::new(dependency_map.clone())).await
                    }
                };
                match dep.scope {
                    DepScope::Cloneable => {
                        let codec = dep.cloneable_codec.as_ref().unwrap_or_else(|| {
                            panic!("Cloneable dep '{}' missing CloneableCodec", dep.name)
                        });
                        out.cloneable_wire_bytes
                            .push((dep.qualified_id(), (codec.to_wire)(value.clone())));
                        // Keep the parent-constructed value too, for the
                        // no-spawn-workers code path that installs Cloneable
                        // values directly into the execution tree (instead
                        // of re-running the constructor inside
                        // `materialize_deps`).
                        out.cloneable_local_values
                            .push((dep.qualified_id(), value.clone()));
                    }
                    DepScope::Hosted => {
                        let codec = dep.hosted_codec.as_ref().unwrap_or_else(|| {
                            panic!("Hosted dep '{}' missing hosted codec", dep.name)
                        });
                        out.hosted_descriptor_bytes
                            .push((dep.qualified_id(), (codec.to_wire)(value.clone())));
                        out.hosted_owners.push(value.clone());
                    }
                    DepScope::HostedRpc => {
                        let factory = dep.rpc_factory.as_ref().unwrap_or_else(|| {
                            panic!("HostedRpc dep '{}' missing RpcFactory", dep.name)
                        });
                        let cell = (factory.owner_into_cell)(value.clone());
                        out.hosted_rpc_owner_cells.push((dep.qualified_id(), cell));
                    }
                    DepScope::Shared | DepScope::PerWorker => {}
                }

                dependency_map.insert(dep.name.clone(), value);
            }

            for inner in &self.inner {
                inner
                    .collect_parent_shared_dependencies_into_async(&dependency_map, out)
                    .await;
            }
            dependency_map
        })
    }

    /// Async Hosted-only collection helper retained for focused callers.
    #[cfg(feature = "tokio")]
    #[cfg(test)]
    pub async fn collect_hosted_descriptor_bytes_async(&self) -> HostedDescriptorCollection {
        let collected = self.collect_parent_shared_dependencies_async().await;
        (collected.hosted_descriptor_bytes, collected.hosted_owners)
    }

    /// Async Cloneable-only collection helper retained for focused callers.
    ///
    /// **Intentionally `!Send`.** The underlying `DependencyConstructor::Async`
    /// future is not `Send`, so the returned future from this collector cannot
    /// be either. Must be awaited on the root runner task (i.e., under
    /// `Runtime::block_on` or directly inside `test_runner`) — never inside
    /// `tokio::spawn` / a `JoinSet`. If we ever want to spawn Cloneable
    /// collection onto a worker, the constructor type would need to require
    /// `Send` first.
    #[cfg(feature = "tokio")]
    #[cfg(test)]
    pub async fn collect_cloneable_wire_bytes_async(&self) -> Vec<(String, Vec<u8>)> {
        self.collect_parent_shared_dependencies_async()
            .await
            .cloneable_wire_bytes
    }

    /// Worker-side counterpart to [`Self::collect_cloneable_wire_bytes_sync`]:
    /// pre-populates the Cloneable dep value at the node where the dep is
    /// registered, so the upcoming `materialize_deps_sync` call uses the
    /// provided value instead of running the original constructor. The lookup
    /// is keyed by the dep's fully-qualified id
    /// (`{crate}::{module}::{name}`), but the value is stored under the local
    /// `name` so the rest of the materialisation logic keeps working unchanged.
    /// Returns `true` if a matching dep was found in any node of the subtree.
    pub fn provide_cloneable_value(
        &mut self,
        dep_id: &str,
        value: Arc<dyn Any + Send + Sync>,
    ) -> bool {
        let applied = self.provide_cloneable_value_internal(dep_id, value);
        if applied {
            self.prune_unused_deps();
        }
        applied
    }

    fn provide_cloneable_value_internal(
        &mut self,
        dep_id: &str,
        value: Arc<dyn Any + Send + Sync>,
    ) -> bool {
        let mut applied = false;
        if let Some((local_name, dep_idx)) = self
            .dependencies
            .iter()
            .enumerate()
            .find(|(_, d)| d.qualified_id() == dep_id)
            .map(|(idx, d)| (d.name.clone(), idx))
        {
            // From the worker execution tree's perspective this dependency is
            // now a leaf: its value came from wire bytes or a HostedRpc channel,
            // so the worker must not instantiate constructor-only dependencies
            // that were needed solely in the parent collection context.
            self.dependencies[dep_idx].dependencies.clear();
            self.materialized_dependencies
                .insert(local_name, value.clone());
            applied = true;
        }
        for inner in &mut self.inner {
            applied |= inner.provide_cloneable_value_internal(dep_id, value.clone());
        }
        applied
    }

    /// Returns true if there are any tests that require capturing, based on the given default setting
    /// and the per-test CaptureControl overrides.
    pub fn requires_capturing(&self, capture_by_default: bool) -> bool {
        self.tests.iter().any(|test| {
            test.props
                .capture_control
                .requires_capturing(capture_by_default)
        }) || self
            .inner
            .iter()
            .any(|inner| inner.requires_capturing(capture_by_default))
    }

    #[cfg(feature = "tokio")]
    pub async fn pick_next(&mut self) -> Option<TestExecution> {
        if self.is_empty() {
            None
        } else {
            match self
                .pick_next_internal(&self.create_dependency_map(&HashMap::new()))
                .await
            {
                Some((test, deps, seq_lock, in_progress_counter)) => {
                    let index = self.idx;
                    self.idx += 1;
                    Some(TestExecution {
                        test: test.clone(),
                        deps: Arc::new(deps),
                        index,
                        _seq_lock: seq_lock,
                        in_progress_counter,
                    })
                }
                None => None,
            }
        }
    }

    pub fn pick_next_sync(&mut self) -> Option<TestExecution> {
        match self.pick_next_internal_sync(&HashMap::new()) {
            Some((test, deps, seq_lock, in_progress_counter)) => {
                let index = self.idx;
                self.idx += 1;
                Some(TestExecution {
                    test: test.clone(),
                    deps: Arc::new(deps),
                    index,
                    _seq_lock: seq_lock,
                    in_progress_counter,
                })
            }
            None => None,
        }
    }

    #[cfg(feature = "tokio")]
    #[allow(clippy::type_complexity)]
    async fn pick_next_internal(
        &mut self,
        materialized_parent_deps: &HashMap<String, Arc<dyn Any + Send + Sync>>,
    ) -> Option<(
        RegisteredTest,
        HashMap<String, Arc<dyn Any + Send + Sync>>,
        SequentialExecutionLockGuard,
        Arc<AtomicUsize>,
    )> {
        if self.is_empty() {
            None
        } else {
            let dependency_map = if !self.is_materialized() {
                self.materialize_deps(materialized_parent_deps).await
            } else {
                self.create_dependency_map(materialized_parent_deps)
            };

            let locked = self.sequential_lock.is_locked().await;
            let result = if self.tests.is_empty() || locked {
                let current = self.inner.iter_mut();
                let mut result = None;
                for inner in current {
                    if let Some((test, deps, seq_lock, in_progress_counter)) =
                        Box::pin(inner.pick_next_internal(&dependency_map)).await
                    {
                        result = Some((test, deps, seq_lock, in_progress_counter));
                        break;
                    }
                }
                self.inner.retain(|inner| !inner.is_empty());

                result
            } else {
                let guard = self.sequential_lock.lock(self.is_sequential).await;
                self.in_progress.fetch_add(1, Ordering::Release);
                self.tests
                    .pop()
                    .map(|test| (test, dependency_map, guard, self.in_progress.clone()))
            };
            if result.is_none()
                && self.is_empty()
                && self.is_materialized()
                && !locked
                && self.in_progress.load(Ordering::Acquire) == 0
            {
                self.drop_deps();
            }
            if result.is_some() {
                self.remaining_count -= 1;
            }
            result
        }
    }

    #[allow(clippy::type_complexity)]
    fn pick_next_internal_sync(
        &mut self,
        materialized_parent_deps: &HashMap<String, Arc<dyn Any + Send + Sync>>,
    ) -> Option<(
        RegisteredTest,
        HashMap<String, Arc<dyn Any + Send + Sync>>,
        SequentialExecutionLockGuard,
        Arc<AtomicUsize>,
    )> {
        if self.is_empty() {
            None
        } else {
            let dependency_map = if !self.is_materialized() {
                self.materialize_deps_sync(materialized_parent_deps)
            } else {
                self.create_dependency_map(materialized_parent_deps)
            };

            let locked = self.sequential_lock.is_locked_sync();
            let result = if self.tests.is_empty() || locked {
                let current = self.inner.iter_mut();
                let mut result = None;
                for inner in current {
                    if let Some((test, deps, seq_lock, in_progress_counter)) =
                        inner.pick_next_internal_sync(&dependency_map)
                    {
                        result = Some((test, deps, seq_lock, in_progress_counter));
                        break;
                    }
                }

                self.inner.retain(|inner| !inner.is_empty());
                result
            } else {
                let guard = self.sequential_lock.lock_sync(self.is_sequential);
                self.in_progress.fetch_add(1, Ordering::Release);
                self.tests
                    .pop()
                    .map(|test| (test, dependency_map, guard, self.in_progress.clone()))
            };
            if result.is_none()
                && self.is_materialized()
                && !locked
                && self.in_progress.load(Ordering::Acquire) == 0
            {
                self.drop_deps();
            }
            if result.is_some() {
                self.remaining_count -= 1;
            }
            result
        }
    }

    fn create_dependency_map(
        &self,
        parent_map: &HashMap<String, Arc<dyn Any + Send + Sync>>,
    ) -> HashMap<String, Arc<dyn Any + Send + Sync>> {
        let mut result = parent_map.clone();
        for (key, dep) in &self.materialized_dependencies {
            result.insert(key.clone(), dep.clone());
        }
        result
    }

    fn root(
        deps: Vec<RegisteredDependency>,
        tests: Vec<RegisteredTest>,
        props: Vec<RegisteredTestSuiteProperty>,
    ) -> Self {
        let total_count = tests.len();
        let is_sequential = props
            .iter()
            .any(|prop| matches!(prop, RegisteredTestSuiteProperty::Sequential { .. }))
            || tests.iter().any(|test| test.run.is_bench());
        Self {
            crate_and_module: String::new(),
            dependencies: deps,
            tests,
            props,
            inner: Vec::new(),
            materialized_dependencies: HashMap::new(),
            remaining_count: total_count,
            idx: 0,
            sequential_lock: SequentialExecutionLock::new(),
            is_sequential,
            skip_creating_dependencies: false,
            in_progress: Arc::new(AtomicUsize::new(0)),
        }
    }

    fn add_dependency(&mut self, dep: RegisteredDependency) {
        let crate_and_module = dep.crate_and_module();
        if self.crate_and_module == crate_and_module {
            self.dependencies.push(dep);
        } else {
            let mut found = false;
            for inner in &mut self.inner {
                if Self::is_prefix_of(&inner.crate_and_module, &crate_and_module) {
                    inner.add_dependency(dep.clone());
                    found = true;
                    break;
                }
            }
            if !found {
                let mut inner = Self {
                    crate_and_module: Self::next_level(&self.crate_and_module, &crate_and_module),
                    dependencies: vec![],
                    tests: vec![],
                    inner: vec![],
                    props: vec![],
                    materialized_dependencies: HashMap::new(),
                    remaining_count: 0,
                    idx: 0,
                    is_sequential: false,
                    sequential_lock: SequentialExecutionLock::new(),
                    skip_creating_dependencies: false,
                    in_progress: Arc::new(AtomicUsize::new(0)),
                };
                inner.add_dependency(dep);
                self.inner.push(inner);
            }
        }
    }

    fn add_test(&mut self, test: RegisteredTest) {
        let crate_and_module = test.crate_and_module();
        if self.crate_and_module == crate_and_module {
            self.tests.push(test.clone());

            if test.run.is_bench() {
                self.is_sequential = true;
            }
        } else {
            let mut found = false;
            for inner in &mut self.inner {
                if Self::is_prefix_of(&inner.crate_and_module, &crate_and_module) {
                    inner.add_test(test.clone());
                    found = true;
                    break;
                }
            }
            if !found {
                let mut inner = Self {
                    crate_and_module: Self::next_level(&self.crate_and_module, &crate_and_module),
                    dependencies: vec![],
                    tests: vec![],
                    inner: vec![],
                    props: vec![],
                    materialized_dependencies: HashMap::new(),
                    remaining_count: 0,
                    idx: 0,
                    is_sequential: false,
                    sequential_lock: SequentialExecutionLock::new(),
                    skip_creating_dependencies: false,
                    in_progress: Arc::new(AtomicUsize::new(0)),
                };
                inner.add_test(test);
                self.inner.push(inner);
            }
        }
        self.remaining_count += 1;
    }

    fn add_prop(&mut self, prop: RegisteredTestSuiteProperty) {
        let crate_and_module = prop.crate_and_module();
        if self.crate_and_module == crate_and_module {
            if matches!(prop, RegisteredTestSuiteProperty::Sequential { .. }) {
                self.is_sequential = true;
            }
            self.props.push(prop);
        } else {
            let mut found = false;
            for inner in &mut self.inner {
                if Self::is_prefix_of(&inner.crate_and_module, &crate_and_module) {
                    inner.add_prop(prop.clone());
                    found = true;
                    break;
                }
            }
            if !found {
                let mut inner = Self {
                    crate_and_module: Self::next_level(&self.crate_and_module, &crate_and_module),
                    dependencies: vec![],
                    tests: vec![],
                    inner: vec![],
                    props: vec![],
                    materialized_dependencies: HashMap::new(),
                    remaining_count: 0,
                    idx: 0,
                    is_sequential: false,
                    sequential_lock: SequentialExecutionLock::new(),
                    skip_creating_dependencies: false,
                    in_progress: Arc::new(AtomicUsize::new(0)),
                };
                inner.add_prop(prop);
                self.inner.push(inner);
            }
        }
    }

    fn is_materialized(&self) -> bool {
        self.skip_creating_dependencies
            || self.materialized_dependencies.len() == self.dependencies.len()
    }

    #[cfg(feature = "tokio")]
    async fn materialize_deps(
        &mut self,
        parent_map: &HashMap<String, Arc<dyn Any + Send + Sync>>,
    ) -> HashMap<String, Arc<dyn Any + Send + Sync>> {
        // Start with any pre-populated values (e.g. Cloneable deps received
        // from the parent via ProvideCloneable IPC).
        let mut deps = self.materialized_dependencies.clone();
        let mut dependency_map = parent_map.clone();
        for (k, v) in &deps {
            dependency_map.insert(k.clone(), v.clone());
        }

        let sorted_dependencies = self.sorted_dependencies();
        for dep in &sorted_dependencies {
            if deps.contains_key(&dep.name) {
                continue;
            }
            let materialized_dep = match &dep.constructor {
                DependencyConstructor::Sync(cons) => cons(Arc::new(dependency_map.clone())),
                DependencyConstructor::Async(cons) => cons(Arc::new(dependency_map.clone())).await,
            };
            deps.insert(dep.name.clone(), materialized_dep.clone());
            dependency_map.insert(dep.name.clone(), materialized_dep);
        }
        self.materialized_dependencies = deps;
        dependency_map
    }

    fn materialize_deps_sync(
        &mut self,
        parent_map: &HashMap<String, Arc<dyn Any + Send + Sync>>,
    ) -> HashMap<String, Arc<dyn Any + Send + Sync>> {
        // Start with any pre-populated values (e.g. Cloneable deps received
        // from the parent via ProvideCloneable IPC).
        let mut deps = self.materialized_dependencies.clone();
        let mut dependency_map = parent_map.clone();
        for (k, v) in &deps {
            dependency_map.insert(k.clone(), v.clone());
        }

        let sorted_dependencies = self.sorted_dependencies();
        for dep in &sorted_dependencies {
            if deps.contains_key(&dep.name) {
                continue;
            }
            let materialized_dep = match &dep.constructor {
                DependencyConstructor::Sync(cons) => cons(Arc::new(dependency_map.clone())),
                DependencyConstructor::Async(cons) => {
                    futures::executor::block_on(cons(Arc::new(dependency_map.clone())))
                }
            };
            deps.insert(dep.name.clone(), materialized_dep.clone());
            dependency_map.insert(dep.name.clone(), materialized_dep);
        }
        self.materialized_dependencies = deps;
        dependency_map
    }

    fn sorted_dependencies(&self) -> Vec<&RegisteredDependency> {
        let mut ts: TopologicalSort<&RegisteredDependency> = TopologicalSort::new();
        for dep in &self.dependencies {
            let mut added = false;
            for dep_dep_name in &dep.dependencies {
                if let Some(dep_dep) = self.dependencies.iter().find(|d| &d.name == dep_dep_name) {
                    ts.add_dependency(dep_dep, dep);
                    added = true;
                } else {
                    // otherwise it is expected to come from the parent level
                }
            }
            if !added {
                ts.insert(dep);
            }
        }
        let mut result = Vec::with_capacity(self.dependencies.len());
        loop {
            let chunk = ts.pop_all();
            if chunk.is_empty() {
                break;
            }
            result.extend(chunk);
        }
        result
    }

    fn drop_deps(&mut self) {
        self.materialized_dependencies.clear();
    }

    /// Prunes dependencies that are not needed by any test in this subtree.
    /// Returns `Some(needed_from_parent)` with dep names needed from ancestor levels,
    /// or `None` if pruning is disabled for this subtree (unknown deps).
    fn prune_unused_deps(&mut self) -> Option<HashSet<String>> {
        // Collect dep names needed by tests at this level
        let mut needed: Option<HashSet<String>> = Some(HashSet::new());
        for test in &self.tests {
            match &test.dependencies {
                None => {
                    needed = None;
                    break;
                }
                Some(deps) => {
                    if let Some(ref mut set) = needed {
                        set.extend(deps.iter().cloned());
                    }
                }
            }
        }

        // Merge children's needs
        for inner in &mut self.inner {
            let child_needs = inner.prune_unused_deps();
            needed = match (needed, child_needs) {
                (None, _) | (_, None) => None,
                (Some(mut a), Some(b)) => {
                    a.extend(b);
                    Some(a)
                }
            };
        }

        // If any test has unknown deps, keep everything
        let needed = needed?;

        // Determine which local deps to keep
        let local_names: HashSet<String> =
            self.dependencies.iter().map(|d| d.name.clone()).collect();
        let mut keep_local: HashSet<String> = needed.intersection(&local_names).cloned().collect();

        // Expand transitive closure for local deps only (fixpoint)
        let mut queue: VecDeque<String> = keep_local.iter().cloned().collect();
        let mut needed_from_parent: HashSet<String> =
            needed.difference(&local_names).cloned().collect();

        while let Some(dep_name) = queue.pop_front() {
            if let Some(dep) = self.dependencies.iter().find(|d| d.name == dep_name) {
                for transitive in &dep.dependencies {
                    if local_names.contains(transitive) {
                        if keep_local.insert(transitive.clone()) {
                            queue.push_back(transitive.clone());
                        }
                    } else {
                        needed_from_parent.insert(transitive.clone());
                    }
                }
                // Companions are planner-only sibling links — no
                // constructor argument is derived from them, but they
                // must be retained together with the dep they are
                // declared on. Used by the
                // `#[test_dep(scope = Hosted, worker = both(T))]`
                // lowering to keep the Hosted owner half and the
                // HostedRpc stub half as a pair even when the
                // selected tests only parameterise on one of them.
                for companion in &dep.companions {
                    if local_names.contains(companion) {
                        if keep_local.insert(companion.clone()) {
                            queue.push_back(companion.clone());
                        }
                    } else {
                        needed_from_parent.insert(companion.clone());
                    }
                }
            }
        }

        // Prune
        self.dependencies.retain(|d| keep_local.contains(&d.name));

        Some(needed_from_parent)
    }

    fn is_prefix_of(this: &str, that: &str) -> bool {
        this.is_empty() || this == that || that.starts_with(&format!("{this}::"))
    }

    fn next_level(from: &str, to: &str) -> String {
        assert!(Self::is_prefix_of(from, to));
        let remaining = if from.is_empty() {
            to
        } else {
            &to[from.len() + 2..]
        };

        let result = if let Some((next, _tail)) = remaining.split_once("::") {
            format!("{from}::{next}")
        } else {
            format!("{from}::{remaining}")
        };
        result.trim_start_matches("::").to_string()
    }

    fn propagate_sequential(&mut self, inherited_lock: Option<&SequentialExecutionLock>) {
        if let Some(parent_lock) = inherited_lock {
            self.is_sequential = true;
            self.sequential_lock = parent_lock.clone();
        }

        let lock_for_children = if self.is_sequential {
            Some(self.sequential_lock.clone())
        } else {
            None
        };

        for child in &mut self.inner {
            child.propagate_sequential(lock_for_children.as_ref());
        }
    }
}

impl Debug for TestSuiteExecution {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        writeln!(
            f,
            "'{}' {} [{}]",
            self.crate_and_module,
            self.props
                .iter()
                .map(|x| format!("{x:?}"))
                .collect::<Vec<_>>()
                .join(", "),
            if self.is_sequential { "S" } else { "P" }
        )?;
        writeln!(f, "  deps:")?;
        for dep in &self.dependencies {
            writeln!(f, "    '{}'", dep.name)?;
        }
        writeln!(f, "  tests:")?;
        for test in &self.tests {
            writeln!(f, "    '{}' [{:?}]", test.name, test.props.test_type)?;
        }
        for inner in &self.inner {
            let inner_str = format!("{inner:?}");
            for inner_line in inner_str.lines() {
                writeln!(f, "  {inner_line}")?;
            }
        }
        Ok(())
    }
}

impl DependencyView for HashMap<String, Arc<dyn Any + Send + Sync>> {
    fn get(&self, name: &str) -> Option<Arc<dyn Any + Send + Sync>> {
        self.get(name).cloned()
    }
}

pub struct TestExecution {
    pub test: RegisteredTest,
    pub deps: Arc<dyn DependencyView + Send + Sync>,
    pub index: usize,
    _seq_lock: SequentialExecutionLockGuard,
    in_progress_counter: Arc<AtomicUsize>,
}

impl Drop for TestExecution {
    fn drop(&mut self) {
        self.in_progress_counter.fetch_sub(1, Ordering::Release);
    }
}

#[allow(dead_code)]
enum SequentialExecutionLockGuard {
    None,
    #[cfg(feature = "tokio")]
    Async(tokio::sync::OwnedMutexGuard<()>),
    Sync(parking_lot::ArcMutexGuard<parking_lot::RawMutex, ()>),
}

#[derive(Clone)]
struct SequentialExecutionLock {
    #[cfg(feature = "tokio")]
    async_mutex: Arc<tokio::sync::Mutex<()>>,
    sync_mutex: Arc<parking_lot::Mutex<()>>,
}

impl SequentialExecutionLock {
    pub fn new() -> Self {
        Self {
            #[cfg(feature = "tokio")]
            async_mutex: Arc::new(tokio::sync::Mutex::new(())),
            sync_mutex: Arc::new(parking_lot::Mutex::new(())),
        }
    }

    #[cfg(feature = "tokio")]
    pub async fn is_locked(&self) -> bool {
        self.async_mutex.try_lock().is_err()
    }

    pub fn is_locked_sync(&self) -> bool {
        self.sync_mutex.try_lock().is_none()
    }

    #[cfg(feature = "tokio")]
    pub async fn lock(&self, is_sequential: bool) -> SequentialExecutionLockGuard {
        if is_sequential {
            let permit = tokio::sync::Mutex::lock_owned(self.async_mutex.clone()).await;
            SequentialExecutionLockGuard::Async(permit)
        } else {
            SequentialExecutionLockGuard::None
        }
    }

    pub fn lock_sync(&self, is_sequential: bool) -> SequentialExecutionLockGuard {
        if is_sequential {
            let permit = parking_lot::Mutex::lock_arc(&self.sync_mutex);
            SequentialExecutionLockGuard::Sync(permit)
        } else {
            SequentialExecutionLockGuard::None
        }
    }
}

#[cfg(test)]
mod cloneable_tests {
    use super::*;
    use crate::internal::{
        CloneableCodec, DependencyConstructor, RegisteredDependency, RegisteredTest, TestFunction,
        TestProperties,
    };
    use std::sync::atomic::{AtomicUsize, Ordering};

    fn registered_test(name: &str, deps: Vec<String>) -> RegisteredTest {
        registered_test_in_module(name, "", deps)
    }

    fn registered_test_in_module(
        name: &str,
        module_path: &str,
        deps: Vec<String>,
    ) -> RegisteredTest {
        RegisteredTest {
            name: name.to_string(),
            crate_name: "tcrate".to_string(),
            module_path: module_path.to_string(),
            run: TestFunction::Sync(Arc::new(|_| Box::new(()))),
            props: TestProperties::default(),
            dependencies: Some(deps),
        }
    }

    /// A Cloneable dep whose constructor increments a counter (so we can
    /// assert it ran exactly once), encodes via simple little-endian bytes.
    fn registered_cloneable_dep(name: &str, counter: Arc<AtomicUsize>) -> RegisteredDependency {
        registered_cloneable_dep_in(name, "", 0xdead_beef, counter)
    }

    /// Like [`registered_cloneable_dep`] but lets the caller pick the
    /// dep's module path and the constant the constructor emits — so a
    /// collision test can assert two same-named deps in different modules
    /// don't get crossed up.
    fn registered_cloneable_dep_in(
        name: &str,
        module_path: &str,
        constructor_value: u64,
        counter: Arc<AtomicUsize>,
    ) -> RegisteredDependency {
        let constructor_counter = counter.clone();
        let constructor = DependencyConstructor::Sync(Arc::new(move |_view| {
            constructor_counter.fetch_add(1, Ordering::SeqCst);
            Arc::new(constructor_value) as Arc<dyn Any + Send + Sync>
        }));
        let codec = CloneableCodec {
            to_wire: Arc::new(|any: Arc<dyn Any + Send + Sync>| {
                let value: Arc<u64> = any.downcast::<u64>().unwrap();
                (*value).to_le_bytes().to_vec()
            }),
            from_wire_bytes: Arc::new(|bytes: &[u8]| {
                let arr: [u8; 8] = bytes.try_into().unwrap();
                let value = u64::from_le_bytes(arr);
                Arc::new(value) as Arc<dyn Any + Send + Sync>
            }),
        };
        RegisteredDependency {
            name: name.to_string(),
            crate_name: "tcrate".to_string(),
            module_path: module_path.to_string(),
            constructor,
            dependencies: Vec::new(),
            scope: DepScope::Cloneable,
            worker_fn: Some(crate::internal::WorkerReconstructor::Sync(Arc::new(
                |wire_payload, _deps| wire_payload,
            ))),
            cloneable_codec: Some(codec),
            hosted_codec: None,
            rpc_factory: None,
            companions: Vec::new(),
        }
    }

    #[test]
    fn cloneable_wire_collection_runs_constructor_once_and_encodes_value() {
        let counter = Arc::new(AtomicUsize::new(0));
        let dep = registered_cloneable_dep("clone_dep", counter.clone());
        let test = registered_test("t1", vec!["clone_dep".to_string()]);

        let (execution, _filtered) =
            TestSuiteExecution::construct(&Arguments::default(), &[dep], &[test], &[]);

        let collected = execution.collect_cloneable_wire_bytes_sync();
        assert_eq!(collected.len(), 1, "exactly one cloneable dep expected");
        let (dep_id, wire_bytes) = &collected[0];
        assert_eq!(
            dep_id, "tcrate::clone_dep",
            "wire bytes must be keyed by the fully-qualified id, not the local name"
        );
        assert_eq!(
            wire_bytes.as_slice(),
            &0xdead_beef_u64.to_le_bytes(),
            "expected the codec-encoded value to round-trip via to_wire"
        );
        assert_eq!(
            counter.load(Ordering::SeqCst),
            1,
            "constructor must have run exactly once when collecting"
        );
    }

    #[test]
    fn prune_unused_deps_retains_companion_when_only_one_half_is_referenced() {
        // Regression for `#[test_dep(scope = Hosted, worker = both(T))]`
        // pruning. That lowering registers two dep entries (Hosted owner
        // view + HostedRpc stub view) for a single logical dep, backed by
        // a shared `Arc<HostedBothShared>` cache; the macro now declares
        // the two halves as `companions` of each other so that the pruner
        // retains the Hosted half even when selected tests only reference
        // the stub half. Without that, the async-ctor flavour would panic
        // (`Poll::Pending`) at runtime because the shared cache stays
        // empty.
        //
        // This test reproduces the pruner-level invariant cheaply via two
        // Cloneable deps. The `keep_local` traversal in
        // `prune_unused_deps` should expand the keep-set across
        // companions in either direction.

        // Case A: reference only `dep_a`; `dep_b` is declared as a
        // companion of `dep_a` and must survive pruning.
        let counter_a = Arc::new(AtomicUsize::new(0));
        let counter_b = Arc::new(AtomicUsize::new(0));
        let mut dep_a = registered_cloneable_dep("clone_a", counter_a.clone());
        let mut dep_b = registered_cloneable_dep("clone_b", counter_b.clone());
        dep_a.companions = vec!["clone_b".to_string()];
        dep_b.companions = vec!["clone_a".to_string()];

        let test_a = registered_test("t_uses_a", vec!["clone_a".to_string()]);

        let (execution, _filtered) =
            TestSuiteExecution::construct(&Arguments::default(), &[dep_a, dep_b], &[test_a], &[]);

        let kept: Vec<String> = execution
            .collect_cloneable_dependencies()
            .into_iter()
            .map(|d| d.name)
            .collect();
        assert!(
            kept.contains(&"clone_a".to_string()),
            "directly referenced dep must be retained, kept = {kept:?}"
        );
        assert!(
            kept.contains(&"clone_b".to_string()),
            "companion of a retained dep must also be retained (the planner-only \
             sibling link used by `worker = both(...)`), kept = {kept:?}"
        );

        // Case B (reverse direction): reference only `dep_b`, with the
        // same companion link. `dep_a` must survive pruning.
        let counter_a = Arc::new(AtomicUsize::new(0));
        let counter_b = Arc::new(AtomicUsize::new(0));
        let mut dep_a = registered_cloneable_dep("clone_a", counter_a.clone());
        let mut dep_b = registered_cloneable_dep("clone_b", counter_b.clone());
        dep_a.companions = vec!["clone_b".to_string()];
        dep_b.companions = vec!["clone_a".to_string()];

        let test_b = registered_test("t_uses_b", vec!["clone_b".to_string()]);

        let (execution, _filtered) =
            TestSuiteExecution::construct(&Arguments::default(), &[dep_a, dep_b], &[test_b], &[]);

        let kept: Vec<String> = execution
            .collect_cloneable_dependencies()
            .into_iter()
            .map(|d| d.name)
            .collect();
        assert!(
            kept.contains(&"clone_a".to_string()),
            "companion of a stub-referenced dep must be retained, kept = {kept:?}"
        );
        assert!(
            kept.contains(&"clone_b".to_string()),
            "directly referenced dep must be retained, kept = {kept:?}"
        );

        // Sanity: a dep with no companion link and not referenced
        // anywhere is still pruned. (Prevents the test above from
        // accidentally turning into "the pruner never drops anything".)
        let counter_a = Arc::new(AtomicUsize::new(0));
        let counter_b = Arc::new(AtomicUsize::new(0));
        let dep_a = registered_cloneable_dep("clone_a", counter_a.clone());
        let dep_b = registered_cloneable_dep("clone_b", counter_b.clone());
        let test_a = registered_test("t_uses_a", vec!["clone_a".to_string()]);

        let (execution, _filtered) =
            TestSuiteExecution::construct(&Arguments::default(), &[dep_a, dep_b], &[test_a], &[]);

        let kept: Vec<String> = execution
            .collect_cloneable_dependencies()
            .into_iter()
            .map(|d| d.name)
            .collect();
        assert!(
            kept.contains(&"clone_a".to_string()),
            "directly referenced dep must be retained, kept = {kept:?}"
        );
        assert!(
            !kept.contains(&"clone_b".to_string()),
            "without a companion link, an unreferenced dep must be pruned; \
             kept = {kept:?}"
        );
    }

    #[test]
    fn provide_cloneable_value_short_circuits_constructor() {
        let counter = Arc::new(AtomicUsize::new(0));
        let dep = registered_cloneable_dep("clone_dep", counter.clone());
        let test = registered_test("t1", vec!["clone_dep".to_string()]);

        let (mut execution, _filtered) =
            TestSuiteExecution::construct(&Arguments::default(), &[dep], &[test], &[]);

        let pre_value: Arc<dyn Any + Send + Sync> = Arc::new(99_u64);
        let applied = execution.provide_cloneable_value("tcrate::clone_dep", pre_value);
        assert!(
            applied,
            "pre-populated value should match the dep's qualified id"
        );

        // Pick the test — materialize_deps_sync must reuse the pre-populated
        // value instead of running the original constructor.
        let next = execution.pick_next_sync().expect("test should be picked");
        assert_eq!(next.test.name, "t1");

        let view = next.deps.get("clone_dep").expect("dep available");
        let value: Arc<u64> = view.downcast::<u64>().unwrap();
        assert_eq!(*value, 99);

        assert_eq!(
            counter.load(Ordering::SeqCst),
            0,
            "constructor must not run when a pre-populated value is supplied"
        );
    }

    #[test]
    fn provided_shared_value_is_a_worker_side_leaf() {
        let provided_counter = Arc::new(AtomicUsize::new(0));
        let parent_only_counter = Arc::new(AtomicUsize::new(0));
        let mut provided_dep = registered_cloneable_dep("clone_dep", provided_counter.clone());
        provided_dep.dependencies = vec!["parent_only_dep".to_string()];
        let parent_only_dep =
            registered_cloneable_dep("parent_only_dep", parent_only_counter.clone());
        let test = registered_test("t1", vec!["clone_dep".to_string()]);

        let (mut execution, _filtered) = TestSuiteExecution::construct(
            &Arguments::default(),
            &[provided_dep, parent_only_dep],
            &[test],
            &[],
        );

        let pre_value: Arc<dyn Any + Send + Sync> = Arc::new(99_u64);
        let applied = execution.provide_cloneable_value("tcrate::clone_dep", pre_value);
        assert!(applied);

        let next = execution.pick_next_sync().expect("test should be picked");
        let view = next.deps.get("clone_dep").expect("dep available");
        let value: Arc<u64> = view.downcast::<u64>().unwrap();
        assert_eq!(*value, 99);
        assert_eq!(
            provided_counter.load(Ordering::SeqCst),
            0,
            "worker-side provided values must not run their original constructor"
        );
        assert_eq!(
            parent_only_counter.load(Ordering::SeqCst),
            0,
            "constructor dependencies are parent-only once a value arrives from wire bytes or an RPC stub"
        );
    }

    /// Async-constructor counterpart for the parent-side collector used by the
    /// tokio runner. Verifies that a Cloneable owner declared with
    /// `async fn` is awaited on the parent and its wire bytes are produced
    /// keyed by the dep's qualified id.
    #[cfg(feature = "tokio")]
    #[test]
    fn async_cloneable_wire_collection_awaits_async_constructor() {
        use std::pin::Pin;

        let counter = Arc::new(AtomicUsize::new(0));
        let constructor_counter = counter.clone();

        // Build a RegisteredDependency with an Async constructor that
        // genuinely awaits a future (tokio::task::yield_now) on the parent
        // side, then returns a u64.
        let constructor = DependencyConstructor::Async(Arc::new(move |_view| {
            let counter = constructor_counter.clone();
            Box::pin(async move {
                tokio::task::yield_now().await;
                counter.fetch_add(1, Ordering::SeqCst);
                let value: u64 = 0xdead_beef;
                Arc::new(value) as Arc<dyn Any + Send + Sync>
            }) as Pin<Box<dyn std::future::Future<Output = Arc<dyn Any + Send + Sync>>>>
        }));
        let codec = CloneableCodec {
            to_wire: Arc::new(|any| {
                let v: Arc<u64> = any.downcast::<u64>().unwrap();
                (*v).to_le_bytes().to_vec()
            }),
            from_wire_bytes: Arc::new(|bytes| {
                let arr: [u8; 8] = bytes.try_into().unwrap();
                Arc::new(u64::from_le_bytes(arr)) as Arc<dyn Any + Send + Sync>
            }),
        };
        let dep = RegisteredDependency {
            name: "clone_dep".to_string(),
            crate_name: "tcrate".to_string(),
            module_path: String::new(),
            constructor,
            dependencies: Vec::new(),
            scope: DepScope::Cloneable,
            worker_fn: Some(crate::internal::WorkerReconstructor::Sync(Arc::new(
                |wire_payload, _| wire_payload,
            ))),
            cloneable_codec: Some(codec),
            hosted_codec: None,
            rpc_factory: None,
            companions: Vec::new(),
        };
        let test = registered_test("t1", vec!["clone_dep".to_string()]);

        let (execution, _filtered) =
            TestSuiteExecution::construct(&Arguments::default(), &[dep], &[test], &[]);

        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap();
        let collected = runtime.block_on(execution.collect_cloneable_wire_bytes_async());

        assert_eq!(collected.len(), 1);
        assert_eq!(collected[0].0, "tcrate::clone_dep");
        assert_eq!(collected[0].1.as_slice(), &0xdead_beef_u64.to_le_bytes());
        assert_eq!(
            counter.load(Ordering::SeqCst),
            1,
            "async constructor must have run exactly once"
        );
    }

    /// Regression test: two cloneable deps that share a local `name` but live
    /// in different modules must not collide on the wire and must not
    /// cross-apply on the worker side.
    #[test]
    fn cloneable_value_routing_uses_qualified_id_across_modules() {
        let counter_a = Arc::new(AtomicUsize::new(0));
        let counter_b = Arc::new(AtomicUsize::new(0));

        // Same local name `clone_dep`, different module paths.
        let dep_a = registered_cloneable_dep_in("clone_dep", "mod_a", 11, counter_a.clone());
        let dep_b = registered_cloneable_dep_in("clone_dep", "mod_b", 22, counter_b.clone());

        // One test per module, each takes "clone_dep" from its own module.
        let test_a = registered_test_in_module("t_a", "mod_a", vec!["clone_dep".to_string()]);
        let test_b = registered_test_in_module("t_b", "mod_b", vec!["clone_dep".to_string()]);

        let (execution, _filtered) = TestSuiteExecution::construct(
            &Arguments::default(),
            &[dep_a, dep_b],
            &[test_a, test_b],
            &[],
        );

        // The wire bytes must carry distinct qualified ids and distinct payloads.
        let mut collected = execution.collect_cloneable_wire_bytes_sync();
        collected.sort_by(|l, r| l.0.cmp(&r.0));
        assert_eq!(collected.len(), 2);
        assert_eq!(collected[0].0, "tcrate::mod_a::clone_dep");
        assert_eq!(collected[1].0, "tcrate::mod_b::clone_dep");
        assert_eq!(collected[0].1.as_slice(), &11_u64.to_le_bytes());
        assert_eq!(collected[1].1.as_slice(), &22_u64.to_le_bytes());

        // Worker-side routing: pre-populating with `mod_a`'s qualified id must
        // apply only to `mod_a`'s dep, and similarly for `mod_b`. If the
        // routing fell back to plain `name`, both nodes would be updated.
        let mut execution_a = execution;
        let applied_a =
            execution_a.provide_cloneable_value("tcrate::mod_a::clone_dep", Arc::new(111_u64));
        assert!(applied_a, "mod_a dep must be reachable by qualified id");
        let applied_b =
            execution_a.provide_cloneable_value("tcrate::mod_b::clone_dep", Arc::new(222_u64));
        assert!(applied_b, "mod_b dep must be reachable by qualified id");

        // An unrelated qualified id must not apply to anything.
        let applied_unknown =
            execution_a.provide_cloneable_value("tcrate::mod_c::clone_dep", Arc::new(333_u64));
        assert!(
            !applied_unknown,
            "unknown qualified id must not be applied anywhere"
        );

        // Pick both tests and confirm the per-module values stayed separate.
        let first = execution_a.pick_next_sync().expect("first test");
        let second = execution_a.pick_next_sync().expect("second test");

        let pairs: Vec<(String, u64)> = [first, second]
            .into_iter()
            .map(|n| {
                let v: Arc<u64> = n
                    .deps
                    .get("clone_dep")
                    .expect("dep available")
                    .clone()
                    .downcast()
                    .unwrap();
                (n.test.name.clone(), *v)
            })
            .collect();

        let val_a = pairs
            .iter()
            .find(|(n, _)| n == "t_a")
            .expect("t_a picked")
            .1;
        let val_b = pairs
            .iter()
            .find(|(n, _)| n == "t_b")
            .expect("t_b picked")
            .1;
        assert_eq!(
            val_a, 111,
            "mod_a test must see mod_a's pre-populated value"
        );
        assert_eq!(
            val_b, 222,
            "mod_b test must see mod_b's pre-populated value"
        );

        // Each per-module constructor ran exactly once — during the parent-side
        // wire-bytes collection above. The worker-side `provide_cloneable_value`
        // calls must NOT have triggered the constructor a second time on either
        // node (otherwise the qualified-id routing is wrong / it cross-applied).
        assert_eq!(
            counter_a.load(Ordering::SeqCst),
            1,
            "mod_a constructor must have run exactly once (during wire collection)"
        );
        assert_eq!(
            counter_b.load(Ordering::SeqCst),
            1,
            "mod_b constructor must have run exactly once (during wire collection)"
        );
    }

    // -------- Hosted dep tests --------

    /// Builds a Hosted RegisteredDependency for tests. Owner value is a u64
    /// (`payload`). `descriptor()` is modelled by the codec's `to_wire` as
    /// the LE bytes of `payload`, and `from_descriptor` is modelled by the
    /// worker_fn which downcasts the bytes and rebuilds a u64 — so we can
    /// observe both halves of the Hosted round-trip without depending on
    /// the user-facing `HostedDep` trait inside this private test helper.
    fn registered_hosted_dep(
        name: &str,
        payload: u64,
        owner_counter: Arc<AtomicUsize>,
    ) -> RegisteredDependency {
        registered_hosted_dep_in(name, "", payload, owner_counter)
    }

    /// Like [`registered_hosted_dep`] but lets the caller pick the dep's
    /// module path — so a collision test can assert two same-named Hosted
    /// deps in different modules don't get crossed up on the wire / in the
    /// worker routing.
    fn registered_hosted_dep_in(
        name: &str,
        module_path: &str,
        payload: u64,
        owner_counter: Arc<AtomicUsize>,
    ) -> RegisteredDependency {
        let constructor = DependencyConstructor::Sync(Arc::new(move |_view| {
            owner_counter.fetch_add(1, Ordering::SeqCst);
            Arc::new(payload) as Arc<dyn Any + Send + Sync>
        }));
        let codec = CloneableCodec {
            // descriptor() on the owner: encode the payload as LE bytes
            to_wire: Arc::new(|any: Arc<dyn Any + Send + Sync>| {
                let v: Arc<u64> = any.downcast::<u64>().unwrap();
                (*v).to_le_bytes().to_vec()
            }),
            // worker side: box the bytes as Any (worker_fn does
            // from_descriptor on them)
            from_wire_bytes: Arc::new(|bytes: &[u8]| {
                let boxed: Vec<u8> = bytes.to_vec();
                Arc::new(boxed) as Arc<dyn Any + Send + Sync>
            }),
        };
        let worker_fn =
            crate::internal::WorkerReconstructor::Sync(Arc::new(|wire_payload, _deps| {
                let bytes_arc: Arc<Vec<u8>> = wire_payload.downcast::<Vec<u8>>().unwrap();
                let arr: [u8; 8] = (*bytes_arc).as_slice().try_into().unwrap();
                let value: u64 = u64::from_le_bytes(arr);
                Arc::new(value) as Arc<dyn Any + Send + Sync>
            }));
        RegisteredDependency {
            name: name.to_string(),
            crate_name: "tcrate".to_string(),
            module_path: module_path.to_string(),
            constructor,
            dependencies: Vec::new(),
            scope: DepScope::Hosted,
            worker_fn: Some(worker_fn),
            cloneable_codec: None,
            hosted_codec: Some(codec),
            rpc_factory: None,
            companions: Vec::new(),
        }
    }

    #[test]
    fn hosted_descriptor_collection_runs_owner_once_and_keeps_it_alive() {
        let owner_counter = Arc::new(AtomicUsize::new(0));
        let dep = registered_hosted_dep("hosted_dep", 0xcafe_babe_dead_beef, owner_counter.clone());
        let test = registered_test("t1", vec!["hosted_dep".to_string()]);

        let (execution, _filtered) =
            TestSuiteExecution::construct(&Arguments::default(), &[dep], &[test], &[]);

        let (descriptors, owners) = execution.collect_hosted_descriptor_bytes_sync();
        assert_eq!(descriptors.len(), 1, "exactly one hosted dep expected");
        assert_eq!(owners.len(), 1, "exactly one hosted owner kept alive");

        let (dep_id, descriptor_bytes) = &descriptors[0];
        assert_eq!(
            dep_id, "tcrate::hosted_dep",
            "descriptor must be keyed by the fully-qualified id"
        );
        assert_eq!(
            descriptor_bytes.as_slice(),
            &0xcafe_babe_dead_beef_u64.to_le_bytes(),
            "expected descriptor bytes to match codec.to_wire of payload"
        );
        assert_eq!(
            owner_counter.load(Ordering::SeqCst),
            1,
            "owner constructor must have run exactly once"
        );

        // The returned owner Arc<dyn Any> must wrap the same payload value
        // — i.e. the parent really is holding the owner alive.
        let held: Arc<u64> = owners[0].clone().downcast::<u64>().unwrap();
        assert_eq!(*held, 0xcafe_babe_dead_beef);
    }

    #[test]
    fn hosted_descriptor_roundtrips_to_worker_value_via_provide_cloneable_value() {
        let owner_counter = Arc::new(AtomicUsize::new(0));
        let dep = registered_hosted_dep("hosted_dep", 0x1234_5678_u64, owner_counter.clone());
        let test = registered_test("t1", vec!["hosted_dep".to_string()]);

        let (mut execution, _filtered) =
            TestSuiteExecution::construct(&Arguments::default(), &[dep], &[test], &[]);

        // Worker-side simulation: pre-populate a reconstructed value (this
        // is what `apply_provided_wire_bytes` does after running the worker_fn
        // against the descriptor bytes).
        let pre_value: Arc<dyn Any + Send + Sync> = Arc::new(0x1234_5678_u64);
        let applied = execution.provide_cloneable_value("tcrate::hosted_dep", pre_value);
        assert!(
            applied,
            "Hosted dep must accept pre-populated values via the same path as Cloneable"
        );

        // Pick the test — the owner constructor must NOT run on the worker
        // side (we provided a value directly).
        let next = execution.pick_next_sync().expect("test should be picked");
        let view = next.deps.get("hosted_dep").expect("dep available");
        let value: Arc<u64> = view.downcast::<u64>().unwrap();
        assert_eq!(*value, 0x1234_5678);
        assert_eq!(
            owner_counter.load(Ordering::SeqCst),
            0,
            "Hosted owner constructor must not run on the worker side"
        );
    }

    #[test]
    fn has_hosted_dependencies_reports_correctly() {
        let dep = registered_hosted_dep("h", 0, Arc::new(AtomicUsize::new(0)));
        let test = registered_test("t1", vec!["h".to_string()]);
        let (execution, _filtered) =
            TestSuiteExecution::construct(&Arguments::default(), &[dep], &[test], &[]);
        assert!(execution.has_hosted_dependencies());
        assert!(!execution.has_shared_dependencies());
        assert!(!execution.has_cloneable_dependencies());
    }

    /// The owner constructor must run EXACTLY once even with multiple workers —
    /// descriptors are computed once on the parent and shipped to each worker.
    #[test]
    fn hosted_owner_runs_exactly_once_even_when_collecting_multiple_times() {
        // The collector is what the parent calls once; we verify the
        // expected invariant: a single collect call invokes the owner once
        // (even if multiple Hosted deps share the same dep id structure).
        let counter_a = Arc::new(AtomicUsize::new(0));
        let counter_b = Arc::new(AtomicUsize::new(0));

        // Two distinct Hosted deps in the same module/crate.
        let mut dep_a = registered_hosted_dep("hosted_a", 1, counter_a.clone());
        dep_a.name = "hosted_a".to_string();
        let mut dep_b = registered_hosted_dep("hosted_b", 2, counter_b.clone());
        dep_b.name = "hosted_b".to_string();
        let test = registered_test("t1", vec!["hosted_a".to_string(), "hosted_b".to_string()]);

        let (execution, _filtered) =
            TestSuiteExecution::construct(&Arguments::default(), &[dep_a, dep_b], &[test], &[]);

        let (descriptors, owners) = execution.collect_hosted_descriptor_bytes_sync();
        assert_eq!(descriptors.len(), 2);
        assert_eq!(owners.len(), 2);
        assert_eq!(counter_a.load(Ordering::SeqCst), 1);
        assert_eq!(counter_b.load(Ordering::SeqCst), 1);
    }

    /// Regression test for qualified-id routing on Hosted deps: two Hosted
    /// deps that share a local `name` but live in different modules must
    /// not collide on the wire and must not cross-apply on the worker side.
    /// This mirrors `cloneable_value_routing_uses_qualified_id_across_modules`
    /// to make sure the same hardened routing applies to descriptor bytes.
    #[test]
    fn hosted_descriptor_routing_uses_qualified_id_across_modules() {
        let counter_a = Arc::new(AtomicUsize::new(0));
        let counter_b = Arc::new(AtomicUsize::new(0));

        // Same local name `hosted_dep`, different module paths.
        let dep_a = registered_hosted_dep_in("hosted_dep", "mod_a", 11, counter_a.clone());
        let dep_b = registered_hosted_dep_in("hosted_dep", "mod_b", 22, counter_b.clone());

        let test_a = registered_test_in_module("t_a", "mod_a", vec!["hosted_dep".to_string()]);
        let test_b = registered_test_in_module("t_b", "mod_b", vec!["hosted_dep".to_string()]);

        let (execution, _filtered) = TestSuiteExecution::construct(
            &Arguments::default(),
            &[dep_a, dep_b],
            &[test_a, test_b],
            &[],
        );

        // Descriptor bytes must carry distinct qualified ids and distinct payloads.
        let (mut descriptors, _owners) = execution.collect_hosted_descriptor_bytes_sync();
        descriptors.sort_by(|l, r| l.0.cmp(&r.0));
        assert_eq!(descriptors.len(), 2);
        assert_eq!(descriptors[0].0, "tcrate::mod_a::hosted_dep");
        assert_eq!(descriptors[1].0, "tcrate::mod_b::hosted_dep");
        assert_eq!(descriptors[0].1.as_slice(), &11_u64.to_le_bytes());
        assert_eq!(descriptors[1].1.as_slice(), &22_u64.to_le_bytes());

        // Worker-side routing: pre-populating with `mod_a`'s qualified id
        // must apply only to `mod_a`'s dep, and similarly for `mod_b`.
        // Hosted deps use the same routing pathway as Cloneable, so the same
        // qualified-id-routing guarantee applies here.
        let mut execution = execution;
        let applied_a =
            execution.provide_cloneable_value("tcrate::mod_a::hosted_dep", Arc::new(111_u64));
        assert!(
            applied_a,
            "mod_a hosted dep must be reachable by qualified id"
        );
        let applied_b =
            execution.provide_cloneable_value("tcrate::mod_b::hosted_dep", Arc::new(222_u64));
        assert!(
            applied_b,
            "mod_b hosted dep must be reachable by qualified id"
        );

        let applied_unknown =
            execution.provide_cloneable_value("tcrate::mod_c::hosted_dep", Arc::new(333_u64));
        assert!(
            !applied_unknown,
            "unknown qualified id must not be applied to any dep"
        );

        let first = execution.pick_next_sync().expect("first test");
        let second = execution.pick_next_sync().expect("second test");
        let pairs: Vec<(String, u64)> = [first, second]
            .into_iter()
            .map(|n| {
                let v: Arc<u64> = n
                    .deps
                    .get("hosted_dep")
                    .expect("dep available")
                    .clone()
                    .downcast()
                    .unwrap();
                (n.test.name.clone(), *v)
            })
            .collect();

        let val_a = pairs
            .iter()
            .find(|(n, _)| n == "t_a")
            .expect("t_a picked")
            .1;
        let val_b = pairs
            .iter()
            .find(|(n, _)| n == "t_b")
            .expect("t_b picked")
            .1;
        assert_eq!(val_a, 111);
        assert_eq!(val_b, 222);

        // Each per-module owner constructor must have run exactly once
        // (during the parent-side descriptor collection above); the
        // worker-side provide_cloneable_value calls must not have re-run
        // them on either node.
        assert_eq!(counter_a.load(Ordering::SeqCst), 1);
        assert_eq!(counter_b.load(Ordering::SeqCst), 1);
    }

    /// Mode-consistency regression test for the Hosted scope: when the
    /// runner does NOT spawn workers (e.g. `--nocapture`), tests must
    /// still see the *worker-side handle* produced by the registered
    /// `worker_fn` (i.e. `HostedDep::from_descriptor`), not the raw owner
    /// value returned by the parent constructor. This exercises the same
    /// codec + worker_fn round-trip that the runner-side
    /// `apply_hosted_descriptors_locally` helpers in sync.rs / tokio.rs
    /// perform on the no-spawn-workers path.
    #[test]
    fn hosted_no_spawn_workers_uses_worker_side_handle() {
        // Build a Hosted dep whose owner is one u64 value but whose
        // worker reconstructor produces a DIFFERENT u64 value. If the
        // local code path goes through descriptor->worker_fn correctly,
        // the test must see the worker value (not the owner value).
        let owner_counter = Arc::new(AtomicUsize::new(0));
        let constructor_counter = owner_counter.clone();
        let owner_value: u64 = 0xAAAA_AAAA_AAAA_AAAA_u64;
        let constructor = DependencyConstructor::Sync(Arc::new(move |_view| {
            constructor_counter.fetch_add(1, Ordering::SeqCst);
            Arc::new(owner_value) as Arc<dyn Any + Send + Sync>
        }));
        // Owner-side codec serialises the owner value as raw LE bytes.
        // The worker side wraps those bytes in `Vec<u8>` and the
        // worker_fn flips every bit to demonstrate the worker reconstruction
        // path is taken (the bit-flip stands in for any non-identity
        // `HostedDep::from_descriptor` implementation).
        let codec = CloneableCodec {
            to_wire: Arc::new(|any: Arc<dyn Any + Send + Sync>| {
                let v: Arc<u64> = any.downcast::<u64>().unwrap();
                (*v).to_le_bytes().to_vec()
            }),
            from_wire_bytes: Arc::new(|bytes: &[u8]| {
                let boxed: Vec<u8> = bytes.to_vec();
                Arc::new(boxed) as Arc<dyn Any + Send + Sync>
            }),
        };
        let worker_fn =
            crate::internal::WorkerReconstructor::Sync(Arc::new(|wire_payload, _deps| {
                let bytes_arc: Arc<Vec<u8>> = wire_payload.downcast::<Vec<u8>>().unwrap();
                let arr: [u8; 8] = (*bytes_arc).as_slice().try_into().unwrap();
                let raw: u64 = u64::from_le_bytes(arr);
                let handle_value: u64 = !raw;
                Arc::new(handle_value) as Arc<dyn Any + Send + Sync>
            }));
        let dep = RegisteredDependency {
            name: "hosted_dep".to_string(),
            crate_name: "tcrate".to_string(),
            module_path: String::new(),
            constructor,
            dependencies: Vec::new(),
            scope: DepScope::Hosted,
            worker_fn: Some(worker_fn.clone()),
            cloneable_codec: None,
            hosted_codec: Some(codec.clone()),
            rpc_factory: None,
            companions: Vec::new(),
        };
        let test = registered_test("t1", vec!["hosted_dep".to_string()]);

        let (mut execution, _filtered) =
            TestSuiteExecution::construct(&Arguments::default(), &[dep], &[test], &[]);

        // Parent runs the owner constructor once and collects descriptor bytes
        // (mirroring `collect_hosted_descriptor_bytes_sync` invoked by the
        // no-spawn-workers parent runner).
        let (descriptors, owners) = execution.collect_hosted_descriptor_bytes_sync();
        assert_eq!(descriptors.len(), 1);
        assert_eq!(owners.len(), 1);
        let (dep_id, wire_bytes) = &descriptors[0];

        // Parent reconstructs the WORKER-side handle locally via
        // codec.from_wire_bytes + worker_fn (mirroring
        // `apply_hosted_descriptors_locally` in sync.rs / tokio.rs).
        let wire_payload = (codec.from_wire_bytes)(wire_bytes.as_slice());
        let empty_deps: Arc<dyn crate::internal::DependencyView + Send + Sync> =
            Arc::new(HashMap::<String, Arc<dyn Any + Send + Sync>>::new());
        let reconstructed = match &worker_fn {
            crate::internal::WorkerReconstructor::Sync(f) => f(wire_payload, empty_deps),
            crate::internal::WorkerReconstructor::Async(_) => unreachable!(),
        };
        let applied = execution.provide_cloneable_value(dep_id, reconstructed);
        assert!(applied);

        // Pick the test — it must see the WORKER handle (~owner), NOT the owner
        // constructor's return value.
        let next = execution.pick_next_sync().expect("test picked");
        let view = next.deps.get("hosted_dep").expect("dep available");
        let value: Arc<u64> = view.clone().downcast::<u64>().unwrap();
        assert_eq!(
            *value,
            !owner_value,
            "Hosted dep must expose the worker-side handle (from_descriptor) even in the no-spawn-workers path"
        );
        assert_eq!(
            owner_counter.load(Ordering::SeqCst),
            1,
            "owner constructor must have run exactly once during descriptor collection"
        );
    }

    /// Hosted owners construct their dependencies in the parent collection
    /// context. Worker-side values reconstructed from descriptors are leaves;
    /// the constructor dependencies are not re-created from wire bytes.
    #[test]
    fn hosted_dep_with_owner_dependencies_constructs_in_parent_context() {
        let dep_counter = Arc::new(AtomicUsize::new(0));
        let owner_counter = Arc::new(AtomicUsize::new(0));
        let dep = registered_cloneable_dep("some_other_dep", dep_counter.clone());
        let mut hosted = registered_hosted_dep("h_with_deps", 0, owner_counter.clone());
        hosted.dependencies = vec!["some_other_dep".to_string()];
        let test = registered_test("t1", vec!["h_with_deps".to_string()]);
        let (execution, _filtered) =
            TestSuiteExecution::construct(&Arguments::default(), &[dep, hosted], &[test], &[]);
        let collected = execution.collect_parent_shared_dependencies_sync();

        assert_eq!(collected.hosted_descriptor_bytes.len(), 1);
        assert_eq!(dep_counter.load(Ordering::SeqCst), 1);
        assert_eq!(owner_counter.load(Ordering::SeqCst), 1);
    }

    /// Tokio path: async owner constructors are awaited on the parent's
    /// collector.
    #[cfg(feature = "tokio")]
    #[test]
    fn async_hosted_descriptor_collection_awaits_async_constructor() {
        use std::pin::Pin;

        let counter = Arc::new(AtomicUsize::new(0));
        let constructor_counter = counter.clone();

        let constructor = DependencyConstructor::Async(Arc::new(move |_view| {
            let counter = constructor_counter.clone();
            Box::pin(async move {
                tokio::task::yield_now().await;
                counter.fetch_add(1, Ordering::SeqCst);
                let value: u64 = 42;
                Arc::new(value) as Arc<dyn Any + Send + Sync>
            }) as Pin<Box<dyn std::future::Future<Output = Arc<dyn Any + Send + Sync>>>>
        }));
        let codec = CloneableCodec {
            to_wire: Arc::new(|any| {
                let v: Arc<u64> = any.downcast::<u64>().unwrap();
                (*v).to_le_bytes().to_vec()
            }),
            from_wire_bytes: Arc::new(|bytes| {
                let boxed: Vec<u8> = bytes.to_vec();
                Arc::new(boxed) as Arc<dyn Any + Send + Sync>
            }),
        };
        let dep = RegisteredDependency {
            name: "hosted_async".to_string(),
            crate_name: "tcrate".to_string(),
            module_path: String::new(),
            constructor,
            dependencies: Vec::new(),
            scope: DepScope::Hosted,
            worker_fn: Some(crate::internal::WorkerReconstructor::Sync(Arc::new(
                |wire_payload, _| {
                    let bytes_arc: Arc<Vec<u8>> = wire_payload.downcast::<Vec<u8>>().unwrap();
                    let arr: [u8; 8] = (*bytes_arc).as_slice().try_into().unwrap();
                    let value: u64 = u64::from_le_bytes(arr);
                    Arc::new(value) as Arc<dyn Any + Send + Sync>
                },
            ))),
            cloneable_codec: None,
            hosted_codec: Some(codec),
            rpc_factory: None,
            companions: Vec::new(),
        };
        let test = registered_test("t1", vec!["hosted_async".to_string()]);

        let (execution, _filtered) =
            TestSuiteExecution::construct(&Arguments::default(), &[dep], &[test], &[]);

        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .unwrap();
        let (descriptors, owners) =
            runtime.block_on(execution.collect_hosted_descriptor_bytes_async());

        assert_eq!(descriptors.len(), 1);
        assert_eq!(owners.len(), 1);
        assert_eq!(descriptors[0].0, "tcrate::hosted_async");
        assert_eq!(descriptors[0].1.as_slice(), &42_u64.to_le_bytes());
        assert_eq!(counter.load(Ordering::SeqCst), 1);

        let held: Arc<u64> = owners[0].clone().downcast::<u64>().unwrap();
        assert_eq!(*held, 42);
    }

    // ===========================================================
    // HostedRpc unit tests
    // ===========================================================

    use crate::internal::{
        HostedRpcChannel, HostedRpcDep, HostedRpcError, HostedRpcOwnerCell, HostedRpcTransport,
        InProcessHostedRpcTransport, RpcFactory,
    };

    /// Owner type for HostedRpc unit tests. A trivial monotonic counter
    /// that increments on every `dispatch(method_idx=1, _)` call and
    /// returns the new value as big-endian bytes.
    struct RpcCounter {
        n: u64,
    }

    impl HostedRpcDep for RpcCounter {
        type Stub = RpcCounterStub;
        fn dispatch(&mut self, method_idx: u32, args: &[u8]) -> Result<Vec<u8>, String> {
            match method_idx {
                1 => {
                    self.n += 1;
                    Ok(self.n.to_be_bytes().to_vec())
                }
                // Large-payload echo. Args are a 4-byte big-endian
                // u32 size; the owner returns `size` bytes filled with a
                // deterministic `i % 251` pattern so framing corruption
                // is caught explicitly, not just length mismatch.
                2 => {
                    let arr: [u8; 4] = args
                        .try_into()
                        .map_err(|_| "method_idx=2 requires exactly 4 bytes (size)".to_string())?;
                    let size = u32::from_be_bytes(arr) as usize;
                    let mut out = vec![0u8; size];
                    for (i, b) in out.iter_mut().enumerate() {
                        *b = (i % 251) as u8;
                    }
                    Ok(out)
                }
                other => Err(format!("RpcCounter: unknown method_idx {other}")),
            }
        }
        fn build_stub(channel: HostedRpcChannel) -> Self::Stub {
            RpcCounterStub { channel }
        }
    }

    /// Worker-visible stub for the test owner above.
    struct RpcCounterStub {
        channel: HostedRpcChannel,
    }

    impl RpcCounterStub {
        fn next(&self) -> u64 {
            let bytes = self.channel.call(1, Vec::new()).expect("rpc call");
            let arr: [u8; 8] = bytes.as_slice().try_into().unwrap();
            u64::from_be_bytes(arr)
        }

        /// Request `size` bytes back from the owner.
        fn echo(&self, size: u32) -> Vec<u8> {
            self.channel
                .call(2, size.to_be_bytes().to_vec())
                .expect("echo rpc call")
        }
    }

    /// Builds a HostedRpc `RegisteredDependency` for tests. The constructor
    /// wraps an [`RpcCounter`] into a [`HostedRpcOwnerCell`] (mirroring the
    /// macro-emitted code), counts its own runs in `counter`, and the
    /// `RpcFactory` performs the symmetric downcast back to a cell.
    fn registered_hosted_rpc_dep(
        name: &str,
        module_path: &str,
        owner_counter: Arc<AtomicUsize>,
    ) -> RegisteredDependency {
        let ctor_counter = owner_counter.clone();
        let constructor = DependencyConstructor::Sync(Arc::new(move |_view| {
            ctor_counter.fetch_add(1, Ordering::SeqCst);
            let cell = HostedRpcOwnerCell::from_owner(RpcCounter { n: 0 });
            Arc::new(cell) as Arc<dyn Any + Send + Sync>
        }));
        let factory = RpcFactory {
            owner_into_cell: Arc::new(|any: Arc<dyn Any + Send + Sync>| {
                any.downcast::<HostedRpcOwnerCell>()
                    .expect("HostedRpc owner downcast")
            }),
            build_stub: Arc::new(|channel: HostedRpcChannel| {
                let stub = <RpcCounter as HostedRpcDep>::build_stub(channel);
                Arc::new(stub) as Arc<dyn Any + Send + Sync>
            }),
        };
        RegisteredDependency {
            name: name.to_string(),
            crate_name: "tcrate".to_string(),
            module_path: module_path.to_string(),
            constructor,
            dependencies: Vec::new(),
            scope: DepScope::HostedRpc,
            worker_fn: None,
            cloneable_codec: None,
            hosted_codec: None,
            rpc_factory: Some(factory),
            companions: Vec::new(),
        }
    }

    #[test]
    fn hosted_rpc_owner_cells_collected_once_and_keyed_by_qualified_id() {
        let counter = Arc::new(AtomicUsize::new(0));
        let dep = registered_hosted_rpc_dep("rpc_dep", "", counter.clone());
        let test = registered_test("t1", vec!["rpc_dep".to_string()]);

        let (execution, _filtered) =
            TestSuiteExecution::construct(&Arguments::default(), &[dep], &[test], &[]);

        assert!(execution.has_hosted_rpc_dependencies());

        let cells = execution.collect_hosted_rpc_owner_cells_sync();
        assert_eq!(cells.len(), 1, "exactly one hosted rpc dep expected");
        let (dep_id, _cell) = &cells[0];
        assert_eq!(dep_id, "tcrate::rpc_dep");
        assert_eq!(
            counter.load(Ordering::SeqCst),
            1,
            "owner constructor must run exactly once on the parent"
        );

        // Collecting again must not re-run the constructor in the same
        // execution tree (the constructor is called inside collect, not
        // memoised). This asserts that we don't accidentally double-collect
        // when the runner makes the call.
        let cells_b = execution.collect_hosted_rpc_owner_cells_sync();
        assert_eq!(cells_b.len(), 1);
        assert_eq!(
            counter.load(Ordering::SeqCst),
            2,
            "collect_hosted_rpc_owner_cells_sync runs the constructor on every call; \
             callers (the runner) are responsible for only calling it once per suite"
        );
    }

    #[test]
    fn hosted_rpc_owner_dependencies_construct_in_parent_context() {
        let parent_only_counter = Arc::new(AtomicUsize::new(0));
        let owner_counter = Arc::new(AtomicUsize::new(0));
        let parent_only_dep =
            registered_cloneable_dep("parent_only_dep", parent_only_counter.clone());
        let mut rpc_dep = registered_hosted_rpc_dep("rpc_dep", "", owner_counter.clone());
        rpc_dep.dependencies = vec!["parent_only_dep".to_string()];
        let test = registered_test("t1", vec!["rpc_dep".to_string()]);

        let (execution, _filtered) = TestSuiteExecution::construct(
            &Arguments::default(),
            &[parent_only_dep, rpc_dep],
            &[test],
            &[],
        );

        let cells = execution.collect_hosted_rpc_owner_cells_sync();
        assert_eq!(cells.len(), 1);
        assert_eq!(parent_only_counter.load(Ordering::SeqCst), 1);
        assert_eq!(owner_counter.load(Ordering::SeqCst), 1);
    }

    #[test]
    fn hosted_rpc_in_process_transport_routes_to_owner_cell() {
        let counter = Arc::new(AtomicUsize::new(0));
        let dep = registered_hosted_rpc_dep("rpc_dep", "", counter.clone());
        let test = registered_test("t1", vec!["rpc_dep".to_string()]);

        let (execution, _filtered) =
            TestSuiteExecution::construct(&Arguments::default(), &[dep], &[test], &[]);

        let cells: HashMap<String, Arc<HostedRpcOwnerCell>> = execution
            .collect_hosted_rpc_owner_cells_sync()
            .into_iter()
            .collect();

        let transport: Arc<dyn HostedRpcTransport> =
            Arc::new(InProcessHostedRpcTransport::new(cells.clone()));
        let channel = HostedRpcChannel::new("tcrate::rpc_dep".to_string(), transport.clone());
        let stub = <RpcCounter as HostedRpcDep>::build_stub(channel);

        assert_eq!(stub.next(), 1);
        assert_eq!(stub.next(), 2);
        assert_eq!(stub.next(), 3);
    }

    #[test]
    fn hosted_rpc_in_process_transport_returns_dispatch_error_on_unknown_method() {
        let counter = Arc::new(AtomicUsize::new(0));
        let dep = registered_hosted_rpc_dep("rpc_dep", "", counter);
        let test = registered_test("t1", vec!["rpc_dep".to_string()]);

        let (execution, _filtered) =
            TestSuiteExecution::construct(&Arguments::default(), &[dep], &[test], &[]);

        let cells: HashMap<String, Arc<HostedRpcOwnerCell>> = execution
            .collect_hosted_rpc_owner_cells_sync()
            .into_iter()
            .collect();
        let transport: Arc<dyn HostedRpcTransport> =
            Arc::new(InProcessHostedRpcTransport::new(cells.clone()));
        let channel = HostedRpcChannel::new("tcrate::rpc_dep".to_string(), transport.clone());

        // Call an unknown method index directly; the owner's `dispatch`
        // returns `Err("…")` which the transport surfaces as a Dispatch error.
        let err = channel.call(999, Vec::new()).unwrap_err();
        match err {
            HostedRpcError::Dispatch(msg) => {
                assert!(
                    msg.contains("unknown method_idx 999"),
                    "expected dispatch error to mention method_idx, got '{msg}'"
                );
            }
            HostedRpcError::Transport(msg) => {
                panic!("expected Dispatch error, got Transport({msg})");
            }
        }
    }

    #[test]
    fn hosted_rpc_in_process_transport_returns_transport_error_on_unknown_dep_id() {
        let cells: HashMap<String, Arc<HostedRpcOwnerCell>> = HashMap::new();
        let transport: Arc<dyn HostedRpcTransport> =
            Arc::new(InProcessHostedRpcTransport::new(cells));
        let channel = HostedRpcChannel::new("tcrate::missing_dep".to_string(), transport.clone());
        let err = channel.call(1, Vec::new()).unwrap_err();
        match err {
            HostedRpcError::Transport(msg) => {
                assert!(
                    msg.contains("unknown dep id 'tcrate::missing_dep'"),
                    "expected transport error to mention dep id, got '{msg}'"
                );
            }
            HostedRpcError::Dispatch(msg) => {
                panic!("expected Transport error, got Dispatch({msg})");
            }
        }
    }

    // -------------------------------------------------------------
    // Coverage for owner panic + mutex poisoning. The owner-cell catches the
    // panic, turns it into `Err("hosted rpc owner panicked: ...")` for the
    // first call, and subsequent calls hit the poisoned mutex and get the
    // stable `"hosted rpc owner poisoned"` error.
    // -------------------------------------------------------------

    /// Owner that panics on every dispatch call. Used to exercise the
    /// catch_unwind + poisoned-mutex paths in `HostedRpcOwnerCell::dispatch`.
    struct PanickingRpcOwner;

    impl HostedRpcDep for PanickingRpcOwner {
        type Stub = RpcCounterStub;
        fn dispatch(&mut self, _method_idx: u32, _args: &[u8]) -> Result<Vec<u8>, String> {
            panic!("owner_panic_for_test");
        }
        fn build_stub(channel: HostedRpcChannel) -> Self::Stub {
            RpcCounterStub { channel }
        }
    }

    /// Async owner used to verify `AsyncHostedRpcDep`-flavoured cells
    /// dispatch through the async path and surface results correctly.
    /// Yields once to force the future to actually `.await` (a no-op
    /// `std::future::ready` wouldn't exercise the async cell machinery).
    #[cfg(feature = "tokio")]
    struct AsyncRpcCounter {
        n: u64,
    }

    #[cfg(feature = "tokio")]
    impl crate::internal::AsyncHostedRpcDep for AsyncRpcCounter {
        type Stub = RpcCounterStub;
        async fn dispatch(&mut self, method_idx: u32, _args: &[u8]) -> Result<Vec<u8>, String> {
            // Force a real `.await` so the async dispatch machinery
            // is actually exercised (not just a `ready(...)` bridge).
            ::tokio::task::yield_now().await;
            if method_idx == 1 {
                self.n += 1;
                Ok(self.n.to_be_bytes().to_vec())
            } else {
                Err(format!("AsyncRpcCounter: unknown method_idx {method_idx}"))
            }
        }
        fn build_stub(channel: HostedRpcChannel) -> Self::Stub {
            RpcCounterStub { channel }
        }
    }

    /// Async owner that always panics — exercises the
    /// `futures::FutureExt::catch_unwind` + poison-flag machinery on
    /// the async cell variant.
    #[cfg(feature = "tokio")]
    struct PanickingAsyncRpcOwner;

    #[cfg(feature = "tokio")]
    impl crate::internal::AsyncHostedRpcDep for PanickingAsyncRpcOwner {
        type Stub = RpcCounterStub;
        async fn dispatch(&mut self, _method_idx: u32, _args: &[u8]) -> Result<Vec<u8>, String> {
            ::tokio::task::yield_now().await;
            panic!("async_owner_panic_for_test");
        }
        fn build_stub(channel: HostedRpcChannel) -> Self::Stub {
            RpcCounterStub { channel }
        }
    }

    /// End-to-end: an async owner registered via `from_async_owner`
    /// dispatches via `dispatch_async` and returns the expected bytes
    /// after actually `.await`ing inside its method body.
    #[cfg(feature = "tokio")]
    #[test]
    fn async_hosted_rpc_owner_dispatches_through_async_cell() {
        let cell = HostedRpcOwnerCell::from_async_owner(AsyncRpcCounter { n: 0 });
        let rt = ::tokio::runtime::Builder::new_multi_thread()
            .enable_all()
            .build()
            .expect("build tokio runtime");

        let bytes_a = rt
            .block_on(cell.dispatch_async(1, &[]))
            .expect("first async dispatch must succeed");
        assert_eq!(bytes_a, 1u64.to_be_bytes().to_vec());

        let bytes_b = rt
            .block_on(cell.dispatch_async(1, &[]))
            .expect("second async dispatch must succeed");
        assert_eq!(bytes_b, 2u64.to_be_bytes().to_vec());
    }

    /// An async owner panic must surface as
    /// `"hosted rpc owner panicked: ..."` and poison the cell so every
    /// subsequent dispatch short-circuits with the stable
    /// `"hosted rpc owner poisoned"` error. Mirrors the sync test.
    #[cfg(feature = "tokio")]
    #[test]
    fn async_hosted_rpc_owner_panic_surfaces_then_poisons() {
        let cell = HostedRpcOwnerCell::from_async_owner(PanickingAsyncRpcOwner);
        let rt = ::tokio::runtime::Builder::new_multi_thread()
            .enable_all()
            .build()
            .expect("build tokio runtime");

        let err1 = rt
            .block_on(cell.dispatch_async(1, &[]))
            .expect_err("first async dispatch must surface the panic as Err");
        assert!(
            err1.contains("hosted rpc owner panicked: async_owner_panic_for_test"),
            "expected first-call error to wrap the async panic payload, got '{err1}'"
        );

        let err2 = rt
            .block_on(cell.dispatch_async(1, &[]))
            .expect_err("second async dispatch must short-circuit on the poisoned cell");
        assert_eq!(
            err2, "hosted rpc owner poisoned",
            "expected poisoned-cell error on the second async call, got '{err2}'"
        );
    }

    /// Regression for the async poison race: a second dispatch that
    /// parks on `tokio::sync::Mutex::lock().await` *before* the first
    /// dispatch panics must still observe the poison flag once it
    /// acquires the mutex, and must not re-enter the owner. Without
    /// the in-lock re-check the second waiter would get a fresh
    /// `MutexGuard` and call the user method on the half-mutated
    /// owner.
    #[cfg(feature = "tokio")]
    #[test]
    fn async_hosted_rpc_owner_poison_blocks_concurrent_waiter() {
        use std::sync::atomic::{AtomicUsize, Ordering};
        use std::sync::Arc;
        use std::time::Duration;

        /// Counts dispatch entries to verify the second call never
        /// re-enters after the first call's panic.
        struct OnePanicThenForbidden {
            entries: Arc<AtomicUsize>,
        }

        impl crate::internal::AsyncHostedRpcDep for OnePanicThenForbidden {
            type Stub = RpcCounterStub;
            async fn dispatch(
                &mut self,
                _method_idx: u32,
                _args: &[u8],
            ) -> Result<Vec<u8>, String> {
                let n = self.entries.fetch_add(1, Ordering::SeqCst);
                // First entry: hold the mutex for long enough that a
                // second dispatch parks on `lock().await`, then panic.
                if n == 0 {
                    ::tokio::time::sleep(Duration::from_millis(50)).await;
                    panic!("first_dispatch_panic_poison_race");
                }
                // Any subsequent re-entry is a bug: the poison flag
                // re-check inside the lock should have short-circuited
                // before we got here.
                panic!("second_dispatch_unexpectedly_re_entered_after_poison");
            }
            fn build_stub(channel: HostedRpcChannel) -> Self::Stub {
                RpcCounterStub { channel }
            }
        }

        let entries = Arc::new(AtomicUsize::new(0));
        let cell = Arc::new(HostedRpcOwnerCell::from_async_owner(
            OnePanicThenForbidden {
                entries: entries.clone(),
            },
        ));

        let rt = ::tokio::runtime::Builder::new_multi_thread()
            .enable_all()
            .build()
            .expect("build tokio runtime");

        rt.block_on(async {
            let cell_a = cell.clone();
            let cell_b = cell.clone();

            let first = ::tokio::spawn(async move { cell_a.dispatch_async(1, &[]).await });

            // Give `first` a head start so it definitely owns the
            // mutex before `second` starts and parks on `lock().await`.
            ::tokio::time::sleep(Duration::from_millis(5)).await;

            let second = ::tokio::spawn(async move { cell_b.dispatch_async(1, &[]).await });

            let first_res = first.await.expect("first task must not be cancelled");
            let second_res = second.await.expect("second task must not be cancelled");

            let first_err =
                first_res.expect_err("first dispatch must surface the panic as Err, not Ok");
            assert!(
                first_err.contains("hosted rpc owner panicked: first_dispatch_panic_poison_race"),
                "expected the first call to surface the panic; got '{first_err}'"
            );

            let second_err = second_res
                .expect_err("second dispatch must short-circuit on the poisoned cell, not Ok");
            assert_eq!(
                second_err, "hosted rpc owner poisoned",
                "expected the second waiter to see the poison flag; got '{second_err}'"
            );
        });

        // Exactly one entry into the owner: the second waiter must
        // have been turned away by the poison re-check, never reaching
        // the user dispatcher body.
        assert_eq!(
            entries.load(Ordering::SeqCst),
            1,
            "owner dispatcher must run at most once across the poisoned pair"
        );
    }

    /// `dispatch_blocking` against an `Async` cell on a multi-thread
    /// tokio runtime must succeed (it bridges to `dispatch_async`
    /// via `block_in_place` + `block_on`).
    #[cfg(feature = "tokio")]
    #[test]
    fn async_hosted_rpc_dispatch_blocking_drives_async_cell() {
        let cell = HostedRpcOwnerCell::from_async_owner(AsyncRpcCounter { n: 0 });
        let rt = ::tokio::runtime::Builder::new_multi_thread()
            .enable_all()
            .build()
            .expect("build tokio runtime");
        let bytes = rt
            .block_on(async {
                ::tokio::task::spawn_blocking(move || cell.dispatch_blocking(1, &[])).await
            })
            .expect("spawn_blocking joined")
            .expect("dispatch_blocking must succeed against an async cell on multi-thread rt");
        assert_eq!(bytes, 1u64.to_be_bytes().to_vec());
    }

    /// `dispatch_blocking` on a `current_thread` runtime must return a
    /// clean `Err` rather than panicking inside `block_in_place`. The
    /// API contract is `Result<_, String>`; a `current_thread` runtime
    /// is unsupported but it must not blow up the dispatcher loop.
    #[cfg(feature = "tokio")]
    #[test]
    fn async_hosted_rpc_dispatch_blocking_rejects_current_thread_runtime() {
        let cell = HostedRpcOwnerCell::from_async_owner(AsyncRpcCounter { n: 0 });
        let rt = ::tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .expect("build current-thread tokio runtime");
        // `dispatch_blocking` is invoked *from* the current-thread
        // runtime so that `Handle::try_current()` resolves to it.
        let err = rt
            .block_on(async { cell.dispatch_blocking(1, &[]) })
            .expect_err("dispatch_blocking must reject current-thread runtimes cleanly");
        assert!(
            err.contains("multi-threaded"),
            "expected the rejection error to mention multi-threaded requirement, got '{err}'"
        );
    }

    /// `InProcessHostedRpcTransport` is the `--nocapture` / no-spawn
    /// codepath. Under the tokio runner it must route to async owner
    /// cells via the sync `call` -> `dispatch_blocking` bridge so the
    /// in-process and IPC modes look identical to the user.
    #[cfg(feature = "tokio")]
    #[test]
    fn async_hosted_rpc_in_process_transport_routes_to_async_cell() {
        use std::collections::HashMap;
        use std::sync::Arc;

        let dep_id = "in_process_async_owner".to_string();
        let cell = Arc::new(HostedRpcOwnerCell::from_async_owner(AsyncRpcCounter {
            n: 0,
        }));
        let mut cells = HashMap::new();
        cells.insert(dep_id.clone(), cell);

        let transport: Arc<dyn crate::internal::HostedRpcTransport> =
            Arc::new(InProcessHostedRpcTransport::new(cells));

        let rt = ::tokio::runtime::Builder::new_multi_thread()
            .enable_all()
            .build()
            .expect("build tokio runtime");

        let transport_clone = transport.clone();
        let dep_id_clone = dep_id.clone();
        let bytes = rt
            .block_on(async move {
                ::tokio::task::spawn_blocking(move || {
                    transport_clone.call(&dep_id_clone, 1, vec![])
                })
                .await
                .expect("spawn_blocking joined")
            })
            .expect("first in-process dispatch must succeed");
        assert_eq!(bytes, 1u64.to_be_bytes().to_vec());

        let transport_clone = transport.clone();
        let dep_id_clone = dep_id.clone();
        let bytes2 = rt
            .block_on(async move {
                ::tokio::task::spawn_blocking(move || {
                    transport_clone.call(&dep_id_clone, 1, vec![])
                })
                .await
                .expect("spawn_blocking joined")
            })
            .expect("second in-process dispatch must succeed");
        assert_eq!(bytes2, 2u64.to_be_bytes().to_vec());
    }

    #[test]
    fn hosted_rpc_owner_panic_surfaces_then_poisons() {
        let cell = HostedRpcOwnerCell::from_owner(PanickingRpcOwner);

        // First call: the owner's `dispatch` panics with the literal string
        // "owner_panic_for_test"; `HostedRpcOwnerCell::dispatch` catches the
        // unwind and converts it into a textual error containing the panic
        // payload prefixed with "hosted rpc owner panicked: ".
        //
        // The catch_unwind catches the panic AFTER the MutexGuard has
        // started unwinding, which still leaves the mutex poisoned (verified
        // by direct std::sync::Mutex behaviour).
        let err1 = cell
            .dispatch(1, &[])
            .expect_err("first call must surface the panic as Err");
        assert!(
            err1.contains("hosted rpc owner panicked: owner_panic_for_test"),
            "expected first-call error to wrap the panic payload, got '{err1}'"
        );

        // Second call: the mutex is now poisoned from the panic above.
        // The cell must short-circuit with the stable "hosted rpc owner
        // poisoned" error and must NOT retry the owner.
        let err2 = cell
            .dispatch(1, &[])
            .expect_err("second call must short-circuit on the poisoned cell");
        assert_eq!(
            err2, "hosted rpc owner poisoned",
            "expected poisoned-cell error on the second call, got '{err2}'"
        );
    }

    // -------------------------------------------------------------
    // Large-payload IPC framing coverage (>64 KiB) and
    // concurrent in-flight RPC requests routed through the in-process
    // transport without deadlock or framing corruption.
    // -------------------------------------------------------------

    #[test]
    fn hosted_rpc_in_process_transport_round_trips_large_payload_exceeding_64_kib() {
        let counter = Arc::new(AtomicUsize::new(0));
        let dep = registered_hosted_rpc_dep("rpc_dep", "", counter);
        let test = registered_test("t1", vec!["rpc_dep".to_string()]);

        let (execution, _filtered) =
            TestSuiteExecution::construct(&Arguments::default(), &[dep], &[test], &[]);
        let cells: HashMap<String, Arc<HostedRpcOwnerCell>> = execution
            .collect_hosted_rpc_owner_cells_sync()
            .into_iter()
            .collect();
        let transport: Arc<dyn HostedRpcTransport> =
            Arc::new(InProcessHostedRpcTransport::new(cells));
        let channel = HostedRpcChannel::new("tcrate::rpc_dep".to_string(), transport);
        let stub = <RpcCounter as HostedRpcDep>::build_stub(channel);

        const SIZE: u32 = 256 * 1024; // 256 KiB
        let bytes = stub.echo(SIZE);
        assert_eq!(
            bytes.len(),
            SIZE as usize,
            "framing dropped/truncated bytes"
        );
        for (i, b) in bytes.iter().enumerate() {
            assert_eq!(
                *b,
                (i % 251) as u8,
                "framing corrupted byte at index {i}: expected {}, got {b}",
                (i % 251) as u8
            );
        }
    }

    #[test]
    fn hosted_rpc_in_process_transport_multiplexes_concurrent_calls_from_threads() {
        use std::thread;

        let counter = Arc::new(AtomicUsize::new(0));
        let dep = registered_hosted_rpc_dep("rpc_dep", "", counter);
        let test = registered_test("t1", vec!["rpc_dep".to_string()]);

        let (execution, _filtered) =
            TestSuiteExecution::construct(&Arguments::default(), &[dep], &[test], &[]);
        let cells: HashMap<String, Arc<HostedRpcOwnerCell>> = execution
            .collect_hosted_rpc_owner_cells_sync()
            .into_iter()
            .collect();
        let transport: Arc<dyn HostedRpcTransport> =
            Arc::new(InProcessHostedRpcTransport::new(cells));

        // Spawn N threads, each making M calls. Every call must return a
        // unique positive id (the owner is a single global counter
        // serialised by its own mutex). If the in-process transport ever
        // deadlocks or routes a reply to the wrong caller, the assertions
        // below would fire (duplicate ids, or the spawned thread would
        // panic and the join() would surface the failure).
        const N: usize = 4;
        const M: usize = 32;
        let mut handles = Vec::new();
        for _ in 0..N {
            let dep_id = "tcrate::rpc_dep".to_string();
            let transport = transport.clone();
            handles.push(thread::spawn(move || {
                let channel = HostedRpcChannel::new(dep_id, transport);
                let stub = <RpcCounter as HostedRpcDep>::build_stub(channel);
                let mut ids = Vec::with_capacity(M);
                for _ in 0..M {
                    ids.push(stub.next());
                }
                ids
            }));
        }
        let mut all = Vec::with_capacity(N * M);
        for h in handles {
            all.extend(h.join().expect("thread panicked"));
        }
        all.sort();
        let mut prev: u64 = 0;
        for id in &all {
            assert!(
                *id > prev,
                "duplicate or non-monotonic id {id} after {prev}"
            );
            prev = *id;
        }
        assert_eq!(
            all.len(),
            N * M,
            "expected exactly {} ids in total, got {}",
            N * M,
            all.len()
        );
    }
}