cosca 0.10.0

Unified cross-platform subprocess management: spawning, stdio, process trees, stable identity, and elevation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
//! Unit tests for Windows Job Object containment helpers.
//! Substantive runtime coverage is in the integration tests (tests/spawn_io.rs).

use crate::test_child::{let_member_exit, spawn_job_member, spawn_job_with_two_members, Console};

#[skuld::test]
fn job_handle_debug_does_not_panic() {
    use super::JobHandle;
    let h = JobHandle::create_empty_for_test();
    let s = format!("{h:?}");
    assert!(s.contains("JobHandle"), "debug output: {s}");
}

/// `wait_drained` after `hard_kill` asks the kernel and reports the drain once the killed member
/// has exited.
#[skuld::test]
fn wait_drained_after_hard_kill_reports_the_drain() {
    let (mut child, job) = spawn_job_member();
    job.hard_kill().expect("hard_kill on a live job");
    let verdict = job.wait_drained(None).expect("wait_drained after hard_kill");
    assert_eq!(verdict, crate::containment::TreeDrain::AllMembersExited);
    let status = child.wait().expect("reap the killed member");
    assert!(!status.success(), "the member was killed: {status:?}");
}

/// Against the real OS, with no forced listing: once `wait_drained` answers `AllMembersExited` after
/// the job's kill, every member the job held (each opened before the kill) is signalled.
///
/// Mutant: an empty listing answers `AllMembersExited` without the snapshot's members.
#[skuld::test]
fn after_the_job_kill_the_drain_answers_only_once_every_member_is_signalled() {
    use std::os::windows::io::{AsRawHandle as _, FromRawHandle as _, OwnedHandle};

    use windows::Win32::Foundation::{HANDLE, WAIT_OBJECT_0};
    use windows::Win32::System::Threading::{OpenProcess, WaitForSingleObject, PROCESS_SYNCHRONIZE};

    use crate::containment::Contains as _;

    let (members, job) = crate::test_child::spawn_job_with_members(3);
    let pids = super::query_job_pid_list(HANDLE(job.handle().as_raw_handle())).expect("list the job's members");
    assert!(pids.len() >= members.len(), "the job holds its members: {pids:?}");
    let held: Vec<(u32, OwnedHandle)> = pids
        .into_iter()
        .map(|pid| {
            // SAFETY: a standard Win32 call; the handle returned is ours and owned from here.
            let handle = unsafe { OpenProcess(PROCESS_SYNCHRONIZE, false, pid) }.expect("open a member of the job");
            // SAFETY: `OpenProcess` returned an open handle that nothing else owns.
            (pid, unsafe { OwnedHandle::from_raw_handle(handle.0) })
        })
        .collect();

    job.send_kill(None).into_result().expect("terminate the job");
    let verdict = job.wait_drained(None).expect("wait for the job to drain");

    assert_eq!(verdict, crate::containment::TreeDrain::AllMembersExited);
    for (pid, handle) in &held {
        // SAFETY: `handle` is open for the call.
        let waited = unsafe { WaitForSingleObject(HANDLE(handle.as_raw_handle()), 0) };
        assert_eq!(
            waited, WAIT_OBJECT_0,
            "pid {pid} is a member the drain answered before it was signalled"
        );
    }
}

/// A kill the OS refuses names no member to wait for: its `Err` is returned, where a wait over members
/// it did not kill would hang. A handle without `JOB_OBJECT_TERMINATE` lists the live member and is
/// refused the kill. The wait is made to end the member where it blocks, so a mutant fails by assertion.
///
/// Mutant: the members are named whether or not the kill landed.
#[skuld::test]
fn a_job_kill_the_os_refuses_waits_for_no_member() {
    use std::cell::Cell;
    use std::rc::Rc;

    use windows::Win32::System::SystemServices::JOB_OBJECT_QUERY;

    use crate::containment::Contains as _;

    let (_child, job) = spawn_job_member();
    let refused = job.duplicate_with_access_for_test(JOB_OBJECT_QUERY);

    let (sent, wait) = refused.kill(None).split();
    assert!(
        sent.into_result().is_err(),
        "a handle without JOB_OBJECT_TERMINATE is refused the kill"
    );
    let job = Rc::new(job);
    let blocked = Rc::new(Cell::new(false));
    let _blocking = crate::wait::block_probe::on_blocking({
        let (job, blocked) = (job.clone(), blocked.clone());
        move || {
            blocked.set(true);
            job.hard_kill().expect("end the member");
        }
    });
    wait.wait().expect("a kill that failed waits for nothing");

    assert!(!blocked.get(), "the wait blocked on a member the kill did not reach");
    job.hard_kill().expect("end the member");
}

/// The members are listed before `TerminateJobObject`, not after it: a member that has left the job's
/// list by the time the kill runs is still named. The member is ended and reaped right before the kill,
/// so it is off the job's list when the kill runs, whatever order the kernel signals a process in.
/// The member is `cmd /C more`, whose children (`more.com`, and the `conhost.exe` its console brings)
/// join the job as `cmd` starts them, so the list holds the member among others.
///
/// Mutant: the list is read after the kill.
#[skuld::test]
fn a_job_lists_its_members_before_it_terminates_them() {
    use std::cell::RefCell;
    use std::rc::Rc;

    use crate::containment::Contains as _;

    let (child, job) = spawn_job_member();
    let id = crate::identity::ProcessId::of(child.id())
        .found()
        .expect("the member's identity");
    let child = Rc::new(RefCell::new(child));
    let _ending = super::fault::set_before_job_kill({
        let child = child.clone();
        move || {
            let mut child = child.borrow_mut();
            child.kill().expect("end the member");
            child.wait().expect("reap the member");
        }
    });

    let (sent, wait) = job.kill(None).split();

    sent.into_result().expect("the job's kill");
    assert!(
        wait.members_for_test().contains(&id),
        "the member was named before the kill ended it: {:?}",
        wait.members_for_test()
    );
}

/// A kill that waits for the members reads the job's list once before it, and one that only sends
/// reads none: the list is work for the wait alone.
///
/// Mutant: `send_kill` lists the members first.
#[skuld::test]
fn only_a_job_kill_that_waits_reads_the_members_list() {
    use crate::containment::Contains as _;

    let (_child, job) = spawn_job_member();
    {
        let _forced = super::fault::force_job_pid_list(Vec::new());
        _ = job.kill(None).split();
        assert_eq!(
            super::fault::job_pid_list_queries(),
            1,
            "a kill that waits lists the job once, before it terminates it"
        );
    }
    let (_child2, job2) = spawn_job_member();
    let _forced = super::fault::force_job_pid_list(Vec::new());
    job2.send_kill(None).into_result().expect("the job's kill");
    assert_eq!(
        super::fault::job_pid_list_queries(),
        0,
        "a kill that only sends reads no list"
    );
}

/// `query_job_pid_list` on a freshly created, unpopulated job reports no members — the fast
/// path `wait_drained_raw` relies on to return `AllMembersExited` without ever opening a
/// process handle.
#[skuld::test]
fn query_job_pid_list_is_empty_for_an_unpopulated_job() {
    use super::JobHandle;
    let job = JobHandle::create_empty_for_test();
    let pids = super::query_job_pid_list(job.raw).expect("query pid list");
    assert!(pids.is_empty(), "an empty job must report no members: {pids:?}");
}

/// `wait_drained_raw`'s empty-job fast path: no member was ever assigned, so the very first
/// re-enumeration already reports `AllMembersExited`, with no wait and no deadline needed.
#[skuld::test]
fn wait_drained_raw_reports_drained_for_an_empty_job() {
    use super::JobHandle;
    let job = JobHandle::create_empty_for_test();
    let verdict = super::wait_drained_raw(&job, None).expect("wait_drained_raw");
    assert_eq!(verdict, crate::containment::TreeDrain::AllMembersExited);
}

/// A listing whose pids are none a live member does not end the drain: a member may have joined after
/// it, so the wait lists the job again, and only an empty fresh listing is a drain. The job is empty,
/// so the second listing says drained; the query count is what tells it from a verdict taken off the
/// first.
fn assert_drained_only_by_a_fresh_listing(listed: Vec<u32>) {
    let job = super::JobHandle::create_empty_for_test();
    let forced = super::fault::force_job_pid_list(listed);
    let verdict = super::wait_drained_raw(&job, None).expect("wait_drained_raw");
    assert_eq!(verdict, crate::containment::TreeDrain::AllMembersExited);
    assert_eq!(
        super::fault::job_pid_list_queries(),
        2,
        "the wait ended on a listing of pids that are no live member, without listing again"
    );
    drop(forced);
}

/// A listed pid that names no process (`OpenProcess` answers `ERROR_INVALID_PARAMETER`) is no live
/// member. Windows ignores a pid's low two bits on lookup, so pids 1-3 resolve to 0, which
/// `OpenProcess` refuses.
///
/// Mutant: a round whose listed pids are all gone answers `AllMembersExited`.
#[skuld::test]
fn wait_drained_raw_lists_again_after_a_listing_of_pids_that_are_gone() {
    assert_drained_only_by_a_fresh_listing(vec![1]);
}

/// A listed pid that opens but is not in the job (its number was reused) is no longer a member.
/// This test's own process is that bystander.
#[skuld::test]
fn wait_drained_raw_lists_again_after_a_listing_of_pids_that_are_not_members() {
    assert_drained_only_by_a_fresh_listing(vec![std::process::id()]);
}

/// A round that starts past the deadline lists; a listing that names only pids that are no live member
/// (a deleted pid shows for one listing) is followed by exactly one more listing, which is final. Here
/// the job is empty, so that listing is empty and the tree drained just before the deadline.
///
/// Mutant: a stale-only round past the deadline answers `MembersRemain` off its one listing.
#[skuld::test]
fn wait_drained_raw_with_an_elapsed_deadline_answers_drained_when_the_one_more_listing_is_empty() {
    let job = super::JobHandle::create_empty_for_test();
    let forced = super::fault::force_job_pid_list(vec![1]);
    let zero = crate::wait::deadline_from(std::time::Duration::ZERO);

    let verdict = super::wait_drained_raw(&job, zero).expect("wait_drained_raw");

    assert_eq!(verdict, crate::containment::TreeDrain::AllMembersExited);
    assert_eq!(super::fault::job_pid_list_queries(), 2, "the listing, and one more");
    drop(forced);
}

/// The one more listing is final: a non-empty one answers `MembersRemain` with no open, no poll and no
/// further round. The real job holds a live member for it.
///
/// Mutant: a non-empty final listing is answered `AllMembersExited`.
#[skuld::test]
fn wait_drained_raw_with_an_elapsed_deadline_answers_members_remain_when_the_one_more_listing_is_not_empty() {
    let (_child, job) = spawn_job_member();
    let forced = super::fault::force_job_pid_list(vec![1]);
    let zero = crate::wait::deadline_from(std::time::Duration::ZERO);

    let verdict = super::wait_drained_raw(&job, zero).expect("wait_drained_raw");

    assert_eq!(verdict, crate::containment::TreeDrain::MembersRemain);
    assert_eq!(super::fault::job_pid_list_queries(), 2, "the listing, and one more");
    drop(forced);
    job.hard_kill().expect("end the member");
}

/// Live members that the zero-ms poll finds unsignalled answer `MembersRemain` off the round's own
/// listing: one query, no final listing. The member blocks on its stdin, which this test holds open.
///
/// Mutant: that branch takes the final listing, which is a second query.
#[skuld::test]
fn wait_drained_raw_with_an_elapsed_deadline_answers_members_remain_off_its_own_listing_for_live_members() {
    let (_child, job) = spawn_job_member();
    let forced = super::fault::force_job_pid_lists([]);
    let zero = crate::wait::deadline_from(std::time::Duration::ZERO);

    let verdict = super::wait_drained_raw(&job, zero).expect("wait_drained_raw");

    assert_eq!(verdict, crate::containment::TreeDrain::MembersRemain);
    assert_eq!(super::fault::job_pid_list_queries(), 1, "the round's own listing only");
    drop(forced);
    job.hard_kill().expect("end the member");
}

/// The final listing is the last: when it too names only stale pids, the answer is `MembersRemain` and
/// nothing lists a third time. A third listing here would be the real, empty job.
///
/// Mutant: the one more listing is another round, which lists again.
#[skuld::test]
fn wait_drained_raw_with_an_elapsed_deadline_lists_no_third_time() {
    let job = super::JobHandle::create_empty_for_test();
    let forced = super::fault::force_job_pid_lists([vec![1], vec![1]]);
    let zero = crate::wait::deadline_from(std::time::Duration::ZERO);

    let verdict = super::wait_drained_raw(&job, zero).expect("wait_drained_raw");

    assert_eq!(verdict, crate::containment::TreeDrain::MembersRemain);
    assert_eq!(super::fault::job_pid_list_queries(), 2, "the listing, and one more");
    drop(forced);
}

/// Past the deadline the settling listing is the final one: it names a live member, so `MembersRemain`,
/// with no third listing.
///
/// Mutant: the round falls through to the final listing after it.
#[skuld::test]
fn wait_drained_raw_with_an_elapsed_deadline_takes_the_listing_that_settles_unproved_pids_as_its_last() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(
            wait_drained_raw_with_an_elapsed_deadline_takes_the_listing_that_settles_unproved_pids_as_its_last
        ),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let refused = crate::identity::windows_fixture::spawn_unkillable(&done);
    let (member, job) = spawn_job_member();
    let forced = super::fault::force_job_pid_lists([vec![refused.pid()], vec![member.id()]]);
    let zero = crate::wait::deadline_from(std::time::Duration::ZERO);

    let verdict = super::wait_drained_raw(&job, zero).expect("wait_drained_raw");

    assert_eq!(verdict, crate::containment::TreeDrain::MembersRemain);
    assert_eq!(
        super::fault::job_pid_list_queries(),
        2,
        "the listing, and the one that settles it"
    );
    drop(forced);
    job.hard_kill().expect("end the member");
}

/// A listed member whose handle the zero-ms poll finds signalled (it has exited, and its process object
/// is still held, so it still opens as a member) is stale evidence too: exactly one more listing
/// answers, here the real job, empty.
///
/// Mutant: a signalled poll at an elapsed deadline answers `MembersRemain` off the listing.
#[skuld::test]
fn wait_drained_raw_with_an_elapsed_deadline_lists_once_more_after_a_signalled_poll() {
    use windows::Win32::System::Threading::{OpenProcess, PROCESS_QUERY_LIMITED_INFORMATION, PROCESS_SYNCHRONIZE};

    let (mut child, job) = spawn_job_member();
    drop(child.stdin.take());
    child.wait().expect("the member exits");
    // The member's own child (`more`) is a member too: the job is empty only once it has gone.
    assert_eq!(
        super::wait_drained_raw(&job, None).expect("the job drains"),
        crate::containment::TreeDrain::AllMembersExited
    );
    // SAFETY: a standard Win32 call; the handle is closed below.
    let opened = unsafe {
        OpenProcess(
            PROCESS_SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION,
            false,
            child.id(),
        )
    };
    let super::Standing::Member(held) = super::member_to_watch(opened, job.raw) else {
        panic!("the exited member is still a member of its job while its process object is held");
    };
    // SAFETY: opened above.
    _ = unsafe { windows::Win32::Foundation::CloseHandle(held) };
    let forced = super::fault::force_job_pid_list(vec![child.id()]);
    let zero = crate::wait::deadline_from(std::time::Duration::ZERO);

    let verdict = super::wait_drained_raw(&job, zero).expect("wait_drained_raw");

    assert_eq!(verdict, crate::containment::TreeDrain::AllMembersExited);
    assert_eq!(super::fault::job_pid_list_queries(), 2, "the listing, and one more");
    drop(forced);
}

/// An empty listing answers `AllMembersExited` whatever the deadline.
///
/// Mutant: a round past the deadline answers `MembersRemain` before it reads the listing.
#[skuld::test]
fn wait_drained_raw_with_an_elapsed_deadline_answers_drained_for_an_empty_listing() {
    let job = super::JobHandle::create_empty_for_test();
    let forced = super::fault::force_job_pid_list(Vec::new());
    let zero = crate::wait::deadline_from(std::time::Duration::ZERO);

    let verdict = super::wait_drained_raw(&job, zero).expect("wait_drained_raw");

    assert_eq!(verdict, crate::containment::TreeDrain::AllMembersExited);
    assert_eq!(super::fault::job_pid_list_queries(), 1, "exactly one listing");
    drop(forced);
}

