summaryrefslogtreecommitdiffstats
path: root/tests/test_mainwindow.cpp
blob: ecaab2f2c4156b3b6a07d6b6bd2499376069ea8a (plain)
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
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
/*
 * qtmaildir - a Qt6 mail client for notmuch-indexed Maildirs
 * Copyright (C) 2026 Danilo M. <danix@danix.xyz>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <QProcess>
#include <QtTest>

#include <QAction>
#include <QApplication>
#include <QFocusEvent>
#include <QCloseEvent>
#include <QDir>
#include <QKeyEvent>
#include <QLabel>
#include <QLineEdit>
#include <QMenuBar>
#include <QMenu>
#include <QPushButton>
#include <QProgressBar>
#include <QFile>
#include <QSet>
#include <QSettings>
#include <QStandardPaths>
#include <QTemporaryDir>

#include <QStyle>
#include <QSplitter>
#include <QTableView>
#include <QToolBar>
#include <QTreeView>
#include <QTimer>

#include "config.h"
#include "keymap.h"
#include "mainwindow.h"
#include "messageview.h"
#include "mimeparser.h"
#include "notmuchworker.h"
#include "carddelegate.h"
#include "composewindow.h"
#include "senddialog.h"
#include "messagesender.h"
#include <QCheckBox>
#include <QPlainTextEdit>
#include <QPointer>
#include <QListWidget>
#include "cardlayout.h"

#include <QImage>
#include <QPainter>
#include <QToolButton>
#include <QHBoxLayout>
#include <QComboBox>
#include <QScrollBar>
#include "tagchip.h"
#include "tagstrip.h"
#include "threadlistmodel.h"
#include "threadlistview.h"
#include "notmuchfixture.h"

/// A MainWindow whose worker is pointed at a throwaway notmuch database.
///
/// **Opt-in, never a suite-wide initTestCase.** Roughly fifty cases here
/// construct a bare MainWindow and must keep costing nothing; making every one
/// of them run `notmuch new` would be minutes of wall clock for a database they
/// never query. Shared mutable state between cases is also how the /proc/locks
/// bug (item 61) reached the whole suite.
///
/// **No test-only hook in MainWindow, deliberately.** wireWorker() already
/// builds the worker from m_config.notmuchConfig(), an ordinary config key, so
/// pointing a written qtmaildir.conf at the fixture exercises the shipping code
/// path rather than a parallel one built for the tests.
///
/// Owns the fixture, because the worker holds the database open on another
/// thread and the fixture's QTemporaryDir deletes the tree in its destructor:
/// it must outlive the window.
class WorkerBackedWindow
{
public:
    /// Builds the database, writes the config and loads it. Check isValid()
    /// and error() before constructing the window.
    /// `accountKey` and `accountMaildir` add one [account.<key>] section, which
    /// is what makes runQuery() scope the bar's text with scopedQuery(). A test
    /// that never selects an account can leave them empty.
    ///
    /// `accountTrash` writes that section's `trash` key, which Delete needs to
    /// know where to move a file to. A DEFAULTED parameter rather than an
    /// overload: an overload would have to repeat the whole body, and every
    /// existing caller passes no account at all and so writes no section and
    /// no trash key either. A caller that names an account and wants Delete to
    /// work has to say where its trash is, which is the same requirement the
    /// real config imposes.
    /// One [account.<key>] section to write.
    ///
    /// `sendCommand` is what makes the account able to send, and its EMPTINESS
    /// is what makes it receive-only: the capability is the key's presence,
    /// not a separate flag, so a receive-only account is written by omitting
    /// it exactly as the real config expresses it.
    struct AccountSpec
    {
        QString key;
        QString maildir;
        QString trash;
        QString sendCommand;
        QString address;
    };

    /// Writes several accounts, for the compose cases.
    ///
    /// Beside build() rather than replacing it: every existing caller passes
    /// at most one account and none of them needs a send command, so widening
    /// the three-argument signature further would make ten call sites carry
    /// two empty strings each for one test's benefit.
    bool buildWithAccounts(const QList<AccountSpec> &accounts,
                           const QString &composeKey = QString())
    {
        m_accounts = accounts;
        m_composeKey = composeKey;
        return build();
    }

    bool build(const QString &accountKey = QString(),
               const QString &accountMaildir = QString(),
               const QString &accountTrash = QString())
    {
        if (!m_fixture.isValid()) {
            m_error = QStringLiteral("fixture directory invalid");
            return false;
        }
        if (!m_fixture.index()) {
            m_error = m_fixture.error();
            return false;
        }
        if (!m_confDir.isValid()) {
            m_error = QStringLiteral("config directory invalid");
            return false;
        }

        const QString path =
            m_confDir.filePath(QStringLiteral("qtmaildir.conf"));
        QFile file(path);
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text)) {
            m_error = QStringLiteral("cannot write qtmaildir.conf");
            return false;
        }
        {
            QTextStream out(&file);
            // QSettings' INI backend treats a section literally named
            // [general] as its own fallback section and strips the prefix, so
            // this key is read as `notmuch_config` and NOT as
            // `general/notmuch_config`. Getting that backwards is how the key
            // went unnoticed as broken once already.
            out << "[general]\n"
                << "notmuch_config=" << m_fixture.configPath() << "\n";
            if (!accountKey.isEmpty()) {
                // QSettings reads `/` in a section name as a group separator,
                // so the section is [account.key], never [account/key].
                out << "\n[account." << accountKey << "]\n"
                    << "maildir=" << accountMaildir << "\n";
                if (!accountTrash.isEmpty())
                    out << "trash=" << accountTrash << "\n";
                // The fixture's folders are lowercase, unlike the Maildir
                // convention Account::inboxFolder() defaults to. Stated rather
                // than assumed, which is the whole point of the key: naming a
                // folder that does not exist would CREATE it.
                out << "inbox=inbox\n";
            }
            if (!m_composeKey.isEmpty())
                out << "\n[compose]\n" << m_composeKey << "\n";
            for (const AccountSpec &account : m_accounts) {
                out << "\n[account." << account.key << "]\n"
                    << "maildir=" << account.maildir << "\n"
                    << "inbox=inbox\n";
                if (!account.trash.isEmpty())
                    out << "trash=" << account.trash << "\n";
                if (!account.address.isEmpty())
                    out << "address=" << account.address << "\n";
                // Written only when non-empty. An account with no
                // send_command is receive-only, which is the shape under test.
                if (!account.sendCommand.isEmpty())
                    out << "send_command=" << account.sendCommand << "\n";
            }
        }
        file.close();

        m_config.load(path);
        if (m_config.notmuchConfig() != m_fixture.configPath()) {
            m_error = QStringLiteral("config did not pick up notmuch_config");
            return false;
        }
        return true;
    }

    NotmuchFixture &fixture() { return m_fixture; }
    const Config &config() const { return m_config; }
    QString error() const { return m_error; }

private:
    NotmuchFixture m_fixture;
    QTemporaryDir m_confDir;
    Config m_config;
    QString m_error;
    QList<AccountSpec> m_accounts;
    QString m_composeKey;
};

/// MainWindow is mostly wiring. Cases that need a real database opt into one
/// through WorkerBackedWindow; the rest construct a bare window. What is
/// checked here is the action registry: the bindings a user configures reach
/// the QActions the menus and the keyboard both read from, and no action is
/// left unreachable.
class TestMainWindow : public QObject
{
    Q_OBJECT
private slots:
    void init();
    void cleanup();
    void noTestCanSeeTheRealLockTable();
    void everyKnownActionIsRegistered();
    void everyRegisteredActionIsKnown();
    void configuredBindingReachesTheAction();
    void cidPrefixesAreBangFree();
    void cidPrefixesAreDistinctPerMessage();
    void uiStateIsNotWrittenIntoTheUserConfig();
    void uiStateSurvivesARestart();
    void missingUiStateLeavesTheDefaults();
    void splitterStateFromAWiderWindowKeepsTheMessagePane();
    void usableSplitterStateIsRestoredUntouched();
    void headerStateFromADifferentColumnLayoutIsDiscarded();
    void returnInTheQueryBarRunsTheQueryNotOpenThread();
    void markReadTimerRestartsRatherThanStacking();
    void markReadTimerIsNotArmedForAReadThread();
    void aConfirmedEditArmsTheAutoSync();
    void autoSyncDebouncesABurstOfEdits();
    void aSearchFromThePaneReplacesTheQuery();
    void aSearchFromThePaneCanNarrowTheQuery();
    void narrowingAnEmptyQueryBarIsAPlainSearch();
    void aMalformedAccountIsReportedWithoutBlockingTheConstructor();
    void aWorkerBackedWindowReturnsRealThreads();

    // Compose and send, item 123 task 12.
    void theMailRootComesFromTheConfigNotTheIndex();
    void replyIsDisabledOnAReceiveOnlyAccountsMail();
    void theReceiveOnlyRibbonNamesTheAccount();
    void replyIsEnabledOnASendingAccountsMail();
    void composeIsDisabledOnlyWhenNoAccountCanSend();
    void quittingWithACleanComposerAsksNothing();
    void quittingWithUnsavedEditsReportsEveryComposer();
    void closingAComposerCompactsTheRegistry();
    void savingAMessageRefusesToEscapeTheChosenDirectory();
    void aHostileSubjectCannotEscapeTheSaveDirectory();
    void savingTwiceDoesNotOverwriteTheFirstFile();
    void savingAMessageWithAHostileSubjectStaysInTheDirectory();
    void aStuckComposeRequestDoesNotHijackTheNextPaneLoad();
    void theSaveLoopToleratesAComposerClosedUnderTheDialog();
    void forwardingCarriesTheOriginalsAttachments();
    void forwardSeedsHtmlFromTheConfigNotTheOriginal();
    void aStartupAccountScopesTheStartupQuery();
    void aStartupAccountAlsoScopesASavedStartupQuery();
    void aGeneratedStartupQueryActuallyRuns();
    void everyBuiltinFilterButtonCarriesAnIconAndItsText();
    void aQueryInTheMenuCanActuallyBeRun();
    void theFourBuiltinFiltersAreOnTheRowInOrder();
    void aFilterComposesWithTheSelectedAccount();
    void aFilterAcrossAllAccountsIsUnscoped();
    void aFilterDoesNotClearTheAccountSelection();
    void theActiveFilterButtonIsChecked();
    void aHandEditedQueryChecksNoFilterButton();
    void theCheckedFilterFollowsTheAccount();
    void aSavedQueryStillClearsTheAccountSelection();
    void aFilterOffersNoEditOrDeleteActions();
    void changingTheAccountRunsNothing();
    void arrivingBatchesUpdateTheStatusBarWithTheCountSoFar();
    void aRefreshsBatchesLeaveTheStatusBarAlone();
    void selectingAThreadRootShowsItInTheMessagePane();
    void anUnexpandedRootRendersOneMessageNotTheConversation();
    void aSingleMessageIdQuerysCardOpensInTheMessagePane();
    void autoSyncIsNotArmedWhenDisabledOrWithNothingPending();
    void autoSyncSkipsWhileABackgroundSyncIsRunning();
    void aSkippedAutoSyncRearmsRatherThanGivingUp();
    void aHeldEditIsSentBeforeTheSyncEndRefreshReadsTheDatabase();
    void aSuccessfulSyncRefreshesRatherThanRerunningTheQuery();
    void markReadCanBeDisabled();
    void pendingEditCountSurvivesAQuery();
    void aFailedSyncDoesNotClearThePendingCount();
    void closingWithNoPendingEditsDoesNotPrompt();
    void syncOnExitNeverClosesSilently();
    void selectAllIsBoundAndSelectsEveryRow();
    void aMultiRowSelectionDoesNotArmTheMarkReadTimer();
    void growingASelectionCancelsAnAlreadyArmedTimer();
    void collapsingBackToOneRowLoadsThatThreadAgain();
    void theStatusBarReportsAMultiRowSelection();
    void clearSelectionBlanksThePaneAndDeselects();
    void clearPaneLeavesTheSelectionAlone();
    void maildirOverviewShowsUnknownRatherThanZero();
    void maildirOverviewIgnoresAStaleReply();
    void theThreadListOffersAContextMenu();
    void aSecondRowBlanksThePaneNotOnlyAThird();
    void aLocalSyncIsNotReportedAsABackgroundOne();
    void aLocalSyncsOwnLockIsNeverReportedAsBackground();
    void aSkippedLocalSyncStillReportsTheOtherRunFinishing();
    void aCronSyncRefreshesTheListWithoutAQuery();
    void aCronSyncRefreshesOverASelectionWithoutClearingIt();
    void aCronSyncRefreshesTheLastRunQueryNotTheQueryBar();
    void aRefreshAddsNewMailAndDropsWhatStoppedMatching();
    void theOpenThreadLeavingTheListRaisesTheStaleNotice();
    void aThreadStillMatchingRaisesNoStaleNotice();
    void theStaleNoticeCarriesTheMessageBeingRead();
    void recoveringAStaleThreadQueriesTheWholeThread();
    void recoveryReselectsTheMessageThatWasBeingRead();
    void recoveryOnTheFirstMessageSelectsTheThreadRow();
    void doubleClickingAThreadOpensThatThreadAlone();
    void doubleClickingAReplyOpensItsThreadNotTheReplyAlone();
    void doubleClickingDoesNotLeaveTheMarkReadTimerArmed();
    void aUserQueryAbandonsAPendingRecovery();
    void blankingThePaneAlsoDropsTheStaleNotice();
    void aNewQueryDropsTheStaleNotice();
    void aFinishedBackgroundSyncStopsSayingItIsRunning();
    void aRefreshDoesNotStampOverASelectionMessage();
    void aRefreshDoesNotOpenNewMailByItself();
    void openingAnotherMessageDropsTheStaleNoticeOfThePreviousOne();
    void theStaleNoticeKeepsTheMessageOfAThreadRootToo();
    void recoveryExpandsTheThreadAndSelectsRatherThanOnlyPointing();
    void recoveryFromAnExpandedThreadRestoresTheReply();
    void theRecoveryButtonSurvivesThePaneBeingBlanked();
    void anUnobservableLockTableLeavesTheSyncButtonUsable();
    void theStatusBarFollowsTheSyncPhase();
    void aSelectedReadThreadIsNotDimmedIntoTheHighlight();
    void childRowsAreIndentedUnderTheirThread();
    void aThreadWithRepliesDrawsAVisibleExpander();
    void cardsNeverScrollSideways();
    void selectingARootCardKeepsItsThreadForMarkRead();
    void nextThreadLeavesTheLastReply();
    void altDownSkipsReplies();
    void bothThreadStepBindingsReachTheAction();
    void sortChoiceSurvivesRestart();
    void accountEntriesCarryTheirColour();
    void replyRowsKeepTheirTextUnderTheThreadLine();
    void clickingTheExpanderTogglesTheThread();
    void selectingAMessageRowTargetsThatMessageNotItsThread();
    void selectingAThreadRowNamesHowManyMessagesItStandsFor();
    void selectingAMessageRowReportsNoBulkCount();
    void anActionOnAThreadRowSaysItHitTheWholeThread();
    void anActionOnAMessageRowTagsThatMessageNotTheThread();
    void markAllReadIsDisabledUntilTheQueryFinishes();
    void markAllReadActsOnEveryRowAndUndoesInOneStep();
    void markAllReadDoesNothingWhenNothingIsUnread();
    void theSyncActionIsDisabledWhileABackgroundSyncHoldsTheLock();
    void escapeBlanksTheMessagePane();
    void deleteTogglesOnAnAlreadyDeletedThread();
    void deleteOnAMixedSelectionDeletesRatherThanSplittingIt();
    void deleteOnAReplyReadsItsOwnThreadNotTheFirstInTheList();
    void toggleUnreadOnAReplyReadsItsOwnThreadNotTheFirstInTheList();
    void importantOnAnAlreadyImportantThreadRemovesTheTag();
    void importantOnAPlainThreadStillAddsTheTag();
    void importantOnAReplyReadsItsOwnStateNotItsThreads();
    void editTagsOnAReplyCountsItsOwnThreadNotTheFirstInTheList();
    void markCurrentThreadReadResolvesTheThreadThroughTheIndex();
    void deletingAReplyRepaintsThatReplyRow();
    void toggleUnreadOnAReplyReadsTheReplysOwnState();
    void toggleUnreadOnAReplyRepaintsItInBothDirections();
    void taggingTheOpenReplyUpdatesTheMessagePaneStrip();
    void taggingAnUnrelatedReplyLeavesTheStripAlone();
    void aHeldMessageEditIsSentWhenTheSyncEnds();
    void anActionOnAThreadRowActsOnTheMessageItDisplays();
    void theThreadSubmenuIsReachableFromBothMenus();
    void autoMarkReadTouchesOnlyTheMessageOnDisplay();
    void autoMarkReadArmsForAReplyToo();
    void taggingTheOpenRootMessageKeepsTheStripPopulated();
    void aLoadedMessageCorrectsTheStripFromTheThreadsUnion();
    void aLoadedRootMessageGivesTheCardItsOwnTags();
    void aTransientStatusMessageExpires();
    void theSelectionCountIsStateAndDoesNotExpire();
    void anEditUndoneNettsBackToZero();
    void aDifferentTagOnTheSameMessageStillCounts();
    void anEditWithNoMessageIdsStillCounts();
    void anEditDuringABackgroundSyncIsNotSentYet();
    void aHeldEditIsSentWhenTheBackgroundSyncEnds();
    void aHeldEditCountsAsUnsynced();
    void anUnreadableLockTableStillSendsTheEdit();
    void aRejectedWriteKeepsEarlierUndoHistory();

    void aSuccessfulCronSyncClearsThePendingCount();
    void aFailedCronSyncLeavesThePendingCount();
    void anUnreadableSyncLogLeavesThePendingCount();
    void anUnknownExternalStateClearsNothing();
    void aSuccessfulCronSyncDrainsTheEditedAccounts();
    void aCronSyncDoesNotClearAnEditMadeWhileItRan();

    void everyActionCarriesAnIcon();
    void everyActionIsReachableFromAMenu();
    void noMenuHasTwoEntriesSharingAMnemonic();
    void theToolbarDoesNotOverrideTheDesktopButtonStyle();
    void theImportantActionIsLabelledImportant();
    void theImportantActionStillWritesTheFlaggedTag();
    void theToolbarUsesTheConfiguredIconSize();
    void thereIsNoSentButtonWithoutASentKey();
    void theSentButtonRunsEveryConfiguredAccount();
    void theSentButtonSurvivesABracketedPath();
    void placeholderCountsSkipSentAndDraftsWithoutTheKeys();
    void placeholderCountsCarrySentAndDrafts();
    void placeholderLabelsStayPairedWithTheirQueries();
    void placeholderCountsDropAnUncountableQuery();
    void flatModeDoesNotSurviveTheNextQuery();
    void noTwoActionsShareAnIcon();

    void onlyPinnedQueriesBecomeButtons();
    void unpinnedQueriesReachTheMenu();
    void pinnedButtonsFollowTheDocumentOrder();
    void theSavedQueryMenuIsHiddenWhenEveryQueryIsPinned();
    void aScopedSavedQuerySelectsItsAccount();
    void anUnscopedSavedQueryClearsTheAccount();
    void theSaveQueryActionIsDisabledOnAnEmptyQuery();
    void thereIsASaveButtonBesideTheQueryBar();
    void theMenuIsRightAlignedAwayFromTheButtons();
    void theRowSurvivesWithNothingButUnpinnedQueries();
    void aStoredGeneratedQueryRunsFlatAndComposed();
    void aRenamedSentEntryKeepsWorking();
    void aGeneratedQueryWithNothingToShowIsSkipped();
    void aSavedQueryButtonOffersEditUnpinAndDelete();
    void onlyAStoredQueryOffersToBecomeATaggingRule();
    void unpinningMovesAQueryToTheMenu();
    void deletingRemovesTheQueryFromTheFile();
    void anEditedQueryKeepsItsUnknownFields();
    void renamingReplacesRatherThanDuplicating();

    void deleteMovesTheMessageToTrash();
    void deleteRecordsWhereTheMessageCameFrom();
    void undoMovesTheMessageBack();
    void deleteOnAReplyMovesThatReplyOnly();
    void deleteWithoutATrashFolderSaysSoRatherThanDoingNothing();
    void undoingADeleteConsumesItsCommandRatherThanPushingAnother();
    void aDeleteHeldDuringASyncCountsAsUnsyncedWork();
    void twoDeletesToOneTrashBothGetTheirTags();
    void deletingTwiceLeavesNoOriginTagBehind();
    void undoOfADeleteRemovesTheOriginTagToo();
    void deletingAThreadRootTwiceRestoresItRatherThanRedeleting();
    void deleteThreadMovesEveryMessageAndRepaintsTheRootCard();
    void aFolderNameWithASpaceSurvivesTheRoundTrip();
    void deleteIsBoundToTheDeleteKey();
    void theDeleteKeyEditsTextInTheQueryBar();
    void restoreIsReachableWithoutTheKeyboard();
    void restoreIsOnlyEnabledInTheTrashView();
    void restoreReturnsAMessageToItsOriginFolder();
    void restoreFallsBackToInboxWithoutAnOriginTag();
    void theCleanupQueryFindsStrandedMail();
    void theCleanupQueryExcludesMailAlreadyInTrash();
    void aMoveThatRelocatesNothingWritesNoTag();
    void restoringFromTheTrashViewRefreshesTheList();
    void theRefreshAfterARestoreLeavesUndoIntact();
    void deletingOutsideTheTrashViewLeavesTheRowInPlace();

    // ComposeWindow, item 123. These need a window but no worker: the composer
    // never touches NotmuchWorker, it reads its context from the value struct
    // MainWindow hands it, so a Config written to a temporary INI is the whole
    // fixture.
    void aComposerOpensClean();
    void typingMarksTheComposerDirty();
    void anAutosaveWritesADraftAndClearsTheDirtyFlag();
    void anUnwritableDraftsFolderRaisesThePersistentBanner();
    void aSuccessfulSaveClearsTheBanner();
    void anAccountWithoutADraftsFolderReportsNoFailure();
    void aRewrittenDraftUnlinksThePreviousRevision();
    void theComposerBuildsTheMessageItsWidgetsShow();
    void theFromDropdownDecidesWhichAccountSends();
    void aFormatEditPreservesTheUndoStack();
    void aFormatEditRestoresTheSelectionItAsksFor();
    void aFormatEditOnAnEmptySelectionLandsBetweenTheTokens();
    void theAttachmentWarningRespectsTheConfiguredThreshold();
    void aDisabledAttachmentWarningWarnsAboutNothing();
    void theQuotePositionDecidesWhereTheQuoteLands();
    void theSeededQuoteIsNotAnUndoStep();
    void aReplySeedsTheHtmlToggleFromTheOriginal();
    void aNewMessageSeedsTheHtmlToggleFromConfig();
    void disablingInputsCoversEveryFieldAndTheToolbar();
    void aFailedSendCanBeRetriedWithoutFilingTheWrongCopy();
    void anUnchangedMessageIsNotWrittenAgain();
    void closingInsideTheDebounceStillSavesTheDraft();
    void closingAfterASendWritesNoFurtherDraft();
    void aCloseDuringTheCountdownIsRefused();
    void aFailedSendKeepsTheTextThatFailedToGo();
    void aSmallSizeLimitIsNotDescribedAsZeroMegabytes();

private:
    /// Owns the throwaway lock table init() points every test at. A pointer
    /// rather than a value because it is rebuilt per test, and QTemporaryDir
    /// removes its directory when destroyed.
    QTemporaryDir *m_lockDir = nullptr;
};

/// Item 61. Points every test at a lock table it owns, before every test.
///
/// Without this the suite reads the real `/proc/locks`, so a `mailsync.sh` run
/// on the developer's machine makes `SyncMonitor` report a sync in progress and
/// tests that never mention syncing fail. It is not a rare race: measured 0
/// failures in 30 runs with no lock held and 30 in 30 with one held, and it
/// cost three separate misdiagnoses before the cause was found. Reproduce with
/// `flock /tmp/mbsync.lock -c 'sleep 60'` in one shell and the suite in
/// another.
///
/// An EMPTY file rather than a fabricated table: `SyncMonitor` reads it and
/// finds no entry, which is exactly "no sync is running". A test that wants to
/// see a sync writes its own content, which three already do.
///
/// This also replaces the pattern those three used of restoring
/// `"/proc/locks"` when finished. That restoration was itself a defect: it
/// handed the real table back to whichever test ran next, so one test opting
/// in re-exposed all the others.
void TestMainWindow::init()
{
    m_lockDir = new QTemporaryDir;
    QVERIFY(m_lockDir->isValid());

    const QString locks = m_lockDir->filePath(QStringLiteral("locks"));
    QFile file(locks);
    QVERIFY(file.open(QIODevice::WriteOnly));
    file.close();

    MainWindow::setLocksPathForTesting(locks);
}

void TestMainWindow::cleanup()
{
    // Left pointing at the temporary path deliberately. Restoring
    // "/proc/locks" here would re-expose the next test between cleanup() and
    // its own init(), which is the trap this fixture exists to close.
    delete m_lockDir;
    m_lockDir = nullptr;
}

void TestMainWindow::noTestCanSeeTheRealLockTable()
{
    // The guard for the fixture itself. A test that asserts on sync state
    // proves nothing if the path silently reverts to /proc/locks, and this
    // fails the moment init() stops being applied or someone restores the real
    // table at the end of a test.
    QVERIFY2(MainWindow::locksPath() != QStringLiteral("/proc/locks"),
             "the suite is reading the real kernel lock table; a sync running "
             "on this machine will fail unrelated tests (item 61)");
    QVERIFY(MainWindow::locksPath().startsWith(QDir::tempPath()));
}

void TestMainWindow::everyKnownActionIsRegistered()
{
    // KeyMap::knownActions() is what loadOverrides() validates config bindings
    // against. An action listed there but never registered means a user can
    // bind a key in qtmaildir.conf, get no warning, and have it do nothing.
    //
    // registeredActionNames() is now derived from the QActions themselves, so
    // this compares against what the window really installed.
    const Config config;
    MainWindow window(config);

    const QStringList known = KeyMap::knownActions();
    const QStringList registered = window.registeredActionNames();

    for (const QString &action : known) {
        QVERIFY2(registered.contains(action),
                 qPrintable(QStringLiteral("known action '%1' is never registered "
                                           "by MainWindow").arg(action)));
    }
}

void TestMainWindow::everyRegisteredActionIsKnown()
{
    // The reverse drift: an action MainWindow implements but KeyMap rejects.
    // The user would get "unknown action" for a binding that is really there.
    const Config config;
    MainWindow window(config);

    const QStringList known = KeyMap::knownActions();
    const QStringList registered = window.registeredActionNames();

    for (const QString &action : registered) {
        QVERIFY2(known.contains(action),
                 qPrintable(QStringLiteral("registered action '%1' is not in "
                                           "KeyMap::knownActions()").arg(action)));
    }
}

void TestMainWindow::configuredBindingReachesTheAction()
{
    // The whole point of [keys]: a user's override must end up on the QAction,
    // which is what both the keyboard and the menus read.
    QTemporaryDir dir;
    const QString path = dir.filePath(QStringLiteral("qtmaildir.conf"));
    {
        QSettings s(path, QSettings::IniFormat);
        s.beginGroup(QStringLiteral("keys"));
        s.setValue(QStringLiteral("Ctrl+Alt+A"), QStringLiteral("archive"));
        s.endGroup();
    }

    // MainWindow reads its keymap from Config::defaultPath(), so point that
    // at the temporary file for this test.
    const QString previous = qEnvironmentVariable("XDG_CONFIG_HOME");
    QVERIFY(QDir().mkpath(dir.filePath(QStringLiteral("qtmaildir"))));
    QVERIFY(QFile::copy(path, dir.filePath(QStringLiteral("qtmaildir/qtmaildir.conf"))));
    qputenv("XDG_CONFIG_HOME", dir.path().toUtf8());

    {
        const Config config;
        MainWindow window(config);
        const QAction *archive =
            window.findChild<QAction *>(QStringLiteral("archive"));
        QVERIFY(archive);
        QCOMPARE(archive->shortcut(), QKeySequence(QStringLiteral("Ctrl+Alt+A")));
    }

    if (previous.isEmpty())
        qunsetenv("XDG_CONFIG_HOME");
    else
        qputenv("XDG_CONFIG_HOME", previous.toUtf8());
}

void TestMainWindow::cidPrefixesAreBangFree()
{
    // MainWindow is the only producer of cidPrefix in the application. The
    // '!' separator that keeps two messages' cid: references apart is only
    // unambiguous while the prefix half contains none.
    for (int i : { 0, 1, 9, 10, 99, 1000 }) {
        const QString prefix = MainWindow::cidPrefixForIndex(i);
        QVERIFY(!prefix.isEmpty());
        QVERIFY2(!prefix.contains(QLatin1Char('!')),
                 qPrintable(QStringLiteral("prefix '%1' contains '!'").arg(prefix)));
    }
}

void TestMainWindow::cidPrefixesAreDistinctPerMessage()
{
    // Two messages sharing a prefix would share a cid: namespace, which is the
    // collision the namespacing exists to prevent.
    QSet<QString> seen;
    for (int i = 0; i < 200; ++i) {
        const QString prefix = MainWindow::cidPrefixForIndex(i);
        QVERIFY2(!seen.contains(prefix),
                 qPrintable(QStringLiteral("prefix '%1' repeats").arg(prefix)));
        seen.insert(prefix);
    }
}

void TestMainWindow::uiStateIsNotWrittenIntoTheUserConfig()
{
    // The config file is hand-edited and must never gain a base64 geometry
    // blob, nor be rewritten on exit: QSettings preserves neither comments nor
    // key order, so writing it would quietly destroy the user's formatting.
    QVERIFY(MainWindow::uiStatePath() != Config::defaultPath());

    // One qtmaildir component, not two. QStandardPaths::StateLocation appends
    // both the organization and the application name, and here both are
    // "qtmaildir", so using it nests the directory inside itself.
    QCOMPARE(MainWindow::uiStatePath().count(QStringLiteral("/qtmaildir/")), 1);
    QVERIFY(MainWindow::uiStatePath().endsWith(
        QStringLiteral("/qtmaildir/uistate.conf")));
}

void TestMainWindow::uiStateSurvivesARestart()
{
    // Test mode redirects QStandardPaths at the process level, so the state
    // file lands in a scratch directory rather than the real ~/.local/state.
    QStandardPaths::setTestModeEnabled(true);
    QFile::remove(MainWindow::uiStatePath());

    // Must fit the smallest screen this ever runs against: the offscreen
    // platform reports 800x800, and restoreGeometry() clamps to the available
    // area, so a 940px width came back as 798 and failed only under offscreen.
    // The number carries no meaning beyond differing from the default size.
    const QSize resized(640, 560);
    {
        const Config config;
        MainWindow window(config);
        window.resize(resized);
        window.findChild<MessageView *>()->setZoomFactor(1.4);
        window.close();  // closeEvent() is what persists the state
    }

    QVERIFY2(QFile::exists(MainWindow::uiStatePath()),
             qPrintable(QStringLiteral("no state file at %1")
                            .arg(MainWindow::uiStatePath())));

    const Config config;
    MainWindow reopened(config);
    QCOMPARE(reopened.size(), resized);
    QCOMPARE(reopened.findChild<MessageView *>()->zoomFactor(), 1.4);

    QFile::remove(MainWindow::uiStatePath());
    QStandardPaths::setTestModeEnabled(false);
}

void TestMainWindow::missingUiStateLeavesTheDefaults()
{
    // A restore that silently succeeded on an empty blob would give a
    // zero-size window on first launch. Absent state must be a no-op.
    QStandardPaths::setTestModeEnabled(true);
    QFile::remove(MainWindow::uiStatePath());

    const Config config;
    MainWindow window(config);
    QCOMPARE(window.size(), QSize(1200, 800));

    QStandardPaths::setTestModeEnabled(false);
}

void TestMainWindow::splitterStateFromAWiderWindowKeepsTheMessagePane()
{
    // The reported symptom, from a real state file: a splitter position saved
    // at 1285/1252 and restored into a 1136px window. QSplitter honours the
    // first size and gives the second what is left, so the message pane came
    // back roughly 30px wide, a sliver of rendered mail against a full-width
    // thread list. It is not a first-run problem, which is why widening the
    // default window would not have helped: the wider the window ever was, the
    // worse the next narrower session is.
    QStandardPaths::setTestModeEnabled(true);
    QFile::remove(MainWindow::uiStatePath());

    {
        const Config config;
        MainWindow window(config);
        window.show();
        QVERIFY(QTest::qWaitForWindowExposed(&window));

        auto *splitter = window.findChild<QSplitter *>();
        QVERIFY(splitter);
        // Guard: the forged sizes have to be genuinely wider than the window
        // this reopens into, or the test proves nothing.
        QVERIFY(splitter->width() > 0);
        // Times four, not times two: the offscreen platform chooses the window
        // width itself and has been seen to choose differently between runs of
        // this binary, so a margin that only just exceeds THIS window's width
        // can fail to exceed the reopened one's and quietly stop reproducing
        // the bug.
        const int overwide = splitter->width() * 4;
        splitter->setSizes({ overwide, overwide });
        window.close();
    }

    const Config config;
    MainWindow reopened(config);
    reopened.show();
    QVERIFY(QTest::qWaitForWindowExposed(&reopened));

    auto *splitter = reopened.findChild<QSplitter *>();
    QVERIFY(splitter);
    const QList<int> sizes = splitter->sizes();
    QCOMPARE(sizes.size(), 2);

    // Asserted against the pane's OWN minimum rather than a number repeated
    // here: the floor is private to MainWindow, and a literal copy would keep
    // passing if the shipped one were lowered back to a width mail cannot be
    // read at. The guard below is what stops that from being vacuous.
    auto *pane = reopened.findChild<MessageView *>();
    QVERIFY(pane);
    QVERIFY2(pane->minimumWidth() >= 300,
             qPrintable(QStringLiteral("message pane floor is %1px")
                            .arg(pane->minimumWidth())));
    QVERIFY2(sizes.at(1) >= pane->minimumWidth(),
             qPrintable(QStringLiteral("message pane restored %1px wide in a "
                                       "%2px splitter")
                            .arg(sizes.at(1))
                            .arg(splitter->width())));

    reopened.close();
    QFile::remove(MainWindow::uiStatePath());
    QStandardPaths::setTestModeEnabled(false);
}

void TestMainWindow::usableSplitterStateIsRestoredUntouched()
{
    // The rescue must not fire on a position that fits. This is item 1's whole
    // point: a splitter the user dragged comes back where they left it.
    QStandardPaths::setTestModeEnabled(true);
    QFile::remove(MainWindow::uiStatePath());

    QList<int> saved;
    {
        const Config config;
        MainWindow window(config);
        window.show();
        QVERIFY(QTest::qWaitForWindowExposed(&window));

        auto *splitter = window.findChild<QSplitter *>();
        QVERIFY(splitter);
        // Lopsided but both panes usable, so nothing should touch it. Sized as
        // a fraction rather than "width minus 250": this window's width is the
        // platform's to choose and has been seen to differ between two runs of
        // the same binary, so an absolute split saved here can arrive too wide
        // for the reopened window and trip the rescue the test is asserting
        // does NOT fire.
        const int total = splitter->width();
        QVERIFY(total > 500);
        splitter->setSizes({ total / 4, total - total / 4 });
        saved = splitter->sizes();
        QVERIFY(saved.at(0) < saved.at(1));
        window.close();
    }

    const Config config;
    MainWindow reopened(config);
    reopened.show();
    QVERIFY(QTest::qWaitForWindowExposed(&reopened));

    auto *splitter = reopened.findChild<QSplitter *>();
    QVERIFY(splitter);
    // The saved ratio, not the saved pixels: QSplitter redistributes to the
    // current width, and only a rescue would flip which pane is the larger.
    // The FIRST pane's saved pixel width is what must survive: QSplitter
    // restores it verbatim and gives the second whatever the current window
    // leaves, so this is the value a rescue would overwrite and the only one
    // that does not move with the window width. Which matters here, because
    // the offscreen platform picks that width itself and has been seen to
    // pick differently between two runs of this same binary (1181 and 779),
    // so any assertion on a ratio or on pane 1 is checking the platform's
    // mood rather than this code.
    const QList<int> sizes = splitter->sizes();
    QCOMPARE(sizes.at(0), saved.at(0));

    reopened.close();
    QFile::remove(MainWindow::uiStatePath());
    QStandardPaths::setTestModeEnabled(false);
}

void TestMainWindow::headerStateFromADifferentColumnLayoutIsDiscarded()
{
    // The upgrade hazard: a 0.3.0 state file holds a three-column header blob,
    // and 0.4.0 added the attachment column in front. QHeaderView::
    // restoreState() returns TRUE for a blob with fewer sections than the
    // model and applies the old widths shifted one column right, mangling the
    // layout with no error to detect it by (verified on Qt 6.11). The stored
    // column count is what makes that detectable.
    QStandardPaths::setTestModeEnabled(true);
    QFile::remove(MainWindow::uiStatePath());

    {
        const Config config;
        MainWindow window(config);
        window.close();
    }

    // Forge a state file from the five-column layout. Nothing reads these keys
    // any more, and that is exactly what must be verified: a blob saved by an
    // older version has to be ignored rather than applied to a one-column view.
    {
        QSettings state(MainWindow::uiStatePath(), QSettings::IniFormat);
        state.setValue(QStringLiteral("threadlist/columns"), 5);
        state.setValue(QStringLiteral("threadlist/header"),
                       QByteArray("not a header this model could have saved"));
    }

    // Constructing must not apply it, and must not crash on the garbage blob.
    const Config config;
    MainWindow reopened(config);

    auto *view = reopened.findChild<QTreeView *>();
    QVERIFY(view);
    QCOMPARE(view->model()->columnCount(), 1);

    QFile::remove(MainWindow::uiStatePath());
    QStandardPaths::setTestModeEnabled(false);
}

void TestMainWindow::returnInTheQueryBarRunsTheQueryNotOpenThread()
{
    // Return is bound to open_thread as a WindowShortcut, and the query bar has
    // to win it back while it has focus. Qt withholds a plain-LETTER shortcut
    // from an editable widget, but Return is not a letter and gets no such
    // protection, so without an explicit override the action fires, the query
    // never runs, and focus jumps to the thread list.
    //
    // The delivery order matters and is the reason this bug survived earlier
    // tests: real input sends ShortcutOverride first and only dispatches the
    // shortcut if nothing accepts it. QTest::keyClick() skips that round trip,
    // so a test written with it passes against the broken code.
    const Config config;
    MainWindow window(config);
    window.show();
    QVERIFY(QTest::qWaitForWindowExposed(&window));

    auto *edit = window.findChild<QLineEdit *>();
    QVERIFY(edit);
    edit->setFocus();
    QTRY_COMPARE(QApplication::focusWidget(), edit);
    edit->setText(QStringLiteral("tag:unread"));

    QAction *openThread = window.findChild<QAction *>(QStringLiteral("open_thread"));
    QVERIFY(openThread);
    bool actionFired = false;
    connect(openThread, &QAction::triggered, &window, [&actionFired]() {
        actionFired = true;
    });

    // The query bar must claim the override, which is what stops the shortcut
    // from ever being dispatched.
    QKeyEvent override(QEvent::ShortcutOverride, Qt::Key_Return, Qt::NoModifier);
    override.ignore();
    QApplication::sendEvent(edit, &override);
    QVERIFY2(override.isAccepted(),
             "the query bar let Return through to the open_thread shortcut");

    QVERIFY(!actionFired);
}

/// Selects a top-level THREAD row, replacing QTableView::selectRow which a
/// QTreeView does not have.
///
/// Not merely a rename: setCurrentIndex alone leaves the selection model empty,
/// and select() alone leaves current invalid, so every test asserting on either
/// would break in a different way. Both are set here, exactly as
/// QTableView::selectRow did.
static void selectThreadRow(QTreeView *view, int row)
{
    const QModelIndex index = view->model()->index(row, 0, QModelIndex());
    view->selectionModel()->select(
        index, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
    view->setCurrentIndex(index);
}

/// The height of a top-level row, replacing QTableView::rowHeight(int).
static int threadRowHeight(QTreeView *view, int row)
{
    return view->visualRect(view->model()->index(row, 0, QModelIndex())).height();
}

/// A thread summary carrying the tags a test needs. Enough to drive selection;
/// nothing here touches a database.
static ThreadSummary makeThread(const QString &id, const QStringList &tags)
{
    ThreadSummary thread;
    thread.threadId = id;
    thread.subject = QStringLiteral("Subject ") + id;
    thread.authors = QStringLiteral("Someone <someone@example.org>");
    thread.tags = tags;

    // The message the row displays, which the real worker fills in from the
    // query. Required since item 108: an ordinary tag action resolves a thread
    // row to THIS id, so a summary without one names no message and every
    // action on it does nothing. A fixture missing it fails with "the action
    // did not happen", which reads as a defect in the action.
    thread.firstMessageId = id + QStringLiteral("-first@example.org");
    return thread;
}

void TestMainWindow::childRowsAreIndentedUnderTheirThread()
{
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY2(view, "the thread list is not a QTreeView, so it cannot indent");

    // With an account tag, so the thread row draws the chip that a reply row
    // does not. That asymmetry is the whole reason the indent has to be wide,
    // and a test against an untagged thread never sees it.
    model->appendBatch({ makeThread(
        QStringLiteral("t1"),
        QStringList{ TagColors::tagForAccountKey(QStringLiteral("work")) }) });

    MessageNode first;
    first.messageId = QStringLiteral("m0@example.org");
    first.threadId = QStringLiteral("t1");
    first.depth = 0;
    MessageNode reply;
    reply.messageId = QStringLiteral("m1@example.org");
    reply.threadId = QStringLiteral("t1");
    reply.from = QStringLiteral("A Replier <replier@example.org>");
    reply.depth = 1;
    model->setThreadMessages(QStringLiteral("t1"), { first, reply });

    window.resize(1400, 300);
    window.show();
    QVERIFY(QTest::qWaitForWindowExposed(&window));

    const QModelIndex root = model->index(0, 0, QModelIndex());
    view->expand(root);
    QApplication::processEvents();

    // One column, and setIndentation(0): Qt indents nothing, CardLayout draws
    // the indent itself. So visualRect reports the SAME rect for a thread and
    // its reply, and the indent has to be read off the layout rather than off
    // the geometry. That is the trap CLAUDE.md records in reverse: there,
    // visualRect reported an indent the text did not have; here it reports
    // none while the text is indented.
    const QModelIndex rootCell = model->index(0, 0, QModelIndex());
    const QModelIndex child = model->index(0, 0, root);
    QVERIFY(child.isValid());

    // Guards before the claim: a probe that cannot see both rows can report
    // anything it likes about their relative position.
    QVERIFY2(view->visualRect(rootCell).height() > 0,
             "the thread row has no height, so nothing about it is measurable");
    QVERIFY2(view->visualRect(child).height() > 0,
             "the reply row has no height: it is collapsed or off-screen, and "
             "an indent test against it would pass without drawing anything");

    // The indent is NOT in the geometry. setIndentation(0) means visualRect
    // reports the same left edge for both rows, deliberately: CardLayout draws
    // the indent inside the card's own rect. Asserting on visualRect here
    // would fail against a perfectly indented list, which is the mirror of the
    // trap CLAUDE.md records for item 20, where visualRect reported an indent
    // the text did not have.
    //
    // So the real property, as before: where the TEXT lands. It is read off
    // the layout, which is what the delegate paints from.
    CardLayout::Input threadIn;
    threadIn.isMessage = false;
    threadIn.depth = 0;
    CardLayout::Input replyIn;
    replyIn.isMessage = true;
    replyIn.depth =
        model->data(child, ThreadListModel::MessageDepthRole).toInt();
    QVERIFY2(replyIn.depth > 0,
             "the reply reports depth 0, so there is no nesting to measure");

    const QRect rect = view->visualRect(rootCell);
    const CardLayout threadCard =
        CardLayout::compute(threadIn, rect, view->font());
    const CardLayout replyCard =
        CardLayout::compute(replyIn, rect, view->font());

    QVERIFY2(replyCard.contentLeft > threadCard.contentLeft,
             qPrintable(QStringLiteral("the reply's text starts at x=%1, not "
                                       "right of the thread's at x=%2: the "
                                       "nesting is invisible")
                            .arg(replyCard.contentLeft)
                            .arg(threadCard.contentLeft)));

    // And the spine that makes the nesting read as one block rather than as an
    // arbitrary offset.
    QCOMPARE(replyCard.spines.size(), replyIn.depth);
}

namespace {

/// Two threads, the first with one reply, expanded. The shared fixture for the
/// two navigation tests below.
struct NavFixture
{
    QTreeView *view = nullptr;
    ThreadListModel *model = nullptr;
    QModelIndex root;
    QModelIndex reply;
};

NavFixture buildNavFixture(MainWindow &window)
{
    NavFixture f;
    f.view = window.findChild<QTreeView *>();
    f.model = window.findChild<ThreadListModel *>();

    // unread, so a selection arms the mark-read timer: scheduleMarkRead()
    // returns early for a thread that is already read, and a fixture without
    // it would make a mark-read assertion pass for the wrong reason.
    ThreadSummary first = makeThread(QStringLiteral("T1"),
                                     QStringList{ QStringLiteral("inbox"),
                                                  QStringLiteral("unread") });
    first.totalCount = 2;
    ThreadSummary second = makeThread(QStringLiteral("T2"),
                                      QStringList{ QStringLiteral("inbox") });
    second.totalCount = 1;
    f.model->appendBatch({ first, second });

    MessageNode rootNode;
    rootNode.messageId = QStringLiteral("M1");
    rootNode.threadId = QStringLiteral("T1");
    rootNode.depth = 0;
    MessageNode replyNode;
    replyNode.messageId = QStringLiteral("M2");
    replyNode.threadId = QStringLiteral("T1");
    replyNode.depth = 1;
    f.model->setThreadMessages(QStringLiteral("T1"), { rootNode, replyNode });

    f.root = f.model->index(0, 0);
    f.view->expand(f.root);
    f.reply = f.model->index(0, 0, f.root);
    return f;
}

}  // namespace

void TestMainWindow::selectingARootCardKeepsItsThreadForMarkRead()
{
    // A root card is BOTH a message and a thread: it renders the thread's
    // first message, and it is still the thread that gets marked read and
    // repainted on a tag change. The message-row path deliberately clears the
    // current thread id; doing that here too would silently disable mark-read
    // and the tag-change repaint for every thread root in the list.
    const Config config;
    MainWindow window(config);
    window.show();
    QVERIFY(QTest::qWaitForWindowExposed(&window));

    const NavFixture f = buildNavFixture(window);
    f.view->setCurrentIndex(f.root);
    QApplication::processEvents();

    // Guard: the fixture loads replies, so the root knows its own message and
    // the branch under test is the one that runs.
    QVERIFY2(!f.model->data(f.root, ThreadListModel::MessageIdRole)
                  .toString().isEmpty(),
             "the root card does not know its first message, so this exercises "
             "the fallback rather than the path it is written for");

    // A mark-read timer armed for the thread is what proves the thread id
    // survived: scheduleMarkRead() is only reached on the thread-row path.
    auto *timer = window.findChild<QTimer *>(QStringLiteral("markReadTimer"));
    QVERIFY(timer);
    QVERIFY2(timer->isActive(),
             "no mark-read timer for a selected root card: its thread id was "
             "cleared along with the switch to rendering one message");
}

void TestMainWindow::nextThreadLeavesTheLastReply()
{
    const Config config;
    MainWindow window(config);
    window.show();
    QVERIFY(QTest::qWaitForWindowExposed(&window));

    const NavFixture f = buildNavFixture(window);
    QVERIFY(f.reply.isValid());
    QVERIFY2(f.view->isExpanded(f.root),
             "the thread is collapsed, so this test would arrow down a flat "
             "list and pass against the bug it exists to catch");

    f.view->setCurrentIndex(f.reply);

    // The defect (item 60): selectRow(current.row() + 1) asked for row 1 UNDER
    // T1, which does not exist, so the action did nothing at all.
    window.findChild<QAction *>(QStringLiteral("next_thread"))->trigger();

    QCOMPARE(f.view->currentIndex().data(ThreadListModel::ThreadIdRole)
                 .toString(),
             QStringLiteral("T2"));
}

void TestMainWindow::altDownSkipsReplies()
{
    const Config config;
    MainWindow window(config);
    window.show();
    QVERIFY(QTest::qWaitForWindowExposed(&window));

    const NavFixture f = buildNavFixture(window);
    QVERIFY(f.view->isExpanded(f.root));

    // From the thread ROOT with its replies showing: one step must land on the
    // next THREAD, not on the first reply. That is what makes the action mean
    // thread-to-thread while plain Up/Down still steps message-to-message.
    f.view->setCurrentIndex(f.root);
    window.findChild<QAction *>(QStringLiteral("next_thread"))->trigger();

    QCOMPARE(f.view->currentIndex().data(ThreadListModel::ThreadIdRole)
                 .toString(),
             QStringLiteral("T2"));
    QVERIFY(!f.view->currentIndex().data(ThreadListModel::IsMessageRole)
                 .toBool());

    // And back, which is the mirror case the old arithmetic also failed.
    window.findChild<QAction *>(QStringLiteral("prev_thread"))->trigger();
    QCOMPARE(f.view->currentIndex().data(ThreadListModel::ThreadIdRole)
                 .toString(),
             QStringLiteral("T1"));
    QVERIFY(!f.view->currentIndex().data(ThreadListModel::IsMessageRole)
                 .toBool());
}

void TestMainWindow::bothThreadStepBindingsReachTheAction()
{
    const Config config;
    MainWindow window(config);

    // Two bindings per action, which needs setShortcuts rather than
    // setShortcut: Ctrl+J/K for a neomutt hand, Alt+Up/Down for a mouse one.
    // Alt because Shift+arrows is QTreeView's built-in extend-selection that
    // multi-row tagging depends on, and a bare arrow cannot be a window
    // shortcut without breaking every text field in the window.
    for (const auto &pair : { std::pair<const char *, const char *>{
                                  "next_thread", "Alt+Down" },
                              { "prev_thread", "Alt+Up" } }) {
        auto *action =
            window.findChild<QAction *>(QString::fromLatin1(pair.first));
        QVERIFY2(action, pair.first);
        const QList<QKeySequence> shortcuts = action->shortcuts();
        QVERIFY2(shortcuts.size() >= 2,
                 qPrintable(QStringLiteral("%1 carries %2 shortcut(s), so the "
                                           "second binding is unreachable")
                                .arg(QString::fromLatin1(pair.first))
                                .arg(shortcuts.size())));
        QVERIFY2(shortcuts.contains(
                     QKeySequence(QString::fromLatin1(pair.second))),
                 pair.second);
    }
}

void TestMainWindow::sortChoiceSurvivesRestart()
{
    QStandardPaths::setTestModeEnabled(true);
    QFile::remove(MainWindow::uiStatePath());

    {
        const Config config;
        MainWindow window(config);
        auto *sort = window.findChild<QComboBox *>(QStringLiteral("sortOrder"));
        QVERIFY(sort);
        QCOMPARE(sort->count(), 2);
        QCOMPARE(sort->currentIndex(), 0);  // Newest first by default.
        sort->setCurrentIndex(1);
        window.close();
    }

    const Config config;
    MainWindow second(config);
    auto *sort = second.findChild<QComboBox *>(QStringLiteral("sortOrder"));
    QVERIFY(sort);
    QCOMPARE(sort->currentIndex(), 1);

    // A stale or hand-edited file can hold anything, which is the lesson item
    // 58 recorded: an out-of-range value must fall back rather than select a
    // row that does not exist.
    {
        QSettings state(MainWindow::uiStatePath(), QSettings::IniFormat);
        state.setValue(QStringLiteral("threadlist/sortOrder"), 47);
    }
    MainWindow third(config);
    auto *thirdSort = third.findChild<QComboBox *>(QStringLiteral("sortOrder"));
    QCOMPARE(thirdSort->currentIndex(), 0);

    QFile::remove(MainWindow::uiStatePath());
    QStandardPaths::setTestModeEnabled(false);
}

void TestMainWindow::accountEntriesCarryTheirColour()
{
    // Its own config, not the environment's. Reading the real one made this
    // SKIP wherever no accounts are configured, which is a test that asserts
    // nothing while reporting success.
    QTemporaryDir dir;
    const QString path = dir.filePath(QStringLiteral("qtmaildir.conf"));
    {
        QSettings s(path, QSettings::IniFormat);
        s.beginGroup(QStringLiteral("account.work"));
        s.setValue(QStringLiteral("maildir"), QStringLiteral("work"));
        s.setValue(QStringLiteral("color"), QStringLiteral("#3d7fd1"));
        s.endGroup();
        s.beginGroup(QStringLiteral("account.personal"));
        s.setValue(QStringLiteral("maildir"), QStringLiteral("personal"));
        s.endGroup();
    }

    Config config;
    config.load(path);
    QCOMPARE(config.accounts().size(), 2);

    MainWindow window(config);
    auto *box = window.findChild<QComboBox *>(QStringLiteral("accountBox"));
    QVERIFY(box);
    QCOMPARE(box->count(), 3);

    // "All accounts" is not an account and carries no swatch.
    QVERIFY(!box->itemData(0, Qt::DecorationRole).isValid());

    // Every real account does, including the one with no color= key:
    // colourFor() never fails, deriving a stable colour from the tag name, so
    // adding an account and forgetting to colour it degrades to something
    // usable rather than to nothing.
    QSet<QRgb> seen;
    for (int i = 1; i < box->count(); ++i) {
        const QVariant swatch = box->itemData(i, Qt::DecorationRole);
        QVERIFY2(swatch.isValid(),
                 qPrintable(QStringLiteral("account %1 carries no swatch")
                                .arg(box->itemText(i))));
        const QColor colour = swatch.value<QColor>();
        QVERIFY(colour.isValid());
        seen.insert(colour.rgb());
    }

    // Guard: two accounts sharing one colour would make the swatches useless
    // as a key to the accent bars, and would let a broken lookup pass.
    QCOMPARE(seen.size(), 2);
}

void TestMainWindow::cardsNeverScrollSideways()
{
    const Config config;
    MainWindow window(config);
    window.show();
    QVERIFY(QTest::qWaitForWindowExposed(&window));

    auto *view = window.findChild<ThreadListView *>();
    QVERIFY(view);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    // A long subject, so the guard below is not vacuous: this is exactly the
    // content that used to make the subject column wider than the viewport.
    model->appendBatch({ makeThread(
        QStringLiteral("t1"),
        QStringList{ QStringLiteral("inbox") }) });
    QApplication::processEvents();

    // Item 51: clicking a row used to scroll the list sideways, because the
    // subject column was wider than the viewport and auto-scroll brought the
    // clicked index fully into view. A card is exactly viewport width, so
    // there is nowhere to scroll to.
    QVERIFY2(view->visualRect(model->index(0, 0)).height() > 0,
             "no card is drawn, so there is no layout to assert about");
    QCOMPARE(view->horizontalScrollBar()->minimum(),
             view->horizontalScrollBar()->maximum());
}

void TestMainWindow::aThreadWithRepliesDrawsAVisibleExpander()
{
    // The expander is the ONLY thing saying a thread can be opened, and it took
    // four wrong attempts to get on screen before item 53, each of which looked
    // correct in code and none of which a geometry or role assertion could see.
    // So this counts painted pixels.
    //
    // Painted through the DELEGATE rather than through viewport()->render().
    // The viewport render returns a blank image here: CLAUDE.md records that it
    // does so in several ordinary situations, and this test proved it again,
    // reporting zero ink over a card the delegate demonstrably paints 2183
    // pixels into. A probe that sees nothing cannot report on anything.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);

    // Two threads: one with replies, one without. The second is the control,
    // and without it a test that counts ink would pass on any card.
    ThreadSummary withReplies = makeThread(
        QStringLiteral("t1"),
        QStringList{ TagColors::tagForAccountKey(QStringLiteral("work")) });
    withReplies.totalCount = 3;
    ThreadSummary lone = makeThread(
        QStringLiteral("t2"),
        QStringList{ TagColors::tagForAccountKey(QStringLiteral("work")) });
    lone.totalCount = 1;
    model->appendBatch({ withReplies, lone });

    const QModelIndex first = model->index(0, 0, QModelIndex());
    const QModelIndex second = model->index(1, 0, QModelIndex());

    // Guards: the model agrees about which thread has replies, and only that
    // one is offered an expander at all.
    QCOMPARE(model->data(first, ThreadListModel::ReplyCountRole).toInt(), 2);
    QCOMPARE(model->data(second, ThreadListModel::ReplyCountRole).toInt(), 0);

    const QFont font = view->font();
    const int height = CardLayout::heightFor(font);

    const auto inkInExpander = [&](const QModelIndex &index) {
        QImage shot(400, height, QImage::Format_ARGB32);
        shot.fill(Qt::white);
        QPainter painter(&shot);
        QStyleOptionViewItem option;
        option.rect = QRect(0, 0, 400, height);
        option.font = font;
        option.palette = QApplication::palette();
        option.state = QStyle::State_Enabled;
        CardDelegate delegate;
        delegate.paint(&painter, option, index);
        painter.end();

        const QRect rect = CardDelegate::expanderRectFor(option, index);
        int found = 0;
        for (int y = rect.top(); y <= rect.bottom() && y < shot.height(); ++y) {
            for (int x = rect.left(); x <= rect.right() && x < shot.width();
                 ++x) {
                if ((shot.pixel(x, y) | 0xff000000) != 0xffffffffu)
                    ++found;
            }
        }

        // Guard on the probe itself: prove it can see the card's own text
        // before trusting it about the expander. A probe that finds no ink
        // anywhere reports "nothing was drawn" whatever the delegate did.
        int anyInk = 0;
        for (int y = 0; y < shot.height(); ++y)
            for (int x = 0; x < shot.width(); ++x)
                if ((shot.pixel(x, y) | 0xff000000) != 0xffffffffu)
                    ++anyInk;
        return std::pair<int, int>(found, anyInk);
    };

    const auto [drawn, drawnAnywhere] = inkInExpander(first);
    const auto [control, controlAnywhere] = inkInExpander(second);

    QVERIFY2(drawnAnywhere > 0 && controlAnywhere > 0,
             "the probe finds no ink on either card, so it cannot report on "
             "the expander either");

    QVERIFY2(drawn > 12,
             qPrintable(QStringLiteral("only %1 pixels in the expander's rect: "
                                       "the reply count is clipped or painted "
                                       "over").arg(drawn)));
    QVERIFY2(control == 0,
             qPrintable(QStringLiteral("a thread with no replies drew %1 "
                                       "pixels where an expander would go")
                            .arg(control)));
}

void TestMainWindow::selectingAThreadRowNamesHowManyMessagesItStandsFor()
{
    // With two kinds of row selectable, one selected row no longer says how
    // much an action will touch. CLAUDE.md forbids a confirmation dialog for
    // tag mutations, so the scope is made visible instead: this is the "before"
    // half of that, and the count has to come from the thread's own total, not
    // from whatever happens to be expanded.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *status = window.findChild<QLabel *>(QStringLiteral("statusMessage"));
    QVERIFY(status);

    ThreadSummary t = makeThread(QStringLiteral("t1"), {});
    t.totalCount = 7;
    model->appendBatch({ t });

    window.show();
    QVERIFY(QTest::qWaitForWindowExposed(&window));

    // Guard: nothing is expanded, so a count taken from the loaded children
    // would read 0 and this test would be measuring the wrong source.
    QCOMPARE(model->rowCount(model->index(0, 0, QModelIndex())), 0);

    selectThreadRow(view, 0);
    QApplication::processEvents();

    QVERIFY2(status->text().contains(QStringLiteral("7")),
             qPrintable(QStringLiteral("the status bar says '%1', which does "
                                       "not name the 7 messages the thread "
                                       "stands for")
                            .arg(status->text())));
}

void TestMainWindow::selectingAMessageRowReportsNoBulkCount()
{
    // Reading one message is not a bulk action, so it gets no count. A message
    // row reporting "1 thread selected" would be actively wrong about what an
    // action would touch.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *status = window.findChild<QLabel *>(QStringLiteral("statusMessage"));
    QVERIFY(status);

    ThreadSummary t = makeThread(QStringLiteral("t1"), {});
    t.totalCount = 3;
    model->appendBatch({ t });

    MessageNode root;
    root.messageId = QStringLiteral("m0@example.org");
    root.threadId = QStringLiteral("t1");
    root.depth = 0;
    MessageNode reply;
    reply.messageId = QStringLiteral("m1@example.org");
    reply.threadId = QStringLiteral("t1");
    reply.depth = 1;
    model->setThreadMessages(QStringLiteral("t1"), { root, reply });

    window.show();
    QVERIFY(QTest::qWaitForWindowExposed(&window));

    const QModelIndex threadRow = model->index(0, 0, QModelIndex());
    view->expand(threadRow);
    QApplication::processEvents();

    const QModelIndex messageRow = model->index(0, 0, threadRow);
    QVERIFY(model->isMessageRow(messageRow));

    view->selectionModel()->select(
        messageRow,
        QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
    view->setCurrentIndex(messageRow);
    QApplication::processEvents();

    QVERIFY2(!status->text().contains(QStringLiteral("thread")),
             qPrintable(QStringLiteral("a single message row reports '%1', "
                                       "which claims a thread-wide scope it "
                                       "does not have")
                            .arg(status->text())));
}

void TestMainWindow::anActionOnAThreadRowSaysItHitTheWholeThread()
{
    // The "after" half. Undo is the safety net this project chose over a
    // confirmation dialog, and undo is only usable if the user can tell that
    // something bigger than they intended just happened.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *status = window.findChild<QLabel *>(QStringLiteral("statusMessage"));
    QVERIFY(status);

    ThreadSummary t = makeThread(QStringLiteral("t1"), {});
    t.totalCount = 7;
    model->appendBatch({ t });

    window.show();
    QVERIFY(QTest::qWaitForWindowExposed(&window));

    selectThreadRow(view, 0);
    QApplication::processEvents();

    // The THREAD action since item 108. The plain `archive` now acts on the
    // one message a card displays, and would rightly not claim to have taken
    // the whole thread; this suffix belongs to the action that really does.
    auto *archive = window.findChild<QAction *>(QStringLiteral("archive_thread"));
    QVERIFY2(archive, "no archive_thread action to trigger");
    archive->trigger();

    // Read BEFORE processEvents, deliberately. This binary has no worker
    // (backlog item 36), so the queued applyTagsToThreads reaches a throwaway
    // database that has never heard of thread t1 and answers with
    // errorOccurred, which overwrites the status bar. Draining the event loop
    // here would assert on that error rather than on the scope message, and
    // the test would fail against correct code.
    const QString message = status->text();

    QVERIFY2(message.contains(QStringLiteral("7")),
             qPrintable(QStringLiteral("after archiving a 7-message thread the "
                                       "status bar says '%1', which does not "
                                       "say how much was touched")
                            .arg(message)));

    // And it must say the whole thread went, not merely how many messages: the
    // count alone does not distinguish "7 messages you picked" from "7 messages
    // because you picked their thread".
    QVERIFY2(message.contains(QStringLiteral("whole thread")),
             qPrintable(QStringLiteral("the status bar says '%1', which does "
                                       "not say the action took the whole "
                                       "thread")
                            .arg(message)));
}

void TestMainWindow::anActionOnAMessageRowTagsThatMessageNotTheThread()
{
    // The routing itself, which nothing else here can see. A message row sent
    // down the THREAD path produces the same undo depth and the same status
    // text while tagging every sibling in the conversation: a mutation that did
    // exactly that passed the entire suite, so this test exists because that
    // gap was found rather than because the path looked risky.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);

    ThreadSummary t = makeThread(QStringLiteral("t1"), {});
    t.totalCount = 3;
    model->appendBatch({ t });

    MessageNode root;
    root.messageId = QStringLiteral("m0@example.org");
    root.threadId = QStringLiteral("t1");
    root.depth = 0;
    MessageNode reply;
    reply.messageId = QStringLiteral("m1@example.org");
    reply.threadId = QStringLiteral("t1");
    reply.depth = 1;
    model->setThreadMessages(QStringLiteral("t1"), { root, reply });

    window.show();
    QVERIFY(QTest::qWaitForWindowExposed(&window));

    const QModelIndex threadRow = model->index(0, 0, QModelIndex());
    view->expand(threadRow);
    QApplication::processEvents();

    const QModelIndex messageRow = model->index(0, 0, threadRow);
    QVERIFY(model->isMessageRow(messageRow));

    view->selectionModel()->select(
        messageRow,
        QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
    view->setCurrentIndex(messageRow);
    QApplication::processEvents();

    auto *archive = window.findChild<QAction *>(QStringLiteral("archive"));
    QVERIFY(archive);
    archive->trigger();

    // The change must carry the MESSAGE id and no thread id. Sent as a thread
    // id it would archive the root and every other reply along with it.
    QCOMPARE(window.pendingMessageIdsForTesting(),
             QStringList{ QStringLiteral("m1@example.org") });
    QVERIFY2(window.pendingThreadIdsForTesting().isEmpty(),
             qPrintable(QStringLiteral("the action was sent for thread(s) %1: a "
                                       "message row must not tag its siblings")
                            .arg(window.pendingThreadIdsForTesting()
                                     .join(QStringLiteral(", ")))));

    // And it is undoable, on its own terms rather than the thread's.
    QCOMPARE(window.undoDepthForTesting(), 1);
}

void TestMainWindow::selectingAMessageRowTargetsThatMessageNotItsThread()
{
    // This case builds a bare window, so it has no worker and cannot assert on
    // what the pane renders. A case that needs one opts into
    // WorkerBackedWindow; that is deliberately not done here, since the
    // decision under test is made before any load. What it CAN assert is that
    // decision:
    // a message row must stop tracking a current thread, or a reply arriving
    // for either kind of selection cannot tell which one it belongs to.
    //
    // The trap this covers is specific. threadAt() takes a TOP-LEVEL row
    // number, and a child's row number indexes its siblings, so handing a
    // message row's number to it loads whichever thread happens to sit at that
    // position in the list. Row 0 under a thread is a plausible-looking wrong
    // answer, which is why the fixture puts the reply under the SECOND thread.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);

    ThreadSummary first = makeThread(QStringLiteral("t1"), {});
    ThreadSummary second = makeThread(QStringLiteral("t2"), {});
    second.totalCount = 2;
    model->appendBatch({ first, second });

    MessageNode root;
    root.messageId = QStringLiteral("m0@example.org");
    root.threadId = QStringLiteral("t2");
    root.depth = 0;
    MessageNode reply;
    reply.messageId = QStringLiteral("m1@example.org");
    reply.threadId = QStringLiteral("t2");
    reply.depth = 1;
    model->setThreadMessages(QStringLiteral("t2"), { root, reply });

    window.resize(1400, 300);
    window.show();
    QVERIFY(QTest::qWaitForWindowExposed(&window));

    // Start on a thread row, so the transition to a message row is what is
    // being observed rather than the initial state.
    const QModelIndex threadRow = model->index(1, 0, QModelIndex());
    selectThreadRow(view, 1);
    QApplication::processEvents();
    QCOMPARE(window.currentThreadId(), QStringLiteral("t2"));

    view->expand(threadRow);
    QApplication::processEvents();

    const QModelIndex messageRow = model->index(0, 0, threadRow);
    QVERIFY(messageRow.isValid());
    QVERIFY2(model->isMessageRow(messageRow),
             "the fixture did not produce a message row, so this test would "
             "assert nothing about one");

    view->selectionModel()->select(
        messageRow,
        QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
    view->setCurrentIndex(messageRow);
    QApplication::processEvents();

    // The thread is no longer what the pane is about. Left set, a late
    // loadThread reply would repaint the whole conversation over the single
    // message the user asked for.
    QVERIFY2(window.currentThreadId().isEmpty(),
             qPrintable(QStringLiteral("selecting a reply left the current "
                                       "thread set to '%1': the pane is still "
                                       "tracking the conversation")
                            .arg(window.currentThreadId())));
}

void TestMainWindow::clickingTheExpanderTogglesTheThread()
{
    // The glyph being VISIBLE and the glyph being CLICKABLE are separate
    // properties, and the pixel test for the first passes happily against a
    // triangle nothing can hit. Turning off rootIsDecorated to stop the style
    // drawing its own dot under ours also removed the style's hit area, so the
    // expander rendered perfectly and did nothing.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);

    ThreadSummary t = makeThread(
        QStringLiteral("t1"),
        QStringList{ TagColors::tagForAccountKey(QStringLiteral("work")) });
    t.totalCount = 3;
    model->appendBatch({ t });

    window.resize(1400, 300);
    window.show();
    QVERIFY(QTest::qWaitForWindowExposed(&window));
    QApplication::processEvents();

    const QModelIndex root = model->index(0, 0, QModelIndex());
    const QModelIndex subject =
        model->index(0, 0, QModelIndex());
    const QRect rect = view->visualRect(subject);

    // Guards: the row is drawn, it claims to have replies, and it starts
    // collapsed. Without the last one a toggle test can pass by doing nothing.
    QVERIFY2(rect.height() > 0, "the thread row is not on screen");
    QVERIFY(model->data(subject, ThreadListModel::HasRepliesRole).toBool());
    QVERIFY(!view->isExpanded(root));

    // Aimed at the rect the delegate reports, not at one reconstructed here:
    // the drawn target and the clickable one cannot drift if both come from
    // the same call.
    QStyleOptionViewItem option;
    option.rect = rect;
    option.font = view->font();
    const QRect expander = CardDelegate::expanderRectFor(option, subject);
    QVERIFY2(!expander.isEmpty(), "the card offers no expander to click");
    const QPoint hit = expander.center();

    QTest::mouseClick(view->viewport(), Qt::LeftButton, Qt::NoModifier, hit);
    QApplication::processEvents();
    QVERIFY2(view->isExpanded(root),
             "clicking the expander did not open the thread");

    QTest::mouseClick(view->viewport(), Qt::LeftButton, Qt::NoModifier, hit);
    QApplication::processEvents();
    QVERIFY2(!view->isExpanded(root),
             "clicking the expander again did not close the thread");
}

void TestMainWindow::replyRowsKeepTheirTextUnderTheThreadLine()
{
    // paintEvent runs AFTER the cells, so anything it fills across a reply row
    // covers the text the delegate just drew. The tint and the thread line are
    // both painted there, which makes this the obvious way to ship a block of
    // blank rows.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);

    ThreadSummary t = makeThread(
        QStringLiteral("t1"),
        QStringList{ TagColors::tagForAccountKey(QStringLiteral("work")) });
    t.totalCount = 2;
    model->appendBatch({ t });

    MessageNode first;
    first.messageId = QStringLiteral("m0@example.org");
    first.threadId = QStringLiteral("t1");
    first.depth = 0;
    MessageNode reply;
    reply.messageId = QStringLiteral("m1@example.org");
    reply.threadId = QStringLiteral("t1");
    reply.from = QStringLiteral("A Replier <replier@example.org>");
    reply.subject = QStringLiteral("Re: a subject");
    reply.depth = 1;
    model->setThreadMessages(QStringLiteral("t1"), { first, reply });

    // 600, not 300. The query row carries four built-in filter buttons since
    // item 93, and at 300 the reply row was pushed below the viewport: the
    // pixel loop then ran zero times and reported "0 pixels, the row was
    // painted over", which is a different defect from the one that existed.
    window.resize(1400, 600);
    window.show();
    QVERIFY(QTest::qWaitForWindowExposed(&window));

    const QModelIndex root = model->index(0, 0, QModelIndex());
    view->expand(root);
    QApplication::processEvents();

    const QModelIndex child =
        model->index(0, 0, root);
    const QRect rect = view->visualRect(child);
    QVERIFY2(rect.height() > 0, "the reply row is not on screen");

    // visualRect reports a height for a row scrolled out of the viewport, so
    // the check above passes while the loop below has nothing to walk. Assert
    // the rect is really inside the image, or this measures nothing and says
    // the row was painted over.
    QVERIFY2(rect.top() >= 0
                 && rect.bottom() < view->viewport()->height()
                 && rect.left() >= 0
                 && rect.right() <= view->viewport()->width(),
             qPrintable(QStringLiteral("the reply row at %1..%2 is outside the "
                                       "%3px viewport, so the pixel count "
                                       "below would measure nothing")
                            .arg(rect.top())
                            .arg(rect.bottom())
                            .arg(view->viewport()->height())));

    QImage shot(view->viewport()->size(), QImage::Format_ARGB32);
    shot.fill(Qt::transparent);
    view->viewport()->render(&shot);

    // Count pixels in the sender cell that differ from the row's own tint.
    // Text is the only thing that can produce them.
    const QRgb tint = ThreadListModel::replyBackground().rgb() | 0xff000000;
    int textPixels = 0;
    for (int y = rect.top(); y < qMin(rect.bottom(), shot.height()); ++y) {
        for (int x = rect.left(); x < qMin(rect.right(), shot.width()); ++x) {
            if ((shot.pixel(x, y) | 0xff000000) != tint)
                ++textPixels;
        }
    }

    QVERIFY2(textPixels > 20,
             qPrintable(QStringLiteral("only %1 non-background pixels in the "
                                       "reply's sender cell: the row was "
                                       "painted over after its text was drawn")
                            .arg(textPixels)));
}

void TestMainWindow::aSelectedReadThreadIsNotDimmedIntoTheHighlight()
{
    // Read threads carry a dimmed Qt::ForegroundRole, blended against the
    // UNSELECTED background. Qt's own painting prefers a model foreground over
    // HighlightedText, so without SubjectDelegate::initStyleOption reversing
    // that, selecting a read row paints it grey on the selection colour, which
    // is close to unreadable. Seen in a screenshot before it was caught here.
    //
    // Rendered rather than asserted on roles: the model is right either way,
    // and the defect lives entirely in how the delegate resolves them.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);

    // Both rows READ, so both are dimmed and neither is bold: the only thing
    // that could differ is how the dimming composites against the selection.
    //
    // Comparing a read row against an unread one would not work, and an
    // earlier version of this test did exactly that. Unread also paints bold,
    // so the rows differ legitimately and the comparison says nothing about
    // the selection. That version passed only because the machine it was
    // written on had its Qt font configured Bold, which made every row bold
    // and hid the difference.
    ThreadSummary first = makeThread(QStringLiteral("t1"), {});
    ThreadSummary second = makeThread(QStringLiteral("t2"), {});
    first.subject = second.subject = QStringLiteral("Same subject both rows");
    first.authors = second.authors = QStringLiteral("Someone <s@example.org>");
    model->appendBatch({ first, second });

    window.resize(900, 300);
    window.show();
    QVERIFY(QTest::qWaitForWindowExposed(&window));

    // Row 0 selected, row 1 not. The property under test is that selecting a
    // dimmed row switches it to the highlight's own text colour, so the two
    // rows MUST differ; comparing two identically-styled rows would pass
    // against a delegate that did nothing at all.
    selectThreadRow(view, 0);
    QApplication::processEvents();

    const int rowHeight = threadRowHeight(view, 0);
    QVERIFY(rowHeight > 0);

    QImage shot(view->viewport()->size(), QImage::Format_ARGB32);
    shot.fill(Qt::transparent);
    view->viewport()->render(&shot);

    // What the delegate resolves for each row, which is the thing the fix
    // changes. Rendering alone cannot separate "used the highlight colour"
    // from "used the dim over a highlighted background".
    QStyleOptionViewItem selected;
    selected.initFrom(view);
    selected.state |= QStyle::State_Selected;
    QStyleOptionViewItem unselected;
    unselected.initFrom(view);
    unselected.state &= ~QStyle::State_Selected;

    auto *delegate = qobject_cast<QStyledItemDelegate *>(view->itemDelegate());
    QVERIFY2(delegate, "the thread view has no styled delegate");

    const QModelIndex index =
        model->index(0, 0);

    // initStyleOption is protected, so the resolved palette is reached the way
    // the painter does: through a subclass that exposes it.
    struct Probe : CardDelegate {
        using CardDelegate::initStyleOption;
    };
    const auto *probe = static_cast<const Probe *>(
        static_cast<const CardDelegate *>(delegate));

    probe->initStyleOption(&selected, index);
    probe->initStyleOption(&unselected, index);

    QVERIFY2(selected.palette.color(QPalette::Text)
                 == selected.palette.color(QPalette::HighlightedText),
             "a selected row still resolves to the dimmed text colour, so the "
             "dimming will paint over the selection highlight");
    QVERIFY2(unselected.palette.color(QPalette::Text)
                 != selected.palette.color(QPalette::Text),
             "an unselected read row lost its dimming");
}

void TestMainWindow::markAllReadIsDisabledUntilTheQueryFinishes()
{
    // Threads arrive in batches, so acting mid-load would silently skip
    // whatever had not arrived. Rather than acting on part of the view and
    // calling it "all", or stalling on a wait the user cannot see, the action
    // is simply unavailable until the result set is complete.
    const Config config;
    MainWindow window(config);

    auto *action = window.findChild<QAction *>(QStringLiteral("mark_all_read"));
    QVERIFY2(action, "no mark_all_read action registered");

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);

    // A query in flight: rows are arriving but the worker has not said it is
    // done, so the action must stay out of reach.
    // A query is needed for runCurrentQuery to do anything: it returns early
    // on an empty one, which would leave the flag untouched.
    window.findChild<QLineEdit *>()->setText(QStringLiteral("tag:inbox"));
    QMetaObject::invokeMethod(&window, "runCurrentQuery");
    model->appendBatch({ makeThread(QStringLiteral("t1"),
                                    { QStringLiteral("unread") }) });
    QVERIFY2(!action->isEnabled(),
             "the action was live while the query was still loading");

    // The generation must match or the reply is discarded as stale, which is
    // how a superseded query is ignored everywhere else in this window.
    const quint64 generation = window.currentGenerationForTesting();
    QMetaObject::invokeMethod(&window, "onQueryFinished",
                              Q_ARG(int, 1), Q_ARG(quint64, generation));
    QVERIFY2(action->isEnabled(),
             "the action stayed disabled after the query finished");
}

void TestMainWindow::markAllReadActsOnEveryRowAndUndoesInOneStep()
{
    // Every row in the view, not just the selected ones, and one undo entry for
    // the batch: a user who marks 400 threads read expects one Ctrl+Z to be
    // enough.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *action = window.findChild<QAction *>(QStringLiteral("mark_all_read"));
    QVERIFY(action);

    // A query is needed for runCurrentQuery to do anything: it returns early
    // on an empty one, which would leave the flag untouched.
    window.findChild<QLineEdit *>()->setText(QStringLiteral("tag:inbox"));
    QMetaObject::invokeMethod(&window, "runCurrentQuery");
    model->appendBatch({ makeThread(QStringLiteral("t1"),
                                    { QStringLiteral("unread") }),
                         makeThread(QStringLiteral("t2"),
                                    { QStringLiteral("unread") }),
                         makeThread(QStringLiteral("t3"),
                                    { QStringLiteral("unread"),
                                      QStringLiteral("flagged") }) });
    QMetaObject::invokeMethod(&window, "onQueryFinished", Q_ARG(int, 3),
                              Q_ARG(quint64,
                                    window.currentGenerationForTesting()));

    // One row selected, to prove the action ignores the selection rather than
    // acting on it.
    selectThreadRow(view, 0);

    action->trigger();

    for (int row = 0; row < 3; ++row) {
        QVERIFY2(!model->threadAt(row).tags.contains(QStringLiteral("unread")),
                 qPrintable(QStringLiteral("row %1 kept its unread tag")
                                .arg(row)));
    }
    // An unrelated tag on a row is untouched: only unread is removed.
    QVERIFY(model->threadAt(2).tags.contains(QStringLiteral("flagged")));

    // ONE undo entry for the whole batch, not one per thread. Asserted as a
    // depth, since triggering undo once and finding everything restored would
    // also pass if three commands had been pushed and the model happened to
    // recover on the first.
    QCOMPARE(window.undoDepthForTesting(), 1);

    auto *undo = window.findChild<QAction *>(QStringLiteral("undo"));
    QVERIFY(undo);
    undo->trigger();

    for (int row = 0; row < 3; ++row) {
        QVERIFY2(model->threadAt(row).tags.contains(QStringLiteral("unread")),
                 qPrintable(QStringLiteral("row %1 was not restored by one undo")
                                .arg(row)));
    }
}

void TestMainWindow::markAllReadDoesNothingWhenNothingIsUnread()
{
    // No write, no undo entry, and no pending edit for a view that is already
    // read: an undo entry that restores nothing is worse than none, since it
    // absorbs a Ctrl+Z the user meant for their previous action.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *action = window.findChild<QAction *>(QStringLiteral("mark_all_read"));
    QVERIFY(action);

    // A query is needed for runCurrentQuery to do anything: it returns early
    // on an empty one, which would leave the flag untouched.
    window.findChild<QLineEdit *>()->setText(QStringLiteral("tag:inbox"));
    QMetaObject::invokeMethod(&window, "runCurrentQuery");
    model->appendBatch({ makeThread(QStringLiteral("t1"),
                                    { QStringLiteral("flagged") }),
                         makeThread(QStringLiteral("t2"), {}) });
    QMetaObject::invokeMethod(&window, "onQueryFinished", Q_ARG(int, 2),
                              Q_ARG(quint64,
                                    window.currentGenerationForTesting()));

    QCOMPARE(window.undoDepthForTesting(), 0);

    action->trigger();

    // The real assertion: no command was pushed. Checking only that the tags
    // did not change would pass against a version that sent a no-op write for
    // every row, which still costs an undo entry and a pending edit each. The
    // undo QAction cannot answer this: it is always enabled and tests canUndo()
    // when triggered.
    QVERIFY2(window.undoDepthForTesting() == 0,
             "an undo entry was pushed for a view with nothing unread");
    QVERIFY(model->threadAt(0).tags.contains(QStringLiteral("flagged")));

    // And it says so rather than appearing to have done something.
    auto *status = window.findChild<QLabel *>(QStringLiteral("statusMessage"));
    QVERIFY(status);
    QVERIFY2(status->text().contains(QStringLiteral("Nothing unread")),
             qPrintable(status->text()));
}

void TestMainWindow::markReadTimerRestartsRatherThanStacking()
{
    // The plan's hard requirement: arrowing quickly down a list must not mark
    // every thread passed through as read, only the one still selected when the
    // timer fires. A stacked timer per selection would mark all of them.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *timer = window.findChild<QTimer *>(QStringLiteral("markReadTimer"));
    QVERIFY(timer);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);

    model->appendBatch({ makeThread(QStringLiteral("t1"),
                                    { QStringLiteral("unread") }),
                         makeThread(QStringLiteral("t2"),
                                    { QStringLiteral("unread") }),
                         makeThread(QStringLiteral("t3"),
                                    { QStringLiteral("unread") }) });

    selectThreadRow(view, 0);
    QVERIFY2(timer->isActive(), "no timer armed for an unread thread");

    // Move on before it can fire. One timer stays armed, not three.
    selectThreadRow(view, 1);
    QVERIFY(timer->isActive());
    selectThreadRow(view, 2);
    QVERIFY(timer->isActive());

    // Exactly one timer exists at all, which is what "restarted, not stacked"
    // means concretely.
    QCOMPARE(window.findChildren<QTimer *>(QStringLiteral("markReadTimer")).size(),
             1);
}

void TestMainWindow::markReadTimerIsNotArmedForAReadThread()
{
    // Opening a thread that is already read must not schedule a write that
    // would change nothing.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *timer = window.findChild<QTimer *>(QStringLiteral("markReadTimer"));
    QVERIFY(timer);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);

    model->appendBatch({ makeThread(QStringLiteral("read"),
                                    { QStringLiteral("inbox") }),
                         makeThread(QStringLiteral("unread"),
                                    { QStringLiteral("unread") }) });

    selectThreadRow(view, 0);
    QVERIFY2(!timer->isActive(), "armed a timer for an already-read thread");

    // And the unread one still arms, so this is not "never arms".
    selectThreadRow(view, 1);
    QVERIFY(timer->isActive());

    // Moving back to a read thread disarms it again, rather than leaving the
    // previous thread's timer running to fire against the wrong row.
    selectThreadRow(view, 0);
    QVERIFY(!timer->isActive());
}

void TestMainWindow::markReadCanBeDisabled()
{
    // A negative delay turns the behaviour off entirely. Documented, so it must
    // work rather than being clamped to "immediately".
    QTemporaryDir dir;
    const QString path = dir.filePath(QStringLiteral("qtmaildir.conf"));
    {
        QFile file(path);
        QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
        file.write("[general]\nmark_read_delay_ms=-1\n");
    }

    Config config;
    config.load(path);
    QCOMPARE(config.markReadDelayMs(), -1);

    MainWindow window(config);
    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *timer = window.findChild<QTimer *>(QStringLiteral("markReadTimer"));
    QVERIFY(timer);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);

    model->appendBatch({ makeThread(QStringLiteral("t1"),
                                    { QStringLiteral("unread") }) });
    selectThreadRow(view, 0);

    QVERIFY2(!timer->isActive(),
             "a negative mark_read_delay_ms must disable the timer");
}

void TestMainWindow::pendingEditCountSurvivesAQuery()
{
    // The defining property, and the reason this is a counter of its own rather
    // than QUndoStack::isClean(): the undo stack is cleared on every query,
    // because its entries refer to rows the new result set discards. Tag a
    // thread, run any query, and the stack is empty while the change is still
    // sitting unsynced in the database.
    const Config config;
    MainWindow window(config);

    auto *label = window.findChild<QLabel *>(QStringLiteral("pendingEdits"));
    QVERIFY(label);
    QVERIFY2(label->isHidden(), "the indicator must start hidden at zero");

    // Confirm a write the way the worker really does, by emitting the signal
    // the window listens to. No test-only entry point on MainWindow.
    TagChange change;
    change.added = { QStringLiteral("deleted") };
    change.description = QStringLiteral("Delete");
    QVERIFY(QMetaObject::invokeMethod(&window, "onTagsApplied",
                                      Q_ARG(TagChange, change)));

    QVERIFY2(!label->isHidden(), "a confirmed edit must show the indicator");
    const QString afterEdit = label->text();
    QVERIFY(!afterEdit.isEmpty());

    // Now run a query, which clears the undo stack. The indicator must not
    // follow it down.
    window.findChild<QLineEdit *>()->setText(QStringLiteral("tag:inbox"));
    QMetaObject::invokeMethod(&window, "runCurrentQuery");

    QVERIFY2(!label->isHidden(),
             "the indicator was cleared by a query, so it is tracking the undo "
             "stack rather than unsynced state");
    QCOMPARE(label->text(), afterEdit);
}

void TestMainWindow::aFailedSyncDoesNotClearThePendingCount()
{
    // A failed sync means the edits are still unsynced. Clearing here would
    // assert the opposite, and the user would quit believing their tagging had
    // been carried over.
    const Config config;
    MainWindow window(config);

    auto *label = window.findChild<QLabel *>(QStringLiteral("pendingEdits"));
    QVERIFY(label);

    TagChange change;
    change.added = { QStringLiteral("flagged") };
    QVERIFY(QMetaObject::invokeMethod(&window, "onTagsApplied",
                                      Q_ARG(TagChange, change)));
    QVERIFY(!label->isHidden());
    const QString afterEdit = label->text();

    QMetaObject::invokeMethod(&window, "onSyncFinished",
                              Q_ARG(bool, false), Q_ARG(int, 1));
    QVERIFY2(!label->isHidden(), "a FAILED sync cleared the pending count");
    QCOMPARE(label->text(), afterEdit);

    // A successful one does clear it, so this is not "never clears".
    QMetaObject::invokeMethod(&window, "onSyncFinished",
                              Q_ARG(bool, true), Q_ARG(int, 0));
    QVERIFY2(label->isHidden(), "a successful sync must clear the indicator");
}

/// Closes a window and reports whether it accepted, failing rather than hanging
/// if a modal appears.
///
/// A modal spins its own event loop, so a test that simply sends a close event
/// blocks forever when a dialog it did not expect opens. This arms a timer that
/// closes any active modal and records that one was there, which turns "a
/// dialog appeared" into an assertion instead of a hung run.
struct CloseProbe
{
    bool accepted = false;
    bool sawModal = false;

    void run(MainWindow *window)
    {
        QTimer poll;
        poll.setInterval(50);
        int ticks = 0;
        QObject::connect(&poll, &QTimer::timeout, [this, &poll, &ticks]() {
            if (QWidget *modal = QApplication::activeModalWidget()) {
                sawModal = true;
                modal->close();
                poll.stop();
                return;
            }
            if (++ticks > 20)   // one second is ample for a synchronous close
                poll.stop();
        });
        poll.start();

        QCloseEvent event;
        QApplication::sendEvent(window, &event);
        accepted = event.isAccepted();
        poll.stop();
    }
};

void TestMainWindow::closingWithNoPendingEditsDoesNotPrompt()
{
    // Nothing outstanding means nothing to ask about. If a prompt fires here it
    // is keying off something other than there being work to lose, and every
    // quit would carry a dialog.
    const Config config;
    MainWindow window(config);

    CloseProbe probe;
    probe.run(&window);

    QVERIFY2(!probe.sawModal, "a clean window prompted on close");
    QVERIFY2(probe.accepted, "a clean window refused to close");
}

void TestMainWindow::syncOnExitNeverClosesSilently()
{
    // "never" is the behaviour that existed before the prompt did, and it has
    // to stay reachable for anyone who does not want to be asked. It must hold
    // whether or not a sync command is configured, so this covers both: the
    // no-command path has its own dialog, and "never" must skip that one too.
    QTemporaryDir dir;
    const QString path = dir.filePath(QStringLiteral("qtmaildir.conf"));
    {
        QFile file(path);
        QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
        file.write("[general]\nsync_on_exit=never\n\n[sync]\ncommand=/bin/true\n");
    }

    Config config;
    config.load(path);
    QCOMPARE(config.syncOnExit(), Config::SyncOnExit::Never);

    MainWindow window(config);

    // Give it something to lose, so this is not passing for the same reason
    // the previous test does.
    TagChange change;
    change.added = { QStringLiteral("deleted") };
    QVERIFY(QMetaObject::invokeMethod(&window, "onTagsApplied",
                                      Q_ARG(TagChange, change)));
    auto *label = window.findChild<QLabel *>(QStringLiteral("pendingEdits"));
    QVERIFY(label);
    QVERIFY(!label->isHidden());

    CloseProbe probe;
    probe.run(&window);

    QVERIFY2(!probe.sawModal,
             "sync_on_exit=never prompted anyway");
    QVERIFY2(probe.accepted,
             "sync_on_exit=never must close without prompting");
}

void TestMainWindow::selectAllIsBoundAndSelectsEveryRow()
{
    // Multi-select already worked by Ctrl+click and Shift+click; what was
    // missing was a keyboard and menu route to it. The action has to exist as a
    // registered action, not as a raw view shortcut, so it reaches the menu,
    // the shortcut reference and [keys] like every other binding.
    const Config config;
    MainWindow window(config);

    auto *action = window.findChild<QAction *>(QStringLiteral("select_all"));
    QVERIFY2(action, "no select_all action registered");
    QCOMPARE(action->shortcut(), QKeySequence(QStringLiteral("Ctrl+A")));

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);

    model->appendBatch({ makeThread(QStringLiteral("t1"), {}),
                         makeThread(QStringLiteral("t2"), {}),
                         makeThread(QStringLiteral("t3"), {}) });

    action->trigger();

    QCOMPARE(view->selectionModel()->selectedRows().size(), 3);
}

void TestMainWindow::aMultiRowSelectionDoesNotArmTheMarkReadTimer()
{
    // A selection gesture must never mutate mail. current follows the keyboard
    // cursor as a selection extends, so without a guard every row swept through
    // by Shift+arrow would be queued to be marked read: threads the user only
    // ever selected, never opened.
    //
    // Note selectAll() on a fresh view is NOT the case to test here: it leaves
    // current invalid and emits no currentRowChanged at all (verified against
    // Qt 6.11), so it would pass without any guard in place. The real path is a
    // row already current, which is how a user reaches select-all: click a
    // thread, then Ctrl+A.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *timer = window.findChild<QTimer *>(QStringLiteral("markReadTimer"));
    QVERIFY(timer);

    model->appendBatch({ makeThread(QStringLiteral("t1"),
                                    { QStringLiteral("unread") }),
                         makeThread(QStringLiteral("t2"),
                                    { QStringLiteral("unread") }),
                         makeThread(QStringLiteral("t3"),
                                    { QStringLiteral("unread") }) });

    // Sweep down as Shift+arrow does: current moves onto a row while the
    // selection already spans more than one.
    selectThreadRow(view, 0);
    view->selectionModel()->select(
        model->index(1, 0),
        QItemSelectionModel::Select | QItemSelectionModel::Rows);
    view->selectionModel()->setCurrentIndex(
        model->index(1, 0),
        QItemSelectionModel::Select | QItemSelectionModel::Rows);

    QVERIFY2(view->selectionModel()->selectedRows().size() > 1,
             "test setup failed to build a multi-row selection");
    QVERIFY2(!timer->isActive(),
             "a multi-row selection armed the mark-read timer");
}

void TestMainWindow::growingASelectionCancelsAnAlreadyArmedTimer()
{
    // The ordering trap: clicking one row arms the timer legitimately, and only
    // then does the selection grow. Guarding the new selection alone is not
    // enough, the timer already running for the first row has to be cancelled
    // or that thread goes read behind a pane that no longer shows it.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *timer = window.findChild<QTimer *>(QStringLiteral("markReadTimer"));
    QVERIFY(timer);

    model->appendBatch({ makeThread(QStringLiteral("t1"),
                                    { QStringLiteral("unread") }),
                         makeThread(QStringLiteral("t2"),
                                    { QStringLiteral("unread") }) });

    selectThreadRow(view, 0);
    QVERIFY2(timer->isActive(), "no timer armed for a single unread thread");

    // Extend to a second row, as Shift+click would.
    view->selectionModel()->select(
        model->index(1, 0),
        QItemSelectionModel::Select | QItemSelectionModel::Rows);

    QVERIFY2(!timer->isActive(),
             "extending the selection left the first row's timer running");
}

void TestMainWindow::collapsingBackToOneRowLoadsThatThreadAgain()
{
    // The guard must not be a one-way door. Narrowing a multi-row selection
    // back to a single row is ordinary reading again, so the timer arms as it
    // always did.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *timer = window.findChild<QTimer *>(QStringLiteral("markReadTimer"));
    QVERIFY(timer);

    model->appendBatch({ makeThread(QStringLiteral("t1"),
                                    { QStringLiteral("unread") }),
                         makeThread(QStringLiteral("t2"),
                                    { QStringLiteral("unread") }) });

    view->selectAll();
    QVERIFY(!timer->isActive());

    // Back to one row, as a plain click would leave it.
    selectThreadRow(view, 1);

    QVERIFY2(timer->isActive(),
             "collapsing back to one row did not resume mark-read");
}

void TestMainWindow::theStatusBarReportsAMultiRowSelection()
{
    // The actual discoverability gap: the UI never acknowledged a selection, so
    // nothing taught the user that selecting more than one row was possible.
    // A count that appears while the selection is being built does.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *status = window.findChild<QLabel *>(QStringLiteral("statusMessage"));
    QVERIFY2(status, "no status label to report into");

    model->appendBatch({ makeThread(QStringLiteral("t1"), {}),
                         makeThread(QStringLiteral("t2"), {}),
                         makeThread(QStringLiteral("t3"), {}) });

    view->selectAll();

    QVERIFY2(status->text().contains(QStringLiteral("3")),
             qPrintable(QStringLiteral("status bar does not report the selection "
                                       "size, it says '%1'").arg(status->text())));
}

void TestMainWindow::clearSelectionBlanksThePaneAndDeselects()
{
    // Item 50: the user asked for "two actions instead of one", so this is the
    // new action and clearPaneLeavesTheSelectionAlone() below pins the old one.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);

    model->appendBatch({ makeThread(QStringLiteral("t1"), {}),
                         makeThread(QStringLiteral("t2"), {}) });

    // From a row that is already current, which is both how a user reaches this
    // and what CLAUDE.md requires: selectAll() on a fresh view emits no
    // currentRowChanged at all, so a test starting there passes against a
    // missing guard.
    selectThreadRow(view, 0);
    QCOMPARE(view->selectionModel()->selectedRows().size(), 1);

    auto *action = window.findChild<QAction *>(QStringLiteral("clear_selection"));
    QVERIFY2(action, "no clear_selection action");
    action->trigger();

    QVERIFY2(view->selectionModel()->selectedRows().isEmpty(),
             "the row is still selected: this action's whole point is that it "
             "deselects as well as blanking");

    // The hazard: clearSelection() leaves currentIndex() VALID, so
    // onSelectionChanged() takes its "one or fewer rows" branch, sees a current
    // row whose id differs from the just-cleared m_currentThreadId, and calls
    // onThreadSelected for it, which sets m_currentThreadId again and sends a
    // loadThread. The pane would then repaint itself a moment later.
    //
    // **currentThreadId() is what detects that, not the pane.** This fixture
    // has no worker, so loadThread never replies and nothing ever repaints;
    // asserting showingPlaceholder() here passes whatever the code does, which
    // CLAUDE.md records as the standing limit of test_mainwindow. What IS
    // observable is the id the window set on its way to that request.
    QVERIFY2(window.currentThreadId().isEmpty(),
             qPrintable(QStringLiteral("a thread was re-adopted after the "
                                       "selection was cleared: currentThreadId "
                                       "is '%1', and a loadThread for it is "
                                       "already in flight")
                            .arg(window.currentThreadId())));

    // And current itself is gone, so no later collapse-to-one-row can reload
    // it either.
    QVERIFY2(!view->currentIndex().isValid(),
             "currentIndex is still valid, so onSelectionChanged can reload "
             "that row on the next selection change");
}

void TestMainWindow::clearPaneLeavesTheSelectionAlone()
{
    // The pre-existing action keeps its behaviour. Item 32 built it to blank
    // WITHOUT touching the selection, and a user who binds it is entitled to
    // that; item 50 adds a second action rather than changing this one.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);

    model->appendBatch({ makeThread(QStringLiteral("t1"), {}),
                         makeThread(QStringLiteral("t2"), {}) });

    selectThreadRow(view, 0);
    QCOMPARE(view->selectionModel()->selectedRows().size(), 1);

    auto *action = window.findChild<QAction *>(QStringLiteral("clear_pane"));
    QVERIFY2(action, "no clear_pane action");
    action->trigger();

    QCOMPARE(view->selectionModel()->selectedRows().size(), 1);
}

void TestMainWindow::maildirOverviewShowsUnknownRatherThanZero()
{
    // A field notmuch could not answer must not render as 0. "0 messages" says
    // the Maildir is empty, which is a claim; the truth is that the count
    // failed, and telling someone their mail is gone is the worst available
    // way to report an unreadable index.
    const Config config;
    MainWindow window(config);

    auto *action = window.findChild<QAction *>(QStringLiteral("maildirOverview"));
    QVERIFY2(action, "no maildirOverview action");
    action->trigger();

    auto *counts = window.findChild<QLabel *>(QStringLiteral("maildirCounts"));
    QVERIFY2(counts, "the overview dialog has no counts label");

    // The worker never answers in this fixture, so drive the slot directly
    // with the all-unknown stats a failed open produces.
    QMetaObject::invokeMethod(
        &window, "onDatabaseStatsReady", Qt::DirectConnection,
        Q_ARG(DatabaseStats, DatabaseStats{}),
        Q_ARG(quint64, window.statsGenerationForTesting()));

    QVERIFY2(counts->text().contains(QStringLiteral("unknown")),
             qPrintable(QStringLiteral("counts label says '%1'")
                            .arg(counts->text())));
    QVERIFY2(!counts->text().contains(QStringLiteral(">0<")),
             qPrintable(QStringLiteral("an unanswered count rendered as zero: "
                                       "'%1'").arg(counts->text())));

    // WA_DeleteOnClose, so closing is what frees it. Left open, each test
    // leaks a window for the rest of the run.
    counts->window()->close();
}

void TestMainWindow::maildirOverviewIgnoresAStaleReply()
{
    // Counting every message is slow enough that closing and reopening the
    // dialog while one runs is realistic. The older answer must not fill in the
    // newer dialog, or the numbers silently predate whatever prompted the
    // reopen.
    const Config config;
    MainWindow window(config);

    auto *action = window.findChild<QAction *>(QStringLiteral("maildirOverview"));
    QVERIFY(action);
    action->trigger();

    const quint64 stale = window.statsGenerationForTesting();

    // Reopening bumps the generation, which is what makes the first reply old.
    action->trigger();
    QVERIFY2(window.statsGenerationForTesting() != stale,
             "reopening the dialog did not bump the generation, so a reply for "
             "the previous one cannot be told apart");

    auto *counts = window.findChild<QLabel *>(QStringLiteral("maildirCounts"));
    QVERIFY(counts);
    const QString before = counts->text();

    DatabaseStats old;
    old.messages = 4321;
    old.threads = 999;
    old.tags = 42;
    QMetaObject::invokeMethod(&window, "onDatabaseStatsReady",
                              Qt::DirectConnection,
                              Q_ARG(DatabaseStats, old),
                              Q_ARG(quint64, stale));

    QCOMPARE(counts->text(), before);
    QVERIFY2(!counts->text().contains(QStringLiteral("4321")),
             "a reply for the previous dialog filled in the current one");

    QPointer<QLabel> watch(counts);
    counts->window()->close();

    // WA_DeleteOnClose deletes through deleteLater, so the label outlives
    // close() until the event loop runs. Drain it, or the "reply after the
    // dialog is gone" case below is not actually being tested.
    QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);
    QVERIFY2(watch.isNull(),
             "the dialog was not destroyed, so the case below is not the one "
             "this test means to exercise");

    // The QPointer's reason for being: counting a large database takes long
    // enough that closing the dialog first is ordinary, and the reply then
    // arrives for a label that has been deleted. A raw pointer would dangle
    // here, so this must not crash.
    QMetaObject::invokeMethod(&window, "onDatabaseStatsReady",
                              Qt::DirectConnection,
                              Q_ARG(DatabaseStats, old),
                              Q_ARG(quint64,
                                    window.statsGenerationForTesting()));
}

void TestMainWindow::theThreadListOffersAContextMenu()
{
    // Right-click is the other half of discoverability: until now every tag
    // action was keyboard-only, so the Ctrl+T dialog in particular could not be
    // reached with the mouse at all.
    const Config config;
    MainWindow window(config);

    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    QCOMPARE(view->contextMenuPolicy(), Qt::CustomContextMenu);

    // The menu must reuse the registered QActions rather than build parallel
    // ones, or a [keys] rebinding would show the old shortcut here and the
    // menu could drift out of step with what the keyboard really does.
    auto *menu = window.findChild<QMenu *>(QStringLiteral("threadContextMenu"));
    QVERIFY2(menu, "no thread-list context menu");

    const QStringList expected = { QStringLiteral("archive"),
                                   QStringLiteral("delete"),
                                   QStringLiteral("spam"),
                                   QStringLiteral("toggle_unread"),
                                   QStringLiteral("edit_tags"),
                                   QStringLiteral("flag") };
    for (const QString &name : expected) {
        QAction *action = window.findChild<QAction *>(name);
        QVERIFY2(action, qPrintable(QStringLiteral("no action '%1'").arg(name)));
        QVERIFY2(menu->actions().contains(action),
                 qPrintable(QStringLiteral("context menu is missing the "
                                           "registered '%1' action").arg(name)));
    }
}

void TestMainWindow::aSecondRowBlanksThePaneNotOnlyAThird()
{
    // Reported by hand testing: selecting a second thread left it displayed,
    // and only a third blanked the pane. The cause is that currentRowChanged is
    // emitted before the selection model updates, so the Ctrl+click that makes
    // the count two arrives at onThreadSelected still reporting one, which
    // loads the thread; onSelectionChanged then blanks the pane, and the load,
    // being queued to the worker, paints over the blank when it returns. By the
    // third row m_currentThreadId is already cleared, so the late result is
    // discarded and the blank survives, which is why the fault looked like an
    // off-by-one in the threshold rather than a race.
    //
    // Two rows must behave exactly as three do.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *timer = window.findChild<QTimer *>(QStringLiteral("markReadTimer"));
    QVERIFY(timer);

    model->appendBatch({ makeThread(QStringLiteral("t1"),
                                    { QStringLiteral("unread") }),
                         makeThread(QStringLiteral("t2"),
                                    { QStringLiteral("unread") }),
                         makeThread(QStringLiteral("t3"),
                                    { QStringLiteral("unread") }) });

    // One row: ordinary reading, so a timer is armed and a thread is current.
    selectThreadRow(view, 0);
    QCOMPARE(view->selectionModel()->selectedRows().size(), 1);
    QVERIFY(timer->isActive());

    // Ctrl+click a second row. This is the exact gesture that failed: the
    // selection becomes two while currentRowChanged still reports one.
    view->selectionModel()->setCurrentIndex(
        model->index(1, 0),
        QItemSelectionModel::Select | QItemSelectionModel::Rows);

    QCOMPARE(view->selectionModel()->selectedRows().size(), 2);
    QVERIFY2(!timer->isActive(),
             "two selected rows left the mark-read timer armed");

    // A blanked pane is one with no current thread: anything still in flight
    // for that id would repaint over it.
    QVERIFY2(window.currentThreadId().isEmpty(),
             qPrintable(QStringLiteral("two selected rows left thread '%1' "
                                       "loaded in the pane")
                            .arg(window.currentThreadId())));
}

void TestMainWindow::aLocalSyncIsNotReportedAsABackgroundOne()
{
    // Reported by hand testing: a manual sync ended with "Sync finished
    // elsewhere" stamped over its own result. The monitor sees the lock the
    // local run takes, and while the process lives isRunning() suppresses the
    // message; but the process exits, and therefore isRunning() goes false,
    // BEFORE the next poll notices the lock was released. That poll then
    // reported a local sync as a background one.
    //
    // Ownership is latched when the lock appears, so the release can still be
    // attributed after the process is gone.
    const Config config;
    MainWindow window(config);

    auto *status = window.findChild<QLabel *>(QStringLiteral("statusMessage"));
    QVERIFY(status);

    // The lock appears while no local sync is running: a background one.
    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Running));
    QVERIFY2(status->text().contains(QStringLiteral("Background")),
             qPrintable(QStringLiteral("a background sync was not announced, "
                                       "status says '%1'").arg(status->text())));

    // The finish is no longer ANNOUNCED, since item 35b made the refresh
    // silent, but it must still be acted on: the running message it wrote is
    // retired. Asserting the absence of "running" rather than the presence of
    // "completed" keeps the test on this window's subject, which is that the
    // lock period was attributed to a background sync rather than to a local
    // one, without pinning wording that has already changed once.
    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Idle));
    QVERIFY2(!status->text().contains(QStringLiteral("running")),
             qPrintable(QStringLiteral("a finished background sync left the bar "
                                       "claiming it was still running: '%1'")
                            .arg(status->text())));

}

void TestMainWindow::aLocalSyncsOwnLockIsNeverReportedAsBackground()
{
    // The reported bug, staged at the seam where it actually lives.
    //
    // A real child process was tried first and abandoned: it needs a sync
    // command in the config, it leaves a live process behind for the length of
    // the test, and it made the suite pop a dialog. None of that is needed,
    // because the defect is not in MailSync. It is that ownership of a lock
    // period was decided at RELEASE time, when MailSync::isRunning() has
    // already gone false, instead of being latched when the lock appeared.
    //
    // With no sync command configured isRunning() is false throughout, which is
    // exactly the state the buggy code misread. So: announce a Running that the
    // window believes is external, then a matching Idle. Both must be reported.
    // The local case is covered by the latch being set only inside the Running
    // branch, and by aSkippedLocalSyncStillReportsTheOtherRunFinishing()
    // proving the latch is handed back when the lock was never ours.
    const Config config;
    MainWindow window(config);

    auto *status = window.findChild<QLabel *>(QStringLiteral("statusMessage"));
    QVERIFY(status);
    auto *progress =
        window.findChild<QProgressBar *>(QStringLiteral("syncProgress"));
    QVERIFY(progress);

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Running));
    QVERIFY2(progress->isVisibleTo(&window),
             "a background sync did not show the progress bar");

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Idle));
    QVERIFY2(!progress->isVisibleTo(&window),
             "the progress bar outlived the background sync");

    // An Unknown transition means the lock table could not be read. Nothing was
    // observed, so nothing may be claimed: the previous message must stand.
    status->setText(QStringLiteral("untouched"));
    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Unknown));
    QCOMPARE(status->text(), QStringLiteral("untouched"));
}

void TestMainWindow::aSkippedLocalSyncStillReportsTheOtherRunFinishing()
{
    // The narrow case the latch could break: a manual sync that exits 75
    // because cron already holds the lock. If both started inside one poll
    // interval the monitor sees the lock appear while isRunning() is true and
    // latches it local, even though the lock belongs to the cron run. The
    // completion of that run would then be swallowed. onSyncFinished() hands
    // ownership back when it sees the skip code.
    const Config config;
    MainWindow window(config);

    auto *status = window.findChild<QLabel *>(QStringLiteral("statusMessage"));
    QVERIFY(status);

    QMetaObject::invokeMethod(&window, "onSyncFinished",
                              Q_ARG(bool, false),
                              Q_ARG(int, MainWindow::kSyncSkippedExitCode));

    // The skip itself is reported, and not as a failure.
    QVERIFY2(!status->text().contains(QStringLiteral("failed")),
             qPrintable(QStringLiteral("a skip was reported as a failure: '%1'")
                            .arg(status->text())));

    // The other run finishing must still be ACTED ON. What that means depends
    // on the state of the list, and this fixture has an empty one with nothing
    // selected, so item 35a's free-refresh branch is what a handed-back lock
    // reaches: the window re-runs the query rather than printing "press Enter".
    //
    // Asserting the generation rather than the status text is deliberate. The
    // property under test is that the Idle was attributed to the OTHER run
    // instead of being swallowed as this window's own; which of the two
    // responses it then produces is item 35a's business, and pinning the
    // wording here would fail every time that decision is revisited.
    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);
    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();
    const quint64 before = window.currentGenerationForTesting();

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Idle));
    QVERIFY2(window.currentGenerationForTesting() > before,
             "after a skipped local sync, the other run finishing was "
             "swallowed rather than attributed to the background sync");
}

void TestMainWindow::aCronSyncRefreshesTheListWithoutAQuery()
{
    // Item 35b. A background sync brings the list up to date on its own, with
    // no keystroke: this is the whole point of the item, and it holds whether
    // the list is empty or full.
    const Config config;
    MainWindow window(config);

    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);
    queryEdit->setText(QStringLiteral("tag:unread"));
    queryEdit->returnPressed();

    // The refresh is observed as a NEW query being issued. There is no worker
    // in this fixture, so no result ever arrives; what is asserted is that the
    // query went out at all, which is what used to be missing.
    const quint64 before = window.currentGenerationForTesting();

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Idle));

    QVERIFY2(window.currentGenerationForTesting() > before,
             "a finished cron sync did not refresh the list");
}

void TestMainWindow::aCronSyncRefreshesOverASelectionWithoutClearingIt()
{
    // The behaviour 0.8.0 refused to build and the reason it refused: a refresh
    // used to mean runCurrentQuery(), which clears the model, the selection and
    // the message pane, so firing it on a cron timer would close the thread
    // being read six times an hour.
    //
    // refreshCurrentQuery() reconciles instead, so the refresh runs AND the
    // selection survives. A test that only checked the query was issued would
    // pass against the destructive version, which is the version this item
    // exists to avoid.
    const Config config;
    MainWindow window(config);

    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);
    queryEdit->setText(QStringLiteral("tag:unread"));
    queryEdit->returnPressed();

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<ThreadListView *>();
    QVERIFY(view);

    model->appendBatch({ makeThread(QStringLiteral("T1"),
                                    { QStringLiteral("unread") }) });
    const QModelIndex first = model->index(0, 0, QModelIndex());
    QVERIFY(first.isValid());
    view->setCurrentIndex(first);
    QVERIFY2(view->selectionModel()->hasSelection(),
             "the fixture failed to select a row, so this proves nothing");

    const quint64 before = window.currentGenerationForTesting();

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Idle));

    QVERIFY2(window.currentGenerationForTesting() > before,
             "a cron sync did not refresh a populated list");

    // The rows are untouched until the result comes back, and the selection is
    // still there. A clear() would have emptied both.
    QCOMPARE(model->rowCount(QModelIndex()), 1);
    QVERIFY2(view->selectionModel()->hasSelection(),
             "the refresh cleared the selection, which is what made the old "
             "one unusable on a cron timer");
}

void TestMainWindow::aCronSyncRefreshesTheLastRunQueryNotTheQueryBar()
{
    // The query bar holds text the user has typed but not run, and a refresh
    // must not execute it: that is a search they never asked for. The refresh
    // re-runs the LAST RUN query, so with none there is nothing to do.
    //
    // Since item 93 a default Config DOES run a query at startup: the default
    // startup name resolves to the built-in Unread filter, where before it
    // named nothing and a fresh window had no last-run query at all.
    //
    // The property under test survives that, and is the one that matters on a
    // cron timer: the refresh re-runs the LAST RUN query, never the text
    // sitting in the bar. So the bar is given something the user has typed and
    // not run, and the assertion is that what the refresh runs is still the
    // startup query.
    const Config config;
    MainWindow window(config);

    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);

    const QString ranAtStartup = window.lastRunQueryForTesting();
    QVERIFY2(!ranAtStartup.isEmpty(),
             "no startup query ran, so a refresh has nothing to re-run and "
             "this test cannot distinguish the two sources");

    queryEdit->setText(QStringLiteral("tag:draft-i-was-typing"));

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Idle));

    QCOMPARE(window.lastRunQueryForTesting(), ranAtStartup);
    QVERIFY2(!window.lastRunQueryForTesting().contains(
                 QStringLiteral("draft-i-was-typing")),
             "the refresh executed the text in the query bar, which is a "
             "search the user never asked for");
}

void TestMainWindow::aRefreshAddsNewMailAndDropsWhatStoppedMatching()
{
    // The round trip end to end, driven through the real handlers: the refresh
    // query goes out, its batches accumulate, and the result reconciles into
    // the model in one go.
    const Config config;
    MainWindow window(config);

    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);
    queryEdit->setText(QStringLiteral("tag:unread"));
    queryEdit->returnPressed();

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    model->appendBatch({ makeThread(QStringLiteral("T1"),
                                    { QStringLiteral("unread") }),
                         makeThread(QStringLiteral("T2"),
                                    { QStringLiteral("unread") }) });
    QCOMPARE(model->rowCount(QModelIndex()), 2);

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Idle));
    const quint64 refresh = window.currentGenerationForTesting();

    // T2 was read elsewhere and no longer matches; T3 is new mail.
    const QVector<ThreadSummary> result{
        makeThread(QStringLiteral("T3"), { QStringLiteral("unread") }),
        makeThread(QStringLiteral("T1"), { QStringLiteral("unread") })
    };
    QMetaObject::invokeMethod(&window, "onThreadsReady",
                              Q_ARG(QVector<ThreadSummary>, result),
                              Q_ARG(quint64, refresh));

    // Nothing has changed yet: a refresh applies its result whole, never batch
    // by batch, or the first batch would delete every row after it.
    QCOMPARE(model->rowCount(QModelIndex()), 2);
    QCOMPARE(model->threadAt(0).threadId, QStringLiteral("T1"));

    QMetaObject::invokeMethod(&window, "onQueryFinished",
                              Q_ARG(int, 2), Q_ARG(quint64, refresh));

    QCOMPARE(model->rowCount(QModelIndex()), 2);
    QCOMPARE(model->threadAt(0).threadId, QStringLiteral("T3"));
    QCOMPARE(model->threadAt(1).threadId, QStringLiteral("T1"));
}

void TestMainWindow::theOpenThreadLeavingTheListRaisesTheStaleNotice()
{
    // The user is reading a thread when a refresh drops it: read the last
    // unread message and the thread stops matching tag:unread. The pane keeps
    // rendering it, correctly, so without a notice the message becomes an
    // orphan with no route back to its thread.
    const Config config;
    MainWindow window(config);

    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);
    queryEdit->setText(QStringLiteral("tag:unread"));
    queryEdit->returnPressed();

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<ThreadListView *>();
    QVERIFY(view);
    auto *pane = window.findChild<MessageView *>();
    QVERIFY(pane);

    model->appendBatch({ makeThread(QStringLiteral("T1"),
                                    { QStringLiteral("unread") }) });
    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    QCOMPARE(window.currentThreadId(), QStringLiteral("T1"));
    QVERIFY(pane->staleThreadId().isEmpty());

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Idle));
    const quint64 refresh = window.currentGenerationForTesting();

    // The refresh comes back empty: the thread was read and is gone.
    QMetaObject::invokeMethod(&window, "onQueryFinished",
                              Q_ARG(int, 0), Q_ARG(quint64, refresh));

    QCOMPARE(model->rowCount(QModelIndex()), 0);
    QCOMPARE(pane->staleThreadId(), QStringLiteral("T1"));
}

void TestMainWindow::aThreadStillMatchingRaisesNoStaleNotice()
{
    // The common case, and the guard on the test above: a refresh that changes
    // nothing must be invisible. A notice on every sync would be noise, and it
    // would be a lie.
    const Config config;
    MainWindow window(config);

    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);
    queryEdit->setText(QStringLiteral("tag:unread"));
    queryEdit->returnPressed();

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<ThreadListView *>();
    QVERIFY(view);
    auto *pane = window.findChild<MessageView *>();
    QVERIFY(pane);

    model->appendBatch({ makeThread(QStringLiteral("T1"),
                                    { QStringLiteral("unread") }) });
    view->setCurrentIndex(model->index(0, 0, QModelIndex()));

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Idle));
    const quint64 refresh = window.currentGenerationForTesting();

    const QVector<ThreadSummary> result{
        makeThread(QStringLiteral("T1"), { QStringLiteral("unread") })
    };
    QMetaObject::invokeMethod(&window, "onThreadsReady",
                              Q_ARG(QVector<ThreadSummary>, result),
                              Q_ARG(quint64, refresh));
    QMetaObject::invokeMethod(&window, "onQueryFinished",
                              Q_ARG(int, 1), Q_ARG(quint64, refresh));

    QCOMPARE(model->rowCount(QModelIndex()), 1);
    QVERIFY2(pane->staleThreadId().isEmpty(),
             "a thread that still matches was reported as stale");
}

void TestMainWindow::theStaleNoticeCarriesTheMessageBeingRead()
{
    // Reading reply four of eight when the thread drops out. Recovery has to
    // restore the READER'S place, so the notice carries the message id as well
    // as the thread; without it the thread reopens at its first message.
    //
    // Selecting a message row clears m_currentThreadId, so a notice keyed on
    // that alone never fires for exactly the reader who is deepest into a
    // thread. That is the case this pins.
    const Config config;
    MainWindow window(config);

    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);
    queryEdit->setText(QStringLiteral("tag:unread"));
    queryEdit->returnPressed();

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<ThreadListView *>();
    QVERIFY(view);
    auto *pane = window.findChild<MessageView *>();
    QVERIFY(pane);

    ThreadSummary thread = makeThread(QStringLiteral("T1"),
                                      { QStringLiteral("unread") });
    thread.totalCount = 3;
    model->appendBatch({ thread });

    MessageNode root;
    root.messageId = QStringLiteral("m0@example.org");
    root.threadId = QStringLiteral("T1");
    root.depth = 0;
    MessageNode reply;
    reply.messageId = QStringLiteral("m1@example.org");
    reply.threadId = QStringLiteral("T1");
    reply.depth = 1;
    model->setThreadMessages(QStringLiteral("T1"), { root, reply });

    const QModelIndex threadIndex = model->index(0, 0, QModelIndex());
    const QModelIndex replyIndex = model->index(0, 0, threadIndex);
    QVERIFY(replyIndex.isValid());
    view->setCurrentIndex(replyIndex);

    // A message row, so the window is tracking a message rather than a thread.
    QVERIFY2(window.currentThreadId().isEmpty(),
             "the fixture selected a thread row, so this proves nothing");

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Idle));
    const quint64 refresh = window.currentGenerationForTesting();
    QMetaObject::invokeMethod(&window, "onQueryFinished",
                              Q_ARG(int, 0), Q_ARG(quint64, refresh));

    QCOMPARE(pane->staleThreadId(), QStringLiteral("T1"));
    QCOMPARE(pane->staleMessageId(), QStringLiteral("m1@example.org"));
}

void TestMainWindow::recoveringAStaleThreadQueriesTheWholeThread()
{
    // Clicking "Show it anyway" runs thread:<id>, not a query for the single
    // message: the user asked to get the whole conversation back, with their
    // place in it, so the list has to offer every message of it.
    const Config config;
    MainWindow window(config);

    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);
    queryEdit->setText(QStringLiteral("tag:unread"));
    queryEdit->returnPressed();

    QMetaObject::invokeMethod(&window, "recoverStaleThread",
                              Q_ARG(QString, QStringLiteral("T1")),
                              Q_ARG(QString, QStringLiteral("m1@example.org")));

    QCOMPARE(queryEdit->text(), QStringLiteral("thread:T1"));
}

void TestMainWindow::recoveryReselectsTheMessageThatWasBeingRead()
{
    // The whole point of carrying the message id: reading reply four of eight,
    // the thread comes back, and the selection lands on reply four rather than
    // at the top of the thread.
    //
    // Driven through the real handlers because that is the only way the
    // sequencing is exercised: the query has to come back before the thread
    // can be expanded, and the expansion before the reply row exists at all.
    const Config config;
    MainWindow window(config);

    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);
    queryEdit->setText(QStringLiteral("tag:unread"));
    queryEdit->returnPressed();

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<ThreadListView *>();
    QVERIFY(view);

    QMetaObject::invokeMethod(&window, "recoverStaleThread",
                              Q_ARG(QString, QStringLiteral("T1")),
                              Q_ARG(QString, QStringLiteral("m2@example.org")));
    const quint64 generation = window.currentGenerationForTesting();

    ThreadSummary thread = makeThread(QStringLiteral("T1"), {});
    thread.totalCount = 3;
    const QVector<ThreadSummary> result{ thread };
    QMetaObject::invokeMethod(&window, "onThreadsReady",
                              Q_ARG(QVector<ThreadSummary>, result),
                              Q_ARG(quint64, generation));
    QMetaObject::invokeMethod(&window, "onQueryFinished",
                              Q_ARG(int, 1), Q_ARG(quint64, generation));

    // The thread is listed, and nothing can be selected inside it yet: its
    // replies are not loaded, so the recovery is still pending.
    QCOMPARE(model->rowCount(QModelIndex()), 1);

    MessageNode root;
    root.messageId = QStringLiteral("m0@example.org");
    root.threadId = QStringLiteral("T1");
    root.depth = 0;
    MessageNode first;
    first.messageId = QStringLiteral("m1@example.org");
    first.threadId = QStringLiteral("T1");
    first.depth = 1;
    MessageNode target;
    target.messageId = QStringLiteral("m2@example.org");
    target.threadId = QStringLiteral("T1");
    target.depth = 1;
    const QVector<MessageNode> nodes{ root, first, target };
    QMetaObject::invokeMethod(&window, "onThreadTreeLoaded",
                              Q_ARG(QVector<MessageNode>, nodes),
                              Q_ARG(quint64, generation));

    const QModelIndex current = view->currentIndex();
    QVERIFY2(current.isValid(), "recovery selected nothing");
    QVERIFY2(model->isMessageRow(current),
             "recovery landed on the thread rather than on the message");
    QCOMPARE(model->messageAt(current).messageId,
             QStringLiteral("m2@example.org"));
}

void TestMainWindow::recoveryOnTheFirstMessageSelectsTheThreadRow()
{
    // The trap in the model: setThreadMessages DROPS the depth-0 message,
    // because the root card is that message. So a reader recovering from the
    // thread's first message must land on the ROOT row; looking for it among
    // the children finds nothing and would leave the selection nowhere.
    const Config config;
    MainWindow window(config);

    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);
    queryEdit->setText(QStringLiteral("tag:unread"));
    queryEdit->returnPressed();

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<ThreadListView *>();
    QVERIFY(view);

    QMetaObject::invokeMethod(&window, "recoverStaleThread",
                              Q_ARG(QString, QStringLiteral("T1")),
                              Q_ARG(QString, QStringLiteral("m0@example.org")));
    const quint64 generation = window.currentGenerationForTesting();

    ThreadSummary thread = makeThread(QStringLiteral("T1"), {});
    thread.totalCount = 2;
    const QVector<ThreadSummary> result{ thread };
    QMetaObject::invokeMethod(&window, "onThreadsReady",
                              Q_ARG(QVector<ThreadSummary>, result),
                              Q_ARG(quint64, generation));

    MessageNode root;
    root.messageId = QStringLiteral("m0@example.org");
    root.threadId = QStringLiteral("T1");
    root.depth = 0;
    MessageNode reply;
    reply.messageId = QStringLiteral("m1@example.org");
    reply.threadId = QStringLiteral("T1");
    reply.depth = 1;
    const QVector<MessageNode> nodes{ root, reply };
    QMetaObject::invokeMethod(&window, "onThreadTreeLoaded",
                              Q_ARG(QVector<MessageNode>, nodes),
                              Q_ARG(quint64, generation));

    QMetaObject::invokeMethod(&window, "onQueryFinished",
                              Q_ARG(int, 1), Q_ARG(quint64, generation));

    const QModelIndex current = view->currentIndex();
    QVERIFY2(current.isValid(), "recovery selected nothing");
    QVERIFY2(!model->isMessageRow(current),
             "the thread's first message is the ROOT row, not a child");
    QCOMPARE(model->threadAt(current.row()).threadId, QStringLiteral("T1"));
}

void TestMainWindow::doubleClickingAThreadOpensThatThreadAlone()
{
    // Item 91, the thread case: "double click on a thread loads the whole
    // thread expanded in a view by itself and load the first message in the
    // right pane."
    //
    // The view becomes thread:<id>. Asserted on the query text because that is
    // what makes it a view "by itself"; a gesture that only expanded the row in
    // place would leave every other thread on screen and pass any assertion
    // about the expansion alone.
    const Config config;
    MainWindow window(config);

    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);
    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<ThreadListView *>();
    QVERIFY(view);

    ThreadSummary first = makeThread(QStringLiteral("T1"), {});
    first.totalCount = 3;
    model->appendBatch({ first, makeThread(QStringLiteral("T2"), {}) });
    QCOMPARE(model->rowCount(QModelIndex()), 2);

    const QModelIndex thread = model->index(0, 0, QModelIndex());
    QVERIFY(thread.isValid());

    emit view->doubleClicked(thread);

    QCOMPARE(queryEdit->text(), QStringLiteral("thread:T1"));

    // The recovery target is what carries the expansion and the selection
    // across the two round-trips this takes. Without it the query would run and
    // land on a collapsed card with a blank pane.
    QVERIFY2(window.hasPendingRecoveryForTesting(),
             "the double-click ran a query but asked for nothing to be "
             "expanded or selected in the result");
}

void TestMainWindow::doubleClickingAReplyOpensItsThreadNotTheReplyAlone()
{
    // Item 91, the case most likely to be built wrong: "double click on a reply
    // in a thread should still load the whole thread expanded in a view by
    // itself, with the reply I clicked on visible in the right pane."
    //
    // So NOT id:<reply>. The obvious reading of "open it by itself" is a query
    // for that one message, and it is not what was asked for: the view is the
    // thread, the pane is the reply.
    const Config config;
    MainWindow window(config);

    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);
    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<ThreadListView *>();
    QVERIFY(view);

    ThreadSummary summary = makeThread(QStringLiteral("T1"), {});
    summary.totalCount = 3;
    model->appendBatch({ summary });

    const QModelIndex thread = model->index(0, 0, QModelIndex());
    QVERIFY(thread.isValid());

    // Replies exist only once the tree has loaded, which is what gives this
    // test a child row to double-click at all.
    MessageNode root;
    root.messageId = QStringLiteral("m0@example.org");
    root.threadId = QStringLiteral("T1");
    root.depth = 0;
    MessageNode reply;
    reply.messageId = QStringLiteral("m1@example.org");
    reply.threadId = QStringLiteral("T1");
    reply.depth = 1;
    model->setThreadMessages(QStringLiteral("T1"), { root, reply });

    const QModelIndex replyRow = model->index(0, 0, thread);
    QVERIFY2(replyRow.isValid(), "the fixture built no reply row");
    QVERIFY2(model->isMessageRow(replyRow), "that row is not a message row");

    emit view->doubleClicked(replyRow);

    // The THREAD, not the reply. A query of id:m1@example.org here would be the
    // defect this test exists for.
    QCOMPARE(queryEdit->text(), QStringLiteral("thread:T1"));
    QVERIFY2(window.hasPendingRecoveryForTesting(),
             "nothing was remembered to select the reply once it comes back");
}

void TestMainWindow::doubleClickingDoesNotLeaveTheMarkReadTimerArmed()
{
    // A double-click delivers a single click FIRST, which selects the row and
    // arms the mark-read timer. The user is passing through on their way to
    // opening the thread, so the message must not be marked read behind the
    // drill-down: the timer that the first click armed has to be cancelled.
    //
    // This is the same reasoning the multi-row branch of onThreadSelected()
    // uses, where a selection gesture must not mutate mail.
    const Config config;
    MainWindow window(config);

    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);
    queryEdit->setText(QStringLiteral("tag:unread"));
    queryEdit->returnPressed();

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<ThreadListView *>();
    QVERIFY(view);
    auto *timer = window.findChild<QTimer *>(QStringLiteral("markReadTimer"));
    QVERIFY(timer);

    model->appendBatch({ makeThread(QStringLiteral("T1"),
                                    { QStringLiteral("unread") }) });
    const QModelIndex thread = model->index(0, 0, QModelIndex());
    QVERIFY(thread.isValid());

    // The single click a real double-click delivers first. Asserted, so this
    // test cannot pass by the timer never having been armed at all.
    selectThreadRow(view, 0);
    QVERIFY2(timer->isActive(),
             "the selection did not arm the timer, so this proves nothing");

    emit view->doubleClicked(thread);

    QVERIFY2(!timer->isActive(),
             "the drill-down left a mark-read armed for a message the user "
             "only passed through");
}

void TestMainWindow::aUserQueryAbandonsAPendingRecovery()
{
    // A recovery spans two round-trips, so the user can type a query in the
    // middle of one. That is them choosing to go somewhere else, and the
    // pending selection must not follow them there: restoring a thread's
    // message into a result the user asked for something else from would yank
    // the view out from under them.
    const Config config;
    MainWindow window(config);

    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);
    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<ThreadListView *>();
    QVERIFY(view);

    // Recovering the thread with NO message pinned, so the pending recovery
    // selects its thread row the moment that thread appears. A recovery
    // waiting on a specific reply would pass this test without the guard,
    // simply by never reaching its target: it would sit expanding a thread
    // whose replies this fixture never delivers.
    QMetaObject::invokeMethod(&window, "recoverStaleThread",
                              Q_ARG(QString, QStringLiteral("T1")),
                              Q_ARG(QString, QString()));

    // The user changes their mind before the recovery's query comes back.
    queryEdit->setText(QStringLiteral("tag:flagged"));
    queryEdit->returnPressed();
    const quint64 generation = window.currentGenerationForTesting();

    // That query happens to contain the same thread, which is what makes this
    // a trap rather than a theoretical case: the recovery would find its
    // target and select it.
    ThreadSummary thread = makeThread(QStringLiteral("T1"), {});
    thread.totalCount = 2;
    const QVector<ThreadSummary> result{
        makeThread(QStringLiteral("T9"), {}), thread
    };
    QMetaObject::invokeMethod(&window, "onThreadsReady",
                              Q_ARG(QVector<ThreadSummary>, result),
                              Q_ARG(quint64, generation));
    QMetaObject::invokeMethod(&window, "onQueryFinished",
                              Q_ARG(int, 2), Q_ARG(quint64, generation));

    QVERIFY2(!view->currentIndex().isValid(),
             "an abandoned recovery selected a row in the query the user ran "
             "instead");
}

void TestMainWindow::blankingThePaneAlsoDropsTheStaleNotice()
{
    // Reported by the user against the first build of item 35b. The notice
    // outlived the message it describes: blanking the pane left the bar sitting
    // above an empty pane, still naming a thread that was no longer shown, with
    // a button offering to recover it.
    //
    // The bar belongs to the rendered message, exactly as the remote-content
    // bar does, and MessageView::clear() already hides that one. This is the
    // same rule applied to the same place.
    const Config config;
    MainWindow window(config);

    auto *pane = window.findChild<MessageView *>();
    QVERIFY(pane);

    pane->setStaleThread(QStringLiteral("T1"),
                         QStringLiteral("m1@example.org"));
    QCOMPARE(pane->staleThreadId(), QStringLiteral("T1"));

    pane->clear();

    QVERIFY2(pane->staleThreadId().isEmpty(),
             "the stale notice survived the pane being blanked, so it names a "
             "message that is no longer displayed");
    QVERIFY2(pane->staleMessageId().isEmpty(),
             "the stale notice kept the message id of a cleared pane");
}

void TestMainWindow::aNewQueryDropsTheStaleNotice()
{
    // The user's actual route to the bug: read a thread out of a view, get the
    // notice, then type a new query. The pane blanks and the bar must go with
    // it. Driven through the window rather than through MessageView::clear()
    // directly, because the defect was that nothing on this path called it.
    const Config config;
    MainWindow window(config);

    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);
    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<ThreadListView *>();
    QVERIFY(view);
    auto *pane = window.findChild<MessageView *>();
    QVERIFY(pane);

    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();

    model->appendBatch({ makeThread(QStringLiteral("T1"),
                                    { QStringLiteral("unread") }) });
    view->setCurrentIndex(model->index(0, 0, QModelIndex()));

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Idle));
    const quint64 refresh = window.currentGenerationForTesting();
    QMetaObject::invokeMethod(&window, "onQueryFinished",
                              Q_ARG(int, 0), Q_ARG(quint64, refresh));
    QCOMPARE(pane->staleThreadId(), QStringLiteral("T1"));

    // The user runs a different query.
    queryEdit->setText(QStringLiteral("tag:unread"));
    queryEdit->returnPressed();

    QVERIFY2(pane->staleThreadId().isEmpty(),
             "after a new query the notice still offered to recover the thread "
             "from the previous view");
}

void TestMainWindow::aFinishedBackgroundSyncStopsSayingItIsRunning()
{
    // Reported by the user against item 35b. "Background sync running..." is
    // written straight to the status label when the lock appears, and the
    // "Background sync completed" message on the way out was the only thing
    // that ever replaced it. Removing that message, so a refresh could be
    // silent, left the bar claiming a sync was running long after it finished.
    //
    // Silent means "says nothing NEW", not "leaves a stale claim standing".
    const Config config;
    MainWindow window(config);

    auto *status = window.findChild<QLabel *>(QStringLiteral("statusMessage"));
    QVERIFY(status);

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Running));
    QVERIFY2(status->text().contains(QStringLiteral("running")),
             qPrintable(QStringLiteral("the fixture never announced a running "
                                       "sync, status says '%1'")
                            .arg(status->text())));

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Idle));

    QVERIFY2(!status->text().contains(QStringLiteral("running")),
             qPrintable(QStringLiteral("the status bar still claims a sync is "
                                       "running after it finished: '%1'")
                            .arg(status->text())));
}

void TestMainWindow::aRefreshDoesNotStampOverASelectionMessage()
{
    // The other half, and the reason this is not simply "always write the
    // thread count". A refresh runs on a cron timer under a user who may be
    // doing something, and the bar carries their selection count while they
    // are. Overwriting that every ten minutes is the noise the silence rule
    // exists to prevent.
    const Config config;
    MainWindow window(config);

    auto *status = window.findChild<QLabel *>(QStringLiteral("statusMessage"));
    QVERIFY(status);
    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);
    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<ThreadListView *>();
    QVERIFY(view);

    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();

    model->appendBatch({ makeThread(QStringLiteral("T1"), {}),
                         makeThread(QStringLiteral("T2"), {}) });
    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    view->selectionModel()->select(model->index(1, 0, QModelIndex()),
                                   QItemSelectionModel::Select);

    const QString before = status->text();
    QVERIFY2(!before.isEmpty(),
             "the fixture left the status bar empty, so this proves nothing");

    // A refresh completes with no sync ever having been announced.
    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Idle));
    const quint64 refresh = window.currentGenerationForTesting();
    const QVector<ThreadSummary> result{
        makeThread(QStringLiteral("T1"), {}), makeThread(QStringLiteral("T2"), {})
    };
    QMetaObject::invokeMethod(&window, "onThreadsReady",
                              Q_ARG(QVector<ThreadSummary>, result),
                              Q_ARG(quint64, refresh));
    QMetaObject::invokeMethod(&window, "onQueryFinished",
                              Q_ARG(int, 2), Q_ARG(quint64, refresh));

    QCOMPARE(status->text(), before);
}

void TestMainWindow::aRefreshDoesNotOpenNewMailByItself()
{
    // Reported by the user against item 35b. A thread read to the end empties
    // an Unread view; the refresh then brings in one new message, and it opens
    // ITSELF in the message pane, marking it read two seconds later without
    // the user ever having looked at it.
    //
    // Nothing in MainWindow selects it: QTreeView sets a current index of its
    // own when rows are inserted into a model that had none, and selecting a
    // row is what loads it. An automatic refresh must not do that, or a cron
    // timer decides what the user is reading.
    const Config config;
    MainWindow window(config);

    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);
    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<ThreadListView *>();
    QVERIFY(view);

    queryEdit->setText(QStringLiteral("tag:unread"));
    queryEdit->returnPressed();

    // The view has been read empty: no rows, nothing current.
    QCOMPARE(model->rowCount(QModelIndex()), 0);
    QVERIFY(!view->currentIndex().isValid());

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Idle));
    const quint64 refresh = window.currentGenerationForTesting();

    const QVector<ThreadSummary> result{
        makeThread(QStringLiteral("NEW"), { QStringLiteral("unread") })
    };
    QMetaObject::invokeMethod(&window, "onThreadsReady",
                              Q_ARG(QVector<ThreadSummary>, result),
                              Q_ARG(quint64, refresh));
    QMetaObject::invokeMethod(&window, "onQueryFinished",
                              Q_ARG(int, 1), Q_ARG(quint64, refresh));

    QCOMPARE(model->rowCount(QModelIndex()), 1);

    // Insertion alone does not do it, which is why the bug only showed up when
    // the user came back from another desktop: QTreeView gives itself a current
    // index when it takes FOCUS with none set. Reproduced here rather than
    // asserted from the report, since a test that only inserts rows passes
    // against the defect.
    view->setFocus();
    QApplication::sendEvent(view, new QFocusEvent(QEvent::FocusIn));

    QVERIFY2(window.currentThreadId().isEmpty(),
             "the refresh opened the new mail in the message pane, which marks "
             "it read without the user having looked at it");
}

void TestMainWindow::openingAnotherMessageDropsTheStaleNoticeOfThePreviousOne()
{
    // The other half of the user's report: the pane showed the new message
    // while the notice above it still named the thread they had been reading.
    //
    // The notice itself was right at the moment it was raised. What made it a
    // lie was the pane being replaced underneath it, so this pins the rule that
    // the notice belongs to whatever is currently rendered: selecting anything
    // else retires it, exactly as blanking the pane does.
    const Config config;
    MainWindow window(config);

    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);
    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<ThreadListView *>();
    QVERIFY(view);
    auto *pane = window.findChild<MessageView *>();
    QVERIFY(pane);

    queryEdit->setText(QStringLiteral("tag:unread"));
    queryEdit->returnPressed();

    model->appendBatch({ makeThread(QStringLiteral("OLD"),
                                    { QStringLiteral("unread") }) });
    view->setCurrentIndex(model->index(0, 0, QModelIndex()));

    // The refresh drops the thread being read and brings in new mail.
    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Idle));
    const quint64 refresh = window.currentGenerationForTesting();
    const QVector<ThreadSummary> result{
        makeThread(QStringLiteral("NEW"), { QStringLiteral("unread") })
    };
    QMetaObject::invokeMethod(&window, "onThreadsReady",
                              Q_ARG(QVector<ThreadSummary>, result),
                              Q_ARG(quint64, refresh));
    QMetaObject::invokeMethod(&window, "onQueryFinished",
                              Q_ARG(int, 1), Q_ARG(quint64, refresh));

    QCOMPARE(pane->staleThreadId(), QStringLiteral("OLD"));

    // The user chooses to open the new mail themselves.
    const QModelIndex fresh = model->index(0, 0, QModelIndex());
    QVERIFY(fresh.isValid());
    QCOMPARE(model->threadAt(0).threadId, QStringLiteral("NEW"));
    view->setCurrentIndex(fresh);
    view->selectionModel()->select(fresh, QItemSelectionModel::Select);

    QVERIFY2(pane->staleThreadId().isEmpty(),
             "the notice still named the previous thread while the pane showed "
             "a different message");
}

void TestMainWindow::theStaleNoticeKeepsTheMessageOfAThreadRootToo()
{
    // Reported by the user: recovering a thread they were reading brought the
    // thread back collapsed, with the pane blank, instead of reopening the
    // message they had been on.
    //
    // The cause is that a thread ROOT sets both ids. The root card IS the
    // thread's first message and the pane renders exactly that message, so
    // m_currentThreadId and m_currentMessageId are both filled; the notice read
    // the message id only when the thread id was EMPTY, so the root case threw
    // away a message id it had. Recovery then had nothing to restore, landed on
    // the thread row and never expanded it.
    const Config config;
    MainWindow window(config);

    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);
    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<ThreadListView *>();
    QVERIFY(view);
    auto *pane = window.findChild<MessageView *>();
    QVERIFY(pane);

    queryEdit->setText(QStringLiteral("tag:unread"));
    queryEdit->returnPressed();

    ThreadSummary thread = makeThread(QStringLiteral("T1"),
                                      { QStringLiteral("unread") });
    thread.totalCount = 4;
    model->appendBatch({ thread });

    // The root knows its own message once the tree is loaded, which is what
    // makes the pane show one message rather than the conversation.
    MessageNode root;
    root.messageId = QStringLiteral("m0@example.org");
    root.threadId = QStringLiteral("T1");
    root.depth = 0;
    MessageNode reply;
    reply.messageId = QStringLiteral("m1@example.org");
    reply.threadId = QStringLiteral("T1");
    reply.depth = 1;
    model->setThreadMessages(QStringLiteral("T1"), { root, reply });

    const QModelIndex threadIndex = model->index(0, 0, QModelIndex());
    view->setCurrentIndex(threadIndex);
    view->selectionModel()->select(threadIndex, QItemSelectionModel::Select);
    QCOMPARE(window.currentThreadId(), QStringLiteral("T1"));

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Idle));
    const quint64 refresh = window.currentGenerationForTesting();
    QMetaObject::invokeMethod(&window, "onQueryFinished",
                              Q_ARG(int, 0), Q_ARG(quint64, refresh));

    QCOMPARE(pane->staleThreadId(), QStringLiteral("T1"));
    QVERIFY2(!pane->staleMessageId().isEmpty(),
             "the notice dropped the message of a thread root, so recovery has "
             "nothing to reopen and lands on a collapsed thread");
    QCOMPARE(pane->staleMessageId(), QStringLiteral("m0@example.org"));
}

void TestMainWindow::recoveryExpandsTheThreadAndSelectsRatherThanOnlyPointing()
{
    // The rest of the same report: recovery brought the thread back COLLAPSED
    // with the pane BLANK. Two separate faults behind one symptom.
    //
    // setCurrentIndex() alone sets a current row without selecting it, and
    // since the fix for the auto-open defect onThreadSelected() ignores exactly
    // that: an unselected current index is Qt's housekeeping, not the user. So
    // recovery pointed at the row and nothing rendered.
    //
    // And nothing expanded the thread, so the reply the user had been reading
    // was not on screen even when it was the target.
    const Config config;
    MainWindow window(config);

    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);
    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<ThreadListView *>();
    QVERIFY(view);

    queryEdit->setText(QStringLiteral("tag:unread"));
    queryEdit->returnPressed();

    // Recovering onto the thread's FIRST message, which is the root card: the
    // case the user hit by opening a thread rather than a reply.
    QMetaObject::invokeMethod(&window, "recoverStaleThread",
                              Q_ARG(QString, QStringLiteral("T1")),
                              Q_ARG(QString, QStringLiteral("m0@example.org")));
    const quint64 generation = window.currentGenerationForTesting();

    ThreadSummary thread = makeThread(QStringLiteral("T1"), {});
    thread.totalCount = 4;
    const QVector<ThreadSummary> result{ thread };
    QMetaObject::invokeMethod(&window, "onThreadsReady",
                              Q_ARG(QVector<ThreadSummary>, result),
                              Q_ARG(quint64, generation));
    QMetaObject::invokeMethod(&window, "onQueryFinished",
                              Q_ARG(int, 1), Q_ARG(quint64, generation));

    // No tree reply. This is the user's actual case and the one the earlier
    // test missed: the thread comes back from the query with its replies NOT
    // yet loaded, which is the normal state of a freshly queried row. Recovery
    // has to ask for them rather than assuming they are already there.
    const QModelIndex threadIndex = model->index(0, 0, QModelIndex());
    QVERIFY(threadIndex.isValid());
    QCOMPARE(model->rowCount(threadIndex), 0);

    QVERIFY2(view->selectionModel()->hasSelection(),
             "recovery pointed at the row without selecting it, so nothing "
             "renders and the pane stays blank");
    QCOMPARE(window.currentThreadId(), QStringLiteral("T1"));
    QVERIFY2(view->isExpanded(threadIndex),
             "recovery brought the thread back collapsed, so the conversation "
             "the user was reading is not on screen");
}

void TestMainWindow::recoveryFromAnExpandedThreadRestoresTheReply()
{
    // The user's case, staged exactly: a thread ALREADY EXPANDED with the
    // fourth reply selected and rendered, dropped by a refresh, then recovered.
    // Reported twice as still broken while the earlier recovery tests passed,
    // which means those tests were not reproducing it.
    //
    // What they missed is the whole round trip. Recovery re-runs thread:<id>,
    // and that query REPLACES the model contents, so the recovered thread
    // arrives collapsed with no replies loaded whatever state the old row was
    // in. The reply the user wants therefore does not exist as a row at the
    // moment recovery first runs, and the only thing that can create it is the
    // tree reply arriving after an expand.
    const Config config;
    MainWindow window(config);

    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);
    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<ThreadListView *>();
    QVERIFY(view);

    queryEdit->setText(QStringLiteral("tag:unread"));
    queryEdit->returnPressed();

    // Reading reply 4 of a 4-message thread, expanded.
    ThreadSummary thread = makeThread(QStringLiteral("T1"),
                                      { QStringLiteral("unread") });
    thread.totalCount = 4;
    model->appendBatch({ thread });

    QVector<MessageNode> nodes;
    for (int i = 0; i < 4; ++i) {
        MessageNode n;
        n.messageId = QStringLiteral("m%1@example.org").arg(i);
        n.threadId = QStringLiteral("T1");
        n.depth = i == 0 ? 0 : 1;
        nodes.append(n);
    }
    model->setThreadMessages(QStringLiteral("T1"), nodes);

    const QModelIndex threadIndex = model->index(0, 0, QModelIndex());
    view->expand(threadIndex);
    QCOMPARE(model->rowCount(threadIndex), 3);

    const QModelIndex fourth = model->index(2, 0, threadIndex);
    QVERIFY(fourth.isValid());
    QCOMPARE(model->messageAt(fourth).messageId,
             QStringLiteral("m3@example.org"));
    view->setCurrentIndex(fourth);
    view->selectionModel()->select(fourth, QItemSelectionModel::Select);

    // The refresh drops it, and the user follows the notice.
    QMetaObject::invokeMethod(&window, "recoverStaleThread",
                              Q_ARG(QString, QStringLiteral("T1")),
                              Q_ARG(QString, QStringLiteral("m3@example.org")));
    const quint64 generation = window.currentGenerationForTesting();

    // The recovery query comes back: ONE collapsed thread, no replies. This is
    // what the query really returns, and the state the earlier tests skipped.
    const QVector<ThreadSummary> recovered{ thread };
    QMetaObject::invokeMethod(&window, "onThreadsReady",
                              Q_ARG(QVector<ThreadSummary>, recovered),
                              Q_ARG(quint64, generation));
    QMetaObject::invokeMethod(&window, "onQueryFinished",
                              Q_ARG(int, 1), Q_ARG(quint64, generation));

    const QModelIndex back = model->index(0, 0, QModelIndex());
    QVERIFY(back.isValid());
    QVERIFY2(view->isExpanded(back),
             "the recovered thread came back collapsed");

    // Expanding asks the worker for the tree; that reply is what creates the
    // reply rows. Without it the conversation is not on screen at all.
    QMetaObject::invokeMethod(&window, "onThreadTreeLoaded",
                              Q_ARG(QVector<MessageNode>, nodes),
                              Q_ARG(quint64, generation));

    QVERIFY2(view->isExpanded(back),
             "the thread collapsed again once its replies arrived");
    QCOMPARE(model->rowCount(back), 3);

    const QModelIndex current = view->currentIndex();
    QVERIFY2(current.isValid(), "recovery left nothing selected");
    QVERIFY2(model->isMessageRow(current),
             "recovery landed on the thread rather than on the reply the user "
             "was reading");
    QCOMPARE(model->messageAt(current).messageId,
             QStringLiteral("m3@example.org"));
}

void TestMainWindow::theRecoveryButtonSurvivesThePaneBeingBlanked()
{
    // The defect that survived six wrong diagnoses and every other recovery
    // test in this file, because all of them reach the slot through
    // invokeMethod, which COPIES its arguments.
    //
    // MessageView emitted the signal with its own members, so a direct
    // connection handed MainWindow::recoverStaleThread() references to them.
    // That slot calls runCurrentQuery(), which blanks the pane, which calls
    // setStaleThread() and assigns to those very members. The ids the slot was
    // still holding went empty mid-call, the recovery target was stored as an
    // empty string, and nothing was ever recovered: the thread came back
    // collapsed with the pane blank.
    //
    // Driven through the real button so the real signal runs. A test that
    // calls the slot directly cannot see this and will pass against it.
    const Config config;
    MainWindow window(config);

    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);
    auto *pane = window.findChild<MessageView *>();
    QVERIFY(pane);
    auto *button =
        pane->findChild<QPushButton *>(QStringLiteral("staleThreadButton"));
    QVERIFY(button);

    queryEdit->setText(QStringLiteral("tag:unread"));
    queryEdit->returnPressed();

    pane->setStaleThread(QStringLiteral("T1"),
                         QStringLiteral("m3@example.org"));
    QCOMPARE(pane->staleThreadId(), QStringLiteral("T1"));

    button->click();

    // The query the button ran is the thread's own, which is only true if the
    // id survived the round trip.
    QCOMPARE(queryEdit->text(), QStringLiteral("thread:T1"));

    // And the target is still pending, waiting for the result. Empty here means
    // the reference was clobbered and the recovery is already dead.
    QVERIFY2(window.hasPendingRecoveryForTesting(),
             "the recovery target was lost during the slot, so the thread will "
             "come back collapsed with a blank pane");
}

void TestMainWindow::theSyncActionIsDisabledWhileABackgroundSyncHoldsTheLock()
{
    // Item 29 shipped for the QPushButton only: onExternalSyncStateChanged
    // disabled m_syncButton and never touched the QAction, so the toolbar and
    // menu Sync stayed clickable during a cron sync and could only produce the
    // EX_TEMPFAIL skip. The button-based test passed throughout, because it
    // drove the half that worked.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    QVERIFY(QDir().mkpath(dir.filePath(QStringLiteral("qtmaildir"))));
    const QString conf = dir.filePath(QStringLiteral("qtmaildir/qtmaildir.conf"));
    {
        QSettings s(conf, QSettings::IniFormat);
        s.setValue(QStringLiteral("sync/command"), QStringLiteral("/bin/true"));
    }

    const QString locks = dir.filePath(QStringLiteral("locks"));
    {
        QFile f(locks);
        QVERIFY(f.open(QIODevice::WriteOnly));
    }
    MainWindow::setLocksPathForTesting(locks);

    Config config;
    config.load(conf);
    MainWindow window(config);

    auto *action = window.findChild<QAction *>(QStringLiteral("sync"));
    QVERIFY2(action, "no sync action to check");
    QVERIFY2(action->isEnabled(), "the action starts disabled with a command set");

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Running));
    QVERIFY2(!action->isEnabled(),
             "the sync action stayed enabled during a background sync");

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Idle));
    QVERIFY2(action->isEnabled(),
             "the sync action was not re-enabled after the background sync");

    // No restore to "/proc/locks": init() points every test at its own
    // table, and handing the real one back would re-expose the next test.
}

// Item 71. A confirmed tag edit arms a debounce that syncs it out, so an edit
// no longer waits for a manual sync or the user's cron job.
//
// All four of these assert on the TIMER rather than on a sync actually running.
// Starting a real one from a test would launch the configured command, and the
// thing worth guarding here is the decision to sync, not QProcess.
static QString writeSyncConfig(QTemporaryDir &dir, const QString &extra = {})
{
    QDir().mkpath(dir.filePath(QStringLiteral("qtmaildir")));
    const QString conf = dir.filePath(QStringLiteral("qtmaildir/qtmaildir.conf"));
    QSettings s(conf, QSettings::IniFormat);
    // /bin/true exists, so Config keeps it and MailSync reports available. A
    // config with no command disables the automatic sync by design, which would
    // make every one of these tests pass against a stub.
    s.setValue(QStringLiteral("sync/command"), QStringLiteral("/bin/true"));
    // NOT "general/auto_sync_delay_ms": QSettings' INI backend treats a section
    // literally named [general] as its own fallback section and strips it, so a
    // prefixed lookup silently matches nothing. Writing it prefixed here left
    // the default in place and the -1 case read 2000.
    if (!extra.isEmpty())
        s.setValue(QStringLiteral("auto_sync_delay_ms"), extra);
    s.sync();
    return conf;
}

void TestMainWindow::aConfirmedEditArmsTheAutoSync()
{
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    config.load(writeSyncConfig(dir));
    QCOMPARE(config.autoSyncDelayMs(), 2000);

    MainWindow window(config);
    auto *timer = window.findChild<QTimer *>(QStringLiteral("autoSyncTimer"));
    QVERIFY2(timer, "no autoSyncTimer to observe");
    QVERIFY2(!timer->isActive(), "the debounce is armed before any edit");

    TagChange change;
    change.messageIds = { QStringLiteral("m1") };
    change.added = { QStringLiteral("flagged") };
    QVERIFY(QMetaObject::invokeMethod(&window, "onTagsApplied",
                                      Q_ARG(TagChange, change)));

    QVERIFY2(timer->isActive(), "a confirmed edit did not arm the automatic sync");
    QCOMPARE(timer->interval(), 2000);
}

void TestMainWindow::autoSyncDebouncesABurstOfEdits()
{
    // The point of the debounce. "Mark all read" confirms one write per thread,
    // and one sync per thread is what this prevents. A timer that STACKED would
    // still be active here, so the assertion is on the count of timers and on
    // the remaining interval having been reset, not merely on isActive().
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    config.load(writeSyncConfig(dir));

    MainWindow window(config);
    auto *timer = window.findChild<QTimer *>(QStringLiteral("autoSyncTimer"));
    QVERIFY(timer);

    TagChange first;
    first.messageIds = { QStringLiteral("m1") };
    first.added = { QStringLiteral("flagged") };
    QVERIFY(QMetaObject::invokeMethod(&window, "onTagsApplied",
                                      Q_ARG(TagChange, first)));
    QVERIFY(timer->isActive());

    // Long enough that a restart is unambiguous. Comparing remainingTime()
    // before and after with ">" was tried and is FLAKY: the two reads can land
    // in the same millisecond bucket, and the test then fails against correct
    // code. Assert instead that the remaining time went back up near the full
    // interval, which a stacked or un-restarted timer cannot produce.
    QTest::qWait(500);
    const int afterWait = timer->remainingTime();
    QVERIFY2(afterWait < 1800, "the timer did not start counting down");

    TagChange second;
    second.messageIds = { QStringLiteral("m2") };
    second.added = { QStringLiteral("flagged") };
    QVERIFY(QMetaObject::invokeMethod(&window, "onTagsApplied",
                                      Q_ARG(TagChange, second)));

    QVERIFY2(timer->remainingTime() > 1800,
             "the second edit did not restart the debounce, so a burst of edits "
             "syncs on the schedule of the FIRST one");
    QCOMPARE(window.findChildren<QTimer *>(QStringLiteral("autoSyncTimer")).size(),
             1);
}

void TestMainWindow::aSearchFromThePaneReplacesTheQuery()
{
    const Config config;
    MainWindow window(config);

    QLineEdit *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY2(queryEdit, "no query bar: the window was never built");

    MessageView *view = window.findChild<MessageView *>();
    QVERIFY2(view, "no message view");

    queryEdit->setText(QStringLiteral("tag:inbox"));
    emit view->searchRequested(QStringLiteral("from:\"foo@example.org\""),
                               SearchTerm::SearchMode::Replace);

    QCOMPARE(queryEdit->text(), QStringLiteral("from:\"foo@example.org\""));
}

void TestMainWindow::aSearchFromThePaneCanNarrowTheQuery()
{
    // The case the feature exists for: a query returning a thousand threads is
    // narrowed by adding a condition. BOTH sides are parenthesised, because
    // 'a or b AND c' binds as 'a or (b AND c)', which WIDENS a search the user
    // asked to narrow, and notmuch reports no error for it.
    const Config config;
    MainWindow window(config);

    QLineEdit *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY2(queryEdit, "no query bar: the window was never built");

    MessageView *view = window.findChild<MessageView *>();
    QVERIFY2(view, "no message view");

    queryEdit->setText(QStringLiteral("tag:inbox or tag:flagged"));
    emit view->searchRequested(QStringLiteral("from:\"foo@example.org\""),
                               SearchTerm::SearchMode::Narrow);

    QCOMPARE(queryEdit->text(),
             QStringLiteral("(tag:inbox or tag:flagged) AND (from:\"foo@example.org\")"));
}

void TestMainWindow::narrowingAnEmptyQueryBarIsAPlainSearch()
{
    // Rather than "() AND (x)", which matches nothing.
    const Config config;
    MainWindow window(config);

    QLineEdit *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY2(queryEdit, "no query bar: the window was never built");

    MessageView *view = window.findChild<MessageView *>();
    QVERIFY2(view, "no message view");

    queryEdit->clear();
    emit view->searchRequested(QStringLiteral("tag:inbox"),
                               SearchTerm::SearchMode::Narrow);

    QCOMPARE(queryEdit->text(), QStringLiteral("tag:inbox"));
}

void TestMainWindow::autoSyncIsNotArmedWhenDisabledOrWithNothingPending()
{
    // A negative delay is the switch that restores the pre-0.16.0 behaviour, so
    // it must arm nothing at all.
    QTemporaryDir off;
    QVERIFY(off.isValid());
    Config disabled;
    disabled.load(writeSyncConfig(off, QStringLiteral("-1")));
    QCOMPARE(disabled.autoSyncDelayMs(), -1);

    MainWindow disabledWindow(disabled);
    auto *disabledTimer =
        disabledWindow.findChild<QTimer *>(QStringLiteral("autoSyncTimer"));
    QVERIFY(disabledTimer);

    TagChange change;
    change.messageIds = { QStringLiteral("m1") };
    change.added = { QStringLiteral("flagged") };
    QVERIFY(QMetaObject::invokeMethod(&disabledWindow, "onTagsApplied",
                                      Q_ARG(TagChange, change)));
    QVERIFY2(!disabledTimer->isActive(),
             "auto_sync_delay_ms = -1 still armed a sync");

    // An edit netted against its own inverse leaves nothing outstanding (item
    // 28), and syncing for it would run mbsync over an unchanged mail store.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    config.load(writeSyncConfig(dir));

    MainWindow window(config);
    auto *timer = window.findChild<QTimer *>(QStringLiteral("autoSyncTimer"));
    QVERIFY(timer);

    TagChange added;
    added.messageIds = { QStringLiteral("m1") };
    added.added = { QStringLiteral("unread") };
    QVERIFY(QMetaObject::invokeMethod(&window, "onTagsApplied",
                                      Q_ARG(TagChange, added)));
    QVERIFY2(timer->isActive(), "the first edit did not arm anything, so the "
                                "netting assertion below proves nothing");

    timer->stop();
    TagChange undone;
    undone.messageIds = { QStringLiteral("m1") };
    undone.removed = { QStringLiteral("unread") };
    QVERIFY(QMetaObject::invokeMethod(&window, "onTagsApplied",
                                      Q_ARG(TagChange, undone)));
    QVERIFY2(!timer->isActive(),
             "an edit and its inverse left nothing pending but still armed a sync");
}

void TestMainWindow::autoSyncSkipsWhileABackgroundSyncIsRunning()
{
    // Item 71 requires skipping rather than queueing: the cron job holds the
    // same lock and mbsync's answer to a second run is to fail on it. The edits
    // are not lost, they stay pending.
    //
    // The locks path is redirected so this does not depend on whether a real
    // sync is running on the machine, which is item 61's failure mode.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    const QString locks = dir.filePath(QStringLiteral("locks"));
    {
        QFile f(locks);
        QVERIFY(f.open(QIODevice::WriteOnly));
    }
    MainWindow::setLocksPathForTesting(locks);

    Config config;
    config.load(writeSyncConfig(dir));

    MainWindow window(config);
    auto *timer = window.findChild<QTimer *>(QStringLiteral("autoSyncTimer"));
    QVERIFY(timer);

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Running));

    TagChange change;
    change.messageIds = { QStringLiteral("m1") };
    change.added = { QStringLiteral("flagged") };
    QVERIFY(QMetaObject::invokeMethod(&window, "onTagsApplied",
                                      Q_ARG(TagChange, change)));

    // Armed, because the edit is real and will still need carrying. The skip
    // belongs to the moment the timer FIRES, not to arming it.
    QVERIFY2(timer->isActive(), "the edit did not arm the debounce at all");

    auto *label = window.findChild<QLabel *>(QStringLiteral("pendingEdits"));
    QVERIFY(label);
    QVERIFY(!label->isHidden());

    QVERIFY(QMetaObject::invokeMethod(&window, "runAutoSync"));

    // The edit is still pending: a skipped sync must not clear the indicator,
    // which is the fault that would leave the user quitting on unsynced work.
    QVERIFY2(!label->isHidden(),
             "a skipped automatic sync cleared the pending indicator");

    // No restore to "/proc/locks": init() points every test at its own
    // table, and handing the real one back would re-expose the next test.
}

void TestMainWindow::aSkippedAutoSyncRearmsRatherThanGivingUp()
{
    // Item 89, the concrete half. Skipping is correct and must stay, but the
    // skip used to be the END of the attempt: the timer had fired, nothing
    // re-armed it, and the edit waited for a manual sync or the next cron run.
    //
    // The comment defending it said the running sync was "very likely" to carry
    // the edit, since it reached the mail store at edit time. Very likely is not
    // always: an edit made after mbsync has already passed that account's
    // mailbox is not carried by it, and the pending count then sits non-zero
    // with nothing scheduled to clear it.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    const QString locks = dir.filePath(QStringLiteral("locks"));
    {
        QFile f(locks);
        QVERIFY(f.open(QIODevice::WriteOnly));
    }
    MainWindow::setLocksPathForTesting(locks);

    Config config;
    config.load(writeSyncConfig(dir));

    MainWindow window(config);
    auto *timer = window.findChild<QTimer *>(QStringLiteral("autoSyncTimer"));
    QVERIFY(timer);

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Running));

    TagChange change;
    change.messageIds = { QStringLiteral("m1") };
    change.added = { QStringLiteral("flagged") };
    QVERIFY(QMetaObject::invokeMethod(&window, "onTagsApplied",
                                      Q_ARG(TagChange, change)));
    QVERIFY2(timer->isActive(), "the edit did not arm the debounce at all");

    // Fire it by hand rather than waiting out the delay. A QTimer that has
    // fired is no longer active, so this is also what makes the assertion
    // below mean something: without the re-arm it is inactive here.
    timer->stop();
    QVERIFY(QMetaObject::invokeMethod(&window, "runAutoSync"));

    QVERIFY2(timer->isActive(),
             "a skipped automatic sync left nothing armed to carry the edit");

    // Re-armed at the configured debounce, not at some shorter interval that
    // would spin against a long external sync. SyncMonitor polls /proc/locks,
    // so an m_externalSyncBusy that never clears re-arms at this interval
    // indefinitely, which is cheap only because the interval is the user's own.
    QCOMPARE(timer->interval(), config.autoSyncDelayMs());

    // The edit is still pending throughout: a retry must not look like a
    // completed sync to the indicator.
    auto *label = window.findChild<QLabel *>(QStringLiteral("pendingEdits"));
    QVERIFY(label);
    QVERIFY2(!label->isHidden(),
             "the re-armed sync cleared the pending indicator");
}

void TestMainWindow::aHeldEditIsSentBeforeTheSyncEndRefreshReadsTheDatabase()
{
    // Reported by hand, 2026-08-15: read a message while a cron sync is
    // running, watch the unread tag go and the status bar say the change will
    // be applied when the sync finishes, and when it does the message is unread
    // again.
    //
    // The edit is held during a sync because the worker's read-write open
    // BLOCKS on notmuch's exclusive lock. At sync end the handler refreshed the
    // list FIRST and flushed the held edits afterwards, so the refresh read a
    // database that still carried `unread`, reconciled it into the model, and
    // overwrote the optimistic update. The flush then wrote the tag correctly.
    // The database ended up right and the list ended up wrong, with nothing
    // scheduled to re-read it, which is why it looked like the edit was lost.
    //
    // Asserted on the ORDER, not on the tag: this window has no worker to
    // answer either the refresh or the write, so the rows cannot show the
    // outcome. What decides the bug is whether the edit had been sent by the
    // time the refresh was issued.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    const QString locks = dir.filePath(QStringLiteral("locks"));
    {
        QFile f(locks);
        QVERIFY(f.open(QIODevice::WriteOnly));
        // A held lock, so the edit below is held rather than sent.
        f.write("1: FLOCK  ADVISORY  WRITE 1 00:00:0 0\n");
    }
    MainWindow::setLocksPathForTesting(locks);

    Config config;
    config.load(writeSyncConfig(dir));
    MainWindow window(config);

    auto *queryEdit = window.findChild<QLineEdit *>();
    QVERIFY(queryEdit);
    queryEdit->setText(QStringLiteral("tag:unread"));
    queryEdit->returnPressed();

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<ThreadListView *>();
    QVERIFY(view);
    model->appendBatch({ makeThread(QStringLiteral("T1"),
                                    { QStringLiteral("unread") }) });
    selectThreadRow(view, 0);

    // The sync is observed as running, which is what makes the edit held.
    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Running));

    auto *toggleUnread =
        window.findChild<QAction *>(QStringLiteral("toggle_unread"));
    QVERIFY(toggleUnread);
    toggleUnread->trigger();

    QVERIFY2(window.hasEditAwaitingSend(),
             "the edit was not held, so this test proves nothing about the "
             "order the sync-end handler does things in");

    const quint64 before = window.currentGenerationForTesting();

    // The sync ends. Both the refresh and the flush happen in this one call.
    {
        QFile f(locks);
        QVERIFY(f.open(QIODevice::WriteOnly | QIODevice::Truncate));
    }
    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Idle));

    QVERIFY2(!window.hasEditAwaitingSend(),
             "the sync ended without ever flushing the held edit");

    // The write went out. Nothing else in this handler sends one, so its
    // presence is what proves the flush ran, and the refresh below is what it
    // has to have run BEFORE.
    //
    // Either scope counts. The gesture is Toggle unread on a thread row, which
    // is message-scoped since item 108, so the ids land in the message list;
    // the ORDER this test exists for is the same either way, and pinning the
    // scope here would make it fail for a reason it does not care about.
    QVERIFY2(!window.pendingThreadIdsForTesting().isEmpty()
                 || !window.pendingMessageIdsForTesting().isEmpty(),
             "the held edit was dropped rather than sent");

    const quint64 after = window.currentGenerationForTesting();
    QVERIFY2(after > before, "the sync end did not refresh the list at all");

    // THE ASSERTION THIS TEST EXISTS FOR. Both orders leave identical end
    // state, so everything above passes against the defect; only the
    // generation stamped at flush time separates them.
    //
    // Flushing first means the stamp is the generation from BEFORE the refresh
    // bumped it. Refreshing first means the flush sees the bumped one, and the
    // refresh has already read a database that still carries the old tag.
    QCOMPARE(window.flushGenerationForTesting(), before);
}

void TestMainWindow::aSuccessfulSyncRefreshesRatherThanRerunningTheQuery()
{
    // Reported by hand against item 71: reading a message in the Unread view,
    // the automatic mark-read tags it, the automatic sync fires two seconds
    // later, and the message pane went blank because the thread had stopped
    // matching "tag:unread".
    //
    // The cause was not the stale-thread notice, which handles exactly this and
    // has since item 35. It was that onSyncFinished() called runCurrentQuery()
    // where the cron path calls refreshCurrentQuery(): a re-run clears the
    // model, the undo stack and the pane, so there was nothing left for the
    // notice to describe. Before item 71 a local sync only ever followed a
    // click on Sync, which is why the difference went unnoticed.
    //
    // Asserted on the UNDO STACK rather than on the pane. Both paths issue a
    // queued query this test has no worker to answer, so the pane ends up blank
    // either way and an assertion on it would pass against both. The undo stack
    // is cleared by runCurrentQuery() and deliberately kept by
    // refreshCurrentQuery(), so it names which path actually ran.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);

    window.findChild<QLineEdit *>()->setText(QStringLiteral("tag:unread"));
    QMetaObject::invokeMethod(&window, "runCurrentQuery");
    model->appendBatch({ makeThread(QStringLiteral("t1"),
                                    { QStringLiteral("unread") }) });
    QMetaObject::invokeMethod(&window, "onQueryFinished", Q_ARG(int, 1),
                              Q_ARG(quint64,
                                    window.currentGenerationForTesting()));

    // A real edit, so there is something on the undo stack to lose.
    selectThreadRow(view, 0);
    auto *markRead = window.findChild<QAction *>(QStringLiteral("mark_all_read"));
    QVERIFY(markRead);
    markRead->trigger();
    QCOMPARE(window.undoDepthForTesting(), 1);

    QMetaObject::invokeMethod(&window, "onSyncFinished",
                              Q_ARG(bool, true), Q_ARG(int, 0));

    QVERIFY2(window.undoDepthForTesting() == 1,
             "a successful sync cleared the undo stack, so it re-ran the query "
             "instead of refreshing it, and a message open in the pane is read "
             "out from under the user");
}

void TestMainWindow::theStatusBarFollowsTheSyncPhase()
{
    // Item 42: "Syncing..." said nothing about what was happening, while the
    // script was already streaming its phase into the log pane and the app was
    // throwing it away.
    //
    // Driven through a real script rather than by calling the tracker directly,
    // because the defect this guards is in the wiring: the chunks QProcess
    // hands over split mid-line, so a handler that fed them straight to the
    // tracker would stall on the first partial line.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    QVERIFY(QDir().mkpath(dir.filePath(QStringLiteral("qtmaildir"))));

    const QString script = dir.filePath(QStringLiteral("fakesync.sh"));
    {
        QFile f(script);
        QVERIFY(f.open(QIODevice::WriteOnly));
        // Shaped like the real thing: timestamped lines, the noise that makes
        // up the bulk of a run, mbsync's one summary, then notmuch's output.
        // Paced, not dumped. A script that prints everything at once is
        // delivered in a single readyRead, so the tracker sees the whole run in
        // one call and only its final phase is ever painted: the intermediate
        // ones would be unobservable and the test would assert nothing. A real
        // sync takes tens of seconds and arrives in separate chunks, which the
        // sleeps stand in for.
        f.write("#!/bin/sh\n"
                "echo '===== RUN START: 2026-08-07T11:00:00+02:00 ====='\n"
                "echo '11:00:01 Note: Ignoring non-mail file: /home/you/Mail/x/.uidvalidity'\n"
                "sleep 0.2\n"
                "echo '11:00:02 Channels: 5    Boxes: 39    Far: +0 *1 #0 -0    Near: +1 *0 #0 -0'\n"
                "sleep 0.2\n"
                "echo '11:00:03 Processed 79 total files in almost no time.'\n"
                "echo '11:00:03 Added 1 new message to the database.'\n"
                "sleep 0.2\n"
                "echo '===== RUN END: 2026-08-07T11:00:03+02:00  status=OK ====='\n");
        f.close();
        QVERIFY(QFile::setPermissions(script,
                                      QFile::ReadOwner | QFile::WriteOwner
                                          | QFile::ExeOwner));
    }

    const QString conf = dir.filePath(QStringLiteral("qtmaildir/qtmaildir.conf"));
    {
        QSettings s(conf, QSettings::IniFormat);
        s.setValue(QStringLiteral("sync/command"), script);
    }

    const QString locks = dir.filePath(QStringLiteral("locks"));
    {
        QFile f(locks);
        QVERIFY(f.open(QIODevice::WriteOnly));
    }
    MainWindow::setLocksPathForTesting(locks);

    Config config;
    config.load(conf);
    QCOMPARE(config.syncCommand(), script);
    MainWindow window(config);

    auto *status = window.findChild<QLabel *>(QStringLiteral("statusMessage"));
    QVERIFY(status);

    // Every value the label takes, recorded as it changes. A run this small
    // finishes in well under a second, so polling for an intermediate phase
    // races the process and usually sees only "Sync complete": the sequence has
    // to be captured, not sampled.
    // QLabel has no textChanged signal, so the label is sampled on a fast timer
    // rather than watched. Each distinct value is recorded once.
    QStringList seen;
    QTimer sampler;
    sampler.setInterval(1);
    connect(&sampler, &QTimer::timeout, &sampler, [&seen, status]() {
        const QString text = status->text();
        if (seen.isEmpty() || seen.constLast() != text)
            seen.append(text);
    });
    sampler.start();

    QVERIFY(QMetaObject::invokeMethod(&window, "startSync"));

    QTRY_VERIFY_WITH_TIMEOUT(
        std::any_of(seen.cbegin(), seen.cend(), [](const QString &s) {
            return s.contains(QStringLiteral("Sync complete"));
        }),
        10000);

    const QString trace = seen.join(QStringLiteral(" | "));

    // mbsync's summary is the only concrete thing the stream carries, since it
    // names no channel unless run verbose. The counts must reach the label.
    QVERIFY2(std::any_of(seen.cbegin(), seen.cend(), [](const QString &s) {
                 return s.contains(QStringLiteral("39"))
                     && s.contains(QStringLiteral("5"));
             }),
             qPrintable(QStringLiteral("the mbsync summary never reached the "
                                       "status bar. Saw: ") + trace));

    // Then the reindex phase, which is a different message entirely. Without
    // the wiring the label went from "Syncing..." straight to "Sync complete",
    // which is exactly what the defect looked like.
    QVERIFY2(std::any_of(seen.cbegin(), seen.cend(), [](const QString &s) {
                 return s.contains(QStringLiteral("notmuch"));
             }),
             qPrintable(QStringLiteral("the notmuch phase never reached the "
                                       "status bar. Saw: ") + trace));

    // The banners are not a phase and must never appear in the status bar.
    for (const QString &s : seen) {
        QVERIFY2(!s.contains(QStringLiteral("RUN ")), qPrintable(s));
        QVERIFY2(!s.contains(QStringLiteral("status=")), qPrintable(s));
    }

    // No restore to "/proc/locks": init() points every test at its own
    // table, and handing the real one back would re-expose the next test.
}

void TestMainWindow::anUnobservableLockTableLeavesTheSyncButtonUsable()
{
    // Unknown means /proc/locks could not be read, so nothing was observed. A
    // button left permanently disabled on a platform that cannot see the lock
    // is worse than one that occasionally offers a run that gets skipped.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    QVERIFY(QDir().mkpath(dir.filePath(QStringLiteral("qtmaildir"))));
    const QString conf = dir.filePath(QStringLiteral("qtmaildir/qtmaildir.conf"));
    {
        QSettings s(conf, QSettings::IniFormat);
        s.setValue(QStringLiteral("sync/command"), QStringLiteral("/bin/true"));
    }

    Config config;
    config.load(conf);
    MainWindow window(config);

    auto *action = window.findChild<QAction *>(QStringLiteral("sync"));
    QVERIFY(action);

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Running));
    QVERIFY(!action->isEnabled());

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Unknown));
    QVERIFY2(action->isEnabled(),
             "an unobservable lock table left the sync action disabled");
}

void TestMainWindow::escapeBlanksTheMessagePane()
{
    // A registered action like any other, so it reaches the menus, the shortcut
    // reference and [keys]. Clearing m_currentThreadId with the pane is the
    // part that matters: a late threadLoaded would otherwise paint the thread
    // straight back, which is the race fixed in 0.8.0.
    const Config config;
    MainWindow window(config);

    auto *action = window.findChild<QAction *>(QStringLiteral("clear_pane"));
    QVERIFY2(action, "no clear_pane action registered");

    // Shift+Esc since item 50: plain Escape now clears the selection too, and
    // this narrower action kept the same key with a modifier. The behaviour
    // asserted below is unchanged, which is the point of keeping both.
    QCOMPARE(action->shortcut(),
             QKeySequence(Qt::ShiftModifier | Qt::Key_Escape));

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);

    model->appendBatch({ makeThread(QStringLiteral("t1"), {}),
                         makeThread(QStringLiteral("t2"), {}) });

    selectThreadRow(view, 0);
    QVERIFY2(!window.currentThreadId().isEmpty(),
             "no thread was opened to blank");

    action->trigger();
    QVERIFY2(window.currentThreadId().isEmpty(),
             "Escape left the thread loaded in the pane");

    // Blanking is a view change, not a mail change: the selection stays.
    QCOMPARE(view->selectionModel()->selectedRows().size(), 1);
}

void TestMainWindow::deleteTogglesOnAnAlreadyDeletedThread()
{
    // Hitting Delete twice is the natural way to say "no, put it back", and
    // adding a tag that is already present is a no-op the user cannot see.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    // The THREAD action, since this is about a THREAD's state. Item 108 made
    // the plain `delete` act on the one message a card displays, and a thread
    // summary carrying `deleted` says nothing about that message's own tags.
    auto *action = window.findChild<QAction *>(QStringLiteral("delete_thread"));
    QVERIFY(action);

    model->appendBatch({ makeThread(QStringLiteral("t1"),
                                    { QStringLiteral("deleted") }) });
    selectThreadRow(view, 0);

    action->trigger();

    // The optimistic model update is synchronous, so the row reflects the
    // change without a worker.
    QVERIFY2(!model->threadAt(0).isDeleted(),
             "delete on an already-deleted thread did not undelete it");
}

void TestMainWindow::deleteOnAMixedSelectionDeletesRatherThanSplittingIt()
{
    // The constraint that makes this more than a one-liner: toggling each
    // thread independently would leave one keystroke with the selection in two
    // states, which is worse than either outcome. Undelete only when every
    // selected thread is already deleted.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *action = window.findChild<QAction *>(QStringLiteral("delete_thread"));
    QVERIFY(action);

    model->appendBatch({ makeThread(QStringLiteral("t1"),
                                    { QStringLiteral("deleted") }),
                         makeThread(QStringLiteral("t2"), {}) });

    view->selectAll();
    QCOMPARE(view->selectionModel()->selectedRows().size(), 2);

    action->trigger();

    QVERIFY2(model->threadAt(0).isDeleted() && model->threadAt(1).isDeleted(),
             "a mixed selection split instead of deleting the whole selection");
}

/// Builds a window whose SECOND thread is expanded and carries one reply, with
/// the two threads deliberately in opposite states.
///
/// Item 88's shape in one place. A tree numbers rows per parent, so the first
/// reply of any thread has row() == 0 and threadAt(0) answers about the FIRST
/// THREAD IN THE LIST. Every test below selects that reply and asserts on
/// behaviour that can only be right if the thread was resolved through the
/// index: with the row number, each one reads t1's state while acting on t2.
///
/// The opposite states are what makes the tests able to fail. Two threads in
/// the same state give the same answer either way, which is how the reverted
/// item 87 fix passed while corrupting mail.
///
/// \p replyTags defaults to the thread's own tags, which is the usual case.
/// Pass it explicitly to make a reply DISAGREE with its thread, which is what
/// separates "reads the right thread" from "reads the right message": a reply
/// can be unread inside a thread that is not, and vice versa.
static QModelIndex expandSecondThreadAndSelectItsReply(
    QTreeView *view, ThreadListModel *model, const QStringList &firstTags,
    const QStringList &secondTags,
    const std::optional<QStringList> &replyTags = std::nullopt)
{
    ThreadSummary first = makeThread(QStringLiteral("t1"), firstTags);
    ThreadSummary second = makeThread(QStringLiteral("t2"), secondTags);
    second.totalCount = 2;
    model->appendBatch({ first, second });

    MessageNode root;
    root.messageId = QStringLiteral("m0@example.org");
    root.threadId = QStringLiteral("t2");
    root.tags = secondTags;
    root.depth = 0;
    MessageNode reply;
    reply.messageId = QStringLiteral("m1@example.org");
    reply.threadId = QStringLiteral("t2");
    reply.tags = replyTags.value_or(secondTags);
    reply.depth = 1;
    model->setThreadMessages(QStringLiteral("t2"), { root, reply });

    const QModelIndex threadRow = model->index(1, 0, QModelIndex());
    view->expand(threadRow);

    const QModelIndex replyRow = model->index(0, 0, threadRow);
    if (!replyRow.isValid() || !model->isMessageRow(replyRow))
        return {};

    // Row 0 under its parent, which is the trap: the number is a plausible
    // top-level row and threadAt() cannot tell the difference.
    if (replyRow.row() != 0)
        return {};

    view->selectionModel()->select(
        replyRow, QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
    view->setCurrentIndex(replyRow);
    QApplication::processEvents();
    return replyRow;
}

void TestMainWindow::deleteOnAReplyReadsItsOwnThreadNotTheFirstInTheList()
{
    // Item 88's trap, still live: a toggle must read the state of the row it
    // is on, not of whichever thread sits at that row NUMBER in the list.
    //
    // Through `delete_thread` rather than `delete`. Since item 103 Delete
    // MOVES the file, so it is no longer a pure toggle over a tag and needs a
    // configured trash folder and a worker; `delete_thread` is the variant
    // that stayed tag-only, and it is a toggle over `deleted` exactly as
    // Delete used to be. The message-scoped Delete's own direction choice is
    // covered by the worker-backed cases at the bottom of this file, which is
    // where a move can actually be observed.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *action = window.findChild<QAction *>(QStringLiteral("delete_thread"));
    QVERIFY(action);

    // t1 deleted, t2 not. Reading t1's state for a reply of t2 makes the
    // toggle choose UNDELETE for a thread that was never deleted.
    const QModelIndex reply = expandSecondThreadAndSelectItsReply(
        view, model, { QStringLiteral("deleted") }, {});
    QVERIFY2(reply.isValid(),
             "the fixture did not produce a reply row at row 0, so this test "
             "would assert nothing about item 88's trap");

    // t2 is the reply's thread and is NOT deleted, so the correct direction
    // is Delete. Reading t1's state instead would choose Undelete.
    QVERIFY2(!model->threadAt(1).isDeleted(),
             "the fixture's second thread is already deleted, so both "
             "directions would look alike and this test would assert nothing");

    action->trigger();

    // Asserted on the MODEL, not on the undo stack. Delete thread MOVES since
    // item 103's follow-up, and the undo entry is pushed once the worker
    // confirms the move, which this bare window has no database to perform.
    // The DIRECTION is chosen synchronously and is what item 88's trap was
    // about: the repaint below happens only on the delete direction.
    QVERIFY2(model->threadAt(1).isDeleted(),
             "Delete on a reply of an undeleted thread chose the wrong "
             "direction: it read the FIRST thread's state, which is deleted");
    // And the OTHER thread is untouched: the action must act on the reply's
    // own conversation, not on both.
    QVERIFY2(model->threadAt(0).isDeleted(),
             "the fixture's first thread stopped being deleted, which means "
             "the action reached a thread it was never pointed at");
}

void TestMainWindow::toggleUnreadOnAReplyReadsItsOwnThreadNotTheFirstInTheList()
{
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *action = window.findChild<QAction *>(QStringLiteral("toggle_unread"));
    QVERIFY(action);

    // t1 unread, t2 read. Reading t1's state marks an already-read message
    // read again, which is a no-op write the user sees as a dead key.
    const QModelIndex reply = expandSecondThreadAndSelectItsReply(
        view, model, { QStringLiteral("unread") }, {});
    QVERIFY2(reply.isValid(), "the fixture did not produce a reply row at row 0");

    action->trigger();

    QCOMPARE(window.undoDepthForTesting(), 1);
    QVERIFY2(window.undoTextForTesting().contains(QStringLiteral("Mark unread")),
             qPrintable(QStringLiteral(
                            "Toggle unread on a reply of a READ thread chose "
                            "the wrong direction: %1. It read the FIRST "
                            "thread's state, which is unread.")
                            .arg(window.undoTextForTesting())));
}

void TestMainWindow::importantOnAnAlreadyImportantThreadRemovesTheTag()
{
    // Item 98. `flag` was a one-way add, so pressing it on a thread that is
    // already important re-sent a tag the thread had: a no-op write, and a
    // no-op repaints nothing, so the key read as dead. Its two neighbours,
    // Delete and Toggle unread, had been toggles for a long time.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *action = window.findChild<QAction *>(QStringLiteral("flag"));
    QVERIFY(action);

    model->appendBatch({ makeThread(QStringLiteral("t1"),
                                    QStringList{ QStringLiteral("inbox"),
                                                 QStringLiteral("flagged") }) });
    QApplication::processEvents();

    view->setCurrentIndex(model->index(0, 0));
    view->selectionModel()->select(model->index(0, 0),
                                   QItemSelectionModel::ClearAndSelect
                                       | QItemSelectionModel::Rows);
    QApplication::processEvents();

    action->trigger();

    QCOMPARE(window.undoDepthForTesting(), 1);
    QVERIFY2(window.undoTextForTesting().contains(
                 QStringLiteral("Unmark important")),
             qPrintable(QStringLiteral(
                            "Important on an already-important thread did not "
                            "remove the tag: %1. A one-way add is a no-op the "
                            "user cannot see.")
                            .arg(window.undoTextForTesting())));
}

void TestMainWindow::importantOnAPlainThreadStillAddsTheTag()
{
    // The other direction, so a mutation inverting the test above cannot pass.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *action = window.findChild<QAction *>(QStringLiteral("flag"));
    QVERIFY(action);

    model->appendBatch({ makeThread(QStringLiteral("t1"),
                                    QStringList{ QStringLiteral("inbox") }) });
    QApplication::processEvents();

    view->setCurrentIndex(model->index(0, 0));
    view->selectionModel()->select(model->index(0, 0),
                                   QItemSelectionModel::ClearAndSelect
                                       | QItemSelectionModel::Rows);
    QApplication::processEvents();

    action->trigger();

    QCOMPARE(window.undoDepthForTesting(), 1);
    QVERIFY2(window.undoTextForTesting().contains(
                 QStringLiteral("Mark important"))
                 && !window.undoTextForTesting().contains(
                     QStringLiteral("Unmark")),
             qPrintable(QStringLiteral(
                            "Important on a plain thread did not add the tag: "
                            "%1")
                            .arg(window.undoTextForTesting())));
}

void TestMainWindow::importantOnAReplyReadsItsOwnStateNotItsThreads()
{
    // The trap items 88 and 105 each fixed once, which is why item 98 says to
    // call everySelectedRowHasTag() rather than copy the then-current Delete
    // loop.
    //
    // THREE states, all different, which is what the test needs to distinguish
    // the two wrong answers from the right one. t1 (the first thread in the
    // list) is unflagged, t2 (the reply's own thread) is flagged, and the
    // REPLY is unflagged. Reading t1 by row number answers "not flagged" and
    // reading the reply's THREAD answers "flagged", so only a read of the
    // message itself gives "not flagged" for the right reason.
    //
    // Leaving the reply's tags defaulted to its thread's is the trap: a reply
    // in the same state as its thread answers identically whichever of the two
    // the code reads, and the mutation putting item 105's bug back stays green.
    // Measured: it did.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *action = window.findChild<QAction *>(QStringLiteral("flag"));
    QVERIFY(action);

    const QModelIndex reply = expandSecondThreadAndSelectItsReply(
        view, model, {}, { QStringLiteral("flagged") },
        QStringList{});
    QVERIFY2(reply.isValid(),
             "the fixture did not produce a reply row at row 0, so this test "
             "would assert nothing");

    action->trigger();

    QVERIFY2(window.pendingMessageIdsForTesting().contains(
                 QStringLiteral("m1@example.org")),
             "Important on a reply did not act on that reply");
    QCOMPARE(window.undoDepthForTesting(), 1);
    QVERIFY2(window.undoTextForTesting().contains(
                 QStringLiteral("Mark important"))
                 && !window.undoTextForTesting().contains(
                     QStringLiteral("Unmark")),
             qPrintable(QStringLiteral(
                            "Important on an unflagged reply chose the wrong "
                            "direction: %1. Its own THREAD is flagged, so "
                            "reading the thread gives Unmark.")
                            .arg(window.undoTextForTesting())));
}

void TestMainWindow::editTagsOnAReplyCountsItsOwnThreadNotTheFirstInTheList()
{
    // The tag dialog is modal, so what is tested is the count it is BUILT
    // from. Those counts drive its tri-state checkboxes, so a wrong count
    // offers to remove a tag the message does not carry and shows the ones it
    // does as unset.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);

    const QModelIndex reply = expandSecondThreadAndSelectItsReply(
        view, model, { QStringLiteral("t1only") }, { QStringLiteral("t2only") });
    QVERIFY2(reply.isValid(), "the fixture did not produce a reply row at row 0");

    const QHash<QString, int> counts = window.selectionTagCountsForTesting();

    QVERIFY2(counts.contains(QStringLiteral("t2only")),
             "the tag dialog would not offer the tag the selected reply "
             "actually carries");
    QVERIFY2(!counts.contains(QStringLiteral("t1only")),
             "the tag dialog counted the FIRST thread's tags for a reply of "
             "the second, so it would offer to remove a tag that is not there");
}

void TestMainWindow::markCurrentThreadReadResolvesTheThreadThroughTheIndex()
{
    // Item 87 is blocked on this and will scope the write to one message. Today
    // an unrelated guard hides the defect: onThreadSelected clears
    // m_currentThreadId for a message row, so markCurrentThreadRead returns
    // before it can read the wrong thread. That guard is not the protection
    // this needs, and item 87 does not remove it, so the assertion here is on
    // the resolution itself rather than on a write that cannot currently
    // happen.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);

    const QModelIndex reply = expandSecondThreadAndSelectItsReply(
        view, model, { QStringLiteral("unread") }, { QStringLiteral("unread") });
    QVERIFY2(reply.isValid(), "the fixture did not produce a reply row at row 0");

    // The question the timer's handler asks, in isolation: which thread is the
    // current row part of. With the row number this answers "t1" for a reply
    // of t2.
    QCOMPARE(window.threadForCurrentRowForTesting().threadId,
             QStringLiteral("t2"));
}

void TestMainWindow::deletingAReplyRepaintsThatReplyRow()
{
    // `spam`, not `delete`. Since item 103 Delete MOVES the file, so it needs
    // an account with a configured trash folder and a worker to do the move;
    // this bare window has neither, and Delete correctly refuses. What is
    // under test here is unchanged by that: `spam` is the other message-scoped
    // tag-only action, and it paints the same doomed state.
    // The user's report, at the gesture level: "I'm hitting delete on a reply
    // to a thread, I see the edits counter increasing but I have no feedback
    // if that message is being deleted." The model-level test proves
    // applyMessageTagChange works; this proves the action reaches it, which is
    // the half that was actually missing.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *action = window.findChild<QAction *>(QStringLiteral("spam"));
    QVERIFY(action);

    const QModelIndex reply =
        expandSecondThreadAndSelectItsReply(view, model, {}, {});
    QVERIFY2(reply.isValid(), "the fixture did not produce a reply row at row 0");

    // Nothing to see before the gesture, so the assertion after it means
    // something.
    QVERIFY(!model->messageAt(reply).isSpam());
    const QVariant before = model->data(reply, Qt::BackgroundRole);

    QSignalSpy spy(model, &QAbstractItemModel::dataChanged);
    action->trigger();

    QVERIFY2(model->messageAt(reply).isSpam(),
             "Delete on a reply left the reply's own row unchanged, so the "
             "pending count moved and the user saw nothing");
    QVERIFY2(spy.count() >= 1, "no repaint was requested for the reply's row");
    QVERIFY2(model->data(reply, Qt::BackgroundRole) != before,
             "the deleted reply paints exactly as it did before");

    // The THREAD row must not follow: it stands for the whole conversation,
    // and one deleted reply does not doom it.
    const QModelIndex threadRow = reply.parent();
    QVERIFY2(!model->threadFor(threadRow).isSpam(),
             "deleting one reply marked its whole thread deleted");
}

void TestMainWindow::toggleUnreadOnAReplyReadsTheReplysOwnState()
{
    // The user's report: "read/unread still doesn't trigger a repaint of the
    // reply". The write was already message-scoped and the model already
    // repaints a message row, so neither was the fault. The DIRECTION was:
    // the action read threadFor(current).isUnread(), the THREAD's state, even
    // when the selected row is a reply.
    //
    // The consequence is a dead key rather than a wrong write. On a read
    // thread the answer is always "add unread", so pressing it on an
    // already-unread reply re-adds a tag it has, which is a no-op the model
    // correctly declines to repaint. Item 88 fixed WHICH thread this reads;
    // this is about reading a message at all.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *action = window.findChild<QAction *>(QStringLiteral("toggle_unread"));
    QVERIFY(action);

    // A thread that is READ carrying a reply that is UNREAD. That disagreement
    // is the whole test: with the thread's state the answer is "mark unread",
    // with the message's it is "mark read".
    const QModelIndex reply = expandSecondThreadAndSelectItsReply(
        view, model, {}, {}, QStringList{ QStringLiteral("unread") });
    QVERIFY2(reply.isValid(), "the fixture did not produce a reply row at row 0");
    QVERIFY(model->messageAt(reply).isUnread());
    QVERIFY2(!model->threadFor(reply).isUnread(),
             "the fixture's thread is unread too, so this test cannot tell the "
             "two sources apart");

    action->trigger();

    QVERIFY2(!model->messageAt(reply).isUnread(),
             "Toggle unread on an unread reply did not mark it read: the "
             "direction came from the THREAD, which is already read, so it "
             "re-added a tag the reply already had and nothing changed");
    QVERIFY2(window.undoTextForTesting().contains(QStringLiteral("Mark read")),
             qPrintable(QStringLiteral("wrong direction: %1")
                            .arg(window.undoTextForTesting())));
}

void TestMainWindow::toggleUnreadOnAReplyRepaintsItInBothDirections()
{
    // Visible BOTH ways. The user reached the repaint only by deleting and
    // undoing, which is a different write forcing the row to redraw; the
    // unread change itself has to do it on its own, in each direction.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *action = window.findChild<QAction *>(QStringLiteral("toggle_unread"));
    QVERIFY(action);

    const QModelIndex reply = expandSecondThreadAndSelectItsReply(
        view, model, {}, {}, QStringList{ QStringLiteral("unread") });
    QVERIFY(reply.isValid());

    const QVariant unreadForeground = model->data(reply, Qt::ForegroundRole);
    const QVariant unreadFont = model->data(reply, Qt::FontRole);

    QSignalSpy spy(model, &QAbstractItemModel::dataChanged);
    action->trigger();

    QVERIFY2(spy.count() >= 1, "marking a reply read requested no repaint");
    QVERIFY2(model->data(reply, Qt::ForegroundRole) != unreadForeground,
             "a reply marked read paints exactly as it did while unread");
    QVERIFY2(model->data(reply, Qt::FontRole) != unreadFont,
             "a reply marked read keeps the unread font");

    // And back. A toggle that is only visible one way is half a toggle.
    spy.clear();
    action->trigger();
    QVERIFY(model->messageAt(reply).isUnread());
    QVERIFY2(spy.count() >= 1, "marking a reply unread again requested no repaint");
    QCOMPARE(model->data(reply, Qt::ForegroundRole), unreadForeground);
    QCOMPARE(model->data(reply, Qt::FontRole), unreadFont);
}

void TestMainWindow::taggingTheOpenReplyUpdatesTheMessagePaneStrip()
{
    // `spam`, not `delete`. Since item 103 Delete MOVES the file, so it needs
    // an account with a configured trash folder and a worker to do the move;
    // this bare window has neither, and Delete correctly refuses. What is
    // under test here is unchanged by that: `spam` is the other message-scoped
    // tag-only action, and it paints the same doomed state.
    // The user's report: "the right pane chips are not [repainted], for it to
    // sync I have to change message and go back to the edited one".
    //
    // sendThreadTagChange refreshes the strip when the edited thread is the
    // open one. sendMessageTagChange had no equivalent, so a message-scoped
    // write updated the list row and left the pane's chips describing the
    // message as it was before the edit.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *strip = window.findChild<TagStrip *>();
    QVERIFY2(strip, "no tag strip in the message pane");

    // visible + hidden: TagStrip collapses what does not fit into a "+N" chip,
    // and an unshown window has no width, so visibleTags() alone measures the
    // layout rather than the data.
    const auto stripTags = [strip]() {
        return strip->visibleTags() + strip->hiddenTags();
    };
    auto *action = window.findChild<QAction *>(QStringLiteral("spam"));
    QVERIFY(action);

    // A tag the strip will actually draw. Account tags are filtered out by the
    // strip, and `unread` and `inbox` are hidden on the card but not here, so
    // the fixture uses a plain functional tag to keep the assertion honest.
    const QModelIndex reply = expandSecondThreadAndSelectItsReply(
        view, model, {}, {}, QStringList{ QStringLiteral("todo") });
    QVERIFY2(reply.isValid(), "the fixture did not produce a reply row at row 0");

    // Selecting the reply is what puts it in the pane, and the strip has to be
    // showing the reply's own tags before the edit or this asserts nothing.
    QVERIFY2(stripTags().contains(QStringLiteral("todo")),
             "the strip does not show the selected reply's tags, so this test "
             "cannot tell a missing refresh from a strip that never had them");
    QVERIFY(!stripTags().contains(QStringLiteral("spam")));

    action->trigger();

    QVERIFY2(stripTags().contains(QStringLiteral("spam")),
             "the message pane's chips still describe the reply as it was "
             "before the edit; the user has to select away and back to see it");
}

void TestMainWindow::taggingAnUnrelatedReplyLeavesTheStripAlone()
{
    // The guard, not the refresh. The strip describes the message ON DISPLAY,
    // so a write to a different message must not repaint it with that
    // message's tags. The thread path has the same guard, keyed on
    // m_currentThreadId.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *strip = window.findChild<TagStrip *>();
    QVERIFY(strip);

    // visible + hidden: TagStrip collapses what does not fit into a "+N" chip,
    // and an unshown window has no width, so visibleTags() alone measures the
    // layout rather than the data.
    const auto stripTags = [strip]() {
        return strip->visibleTags() + strip->hiddenTags();
    };

    ThreadSummary thread = makeThread(QStringLiteral("t1"), {});
    thread.totalCount = 3;
    model->appendBatch({ thread });

    MessageNode root;
    root.messageId = QStringLiteral("m0@example.org");
    root.threadId = QStringLiteral("t1");
    root.depth = 0;
    MessageNode first;
    first.messageId = QStringLiteral("m1@example.org");
    first.threadId = QStringLiteral("t1");
    first.tags = QStringList{ QStringLiteral("todo") };
    first.depth = 1;
    MessageNode second;
    second.messageId = QStringLiteral("m2@example.org");
    second.threadId = QStringLiteral("t1");
    second.tags = QStringList{ QStringLiteral("later") };
    second.depth = 1;
    model->setThreadMessages(QStringLiteral("t1"), { root, first, second });

    const QModelIndex threadRow = model->index(0, 0, QModelIndex());
    view->expand(threadRow);

    // The FIRST reply is the one on display.
    const QModelIndex displayed = model->index(0, 0, threadRow);
    view->selectionModel()->select(
        displayed,
        QItemSelectionModel::ClearAndSelect | QItemSelectionModel::Rows);
    view->setCurrentIndex(displayed);
    QApplication::processEvents();
    QVERIFY(stripTags().contains(QStringLiteral("todo")));

    // A write to the OTHER reply, reaching the send path directly: driving it
    // through the action would move the selection and change what is on
    // display, which is the thing being held still.
    window.sendMessageTagChangeForTesting({ QStringLiteral("m2@example.org") },
                                          { QStringLiteral("deleted") }, {},
                                          QStringLiteral("Delete"));

    QVERIFY2(!stripTags().contains(QStringLiteral("deleted")),
             "the strip took on the tags of a message that is not the one in "
             "the pane");
    QVERIFY2(stripTags().contains(QStringLiteral("todo")),
             "the strip stopped describing the message on display");
}

void TestMainWindow::aHeldMessageEditIsSentWhenTheSyncEnds()
{
    // `spam`, not `delete`. Since item 103 Delete MOVES the file, so it needs
    // an account with a configured trash folder and a worker to do the move;
    // this bare window has neither, and Delete correctly refuses. What is
    // under test here is unchanged by that: `spam` is the other message-scoped
    // tag-only action, and it paints the same doomed state.
    // Found by reading while fixing the strip refresh, not reported.
    //
    // flushHeldEdits() looped over edit.threadIds and called
    // sendThreadTagChange() only. A message-scoped edit held during a sync
    // carries no thread ids at all, so the loop did nothing, the send
    // early-returned on an empty list, and the edit was DROPPED: applied
    // optimistically to the row, counted as unsynced, and never written. The
    // user would have seen the change, been told it was pending, and lost it.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *action = window.findChild<QAction *>(QStringLiteral("spam"));
    QVERIFY(action);

    const QModelIndex reply =
        expandSecondThreadAndSelectItsReply(view, model, {}, {});
    QVERIFY(reply.isValid());

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Running));
    action->trigger();
    QVERIFY2(window.hasEditAwaitingSend(),
             "a message edit made during a sync was not held");

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Idle));

    QVERIFY2(!window.hasEditAwaitingSend(),
             "the sync ending did not send the held message edit, so it was "
             "silently dropped: shown on the row, counted as pending, never "
             "written");

    // Sent for the MESSAGE, not escalated to its thread. Losing the scope on
    // the way out of the hold would delete every message in the thread.
    QVERIFY2(window.pendingMessageIdsForTesting().contains(
                 QStringLiteral("m1@example.org")),
             "the held edit was not sent with its message scope");
    QVERIFY2(window.pendingThreadIdsForTesting().isEmpty(),
             "a held MESSAGE edit was sent as a thread edit, which would tag "
             "every message in the thread");

    // And the row still shows it: the flush takes the optimistic update back
    // before re-sending, so a bug there leaves the row wrong in the other
    // direction.
    QVERIFY2(model->messageAt(reply).isSpam(),
             "sending the held edit lost the tag from the reply's row");
}

void TestMainWindow::anActionOnAThreadRowActsOnTheMessageItDisplays()
{
    // `spam`, not `delete`. Since item 103 Delete MOVES the file, so it needs
    // an account with a configured trash folder and a worker to do the move;
    // this bare window has neither, and Delete correctly refuses. What is
    // under test here is unchanged by that: `spam` is the other message-scoped
    // tag-only action, and it paints the same doomed state.
    // Item 108, the whole point of it. A root card renders ONE message since
    // item 66, so acting on it acts on that message; the conversation is
    // reached through the explicit thread actions.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);

    ThreadSummary t = makeThread(QStringLiteral("t1"), {});
    t.totalCount = 7;
    model->appendBatch({ t });
    selectThreadRow(view, 0);

    auto *deleteAction = window.findChild<QAction *>(QStringLiteral("spam"));
    QVERIFY(deleteAction);
    deleteAction->trigger();

    QCOMPARE(window.pendingMessageIdsForTesting(),
             QStringList{ QStringLiteral("t1-first@example.org") });
    QVERIFY2(window.pendingThreadIdsForTesting().isEmpty(),
             "the ordinary Delete still acted on the whole thread, so it "
             "touched six messages the card does not display");

    // The thread action is how the conversation is reached, and it must still
    // work from the same selection.
    //
    // Asserted on the MODEL rather than on a pending write. Delete thread
    // MOVES every message since item 103's follow-up, and a move needs ids and
    // paths that only the database holds for a thread this bare window never
    // expanded, so the write is issued after a worker round trip that never
    // completes here. What is synchronous, and what this test is about, is the
    // scope: the whole thread is marked, not the one message its card shows.
    auto *deleteThread =
        window.findChild<QAction *>(QStringLiteral("delete_thread"));
    QVERIFY(deleteThread);
    QVERIFY2(!model->threadAt(0).isDeleted(),
             "the thread already read as deleted, so the check below would "
             "pass without the action doing anything");
    deleteThread->trigger();

    QVERIFY2(model->threadAt(0).isDeleted(),
             "Delete thread did not mark the whole thread, so the card paints "
             "undeleted until the row is clicked");
}

void TestMainWindow::theThreadSubmenuIsReachableFromBothMenus()
{
    // The user asked for "a submenu when right clicking and the same submenu
    // under Message in the top menu". Both, not one: the context menu is where
    // the gesture starts and the menu bar is where a shortcut is discovered.
    //
    // A QMenu belongs to ONE menu tree, so these are two instances holding the
    // same actions. Adding a single instance to both silently gives it to
    // whichever added it last, which is the failure this pins.
    const Config config;
    MainWindow window(config);

    auto *context =
        window.findChild<QMenu *>(QStringLiteral("threadContextMenu"));
    QVERIFY(context);

    const QStringList expected = {
        QStringLiteral("archive_thread"),
        QStringLiteral("delete_thread"),
        QStringLiteral("spam_thread"),
        QStringLiteral("toggle_unread_thread"),
        QStringLiteral("flag_thread"),
    };

    // Every submenu instance in the window, wherever it was added.
    const QList<QMenu *> submenus =
        window.findChildren<QMenu *>(QStringLiteral("threadActionsMenu"));
    QVERIFY2(submenus.size() >= 2,
             qPrintable(QStringLiteral("expected the thread submenu in both "
                                       "the context menu and the menu bar, "
                                       "found %1 instance(s)")
                            .arg(submenus.size())));

    for (QMenu *menu : submenus) {
        QStringList names;
        for (QAction *action : menu->actions()) {
            if (!action->isSeparator())
                names.append(action->objectName());
        }
        QCOMPARE(names, expected);
    }

    // One of them is the context menu's own, reached as a submenu rather than
    // as a loose action.
    bool inContextMenu = false;
    for (QAction *action : context->actions()) {
        if (action->menu()
            && action->menu()->objectName()
                   == QStringLiteral("threadActionsMenu")) {
            inContextMenu = true;
            break;
        }
    }
    QVERIFY2(inContextMenu,
             "right-clicking a thread offers no whole-thread submenu");
}

void TestMainWindow::autoMarkReadTouchesOnlyTheMessageOnDisplay()
{
    // Item 87, reported 2026-08-14: "with the first message in a thread
    // selected (not expanded), the 2s delay that marks it read applies to the
    // whole thread, so all answers are marked read as well."
    //
    // Not cosmetic. maildir.synchronize_flags is on, so removing `unread`
    // rewrites Maildir filenames and the next sync carries it to the server:
    // mail the user never opened stops being unread everywhere, and nothing
    // here can put it back except reading it again by hand.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    const QString path = dir.filePath(QStringLiteral("qtmaildir.conf"));
    QFile file(path);
    QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
    file.write("[general]\nmark_read_delay_ms = 0\n");
    file.close();

    Config config;
    config.load(path);
    QCOMPARE(config.markReadDelayMs(), 0);

    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *timer = window.findChild<QTimer *>(QStringLiteral("markReadTimer"));
    QVERIFY(timer);

    ThreadSummary t = makeThread(QStringLiteral("t1"),
                                 { QStringLiteral("unread") });
    t.totalCount = 7;
    model->appendBatch({ t });

    selectThreadRow(view, 0);
    QApplication::processEvents();
    QVERIFY2(timer->isActive() || !window.pendingMessageIdsForTesting().isEmpty(),
             "selecting an unread thread armed no mark-read at all");

    // Fire it. A zero-interval timer still goes through the event loop.
    QTRY_VERIFY_WITH_TIMEOUT(!timer->isActive(), 2000);
    QApplication::processEvents();

    // ONE message, the one the card renders, and named rather than merely
    // counted: a thread of seven whose first message is the target is exactly
    // the case where a count of one could still be the wrong one.
    QCOMPARE(window.pendingMessageIdsForTesting(),
             QStringList{ QStringLiteral("t1-first@example.org") });
    QVERIFY2(window.pendingThreadIdsForTesting().isEmpty(),
             "the automatic mark-read still wrote to the whole thread, so six "
             "messages the user never displayed were marked read and the next "
             "sync carries that to the server");

    // Still not on the undo stack. The user never took this action, so
    // hijacking Ctrl+Z to reverse it would undo something they did not do.
    QCOMPARE(window.undoDepthForTesting(), 0);
}

void TestMainWindow::autoMarkReadArmsForAReplyToo()
{
    // Selecting a reply displays that message, so the same rule applies to it.
    // Before item 87 the timer was deliberately not armed for a message row,
    // because the write it would have made was thread-scoped and would have
    // marked the whole conversation read. With the write scoped to one message
    // that objection is gone, and leaving it unarmed would mean the message
    // the user is reading is the one kind that never gets marked read.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    const QString path = dir.filePath(QStringLiteral("qtmaildir.conf"));
    QFile file(path);
    QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
    file.write("[general]\nmark_read_delay_ms = 0\n");
    file.close();

    Config config;
    config.load(path);

    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *timer = window.findChild<QTimer *>(QStringLiteral("markReadTimer"));
    QVERIFY(timer);

    // The reply is unread; its thread is not, so a thread-keyed timer would
    // have declined to arm at all.
    //
    // No "is it still unread" guard before the wait: the delay is 0 and the
    // helper pumps the event loop, so the write has already happened by the
    // time selection returns. The assertions below are on the write itself,
    // which is what this test is about, and the fixture above is what
    // establishes the reply started unread.
    const QModelIndex reply = expandSecondThreadAndSelectItsReply(
        view, model, {}, {}, QStringList{ QStringLiteral("unread") });
    QVERIFY(reply.isValid());

    QTRY_VERIFY_WITH_TIMEOUT(!timer->isActive(), 2000);
    QApplication::processEvents();

    QCOMPARE(window.pendingMessageIdsForTesting(),
             QStringList{ QStringLiteral("m1@example.org") });
    QVERIFY2(window.pendingThreadIdsForTesting().isEmpty(),
             "reading one reply marked its whole thread read");

    // And the row shows it, which is the half item 105 built.
    QVERIFY2(!model->messageAt(reply).isUnread(),
             "the reply was marked read without its row following");
}

void TestMainWindow::taggingTheOpenRootMessageKeepsTheStripPopulated()
{
    // `spam`, not `delete`. Since item 103 Delete MOVES the file, so it needs
    // an account with a configured trash folder and a worker to do the move;
    // this bare window has neither, and Delete correctly refuses. What is
    // under test here is unchanged by that: `spam` is the other message-scoped
    // tag-only action, and it paints the same doomed state.
    // The user, 2026-08-16: "right pane loses the chip row when repainting, it
    // simply disappears".
    //
    // The strip refresh added for item 105 reads the message's tags through
    // messageById(), which searches only the loaded CHILDREN. A root card's
    // message is never among them, so the lookup returned a default-constructed
    // node and the refresh set the strip to that node's empty tag list, wiping
    // a strip that had been correct a moment earlier. Worse than not
    // refreshing: it actively destroyed what was there.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *strip = window.findChild<TagStrip *>();
    QVERIFY(strip);

    ThreadSummary t = makeThread(QStringLiteral("t1"),
                                 { QStringLiteral("todo") });
    t.totalCount = 1;
    model->appendBatch({ t });

    selectThreadRow(view, 0);
    QApplication::processEvents();

    // visible + hidden, not visible alone. TagStrip is a single row that
    // collapses whatever does not fit into a trailing "+N" chip, and this
    // window is never shown, so it has no width to lay out with and puts
    // almost everything in the hidden half. Asserting on visibleTags() alone
    // measures the layout, not the data, and fails for a reason this test does
    // not care about.
    const auto stripTags = [strip]() {
        return strip->visibleTags() + strip->hiddenTags();
    };

    // The guard: the strip has to be showing something before the edit, or
    // this cannot tell "wiped" from "never populated".
    QVERIFY2(stripTags().contains(QStringLiteral("todo")),
             "the strip never showed the selected thread's tags");

    auto *action = window.findChild<QAction *>(QStringLiteral("spam"));
    QVERIFY(action);
    action->trigger();

    QVERIFY2(!stripTags().isEmpty(),
             "the chip row was emptied: the refresh looked the message up "
             "among the loaded replies, where a root card's message never is, "
             "and set the strip to the resulting empty tag list");
    QVERIFY2(stripTags().contains(QStringLiteral("todo")),
             "the strip lost the tag the message still carries");
    QVERIFY2(stripTags().contains(QStringLiteral("spam")),
             "the strip did not pick up the tag just written");
}

void TestMainWindow::aLoadedMessageCorrectsTheStripFromTheThreadsUnion()
{
    // Reported by hand, 2026-08-16, against a real four-message thread whose
    // root carried `unread` and whose THIRD message carried `signed`:
    // "the right pane chips update and both signed and unread disappear ...
    // changing message and going back makes them reappear".
    //
    // Neither half was the write's doing. Selecting a thread row sets the strip
    // from ThreadSummary::tags, which is notmuch's UNION over the thread, so
    // the pane claimed the root message was `signed` when a sibling was. The
    // mark-read write then replaced it with the root's real tags, correctly
    // dropping both, and reselecting put the union back. The pane was lying
    // BEFORE the write, not after it.
    //
    // The load is the authority: MessageRef carries the message's own tags.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *strip = window.findChild<TagStrip *>();
    QVERIFY(strip);

    const auto stripTags = [strip]() {
        return strip->visibleTags() + strip->hiddenTags();
    };

    // The union carries `signed`; the root message does not.
    ThreadSummary t = makeThread(QStringLiteral("t1"),
                                 { QStringLiteral("inbox"),
                                   QStringLiteral("signed"),
                                   QStringLiteral("unread") });
    t.totalCount = 4;
    model->appendBatch({ t });

    selectThreadRow(view, 0);
    QApplication::processEvents();

    // Before the load the strip can only show the union, which is what the
    // model holds. That is the state the user saw and reported.
    QVERIFY(stripTags().contains(QStringLiteral("signed")));

    // The worker answers with the message's OWN tags.
    MessageRef ref;
    ref.messageId = QStringLiteral("t1-first@example.org");
    ref.tags = QStringList{ QStringLiteral("inbox"), QStringLiteral("unread") };
    QMetaObject::invokeMethod(
        &window, "onMessageLoaded", Qt::DirectConnection,
        Q_ARG(QVector<MessageRef>, QVector<MessageRef>{ ref }),
        Q_ARG(quint64, window.currentGenerationForTesting()));
    QApplication::processEvents();

    QVERIFY2(!stripTags().contains(QStringLiteral("signed")),
             "the pane still claims the root message is signed, which is a "
             "sibling's tag: it is showing the thread's union rather than the "
             "message on display");
    QVERIFY2(stripTags().contains(QStringLiteral("unread")),
             "the pane lost a tag the message really carries");
}

void TestMainWindow::aLoadedRootMessageGivesTheCardItsOwnTags()
{
    // The same correction, reaching the MODEL, which is what fixes the two
    // repaint reports: "if I mark the root message read the left pane entry
    // doesn't repaint (stays bold)" and the same for delete.
    //
    // The card could not repaint because the model had no per-message tags for
    // a root at all, so a message-scoped write updated the thread summary only
    // when the thread was a single message. A load gives the root the same
    // per-message node a reply has had all along, and from then on the card
    // draws the message it displays.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);

    ThreadSummary t = makeThread(QStringLiteral("t1"),
                                 { QStringLiteral("inbox"),
                                   QStringLiteral("signed"),
                                   QStringLiteral("unread") });
    t.totalCount = 4;
    model->appendBatch({ t });

    selectThreadRow(view, 0);
    QApplication::processEvents();

    MessageRef ref;
    ref.messageId = QStringLiteral("t1-first@example.org");
    ref.tags = QStringList{ QStringLiteral("inbox"), QStringLiteral("unread") };
    QMetaObject::invokeMethod(
        &window, "onMessageLoaded", Qt::DirectConnection,
        Q_ARG(QVector<MessageRef>, QVector<MessageRef>{ ref }),
        Q_ARG(quint64, window.currentGenerationForTesting()));
    QApplication::processEvents();

    // The model now knows what the root message itself carries.
    const MessageNode root =
        model->messageById(QStringLiteral("t1-first@example.org"));
    QCOMPARE(root.messageId, QStringLiteral("t1-first@example.org"));
    QVERIFY2(!root.tags.contains(QStringLiteral("signed")),
             "the root's node still carries a sibling's tag");

    const QModelIndex threadIndex = model->index(0, 0, QModelIndex());
    QSignalSpy spy(model, &QAbstractItemModel::dataChanged);

    auto *toggle = window.findChild<QAction *>(QStringLiteral("toggle_unread"));
    QVERIFY(toggle);
    toggle->trigger();

    QVERIFY2(spy.count() >= 1, "marking the root message read repainted nothing");
    QVERIFY2(!model->messageById(QStringLiteral("t1-first@example.org"))
                  .isUnread(),
             "the root message is still unread after being marked read");

    // The card now draws that message, so it stops looking unread. Asserted on
    // the FONT, which is what the user means by "stays bold".
    QVERIFY2(!model->data(threadIndex, Qt::FontRole).value<QFont>().bold(),
             "the card still reads as unread, so the row stays bold and the "
             "user sees nothing");
    QVERIFY2(model->threadAt(0).isUnread(),
             "the thread summary was rewritten, claiming a four-message thread "
             "is read when three of its messages still are not");
}

void TestMainWindow::aTransientStatusMessageExpires()
{
    // "Sync complete" describes an event, not a state, and reads as though it
    // describes the present until something else overwrites it.
    const Config config;
    MainWindow window(config);

    auto *status = window.findChild<QLabel *>(QStringLiteral("statusMessage"));
    QVERIFY(status);
    auto *timer = window.findChild<QTimer *>(QStringLiteral("statusTimer"));
    QVERIFY2(timer, "no status expiry timer");

    QMetaObject::invokeMethod(&window, "showTransientStatus",
                              Q_ARG(QString, QStringLiteral("Sync complete")));
    QCOMPARE(status->text(), QStringLiteral("Sync complete"));
    QVERIFY(timer->isActive());

    // Fire it rather than waiting out the real interval.
    timer->setInterval(0);
    QTRY_VERIFY_WITH_TIMEOUT(status->text() != QStringLiteral("Sync complete"),
                             2000);
}

void TestMainWindow::theSelectionCountIsStateAndDoesNotExpire()
{
    // Not everything in the status bar is an event. The selection count
    // describes what is true right now and must persist while it stays true;
    // expiring it would undo the 0.8.0 discoverability work.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *status = window.findChild<QLabel *>(QStringLiteral("statusMessage"));
    QVERIFY(status);
    auto *timer = window.findChild<QTimer *>(QStringLiteral("statusTimer"));
    QVERIFY(timer);

    model->appendBatch({ makeThread(QStringLiteral("t1"), {}),
                         makeThread(QStringLiteral("t2"), {}) });
    view->selectAll();

    QVERIFY2(status->text().contains(QStringLiteral("2")),
             "the selection count was not reported");
    QVERIFY2(!timer->isActive(),
             "the selection count armed the expiry timer; it is state, "
             "not an event");
}

void TestMainWindow::anEditUndoneNettsBackToZero()
{
    // Reported by the user: open a thread, let the 2 s auto-mark-read remove
    // `unread`, then press Ctrl+U to put it back. The indicator read 2 unsynced
    // changes when the mail store was exactly where it started.
    //
    // The count tracks NET state, not writes. Two writes did happen, but their
    // effect cancels, and what the user needs to know is whether quitting now
    // would strand work.
    const Config config;
    MainWindow window(config);

    auto *label = window.findChild<QLabel *>(QStringLiteral("pendingEdits"));
    QVERIFY(label);
    QVERIFY(label->isHidden());

    // The automatic mark-read: remove `unread` from one message.
    TagChange off;
    off.messageIds = { QStringLiteral("m1") };
    off.removed = { QStringLiteral("unread") };
    off.description = QStringLiteral("Mark read");
    QVERIFY(QMetaObject::invokeMethod(&window, "onTagsApplied",
                                      Q_ARG(TagChange, off)));
    QVERIFY2(!label->isHidden(), "one edit must show the indicator");

    // Ctrl+U puts it back on the same message.
    TagChange on;
    on.messageIds = { QStringLiteral("m1") };
    on.added = { QStringLiteral("unread") };
    on.description = QStringLiteral("Mark unread");
    QVERIFY(QMetaObject::invokeMethod(&window, "onTagsApplied",
                                      Q_ARG(TagChange, on)));

    QVERIFY2(label->isHidden(),
             qPrintable(QStringLiteral("an edit and its inverse left the "
                                       "indicator showing '%1'")
                            .arg(label->text())));
}

void TestMainWindow::aDifferentTagOnTheSameMessageStillCounts()
{
    // Netting must be per (message, tag), not per message. Removing `unread`
    // and adding `flagged` on one message are two independent changes, and
    // neither cancels the other.
    const Config config;
    MainWindow window(config);

    auto *label = window.findChild<QLabel *>(QStringLiteral("pendingEdits"));
    QVERIFY(label);

    TagChange a;
    a.messageIds = { QStringLiteral("m1") };
    a.removed = { QStringLiteral("unread") };
    a.description = QStringLiteral("Mark read");
    QVERIFY(QMetaObject::invokeMethod(&window, "onTagsApplied",
                                      Q_ARG(TagChange, a)));

    TagChange b;
    b.messageIds = { QStringLiteral("m1") };
    b.added = { QStringLiteral("flagged") };
    b.description = QStringLiteral("Flag");
    QVERIFY(QMetaObject::invokeMethod(&window, "onTagsApplied",
                                      Q_ARG(TagChange, b)));

    QVERIFY2(!label->isHidden(),
             "two different tags on one message cancelled each other");
}

void TestMainWindow::anEditWithNoMessageIdsStillCounts()
{
    // A TagChange carrying no message ids cannot be netted against anything,
    // and must still register rather than silently counting as zero. Losing an
    // edit understates the indicator, which is the direction that costs the
    // user work.
    const Config config;
    MainWindow window(config);

    auto *label = window.findChild<QLabel *>(QStringLiteral("pendingEdits"));
    QVERIFY(label);

    TagChange change;
    change.added = { QStringLiteral("deleted") };
    change.description = QStringLiteral("Delete");
    QVERIFY(QMetaObject::invokeMethod(&window, "onTagsApplied",
                                      Q_ARG(TagChange, change)));

    QVERIFY2(!label->isHidden(),
             "an edit with no message ids was not counted at all");
}

// Item 37. A tag edit made while a background sync holds notmuch's write lock
// used to stall the worker: the read-write open BLOCKS until the lock frees
// (measured 9.158s against a 12s hold, returning NOTMUCH_STATUS_SUCCESS), so
// every later query and thread load queued behind it. These cases pin the fix:
// do not send the write while a sync is running, send it when the sync ends.

void TestMainWindow::anEditDuringABackgroundSyncIsNotSentYet()
{
    // The defect. Sending during the sync is what stalls the worker, so the
    // edit is held instead. The rows still show it: it is what the user asked
    // for and it is going to be applied.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    // The THREAD action: this test asserts on the thread ROW, which a
    // message-scoped write deliberately leaves alone since item 108. What
    // is under test is the HOLD, which is identical either way.
    auto *action = window.findChild<QAction *>(QStringLiteral("flag_thread"));
    QVERIFY2(action, "no flag action registered");

    model->appendBatch({ makeThread(QStringLiteral("t1"), {}) });
    selectThreadRow(view, 0);

    // A cron sync takes the lock.
    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Running));

    action->trigger();

    QVERIFY2(window.hasEditAwaitingSend(),
             "the edit was sent straight into a running sync, which is the "
             "blocking open that stalls the worker");
    QVERIFY2(model->threadAt(0).tags.contains(QStringLiteral("flagged")),
             "holding the edit also dropped it from the rows");
}

void TestMainWindow::aHeldEditIsSentWhenTheBackgroundSyncEnds()
{
    // The release. SyncMonitor already reports this transition for item 27, so
    // the held edit rides a signal that exists rather than a timer.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    // The THREAD action: this test asserts on the thread ROW, which a
    // message-scoped write deliberately leaves alone since item 108. What
    // is under test is the HOLD, which is identical either way.
    auto *action = window.findChild<QAction *>(QStringLiteral("flag_thread"));
    QVERIFY(action);

    model->appendBatch({ makeThread(QStringLiteral("t1"), {}) });
    selectThreadRow(view, 0);

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Running));
    action->trigger();
    QVERIFY(window.hasEditAwaitingSend());

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Idle));

    QVERIFY2(!window.hasEditAwaitingSend(),
             "the sync ending did not send the held edit");
    QVERIFY2(model->threadAt(0).tags.contains(QStringLiteral("flagged")),
             "sending the held edit lost the tag from the rows");
}

void TestMainWindow::aHeldEditCountsAsUnsynced()
{
    // A held edit has not reached the index, so onTagsApplied() never counted
    // it. It must still count here, because this is what the exit prompt reads:
    // quitting on a held edit loses it outright, which is the whole failure the
    // prompt exists to prevent.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *action = window.findChild<QAction *>(QStringLiteral("flag"));
    QVERIFY(action);
    auto *label = window.findChild<QLabel *>(QStringLiteral("pendingEdits"));
    QVERIFY(label);
    QVERIFY2(label->isHidden(), "the indicator starts hidden at zero");

    model->appendBatch({ makeThread(QStringLiteral("t1"), {}) });
    selectThreadRow(view, 0);

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Running));
    action->trigger();

    QVERIFY2(!label->isHidden(),
             "an edit held for a running sync was not counted as unsynced, so "
             "the exit prompt would let the user quit on it");
}

void TestMainWindow::anUnreadableLockTableStillSendsTheEdit()
{
    // State::Unknown means /proc/locks could not be read, so nothing is
    // observed. Holding writes there would strand every edit forever on a
    // platform that cannot see the lock at all. Unknown is not "running".
    //
    // Driven from Running, not from a fresh window: the guard is that Unknown
    // CLEARS the busy flag, and a window that was never busy would pass this
    // whatever Unknown did. Reaching Unknown by way of Running is also the only
    // way a real monitor gets there, when /proc/locks becomes unreadable
    // mid-session.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *action = window.findChild<QAction *>(QStringLiteral("flag"));
    QVERIFY(action);

    model->appendBatch({ makeThread(QStringLiteral("t1"), {}),
                         makeThread(QStringLiteral("t2"), {}) });

    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Running));
    selectThreadRow(view, 0);
    action->trigger();
    QVERIFY2(window.hasEditAwaitingSend(),
             "the edit was not held during a running sync, so this test is not "
             "exercising the Unknown transition it claims to");

    // The lock table becomes unreadable. That is not evidence of a sync, so
    // writing must resume and the held edit must go out.
    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Unknown));
    QVERIFY2(!window.hasEditAwaitingSend(),
             "an unreadable lock table kept the edit held, stranding it on any "
             "platform without /proc/locks");

    // And a NEW edit is sent rather than held.
    selectThreadRow(view, 1);
    action->trigger();
    QVERIFY2(!window.hasEditAwaitingSend(),
             "an unreadable lock table held a new edit, so writes never resume");
}

void TestMainWindow::aRejectedWriteKeepsEarlierUndoHistory()
{
    // revertPendingTagChange() used to undo the failed command and then CLEAR
    // the whole stack, so one rejected write threw away every undo step the
    // user had built up. Undoing the failed command is enough: it is already
    // off the stack afterwards.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);
    auto *flag = window.findChild<QAction *>(QStringLiteral("flag"));
    QVERIFY(flag);
    auto *archive = window.findChild<QAction *>(QStringLiteral("archive"));
    QVERIFY2(archive, "no archive action registered");

    model->appendBatch({ makeThread(QStringLiteral("t1"),
                                    { QStringLiteral("inbox") }) });
    selectThreadRow(view, 0);

    // One edit that succeeds, so there is history worth keeping.
    archive->trigger();
    TagChange applied;
    applied.messageIds = { QStringLiteral("m1") };
    applied.removed = { QStringLiteral("inbox") };
    applied.description = QStringLiteral("Archive");
    QVERIFY(QMetaObject::invokeMethod(&window, "onTagsApplied",
                                      Q_ARG(TagChange, applied)));

    // A second edit that the worker rejects outright.
    flag->trigger();
    QVERIFY(QMetaObject::invokeMethod(
        &window, "onWorkerError",
        Q_ARG(QString, QStringLiteral("Cannot resolve threads"))));

    QVERIFY2(window.canUndo(),
             "a rejected write cleared the undo history of edits that had "
             "already succeeded");
}

// Item 54. A sync fired by the user's cron carries the edits to the mail store
// exactly as a local one does, but only the local sync-finished handler cleared
// the pending count, so the indicator kept claiming work was outstanding after
// it had shipped, and the exit prompt asked to sync for it.
//
// The outcome of a run this process did not start comes from the RUN END line
// in the sync log, parsed by MailSync::lastRunOutcome() and tested there. These
// tests are about what MainWindow does with each answer.

namespace {

/// Writes a config naming \p logPath as the sync log, and loads it.
///
/// The log path has to come from config rather than the real
/// ~/.local/state/mailsync.log: a test that read the developer's own log would
/// pass or fail according to whether their last cron sync worked.
void loadConfigWithSyncLog(Config &config, const QTemporaryDir &dir,
                           const QString &logPath)
{
    const QString path = dir.filePath(QStringLiteral("qtmaildir.conf"));
    QFile file(path);
    QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
    file.write(QStringLiteral("[sync]\nlog=%1\n").arg(logPath).toUtf8());
    file.close();

    config.load(path);
    QCOMPARE(config.syncLog(), logPath);
}

/// Writes a sync log whose last completed run ended with \p status.
void writeSyncLog(const QString &path, const QString &status)
{
    QFile file(path);
    QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
    file.write(QStringLiteral("===== RUN END: 2026-08-09T10:20:03+02:00  status=%1 "
                              "=====\n").arg(status).toUtf8());
    file.close();
}

/// Records one confirmed edit, the way onTagsApplied() does for a real write.
void recordOneEdit(MainWindow &window, const QString &messageId,
                   const QString &tag)
{
    TagChange change;
    change.messageIds = { messageId };
    change.added = { tag };
    change.description = QStringLiteral("Flag");
    QVERIFY(QMetaObject::invokeMethod(&window, "onTagsApplied",
                                      Q_ARG(TagChange, change)));
}

/// Drives a complete external sync: the lock appears, then it is released.
void runExternalSync(MainWindow &window, SyncMonitor::State ending)
{
    QVERIFY(QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                                      Q_ARG(SyncMonitor::State,
                                            SyncMonitor::State::Running)));
    QVERIFY(QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                                      Q_ARG(SyncMonitor::State, ending)));
}

} // namespace

void TestMainWindow::aSuccessfulCronSyncClearsThePendingCount()
{
    // The reported defect: edits applied, cron syncs, indicator still says N.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    const QString logPath = dir.filePath(QStringLiteral("mailsync.log"));
    writeSyncLog(logPath, QStringLiteral("OK"));

    Config config;
    loadConfigWithSyncLog(config, dir, logPath);
    MainWindow window(config);

    auto *label = window.findChild<QLabel *>(QStringLiteral("pendingEdits"));
    QVERIFY(label);

    recordOneEdit(window, QStringLiteral("m1"), QStringLiteral("flagged"));
    QVERIFY2(!label->isHidden(), "the edit was not counted at all");

    runExternalSync(window, SyncMonitor::State::Idle);

    QVERIFY2(label->isHidden(),
             qPrintable(QStringLiteral("a successful cron sync left the "
                                       "indicator showing '%1'")
                            .arg(label->text())));
}

void TestMainWindow::aFailedCronSyncLeavesThePendingCount()
{
    // The rule the local path already follows: only a SUCCESSFUL sync clears
    // the count. Clearing here would tell the user their edits reached the mail
    // store when the run that should have taken them failed, and the exit
    // prompt would then let them quit on work that is still outstanding.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    const QString logPath = dir.filePath(QStringLiteral("mailsync.log"));
    writeSyncLog(logPath, QStringLiteral("FAILED  mbsync=1 notmuch=0"));

    Config config;
    loadConfigWithSyncLog(config, dir, logPath);
    MainWindow window(config);

    auto *label = window.findChild<QLabel *>(QStringLiteral("pendingEdits"));
    QVERIFY(label);

    recordOneEdit(window, QStringLiteral("m1"), QStringLiteral("flagged"));
    runExternalSync(window, SyncMonitor::State::Idle);

    QVERIFY2(!label->isHidden(),
             "a FAILED cron sync cleared the pending count, claiming edits "
             "reached the mail store when the sync that carries them failed");
}

void TestMainWindow::anUnreadableSyncLogLeavesThePendingCount()
{
    // No log at all: SyncOutcome::Unknown. Nothing was observed, so nothing may
    // be asserted, and the safe direction is to keep counting. Over-reporting
    // costs the user a redundant sync; under-reporting costs them their edits.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    const QString logPath = dir.filePath(QStringLiteral("absent.log"));
    QVERIFY(!QFileInfo::exists(logPath));

    Config config;
    loadConfigWithSyncLog(config, dir, logPath);
    MainWindow window(config);

    auto *label = window.findChild<QLabel *>(QStringLiteral("pendingEdits"));
    QVERIFY(label);

    recordOneEdit(window, QStringLiteral("m1"), QStringLiteral("flagged"));
    runExternalSync(window, SyncMonitor::State::Idle);

    QVERIFY2(!label->isHidden(),
             "an unreadable sync log cleared the pending count on no evidence");
}

void TestMainWindow::anUnknownExternalStateClearsNothing()
{
    // State::Unknown means /proc/locks could not be read, so no sync was
    // observed finishing. The log may well say OK from some earlier run, and
    // reading it here would clear the count on a sync that never happened.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    const QString logPath = dir.filePath(QStringLiteral("mailsync.log"));
    writeSyncLog(logPath, QStringLiteral("OK"));

    Config config;
    loadConfigWithSyncLog(config, dir, logPath);
    MainWindow window(config);

    auto *label = window.findChild<QLabel *>(QStringLiteral("pendingEdits"));
    QVERIFY(label);

    recordOneEdit(window, QStringLiteral("m1"), QStringLiteral("flagged"));
    runExternalSync(window, SyncMonitor::State::Unknown);

    QVERIFY2(!label->isHidden(),
             "an Unknown lock state cleared the pending count from a log line "
             "written by an earlier run");
}

void TestMainWindow::aSuccessfulCronSyncDrainsTheEditedAccounts()
{
    // The same defect in item 49's state, and invisible in the indicator: the
    // count can reach zero while the account set stays full, in which case the
    // next manual sync runs channels that have nothing to carry. Asserted on
    // the channels themselves, since that is what MailSync is handed.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    const QString logPath = dir.filePath(QStringLiteral("mailsync.log"));
    writeSyncLog(logPath, QStringLiteral("OK"));

    const QString confPath = dir.filePath(QStringLiteral("qtmaildir.conf"));
    QFile conf(confPath);
    QVERIFY(conf.open(QIODevice::WriteOnly | QIODevice::Text));
    conf.write(QStringLiteral("[sync]\nlog=%1\n\n"
                              "[account.work]\n"
                              "maildir=work-mail\n"
                              "channel=work-channel\n")
                   .arg(logPath).toUtf8());
    conf.close();

    Config config;
    config.load(confPath);
    QCOMPARE(config.accounts().size(), 1);

    MainWindow window(config);

    // One thread carrying the work account's tag, so sendThreadTagChange() can
    // resolve an account key from it the way it does for a real edit.
    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    ThreadSummary thread;
    thread.threadId = QStringLiteral("t1");
    thread.subject = QStringLiteral("Subject");
    thread.tags = { QStringLiteral("account-work"), QStringLiteral("inbox") };
    model->appendBatch({ thread });
    QCOMPARE(model->accountKeysForThread(QStringLiteral("t1")),
             QStringList{ QStringLiteral("work") });

    QVERIFY(QMetaObject::invokeMethod(
        &window, "sendThreadTagChange",
        Q_ARG(QStringList, QStringList{ QStringLiteral("t1") }),
        Q_ARG(QStringList, QStringList{ QStringLiteral("flagged") }),
        Q_ARG(QStringList, QStringList{}),
        Q_ARG(QString, QStringLiteral("Flag"))));

    // The guard: the account really is recorded, so the assertion below is
    // about draining it rather than about it never having been there.
    QStringList channels;
    QVERIFY(QMetaObject::invokeMethod(&window, "pendingSyncChannels",
                                      Q_RETURN_ARG(QStringList, channels)));
    QCOMPARE(channels, QStringList{ QStringLiteral("work-channel") });

    runExternalSync(window, SyncMonitor::State::Idle);

    QVERIFY(QMetaObject::invokeMethod(&window, "pendingSyncChannels",
                                      Q_RETURN_ARG(QStringList, channels)));
    QVERIFY2(channels.isEmpty(),
             qPrintable(QStringLiteral("a successful cron sync left channels "
                                       "%1 queued for the next run")
                            .arg(channels.join(QLatin1Char(',')))));
}

void TestMainWindow::aCronSyncDoesNotClearAnEditMadeWhileItRan()
{
    // The race the local path solves by snapshotting before the flush. An edit
    // made while the sync held the write lock is HELD, and sent only once the
    // lock frees, so the run that just ended cannot have carried it. Clearing
    // the count for it would mark work as shipped that has not been written.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    const QString logPath = dir.filePath(QStringLiteral("mailsync.log"));
    writeSyncLog(logPath, QStringLiteral("OK"));

    Config config;
    loadConfigWithSyncLog(config, dir, logPath);
    MainWindow window(config);

    auto *label = window.findChild<QLabel *>(QStringLiteral("pendingEdits"));
    QVERIFY(label);

    // The sync starts, then the user edits while it is running.
    QVERIFY(QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                                      Q_ARG(SyncMonitor::State,
                                            SyncMonitor::State::Running)));
    recordOneEdit(window, QStringLiteral("m1"), QStringLiteral("flagged"));
    QVERIFY2(!label->isHidden(), "the edit was not counted at all");

    QVERIFY(QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                                      Q_ARG(SyncMonitor::State,
                                            SyncMonitor::State::Idle)));

    // onTagsApplied() ran while the lock was held, so this edit reached the
    // INDEX during the sync. Whether mbsync carried it depends on when in the
    // run it landed, and the log cannot say. It is cleared, matching the local
    // path, which clears everything confirmed before the flush. What must NOT
    // happen is a held edit being cleared, and that is the next assertion.
    QVERIFY(label->isHidden());

    // A second sync, with an edit held across it: aSyncHoldsTheWriteLock() is
    // false here with no lock file, so this documents the reachable half. The
    // held-edit path has its own coverage in aHeldEditCountsAsUnsynced().
    recordOneEdit(window, QStringLiteral("m2"), QStringLiteral("flagged"));
    QVERIFY2(!label->isHidden(),
             "an edit made after the sync ended was swallowed by it");
}

// Items 56 and 57.

void TestMainWindow::everyActionIsReachableFromAMenu()
{
    // The fourth registration site nothing enforced. CLAUDE.md says adding an
    // action is four places: knownActions(), defaultBindings(), the icon table
    // and the action itself. It is FIVE, and the fifth is a menu.
    //
    // Found the hard way on the trash branch: `restore` shipped keyboard-only,
    // reachable by a chord and by nothing a user could see or discover, and no
    // test noticed. The three existing coverage tests each assert a different
    // property and all three pass against an action that appears nowhere in
    // the interface.
    //
    // The MENU rather than the toolbar, since the toolbar is a small
    // deliberate subset and always will be. Every menu is walked, submenus
    // included, because the five whole-thread actions live only in the "Whole
    // thread" submenu.
    const Config config;
    MainWindow window(config);

    auto *bar = window.menuBar();
    QVERIFY(bar);

    QSet<QAction *> reachable;
    QList<QMenu *> pending;
    const auto topLevel = bar->actions();
    for (QAction *action : topLevel) {
        if (action->menu())
            pending.append(action->menu());
    }
    QVERIFY2(!pending.isEmpty(), "the menu bar holds no menus");

    while (!pending.isEmpty()) {
        QMenu *menu = pending.takeFirst();
        const auto entries = menu->actions();
        for (QAction *entry : entries) {
            if (QMenu *sub = entry->menu()) {
                pending.append(sub);
                // An action owning a menu emits no `triggered`, so it is the
                // submenu that makes its children reachable and never the
                // parent entry itself. Not counted as reachable.
                continue;
            }
            reachable.insert(entry);
        }
    }

    // The guard, before anything is asserted about what is missing: a walk
    // that found nothing would report every action as unreachable and read as
    // a catastrophic regression rather than as a broken probe.
    QVERIFY2(reachable.size() > 10,
             qPrintable(QStringLiteral("the menu walk found only %1 entries")
                            .arg(reachable.size())));

    QStringList unreachable;
    for (const QString &name : KeyMap::knownActions()) {
        auto *action = window.findChild<QAction *>(name);
        QVERIFY2(action, qPrintable(QStringLiteral("no action named %1").arg(name)));
        if (!reachable.contains(action))
            unreachable.append(name);
    }

    QVERIFY2(unreachable.isEmpty(),
             qPrintable(QStringLiteral("%1 action(s) reach no menu, so they "
                                       "exist only for whoever already knows "
                                       "the chord: %2")
                            .arg(unreachable.size())
                            .arg(unreachable.join(QStringLiteral(", ")))));
}

void TestMainWindow::noMenuHasTwoEntriesSharingAMnemonic()
{
    // The sibling of everyActionIsReachableFromAMenu(), and it exists because
    // the rule it enforces had lived only in prose and in one other test's
    // COMMENT, and was duly broken the first time a batch of entries was added
    // to a menu (item 123: `&Reply` against the pre-existing `&Restore from
    // trash`, both Alt+R).
    //
    // Qt does not error on a duplicate mnemonic. It CYCLES between the
    // colliding entries instead of activating either, so the key silently
    // stops working and merely moves a highlight. That is worse than it
    // sounds in the Message menu, where `restore` is deliberately greyed
    // outside the trash view: the ordinary case was pressing Alt+R and landing
    // on a disabled entry.
    //
    // Item 57 already decided this is a property rather than a taste. It
    // rejected the label "Starred" for `flag` precisely because it would have
    // collided with `Mark &spam`, and theImportantActionIsLabelledImportant()
    // pins the surviving label with that reasoning in its comment. A decision
    // recorded only in prose is one nobody re-derives.
    //
    // Scoped PER MENU, which is what the collision actually is: a mnemonic is
    // resolved among the entries of the menu that is open, so the same letter
    // in File and in View is not a conflict.
    const Config config;
    MainWindow window(config);

    auto *bar = window.menuBar();
    QVERIFY(bar);

    // The menu bar's own top-level titles are one such scope too, so the walk
    // starts by treating the bar as a menu and then descends.
    QList<QPair<QString, QList<QAction *>>> scopes;
    scopes.append({ QStringLiteral("the menu bar"), bar->actions() });

    QList<QMenu *> pending;
    const auto topLevel = bar->actions();
    for (QAction *action : topLevel) {
        if (action->menu())
            pending.append(action->menu());
    }
    QVERIFY2(!pending.isEmpty(), "the menu bar holds no menus");

    while (!pending.isEmpty()) {
        QMenu *menu = pending.takeFirst();
        const auto entries = menu->actions();
        scopes.append({ menu->title(), entries });
        for (QAction *entry : entries) {
            if (QMenu *sub = entry->menu())
                pending.append(sub);
        }
    }

    // The four collisions that PREDATE this test, measured by running it
    // against the tree before item 123 touched any label. They are listed
    // rather than fixed, and rather than being hidden by narrowing the test,
    // because renaming a shipped menu entry is the user's call and not a
    // test's: three of them are in menus a user has had in their fingers
    // since 0.1.0.
    //
    // Listed as exact pairs, not as "ignore Alt+R", so this is a freeze and
    // not an amnesty: a NEW entry colliding on any of these same keys still
    // fails, because its pair is not on this list. Fixing one is then a
    // one-line deletion here, which is the point of writing them out.
    // Written as the FULL GROUP of labels sharing one key in one menu, not as
    // a pair. A pair is keyed on which entry the walk happened to see first,
    // so adding a colliding entry ABOVE a frozen one silently re-pairs it and
    // the new defect gets reported as "a frozen collision no longer happens",
    // which names the wrong thing entirely. Measured: reinstating `&Reply`
    // did exactly that before this was changed. A group is order-independent,
    // so a new entry grows the group and fails as a new collision.
    static const QStringList knownPreExistingCollisions = {
        QStringLiteral("&Message: Alt+R shared by \"&Restore from trash\", \"Mark all &read\", \"Tagging &rules...\""),
        QStringLiteral("&Message: Alt+S shared by \"Mark &spam\", \"Find &stranded deleted mail\""),
        QStringLiteral("&View: Alt+O shared by \"&Open thread\", \"Zoom &out\""),
    };

    QStringList collisions;
    int compared = 0;

    for (const auto &scope : scopes) {
        // Keyed on the mnemonic Qt itself derives, not on a hand-parsed '&'.
        // The question is which key Qt will dispatch, and only Qt answers it:
        // "&&" is a literal ampersand and carries no mnemonic at all.
        //
        // A QMap rather than a QHash so the groups come out in a stable key
        // order, which is what lets the frozen list above be written once and
        // stay matching.
        QMap<QString, QStringList> byMnemonic;
        for (QAction *entry : scope.second) {
            if (entry->isSeparator())
                continue;
            const QKeySequence mnemonic = QKeySequence::mnemonic(entry->text());
            if (mnemonic.isEmpty())
                continue;
            ++compared;
            byMnemonic[mnemonic.toString(QKeySequence::NativeText)]
                .append(QStringLiteral("\"%1\"").arg(entry->text()));
        }

        for (auto it = byMnemonic.cbegin(); it != byMnemonic.cend(); ++it) {
            if (it.value().size() < 2)
                continue;
            // Names the menu, the key and EVERY label in the group, so a
            // future failure says what to rename without anyone going looking.
            collisions.append(QStringLiteral("%1: %2 shared by %3")
                                  .arg(scope.first, it.key(),
                                       it.value().join(QStringLiteral(", "))));
        }
    }

    // The guard, and it is not ceremonial: every assertion below is a loop
    // that reports success when it runs zero times. A walk that found no
    // mnemonics at all would pass this test against any label whatsoever.
    QVERIFY2(compared > 20,
             qPrintable(QStringLiteral("only %1 menu entries carried a "
                                       "mnemonic, so this probe measured "
                                       "almost nothing")
                            .arg(compared)));

    // Matched on the menu and key only, with the labels compared separately
    // below. Comparing whole strings made a GROWING group read as a frozen one
    // disappearing: adding `&Reply` took Alt+R from three labels to four, the
    // frozen three-label string stopped matching, and the failure said "this
    // collision no longer happens" about the very key that had just got worse.
    // Measured twice, once per attempt, which is why the two questions are
    // asked separately.
    const auto scopeAndKey = [](const QString &collision) {
        return collision.left(collision.indexOf(QStringLiteral(" shared by ")));
    };

    QHash<QString, QString> frozen;
    for (const QString &known : knownPreExistingCollisions)
        frozen.insert(scopeAndKey(known), known);

    QStringList unexpected;
    QSet<QString> stillPresent;
    for (const QString &collision : collisions) {
        const QString key = scopeAndKey(collision);
        const auto known = frozen.constFind(key);
        if (known == frozen.constEnd()) {
            // A collision on a key nothing froze: entirely new.
            unexpected.append(collision);
            continue;
        }
        stillPresent.insert(key);
        if (*known != collision) {
            // The key was already colliding, but the CAST has changed, which
            // for a frozen entry means an entry joined it. Reported as the
            // new collision it is, naming both what was frozen and what is
            // there now.
            unexpected.append(
                QStringLiteral("%1 (frozen as [%2], now [%3])")
                    .arg(key, *known, collision));
        }
    }

    // A frozen entry that has since been FIXED must not stay on the list
    // silently, or the list becomes a place stale claims accumulate.
    QStringList stale;
    for (const QString &known : knownPreExistingCollisions) {
        if (!stillPresent.contains(scopeAndKey(known)))
            stale.append(known);
    }
    QVERIFY2(stale.isEmpty(),
             qPrintable(QStringLiteral("%1 frozen collision(s) no longer "
                                       "happen, so delete them from "
                                       "knownPreExistingCollisions: %2")
                            .arg(stale.size())
                            .arg(stale.join(QStringLiteral("; ")))));

    QVERIFY2(unexpected.isEmpty(),
             qPrintable(QStringLiteral("%1 menu mnemonic collision(s), where "
                                       "Qt cycles the highlight instead of "
                                       "activating: %2")
                            .arg(unexpected.size())
                            .arg(unexpected.join(QStringLiteral("; ")))));
}

void TestMainWindow::everyActionCarriesAnIcon()
{
    // Item 56. The complaint was inconsistency, not absence: eight actions had
    // themed icons and the other sixteen had none, so adjacent entries in one
    // menu disagreed, and the toolbar's TextBesideIcon style laid out an empty
    // slot for each of the sixteen.
    //
    // What this test can and cannot prove is worth stating, because it is
    // weaker than it looks. QIcon::fromTheme() resolves against the icon theme
    // of the machine running the test, so a PASS says "this desktop has art for
    // every name assigned", not "every name is correct" and not "the icon suits
    // the action". A machine with a sparse theme fails this through no fault of
    // the code. It is still worth having: it catches the actual regression,
    // which is an action registered with no name assigned at all.
    const Config config;
    MainWindow window(config);

    // The guard. Without it a MainWindow that registered nothing would pass an
    // empty loop, which is the classic way a "for each" assertion goes vacuous.
    const QList<QAction *> actions =
        window.findChildren<QAction *>(QString(), Qt::FindDirectChildrenOnly);
    QVERIFY2(actions.size() >= KeyMap::knownActions().size(),
             qPrintable(QStringLiteral("expected at least %1 actions, found %2")
                            .arg(KeyMap::knownActions().size())
                            .arg(actions.size())));

    QStringList missing;
    for (const QString &name : KeyMap::knownActions()) {
        auto *action = window.findChild<QAction *>(name);
        QVERIFY2(action, qPrintable(QStringLiteral("no action named %1").arg(name)));
        if (action->icon().isNull())
            missing.append(name);
    }

    QVERIFY2(missing.isEmpty(),
             qPrintable(QStringLiteral("%1 action(s) have no icon: %2")
                            .arg(missing.size())
                            .arg(missing.join(QStringLiteral(", ")))));
}

void TestMainWindow::theToolbarDoesNotOverrideTheDesktopButtonStyle()
{
    // The second half of the user's note: "Buttons should honor the 'Icon only'
    // option". They cannot while the toolbar asserts its own style. Qt takes
    // the desktop's preference from the platform theme and exposes it as
    // SH_ToolButtonStyle; a hardcoded setToolButtonStyle() overrides it, so the
    // user's setting has no effect whatever it is set to.
    const Config config;
    MainWindow window(config);

    auto *toolBar = window.findChild<QToolBar *>(QStringLiteral("main_toolbar"));
    QVERIFY(toolBar);

    const auto expected = static_cast<Qt::ToolButtonStyle>(
        window.style()->styleHint(QStyle::SH_ToolButtonStyle, nullptr, toolBar));

    QCOMPARE(toolBar->toolButtonStyle(), expected);
}

void TestMainWindow::theImportantActionIsLabelledImportant()
{
    // Item 57. The user picked "Important" over "Starred": the Message menu
    // already has `Mark &spam`, so "Starred" would have had to take an
    // accelerator from inside the word, while "Important" takes a free &I.
    const Config config;
    MainWindow window(config);

    // The action NAME is unchanged on purpose. It is the key a user writes in
    // the config's [keys] section, so renaming it would silently break every
    // existing binding for a change that is only about wording.
    auto *action = window.findChild<QAction *>(QStringLiteral("flag"));
    QVERIFY(action);

    QVERIFY2(action->text().contains(QStringLiteral("Important")),
             qPrintable(QStringLiteral("the action still reads '%1'")
                            .arg(action->text())));
    QVERIFY2(!action->text().contains(QStringLiteral("Flag")),
             qPrintable(QStringLiteral("the action still reads '%1'")
                            .arg(action->text())));

    // The accelerator the item chose, and the reason "Starred" was rejected.
    QCOMPARE(action->text(), QStringLiteral("&Important"));
}

void TestMainWindow::theImportantActionStillWritesTheFlaggedTag()
{
    // The rename is a LABEL change and must not reach the mail store. `flagged`
    // is a notmuch tag: neomutt reads it, the user's saved queries match on it,
    // ThreadSummary::isFlagged() tests for it and TagColors colours it. A
    // rename that followed the label through to the tag would rewrite the store
    // and desynchronise every other tool that reads the same Maildir.
    //
    // Asserted on the ids and tags actually sent to the worker, which is the
    // only place the distinction is observable.
    const Config config;
    MainWindow window(config);

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<QTreeView *>();
    QVERIFY(view);

    model->appendBatch({ makeThread(QStringLiteral("t1"),
                                    { QStringLiteral("inbox") }) });

    // The guard: the thread must NOT already carry the tag, or the assertion
    // below would pass against an action that did nothing at all.
    QVERIFY(!model->threadAt(0).isFlagged());

    selectThreadRow(view, 0);

    auto *action = window.findChild<QAction *>(QStringLiteral("flag"));
    QVERIFY(action);
    action->trigger();

    // Asserted on the CHANGE that was sent rather than on the thread's row.
    // Since item 108 this action is message-scoped, so it writes to the
    // message the card displays and the thread summary is deliberately left
    // alone. The tag name is what this test is about, and the change carries
    // it whichever scope the action uses.
    const TagChange sent = window.pendingChangeForTesting();
    QVERIFY2(sent.added.contains(QStringLiteral("flagged")),
             "the renamed action no longer writes the `flagged` tag");
    QVERIFY2(!sent.added.contains(QStringLiteral("important")),
             "the rename reached the mail store: an `important` tag was "
             "written, which no other tool reading this Maildir knows");
    QVERIFY2(sent.removed.isEmpty(),
             "marking important removed a tag, which it must not");
}

void TestMainWindow::theToolbarUsesTheConfiguredIconSize()
{
    // With the toolbar following the desktop's "Icon only" style, the icons are
    // the whole control, and this style reports 16px, which is a small target.
    // The size is configurable with a 24px default; this proves the config
    // value actually reaches the widget rather than sitting in Config unread.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    const QString path = dir.filePath(QStringLiteral("qtmaildir.conf"));
    QFile file(path);
    QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
    file.write("[general]\ntoolbar_icon_size = 40\n");
    file.close();

    Config config;
    config.load(path);
    QCOMPARE(config.toolbarIconSize(), 40);

    MainWindow window(config);
    auto *toolBar = window.findChild<QToolBar *>(QStringLiteral("main_toolbar"));
    QVERIFY(toolBar);

    // 40 is deliberately not any of this style's own metrics (16 small, 32
    // large), so the assertion cannot pass by the widget happening to agree
    // with the theme.
    QCOMPARE(toolBar->iconSize(), QSize(40, 40));
}

namespace {

/// A config whose accounts carry the given maildir/sent pairs. An empty `sent`
/// writes no key at all, which is the account-without-a-sent-folder case.
QString writeSentConfig(const QTemporaryDir &dir,
                        const QList<QPair<QString, QString>> &accounts,
                        const QString &startupQuery = QString())
{
    const QString path = dir.filePath(QStringLiteral("qtmaildir.conf"));
    QSettings s(path, QSettings::IniFormat);
    // [general] keys are read WITHOUT the prefix: QSettings' INI backend treats
    // a section literally named [general] as its own fallback section and
    // strips it, so setValue("general/startup_query") would write a key nothing
    // reads.
    if (!startupQuery.isEmpty())
        s.setValue(QStringLiteral("startup_query"), startupQuery);
    for (const auto &account : accounts) {
        s.beginGroup(QStringLiteral("account.") + account.first);
        s.setValue(QStringLiteral("maildir"), account.first);
        if (!account.second.isEmpty())
            s.setValue(QStringLiteral("sent"), account.second);
        s.endGroup();
    }
    s.sync();
    return path;
}

}  // namespace

void TestMainWindow::thereIsNoSentButtonWithoutASentKey()
{
    // Hidden entirely rather than present and finding nothing. An account may
    // legitimately keep no sent mail locally, and a button that always returns
    // an empty list reads as a broken feature rather than an absent one.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    config.load(writeSentConfig(dir, {{QStringLiteral("provider-c"), {}}}));
    QVERIFY(config.allSentQuery().isEmpty());

    MainWindow window(config);
    QVERIFY(!window.findChild<QAbstractButton *>(QStringLiteral("sentButton")));
}

void TestMainWindow::theSentButtonRunsEveryConfiguredAccount()
{
    // The button composes its query rather than storing one, which is the whole
    // reason it is not a [queries] entry: a saved query is a fixed string and
    // would not gain the third account here without the user editing it.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    config.load(writeSentConfig(dir, {
        {QStringLiteral("webmail-primary"), QStringLiteral("Sent")},
        {QStringLiteral("provider-c"), {}},
        {QStringLiteral("webmail-secondary"), QStringLiteral("Sent")},
    }));

    MainWindow window(config);
    auto *button = window.findChild<QAbstractButton *>(QStringLiteral("sentButton"));
    QVERIFY(button);

    auto *queryEdit = window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(queryEdit);

    button->click();
    const QString query = queryEdit->text();

    QVERIFY(query.contains(QStringLiteral("webmail-primary/Sent")));
    QVERIFY(query.contains(QStringLiteral("webmail-secondary/Sent")));

    // The account with no key contributes nothing, and leaves no bare "or"
    // behind: notmuch accepts that and silently returns a different result.
    QVERIFY(!query.contains(QStringLiteral("provider-c")));
    QVERIFY(!query.contains(QStringLiteral("or  or")));
    QCOMPARE(query.count(QStringLiteral(" or ")), 1);
}

void TestMainWindow::theSentButtonSurvivesABracketedPath()
{
    // A real provider nests its sent folder under a bracketed parent, and "["
    // and "]" are Xapian syntax. The quoting has to survive the trip from the
    // config through Account::sentQuery() into the query bar; unquoted, the
    // query looks plausible and matches nothing.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    config.load(writeSentConfig(dir, {
        {QStringLiteral("provider-a"), QStringLiteral("[Provider]/Posta inviata")},
    }));

    MainWindow window(config);
    auto *button = window.findChild<QAbstractButton *>(QStringLiteral("sentButton"));
    QVERIFY(button);
    auto *queryEdit = window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(queryEdit);

    button->click();
    QCOMPARE(queryEdit->text(),
             QStringLiteral("path:\"provider-a/[Provider]/Posta inviata/**\""));
}

namespace {

/// Writes a config whose accounts carry a sent folder, a drafts folder, or
/// neither. Separate from writeSentConfig() because the two keys are
/// independent: the interesting cases here are exactly the ones where an
/// account has one and not the other.
struct FolderAccount {
    QString maildir;
    QString sent;
    QString drafts;
};

QString writeFolderConfig(const QTemporaryDir &dir,
                          const QList<FolderAccount> &accounts)
{
    const QString path = dir.filePath(QStringLiteral("qtmaildir.conf"));
    QSettings s(path, QSettings::IniFormat);
    for (const FolderAccount &account : accounts) {
        s.beginGroup(QStringLiteral("account.") + account.maildir);
        s.setValue(QStringLiteral("maildir"), account.maildir);
        if (!account.sent.isEmpty())
            s.setValue(QStringLiteral("sent"), account.sent);
        if (!account.drafts.isEmpty())
            s.setValue(QStringLiteral("drafts"), account.drafts);
        s.endGroup();
    }
    s.sync();
    return path;
}

/// The label of the helper whose query is `query`, or a null string.
QString labelForQuery(const QList<HtmlBuilder::PlaceholderHelper> &helpers,
                      const QString &query)
{
    for (const HtmlBuilder::PlaceholderHelper &helper : helpers) {
        if (helper.query == query)
            return helper.label;
    }
    return QString();
}

}  // namespace

void TestMainWindow::placeholderCountsSkipSentAndDraftsWithoutTheKeys()
{
    // The tag: lines are unconditional, the folder lines are not. An account
    // with no sent or drafts folder must contribute no line at all rather than
    // a line reading 0: item 63 established that a missing folder is a real
    // configuration, and "0 sent" claims the user has sent nothing.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    config.load(writeFolderConfig(dir, {{QStringLiteral("provider-c"), {}, {}}}));

    MainWindow window(config);

    const QStringList queries = window.placeholderQueriesForTesting();
    QCOMPARE(queries.size(), 3);
    QVERIFY(queries.contains(QStringLiteral("tag:unread")));
    QVERIFY(queries.contains(QStringLiteral("tag:flagged")));
    QVERIFY(queries.contains(QStringLiteral("tag:inbox")));
}

void TestMainWindow::placeholderCountsCarrySentAndDrafts()
{
    // Both composed from the folder keys rather than from a tag. `tag:draft`
    // counts 0 against the user's real database (measured 2026-08-11) and no
    // draft-ish tag exists in it at all, so a tag-based drafts line would be a
    // permanent zero that looks like working code.
    //
    // The account layout is the real one's shape: one account with both keys,
    // one with drafts and NO sent, which is what proves the two are collected
    // independently rather than per-account in one pass.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    config.load(writeFolderConfig(dir, {
        {QStringLiteral("webmail-primary"), QStringLiteral("Sent"),
         QStringLiteral("Drafts")},
        {QStringLiteral("provider-a"), {}, QStringLiteral("[Provider]/Bozze")},
    }));

    MainWindow window(config);

    const QStringList queries = window.placeholderQueriesForTesting();
    QCOMPARE(queries.size(), 5);

    // The quoting survives the trip, which is the trap this whole composition
    // exists for: unquoted, "[" and "]" are Xapian syntax and the count reads
    // 0 while looking like an empty folder.
    QVERIFY2(queries.contains(config.allDraftsQuery()),
             qPrintable(QStringLiteral("drafts query missing, got: %1")
                            .arg(queries.join(QStringLiteral(" | ")))));
    QVERIFY(queries.contains(config.allSentQuery()));
    QVERIFY(config.allDraftsQuery().contains(
        QStringLiteral("path:\"provider-a/[Provider]/Bozze/**\"")));

    // The sent term comes from the account that has one, and the drafts terms
    // from both. An implementation that emitted a folder line per account
    // would produce four folder queries instead of two.
    QCOMPARE(config.allSentQuery().count(QStringLiteral("path:")), 1);
    QCOMPARE(config.allDraftsQuery().count(QStringLiteral("path:")), 2);
}

void TestMainWindow::placeholderLabelsStayPairedWithTheirQueries()
{
    // The defect this guards is the one the old fixed array invited: the
    // labels were written positionally against a separate query array, so
    // inserting an entry in one and not the other put a real number against
    // the wrong name. Asserting the PAIRING rather than the order is what
    // survives a later reshuffle.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    config.load(writeFolderConfig(dir, {
        {QStringLiteral("webmail-primary"), QStringLiteral("Sent"),
         QStringLiteral("Drafts")},
    }));

    MainWindow window(config);

    const QStringList queries = window.placeholderQueriesForTesting();
    QCOMPARE(queries.size(), 5);

    // Distinct counts, so a label reading the wrong index cannot coincide with
    // the right answer. Positional against the queries the window just asked
    // for, which is exactly the contract requestCounts() replies under.
    QVector<int> counts;
    for (int i = 0; i < queries.size(); ++i)
        counts.append((i + 1) * 10);

    QMetaObject::invokeMethod(&window, "onCountsReady",
                              Q_ARG(QVector<int>, counts),
                              Q_ARG(quint64, window.countsGenerationForTesting()));

    const QList<HtmlBuilder::PlaceholderHelper> helpers =
        window.placeholderHelpersForTesting();

    for (int i = 0; i < queries.size(); ++i) {
        const QString label = labelForQuery(helpers, queries.at(i));
        QVERIFY2(!label.isNull(),
                 qPrintable(QStringLiteral("no helper for query '%1'")
                                .arg(queries.at(i))));
        QVERIFY2(label.contains(QString::number(counts.at(i))),
                 qPrintable(QStringLiteral("query '%1' was labelled '%2', which "
                                           "does not carry its own count %3")
                                .arg(queries.at(i), label)
                                .arg(counts.at(i))));
    }

    // And the folder lines say what they are, not "in inbox".
    QVERIFY(labelForQuery(helpers, config.allSentQuery())
                .contains(QStringLiteral("sent")));
    QVERIFY(labelForQuery(helpers, config.allDraftsQuery())
                .contains(QStringLiteral("draft")));
}

void TestMainWindow::placeholderCountsDropAnUncountableQuery()
{
    // The worker answers -1 for a query it could not count, rather than
    // skipping the entry, precisely so the positional pairing holds. The pane
    // must then drop that LINE rather than print a negative number, and drop
    // only that one.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    config.load(writeFolderConfig(dir, {
        {QStringLiteral("webmail-primary"), QStringLiteral("Sent"),
         QStringLiteral("Drafts")},
    }));

    MainWindow window(config);

    const QStringList queries = window.placeholderQueriesForTesting();
    QVector<int> counts;
    for (int i = 0; i < queries.size(); ++i)
        counts.append(i == 0 ? -1 : (i + 1) * 10);

    QMetaObject::invokeMethod(&window, "onCountsReady",
                              Q_ARG(QVector<int>, counts),
                              Q_ARG(quint64, window.countsGenerationForTesting()));

    const QList<HtmlBuilder::PlaceholderHelper> helpers =
        window.placeholderHelpersForTesting();

    QVERIFY(labelForQuery(helpers, queries.at(0)).isNull());
    for (int i = 1; i < queries.size(); ++i) {
        QVERIFY2(!labelForQuery(helpers, queries.at(i)).isNull(),
                 qPrintable(QStringLiteral("an uncountable query took '%1' "
                                           "down with it").arg(queries.at(i))));
    }
}

void TestMainWindow::flatModeDoesNotSurviveTheNextQuery()
{
    // The condition the user set for this feature: a flat Sent list is fine, a
    // flat anything-else is not. Asserted at the window rather than the model,
    // because the leak this guards against is in the WIRING, not in the model:
    // setFlatMode(true) from the button with no matching false anywhere else
    // passes every model test and flattens the app from the first Sent click
    // until it restarts.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    config.load(writeSentConfig(dir, {
        {QStringLiteral("webmail-primary"), QStringLiteral("Sent")},
    }));

    MainWindow window(config);
    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *button = window.findChild<QAbstractButton *>(QStringLiteral("sentButton"));
    QVERIFY(button);
    auto *queryEdit = window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(queryEdit);

    QVERIFY2(!model->flatMode(), "the model starts flat");

    button->click();
    QVERIFY2(model->flatMode(), "the Sent button did not flatten the list");

    // Any other query restores the tree. Typed by hand rather than through a
    // saved-query button, since that is the route with no flag of its own and
    // therefore the one most likely to be forgotten.
    queryEdit->setText(QStringLiteral("tag:inbox"));
    QMetaObject::invokeMethod(&window, "runCurrentQuery");
    QVERIFY2(!model->flatMode(),
             "flat mode survived into an ordinary query, so every view after "
             "one Sent click lost its replies");

    // And back, so the button still works after the round trip.
    button->click();
    QVERIFY(model->flatMode());

    // Even the SAME query typed by hand comes back as a tree: the flag follows
    // the button, not the text, which is the rule the user chose.
    queryEdit->setText(config.allSentQuery());
    QMetaObject::invokeMethod(&window, "runCurrentQuery");
    QVERIFY(!model->flatMode());
}

void TestMainWindow::noTwoActionsShareAnIcon()
{
    // Reported by the user against the icons shipped in 0.12.0: Archive and
    // Mark all read both used `mail-mark-read`. With the toolbar following a
    // desktop set to icon-only, the icon is the entire control, so two buttons
    // with different consequences were indistinguishable.
    //
    // Asserted over every action rather than that one pair, because the defect
    // is the class and not the instance: the icon table is hand-written and
    // twenty-four entries long, so the next duplicate is a plausible typo.
    //
    // Compared by cacheKey() rather than by the theme NAME, which this window
    // does not keep. Two distinct names that resolve to the same art on a given
    // theme are just as ambiguous on screen, and that is what the user sees.
    // Narrowed by item 108 to the actions that can reach the TOOLBAR, which is
    // where the rule comes from: an icon-only toolbar makes the icon the whole
    // control. The five whole-thread actions live only in the "Whole thread"
    // submenu, whose entries always carry text, and each deliberately shares
    // the icon of its message-scoped twin: same operation, wider scope, with
    // the words saying which. Giving them five invented shapes would be less
    // clear than the pairing.
    //
    // reply_no_quote joined them in item 123 for exactly the same reason: it
    // shares reply's icon, it is a menu entry that always carries its text,
    // and it is not on the toolbar. The list is therefore no longer only the
    // thread tier, which is why it is named for the PROPERTY that earns the
    // exemption rather than for the tier that first needed it.
    //
    // Named as an exception list rather than by asking the toolbar what it
    // holds, so that PUTTING one of these on the toolbar fails this test
    // rather than silently passing it.
    static const QStringList menuOnlySharedIconActions = {
        QStringLiteral("archive_thread"),
        QStringLiteral("delete_thread"),
        QStringLiteral("spam_thread"),
        QStringLiteral("toggle_unread_thread"),
        QStringLiteral("flag_thread"),
        QStringLiteral("reply_no_quote"),
    };

    const Config config;
    MainWindow window(config);

    // The exception must not become a hiding place: every one of them still
    // has to carry an icon, which everyActionCarriesAnIcon asserts, and none
    // may sit on the toolbar.
    auto *toolBar = window.findChild<QToolBar *>();
    QVERIFY(toolBar);
    for (const QString &name : menuOnlySharedIconActions) {
        auto *action = window.findChild<QAction *>(name);
        QVERIFY2(action, qPrintable(QStringLiteral("no action named %1").arg(name)));
        QVERIFY2(!toolBar->actions().contains(action),
                 qPrintable(QStringLiteral("%1 is on the toolbar, where a "
                                           "shared icon is ambiguous, so it "
                                           "cannot be exempt from this rule")
                                .arg(name)));
    }

    QHash<qint64, QString> owners;
    QStringList collisions;
    int withIcons = 0;
    int compared = 0;

    for (const QString &name : KeyMap::knownActions()) {
        auto *action = window.findChild<QAction *>(name);
        QVERIFY2(action, qPrintable(QStringLiteral("no action named %1").arg(name)));
        if (!action->icon().isNull())
            ++withIcons;
        if (menuOnlySharedIconActions.contains(name))
            continue;
        if (action->icon().isNull())
            continue;
        ++compared;

        const qint64 key = action->icon().cacheKey();
        const auto existing = owners.constFind(key);
        if (existing != owners.constEnd()) {
            collisions.append(QStringLiteral("%1 and %2")
                                  .arg(existing.value(), name));
        } else {
            owners.insert(key, name);
        }
    }

    // The guard. On a theme that resolves nothing every icon is null, the loop
    // body never runs, and the assertion below would pass having compared
    // nothing at all.
    QVERIFY2(withIcons >= KeyMap::knownActions().size(),
             qPrintable(QStringLiteral("only %1 of %2 actions had an icon to "
                                       "compare")
                            .arg(withIcons)
                            .arg(KeyMap::knownActions().size())));

    // And the exception list did not swallow the comparison itself.
    QCOMPARE(compared, KeyMap::knownActions().size()
                           - menuOnlySharedIconActions.size());

    QVERIFY2(collisions.isEmpty(),
             qPrintable(QStringLiteral("actions sharing one icon: %1")
                            .arg(collisions.join(QStringLiteral("; ")))));
}

// Constructing a MainWindow needs a QApplication and a platform plugin. The
// test has no display under ctest, so it runs offscreen unless the caller
// asked for something else.
int main(int argc, char *argv[])
{
    qputenv("QT_QPA_PLATFORM", qgetenv("QT_QPA_PLATFORM").isEmpty()
                                   ? QByteArray("offscreen")
                                   : qgetenv("QT_QPA_PLATFORM"));
    QApplication app(argc, argv);
    TestMainWindow test;
    return QTest::qExec(&test, argc, argv);
}

// ---------------------------------------------------------------------------
// Saved queries in the query row (item 23)
// ---------------------------------------------------------------------------

/// Writes a config plus a queries.json beside it, and loads both.
static void loadWithQueries(Config &config, QTemporaryDir &dir,
                            const QString &queriesJson,
                            const QString &iniExtra = {})
{
    QDir().mkpath(dir.filePath(QStringLiteral("qtmaildir")));
    const QString conf =
        dir.filePath(QStringLiteral("qtmaildir/qtmaildir.conf"));
    QFile ini(conf);
    ini.open(QIODevice::WriteOnly | QIODevice::Text);
    ini.write(iniExtra.toUtf8());
    ini.close();

    QFile json(dir.filePath(QStringLiteral("qtmaildir/queries.json")));
    json.open(QIODevice::WriteOnly);
    json.write(queriesJson.toUtf8());
    json.close();

    config.load(conf);
}

/// Buttons in the saved-query row, by label, in the order they are laid out.
/// Whether this is one of the four shipped filters rather than a saved query.
///
/// By object name, not by label: the labels are translated, and a user may name
/// their own query "Unread" too.
static bool isBuiltinFilterButton(QAbstractButton *button)
{
    for (const SavedQuery &filter : Config::builtinFilters()) {
        if (button->objectName() == filter.generated + QStringLiteral("Button"))
            return true;
    }
    return false;
}

static QStringList savedQueryButtonLabels(MainWindow &window)
{
    QStringList labels;
    auto *row = window.findChild<QWidget *>(QStringLiteral("savedQueryRow"));
    if (!row)
        return labels;
    // QAbstractButton, not QPushButton: the built-in filters are QToolButtons
    // so they can carry an icon beside their text, and findChildren on the
    // narrower type would silently skip them, leaving the filter below with
    // nothing to filter.
    const QList<QAbstractButton *> buttons =
        row->findChildren<QAbstractButton *>(QString(),
                                             Qt::FindDirectChildrenOnly);
    for (QAbstractButton *button : buttons) {
        // The menu button is not a saved query and must not be counted as one.
        if (button->objectName() == QStringLiteral("savedQueryMenuButton"))
            continue;
        // Neither are the four built-in filters (item 93), which sit first on
        // the row and are not in queries.json at all. Every caller of this
        // helper is asking about the USER's queries, so counting the filters
        // would make each of them assert on a number it does not care about.
        if (isBuiltinFilterButton(button))
            continue;
        labels.append(button->text());
    }
    return labels;
}

/// The user's own pinned button carrying `label`, or null.
///
/// Positional lookup does not work any more: the built-in filters occupy the
/// first four places on the row, so row->findChild<QPushButton *>() returns
/// Unread rather than the query a test means.
static QPushButton *savedQueryButton(MainWindow &window, const QString &label)
{
    auto *row = window.findChild<QWidget *>(QStringLiteral("savedQueryRow"));
    if (!row)
        return nullptr;
    const QList<QPushButton *> buttons =
        row->findChildren<QPushButton *>(QString(), Qt::FindDirectChildrenOnly);
    for (QPushButton *button : buttons) {
        if (isBuiltinFilterButton(button))
            continue;
        if (button->text() == label)
            return button;
    }
    return nullptr;
}

void TestMainWindow::onlyPinnedQueriesBecomeButtons()
{
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    loadWithQueries(config, dir, QStringLiteral(R"({
        "version": 1,
        "queries": [
            { "name": "Inbox", "query": "tag:inbox", "pinned": true },
            { "name": "Buried", "query": "tag:buried" }
        ]
    })"));

    MainWindow window(config);
    const QStringList labels = savedQueryButtonLabels(window);

    QVERIFY2(labels.contains(QStringLiteral("Inbox")),
             "a pinned query must have a button");
    QVERIFY2(!labels.contains(QStringLiteral("Buried")),
             "an unpinned query must NOT have a button");
}

void TestMainWindow::unpinnedQueriesReachTheMenu()
{
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    loadWithQueries(config, dir, QStringLiteral(R"({
        "version": 1,
        "queries": [
            { "name": "Inbox", "query": "tag:inbox", "pinned": true },
            { "name": "Buried", "query": "tag:buried" }
        ]
    })"));

    MainWindow window(config);
    auto *menuButton =
        window.findChild<QPushButton *>(QStringLiteral("savedQueryMenuButton"));
    QVERIFY2(menuButton, "an unpinned query needs a menu to live in");
    QVERIFY(menuButton->menu());

    QStringList entries;
    const QList<QAction *> actions = menuButton->menu()->actions();
    for (QAction *action : actions)
        entries.append(action->text());

    QVERIFY2(entries.contains(QStringLiteral("Buried")),
             "the unpinned query is missing from the menu");
    // A pinned query is already a button; listing it twice is the duplicate
    // this asserts against.
    QVERIFY2(!entries.contains(QStringLiteral("Inbox")),
             "a pinned query must not also appear in the menu");
}

/// The property the whole storage change was made for. "Zebra" is written
/// first and must stay first; alphabetical order would put it last.
void TestMainWindow::pinnedButtonsFollowTheDocumentOrder()
{
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    loadWithQueries(config, dir, QStringLiteral(R"({
        "version": 1,
        "queries": [
            { "name": "Zebra", "query": "tag:zebra", "pinned": true },
            { "name": "Apple", "query": "tag:apple", "pinned": true }
        ]
    })"));

    MainWindow window(config);
    const QStringList labels = savedQueryButtonLabels(window);

    QCOMPARE(labels.size(), 2);
    QCOMPARE(labels.at(0), QStringLiteral("Zebra"));
    QCOMPARE(labels.at(1), QStringLiteral("Apple"));
}

void TestMainWindow::theSavedQueryMenuIsHiddenWhenEveryQueryIsPinned()
{
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    loadWithQueries(config, dir, QStringLiteral(R"({
        "version": 1,
        "queries": [
            { "name": "Inbox", "query": "tag:inbox", "pinned": true }
        ]
    })"));

    MainWindow window(config);

    // The guard the assertion below needs. Asserting only that the menu button
    // is absent passed against NO implementation at all, before any of this
    // was built, so it has to prove first that the row it is looking in was
    // populated and that a button was found.
    const QStringList labels = savedQueryButtonLabels(window);
    QCOMPARE(labels, QStringList{ QStringLiteral("Inbox") });

    auto *menuButton =
        window.findChild<QPushButton *>(QStringLiteral("savedQueryMenuButton"));
    QVERIFY2(!menuButton,
             "an empty menu button is a control that always does nothing");
}

/// The scope goes through the account dropdown rather than being baked into
/// the query text. runQuery() already scopes by that dropdown, so pre-scoping
/// the text would apply the path twice, and the selection would be invisible.
void TestMainWindow::aScopedSavedQuerySelectsItsAccount()
{
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    loadWithQueries(config, dir, QStringLiteral(R"({
        "version": 1,
        "queries": [
            { "name": "Billing", "query": "from:billing",
              "account": "work", "pinned": true }
        ]
    })"), QStringLiteral(
        "[account.work]\n"
        "name=Test User\n"
        "address=user@example.org\n"
        "maildir=work-mail\n"
        "\n"
        "[account.personal]\n"
        "name=Test User\n"
        "address=me@example.net\n"
        "maildir=personal\n"
    ));

    MainWindow window(config);
    auto *accountBox =
        window.findChild<QComboBox *>(QStringLiteral("accountBox"));
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(accountBox);
    QVERIFY(queryEdit);

    // Start somewhere else, so a passing result cannot be the default.
    accountBox->setCurrentIndex(accountBox->findData(
        QStringLiteral("personal")));
    QCOMPARE(accountBox->currentData().toString(), QStringLiteral("personal"));

    auto *row = window.findChild<QWidget *>(QStringLiteral("savedQueryRow"));
    QVERIFY(row);
    auto *button = savedQueryButton(window, QStringLiteral("Billing"));
    QVERIFY(button);
    button->click();

    QCOMPARE(accountBox->currentData().toString(), QStringLiteral("work"));
    // The text is the bare query. The path scope is applied once, by
    // runQuery(), from the dropdown this just set.
    QCOMPARE(queryEdit->text(), QStringLiteral("from:billing"));
    QVERIFY2(!queryEdit->text().contains(QStringLiteral("path:")),
             "the scope must not be baked into the query text");
}

/// A query with no account must CLEAR the dropdown, not inherit whatever the
/// last one left there. Confirmed against the same defect in the rules
/// preview, where an already-selected account survived the click.
void TestMainWindow::anUnscopedSavedQueryClearsTheAccount()
{
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    loadWithQueries(config, dir, QStringLiteral(R"({
        "version": 1,
        "queries": [
            { "name": "Everywhere", "query": "tag:inbox", "pinned": true }
        ]
    })"), QStringLiteral(
        "[account.work]\n"
        "name=Test User\n"
        "address=user@example.org\n"
        "maildir=work-mail\n"
    ));

    MainWindow window(config);
    auto *accountBox =
        window.findChild<QComboBox *>(QStringLiteral("accountBox"));
    QVERIFY(accountBox);

    accountBox->setCurrentIndex(accountBox->findData(QStringLiteral("work")));
    QCOMPARE(accountBox->currentData().toString(), QStringLiteral("work"));

    auto *row = window.findChild<QWidget *>(QStringLiteral("savedQueryRow"));
    QVERIFY(row);
    auto *button = savedQueryButton(window, QStringLiteral("Everywhere"));
    QVERIFY(button);
    button->click();

    QVERIFY2(accountBox->currentData().toString().isEmpty(),
             "an unscoped saved query must clear the account selection");
}

void TestMainWindow::theSaveQueryActionIsDisabledOnAnEmptyQuery()
{
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    loadWithQueries(config, dir, QStringLiteral(R"({
        "version": 1, "queries": []
    })"));

    MainWindow window(config);
    auto *save = window.findChild<QAction *>(QStringLiteral("save_query"));
    QVERIFY2(save, "there is no way to save a query");

    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(queryEdit);

    queryEdit->clear();
    QVERIFY2(!save->isEnabled(),
             "saving an empty query would store a query that matches nothing");

    queryEdit->setText(QStringLiteral("tag:inbox"));
    QVERIFY2(save->isEnabled(), "a real query must be savable");

    // Whitespace is not a query. setText does not drive a completer, but it
    // does emit textChanged, which is what the enabling is hung on.
    queryEdit->setText(QStringLiteral("   "));
    QVERIFY(!save->isEnabled());
}

/// A menu entry and a shortcut are not a button. The spec asks for one beside
/// the query bar, and the user went looking for it there and did not find it.
void TestMainWindow::thereIsASaveButtonBesideTheQueryBar()
{
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    loadWithQueries(config, dir, QStringLiteral(R"({
        "version": 1, "queries": []
    })"));

    MainWindow window(config);
    auto *button =
        window.findChild<QAbstractButton *>(QStringLiteral("saveQueryButton"));
    QVERIFY2(button, "no Save query button beside the query bar");

    // Icon AND text. An icon alone was the first version and read as
    // ambiguous: "save" is a familiar shape whose meaning is always "save
    // what?".
    auto *toolButton = qobject_cast<QToolButton *>(button);
    QVERIFY(toolButton);
    QCOMPARE(toolButton->toolButtonStyle(), Qt::ToolButtonTextBesideIcon);
    QVERIFY2(!button->icon().isNull(), "the button has no icon");
    QVERIFY2(!button->text().isEmpty(), "the button has no text");

    // Button phrasing, not the menu's: no accelerator ampersand, and no
    // ellipsis. setDefaultAction copies the action's text, so this asserts the
    // override survived it.
    QVERIFY2(!button->text().contains(QLatin1Char('&')),
             "the menu accelerator leaked onto the button");
    QVERIFY2(!button->text().contains(QStringLiteral("...")),
             "the menu's ellipsis leaked onto the button");

    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(queryEdit);

    // In the query row itself, not somewhere else in the window that a
    // findChild would also reach.
    QCOMPARE(button->parentWidget(), queryEdit->parentWidget());

    // Follows the action, so it cannot offer to save an empty query while the
    // menu entry correctly refuses.
    queryEdit->clear();
    QVERIFY2(!button->isEnabled(),
             "the button must follow the action's enabled state");
    queryEdit->setText(QStringLiteral("tag:inbox"));
    QVERIFY(button->isEnabled());
}

/// Right-aligned, meaning a stretch sits between the buttons and the menu.
/// Asserted on the layout rather than on x coordinates: the offscreen platform
/// lays out widgets, but a geometry assertion here would also pass for a row
/// that simply ran out of width.
void TestMainWindow::theMenuIsRightAlignedAwayFromTheButtons()
{
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    loadWithQueries(config, dir, QStringLiteral(R"({
        "version": 1,
        "queries": [
            { "name": "Inbox", "query": "tag:inbox", "pinned": true },
            { "name": "Buried", "query": "tag:buried" }
        ]
    })"));

    MainWindow window(config);
    auto *row = window.findChild<QWidget *>(QStringLiteral("savedQueryRow"));
    QVERIFY(row);
    auto *box = qobject_cast<QHBoxLayout *>(row->layout());
    QVERIFY(box);

    auto *menuButton =
        window.findChild<QPushButton *>(QStringLiteral("savedQueryMenuButton"));
    QVERIFY(menuButton);

    int menuIndex = -1;
    int stretchIndex = -1;
    for (int i = 0; i < box->count(); ++i) {
        QLayoutItem *item = box->itemAt(i);
        if (item->widget() == menuButton)
            menuIndex = i;
        else if (!item->widget() && item->spacerItem())
            stretchIndex = i;
    }

    QVERIFY2(stretchIndex >= 0, "the row has no stretch to align against");
    QVERIFY2(menuIndex > stretchIndex,
             "the menu must come AFTER the stretch to sit at the right edge");
}

/// The row must not vanish when every saved query is unpinned: the menu is
/// then the only way to reach any of them, and hiding the row buries it.
void TestMainWindow::theRowSurvivesWithNothingButUnpinnedQueries()
{
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    loadWithQueries(config, dir, QStringLiteral(R"({
        "version": 1,
        "queries": [
            { "name": "Buried", "query": "tag:buried" },
            { "name": "AlsoBuried", "query": "tag:also" }
        ]
    })"));

    MainWindow window(config);
    auto *row = window.findChild<QWidget *>(QStringLiteral("savedQueryRow"));
    QVERIFY(row);
    QVERIFY2(!row->isHidden(),
             "the row was hidden, so the only route to these queries is gone");

    auto *menuButton =
        window.findChild<QPushButton *>(QStringLiteral("savedQueryMenuButton"));
    QVERIFY(menuButton);
    QCOMPARE(menuButton->menu()->actions().size(), 2);
}

static QString oneAccountWithSent()
{
    return QStringLiteral(
        "[account.work]\n"
        "name=Test User\n"
        "address=user@example.org\n"
        "maildir=work-mail\n"
        "sent=Sent\n"
    );
}

/// The existing Sent tests reach the generated entry through MIGRATION, since
/// their configs have no queries.json. This one starts from a stored file, so
/// it covers the path a user is on from the second launch onwards.
void TestMainWindow::aStoredGeneratedQueryRunsFlatAndComposed()
{
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    loadWithQueries(config, dir, QStringLiteral(R"({
        "version": 1,
        "queries": [
            { "name": "Sent", "generated": "sent", "pinned": true }
        ]
    })"), oneAccountWithSent());

    MainWindow window(config);
    auto *button =
        window.findChild<QAbstractButton *>(QStringLiteral("sentButton"));
    QVERIFY(button);

    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(queryEdit);
    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    QVERIFY2(!model->flatMode(), "the model starts threaded");

    button->click();

    // Composed from the account, not read from the file: the entry stores no
    // query at all.
    QCOMPARE(queryEdit->text(), config.allSentQuery());
    QVERIFY(queryEdit->text().contains(
        QStringLiteral("path:\"work-mail/Sent/**\"")));
    QVERIFY2(model->flatMode(),
             "a sent view must be flat, or replies fold back into the thread");
}

/// The point of the change: Sent is the user's row now. Renaming it must not
/// break it, which it would if anything keyed on the literal name "Sent".
void TestMainWindow::aRenamedSentEntryKeepsWorking()
{
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    loadWithQueries(config, dir, QStringLiteral(R"({
        "version": 1,
        "queries": [
            { "name": "Posta inviata", "generated": "sent", "pinned": true }
        ]
    })"), oneAccountWithSent());

    MainWindow window(config);

    // The renamed entry is UNPINNED on load now, because item 93 ships Sent as
    // a built-in filter and two Sent buttons on the row, one editable and one
    // not, is worse than one of each in its own place. It keeps its name, it
    // keeps working, and it is in the menu rather than on the row.
    QVERIFY2(savedQueryButtonLabels(window).isEmpty(),
             "a stored generated entry is still a button beside the built-in "
             "filter that duplicates it");

    bool found = false;
    for (const SavedQuery &saved : config.savedQueries()) {
        if (saved.name != QStringLiteral("Posta inviata"))
            continue;
        found = true;
        QVERIFY2(saved.isGenerated(),
                 "the entry lost its generator when renamed");
        QVERIFY2(!saved.pinned, "the entry was not unpinned");
    }
    QVERIFY2(found, "the renamed entry was DROPPED rather than unpinned");

    // The built-in Sent still resolves the same query, so nothing the user
    // could reach before became unreachable.
    auto *button =
        window.findChild<QAbstractButton *>(QStringLiteral("sentButton"));
    QVERIFY(button);
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    button->click();
    QCOMPARE(queryEdit->text(), config.allSentQuery());
}

/// The hardcoded button was hidden entirely when no account configured a sent
/// folder, rather than offering one that always finds nothing. A stored row
/// must behave the same way.
void TestMainWindow::aGeneratedQueryWithNothingToShowIsSkipped()
{
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    loadWithQueries(config, dir, QStringLiteral(R"({
        "version": 1,
        "queries": [
            { "name": "Inbox", "query": "tag:inbox", "pinned": true },
            { "name": "Sent", "generated": "sent", "pinned": true }
        ]
    })"), QStringLiteral(
        "[account.work]\n"
        "name=Test User\n"
        "address=user@example.org\n"
        "maildir=work-mail\n"
    ));

    MainWindow window(config);

    // The guard: the row was built and the other entry did get a button, so a
    // missing Sent means it was skipped rather than that nothing was built.
    QCOMPARE(savedQueryButtonLabels(window),
             QStringList{ QStringLiteral("Inbox") });
    QVERIFY2(!window.findChild<QAbstractButton *>(QStringLiteral("sentButton")),
             "a generated query with nothing to show must not get a button");
}

/// Reads queries.json back from disk, which is what "it was saved" means.
static QJsonArray storedQueries(const QTemporaryDir &dir)
{
    QFile f(dir.filePath(QStringLiteral("qtmaildir/queries.json")));
    if (!f.open(QIODevice::ReadOnly))
        return {};
    const QJsonObject root = QJsonDocument::fromJson(f.readAll()).object();
    return root.value(QStringLiteral("queries")).toArray();
}

static QAction *contextActionNamed(MainWindow &window, QWidget *target,
                                   const QString &objectName)
{
    const QList<QAction *> actions = target->actions();
    for (QAction *action : actions) {
        if (action->objectName() == objectName)
            return action;
    }
    Q_UNUSED(window);
    return nullptr;
}

void TestMainWindow::aSavedQueryButtonOffersEditUnpinAndDelete()
{
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    loadWithQueries(config, dir, QStringLiteral(R"({
        "version": 1,
        "queries": [
            { "name": "Inbox", "query": "tag:inbox", "pinned": true }
        ]
    })"));

    MainWindow window(config);
    auto *row = window.findChild<QWidget *>(QStringLiteral("savedQueryRow"));
    QVERIFY(row);
    auto *button = savedQueryButton(window, QStringLiteral("Inbox"));
    QVERIFY(button);

    // A context menu, so the actions live on the widget itself.
    QCOMPARE(button->contextMenuPolicy(), Qt::ActionsContextMenu);
    QVERIFY(contextActionNamed(window, button, QStringLiteral("editQuery")));
    QVERIFY(contextActionNamed(window, button, QStringLiteral("pinQuery")));
    QVERIFY(contextActionNamed(window, button, QStringLiteral("deleteQuery")));
}

void TestMainWindow::onlyAStoredQueryOffersToBecomeATaggingRule()
{
    // A generated entry composes its query from the accounts, so a rule made
    // from one freezes a snapshot that goes stale when an account is added.
    //
    // Both halves are asserted together on purpose: a test that only checks a
    // menu item is ABSENT passes just as well against a feature that was never
    // built, which item 82 recorded the hard way.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    // The account is not decoration. A generated entry that resolves to an
    // empty query is skipped entirely (src/mainwindow.cpp:1692), so without a
    // configured sent folder the Sent button is never built and the half of
    // this test that matters would pass by finding nothing.
    loadWithQueries(config, dir, QStringLiteral(R"({
        "version": 1,
        "queries": [
            { "name": "Inbox", "query": "tag:inbox", "pinned": true },
            { "name": "Sent", "generated": "sent", "pinned": true }
        ]
    })"),
                    QStringLiteral(
                        "[account.work]\n"
                        "name=Test User\n"
                        "address=user@example.org\n"
                        "maildir=work-mail\n"
                        "sent=work-mail/Sent\n"));

    MainWindow window(config);
    auto *row = window.findChild<QWidget *>(QStringLiteral("savedQueryRow"));
    QVERIFY(row);

    // By object name, which rebuildSavedQueryRow assigns precisely so a test
    // need not depend on a label the user can rename.
    auto *generated = row->findChild<QAbstractButton *>(
        QStringLiteral("sentButton"));

    // savedQueryButton(), not a scan for the label: item 93 puts a BUILT-IN
    // Inbox filter on the row too, and it carries no context actions by design,
    // so a scan finds that one and the assertion below fails against correct
    // code.
    QPushButton *stored = savedQueryButton(window, QStringLiteral("Inbox"));

    QVERIFY2(stored, "no button was built for the stored query");
    QVERIFY2(generated, "no button was built for the generated query");

    QVERIFY2(contextActionNamed(window, stored, QStringLiteral("queryToRule")),
             "a stored query must offer Create tagging rule");
    QVERIFY2(!contextActionNamed(window, generated,
                                 QStringLiteral("queryToRule")),
             "a generated query must not: its query is a snapshot");

    // The guard, and it has moved since item 93. It used to prove the generated
    // button HAS a menu, so the assertion above was about one action rather
    // than about a button with none. `generated` is now the BUILT-IN Sent
    // filter, which correctly carries no actions at all, so proving the
    // machinery works has to happen on the button that does have them.
    QVERIFY2(contextActionNamed(window, stored, QStringLiteral("deleteQuery")),
             "the stored button lost its other actions, so the assertion above "
             "is not about queryToRule in particular");
}

void TestMainWindow::unpinningMovesAQueryToTheMenu()
{
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    loadWithQueries(config, dir, QStringLiteral(R"({
        "version": 1,
        "queries": [
            { "name": "Inbox", "query": "tag:inbox", "pinned": true },
            { "name": "Other", "query": "tag:other", "pinned": true }
        ]
    })"));

    MainWindow window(config);
    auto *row = window.findChild<QWidget *>(QStringLiteral("savedQueryRow"));
    QVERIFY(row);
    QCOMPARE(savedQueryButtonLabels(window).size(), 2);
    QVERIFY(!window.findChild<QAbstractButton *>(
        QStringLiteral("savedQueryMenuButton")));

    auto *button = savedQueryButton(window, QStringLiteral("Inbox"));
    QVERIFY(button);
    QAction *pin = contextActionNamed(window, button, QStringLiteral("pinQuery"));
    QVERIFY(pin);
    pin->trigger();

    // Off the row, into the menu, and written to the file: an unpin that only
    // redrew would come back pinned on the next launch.
    QCOMPARE(savedQueryButtonLabels(window), QStringList{ QStringLiteral("Other") });
    auto *menuButton =
        window.findChild<QPushButton *>(QStringLiteral("savedQueryMenuButton"));
    QVERIFY(menuButton);
    QCOMPARE(menuButton->menu()->actions().size(), 1);

    const QJsonArray stored = storedQueries(dir);
    QCOMPARE(stored.size(), 2);
    QCOMPARE(stored.at(0).toObject().value(QStringLiteral("name")).toString(),
             QStringLiteral("Inbox"));
    QVERIFY2(!stored.at(0).toObject().contains(QStringLiteral("pinned")),
             "the unpin did not reach the file");
}

void TestMainWindow::deletingRemovesTheQueryFromTheFile()
{
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    loadWithQueries(config, dir, QStringLiteral(R"({
        "version": 1,
        "queries": [
            { "name": "Doomed", "query": "tag:doomed", "pinned": true },
            { "name": "Keeper", "query": "tag:keeper", "pinned": true }
        ]
    })"));

    MainWindow window(config);
    auto *row = window.findChild<QWidget *>(QStringLiteral("savedQueryRow"));
    QVERIFY(row);
    auto *button = savedQueryButton(window, QStringLiteral("Doomed"));
    QVERIFY(button);
    QCOMPARE(button->text(), QStringLiteral("Doomed"));

    QAction *del =
        contextActionNamed(window, button, QStringLiteral("deleteQuery"));
    QVERIFY(del);
    // Destructive and not on the undo stack, so it confirms. Suppressed here
    // rather than driven through the modal dialog, which would hang the test.
    window.setConfirmDeleteForTesting(false);
    del->trigger();

    QCOMPARE(savedQueryButtonLabels(window),
             QStringList{ QStringLiteral("Keeper") });

    const QJsonArray stored = storedQueries(dir);
    QCOMPARE(stored.size(), 1);
    QCOMPARE(stored.at(0).toObject().value(QStringLiteral("name")).toString(),
             QStringLiteral("Keeper"));
}

/// A field a later build wrote must survive an edit here, or upgrading and
/// downgrading silently strips config the user set.
void TestMainWindow::anEditedQueryKeepsItsUnknownFields()
{
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    loadWithQueries(config, dir, QStringLiteral(R"({
        "version": 1,
        "queries": [
            { "name": "Inbox", "query": "tag:inbox", "pinned": true,
              "icon": "mail-inbox" }
        ]
    })"));

    MainWindow window(config);

    // Through the EDIT path, with a replacement carrying no unknown fields of
    // its own, which is exactly what SaveQueryDialog returns. Driving this
    // through unpin instead proved nothing: unpin copies the stored entry, so
    // it carries `unknown` along by itself and the merge is never exercised.
    // That version passed with the merge deleted.
    SavedQuery edited;
    edited.name = QStringLiteral("Inbox");
    edited.query = QStringLiteral("tag:inbox and not tag:muted");
    edited.pinned = true;
    QVERIFY(edited.unknown.isEmpty());
    window.replaceSavedQueryForTesting(QStringLiteral("Inbox"), edited);

    const QJsonArray stored = storedQueries(dir);
    QCOMPARE(stored.size(), 1);
    const QJsonObject entry = stored.at(0).toObject();
    // The edit landed...
    QCOMPARE(entry.value(QStringLiteral("query")).toString(),
             QStringLiteral("tag:inbox and not tag:muted"));
    // ...and did not take the unknown field down with it.
    QCOMPARE(entry.value(QStringLiteral("icon")).toString(),
             QStringLiteral("mail-inbox"));
}

/// Renaming must match on the name the dialog OPENED with. Matching on the
/// returned name leaves the original in place and adds a second entry.
void TestMainWindow::renamingReplacesRatherThanDuplicating()
{
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    loadWithQueries(config, dir, QStringLiteral(R"({
        "version": 1,
        "queries": [
            { "name": "Old", "query": "tag:old", "pinned": true }
        ]
    })"));

    MainWindow window(config);

    SavedQuery renamed;
    renamed.name = QStringLiteral("New");
    renamed.query = QStringLiteral("tag:old");
    renamed.pinned = true;
    window.replaceSavedQueryForTesting(QStringLiteral("Old"), renamed);

    const QJsonArray stored = storedQueries(dir);
    QCOMPARE(stored.size(), 1);
    QCOMPARE(stored.at(0).toObject().value(QStringLiteral("name")).toString(),
             QStringLiteral("New"));
    QCOMPARE(savedQueryButtonLabels(window), QStringList{ QStringLiteral("New") });
}

void TestMainWindow::aMalformedAccountIsReportedWithoutBlockingTheConstructor()
{
    // The exact shape that hung the suite on 2026-08-14: an account section
    // carrying `sent=` and no `maildir=`. Config::load handles it correctly,
    // recording a problem and carrying on, but showWarnings() then raised a
    // modal FROM THE CONSTRUCTOR, which nothing can dismiss under the
    // offscreen platform, so MainWindow never finished constructing.
    //
    // Constructing the window at all is therefore half the assertion: if the
    // modal comes back, this test does not fail, it HANGS.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());

    const QString path = dir.filePath(QStringLiteral("qtmaildir.conf"));
    {
        QFile file(path);
        QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
        QTextStream out(&file);
        out << "[account.one]\n"
            << "sent=Sent\n";
    }

    Config config;
    config.load(path);

    MainWindow window(config);

    // The problem is reported rather than swallowed, and it names the account.
    const QStringList problems = window.configProblems();
    QVERIFY2(!problems.isEmpty(),
             "a malformed account produced no problem to report");
    QVERIFY(problems.join(QLatin1Char('\n')).contains(QStringLiteral("one")));
}

void TestMainWindow::aWorkerBackedWindowReturnsRealThreads()
{
    // The guard test for every fixture-backed case after it. A probe that
    // cannot find what it expects to find will report a miss forever, so this
    // proves the database, the config and the worker are connected BEFORE
    // anything relies on a signal not arriving.
    WorkerBackedWindow backed;
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("inbox"), QStringLiteral("root@example.org"),
        QStringLiteral("A subject"), QStringLiteral("sender@example.org"),
        // Friday, verified with `date -d 2026-08-14 +%A`. Qt::RFC2822Date
        // validates the weekday against the date and rejects a mismatch.
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY2(backed.build(), qPrintable(backed.error()));

    MainWindow window(backed.config());

    QLineEdit *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY2(queryEdit, "no query bar: the window was never built");

    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY2(model, "no thread list model");

    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();

    // QTRY_VERIFY re-tests until the condition holds or the timeout expires,
    // which is what makes this safe against a worker on another thread. Not a
    // fixed qWait(n): a guessed sleep races under load and, worse, passes when
    // the result never arrives at all because nothing re-checks.
    //
    // The worker itself is unreachable from here by construction, not by
    // oversight: wireWorker() creates it parentless and moves it to its own
    // thread, so findChild() cannot see it. Observing the window's own state
    // is both the only route and the better assertion.
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);
}

namespace {

/// A worker-backed window with one message in one account's maildir.
///
/// The compose cases all need the same three things: a message on disk, an
/// account owning the folder it landed in, and a selected row. Repeating that
/// in six tests is how one of them ends up subtly different from the rest.
struct WorkerComposeFixture
{
    WorkerBackedWindow backed;

    /// Writes one message into <accountMaildir>/inbox and indexes it.
    /// \p composeKey, when given, is written as one line under [compose].
    bool seed(const QList<WorkerBackedWindow::AccountSpec> &accounts,
              const QString &folder, const QString &composeKey = QString())
    {
        if (!backed.fixture().addMessage(
                folder, QStringLiteral("compose1@example.org"),
                QStringLiteral("A subject"),
                QStringLiteral("sender@example.org"),
                // Friday, verified with `date -d 2026-08-14 +%A`.
                QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
                QStringLiteral("Body text."))) {
            return false;
        }
        return backed.buildWithAccounts(accounts, composeKey);
    }

    /// Runs a query and puts the current index on its one row.
    ///
    /// Waits on the MAIL ROOT as well as on the row. The reply family is gated
    /// on which account owns the message, which needs the root, and that
    /// arrives on its own queued signal: asserting on an action's enabled
    /// state before it lands measures the startup race rather than the rule.
    static bool selectTheMessage(MainWindow &window)
    {
        auto *model = window.findChild<ThreadListModel *>();
        auto *view = window.findChild<ThreadListView *>();
        auto *queryEdit =
            window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
        if (!model || !view || !queryEdit)
            return false;

        queryEdit->setText(QStringLiteral("tag:inbox"));
        queryEdit->returnPressed();

        bool ready = false;
        for (int attempt = 0; attempt < 150 && !ready; ++attempt) {
            ready = model->rowCount(QModelIndex()) == 1
                    && !window.mailRootForTesting().isEmpty();
            if (!ready)
                QTest::qWait(100);
        }
        if (!ready)
            return false;

        view->setCurrentIndex(model->index(0, 0, QModelIndex()));
        return true;
    }
};

}  // namespace

void TestMainWindow::theMailRootComesFromTheConfigNotTheIndex()
{
    // Item 124's rule, for the path the composer composes drafts and sent
    // copies under. splitIndex() is what makes this test able to fail at all:
    // in the ordinary layout notmuch_database_get_path() and
    // NOTMUCH_CONFIG_MAIL_ROOT return the SAME string, so a test written
    // against it passes whichever accessor the code uses.
    WorkerComposeFixture fixture;
    fixture.backed.fixture().splitIndex();
    QVERIFY2(fixture.seed({ { QStringLiteral("work"), QStringLiteral("work"),
                              QString(), QStringLiteral("/bin/true"),
                              QStringLiteral("you@example.org") } },
                          QStringLiteral("work/inbox")),
             qPrintable(fixture.backed.error()));

    MainWindow window(fixture.backed.config());

    QTRY_VERIFY_WITH_TIMEOUT(!window.mailRootForTesting().isEmpty(), 15000);

    // The MAIL root, not the index directory. Under the split layout these are
    // different directories, and a draft composed under the index one is
    // written into the Xapian tree.
    QCOMPARE(window.mailRootForTesting(),
             QDir(fixture.backed.fixture().maildirPath()).absolutePath());
    QVERIFY2(window.mailRootForTesting()
                 != QDir(fixture.backed.fixture().indexPath()).absolutePath(),
             "the window took the index directory for the mail root");
}

void TestMainWindow::replyIsDisabledOnAReceiveOnlyAccountsMail()
{
    // The capability IS the send_command's presence, so this account is
    // written without one.
    WorkerComposeFixture fixture;
    QVERIFY2(fixture.seed({ { QStringLiteral("listsonly"),
                              QStringLiteral("listsonly"), QString(),
                              /*sendCommand=*/QString(),
                              QStringLiteral("you@example.org") } },
                          QStringLiteral("listsonly/inbox")),
             qPrintable(fixture.backed.error()));

    MainWindow window(fixture.backed.config());
    QVERIFY(WorkerComposeFixture::selectTheMessage(window));

    for (const QString &name : { QStringLiteral("reply"),
                                 QStringLiteral("reply_all"),
                                 QStringLiteral("reply_no_quote"),
                                 QStringLiteral("forward") }) {
        auto *action = window.findChild<QAction *>(name);
        QVERIFY2(action, qPrintable(QStringLiteral("no action %1").arg(name)));
        QVERIFY2(!action->isEnabled(),
                 qPrintable(QStringLiteral("%1 was live on receive-only mail")
                                .arg(name)));
    }

    // save_message is NEVER disabled, including here. It is the escape hatch
    // for exactly this case: write the raw message out and attach it to a new
    // message from an account that can send.
    auto *save = window.findChild<QAction *>(QStringLiteral("save_message"));
    QVERIFY(save);
    QVERIFY2(save->isEnabled(),
             "save_message was disabled, removing the escape hatch");
}

void TestMainWindow::replyIsEnabledOnASendingAccountsMail()
{
    // The guard for the test above. Without it, a bug disabling the reply
    // family unconditionally would pass every assertion there while removing
    // the feature entirely.
    WorkerComposeFixture fixture;
    QVERIFY2(fixture.seed({ { QStringLiteral("work"), QStringLiteral("work"),
                              QString(), QStringLiteral("/bin/true"),
                              QStringLiteral("you@example.org") } },
                          QStringLiteral("work/inbox")),
             qPrintable(fixture.backed.error()));

    MainWindow window(fixture.backed.config());
    QVERIFY(WorkerComposeFixture::selectTheMessage(window));

    for (const QString &name : { QStringLiteral("reply"),
                                 QStringLiteral("reply_all"),
                                 QStringLiteral("reply_no_quote"),
                                 QStringLiteral("forward") }) {
        auto *action = window.findChild<QAction *>(name);
        QVERIFY2(action, qPrintable(QStringLiteral("no action %1").arg(name)));
        QVERIFY2(action->isEnabled(),
                 qPrintable(QStringLiteral("%1 was disabled on mail from an "
                                           "account that can send").arg(name)));
    }

    // And no ribbon: this account can send, so there is nothing to explain.
    auto *ribbon =
        window.findChild<QLabel *>(QStringLiteral("receiveOnlyRibbon"));
    QVERIFY(ribbon);
    QVERIFY2(ribbon->isHidden(),
             "the receive-only ribbon showed on an account that can send");
}

void TestMainWindow::theReceiveOnlyRibbonNamesTheAccount()
{
    // The ribbon is a WIDGET in MessageView's layout, not markup inside the
    // web view. Composing HTML from configuration into the one document that
    // renders input from strangers is the wrong direction.
    WorkerComposeFixture fixture;
    QVERIFY2(fixture.seed({ { QStringLiteral("listsonly"),
                              QStringLiteral("listsonly"), QString(),
                              QString(), QStringLiteral("you@example.org") } },
                          QStringLiteral("listsonly/inbox")),
             qPrintable(fixture.backed.error()));

    MainWindow window(fixture.backed.config());
    QVERIFY(WorkerComposeFixture::selectTheMessage(window));

    auto *ribbon =
        window.findChild<QLabel *>(QStringLiteral("receiveOnlyRibbon"));
    QVERIFY2(ribbon, "no ribbon widget exists");

    // isHidden() rather than isVisibleTo(): under the offscreen platform an
    // unshown window's children report not visible whatever the code does, so
    // isVisibleTo would fail against correct code. What is being asserted is
    // that the ribbon was not left explicitly hidden.
    QVERIFY2(!ribbon->isHidden(),
             "the ribbon did not appear on receive-only mail");
    QVERIFY2(ribbon->text().contains(QStringLiteral("listsonly")),
             qPrintable(QStringLiteral("the ribbon does not name the account: %1")
                            .arg(ribbon->text())));

    // PlainText, not AutoText. A QLabel guesses under AutoText, and this is
    // the same protection MessageDetailsDialog states on every value.
    QCOMPARE(ribbon->textFormat(), Qt::PlainText);
}

void TestMainWindow::composeIsDisabledOnlyWhenNoAccountCanSend()
{
    // An installation with no send_command anywhere is a valid read-only
    // installation and is not warned about; compose is simply unavailable.
    {
        WorkerComposeFixture fixture;
        QVERIFY2(fixture.seed({ { QStringLiteral("listsonly"),
                                  QStringLiteral("listsonly"), QString(),
                                  QString(), QStringLiteral("you@example.org") } },
                              QStringLiteral("listsonly/inbox")),
                 qPrintable(fixture.backed.error()));

        MainWindow window(fixture.backed.config());
        auto *compose = window.findChild<QAction *>(QStringLiteral("compose"));
        QVERIFY(compose);
        QVERIFY2(!compose->isEnabled(),
                 "compose was live with no account able to send");
    }
    {
        WorkerComposeFixture fixture;
        QVERIFY2(fixture.seed(
                     { { QStringLiteral("listsonly"),
                         QStringLiteral("listsonly"), QString(), QString(),
                         QStringLiteral("you@example.org") },
                       { QStringLiteral("work"), QStringLiteral("work"),
                         QString(), QStringLiteral("/bin/true"),
                         QStringLiteral("work@example.org") } },
                     QStringLiteral("listsonly/inbox")),
                 qPrintable(fixture.backed.error()));

        MainWindow window(fixture.backed.config());
        auto *compose = window.findChild<QAction *>(QStringLiteral("compose"));
        QVERIFY(compose);
        QVERIFY2(compose->isEnabled(),
                 "compose was disabled although one account can send");
    }
}

void TestMainWindow::quittingWithACleanComposerAsksNothing()
{
    // Case 1: every composer clean, quit directly, no dialog. A dialog here
    // would be the "are you sure" this project deliberately does not do.
    WorkerComposeFixture fixture;
    QVERIFY2(fixture.seed({ { QStringLiteral("work"), QStringLiteral("work"),
                              QString(), QStringLiteral("/bin/true"),
                              QStringLiteral("you@example.org") } },
                          QStringLiteral("work/inbox")),
             qPrintable(fixture.backed.error()));

    MainWindow window(fixture.backed.config());
    QTRY_VERIFY_WITH_TIMEOUT(!window.mailRootForTesting().isEmpty(), 15000);

    QVERIFY2(window.openComposerForTest(), "no composer opened");
    QCOMPARE(window.openComposerCount(), 1);

    QVERIFY2(window.composersBlockingQuit().isEmpty(),
             "a clean composer was reported as blocking quit");

    // Composers are parentless top-level windows and outlive this MainWindow,
    // carrying a MessageSender and a running autosave timer into whatever test
    // runs next. Closed here rather than left for the destructor, which never
    // touches m_composers.
    for (ComposeWindow *composer : window.openComposersForTest()) {
        composer->show();
        composer->close();
    }
}

void TestMainWindow::quittingWithUnsavedEditsReportsEveryComposer()
{
    // Case 2: ONE dialog whatever the count, so the quit path has to see BOTH
    // composers rather than stopping at the first dirty one.
    WorkerComposeFixture fixture;
    QVERIFY2(fixture.seed({ { QStringLiteral("work"), QStringLiteral("work"),
                              QString(), QStringLiteral("/bin/true"),
                              QStringLiteral("you@example.org") } },
                          QStringLiteral("work/inbox")),
             qPrintable(fixture.backed.error()));

    MainWindow window(fixture.backed.config());
    QTRY_VERIFY_WITH_TIMEOUT(!window.mailRootForTesting().isEmpty(), 15000);

    QVERIFY(window.openComposerForTest());
    QVERIFY(window.openComposerForTest());
    QCOMPARE(window.openComposerCount(), 2);

    // Clean until something is typed, which is the case-1 assertion holding
    // here too and the guard that this test can distinguish the two states.
    QVERIFY(window.composersBlockingQuit().isEmpty());

    window.markComposersDirtyForTest();
    QCOMPARE(window.composersBlockingQuit().size(), 2);

    // Left open, these are parentless top-level windows with a live autosave
    // timer, surviving into later tests. See the note in the clean-composer
    // case above.
    for (ComposeWindow *composer : window.openComposersForTest()) {
        composer->show();
        composer->close();
    }
}

void TestMainWindow::closingAComposerCompactsTheRegistry()
{
    // The closed() signal's ONE job. The QPointer alone would keep
    // composersBlockingQuit() correct, since it nulls on destruction, but the
    // entry would stay in the list for the session's lifetime. This asserts
    // the list is compacted, which only the signal can do.
    WorkerComposeFixture fixture;
    QVERIFY2(fixture.seed({ { QStringLiteral("work"), QStringLiteral("work"),
                              QString(), QStringLiteral("/bin/true"),
                              QStringLiteral("you@example.org") } },
                          QStringLiteral("work/inbox")),
             qPrintable(fixture.backed.error()));

    MainWindow window(fixture.backed.config());
    QTRY_VERIFY_WITH_TIMEOUT(!window.mailRootForTesting().isEmpty(), 15000);

    ComposeWindow *composer = window.openComposerForTest();
    QVERIFY(composer);
    QCOMPARE(window.openComposerCount(), 1);

    // A composer that was never shown returns early from close() WITHOUT
    // reaching closeEvent(), so the signal would never fire and this test
    // would assert nothing at all.
    composer->show();
    QVERIFY(composer->close());

    // And the quit path must not see a destroyed window, which is the
    // QPointer's job rather than the signal's.
    QCOMPARE(window.openComposerCount(), 0);
    QVERIFY(window.composersBlockingQuit().isEmpty());
}

void TestMainWindow::savingAMessageRefusesToEscapeTheChosenDirectory()
{
    // A subject is input from a stranger and is what the default filename is
    // derived from, so it may carry separators and "..". Asserted through
    // Attachment's own helpers, which is what saveDisplayedMessage() calls:
    // a second implementation of the check here would prove nothing about the
    // one that runs.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    const QString directory = dir.path();

    Attachment naming;
    naming.filename = QStringLiteral("../../etc/passwd");
    const QString target =
        QDir(directory).absoluteFilePath(naming.safeFilename());

    QVERIFY2(Attachment::isPathInsideDirectory(directory, target),
             "a traversing subject escaped the chosen directory");
    QVERIFY2(!target.contains(QStringLiteral("/etc/passwd")),
             qPrintable(QStringLiteral("the traversal survived: %1").arg(target)));

    // Compared as PATHS, never with startsWith(): a sibling directory whose
    // name merely begins with the chosen one's is not inside it.
    QVERIFY2(!Attachment::isPathInsideDirectory(
                 directory, directory + QStringLiteral("-evil/message.eml")),
             "a sibling directory passed the containment check");
}

void TestMainWindow::aHostileSubjectCannotEscapeTheSaveDirectory()
{
    // Asserted through MainWindow::defaultMessageFilename(), which is what
    // saveDisplayedMessage() actually calls. The previous version of this
    // check built an Attachment by hand and called safeFilename() directly:
    // that proves what Attachment does and nothing about whether save_message
    // asks it anything, and three mutations to the real path left it green.
    // CLAUDE.md: assert through the function the production path calls, not
    // through the one it calls INTO.
    const QString traversal =
        MainWindow::defaultMessageFilename(QStringLiteral("../../etc/passwd"));

    // No separator survives, so the name cannot address another directory.
    QVERIFY2(!traversal.contains(QLatin1Char('/')),
             qPrintable(QStringLiteral("a separator survived: %1").arg(traversal)));
    // NOT asserting the absence of "..": with every separator replaced, a
    // literal ".." inside a filename addresses nothing and is a legitimate
    // part of a name. What matters is that the result is a single path
    // COMPONENT, which is what makes traversal impossible.
    QCOMPARE(QFileInfo(traversal).fileName(), traversal);
    QVERIFY2(traversal != QStringLiteral("..")
                 && traversal != QStringLiteral("."),
             qPrintable(QStringLiteral("the name is a directory reference: %1")
                            .arg(traversal)));

    // And joining it onto a directory really does stay inside.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Attachment naming;
    naming.filename = traversal;
    const QString target =
        QDir(dir.path()).absoluteFilePath(naming.safeFilename());
    QVERIFY2(Attachment::isPathInsideDirectory(dir.path(), target),
             qPrintable(QStringLiteral("escaped the directory: %1").arg(target)));

    // A backslash is a separator too, on a name written by Windows software.
    const QString backslash = MainWindow::defaultMessageFilename(
        QStringLiteral("..\\..\\Windows\\System32\\config"));
    QVERIFY2(!backslash.contains(QLatin1Char('\\')),
             qPrintable(QStringLiteral("a backslash survived: %1").arg(backslash)));

    // A subject with nothing usable still yields a name rather than "" or a
    // bare extension, which would make the write land on a dotfile.
    const QString empty = MainWindow::defaultMessageFilename(QString());
    QVERIFY2(empty.startsWith(QStringLiteral("message")),
             qPrintable(QStringLiteral("empty subject gave: %1").arg(empty)));

    // The extension survives truncation. Truncating AFTER appending it would
    // cut ".eml" off a long subject and write an extensionless file.
    const QString long_ = MainWindow::defaultMessageFilename(
        QString(400, QLatin1Char('a')));
    QVERIFY2(long_.endsWith(QStringLiteral(".eml")),
             qPrintable(QStringLiteral("the extension was truncated away: %1")
                            .arg(long_.right(20))));
}

void TestMainWindow::savingTwiceDoesNotOverwriteTheFirstFile()
{
    // Two messages very often share a subject, and the filename is derived
    // from it, so the second save must not destroy the first. Driven through
    // saveDisplayedMessage() by way of the directory seam, which is the only
    // way to reach the write guard at all: the file dialog is a modal the
    // offscreen platform cannot click.
    WorkerComposeFixture fixture;
    QVERIFY2(fixture.seed({ { QStringLiteral("work"), QStringLiteral("work"),
                              QString(), QStringLiteral("/bin/true"),
                              QStringLiteral("you@example.org") } },
                          QStringLiteral("work/inbox")),
             qPrintable(fixture.backed.error()));

    MainWindow window(fixture.backed.config());
    QVERIFY(WorkerComposeFixture::selectTheMessage(window));

    QTemporaryDir out;
    QVERIFY(out.isValid());

    window.saveDisplayedMessageForTest(out.path());
    window.saveDisplayedMessageForTest(out.path());

    // Two files, not one overwritten. Asserted on the COUNT rather than on the
    // second name, so the disambiguation scheme can change without the test
    // caring what it is called.
    const QStringList written =
        QDir(out.path()).entryList(QDir::Files | QDir::NoDotAndDotDot);
    QCOMPARE(written.size(), 2);

    // And both are real copies rather than one empty placeholder.
    for (const QString &name : written) {
        QVERIFY2(QFileInfo(QDir(out.path()).absoluteFilePath(name)).size() > 0,
                 qPrintable(QStringLiteral("%1 is empty").arg(name)));
    }
}

void TestMainWindow::savingAMessageWithAHostileSubjectStaysInTheDirectory()
{
    // Driven through saveDisplayedMessage() with a real hostile subject, which
    // is the only shape that covers the production write path. An earlier
    // version of this coverage built an Attachment by hand and called
    // safeFilename() and isPathInsideDirectory() directly, which proves what
    // Attachment does and nothing about whether save_message asks it anything.
    //
    // WHAT THIS CAN AND CANNOT CATCH, measured rather than assumed, because
    // the numbers are surprising and the next person will otherwise redo the
    // work. Three independent layers stand between a subject and the write:
    // defaultMessageFilename() replaces separators, Attachment::safeFilename()
    // reduces to a basename, and Attachment::isPathInsideDirectory() refuses
    // the write. EACH ONE ALONE IS SUFFICIENT, so removing any single layer
    // leaves this test green: measured, all three single-layer mutations pass.
    // Removing all three fails it. That is real defence-in-depth rather than a
    // probe pointed at the wrong object, and mimeparser.h:71-77 already says
    // the same of isPathInsideDirectory, but it does mean this test is a guard
    // against the DEFENCES COLLECTIVELY disappearing, not a guard on any one
    // of them. aHostileSubjectCannotEscapeTheSaveDirectory() covers the first
    // layer on its own, and a single-layer mutation there does fail.
    //
    // The subject is ABSOLUTE rather than "../..", and that matters.
    // QDir::absoluteFilePath() does not resolve ".." (measured: it
    // concatenates), but the collision loop below can rename a relative
    // traversal by accident when the target happens to exist, which makes it
    // the weaker probe. An absolute candidate replaces the directory outright.
    WorkerComposeFixture fixture;
    QVERIFY(fixture.backed.fixture().addMessage(
        QStringLiteral("work/inbox"), QStringLiteral("hostile@example.org"),
        // The subject is the attacker's input, and it is what the default
        // filename is derived from.
        // Absolute, not "../..". QDir::absoluteFilePath() does NOT resolve
        // ".." (measured: it concatenates, giving "<dir>/../../x"), but an
        // ABSOLUTE candidate replaces the directory outright, which is the
        // escape that survives every accident. A relative traversal can be
        // neutralised by the collision loop renaming it when the target
        // happens to exist, so it is the weaker probe of the two.
        QStringLiteral("/tmp/qtmaildir-pwned-probe"),
        QStringLiteral("sender@example.org"),
        // Friday, verified with `date -d 2026-08-14 +%A`.
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY2(fixture.backed.buildWithAccounts(
                 { { QStringLiteral("work"), QStringLiteral("work"), QString(),
                     QStringLiteral("/bin/true"),
                     QStringLiteral("you@example.org") } }),
             qPrintable(fixture.backed.error()));

    MainWindow window(fixture.backed.config());
    QVERIFY(WorkerComposeFixture::selectTheMessage(window));

    // A directory INSIDE another, so an escape has somewhere to land that the
    // test can then look at. Escaping "out" writes into parent/, which is what
    // the assertions below check is still empty.
    QTemporaryDir parent;
    QVERIFY(parent.isValid());
    const QString out = parent.filePath(QStringLiteral("out"));
    QVERIFY(QDir().mkpath(out));

    window.saveDisplayedMessageForTest(out);

    // The file landed inside the chosen directory.
    // NOT QDir::Hidden. A file whose name begins with a dot is hidden on every
    // Unix desktop, so the write would succeed while the user could not find
    // what they saved. Listing without Hidden is what makes this assertion
    // notice that, and it is how the leading-dot case was found: a traversing
    // subject reduces to "..-..-etc-passwd" once its separators are replaced,
    // which is a dotfile.
    const QStringList inside =
        QDir(out).entryList(QDir::Files | QDir::NoDotAndDotDot);
    QCOMPARE(inside.size(), 1);
    QVERIFY2(!inside.first().startsWith(QLatin1Char('.')),
             qPrintable(QStringLiteral("the saved message is hidden: %1")
                            .arg(inside.first())));

    // And nothing was written beside it, which is where a traversal would go.
    const QStringList escaped =
        QDir(parent.path()).entryList(QDir::Files | QDir::NoDotAndDotDot);
    QVERIFY2(escaped.isEmpty(),
             qPrintable(QStringLiteral("a file escaped the directory: %1")
                            .arg(escaped.join(QLatin1Char(' ')))));

    // The written path really is contained, compared as PATHS rather than with
    // startsWith(): a sibling directory whose name merely begins with the
    // chosen one's is not inside it.
    const QString written = QDir(out).absoluteFilePath(inside.first());
    QVERIFY2(Attachment::isPathInsideDirectory(out, written),
             qPrintable(QStringLiteral("escaped: %1").arg(written)));
    QVERIFY2(QFileInfo(written).size() > 0, "the saved message is empty");
}

void TestMainWindow::aStuckComposeRequestDoesNotHijackTheNextPaneLoad()
{
    // A compose request for a message that is not in the index used to stay
    // armed for ever, because it was cleared only on the branch that FOUND the
    // id. The delayed symptom is the bad one: the pane's own loads are the
    // traffic being matched against, so merely selecting that message later
    // matched, opened a composer nobody asked for, and returned before
    // renderMessages() leaving the pane blank on the row just clicked.
    WorkerComposeFixture fixture;
    QVERIFY2(fixture.seed({ { QStringLiteral("work"), QStringLiteral("work"),
                              QString(), QStringLiteral("/bin/true"),
                              QStringLiteral("you@example.org") } },
                          QStringLiteral("work/inbox")),
             qPrintable(fixture.backed.error()));

    MainWindow window(fixture.backed.config());
    QVERIFY(WorkerComposeFixture::selectTheMessage(window));

    // Arm a request for an id the database does not hold. loadMessage() emits
    // an empty result for it, which is what must disarm the request.
    window.requestMessageForComposeForTest(
        QStringLiteral("nosuchmessage@example.org"),
        ComposeContext::Kind::Reply, true);

    // No composer, and the request stops being armed.
    QTRY_VERIFY_WITH_TIMEOUT(!window.composeRequestPendingForTest(), 15000);
    QCOMPARE(window.openComposerCount(), 0);

    // Now the delayed half. Select the real message: the pane must render it,
    // and no composer may appear. With the request still armed this failed
    // only if the ids matched, so the request is re-armed for the REAL id to
    // make the hijack reachable at all.
    window.requestMessageForComposeForTest(
        QStringLiteral("compose1@example.org"), ComposeContext::Kind::Reply,
        true);
    QTRY_VERIFY_WITH_TIMEOUT(!window.composeRequestPendingForTest(), 15000);

    // That one DID match, so it opened a composer. Close it and clear the
    // pane, then re-select and assert the pane renders rather than a second
    // composer opening.
    for (ComposeWindow *composer : window.openComposersForTest()) {
        composer->show();
        composer->close();
    }
    QCOMPARE(window.openComposerCount(), 0);

    auto *model = window.findChild<ThreadListModel *>();
    auto *view = window.findChild<ThreadListView *>();
    QVERIFY(model && view);
    view->setCurrentIndex(QModelIndex());
    view->setCurrentIndex(model->index(0, 0, QModelIndex()));

    auto *pane = window.findChild<MessageView *>();
    QVERIFY(pane);
    QTRY_VERIFY_WITH_TIMEOUT(!pane->showingPlaceholder(), 15000);
    QCOMPARE(window.openComposerCount(), 0);
}

void TestMainWindow::theSaveLoopToleratesAComposerClosedUnderTheDialog()
{
    // The regression for a measured use-after-free. composersBlockingQuit()
    // used to return raw pointers, and the quit path held that list across
    // QMessageBox::exec(). A nested event loop PROCESSES deleteLater(),
    // verified in a standalone Qt program: a parentless WA_DeleteOnClose
    // window closed while a modal is up is destroyed BEFORE exec() returns.
    // The dialog is window-modal to the main window only, so a user really can
    // close a composer from under it, and Save then ran on freed memory.
    //
    // The modal itself cannot be driven under the offscreen platform, so what
    // is asserted is the property that makes the loop safe: the list holds
    // QPointers, and an entry whose window is destroyed reads as null rather
    // than as a dangling pointer. That is exactly what the null check in the
    // Save loop consumes. Stated plainly because it is NOT full coverage of
    // closeEvent(): see the report.
    WorkerComposeFixture fixture;
    QVERIFY2(fixture.seed({ { QStringLiteral("work"), QStringLiteral("work"),
                              QString(), QStringLiteral("/bin/true"),
                              QStringLiteral("you@example.org") } },
                          QStringLiteral("work/inbox")),
             qPrintable(fixture.backed.error()));

    MainWindow window(fixture.backed.config());
    QTRY_VERIFY_WITH_TIMEOUT(!window.mailRootForTesting().isEmpty(), 15000);

    QVERIFY(window.openComposerForTest());
    QVERIFY(window.openComposerForTest());
    window.markComposersDirtyForTest();

    QList<QPointer<ComposeWindow>> blocking = window.composersBlockingQuit();
    QCOMPARE(blocking.size(), 2);

    // Destroy one exactly as closing it under the dialog would, including the
    // deleteLater() a nested exec() would process.
    ComposeWindow *doomed = blocking.first().data();
    QVERIFY(doomed);
    doomed->show();
    doomed->close();
    QCoreApplication::sendPostedEvents(nullptr, QEvent::DeferredDelete);

    // The held list reports it as gone rather than handing back a dangling
    // pointer. A raw QList<ComposeWindow *> could not express this at all.
    QVERIFY2(blocking.first().isNull(),
             "the held entry did not null when its window was destroyed");
    QVERIFY2(!blocking.last().isNull(),
             "the surviving composer was lost too");

    // And the loop the quit path runs skips the null and still saves the
    // survivor, which is the behaviour the crash destroyed: the remaining
    // drafts were never written because the crash happened mid-loop.
    int saved = 0;
    for (const QPointer<ComposeWindow> &composer : blocking) {
        if (composer) {
            composer->saveDraftNow();
            ++saved;
        }
    }
    QCOMPARE(saved, 1);
}

namespace {

/// Writes a multipart/mixed message with one named attachment part.
///
/// Hand-written rather than built with MessageBuilder: this is the INPUT to
/// the forward path, and generating it with the same library that consumes it
/// would let an encoding mistake agree with itself.
bool writeMessageWithAttachment(const QString &path, const QString &attachName,
                                const QByteArray &attachBody)
{
    QFile file(path);
    if (!file.open(QIODevice::WriteOnly))
        return false;
    QByteArray raw =
        "From: sender@example.org\n"
        "To: you@example.org\n"
        "Subject: Quarterly report\n"
        "Message-ID: <fwd-1@example.org>\n"
        // Friday, verified with `date -d 2026-08-14 +%A`. Qt::RFC2822Date
        // validates the weekday against the date.
        "Date: Fri, 14 Aug 2026 10:00:00 +0200\n"
        "MIME-Version: 1.0\n"
        "Content-Type: multipart/mixed; boundary=\"MIX\"\n"
        "\n"
        "--MIX\n"
        "Content-Type: text/plain; charset=utf-8\n"
        "\n"
        "See the attached document.\n"
        "--MIX\n"
        "Content-Type: application/octet-stream; name=\"" + attachName.toUtf8() + "\"\n"
        "Content-Disposition: attachment; filename=\"" + attachName.toUtf8() + "\"\n"
        "\n" + attachBody + "\n"
        "--MIX--\n";
    file.write(raw);
    file.close();
    return true;
}

/// Writes a multipart/alternative message that DOES carry a text/html part.
bool writeHtmlMessage(const QString &path)
{
    QFile file(path);
    if (!file.open(QIODevice::WriteOnly))
        return false;
    file.write(
        "From: sender@example.org\n"
        "To: you@example.org\n"
        "Subject: Has HTML\n"
        "Message-ID: <html-1@example.org>\n"
        "Date: Fri, 14 Aug 2026 10:00:00 +0200\n"
        "MIME-Version: 1.0\n"
        "Content-Type: multipart/alternative; boundary=\"ALT\"\n"
        "\n"
        "--ALT\n"
        "Content-Type: text/plain; charset=utf-8\n"
        "\n"
        "plain\n"
        "--ALT\n"
        "Content-Type: text/html; charset=utf-8\n"
        "\n"
        "<p>html</p>\n"
        "--ALT--\n");
    file.close();
    return true;
}

}  // namespace

void TestMainWindow::forwardingCarriesTheOriginalsAttachments()
{
    // The spec requires Forward to carry attachments, twice. The context field
    // existed and was never assigned, so a Forward opened with an empty
    // attachment list: the composer looked entirely correct, and the recipient
    // received a body quoting a document that was not attached, with nothing
    // erroring anywhere.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    const QString original = dir.filePath(QStringLiteral("original.eml"));
    QVERIFY(writeMessageWithAttachment(original, QStringLiteral("report.pdf"),
                                       QByteArray("PDFBYTES")));

    QTemporaryDir confDir;
    QVERIFY(confDir.isValid());
    const QString confPath = confDir.filePath(QStringLiteral("qtmaildir.conf"));
    {
        QSettings settings(confPath, QSettings::IniFormat);
        settings.beginGroup(QStringLiteral("account.work"));
        settings.setValue(QStringLiteral("maildir"), QStringLiteral("work"));
        settings.setValue(QStringLiteral("address"),
                          QStringLiteral("you@example.org"));
        settings.setValue(QStringLiteral("send_command"),
                          QStringLiteral("/bin/true"));
        settings.endGroup();
        settings.sync();
    }
    Config config;
    config.load(confPath);

    ComposeContext context;
    context.kind = ComposeContext::Kind::Forward;
    context.accountKey = QStringLiteral("work");
    context.originalPath = original;
    context.subject = QStringLiteral("Fwd: Quarterly report");

    ComposeWindow composer(context, config, dir.path());

    // The attachment is present, and it is a REAL FILE on disk rather than a
    // remembered name: MessageBuilder reads every attachment by path at build
    // time and refuses a build naming one that does not exist.
    const QStringList attached = composer.attachments();
    QCOMPARE(attached.size(), 1);
    QVERIFY2(QFileInfo::exists(attached.first()),
             qPrintable(QStringLiteral("the extracted path does not exist: %1")
                            .arg(attached.first())));
    QCOMPARE(QFileInfo(attached.first()).fileName(),
             QStringLiteral("report.pdf"));

    // And the bytes are the original's, not an empty placeholder.
    QFile written(attached.first());
    QVERIFY(written.open(QIODevice::ReadOnly));
    QCOMPARE(written.readAll(), QByteArray("PDFBYTES"));
    written.close();

    // A Reply to the same message carries NOTHING. The spec says attachments
    // are carried "for Forward, empty otherwise", and a reply that re-attached
    // the original's documents would send them back to their own sender.
    ComposeContext replyContext = context;
    replyContext.kind = ComposeContext::Kind::Reply;
    ComposeWindow replyComposer(replyContext, config, dir.path());
    QVERIFY2(replyComposer.attachments().isEmpty(),
             "a reply carried the original's attachments");
}

void TestMainWindow::forwardSeedsHtmlFromTheConfigNotTheOriginal()
{
    // MEASURED, and it revises what the spec review reported. Forward was
    // NEVER seeding from the original: ComposeWindow::seedFields() already
    // implements the split itself (composewindow.cpp, `isReply ?
    // m_context.seedHtml : m_config.compose().sendHtml`), so the context's
    // value is IGNORED for a forward and the config won regardless. The
    // openComposerFor() line this test also covers was therefore cosmetic
    // rather than a live defect: it stopped the context carrying a value that
    // nothing read, which is worth doing but changed no behaviour.
    //
    // The consequence for this test: EITHER layer alone enforces the rule, so
    // neither single-layer mutation fails it, and only mutating both does.
    // Verified in both directions rather than assumed.
    //
    // The spec splits these: New and Forward seed from [compose] send_html,
    // Reply and Reply-all from whether the original carried a text/html part.
    // An HTML part in the original is a fact about the SENDER's software, so
    // it is the right seed when answering them and says nothing about a
    // forward, which is a new message to somebody else.
    //
    // Asserted on the CONTEXT the window is built from rather than through the
    // checkbox, because what is under test is which source the value comes
    // from. The two sources must DISAGREE or the test passes either way: the
    // config says false while the original is plain text, so reading the
    // original would give false as well. Hence send_html=true against a plain
    // original: config true, original false.
    // The two sources must DISAGREE or the test passes whichever one is read,
    // and getting that wrong is why an earlier version of this survived every
    // mutation: config send_html=FALSE against an original that DOES carry a
    // text/html part. Reading the original gives true, reading the config
    // gives false, so the assertion below can only be satisfied one way.
    WorkerComposeFixture fixture;
    QVERIFY2(fixture.seed({ { QStringLiteral("work"), QStringLiteral("work"),
                              QString(), QStringLiteral("/bin/true"),
                              QStringLiteral("you@example.org") } },
                          QStringLiteral("work/inbox"),
                          QStringLiteral("send_html=false")),
             qPrintable(fixture.backed.error()));

    MainWindow window(fixture.backed.config());
    QTRY_VERIFY_WITH_TIMEOUT(!window.mailRootForTesting().isEmpty(), 15000);
    QCOMPARE(fixture.backed.config().compose().sendHtml, false);

    // The original lives inside the account's maildir so accountForReply()
    // can resolve it; its CONTENT is what matters, not that notmuch indexed it.
    const QString original =
        QDir(window.mailRootForTesting())
            .absoluteFilePath(QStringLiteral("work/inbox/cur/fwd-original"));
    QVERIFY(writeHtmlMessage(original));

    MimeParser parser;
    const ParsedMessage parsed = parser.parse(original);
    QVERIFY(parsed.ok);
    QCOMPARE(parsed.hasHtml(), true);

    // Through openComposerFor(), which is the production line that chooses
    // the source. Building the context by hand here and asserting on the
    // checkbox proved only that ComposeWindow honours what it is given: the
    // mutation putting `original.hasHtml()` back stayed green, because the
    // test was setting seedHtml itself.
    MessageRef ref;
    ref.messageId = QStringLiteral("html-1@example.org");
    ref.filePath = original;
    ref.matched = true;

    window.openComposerForTest(ref, ComposeContext::Kind::Forward, true);

    QList<ComposeWindow *> opened = window.openComposersForTest();
    QCOMPARE(opened.size(), 1);
    auto *sendHtml =
        opened.first()->findChild<QCheckBox *>(QStringLiteral("sendHtml"));
    QVERIFY(sendHtml);
    QVERIFY2(!sendHtml->isChecked(),
             "Forward seeded sendHtml from the original's HTML part rather "
             "than from [compose] send_html");

    // The counterpart, and it is what stops this asserting "always false":
    // a REPLY to the same message seeds from the original, so it is checked
    // where the forward is not. Without this half, disabling the checkbox
    // outright would pass.
    window.openComposerForTest(ref, ComposeContext::Kind::Reply, true);
    const QList<ComposeWindow *> both = window.openComposersForTest();
    QCOMPARE(both.size(), 2);
    auto *replyHtml =
        both.last()->findChild<QCheckBox *>(QStringLiteral("sendHtml"));
    QVERIFY(replyHtml);
    QVERIFY2(replyHtml->isChecked(),
             "Reply did not seed sendHtml from the original's HTML part");

    for (ComposeWindow *composer : both) {
        composer->show();
        composer->close();
    }
}

void TestMainWindow::aStartupAccountScopesTheStartupQuery()
{
    // "Start me in Work - Inbox rather than All accounts - Inbox." The account
    // dropdown is set before the startup query runs, and because a built-in
    // filter COMPOSES with the dropdown, the query it runs is scoped to that
    // account without the filter knowing anything about startup.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    const QString path = dir.filePath(QStringLiteral("qtmaildir.conf"));
    {
        QSettings s(path, QSettings::IniFormat);
        // [general] keys are read WITHOUT the prefix, see writeSentConfig.
        s.setValue(QStringLiteral("startup_query"), QStringLiteral("Inbox"));
        s.setValue(QStringLiteral("startup_account"), QStringLiteral("work"));
        s.beginGroup(QStringLiteral("account.work"));
        s.setValue(QStringLiteral("maildir"), QStringLiteral("work"));
        s.endGroup();
        s.beginGroup(QStringLiteral("account.personal"));
        s.setValue(QStringLiteral("maildir"), QStringLiteral("personal"));
        s.endGroup();
        s.sync();
    }
    Config config;
    config.load(path);

    MainWindow window(config);

    QCOMPARE(window.selectedAccountForTesting(), QStringLiteral("work"));

    auto *queryEdit = window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(queryEdit);
    QCOMPARE(queryEdit->text(),
             QStringLiteral("path:\"work/**\" and (tag:inbox)"));

    // And the query that actually RAN carries the scope, not merely the text in
    // the bar. runQuery() is told the filter is already scoped, so a mistake
    // here would drop the scope rather than double it, and the bar would still
    // look right.
    // The exact string, not contains(): the double-scoped
    //     path:"work/**" and (path:"work/**" and (tag:inbox))
    // contains the scope too, returns exactly the right rows, and passed a
    // contains() assertion while being the very thing item 93 exists to avoid.
    QCOMPARE(window.lastRunQueryForTesting(),
             QStringLiteral("path:\"work/**\" and (tag:inbox)"));
}

void TestMainWindow::aStartupAccountAlsoScopesASavedStartupQuery()
{
    // The other half, and the one the scoping shortcut can silently drop.
    // resolvedQuery(query, accountKey) ignores the account key for a SAVED
    // query, because a saved query states its own scope. So the startup path
    // gets back an unscoped string, and telling runQuery() the query is
    // "already scoped" would leave it unscoped for good, with the dropdown
    // sitting on Work and the list showing everything.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    const QString path = dir.filePath(QStringLiteral("qtmaildir.conf"));
    {
        QSettings s(path, QSettings::IniFormat);
        s.setValue(QStringLiteral("startup_query"), QStringLiteral("Mine"));
        s.setValue(QStringLiteral("startup_account"), QStringLiteral("work"));
        s.beginGroup(QStringLiteral("account.work"));
        s.setValue(QStringLiteral("maildir"), QStringLiteral("work"));
        s.endGroup();
        s.sync();
    }
    QFile queries(dir.filePath(QStringLiteral("queries.json")));
    QVERIFY(queries.open(QIODevice::WriteOnly | QIODevice::Text));
    queries.write(QStringLiteral(R"({
        "version": 1,
        "queries": [ { "name": "Mine", "query": "tag:todo" } ]
    })").toUtf8());
    queries.close();

    Config config;
    config.load(path);

    MainWindow window(config);

    QCOMPARE(window.selectedAccountForTesting(), QStringLiteral("work"));
    QVERIFY2(window.lastRunQueryForTesting()
                 == QStringLiteral("path:\"work/**\" and (tag:todo)"),
             qPrintable(QStringLiteral("the dropdown says Work and the query "
                                       "that ran was: ")
                            + window.lastRunQueryForTesting()));
}

void TestMainWindow::aGeneratedStartupQueryActuallyRuns()
{
    // The second half of the startup defect. The constructor read
    // startup.query directly, and a generated entry stores no query: its text
    // is composed from the accounts at run time. So even once
    // startupSavedQuery() could return a built-in filter, the window opened on
    // an empty bar and ran nothing.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    config.load(writeSentConfig(dir, {
        {QStringLiteral("work"), QStringLiteral("Sent")},
    }, QStringLiteral("Sent")));

    MainWindow window(config);
    auto *queryEdit = window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(queryEdit);

    // Sent, because it is the filter whose query is composed rather than
    // constant: a tag filter would pass against code that only handled the
    // easy half.
    QCOMPARE(queryEdit->text(), config.allSentQuery());
}

void TestMainWindow::everyBuiltinFilterButtonCarriesAnIconAndItsText()
{
    // The filters are part of the application now, so they carry icons like the
    // Save button beside them rather than reading as bare text among the user's
    // own queries.
    //
    // Text BESIDE the icon, not instead of it. The toolbar follows the
    // desktop's own button style, but this row is a row of text buttons: an
    // icon on its own here reads as a different kind of control than it is,
    // which is the same argument the Save button records.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    const QString path = writeSentConfig(dir, {
        {QStringLiteral("work"), QStringLiteral("Sent")},
    });
    // A trash key too, or the Trash filter finds nothing and is skipped from
    // the row entirely (item 103), leaving no trashButton for this loop to
    // find.
    {
        QSettings s(path, QSettings::IniFormat);
        s.beginGroup(QStringLiteral("account.work"));
        s.setValue(QStringLiteral("trash"), QStringLiteral("Trash"));
        s.endGroup();
    }
    Config config;
    config.load(path);

    MainWindow window(config);

    for (const SavedQuery &filter : Config::builtinFilters()) {
        auto *button = window.findChild<QToolButton *>(
            filter.generated + QStringLiteral("Button"));
        QVERIFY2(button, qPrintable(QStringLiteral("no button for filter '%1'")
                                        .arg(filter.generated)));

        QCOMPARE(button->toolButtonStyle(), Qt::ToolButtonTextBesideIcon);
        QCOMPARE(button->text(), filter.name);

        // Whether the icon RESOLVES depends on the running icon theme, which a
        // test cannot assume: QIcon::fromTheme returns a null icon under a
        // platform with no theme installed, so asserting on isNull() would fail
        // for a reason that has nothing to do with this code. What is asserted
        // is that a name was asked for, which is the part that lives here.
        QVERIFY2(!button->icon().name().isEmpty(),
                 qPrintable(QStringLiteral("filter '%1' was given no themed "
                                           "icon name").arg(filter.generated)));
    }
}

void TestMainWindow::aQueryInTheMenuCanActuallyBeRun()
{
    // An unpinned query was UNRUNNABLE. Its action carried both a triggered
    // connection and a submenu of edit actions, and Qt does not emit triggered
    // for an action that owns a menu: clicking it opens the submenu and nothing
    // else. The connection had never fired.
    //
    // It shipped unnoticed because the menu was the rarely-used half while the
    // user's queries were pinned buttons. Item 93 moved every one of them into
    // the menu, which is how it surfaced, and item 94 makes the menu their only
    // home, so this is the path that has to work.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    loadWithQueries(config, dir, QStringLiteral(R"({
        "version": 1,
        "queries": [
            { "name": "Menued", "query": "tag:menued", "pinned": false }
        ]
    })"));

    MainWindow window(config);
    auto *queryEdit = window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(queryEdit);

    auto *menuButton =
        window.findChild<QPushButton *>(QStringLiteral("savedQueryMenuButton"));
    QVERIFY2(menuButton, "no overflow menu for an unpinned query");
    QVERIFY(menuButton->menu());

    QAction *entry = nullptr;
    for (QAction *action : menuButton->menu()->actions()) {
        if (action->text() == QStringLiteral("Menued"))
            entry = action;
    }
    QVERIFY2(entry, "the unpinned query is not in the menu");

    // The entry keeps its submenu, because an unpinned query must still be
    // editable and deletable. What it cannot be is the ONLY thing there: Qt
    // does not emit triggered for an action that owns a menu, so running the
    // query needs an item of its own.
    QVERIFY2(entry->menu(), "the per-query actions are gone");

    QAction *run = nullptr;
    for (QAction *action : entry->menu()->actions()) {
        if (action->objectName() == QStringLiteral("runQuery"))
            run = action;
    }
    QVERIFY2(run, "no way to run the query: its submenu offers only edit "
                  "actions, and Qt never emits triggered for the parent");

    // First, before the edit actions. Running is what the entry is for; editing
    // is what one does to it occasionally.
    QCOMPARE(entry->menu()->actions().constFirst(), run);

    run->trigger();
    QCOMPARE(queryEdit->text(), QStringLiteral("tag:menued"));

    // The edit actions survived beside it.
    QStringList names;
    for (QAction *action : entry->menu()->actions())
        names.append(action->objectName());
    QVERIFY2(names.contains(QStringLiteral("editQuery")),
             "the entry lost Edit");
    QVERIFY2(names.contains(QStringLiteral("deleteQuery")),
             "the entry lost Delete");
}

void TestMainWindow::theFourBuiltinFiltersAreOnTheRowInOrder()
{
    // Shipped, not pinned. Nothing in this config names a query, so a row with
    // four buttons on it can only have got them from the built-in set: before
    // item 93 a fresh install had an empty query row.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    config.load(writeSentConfig(dir, {
        {QStringLiteral("work"), QStringLiteral("Sent")},
    }));

    MainWindow window(config);
    auto *row = window.findChild<QWidget *>(QStringLiteral("savedQueryRow"));
    QVERIFY(row);

    // QAbstractButton: the filters are QToolButtons, so they carry an icon
    // beside their text like the Save button at the other end of the row.
    QStringList labels;
    for (QAbstractButton *button : row->findChildren<QAbstractButton *>()) {
        // The overflow menu is a control over the set, not a member of it.
        if (button->objectName() == QStringLiteral("savedQueryMenuButton"))
            continue;
        labels.append(button->text());
    }

    QCOMPARE(labels, (QStringList{ QStringLiteral("Unread"),
                                   QStringLiteral("Inbox"),
                                   QStringLiteral("Important"),
                                   QStringLiteral("Sent") }));
}

void TestMainWindow::aFilterComposesWithTheSelectedAccount()
{
    // Item 90, and the whole point of item 93. Select an account, hit Unread,
    // and get that account's unread mail rather than everyone's.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    config.load(writeSentConfig(dir, {
        {QStringLiteral("work"), QStringLiteral("Sent")},
        {QStringLiteral("personal"), QStringLiteral("Sent")},
    }));

    MainWindow window(config);
    auto *queryEdit = window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(queryEdit);

    window.selectAccountForTesting(QStringLiteral("work"));
    QCOMPARE(window.selectedAccountForTesting(), QStringLiteral("work"));

    auto *unread =
        window.findChild<QAbstractButton *>(QStringLiteral("unreadButton"));
    QVERIFY2(unread, "no built-in Unread button");
    unread->click();

    QCOMPARE(queryEdit->text(),
             QStringLiteral("path:\"work/**\" and (tag:unread)"));

    // Sent under the same account is the account's OWN folder, not the union
    // wrapped in a scope. See the Config test of the same name for why a row
    // count cannot tell the two apart.
    auto *sent = window.findChild<QAbstractButton *>(QStringLiteral("sentButton"));
    QVERIFY(sent);
    sent->click();
    QCOMPARE(queryEdit->text(), QStringLiteral("path:\"work/Sent/**\""));
    QVERIFY2(!queryEdit->text().contains(QStringLiteral("personal")),
             "another account's sent folder leaked into a scoped Sent filter");
}

void TestMainWindow::aFilterAcrossAllAccountsIsUnscoped()
{
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    config.load(writeSentConfig(dir, {
        {QStringLiteral("work"), QStringLiteral("Sent")},
    }));

    MainWindow window(config);
    auto *queryEdit = window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(queryEdit);

    // "All accounts" is the default selection, so this is the fresh state.
    QVERIFY(window.selectedAccountForTesting().isEmpty());

    auto *unread =
        window.findChild<QAbstractButton *>(QStringLiteral("unreadButton"));
    QVERIFY(unread);
    unread->click();

    QCOMPARE(queryEdit->text(), QStringLiteral("tag:unread"));
}

void TestMainWindow::theActiveFilterButtonIsChecked()
{
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    config.load(writeSentConfig(dir, {
        {QStringLiteral("work"), QStringLiteral("Sent")},
    }));

    MainWindow window(config);
    auto *inbox =
        window.findChild<QAbstractButton *>(QStringLiteral("inboxButton"));
    auto *unread =
        window.findChild<QAbstractButton *>(QStringLiteral("unreadButton"));
    QVERIFY(inbox);
    QVERIFY(unread);

    // Checkable is what lets the style draw the active look at all. A test that
    // only asserted isChecked() would pass against a button that can hold the
    // state and never shows it.
    QVERIFY2(inbox->isCheckable(), "the filter button cannot show a checked state");

    // Unread is checked before anything is clicked, and that is correct rather
    // than incidental: startup_query defaults to Unread, so the window opens
    // showing it and the highlight describes the view from the first frame. The
    // window having opened on a filter is asserted here so the Inbox
    // assertions below are known to be a CHANGE of state rather than a button
    // that happened to start unchecked.
    QVERIFY2(unread->isChecked(),
             "the default startup view is Unread, so its button should open "
             "highlighted");
    QVERIFY(!inbox->isChecked());

    inbox->click();
    QVERIFY2(inbox->isChecked(), "the filter that ran is not highlighted");
    QVERIFY2(!unread->isChecked(), "a filter that did not run is highlighted");

    // And the highlight MOVES rather than accumulating. Exactly one button can
    // describe the current view.
    unread->click();
    QVERIFY(unread->isChecked());
    QVERIFY2(!inbox->isChecked(), "the previous filter stayed highlighted");
}

void TestMainWindow::aHandEditedQueryChecksNoFilterButton()
{
    // The behaviour the user chose over "remember the last click": the
    // highlight describes what is on screen, so editing the query away from a
    // filter's own query clears it rather than leaving a button lit over a view
    // it no longer describes.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    config.load(writeSentConfig(dir, {
        {QStringLiteral("work"), QStringLiteral("Sent")},
    }));

    MainWindow window(config);
    auto *queryEdit = window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    auto *inbox =
        window.findChild<QAbstractButton *>(QStringLiteral("inboxButton"));
    QVERIFY(queryEdit);
    QVERIFY(inbox);

    inbox->click();
    QVERIFY(inbox->isChecked());

    queryEdit->setText(QStringLiteral("from:someone@example.org"));
    QVERIFY2(!inbox->isChecked(),
             "a hand-edited query left the Inbox button highlighted");

    // And typing a filter's query by hand lights it, since the highlight is a
    // property of the query rather than a record of which button was pressed.
    queryEdit->setText(QStringLiteral("tag:inbox"));
    QVERIFY2(inbox->isChecked(),
             "a query equal to the Inbox filter did not highlight it");

    // An empty bar is not "every filter matches nothing", which a naive
    // comparison against an unresolvable query would make it.
    queryEdit->clear();
    QVERIFY(!inbox->isChecked());
}

void TestMainWindow::theCheckedFilterFollowsTheAccount()
{
    // Changing the account re-resolves the filter to a different query string,
    // and both are still "Inbox". The highlight is recomputed rather than
    // dropped, or switching account would silently un-highlight the view the
    // user is still looking at.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    config.load(writeSentConfig(dir, {
        {QStringLiteral("work"), QStringLiteral("Sent")},
        {QStringLiteral("personal"), QStringLiteral("Sent")},
    }));

    MainWindow window(config);
    auto *queryEdit = window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    auto *inbox =
        window.findChild<QAbstractButton *>(QStringLiteral("inboxButton"));
    QVERIFY(queryEdit);
    QVERIFY(inbox);

    window.selectAccountForTesting(QStringLiteral("work"));
    inbox->click();
    QCOMPARE(queryEdit->text(),
             QStringLiteral("path:\"work/**\" and (tag:inbox)"));
    QVERIFY(inbox->isChecked());

    // The query bar still holds work's inbox query, which is NOT personal's, so
    // the button correctly stops describing the view. The state after an
    // account change is asserted rather than assumed: this is the case where a
    // highlight keyed on the last click would go on lying.
    window.selectAccountForTesting(QStringLiteral("personal"));
    QVERIFY2(!inbox->isChecked(),
             "the highlight survived an account change that left a query "
             "belonging to the other account in the bar");

    // Running it again under the new account lights it once more.
    inbox->click();
    QCOMPARE(queryEdit->text(),
             QStringLiteral("path:\"personal/**\" and (tag:inbox)"));
    QVERIFY(inbox->isChecked());
}

void TestMainWindow::aFilterDoesNotClearTheAccountSelection()
{
    // The defect item 90 filed: the button used to reset the dropdown to "All
    // accounts" before running, so the selection was gone before the query ran.
    // A filter leaves it exactly where the user put it.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    config.load(writeSentConfig(dir, {
        {QStringLiteral("work"), QStringLiteral("Sent")},
    }));

    MainWindow window(config);
    window.selectAccountForTesting(QStringLiteral("work"));

    auto *unread =
        window.findChild<QAbstractButton *>(QStringLiteral("unreadButton"));
    QVERIFY(unread);
    unread->click();

    QCOMPARE(window.selectedAccountForTesting(), QStringLiteral("work"));
}

void TestMainWindow::aSavedQueryStillClearsTheAccountSelection()
{
    // The other half of the design, and the reason item 90 was not fixed in
    // place. A saved query is a DESTINATION: it states its own scope, so an
    // unscoped one clears the selection rather than inheriting it. That is the
    // behaviour the rules preview depends on and it must survive item 93.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    const QString path = writeSentConfig(dir, {
        {QStringLiteral("work"), QStringLiteral("Sent")},
    });
    {
        QSettings s(path, QSettings::IniFormat);
        s.beginGroup(QStringLiteral("queries"));
        s.setValue(QStringLiteral("Mine"), QStringLiteral("tag:todo"));
        s.endGroup();
        s.sync();
    }
    config.load(path);

    MainWindow window(config);
    window.selectAccountForTesting(QStringLiteral("work"));

    // The migrated [queries] entry is pinned, so it is a button beside the
    // filters. Found by its label, since only the filters have stable object
    // names.
    QPushButton *mine = nullptr;
    auto *row = window.findChild<QWidget *>(QStringLiteral("savedQueryRow"));
    QVERIFY(row);
    for (QPushButton *button : row->findChildren<QPushButton *>()) {
        if (button->text() == QStringLiteral("Mine"))
            mine = button;
    }
    QVERIFY2(mine, "the user's own pinned query is not on the row");

    mine->click();
    QVERIFY2(window.selectedAccountForTesting().isEmpty(),
             "an unscoped saved query no longer clears the account selection");
}

void TestMainWindow::aFilterOffersNoEditOrDeleteActions()
{
    // A filter is not the user's to edit, rename or delete: it is shipped, and
    // it is not in queries.json at all. Offering the actions would produce a
    // dialog that writes an entry the row does not read.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    config.load(writeSentConfig(dir, {
        {QStringLiteral("work"), QStringLiteral("Sent")},
    }));

    MainWindow window(config);
    auto *unread =
        window.findChild<QAbstractButton *>(QStringLiteral("unreadButton"));
    QVERIFY(unread);

    for (QAction *action : unread->actions()) {
        QVERIFY2(action->objectName() != QStringLiteral("editQuery"),
                 "a built-in filter offered Edit");
        QVERIFY2(action->objectName() != QStringLiteral("pinQuery"),
                 "a built-in filter offered a pin toggle");
        QVERIFY2(action->objectName() != QStringLiteral("deleteQuery"),
                 "a built-in filter offered Delete");
    }
}

void TestMainWindow::changingTheAccountRunsNothing()
{
    // The user's decision, 2026-08-15: "changing the account should not run the
    // query, hitting the button after changing the account is what queries."
    // The dropdown selects scope; the button is the verb.
    //
    // Today m_accountBox has no signal connected at all, so this guards against
    // wiring one up by reflex while making the filters compose.
    QTemporaryDir dir;
    QVERIFY(dir.isValid());
    Config config;
    config.load(writeSentConfig(dir, {
        {QStringLiteral("work"), QStringLiteral("Sent")},
    }));

    MainWindow window(config);
    auto *queryEdit = window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(queryEdit);

    // A known starting point, so "nothing happened" is distinguishable from
    // "it was empty all along", which would pass against a re-run that clears.
    queryEdit->setText(QStringLiteral("tag:todo"));
    const quint64 before = window.currentGenerationForTesting();

    window.selectAccountForTesting(QStringLiteral("work"));

    QCOMPARE(queryEdit->text(), QStringLiteral("tag:todo"));
    QCOMPARE(window.currentGenerationForTesting(), before);
}

void TestMainWindow::arrivingBatchesUpdateTheStatusBarWithTheCountSoFar()
{
    // Item 74: "Searching..." was set once by runQuery and cleared only on
    // queryFinished, so it kept claiming the query was running for the whole
    // cold-cache walk, measured at 5.7 s against a 1.1 GB index, while rows
    // were visibly arriving behind it from 642 ms. A slow query read as a
    // frozen one.
    const Config config;
    MainWindow window(config);

    auto *status =
        window.findChild<QLabel *>(QStringLiteral("statusMessage"));
    QVERIFY2(status, "no status label");
    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);

    window.findChild<QLineEdit *>()->setText(QStringLiteral("tag:inbox"));
    QMetaObject::invokeMethod(&window, "runCurrentQuery");

    // The guard: before any batch, the bar says what it has always said. If
    // this ever stops holding, the assertions below are measuring the wrong
    // thing and would pass against a bar that never changes at all.
    QCOMPARE(status->text(), QStringLiteral("Searching..."));

    const quint64 generation = window.currentGenerationForTesting();
    const QVector<ThreadSummary> first = {
        makeThread(QStringLiteral("t1"), {}),
        makeThread(QStringLiteral("t2"), {})
    };
    QVERIFY(QMetaObject::invokeMethod(
        &window, "onThreadsReady",
        Q_ARG(QVector<ThreadSummary>, first), Q_ARG(quint64, generation)));

    const QString afterFirst = status->text();
    QVERIFY2(afterFirst != QStringLiteral("Searching..."),
             "the bar still claimed the query was running after rows arrived");
    QVERIFY2(afterFirst.contains(QStringLiteral("2")),
             qPrintable(QStringLiteral("no count so far in: ") + afterFirst));

    // A second batch, because the count has to keep moving. A bar that says
    // "2" forever is the same lie in a shorter sentence.
    const QVector<ThreadSummary> second = {
        makeThread(QStringLiteral("t3"), {})
    };
    QVERIFY(QMetaObject::invokeMethod(
        &window, "onThreadsReady",
        Q_ARG(QVector<ThreadSummary>, second), Q_ARG(quint64, generation)));
    QVERIFY2(status->text().contains(QStringLiteral("3")),
             qPrintable(QStringLiteral("count did not advance: ")
                        + status->text()));

    // And the finished text still wins, so the running message cannot outlive
    // the query that armed it.
    // The literal "(s)" is what an untranslated %n plural renders as with no
    // translator loaded, which is the case in the suite. Asserting the plural
    // form Qt would pick under a translation would fail against correct code.
    QMetaObject::invokeMethod(&window, "onQueryFinished", Q_ARG(int, 3),
                              Q_ARG(quint64, generation));
    QCOMPARE(status->text(), QStringLiteral("3 thread(s)"));
}

void TestMainWindow::aRefreshsBatchesLeaveTheStatusBarAlone()
{
    // A refresh after a sync is meant to be silent: onQueryFinished updates
    // the FALLBACK text without stamping over the bar, and its batches must
    // not do what its completion deliberately does not. Without this, a cron
    // sync would overwrite whatever the bar was telling the user with a
    // running count they never asked for.
    const Config config;
    MainWindow window(config);

    auto *status =
        window.findChild<QLabel *>(QStringLiteral("statusMessage"));
    QVERIFY(status);

    window.findChild<QLineEdit *>()->setText(QStringLiteral("tag:inbox"));
    QMetaObject::invokeMethod(&window, "runCurrentQuery");
    const quint64 generation = window.currentGenerationForTesting();
    QMetaObject::invokeMethod(&window, "onQueryFinished", Q_ARG(int, 1),
                              Q_ARG(quint64, generation));

    // Something the user is being told, which the refresh must not erase.
    status->setText(QStringLiteral("Sync complete."));

    // A refresh's own generation. refreshCurrentQuery() needs a worker to set
    // one, and a bare window has none, so the generation is advanced the same
    // way the refresh does and declared to the window through the seam.
    const quint64 refreshGeneration =
        window.beginRefreshForTesting();
    QVERIFY(QMetaObject::invokeMethod(
        &window, "onThreadsReady",
        Q_ARG(QVector<ThreadSummary>, { makeThread(QStringLiteral("t1"), {}) }),
        Q_ARG(quint64, refreshGeneration)));

    QCOMPARE(status->text(), QStringLiteral("Sync complete."));
}

void TestMainWindow::selectingAThreadRootShowsItInTheMessagePane()
{
    // Item 66: "selecting a thread (click on main message), nothing appears in
    // right pane, after expanding and selecting a reply, clicking on the main
    // message correctly shows the email."
    //
    // EXPECTED TO FAIL until that defect is fixed. It is the reproduction the
    // item has needed since 2026-08-04 and could not have while test_mainwindow
    // had no worker to deliver threadLoaded.
    WorkerBackedWindow backed;
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("inbox"), QStringLiteral("root@example.org"),
        QStringLiteral("A conversation"), QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("The first message.")));
    // A real In-Reply-To chain, which is the only way the fixture builds a
    // thread: notmuch groups on the headers, not on the subject.
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("inbox"), QStringLiteral("reply@example.org"),
        QStringLiteral("Re: A conversation"),
        QStringLiteral("other@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 11:00:00 +0200"),
        QStringLiteral("The reply."), true,
        QStringLiteral("root@example.org")));
    QVERIFY2(backed.build(), qPrintable(backed.error()));

    MainWindow window(backed.config());

    QLineEdit *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY2(queryEdit, "no query bar: the window was never built");
    auto *view = window.findChild<ThreadListView *>();
    QVERIFY2(view, "no thread list view");
    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY2(model, "no thread list model");

    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();

    // One thread carrying both messages, not two threads.
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);

    const QModelIndex root = model->index(0, 0, QModelIndex());
    QVERIFY(root.isValid());
    // hasChildren, NOT rowCount. Children are populated only when a thread is
    // expanded, so rowCount(root) is legitimately 0 here and asserting on it
    // fails against correct code. Before expansion the summary's count is all
    // there is, which is also what proves the two messages threaded rather
    // than arriving as two separate rows.
    QVERIFY2(model->hasChildren(root),
             "the two messages did not form one thread: nothing to expand");

    auto *pane = window.findChild<MessageView *>();
    QVERIFY2(pane, "no message view");

    // The pane starts on the placeholder, which is what "blank" means here.
    // Asserting this BEFORE the click is what makes the assertion after it
    // mean something: without it a pane that was never blank would pass.
    QVERIFY2(pane->showingPlaceholder(),
             "the pane was not blank to begin with");

    // Select the ROOT, without expanding it first. That is the gesture the
    // user reports as leaving the pane blank.
    view->setCurrentIndex(root);

    // NOT currentThreadId(): that is assigned synchronously in the selection
    // handler, before any worker round-trip, so it reports the INTENT to show
    // a thread rather than content arriving. A mutation disabling
    // onThreadLoaded() entirely left it passing, which is how that was found.
    //
    // showingPlaceholder() is what the user sees. QTRY rather than a single
    // check because the load crosses to the worker thread and back, so a
    // failure here means the pane stayed blank for fifteen seconds, not that
    // the test looked too early.
    QTRY_VERIFY_WITH_TIMEOUT(!pane->showingPlaceholder(), 15000);
}

void TestMainWindow::anUnexpandedRootRendersOneMessageNotTheConversation()
{
    // Item 66, the half that reproduced. Clicking a thread root that has never
    // been expanded used to render the whole conversation, because the model
    // learned the thread's first message only when the replies loaded. The
    // identical click rendered ONE message afterwards. The user reported the
    // inconsistency and asked for the single-message behaviour throughout.
    WorkerBackedWindow backed;
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("inbox"), QStringLiteral("root@example.org"),
        QStringLiteral("A conversation"), QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("The first message.")));
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("inbox"), QStringLiteral("reply@example.org"),
        QStringLiteral("Re: A conversation"),
        QStringLiteral("other@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 11:00:00 +0200"),
        QStringLiteral("The reply."), true,
        QStringLiteral("root@example.org")));
    QVERIFY2(backed.build(), qPrintable(backed.error()));

    MainWindow window(backed.config());

    QLineEdit *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY2(queryEdit, "no query bar: the window was never built");
    auto *view = window.findChild<ThreadListView *>();
    QVERIFY2(view, "no thread list view");
    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY2(model, "no thread list model");
    auto *pane = window.findChild<MessageView *>();
    QVERIFY2(pane, "no message view");

    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);

    const QModelIndex root = model->index(0, 0, QModelIndex());
    QVERIFY(root.isValid());
    QVERIFY2(model->hasChildren(root), "the two messages did not thread");

    // NEVER expanded. That is the whole point: this is the state in which the
    // old code fell back to the conversation render.
    QVERIFY2(!view->isExpanded(root), "the test expanded the thread itself");

    // The root's message id is known anyway, because the query carries it now.
    QVERIFY2(!model->data(root, ThreadListModel::MessageIdRole)
                  .toString()
                  .isEmpty(),
             "an unexpanded root still has no message id: the query is not "
             "carrying firstMessageId");

    view->setCurrentIndex(root);
    QTRY_VERIFY_WITH_TIMEOUT(!pane->showingPlaceholder(), 15000);

    // ONE message, not a conversation. headerSearchOffers() is populated only
    // when the header states a single message's own From/To/Cc; for a thread
    // the header says "N messages in thread" and carries no such offers, so an
    // empty list here is exactly the conversation render this replaced.
    QVERIFY2(!pane->headerSearchOffers().isEmpty(),
             "the pane rendered a conversation, not a single message");
}

void TestMainWindow::aSingleMessageIdQuerysCardOpensInTheMessagePane()
{
    // Item 66's unverified half, reported again against 0.23.0 with a
    // screenshot: an `id:` query in the bar produces exactly one card, the
    // status bar reports "1 thread selected (1 message)", and the pane stays
    // on the placeholder.
    //
    // The id is shaped like the real one that fails: dots, digits and an @.
    //
    // An ACCOUNT is configured and selected, because that is the state the
    // report was made from and it is the only thing that changes the query:
    // runQuery() wraps the bar's text in path:"<maildir>/**" and (...).
    WorkerBackedWindow backed;
    const QString wanted =
        QStringLiteral("1786718040388.1f6b48f1-64d1@mail.example.org");
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/Inbox"), wanted,
        QStringLiteral("A single message"),
        QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("The only message.")));
    QVERIFY2(backed.build(QStringLiteral("acct"), QStringLiteral("acct")),
             qPrintable(backed.error()));

    MainWindow window(backed.config());

    QLineEdit *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY2(queryEdit, "no query bar: the window was never built");
    auto *view = window.findChild<ThreadListView *>();
    QVERIFY2(view, "no thread list view");
    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY2(model, "no thread list model");
    auto *pane = window.findChild<MessageView *>();
    QVERIFY2(pane, "no message view");

    auto *accountBox =
        window.findChild<QComboBox *>(QStringLiteral("accountBox"));
    QVERIFY2(accountBox, "no account dropdown");
    const int account = accountBox->findData(QStringLiteral("acct"));
    QVERIFY2(account >= 0, "the configured account is not in the dropdown");
    accountBox->setCurrentIndex(account);

    // THE SAME MESSAGE IS READ FIRST, and that is the whole defect. The id is
    // copied out of the details dialog of a message being read, so the `id:`
    // query is always typed while that very thread is the current one.
    // runQuery() blanks the pane but leaves m_currentThreadId naming it, so
    // when the row comes back onSelectionChanged() compares the two, finds them
    // equal, and never calls onThreadSelected: nothing is ever loaded.
    //
    // A first query returning a DIFFERENT thread passes against the bug, which
    // is why the earlier version of this test was green.
    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);
    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    QTRY_VERIFY_WITH_TIMEOUT(!pane->showingPlaceholder(), 15000);
    const QString firstThreadId = window.currentThreadId();
    QVERIFY2(!firstThreadId.isEmpty(), "the first view never opened a thread");

    // Exactly what the user types, `id:` and the bare id, unquoted.
    queryEdit->setText(QStringLiteral("id:%1").arg(wanted));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);

    const QModelIndex root = model->index(0, 0, QModelIndex());
    QVERIFY(root.isValid());

    // The row must name the message before the click can be blamed for
    // anything: an empty MessageIdRole is a different defect and would make
    // the assertion below true for the wrong reason.
    QCOMPARE(model->data(root, ThreadListModel::MessageIdRole).toString(),
             wanted);

    QVERIFY2(pane->showingPlaceholder(),
             "the pane was not blank to begin with");
    // The row IS the thread that was showing when the query ran. Asserted so a
    // fixture change that made them different threads could not quietly turn
    // this back into the passing test it was before the cause was found.
    QCOMPARE(model->data(root, ThreadListModel::ThreadIdRole).toString(),
             firstThreadId);

    // The pane was blanked, so nothing is on display and the window must not
    // still claim otherwise. This is the fix's own contract: leave it set and
    // the selection below is read as "already showing" and never loads.
    QVERIFY2(window.currentThreadId().isEmpty(),
             "runQuery blanked the pane but still names a current thread");

    view->setCurrentIndex(root);
    QTRY_VERIFY_WITH_TIMEOUT(!pane->showingPlaceholder(), 15000);
}

/// Whether any file in `dir` belongs to the message whose filename starts with
/// `stem`.
///
/// A Maildir filename is NOT stable across a move, which is the trap this
/// exists to avoid. `maildir.synchronize_flags` is on, so notmuch rewrites the
/// name to carry the read/seen flags: a message that leaves `new/del1.x` lands
/// as `cur/del1.x:2,S`. Asserting on the exact basename therefore fails
/// against a move that worked perfectly, which is how three of these tests
/// first "failed".
/// Counts messages matching `query` in the fixture's database, by running
/// notmuch itself.
///
/// Asked directly rather than through the query bar because the UI's
/// rowCount() reads 0 for the whole interval before the worker answers, so an
/// assertion that a tag is ABSENT is satisfied by the gap before any answer
/// arrives and passes against a database that still carries the tag.
static int notmuchCount(const QString &configPath, const QString &query)
{
    QProcess process;
    QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
    env.insert(QStringLiteral("NOTMUCH_CONFIG"), configPath);
    process.setProcessEnvironment(env);
    process.start(QStringLiteral("notmuch"),
                  { QStringLiteral("count"), query });
    if (!process.waitForFinished(15000))
        return -1;
    bool ok = false;
    const int count =
        QString::fromUtf8(process.readAllStandardOutput()).trimmed().toInt(&ok);
    return ok ? count : -1;
}

/// Applies a tag change with the notmuch binary, for the one thing the UI
/// cannot produce any more: a message tagged `deleted` while its file is still
/// in the inbox. That is the state the OLD Delete left mail in, and the state
/// the cleanup action exists to find, so a test for it has to write it
/// directly rather than through an action that now moves the file too.
static bool notmuchTag(const QString &configPath, const QStringList &args)
{
    QProcess process;
    QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
    env.insert(QStringLiteral("NOTMUCH_CONFIG"), configPath);
    process.setProcessEnvironment(env);
    process.start(QStringLiteral("notmuch"),
                  QStringList{ QStringLiteral("tag") } + args);
    return process.waitForFinished(15000) && process.exitCode() == 0;
}

/// Whether `dir` holds the message whose id the fixture wrote as `stem`.
///
/// Matches on the MESSAGE-ID INSIDE each file, never on the filename. It used
/// to compare filenames, which worked only while a move carried the name
/// across unchanged. It no longer does: mbsync requires an MUA to rename a
/// file when it moves it between folders, so `NotmuchWorker::moveMessages()`
/// generates a fresh name and this helper could never find a moved message
/// again. Every one of these assertions failed at once, each reporting "the
/// file is not there" about a file that was.
///
/// `stem` stays in the fixture's `<local>.example.org` form so the fifty-odd
/// call sites did not have to change; it is turned back into
/// `<local@example.org>` here.
static bool folderHasMessageFile(const QString &dir, const QString &stem)
{
    QDir directory(dir);
    if (!directory.exists())
        return false;

    // `del1.example.org` is the fixture's rendering of `del1@example.org`:
    // it replaces the `@` to make a filename-safe stem. Only the LAST dot
    // before the domain is the substituted one, so the split is on the first
    // dot, which is where the local part ends for every id these tests use.
    const int dot = stem.indexOf(QLatin1Char('.'));
    if (dot < 0)
        return false;
    const QString messageId = QStringLiteral("<%1@%2>")
                                  .arg(stem.left(dot), stem.mid(dot + 1));

    const QStringList entries = directory.entryList(QDir::Files);
    for (const QString &entry : entries) {
        QFile file(directory.filePath(entry));
        if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
            continue;
        // The header block only: a quoted id in a body must not count.
        while (!file.atEnd()) {
            const QByteArray line = file.readLine();
            if (line.trimmed().isEmpty())
                break;
            if (line.startsWith("Message-ID:") || line.startsWith("Message-Id:")) {
                if (QString::fromUtf8(line).contains(messageId))
                    return true;
                break;
            }
        }
    }
    return false;
}

void TestMainWindow::deleteMovesTheMessageToTrash()
{
    // The whole point of item 103. Before it, Delete added a tag and moved no
    // file, so deleted mail sat in the inbox for good.
    WorkerBackedWindow backed;
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("del1@example.org"),
        QStringLiteral("Delete me"), QStringLiteral("sender@example.org"),
        // Friday, verified with `date -d 2026-08-14 +%A`.
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY2(backed.build(QStringLiteral("acct"), QStringLiteral("acct"),
                          QStringLiteral("Trash")),
             qPrintable(backed.error()));

    MainWindow window(backed.config());
    auto *model = window.findChild<ThreadListModel *>();
    QVERIFY(model);
    auto *view = window.findChild<ThreadListView *>();
    QVERIFY(view);
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(queryEdit);

    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);

    const QString root = backed.fixture().maildirPath();
    const QString inbox = root + QStringLiteral("/acct/inbox/new");
    const QString stem = QStringLiteral("del1.example.org");
    QVERIFY(folderHasMessageFile(inbox, stem));

    view->setCurrentIndex(model->index(0, 0, QModelIndex()));

    auto *del = window.findChild<QAction *>(QStringLiteral("delete"));
    QVERIFY(del);
    del->trigger();

    // The filesystem half. cur/, never new/: a file in new/ is re-announced as
    // fresh mail by every reader of the Maildir.
    const QString trash = root + QStringLiteral("/acct/Trash/cur");
    QTRY_VERIFY_WITH_TIMEOUT(folderHasMessageFile(trash, stem), 15000);
    QVERIFY2(!folderHasMessageFile(inbox, stem),
             "the file is in the trash and still in the inbox");
    QVERIFY2(!folderHasMessageFile(root + QStringLiteral("/acct/inbox/cur"),
                                   stem),
             "the file is in the trash and still in the inbox");

    // The index half, which the filesystem cannot see. A moved file with a
    // stale index entry sits correctly on disk and is invisible to every query.
    queryEdit->setText(QStringLiteral("path:\"acct/Trash/**\""));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);
}

void TestMainWindow::deleteRecordsWhereTheMessageCameFrom()
{
    // A Maildir filename does not record where a message came from, and once
    // the file has moved notmuch cannot know either. The tag is the only
    // record, and Restore needs it days later.
    WorkerBackedWindow backed;
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("del2@example.org"),
        QStringLiteral("Delete me too"), QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY2(backed.build(QStringLiteral("acct"), QStringLiteral("acct"),
                          QStringLiteral("Trash")),
             qPrintable(backed.error()));

    MainWindow window(backed.config());
    auto *model = window.findChild<ThreadListModel *>();
    auto *view = window.findChild<ThreadListView *>();
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(model && view && queryEdit);

    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);

    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    window.findChild<QAction *>(QStringLiteral("delete"))->trigger();

    // Asked of the database, not of the model: the model's optimistic update
    // would report the tag whether or not the write ever landed.
    // Re-queried by id and asserted on the TAG LIST the database returns.
    //
    // Not with `tag:"deleted-from:inbox"` in the query: notmuch's parser does
    // not match a quoted tag containing a colon that way, so such a query
    // returns nothing against a perfectly tagged message and reads as the
    // feature being broken. Asking for the message and inspecting its tags
    // cannot fail that way.
    // Re-run per attempt, not once. The tag write is QUEUED behind the move,
    // so a single query can land before the tags do; QTRY_VERIFY on the
    // model's contents would then re-test a result that can never change,
    // because nothing re-asks the database. Asking again each time is what
    // makes this wait for the write rather than for the clock.
    bool tagged = false;
    for (int attempt = 0; attempt < 30 && !tagged; ++attempt) {
        queryEdit->setText(QStringLiteral("id:del2@example.org"));
        queryEdit->returnPressed();
        QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);
        const QStringList tags = model->threadAt(0).tags;
        tagged = tags.contains(QStringLiteral("deleted"))
                 && tags.contains(QStringLiteral("deleted-from:inbox"));
        if (!tagged)
            QTest::qWait(200);
    }
    QVERIFY2(tagged,
             qPrintable(QStringLiteral("tags after the delete: %1")
                            .arg(model->threadAt(0).tags.join(
                                QLatin1Char(' ')))));
}

void TestMainWindow::deletingTwiceLeavesNoOriginTagBehind()
{
    // Delete twice is the ordinary way back: the action toggles, so a second
    // press on a deleted message restores it. That path is NOT the undo path
    // and had its own defect.
    //
    // onMessagesMoved() resolved the origin placeholder from the folder the
    // WORKER reported, which is where the message came FROM. On a delete that
    // is the inbox and correct. On a restore it is the TRASH, so the restore
    // asked to remove `deleted-from:Trash`, a tag that had never been written,
    // while the real `deleted-from:inbox` was never named and stayed on the
    // message. It came home still claiming to have been deleted from
    // somewhere, which makes Restore offer to move a message already at home.
    //
    // Reported from a hand test. The undo test passed throughout, because undo
    // carries its tags on the command and never resolves a placeholder.
    WorkerBackedWindow backed;
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("twice@example.org"),
        QStringLiteral("Delete me twice"), QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY2(backed.build(QStringLiteral("acct"), QStringLiteral("acct"),
                          QStringLiteral("Trash")),
             qPrintable(backed.error()));

    MainWindow window(backed.config());
    auto *model = window.findChild<ThreadListModel *>();
    auto *view = window.findChild<ThreadListView *>();
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(model && view && queryEdit);

    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);

    const QString root = backed.fixture().maildirPath();
    const QString stem = QStringLiteral("twice.example.org");
    const QString trash = root + QStringLiteral("/acct/Trash/cur");

    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    window.findChild<QAction *>(QStringLiteral("delete"))->trigger();
    QTRY_VERIFY_WITH_TIMEOUT(folderHasMessageFile(trash, stem), 15000);

    // The origin tag really was written, so the assertion after the second
    // delete is about it being REMOVED rather than never having existed.
    //
    // Asked of the DATABASE, not through the query bar. The file arriving in
    // the trash is not the end of the delete: the tag writes land after the
    // rename this test waits for, and a query bar run inside that gap returns
    // zero rows FOREVER, because QTRY_VERIFY re-reads rowCount() and never
    // re-runs the query. Measured 1 failure in 3 runs, each burning the full
    // 15s timeout on a guard that was correct about a database it had asked
    // too early.
    QTRY_VERIFY_WITH_TIMEOUT(
        notmuchCount(backed.fixture().configPath(),
                     QStringLiteral("id:twice@example.org and "
                                    "tag:\"deleted-from:inbox\"")) == 1,
        15000);

    // Second press on the same message, which restores it.
    queryEdit->setText(QStringLiteral("id:twice@example.org"));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);
    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    window.findChild<QAction *>(QStringLiteral("delete"))->trigger();

    QTRY_VERIFY_WITH_TIMEOUT(
        folderHasMessageFile(root + QStringLiteral("/acct/inbox/cur"), stem)
            || folderHasMessageFile(root + QStringLiteral("/acct/inbox/new"),
                                    stem),
        15000);

    // The file arriving is NOT the end of the restore. The tags are written
    // only once the worker confirms the move, so the writes land after the
    // rename the assertion above waits for. Querying in that gap reads the
    // state before the restore finished tagging, which is how an earlier
    // version of this test passed against the bug it exists to catch.
    //
    // Waited on the `deleted` tag, which the restore removes on every code
    // path, rather than on a fixed sleep.
    QTRY_VERIFY_WITH_TIMEOUT(
        notmuchCount(backed.fixture().configPath(),
                     QStringLiteral("id:twice@example.org and tag:deleted"))
            == 0,
        15000);

    // BOTH tags gone, asked of the database. `deleted-from:` left behind is
    // the defect this covers, and it survived a green suite before.
    // The origin tag specifically, asserted on its OWN query.
    //
    // A combined `tag:deleted or tag:"deleted-from:inbox"` query is NOT
    // equivalent and passed against the bug: `deleted` is removed correctly
    // and promptly, so the disjunction went to zero on that term alone while
    // the origin tag was still on the message. Split, so the assertion can
    // only be satisfied by the tag it names.
    // Asked of notmuch DIRECTLY, not through the query bar.
    //
    // A UI query cannot answer this reliably: rowCount() is 0 for the whole
    // interval before the worker replies, so QTRY_VERIFY(rowCount() == 0) is
    // satisfied instantly by the empty pre-result and passes against any
    // state of the database. Measured while building this test: 0 right after
    // returnPressed(), 1 once the answer actually landed. The database is the
    // thing under test here, so it is asked directly.
    const QString cfg = backed.fixture().configPath();

    // The message still exists: an assertion that a tag is absent would be
    // satisfied just as well by the message having vanished.
    QCOMPARE(notmuchCount(cfg, QStringLiteral("id:twice@example.org")), 1);

    // The origin tag is gone. This is the defect: it used to survive the
    // restore, because the placeholder resolved to `deleted-from:Trash`, the
    // folder the message was coming FROM, and stripped a tag that had never
    // been written.
    QCOMPARE(notmuchCount(cfg,
                          QStringLiteral("id:twice@example.org and "
                                         "tag:\"deleted-from:inbox\"")),
             0);

    // And no tag naming the trash was invented in its place.
    QCOMPARE(notmuchCount(cfg,
                          QStringLiteral("id:twice@example.org and "
                                         "tag:\"deleted-from:Trash\"")),
             0);

    // `deleted` itself, so a fix that dropped this one instead cannot hide.
    QCOMPARE(notmuchCount(
                 cfg, QStringLiteral("id:twice@example.org and tag:deleted")),
             0);

}

void TestMainWindow::undoOfADeleteRemovesTheOriginTagToo()
{
    // Ctrl+Z is a THIRD way back, beside the second Delete, and it had the
    // same defect for a different reason.
    //
    // MoveCommand was constructed with pending.add, which still holds the
    // unresolved origin PLACEHOLDER: onMessagesMoved() resolved the
    // placeholder for the tags it wrote to the database, but handed the undo
    // command the raw list. Undo then asked to remove a tag by the
    // placeholder's literal name, which no message carries, so the removal
    // was a silent no-op and `deleted-from:inbox` survived. The message came
    // home still claiming to have been deleted from somewhere, which makes
    // Restore offer to move a message that is already at home.
    //
    // Reported from a hand test after the second-Delete path was fixed: that
    // fix did not touch this one, and the existing undo test asserted on the
    // file's location rather than on its tags.
    WorkerBackedWindow backed;
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("undotag@example.org"),
        QStringLiteral("Undo my tags"), QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY2(backed.build(QStringLiteral("acct"), QStringLiteral("acct"),
                          QStringLiteral("Trash")),
             qPrintable(backed.error()));

    MainWindow window(backed.config());
    auto *model = window.findChild<ThreadListModel *>();
    auto *view = window.findChild<ThreadListView *>();
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(model && view && queryEdit);

    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);

    const QString root = backed.fixture().maildirPath();
    const QString stem = QStringLiteral("undotag.example.org");
    const QString cfg = backed.fixture().configPath();

    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    window.findChild<QAction *>(QStringLiteral("delete"))->trigger();
    QTRY_VERIFY_WITH_TIMEOUT(
        folderHasMessageFile(root + QStringLiteral("/acct/Trash/cur"), stem),
        15000);

    // The origin tag really was written, so the assertion after the undo is
    // about it being REMOVED rather than never having existed.
    QTRY_VERIFY_WITH_TIMEOUT(
        notmuchCount(cfg, QStringLiteral("id:undotag@example.org and "
                                         "tag:\"deleted-from:inbox\"")) == 1,
        15000);

    window.findChild<QAction *>(QStringLiteral("undo"))->trigger();
    QTRY_VERIFY_WITH_TIMEOUT(
        folderHasMessageFile(root + QStringLiteral("/acct/inbox/cur"), stem)
            || folderHasMessageFile(root + QStringLiteral("/acct/inbox/new"),
                                    stem),
        15000);

    // The file arriving is not the end of the undo: the tags are written only
    // once the worker confirms the move, so they land after the rename.
    QTRY_VERIFY_WITH_TIMEOUT(
        notmuchCount(cfg,
                     QStringLiteral("id:undotag@example.org and tag:deleted"))
            == 0,
        15000);

    // Asked of notmuch directly. A UI query cannot answer this: rowCount() is
    // 0 for the whole interval before the worker replies, so an assertion
    // that a tag is absent is satisfied by the gap before any answer arrives.
    QCOMPARE(notmuchCount(cfg, QStringLiteral("id:undotag@example.org")), 1);
    QCOMPARE(notmuchCount(cfg,
                          QStringLiteral("id:undotag@example.org and "
                                         "tag:\"deleted-from:inbox\"")),
             0);
    QCOMPARE(notmuchCount(cfg,
                          QStringLiteral("id:undotag@example.org and "
                                         "tag:\"deleted-from:Trash\"")),
             0);
}

void TestMainWindow::deletingAThreadRootTwiceRestoresItRatherThanRedeleting()
{
    // The toggle asked a THREAD ROW about its thread's tags, which notmuch
    // gives as a UNION over the conversation. Delete the root of a
    // three-message thread and the two replies are untouched, so the union
    // carries no `deleted`, so a second press read the row as not-deleted and
    // ran Delete AGAIN: the message was moved trash-to-trash and came out
    // carrying `deleted`, `deleted-from:inbox` AND `deleted-from:Trash`, with
    // no way back, since a later restore would send it to the trash it now
    // claims to have come from.
    //
    // The union was a documented approximation, called bounded because the
    // worst case for a TAG toggle was re-applying a tag the message already
    // had, which is a no-op. A MOVE re-applies the move. The comment outlived
    // the code it described.
    //
    // The row must be left ALONE between the two presses: a re-query rebuilds
    // it from the database and hides the defect, which is why an earlier
    // version of this probe passed. The user's gesture is two presses on the
    // list as it stands.
    WorkerBackedWindow backed;
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("troot@example.org"),
        QStringLiteral("Thread root"), QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Root body.")));
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("trep1@example.org"),
        QStringLiteral("Re: Thread root"), QStringLiteral("other@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 11:00:00 +0200"),
        QStringLiteral("Reply one."), true,
        QStringLiteral("troot@example.org")));
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("trep2@example.org"),
        QStringLiteral("Re: Thread root"), QStringLiteral("third@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 12:00:00 +0200"),
        QStringLiteral("Reply two."), true,
        QStringLiteral("troot@example.org")));
    QVERIFY2(backed.build(QStringLiteral("acct"), QStringLiteral("acct"),
                          QStringLiteral("Trash")),
             qPrintable(backed.error()));

    MainWindow window(backed.config());
    auto *model = window.findChild<ThreadListModel *>();
    auto *view = window.findChild<ThreadListView *>();
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(model && view && queryEdit);

    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);

    const QString root = backed.fixture().maildirPath();
    const QString cfg = backed.fixture().configPath();
    const QString stem = QStringLiteral("troot.example.org");
    const QString trash = root + QStringLiteral("/acct/Trash/cur");

    // Three messages, so the union genuinely differs from the root's own
    // tags. With one message the two are identical and the defect cannot
    // appear at all.
    QCOMPARE(notmuchCount(cfg, QStringLiteral("thread:{id:troot@example.org}")),
             3);

    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    window.findChild<QAction *>(QStringLiteral("delete"))->trigger();
    QTRY_VERIFY_WITH_TIMEOUT(folderHasMessageFile(trash, stem), 15000);
    QTRY_VERIFY_WITH_TIMEOUT(
        notmuchCount(cfg, QStringLiteral("id:troot@example.org and "
                                         "tag:\"deleted-from:inbox\"")) == 1,
        15000);

    // Only the root moved. The replies are what make the union disagree, so
    // this is also the guard the rest of the test depends on.
    QCOMPARE(notmuchCount(cfg, QStringLiteral("id:trep1@example.org and "
                                              "tag:deleted")),
             0);
    QCOMPARE(notmuchCount(cfg, QStringLiteral("id:trep2@example.org and "
                                              "tag:deleted")),
             0);

    // Second press on the row as it stands, no re-query.
    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    window.findChild<QAction *>(QStringLiteral("delete"))->trigger();

    QTRY_VERIFY_WITH_TIMEOUT(
        folderHasMessageFile(root + QStringLiteral("/acct/inbox/cur"), stem)
            || folderHasMessageFile(root + QStringLiteral("/acct/inbox/new"),
                                    stem),
        15000);
    QTRY_VERIFY_WITH_TIMEOUT(
        notmuchCount(cfg,
                     QStringLiteral("id:troot@example.org and tag:deleted"))
            == 0,
        15000);

    // Asked of notmuch directly: a UI query reads 0 rows for the whole
    // interval before the worker answers, so an absence assertion through the
    // query bar passes against any state of the database.
    QCOMPARE(notmuchCount(cfg, QStringLiteral("id:troot@example.org")), 1);
    QCOMPARE(notmuchCount(cfg, QStringLiteral("id:troot@example.org and "
                                              "tag:\"deleted-from:inbox\"")),
             0);
    // The tag the re-delete invented. Its presence is the signature of this
    // defect rather than a variation on the origin-tag ones.
    QCOMPARE(notmuchCount(cfg, QStringLiteral("id:troot@example.org and "
                                              "tag:\"deleted-from:Trash\"")),
             0);
    QVERIFY2(!folderHasMessageFile(trash, stem),
             "the second press left the message in the trash");
}

void TestMainWindow::deleteThreadMovesEveryMessageAndRepaintsTheRootCard()
{
    // Two defects in one gesture, both reported from a hand test.
    //
    // Delete thread never moved anything: it was left calling tagSelected()
    // when Delete became a move, so a whole conversation stayed in the inbox
    // wearing a `deleted` chip, which is the half-deleted state item 103
    // existed to remove. It moves every message now, each carrying its own
    // `deleted-from:` origin so a thread spanning folders reassembles.
    //
    // And the ROOT card did not repaint until it was clicked, while its
    // replies did. A thread-scoped move updated each message's node;
    // applyMessageTagChange() deliberately leaves a multi-message thread's
    // SUMMARY alone, because one message's edit does not describe the
    // conversation. The replies have nodes and repainted; the root card reads
    // the summary and did not. A thread-scoped move DID change every message,
    // so the summary genuinely moves and applyTagChange() is the right update.
    //
    // The stale summary was also why a second press did nothing: the toggle
    // asks the summary for its direction and kept reading "not deleted".
    WorkerBackedWindow backed;
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("dt0@example.org"),
        QStringLiteral("DT root"), QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Root body.")));
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("dt1@example.org"),
        QStringLiteral("Re: DT root"), QStringLiteral("other@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 11:00:00 +0200"),
        QStringLiteral("Reply one."), true, QStringLiteral("dt0@example.org")));
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("dt2@example.org"),
        QStringLiteral("Re: DT root"), QStringLiteral("third@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 12:00:00 +0200"),
        QStringLiteral("Reply two."), true, QStringLiteral("dt0@example.org")));
    QVERIFY2(backed.build(QStringLiteral("acct"), QStringLiteral("acct"),
                          QStringLiteral("Trash")),
             qPrintable(backed.error()));

    MainWindow window(backed.config());
    auto *model = window.findChild<ThreadListModel *>();
    auto *view = window.findChild<ThreadListView *>();
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(model && view && queryEdit);

    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);

    const QString root = backed.fixture().maildirPath();
    const QString cfg = backed.fixture().configPath();
    const QString trash = root + QStringLiteral("/acct/Trash/cur");
    const QString thread = QStringLiteral("thread:{id:dt0@example.org}");

    // Three messages, so a thread-scoped action is distinguishable from a
    // message-scoped one at all.
    QCOMPARE(notmuchCount(cfg, thread), 3);

    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    view->expand(model->index(0, 0, QModelIndex()));
    window.findChild<QAction *>(QStringLiteral("delete_thread"))->trigger();

    // Every message MOVED, not merely tagged. This is the half that was
    // missing entirely: the action tagged and moved nothing.
    QTRY_VERIFY_WITH_TIMEOUT(
        folderHasMessageFile(trash, QStringLiteral("dt0.example.org"))
            && folderHasMessageFile(trash, QStringLiteral("dt1.example.org"))
            && folderHasMessageFile(trash, QStringLiteral("dt2.example.org")),
        15000);
    QTRY_VERIFY_WITH_TIMEOUT(
        notmuchCount(cfg, thread + QStringLiteral(" and tag:deleted")) == 3,
        15000);
    // Each with its own origin, which is what makes the move reversible.
    QCOMPARE(notmuchCount(cfg, thread
                                   + QStringLiteral(" and "
                                                    "tag:\"deleted-from:inbox\"")),
             3);

    // The ROOT CARD's own state, which is what the user watches. Read from the
    // summary because that is what a thread row draws, and it is the value
    // that stayed stale: the replies repainted and the root did not.
    QVERIFY2(model->threadAt(0).tags.contains(QStringLiteral("deleted")),
             "the root card still reads as not deleted, so it paints "
             "undeleted until the row is clicked");

    // Second press restores the whole thread, which only works if the toggle
    // can see the state the first press produced.
    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    window.findChild<QAction *>(QStringLiteral("delete_thread"))->trigger();

    QTRY_VERIFY_WITH_TIMEOUT(
        notmuchCount(cfg, thread + QStringLiteral(" and tag:deleted")) == 0,
        15000);

    // Home, and nothing left behind in the trash.
    QCOMPARE(notmuchCount(cfg, thread), 3);
    QCOMPARE(notmuchCount(cfg, thread
                                   + QStringLiteral(" and "
                                                    "tag:\"deleted-from:inbox\"")),
             0);
    QVERIFY(!folderHasMessageFile(trash, QStringLiteral("dt0.example.org")));
    QVERIFY(!folderHasMessageFile(trash, QStringLiteral("dt1.example.org")));
    QVERIFY(!folderHasMessageFile(trash, QStringLiteral("dt2.example.org")));
}

void TestMainWindow::aFolderNameWithASpaceSurvivesTheRoundTrip()
{
    // A notmuch tag MAY contain a space, and a Maildir folder name may too.
    // The worker reported each message's tags as one space-joined string, so
    // `deleted-from:Inbox/SlackBuilds users` was split back into
    // "deleted-from:Inbox/SlackBuilds" and "users", and Restore moved the
    // messages to the truncated folder, CREATING it. On the user's real
    // Maildir that put four messages into a directory mbsync does not sync,
    // beside the real folder of 808, and they read as missing.
    //
    // The leftover origin tag was the visible half: the restore stripped the
    // truncated name, which no message carried, so the real tag stayed on.
    //
    // Separator is a TAB now. A tag cannot contain one, since notmuch's own
    // dump format is line-based and whitespace-delimited.
    WorkerBackedWindow backed;
    const QString folder = QStringLiteral("acct/Inbox/SlackBuilds users");
    QVERIFY(backed.fixture().addMessage(
        folder, QStringLiteral("sp0@example.org"), QStringLiteral("SP root"),
        QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Root body.")));
    QVERIFY(backed.fixture().addMessage(
        folder, QStringLiteral("sp1@example.org"),
        QStringLiteral("Re: SP root"), QStringLiteral("other@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 11:00:00 +0200"),
        QStringLiteral("Reply."), true, QStringLiteral("sp0@example.org")));
    QVERIFY2(backed.build(QStringLiteral("acct"), QStringLiteral("acct"),
                          QStringLiteral("Trash")),
             qPrintable(backed.error()));

    MainWindow window(backed.config());
    auto *model = window.findChild<ThreadListModel *>();
    auto *view = window.findChild<ThreadListView *>();
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(model && view && queryEdit);

    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);

    const QString root = backed.fixture().maildirPath();
    const QString cfg = backed.fixture().configPath();
    const QString thread = QStringLiteral("thread:{id:sp0@example.org}");
    const QString home = root + QLatin1Char('/') + folder;

    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    window.findChild<QAction *>(QStringLiteral("delete_thread"))->trigger();

    QTRY_VERIFY_WITH_TIMEOUT(
        notmuchCount(cfg, thread + QStringLiteral(" and tag:deleted")) == 2,
        15000);

    // The origin tag carries the WHOLE folder name, space included.
    QCOMPARE(notmuchCount(cfg,
                          thread
                              + QStringLiteral(" and tag:\"deleted-from:"
                                               "Inbox/SlackBuilds users\"")),
             2);

    // Back again.
    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    window.findChild<QAction *>(QStringLiteral("delete_thread"))->trigger();

    QTRY_VERIFY_WITH_TIMEOUT(
        notmuchCount(cfg, thread + QStringLiteral(" and tag:deleted")) == 0,
        15000);

    // No origin tag left behind. This is the half the user saw: a tag they
    // could see, could not type, and could not remove.
    QCOMPARE(notmuchCount(cfg,
                          thread
                              + QStringLiteral(" and tag:\"deleted-from:"
                                               "Inbox/SlackBuilds users\"")),
             0);
    // Nor a truncated one, which is what a space-split would have written.
    QCOMPARE(notmuchCount(cfg,
                          thread
                              + QStringLiteral(" and tag:\"deleted-from:"
                                               "Inbox/SlackBuilds\"")),
             0);

    // Home, in the folder with the space in its name.
    QVERIFY2(folderHasMessageFile(home + QStringLiteral("/cur"),
                                  QStringLiteral("sp0.example.org")),
             "the root did not come back to the folder it was deleted from");
    QVERIFY2(folderHasMessageFile(home + QStringLiteral("/cur"),
                                  QStringLiteral("sp1.example.org")),
             "the reply did not come back to the folder it was deleted from");

    // And the truncated folder was never created. Its existence is the defect
    // that hid four real messages from the user and from mbsync.
    QVERIFY2(!QDir(root + QStringLiteral("/acct/Inbox/SlackBuilds")).exists(),
             "a folder named after the truncated origin was created, so the "
             "messages are somewhere mbsync will never sync");
}

void TestMainWindow::deleteIsBoundToTheDeleteKey()
{
    // Del is the key a user reaches for, and Ctrl+D is not a guess anyone
    // makes. Both are bound; this asserts the bare one is really there,
    // since setShortcut() keeps only the LAST of several and silently drops
    // the rest, which would leave the documented binding absent.
    const Config config;
    MainWindow window(config);

    auto *action = window.findChild<QAction *>(QStringLiteral("delete"));
    QVERIFY(action);

    QVERIFY2(action->shortcuts().contains(QKeySequence(Qt::Key_Delete)),
             qPrintable(QStringLiteral("delete is not on the Del key; it has: %1")
                            .arg(QKeySequence::listToString(action->shortcuts()))));
}

void TestMainWindow::theDeleteKeyEditsTextInTheQueryBar()
{
    // `delete` is bound to bare Del, and a QAction shortcut is dispatched
    // BEFORE the focused widget sees the key. Qt withholds only plain LETTERS
    // from editable widgets, so by the argument that made bare Return break
    // the query bar, Delete should move mail to the trash while the user is
    // editing a query.
    //
    // It does not: QLineEdit accepts the ShortcutOverride for Delete itself,
    // because Delete is one of its own editing keys, which Return is not. That
    // is a property of Qt rather than of this code, which is exactly why it is
    // pinned here: it is the assumption the bare binding rests on, and if a
    // future Qt or a future focus proxy changes it, mail gets deleted while
    // someone types.
    //
    // Asserted on the ACTION not firing, not on the ShortcutOverride phase. A
    // probe on the override reports notify=1 accepted=1 whether or not this
    // window filters the key, since QLineEdit accepts it either way, so it
    // cannot distinguish the two and passes against any implementation.
    // Measured, while trying to write this test the obvious way.
    const Config config;
    MainWindow window(config);
    window.show();
    QVERIFY(QTest::qWaitForWindowExposed(&window));

    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    auto *deleteAction = window.findChild<QAction *>(QStringLiteral("delete"));
    QVERIFY(queryEdit && deleteAction);

    int fired = 0;
    QObject::connect(deleteAction, &QAction::triggered,
                     [&fired]() { ++fired; });

    queryEdit->setFocus();
    QTRY_VERIFY(queryEdit->hasFocus());
    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->setCursorPosition(0);

    QTest::keyClick(queryEdit, Qt::Key_Delete);

    QCOMPARE(fired, 0);
    QCOMPARE(queryEdit->text(), QStringLiteral("ag:inbox"));
}

void TestMainWindow::restoreIsReachableWithoutTheKeyboard()
{
    // Restore shipped as a keyboard shortcut and nothing else: registered,
    // iconned, enabled correctly, and present in no menu at all. A user who
    // does not read the changelog would never learn it exists, and Ctrl+R is
    // not a guess anyone makes.
    //
    // The four places an action must touch are enforced by tests
    // (knownActions, defaultBindings, the icon table); being REACHABLE is a
    // fifth that nothing checked, which is why the gap survived a green suite.
    const Config config;
    MainWindow window(config);

    auto *restore = window.findChild<QAction *>(QStringLiteral("restore"));
    QVERIFY(restore);

    const auto menuContains = [](const QMenu *menu, const QAction *action) {
        return menu && menu->actions().contains(action);
    };

    // A menu on the MENU BAR, beside Delete whose inverse it is. The context
    // menu is excluded here so this assertion cannot be satisfied by the one
    // the next assertion checks: findChildren finds both.
    auto *context =
        window.findChild<QMenu *>(QStringLiteral("threadContextMenu"));
    QVERIFY(context);

    bool inAMenuBarMenu = false;
    for (const QMenu *menu : window.findChildren<QMenu *>()) {
        if (menu != context && menuContains(menu, restore)) {
            inAMenuBarMenu = true;
            break;
        }
    }
    QVERIFY2(inAMenuBarMenu,
             "Restore is in no menu-bar menu, so a user browsing the menus "
             "would never learn it exists");

    // And the thread list's context menu, which is where the other
    // message-scoped actions are reached by mouse.
    QVERIFY2(menuContains(context, restore),
             "Restore is missing from the thread context menu");
}

void TestMainWindow::restoreIsOnlyEnabledInTheTrashView()
{
    // Restore has no meaning outside the trash, and an enabled action that
    // does nothing is worse than an absent one.
    //
    // Enabled from the QUERY rather than from the selection's tags: a message
    // trashed by another client carries no tag of ours and must still be
    // restorable, which is the whole reason the trash view is path-based.
    WorkerBackedWindow backed;
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("re1@example.org"),
        QStringLiteral("In the inbox"), QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/Trash"), QStringLiteral("re2@example.org"),
        QStringLiteral("In the trash"), QStringLiteral("other@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 11:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY2(backed.build(QStringLiteral("acct"), QStringLiteral("acct"),
                          QStringLiteral("Trash")),
             qPrintable(backed.error()));

    MainWindow window(backed.config());
    auto *model = window.findChild<ThreadListModel *>();
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    auto *restore = window.findChild<QAction *>(QStringLiteral("restore"));
    QVERIFY(model && queryEdit);
    QVERIFY2(restore, "there is no restore action");

    // An ordinary view. Both fixture messages carry `inbox`, since the
    // fixture tags all new mail that way regardless of folder, so this is two
    // rows rather than one; the count is not what is under test.
    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 2, 15000);
    QVERIFY2(!restore->isEnabled(),
             "Restore is enabled in an ordinary view, where it means nothing");

    // The trash view, which is the account's own generated trash query.
    queryEdit->setText(QStringLiteral("path:\"acct/Trash/**\""));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);
    QVERIFY2(restore->isEnabled(),
             "Restore is disabled in the trash view, where it is the point");
}

void TestMainWindow::restoreReturnsAMessageToItsOriginFolder()
{
    WorkerBackedWindow backed;
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("ro1@example.org"),
        QStringLiteral("Send me back"), QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY2(backed.build(QStringLiteral("acct"), QStringLiteral("acct"),
                          QStringLiteral("Trash")),
             qPrintable(backed.error()));

    MainWindow window(backed.config());
    auto *model = window.findChild<ThreadListModel *>();
    auto *view = window.findChild<ThreadListView *>();
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(model && view && queryEdit);

    const QString root = backed.fixture().maildirPath();
    const QString cfg = backed.fixture().configPath();
    const QString stem = QStringLiteral("ro1.example.org");

    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);
    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    window.findChild<QAction *>(QStringLiteral("delete"))->trigger();
    QTRY_VERIFY_WITH_TIMEOUT(
        folderHasMessageFile(root + QStringLiteral("/acct/Trash/cur"), stem),
        15000);

    // Now from the trash view, through Restore rather than through a second
    // Delete: this is the action the user reaches for when browsing trash.
    queryEdit->setText(QStringLiteral("path:\"acct/Trash/**\""));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);
    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    window.findChild<QAction *>(QStringLiteral("restore"))->trigger();

    QTRY_VERIFY_WITH_TIMEOUT(
        folderHasMessageFile(root + QStringLiteral("/acct/inbox/cur"), stem)
            || folderHasMessageFile(root + QStringLiteral("/acct/inbox/new"),
                                    stem),
        15000);
    // Waited on the ORIGIN tag, not on `deleted`.
    //
    // Both come off in one write, but the file rename and the tag write are
    // separate operations and the assertions below raced the second one:
    // measured 1 failure in 3 runs waiting on `deleted` alone, reporting the
    // origin tag still present. Waiting on the tag this test is actually about
    // removes the race rather than papering over it with a longer timeout.
    QTRY_VERIFY_WITH_TIMEOUT(
        notmuchCount(cfg, QStringLiteral("id:ro1@example.org and "
                                         "tag:\"deleted-from:inbox\"")) == 0,
        15000);
    QTRY_VERIFY_WITH_TIMEOUT(
        notmuchCount(cfg,
                     QStringLiteral("id:ro1@example.org and tag:deleted")) == 0,
        15000);

    QCOMPARE(notmuchCount(cfg, QStringLiteral("id:ro1@example.org")), 1);
    QVERIFY(!folderHasMessageFile(root + QStringLiteral("/acct/Trash/cur"),
                                  stem));
}

void TestMainWindow::restoreFallsBackToInboxWithoutAnOriginTag()
{
    // A message trashed by ANOTHER client: it sits in the trash folder and
    // carries no `deleted-from:` tag, because nothing here put it there. The
    // real Maildir has such messages, which is why the trash view is path
    // based rather than tag based.
    //
    // Inbox is the documented fallback. Refusing to move it would leave the
    // user with a message they can see in the trash and cannot get out.
    WorkerBackedWindow backed;
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/Trash"), QStringLiteral("foreign@example.org"),
        QStringLiteral("Trashed elsewhere"), QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY2(backed.build(QStringLiteral("acct"), QStringLiteral("acct"),
                          QStringLiteral("Trash")),
             qPrintable(backed.error()));

    MainWindow window(backed.config());
    auto *model = window.findChild<ThreadListModel *>();
    auto *view = window.findChild<ThreadListView *>();
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(model && view && queryEdit);

    const QString root = backed.fixture().maildirPath();
    const QString cfg = backed.fixture().configPath();
    const QString stem = QStringLiteral("foreign.example.org");

    // The guard this test needs: no origin tag, so the fallback is what is
    // under test rather than an ordinary restore.
    QCOMPARE(notmuchCount(cfg, QStringLiteral("id:foreign@example.org and "
                                              "tag:\"deleted-from:inbox\"")),
             0);

    queryEdit->setText(QStringLiteral("path:\"acct/Trash/**\""));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);
    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    window.findChild<QAction *>(QStringLiteral("restore"))->trigger();

    QTRY_VERIFY_WITH_TIMEOUT(
        folderHasMessageFile(root + QStringLiteral("/acct/inbox/cur"), stem)
            || folderHasMessageFile(root + QStringLiteral("/acct/inbox/new"),
                                    stem),
        15000);
    QVERIFY2(!folderHasMessageFile(root + QStringLiteral("/acct/Trash/cur"),
                                   stem),
             "the message was copied out of the trash rather than moved");
}

void TestMainWindow::undoMovesTheMessageBack()
{
    // Undo is this project's answer to the confirmation dialog it rules out,
    // so a delete that cannot be undone is a delete with no safety net at all.
    WorkerBackedWindow backed;
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("del3@example.org"),
        QStringLiteral("Put me back"), QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY2(backed.build(QStringLiteral("acct"), QStringLiteral("acct"),
                          QStringLiteral("Trash")),
             qPrintable(backed.error()));

    MainWindow window(backed.config());
    auto *model = window.findChild<ThreadListModel *>();
    auto *view = window.findChild<ThreadListView *>();
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(model && view && queryEdit);

    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);

    const QString root = backed.fixture().maildirPath();
    const QString stem = QStringLiteral("del3.example.org");
    const QString trash = root + QStringLiteral("/acct/Trash/cur");

    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    window.findChild<QAction *>(QStringLiteral("delete"))->trigger();
    QTRY_VERIFY_WITH_TIMEOUT(folderHasMessageFile(trash, stem), 15000);

    window.findChild<QAction *>(QStringLiteral("undo"))->trigger();

    // Back in the EXACT folder it came from. A move-back that guessed "inbox"
    // for every account would pass a laxer assertion than this one.
    //
    // cur/, not the new/ it started in: a file coming back from the trash has
    // been read, and re-announcing it as fresh mail is worse than the flag
    // change.
    QTRY_VERIFY_WITH_TIMEOUT(
        folderHasMessageFile(root + QStringLiteral("/acct/inbox/cur"), stem)
            || folderHasMessageFile(root + QStringLiteral("/acct/inbox/new"),
                                    stem),
        15000);
    QVERIFY2(!folderHasMessageFile(trash, stem),
             "undo restored the file and left a copy in the trash");

    // Both tags gone, asked of the database. `deleted-from:` left behind would
    // make Restore offer to move a message that is already home.
    queryEdit->setText(QStringLiteral(
        "id:del3@example.org and (tag:deleted or tag:\"deleted-from:inbox\")"));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 0, 15000);
    // The guard the assertion above needs: a query that matches nothing
    // because the message vanished would pass it too.
    queryEdit->setText(QStringLiteral("id:del3@example.org"));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);
}

void TestMainWindow::deleteOnAReplyMovesThatReplyOnly()
{
    // The reply case. A test asserting on a root selection is the one case
    // where the wrong resolution is accidentally right, so a mutation on this
    // path stays green without it.
    //
    // Put under the SECOND thread, so the wrong answer is plausible rather
    // than accidentally correct.
    WorkerBackedWindow backed;
    NotmuchFixture &fx = backed.fixture();
    QVERIFY(fx.addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("other@example.org"),
        QStringLiteral("An unrelated thread"),
        QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 09:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY(fx.addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("rootof@example.org"),
        QStringLiteral("A conversation"), QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY(fx.addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("reply@example.org"),
        QStringLiteral("Re: A conversation"),
        QStringLiteral("other@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 11:00:00 +0200"),
        QStringLiteral("Reply body."), true,
        QStringLiteral("rootof@example.org")));
    QVERIFY2(backed.build(QStringLiteral("acct"), QStringLiteral("acct"),
                          QStringLiteral("Trash")),
             qPrintable(backed.error()));

    MainWindow window(backed.config());
    auto *model = window.findChild<ThreadListModel *>();
    auto *view = window.findChild<ThreadListView *>();
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(model && view && queryEdit);

    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 2, 15000);

    // Whichever row holds the conversation. The sort is the model's business,
    // so this asks rather than assuming.
    QModelIndex conversation;
    for (int row = 0; row < model->rowCount(QModelIndex()); ++row) {
        const QModelIndex index = model->index(row, 0, QModelIndex());
        if (model->threadAt(row).totalCount > 1) {
            conversation = index;
            break;
        }
    }
    QVERIFY2(conversation.isValid(), "no multi-message thread in the list");

    view->expand(conversation);
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(conversation) == 1, 15000);

    const QModelIndex replyIndex = model->index(0, 0, conversation);
    QVERIFY(model->isMessageRow(replyIndex));
    QCOMPARE(model->messageAt(replyIndex).messageId,
             QStringLiteral("reply@example.org"));

    view->setCurrentIndex(replyIndex);
    window.findChild<QAction *>(QStringLiteral("delete"))->trigger();

    const QString root = fx.maildirPath();
    QTRY_VERIFY_WITH_TIMEOUT(
        folderHasMessageFile(root + QStringLiteral("/acct/Trash/cur"),
                             QStringLiteral("reply.example.org")),
        15000);

    // Only that reply. Escalating a message-scoped delete to its thread would
    // move the root as well, which is the failure worth naming: the user
    // deleted one reply and lost the conversation.
    QVERIFY2(!folderHasMessageFile(root + QStringLiteral("/acct/Trash/cur"),
                                   QStringLiteral("rootof.example.org")),
             "deleting a reply moved its thread's root as well");
}

void TestMainWindow::undoingADeleteConsumesItsCommandRatherThanPushingAnother()
{
    // A move is confirmed through onMessagesMoved(), and so is the move an
    // UNDO makes. Pushing a command there unconditionally meant undo left a
    // fresh command on the stack instead of consuming the one it undid, so
    // the stack grew on every press: "Delete", "Undo Delete", "Undo Undo
    // Delete". A user pressing undo twice to be sure re-deleted the mail they
    // had just rescued, which is the opposite of what undo is for here, undo
    // being this project's stand-in for a confirmation dialog.
    WorkerBackedWindow backed;
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("undo2@example.org"),
        QStringLiteral("Undo twice"), QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY2(backed.build(QStringLiteral("acct"), QStringLiteral("acct"),
                          QStringLiteral("Trash")),
             qPrintable(backed.error()));

    MainWindow window(backed.config());
    auto *model = window.findChild<ThreadListModel *>();
    auto *view = window.findChild<ThreadListView *>();
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(model && view && queryEdit);

    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);

    const QString root = backed.fixture().maildirPath();
    const QString stem = QStringLiteral("undo2.example.org");
    const QString trash = root + QStringLiteral("/acct/Trash/cur");
    const auto inInbox = [&] {
        return folderHasMessageFile(root + QStringLiteral("/acct/inbox/cur"),
                                    stem)
               || folderHasMessageFile(
                   root + QStringLiteral("/acct/inbox/new"), stem);
    };

    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    window.findChild<QAction *>(QStringLiteral("delete"))->trigger();
    QTRY_VERIFY_WITH_TIMEOUT(folderHasMessageFile(trash, stem), 15000);

    // The guard the assertions below need: one command, from the delete.
    QTRY_VERIFY_WITH_TIMEOUT(window.undoDepthForTesting() == 1, 15000);

    window.findChild<QAction *>(QStringLiteral("undo"))->trigger();
    QTRY_VERIFY_WITH_TIMEOUT(inInbox(), 15000);

    // The stack is spent. Asserted on undoText rather than depth alone
    // because a command that is merely marked done still reports its text,
    // and it is the text the user reads off the Edit menu.
    QTRY_VERIFY_WITH_TIMEOUT(window.undoTextForTesting().isEmpty(), 15000);

    // And the real point: pressing undo again must not move the message
    // anywhere. Before the fix this put it straight back in the trash.
    window.findChild<QAction *>(QStringLiteral("undo"))->trigger();
    QTest::qWait(1500);
    QVERIFY2(!folderHasMessageFile(trash, stem),
             "a second undo re-deleted the message the first one restored");
    QVERIFY2(inInbox(), "a second undo moved the message out of the inbox");
}

void TestMainWindow::aDeleteHeldDuringASyncCountsAsUnsyncedWork()
{
    // pendingEditCount() summed the held TAG edits and not the held MOVES, so
    // a Delete pressed during a sync left the count at zero: the indicator
    // stayed hidden and closeEvent()'s `pendingEditCount() > 0` guard never
    // fired, discarding the move on quit with no prompt. That is item 106's
    // data loss with a worse shape, since a dropped move leaves the file in
    // the folder the user asked it out of.
    WorkerBackedWindow backed;
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("held1@example.org"),
        QStringLiteral("Held by a sync"), QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY2(backed.build(QStringLiteral("acct"), QStringLiteral("acct"),
                          QStringLiteral("Trash")),
             qPrintable(backed.error()));

    MainWindow window(backed.config());
    auto *model = window.findChild<ThreadListModel *>();
    auto *view = window.findChild<ThreadListView *>();
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    auto *label = window.findChild<QLabel *>(QStringLiteral("pendingEdits"));
    QVERIFY(model && view && queryEdit && label);

    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);

    // Hidden before the gesture, so the assertion after it means something.
    QVERIFY2(label->isHidden(), "the pending indicator was already showing");

    // A sync now holds the write lock, which is what makes the move held
    // rather than sent.
    QMetaObject::invokeMethod(&window, "onExternalSyncStateChanged",
                              Q_ARG(SyncMonitor::State,
                                    SyncMonitor::State::Running));

    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    window.findChild<QAction *>(QStringLiteral("delete"))->trigger();

    QVERIFY2(!label->isHidden(),
             "a Delete held by a sync did not count as unsynced work, so "
             "quitting would have discarded it with no prompt");

    // The file really is still where it was: this is a HELD move, not a
    // failed one, and the indicator would be meaningless otherwise.
    const QString root = backed.fixture().maildirPath();
    QVERIFY(!folderHasMessageFile(root + QStringLiteral("/acct/Trash/cur"),
                                  QStringLiteral("held1.example.org")));
}

void TestMainWindow::twoDeletesToOneTrashBothGetTheirTags()
{
    // The pending-move table was keyed on the destination folder, so two
    // Deletes in one account before the first confirmation arrived both named
    // `acct/Trash`: the second insert overwrote the first and the second
    // confirmation took an empty entry. That file reached the trash carrying
    // neither `deleted` nor `deleted-from:`, which makes it unrestorable by
    // Restore and invisible to a `tag:deleted` query.
    WorkerBackedWindow backed;
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("two1@example.org"),
        QStringLiteral("First"), QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("two2@example.org"),
        QStringLiteral("Second"), QStringLiteral("other@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 11:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY2(backed.build(QStringLiteral("acct"), QStringLiteral("acct"),
                          QStringLiteral("Trash")),
             qPrintable(backed.error()));

    MainWindow window(backed.config());
    auto *model = window.findChild<ThreadListModel *>();
    auto *view = window.findChild<ThreadListView *>();
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(model && view && queryEdit);

    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 2, 15000);

    // Both Deletes issued back to back, WITHOUT waiting for the first to be
    // confirmed. That is the whole point: waiting would serialise them and
    // the keyed table would have coped.
    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    window.findChild<QAction *>(QStringLiteral("delete"))->trigger();
    view->setCurrentIndex(model->index(1, 0, QModelIndex()));
    window.findChild<QAction *>(QStringLiteral("delete"))->trigger();

    const QString root = backed.fixture().maildirPath();
    const QString trash = root + QStringLiteral("/acct/Trash/cur");
    QTRY_VERIFY_WITH_TIMEOUT(
        folderHasMessageFile(trash, QStringLiteral("two1.example.org"))
            && folderHasMessageFile(trash, QStringLiteral("two2.example.org")),
        15000);

    // Both carry BOTH tags, asked of the database rather than of the model:
    // the defect was a write that never happened, and the model would have
    // shown the optimistic state either way.
    queryEdit->setText(QStringLiteral(
        "tag:deleted and tag:\"deleted-from:inbox\" and "
        "(id:two1@example.org or id:two2@example.org)"));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 2, 15000);
}

void TestMainWindow::deleteWithoutATrashFolderSaysSoRatherThanDoingNothing()
{
    // Task 2 warns at config load. This is the second line of defence: a key
    // the user never fixed must not leave Delete silently inert.
    WorkerBackedWindow backed;
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("notrash@example.org"),
        QStringLiteral("Nowhere to go"), QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Body text.")));
    // No trash key, which is what this is about.
    QVERIFY2(backed.build(QStringLiteral("acct"), QStringLiteral("acct")),
             qPrintable(backed.error()));

    MainWindow window(backed.config());
    auto *model = window.findChild<ThreadListModel *>();
    auto *view = window.findChild<ThreadListView *>();
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    auto *status = window.findChild<QLabel *>(QStringLiteral("statusMessage"));
    QVERIFY(model && view && queryEdit && status);

    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);

    view->setCurrentIndex(model->index(0, 0, QModelIndex()));

    // Cleared FIRST, so the assertion below cannot be satisfied by whatever
    // the query left behind. Without this the test passes against a Delete
    // that says nothing at all, which is exactly what it exists to catch: it
    // did, before the implementation landed.
    status->clear();
    window.findChild<QAction *>(QStringLiteral("delete"))->trigger();

    QVERIFY2(status->text().contains(QStringLiteral("trash")),
             qPrintable(QStringLiteral(
                            "Delete with no trash folder configured said: '%1'")
                            .arg(status->text())));

    // And it did not tag the message either. A `deleted` tag with the file
    // still in the inbox is exactly the half-done state item 103 removes.
    const QString mail = backed.fixture().maildirPath();
    QVERIFY(folderHasMessageFile(mail + QStringLiteral("/acct/inbox/new"),
                                 QStringLiteral("notrash.example.org"))
            || folderHasMessageFile(mail + QStringLiteral("/acct/inbox/cur"),
                                    QStringLiteral("notrash.example.org")));
}

void TestMainWindow::theCleanupQueryFindsStrandedMail()
{
    // The state 848 real messages are in today: tagged `deleted` by a version
    // of Delete that only ever tagged, with the file still sitting in the
    // inbox. Nothing moves them on their own, so the action reports them and
    // the user decides.
    WorkerBackedWindow backed;
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("strand@example.org"),
        QStringLiteral("Tagged but never moved"),
        QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("keep@example.org"),
        QStringLiteral("Perfectly ordinary mail"),
        QStringLiteral("other@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 11:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY2(backed.build(QStringLiteral("acct"), QStringLiteral("acct"),
                          QStringLiteral("Trash")),
             qPrintable(backed.error()));

    const QString cfg = backed.fixture().configPath();
    QVERIFY(notmuchTag(cfg, { QStringLiteral("+deleted"),
                              QStringLiteral("--"),
                              QStringLiteral("id:strand@example.org") }));
    // The guard, before anything is asserted about what the action finds: one
    // message is stranded and one is not, so a query that simply returns
    // everything cannot pass.
    QCOMPARE(notmuchCount(cfg, QStringLiteral("tag:deleted")), 1);

    MainWindow window(backed.config());
    auto *model = window.findChild<ThreadListModel *>();
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    auto *cleanup =
        window.findChild<QAction *>(QStringLiteral("cleanup_stranded"));
    QVERIFY(model && queryEdit);
    QVERIFY2(cleanup, "there is no cleanup_stranded action");

    cleanup->trigger();

    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);
    // The query lands in the bar, like every other generated query, so what
    // ran is visible and the user can edit it.
    QVERIFY2(queryEdit->text().contains(QStringLiteral("tag:deleted")),
             qPrintable(QStringLiteral("the bar holds '%1'")
                            .arg(queryEdit->text())));
    QVERIFY2(queryEdit->text().contains(QStringLiteral("not ")),
             qPrintable(QStringLiteral("the bar holds '%1'")
                            .arg(queryEdit->text())));

    // It reports and moves NOTHING. A cleanup that acted on its own would be a
    // bulk delete with no selection behind it, which is the opposite of what
    // the user asked for.
    const QString mail = backed.fixture().maildirPath();
    QVERIFY(folderHasMessageFile(mail + QStringLiteral("/acct/inbox/new"),
                                 QStringLiteral("strand.example.org"))
            || folderHasMessageFile(mail + QStringLiteral("/acct/inbox/cur"),
                                    QStringLiteral("strand.example.org")));
    QCOMPARE(notmuchCount(cfg, QStringLiteral("path:\"acct/Trash/**\"")), 0);
}

void TestMainWindow::theCleanupQueryExcludesMailAlreadyInTrash()
{
    // Properly trashed mail carries the tag AND sits in the folder. Without
    // the exclusion this reports every deleted message ever, which makes the
    // action useless the moment Delete starts working.
    WorkerBackedWindow backed;
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("cln1@example.org"),
        QStringLiteral("Going to the trash"),
        QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY2(backed.build(QStringLiteral("acct"), QStringLiteral("acct"),
                          QStringLiteral("Trash")),
             qPrintable(backed.error()));

    MainWindow window(backed.config());
    auto *model = window.findChild<ThreadListModel *>();
    auto *view = window.findChild<ThreadListView *>();
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(model && view && queryEdit);

    const QString cfg = backed.fixture().configPath();
    const QString mail = backed.fixture().maildirPath();

    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);
    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    window.findChild<QAction *>(QStringLiteral("delete"))->trigger();

    // Asked of the database, never of the list: rowCount() reads 0 for the
    // whole interval before the worker answers, so "the cleanup found
    // nothing" would pass against a delete that never happened.
    QTRY_VERIFY_WITH_TIMEOUT(
        folderHasMessageFile(mail + QStringLiteral("/acct/Trash/cur"),
                             QStringLiteral("cln1.example.org")),
        15000);
    QTRY_VERIFY_WITH_TIMEOUT(
        notmuchCount(cfg, QStringLiteral("tag:deleted")) == 1, 15000);

    auto *cleanup =
        window.findChild<QAction *>(QStringLiteral("cleanup_stranded"));
    QVERIFY2(cleanup, "there is no cleanup_stranded action");
    cleanup->trigger();

    // The query the action ran, asked of notmuch directly. The list is the
    // wrong instrument for an emptiness claim, for the reason above.
    QTRY_VERIFY_WITH_TIMEOUT(!queryEdit->text().isEmpty(), 15000);
    QCOMPARE(notmuchCount(cfg, queryEdit->text()), 0);
}

void TestMainWindow::aMoveThatRelocatesNothingWritesNoTag()
{
    // The spec's ordering bullet, at the UI level: a failed rename must leave
    // no tag. The worker half is moveMessagesReportsOnlyWhatMoved(); this is
    // the other half, that the window writes tags only for what the worker
    // reported as actually moved.
    //
    // The failure is provoked by making the destination unwritable, which is
    // the closest thing to a failed rename that a test can arrange without
    // stubbing the worker. A tag written anyway would be the exact half-done
    // state item 103 exists to remove: a message marked deleted, with its file
    // still in the inbox and its origin tag lying about where it went.
    WorkerBackedWindow backed;
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("nomove@example.org"),
        QStringLiteral("Cannot be moved"), QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY2(backed.build(QStringLiteral("acct"), QStringLiteral("acct"),
                          QStringLiteral("Trash")),
             qPrintable(backed.error()));

    const QString root = backed.fixture().maildirPath();
    const QString cfg = backed.fixture().configPath();

    // The trash as a FILE where the folder must be, so creating the Maildir
    // subdirectories under it cannot succeed. A read-only directory would be
    // ignored by a test running as root, which this one must not depend on.
    QFile blocker(root + QStringLiteral("/acct/Trash"));
    QVERIFY2(blocker.open(QIODevice::WriteOnly),
             "could not put a file where the trash folder would go");
    blocker.close();

    MainWindow window(backed.config());
    auto *model = window.findChild<ThreadListModel *>();
    auto *view = window.findChild<ThreadListView *>();
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(model && view && queryEdit);

    queryEdit->setText(QStringLiteral("tag:inbox"));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);
    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    window.findChild<QAction *>(QStringLiteral("delete"))->trigger();

    // The guard, so this cannot pass by the delete never having been
    // attempted: the message is still there and still findable afterwards.
    QTRY_VERIFY_WITH_TIMEOUT(
        notmuchCount(cfg, QStringLiteral("id:nomove@example.org")) == 1, 15000);

    // A tag write is a round trip, so an immediate read would pass against a
    // write still in flight. Given time to arrive, then asserted absent.
    QTest::qWait(1500);
    QCOMPARE(notmuchCount(cfg, QStringLiteral("id:nomove@example.org and "
                                              "tag:deleted")), 0);
    QCOMPARE(notmuchCount(cfg, QStringLiteral("id:nomove@example.org and "
                                              "tag:\"deleted-from:inbox\"")), 0);

    // And the file never left.
    QVERIFY(folderHasMessageFile(root + QStringLiteral("/acct/inbox/new"),
                                 QStringLiteral("nomove.example.org"))
            || folderHasMessageFile(root + QStringLiteral("/acct/inbox/cur"),
                                    QStringLiteral("nomove.example.org")));
}

void TestMainWindow::restoringFromTheTrashViewRefreshesTheList()
{
    // Reported from a hand test: Restore moved the message correctly and the
    // row it came from sat in the trash list until the Trash filter was
    // clicked again.
    //
    // The trash view is PATH based, so a restored message no longer matches
    // the query the list was built from. That is a state no tag change can
    // express: onMessagesMoved() updates tags and the undo stack and never
    // removes a row, which is right in an ordinary view (a deleted message's
    // card should stay put) and wrong here, where the row is the one thing
    // that is now false.
    WorkerBackedWindow backed;
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/Trash"), QStringLiteral("refr1@example.org"),
        QStringLiteral("Restore me"), QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY2(backed.build(QStringLiteral("acct"), QStringLiteral("acct"),
                          QStringLiteral("Trash")),
             qPrintable(backed.error()));

    MainWindow window(backed.config());
    auto *model = window.findChild<ThreadListModel *>();
    auto *view = window.findChild<ThreadListView *>();
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(model && view && queryEdit);

    const QString root = backed.fixture().maildirPath();
    const QString stem = QStringLiteral("refr1.example.org");

    // The account's own generated trash query, which is what the Trash filter
    // puts in the bar.
    queryEdit->setText(QStringLiteral("path:\"acct/Trash/**\""));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);

    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    window.findChild<QAction *>(QStringLiteral("restore"))->trigger();

    // The move really happened, waited on the FILE. Without this the row
    // assertion below could pass against a restore that never ran.
    QTRY_VERIFY_WITH_TIMEOUT(
        folderHasMessageFile(root + QStringLiteral("/acct/inbox/cur"), stem)
            || folderHasMessageFile(root + QStringLiteral("/acct/inbox/new"),
                                    stem),
        15000);

    // And the list no longer shows it, without the user touching anything.
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 0, 15000);
}

void TestMainWindow::theRefreshAfterARestoreLeavesUndoIntact()
{
    // The refresh that fixes the stale trash row runs immediately after the
    // undo entry is pushed, so it must not be the thing that destroys it.
    // runCurrentQuery() clears the undo stack outright, which would make
    // Restore the one mutation in the window with no way back; this asserts
    // the non-destructive refresh was used and stayed non-destructive.
    WorkerBackedWindow backed;
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/Trash"), QStringLiteral("undoref@example.org"),
        QStringLiteral("Restore then undo"),
        QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY2(backed.build(QStringLiteral("acct"), QStringLiteral("acct"),
                          QStringLiteral("Trash")),
             qPrintable(backed.error()));

    MainWindow window(backed.config());
    auto *model = window.findChild<ThreadListModel *>();
    auto *view = window.findChild<ThreadListView *>();
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(model && view && queryEdit);

    const QString root = backed.fixture().maildirPath();
    const QString stem = QStringLiteral("undoref.example.org");

    queryEdit->setText(QStringLiteral("path:\"acct/Trash/**\""));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);

    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    window.findChild<QAction *>(QStringLiteral("restore"))->trigger();

    QTRY_VERIFY_WITH_TIMEOUT(
        folderHasMessageFile(root + QStringLiteral("/acct/inbox/cur"), stem)
            || folderHasMessageFile(root + QStringLiteral("/acct/inbox/new"),
                                    stem),
        15000);
    // The refresh has run by now, which is what the row count proves.
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 0, 15000);

    // And the undo entry is still there afterwards.
    auto *undo = window.findChild<QAction *>(QStringLiteral("undo"));
    QVERIFY(undo);
    QVERIFY2(undo->isEnabled(),
             "the refresh after a restore cleared the undo stack");

    undo->trigger();

    // Back in the trash, asserted on the FILE: the undo has to move it, not
    // merely re-tag it.
    QTRY_VERIFY_WITH_TIMEOUT(
        folderHasMessageFile(root + QStringLiteral("/acct/Trash/cur"), stem),
        15000);
}

void TestMainWindow::deletingOutsideTheTrashViewLeavesTheRowInPlace()
{
    // The other half of the trash-view refresh, and the reason it is gated.
    //
    // A Delete is a move too and reaches the same confirmation slot. Refreshing
    // on every move would make the row vanish from under the user in every
    // ordinary view, which this project has decided against twice: the card
    // deliberately stays put, because one deleted message does not doom the
    // conversation and a row disappearing mid-gesture loses the user's place.
    //
    // Nothing asserted this, so a mutation dropping the isShowingTrash() gate
    // passed the whole suite.
    WorkerBackedWindow backed;
    QVERIFY(backed.fixture().addMessage(
        QStringLiteral("acct/inbox"), QStringLiteral("stay1@example.org"),
        QStringLiteral("Stay on screen"), QStringLiteral("sender@example.org"),
        QStringLiteral("Fri, 14 Aug 2026 10:00:00 +0200"),
        QStringLiteral("Body text.")));
    QVERIFY2(backed.build(QStringLiteral("acct"), QStringLiteral("acct"),
                          QStringLiteral("Trash")),
             qPrintable(backed.error()));

    MainWindow window(backed.config());
    auto *model = window.findChild<ThreadListModel *>();
    auto *view = window.findChild<ThreadListView *>();
    auto *queryEdit =
        window.findChild<QLineEdit *>(QStringLiteral("queryEdit"));
    QVERIFY(model && view && queryEdit);

    const QString root = backed.fixture().maildirPath();
    const QString stem = QStringLiteral("stay1.example.org");

    // An ORDINARY view that the message STOPS MATCHING once the delete lands.
    // Both halves matter and the first draft of this test had only one: a
    // `tag:inbox` view looks ordinary but Delete adds `deleted` and the origin
    // tag and removes nothing, so the message keeps `inbox` and keeps matching.
    // A refresh there is a no-op, and the mutation dropping the
    // isShowingTrash() gate passed against it.
    //
    // A path query on the inbox folder is the honest instrument: the file
    // really leaves that folder, so the row survives only because nothing
    // refreshed.
    queryEdit->setText(QStringLiteral("path:\"acct/inbox/**\""));
    queryEdit->returnPressed();
    QTRY_VERIFY_WITH_TIMEOUT(model->rowCount(QModelIndex()) == 1, 15000);

    view->setCurrentIndex(model->index(0, 0, QModelIndex()));
    window.findChild<QAction *>(QStringLiteral("delete"))->trigger();

    // The delete really happened, waited on the file rather than on the list.
    QTRY_VERIFY_WITH_TIMEOUT(
        folderHasMessageFile(root + QStringLiteral("/acct/Trash/cur"), stem),
        15000);

    // A refresh is a queued round trip, so an immediate read would pass against
    // one still in flight. Given time to arrive, then asserted not to have
    // taken the row away.
    QTest::qWait(1500);
    QCOMPARE(model->rowCount(QModelIndex()), 1);
}

// ---------------------------------------------------------------------------
// ComposeWindow, item 123.
//
// The composer owns widgets and nothing else here does, which is why its cases
// live in this file. What is asserted is deliberately NOT what it looks like:
// the autosave dirty check, the banner state, the message its widgets produce,
// the format edits and the seeding rules, all of which are observable without
// a painter. CLAUDE.md's "Rendering probes lie" section covers why counting
// pixels here would prove nothing.
// ---------------------------------------------------------------------------

namespace {

/// A Config written to a temporary INI, plus a Maildir root to write into.
///
/// No notmuch database and no worker: the composer never touches
/// NotmuchWorker, so building one would only cost every case a `notmuch new`.
/// The mail root is passed to ComposeWindow explicitly, exactly as MainWindow
/// passes what the worker reported (item 124: it is NOT database.path).
class ComposeFixture
{
public:
    /// `drafts` and `sent` are written only when non-empty, so a test can
    /// build the account-without-a-drafts-folder case by passing an empty
    /// string rather than by needing a second fixture.
    /// `secondAccount` writes a SECOND sending account, which is what makes
    /// the From dropdown have something to choose between. Off by default:
    /// every other case here wants exactly one, so a two-account fixture
    /// everywhere would let a test pass by picking the only entry there is.
    bool build(const QString &drafts = QStringLiteral("Drafts"),
               const QString &sent = QStringLiteral("Sent"),
               const QString &extraCompose = QString(),
               bool secondAccount = false)
    {
        if (!m_confDir.isValid() || !m_mailDir.isValid())
            return false;

        const QString path = m_confDir.filePath(QStringLiteral("qtmaildir.conf"));
        QFile file(path);
        if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
            return false;
        {
            QTextStream out(&file);
            // QSettings reads `/` in a section name as a group separator, so
            // the section is [account.acct], never [account/acct].
            out << "[account.acct]\n"
                << "name=Test User\n"
                << "address=user@example.org\n"
                << "maildir=acct\n"
                << "trash=Trash\n";
            if (!drafts.isEmpty())
                out << "drafts=" << drafts << "\n";
            if (!sent.isEmpty())
                out << "sent=" << sent << "\n";
            // A command that exists and does nothing. canSend() is what the
            // From dropdown filters on, so an account without this one line
            // would not appear in it at all.
            out << "send_command=/bin/true\n";
            if (secondAccount) {
                out << "\n[account.other]\n"
                    << "name=Other User\n"
                    << "address=other@example.org\n"
                    << "maildir=other\n"
                    << "trash=Trash\n"
                    << "drafts=Drafts\n"
                    << "sent=Sent\n"
                    << "send_command=/bin/true\n";
            }
            out << "\n[compose]\n";
            if (!extraCompose.isEmpty())
                out << extraCompose << "\n";
        }
        file.close();

        m_config.load(path);
        return true;
    }

    const Config &config() const { return m_config; }
    QString mailRoot() const { return m_mailDir.path(); }

    /// The account's drafts folder, as the composer will resolve it.
    QString draftsCur() const
    {
        return m_mailDir.path() + QStringLiteral("/acct/Drafts/cur");
    }

    /// The second account's drafts folder.
    QString otherDraftsCur() const
    {
        return m_mailDir.path() + QStringLiteral("/other/Drafts/cur");
    }

    /// How many message files sit in the drafts folder.
    int draftCount() const
    {
        return QDir(draftsCur(), {}, QDir::Name, QDir::Files).count();
    }

private:
    QTemporaryDir m_confDir;
    QTemporaryDir m_mailDir;
    Config m_config;
};

/// A minimal New-message context for the fixture's one account.
ComposeContext newContext()
{
    ComposeContext context;
    context.accountKey = QStringLiteral("acct");
    context.kind = ComposeContext::Kind::New;
    return context;
}

}  // namespace

void TestMainWindow::aComposerOpensClean()
{
    ComposeFixture fixture;
    QVERIFY(fixture.build());

    ComposeWindow window(newContext(), fixture.config(), fixture.mailRoot());

    // Seeding fills every field, which emits every field's change signal. A
    // composer that counted those as edits would autosave a draft nobody
    // asked for, and would tell the quit path there is unsaved work in a
    // window the user opened and closed without typing.
    QVERIFY(!window.hasUnsavedEdits());
    QVERIFY(!window.lastSaveFailed());

    auto *timer = window.findChild<QTimer *>(QStringLiteral("autosave"));
    QVERIFY2(timer, "no autosave timer: the window was never built");
    QVERIFY2(!timer->isActive(),
             "seeding armed the autosave timer, so a untouched composer writes");
}

void TestMainWindow::typingMarksTheComposerDirty()
{
    ComposeFixture fixture;
    QVERIFY(fixture.build());

    ComposeWindow window(newContext(), fixture.config(), fixture.mailRoot());
    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(body);

    QVERIFY(!window.hasUnsavedEdits());
    body->setPlainText(QStringLiteral("Some text."));
    QVERIFY(window.hasUnsavedEdits());

    // The subject is part of the message as much as the body is: a draft that
    // saved the body but not the address it was going to would be worse than
    // none.
    ComposeWindow second(newContext(), fixture.config(), fixture.mailRoot());
    auto *subject = second.findChild<QLineEdit *>(QStringLiteral("subject"));
    QVERIFY(subject);
    QVERIFY(!second.hasUnsavedEdits());
    subject->setText(QStringLiteral("A subject"));
    QVERIFY(second.hasUnsavedEdits());
}

void TestMainWindow::anAutosaveWritesADraftAndClearsTheDirtyFlag()
{
    ComposeFixture fixture;
    QVERIFY(fixture.build());

    ComposeWindow window(newContext(), fixture.config(), fixture.mailRoot());
    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(body);
    body->setPlainText(QStringLiteral("Draft body."));
    QVERIFY(window.hasUnsavedEdits());

    QVERIFY2(window.saveDraftNow(), "the draft write reported failure");

    QCOMPARE(fixture.draftCount(), 1);
    QVERIFY2(!window.hasUnsavedEdits(),
             "the flag survived a successful save, so the quit path would ask");
    QVERIFY(!window.lastSaveFailed());

    // The bytes really are the message, not an empty file: the draft is
    // byte-identical to what would be sent, which is the property the one
    // built message exists for.
    const QStringList files =
        QDir(fixture.draftsCur(), {}, QDir::Name, QDir::Files).entryList();
    QCOMPARE(files.size(), 1);
    QFile written(fixture.draftsCur() + QLatin1Char('/') + files.first());
    QVERIFY(written.open(QIODevice::ReadOnly));
    const QByteArray bytes = written.readAll();
    QVERIFY2(bytes.contains("Draft body."), "the draft does not carry the body");
    // Written with the Maildir draft flag, not left bare.
    QVERIFY2(files.first().endsWith(QStringLiteral(":2,D")),
             qPrintable(QStringLiteral("wrong maildir flags: ") + files.first()));
}

void TestMainWindow::anUnwritableDraftsFolderRaisesThePersistentBanner()
{
    ComposeFixture fixture;
    QVERIFY(fixture.build());

    ComposeWindow window(newContext(), fixture.config(), fixture.mailRoot());
    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(body);
    body->setPlainText(QStringLiteral("Draft body."));

    // A FILE where the folder must go. mkpath then fails, which is a real
    // failure mode and needs no permission games that root would defeat.
    const QString accountDir = fixture.mailRoot() + QStringLiteral("/acct");
    QVERIFY(QDir().mkpath(accountDir));
    QFile blocker(accountDir + QStringLiteral("/Drafts"));
    QVERIFY(blocker.open(QIODevice::WriteOnly));
    blocker.write("not a directory");
    blocker.close();

    QVERIFY2(!window.saveDraftNow(), "an unwritable folder reported success");

    auto *banner = window.findChild<QLabel *>(QStringLiteral("draftBanner"));
    QVERIFY2(banner, "no banner widget");
    QVERIFY2(!banner->text().isEmpty(), "the banner says nothing");
    QVERIFY2(window.lastSaveFailed(),
             "lastSaveFailed() is false after a failed write, so the quit "
             "path would let the text go");
    QVERIFY2(window.hasUnsavedEdits(),
             "a failed save cleared the dirty flag, which claims the text is "
             "safe on disk when it is not");
}

void TestMainWindow::aSuccessfulSaveClearsTheBanner()
{
    ComposeFixture fixture;
    QVERIFY(fixture.build());

    ComposeWindow window(newContext(), fixture.config(), fixture.mailRoot());
    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(body);
    body->setPlainText(QStringLiteral("First."));

    const QString accountDir = fixture.mailRoot() + QStringLiteral("/acct");
    QVERIFY(QDir().mkpath(accountDir));
    QFile blocker(accountDir + QStringLiteral("/Drafts"));
    QVERIFY(blocker.open(QIODevice::WriteOnly));
    blocker.close();

    QVERIFY(!window.saveDraftNow());
    QVERIFY(window.lastSaveFailed());

    // Remove the obstruction and save again. The banner must go: a warning
    // that stays after the thing it warned about is fixed teaches the user to
    // ignore warnings, which is the second lesson in the TagRules entry.
    QVERIFY(QFile::remove(accountDir + QStringLiteral("/Drafts")));
    body->setPlainText(QStringLiteral("Second."));

    QVERIFY2(window.saveDraftNow(), "the retry failed");
    QVERIFY2(!window.lastSaveFailed(), "lastSaveFailed() stayed set");

    auto *banner = window.findChild<QLabel *>(QStringLiteral("draftBanner"));
    QVERIFY(banner);
    QVERIFY2(banner->isHidden(), "the banner is still up after a good save");
}

void TestMainWindow::anAccountWithoutADraftsFolderReportsNoFailure()
{
    ComposeFixture fixture;
    // No drafts key at all: a real configuration, warned about at startup.
    QVERIFY(fixture.build(QString()));

    ComposeWindow window(newContext(), fixture.config(), fixture.mailRoot());
    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(body);
    body->setPlainText(QStringLiteral("Nowhere to save this."));

    // Nothing was written and nothing failed. Reporting a failure here would
    // make the quit path offer a retry for a state no retry can change.
    QVERIFY2(window.saveDraftNow(),
             "a missing drafts folder was reported as a save failure");
    QVERIFY2(!window.lastSaveFailed(), "the banner state was set");

    auto *banner = window.findChild<QLabel *>(QStringLiteral("draftBanner"));
    QVERIFY(banner);
    QVERIFY(banner->isHidden());
}

void TestMainWindow::aRewrittenDraftUnlinksThePreviousRevision()
{
    ComposeFixture fixture;
    QVERIFY(fixture.build());

    ComposeWindow window(newContext(), fixture.config(), fixture.mailRoot());
    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(body);

    body->setPlainText(QStringLiteral("Revision one."));
    QVERIFY(window.saveDraftNow());
    QCOMPARE(fixture.draftCount(), 1);

    body->setPlainText(QStringLiteral("Revision two."));
    QVERIFY(window.saveDraftNow());

    // ONE file, not two. Maildir has no in-place edit, so a draft rewritten
    // every thirty seconds would otherwise accumulate one file per pause, and
    // every one of them is a message mbsync uploads.
    QCOMPARE(fixture.draftCount(), 1);

    const QStringList files =
        QDir(fixture.draftsCur(), {}, QDir::Name, QDir::Files).entryList();
    QFile written(fixture.draftsCur() + QLatin1Char('/') + files.first());
    QVERIFY(written.open(QIODevice::ReadOnly));
    const QByteArray bytes = written.readAll();
    QVERIFY2(bytes.contains("Revision two."), "the surviving file is the old one");
}

void TestMainWindow::theComposerBuildsTheMessageItsWidgetsShow()
{
    ComposeFixture fixture;
    QVERIFY(fixture.build());

    ComposeContext context = newContext();
    context.kind = ComposeContext::Kind::Reply;
    context.inReplyTo = QStringLiteral("original@example.org");
    context.references = { QStringLiteral("root@example.org"),
                           QStringLiteral("original@example.org") };
    context.to = { QStringLiteral("one@example.org") };
    context.subject = QStringLiteral("Re: a subject");

    ComposeWindow window(context, fixture.config(), fixture.mailRoot());

    auto *cc = window.findChild<QLineEdit *>(QStringLiteral("cc"));
    auto *bcc = window.findChild<QLineEdit *>(QStringLiteral("bcc"));
    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(cc && bcc && body);

    // A field the user typed, split on commas. That is wrong for a RAW header
    // and right here: this is the composer's own rendering, which joins with
    // ", ".
    cc->setText(QStringLiteral("two@example.org, three@example.org"));
    bcc->setText(QStringLiteral("  four@example.org  "));
    body->setPlainText(QStringLiteral("The body."));

    const OutgoingMessage message = window.currentMessage();
    QCOMPARE(message.accountKey, QStringLiteral("acct"));
    QCOMPARE(message.to, QStringList{ QStringLiteral("one@example.org") });
    QCOMPARE(message.cc, (QStringList{ QStringLiteral("two@example.org"),
                                       QStringLiteral("three@example.org") }));
    // Trimmed, or the whitespace reaches the wire as part of the address.
    QCOMPARE(message.bcc, QStringList{ QStringLiteral("four@example.org") });
    QCOMPARE(message.subject, QStringLiteral("Re: a subject"));
    QCOMPARE(message.markdownBody, QStringLiteral("The body."));

    // NOT optional. Without them a reply appears as an orphan thread in the
    // sender's own client, which is invisible locally.
    QCOMPARE(message.inReplyTo, QStringLiteral("original@example.org"));
    QCOMPARE(message.references.size(), 2);
    QCOMPARE(message.references.last(), QStringLiteral("original@example.org"));
}

void TestMainWindow::theFromDropdownDecidesWhichAccountSends()
{
    // TWO sending accounts, because a dropdown with one entry cannot be
    // changed and a test against it passes whether the code reads the dropdown
    // or the context. The first revision of this test did exactly that: it
    // asserted count() == 1 and then re-asserted a property another case
    // already covers, and a mutation making currentAccount() read
    // m_context.accountKey survived it.
    ComposeFixture fixture;
    QVERIFY(fixture.build(QStringLiteral("Drafts"), QStringLiteral("Sent"),
                          QString(), /*secondAccount=*/true));

    ComposeWindow window(newContext(), fixture.config(), fixture.mailRoot());
    auto *from = window.findChild<QComboBox *>(QStringLiteral("from"));
    QVERIFY2(from, "no From dropdown");

    // Both sending accounts are offered, seeded to the context's.
    QCOMPARE(from->count(), 2);
    QCOMPARE(from->currentData().toString(), QStringLiteral("acct"));
    QCOMPARE(window.currentMessage().accountKey, QStringLiteral("acct"));

    // Now change it. The dropdown is the authority once the window is open:
    // reading the context here would send from the seeded account while the
    // interface said otherwise.
    const int other = from->findData(QStringLiteral("other"));
    QVERIFY2(other >= 0, "the second account is not in the dropdown");
    from->setCurrentIndex(other);

    QCOMPARE(window.currentMessage().accountKey, QStringLiteral("other"));

    // And the choice reaches the DRAFT's destination, not just the value:
    // a draft is written into the sending account's own folder, so a composer
    // that read the context would file it under the wrong account.
    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(body);
    body->setPlainText(QStringLiteral("From the other account."));
    QVERIFY(window.saveDraftNow());

    QCOMPARE(QDir(fixture.otherDraftsCur(), {}, QDir::Name, QDir::Files).count(),
             1u);
    QCOMPARE(QDir(fixture.draftsCur(), {}, QDir::Name, QDir::Files).count(), 0u);
}

void TestMainWindow::aFormatEditPreservesTheUndoStack()
{
    ComposeFixture fixture;
    QVERIFY(fixture.build());

    ComposeWindow window(newContext(), fixture.config(), fixture.mailRoot());
    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(body);

    // Typed through a cursor, which is what makes it an undoable edit;
    // setPlainText() would not be one.
    QTextCursor typing = body->textCursor();
    typing.insertText(QStringLiteral("hello"));
    QVERIFY(body->document()->isUndoAvailable());

    QTextCursor selection = body->textCursor();
    selection.setPosition(0);
    selection.setPosition(5, QTextCursor::KeepAnchor);
    body->setTextCursor(selection);

    auto *bold = window.findChild<QAction *>(QStringLiteral("format_bold"));
    QVERIFY2(bold, "no bold action");
    bold->trigger();

    QCOMPARE(body->toPlainText(), QStringLiteral("**hello**"));

    // The property the plan's setPlainText() draft would have lost. Measured
    // in a standalone probe: setPlainText() takes isUndoAvailable from true to
    // false, so every toolbar press would throw away everything the user could
    // undo.
    QVERIFY2(body->document()->isUndoAvailable(),
             "the format edit destroyed the undo stack");

    // And it is ONE undo step, not one per character: a whole-document
    // replacement inside an edit block collapses to a single entry, so one
    // Ctrl+Z takes the tokens off and leaves the typed word.
    body->undo();
    QCOMPARE(body->toPlainText(), QStringLiteral("hello"));
}

void TestMainWindow::aFormatEditRestoresTheSelectionItAsksFor()
{
    ComposeFixture fixture;
    QVERIFY(fixture.build());

    ComposeWindow window(newContext(), fixture.config(), fixture.mailRoot());
    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(body);
    body->setPlainText(QStringLiteral("hello world"));

    // A BACKWARDS selection, anchor after the cursor, which is what a
    // right-to-left drag produces and an ordinary gesture. Measured against a
    // real widget: selectionStart()/selectionEnd() come back normalised even
    // then, so the anchor's side does not reach MarkdownFormat.
    QTextCursor selection = body->textCursor();
    selection.setPosition(5);
    selection.setPosition(0, QTextCursor::KeepAnchor);
    body->setTextCursor(selection);
    QCOMPARE(body->textCursor().selectionStart(), 0);
    QCOMPARE(body->textCursor().selectionEnd(), 5);

    auto *italic = window.findChild<QAction *>(QStringLiteral("format_italic"));
    QVERIFY(italic);
    italic->trigger();

    QCOMPARE(body->toPlainText(), QStringLiteral("*hello* world"));

    // The selection is preserved precisely so a second press can apply a
    // SECOND token to the same words, bold then italic without reselecting.
    QCOMPARE(body->textCursor().selectedText(), QStringLiteral("hello"));

    auto *bold = window.findChild<QAction *>(QStringLiteral("format_bold"));
    QVERIFY(bold);
    bold->trigger();
    QCOMPARE(body->toPlainText(), QStringLiteral("***hello*** world"));
}

void TestMainWindow::aFormatEditOnAnEmptySelectionLandsBetweenTheTokens()
{
    ComposeFixture fixture;
    QVERIFY(fixture.build());

    ComposeWindow window(newContext(), fixture.config(), fixture.mailRoot());
    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(body);
    body->setPlainText(QStringLiteral("ab"));

    QTextCursor cursor = body->textCursor();
    cursor.setPosition(1);
    body->setTextCursor(cursor);

    auto *bold = window.findChild<QAction *>(QStringLiteral("format_bold"));
    QVERIFY(bold);
    bold->trigger();

    QCOMPARE(body->toPlainText(), QStringLiteral("a****b"));

    // The property a user notices immediately when it is wrong, and the one
    // invisible to a test that only compares the resulting text: typing must
    // continue INSIDE the pair, not after it.
    QCOMPARE(body->textCursor().position(), 3);
    QVERIFY(!body->textCursor().hasSelection());

    QTextCursor typing = body->textCursor();
    typing.insertText(QStringLiteral("x"));
    QCOMPARE(body->toPlainText(), QStringLiteral("a**x**b"));
}

void TestMainWindow::theAttachmentWarningRespectsTheConfiguredThreshold()
{
    ComposeFixture fixture;
    QVERIFY(fixture.build(QStringLiteral("Drafts"), QStringLiteral("Sent"),
                          QStringLiteral("attachment_warn_bytes=1000")));

    ComposeWindow window(newContext(), fixture.config(), fixture.mailRoot());

    // The threshold, not the modal. The question itself needs a user, so what
    // is asserted is the predicate that decides whether to ask.
    QVERIFY2(!window.attachmentNeedsWarning(999), "warned below the limit");
    QVERIFY2(!window.attachmentNeedsWarning(1000),
             "warned AT the limit, which is not above it");
    QVERIFY2(window.attachmentNeedsWarning(1001), "did not warn above the limit");
}

void TestMainWindow::aDisabledAttachmentWarningWarnsAboutNothing()
{
    ComposeFixture fixture;
    QVERIFY(fixture.build(QStringLiteral("Drafts"), QStringLiteral("Sent"),
                          QStringLiteral("attachment_warn_bytes=0")));

    ComposeWindow window(newContext(), fixture.config(), fixture.mailRoot());

    // Zero means off, not "warn about everything". Read as a threshold it
    // would question an empty file, which is the opposite of what turning a
    // warning off means.
    QVERIFY(!window.attachmentNeedsWarning(0));
    QVERIFY(!window.attachmentNeedsWarning(1));
    QVERIFY(!window.attachmentNeedsWarning(100LL * 1024 * 1024));
}

void TestMainWindow::theQuotePositionDecidesWhereTheQuoteLands()
{
    const QString quote = QStringLiteral("> the original");

    {
        ComposeFixture above;
        QVERIFY(above.build(QStringLiteral("Drafts"), QStringLiteral("Sent"),
                            QStringLiteral("quote_position=above")));
        ComposeContext context = newContext();
        context.kind = ComposeContext::Kind::Reply;
        context.quotedBody = quote;

        ComposeWindow window(context, above.config(), above.mailRoot());
        auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
        QVERIFY(body);
        QVERIFY2(body->toPlainText().startsWith(quote),
                 "quote_position=above did not put the quote first");
    }

    {
        ComposeFixture below;
        QVERIFY(below.build(QStringLiteral("Drafts"), QStringLiteral("Sent"),
                            QStringLiteral("quote_position=below")));
        ComposeContext context = newContext();
        context.kind = ComposeContext::Kind::Reply;
        context.quotedBody = quote;

        ComposeWindow window(context, below.config(), below.mailRoot());
        auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
        QVERIFY(body);
        QVERIFY2(body->toPlainText().endsWith(quote),
                 "quote_position=below did not put the quote last");
        QVERIFY2(!body->toPlainText().startsWith(quote),
                 "the quote is at the top under quote_position=below");
    }
}

void TestMainWindow::theSeededQuoteIsNotAnUndoStep()
{
    ComposeFixture fixture;
    QVERIFY(fixture.build());

    ComposeContext context = newContext();
    context.kind = ComposeContext::Kind::Reply;
    context.quotedBody = QStringLiteral("> the original");

    ComposeWindow window(context, fixture.config(), fixture.mailRoot());
    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(body);
    QVERIFY(!body->toPlainText().isEmpty());

    // The seeded quote is not an edit the user made. One Ctrl+Z on a fresh
    // composer must not wipe it, which reads as the buffer losing its content.
    //
    // Worth knowing before judging this test dead weight: removing
    // clearUndoRedoStacks() alone leaves it GREEN, because setPlainText()
    // already leaves undo unavailable. The line it guards becomes load-bearing
    // the moment seedBody() stops using setPlainText, which is a change with
    // reasons to happen: applyEdit() switched to a QTextCursor replacement for
    // exactly the undo-stack property this asserts, and a later revision
    // seeding the quote the same way would put it on the stack. The combined
    // mutation (seed through a cursor AND drop the clear) does kill this.
    QVERIFY2(!body->document()->isUndoAvailable(),
             "the seeded quote is on the undo stack");
}

void TestMainWindow::aReplySeedsTheHtmlToggleFromTheOriginal()
{
    ComposeFixture fixture;
    // Config says yes; the original says no. The original wins for a reply:
    // an HTML part in it is a fact about the sender's software, not a guess
    // about their taste.
    QVERIFY(fixture.build(QStringLiteral("Drafts"), QStringLiteral("Sent"),
                          QStringLiteral("send_html=true")));

    ComposeContext context = newContext();
    context.kind = ComposeContext::Kind::Reply;
    context.seedHtml = false;

    ComposeWindow window(context, fixture.config(), fixture.mailRoot());
    auto *toggle = window.findChild<QCheckBox *>(QStringLiteral("sendHtml"));
    QVERIFY2(toggle, "no send-html toggle");
    QVERIFY2(!toggle->isChecked(),
             "a reply seeded from config rather than from the original");

    // And the other way round, so the test cannot pass by always reading
    // false: a plain-text config with an HTML original still offers HTML.
    ComposeFixture plain;
    QVERIFY(plain.build(QStringLiteral("Drafts"), QStringLiteral("Sent"),
                        QStringLiteral("send_html=false")));
    ComposeContext htmlReply = newContext();
    htmlReply.kind = ComposeContext::Kind::ReplyAll;
    htmlReply.seedHtml = true;

    ComposeWindow second(htmlReply, plain.config(), plain.mailRoot());
    auto *secondToggle =
        second.findChild<QCheckBox *>(QStringLiteral("sendHtml"));
    QVERIFY(secondToggle);
    QVERIFY2(secondToggle->isChecked(),
             "a reply-all ignored an HTML original");
}

void TestMainWindow::aNewMessageSeedsTheHtmlToggleFromConfig()
{
    ComposeFixture off;
    QVERIFY(off.build(QStringLiteral("Drafts"), QStringLiteral("Sent"),
                      QStringLiteral("send_html=false")));

    // seedHtml is deliberately TRUE here and must be ignored: a New message
    // has no original to take evidence from, so a composer reading it would be
    // reading a field nothing filled in.
    ComposeContext context = newContext();
    context.seedHtml = true;

    ComposeWindow window(context, off.config(), off.mailRoot());
    auto *toggle = window.findChild<QCheckBox *>(QStringLiteral("sendHtml"));
    QVERIFY(toggle);
    QVERIFY2(!toggle->isChecked(), "a New message ignored [compose] send_html");

    ComposeFixture on;
    QVERIFY(on.build(QStringLiteral("Drafts"), QStringLiteral("Sent"),
                     QStringLiteral("send_html=true")));
    ComposeContext forward = newContext();
    forward.kind = ComposeContext::Kind::Forward;
    forward.seedHtml = false;

    ComposeWindow second(forward, on.config(), on.mailRoot());
    auto *secondToggle =
        second.findChild<QCheckBox *>(QStringLiteral("sendHtml"));
    QVERIFY(secondToggle);
    QVERIFY2(secondToggle->isChecked(),
             "a Forward seeded from the original rather than from config");
}

void TestMainWindow::disablingInputsCoversEveryFieldAndTheToolbar()
{
    ComposeFixture fixture;
    // Zero delay: the countdown is skipped and the send commits at once, which
    // is the state the inputs must already be disabled in.
    QVERIFY(fixture.build(QStringLiteral("Drafts"), QStringLiteral("Sent"),
                          QStringLiteral("send_delay_ms=0")));

    ComposeContext context = newContext();
    context.to = { QStringLiteral("someone@example.org") };

    // Heap-allocated and tracked with a QPointer, because ComposeWindow sets
    // WA_DeleteOnClose and this case really does complete a send: the window
    // deletes itself on the way out, so a stack instance would be destroyed
    // twice. Every other case here stays on the stack, since none of them
    // closes.
    QPointer<ComposeWindow> window =
        new ComposeWindow(context, fixture.config(), fixture.mailRoot());
    auto *body = window->findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(body);
    body->setPlainText(QStringLiteral("Text."));

    auto *toolbar = window->findChild<QToolBar *>(QStringLiteral("formatToolbar"));
    auto *to = window->findChild<QLineEdit *>(QStringLiteral("to"));
    auto *subject = window->findChild<QLineEdit *>(QStringLiteral("subject"));
    auto *from = window->findChild<QComboBox *>(QStringLiteral("from"));
    auto *toggle = window->findChild<QCheckBox *>(QStringLiteral("sendHtml"));
    QVERIFY(toolbar && to && subject && from && toggle);

    QVERIFY(to->isEnabled());
    QVERIFY(!body->isReadOnly());

    auto *sendAction = window->findChild<QAction *>(QStringLiteral("compose_send"));
    QVERIFY2(sendAction, "no send action");
    sendAction->trigger();

    // The message must not change between pressing Send and the bytes being
    // built, so every input goes down for the WHOLE operation, countdown
    // included. The body is made read-only rather than disabled, so its text
    // stays selectable and legible while the send runs.
    QVERIFY2(!to->isEnabled(), "the To field is still editable during a send");
    QVERIFY2(!subject->isEnabled(), "the subject is still editable");
    QVERIFY2(!from->isEnabled(), "the account can still be changed");
    QVERIFY2(!toggle->isEnabled(), "the HTML toggle can still be flipped");
    QVERIFY2(body->isReadOnly(), "the body is still writable during a send");
    QVERIFY2(!toolbar->isEnabled(), "the formatting toolbar is still live");
    auto *attachments =
        window->findChild<QListWidget *>(QStringLiteral("attachments"));
    QVERIFY(attachments);
    QVERIFY2(!attachments->isEnabled(),
             "the attachment list is still live during a send");

    // /bin/true is the fixture's send command, so the send succeeds and the
    // composer closes itself: the message went, and holding a composer open
    // for a message already sent invites sending it twice. Waited on rather
    // than asserted immediately, since the process is handed to the event loop
    // and nothing here blocks on it. WA_DeleteOnClose then destroys the
    // window, which is what the QPointer observes.
    QTRY_VERIFY_WITH_TIMEOUT(window.isNull(), 15000);

    // And the sent copy really was filed, which is the stage after the send
    // and the one whose failure the design treats as the worst outcome here.
    const QString sentCur =
        fixture.mailRoot() + QStringLiteral("/acct/Sent/cur");
    QCOMPARE(QDir(sentCur, {}, QDir::Name, QDir::Files).count(), 1u);
}

void TestMainWindow::aFailedSendCanBeRetriedWithoutFilingTheWrongCopy()
{
    ComposeFixture fixture;
    QVERIFY(fixture.build(QStringLiteral("Drafts"), QStringLiteral("Sent"),
                          QStringLiteral("send_delay_ms=0")));

    // A stub whose outcome is switched by a sentinel file, so ONE configured
    // command can fail and then succeed. It appends its stdin to a log, which
    // is what makes the delivery count observable: the defect this guards
    // against files a sent copy of the FIRST message when the second finishes,
    // and a receiver count is the only thing that shows it.
    QTemporaryDir stubDir;
    QVERIFY(stubDir.isValid());
    const QString sentinel = stubDir.filePath(QStringLiteral("succeed"));
    const QString stub = stubDir.filePath(QStringLiteral("send.sh"));
    {
        QFile script(stub);
        QVERIFY(script.open(QIODevice::WriteOnly | QIODevice::Text));
        QTextStream out(&script);
        out << "#!/bin/sh\n"
            << "cat >> " << stubDir.filePath(QStringLiteral("stdin.log")) << "\n"
            << "[ -f " << sentinel << " ] || { echo 'refused' >&2; exit 1; }\n"
            << "exit 0\n";
    }
    QVERIFY(QFile::setPermissions(
        stub, QFileDevice::ReadOwner | QFileDevice::WriteOwner
                  | QFileDevice::ExeOwner));

    // A FRESH Config, not a copy of the fixture's reloaded: Config::load()
    // does not clear what a previous load put there, so a copy keeps the
    // fixture's /bin/true and this test would silently exercise a command that
    // always succeeds. Measured, and it produced a green nothing.
    Config config;
    {
        const QString path = QStringLiteral("%1/retry.conf").arg(stubDir.path());
        QFile file(path);
        QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
        QTextStream out(&file);
        out << "[account.acct]\n"
            << "name=Test User\n"
            << "address=user@example.org\n"
            << "maildir=acct\n"
            << "trash=Trash\n"
            << "drafts=Drafts\n"
            << "sent=Sent\n"
            << "send_command=" << stub << "\n"
            << "\n[compose]\n"
            << "send_delay_ms=0\n";
        file.close();
        config.load(path);
    }

    ComposeContext context = newContext();
    context.to = { QStringLiteral("someone@example.org") };

    QPointer<ComposeWindow> window =
        new ComposeWindow(context, config, fixture.mailRoot());
    auto *body = window->findChild<QPlainTextEdit *>(QStringLiteral("body"));
    auto *sendAction = window->findChild<QAction *>(QStringLiteral("compose_send"));
    QVERIFY(body && sendAction);

    body->setPlainText(QStringLiteral("FIRST attempt."));
    sendAction->trigger();

    // The failure re-enables the composer intact and shows the stderr; the
    // window stays open and the draft stays.
    auto *pane = window->findChild<QWidget *>(QStringLiteral("sendLogPane"));
    QVERIFY(pane);
    QTRY_VERIFY_WITH_TIMEOUT(!pane->isHidden(), 15000);

    QVERIFY2(!window.isNull(), "a failed send closed the composer");
    QVERIFY2(body->isEnabled() && !body->isReadOnly(),
             "a failed send left the composer disabled");

    // Correct the message and send again, this time succeeding. Without
    // Qt::SingleShotConnection on the per-send connect, the first send's
    // lambda is still attached: the second result runs BOTH, and the first
    // still holds the FIRST message's bytes, so it files a sent copy of the
    // wrong message and acts on a dialog it already destroyed.
    QFile marker(sentinel);
    QVERIFY(marker.open(QIODevice::WriteOnly));
    marker.close();

    body->setPlainText(QStringLiteral("SECOND attempt."));
    sendAction->trigger();

    QTRY_VERIFY_WITH_TIMEOUT(window.isNull(), 15000);

    // Exactly ONE sent copy, and it is the second message. Two files, or one
    // carrying the first attempt, is the accumulated-receiver defect.
    const QString sentCur = fixture.mailRoot() + QStringLiteral("/acct/Sent/cur");
    const QStringList filed =
        QDir(sentCur, {}, QDir::Name, QDir::Files).entryList();
    QCOMPARE(filed.size(), 1);

    QFile copy(sentCur + QLatin1Char('/') + filed.first());
    QVERIFY(copy.open(QIODevice::ReadOnly));
    const QByteArray bytes = copy.readAll();
    QVERIFY2(bytes.contains("SECOND attempt."),
             "the filed copy is not the message that was sent");
    QVERIFY2(!bytes.contains("FIRST attempt."),
             "the filed copy is the FIRST message, which never went");
}

void TestMainWindow::anUnchangedMessageIsNotWrittenAgain()
{
    ComposeFixture fixture;
    QVERIFY(fixture.build());

    ComposeWindow window(newContext(), fixture.config(), fixture.mailRoot());
    auto *body = window.findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(body);

    body->setPlainText(QStringLiteral("Once."));
    QVERIFY(window.saveDraftNow());
    QCOMPARE(fixture.draftCount(), 1);

    const QStringList first =
        QDir(fixture.draftsCur(), {}, QDir::Name, QDir::Files).entryList();
    QCOMPARE(first.size(), 1);

    // Nothing has changed, so nothing is written. Every autosave produces a
    // Maildir write that mbsync uploads, so this check and the debounce
    // together are what keep a message to a few revisions rather than dozens.
    //
    // The FILENAME is what shows it: DraftStore always generates a fresh name
    // and unlinks the previous one, so a redundant write leaves exactly one
    // file too, and a count alone cannot tell a skipped write from a repeated
    // one. Two runs of this test asserting only on the count would pass
    // against no check at all.
    QVERIFY2(window.saveDraftNow(), "the redundant save reported failure");
    QCOMPARE(fixture.draftCount(), 1);
    const QStringList second =
        QDir(fixture.draftsCur(), {}, QDir::Name, QDir::Files).entryList();
    QCOMPARE(second, first);

    // And a real change still writes: a check that skipped everything would
    // pass the assertion above and lose the user's text.
    body->setPlainText(QStringLiteral("Twice."));
    QVERIFY(window.saveDraftNow());
    const QStringList third =
        QDir(fixture.draftsCur(), {}, QDir::Name, QDir::Files).entryList();
    QCOMPARE(third.size(), 1);
    QVERIFY2(third != first, "a changed message was not written");
}

void TestMainWindow::closingInsideTheDebounceStillSavesTheDraft()
{
    ComposeFixture fixture;
    // A debounce far longer than this test, so the timer provably never fires
    // and the only thing that can write is the close itself.
    QVERIFY(fixture.build(QStringLiteral("Drafts"), QStringLiteral("Sent"),
                          QStringLiteral("autosave_interval_ms=600000")));

    // Heap-allocated: WA_DeleteOnClose destroys the window on the way out, so
    // a stack instance would be destroyed twice.
    QPointer<ComposeWindow> window =
        new ComposeWindow(newContext(), fixture.config(), fixture.mailRoot());
    auto *body = window->findChild<QPlainTextEdit *>(QStringLiteral("body"));
    QVERIFY(body);

    body->setPlainText(QStringLiteral("A paragraph typed and not yet saved."));
    QVERIFY(window->hasUnsavedEdits());

    // The timer has NOT fired. Asserted rather than assumed: if it had, the
    // draft below would prove nothing about the close path.
    auto *timer = window->findChild<QTimer *>(QStringLiteral("autosave"));
    QVERIFY(timer);
    QVERIFY2(timer->isActive(), "the debounce is not running");
    QCOMPARE(fixture.draftCount(), 0);

    // The window manager's X button, which is the route that reaches
    // closeEvent. Typing a paragraph and pressing it inside the debounce
    // interval must not lose the text.
    window->close();
    QTRY_VERIFY_WITH_TIMEOUT(window.isNull(), 5000);

    QCOMPARE(fixture.draftCount(), 1);
    const QStringList files =
        QDir(fixture.draftsCur(), {}, QDir::Name, QDir::Files).entryList();
    QCOMPARE(files.size(), 1);
    QFile written(fixture.draftsCur() + QLatin1Char('/') + files.first());
    QVERIFY(written.open(QIODevice::ReadOnly));
    QVERIFY2(written.readAll().contains("A paragraph typed and not yet saved."),
             "the close wrote a draft that is not the text that was typed");
}

void TestMainWindow::closingAfterASendWritesNoFurtherDraft()
{
    ComposeFixture fixture;
    QVERIFY(fixture.build(QStringLiteral("Drafts"), QStringLiteral("Sent"),
                          QStringLiteral("send_delay_ms=0")));

    ComposeContext context = newContext();
    context.to = { QStringLiteral("someone@example.org") };

    QPointer<ComposeWindow> window =
        new ComposeWindow(context, fixture.config(), fixture.mailRoot());
    auto *body = window->findChild<QPlainTextEdit *>(QStringLiteral("body"));
    auto *sendAction = window->findChild<QAction *>(QStringLiteral("compose_send"));
    QVERIFY(body && sendAction);

    body->setPlainText(QStringLiteral("Text that is about to be sent."));

    // A draft on disk first, so the send's removal of it is observable and the
    // close-path save has something it could wrongly put back.
    QVERIFY(window->saveDraftNow());
    QCOMPARE(fixture.draftCount(), 1);

    // Now edit again WITHOUT saving, so m_dirty is true at the moment the
    // send completes. This is what makes the m_finished guard load-bearing:
    // without it the close that follows a successful send would write a draft
    // for a message already sent, restoring the file the send just unlinked.
    body->setPlainText(QStringLiteral("Text that is about to be sent, edited."));
    QVERIFY(window->hasUnsavedEdits());

    sendAction->trigger();
    QTRY_VERIFY_WITH_TIMEOUT(window.isNull(), 15000);

    // The message went, so the drafts folder is EMPTY. A draft left behind is
    // a message the user sees waiting to be finished when it has already been
    // delivered.
    QCOMPARE(fixture.draftCount(), 0);

    // And the sent copy is there, so this is a completed send rather than a
    // send that never happened leaving nothing behind either way.
    const QString sentCur = fixture.mailRoot() + QStringLiteral("/acct/Sent/cur");
    QCOMPARE(QDir(sentCur, {}, QDir::Name, QDir::Files).count(), 1u);
}

void TestMainWindow::aCloseDuringTheCountdownIsRefused()
{
    ComposeFixture fixture;
    // A countdown long enough to close inside. The default is 5000; this is
    // the window the guard exists for and it must be provably still open when
    // the close is attempted.
    QVERIFY(fixture.build(QStringLiteral("Drafts"), QStringLiteral("Sent"),
                          QStringLiteral("send_delay_ms=30000")));

    ComposeContext context = newContext();
    context.to = { QStringLiteral("someone@example.org") };

    QPointer<ComposeWindow> window =
        new ComposeWindow(context, fixture.config(), fixture.mailRoot());
    auto *body = window->findChild<QPlainTextEdit *>(QStringLiteral("body"));
    auto *sendAction = window->findChild<QAction *>(QStringLiteral("compose_send"));
    QVERIFY(body && sendAction);
    body->setPlainText(QStringLiteral("Sent after a countdown."));

    sendAction->trigger();

    // Still counting down: the popup is up and nothing has been sent. The
    // sent folder is the evidence, since it is written only after the command
    // succeeds.
    auto *dialog = window->findChild<SendDialog *>();
    QVERIFY2(dialog, "no send popup");
    QVERIFY2(!dialog->isCommitted(), "the countdown already committed");

    // Close during the countdown. Refused: accepting it would destroy this
    // window, take the parented SendDialog down with it, and committed() would
    // never fire. The user pressed Send, watched a countdown, and would
    // believe the mail went.
    window->close();

    // Given a moment for a deletion event to be delivered if one was posted,
    // then asserted still alive. An immediate check would pass against a
    // deleteLater() already queued.
    QTest::qWait(300);
    QVERIFY2(!window.isNull(),
             "the close was accepted during the countdown, so the send was "
             "silently abandoned after the user pressed Send");
    QVERIFY2(window->isVisible() || !window.isNull(), "the window went away");

    // The send never happened, which is the point: nothing was filed.
    const QString sentCur = fixture.mailRoot() + QStringLiteral("/acct/Sent/cur");
    QCOMPARE(QDir(sentCur, {}, QDir::Name, QDir::Files).count(), 0u);

    // Cleaned up by hand, since the window refuses to close while the popup is
    // up and the test must not leak it into the next case.
    delete window;
}

void TestMainWindow::aFailedSendKeepsTheTextThatFailedToGo()
{
    ComposeFixture fixture;
    QVERIFY(fixture.build(QStringLiteral("Drafts"), QStringLiteral("Sent"),
                          QStringLiteral("send_delay_ms=0")));

    QTemporaryDir stubDir;
    QVERIFY(stubDir.isValid());
    const QString stub = stubDir.filePath(QStringLiteral("fail.sh"));
    {
        QFile script(stub);
        QVERIFY(script.open(QIODevice::WriteOnly | QIODevice::Text));
        QTextStream out(&script);
        out << "#!/bin/sh\ncat > /dev/null\necho 'refused' >&2\nexit 1\n";
    }
    QVERIFY(QFile::setPermissions(
        stub, QFileDevice::ReadOwner | QFileDevice::WriteOwner
                  | QFileDevice::ExeOwner));

    Config config;
    {
        const QString path = stubDir.filePath(QStringLiteral("fail.conf"));
        QFile file(path);
        QVERIFY(file.open(QIODevice::WriteOnly | QIODevice::Text));
        QTextStream out(&file);
        out << "[account.acct]\n"
            << "name=Test User\naddress=user@example.org\n"
            << "maildir=acct\ntrash=Trash\ndrafts=Drafts\nsent=Sent\n"
            << "send_command=" << stub << "\n"
            << "\n[compose]\nsend_delay_ms=0\n";
        file.close();
        config.load(path);
    }

    ComposeContext context = newContext();
    context.to = { QStringLiteral("someone@example.org") };

    QPointer<ComposeWindow> window =
        new ComposeWindow(context, config, fixture.mailRoot());
    auto *body = window->findChild<QPlainTextEdit *>(QStringLiteral("body"));
    auto *sendAction = window->findChild<QAction *>(QStringLiteral("compose_send"));
    QVERIFY(body && sendAction);

    // An OLD revision on disk, then an edit that is not saved. send() builds
    // from the widgets without saving, so without the fix the file left behind
    // after the failure is the old text: the user watches their correction be
    // sent, sees it fail, and gets the uncorrected version back.
    body->setPlainText(QStringLiteral("The ORIGINAL text."));
    QVERIFY(window->saveDraftNow());
    QCOMPARE(fixture.draftCount(), 1);

    body->setPlainText(QStringLiteral("The CORRECTED text."));
    sendAction->trigger();

    auto *pane = window->findChild<QWidget *>(QStringLiteral("sendLogPane"));
    QVERIFY(pane);
    QTRY_VERIFY_WITH_TIMEOUT(!pane->isHidden(), 15000);
    QVERIFY2(!window.isNull(), "a failed send closed the composer");

    // Exactly one draft, and it is the text that was attempted.
    QCOMPARE(fixture.draftCount(), 1);
    const QStringList files =
        QDir(fixture.draftsCur(), {}, QDir::Name, QDir::Files).entryList();
    QCOMPARE(files.size(), 1);
    QFile written(fixture.draftsCur() + QLatin1Char('/') + files.first());
    QVERIFY(written.open(QIODevice::ReadOnly));
    const QByteArray bytes = written.readAll();
    QVERIFY2(bytes.contains("The CORRECTED text."),
             "the draft kept after a failed send is not what was attempted");
    QVERIFY2(!bytes.contains("The ORIGINAL text."),
             "the draft kept after a failed send is the PRE-EDIT revision");

    delete window;
}

void TestMainWindow::aSmallSizeLimitIsNotDescribedAsZeroMegabytes()
{
    // Integer MB division made every figure under a megabyte read as "0 MB",
    // in BOTH halves of the same sentence: "'x' is 0 MB. Many mail servers
    // refuse messages above about 0 MB."
    QVERIFY2(!ComposeWindow::humanSize(500 * 1024).contains(QStringLiteral("0 MB")),
             "half a megabyte is described as 0 MB");
    QVERIFY2(!ComposeWindow::humanSize(1000).contains(QStringLiteral("0 MB")),
             "a kilobyte is described as 0 MB");

    // The unit steps down rather than reporting zero of a larger one.
    QVERIFY(ComposeWindow::humanSize(500 * 1024).contains(QStringLiteral("KB")));
    QVERIFY(ComposeWindow::humanSize(512).contains(QStringLiteral("bytes")));

    // A decimal while the figure is small enough for it to say something, so
    // 26 MB and 26.2 MB are not the same string.
    QVERIFY(ComposeWindow::humanSize(26214400).contains(QStringLiteral("MB")));
    QVERIFY2(ComposeWindow::humanSize(1024 * 1024 * 3 / 2)
                 .contains(QStringLiteral(".")),
             "1.5 MB lost its decimal");
}

#include "test_mainwindow.moc"