computer-use-linux 0.7.52

Native Linux desktop control over MCP and CLI: semantic AT-SPI accessibility trees, multi-compositor window targeting (GNOME, KDE/KWin, Hyprland, niri, COSMIC, i3, Sway, labwc and other wlroots compositors, X11), bounded screenshots, Wayland/X11 input, and an on-screen agent activity indicator.
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
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
use crate::windowing::registry::{
    self, COSMIC_WAYLAND_BACKEND, GNOME_SHELL_EXTENSION_BACKEND, GNOME_SHELL_INTROSPECT_BACKEND,
    HYPRLAND_BACKEND, I3_BACKEND, KWIN_BACKEND, LABWC_BACKEND, NIRI_BACKEND,
    WLR_FOREIGN_TOPLEVEL_BACKEND, X11_BACKEND,
};
use crate::ydotool;
use schemars::JsonSchema;
use serde::Serialize;
use std::{
    collections::{BTreeMap, HashMap},
    env, fs,
    fs::OpenOptions,
    os::unix::fs::MetadataExt,
    path::{Path, PathBuf},
    process::Command,
    sync::Mutex,
};

const DESKTOP_ENV_KEYS: &[&str] = &[
    "DBUS_SESSION_BUS_ADDRESS",
    "DESKTOP_SESSION",
    "DISPLAY",
    "HYPRLAND_INSTANCE_SIGNATURE",
    "NIRI_SOCKET",
    "XAUTHORITY",
    "YDOTOOL_SOCKET",
    "XDG_SESSION_DESKTOP",
    "WAYLAND_DISPLAY",
    "XDG_CURRENT_DESKTOP",
    "XDG_RUNTIME_DIR",
    "XDG_SESSION_TYPE",
];
const FORCE_YDOTOOL_KEYBOARD_ENV_KEYS: &[&str] = &["COMPUTER_USE_LINUX_FORCE_YDOTOOL_KEYBOARD"];
const FORCE_YDOTOOL_POINTER_ENV_KEYS: &[&str] = &["COMPUTER_USE_LINUX_FORCE_YDOTOOL_POINTER"];
const FORCE_XDOTOOL_KEYBOARD_ENV_KEYS: &[&str] = &["COMPUTER_USE_LINUX_FORCE_XDOTOOL_KEYBOARD"];
const FORCE_PORTAL_KEYBOARD_ENV_KEYS: &[&str] = &["COMPUTER_USE_LINUX_FORCE_PORTAL_KEYBOARD"];
const FORCE_PORTAL_POINTER_ENV_KEYS: &[&str] = &["COMPUTER_USE_LINUX_FORCE_PORTAL_POINTER"];
const PORTAL_DEVICE_KEYBOARD: u32 = 1;
const PORTAL_DEVICE_POINTER: u32 = 2;
const PORTAL_SOURCE_MONITOR: u32 = 1;
/// ScreenCast version that added `AvailableCursorModes` and `cursor_mode`.
const SCREENCAST_CURSOR_MODE_VERSION: u32 = 2;
const REMOTE_DESKTOP_KEYBOARD_METHODS: &[&str] = &[
    "CreateSession",
    "SelectDevices",
    "Start",
    "NotifyKeyboardKeycode",
    "NotifyKeyboardKeysym",
];
const REMOTE_DESKTOP_POINTER_METHODS: &[&str] = &[
    "CreateSession",
    "SelectDevices",
    "Start",
    "NotifyPointerMotionAbsolute",
    "NotifyPointerButton",
    "NotifyPointerAxisDiscrete",
];
const SCREENCAST_POINTER_METHODS: &[&str] = &["SelectSources"];
// Methods each standalone portal entry must export before doctor reports it.
// `busctl introspect` exits 0 and prints only its header for an interface the
// portal does not export, so exit status alone proves nothing (issue #156).
const SCREENSHOT_PORTAL_METHODS: &[&str] = &["Screenshot"];
const SCREENCAST_PORTAL_METHODS: &[&str] = &["CreateSession", "SelectSources", "Start"];
const INPUT_CAPTURE_PORTAL_METHODS: &[&str] = &["GetZones", "Enable", "ConnectToEIS"];

#[derive(Debug, Clone, Serialize, JsonSchema)]
pub struct DoctorReport {
    pub platform: PlatformReport,
    pub portals: PortalReport,
    pub accessibility: AccessibilityReport,
    pub windowing: WindowingReport,
    pub input: InputReport,
    pub readiness: ReadinessReport,
    /// Which interchangeable backends this environment supports, per layer, plus
    /// the one the tool prefers. Lets an agent (or selector) understand what's
    /// available and choose accordingly instead of assuming one fixed path.
    pub capabilities: CapabilityMap,
}

#[derive(Debug, Clone, Serialize, JsonSchema)]
pub struct CapabilityMap {
    /// Pointer/keyboard injection backends, best-first.
    pub input: Vec<String>,
    /// Screen capture backends, best-first.
    pub screenshot: Vec<String>,
    /// Window listing/focus backends available.
    pub window_control: Vec<String>,
    /// Accessibility (element-targeted, non-pointer) backends.
    pub accessibility: Vec<String>,
    /// Display/session isolation contexts the host can provide.
    pub isolation: Vec<String>,
    /// The backend the tool will use by default for each selectable layer.
    pub preferred: PreferredBackends,
}

#[derive(Debug, Clone, Serialize, JsonSchema)]
pub struct PreferredBackends {
    pub input: Option<String>,
    pub screenshot: Option<String>,
    pub window_control: Option<String>,
}

#[derive(Debug, Clone, Serialize, JsonSchema)]
pub struct PlatformReport {
    pub os: String,
    pub arch: String,
    pub desktop_session: Option<String>,
    pub xdg_session_type: Option<String>,
    pub xdg_current_desktop: Option<String>,
    pub wayland_display: Option<String>,
    pub display: Option<String>,
    pub xauthority: Option<String>,
    pub dbus_session_bus_address: Option<String>,
    pub xdg_runtime_dir: Option<String>,
    pub gnome_shell_version: Check,
    pub gnome_screenshot: Check,
    /// Native X11 display for the root-window screenshot route. Fails on
    /// Wayland sessions, including XWayland, by design.
    pub x11_display: Check,
}

#[derive(Debug, Clone, Serialize, JsonSchema)]
pub struct PortalReport {
    pub desktop_portal: Check,
    pub remote_desktop: Check,
    pub remote_desktop_keyboard: Check,
    pub screencast: Check,
    pub screenshot: Check,
    pub input_capture: Check,
    pub mutter_remote_desktop: Check,
    pub mutter_screencast: Check,
}

#[derive(Debug, Clone, Serialize, JsonSchema)]
pub struct AccessibilityReport {
    pub at_spi_bus: Check,
    pub toolkit_accessibility: Check,
    pub at_spi_enabled: Check,
    pub screen_reader_enabled: Check,
    pub forced_toolkit_bridge: bool,
}

#[derive(Debug, Clone, Serialize, JsonSchema)]
pub struct WindowingReport {
    pub gnome_shell_introspect: Check,
    pub computer_use_linux_gnome_shell_extension: Check,
    pub cosmic_helper: Check,
    pub kwin: Check,
    pub hyprland: Check,
    pub niri: Check,
    pub backends: BTreeMap<String, Check>,
    pub can_list_windows: bool,
    pub can_focus_apps: bool,
    pub can_focus_windows: bool,
    pub note: String,
}

#[derive(Debug, Clone, Serialize, JsonSchema)]
pub struct InputReport {
    pub ydotool: Check,
    pub ydotoold: Check,
    pub ydotool_socket: Check,
    pub uinput: Check,
    /// X11 XTEST keyboard backend. Preferred over ydotool on X11 sessions,
    /// where raw evdev scancodes are re-mapped by the active XKB layout.
    pub xdotool: Check,
    /// Wayland virtual-keyboard backend for layout-safe Unicode literal text.
    pub wtype: Check,
    /// Selkies pixelflux computer-use server. Detail is the port only.
    pub pixelflux: Check,
}

#[derive(Debug, Clone, Serialize, JsonSchema)]
pub struct ReadinessReport {
    pub can_register_mcp_tools: bool,
    pub can_build_accessibility_tree: bool,
    pub can_query_windows: bool,
    pub can_focus_apps: bool,
    pub can_focus_windows: bool,
    pub can_send_development_input: bool,
    /// A raw screenshot was successfully captured while producing this report.
    /// Doctor only detects routes, so this remains false until capture is tested.
    pub can_capture_screenshots: bool,
    pub screenshot_capture_status: ScreenshotCaptureStatus,
    pub recommended_next_step: String,
    pub blockers: Vec<String>,
    /// Untested capabilities and optional remediation, without blocking setup.
    pub warnings: Vec<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum ScreenshotCaptureStatus {
    /// No backend route was detected; no capture was attempted.
    Unavailable,
    /// At least one route was detected; no capture was attempted.
    Unverified,
    /// A raw screenshot was captured successfully for this report.
    Verified,
    /// The raw capture attempted for this report failed.
    Failed,
}

const SCREENSHOT_UNAVAILABLE: &str = "No screenshot route was detected: GNOME Shell is absent, the XDG portal does not export org.freedesktop.portal.Screenshot, this is not a native X11 session, and gnome-screenshot is not installed. get_app_state and screenshot return no image; element-aware actions from the accessibility tree still work.";
const SCREENSHOT_UNVERIFIED: &str = "Screenshot routes were detected, but capture is unverified. Doctor does not take a screenshot or check screenshot consent. GNOME Shell can reject callers, and a portal may require consent for the caller's app ID. Call screenshot or get_app_state to verify capture.";
const SCREENSHOT_MISSING_GNOME_FALLBACK: &str = "gnome-screenshot is not installed. On GNOME it provides a fallback when Shell or portal calls are denied; install it or inspect the desktop portal logs and screenshot permissions for the actual caller.";
const SCREENSHOT_FAILED: &str = "Screenshot capture failed for this request. Inspect screenshot_error for the full backend errors; accessibility-tree actions still work.";
const SCREENSHOT_FAILED_NEXT_STEP: &str = "Inspect screenshot_error and the desktop portal logs for the failed capture. On GNOME, gnome-screenshot can provide a fallback when Shell or portal calls are denied.";
const READY_NEXT_STEP: &str = "Computer Use is ready: AT-SPI tree support, window targeting, and a Linux input backend are available.";

impl ReadinessReport {
    /// Record only the raw backend capture, before crop or encoding can fail.
    pub(crate) fn record_screenshot_result(&mut self, succeeded: bool) {
        self.can_capture_screenshots = succeeded;
        self.screenshot_capture_status = if succeeded {
            ScreenshotCaptureStatus::Verified
        } else {
            ScreenshotCaptureStatus::Failed
        };
        self.warnings.retain(|warning| {
            warning != SCREENSHOT_UNVERIFIED && warning != SCREENSHOT_MISSING_GNOME_FALLBACK
        });
        self.blockers
            .retain(|blocker| blocker != SCREENSHOT_UNAVAILABLE && blocker != SCREENSHOT_FAILED);
        if !succeeded {
            self.blockers.push(SCREENSHOT_FAILED.to_string());
        }
        if self.can_build_accessibility_tree
            && self.can_query_windows
            && self.can_focus_windows
            && self.can_send_development_input
        {
            self.recommended_next_step = if succeeded {
                READY_NEXT_STEP
            } else {
                SCREENSHOT_FAILED_NEXT_STEP
            }
            .to_string();
        }
    }
}

#[derive(Debug, Clone, Serialize, JsonSchema)]
pub struct SetupReport {
    pub before: DoctorReport,
    pub accessibility_command: Check,
    pub after: DoctorReport,
    pub changed_accessibility: bool,
    pub requires_target_app_restart: bool,
    pub message: String,
}

#[derive(Debug, Clone, Serialize, JsonSchema)]
pub struct Check {
    pub ok: bool,
    pub detail: String,
}

impl Check {
    fn ok(detail: impl Into<String>) -> Self {
        Self {
            ok: true,
            detail: detail.into(),
        }
    }