/// An empty first listing is no answer: the drain looks for the job's members in a system snapshot. The
/// member is alive, in the job, when the listing (forced) comes back empty; at an elapsed deadline
/// the drain polls it and answers `MembersRemain`.
///
/// Mutant: an empty first listing answers `AllMembersExited` off the list alone.
#[skuld::test]
fn wait_drained_raw_does_not_answer_an_empty_first_listing_while_a_member_runs() {
    let (_child, job) = spawn_job_member();
    let forced = super::fault::force_job_pid_list(Vec::new());
    let zero = crate::wait::deadline_from(std::time::Duration::ZERO);

    let verdict = super::wait_drained_raw(&job, zero);

    drop(forced);
    job.hard_kill().expect("end the member");
    assert_eq!(
        verdict.expect("wait_drained_raw"),
        crate::containment::TreeDrain::MembersRemain,
        "the drain was answered off an empty listing while a member's process object was unsignalled"
    );
}

/// A member the wait never opened is waited for too: with more live members than one wait set holds
/// (`MAXIMUM_WAIT_OBJECTS - 1`), the last is left unopened, and the listing after the rest exit is
/// empty (forced) while it runs.
///
/// Mutant: the drain confirms only the members it opened.
#[skuld::test]
fn wait_drained_raw_waits_for_a_member_it_never_opened_past_the_wait_set() {
    use crate::test_child::spawn_job_with_members;

    let (mut members, job) = spawn_job_with_members(super::MAXIMUM_WAIT_OBJECTS);
    let unopened = members.pop().expect("a last member");
    let mut listed: Vec<u32> = members.iter().map(|member| member.id()).collect();
    listed.push(unopened.id());
    // Reaped, but each handle stays open, so each pid still opens as a signalled member.
    for member in &mut members {
        drop(member.stdin.take());
        member.wait().expect("wait for a member to exit");
    }
    let forced = super::fault::force_job_pid_lists([listed, Vec::new()]);
    let zero = crate::wait::deadline_from(std::time::Duration::ZERO);

    let verdict = super::wait_drained_raw(&job, zero);

    drop(forced);
    job.hard_kill().expect("end the member");
    assert_eq!(
        verdict.expect("wait_drained_raw"),
        crate::containment::TreeDrain::MembersRemain,
        "the drain was answered while a member it never opened was unsignalled"
    );
}

/// A system snapshot that cannot be taken is the drain's failure, with its cause: an empty listing
/// of a job that has had a process is never answered `AllMembersExited` without it.
#[skuld::test]
fn wait_drained_raw_fails_when_the_snapshot_for_an_empty_listing_cannot_be_taken() {
    let (_child, job) = spawn_job_member();
    let _snapshot = crate::containment::enumerate::backend_fault::force_snapshot_failure();
    let forced = super::fault::force_job_pid_list(Vec::new());

    let verdict = super::wait_drained_raw(&job, None);

    drop(forced);
    job.hard_kill().expect("end the member");
    let error = verdict.expect_err("an empty listing with no snapshot is not a drain");
    assert!(
        matches!(&error, crate::error::Error::Unassessable { source: Some(_), .. }),
        "{error:?}"
    );
}

/// A job that has never had a process has none to wait for: an empty listing answers
/// `AllMembersExited` with no snapshot (a forced failure of it is never met).
///
/// Mutant: the stored count starts at a value no count equals.
#[skuld::test]
fn an_empty_listing_of_a_job_that_never_had_a_process_takes_no_snapshot() {
    let job = super::JobHandle::create_empty_for_test();
    let _snapshot = crate::containment::enumerate::backend_fault::force_snapshot_failure();
    let forced = super::fault::force_job_pid_list(Vec::new());

    let verdict = super::wait_drained_raw(&job, None);

    drop(forced);
    assert_eq!(
        verdict.expect("no snapshot is needed"),
        crate::containment::TreeDrain::AllMembersExited
    );
}

/// A drain that confirmed every member leaves nothing to confirm while no process joins the job: the
/// next empty listing answers `AllMembersExited` with no snapshot (a forced failure of it is never met).
///
/// Mutant: the count of the confirmed drain is not stored, so every empty listing scans.
#[skuld::test]
fn a_second_empty_listing_with_no_process_since_takes_no_snapshot() {
    let (child, job) = spawn_job_member();
    let_member_exit(child);
    assert_eq!(
        job.wait_drained(None).expect("the job drains"),
        crate::containment::TreeDrain::AllMembersExited
    );
    let _snapshot = crate::containment::enumerate::backend_fault::force_snapshot_failure();

    let verdict = job.wait_drained(None);

    assert_eq!(
        verdict.expect("nothing joined, so nothing is scanned"),
        crate::containment::TreeDrain::AllMembersExited
    );
}

/// A process that joins the job after a confirmed drain is confirmed too: the count has risen, so the
/// next empty listing (forced) takes the snapshot, finds the new member running and, at an elapsed
/// deadline, answers `MembersRemain`. A job's kill can leave exactly that listing empty over an
/// unsignalled member.
///
/// Mutant: a confirmed drain's stored count is trusted whatever the job's count is now.
#[skuld::test]
fn a_member_assigned_after_a_confirmed_drain_is_confirmed_too() {
    use std::os::windows::io::AsRawHandle as _;

    let (child, job) = spawn_job_member();
    let_member_exit(child);
    assert_eq!(
        job.wait_drained(None).expect("the job drains"),
        crate::containment::TreeDrain::AllMembersExited
    );
    let second = crate::test_child::spawn_more_blocker();
    job.assign_for_test(second.as_raw_handle())
        .expect("assign a new member to the job");
    let forced = super::fault::force_job_pid_list(Vec::new());
    let zero = crate::wait::deadline_from(std::time::Duration::ZERO);

    let verdict = job.wait_drained(zero);

    drop(forced);
    job.hard_kill().expect("end the member");
    assert_eq!(
        verdict.expect("wait_drained"),
        crate::containment::TreeDrain::MembersRemain,
        "the drain stood for a member it never confirmed"
    );
}

/// A drain that left a member out as settled has not confirmed it: the count is not stored, and a later
/// drain scans, finds the member (still running) and answers `MembersRemain`.
///
/// Mutant: the count is stored although a settled member was left out of the walk.
#[skuld::test]
fn a_drain_that_left_a_settled_member_out_does_not_stand_for_it_later() {
    use crate::containment::tree_kill::Settled;

    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(a_drain_that_left_a_settled_member_out_does_not_stand_for_it_later),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    // A member the caller may not wait on, with no console host beside it: a later wait for it fails.
    let member = crate::identity::windows_fixture::spawn_query_only_without_console(&done);
    let id = crate::identity::windows_identity_from_handle_result(
        &crate::identity::HostKernel,
        member.handle(),
        member.pid(),
    )
    .expect("the restricted member's identity");
    let job = super::JobHandle::create_empty_for_test();
    job.assign_for_test(member.handle().0)
        .expect("assign the restricted member to the job");
    let forced = super::fault::force_job_pid_lists([Vec::new(), Vec::new()]);

    let first = job.wait_drained_past(None, &Settled::new(vec![id], Vec::new()));
    let second = job.wait_drained(None);

    drop(forced);
    assert_eq!(
        first.expect("the first wait"),
        crate::containment::TreeDrain::AllMembersExited,
        "the settled member is not waited for"
    );
    let error = second.expect_err("the second wait finds the member it may not wait on");
    assert!(
        matches!(&error, crate::error::Error::Unassessable { detail, .. } if detail.contains(&member.pid().to_string())),
        "the first drain's count stood for a member it left out: {error:?}"
    );
}

/// A snapshot pid whose membership cannot be established (here a forced `IsProcessInJob` failure on
/// the only member, which has no console host beside it) is not shown to be the job's: it is skipped,
/// as a pid that cannot be opened is, and the drain is not failed by it.
///
/// Mutant: the failure is the drain's error.
#[skuld::test]
fn a_snapshot_pid_whose_membership_cannot_be_established_is_skipped() {
    use windows::Win32::Foundation::ERROR_ACCESS_DENIED;

    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(a_snapshot_pid_whose_membership_cannot_be_established_is_skipped),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let member = crate::identity::windows_fixture::spawn_query_only_without_console(&done);
    let job = super::JobHandle::create_empty_for_test();
    job.assign_for_test(member.handle().0)
        .expect("assign the restricted member to the job");
    let _failed =
        super::fault::force_next_for_thread(super::fault::Step::Membership, ERROR_ACCESS_DENIED.0, member.pid());
    let forced = super::fault::force_job_pid_list(Vec::new());

    let verdict = super::wait_drained_raw(&job, None);

    drop(forced);
    assert_eq!(
        verdict.expect("a pid whose membership is unknown is skipped, not an error"),
        crate::containment::TreeDrain::AllMembersExited
    );
}

/// A pid the job's list names twice in a row, and whose membership cannot be established either time
/// (a live member whose DACL grants the caller nothing), is a member whose exit cannot be observed: it
/// is returned with its pid, for the kill's wait to report.
///
/// Mutant: the listing skips it as the snapshot does.
#[skuld::test]
fn a_pid_listed_twice_whose_membership_cannot_be_established_is_unobserved_with_its_pid() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(
            a_pid_listed_twice_whose_membership_cannot_be_established_is_unobserved_with_its_pid
        ),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let refused = crate::identity::windows_fixture::spawn_unkillable(&done);
    let pid = refused.pid();
    let job = super::JobHandle::create_empty_for_test();
    job.assign_for_test(refused.handle().0)
        .expect("assign the refusing member to the job");

    let (members, unobserved) = job.list_members(None);

    assert!(members.iter().all(|id| id.pid() != pid), "{members:?}");
    assert_eq!(
        unobserved.iter().filter(|unnamed| unnamed.pid == Some(pid)).count(),
        1,
        "{unobserved:?}"
    );
}

/// A pid the job's list names once whose membership cannot be established, and that the next listing
/// does not name (a deleted member's pid, reused by a process the caller may not open), is dropped, not
/// reported.
///
/// Mutant: the first listing's failure is the answer.
#[skuld::test]
fn a_pid_listed_once_whose_membership_cannot_be_established_is_dropped() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(a_pid_listed_once_whose_membership_cannot_be_established_is_dropped),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let refused = crate::identity::windows_fixture::spawn_unkillable(&done);
    let job = super::JobHandle::create_empty_for_test();
    let forced = super::fault::force_job_pid_list(vec![refused.pid()]);

    let (members, unobserved) = job.list_members(None);

    assert_eq!(
        super::fault::job_pid_list_queries(),
        2,
        "the listing, and the one that settles it"
    );
    drop(forced);
    assert!(members.is_empty(), "{members:?}");
    assert!(unobserved.is_empty(), "{unobserved:?}");
}

/// When the listing that settles the unproved pids fails, neither those pids nor the listing can be
/// cleared: each is returned (the pid named, the listing nameless, with its own cause and its own
/// text, which does not say the first listing failed), for the kill's wait to report.
///
/// Mutant: the failed second listing is dropped, as an empty one is, and the unproved pid with it.
#[skuld::test]
fn list_members_reports_the_unproved_pids_and_the_listing_when_the_second_listing_fails() {
    use windows::Win32::Foundation::ERROR_NOT_ENOUGH_MEMORY;

    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(
            list_members_reports_the_unproved_pids_and_the_listing_when_the_second_listing_fails
        ),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let refused = crate::identity::windows_fixture::spawn_unkillable(&done);
    let job = super::JobHandle::create_empty_for_test();
    let forced = super::fault::force_job_pid_lists_then_failure([vec![refused.pid()]], ERROR_NOT_ENOUGH_MEMORY.0);

    let (members, unobserved) = job.list_members(None);

    drop(forced);
    assert!(members.is_empty(), "{members:?}");
    assert_eq!(unobserved.len(), 2, "{unobserved:?}");
    assert_eq!(
        unobserved
            .iter()
            .filter(|unnamed| unnamed.pid == Some(refused.pid()))
            .count(),
        1,
        "the unproved pid is named: {unobserved:?}"
    );
    let listing = unobserved
        .iter()
        .find(|unnamed| unnamed.pid.is_none())
        .expect("the failed listing is returned, nameless");
    assert_eq!(
        crate::error::first_os_code(listing.why()),
        Some(ERROR_NOT_ENOUGH_MEMORY.0 as i32),
        "{listing:?}"
    );
    assert!(
        matches!(listing.why(), crate::error::Error::Unassessable { detail, .. }
            if !detail.contains("before the kill")),
        "{listing:?}"
    );
}

/// A snapshot member whose start time cannot be read (a forced failure) is an error naming its pid:
/// it is a member, and its exit cannot be observed.
///
/// Mutant: the member is skipped.
#[skuld::test]
fn a_snapshot_member_whose_identity_cannot_be_read_is_an_error() {
    use windows::Win32::Foundation::ERROR_ACCESS_DENIED;

    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(a_snapshot_member_whose_identity_cannot_be_read_is_an_error),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let member = crate::identity::windows_fixture::spawn_query_only_without_console(&done);
    let pid = member.pid();
    let job = super::JobHandle::create_empty_for_test();
    job.assign_for_test(member.handle().0)
        .expect("assign the restricted member to the job");
    let _failed = super::fault::force_next_for_thread(super::fault::Step::Identity, ERROR_ACCESS_DENIED.0, pid);
    let forced = super::fault::force_job_pid_list(Vec::new());

    let verdict = super::wait_drained_raw(&job, None);

    drop(forced);
    let error = verdict.expect_err("a member whose identity cannot be read is not a drain");
    assert!(
        matches!(&error, crate::error::Error::Unassessable { detail, source: Some(_) }
            if detail.contains(&pid.to_string())),
        "{error:?}"
    );
}

/// A member the listing could not name by identity is reported once by a kill's wait: the edge's
/// confirmation finds the same member in the snapshot, its identity unreadable there too (a forced
/// failure), and does not report it again.
///
/// Mutant: the edge is waited for with no memory of the pids the listing could not name, so the
/// confirmation names the same pid a second time.
#[skuld::test]
fn a_kill_wait_reports_a_member_whose_identity_cannot_be_read_once() {
    use windows::Win32::Foundation::ERROR_ACCESS_DENIED;

    use crate::containment::dispatch::TreeKillOutcome;
    use crate::containment::drain_edge::DrainEdge;
    use crate::containment::tree_kill::Unnamed;
    use crate::containment::TreeKill;

    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(a_kill_wait_reports_a_member_whose_identity_cannot_be_read_once),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let member = crate::identity::windows_fixture::spawn_query_only_without_console(&done);
    let pid = member.pid();
    let job = super::JobHandle::create_empty_for_test();
    job.assign_for_test(member.handle().0)
        .expect("assign the restricted member to the job");
    let unnamed = Unnamed::member(
        pid,
        crate::error::Error::Unassessable {
            detail: "its start time could not be read".to_owned(),
            source: None,
        },
    );
    let _failed = super::fault::force_next_for_thread(super::fault::Step::Identity, ERROR_ACCESS_DENIED.0, pid);
    let forced = super::fault::force_job_pid_list(Vec::new());
    let (_, wait) = TreeKill::listed(
        TreeKillOutcome::Ran(Ok(())),
        Vec::new(),
        vec![unnamed],
        DrainEdge::Job(&job),
    )
    .split();

    let verdict = wait.wait();

    drop(forced);
    let error = verdict.expect_err("a member whose exit cannot be observed is not a drain");
    let report = crate::error::Report(&error).to_string();
    assert_eq!(
        report.matches(&format!("pid {pid}")).count(),
        1,
        "the member is named once: {report}"
    );
}

/// A snapshot member whose start time cannot be read is the drain's error whatever the deadline: one
/// still-running member the confirmation would wait for at a deadline does not hide it. A member that
/// runs (A) is in the job beside one that cannot be identified (B, a forced failure); the wait is
/// bounded by an elapsed deadline, so a walk that waits for A first answers `MembersRemain`.
///
/// Mutant: the walk reports B's error only once it reaches it, after waiting for A.
#[skuld::test]
fn a_member_whose_identity_cannot_be_read_is_reported_though_another_still_runs_at_the_deadline() {
    use windows::Win32::Foundation::ERROR_ACCESS_DENIED;

    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(
            a_member_whose_identity_cannot_be_read_is_reported_though_another_still_runs_at_the_deadline
        ),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    use std::os::windows::io::AsRawHandle as _;

    let job = super::JobHandle::create_empty_for_test();
    // A is spawned first, so the snapshot most likely names it first (the order the mutant needs); B
    // is assigned first, as the OS lets a process of the fixture's own job into a new job only first.
    let running = crate::test_child::spawn_more_blocker();
    let unreadable = crate::identity::windows_fixture::spawn_query_only_without_console(&done);
    let pid = unreadable.pid();
    job.assign_for_test(unreadable.handle().0)
        .expect("assign the unreadable member to the job");
    job.assign_for_test(running.as_raw_handle())
        .expect("assign the running member to the job");
    let _failed = super::fault::force_next_for_thread(super::fault::Step::Identity, ERROR_ACCESS_DENIED.0, pid);
    let forced = super::fault::force_job_pid_list(Vec::new());
    let zero = crate::wait::deadline_from(std::time::Duration::ZERO);

    let verdict = super::wait_drained_raw(&job, zero);

    drop(forced);
    job.hard_kill().expect("end the members");
    let error = verdict.expect_err("a member whose identity cannot be read is not a drain, nor remaining");
    assert!(
        matches!(&error, crate::error::Error::Unassessable { detail, .. } if detail.contains(&pid.to_string())),
        "{error:?}"
    );
}

/// A member the kill's wait already reported by its identity (it refuses the wait's open) is reported
/// once, though the confirmation finds it in the snapshot with its start time unreadable (a forced
/// failure): the entry with no identity matches the member by its pid.
///
/// Mutant: `Settled::covers` matches an entry with no identity only against the pids the listing could
/// not name, so the member is named a second time.
#[skuld::test]
fn a_kill_wait_reports_a_member_it_waited_for_once_when_the_snapshot_cannot_read_its_identity() {
    use windows::Win32::Foundation::ERROR_ACCESS_DENIED;

    use crate::containment::dispatch::TreeKillOutcome;
    use crate::containment::drain_edge::DrainEdge;
    use crate::containment::TreeKill;

    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(
            a_kill_wait_reports_a_member_it_waited_for_once_when_the_snapshot_cannot_read_its_identity
        ),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let member = crate::identity::windows_fixture::spawn_query_only_without_console(&done);
    let pid = member.pid();
    let id = crate::identity::windows_identity_from_handle_result(&crate::identity::HostKernel, member.handle(), pid)
        .expect("the restricted member's identity");
    let job = super::JobHandle::create_empty_for_test();
    job.assign_for_test(member.handle().0)
        .expect("assign the restricted member to the job");
    let _failed = super::fault::force_next_for_thread(super::fault::Step::Identity, ERROR_ACCESS_DENIED.0, pid);
    let forced = super::fault::force_job_pid_list(Vec::new());
    let (_, wait) = TreeKill::listed(TreeKillOutcome::Ran(Ok(())), vec![id], Vec::new(), DrainEdge::Job(&job)).split();

    let verdict = wait.wait();

    drop(forced);
    let error = verdict.expect_err("a member whose exit cannot be observed is not a drain");
    let report = crate::error::Report(&error).to_string();
    assert_eq!(
        report.matches(&format!("pid {pid}")).count(),
        1,
        "the member is named once: {report}"
    );
}

/// A member the caller may open for its membership but not wait on (an elevated member under a
/// non-elevated caller) is a member whose exit cannot be observed: the drain says so, naming it,
/// with the OS error as the cause, and never answers `AllMembersExited` while it runs. The member's
/// DACL grants `PROCESS_QUERY_LIMITED_INFORMATION` and nothing else.
///
/// Mutant: membership is decided with the access the wait needs, so a member the wait is refused
/// for is not one.
#[skuld::test]
fn wait_drained_raw_names_a_member_it_may_not_wait_on_when_the_listing_is_empty() {
    use std::error::Error as _;
    use windows::Win32::Foundation::ERROR_ACCESS_DENIED;

    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(
            wait_drained_raw_names_a_member_it_may_not_wait_on_when_the_listing_is_empty
        ),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    // Without a console, so the job's only member is the restricted one: a console host the member
    // brought along would be another member, running, and which the drain reaches first would
    // decide the answer.
    let member = crate::identity::windows_fixture::spawn_query_only_without_console(&done);
    let job = super::JobHandle::create_empty_for_test();
    job.assign_for_test(member.handle().0)
        .expect("assign the restricted member to the job");
    let forced = super::fault::force_job_pid_list(Vec::new());

    let verdict = super::wait_drained_raw(&job, None);

    drop(forced);
    let error = verdict.expect_err("a member whose exit cannot be observed is not a drain");
    assert!(
        matches!(&error, crate::error::Error::Unassessable { detail, source: Some(_) }
            if detail.contains(&member.pid().to_string())),
        "{error:?}"
    );
    assert_eq!(
        crate::error::first_os_code(error.source().expect("the OS error is the cause")),
        Some(ERROR_ACCESS_DENIED.0 as i32),
        "{error:?}"
    );
}

/// [`wait_drained_raw_names_a_member_it_may_not_wait_on_when_the_listing_is_empty`] for a member the
/// job still LISTS: the round's open for its wait is refused, and the answer names its pid with the OS
/// error as the cause, as the confirmation's does.
///
/// Mutant: the round that is refused the only listed member drops its pid from the error.
#[skuld::test]
fn wait_drained_raw_names_a_member_it_may_not_wait_on_while_the_job_lists_it() {
    use std::error::Error as _;
    use windows::Win32::Foundation::ERROR_ACCESS_DENIED;

    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(wait_drained_raw_names_a_member_it_may_not_wait_on_while_the_job_lists_it),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let member = crate::identity::windows_fixture::spawn_query_only_without_console(&done);
    let job = super::JobHandle::create_empty_for_test();
    job.assign_for_test(member.handle().0)
        .expect("assign the restricted member to the job");

    let verdict = super::wait_drained_raw(&job, None);

    let error = verdict.expect_err("a member whose exit cannot be observed is not a drain");
    assert!(
        matches!(&error, crate::error::Error::Unassessable { detail, source: Some(_) }
            if detail.contains(&member.pid().to_string())),
        "{error:?}"
    );
    assert_eq!(
        crate::error::first_os_code(error.source().expect("the OS error is the cause")),
        Some(ERROR_ACCESS_DENIED.0 as i32),
        "{error:?}"
    );
}

/// A listed pid nothing lets the caller open (a deleted member's, reused by the fixture with an empty
/// DACL, not in the job) is no member: with the listing empty after it, `AllMembersExited`.
///
/// Mutant: a refused open of a listed pid is the drain's error whatever membership it could prove.
#[skuld::test]
fn wait_drained_raw_lists_again_when_every_listed_pid_is_refused_and_unproved() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(wait_drained_raw_lists_again_when_every_listed_pid_is_refused_and_unproved),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let refused = crate::identity::windows_fixture::spawn_unkillable(&done);
    let job = super::JobHandle::create_empty_for_test();
    let forced = super::fault::force_job_pid_lists([vec![refused.pid()], Vec::new()]);

    let verdict = super::wait_drained_raw(&job, None);

    assert_eq!(
        super::fault::job_pid_list_queries(),
        2,
        "the listing, and the one that settles it"
    );
    drop(forced);
    assert_eq!(
        verdict.expect("a refused pid the job cannot be shown to hold is not a failed drain"),
        crate::containment::TreeDrain::AllMembersExited
    );
}

/// A pid listed twice in a row and unprovable the second time: `Unassessable` naming it, with no
/// further listing. The fixture is outside the job; the forced listings stand for a live member.
///
/// Mutant: the round lists again through `Round::List`, so a third (real, empty) listing answers
/// `AllMembersExited` and `expect_err` fails.
#[skuld::test]
fn wait_drained_raw_names_a_pid_listed_twice_that_cannot_be_proved_a_member() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(wait_drained_raw_names_a_pid_listed_twice_that_cannot_be_proved_a_member),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let refused = crate::identity::windows_fixture::spawn_unkillable(&done);
    let job = super::JobHandle::create_empty_for_test();
    let forced = super::fault::force_job_pid_lists([vec![refused.pid()], vec![refused.pid()]]);

    let verdict = super::wait_drained_raw(&job, None);

    assert_eq!(
        super::fault::job_pid_list_queries(),
        2,
        "answered by the settling listing, before any further one"
    );
    drop(forced);
    let error = verdict.expect_err("a pid that stays unproved is not a drained tree");
    assert!(
        matches!(&error, crate::error::Error::Unassessable { detail, source: Some(_) }
            if detail.contains(&refused.pid().to_string())),
        "{error:?}"
    );
}

/// A live member in the job that refuses even the query-only open, no deadline: `Unassessable` naming
/// it, not a listing again for as long as it lives.
///
/// The member is listed twice and a third listing fails, where the real job would list it for as
/// long as it lives; that the real job lists such a member twice is
/// `a_pid_listed_twice_whose_membership_cannot_be_established_is_unobserved_with_its_pid`'s.
///
/// Mutant: an unproved pid is not a member, so the round lists a third time and the error and
/// listing-count assertions fail.
#[skuld::test]
fn wait_drained_raw_answers_for_a_live_member_that_refuses_the_query_open() {
    use windows::Win32::Foundation::ERROR_GEN_FAILURE;

    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(wait_drained_raw_answers_for_a_live_member_that_refuses_the_query_open),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let refused = crate::identity::windows_fixture::spawn_unkillable(&done);
    let job = super::JobHandle::create_empty_for_test();
    job.assign_for_test(refused.handle().0)
        .expect("assign the refusing member to the job");
    let forced =
        super::fault::force_job_pid_lists_then_failure([vec![refused.pid()], vec![refused.pid()]], ERROR_GEN_FAILURE.0);

    let verdict = super::wait_drained_raw(&job, None);

    assert_eq!(
        super::fault::job_pid_list_queries(),
        2,
        "answered by the settling listing, before any further one: {verdict:?}"
    );
    drop(forced);
    let error = verdict.expect_err("a live member that cannot be waited for is not a drained tree");
    assert!(
        matches!(&error, crate::error::Error::Unassessable { detail, .. }
            if detail.contains(&refused.pid().to_string())),
        "{error:?}"
    );
}

/// A pid unproved on the first look (forced) that the second look proves a member refusing only the
/// wait: `Unassessable` naming it, with the second look's cause (`ACCESS_DENIED`), on a last round as
/// on any other (as the first look answers it).
///
/// Mutant: the recheck treats that `Err` as an unproved pid, or drops it; or a last round's second
/// look skips the wait open and answers `MembersRemain`.
#[skuld::test]
fn wait_drained_raw_names_a_pid_the_second_look_proves_a_member_that_refuses_the_wait() {
    use std::error::Error as _;
    use windows::Win32::Foundation::{ERROR_ACCESS_DENIED, ERROR_GEN_FAILURE};

    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(
            wait_drained_raw_names_a_pid_the_second_look_proves_a_member_that_refuses_the_wait
        ),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let member = crate::identity::windows_fixture::spawn_query_only_without_console(&done);
    let job = super::JobHandle::create_empty_for_test();
    job.assign_for_test(member.handle().0)
        .expect("assign the restricted member to the job");
    for deadline in [None, Some(std::time::Instant::now())] {
        let first_look =
            super::fault::force_next_for_thread(super::fault::Step::Membership, ERROR_GEN_FAILURE.0, member.pid());
        let forced = super::fault::force_job_pid_lists([vec![member.pid()], vec![member.pid()]]);

        let verdict = super::wait_drained_raw(&job, deadline);

        assert_eq!(
            super::fault::job_pid_list_queries(),
            2,
            "answered by the second look, before any further listing (deadline {deadline:?})"
        );
        drop(forced);
        drop(first_look);
        let error = verdict.expect_err("a member that cannot be waited for is not a drained tree");
        assert!(
            matches!(&error, crate::error::Error::Unassessable { detail, source: Some(_) }
                if detail.contains(&member.pid().to_string())),
            "deadline {deadline:?}: {error:?}"
        );
        assert_eq!(
            crate::error::first_os_code(error.source().expect("the OS error is the cause")),
            Some(ERROR_ACCESS_DENIED.0 as i32),
            "deadline {deadline:?}: {error:?}"
        );
    }
}

/// A pid unproved on the first look (forced) that the second look proves a member the caller may open
/// is watched by the round, not closed for a third listing.
///
/// Mutant: the recheck closes a proved member and answers `Round::List`.
#[skuld::test]
fn drain_round_watches_a_pid_the_second_look_proves_a_member() {
    use windows::Win32::Foundation::ERROR_GEN_FAILURE;

    let (child, job) = spawn_job_member();
    let pid = child.id();
    let first_look = super::fault::force_next_for_thread(super::fault::Step::Membership, ERROR_GEN_FAILURE.0, pid);
    let forced = super::fault::force_job_pid_lists([vec![pid], vec![pid]]);

    let round = super::drain_round(job.raw, false);

    assert_eq!(
        super::fault::job_pid_list_queries(),
        2,
        "the listing, and the one that settles it"
    );
    drop(forced);
    drop(first_look);
    let watched = matches!(&round, Ok(super::Round::Watch(watch)) if watch.handles.len() == 1);
    drop(round);
    job.hard_kill().expect("end the member");
    assert!(watched, "the proved member is watched");
}

/// The first wait open of a listed pid is refused (as another process's hold on the number would have
/// it) while a member the caller can wait on holds it now: the round watches that member, with the
/// handle of the retry made while the membership proof holds the pid, and does not answer for the
/// first open's refusal.
///
/// Mutant: the first open's refusal is the member's, returned at once.
#[skuld::test]
fn a_refused_wait_open_of_a_pid_a_waitable_member_holds_now_is_that_member() {
    use windows::Win32::Foundation::ERROR_ACCESS_DENIED;

    let (child, job) = spawn_job_member();
    let refused = super::fault::force_next_for_thread(super::fault::Step::WaitOpen, ERROR_ACCESS_DENIED.0, child.id());

    let round = super::drain_round(job.raw, false);

    drop(refused);
    let watched = matches!(&round, Ok(super::Round::Watch(watch)) if watch.handles.len() == 1);
    drop(round);
    job.hard_kill().expect("end the member");
    assert!(watched, "a member the caller can wait on is watched");
}

/// The pids a listing left unproved, each with a refusal, for [`super::settle_unproved`].
fn unproved_pids(pids: &[u32]) -> Vec<(u32, std::io::Error)> {
    pids.iter()
        .map(|pid| (*pid, std::io::Error::from_raw_os_error(5)))
        .collect()
}

/// A failed settling listing is its own `Err`, with its own cause.
///
/// Mutant: it is read as an empty listing.
#[skuld::test]
fn settle_unproved_reports_a_failed_listing() {
    use windows::Win32::Foundation::ERROR_NOT_ENOUGH_MEMORY;

    let job = super::JobHandle::create_empty_for_test();
    let forced = super::fault::force_job_pid_lists_then_failure([], ERROR_NOT_ENOUGH_MEMORY.0);

    let outcome = super::settle_unproved(job.raw, &unproved_pids(&[5]));

    drop(forced);
    let Err(cause) = outcome else {
        panic!("the failed listing is reported");
    };
    assert_eq!(
        cause.raw_os_error(),
        Some(ERROR_NOT_ENOUGH_MEMORY.0 as i32),
        "{cause:?}"
    );
}

/// An empty settling listing is `Break`: the caller must not read it as a listing that names no
/// unproved pid, because it is the drain's confirmation.
///
/// Mutant: it is `Continue` with no pids.
#[skuld::test]
fn settle_unproved_reports_an_empty_listing() {
    use std::ops::ControlFlow;

    let job = super::JobHandle::create_empty_for_test();
    let forced = super::fault::force_job_pid_list(Vec::new());

    let outcome = super::settle_unproved(job.raw, &unproved_pids(&[5]));

    drop(forced);
    assert!(matches!(outcome, Ok(ControlFlow::Break(()))));
}