    fn fail(detail: impl Into<String>) -> Self {
        Self {
            ok: false,
            detail: detail.into(),
        }
    }
}

pub fn doctor_report() -> DoctorReport {
    hydrate_session_bus_env();

    let platform = platform_report();
    let portals = portal_report();
    let accessibility = accessibility_report();
    let windowing = windowing_report(&platform);
    let input = input_report();
    let readiness = readiness_report_with_portal_keyboard(
        &platform,
        &portals.remote_desktop_keyboard,
        &accessibility,
        &windowing,
        &input,
        !screenshot_backends(&platform, &portals, &input).is_empty(),
    );

    let capabilities = capability_map_with_portal_keyboard(
        &platform,
        &portals,
        &portals.remote_desktop_keyboard,
        &accessibility,
        &windowing,
        &input,
    );

    DoctorReport {
        platform,
        portals,
        accessibility,
        windowing,
        input,
        readiness,
        capabilities,
    }
}

/// Derive the per-layer backend capability map from the individual checks. Lists
/// are ordered best-first and mirror the order the tool actually tries them.
#[cfg(test)]
fn capability_map(
    platform: &PlatformReport,
    portals: &PortalReport,
    accessibility: &AccessibilityReport,
    windowing: &WindowingReport,
    input: &InputReport,
) -> CapabilityMap {
    capability_map_with_portal_keyboard(
        platform,
        portals,
        &portals.remote_desktop,
        accessibility,
        windowing,
        input,
    )
}

fn capability_map_with_portal_keyboard(
    platform: &PlatformReport,
    portals: &PortalReport,
    remote_desktop_keyboard: &Check,
    accessibility: &AccessibilityReport,
    windowing: &WindowingReport,
    input: &InputReport,
) -> CapabilityMap {
    let mut input_backends = Vec::new();
    if input.pixelflux.ok {
        input_backends.push("pixelflux".to_string());
    }
    // Absolute uinput pointer: accurate, non-blocking of coordinates; preferred.
    if input.uinput.ok && platform_is_wayland(platform) && !is_cosmic_wayland_platform(platform) {
        input_backends.push("abs_pointer".to_string());
    }
    let force_ydotool = env_flag_enabled_any(FORCE_YDOTOOL_KEYBOARD_ENV_KEYS);
    let force_xdotool = env_flag_enabled_any(FORCE_XDOTOOL_KEYBOARD_ENV_KEYS);
    let force_portal_keyboard = env_flag_enabled_any(FORCE_PORTAL_KEYBOARD_ENV_KEYS);
    let portal_pointer_available = portal_pointer_input_available(platform, portals);
    let portal_keyboard_available =
        portal_keyboard_input_available(platform, remote_desktop_keyboard);
    let portal_available = portal_pointer_available || portal_keyboard_available;
    let portal_forced_for_all_input = portal_pointer_available
        && portal_keyboard_available
        && force_portal_for_all_input(
            env_flag_enabled_any(FORCE_PORTAL_POINTER_ENV_KEYS),
            env_flag_enabled_any(FORCE_PORTAL_KEYBOARD_ENV_KEYS),
            env_flag_enabled_any(FORCE_YDOTOOL_POINTER_ENV_KEYS),
            force_ydotool,
        );
    if should_advertise_xdotool(platform, input, force_ydotool, force_xdotool) {
        input_backends.push("xdotool".to_string());
    }
    if should_advertise_wtype(
        platform,
        input,
        force_ydotool,
        force_xdotool,
        force_portal_keyboard,
    ) {
        input_backends.push("wtype".to_string());
    }
    let portal_first = portal_forced_for_all_input
        || (is_cosmic_wayland_platform(platform)
            && portal_pointer_available
            && !env_flag_enabled_any(FORCE_YDOTOOL_POINTER_ENV_KEYS));
    if portal_available && portal_first {
        input_backends.push("portal".to_string());
    }
    if input.ydotool.ok && input.ydotool_socket.ok {
        input_backends.push("ydotool".to_string());
    }
    if portal_available && !portal_first {
        input_backends.push("portal".to_string());
    }

    let screenshot_backends = screenshot_backends(platform, portals, input);

    let mut window_backends = Vec::new();
    let x11_available = windowing
        .backends
        .get(X11_BACKEND)
        .is_some_and(|check| check.ok);
    let prefer_x11_over_introspect = windowing.gnome_shell_introspect.ok
        && x11_available
        && registry::backend_can_exact_focus(X11_BACKEND);
    if windowing.computer_use_linux_gnome_shell_extension.ok {
        window_backends.push("gnome_shell_extension".to_string());
    }
    if prefer_x11_over_introspect {
        window_backends.push(X11_BACKEND.to_string());
    }
    if windowing.gnome_shell_introspect.ok {
        window_backends.push("gnome_introspect".to_string());
    }
    if windowing.cosmic_helper.ok {
        window_backends.push("cosmic".to_string());
    }
    if windowing.kwin.ok {
        window_backends.push("kwin".to_string());
    }
    if windowing.hyprland.ok {
        window_backends.push("hyprland".to_string());
    }
    if windowing.niri.ok {
        window_backends.push(NIRI_BACKEND.to_string());
    }
    // i3, labwc, wlr-foreign-toplevel and the generic X11/EWMH backend have
    // no dedicated WindowingReport field; read them from the probe map so the
    // capability list matches the registry order.
    for backend in [I3_BACKEND, LABWC_BACKEND, WLR_FOREIGN_TOPLEVEL_BACKEND] {
        if windowing
            .backends
            .get(backend)
            .is_some_and(|check| check.ok)
        {
            window_backends.push(backend.to_string());
        }
    }
    if x11_available && !prefer_x11_over_introspect {
        window_backends.push(X11_BACKEND.to_string());
    }

    let mut accessibility_backends = Vec::new();
    if can_build_accessibility_tree(accessibility) {
        accessibility_backends.push("at_spi".to_string());
    }

    // Isolation contexts: the live shared session is always available; a headless
    // GNOME session is possible when gnome-shell is installed (it supports
    // --headless --virtual-monitor), giving the agent its own seat.
    let mut isolation = vec!["shared".to_string()];
    if platform.gnome_shell_version.ok {
        isolation.push("headless_gnome".to_string());
    }

    let preferred = PreferredBackends {
        input: input_backends.first().cloned(),
        screenshot: screenshot_backends.first().cloned(),
        window_control: window_backends.first().cloned(),
    };

    CapabilityMap {
        input: input_backends,
        screenshot: screenshot_backends,
        window_control: window_backends,
        accessibility: accessibility_backends,
        isolation,
        preferred,
    }
}

pub fn hydrate_session_bus_env() {
    static HYDRATED_ENV: Mutex<Option<HashMap<String, String>>> = Mutex::new(None);
    let mut hydrated = HYDRATED_ENV
        .lock()
        .unwrap_or_else(|error| error.into_inner());
    let current = desktop_environment();
    if hydrated.as_ref() == Some(&current) {
        return;
    }
    hydrate_common_command_path();
    hydrate_desktop_env_from_process_tree();
    let systemd_discovery_succeeded = hydrate_desktop_env_from_systemd_user();

    if env_var("XDG_RUNTIME_DIR").is_none() {
        if let Some(runtime) = xdg_runtime_dir() {
            if runtime.exists() {
                env::set_var("XDG_RUNTIME_DIR", runtime);
            }
        }
    }

    if env_var("DBUS_SESSION_BUS_ADDRESS").is_none() {
        if let Some(runtime) = xdg_runtime_dir() {
            let bus = runtime.join("bus");
            if bus.exists() {
                env::set_var(
                    "DBUS_SESSION_BUS_ADDRESS",
                    format!("unix:path={}", bus.display()),
                );
            }
        }
    }
    let current = desktop_environment();
    if systemd_discovery_succeeded
        && current.contains_key("DBUS_SESSION_BUS_ADDRESS")
        && current.contains_key("XDG_RUNTIME_DIR")
        && current.contains_key("XDG_CURRENT_DESKTOP")
        && current.contains_key("XDG_SESSION_TYPE")
        && process_env_has_graphical_display(&current)
    {
        *hydrated = Some(current);
    }
}

fn desktop_environment() -> HashMap<String, String> {
    DESKTOP_ENV_KEYS
        .iter()
        .copied()
        .chain(["PATH"])
        .filter_map(|key| env_var(key).map(|value| (key.to_string(), value)))
        .collect()
}

fn hydrate_common_command_path() {
    let mut entries = env::var_os("PATH")
        .map(|path| env::split_paths(&path).collect::<Vec<_>>())
        .unwrap_or_default();
    for path in [
        "/run/current-system/sw/bin",
        "/usr/local/bin",
        "/usr/bin",
        "/bin",
    ] {
        let path = PathBuf::from(path);
        if path.exists() && !entries.iter().any(|entry| entry == &path) {
            entries.push(path);
        }
    }
    if let Ok(path) = env::join_paths(entries) {
        env::set_var("PATH", path);
    }
}

fn hydrate_desktop_env_from_process_tree() {
    for process_env in desktop_process_environments() {
        hydrate_desktop_env_from_map(&process_env);

        if DESKTOP_ENV_KEYS.iter().all(|key| env_var(key).is_some()) {
            break;
        }
    }
}

fn hydrate_desktop_env_from_systemd_user() -> bool {
    let mut command = Command::new("systemctl");
    command.args(["--user", "show-environment"]);
    let Ok(output) =
        crate::command_runner::output_blocking(&mut command, "read systemd user environment")
    else {
        return false;
    };
    if !output.status.success() {
        return false;
    }
    let env_map = parse_line_environment(&output.stdout);
    hydrate_desktop_env_from_map(&env_map);
    true
}

fn hydrate_desktop_env_from_map(process_env: &HashMap<String, String>) {
    let current_env = DESKTOP_ENV_KEYS
        .iter()
        .filter_map(|key| env_var(key).map(|value| ((*key).to_string(), value)))
        .collect();
    for (key, value) in desktop_env_hydration_updates(&current_env, process_env) {
        env::set_var(key, value);
    }
}

fn desktop_env_hydration_updates(
    current_env: &HashMap<String, String>,
    source_env: &HashMap<String, String>,
) -> Vec<(&'static str, String)> {
    // A nested X11 desktop can share a user manager with a Wayland host.
    // Preserve its complete process-local session instead of grafting the
    // host's WAYLAND_DISPLAY onto it.
    let preserve_native_x11 = current_env
        .get("XDG_SESSION_TYPE")
        .is_some_and(|value| value.trim().eq_ignore_ascii_case("x11"))
        && current_env
            .get("DISPLAY")
            .is_some_and(|value| !value.trim().is_empty())
        && current_env
            .get("WAYLAND_DISPLAY")
            .is_none_or(|value| value.trim().is_empty());

    DESKTOP_ENV_KEYS
        .iter()
        .filter_map(|key| {
            if current_env
                .get(*key)
                .is_some_and(|value| !value.trim().is_empty())
                || preserve_native_x11 && *key == "WAYLAND_DISPLAY"
            {
                return None;
            }
            source_env
                .get(*key)
                .filter(|value| !value.trim().is_empty())
                .map(|value| (*key, value.clone()))
        })
        .collect()
}

fn desktop_process_environments() -> Vec<HashMap<String, String>> {
    let mut environments = Vec::new();
    let mut visited_pids = Vec::new();
    let mut pid = parent_pid("self");

    for _ in 0..8 {
        let Some(current_pid) = pid else {
            break;
        };
        if current_pid <= 1 {
            break;
        }

        visited_pids.push(current_pid);
        if let Some(process_env) = read_process_environ(current_pid) {
            environments.push(process_env);
        }
        pid = parent_pid(&current_pid.to_string());
    }

    if !visited_pids.contains(&1) && process_owner_matches_current_user(1) {
        if let Some(process_env) = read_process_environ(1).filter(process_env_has_graphical_display)
        {
            environments.push(process_env);
        }
    }

    environments
}

fn parent_pid(pid: &str) -> Option<u32> {
    let status = fs::read_to_string(format!("/proc/{pid}/status")).ok()?;
    parse_parent_pid(&status)
}

fn parse_parent_pid(status: &str) -> Option<u32> {
    status.lines().find_map(|line| {
        let value = line.strip_prefix("PPid:")?.trim();
        value.parse::<u32>().ok()
    })
}

fn read_process_environ(pid: u32) -> Option<HashMap<String, String>> {
    let bytes = fs::read(format!("/proc/{pid}/environ")).ok()?;
    Some(parse_environ(&bytes))
}

fn process_owner_matches_current_user(pid: u32) -> bool {
    let Some(current_uid) = user_id().and_then(|uid| uid.parse::<u32>().ok()) else {
        return false;
    };
    fs::metadata(format!("/proc/{pid}"))
        .ok()
        .is_some_and(|metadata| metadata.uid() == current_uid)
}

fn process_env_has_graphical_display(process_env: &HashMap<String, String>) -> bool {
    process_env
        .get("DISPLAY")
        .or_else(|| process_env.get("WAYLAND_DISPLAY"))
        .is_some_and(|value| !value.trim().is_empty())
}

fn parse_environ(bytes: &[u8]) -> HashMap<String, String> {
    bytes
        .split(|byte| *byte == 0)
        .filter_map(|entry| {
            if entry.is_empty() {
                return None;
            }
            let split = entry.iter().position(|byte| *byte == b'=')?;
            let (key, value) = entry.split_at(split);
            let value = &value[1..];
            let key = std::str::from_utf8(key).ok()?.to_string();
            let value = std::str::from_utf8(value).ok()?.to_string();
            Some((key, value))
        })
        .collect()
}

fn parse_line_environment(bytes: &[u8]) -> HashMap<String, String> {
    bytes
        .split(|byte| *byte == b'\n')
        .filter_map(|entry| {
            if entry.is_empty() {
                return None;
            }
            let split = entry.iter().position(|byte| *byte == b'=')?;
            let (key, value) = entry.split_at(split);
            let value = &value[1..];
            let key = std::str::from_utf8(key).ok()?.to_string();
            let value = std::str::from_utf8(value).ok()?.to_string();
            Some((key, value))
        })
        .collect()
}

pub fn setup_accessibility_report() -> SetupReport {
    hydrate_session_bus_env();

    let before = doctor_report();
    let accessibility_command =
        enable_accessibility(&before.accessibility, command_check_with_session_bus);
    let after = doctor_report();
    let before_ready = before.readiness.can_build_accessibility_tree;
    let after_ready = after.readiness.can_build_accessibility_tree;
    let saved_before = check_detail_contains_true(&before.accessibility.toolkit_accessibility);
    let saved_after = check_detail_contains_true(&after.accessibility.toolkit_accessibility);
    let changed_accessibility = after_ready && saved_after && (!before_ready || !saved_before);
    let requires_target_app_restart = changed_accessibility;
    let message = if after_ready && !saved_after {
        "AT-SPI is available at runtime, but toolkit-accessibility could not be verified as enabled in GSettings. Newly launched apps may have no accessibility tree. Check accessibility_command and the saved setting; other accessibility tools can change it."
    } else if after_ready {
        if changed_accessibility {
            "AT-SPI accessibility is enabled. Restart already-running target apps if their AT-SPI tree is still empty."
        } else {
            "AT-SPI accessibility is ready."
        }
    } else {
        "Could not enable AT-SPI accessibility automatically. Check the accessibility_command detail and enable org.a11y.Status IsEnabled or org.gnome.desktop.interface toolkit-accessibility manually."
    }
    .to_string();

    SetupReport {
        before,
        accessibility_command,
        after,
        changed_accessibility,
        requires_target_app_restart,
        message,
    }
}

// Runtime IsEnabled does not establish the setting read by newly launched
// GTK apps. Always check the saved key, even when an existing tree is usable.
fn enable_accessibility(
    before: &AccessibilityReport,
    mut run: impl FnMut(&str, &[&str]) -> Check,
) -> Check {
    let setting = if check_detail_contains_true(&before.toolkit_accessibility) {
        before.toolkit_accessibility.clone()
    } else {
        let write = run(
            "gsettings",
            &[
                "set",
                "org.gnome.desktop.interface",
                "toolkit-accessibility",
                "true",
            ],
        );
        let read = run(
            "gsettings",
            &[
                "get",
                "org.gnome.desktop.interface",
                "toolkit-accessibility",
            ],
        );
        if !check_detail_contains_true(&read) {
            let runtime = run(
                "busctl",
                &[
                    "--user",
                    "set-property",
                    "org.a11y.Bus",
                    "/org/a11y/bus",
                    "org.a11y.Status",
                    "IsEnabled",
                    "b",
                    "true",
                ],
            );
            return Check::fail(format!(
                "Saved toolkit-accessibility was not verified: {}; write: {}; runtime fallback: {}",
                read.detail, write.detail, runtime.detail
            ));
        }
        read
    };
    if !can_build_accessibility_tree(before) {
        let runtime = run(
            "busctl",
            &[
                "--user",
                "set-property",
                "org.a11y.Bus",
                "/org/a11y/bus",
                "org.a11y.Status",
                "IsEnabled",
                "b",
                "true",
            ],
        );
        return Check::ok(format!(
            "Saved toolkit-accessibility verified: {}; runtime request: {}",
            setting.detail, runtime.detail
        ));
    }
    Check::ok("Saved toolkit-accessibility is enabled; runtime AT-SPI is available")
}

fn platform_report() -> PlatformReport {
    PlatformReport {
        os: std::env::consts::OS.to_string(),
        arch: std::env::consts::ARCH.to_string(),
        desktop_session: env_var("DESKTOP_SESSION"),
        xdg_session_type: env_var("XDG_SESSION_TYPE"),
        xdg_current_desktop: env_var("XDG_CURRENT_DESKTOP"),
        wayland_display: env_var("WAYLAND_DISPLAY"),
        display: env_var("DISPLAY"),
        xauthority: env_var("XAUTHORITY"),
        dbus_session_bus_address: dbus_session_address(),
        xdg_runtime_dir: xdg_runtime_dir().map(|path| path.display().to_string()),
        gnome_shell_version: command_check("gnome-shell", &["--version"]),
        gnome_screenshot: command_check("gnome-screenshot", &["--version"]),
        x11_display: x11_display_check(),
    }
}

fn x11_display_check() -> Check {
    if !crate::x11_display::is_native_x11_session() {
        return Check::fail("not a native X11 session");
    }
    // The transport enforces the deadline, so no detached worker remains
    // blocked after a diagnostic timeout.
    match crate::x11_display::X11Display::connect().map(|display| display.describe()) {
        Ok(detail) => Check::ok(detail),
        Err(error) => Check::fail(format!("{error:#}")),
    }
}

fn portal_report() -> PortalReport {
    let (remote_desktop, remote_desktop_keyboard, screencast) = remote_desktop_portal_checks();
    PortalReport {
        remote_desktop_keyboard,
        desktop_portal: bus_name_check("org.freedesktop.portal.Desktop"),
        remote_desktop,
        screencast,
        screenshot: portal_interface_methods_check(
            "org.freedesktop.portal.Screenshot",
            SCREENSHOT_PORTAL_METHODS,
        ),
        input_capture: portal_interface_methods_check(
            "org.freedesktop.portal.InputCapture",
            INPUT_CAPTURE_PORTAL_METHODS,
        ),
        mutter_remote_desktop: bus_name_check("org.gnome.Mutter.RemoteDesktop"),
        mutter_screencast: bus_name_check("org.gnome.Mutter.ScreenCast"),
    }
}

fn accessibility_report() -> AccessibilityReport {
    AccessibilityReport {
        at_spi_bus: atspi_bus_address_check(),
        toolkit_accessibility: command_check_with_session_bus(
            "gsettings",
            &[
                "get",
                "org.gnome.desktop.interface",
                "toolkit-accessibility",
            ],
        ),
        at_spi_enabled: at_spi_enabled_check(),
        screen_reader_enabled: screen_reader_enabled_check(),
        forced_toolkit_bridge: toolkit_bridge_forced(),
    }
}

fn windowing_report(platform: &PlatformReport) -> WindowingReport {
    let probes = registry::probe_backends();
    let backend_check = |id: &str| {
        probes
            .iter()
            .find(|probe| probe.id == id)
            .map(check_from_backend_probe)
            .unwrap_or_else(|| Check::fail("backend probe did not run"))
    };
    let gnome_shell_introspect = backend_check(GNOME_SHELL_INTROSPECT_BACKEND);
    let computer_use_linux_gnome_shell_extension = backend_check(GNOME_SHELL_EXTENSION_BACKEND);
    let cosmic_helper = backend_check(COSMIC_WAYLAND_BACKEND);
    let kwin = backend_check(KWIN_BACKEND);
    let hyprland = backend_check(HYPRLAND_BACKEND);
    let niri = backend_check(NIRI_BACKEND);
    let backends = probes
        .iter()
        .map(|probe| (probe.id.to_string(), check_from_backend_probe(probe)))
        .collect::<BTreeMap<_, _>>();
    let can_list_windows = probes.iter().any(|probe| probe.can_list_windows);
    let can_focus_apps = probes.iter().any(|probe| probe.can_focus_apps);
    let can_focus_windows = probes.iter().any(|probe| probe.can_focus_windows);
    let note = if can_list_windows {
        if !can_focus_windows {
            "A window listing backend is available for list_windows, but focused-window and targeted-input verification are unavailable (for example wmctrl is present but xprop is missing on X11)."
        } else if cosmic_helper.ok && is_cosmic_wayland_platform(platform) {
            "A COSMIC Wayland window backend is available for list_windows, focused_window, and targeted input verification."
        } else if kwin.ok {
            "A KWin/Plasma window backend is available for list_windows, focused_window, and targeted input verification."
        } else if hyprland.ok {
            "A Hyprland window backend is available for list_windows, focused_window, and targeted input verification."
        } else if niri.ok {
            "A niri window backend is available for list_windows, focused_window, and targeted input verification."
        } else {
            "A window listing backend is available for list_windows, focused_window, and targeted input verification."
        }
    } else {
        "Window listing is unavailable or denied. Computer Use can still use screenshots, AT-SPI, and global ydotool input, but targeted window input cannot be verified. On GNOME, run setup_window_targeting to install the optional GNOME Shell extension backend. On COSMIC, ensure the bundled COSMIC helper is present and can connect to the session. On KDE/Plasma, ensure KWin exposes org.kde.KWin scripting on the session bus. On Hyprland, ensure hyprctl is available in the session. On niri, ensure the session exposes NIRI_SOCKET so the niri IPC socket is reachable. On labwc, the session socket labwc.sock lists windows. On other wlroots compositors, zwlr_foreign_toplevel_manager_v1 lists windows."
    }
    .to_string();

    WindowingReport {
        gnome_shell_introspect,
        computer_use_linux_gnome_shell_extension,
        cosmic_helper,
        kwin,
        hyprland,
        niri,
        backends,
        can_list_windows,
        can_focus_apps,
        can_focus_windows,
        note,
    }
}

fn check_from_backend_probe(probe: &registry::BackendProbe) -> Check {
    if probe.ok {
        Check::ok(probe.detail.clone())
    } else {
        Check::fail(probe.detail.clone())
    }
}

fn pixelflux_check() -> Check {
    let Some(config) = crate::pixelflux::PixelfluxConfig::from_env() else {
        return Check::fail("PIXELFLUX_CU is not set");
    };
    let port = config.port;
    let probed = std::thread::spawn(move || {
        let runtime = tokio::runtime::Builder::new_current_thread()
            .enable_all()
            .build()
            .ok()?;
        runtime.block_on(crate::pixelflux::probe(&config)).ok()
    })
    .join();
    match probed {
        Ok(Some(())) => Check::ok(port.to_string()),
        _ => Check::fail(format!("{port} unreachable")),
    }
}

fn input_report() -> InputReport {
    InputReport {
        ydotool: match ydotool::ensure_supported() {
            Ok(support) => Check::ok(support.detail),
            Err(detail) => Check::fail(detail),
        },
        ydotoold: process_check("ydotoold"),
        ydotool_socket: ydotool_socket_check(),
        uinput: read_write_path_check(Path::new("/dev/uinput")),
        xdotool: command_path_check("xdotool"),
        wtype: command_path_check("wtype"),
        pixelflux: pixelflux_check(),
    }
}

#[cfg(test)]
fn readiness_report(
    platform: &PlatformReport,
    portals: &PortalReport,
    accessibility: &AccessibilityReport,
    windowing: &WindowingReport,
    input: &InputReport,
) -> ReadinessReport {
    readiness_report_with_portal_keyboard(
        platform,
        &portals.remote_desktop,
        accessibility,
        windowing,
        input,
        !screenshot_backends(platform, portals, input).is_empty(),
    )
}

/// Screenshot routes in the order `capture_screenshot_raw` tries them. Both the
/// capability map and readiness read this list so they cannot disagree.
fn screenshot_backends(
    platform: &PlatformReport,
    portals: &PortalReport,
    input: &InputReport,
) -> Vec<String> {
    let mut backends = Vec::new();
    if input.pixelflux.ok {
        backends.push("pixelflux".to_string());
    }
    if platform.gnome_shell_version.ok {
        backends.push("gnome_shell".to_string());
    }
    if portals.screenshot.ok {
        backends.push("portal".to_string());
    }
    if platform.x11_display.ok {
        backends.push("x11".to_string());
    }
    // Subprocess fallback for background/systemd contexts the DBus paths reject.
    if platform.gnome_screenshot.ok {
        backends.push("gnome_screenshot".to_string());
    }
    backends
}

fn readiness_report_with_portal_keyboard(
    platform: &PlatformReport,
    remote_desktop_keyboard: &Check,
    accessibility: &AccessibilityReport,
    windowing: &WindowingReport,
    input: &InputReport,
    screenshot_routes_detected: bool,
) -> ReadinessReport {
    let mut blockers = Vec::new();
    let mut warnings = Vec::new();
    let can_build_accessibility_tree = can_build_accessibility_tree(accessibility);
    let can_query_windows = windowing.can_list_windows;
    let can_focus_apps = windowing.can_focus_apps;
    let can_focus_windows = windowing.can_focus_windows;
    let can_send_development_input =
        can_send_development_input(platform, remote_desktop_keyboard, input);

    if let Some(blocker) =
        atspi_disabled_blocker(accessibility, accessibility.forced_toolkit_bridge)
    {
        blockers.push(blocker);
    }

    if !can_query_windows {
        blockers.push(if is_cosmic_wayland_platform(platform) {
            "COSMIC Wayland window introspection is unavailable; targeted window focus and verification will be disabled.".to_string()
        } else {
            "Window introspection is unavailable; targeted window focus and verification will be disabled."
                .to_string()
        });
    }

    if can_query_windows && !can_focus_windows {
        blockers.push(
            "Exact window activation is unavailable; app-level focus may work, but window_id/title/terminal-targeted input cannot be verified."
                .to_string(),
        );
    }

    if !can_send_development_input {
        blockers.push(
            "Development keyboard input is unavailable; enable XDG RemoteDesktop portal input or install wtype on compatible Wayland compositors, install xdotool with DISPLAY on X11, or use ydotool with a connectable ydotoold socket. Read/write /dev/uinput alone provides only absolute pointer input."
                .to_string(),
        );
    }

    if screenshot_routes_detected {
        warnings.push(SCREENSHOT_UNVERIFIED.to_string());
        if platform.gnome_shell_version.ok && !platform.gnome_screenshot.ok {
            warnings.push(SCREENSHOT_MISSING_GNOME_FALLBACK.to_string());
        }
    } else {
        blockers.push(SCREENSHOT_UNAVAILABLE.to_string());
    }

    let recommended_next_step = if !can_build_accessibility_tree {
        "Run setup_accessibility to enable AT-SPI accessibility before element-aware actions."
            .to_string()
    } else if !can_query_windows {
        format!(
            "Enable a supported window backend before using targeted keyboard input: {}",
            registry::descriptors()
                .iter()
                .map(|descriptor| descriptor.missing_hint)
                .collect::<Vec<_>>()
                .join(" ")
        )
    } else if !can_focus_windows {
        "Enable an exact-focus window backend before using window_id, title, or terminal-targeted input.".to_string()
    } else if !can_send_development_input {
        "Enable a keyboard-capable input backend: enable the XDG RemoteDesktop portal or install wtype on compatible Wayland compositors, install xdotool for X11, or start ydotoold with a socket accessible to this desktop user."
            .to_string()
    } else if !screenshot_routes_detected {
        "Enable a screenshot route: install an XDG desktop portal backend that implements Screenshot for this desktop, or install gnome-screenshot. Accessibility-tree actions work meanwhile."
            .to_string()
    } else {
        "AT-SPI tree support, window targeting, and a Linux input backend are available. Call screenshot or get_app_state to verify screenshot capture."
            .to_string()
    };

    ReadinessReport {
        can_register_mcp_tools: true,
        can_build_accessibility_tree,
        can_query_windows,
        can_focus_apps,
        can_focus_windows,
        can_send_development_input,
        can_capture_screenshots: false,
        screenshot_capture_status: if screenshot_routes_detected {
            ScreenshotCaptureStatus::Unverified
        } else {
            ScreenshotCaptureStatus::Unavailable
        },
        recommended_next_step,
        blockers,
        warnings,
    }
}

fn can_send_development_input(
    platform: &PlatformReport,
    remote_desktop_keyboard: &Check,
    input: &InputReport,
) -> bool {
    let force_ydotool = env_flag_enabled_any(FORCE_YDOTOOL_KEYBOARD_ENV_KEYS);
    let force_xdotool = env_flag_enabled_any(FORCE_XDOTOOL_KEYBOARD_ENV_KEYS);
    input.pixelflux.ok
        || portal_keyboard_input_available(platform, remote_desktop_keyboard)
        || platform_is_wayland(platform)
            && wtype_compatible_wayland_desktop(platform.xdg_current_desktop.as_deref())
            && input.wtype.ok
            && !force_ydotool
        || should_advertise_xdotool(platform, input, force_ydotool, force_xdotool)
        || input.ydotool.ok && input.ydotool_socket.ok
}

fn portal_pointer_input_available(platform: &PlatformReport, portals: &PortalReport) -> bool {
    platform_is_wayland(platform) && portals.remote_desktop.ok
}

fn portal_keyboard_input_available(
    platform: &PlatformReport,
    remote_desktop_keyboard: &Check,
) -> bool {
    platform_is_wayland(platform) && remote_desktop_keyboard.ok
}

fn is_cosmic_wayland_platform(platform: &PlatformReport) -> bool {
    platform
        .xdg_current_desktop
        .as_deref()
        .is_some_and(|desktop| desktop.to_ascii_lowercase().contains("cosmic"))
        && platform.xdg_session_type.as_deref() == Some("wayland")
}

pub(crate) fn can_build_accessibility_tree_now() -> bool {
    hydrate_session_bus_env();
    let accessibility = accessibility_report();
    can_build_accessibility_tree(&accessibility)
}

fn can_build_accessibility_tree(accessibility: &AccessibilityReport) -> bool {
    accessibility_tree_ready(accessibility, accessibility.forced_toolkit_bridge)
}

fn accessibility_tree_ready(accessibility: &AccessibilityReport, forced_bridge: bool) -> bool {
    accessibility.at_spi_bus.ok
        && (forced_bridge
            || check_detail_contains_true(&accessibility.at_spi_enabled)
            || check_detail_contains_true(&accessibility.toolkit_accessibility))
}

fn atspi_disabled_blocker(
    accessibility: &AccessibilityReport,
    forced_bridge: bool,
) -> Option<String> {
    if accessibility_tree_ready(accessibility, forced_bridge) {
        None
    } else {
        Some(
            "AT-SPI accessibility is disabled; enable org.a11y.Status IsEnabled or org.gnome.desktop.interface toolkit-accessibility for tree extraction."
                .to_string(),
        )
    }
}

fn toolkit_bridge_forced() -> bool {
    if toolkit_bridge_forced_by_env(env_var) {
        return true;
    }
    let compositor = compositor_environ();
    toolkit_bridge_forced_by_env(|key| {
        env_var(key).or_else(|| compositor.as_ref().and_then(|vars| vars.get(key).cloned()))
    })
}

fn toolkit_bridge_forced_by_env(vars: impl Fn(&str) -> Option<String>) -> bool {
    vars("QT_LINUX_ACCESSIBILITY_ALWAYS_ON").is_some_and(|value| value == "1")
        || vars("GTK_MODULES").is_some_and(|value| value.contains("atk-bridge"))
}

fn compositor_environ() -> Option<HashMap<String, String>> {
    // Read only the process proven to own the selected Wayland connection.
    // Another desktop or nested compositor must not enable this session's tree.
    let pid = crate::windowing::wayland_session::compositor_pid()?;
    read_process_environ(pid)
}

fn check_detail_contains_true(check: &Check) -> bool {
    check.ok && check.detail.to_ascii_lowercase().contains("true")
}

fn env_var(key: &str) -> Option<String> {
    env::var(key).ok().filter(|value| !value.trim().is_empty())
}

fn xdg_runtime_dir() -> Option<PathBuf> {
    if let Some(value) = env_var("XDG_RUNTIME_DIR") {
        return Some(PathBuf::from(value));
    }
    user_id().map(|uid| PathBuf::from(format!("/run/user/{uid}")))
}

fn dbus_session_address() -> Option<String> {
    if let Some(value) = env_var("DBUS_SESSION_BUS_ADDRESS") {
        return Some(value);
    }
    xdg_runtime_dir()
        .map(|runtime| format!("unix:path={}", runtime.join("bus").display()))
        .filter(|address| {
            address
                .strip_prefix("unix:path=")
                .is_some_and(|p| Path::new(p).exists())
        })
}

fn ydotool_socket_check() -> Check {
    match ydotool::connectable_socket_path() {
        Ok(path) => Check::ok(format!("connectable: {}", path.display())),
        Err(detail) => Check::fail(format!("no trusted connectable ydotool socket ({detail})")),
    }
}

fn user_id() -> Option<String> {
    let mut command = Command::new("id");
    command.arg("-u");
    let output = crate::command_runner::output_blocking(&mut command, "query user id").ok()?;
    output
        .status
        .success()
        .then(|| String::from_utf8_lossy(&output.stdout).trim().to_string())
        .filter(|value| !value.is_empty())
}

fn command_path_check(command: &str) -> Check {
    command_check("sh", &["-c", &format!("command -v {command}")])
}

fn platform_is_wayland(platform: &PlatformReport) -> bool {
    match platform.xdg_session_type.as_deref() {
        Some(value) => value.eq_ignore_ascii_case("wayland"),
        None => platform
            .wayland_display
            .as_deref()
            .is_some_and(|display| !display.trim().is_empty()),
    }
}

pub(crate) fn wtype_compatible_wayland_desktop(desktop: Option<&str>) -> bool {
    desktop.is_none_or(|desktop| {
        let desktop = desktop.to_ascii_lowercase();
        !["gnome", "kde", "plasma", "cosmic"]
            .iter()
            .any(|known_incompatible| desktop.contains(known_incompatible))
    })
}

fn should_advertise_wtype(
    platform: &PlatformReport,
    input: &InputReport,
    force_ydotool: bool,
    force_xdotool: bool,
    force_portal_keyboard: bool,
) -> bool {
    // The server routes keyboard input to xdotool before wtype only when
    // xdotool can run, and to the portal whenever it is forced.
    platform_is_wayland(platform)
        && wtype_compatible_wayland_desktop(platform.xdg_current_desktop.as_deref())
        && input.wtype.ok
        && !force_ydotool
        && !should_advertise_xdotool(platform, input, force_ydotool, force_xdotool)
        && !force_portal_keyboard
}

fn should_advertise_xdotool(
    platform: &PlatformReport,
    input: &InputReport,
    force_ydotool: bool,
    force_xdotool: bool,
) -> bool {
    !force_ydotool
        && input.xdotool.ok
        && platform
            .display
            .as_deref()
            .is_some_and(|display| !display.trim().is_empty())
        && (force_xdotool || !platform_is_wayland(platform))
}

fn env_flag_enabled_any(keys: &[&str]) -> bool {
    keys.iter()
        .any(|key| env::var(key).ok().as_deref() == Some("1"))
}

fn force_portal_for_all_input(
    force_portal_pointer: bool,
    force_portal_keyboard: bool,
    force_ydotool_pointer: bool,
    force_ydotool_keyboard: bool,
) -> bool {
    force_portal_pointer
        && force_portal_keyboard
        && !force_ydotool_pointer
        && !force_ydotool_keyboard
}

fn process_check(process_name: &str) -> Check {
    command_check("pgrep", &["-a", process_name])
}

#[cfg(test)]
fn socket_connect_check(path: &Path) -> Check {
    match socket_connect_result(path) {
        Ok(()) => Check::ok(format!("connectable: {}", path.display())),
        Err(detail) => Check::fail(detail),
    }
}

#[cfg(test)]
fn socket_connect_result(path: &Path) -> std::result::Result<(), String> {
    if !path.exists() {
        return Err(format!("missing: {}", path.display()));
    }

    std::os::unix::net::UnixDatagram::unbound()
        .and_then(|socket| socket.connect(path))
        .map_err(|error| format!("{}: datagram: {error}", path.display()))
}

fn read_write_path_check(path: &Path) -> Check {
    if !path.exists() {
        return Check::fail(format!("missing: {}", path.display()));
    }

    match OpenOptions::new().read(true).write(true).open(path) {
        Ok(_) => Check::ok(format!("read/write: {}", path.display())),
        Err(error) => Check::fail(format!("{}: {error}", path.display())),
    }
}

fn bus_name_check(name: &str) -> Check {
    command_check_with_session_bus("busctl", &["--user", "status", name])
}

fn portal_interface_check(interface: &str) -> Check {
    command_check_with_session_bus(
        "busctl",
        &[
            "--user",
            "introspect",
            "org.freedesktop.portal.Desktop",
            "/org/freedesktop/portal/desktop",
            interface,
        ],
    )
}

/// Introspect a portal interface and require it to export `methods`.
fn portal_interface_methods_check(interface: &str, methods: &[&str]) -> Check {
    require_busctl_methods(portal_interface_check(interface), interface, methods)
}

fn require_busctl_methods(introspection: Check, interface: &str, methods: &[&str]) -> Check {
    if !introspection.ok {
        return introspection;
    }
    let missing = missing_busctl_methods(&introspection, methods);
    if missing.is_empty() {
        return introspection;
    }
    if !busctl_introspection_has_members(&introspection.detail) {
        return Check::fail(format!(
            "{interface} is not exported by the portal (introspection listed no members)"
        ));
    }
    Check::fail(format!(
        "{interface} is missing required methods: {}",
        missing.join(", ")
    ))
}

fn busctl_introspection_has_members(detail: &str) -> bool {
    detail
        .lines()
        .any(|line| line.trim_start().starts_with('.'))
}

fn remote_desktop_portal_checks() -> (Check, Check, Check) {
    let introspection = portal_interface_check("org.freedesktop.portal.RemoteDesktop");
    let screencast = portal_interface_methods_check(
        "org.freedesktop.portal.ScreenCast",
        SCREENCAST_PORTAL_METHODS,
    );
    if !introspection.ok {
        return (introspection.clone(), introspection, screencast);
    }

    let available_device_types = command_check_with_session_bus(
        "busctl",
        &[
            "--user",
            "get-property",
            "org.freedesktop.portal.Desktop",
            "/org/freedesktop/portal/desktop",
            "org.freedesktop.portal.RemoteDesktop",
            "AvailableDeviceTypes",
        ],
    );
    let available_source_types = command_check_with_session_bus(
        "busctl",
        &[
            "--user",
            "get-property",
            "org.freedesktop.portal.Desktop",
            "/org/freedesktop/portal/desktop",
            "org.freedesktop.portal.ScreenCast",
            "AvailableSourceTypes",
        ],
    );
    let screencast_version = command_check_with_session_bus(
        "busctl",
        &[
            "--user",
            "get-property",
            "org.freedesktop.portal.Desktop",
            "/org/freedesktop/portal/desktop",
            "org.freedesktop.portal.ScreenCast",
            "version",
        ],
    );
    let available_cursor_modes = command_check_with_session_bus(
        "busctl",
        &[
            "--user",
            "get-property",
            "org.freedesktop.portal.Desktop",
            "/org/freedesktop/portal/desktop",
            "org.freedesktop.portal.ScreenCast",
            "AvailableCursorModes",
        ],
    );
    let pointer = remote_desktop_pointer_check_from(
        &introspection,
        &screencast,
        &available_device_types,
        &available_source_types,
        &screencast_version,
        &available_cursor_modes,
    );
    let keyboard = remote_desktop_keyboard_check_from(&introspection, &available_device_types);
    (pointer, keyboard, screencast)
}

fn remote_desktop_pointer_check_from(
    introspection: &Check,
    screencast: &Check,
    available_device_types: &Check,
    available_source_types: &Check,
    screencast_version: &Check,
    available_cursor_modes: &Check,
) -> Check {
    if !introspection.ok {
        return Check::fail(introspection.detail.clone());
    }
    let missing_methods = missing_busctl_methods(introspection, REMOTE_DESKTOP_POINTER_METHODS);
    if !missing_methods.is_empty() {
        return Check::fail(format!(
            "RemoteDesktop interface is missing required pointer methods: {}",
            missing_methods.join(", ")
        ));
    }

    if !screencast.ok {
        return Check::fail(format!(
            "ScreenCast interface is unavailable for portal pointer input: {}",
            screencast.detail
        ));
    }
    let missing_screencast_methods = missing_busctl_methods(screencast, SCREENCAST_POINTER_METHODS);
    if !missing_screencast_methods.is_empty() {
        return Check::fail(format!(
            "ScreenCast interface is missing required pointer methods: {}",
            missing_screencast_methods.join(", ")
        ));
    }

    let device_types = match remote_desktop_device_types(available_device_types) {
        Ok(device_types) => device_types,
        Err(detail) => return Check::fail(detail),
    };
    if device_types & PORTAL_DEVICE_POINTER == 0 {
        return Check::fail(format!(
            "RemoteDesktop AvailableDeviceTypes={device_types} does not include pointer input"
        ));
    }

    let source_types = match screencast_source_types(available_source_types) {
        Ok(source_types) => source_types,
        Err(detail) => return Check::fail(detail),
    };
    if source_types & PORTAL_SOURCE_MONITOR == 0 {
        return Check::fail(format!(
            "ScreenCast AvailableSourceTypes={source_types} does not include monitor sources"
        ));
    }

    let version = match screencast_version_number(screencast_version) {
        Ok(version) => version,
        Err(detail) => return Check::fail(detail),
    };
    if version >= SCREENCAST_CURSOR_MODE_VERSION {
        let cursor_modes = match screencast_cursor_modes(available_cursor_modes) {
            Ok(cursor_modes) => cursor_modes,
            Err(detail) => return Check::fail(detail),
        };
        if cursor_modes & crate::remote_desktop::CURSOR_MODE_HIDDEN == 0 {
            return Check::fail(format!(
                "ScreenCast AvailableCursorModes={cursor_modes} does not include hidden cursor mode"
            ));
        }

        return Check::ok(format!(
            "pointer-capable RemoteDesktop portal (AvailableDeviceTypes={device_types}, AvailableSourceTypes={source_types}, ScreenCast version={version}, AvailableCursorModes={cursor_modes})"
        ));
    }

    Check::ok(format!(
        "pointer-capable RemoteDesktop portal (AvailableDeviceTypes={device_types}, AvailableSourceTypes={source_types}, ScreenCast version={version}, hidden cursor mode is the interface default)"
    ))
}

fn screencast_version_number(screencast_version: &Check) -> Result<u32, String> {
    if !screencast_version.ok {
        return Err(format!(
            "ScreenCast version is unavailable: {}",
            screencast_version.detail
        ));
    }
    parse_busctl_u32_property(&screencast_version.detail).ok_or_else(|| {
        format!(
            "ScreenCast version has an unexpected value: {}",
            screencast_version.detail
        )
    })
}

fn screencast_cursor_modes(available_cursor_modes: &Check) -> Result<u32, String> {
    if !available_cursor_modes.ok {
        return Err(format!(
            "ScreenCast AvailableCursorModes is unavailable: {}",
            available_cursor_modes.detail
        ));
    }
    parse_busctl_u32_property(&available_cursor_modes.detail).ok_or_else(|| {
        format!(
            "ScreenCast AvailableCursorModes has an unexpected value: {}",
            available_cursor_modes.detail
        )
    })
}

fn remote_desktop_keyboard_check_from(
    introspection: &Check,
    available_device_types: &Check,
) -> Check {
    if !introspection.ok {
        return Check::fail(introspection.detail.clone());
    }

    let missing_methods = missing_busctl_methods(introspection, REMOTE_DESKTOP_KEYBOARD_METHODS);
    if !missing_methods.is_empty() {
        return Check::fail(format!(
            "RemoteDesktop interface is missing required keyboard methods: {}",
            missing_methods.join(", ")
        ));
    }

    let device_types = match remote_desktop_device_types(available_device_types) {
        Ok(device_types) => device_types,
        Err(detail) => return Check::fail(detail),
    };
    if device_types & PORTAL_DEVICE_KEYBOARD == 0 {
        return Check::fail(format!(
            "RemoteDesktop AvailableDeviceTypes={device_types} does not include keyboard input"
        ));
    }

    Check::ok(format!(
        "keyboard-capable RemoteDesktop portal (AvailableDeviceTypes={device_types})"
    ))
}

fn missing_busctl_methods<'a>(introspection: &Check, methods: &'a [&'a str]) -> Vec<&'a str> {
    methods
        .iter()
        .copied()
        .filter(|method| !busctl_introspection_has_method(&introspection.detail, method))
        .collect()
}

fn remote_desktop_device_types(available_device_types: &Check) -> Result<u32, String> {
    if !available_device_types.ok {
        return Err(format!(
            "RemoteDesktop AvailableDeviceTypes is unavailable: {}",
            available_device_types.detail
        ));
    }
    parse_busctl_u32_property(&available_device_types.detail).ok_or_else(|| {
        format!(
            "RemoteDesktop AvailableDeviceTypes has an unexpected value: {}",
            available_device_types.detail
        )
    })
}

fn screencast_source_types(available_source_types: &Check) -> Result<u32, String> {
    if !available_source_types.ok {
        return Err(format!(
            "ScreenCast AvailableSourceTypes is unavailable: {}",
            available_source_types.detail
        ));
    }
    parse_busctl_u32_property(&available_source_types.detail).ok_or_else(|| {
        format!(
            "ScreenCast AvailableSourceTypes has an unexpected value: {}",
            available_source_types.detail
        )
    })
}

fn busctl_introspection_has_method(detail: &str, method: &str) -> bool {
    detail.lines().any(|line| {
        let mut fields = line.split_whitespace();
        fields
            .next()
            .is_some_and(|name| name.trim_start_matches('.') == method)
            && fields.next() == Some("method")
    })
}

fn parse_busctl_u32_property(detail: &str) -> Option<u32> {
    let mut fields = detail.split_whitespace();
    if fields.next()? != "u" {
        return None;
    }
    let value = fields.next()?;
    value
        .strip_prefix("0x")
        .map(|hex| u32::from_str_radix(hex, 16).ok())
        .unwrap_or_else(|| value.parse().ok())
}

fn atspi_bus_address_check() -> Check {
    let busctl = command_check_with_session_bus(
        "busctl",
        &[
            "--user",
            "call",
            "org.a11y.Bus",
            "/org/a11y/bus",
            "org.a11y.Bus",
            "GetAddress",
        ],
    );
    if busctl.ok {
        return busctl;
    }

    gdbus_call_check(
        "org.a11y.Bus",
        "/org/a11y/bus",
        "org.a11y.Bus.GetAddress",
        &[],
    )
}

fn atspi_status_property_check(property: &str) -> Check {
    let busctl = command_check_with_session_bus(
        "busctl",
        &[
            "--user",
            "get-property",
            "org.a11y.Bus",
            "/org/a11y/bus",
            "org.a11y.Status",
            property,
        ],
    );
    if busctl.ok {
        return busctl;
    }

    gdbus_call_check(
        "org.a11y.Bus",
        "/org/a11y/bus",
        "org.freedesktop.DBus.Properties.Get",
        &["org.a11y.Status", property],
    )
}

// check_detail_contains_true reads this detail, so keep "true" in it.
fn at_spi_enabled_check() -> Check {
    describe_bool(
        atspi_status_property_check("IsEnabled"),
        "AT-SPI IsEnabled is true",
        "AT-SPI IsEnabled is false",
    )
}

// at-spi2-core 2.62 removed ScreenReaderEnabled; it mirrored this key.
fn screen_reader_enabled_check() -> Check {
    let mut check = atspi_status_property_check("ScreenReaderEnabled");
    if !check.ok {
        check = command_check_with_session_bus(
            "gsettings",
            &[
                "get",
                "org.gnome.desktop.a11y.applications",
                "screen-reader-enabled",
            ],
        );
    }
    describe_bool(check, "Screen reader is on", "Screen reader is off")
}

// Replaces raw busctl, gdbus or gsettings boolean output with text.
fn describe_bool(check: Check, on: &str, off: &str) -> Check {
    let detail = check.detail.to_ascii_lowercase();
    match (check.ok, detail.contains("true"), detail.contains("false")) {
        (true, true, false) => Check::ok(on),
        (true, false, true) => Check::ok(off),
        _ => check,
    }
}

fn gdbus_call_check(destination: &str, object_path: &str, method: &str, args: &[&str]) -> Check {
    let mut command_args = vec![
        "call",
        "--session",
        "--dest",
        destination,
        "--object-path",
        object_path,
        "--method",
        method,
    ];
    command_args.extend_from_slice(args);
    command_check_with_session_bus("gdbus", &command_args)
}

fn command_check(command: &str, args: &[&str]) -> Check {
    run_command(command, args, false)
}

fn command_check_with_session_bus(command: &str, args: &[&str]) -> Check {
    run_command(command, args, true)
}

fn run_command(command: &str, args: &[&str], with_session_bus: bool) -> Check {
    let mut cmd = Command::new(command);
    cmd.args(args);

    if with_session_bus {
        if let Some(address) = dbus_session_address() {
            cmd.env("DBUS_SESSION_BUS_ADDRESS", address);
        }
        if let Some(runtime) = xdg_runtime_dir() {
            cmd.env("XDG_RUNTIME_DIR", runtime);
        }
    }

    // Introspection can block while a portal retrieves backend properties.
    // Bound every probe and reap its process group instead of stalling doctor
    // and every get_app_state request waiting for that diagnostic.
    match crate::command_runner::output_blocking(&mut cmd, &format!("run diagnostic {command}")) {
        Ok(output) if output.status.success() => {
            let detail = String::from_utf8_lossy(&output.stdout).trim().to_string();
            Check::ok(if detail.is_empty() {
                "ok".into()
            } else {
                detail
            })
        }
        Ok(output) => {
            let stderr = String::from_utf8_lossy(&output.stderr).trim().to_string();
            let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string();
            let detail = if !stderr.is_empty() { stderr } else { stdout };
            Check::fail(if detail.is_empty() {
                format!("exit status {}", output.status)
            } else {
                detail
            })
        }
        Err(error) => Check::fail(format!("{error:#}")),
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;

    /// A complete report for tests outside this module. With accessibility
    /// disabled, get_app_state skips the tree and touches no desktop service.
    pub(crate) fn doctor_report_fixture(
        accessibility_enabled: bool,
        windows_available: bool,
    ) -> DoctorReport {
        let platform = platform_report();
        let portals = portal_report(Check::fail("missing"));
        let accessibility = accessibility_report(
            Check::ok("bus"),
            Check::ok(accessibility_enabled.to_string()),
        );
        let windowing = windowing_report(windows_available, windows_available);
        let input = input_report(true);
        DoctorReport {
            readiness: readiness_report(&platform, &portals, &accessibility, &windowing, &input),
            capabilities: capability_map(&platform, &portals, &accessibility, &windowing, &input),
            platform,
            portals,
            accessibility,
            windowing,
            input,
        }
    }

    #[test]
    fn describe_bool_reads_busctl_gdbus_and_gsettings_output() {
        for (raw, expected) in [("b true", "on"), ("(<false>,)", "off"), ("false", "off")] {
            assert_eq!(describe_bool(Check::ok(raw), "on", "off").detail, expected);
        }
        let failed = describe_bool(Check::fail("no such property"), "on", "off");
        assert!(!failed.ok);
        assert_eq!(failed.detail, "no such property");
    }

    #[test]
    fn diagnostic_command_timeout_reaps_the_hung_process() {
        use std::time::{Duration, Instant};

        let pid_path = std::env::temp_dir().join(format!(
            "cul-diagnostic-timeout-{}-{}.pid",
            std::process::id(),
            getrandom::u64().unwrap()
        ));
        let started = Instant::now();
        let result = run_command(
            "/bin/sh",
            &[
                "-c",
                "printf '%s' \"$$\" > \"$1\"; exec /bin/sleep 60",
                "diagnostic-timeout-test",
                pid_path.to_str().unwrap(),
            ],
            false,
        );
        let elapsed = started.elapsed();
        let pid = fs::read_to_string(&pid_path);
        let _ = fs::remove_file(&pid_path);
        let pid: u32 = pid.expect("fixture must record its pid").parse().unwrap();

        assert!(!result.ok, "hung diagnostic must fail");
        assert!(result.detail.contains("timed out after 2000 ms"));
        assert!(elapsed < Duration::from_secs(4), "took {elapsed:?}");
        assert!(
            !Path::new(&format!("/proc/{pid}")).exists(),
            "diagnostic child must be killed and reaped before returning"
        );
    }

    #[test]
    fn diagnostic_spawn_failure_keeps_the_os_cause() {
        let path = std::env::temp_dir().join(format!(
            "cul-missing-diagnostic-{}-{}",
            std::process::id(),
            getrandom::u64().unwrap()
        ));
        let result = run_command(path.to_str().unwrap(), &[], false);
        assert!(!result.ok);
        assert!(result.detail.contains("failed to run diagnostic"));
        assert!(result.detail.contains("os error 2"));
    }

    #[test]
    fn setup_repairs_saved_key_even_when_runtime_is_ready() {
        let mut before = accessibility_report(Check::ok("bus"), Check::ok("false"));
        before.at_spi_enabled = Check::ok("b true");
        let mut calls = Vec::new();
        let result = enable_accessibility(&before, |program, args| {
            calls.push((program.to_string(), args.join(" ")));
            Check::ok(if args[0] == "get" { "true" } else { "" })
        });
        assert!(result.ok);
        assert_eq!(
            calls,
            vec![
                (
                    "gsettings".into(),
                    "set org.gnome.desktop.interface toolkit-accessibility true".into()
                ),
                (
                    "gsettings".into(),
                    "get org.gnome.desktop.interface toolkit-accessibility".into()
                ),
            ]
        );
    }

    #[test]
    fn setup_does_not_claim_saved_success_from_command_exit_status() {
        let before = accessibility_report(Check::ok("bus"), Check::ok("false"));
        let mut calls = Vec::new();
        let result = enable_accessibility(&before, |program, args| {
            calls.push(program.to_string());
            Check::ok(if args[0] == "get" {
                "false"
            } else {
                "exit status 0"
            })
        });
        assert!(!result.ok);
        assert!(result.detail.contains("not verified"));
        assert_eq!(calls, ["gsettings", "gsettings", "busctl"]);
    }

    #[test]
    fn setup_preserves_enabled_settings_without_writing() {
        let before = accessibility_report(Check::ok("bus"), Check::ok("true"));
        let result = enable_accessibility(&before, |_, _| panic!("already enabled"));
        assert!(result.ok);
    }

    fn platform_report() -> PlatformReport {
        PlatformReport {
            os: "linux".to_string(),
            arch: "x86_64".to_string(),
            desktop_session: None,
            xdg_session_type: Some("wayland".to_string()),
            xdg_current_desktop: Some("GNOME".to_string()),
            wayland_display: Some("wayland-0".to_string()),
            display: Some(":0".to_string()),
            xauthority: Some("/run/user/1000/Xauthority".to_string()),
            dbus_session_bus_address: Some("unix:path=/run/user/1000/bus".to_string()),
            xdg_runtime_dir: Some("/run/user/1000".to_string()),
            gnome_shell_version: Check::ok("GNOME Shell 46.0"),
            gnome_screenshot: Check::ok("gnome-screenshot 41.0"),
            x11_display: Check::fail("not a native X11 session"),
        }
    }

    fn portal_report(remote_desktop: Check) -> PortalReport {
        PortalReport {
            remote_desktop_keyboard: remote_desktop.clone(),
            desktop_portal: Check::ok("ok"),
            remote_desktop,
            screencast: Check::fail("missing"),
            screenshot: Check::fail("missing"),
            input_capture: Check::fail("missing"),
            mutter_remote_desktop: Check::fail("missing"),
            mutter_screencast: Check::fail("missing"),
        }
    }

    fn accessibility_report(
        at_spi_bus: Check,
        toolkit_accessibility: Check,
    ) -> AccessibilityReport {
        AccessibilityReport {
            at_spi_bus,
            toolkit_accessibility,
            at_spi_enabled: Check::fail("(<false>,)"),
            screen_reader_enabled: Check::fail("(<false>,)"),
            forced_toolkit_bridge: false,
        }
    }
    #[test]
    fn bus_reachable_and_forced_bridge_is_enabled() {
        let enabled = accessibility_report(Check::ok("bus"), Check::fail("false"));
        assert!(accessibility_tree_ready(&enabled, true));
        assert!(atspi_disabled_blocker(&enabled, true).is_none());
        let down = accessibility_report(Check::fail("no bus"), Check::fail("false"));
        assert!(!accessibility_tree_ready(&down, true));
        assert!(
            atspi_disabled_blocker(&down, true).is_some_and(|blocker| blocker.contains("disabled"))
        );
        assert!(toolkit_bridge_forced_by_env(|key| {
            (key == "QT_LINUX_ACCESSIBILITY_ALWAYS_ON").then(|| "1".to_string())
        }));
        assert!(toolkit_bridge_forced_by_env(|key| {
            (key == "GTK_MODULES").then(|| "gail:atk-bridge".to_string())
        }));
        assert!(!toolkit_bridge_forced_by_env(|_| None));
    }

    fn windowing_report(can_list_windows: bool, can_focus_windows: bool) -> WindowingReport {
        WindowingReport {
            gnome_shell_introspect: if can_list_windows {
                Check::ok("ok")
            } else {
                Check::fail("denied")
            },
            computer_use_linux_gnome_shell_extension: if can_focus_windows {
                Check::ok("ok")
            } else {
                Check::fail("missing")
            },
            cosmic_helper: Check::fail("missing"),
            kwin: Check::fail("not a KWin session"),
            hyprland: Check::fail("not a Hyprland session"),
            niri: Check::fail("not a niri session"),
            backends: BTreeMap::new(),
            can_list_windows,
            can_focus_apps: true,
            can_focus_windows,
            note: String::new(),
        }
    }

    fn input_report(can_send_input: bool) -> InputReport {
        let check = if can_send_input {
            Check::ok("ok")
        } else {
            Check::fail("missing")
        };
        input_report_parts(check.clone(), check.clone(), check.clone(), check)
    }

    fn input_report_parts(
        ydotool: Check,
        ydotoold: Check,
        ydotool_socket: Check,
        uinput: Check,
    ) -> InputReport {
        InputReport {
            ydotool,
            ydotoold,
            ydotool_socket,
            uinput,
            xdotool: Check::fail("missing xdotool"),
            wtype: Check::fail("missing wtype"),
            pixelflux: Check::fail("PIXELFLUX_CU is not set"),
        }
    }

    #[test]
    fn accessibility_tree_requires_reachable_at_spi_bus() {
        let report = accessibility_report(Check::fail("permission denied"), Check::ok("true"));

        assert!(!can_build_accessibility_tree(&report));
    }

    #[test]
    fn accessibility_tree_is_ready_when_bus_and_toolkit_are_ready() {
        let report = accessibility_report(
            Check::ok("('unix:path=/run/user/1000/at-spi/bus',)"),
            Check::ok("true"),
        );

        assert!(can_build_accessibility_tree(&report));
    }

    #[test]
    fn capability_map_advertises_only_a_buildable_accessibility_tree() {
        let platform = platform_report();
        let portals = portal_report(Check::fail("missing"));
        let windowing = windowing_report(false, false);
        let input = input_report(false);

        for accessibility in [
            accessibility_report(Check::fail("permission denied"), Check::ok("true")),
            accessibility_report(
                Check::ok("('unix:path=/run/user/1000/at-spi/bus',)"),
                Check::ok("false"),
            ),
        ] {
            let capabilities =
                capability_map(&platform, &portals, &accessibility, &windowing, &input);
            assert!(capabilities.accessibility.is_empty());
        }
    }

    #[test]
    fn parses_parent_pid_from_proc_status() {
        let status = "Name:\ttest\nPid:\t42\nPPid:\t7\n";

        assert_eq!(parse_parent_pid(status), Some(7));
    }

    #[test]
    fn parses_nul_separated_process_environment() {
        let environment = parse_environ(
            b"DISPLAY=:0\0WAYLAND_DISPLAY=wayland-0\0EMPTY=\0NO_EQUALS\0XDG_SESSION_TYPE=wayland\0",
        );

        assert_eq!(environment.get("DISPLAY").map(String::as_str), Some(":0"));
        assert_eq!(
            environment.get("WAYLAND_DISPLAY").map(String::as_str),
            Some("wayland-0")
        );
        assert_eq!(environment.get("EMPTY").map(String::as_str), Some(""));
        assert!(!environment.contains_key("NO_EQUALS"));
    }

    #[test]
    fn desktop_env_hydration_includes_xauthority() {
        assert!(DESKTOP_ENV_KEYS.contains(&"XAUTHORITY"));
    }

    #[test]
    fn desktop_env_hydration_preserves_explicit_native_x11() {
        let current_env = HashMap::from([
            ("DISPLAY".to_string(), ":90".to_string()),
            ("XDG_SESSION_TYPE".to_string(), "x11".to_string()),
        ]);
        let host_env = HashMap::from([
            ("WAYLAND_DISPLAY".to_string(), "wayland-0".to_string()),
            (
                "XDG_CURRENT_DESKTOP".to_string(),
                "ubuntu:GNOME".to_string(),
            ),
        ]);

        let updates = desktop_env_hydration_updates(&current_env, &host_env);

        assert!(!updates.iter().any(|(key, _)| *key == "WAYLAND_DISPLAY"));
        assert!(updates
            .iter()
            .any(|(key, value)| { *key == "XDG_CURRENT_DESKTOP" && value == "ubuntu:GNOME" }));
    }

    #[test]
    fn desktop_env_hydration_still_imports_wayland_for_incomplete_sessions() {
        let current_env = HashMap::new();
        let host_env = HashMap::from([("WAYLAND_DISPLAY".to_string(), "wayland-0".to_string())]);

        let updates = desktop_env_hydration_updates(&current_env, &host_env);

        assert!(updates
            .iter()
            .any(|(key, value)| *key == "WAYLAND_DISPLAY" && value == "wayland-0"));
    }

    #[test]
    fn graphical_process_env_requires_display() {
        let with_display = HashMap::from([("DISPLAY".to_string(), ":0".to_string())]);
        let with_wayland =
            HashMap::from([("WAYLAND_DISPLAY".to_string(), "wayland-0".to_string())]);
        let without_display = HashMap::from([("XAUTHORITY".to_string(), "/tmp/xauth".to_string())]);

        assert!(process_env_has_graphical_display(&with_display));
        assert!(process_env_has_graphical_display(&with_wayland));
        assert!(!process_env_has_graphical_display(&without_display));
    }

    #[test]
    fn capabilities_prefer_xdotool_before_ydotool_on_x11() {
        let mut platform = platform_report();
        platform.xdg_session_type = Some("x11".to_string());
        platform.wayland_display = None;
        platform.display = Some(":0".to_string());
        let input = InputReport {
            ydotool: Check::ok("ydotool"),
            ydotoold: Check::ok("ydotoold"),
            ydotool_socket: Check::ok("connectable"),
            uinput: Check::fail("missing"),
            xdotool: Check::ok("xdotool"),
            wtype: Check::fail("missing wtype"),
            pixelflux: Check::fail("PIXELFLUX_CU is not set"),
        };

        let capabilities = capability_map(
            &platform,
            &portal_report(Check::fail("missing")),
            &accessibility_report(Check::fail("missing"), Check::fail("missing")),
            &windowing_report(false, false),
            &input,
        );

        assert_eq!(capabilities.input, ["xdotool", "ydotool"]);
        assert_eq!(capabilities.preferred.input.as_deref(), Some("xdotool"));
    }

    #[test]
    fn pixelflux_only_desktop_is_input_ready() {
        let platform = platform_report();
        let mut input = input_report(false);
        input.pixelflux = Check::ok("loopback port 8080");
        assert!(can_send_development_input(
            &platform,
            &Check::fail("missing portal"),
            &input,
        ));
    }

    #[test]
    fn x11_diagnostics_ignore_portal_and_accept_xdotool() {
        let mut platform = platform_report();
        platform.xdg_session_type = Some("x11".to_string());
        platform.wayland_display = None;
        platform.display = Some(":0".to_string());
        let portals = portal_report(Check::ok("org.freedesktop.portal.RemoteDesktop"));
        let accessibility = accessibility_report(Check::ok("bus"), Check::ok("true"));
        let windowing = windowing_report(true, true);
        let mut input = input_report(false);
        input.xdotool = Check::ok("xdotool");

        let capabilities = capability_map(&platform, &portals, &accessibility, &windowing, &input);
        let readiness = readiness_report(&platform, &portals, &accessibility, &windowing, &input);

        assert_eq!(capabilities.input, ["xdotool"]);
        assert_eq!(capabilities.preferred.input.as_deref(), Some("xdotool"));
        assert!(readiness.can_send_development_input);
    }

    #[test]
    fn wayland_diagnostics_advertise_wtype_without_portal_or_ydotool() {
        let mut platform = platform_report();
        platform.xdg_session_type = Some("wayland".to_string());
        platform.xdg_current_desktop = Some("Hyprland".to_string());
        platform.wayland_display = Some("wayland-0".to_string());
        let portals = portal_report(Check::fail("missing"));
        let accessibility = accessibility_report(Check::ok("bus"), Check::ok("true"));
        let windowing = windowing_report(true, true);
        let mut input = input_report(false);
        input.wtype = Check::ok("wtype");

        let capabilities = capability_map(&platform, &portals, &accessibility, &windowing, &input);
        let readiness = readiness_report(&platform, &portals, &accessibility, &windowing, &input);

        assert_eq!(capabilities.input, ["wtype"]);
        assert_eq!(capabilities.preferred.input.as_deref(), Some("wtype"));
        assert!(readiness.can_send_development_input);
    }

    #[test]
    fn wtype_excludes_known_incompatible_wayland_desktops() {
        assert!(wtype_compatible_wayland_desktop(Some("Hyprland")));
        assert!(wtype_compatible_wayland_desktop(Some("sway")));
        assert!(wtype_compatible_wayland_desktop(None));
        assert!(!wtype_compatible_wayland_desktop(Some("GNOME")));
        assert!(!wtype_compatible_wayland_desktop(Some("KDE;Plasma")));
        assert!(!wtype_compatible_wayland_desktop(Some("COSMIC")));
    }

    #[test]
    fn wayland_diagnostics_prefer_ydotool_before_portal() {
        let platform = platform_report();
        let portals = portal_report(Check::ok("org.freedesktop.portal.RemoteDesktop"));
        let accessibility = accessibility_report(Check::ok("bus"), Check::ok("true"));
        let windowing = windowing_report(true, true);
        let input = input_report_parts(
            Check::ok("ydotool"),
            Check::ok("ydotoold"),
            Check::ok("connectable"),
            Check::fail("missing uinput"),
        );

        let capabilities = capability_map(&platform, &portals, &accessibility, &windowing, &input);

        assert_eq!(capabilities.input, ["ydotool", "portal"]);
        assert_eq!(capabilities.preferred.input.as_deref(), Some("ydotool"));
    }

    #[test]
    fn writable_uinput_advertises_abs_pointer_first() {
        let platform = platform_report();
        let portals = portal_report(Check::fail("missing"));
        let accessibility = accessibility_report(Check::ok("bus"), Check::ok("true"));
        let windowing = windowing_report(true, true);
        let input = input_report_parts(
            Check::ok("ydotool"),
            Check::ok("ydotoold"),
            Check::ok("connectable"),
            Check::ok("read/write: /dev/uinput"),
        );

        let capabilities = capability_map(&platform, &portals, &accessibility, &windowing, &input);

        assert_eq!(capabilities.input, ["abs_pointer", "ydotool"]);
    }

    #[test]
    fn portal_force_order_requires_both_input_modalities() {
        assert!(force_portal_for_all_input(true, true, false, false));
        assert!(!force_portal_for_all_input(true, false, false, false));
        assert!(!force_portal_for_all_input(true, true, true, false));
        assert!(!force_portal_for_all_input(true, true, false, true));
    }

    fn remote_desktop_runtime_introspection() -> Check {
        Check::ok(
            "NAME TYPE SIGNATURE RESULT/VALUE FLAGS\n\
             .CreateSession method a{sv} o -\n\
             .SelectDevices method oa{sv} o -\n\
             .Start method osa{sv} o -\n\
             .NotifyPointerMotionAbsolute method oa{sv}udd - -\n\
             .NotifyPointerButton method oa{sv}iu - -\n\
             .NotifyPointerAxisDiscrete method oa{sv}ui - -\n\
             .NotifyKeyboardKeycode method ouu - -\n\
             .NotifyKeyboardKeysym method ouu - -\n\
             .AvailableDeviceTypes property u 3 emits-change",
        )
    }

    fn screencast_runtime_introspection() -> Check {
        Check::ok(
            "NAME TYPE SIGNATURE RESULT/VALUE FLAGS\n\
             .SelectSources method oa{sv} o -",
        )
    }

    #[test]
    fn cosmic_prefers_per_monitor_portal_over_uinput_and_ydotool() {
        let mut platform = platform_report();
        platform.xdg_session_type = Some("wayland".to_string());
        platform.xdg_current_desktop = Some("COSMIC".to_string());
        let input = input_report_parts(
            Check::ok("ydotool"),
            Check::ok("socket"),
            Check::ok("socket"),
            Check::ok("uinput"),
        );
        let capabilities = capability_map(
            &platform,
            &portal_report(Check::ok("portal")),
            &accessibility_report(Check::ok("bus"), Check::ok("true")),
            &windowing_report(true, true),
            &input,
        );
        assert_eq!(capabilities.preferred.input.as_deref(), Some("portal"));
        assert!(!capabilities
            .input
            .iter()
            .any(|backend| backend == "abs_pointer"));
    }

    #[test]
    fn remote_desktop_portal_rejects_header_only_introspection() {
        let introspection = Check::ok("NAME TYPE SIGNATURE RESULT/VALUE FLAGS");
        let available_device_types = Check::ok("u 3");
        let pointer = remote_desktop_pointer_check_from(
            &introspection,
            &screencast_runtime_introspection(),
            &available_device_types,
            &Check::ok("u 1"),
            &Check::ok("u 2"),
            &Check::ok("u 2"),
        );
        let keyboard = remote_desktop_keyboard_check_from(&introspection, &available_device_types);

        assert!(!pointer.ok);
        assert!(pointer.detail.contains("CreateSession"));
        assert!(pointer.detail.contains("NotifyPointerButton"));
        assert!(!keyboard.ok);
        assert!(keyboard.detail.contains("NotifyKeyboardKeysym"));

        let input = input_report_parts(
            Check::ok("ydotool"),
            Check::ok("ydotoold"),
            Check::fail("no connectable ydotool socket"),
            Check::ok("read/write: /dev/uinput"),
        );
        let platform = platform_report();
        let portals = portal_report(pointer);
        let accessibility = accessibility_report(Check::ok("bus"), Check::ok("true"));
        let windowing = windowing_report(true, true);
        let capabilities = capability_map_with_portal_keyboard(
            &platform,
            &portals,
            &keyboard,
            &accessibility,
            &windowing,
            &input,
        );
        let readiness = readiness_report_with_portal_keyboard(
            &platform,
            &keyboard,
            &accessibility,
            &windowing,
            &input,
            true,
        );

        assert!(!capabilities.input.iter().any(|backend| backend == "portal"));
        assert!(!readiness.can_send_development_input);
    }

    #[test]
    fn keyboard_only_portal_remains_advertised_without_pointer_capability() {
        let platform = platform_report();
        let keyboard = remote_desktop_keyboard_check_from(
            &remote_desktop_runtime_introspection(),
            &Check::ok("u 1"),
        );
        let portals = portal_report(Check::fail(
            "ScreenCast AvailableSourceTypes=2 does not include monitor sources",
        ));
        let accessibility = accessibility_report(Check::ok("bus"), Check::ok("true"));
        let windowing = windowing_report(true, true);
        let input = input_report_parts(
            Check::fail("missing ydotool"),
            Check::fail("ydotoold not running"),
            Check::fail("no connectable ydotool socket"),
            Check::fail("/dev/uinput: Permission denied"),
        );

        let capabilities = capability_map_with_portal_keyboard(
            &platform,
            &portals,
            &keyboard,
            &accessibility,
            &windowing,
            &input,
        );
        let readiness = readiness_report_with_portal_keyboard(
            &platform,
            &keyboard,
            &accessibility,
            &windowing,
            &input,
            true,
        );

        assert!(!portals.remote_desktop.ok);
        assert_eq!(capabilities.input, ["portal"]);
        assert_eq!(capabilities.preferred.input.as_deref(), Some("portal"));
        assert!(readiness.can_send_development_input);
    }

    #[test]
    fn remote_desktop_pointer_rejects_missing_screencast_contract() {
        let check = remote_desktop_pointer_check_from(
            &remote_desktop_runtime_introspection(),
            &Check::ok("NAME TYPE SIGNATURE RESULT/VALUE FLAGS"),
            &Check::ok("u 2"),
            &Check::ok("u 1"),
            &Check::ok("u 2"),
            &Check::ok("u 2"),
        );

        assert!(!check.ok);
        assert!(check.detail.contains("SelectSources"));
    }

    #[test]
    fn remote_desktop_pointer_rejects_missing_monitor_source_type() {
        let check = remote_desktop_pointer_check_from(
            &remote_desktop_runtime_introspection(),
            &screencast_runtime_introspection(),
            &Check::ok("u 2"),
            &Check::ok("u 2"),
            &Check::ok("u 2"),
            &Check::ok("u 2"),
        );

        assert!(!check.ok);
        assert!(check.detail.contains("does not include monitor sources"));
    }

    #[test]
    fn remote_desktop_portal_rejects_missing_keyboard_device_type() {
        let check = remote_desktop_keyboard_check_from(
            &remote_desktop_runtime_introspection(),
            &Check::ok("u 2"),
        );

        assert!(!check.ok);
        assert!(check.detail.contains("does not include keyboard input"));
    }

    #[test]
    fn remote_desktop_portal_accepts_runtime_keyboard_contract() {
        let check = remote_desktop_keyboard_check_from(
            &remote_desktop_runtime_introspection(),
            &Check::ok("u 3"),
        );

        assert!(check.ok);
        assert!(check.detail.contains("AvailableDeviceTypes=3"));
    }

    #[test]
    fn pointer_only_portal_remains_advertised_without_keyboard_readiness() {
        let platform = platform_report();
        let available_device_types = Check::ok("u 2");
        let pointer = remote_desktop_pointer_check_from(
            &remote_desktop_runtime_introspection(),
            &screencast_runtime_introspection(),
            &available_device_types,
            &Check::ok("u 1"),
            &Check::ok("u 2"),
            &Check::ok("u 1"),
        );
        let keyboard = remote_desktop_keyboard_check_from(
            &remote_desktop_runtime_introspection(),
            &available_device_types,
        );
        let portals = portal_report(pointer);
        let accessibility = accessibility_report(Check::ok("bus"), Check::ok("true"));
        let windowing = windowing_report(true, true);
        let input = input_report_parts(
            Check::fail("missing ydotool"),
            Check::fail("ydotoold not running"),
            Check::fail("no connectable ydotool socket"),
            Check::fail("/dev/uinput: Permission denied"),
        );

        let capabilities = capability_map_with_portal_keyboard(
            &platform,
            &portals,
            &keyboard,
            &accessibility,
            &windowing,
            &input,
        );
        let readiness = readiness_report_with_portal_keyboard(
            &platform,
            &keyboard,
            &accessibility_report(Check::ok("bus"), Check::ok("true")),
            &windowing_report(true, true),
            &input,
            true,
        );

        assert!(portals.remote_desktop.ok);
        assert!(capabilities.input.iter().any(|backend| backend == "portal"));
        assert!(!readiness.can_send_development_input);
    }

    #[test]
    fn capabilities_require_display_to_advertise_xdotool() {
        let mut platform = platform_report();
        platform.xdg_session_type = Some("x11".to_string());
        platform.wayland_display = None;
        platform.display = None;
        let input = InputReport {
            ydotool: Check::ok("ydotool"),
            ydotoold: Check::ok("ydotoold"),
            ydotool_socket: Check::ok("connectable"),
            uinput: Check::fail("missing"),
            xdotool: Check::ok("xdotool"),
            wtype: Check::fail("missing wtype"),
            pixelflux: Check::fail("PIXELFLUX_CU is not set"),
        };

        let capabilities = capability_map(
            &platform,
            &portal_report(Check::fail("missing")),
            &accessibility_report(Check::fail("missing"), Check::fail("missing")),
            &windowing_report(false, false),
            &input,
        );

        assert_eq!(capabilities.input, ["ydotool"]);
        assert_eq!(capabilities.preferred.input.as_deref(), Some("ydotool"));
    }

    #[test]
    fn xdotool_diagnostics_force_precedence_matches_runtime() {
        let mut platform = platform_report();
        platform.xdg_session_type = Some("wayland".to_string());
        platform.display = Some(":0".to_string());
        let mut input = input_report(false);
        input.xdotool = Check::ok("xdotool");

        assert!(should_advertise_xdotool(&platform, &input, false, true));
        assert!(!should_advertise_xdotool(&platform, &input, true, true));
    }

    #[test]
    fn parses_systemd_show_environment_output() {
        let environment = parse_line_environment(
            b"DISPLAY=:0\nHYPRLAND_INSTANCE_SIGNATURE=abc\nNO_EQUALS\nYDOTOOL_SOCKET=/run/ydotoold/socket\n",
        );

        assert_eq!(environment.get("DISPLAY").map(String::as_str), Some(":0"));
        assert_eq!(
            environment
                .get("HYPRLAND_INSTANCE_SIGNATURE")
                .map(String::as_str),
            Some("abc")
        );
        assert_eq!(
            environment.get("YDOTOOL_SOCKET").map(String::as_str),
            Some("/run/ydotoold/socket")
        );
        assert!(!environment.contains_key("NO_EQUALS"));
    }

    #[test]
    fn readiness_requires_exact_window_focus_for_targeted_input() {
        let platform = platform_report();
        let accessibility = accessibility_report(Check::ok("bus"), Check::ok("true"));
        let windowing = windowing_report(true, false);
        let input = input_report(true);

        let mut readiness = readiness_report(
            &platform,
            &portal_report(Check::fail("missing")),
            &accessibility,
            &windowing,
            &input,
        );

        readiness.record_screenshot_result(true);
        assert!(readiness.can_capture_screenshots);

        assert!(readiness.can_query_windows);
        assert!(!readiness.can_focus_windows);
        assert!(readiness
            .recommended_next_step
            .contains("exact-focus window backend"));
        assert!(readiness
            .blockers
            .iter()
            .any(|blocker| blocker.contains("Exact window activation")));
    }

    #[test]
    fn header_only_introspection_is_not_an_exported_interface() {
        // busctl introspect of an interface the portal lacks: header line, exit 0.
        let header_only = Check::ok("NAME TYPE SIGNATURE RESULT/VALUE FLAGS");

        let check = require_busctl_methods(
            header_only,
            "org.freedesktop.portal.Screenshot",
            SCREENSHOT_PORTAL_METHODS,
        );

        assert!(!check.ok);
        assert!(check.detail.contains("is not exported by the portal"));
    }

    #[test]
    fn portal_interface_must_export_every_required_method() {
        let partial = Check::ok(
            "NAME        TYPE     SIGNATURE RESULT/VALUE FLAGS\n.PickColor  method   sa{sv}    o            -\n.version    property u         2            emits-change",
        );
        let check = require_busctl_methods(
            partial,
            "org.freedesktop.portal.Screenshot",
            SCREENSHOT_PORTAL_METHODS,
        );
        assert!(!check.ok);
        assert!(check
            .detail
            .contains("missing required methods: Screenshot"));

        let full = Check::ok(
            "NAME        TYPE     SIGNATURE RESULT/VALUE FLAGS\n.PickColor  method   sa{sv}    o            -\n.Screenshot method   sa{sv}    o            -",
        );
        assert!(
            require_busctl_methods(
                full,
                "org.freedesktop.portal.Screenshot",
                SCREENSHOT_PORTAL_METHODS
            )
            .ok
        );

        let failed = Check::fail("busctl: No such interface");
        let passthrough = require_busctl_methods(
            failed,
            "org.freedesktop.portal.Screenshot",
            SCREENSHOT_PORTAL_METHODS,
        );
        assert!(!passthrough.ok);
        assert_eq!(passthrough.detail, "busctl: No such interface");
    }

    #[test]
    fn readiness_blocks_when_no_screenshot_route_is_detected() {
        let mut platform = platform_report();
        platform.gnome_shell_version = Check::fail("No such file or directory (os error 2)");
        platform.gnome_screenshot = Check::fail("No such file or directory (os error 2)");
        let accessibility = accessibility_report(Check::ok("bus"), Check::ok("true"));
        let windowing = windowing_report(true, true);
        let input = input_report(true);
        let portals = portal_report(Check::fail("missing"));

        let readiness = readiness_report(&platform, &portals, &accessibility, &windowing, &input);

        assert!(!readiness.can_capture_screenshots);
        assert_eq!(
            readiness.screenshot_capture_status,
            ScreenshotCaptureStatus::Unavailable
        );
        assert!(readiness.warnings.is_empty());
        assert!(readiness
            .blockers
            .iter()
            .any(|blocker| blocker.contains("No screenshot route was detected")));
        assert!(readiness
            .recommended_next_step
            .contains("Enable a screenshot route"));
        let capabilities = capability_map(&platform, &portals, &accessibility, &windowing, &input);
        assert!(capabilities.screenshot.is_empty());
        assert_eq!(capabilities.preferred.screenshot, None);
    }

    #[test]
    fn native_x11_display_is_a_screenshot_route_ahead_of_gnome_screenshot() {
        let mut platform = platform_report();
        platform.gnome_shell_version = Check::fail("missing");
        platform.x11_display = Check::ok("native X11 root window 2880x1920, depth 24");
        let portals = portal_report(Check::fail("missing"));
        let accessibility = accessibility_report(Check::ok("bus"), Check::ok("true"));
        let windowing = windowing_report(true, true);
        let input = input_report(true);

        let capabilities = capability_map(&platform, &portals, &accessibility, &windowing, &input);
        let readiness = readiness_report(&platform, &portals, &accessibility, &windowing, &input);

        assert_eq!(capabilities.screenshot, ["x11", "gnome_screenshot"]);
        assert_eq!(capabilities.preferred.screenshot.as_deref(), Some("x11"));
        assert!(!readiness.can_capture_screenshots);
        assert_eq!(
            readiness.screenshot_capture_status,
            ScreenshotCaptureStatus::Unverified
        );
        assert!(readiness.blockers.is_empty());
    }

    #[test]
    fn screenshot_route_detection_is_not_capture_verification() {
        let mut platform = platform_report();
        platform.gnome_shell_version = Check::fail("missing");
        platform.gnome_screenshot = Check::fail("missing");
        let mut portals = portal_report(Check::fail("missing"));
        portals.screenshot = Check::ok(".Screenshot method sa{sv} o -");
        let accessibility = accessibility_report(Check::ok("bus"), Check::ok("true"));
        let windowing = windowing_report(true, true);
        let input = input_report(true);

        let mut readiness =
            readiness_report(&platform, &portals, &accessibility, &windowing, &input);
        let capabilities = capability_map(&platform, &portals, &accessibility, &windowing, &input);

        assert!(!readiness.can_capture_screenshots);
        assert_eq!(
            readiness.screenshot_capture_status,
            ScreenshotCaptureStatus::Unverified
        );
        assert_eq!(readiness.warnings, [SCREENSHOT_UNVERIFIED]);
        assert!(readiness.blockers.is_empty());
        assert_eq!(capabilities.screenshot, ["portal"]);

        readiness.record_screenshot_result(false);
        assert!(!readiness.can_capture_screenshots);
        assert_eq!(
            readiness.screenshot_capture_status,
            ScreenshotCaptureStatus::Failed
        );
        assert_eq!(readiness.blockers, [SCREENSHOT_FAILED]);
        assert!(readiness.warnings.is_empty());
        assert_eq!(readiness.recommended_next_step, SCREENSHOT_FAILED_NEXT_STEP);

        readiness.record_screenshot_result(true);
        assert!(readiness.can_capture_screenshots);
        assert_eq!(
            readiness.screenshot_capture_status,
            ScreenshotCaptureStatus::Verified
        );
        assert!(readiness.blockers.is_empty());
        assert_eq!(readiness.recommended_next_step, READY_NEXT_STEP);
    }

    #[test]
    fn readiness_treats_kwin_as_full_window_backend() {
        let platform = platform_report();
        let accessibility = accessibility_report(Check::ok("bus"), Check::ok("true"));
        let mut windowing = windowing_report(false, false);
        windowing.kwin = Check::ok("KWin scripting is available");
        windowing.can_list_windows = true;
        windowing.can_focus_apps = true;
        windowing.can_focus_windows = true;
        let input = input_report(true);

        let readiness = readiness_report(
            &platform,
            &portal_report(Check::fail("missing")),
            &accessibility,
            &windowing,
            &input,
        );

        assert!(readiness.can_query_windows);
        assert!(readiness.can_focus_apps);
        assert!(readiness.can_focus_windows);
        assert!(readiness.blockers.is_empty());
    }

    #[test]
    fn readiness_message_mentions_generic_window_targeting() {
        let platform = platform_report();
        let accessibility = accessibility_report(Check::ok("bus"), Check::ok("true"));
        let windowing = windowing_report(true, true);
        let input = input_report(true);

        let readiness = readiness_report(
            &platform,
            &portal_report(Check::fail("missing")),
            &accessibility,
            &windowing,
            &input,
        );

        assert!(readiness.blockers.is_empty());
        assert!(readiness
            .recommended_next_step
            .contains("AT-SPI tree support"));
        assert!(readiness.recommended_next_step.contains("window targeting"));
        assert!(!readiness
            .recommended_next_step
            .contains("GNOME window targeting"));
    }

    #[test]
    fn readiness_accepts_connectable_ydotool_socket_without_direct_uinput_access() {
        let platform = platform_report();
        let accessibility = accessibility_report(Check::ok("bus"), Check::ok("true"));
        let windowing = windowing_report(true, true);
        let input = input_report_parts(
            Check::ok("ydotool"),
            Check::ok("ydotoold"),
            Check::ok("connectable: /tmp/.ydotool_socket"),
            Check::fail("/dev/uinput: Permission denied"),
        );

        let readiness = readiness_report(
            &platform,
            &portal_report(Check::fail("missing")),
            &accessibility,
            &windowing,
            &input,
        );

        assert!(readiness.can_send_development_input);
        assert!(readiness.blockers.is_empty());
    }

    #[test]
    fn readiness_uses_connectable_ydotool_socket_when_process_probe_fails() {
        let platform = platform_report();
        let accessibility = accessibility_report(Check::ok("bus"), Check::ok("true"));
        let windowing = windowing_report(true, true);
        let input = input_report_parts(
            Check::ok("ydotool"),
            Check::fail("ydotoold process name not found"),
            Check::ok("connectable: /run/user/1000/.ydotool_socket"),
            Check::fail("/dev/uinput: Permission denied"),
        );
        let portals = portal_report(Check::fail("missing"));

        let capabilities = capability_map(&platform, &portals, &accessibility, &windowing, &input);
        let readiness = readiness_report(&platform, &portals, &accessibility, &windowing, &input);

        assert!(capabilities
            .input
            .iter()
            .any(|backend| backend == "ydotool"));
        assert!(readiness.can_send_development_input);
    }

    #[test]
    fn wayland_readiness_rejects_pointer_only_uinput_without_keyboard_backend() {
        let platform = platform_report();
        let accessibility = accessibility_report(Check::ok("bus"), Check::ok("true"));
        let windowing = windowing_report(true, true);
        let input = input_report_parts(
            Check::ok("ydotool"),
            Check::fail("ydotoold not running"),
            Check::fail("no connectable ydotool socket"),
            Check::ok("read/write: /dev/uinput"),
        );

        let readiness = readiness_report(
            &platform,
            &portal_report(Check::fail("missing")),
            &accessibility,
            &windowing,
            &input,
        );

        assert!(!readiness.can_send_development_input);
        assert!(readiness
            .blockers
            .iter()
            .any(|blocker| blocker.contains("absolute pointer input")));
        assert!(readiness
            .recommended_next_step
            .contains("keyboard-capable input backend"));
    }

    #[test]
    fn x11_readiness_rejects_pointer_only_uinput_without_keyboard_backend() {
        let mut platform = platform_report();
        platform.xdg_session_type = Some("x11".to_string());
        platform.wayland_display = None;
        let accessibility = accessibility_report(Check::ok("bus"), Check::ok("true"));
        let windowing = windowing_report(true, true);
        let input = input_report_parts(
            Check::fail("missing ydotool"),
            Check::fail("ydotoold not running"),
            Check::fail("no connectable ydotool socket"),
            Check::ok("read/write: /dev/uinput"),
        );

        let readiness = readiness_report(
            &platform,
            &portal_report(Check::fail("missing")),
            &accessibility,
            &windowing,
            &input,
        );

        assert!(!readiness.can_send_development_input);
    }

    #[test]
    fn readiness_accepts_remote_desktop_portal_without_local_input_backend() {
        let platform = platform_report();
        let accessibility = accessibility_report(Check::ok("bus"), Check::ok("true"));
        let windowing = windowing_report(true, true);
        let input = input_report_parts(
            Check::fail("missing ydotool"),
            Check::fail("ydotoold not running"),
            Check::fail("no connectable ydotool socket"),
            Check::fail("/dev/uinput: Permission denied"),
        );

        let readiness = readiness_report(
            &platform,
            &portal_report(Check::ok("org.freedesktop.portal.RemoteDesktop")),
            &accessibility,
            &windowing,
            &input,
        );

        assert!(readiness.can_send_development_input);
        assert!(readiness.blockers.is_empty());
    }

    #[test]
    fn readiness_rejects_inaccessible_ydotool_paths() {
        let platform = platform_report();
        let accessibility = accessibility_report(Check::ok("bus"), Check::ok("true"));
        let windowing = windowing_report(true, true);
        let input = input_report_parts(
            Check::ok("ydotool"),
            Check::ok("ydotoold"),
            Check::fail("/tmp/.ydotool_socket: Permission denied"),
            Check::fail("/dev/uinput: Permission denied"),
        );

        let readiness = readiness_report(
            &platform,
            &portal_report(Check::fail("missing")),
            &accessibility,
            &windowing,
            &input,
        );

        assert!(!readiness.can_send_development_input);
        assert!(readiness
            .recommended_next_step
            .contains("Enable a keyboard-capable input backend"));
        assert!(readiness
            .blockers
            .iter()
            .any(|blocker| blocker.contains("Development keyboard input is unavailable")));
    }

    #[test]
    fn capability_map_rejects_incompatible_ydotool_with_live_daemon() {
        let platform = platform_report();
        let portals = portal_report(Check::fail("missing"));
        let accessibility = accessibility_report(Check::ok("bus"), Check::ok("true"));
        let windowing = windowing_report(true, true);
        let input = input_report_parts(
            Check::fail("unsupported legacy ydotool CLI"),
            Check::ok("ydotoold"),
            Check::ok("connectable socket"),
            Check::fail("/dev/uinput: Permission denied"),
        );

        let capabilities = capability_map(&platform, &portals, &accessibility, &windowing, &input);
        let readiness = readiness_report(&platform, &portals, &accessibility, &windowing, &input);

        assert!(!capabilities
            .input
            .iter()
            .any(|backend| backend == "ydotool"));
        assert!(!readiness.can_send_development_input);
    }

    #[test]
    fn ydotool_socket_check_rejects_legacy_stream_socket() {
        let dir = crate::test_sockets::socket_fixture(
            &format!("cul-diag-stream-{}", std::process::id()),
            "ydotool.sock",
        );
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).expect("create temp diagnostics dir");
        let socket = dir.join("ydotool.sock");
        let listener =
            std::os::unix::net::UnixListener::bind(&socket).expect("bind temp diagnostics socket");

        let check = socket_connect_check(&socket);

        assert!(!check.ok, "{check:?}");
        drop(listener);
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn ydotool_socket_check_accepts_datagram_socket() {
        let dir = crate::test_sockets::socket_fixture(
            &format!("cul-diag-dgram-{}", std::process::id()),
            "ydotool.sock",
        );
        let _ = std::fs::remove_dir_all(&dir);
        std::fs::create_dir_all(&dir).expect("create temp diagnostics dir");
        let socket = dir.join("ydotool.sock");
        let datagram =
            std::os::unix::net::UnixDatagram::bind(&socket).expect("bind temp datagram socket");

        let check = socket_connect_check(&socket);

        assert!(check.ok, "{check:?}");
        drop(datagram);
        let _ = std::fs::remove_dir_all(&dir);
    }

    #[test]
    fn readiness_reports_cosmic_window_blocker_on_cosmic() {
        let mut platform = platform_report();
        platform.xdg_current_desktop = Some("COSMIC".to_string());
        let accessibility = accessibility_report(Check::ok("bus"), Check::ok("true"));
        let windowing = windowing_report(false, false);
        let input = input_report(true);

        let readiness = readiness_report(
            &platform,
            &portal_report(Check::fail("missing")),
            &accessibility,
            &windowing,
            &input,
        );

        assert!(readiness
            .blockers
            .iter()
            .any(|blocker| blocker.contains("COSMIC Wayland window introspection")));
    }

    #[test]
    fn desktop_env_hydration_imports_the_niri_socket() {
        let current_env = HashMap::new();
        let session_env = HashMap::from([(
            "NIRI_SOCKET".to_string(),
            "/run/user/1000/niri.wayland-1.42.sock".to_string(),
        )]);

        let updates = desktop_env_hydration_updates(&current_env, &session_env);

        assert!(updates.iter().any(|(key, value)| {
            *key == "NIRI_SOCKET" && value == "/run/user/1000/niri.wayland-1.42.sock"
        }));
    }

    #[test]
    fn wtype_capability_honors_keyboard_backend_overrides() {
        let mut platform = platform_report();
        platform.xdg_session_type = Some("wayland".to_string());
        platform.xdg_current_desktop = Some("Hyprland".to_string());
        platform.wayland_display = Some("wayland-0".to_string());
        let mut input = input_report(false);
        input.wtype = Check::ok("wtype");

        assert!(should_advertise_wtype(
            &platform, &input, false, false, false
        ));
        assert!(!should_advertise_wtype(
            &platform, &input, true, false, false
        ));
        assert!(!should_advertise_wtype(
            &platform, &input, false, false, true
        ));

        // Forcing xdotool hides wtype only when xdotool can run: installed,
        // with DISPLAY set, as the server's keyboard routing requires.
        assert!(should_advertise_wtype(
            &platform, &input, false, true, false
        ));
        input.xdotool = Check::ok("xdotool");
        assert!(!should_advertise_wtype(
            &platform, &input, false, true, false
        ));
        platform.display = None;
        assert!(should_advertise_wtype(
            &platform, &input, false, true, false
        ));
    }

    #[test]
    fn remote_desktop_pointer_rejects_missing_hidden_cursor_mode() {
        let check = remote_desktop_pointer_check_from(
            &remote_desktop_runtime_introspection(),
            &screencast_runtime_introspection(),
            &Check::ok("u 2"),
            &Check::ok("u 1"),
            &Check::ok("u 2"),
            &Check::ok("u 2"),
        );

        assert!(!check.ok);
        assert!(check.detail.contains("does not include hidden cursor mode"));
    }

    #[test]
    fn remote_desktop_pointer_accepts_screencast_v1_default_hidden_cursor() {
        let check = remote_desktop_pointer_check_from(
            &remote_desktop_runtime_introspection(),
            &screencast_runtime_introspection(),
            &Check::ok("u 2"),
            &Check::ok("u 1"),
            &Check::ok("u 1"),
            &Check::fail("AvailableCursorModes is not available on ScreenCast v1"),
        );

        assert!(check.ok);
        assert!(check
            .detail
            .contains("hidden cursor mode is the interface default"));
    }

    #[test]
    fn capability_map_reports_niri_window_control() {
        let platform = platform_report();
        let portals = portal_report(Check::fail("missing"));
        let accessibility = accessibility_report(Check::ok("bus"), Check::ok("true"));
        let mut windowing = windowing_report(false, false);
        windowing.niri = Check::ok("niri msg returned windows");
        let input = input_report(false);

        let capabilities = capability_map(&platform, &portals, &accessibility, &windowing, &input);

        assert_eq!(capabilities.window_control, vec![NIRI_BACKEND]);
        assert_eq!(
            capabilities.preferred.window_control.as_deref(),
            Some(NIRI_BACKEND)
        );
    }
}