/// The settling listing keeps the unproved pids it names again, in order, and drops the others (a
/// deleted member's pid, which it no longer names); a pid only it names is not one to look at.
///
/// Mutant: every unproved pid is kept, or every listed pid is.
#[skuld::test]
fn settle_unproved_keeps_only_the_unproved_pids_the_listing_names_again() {
    let job = super::JobHandle::create_empty_for_test();
    let forced = super::fault::force_job_pid_list(vec![9, 7, 5]);

    let outcome = super::settle_unproved(job.raw, &unproved_pids(&[5, 6, 7]));

    drop(forced);
    let Ok(std::ops::ControlFlow::Continue(named)) = outcome else {
        panic!("a non-empty listing names pids");
    };
    assert_eq!(named, vec![5, 7]);
}

/// A non-empty settling listing that names none of the unproved pids is `Continue` with none.
///
/// Mutant: it is `Break`, which the drain confirms as drained.
#[skuld::test]
fn settle_unproved_reports_a_listing_that_names_none_of_them() {
    let job = super::JobHandle::create_empty_for_test();
    let forced = super::fault::force_job_pid_list(vec![9]);

    let outcome = super::settle_unproved(job.raw, &unproved_pids(&[5]));

    drop(forced);
    assert!(matches!(outcome, Ok(std::ops::ControlFlow::Continue(named)) if named.is_empty()));
}

/// The round's own settling listing, when it is empty, is confirmed at once, on any round, with no
/// listing after it.
///
/// Mutant: the round answers `Round::List`, which lists a third time.
#[skuld::test]
fn drain_round_confirms_an_empty_settling_listing() {
    use windows::Win32::Foundation::ERROR_GEN_FAILURE;

    let job = super::JobHandle::create_empty_for_test();
    let pid = std::process::id();
    for last_round in [false, true] {
        let first_look = super::fault::force_next_for_thread(super::fault::Step::Membership, ERROR_GEN_FAILURE.0, pid);
        let forced = super::fault::force_job_pid_lists([vec![pid], Vec::new()]);

        let round = super::drain_round(job.raw, last_round);

        assert_eq!(
            super::fault::job_pid_list_queries(),
            2,
            "the listing, and the one that settles it (last round: {last_round})"
        );
        drop(forced);
        drop(first_look);
        assert!(matches!(round, Ok(super::Round::Confirm)), "last round: {last_round}");
    }
}

/// A last round whose settling listing names the unproved pid again, still unprovable, answers
/// `Unassessable` naming it: not `MembersRemain`.
///
/// Mutant: the last-round answer is `MembersRemain` before the pid is looked at again.
#[skuld::test]
fn drain_round_names_an_unproved_pid_the_last_rounds_settling_listing_names_again() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(
            drain_round_names_an_unproved_pid_the_last_rounds_settling_listing_names_again
        ),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let refused = crate::identity::windows_fixture::spawn_unkillable(&done);
    let job = super::JobHandle::create_empty_for_test();
    let forced = super::fault::force_job_pid_lists([vec![refused.pid()], vec![refused.pid()]]);

    let round = super::drain_round(job.raw, true);

    assert_eq!(super::fault::job_pid_list_queries(), 2);
    drop(forced);
    assert!(
        matches!(&round, Err(crate::error::Error::Unassessable { detail, source: Some(_) })
            if detail.contains(&refused.pid().to_string())),
        "{:?}",
        round.map(|_| ())
    );
}

/// A failed settling listing fails the round with its own error.
///
/// Mutant: the unproved pids are dropped as if the listing were empty.
#[skuld::test]
fn drain_round_fails_when_the_settling_listing_fails() {
    use windows::Win32::Foundation::{ERROR_GEN_FAILURE, ERROR_NOT_ENOUGH_MEMORY};

    let job = super::JobHandle::create_empty_for_test();
    let pid = std::process::id();
    let first_look = super::fault::force_next_for_thread(super::fault::Step::Membership, ERROR_GEN_FAILURE.0, pid);
    let forced = super::fault::force_job_pid_lists_then_failure([vec![pid]], ERROR_NOT_ENOUGH_MEMORY.0);

    let round = super::drain_round(job.raw, false);

    drop(forced);
    drop(first_look);
    let Err(crate::error::Error::Io(cause)) = round else {
        panic!("the failed listing fails the round");
    };
    assert_eq!(
        cause.raw_os_error(),
        Some(ERROR_NOT_ENOUGH_MEMORY.0 as i32),
        "{cause:?}"
    );
}

/// A pid unproved on the first look and named again, which the second look proves is not in the job
/// (its number was reused), is dropped: the round lists again.
///
/// Mutant: the pid is an error, or the round watches it.
#[skuld::test]
fn drain_round_drops_a_pid_the_second_look_proves_not_a_member() {
    use windows::Win32::Foundation::ERROR_GEN_FAILURE;

    let job = super::JobHandle::create_empty_for_test();
    let pid = std::process::id();
    let first_look = super::fault::force_next_for_thread(super::fault::Step::Membership, ERROR_GEN_FAILURE.0, pid);
    let forced = super::fault::force_job_pid_lists([vec![pid], vec![pid]]);

    let round = super::drain_round(job.raw, false);

    assert_eq!(super::fault::job_pid_list_queries(), 2);
    drop(forced);
    drop(first_look);
    assert!(matches!(round, Ok(super::Round::List)));
}

/// The kill's listing: a pid unproved on the first look that the second listing names again and the
/// second look proves a member is one of the members the kill's wait waits for.
///
/// Mutant: the second look's member is dropped.
#[skuld::test]
fn list_members_names_a_pid_the_second_look_proves_a_member() {
    use windows::Win32::Foundation::ERROR_GEN_FAILURE;

    let (child, job) = spawn_job_member();
    let pid = child.id();
    let first_look = super::fault::force_next_for_thread(super::fault::Step::Membership, ERROR_GEN_FAILURE.0, pid);
    let forced = super::fault::force_job_pid_lists([vec![pid], vec![pid]]);

    let (members, unobserved) = job.list_members(None);

    drop(forced);
    drop(first_look);
    job.hard_kill().expect("end the member");
    assert_eq!(members.iter().filter(|id| id.pid() == pid).count(), 1, "{members:?}");
    assert!(unobserved.is_empty(), "{unobserved:?}");
}

/// The kill's listing: a pid the second look proves not in the job is neither a member nor
/// unobserved.
///
/// Mutant: it is returned as unobserved.
#[skuld::test]
fn list_members_drops_a_pid_the_second_look_proves_not_a_member() {
    use windows::Win32::Foundation::ERROR_GEN_FAILURE;

    let job = super::JobHandle::create_empty_for_test();
    let pid = std::process::id();
    let first_look = super::fault::force_next_for_thread(super::fault::Step::Membership, ERROR_GEN_FAILURE.0, pid);
    let forced = super::fault::force_job_pid_lists([vec![pid], vec![pid]]);

    let (members, unobserved) = job.list_members(None);

    drop(forced);
    drop(first_look);
    assert!(members.is_empty(), "{members:?}");
    assert!(unobserved.is_empty(), "{unobserved:?}");
}

/// The kill's listing: a pid the second look proves a member whose start time cannot be read is
/// unobserved, with its pid.
///
/// Mutant: the identity error of the second look is dropped.
#[skuld::test]
fn list_members_reports_a_member_the_second_look_cannot_identify() {
    use windows::Win32::Foundation::{ERROR_ACCESS_DENIED, ERROR_GEN_FAILURE};

    let (child, job) = spawn_job_member();
    let pid = child.id();
    let identity = super::fault::force_next_for_thread(super::fault::Step::Identity, ERROR_ACCESS_DENIED.0, pid);
    let first_look = super::fault::force_next_for_thread(super::fault::Step::Membership, ERROR_GEN_FAILURE.0, pid);
    let forced = super::fault::force_job_pid_lists([vec![pid], vec![pid]]);

    let (members, unobserved) = job.list_members(None);

    drop(forced);
    drop(first_look);
    drop(identity);
    job.hard_kill().expect("end the member");
    assert!(members.is_empty(), "{members:?}");
    assert_eq!(
        unobserved.iter().filter(|unnamed| unnamed.pid == Some(pid)).count(),
        1,
        "{unobserved:?}"
    );
}

/// A kill's wait names a member it may not wait on once: the edge's confirmation of the empty listing
/// does not report a member the wait for the members already did.
///
/// Mutant: the edge is waited for with no memory of the members the kill's wait settled, so the
/// confirmation names the same pid a second time.
#[skuld::test]
fn a_kill_wait_names_a_member_it_may_not_wait_on_once() {
    use windows::Win32::Foundation::ERROR_ACCESS_DENIED;

    use crate::containment::dispatch::TreeKillOutcome;
    use crate::containment::drain_edge::DrainEdge;
    use crate::containment::TreeKill;

    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(a_kill_wait_names_a_member_it_may_not_wait_on_once),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    let member = crate::identity::windows_fixture::spawn_query_only_without_console(&done);
    let id = crate::identity::windows_identity_from_handle_result(
        &crate::identity::HostKernel,
        member.handle(),
        member.pid(),
    )
    .expect("the restricted member's identity");
    let job = super::JobHandle::create_empty_for_test();
    job.assign_for_test(member.handle().0)
        .expect("assign the restricted member to the job");
    let forced = super::fault::force_job_pid_list(Vec::new());
    let (_, wait) = TreeKill::listed(TreeKillOutcome::Ran(Ok(())), vec![id], Vec::new(), DrainEdge::Job(&job)).split();

    let verdict = wait.wait();

    drop(forced);
    let error = verdict.expect_err("a member whose exit cannot be observed is not a drain");
    let report = crate::error::Report(&error).to_string();
    assert_eq!(
        report.matches(&format!("pid {}", member.pid())).count(),
        1,
        "the member is named once: {report}"
    );
    assert!(
        report.contains("was killed, but its exit cannot be observed"),
        "the kill's wait names it: {report}"
    );
    assert_eq!(
        crate::error::first_os_code(&error),
        Some(ERROR_ACCESS_DENIED.0 as i32),
        "{report}"
    );
}

/// A root whose wait failed (it refuses the wait's open) is reported once, though it is still in the
/// job's snapshot: the edge's confirmation does not name it again.
///
/// Mutant: a failed root wait leaves the root's identity out of what the kill's wait settled.
#[skuld::test]
fn a_kill_wait_reports_a_root_whose_wait_failed_once() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(a_kill_wait_reports_a_root_whose_wait_failed_once),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    assert_the_failed_root_is_the_one_error(&done, false);
}

/// [`a_kill_wait_reports_a_root_whose_wait_failed_once`] for a root whose start time the snapshot cannot
/// read (a forced failure): it is settled by its pid.
///
/// Mutant: a failed root wait leaves the root's pid out of what the kill's wait reported.
#[skuld::test]
fn a_kill_wait_reports_a_root_whose_wait_failed_once_when_the_snapshot_cannot_read_its_identity() {
    let Some(done) = crate::test_own_process::own_process(
        crate::test_own_process::test_path!(
            a_kill_wait_reports_a_root_whose_wait_failed_once_when_the_snapshot_cannot_read_its_identity
        ),
        crate::test_spawn::spawn,
    ) else {
        return;
    };
    assert_the_failed_root_is_the_one_error(&done, true);
}

fn assert_the_failed_root_is_the_one_error(done: &crate::test_own_process::Completion, unreadable: bool) {
    use windows::Win32::Foundation::ERROR_ACCESS_DENIED;

    use crate::containment::dispatch::TreeKillOutcome;
    use crate::containment::drain_edge::DrainEdge;
    use crate::containment::TreeKill;
    use crate::error::Error;

    let member = crate::identity::windows_fixture::spawn_query_only_without_console(done);
    let id = crate::identity::windows_identity_from_handle_result(
        &crate::identity::HostKernel,
        member.handle(),
        member.pid(),
    )
    .expect("the restricted member's identity");
    let root = crate::os_process::windows::WindowsProcess::open(crate::identity::HostKernel, id)
        .expect("open the restricted member")
        .expect("a live process opens with QUERY alone");
    let job = super::JobHandle::create_empty_for_test();
    job.assign_for_test(member.handle().0)
        .expect("assign the restricted member to the job");
    let _failed = unreadable.then(|| {
        super::fault::force_next_for_thread(super::fault::Step::Identity, ERROR_ACCESS_DENIED.0, member.pid())
    });
    let forced = super::fault::force_job_pid_list(Vec::new());
    let (_, wait) = TreeKill::listed(
        TreeKillOutcome::Ran(Ok(())),
        Vec::new(),
        Vec::new(),
        DrainEdge::Job(&job),
    )
    .split();

    let verdict = wait.with_root(&root, member.pid(), true).wait();

    drop(forced);
    let error = verdict.expect_err("a root whose exit cannot be observed is not a drain");
    assert!(
        matches!(&error, Error::Unassessable { detail, source: Some(source) }
            if detail.starts_with("the root was killed") && !matches!(&**source, Error::Several(_))),
        "the root is the one error: {error:?}"
    );
}

/// An empty listing is the drain only once the members the snapshot names are signalled: the list
/// drops a member before its process object signals. The second member is alive, in the job, when
/// the second listing (forced) comes back empty; the wait must block on it, and the hook ends it
/// when the wait arms, so the drain is answered only after its exit.
///
/// Mutant: an empty listing answers `AllMembersExited` without waiting for the snapshot's members.
#[skuld::test]
fn wait_drained_raw_waits_for_the_members_the_snapshot_names_when_the_listing_empties() {
    use std::cell::Cell;
    use std::rc::Rc;
    use std::time::{Duration, Instant};

    let (mut first, mut second, job) = spawn_job_with_two_members();
    let stdin = second.stdin.take().expect("piped stdin");
    first.kill().expect("end the first member");
    first.wait().expect("reap the first member");
    let forced = super::fault::force_job_pid_lists([vec![first.id(), second.id()], Vec::new()]);
    crate::wait::wait_ms_probe::take();
    let released = Rc::new(Cell::new(false));
    crate::wait::wait_ms_probe::on_second_arm({
        let released = released.clone();
        move || {
            released.set(true);
            drop(stdin); // EOF: the second member exits
        }
    });

    let deadline = Instant::now() + Duration::from_secs(3600);
    let verdict = super::wait_drained_raw(&job, Some(deadline));

    crate::wait::wait_ms_probe::take();
    drop(forced);
    let ended = released.get();
    assert!(
        ended,
        "the drain was answered by the empty listing while a member's process object was unsignalled"
    );
    assert_eq!(
        verdict.expect("wait_drained_raw"),
        crate::containment::TreeDrain::AllMembersExited
    );
}

/// A member that left the listing in an earlier round is still waited for when a later listing is
/// empty. Lists: both members, then the signalled first one alone (the second left the list while
/// unsignalled), then none. The second member stays alive, so the bounded wait ends in
/// `MembersRemain` at its deadline rather than answering the drain off the empty listing.
///
/// Mutant: an empty listing is answered off the earlier rounds' handles, not the snapshot.
#[skuld::test]
fn wait_drained_raw_keeps_waiting_for_a_member_that_left_the_listing_in_an_earlier_round() {
    use std::time::Duration;

    let (mut first, second, job) = spawn_job_with_two_members();
    first.kill().expect("end the first member");
    first.wait().expect("reap the first member");
    let forced = super::fault::force_job_pid_lists([vec![first.id(), second.id()], vec![first.id()], Vec::new()]);
    let deadline = crate::wait::deadline_from(Duration::from_millis(50));

    let verdict = super::wait_drained_raw(&job, deadline);

    drop(forced);
    assert_eq!(
        verdict.expect("wait_drained_raw"),
        crate::containment::TreeDrain::MembersRemain,
        "the drain was answered while a member's process object was unsignalled"
    );
}

/// A round past the deadline whose final listing is empty polls the members the snapshot names, and
/// answers `MembersRemain` for one that is still running.
///
/// Mutant: the final empty listing answers `AllMembersExited` without looking at the snapshot.
#[skuld::test]
fn wait_drained_raw_with_an_elapsed_deadline_polls_the_members_its_empty_final_listing_leaves() {
    let (mut first, second, job) = spawn_job_with_two_members();
    first.kill().expect("end the first member");
    first.wait().expect("reap the first member");
    let forced = super::fault::force_job_pid_lists([vec![first.id(), second.id()], Vec::new()]);
    let zero = crate::wait::deadline_from(std::time::Duration::ZERO);

    let verdict = super::wait_drained_raw(&job, zero);

    assert_eq!(super::fault::job_pid_list_queries(), 2, "the listing, and one more");
    drop(forced);
    assert_eq!(
        verdict.expect("wait_drained_raw"),
        crate::containment::TreeDrain::MembersRemain,
        "the drain was answered while a member's process object was unsignalled"
    );
}

/// A real, still-running job member reports `MembersRemain` at an already-elapsed deadline —
/// not a race: the child is blocked reading its own piped stdin, which this test still holds
/// open at the point of the check, so the member's liveness there is a fact this test itself
/// holds, not a guess about timing. Also exercises `query_job_pid_list`'s non-empty branch (the
/// pid must be visible in the job before `wait_drained_raw` can find it to wait on at all), and
/// — once the child is let go and reaped — the live re-enumeration that turns a real exit into
/// `AllMembersExited`, synchronized on `Child::wait()` rather than on any elapsed time.
#[skuld::test]
fn wait_drained_raw_tracks_a_real_member_through_exit() {
    use std::os::windows::io::AsRawHandle;

    // `cmd /C more`: a binary present on every Windows host, which blocks reading its stdin
    // until EOF, then exits. No new external dependency — this is the OS shell.
    let mut child = crate::test_spawn::spawn(
        std::process::Command::new("cmd")
            .args(["/C", "more"])
            .stdin(std::process::Stdio::piped())
            .stdout(std::process::Stdio::null())
            .stderr(std::process::Stdio::null()),
    )
    .expect("spawn cmd /C more");
    let raw = child.as_raw_handle();

    let job = super::assign_to_kill_on_close_job(raw).expect("assign to job");

    let pids = super::query_job_pid_list(job.raw).expect("query pid list");
    assert_eq!(
        pids,
        vec![child.id()],
        "the job must report exactly the member just assigned"
    );

    // An already-elapsed deadline: `crate::wait::remaining` reads it as Duration::ZERO without
    // blocking at all, so this assertion is instantaneous — the point under test is that a
    // non-empty live member set reports MembersRemain rather than a guessed drain verdict.
    let past = std::time::Instant::now() - std::time::Duration::from_secs(1);
    let verdict = super::wait_drained_raw(&job, Some(past)).expect("wait_drained_raw");
    assert_eq!(verdict, crate::containment::TreeDrain::MembersRemain);

    // Let the child exit on its own terms (EOF on stdin), then confirm the job reports drained
    // once it genuinely has. `Child::wait()` only returns once the OS reports the process gone
    // — a real synchronization point, not a timing guess.
    drop(child.stdin.take());
    child.wait().expect("wait for cmd /C more to exit");

    let verdict = super::wait_drained_raw(&job, None).expect("wait_drained_raw");
    assert_eq!(verdict, crate::containment::TreeDrain::AllMembersExited);
}

// Deadline contract for `wait_drained_raw`: never report `MembersRemain` before the real
// deadline. Seams are documented in `crate::wait`.

/// A never-draining job armed with a sub-millisecond remainder rounds up to 1ms, reports
/// `MembersRemain` only at the deadline, and derives its argument from the site's own deadline.
///
/// Mutant: truncate in `win32_timeout_ms` -> `ms` is 0. Mutant: add slack -> `ms` is above 1.
/// Mutant: ignore the site's deadline -> `requested` is not 5ms.
#[skuld::test]
fn wait_drained_raw_arms_the_ceiling_of_the_remaining_duration() {
    use std::time::{Duration, Instant};
    let (_child, job) = spawn_job_member();
    let (_clock, at) = crate::wait::test_clock::FrozenClockGuard::install();
    crate::wait::wait_ms_probe::take();
    let _override = crate::wait::remaining_override_seam::set(Duration::from_micros(500));
    let deadline = at + Duration::from_millis(5);
    let verdict = super::wait_drained_raw(&job, Some(deadline));
    let reached = Instant::now() >= deadline;
    let arms = crate::wait::wait_ms_probe::take();
    assert_eq!(
        verdict.expect("a live never-exiting member must not report a wait failure"),
        crate::containment::TreeDrain::MembersRemain
    );
    assert!(reached, "the call returned before the real deadline");
    let first = arms.first().expect("at least one armed wait");
    assert_eq!(first.remaining, Duration::from_micros(500));
    assert_eq!(first.ms, 1);
    assert_eq!(
        first.requested,
        Duration::from_millis(5),
        "the site must pass the time left to its own deadline (clock frozen at the deadline's origin)"
    );
}

/// An early, unclamped `WAIT_TIMEOUT` hours before the deadline is not trusted: the site
/// re-arms and reports the real drain.
///
/// Mutant: return `MembersRemain` on the first `WAIT_TIMEOUT` -> one arm, wrong verdict.
#[skuld::test]
fn wait_drained_raw_never_reports_members_remain_before_the_deadline() {
    use std::time::{Duration, Instant};
    let (mut child, job) = spawn_job_member();
    let stdin = child.stdin.take().expect("piped stdin");
    crate::wait::wait_ms_probe::take();
    let _override = crate::wait::remaining_override_seam::set(Duration::from_micros(500));
    crate::wait::wait_ms_probe::on_second_arm(move || drop(stdin)); // EOF -> member exits for real
    let deadline = Instant::now() + Duration::from_secs(3600);
    let verdict = super::wait_drained_raw(&job, Some(deadline));
    let arms = crate::wait::wait_ms_probe::take();
    assert_eq!(
        verdict.expect("a genuinely-drained job must not report a wait failure"),
        crate::containment::TreeDrain::AllMembersExited
    );
    assert!(arms.len() >= 2, "expected a re-arm, got {} arm(s)", arms.len());
}

/// A wait clamped below the deadline re-arms, recomputing `remaining` each round.
///
/// Mutant: return `MembersRemain` on the first `WAIT_TIMEOUT` -> one arm, wrong verdict.
/// Mutant: hoist `remaining` above the loop -> `remaining` does not shrink.
#[skuld::test]
fn wait_drained_raw_re_arms_past_a_clamped_timeout() {
    use std::time::{Duration, Instant};
    let (mut child, job) = spawn_job_member();
    let stdin = child.stdin.take().expect("piped stdin");
    crate::wait::wait_ms_probe::take();
    let _clamp = crate::wait::wait_clamp_seam::set(5);
    crate::wait::wait_ms_probe::on_second_arm(move || drop(stdin));
    let deadline = Instant::now() + Duration::from_secs(3600);
    let verdict = super::wait_drained_raw(&job, Some(deadline));
    let arms = crate::wait::wait_ms_probe::take();
    assert_eq!(
        verdict.expect("a genuinely-drained job must not report a wait failure"),
        crate::containment::TreeDrain::AllMembersExited
    );
    crate::wait::wait_ms_probe::assert_rearmed_with_fresh_remaining(&arms, 5);
}

/// Live coverage of the `Ok(true)` arm against a real console. `Ok(false)` needs the DETACHED
/// helper in the integration suite (a different process); `Err` is not provokable live, hence
/// the fault seam exercised below. This test does NOT guard the integration test against
/// vacuity — that binary carries its own `console=0` / `console=1` assertions, measured inside
/// the helper itself.
#[skuld::test]
fn caller_has_console_is_true_under_cargo_test() {
    assert!(
        matches!(super::caller_has_console(), Ok(true)),
        "the test runner is expected to run with a console attached"
    );
}

/// The `Err` arm's production, via the fault seam — a live `GetConsoleProcessList` cannot be
/// made to fail. This exercises the fault-injection scaffolding, not a real API failure; the
/// consumption side is covered by `invalid_handle_with_a_failed_probe_stays_io`.
#[skuld::test]
fn console_probe_error_surfaces() {
    super::fault::set_force_console_probe_error(true);
    assert!(super::caller_has_console().is_err());
    assert!(!super::fault::armed(), "the seam must be consumed by one call");
    // The very next call is a real probe again.
    assert!(matches!(super::caller_has_console(), Ok(true)));
}

/// `ERROR_INVALID_HANDLE`, the bare Win32 code every `io::Error` cosca returns carries.
const ERROR_INVALID_HANDLE: i32 = 6;
/// `ERROR_ACCESS_DENIED` — a different failure, which must NOT be classified as a missing console.
const ERROR_ACCESS_DENIED: i32 = 5;

fn io_err(code: i32) -> std::io::Error {
    std::io::Error::from_raw_os_error(code)
}

#[skuld::test]
fn invalid_handle_with_no_console_is_typed_no_console() {
    let e = super::classify_ctrl_event_failure(4242, io_err(ERROR_INVALID_HANDLE), Ok(false));
    let crate::error::Error::NoConsole { detail } = e else {
        panic!("expected NoConsole, got {e:?}");
    };
    assert!(detail.contains("4242"), "the detail must name the group: {detail}");
    assert!(
        detail.contains("kill_tree()"),
        "the detail must name the way out: {detail}"
    );
}

#[skuld::test]
fn invalid_handle_with_a_console_attached_stays_io() {
    // The probe contradicts the code: never claim a cause we just measured to be false.
    let e = super::classify_ctrl_event_failure(1, io_err(ERROR_INVALID_HANDLE), Ok(true));
    assert!(matches!(e, crate::error::Error::Io(_)), "got {e:?}");
}

/// The error `GenerateConsoleCtrlEvent` returns when the caller has no console: what the `windows`
/// crate builds from the thread's last error.
fn no_console_failure() -> windows::core::Error {
    // SAFETY: setting the calling thread's last error has no preconditions.
    unsafe {
        windows::Win32::Foundation::SetLastError(windows::Win32::Foundation::ERROR_INVALID_HANDLE);
    }
    windows::core::Error::from_thread()
}

/// `terminate` classifies the OS's own failure: `ERROR_INVALID_HANDLE` from the call, with the
/// probe confirming no console, is `NoConsole`. This is the check that the converted error matches
/// the classifier's code.
///
/// Mutant: `terminate_with` converts with `.into()`, so the code is `0x80070006`, the classifier
/// does not recognise it, and the result is `Io`.
#[skuld::test]
fn terminate_classifies_a_real_no_console_failure() {
    let e = super::terminate_with(4242, || Err(no_console_failure()), || Ok(false)).expect_err("the send failed");
    assert!(matches!(e, crate::error::Error::NoConsole { .. }), "got {e:?}");
}

#[skuld::test]
fn invalid_handle_with_a_failed_probe_stays_io() {
    let probe = Err(std::io::Error::from_raw_os_error(87)); // ERROR_INVALID_PARAMETER
    let e = super::classify_ctrl_event_failure(1, io_err(ERROR_INVALID_HANDLE), probe);
    assert!(matches!(e, crate::error::Error::Io(_)), "got {e:?}");
}

#[skuld::test]
fn a_different_failure_code_stays_io_whatever_the_probe_says() {
    for console in [Ok(true), Ok(false)] {
        let e = super::classify_ctrl_event_failure(1, io_err(ERROR_ACCESS_DENIED), console);
        assert!(matches!(e, crate::error::Error::Io(_)), "got {e:?}");
    }
    let e = super::classify_ctrl_event_failure(1, io_err(ERROR_ACCESS_DENIED), Err(io_err(87)));
    assert!(matches!(e, crate::error::Error::Io(_)), "got {e:?}");
}

// The console facts the creation-flag word settles, one test per row. Nine inputs, three distinct
// outcomes: no constant implementation passes, and the last three rows pin that `own_console_flags` is
// a statement about a console of its own for a group that EXISTS, not a synonym for "detached" — with no
// group flag there is nothing to address, whatever the console situation. The probe's answer is the
// caller's: it passes through whatever the word says.
mod console_facts_tests {
    use crate::command::flags::flags_tests::{contained, uncontained, word};
    use crate::command::flags::{FlagsRequest, SpawnBackend};
    use crate::containment::windows::ConsoleFacts;
    use windows::Win32::System::Threading::{CREATE_NEW_CONSOLE, CREATE_NO_WINDOW, DETACHED_PROCESS};

    /// The word of an uncontained spawn asked for a group: the group bit alone.
    fn group_flags() -> u32 {
        let group = FlagsRequest {
            process_group: Some(true),
            ..Default::default()
        };
        word(&uncontained(), group, false, SpawnBackend::Std)
    }

    /// The word of a contained root: the group bit and the suspend.
    fn root_flags() -> u32 {
        word(&contained(), FlagsRequest::default(), true, SpawnBackend::Std)
    }

    fn facts(word: u32) -> (bool, bool) {
        let facts = ConsoleFacts::of(word, true);
        assert!(facts.caller_had_console && !facts.elevated, "{facts:?}");
        (facts.group, facts.own_console_flags)
    }

    #[skuld::test]
    fn console_facts_report_no_group_for_no_flags() {
        assert_eq!(facts(0), (false, false));
    }

    #[skuld::test]
    fn console_facts_report_a_group_on_our_console_for_group_flags() {
        assert_eq!(facts(group_flags()), (true, false));
    }

    #[skuld::test]
    fn console_facts_report_a_group_on_our_console_for_root_flags() {
        assert_eq!(facts(root_flags()), (true, false));
    }

    #[skuld::test]
    fn console_facts_report_a_group_in_a_console_of_its_own_for_group_plus_detached() {
        assert_eq!(facts(group_flags() | DETACHED_PROCESS.0), (true, true));
    }

    #[skuld::test]
    fn console_facts_report_a_group_in_a_console_of_its_own_for_group_plus_new_console() {
        assert_eq!(facts(group_flags() | CREATE_NEW_CONSOLE.0), (true, true));
    }

    #[skuld::test]
    fn console_facts_report_a_group_in_a_console_of_its_own_for_group_plus_no_window() {
        assert_eq!(facts(group_flags() | CREATE_NO_WINDOW.0), (true, true));
    }

    #[skuld::test]
    fn console_facts_report_no_group_for_detached_without_a_group() {
        assert_eq!(facts(DETACHED_PROCESS.0), (false, true));
    }

    #[skuld::test]
    fn console_facts_report_no_group_for_a_new_console_without_a_group() {
        assert_eq!(facts(CREATE_NEW_CONSOLE.0), (false, true));
    }

    #[skuld::test]
    fn console_facts_report_no_group_for_no_window_without_a_group() {
        assert_eq!(facts(CREATE_NO_WINDOW.0), (false, true));
    }

    #[skuld::test]
    fn console_facts_carry_the_callers_console_as_given() {
        assert!(!ConsoleFacts::of(group_flags(), false).caller_had_console);
        assert!(ConsoleFacts::of(group_flags(), true).caller_had_console);
    }

    /// A probe that failed is recorded as no console, whatever the word says of the rest, and logged.
    ///
    /// Mutants: a failed probe is recorded as a console; the rest of the facts are dropped with it.
    #[skuld::test]
    fn console_facts_record_a_failed_probe_as_no_console() {
        crate::log_capture::install();
        let mark = crate::log_capture::mark();
        let failed = ConsoleFacts::after_probe(group_flags(), Err(std::io::Error::other("forced probe failure")));
        assert_eq!(
            failed,
            ConsoleFacts {
                caller_had_console: false,
                ..ConsoleFacts::of(group_flags(), true)
            }
        );
        assert!(failed.group && !failed.own_console_flags, "{failed:?}");
        let logged = crate::log_capture::records_since_on_current_thread(mark, "the console probe failed");
        assert!(
            matches!(&logged[..], [(log::Level::Debug, said)] if said.contains("forced probe failure")),
            "{logged:?}"
        );
        assert_eq!(
            ConsoleFacts::after_probe(group_flags(), Ok(true)),
            ConsoleFacts::of(group_flags(), true)
        );
    }
}

// ===== job breakaway probe =====

/// The `Unknown` arm exists so cosca never asserts a cause it could not measure. Neither Win32
/// call in the probe can be made to fail on a live system, so the seam is the only route to that
/// arm's production — and without the seam the probe reports a real verdict, which is what makes
/// this assertion about the arm rather than about the constant.
#[skuld::test]
fn probe_reports_unknown_when_the_query_fails() {
    use crate::containment::windows::{fault, probe_job_breakaway, JobBreakaway};
    assert_ne!(
        probe_job_breakaway(),
        JobBreakaway::Unknown,
        "unarmed, the probe must reach a real verdict"
    );
    fault::set_force_job_probe_error(true);
    assert_eq!(probe_job_breakaway(), JobBreakaway::Unknown);
    assert_ne!(
        probe_job_breakaway(),
        JobBreakaway::Unknown,
        "take semantics: the fault applies to exactly one call"
    );
}

/// The env var that tells [`fixture_reports_job_breakaway_probe`] it was re-exec'd deliberately
/// by [`probe_agrees_with_an_independent_is_process_in_job_measurement`], rather than picked up
/// by an ordinary, unfiltered suite run.
const JOB_BREAKAWAY_PROBE_FIXTURE_MARKER: &str = "COSCA_FIXTURE_JOB_BREAKAWAY_PROBE";

/// A RELATIONSHIP, never an absolute: whether CI runs test processes inside a job object is not
/// ours to control. It fails if the probe's first branch is inverted, which is the bug that would
/// make every ambient job read as "no job" and silently disable the typed containment error.
///
/// The measurement itself runs in [`fixture_reports_job_breakaway_probe`], a freshly spawned
/// re-exec of this binary — never in this (top-level) test process. Under nextest, this process
/// is spawned and only ASSIGNED to its job object afterward: nextest's own job-assignment call
/// trails the spawn rather than preceding or blocking on it. Two reads taken directly in this
/// process, moments apart, could straddle that assignment and disagree with each other even
/// though no real job is actually racing. A freshly spawned child inherits its parent's job
/// membership atomically at `CreateProcess`, and nothing assigns it to a job afterward, so both
/// reads taken INSIDE it always agree with each other, whichever way the ambient membership
/// happens to fall.
#[skuld::test]
fn probe_agrees_with_an_independent_is_process_in_job_measurement() {
    let exe = std::env::current_exe().expect("current_exe");
    let fixture = crate::test_child::fixture_path!(fixture_reports_job_breakaway_probe);
    let child = crate::test_spawn::spawn(
        crate::test_reexec::with_json_events(
            crate::test_reexec::command(&exe).args(crate::test_reexec::fixture_args(fixture)),
        )
        .env(JOB_BREAKAWAY_PROBE_FIXTURE_MARKER, "1")
        .stdout(std::process::Stdio::piped())
        .stderr(std::process::Stdio::piped()),
    )
    .expect("spawn the job-breakaway probe fixture");
    let output = child.wait_with_output().expect("wait for the fixture child");
    let stdout = String::from_utf8_lossy(&output.stdout);
    let stderr = String::from_utf8_lossy(&output.stderr);
    if let Err(why) = crate::test_reexec::suite_passed_exactly_one(&output) {
        panic!(
            "job-breakaway probe fixture did not run and pass exactly one test: {why}\n--- stdout ---\n{stdout}\n\
             --- stderr ---\n{stderr}"
        );
    }
}

/// The child half of [`probe_agrees_with_an_independent_is_process_in_job_measurement`]: a no-op
/// when picked up by an ordinary, unfiltered suite run ([`JOB_BREAKAWAY_PROBE_FIXTURE_MARKER`] is
/// unset there). Re-executed via `current_exe() --exact` with that var set, it performs the real
/// measurement — see the driver's own doc for why it must run here, in a freshly spawned process,
/// rather than in the driver itself.
#[skuld::test]
fn fixture_reports_job_breakaway_probe() {
    if std::env::var_os(JOB_BREAKAWAY_PROBE_FIXTURE_MARKER).is_none() {
        return; // picked up by an ordinary suite run — deliberately inert
    }
    use crate::containment::windows::{probe_job_breakaway, JobBreakaway};
    use windows::Win32::System::JobObjects::IsProcessInJob;
    use windows::Win32::System::Threading::GetCurrentProcess;

    let mut in_job = windows::core::BOOL(0);
    // SAFETY: standard Win32; `in_job` is a valid out-param and `None` asks "in ANY job".
    unsafe { IsProcessInJob(GetCurrentProcess(), None, &mut in_job) }.expect("IsProcessInJob");

    let verdict = probe_job_breakaway();
    if in_job.as_bool() {
        assert_ne!(
            verdict,
            JobBreakaway::NotInJob,
            "this process IS in a job, so the probe must not report otherwise"
        );
    } else {
        assert_eq!(
            verdict,
            JobBreakaway::NotInJob,
            "this process is in no job, so the probe must say so"
        );
    }
}

// ===== initial-thread resume ownership =====

/// A suspended process this test made itself, holding its main thread. It sits in a
/// kill-on-close job, so the OS kills it however the test process exits; `RawChild`'s `Drop` kills
/// it sooner.
struct SuspendedHelper {
    child: crate::test_spawn::raw_spawn::RawChild,
    _job: super::JobHandle,
}

impl SuspendedHelper {
    fn new() -> Self {
        use crate::test_spawn::console_flags::Console;
        use crate::test_spawn::raw_spawn::{spawn_locked_raw_with, RawOptions, RawStdio};
        use std::os::windows::io::AsRawHandle as _;
        use windows::Win32::System::Threading::CREATE_SUSPENDED;
        let options = RawOptions {
            stdio: RawStdio::Null,
            ..Default::default()
        };
        let child = spawn_locked_raw_with(
            "cmd",
            &["/C", "more"],
            &[],
            CREATE_SUSPENDED.0,
            Console::Hidden,
            options,
        )
        .expect("spawn the suspended helper");
        let job =
            super::assign_to_kill_on_close_job(child.process().as_raw_handle()).expect("assign the helper to a job");
        Self { child, _job: job }
    }
}

/// Spawn a contained blocker. Whatever is injected or forced on this thread applies to it.
fn spawn_contained_blocker() -> Result<crate::Child, crate::error::Error> {
    let mut cmd = crate::test_child::command(Console::Hidden);
    cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
    cmd.stdin(crate::Stdio::pipe()).expect("set stdin pipe");
    cmd.stdout(crate::Stdio::null()).expect("set stdout");
    cmd.contain();
    cmd.spawn()
}

/// Spawn with an extra snapshot entry for `tid`, owned by `owner` (`None`: the child).
/// Returns the spawn result and what the walk did with the entry.
fn spawn_with_injected_tid(
    tid: u32,
    owner: Option<u32>,
) -> (
    Result<crate::Child, crate::error::Error>,
    Option<Option<crate::containment::windows::Visit>>,
) {
    let injected = crate::containment::windows::fault::inject_snapshot_tid(tid, owner);
    let spawned = spawn_contained_blocker();
    let visit = crate::containment::windows::fault::take_injected_visit();
    drop(injected);
    (spawned, visit)
}

fn finish_child(spawned: Result<crate::Child, crate::error::Error>) {
    let mut child = spawned.expect("a stale snapshot entry must not fail the spawn");
    assert_eq!(
        child.containment(),
        crate::Containment::JobObject,
        "the walk under test only runs for a Job Object spawn"
    );
    drop(child.stdin().expect("piped stdin"));
    child.wait().expect("wait for the contained child");
}

/// A thread id the snapshot lists under the child (owner = the child's pid) but that belongs to
/// another process (a reused id) must not be resumed, and must not fail the spawn. The helper's
/// main thread has suspend count 1; suspending it again returns the previous count, so 1 proves
/// nothing resumed it and 0 proves something did.
#[skuld::test]
fn windows_resume_initial_threads_never_resumes_a_foreign_thread() {
    use crate::containment::windows::Visit;
    use std::os::windows::io::AsRawHandle as _;
    use windows::Win32::System::Threading::{ResumeThread, SuspendThread};

    let helper = SuspendedHelper::new();
    let thread = windows::Win32::Foundation::HANDLE(helper.child.thread().as_raw_handle());
    let (spawned, visit) = spawn_with_injected_tid(helper.child.thread_id(), None);
    assert!(
        spawned.is_ok(),
        "a snapshot entry for another process's thread must not fail the spawn: {:?}",
        spawned.as_ref().err()
    );
    assert_eq!(
        visit,
        Some(Some(Visit::Foreign {
            owner: helper.child.id()
        })),
        "the injected entry must be walked and identified as the helper's"
    );

    // SAFETY: `thread` is the helper's live thread handle with suspend rights.
    let previous = unsafe { SuspendThread(thread) };
    assert_eq!(
        previous, 1,
        "the helper's thread is another process's: the spawn must not have resumed it"
    );
    // SAFETY: as above; restore the count this test added.
    unsafe { ResumeThread(thread) };

    finish_child(spawned);
}

/// The owner filter drops a snapshot entry listed under another pid before any thread is opened.
#[skuld::test]
fn windows_resume_initial_threads_drops_an_entry_owned_by_another_pid() {
    use std::os::windows::io::AsRawHandle as _;
    use windows::Win32::System::Threading::{ResumeThread, SuspendThread};

    let helper = SuspendedHelper::new();
    let thread = windows::Win32::Foundation::HANDLE(helper.child.thread().as_raw_handle());
    let (spawned, visit) = spawn_with_injected_tid(helper.child.thread_id(), Some(helper.child.id()));
    assert_eq!(visit, Some(None), "an entry owned by another pid must be filtered out");
    // SAFETY: `thread` is the helper's live thread handle with suspend rights.
    let previous = unsafe { SuspendThread(thread) };
    assert_eq!(previous, 1, "a filtered entry must not be touched");
    // SAFETY: as above.
    unsafe { ResumeThread(thread) };
    finish_child(spawned);
}

/// A listed thread that exited and whose id was not reused makes `OpenThread` fail with
/// `ERROR_INVALID_PARAMETER`, and must not fail the spawn. The kernel masks an id's low two bits
/// before the lookup, so id 1 resolves to CID 0, which is never allocated to any thread or
/// process: the lookup always fails, whatever ids are live or reused.
#[skuld::test]
fn windows_resume_initial_threads_skips_a_thread_id_that_no_longer_exists() {
    use crate::containment::windows::Visit;

    let (spawned, visit) = spawn_with_injected_tid(1, None);
    assert!(
        spawned.is_ok(),
        "a snapshot entry for a nonexistent thread must not fail the spawn: {:?}",
        spawned.as_ref().err()
    );
    assert_eq!(
        visit,
        Some(Some(Visit::Gone)),
        "the entry must be walked and found gone"
    );
    finish_child(spawned);
}

/// `ERROR_ACCESS_DENIED` on the query-only open marks the id as someone else's: the child's own
/// threads are openable by its creator. Forced onto the child's first thread, that leaves nothing
/// resumed, and the spawn fails with the stale-walk error rather than an open failure.
#[skuld::test]
fn windows_resume_initial_threads_treats_a_denied_query_open_as_stale() {
    use crate::containment::windows::fault::{force_next, Step};
    const ERROR_ACCESS_DENIED: u32 = 5;

    let _forced = force_next(Step::QueryOpen, ERROR_ACCESS_DENIED);
    let spawned = spawn_contained_blocker();
    assert!(spawned.is_err(), "no thread was resumed, so the spawn must fail");
    let text = format!("{:?}", spawned.err());
    assert!(
        text.contains("was stale") && text.contains("access denied"),
        "a denied query open must be reported as a stale skip: {text}"
    );
}

/// Spawn with `step` forced to fail on the child's first thread; the spawn must fail with an
/// error naming `call`, the thread and the pid.
fn assert_forced_failure_fails_the_spawn(step: crate::containment::windows::fault::Step, code: u32, call: &str) {
    let _forced = crate::containment::windows::fault::force_next(step, code);
    let spawned = spawn_contained_blocker();
    assert!(
        spawned.is_err(),
        "a failure on the child's own thread must fail the spawn"
    );
    let text = format!("{:?}", spawned.err());
    assert!(
        text.contains(&format!("{call} failed for thread")) && text.contains("of pid"),
        "the error must name the call, thread and pid ({call}): {text}"
    );
    assert!(!text.contains("was stale"), "a failure is not a stale skip: {text}");
}

#[skuld::test]
fn windows_resume_initial_threads_fails_the_spawn_when_the_query_open_fails() {
    use crate::containment::windows::fault::Step;
    const ERROR_NOT_ENOUGH_MEMORY: u32 = 8;
    assert_forced_failure_fails_the_spawn(Step::QueryOpen, ERROR_NOT_ENOUGH_MEMORY, "OpenThread(query)");
}

/// A denied suspend/resume open of a thread that IS the child's is a failure, not a skip.
#[skuld::test]
fn windows_resume_initial_threads_fails_the_spawn_when_the_resume_open_is_denied() {
    use crate::containment::windows::fault::Step;
    const ERROR_ACCESS_DENIED: u32 = 5;
    assert_forced_failure_fails_the_spawn(Step::ResumeOpen, ERROR_ACCESS_DENIED, "OpenThread(suspend/resume)");
}

#[skuld::test]
fn windows_resume_initial_threads_fails_the_spawn_when_the_owner_is_unreadable() {
    use crate::containment::windows::fault::Step;
    const ERROR_INVALID_HANDLE: u32 = 6;
    assert_forced_failure_fails_the_spawn(Step::Owner, ERROR_INVALID_HANDLE, "GetProcessIdOfThread");
}

#[skuld::test]
fn windows_resume_initial_threads_fails_the_spawn_when_resume_thread_fails() {
    use crate::containment::windows::fault::Step;
    const ERROR_INVALID_HANDLE: u32 = 6;
    assert_forced_failure_fails_the_spawn(Step::Resume, ERROR_INVALID_HANDLE, "ResumeThread");
}

/// The walk keeps the FIRST failure: it is the root cause, and later ones only follow from it.
#[skuld::test]
fn walk_verdict_returns_the_first_failure() {
    use super::{walk_verdict, Walk};
    let mut walk = Walk::default();
    walk.fail("ResumeThread", 1, 9, "first cause");
    walk.fail("ResumeThread", 2, 9, "second cause");
    let text = walk_verdict(9, walk).expect_err("a failure").to_string();
    assert!(text.contains("thread 1") && text.contains("first cause"), "{text}");
    assert!(
        !text.contains("second cause"),
        "the later failure replaced the first: {text}"
    );
}

/// A failed Win32 call on a thread reaches the caller with its plain Win32 code and the kind it
/// classifies to, and the message still names the call, the thread and the pid.
///
/// Mutant: `fail_win32` builds `io::Error::other(msg)`, so the code is lost and the kind is `Other`.
#[skuld::test]
fn fail_win32_keeps_the_win32_code_and_kind() {
    use super::{walk_verdict, Walk};
    use windows::core::{Error, HRESULT};
    let mut walk = Walk::default();
    let cause = Error::from_hresult(HRESULT::from_win32(ERROR_ACCESS_DENIED as u32));
    walk.fail_win32("ResumeThread", 1, 9, cause);
    let err = walk_verdict(9, walk).expect_err("a failure");
    assert_eq!(err.kind(), std::io::ErrorKind::PermissionDenied, "{err:?}");
    assert_eq!(err.raw_os_error(), None, "{err:?}");
    assert_eq!(crate::error::first_os_code(&err), Some(ERROR_ACCESS_DENIED), "{err:?}");
    assert_eq!(err.to_string(), "ResumeThread failed for thread 1 of pid 9");
}

/// A tie in creation times is cosca's finding, not a failed call: no OS code.
#[skuld::test]
fn fail_unidentified_has_no_os_code() {
    use super::{walk_verdict, Walk};
    let mut walk = Walk::default();
    walk.fail_unidentified(7, 12, 34);
    let err = walk_verdict(7, walk).expect_err("a failure");
    assert_eq!(err.raw_os_error(), None, "{err:?}");
    assert_eq!(crate::error::first_os_code(&err), None, "{err:?}");
}

/// When every listed thread was stale the walk fails, and the error names the last skip.
#[skuld::test]
fn walk_verdict_with_only_skips_names_the_last_skip() {
    use super::{walk_verdict, Walk};
    let mut walk = Walk::default();
    walk.skip("thread 7 no longer exists".to_owned());
    let text = walk_verdict(4321, walk).expect_err("nothing was resumed").to_string();
    assert!(
        text.contains("4321") && text.contains("thread 7 no longer exists"),
        "{text}"
    );
    let text = walk_verdict(4321, Walk::default())
        .expect_err("nothing listed")
        .to_string();
    assert!(text.contains("listed no threads"), "{text}");
}

/// The tie error is one single-spaced sentence naming the pid and both threads.
#[skuld::test]
fn fail_unidentified_reports_the_whole_sentence() {
    use super::{walk_verdict, Walk};
    let mut walk = Walk::default();
    walk.fail_unidentified(7, 12, 34);
    let text = walk_verdict(7, walk).expect_err("a failure").to_string();
    assert_eq!(
        text,
        "the initial thread of pid 7 cannot be identified: threads 12 and 34 have the same creation time"
    );
}

/// Every thread the snapshot listed for the child reads back as gone: the identification finds no
/// initial thread, and the walk's error says the listed threads were stale, not that none were listed.
/// An odd id is never a thread id (they are multiples of 4), so each of these is gone.
#[skuld::test]
fn initial_thread_id_records_a_skip_for_each_stale_thread() {
    use super::{initial_thread_id, walk_verdict, Walk};
    let mut walk = Walk::default();
    let found = initial_thread_id(7, &[0xFFFF_FFFF, 0xFFFF_FFFD], &mut walk);
    assert_eq!(
        found, None,
        "no listed thread is readable, so none is the initial thread"
    );
    let text = walk_verdict(7, walk).expect_err("nothing was resumed").to_string();
    assert!(
        text.contains("every thread the snapshot listed for pid 7 was stale")
            && text.contains("thread 4294967293")
            && !text.contains("listed no threads"),
        "the error must say the listed threads were stale: {text}"
    );
}

#[skuld::test]
fn walk_verdict_accepts_a_walk_that_resumed_a_thread() {
    use super::{walk_verdict, Walk};
    let mut walk = Walk {
        resumed: 1,
        ..Walk::default()
    };
    walk.skip("thread 7 no longer exists".to_owned());
    walk_verdict(4321, walk).expect("a resumed thread with stale siblings is a success");
}

/// `GetProcessId` answers 0 for a handle that is not a process.
#[skuld::test]
fn process_pid_of_rejects_a_handle_that_names_no_process() {
    let result = super::process_pid_of(windows::Win32::Foundation::HANDLE::default());
    assert!(result.is_err(), "a null handle names no process: {result:?}");
    let err = result.unwrap_err();
    assert!(
        err.to_string().contains("GetProcessId"),
        "error must name the call: {err}"
    );
}

/// `GetProcessId` failing reaches the caller with its Win32 code and the kind that code classifies
/// to, and the message names the call: a null handle is `ERROR_INVALID_HANDLE`.
///
/// Mutant: `process_pid_of` builds `io::Error::other(format!(..))`, losing the code and the kind.
#[skuld::test]
fn process_pid_of_failure_carries_the_win32_code() {
    let err = super::process_pid_of(windows::Win32::Foundation::HANDLE::default()).expect_err("a null handle");
    assert_eq!(err.raw_os_error(), None, "{err:?}");
    assert_eq!(crate::error::first_os_code(&err), Some(ERROR_INVALID_HANDLE), "{err:?}");
    assert_eq!(
        err.kind(),
        std::io::Error::from_raw_os_error(ERROR_INVALID_HANDLE).kind(),
        "{err:?}"
    );
    assert_eq!(err.to_string(), "GetProcessId failed on the child's handle");
}

/// A handle that names no process breaks the caller's contract. Debug builds assert it.
#[cfg(debug_assertions)]
#[skuld::test]
#[should_panic(expected = "live process handle")]
fn resume_initial_threads_asserts_its_handle_contract_in_debug() {
    _ = super::resume_initial_threads(std::ptr::null_mut(), super::SuspendCount::Any);
}

/// Release builds return the error instead. Runs in the release lane.
#[cfg(not(debug_assertions))]
#[skuld::test]
fn resume_initial_threads_reports_a_broken_handle_contract_in_release() {
    let err = super::resume_initial_threads(std::ptr::null_mut(), super::SuspendCount::Any)
        .expect_err("a null handle names no process");
    assert!(err.to_string().contains("GetProcessId"), "{err}");
}

// ===== frozen release =====

/// `resume_initial_threads` brings a frozen child's initial thread from suspend count 1 to 0: it runs.
#[skuld::test]
fn resume_initial_threads_leaves_no_thread_suspended() {
    let mut child = crate::test_frozen::spawn_suspended_blocker();
    let thread = crate::test_frozen::InitialThread::of(child.id());
    assert_eq!(thread.suspend_count(), 1, "the child is created suspended");

    super::resume_initial_threads(crate::test_frozen::raw(&child), super::SuspendCount::ExactlyOne)
        .expect("resume a frozen child");

    assert_eq!(thread.suspend_count(), 0, "the resumed child's initial thread must run");
    drop(child.stdin.take());
    child.wait().expect("the resumed blocker exits on stdin EOF");
}

/// Something suspended the initial thread a second time during the window (a debugger, an
/// EDR agent): one resume leaves it suspended, and `Ok` there would hand out a frozen process.
#[skuld::test]
fn resume_initial_threads_fails_when_the_initial_thread_was_suspended_twice() {
    let child = crate::test_frozen::spawn_suspended_blocker();
    let thread = crate::test_frozen::InitialThread::of(child.id());
    thread.suspend_once();

    let err = super::resume_initial_threads(crate::test_frozen::raw(&child), super::SuspendCount::ExactlyOne)
        .expect_err("the thread is still suspended");

    let text = err.to_string();
    assert!(
        text.contains("previous suspend count was 2"),
        "the error must name the count it found: {text}"
    );
    assert_eq!(
        thread.suspend_count(),
        1,
        "the walk resumed once; the foreign suspend is still held, and the failure says so"
    );
}

/// Something already ran the child (its count was 0): "ran no instruction" no longer holds, so a
/// frozen child's resume must not report a clean success.
#[skuld::test]
fn resume_initial_threads_fails_when_the_initial_thread_was_already_running() {
    let child = crate::test_frozen::spawn_suspended_blocker();
    let thread = crate::test_frozen::InitialThread::of(child.id());
    assert_eq!(thread.release_one(), 1);

    let err = super::resume_initial_threads(crate::test_frozen::raw(&child), super::SuspendCount::ExactlyOne)
        .expect_err("the child already ran");

    let text = err.to_string();
    assert!(
        text.contains("previous suspend count was 0"),
        "the error must name the count it found: {text}"
    );
}

/// Another party (an EDR agent, a debugger) started a thread in the frozen child, and it runs: the
/// child is still as its creator left it, because the INITIAL thread is suspended exactly once.
#[skuld::test]
fn resume_initial_threads_accepts_a_running_thread_someone_else_started() {
    let mut child = crate::test_frozen::spawn_suspended_blocker();
    let initial = crate::test_frozen::InitialThread::of(child.id());
    let _injected = crate::test_frozen::inject_running_thread_into_std_child(&child);

    super::resume_initial_threads(crate::test_frozen::raw(&child), super::SuspendCount::ExactlyOne)
        .expect("a running second thread must not fail the release of a child whose initial thread is frozen");

    assert_eq!(initial.suspend_count(), 0, "the initial thread must run");
    drop(child.stdin.take());
    child.wait().expect("the resumed blocker exits on stdin EOF");
}

/// A thread another party holds suspended is theirs: the frozen release resumes the initial thread,
/// the only one its creator suspended, and leaves the other at the count it had.
#[skuld::test]
fn resume_initial_threads_leaves_a_thread_someone_else_suspended_suspended() {
    let mut child = crate::test_frozen::spawn_suspended_blocker();
    let initial = crate::test_frozen::InitialThread::of(child.id());
    let held = crate::test_frozen::inject_suspended_thread_into_std_child(&child);

    super::resume_initial_threads(crate::test_frozen::raw(&child), super::SuspendCount::ExactlyOne)
        .expect("the initial thread is frozen exactly once");

    assert_eq!(initial.suspend_count(), 0, "the initial thread must run");
    assert_eq!(
        held.suspend_count(),
        1,
        "the other party's suspension must still be held after the release"
    );
    drop(child.stdin.take());
    child.wait().expect("the resumed blocker exits on stdin EOF");
}

/// The release's resume walk visits only the initial thread: another thread of the child is read
/// for its creation time, but the walk's own `open_query` (`Step::QueryOpen`) is never made for it.
/// The failure is armed for that thread alone, and the walk must leave it unconsumed.
#[skuld::test]
fn resume_initial_threads_visits_only_the_initial_thread() {
    use crate::containment::windows::fault::{force_next_for_thread, forced_is_armed, Step};
    const ERROR_NOT_ENOUGH_MEMORY: u32 = 8;

    let mut child = crate::test_frozen::spawn_suspended_blocker();
    let initial = crate::test_frozen::InitialThread::of(child.id());
    let other = crate::test_frozen::inject_running_thread_into_std_child(&child);
    let _forced = force_next_for_thread(Step::QueryOpen, ERROR_NOT_ENOUGH_MEMORY, other.tid());

    super::resume_initial_threads(crate::test_frozen::raw(&child), super::SuspendCount::ExactlyOne)
        .expect("the resume walk's open_query must not be made for a thread that is not the initial one");

    assert!(forced_is_armed(), "the walk opened the other thread");
    assert_eq!(initial.suspend_count(), 0, "the initial thread must run");
    drop(child.stdin.take());
    child.wait().expect("the resumed blocker exits on stdin EOF");
}

/// A child thread whose creation time cannot be read might be the earlier one, so the initial thread
/// is not identifiable: the release fails, and resumes nothing.
#[skuld::test]
fn resume_initial_threads_resumes_nothing_when_a_childs_creation_time_cannot_be_read() {
    use crate::containment::windows::fault::{force_next_for_thread, Step};
    const ERROR_NOT_ENOUGH_MEMORY: u32 = 8;

    let child = crate::test_frozen::spawn_suspended_blocker();
    let initial = crate::test_frozen::InitialThread::of(child.id());
    let other = crate::test_frozen::inject_running_thread_into_std_child(&child);
    let _forced = force_next_for_thread(Step::CreationTime, ERROR_NOT_ENOUGH_MEMORY, other.tid());

    let err = super::resume_initial_threads(crate::test_frozen::raw(&child), super::SuspendCount::ExactlyOne)
        .expect_err("the initial thread cannot be identified");

    let text = err.to_string();
    assert!(
        text.contains("GetThreadTimes failed") && text.contains(&format!("thread {} of pid", other.tid())),
        "the error must name the call and the unreadable thread: {text}"
    );
    assert_eq!(
        initial.suspend_count(),
        1,
        "nothing may be resumed when the initial thread is unknown"
    );
}

/// Two threads created at the same clock reading: which came first is unknowable (thread ids carry
/// no order), so the release fails and resumes nothing rather than guessing.
#[skuld::test]
fn resume_initial_threads_resumes_nothing_when_the_earliest_creation_times_are_equal() {
    let child = crate::test_frozen::spawn_suspended_blocker();
    let initial = crate::test_frozen::InitialThread::of(child.id());
    let other = crate::test_frozen::inject_running_thread_into_std_child(&child);
    let _equal = crate::containment::windows::fault::equal_creation_times();

    let err = super::resume_initial_threads(crate::test_frozen::raw(&child), super::SuspendCount::ExactlyOne)
        .expect_err("the initial thread cannot be identified");

    // The listing order of the two threads is the snapshot's, so either order is the sentence.
    let (a, b) = (initial.tid(), other.tid());
    let sentence = |first: u32, second: u32| {
        format!(
            "the initial thread of pid {} cannot be identified: threads {first} and {second} have the same creation time",
            child.id()
        )
    };
    let text = err.to_string();
    assert!(
        text == sentence(a, b) || text == sentence(b, a),
        "the error must say why the initial thread is unknown, in single-spaced words: {text}"
    );
    assert_eq!(
        initial.suspend_count(),
        1,
        "nothing may be resumed when the initial thread is unknown"
    );
}

/// `earliest_created`: the earliest time wins wherever it is listed; a tie for the earliest is a
/// tie, whichever ids hold it; a tie among later times is irrelevant.
#[skuld::test]
fn earliest_created_answers_the_unique_earliest_and_reports_a_tie_for_it() {
    use super::Earliest;

    assert_eq!(super::earliest_created([]), Earliest::NoThread);
    assert_eq!(super::earliest_created([(7, 1)]), Earliest::Unique(1));
    assert_eq!(super::earliest_created([(9, 1), (7, 2), (8, 3)]), Earliest::Unique(2));
    assert_eq!(super::earliest_created([(7, 5), (7, 2), (8, 3)]), Earliest::Tie(5, 2));
    assert_eq!(super::earliest_created([(8, 5), (8, 2), (7, 3)]), Earliest::Unique(3));
}

/// The ordinary spawn's walk is unchanged: `Any` resumes every thread of the child, as `contain()`
/// always has.
#[skuld::test]
fn resume_initial_threads_with_any_count_resumes_every_thread() {
    let mut child = crate::test_frozen::spawn_suspended_blocker();
    let initial = crate::test_frozen::InitialThread::of(child.id());
    let second = crate::test_frozen::inject_suspended_thread_into_std_child(&child);

    super::resume_initial_threads(crate::test_frozen::raw(&child), super::SuspendCount::Any)
        .expect("resume every thread");

    assert_eq!(initial.suspend_count(), 0, "the initial thread must run");
    assert_eq!(second.suspend_count(), 0, "`Any` resumes the second thread too");
    drop(child.stdin.take());
    child.wait().expect("the resumed blocker exits on stdin EOF");
}

/// The initial thread ran while a second thread exists: still a failure, and it names the initial
/// thread, not the other one.
#[skuld::test]
fn resume_initial_threads_names_the_initial_thread_when_it_was_already_running() {
    let child = crate::test_frozen::spawn_suspended_blocker();
    let initial = crate::test_frozen::InitialThread::of(child.id());
    assert_eq!(initial.release_one(), 1);
    let _injected = crate::test_frozen::inject_running_thread_into_std_child(&child);

    let err = super::resume_initial_threads(crate::test_frozen::raw(&child), super::SuspendCount::ExactlyOne)
        .expect_err("the initial thread already ran");

    let text = err.to_string();
    assert!(
        text.contains(&format!("thread {} of pid", initial.tid())) && text.contains("previous suspend count was 0"),
        "the error must name the initial thread and its count: {text}"
    );
}

/// The initial thread was suspended twice while a second thread exists: still a failure naming the
/// initial thread.
#[skuld::test]
fn resume_initial_threads_names_the_initial_thread_when_it_was_suspended_twice() {
    let child = crate::test_frozen::spawn_suspended_blocker();
    let initial = crate::test_frozen::InitialThread::of(child.id());
    initial.suspend_once();
    let _injected = crate::test_frozen::inject_running_thread_into_std_child(&child);

    let err = super::resume_initial_threads(crate::test_frozen::raw(&child), super::SuspendCount::ExactlyOne)
        .expect_err("the initial thread is still suspended");

    let text = err.to_string();
    assert!(
        text.contains(&format!("thread {} of pid", initial.tid())) && text.contains("previous suspend count was 2"),
        "the error must name the initial thread and its count: {text}"
    );
}

/// The initial thread is the earliest created, wherever the snapshot lists it: a list that puts the
/// later thread first must not make that one the initial.
#[skuld::test]
fn initial_thread_id_picks_the_earliest_created_thread_whatever_the_list_order() {
    let child = crate::test_frozen::spawn_suspended_blocker();
    let initial = crate::test_frozen::InitialThread::of(child.id());
    let injected = crate::test_frozen::inject_running_thread_into_std_child(&child);

    for tids in [[initial.tid(), injected.tid()], [injected.tid(), initial.tid()]] {
        let mut walk = super::Walk::default();
        let picked = super::initial_thread_id(child.id(), &tids, &mut walk);
        assert_eq!(picked, Some(initial.tid()), "list {tids:?}");
        assert!(walk.last_err.is_none(), "{:?}", walk.last_err);
    }
}

/// A listed thread that is another process's, or gone, is nobody's initial thread, even when it is
/// older than the child's own.
#[skuld::test]
fn initial_thread_id_ignores_a_thread_that_is_not_the_childs() {
    let older = crate::test_frozen::spawn_suspended_blocker();
    let older_thread = crate::test_frozen::InitialThread::of(older.id());
    let child = crate::test_frozen::spawn_suspended_blocker();
    let initial = crate::test_frozen::InitialThread::of(child.id());

    // Thread id 1 resolves to CID 0, never allocated: the lookup always fails.
    let mut walk = super::Walk::default();
    let picked = super::initial_thread_id(child.id(), &[older_thread.tid(), 1, initial.tid()], &mut walk);

    assert_eq!(picked, Some(initial.tid()));
    assert!(
        walk.last_err.is_none(),
        "a foreign or vanished thread is a skip: {:?}",
        walk.last_err
    );
}

/// The ordinary spawn's resume does not ask that the child was suspended exactly once: a thread
/// something else already ran (a debugger's break-in thread, an EDR agent's) must not fail a
/// `contain()` spawn that has always accepted it.
#[skuld::test]
fn resume_initial_threads_with_any_count_accepts_a_thread_that_was_already_running() {
    let child = crate::test_frozen::spawn_suspended_blocker();
    let thread = crate::test_frozen::InitialThread::of(child.id());
    assert_eq!(thread.release_one(), 1);

    super::resume_initial_threads(crate::test_frozen::raw(&child), super::SuspendCount::Any)
        .expect("the ordinary resume accepts a running thread");
}

/// The ordinary `contain()` attach, end to end: the same tolerance as above, through `attach_job`.
#[skuld::test]
fn death_watch_attach_job_succeeds_when_the_initial_thread_was_already_running() {
    let mut child = crate::test_frozen::spawn_suspended_releasable();
    let thread = crate::test_frozen::InitialThread::of(child.id());
    assert_eq!(thread.release_one(), 1);

    let job = super::attach_job(crate::test_frozen::raw(&child), true)
        .expect("an ordinary attach must not fail on a running thread")
        .expect("the job was assigned");

    // KILL_ON_JOB_CLOSE ends the child. `wait` closes its stdin, so a close that killed nothing
    // lets it run to its end.
    drop(job);
    crate::test_frozen::assert_not_released("the child", child.wait().expect("reap the child"));
}

/// A frozen child whose initial thread cannot be resumed fails the attach with that failure as the
/// cause. The job's kill after it, which fails too, is the attach's cleanup: the cause is not
/// changed by it, and it is not in the cause's text.
///
/// Mutants: the kill's failure is only logged; it replaces the resume's as the cause; it is among
/// the cause's `source`s; it is not logged at warn, naming the job and that the tree may still run.
#[skuld::test]
fn attach_job_whose_resume_and_job_kill_both_fail_returns_the_kill_as_cleanup() {
    use super::fault::{force_next, set_before_job_kill, Step};
    const ERROR_ACCESS_DENIED: u32 = 5;
    const ERROR_INVALID_HANDLE: u32 = 6;

    crate::log_capture::install();
    let mark = crate::log_capture::mark();

    let child = crate::test_frozen::spawn_suspended_blocker();
    // One failure is forced at a time: the kill's is armed once the resume's is spent.
    let kill_failure = std::rc::Rc::new(std::cell::RefCell::new(None));
    let _resume = force_next(Step::Resume, ERROR_ACCESS_DENIED);
    let _arms_the_kill = set_before_job_kill({
        let kill_failure = std::rc::Rc::clone(&kill_failure);
        move || *kill_failure.borrow_mut() = Some(force_next(Step::JobKill, ERROR_INVALID_HANDLE))
    });

    let failure = super::attach_job(crate::test_frozen::raw(&child), true).expect_err("the resume failed");

    assert!(
        matches!(failure.cause, crate::error::Error::Containment { .. }),
        "{failure:?}"
    );
    assert_eq!(
        crate::error::first_os_code(&failure.cause),
        Some(ERROR_ACCESS_DENIED as i32),
        "the resume's failure is the cause: {failure:?}"
    );
    let cleanup = failure.cleanup.expect("the job kill's failure is the cleanup");
    assert_eq!(cleanup.iter().len(), 1, "{cleanup:?}");
    assert_eq!(
        crate::error::first_os_code(&**cleanup.first()),
        Some(ERROR_INVALID_HANDLE as i32),
        "{cleanup:?}"
    );
    let warns = crate::log_capture::warns_since(mark, "job kill after a failed resume also failed");
    assert_eq!(warns.len(), 1, "{warns:?}");
    assert!(
        warns[0].contains("the tree may still be running") && warns[0].contains("terminating the job"),
        "{warns:?}"
    );
    assert!(
        crate::error::said(&**cleanup.first()).starts_with(&crate::error::io_said("terminating the job: ")),
        "the cleanup member names its step: {cleanup:?}"
    );
    drop(kill_failure);
}

/// A spawn whose attach fails in its resume, and whose job kill after it fails too, returns the
/// resume as the cause and the kill in the `cleanup` of the error that says the program may have
/// started, beside the child's fate.
///
/// Mutant: the spawn drops the attach's cleanup when it tears the child down.
#[skuld::test]
fn a_spawn_whose_attach_fails_returns_the_job_kill_in_its_cleanup() {
    use super::fault::{force_next, set_before_job_kill, Step};
    const ERROR_ACCESS_DENIED: u32 = 5;
    const ERROR_INVALID_HANDLE: u32 = 6;

    let mut cmd = crate::test_child::command(Console::Hidden);
    cmd.args(crate::test_child::BLOCKER_ARGV.iter().copied());
    cmd.contain();
    let kill_failure = std::rc::Rc::new(std::cell::RefCell::new(None));
    let _resume = force_next(Step::Resume, ERROR_ACCESS_DENIED);
    let _arms_the_kill = set_before_job_kill({
        let kill_failure = std::rc::Rc::clone(&kill_failure);
        move || *kill_failure.borrow_mut() = Some(force_next(Step::JobKill, ERROR_INVALID_HANDLE))
    });

    let error = cmd.spawn().expect_err("the resume failed");

    let crate::error::Error::MayHaveStarted {
        source, cleanup, fate, ..
    } = &error
    else {
        panic!("expected MayHaveStarted, got {error:?}");
    };
    assert_eq!(*fate, crate::error::ChildFate::Reaped, "{error:?}");
    assert!(
        matches!(**source, crate::error::Error::Containment { .. }),
        "{source:?}"
    );
    assert_eq!(
        crate::error::first_os_code(&**source),
        Some(ERROR_ACCESS_DENIED as i32),
        "{source:?}"
    );
    let cleanup = cleanup.as_ref().expect("the job kill's failure is the cleanup");
    assert_eq!(cleanup.iter().len(), 1, "{cleanup:?}");
    assert_eq!(
        crate::error::first_os_code(&**cleanup.first()),
        Some(ERROR_INVALID_HANDLE as i32),
        "{cleanup:?}"
    );
    drop(kill_failure);
}

/// `assign_held` assigns the job and does not resume: the thread stays frozen.
#[skuld::test]
fn death_watch_assign_held_assigns_the_job_without_resuming() {
    let mut child = crate::test_frozen::spawn_suspended_releasable();
    let thread = crate::test_frozen::InitialThread::of(child.id());

    let job = super::assign_held(crate::test_frozen::raw(&child)).expect("assign the held child");

    assert_eq!(thread.suspend_count(), 1, "assign_held must not resume");
    assert_ne!(
        crate::test_frozen::limit_flags(&job).0
            & windows::Win32::System::JobObjects::JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE.0,
        0
    );
    drop(job); // KILL_ON_JOB_CLOSE ends the child
    crate::test_frozen::release(&mut child, &thread);
    crate::test_frozen::assert_not_released("the frozen child", child.wait().expect("reap the frozen child"));
}

// Win32 codes of job failures =====
//
// Each site converts a `windows::core::Error` to an `io::Error`. A bare `.into()` keeps the
// HRESULT (`0x8007xxxx`) where std reports the Win32 code. An event handle is a real handle that
// is not a job, so each job call on it fails with `ERROR_INVALID_HANDLE`: no Job Object is made.

fn event_handle() -> windows::Win32::Foundation::HANDLE {
    // SAFETY: creating an unnamed event has no preconditions.
    unsafe { windows::Win32::System::Threading::CreateEventW(None, true, false, None) }.expect("create an event handle")
}

/// `TerminateJobObject` failing reaches the caller with its Win32 code.
///
/// Mutant: `hard_kill` converts with `.into()`, so the code is `0x80070006`.
#[skuld::test]
fn hard_kill_failure_carries_the_win32_code() {
    let h = super::JobHandle::new(event_handle());
    let err = h.hard_kill().expect_err("an event is not a job");
    assert_eq!(err.raw_os_error(), Some(ERROR_INVALID_HANDLE), "{err:?}");
}

/// A tree kill whose `TerminateJobObject` fails is an `Error::Containment` over the Win32 error: a
/// kill that can have peers returns a variant with a `source`, and the code stays down the chain.
///
/// Mutant: the failed kill is a bare `Error::Io`.
#[skuld::test]
fn a_failed_job_kill_is_a_containment_over_the_win32_error() {
    use crate::containment::Contains as _;
    let h = super::JobHandle::new(event_handle());
    let left = h.send_kill(None).into_result().expect_err("an event is not a job");
    let [error] = &left.members[..] else {
        panic!("{left:?}");
    };
    assert!(matches!(error, crate::error::Error::Containment { .. }), "{error:?}");
    assert_eq!(
        crate::error::first_os_code(error),
        Some(ERROR_INVALID_HANDLE),
        "{error:?}"
    );
}

/// A `CreateJobObjectW` failure carries the plain Win32 code. The call cannot be made to fail, so
/// the failure is injected at `owned_job`.
///
/// Mutant: `owned_job` converts with `.into()`, so the code is `0x80070008`.
#[skuld::test]
fn owned_job_failure_carries_the_win32_code() {
    use windows::core::HRESULT;
    use windows::Win32::Foundation::ERROR_NOT_ENOUGH_MEMORY;
    let failed = windows::core::Error::from_hresult(HRESULT::from_win32(ERROR_NOT_ENOUGH_MEMORY.0));
    let err = super::owned_job(Err(failed)).expect_err("the creation failed");
    assert_eq!(err.raw_os_error(), Some(ERROR_NOT_ENOUGH_MEMORY.0 as i32), "{err:?}");
}

/// `SetInformationJobObject` failing carries the plain Win32 code.
///
/// Mutant: `set_kill_on_close_or_close` converts with `.into()`, so the code is `0x80070006`.
#[skuld::test]
fn set_kill_on_close_failure_carries_the_win32_code() {
    let err = super::set_kill_on_close_or_close(event_handle()).expect_err("an event is not a job");
    assert_eq!(err.raw_os_error(), Some(ERROR_INVALID_HANDLE), "{err:?}");
}

/// `AssignProcessToJobObject` failing carries the plain Win32 code.
///
/// Mutant: `assign_or_close` converts with `.into()`, so the code is `0x80070006`.
#[skuld::test]
fn assign_failure_carries_the_win32_code() {
    // SAFETY: `GetCurrentProcess` returns a pseudo-handle and has no preconditions.
    let me = unsafe { windows::Win32::System::Threading::GetCurrentProcess() };
    let err = super::assign_or_close(event_handle(), me).expect_err("an event is not a job");
    assert_eq!(err.raw_os_error(), Some(ERROR_INVALID_HANDLE), "{err:?}");
}

/// A member check that fails (`IsProcessInJob` on a handle that is not a job) is reported with the
/// plain Win32 code, not dropped.
///
/// Mutant: the `IsProcessInJob` arm of `member_to_watch` converts with `.into()`, so the code is
/// `0x80070006`.
#[skuld::test]
fn member_check_failure_carries_the_win32_code() {
    use windows::Win32::System::Threading::{
        GetCurrentProcessId, OpenProcess, PROCESS_QUERY_LIMITED_INFORMATION, PROCESS_SYNCHRONIZE,
    };
    // SAFETY: opening this process has no preconditions; `member_to_watch` closes the handle.
    let opened = unsafe {
        OpenProcess(
            PROCESS_SYNCHRONIZE | PROCESS_QUERY_LIMITED_INFORMATION,
            false,
            GetCurrentProcessId(),
        )
    };
    let super::Standing::Unproved(err) = super::member_to_watch(opened, event_handle()) else {
        panic!("an event is not a job");
    };
    assert_eq!(err.raw_os_error(), Some(ERROR_INVALID_HANDLE), "{err:?}");
}

/// An `OpenProcess` refusal is reported with the plain Win32 code, while `ERROR_INVALID_PARAMETER`
/// (the process already exited) is not a failure.
///
/// Mutant: the generic `Err(e)` arm of `member_to_watch` converts with `.into()`, so the code is
/// `0x80070005`.
#[skuld::test]
fn denied_open_carries_the_win32_code() {
    use windows::core::HRESULT;
    use windows::Win32::Foundation::{ERROR_ACCESS_DENIED as DENIED, ERROR_INVALID_PARAMETER, HANDLE};
    let refused = |code| Err(windows::core::Error::from_hresult(HRESULT::from_win32(code)));
    let super::Standing::Unproved(err) = super::member_to_watch(refused(DENIED.0), HANDLE::default()) else {
        panic!("a refused open leaves membership unproved");
    };
    assert_eq!(err.raw_os_error(), Some(ERROR_ACCESS_DENIED), "{err:?}");
    let gone = super::member_to_watch(refused(ERROR_INVALID_PARAMETER.0), HANDLE::default());
    assert!(
        matches!(gone, super::Standing::NotMember),
        "an exited pid is not a failure: {gone:?}"
    );
}

/// `Thread32First` failing on a handle that is not a snapshot carries the plain Win32 code.
///
/// Mutant: `thread_entries_closing` converts with `.into()`, so the code is `0x80070006`.
#[skuld::test]
fn thread_walk_failure_carries_the_win32_code() {
    let err = super::thread_entries_closing(event_handle()).expect_err("an event is not a snapshot");
    let code = err.raw_os_error();
    assert_eq!(code, Some(ERROR_INVALID_HANDLE), "ERROR_INVALID_HANDLE expected");
}

/// A `CreateToolhelp32Snapshot` failure carries the plain Win32 code. The call cannot be made to
/// fail, so the failure is injected at `thread_snapshot`.
///
/// Mutant: `thread_snapshot` converts with `.into()`, so the code is `0x80070008`.
#[skuld::test]
fn thread_snapshot_failure_carries_the_win32_code() {
    use windows::core::HRESULT;
    use windows::Win32::Foundation::ERROR_NOT_ENOUGH_MEMORY;
    let failed = windows::core::Error::from_hresult(HRESULT::from_win32(ERROR_NOT_ENOUGH_MEMORY.0));
    let err = super::thread_snapshot(Err(failed)).expect_err("the snapshot failed");
    assert_eq!(err.raw_os_error(), Some(ERROR_NOT_ENOUGH_MEMORY.0 as i32), "{err:?}");
}

/// A `disarm` the OS refuses is returned, with the plain Win32 code, and the flag stays set.
///
/// Mutants: `disarm` logs the failure and returns `Ok`; it converts with `.into()`, so the code is
/// `0x80070005`.
#[skuld::test]
fn a_refused_disarm_is_returned_with_the_win32_code() {
    use super::JobHandle;
    use windows::Win32::Foundation::ERROR_ACCESS_DENIED;

    let job = JobHandle::create_without_set_access_for_test();

    let err = job
        .disarm()
        .expect_err("the OS refuses SetInformationJobObject on this handle");

    assert_eq!(err.raw_os_error(), Some(ERROR_ACCESS_DENIED.0 as i32), "{err:?}");
}

/// A `disarm` on a handle that allows it is `Ok`.
#[skuld::test]
fn disarm_of_an_empty_job_is_ok() {
    super::JobHandle::create_empty_for_test()
        .disarm()
        .expect("a job made by the crate can be disarmed");
}

/// A `disarm` after `hard_kill` clears `KILL_ON_JOB_CLOSE`: the flag the real create path sets.
///
/// Mutant: `disarm` does nothing.
#[skuld::test]
fn disarm_after_hard_kill_clears_the_flag() {
    let (_child, job) = spawn_job_member();
    assert!(
        super::job_kills_on_close(&job),
        "the create path sets KILL_ON_JOB_CLOSE"
    );
    job.hard_kill().expect("hard_kill on a live job");
    job.disarm().expect("disarm after hard_kill");
    assert_eq!(crate::test_frozen::limit_flags(&job).0, 0);
}