mindfork 0.10.2

A terminal AI chat written in Rust: local models via llama.cpp or OpenAI, Anthropic, Gemini and Grok in the cloud, with persistent memory, notes, RAG and tools.
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
//! Tests for the chat screen (via handle_key/render). See mod.rs.

use super::*;
use crate::entities::message::MessageRole;
use crate::features::chat_search::FeedFocus;

fn gen_id() -> Uuid {
    Uuid::new_v4()
}

/// A jump request onto `message` with nothing to highlight (the shape most of
/// these tests care about — they assert the scroll/marker, not the highlight).
fn focus_on(message: Uuid) -> Option<FeedFocus> {
    Some(FeedFocus {
        message,
        query: String::new(),
    })
}

/// A status snapshot with a ready chat server (embeddings/impersonation not configured).
fn ready_statuses() -> ServerStatuses {
    ServerStatuses {
        chat: ServerStatus::Ready,
        embed: ServerStatus::NotConfigured,
        impersonation: ServerStatus::NotConfigured,
    }
}

#[test]
fn chunk_after_midgen_note_goes_to_new_assistant_bubble() {
    // Regression: a note (AppEvent::Error about the round limit) mid-generation
    // used to make `last` a note, and the subsequent stream of the final reply got
    // appended into it (plain text, no markdown). Now a chunk opens a new bubble.
    let mut s = ChatScreen::new();
    let id = gen_id();
    s.push_user_message("собери отзывы".into());
    s.begin_generation(id, None);
    // The assistant called a tool (the assistant bubble is text-empty)...
    s.push_tool_call(
        id,
        "c1".into(),
        "web_search".into(),
        "{}".into(),
        "результаты".into(),
        0,
    );
    // ...the limit is reached — a note goes into the feed.
    s.push_error("Достигнут лимит раундов инструментов (8) — свожу итог.");
    // The forced synthesis streams the final reply.
    s.push_chunk(id, "## Итог\n\n**Вывод**.");
    s.finish_generation(id, FinishReason::Stop, false);

    // The note is a separate element; the final text is in the assistant bubble (markdown),
    // not in the note.
    let notes: Vec<_> = s.feed.iter().filter(|m| m.role == FeedRole::Note).collect();
    assert_eq!(notes.len(), 1, "expected exactly one note about the limit");
    assert!(
        !notes[0].text.contains("## Итог"),
        "the final text must not end up in the note: {:?}",
        notes[0].text
    );
    let last = s.feed.last().unwrap();
    assert_eq!(last.role, FeedRole::Assistant);
    assert_eq!(last.text, "## Итог\n\n**Вывод**.");
    assert!(!last.streaming);
}

#[test]
fn streaming_sequence_builds_feed() {
    let mut s = ChatScreen::new();
    let id = gen_id();
    s.push_user_message("hi".into());
    s.begin_generation(id, None);
    s.push_thoughts(id, "hmm");
    s.push_chunk(id, "Hel");
    s.push_chunk(id, "lo");
    s.finish_generation(id, FinishReason::Stop, false);

    assert_eq!(s.feed.len(), 2);
    assert_eq!(s.feed[0].role, FeedRole::User);
    assert_eq!(s.feed[1].role, FeedRole::Assistant);
    assert_eq!(s.feed[1].text, "Hello");
    assert_eq!(s.feed[1].thoughts, "hmm");
    assert!(!s.feed[1].streaming);
    assert!(!s.generating);
}

#[test]
fn live_stream_with_tool_matches_reload() {
    use crate::entities::message::{Message, ToolCallRecord};

    // Live: round-1 text → a tool call → round-2 text (final).
    let mut s = ChatScreen::new();
    let id = gen_id();
    s.begin_generation(id, None);
    s.push_chunk(id, "Ищу погоду.");
    s.push_tool_call(
        id,
        "c1".into(),
        "web_search".into(),
        "{\"q\":\"погода\"}".into(),
        "ясно".into(),
        0,
    );
    s.push_chunk(id, "Сейчас ясно.");
    s.finish_generation(id, FinishReason::Stop, false);

    let live = s.feed.last().unwrap().clone();
    assert_eq!(live.text, "Ищу погоду.\n\nСейчас ясно.");
    assert_eq!(live.tools.len(), 1);
    assert_eq!(live.tools[0].text_offset, "Ищу погоду.".len());

    // Reload: the same rounds as domain messages (assistant+tool / assistant).
    let mut r1 = Message::assistant("Ищу погоду.");
    r1.tool_calls = vec![ToolCallRecord {
        thought_signature: None,
        images: 0,
        id: "c1".into(),
        name: "web_search".into(),
        arguments: serde_json::json!({"q": "погода"}),
        result: Some("ясно".into()),
        subagent: None,
    }];
    let tool_msg = {
        let mut m = Message::new(MessageRole::Tool, "ясно");
        m.tool_call_id = Some("c1".into());
        m.tool_name = Some("web_search".into());
        m
    };
    let r2 = Message::assistant("Сейчас ясно.");
    let reload = FeedMessage::from_messages(&[r1, tool_msg, r2]);

    // One merged assistant block, the same text and the same call offset.
    assert_eq!(reload.len(), 1);
    assert_eq!(reload[0].text, live.text);
    assert_eq!(reload[0].tools.len(), 1);
    assert_eq!(reload[0].tools[0].text_offset, live.tools[0].text_offset);
}

#[test]
fn live_followup_makes_two_bubbles_matching_reload() {
    use crate::entities::message::{Message, ToolCallRecord};

    // Live: text 1 → followup → text 2.
    let mut s = ChatScreen::new();
    let id = gen_id();
    s.begin_generation(id, None);
    s.push_chunk(id, "Первое сообщение.");
    s.continue_assistant(id);
    s.push_chunk(id, "Второе сообщение.");
    s.finish_generation(id, FinishReason::Stop, false);

    // Two separate assistant bubbles.
    let bubbles: Vec<&FeedMessage> = s
        .feed
        .iter()
        .filter(|m| m.role == FeedRole::Assistant)
        .collect();
    assert_eq!(bubbles.len(), 2);
    assert_eq!(bubbles[0].text, "Первое сообщение.");
    assert_eq!(bubbles[1].text, "Второе сообщение.");

    // Reload: A1 (with a control call) → tool → A2 (new_bubble).
    let mut a1 = Message::assistant("Первое сообщение.");
    a1.tool_calls = vec![ToolCallRecord {
        thought_signature: None,
        images: 0,
        id: "c1".into(),
        name: "send_followup_message".into(),
        arguments: serde_json::json!({}),
        result: Some("ок".into()),
        subagent: None,
    }];
    let tool_msg = {
        let mut m = Message::new(MessageRole::Tool, "ок");
        m.tool_call_id = Some("c1".into());
        m.tool_name = Some("send_followup_message".into());
        m
    };
    let mut a2 = Message::assistant("Второе сообщение.");
    a2.new_bubble = true;
    let reload = FeedMessage::from_messages(&[a1, tool_msg, a2]);
    assert_eq!(reload.len(), 2);
    assert_eq!(reload[0].text, bubbles[0].text);
    assert_eq!(reload[1].text, bubbles[1].text);
}

/// The live bubble names the model from the moment the turn starts — the same
/// name the stored message will carry — so the header does not change under the
/// reader when generation ends. Every bubble the turn opens gets it, including
/// the one `send_followup_message` starts mid-turn. See spec §11.3.
#[test]
fn the_streaming_bubbles_of_a_turn_carry_its_model() {
    let mut s = ChatScreen::new();
    let id = gen_id();
    s.begin_generation(id, Some("gemma-4-31b".into()));
    s.push_chunk(id, "Первое сообщение.");
    s.continue_assistant(id);
    s.push_chunk(id, "Второе сообщение.");

    let bubbles: Vec<&FeedMessage> = s
        .feed
        .iter()
        .filter(|m| m.role == FeedRole::Assistant)
        .collect();
    assert_eq!(bubbles.len(), 2);
    assert!(
        bubbles
            .iter()
            .all(|b| b.model.as_deref() == Some("gemma-4-31b"))
    );

    // A mode that names no model leaves the header exactly as it was before the
    // feature existed.
    let next = gen_id();
    s.begin_generation(next, None);
    assert!(s.feed.last().unwrap().model.is_none());
}

#[test]
fn live_rewrite_discards_partial_text() {
    // Live: partial incorrect text → rewrite → the rewritten reply.
    let mut s = ChatScreen::new();
    let id = gen_id();
    s.begin_generation(id, None);
    s.push_chunk(id, "Непра");
    s.push_chunk(id, "вильный ответ.");
    s.rewrite_assistant(id);
    s.push_chunk(id, "Правильный ответ.");
    s.finish_generation(id, FinishReason::Stop, false);

    // Exactly one assistant bubble with the rewritten text (the partial one is discarded).
    let bubbles: Vec<&FeedMessage> = s
        .feed
        .iter()
        .filter(|m| m.role == FeedRole::Assistant)
        .collect();
    assert_eq!(bubbles.len(), 1);
    assert_eq!(bubbles[0].text, "Правильный ответ.");
}

#[test]
fn ignores_chunks_from_stale_generation() {
    let mut s = ChatScreen::new();
    let current = gen_id();
    let stale = gen_id();
    s.begin_generation(current, None);
    s.push_chunk(stale, "ghost");
    s.push_chunk(current, "real");
    assert_eq!(s.feed[0].text, "real");
}

#[test]
fn cancelled_finish_adds_note() {
    let mut s = ChatScreen::new();
    let id = gen_id();
    s.begin_generation(id, None);
    s.finish_generation(id, FinishReason::Cancelled, false);
    assert!(s.feed.iter().any(|i| i.role == FeedRole::Note));
    assert!(!s.generating);
}

#[test]
fn activate_chat_rebuilds_feed_and_resets_gen() {
    let mut s = ChatScreen::new();
    let prev = gen_id();
    s.begin_generation(prev, None); // as if a generation was running
    s.set_token_usage(prev, 42, Some(123), true, Some(7)); // the previous chat's token counter
    let id = gen_id();
    let messages = vec![
        Message::new(MessageRole::System, "sys"),
        Message::user("привет"),
        Message::assistant("здравствуйте"),
    ];
    s.activate_chat(
        id,
        "Чат".into(),
        &messages,
        "",
        FeedView::default(),
        None,
        None,
    );
    assert_eq!(s.active_chat, Some(id));
    assert!(!s.generating);
    assert!(s.current_gen.is_none());
    // the previous chat's token counter is cleared (otherwise it would linger in the status bar)
    assert_eq!(s.gen_tokens, 0);
    assert!(s.gen_context.is_none());
    assert!(!s.gen_context_exact);
    // a system entry in the list draws as a note row (a dialogue transcript's
    // director intervention, spec §9.13) — three rows, not two
    assert_eq!(s.feed.len(), 3);
}

/// A jump from a search hit (`AppCommand::OpenChatAt`): the feed opens on the
/// requested message instead of the tail, and the message is marked.
/// See docs/history/chat-search-stage2.md §3.
#[test]
fn activate_chat_with_focus_puts_the_feed_on_that_message() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    let mut s = ChatScreen::new();
    let messages: Vec<Message> = (0..12)
        .map(|i| Message::user(format!("реплика-{i}")))
        .collect();
    let target = messages[8].id;
    s.activate_chat(
        gen_id(),
        "Чат".into(),
        &messages,
        "",
        FeedView::default(),
        focus_on(target),
        None,
    );
    assert_eq!(s.feed_view.anchor(), Some(8));
    assert_eq!(s.feed_view.marker(), Some(8));
    // The jump itself is applied by the next render — the row only exists there
    // (§1.1), so `follow` is still on until a frame is drawn.
    let mut term = Terminal::new(TestBackend::new(40, 14)).unwrap();
    term.draw(|f| s.render(f)).unwrap();
    assert!(!s.feed_view.is_following(), "a jump leaves the tail");
    let dump = format!("{:?}", term.backend().buffer());
    assert!(dump.contains("реплика-8"), "{dump}");

    // Without a focus — the usual tail.
    let mut s = ChatScreen::new();
    s.activate_chat(
        gen_id(),
        "Чат".into(),
        &messages,
        "",
        FeedView::default(),
        None,
        None,
    );
    assert_eq!(s.feed_view.anchor(), None);
    assert_eq!(s.feed_view.marker(), None);
    term.draw(|f| s.render(f)).unwrap();
    assert!(s.feed_view.is_following());
    let dump = format!("{:?}", term.backend().buffer());
    assert!(dump.contains("реплика-11"), "the tail: {dump}");
}

/// The query rides the jump all the way into the feed, so the searched word is
/// highlighted inside the message the view lands on (fork S3(b)); an ordinary
/// activation — a plain switch, a new chat, bootstrap — highlights nothing.
#[test]
fn activate_chat_carries_the_highlight_query_only_on_a_jump() {
    let mut s = ChatScreen::new();
    let messages: Vec<Message> = (0..4)
        .map(|i| Message::user(format!("реплика-{i} про маркер")))
        .collect();
    s.activate_chat(
        gen_id(),
        "Чат".into(),
        &messages,
        "",
        FeedView::default(),
        Some(FeedFocus {
            message: messages[2].id,
            query: "маркер".into(),
        }),
        None,
    );
    assert_eq!(s.feed_view.marker(), Some(2));
    assert_eq!(s.feed_view.highlight(), Some("маркер"));

    // A plain activation must also clear the previous jump's query, or it would
    // light up whatever now sits at that index.
    s.activate_chat(
        gen_id(),
        "Чат".into(),
        &messages,
        "",
        FeedView::default(),
        None,
        None,
    );
    assert_eq!(s.feed_view.marker(), None);
    assert_eq!(s.feed_view.highlight(), None);
}

/// A focus request must never survive into the wrong chat: an id the newly
/// activated chat doesn't contain falls back to the tail, and both the anchor
/// and the marker left over from the previous chat are dropped. (The marker
/// outlives a manual scroll on purpose, so a chat switch is the one place that
/// has to clear it explicitly — this is the test that pins it.)
#[test]
fn focus_from_another_chat_falls_back_to_the_tail() {
    let mut s = ChatScreen::new();
    let first: Vec<Message> = (0..12)
        .map(|i| Message::user(format!("первый-{i}")))
        .collect();
    let stale = first[8].id;
    s.activate_chat(
        gen_id(),
        "Первый".into(),
        &first,
        "",
        FeedView::default(),
        focus_on(stale),
        None,
    );
    assert_eq!(s.feed_view.marker(), Some(8));

    let second: Vec<Message> = (0..12)
        .map(|i| Message::user(format!("второй-{i}")))
        .collect();
    s.activate_chat(
        gen_id(),
        "Второй".into(),
        &second,
        "",
        FeedView::default(),
        focus_on(stale),
        None,
    );
    assert_eq!(
        s.feed_view.anchor(),
        None,
        "an id from another chat must not anchor anything here"
    );
    assert_eq!(
        s.feed_view.marker(),
        None,
        "and the previous chat's marker must not leak in"
    );
    assert!(
        s.feed_view.is_following(),
        "and the view falls back to the tail"
    );
}

/// The yank split (docs/history/chat-search-stage2.md §1.3, §4): after the user has
/// scrolled away, content that arrives on its own leaves the view alone, while
/// user-initiated content still goes to the tail.
#[test]
fn arriving_content_does_not_yank_a_scrolled_away_reader() {
    let id = gen_id();
    let mut s = ChatScreen::new();
    s.push_user_message("вопрос".into());
    s.begin_generation(id, None);
    s.feed_view.scroll_up(5); // the user scrolled up to read
    assert!(!s.feed_view.is_following());

    // Arrives on its own — the position is kept.
    s.push_tool_call(
        id,
        "c1".into(),
        "web_search".into(),
        "{}".into(),
        "ок".into(),
        0,
    );
    assert!(!s.feed_view.is_following(), "a tool card must not yank");
    s.push_note("заметка");
    assert!(!s.feed_view.is_following(), "a note must not yank");
    s.continue_assistant(id);
    assert!(!s.feed_view.is_following(), "a followup must not yank");
    s.rewrite_assistant(id);
    assert!(!s.feed_view.is_following(), "a rewrite must not yank");

    // User-initiated — back to the tail.
    s.push_user_message("ещё".into());
    assert!(s.feed_view.is_following(), "sending goes to the tail");

    // While already following, all of them keep following.
    let id2 = gen_id();
    s.begin_generation(id2, None);
    assert!(s.feed_view.is_following());
    s.push_tool_call(
        id2,
        "c1".into(),
        "web_search".into(),
        "{}".into(),
        "ок".into(),
        0,
    );
    s.push_note("ещё заметка");
    assert!(s.feed_view.is_following());
}

#[test]
fn feed_scroll_requests_clear_only_with_vs16_emoji() {
    // Scrolling a feed with plain text doesn't require a full redraw (no flicker),
    // while with a VS16 emoji (`🗂️`) it does (wipes a "hanging" artifact on conhost).
    let id = gen_id();

    let mut clean = ChatScreen::new();
    clean.activate_chat(
        id,
        "Чат".into(),
        &[Message::assistant("обычный текст")],
        "",
        FeedView::default(),
        None,
        None,
    );
    clean.handle_key(KeyEvent::new(KeyCode::PageUp, KeyModifiers::NONE));
    assert!(
        !clean.take_feed_scrolled(),
        "plain text needs no full redraw"
    );
    // the flag is taken exactly once
    assert!(!clean.take_feed_scrolled());

    let mut emoji = ChatScreen::new();
    emoji.activate_chat(
        id,
        "Чат".into(),
        &[Message::assistant("## 🗂️ Хэш")],
        "",
        FeedView::default(),
        None,
        None,
    );
    emoji.handle_key(KeyEvent::new(KeyCode::PageDown, KeyModifiers::NONE));
    assert!(
        emoji.take_feed_scrolled(),
        "a VS16 emoji needs a full redraw"
    );
    // no repeated request without a new scroll
    assert!(!emoji.take_feed_scrolled());

    // The loop takes the flag via the generalized `take_full_redraw` — the feed source is
    // included (otherwise the VS16-artifact fix would silently break when a second source is added).
    emoji.handle_key(KeyEvent::new(KeyCode::PageUp, KeyModifiers::NONE));
    assert!(
        emoji.take_full_redraw(),
        "scrolling the feed with VS16 is part of take_full_redraw"
    );
    assert!(!emoji.take_full_redraw());
}

#[test]
fn enter_sends_when_idle_and_nonempty() {
    let mut s = ChatScreen::new();
    s.set_server_status(ready_statuses());
    for c in "привет".chars() {
        s.handle_key(KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE));
    }
    let intent = s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
    assert_eq!(intent, Some(ChatIntent::Send("привет".into())));
    // the field is cleared
    assert!(
        s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE))
            .is_none()
    );
}

#[test]
fn shift_and_alt_enter_insert_newline_not_send() {
    let mut s = ChatScreen::new();
    s.set_server_status(ready_statuses());
    for c in "ab".chars() {
        s.handle_key(KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE));
    }
    // Shift+Enter — a line break, not a send.
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::SHIFT)),
        None
    );
    for c in "cd".chars() {
        s.handle_key(KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE));
    }
    // Alt+Enter — the same break (a fallback for terminals with no kitty protocol).
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::ALT)),
        None
    );
    for c in "ef".chars() {
        s.handle_key(KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE));
    }
    assert_eq!(s.input.text(), "ab\ncd\nef");
    // A bare Enter still sends the whole multiline input.
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)),
        Some(ChatIntent::Send("ab\ncd\nef".into()))
    );
}

#[test]
fn activate_chat_loads_draft_without_marking_dirty() {
    let mut s = ChatScreen::new();
    // Activating a chat with a saved draft loads it into the input box...
    s.activate_chat(
        gen_id(),
        "Чат".into(),
        &[],
        "недописанный текст",
        FeedView::default(),
        None,
        None,
    );
    assert_eq!(s.input.text(), "недописанный текст");
    // ...but doesn't mark the draft "dirty" (otherwise it would be sent right back).
    assert_eq!(s.take_dirty_draft(), None);
    // Switching to a chat with no draft clears the input box.
    s.activate_chat(
        gen_id(),
        "Новый".into(),
        &[],
        "",
        FeedView::default(),
        None,
        None,
    );
    assert!(s.input.is_empty());
    assert_eq!(s.take_dirty_draft(), None);
}

#[test]
fn typing_marks_draft_dirty_and_take_returns_text_once() {
    let mut s = ChatScreen::new();
    type_str(&mut s, "черновик");
    // The first take returns the typed text...
    assert_eq!(s.take_dirty_draft(), Some("черновик".into()));
    // ...a repeat — None, until the input changes again.
    assert_eq!(s.take_dirty_draft(), None);
}

#[test]
fn sending_clears_draft_to_empty() {
    let mut s = ChatScreen::new();
    s.set_server_status(ready_statuses());
    type_str(&mut s, "вопрос");
    let _ = s.take_dirty_draft(); // took the draft while typing
    let intent = s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
    assert_eq!(intent, Some(ChatIntent::Send("вопрос".into())));
    // Sending cleared the field — the draft became empty (the UI will send SetDraft("")).
    assert_eq!(s.take_dirty_draft(), Some(String::new()));
}

#[test]
fn restore_input_sets_when_empty_and_prepends_when_not() {
    let mut s = ChatScreen::new();
    // An empty field is simply filled.
    s.restore_input("вопрос".into());
    assert_eq!(s.input.text(), "вопрос");
    // Non-empty — the text is prepended, the existing input is kept, and a blank
    // line separates the restored message from the draft (spec §11.7).
    s.input.clear();
    type_str(&mut s, "хвост");
    s.restore_input("голова ".into());
    assert_eq!(s.input.text(), "голова\n\nхвост");
    // The separator is exactly one blank line, whatever whitespace the two
    // sides brought with them.
    s.input.clear();
    type_str(&mut s, "хвост");
    s.restore_input("голова".into());
    assert_eq!(s.input.text(), "голова\n\nхвост");
    // A blank restored message leaves the draft exactly as typed.
    s.input.clear();
    type_str(&mut s, "хвост");
    s.restore_input("   ".into());
    assert_eq!(s.input.text(), "хвост");
}

#[test]
fn ctrl_u_emits_impersonate_with_input_seed() {
    let mut s = ChatScreen::new();
    type_str(&mut s, "начало");
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('u'), KeyModifiers::CONTROL)),
        Some(ChatIntent::Impersonate {
            seed: "начало".into()
        })
    );
    // Suppressed during generation.
    s.begin_generation(gen_id(), None);
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('u'), KeyModifiers::CONTROL)),
        None
    );
}

#[test]
fn impersonation_stream_then_stop_commits_text_to_input() {
    let mut s = ChatScreen::new();
    type_str(&mut s, "Я "); // the seed
    let id = gen_id();
    s.begin_impersonation(id);
    assert!(s.is_impersonating());
    s.push_impersonation_chunk(id, "хочу узнать про Rust");
    s.finish_impersonation(id, FinishReason::Stop);
    assert!(!s.is_impersonating());
    // The reply text (seed + generated) — in the input box.
    assert_eq!(s.input.text(), "Я хочу узнать про Rust");
}

#[test]
fn impersonation_cancel_keeps_seed_and_discards_generated() {
    let mut s = ChatScreen::new();
    type_str(&mut s, "черновик");
    let id = gen_id();
    s.begin_impersonation(id);
    s.push_impersonation_chunk(id, " дополнение");
    // Esc during impersonation — the cancel intent.
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)),
        Some(ChatIntent::CancelImpersonation)
    );
    // Cancellation (Cancelled) discards the generated part — the field keeps the seed.
    s.finish_impersonation(id, FinishReason::Cancelled);
    assert!(!s.is_impersonating());
    assert_eq!(s.input.text(), "черновик");
}

#[test]
fn impersonation_timeout_keeps_partial_text() {
    // An impersonation timeout arrives as `Length` (not `Cancelled`) — the truncated
    // reply must be kept in the field, not vanish.
    let mut s = ChatScreen::new();
    type_str(&mut s, "Я ");
    let id = gen_id();
    s.begin_impersonation(id);
    s.push_impersonation_chunk(id, "хочу узнать про");
    s.finish_impersonation(id, FinishReason::Length);
    assert!(!s.is_impersonating());
    assert_eq!(s.input.text(), "Я хочу узнать про");
}

/// A draft the provider's filter stopped is kept like a length-cut one, and the
/// feed says why it is a fragment (docs/research/content-filter-finish.md).
#[test]
fn impersonation_stopped_by_the_filter_keeps_the_draft_and_says_why() {
    let loc = crate::shared::i18n::locale(crate::shared::i18n::Lang::default());
    let mut s = ChatScreen::new();
    type_str(&mut s, "I ");
    let id = gen_id();
    s.begin_impersonation(id);
    s.push_impersonation_chunk(id, "want to ask about");
    s.finish_impersonation(id, FinishReason::Filtered);
    assert!(!s.is_impersonating());
    assert_eq!(s.input.text(), "I want to ask about");
    let last = s.feed.last().expect("a note");
    assert_eq!(last.role, FeedRole::Note);
    assert_eq!(last.text, loc.t("ui.err.impersonation_filtered"));
}

#[test]
fn keys_ignored_during_impersonation_except_cancel_quit() {
    let mut s = ChatScreen::new();
    s.begin_impersonation(gen_id());
    // A regular key isn't typed into the field (the field is hidden).
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('x'), KeyModifiers::NONE)),
        None
    );
    // Ctrl+Q / F10 still quit (quit moved off Ctrl+C).
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('q'), KeyModifiers::CONTROL)),
        Some(ChatIntent::Quit)
    );
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::F(10), KeyModifiers::NONE)),
        Some(ChatIntent::Quit)
    );
}

#[test]
fn render_during_impersonation_does_not_panic() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    let mut s = ChatScreen::new();
    let id = gen_id();
    s.begin_impersonation(id);
    s.push_impersonation_chunk(id, "текст реплики");
    let mut term = Terminal::new(TestBackend::new(50, 16)).unwrap();
    term.draw(|f| s.render(f)).unwrap();
}

/// The screen threads the chat server's status into the feed: only a reported
/// `NotConfigured` brings the setup routes, and the screen's own starting status
/// (`Connecting`) does not — a configured install must not flash them on launch
/// (docs/research/public-release-readiness.md §3.2).
#[test]
fn setup_routes_follow_the_reported_chat_status() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    let drawn = |s: &mut ChatScreen| {
        let mut term = Terminal::new(TestBackend::new(240, 20)).unwrap();
        term.draw(|f| s.render(f)).unwrap();
        let buf = term.backend().buffer().clone();
        buf.content().iter().map(|c| c.symbol()).collect::<String>()
    };
    let mut s = ChatScreen::new();
    assert!(
        !drawn(&mut s).contains("mindfork demo"),
        "shown before any status"
    );

    s.set_server_status(ServerStatuses {
        chat: ServerStatus::NotConfigured,
        ..ready_statuses()
    });
    assert!(drawn(&mut s).contains("mindfork demo"));

    s.set_server_status(ready_statuses());
    assert!(
        !drawn(&mut s).contains("mindfork demo"),
        "kept after an engine was set"
    );
}

#[test]
fn ctrl_r_and_e_emit_intents_when_idle() {
    let mut s = ChatScreen::new();
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('r'), KeyModifiers::CONTROL)),
        Some(ChatIntent::RegenerateLast)
    );
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('e'), KeyModifiers::CONTROL)),
        Some(ChatIntent::DeleteLastExchange)
    );
}

#[test]
fn ctrl_r_and_e_suppressed_while_generating() {
    let mut s = ChatScreen::new();
    s.begin_generation(gen_id(), None);
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('r'), KeyModifiers::CONTROL)),
        None
    );
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('e'), KeyModifiers::CONTROL)),
        None
    );
}

/// Turns on `Ctrl+R`/`Ctrl+E` confirmation via a settings snapshot.
fn with_confirm() -> ChatScreen {
    let mut s = ChatScreen::new();
    let mut cfg = AppConfig::default();
    cfg.interface.confirm_destructive_keys = true;
    s.set_settings(cfg, Vec::new(), Vec::new(), Default::default(), Vec::new());
    s
}

#[test]
fn ctrl_r_with_confirm_opens_popup_then_enter_confirms() {
    let mut s = with_confirm();
    // The first press doesn't yield an intent — it opens the confirmation popup.
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('r'), KeyModifiers::CONTROL)),
        None
    );
    assert_eq!(s.confirm, Some(ConfirmAction::Regenerate));
    // Enter confirms and closes the popup.
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)),
        Some(ChatIntent::RegenerateLast)
    );
    assert_eq!(s.confirm, None);
}

#[test]
fn ctrl_e_with_confirm_esc_cancels() {
    let mut s = with_confirm();
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('e'), KeyModifiers::CONTROL)),
        None
    );
    assert_eq!(s.confirm, Some(ConfirmAction::DeleteExchange));
    // Esc cancels — the popup closes, no intent.
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)),
        None
    );
    assert_eq!(s.confirm, None);
}

#[test]
fn confirm_popup_ignores_other_keys() {
    let mut s = with_confirm();
    s.handle_key(KeyEvent::new(KeyCode::Char('r'), KeyModifiers::CONTROL));
    // An arbitrary key doesn't close the popup and isn't typed into the input box.
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('x'), KeyModifiers::NONE)),
        None
    );
    assert_eq!(s.confirm, Some(ConfirmAction::Regenerate));
    assert!(s.input.is_empty());
}

#[test]
fn ctrl_q_breaks_through_confirm_popup_to_quit() {
    let mut s = with_confirm();
    s.handle_key(KeyEvent::new(KeyCode::Char('r'), KeyModifiers::CONTROL));
    assert_eq!(s.confirm, Some(ConfirmAction::Regenerate));
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('q'), KeyModifiers::CONTROL)),
        Some(ChatIntent::Quit)
    );
    assert_eq!(s.confirm, None);
}

#[test]
fn ctrl_r_and_e_emit_directly_without_confirm() {
    // By default (with no settings snapshot) confirmation is off.
    let mut s = ChatScreen::new();
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('r'), KeyModifiers::CONTROL)),
        Some(ChatIntent::RegenerateLast)
    );
    assert_eq!(s.confirm, None);
}

// ---------- the dangerous-tool confirmation popup (spec §9.8) ----------

/// The ids the loop parked the call under. They must come back untouched — a
/// reply landing on the wrong turn or the wrong call is what the orchestrator
/// drops on the other side.
const TOOL_CALL_ID: &str = "call-7";

/// A screen with the dangerous-tool popup open, as [`ChatScreen::request_tool_confirm`]
/// opens it. Deliberately **not** generating: the tests below pin the popup's own
/// logic, while `tool_confirm_is_answerable_while_generating` pins the routing
/// order that makes it usable at all.
fn with_tool_confirm() -> (ChatScreen, Uuid) {
    let mut s = ChatScreen::new();
    let id = gen_id();
    s.request_tool_confirm(
        id,
        TOOL_CALL_ID.into(),
        "python_exec".into(),
        r#"{"code":"print(1)"}"#.into(),
        None,
    );
    (s, id)
}

/// The intent the popup must emit for `decision`, carrying the ids back.
fn confirmed(id: Uuid, decision: ToolDecision) -> Option<ChatIntent> {
    Some(ChatIntent::ConfirmTool {
        generation_id: id,
        call_id: TOOL_CALL_ID.into(),
        decision,
    })
}

#[test]
fn tool_confirm_enter_allows_the_call_and_closes_the_popup() {
    let (mut s, id) = with_tool_confirm();
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)),
        confirmed(id, ToolDecision::Allow)
    );
    assert_eq!(s.tool_confirm, None);
}

#[test]
fn tool_confirm_a_allows_the_tool_for_the_rest_of_the_turn() {
    let (mut s, id) = with_tool_confirm();
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('a'), KeyModifiers::NONE)),
        confirmed(id, ToolDecision::AllowForTurn)
    );
    assert_eq!(s.tool_confirm, None);
}

/// Layout-independent, like every other letter shortcut: physical A is `ф` on a
/// Russian layout (see shared::keys).
#[test]
fn tool_confirm_a_works_under_a_cyrillic_layout() {
    let (mut s, id) = with_tool_confirm();
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('ф'), KeyModifiers::NONE)),
        confirmed(id, ToolDecision::AllowForTurn),
        "ф (physical A) — allow for the turn"
    );
}

/// Declining is **not** cancelling the turn: the loop carries on with the
/// refusal, and a second `Esc` — now that the popup is gone — cancels as it
/// always did.
#[test]
fn tool_confirm_esc_declines_without_cancelling_the_turn() {
    let (mut s, id) = with_tool_confirm();
    s.begin_generation(id, None);
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)),
        confirmed(id, ToolDecision::Deny)
    );
    assert_eq!(s.tool_confirm, None);
    // Only now does Esc mean "cancel".
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)),
        Some(ChatIntent::Cancel)
    );
}

#[test]
fn tool_confirm_ignores_other_keys_and_stays_open() {
    let (mut s, _) = with_tool_confirm();
    for key in [
        KeyEvent::new(KeyCode::Char('x'), KeyModifiers::NONE),
        KeyEvent::new(KeyCode::Down, KeyModifiers::NONE),
    ] {
        assert_eq!(s.handle_key(key), None, "{key:?} must be ignored");
        assert!(s.tool_confirm.is_some(), "the popup stays open");
    }
    assert!(s.input.is_empty(), "and nothing is typed into the message");
}

#[test]
fn ctrl_q_and_f10_break_through_the_tool_confirm_popup() {
    let (mut s, _) = with_tool_confirm();
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('q'), KeyModifiers::CONTROL)),
        Some(ChatIntent::Quit)
    );
    assert_eq!(s.tool_confirm, None);

    let (mut s, _) = with_tool_confirm();
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::F(10), KeyModifiers::NONE)),
        Some(ChatIntent::Quit)
    );
    assert_eq!(s.tool_confirm, None);
}

/// The point of the feature: the popup is open **during** the turn — the loop is
/// parked on it. Without the routing order in `handle_key` (the popup checked
/// before the generation gate) `Enter` would be swallowed and `Esc` would cancel
/// the turn instead of answering.
#[test]
fn tool_confirm_is_answerable_while_generating() {
    let (mut s, id) = with_tool_confirm();
    s.begin_generation(id, None);
    assert!(s.generating);
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)),
        confirmed(id, ToolDecision::Allow)
    );
    assert!(s.generating, "answering doesn't end the turn");
}

/// The call is formatted through `features::tools::present` — the same
/// formatting the feed uses afterwards (fork F7) — so the user decides on
/// readable code, not on a JSON blob.
#[test]
fn tool_confirm_popup_shows_the_call_as_code_with_the_three_options() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    let mut s = ChatScreen::new();
    let id = gen_id();
    s.begin_generation(id, None);
    s.request_tool_confirm(
        id,
        TOOL_CALL_ID.into(),
        "python_exec".into(),
        r#"{"code": "print(1)\nprint(2)"}"#.into(),
        None,
    );
    let mut term = Terminal::new(TestBackend::new(90, 20)).unwrap();
    term.draw(|f| s.render(f)).unwrap();
    // Rows joined by hand rather than `format!("{:?}", buffer)`: the buffer's
    // Debug prints rows inside quotes **without escaping** the ones in the
    // content, so a `"` assertion against it can never match — and the "no raw
    // JSON" check below is exactly such an assertion.
    let buf = term.backend().buffer();
    let mut text = String::new();
    for y in buf.area.top()..buf.area.bottom() {
        for x in buf.area.left()..buf.area.right() {
            text.push_str(buf[(x, y)].symbol());
        }
        text.push('\n');
    }

    assert!(text.contains("python_exec"), "the tool name: {text}");
    // The code arrives as lines — each statement on a row of its own, and no
    // trace of the JSON it was extracted from.
    assert!(text.contains("print(1)"), "{text}");
    assert!(text.contains("print(2)"), "{text}");
    assert!(
        !text
            .lines()
            .any(|l| l.contains("print(1)") && l.contains("print(2)")),
        "the two statements belong on separate lines: {text}"
    );
    assert!(
        !text.contains("\"code\""),
        "the raw JSON must not be shown: {text}"
    );
    // The footer spells out all three answers.
    for option in [
        "Enter — выполнить",
        "A — разрешить до конца хода",
        "Esc — отклонить",
    ] {
        assert!(text.contains(option), "missing the \"{option}\" option");
    }
}

/// The popup states what a `python_exec` call would hand the sandbox, and whether that
/// sandbox has the network (docs/history/sandbox-file-exchange.md §12 T6). The compact view of the
/// arguments drops arrays outright — which this test also pins — so `files`, the argument
/// that decides what leaves the chat, would otherwise not be shown at all.
#[test]
fn the_confirm_popup_names_the_files_going_in_and_the_network() {
    use crate::features::chat_inputs::{ConfirmFile, ConfirmInputs};
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    let mut s = ChatScreen::new();
    let id = gen_id();
    s.begin_generation(id, None);
    s.request_tool_confirm(
        id,
        TOOL_CALL_ID.into(),
        "python_exec".into(),
        // `r##"…"##`: the handle `"#9"` carries the sequence that would close a
        // single-hash raw string early.
        r##"{"code": "print(1)", "files": ["sales.xlsx", "#9"]}"##.into(),
        Some(ConfirmInputs {
            files: vec![
                ConfirmFile {
                    handle: "sales.xlsx".into(),
                    resolved: Some(("sales.xlsx".into(), 18_432)),
                },
                ConfirmFile {
                    handle: "#9".into(),
                    resolved: None,
                },
            ],
            net: false,
            over_cap: None,
        }),
    );
    let mut term = Terminal::new(TestBackend::new(90, 20)).unwrap();
    term.draw(|f| s.render(f)).unwrap();
    let buf = term.backend().buffer();
    let mut text = String::new();
    for y in buf.area.top()..buf.area.bottom() {
        for x in buf.area.left()..buf.area.right() {
            text.push_str(buf[(x, y)].symbol());
        }
        text.push('\n');
    }

    // The file that is going in, with the size the copy will cost.
    assert!(text.contains("sales.xlsx"), "{text}");
    assert!(text.contains("18.0 KB"), "{text}");
    // A handle that reaches nothing is named as such, not shown as a file.
    assert!(text.contains("#9"), "{text}");
    // The other half of the decision.
    assert!(text.contains("сеть"), "{text}");
    // The premise: the argument itself never reaches the popup's own rendering.
    assert!(
        !text.contains("\"files\""),
        "the compact view shows no arrays: {text}"
    );
}

/// A call over the per-call cap is refused before it runs (§12 T8), and the popup says so
/// before the user is asked to consent — above the files, which a long list pushes down.
#[test]
fn the_confirm_popup_says_a_call_over_the_files_cap_will_be_refused() {
    use crate::features::chat_inputs::{ConfirmFile, ConfirmInputs, MAX_INPUT_FILES};
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    let mut s = ChatScreen::new();
    let id = gen_id();
    s.begin_generation(id, None);
    let count = MAX_INPUT_FILES + 1;
    s.request_tool_confirm(
        id,
        TOOL_CALL_ID.into(),
        "python_exec".into(),
        r#"{"code": "print(1)", "files": []}"#.into(),
        Some(ConfirmInputs {
            files: (1..=count)
                .map(|n| ConfirmFile {
                    handle: format!("#{n}"),
                    resolved: Some((format!("export-{n:02}.csv"), 18_432)),
                })
                .collect(),
            net: false,
            over_cap: Some((count, 18_432 * count as u64)),
        }),
    );
    let mut term = Terminal::new(TestBackend::new(90, 30)).unwrap();
    term.draw(|f| s.render(f)).unwrap();
    let buf = term.backend().buffer();
    let rows: Vec<String> = (buf.area.top()..buf.area.bottom())
        .map(|y| {
            (buf.area.left()..buf.area.right())
                .map(|x| buf[(x, y)].symbol())
                .collect()
        })
        .collect();
    let text = rows.join("\n");

    let warned = rows.iter().position(|row| row.contains("сверх лимита"));
    let listed = rows.iter().position(|row| row.contains("export-01.csv"));
    assert!(warned.is_some(), "the cap is not stated: {text}");
    assert!(text.contains("отклонён"), "{text}");
    assert!(
        warned < listed,
        "the cap has to be said before the files: {text}"
    );
}

/// The popup exists so the user reads the code before consenting to it — so the code has
/// to be **on screen**. It is rendered with `Wrap`, and sizing it by the lines it holds
/// rather than the rows they take clipped the bottom one the moment any line wrapped. The
/// `files` line does that at two named files, which is the popup's own subject.
#[test]
fn the_confirm_popup_shows_the_code_when_the_files_line_wraps() {
    use crate::features::chat_inputs::{ConfirmFile, ConfirmInputs};
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    let mut s = ChatScreen::new();
    let id = gen_id();
    s.begin_generation(id, None);
    // Long enough that the line cannot fit the popup's 70 columns — three files with sizes
    // is an ordinary call, not a contrived one.
    let files = [
        "quarterly-report-2026.xlsx",
        "customers-export.csv",
        "notes.md",
    ];
    s.request_tool_confirm(
        id,
        TOOL_CALL_ID.into(),
        "python_exec".into(),
        r#"{"code": "print(MARKER)", "files": ["a", "b", "c"]}"#.into(),
        Some(ConfirmInputs {
            files: files
                .iter()
                .map(|name| ConfirmFile {
                    handle: (*name).into(),
                    resolved: Some(((*name).into(), 18_432)),
                })
                .collect(),
            net: false,
            over_cap: None,
        }),
    );
    let mut term = Terminal::new(TestBackend::new(90, 20)).unwrap();
    term.draw(|f| s.render(f)).unwrap();
    let buf = term.backend().buffer();
    let mut text = String::new();
    for y in buf.area.top()..buf.area.bottom() {
        for x in buf.area.left()..buf.area.right() {
            text.push_str(buf[(x, y)].symbol());
        }
        text.push('\n');
    }

    // The premise: the files line really is longer than the popup is wide, so it wraps and
    // the two counts disagree. Without it the test would pass on a popup that never wrapped.
    assert!(
        files.iter().all(|f| text.contains(f)),
        "the files going in are the subject of the popup: {text}"
    );
    assert!(
        text.contains("print(MARKER)"),
        "the code being approved was clipped off the bottom: {text}"
    );
}

#[test]
fn esc_opens_chat_list_else_cancels_generation() {
    let mut s = ChatScreen::new();
    // With no generation running, Esc asks to open the chat-list screen (`app` creates it).
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)),
        Some(ChatIntent::OpenChatList)
    );
    // During generation, Esc first cancels it.
    s.begin_generation(gen_id(), None);
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)),
        Some(ChatIntent::Cancel)
    );
}

#[test]
fn ctrl_q_and_f10_quit_from_chat() {
    let mut s = ChatScreen::new();
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('q'), KeyModifiers::CONTROL)),
        Some(ChatIntent::Quit)
    );
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::F(10), KeyModifiers::NONE)),
        Some(ChatIntent::Quit)
    );
    // Ctrl+C with no selection — a no-op (freed up for copying, not quitting).
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL)),
        None
    );
}

#[test]
fn ctrl_c_copies_selection_ctrl_x_cuts() {
    let mut s = ChatScreen::new();
    s.input.set_text("hello world");
    s.input
        .on_key(KeyEvent::new(KeyCode::Home, KeyModifiers::CONTROL)); // cursor to the start
    for _ in 0..5 {
        s.input
            .on_key(KeyEvent::new(KeyCode::Right, KeyModifiers::SHIFT)); // select "hello"
    }
    // Ctrl+C → an intent to write the selection to the clipboard; the selection clears, the text is intact.
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('c'), KeyModifiers::CONTROL)),
        Some(ChatIntent::CopyToClipboard("hello".into()))
    );
    assert!(!s.input.has_selection());
    assert_eq!(s.input.text(), "hello world");
    // Select the next 5 characters (" worl") and cut them — the text shrinks.
    for _ in 0..5 {
        s.input
            .on_key(KeyEvent::new(KeyCode::Right, KeyModifiers::SHIFT));
    }
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('x'), KeyModifiers::CONTROL)),
        Some(ChatIntent::CopyToClipboard(" worl".into()))
    );
    assert_eq!(s.input.text(), "hellod");
}

/// `?` is a character, not a hotkey — on an empty box as much as on a full
/// one. It opened the help while it was the chat's alias for `F1`, which made
/// it a key that worked on one screen out of six, and on an empty box at that;
/// the help is `F1` (routed above every screen) or `/help` (spec §11.7).
#[test]
fn question_mark_is_typed_never_a_help_key() {
    let mut s = ChatScreen::new();
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('?'), KeyModifiers::NONE)),
        None
    );
    assert_eq!(s.input.text(), "?");
    type_str(&mut s, "abc");
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('?'), KeyModifiers::NONE)),
        None
    );
    assert_eq!(s.input.text(), "?abc?");
}

#[test]
fn settings_event_updates_theme_palette() {
    use crate::shared::config::Theme;
    let mut s = ChatScreen::new();
    assert_eq!(s.palette, Palette::for_theme(Theme::Auto));
    let mut cfg = AppConfig::default();
    cfg.interface.theme = Theme::Dark;
    s.set_settings(cfg, vec![], Vec::new(), Default::default(), Vec::new());
    assert_eq!(s.palette, Palette::for_theme(Theme::Dark));
}

#[test]
fn ctrl_p_opens_settings_only_with_snapshot() {
    let mut s = ChatScreen::new();
    // Without a settings snapshot — Ctrl+P does nothing.
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('p'), KeyModifiers::CONTROL)),
        None
    );
    s.set_settings(
        AppConfig::default(),
        vec![Profile::new("P", "sys")],
        Vec::new(),
        Default::default(),
        Vec::new(),
    );
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('p'), KeyModifiers::CONTROL)),
        Some(ChatIntent::OpenSettings)
    );
}

#[test]
fn ctrl_shortcuts_work_under_cyrillic_layout() {
    // Under a Cyrillic layout physical keys report Cyrillic characters: Ctrl+з (physical P),
    // Ctrl+й (physical Q) — the shortcuts must still fire.
    let mut s = ChatScreen::new();
    s.set_settings(
        AppConfig::default(),
        vec![Profile::new("P", "sys")],
        Vec::new(),
        Default::default(),
        Vec::new(),
    );
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('з'), KeyModifiers::CONTROL)),
        Some(ChatIntent::OpenSettings),
        "Ctrl+з (physical P) opens settings"
    );
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('й'), KeyModifiers::CONTROL)),
        Some(ChatIntent::Quit),
        "Ctrl+й (physical Q) — quit"
    );
}

#[test]
fn rename_chat_updates_title_bar_of_active_chat() {
    let mut s = ChatScreen::new();
    let id = gen_id();
    s.activate_chat(
        id,
        "Старое".into(),
        &[],
        "",
        FeedView::default(),
        None,
        None,
    );
    s.rename_chat(id, "Новое".into());
    assert_eq!(s.title, "Новое");
    // A foreign chat doesn't touch the active chat's header.
    s.rename_chat(gen_id(), "Постороннее".into());
    assert_eq!(s.title, "Новое");
}

#[test]
fn f5_copies_active_chat_in_main_window() {
    let mut s = ChatScreen::new();
    // With no active chat, F5 — a no-op.
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::F(5), KeyModifiers::NONE)),
        None
    );
    let id = gen_id();
    s.activate_chat(id, "Чат".into(), &[], "", FeedView::default(), None, None);
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::F(5), KeyModifiers::NONE)),
        Some(ChatIntent::CopyChat(id))
    );
}

#[test]
fn late_list_op_results_fall_to_feed() {
    // When the list screen is closed, late results of list operations (copy/
    // auto-title) `app` puts into the feed as a note via push_note/push_error.
    let mut s = ChatScreen::new();
    s.push_note("Переписка скопирована в буфер обмена");
    s.push_error("не удалось");
    assert_eq!(
        s.feed.iter().filter(|m| m.role == FeedRole::Note).count(),
        2
    );
}

fn wheel(kind: MouseEventKind) -> MouseEvent {
    MouseEvent {
        kind,
        column: 0,
        row: 0,
        modifiers: KeyModifiers::NONE,
    }
}

fn mouse_at(kind: MouseEventKind, column: u16, row: u16) -> MouseEvent {
    MouseEvent {
        kind,
        column,
        row,
        modifiers: KeyModifiers::NONE,
    }
}

#[test]
fn mouse_click_and_drag_in_input_build_selection() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    use ratatui::crossterm::event::MouseButton;
    let mut s = ChatScreen::new();
    type_str(&mut s, "hello world");
    // Render so the input box remembers its area (last_area).
    let mut term = Terminal::new(TestBackend::new(50, 16)).unwrap();
    term.draw(|f| s.render(f)).unwrap();
    let area = s
        .input
        .last_area_for_test()
        .expect("the field has been rendered");
    // A click at the start of the field — the cursor goes there, no selection yet.
    s.handle_mouse(mouse_at(
        MouseEventKind::Down(MouseButton::Left),
        area.x,
        area.y,
    ));
    assert_eq!(s.input.cursor(), (0, 0));
    assert!(!s.input.has_selection());
    // Dragging right by 5 columns grows the "hello" selection.
    s.handle_mouse(mouse_at(
        MouseEventKind::Drag(MouseButton::Left),
        area.x + 5,
        area.y,
    ));
    assert!(s.input.has_selection());
    assert_eq!(s.input.selected_text().as_deref(), Some("hello"));
}

#[test]
fn mouse_click_outside_input_does_not_move_cursor() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    use ratatui::crossterm::event::MouseButton;
    let mut s = ChatScreen::new();
    type_str(&mut s, "hello");
    let mut term = Terminal::new(TestBackend::new(50, 16)).unwrap();
    term.draw(|f| s.render(f)).unwrap();
    // A click into the feed (top of the screen, outside the input box) — the field's cursor doesn't move.
    s.handle_mouse(mouse_at(MouseEventKind::Down(MouseButton::Left), 0, 0));
    assert_eq!(s.input.cursor(), (0, 5)); // the cursor stayed at the end of the text
    assert!(!s.input.has_selection());
}

#[test]
fn mouse_wheel_up_scrolls_feed_and_disables_follow() {
    let mut s = ChatScreen::new();
    assert!(s.feed_view.is_following());
    s.handle_mouse(wheel(MouseEventKind::ScrollUp));
    assert!(
        !s.feed_view.is_following(),
        "scrolling up disables tail-following"
    );
}

#[test]
fn ctrl_t_and_ctrl_o_report_the_new_collapse_state() {
    // Both toggles are independent and both report the whole view back, so the
    // orchestrator can store it on the chat (spec §11.3, docs/feed-collapse.md).
    let mut s = ChatScreen::new();
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('t'), KeyModifiers::CONTROL)),
        Some(ChatIntent::SetFeedView(FeedView {
            thoughts: true,
            tools: false
        }))
    );
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('o'), KeyModifiers::CONTROL)),
        Some(ChatIntent::SetFeedView(FeedView {
            thoughts: true,
            tools: true
        }))
    );
    // ...and back, one at a time.
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('t'), KeyModifiers::CONTROL)),
        Some(ChatIntent::SetFeedView(FeedView {
            thoughts: false,
            tools: true
        }))
    );
    // Layout-independent: Ctrl+щ is the physical O.
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('щ'), KeyModifiers::CONTROL)),
        Some(ChatIntent::SetFeedView(FeedView::default()))
    );
}

#[test]
fn activating_a_chat_applies_its_stored_collapse_state() {
    // The state belongs to the chat, so switching to one that had things
    // expanded shows them expanded — without a keypress.
    let mut s = ChatScreen::new();
    let expanded = FeedView {
        thoughts: true,
        tools: true,
    };
    s.activate_chat(gen_id(), "Чат".into(), &[], "", expanded, None, None);
    assert_eq!(s.feed_view.view(), expanded);
    // ...and switching to a chat that never expanded anything collapses again.
    s.activate_chat(
        gen_id(),
        "Другой".into(),
        &[],
        "",
        FeedView::default(),
        None,
        None,
    );
    assert_eq!(s.feed_view.view(), FeedView::default());
}

#[test]
fn ctrl_w_toggles_mouse_capture_intent() {
    let mut s = ChatScreen::new();
    // Capture is off by default → the first press turns it on (true).
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('w'), KeyModifiers::CONTROL)),
        Some(ChatIntent::SetMouseCapture(true))
    );
    // The second one — turns it off (false).
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('w'), KeyModifiers::CONTROL)),
        Some(ChatIntent::SetMouseCapture(false))
    );
    // Also works under a Cyrillic layout: Ctrl+ц (physical W).
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('ц'), KeyModifiers::CONTROL)),
        Some(ChatIntent::SetMouseCapture(true))
    );
}

#[test]
fn mouse_wheel_ignored_while_overlay_open() {
    let mut s = ChatScreen::new();
    // The emoji picker stands in for any chat-owned popup (the help dialog,
    // which this test used to open, is the runtime's overlay now).
    s.handle_key(KeyEvent::new(KeyCode::Char('b'), KeyModifiers::CONTROL));
    assert!(s.emoji.is_some());
    s.handle_mouse(wheel(MouseEventKind::ScrollUp));
    assert!(
        s.feed_view.is_following(),
        "with a popup open, the wheel doesn't touch the feed"
    );
}

fn mk_checker() -> SpellChecker {
    let dict = spellbook::Dictionary::new("SET UTF-8\n", "2\nhello\nworld\n").unwrap();
    SpellChecker::new(vec![dict], std::collections::HashSet::new(), None)
}

fn type_str(s: &mut ChatScreen, text: &str) {
    for c in text.chars() {
        s.handle_key(KeyEvent::new(KeyCode::Char(c), KeyModifiers::NONE));
    }
}

#[test]
fn input_with_risky_glyph_requests_full_redraw() {
    // An edit LEFT of a VS16 emoji in the input box moves it onto a foreign symbol's spot:
    // the diff sends the trailing half, the backend prints it with no `MoveTo` and the row drifts
    // right (ratatui#2651, mechanics in `shared::ui`). So a field with such a glyph
    // is drawn with a full redraw; with plain text — it isn't.
    let mut s = ChatScreen::new();
    type_str(&mut s, "hello");
    assert!(!s.take_full_redraw(), "plain text needs no full redraw");

    type_str(&mut s, "\u{2764}\u{FE0F}");
    assert!(s.take_full_redraw(), "a field with ❤️ needs a full redraw");

    // And subsequent edits do too — the glyph is still in the field.
    type_str(&mut s, "x");
    assert!(
        s.take_full_redraw(),
        "an edit while the glyph is live — too"
    );

    // Clearing the field lifts the requirement.
    s.handle_key(KeyEvent::new(KeyCode::Char('k'), KeyModifiers::CONTROL));
    s.take_full_redraw();
    type_str(&mut s, "plain");
    assert!(
        !s.take_full_redraw(),
        "after clearing, plain text needs no redraw"
    );
}

#[test]
fn ctrl_g_opens_suggestions_for_misspelled_word() {
    let mut s = ChatScreen::new();
    s.set_spellchecker(mk_checker());
    type_str(&mut s, "helo"); // cursor at the end of the misspelled word
    s.handle_key(KeyEvent::new(KeyCode::Char('g'), KeyModifiers::CONTROL));
    assert!(s.suggest.is_some());
    // the last item — "add to dictionary"
    let items = &s.suggest.as_ref().unwrap().items;
    assert_eq!(items.last(), Some(&SuggestItem::AddToDictionary));
}

#[test]
fn applying_suggestion_replaces_word() {
    let mut s = ChatScreen::new();
    s.set_spellchecker(mk_checker());
    type_str(&mut s, "helo");
    s.open_suggestions();
    // the first item — the "hello" suggestion; Enter applies it
    assert_eq!(
        s.suggest.as_ref().unwrap().items.first(),
        Some(&SuggestItem::Replace("hello".into()))
    );
    s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
    assert!(s.suggest.is_none());
    assert_eq!(s.input.text(), "hello");
}

#[test]
fn add_to_dictionary_clears_the_error() {
    let mut s = ChatScreen::new();
    s.set_spellchecker(mk_checker());
    type_str(&mut s, "helo");
    s.open_suggestions();
    let last = s.suggest.as_ref().unwrap().items.len() - 1;
    // navigate to "add to dictionary" and apply it
    for _ in 0..last {
        s.handle_key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE));
    }
    s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
    assert!(s.suggest.is_none());
    // the word is now in the personal dictionary — no longer an error
    assert!(
        s.spell
            .as_ref()
            .unwrap()
            .misspelled_word_at("helo", 2)
            .is_none()
    );
}

#[test]
fn esc_closes_suggestions_without_quitting() {
    let mut s = ChatScreen::new();
    s.set_spellchecker(mk_checker());
    type_str(&mut s, "helo");
    s.open_suggestions();
    assert!(s.suggest.is_some());
    let intent = s.handle_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
    assert_eq!(intent, None);
    assert!(s.suggest.is_none());
}

#[test]
fn render_with_suggestions_does_not_panic() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    let mut s = ChatScreen::new();
    s.set_spellchecker(mk_checker());
    type_str(&mut s, "helo");
    s.open_suggestions();
    let mut term = Terminal::new(TestBackend::new(50, 16)).unwrap();
    term.draw(|f| s.render(f)).unwrap();
}

#[test]
fn suggest_popup_selection_matches_other_lists() {
    // Selection in the spellcheck popup — like in the chat list, settings, and the "self-
    // model" screen: a soft `keycap_bg` backdrop + a green `▌` rail, not a reversed line.
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    use ratatui::style::Modifier;
    let mut s = ChatScreen::new();
    s.set_spellchecker(mk_checker());
    type_str(&mut s, "helo");
    s.open_suggestions();
    let palette = s.palette;
    let mut term = Terminal::new(TestBackend::new(50, 16)).unwrap();
    term.draw(|f| s.render(f)).unwrap();
    let buf = term.backend().buffer();

    let rail = (0..buf.area.height)
        .flat_map(|y| (0..buf.area.width).map(move |x| (x, y)))
        .find(|&(x, y)| buf[(x, y)].symbol() == "")
        .expect("the popup's selected line must have a `▌` rail");
    let cell = &buf[rail];
    assert_eq!(cell.style().fg, Some(palette.success), "the rail is green");
    assert_eq!(
        cell.style().bg,
        Some(palette.keycap_bg),
        "the selection is a keycap_bg backdrop"
    );
    // The selected line must not be reversed (reverse video would swap fg↔bg per span).
    let (_, row) = rail;
    for x in 0..buf.area.width {
        assert!(
            !buf[(x, row)]
                .style()
                .add_modifier
                .contains(Modifier::REVERSED),
            "the selected line must not be reversed"
        );
    }
}

#[test]
fn ctrl_b_opens_emoji_picker_and_enter_inserts_at_cursor() {
    let mut s = ChatScreen::new();
    type_str(&mut s, "ab");
    // cursor between 'a' and 'b'
    s.handle_key(KeyEvent::new(KeyCode::Left, KeyModifiers::NONE));
    // Ctrl+B opens the popup (also under a Cyrillic layout: Ctrl+и → physical B).
    s.handle_key(KeyEvent::new(KeyCode::Char('и'), KeyModifiers::CONTROL));
    assert!(s.emoji.is_some());
    // Enter inserts the first emoji at the cursor and closes the popup.
    let intent = s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
    assert_eq!(intent, None);
    assert!(s.emoji.is_none());
    assert_eq!(s.input.text(), "a😀b");
}

#[test]
fn emoji_picker_remembers_last_selection() {
    let mut s = ChatScreen::new();
    // Open it, shift the selection, and insert.
    s.handle_key(KeyEvent::new(KeyCode::Char('b'), KeyModifiers::CONTROL));
    s.handle_key(KeyEvent::new(KeyCode::Right, KeyModifiers::NONE));
    s.handle_key(KeyEvent::new(KeyCode::Right, KeyModifiers::NONE));
    s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
    assert!(s.emoji.is_none());
    // Reopening restores the previous selection (index 2).
    s.handle_key(KeyEvent::new(KeyCode::Char('b'), KeyModifiers::CONTROL));
    assert_eq!(s.emoji.as_ref().unwrap().selected(), 2);
}

#[test]
fn esc_closes_emoji_picker_without_quitting() {
    let mut s = ChatScreen::new();
    s.handle_key(KeyEvent::new(KeyCode::Char('b'), KeyModifiers::CONTROL));
    assert!(s.emoji.is_some());
    let intent = s.handle_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
    assert_eq!(intent, None);
    assert!(s.emoji.is_none());
    assert!(s.input.is_empty(), "no emoji is inserted on cancel");
}

#[test]
fn risky_glyph_detector_covers_emoji_classes_but_not_plain_text() {
    use super::feed::is_risky_glyph;
    // Risk classes: VS16, ZWJ, skin tone, supplementary-plane pictographs, width-2 BMP emoji.
    for c in [
        '\u{FE0F}',
        '\u{200D}',
        '\u{1F3FD}',
        '😀',
        '🔥',
        '',
        '',
        '',
    ] {
        assert!(is_risky_glyph(c), "{c:?} must be considered risky");
    }
    // Plain text, punctuation, typography, and CJK — not risky (otherwise a full redraw
    // would run for every chunk of Chinese/Japanese text for no benefit).
    for c in ['a', 'я', ' ', '·', '', '', '', '', '', ''] {
        assert!(!is_risky_glyph(c), "{c:?} must not be considered risky");
    }
}

#[test]
fn feed_content_change_requests_full_redraw_only_with_risky_glyphs() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    // Artifacts on legacy terminals appeared on a content CHANGE (streaming,
    // a note added), and only scrolling used to "fix" them — it was the sole
    // trigger for a full redraw. Now a feed change is a trigger too.
    let draw = |s: &mut ChatScreen, term: &mut Terminal<TestBackend>| {
        term.draw(|f| s.render(f)).unwrap();
    };

    // Plain text: streaming doesn't require a full redraw.
    let mut plain = ChatScreen::new();
    let mut term = Terminal::new(TestBackend::new(60, 16)).unwrap();
    let id = gen_id();
    plain.begin_generation(id, None);
    plain.push_chunk(id, "обычный текст");
    draw(&mut plain, &mut term);
    assert!(
        !plain.take_full_redraw(),
        "with plain text a feed change needs no redraw"
    );

    // Emoji in the feed: streaming requires it.
    let mut emoji = ChatScreen::new();
    let id = gen_id();
    emoji.begin_generation(id, None);
    emoji.push_chunk(id, "смотри: 😀");
    assert!(
        emoji.take_full_redraw(),
        "a feed change with emoji requests a redraw — BEFORE rendering, so the artifact doesn't flash even for one frame"
    );
    assert!(!emoji.take_full_redraw(), "the flag is taken exactly once");

    // Redrawing again with no changes — the request isn't reissued (otherwise the loop
    // would redraw the whole screen forever).
    draw(&mut emoji, &mut term);
    assert!(
        !emoji.take_full_redraw(),
        "with no content change a redraw isn't needed"
    );

    // A note added to the feed (F5 "Conversation copied...") is also a content change.
    emoji.push_note("Переписка скопирована в буфер обмена");
    draw(&mut emoji, &mut term);
    assert!(emoji.take_full_redraw(), "an added note requests a redraw");
}

#[test]
fn every_feed_mutator_marks_content_change() {
    // A gate for the "mutators set the flag" approach: a forgotten `mark_feed_changed` call
    // would bring back a flashing artifact on legacy terminals. The feed contains an
    // emoji from the very start, so ANY change must request a full redraw.
    let mut s = ChatScreen::new();
    let id = gen_id();

    s.activate_chat(
        id,
        "Чат".into(),
        &[Message::assistant("привет 😀")],
        "",
        FeedView::default(),
        None,
        None,
    );
    assert!(s.take_full_redraw(), "activate_chat");

    s.push_user_message("вопрос".into());
    assert!(s.take_full_redraw(), "push_user_message");

    s.begin_generation(id, None);
    assert!(s.take_full_redraw(), "begin_generation");

    s.push_chunk(id, "ответ");
    assert!(s.take_full_redraw(), "push_chunk");

    s.push_thoughts(id, "мысль");
    assert!(s.take_full_redraw(), "push_thoughts");

    s.push_tool_call(
        id,
        "c1".into(),
        "web_search".into(),
        "{}".into(),
        "ок".into(),
        0,
    );
    assert!(s.take_full_redraw(), "push_tool_call");

    s.continue_assistant(id);
    assert!(s.take_full_redraw(), "continue_assistant");

    s.rewrite_assistant(id);
    assert!(s.take_full_redraw(), "rewrite_assistant");

    s.push_note("заметка");
    assert!(s.take_full_redraw(), "push_note");

    s.push_error("ошибка");
    assert!(s.take_full_redraw(), "push_error");

    // Collapsing "thoughts" (`Ctrl+T`) recomputes all blocks — also a change.
    s.handle_key(KeyEvent::new(KeyCode::Char('t'), KeyModifiers::CONTROL));
    assert!(s.take_full_redraw(), "Ctrl+T (collapsing thoughts)");

    // A compaction adds a divider above a block — a content change like any other.
    s.set_compaction(Some((gen_id(), "ранее обсудили X".into())));
    assert!(s.take_full_redraw(), "set_compaction");
}

#[test]
fn suggest_popup_actions_request_full_redraw() {
    // The same class as the emoji popup: the "➕ add to dictionary" item carries a wide
    // glyph. On CLOSING the popup, the diff sends its trailing cell only for a styled
    // line, so we take out insurance via a full redraw; on a selection shift it wouldn't
    // help anyway (the sentinel skips a wide glyph's tail, ratatui#2651).
    let mut s = ChatScreen::new();
    s.set_spellchecker(mk_checker());
    type_str(&mut s, "helo");
    s.open_suggestions();
    assert!(s.suggest.is_some(), "the suggestion popup opened");
    s.take_full_redraw(); // reset the flag left over from typing

    // A selection shift does NOT require a redraw: the glyph stays wide, the sentinel is
    // required to skip its tail (ratatui#2651) — reprinting the glyph clears the highlight.
    s.handle_key(KeyEvent::new(KeyCode::Down, KeyModifiers::NONE));
    assert!(!s.take_full_redraw(), "a selection shift needs no redraw");

    // A key that doesn't change the popup doesn't request a redraw.
    s.handle_key(KeyEvent::new(KeyCode::Left, KeyModifiers::NONE));
    assert!(!s.take_full_redraw(), "a no-op key needs no redraw");

    // Closing via cancel (`Esc`).
    s.handle_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
    assert!(s.suggest.is_none());
    assert!(
        s.take_full_redraw(),
        "cancelling closes the popup → a redraw"
    );

    // Closing by applying a suggestion (`Enter`).
    s.open_suggestions();
    s.take_full_redraw();
    s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
    assert!(s.suggest.is_none());
    assert!(
        s.take_full_redraw(),
        "applying it closes the popup → a redraw"
    );
}

#[test]
fn emoji_picker_actions_request_full_redraw() {
    // A wide emoji glyph leaves a "hanging" trailing half on conhost when it
    // DISAPPEARS from the screen: the diff doesn't send trailing cells of unstyled grid
    // glyphs (a canary pinning the boundary of the upstream fix — in `widgets::emoji_picker`).
    // So a full redraw is requested by closing the popup — but not by a selection
    // shift, where the glyph stays wide and the sentinel skips its tail (ratatui#2651).
    let mut s = ChatScreen::new();
    assert!(
        !s.take_full_redraw(),
        "with no popup, a redraw isn't needed"
    );

    // Opening it by itself doesn't require a redraw — the glyph doesn't leave anywhere.
    s.handle_key(KeyEvent::new(KeyCode::Char('b'), KeyModifiers::CONTROL));
    assert!(!s.take_full_redraw(), "opening the popup needs no redraw");

    // A selection shift does NOT require a redraw: the glyph stays wide, the sentinel is
    // required to skip its tail (ratatui#2651) — reprinting the glyph clears the backdrop.
    s.handle_key(KeyEvent::new(KeyCode::Right, KeyModifiers::NONE));
    assert!(!s.take_full_redraw(), "a selection shift needs no redraw");

    // Closing via insertion (`Enter`).
    s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
    assert!(s.emoji.is_none());
    assert!(
        s.take_full_redraw(),
        "inserting closes the popup → a redraw"
    );
    assert!(!s.take_full_redraw(), "the flag is taken exactly once");

    // Closing via cancel (`Esc`).
    s.handle_key(KeyEvent::new(KeyCode::Char('b'), KeyModifiers::CONTROL));
    s.take_full_redraw();
    s.handle_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));
    assert!(s.emoji.is_none());
    assert!(
        s.take_full_redraw(),
        "cancelling closes the popup → a redraw"
    );
}

#[test]
fn render_with_emoji_picker_does_not_panic() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    let mut s = ChatScreen::new();
    s.handle_key(KeyEvent::new(KeyCode::Char('b'), KeyModifiers::CONTROL));
    let mut term = Terminal::new(TestBackend::new(50, 16)).unwrap();
    term.draw(|f| s.render(f)).unwrap();
}

#[test]
fn rag_command_intercepted_on_enter() {
    let mut s = ChatScreen::new();
    s.set_server_status(ready_statuses());
    type_str(&mut s, "/rag add d:\\docs -r");
    let intent = s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
    assert_eq!(
        intent,
        Some(ChatIntent::RagAdd {
            path: "d:\\docs".into(),
            recursive: true,
        })
    );
    assert!(s.input.is_empty(), "the field is cleared after the command");
}

#[test]
fn invalid_rag_command_shows_note_and_does_not_send() {
    let mut s = ChatScreen::new();
    s.set_server_status(ready_statuses());
    type_str(&mut s, "/rag");
    let intent = s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
    assert_eq!(intent, None, "an invalid command isn't sent");
    assert!(s.feed.iter().any(|m| m.role == FeedRole::Note));
}

#[test]
fn file_command_intercepted_on_enter() {
    let mut s = ChatScreen::new();
    s.set_server_status(ready_statuses());
    // A path with spaces (quoted) — the whole thing is one argument.
    type_str(&mut s, "/file attach \"d:\\my docs\\notes.md\"");
    let intent = s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
    assert_eq!(
        intent,
        Some(ChatIntent::FileAttach {
            path: "d:\\my docs\\notes.md".into(),
        })
    );
    assert!(s.input.is_empty(), "the field is cleared after the command");

    type_str(&mut s, "/file remove #2");
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)),
        Some(ChatIntent::FileRemove {
            target: "#2".into()
        })
    );
    type_str(&mut s, "/file list");
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)),
        Some(ChatIntent::FileList)
    );
}

#[test]
fn invalid_file_command_shows_note_and_does_not_send() {
    let mut s = ChatScreen::new();
    s.set_server_status(ready_statuses());
    type_str(&mut s, "/file attach");
    let intent = s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
    assert_eq!(intent, None, "an invalid command isn't sent as a message");
    // Errors go into the feed as a service note (there is no separate error role).
    assert!(s.feed.iter().any(|m| m.role == FeedRole::Note));
}

#[test]
fn attachment_chip_shows_count_and_standing_cost() {
    use crate::entities::attachment::{AttachMode, AttachmentInfo};
    let mut s = ChatScreen::new();
    assert_eq!(s.attachments_hint(), None, "no chip without attachments");

    s.set_attachments(vec![
        AttachmentInfo {
            name: "a.md".into(),
            source: "/tmp/a.md".into(),
            bytes: 4096,
            est_tokens: 1200,
            prompt_tokens: 1200,
            mode: AttachMode::Inline,
            has_original: false,
        },
        // A by-reference file contributes only its excerpt, so it must not be
        // counted at full weight in the standing cost.
        AttachmentInfo {
            name: "big.log".into(),
            source: "/tmp/big.log".into(),
            bytes: 900_000,
            est_tokens: 200_000,
            // Only the excerpt is actually re-sent every turn.
            prompt_tokens: 300,
            mode: AttachMode::ByReference,
            has_original: false,
        },
    ]);
    let hint = s.attachments_hint().expect("a chip with attachments");
    assert!(hint.contains('2'), "the count of files: {hint}");
    // The real per-request cost: the inline file in full (1200) plus the
    // by-reference excerpt (300) — not its whole 200k, and not zero either.
    assert!(hint.contains("1.5k"), "the standing cost: {hint}");
    assert!(
        !hint.contains("200k"),
        "a by-reference file must not be counted at full weight: {hint}"
    );
}

#[test]
fn file_list_note_numbers_items_for_removal() {
    use crate::entities::attachment::{AttachMode, AttachmentInfo};
    use crate::features::file_command::FileProgress;
    let mut s = ChatScreen::new();
    s.set_file_progress(FileProgress::Listed {
        stored: Vec::new(),
        images: Vec::new(),
        dir: String::new(),
        items: vec![AttachmentInfo {
            name: "notes.md".into(),
            source: "/tmp/notes.md".into(),
            bytes: 2048,
            est_tokens: 400,
            prompt_tokens: 400,
            mode: AttachMode::Inline,
            has_original: false,
        }],
    });
    let note = s
        .feed
        .iter()
        .find(|m| m.role == FeedRole::Note)
        .expect("a note in the feed");
    // The `#N` handle is what `/file remove #N` accepts.
    assert!(note.text.contains("#1"), "{}", note.text);
    assert!(note.text.contains("notes.md"), "{}", note.text);
}

/// The two notes `/file open` leaves (fork F9, §13 U3/U6): the file opened, or its
/// folder standing in for a type no handler may run — and the path in both, which is
/// what makes a launch that failed on the way recoverable. And when the folder stood in,
/// the reason given depends on whose file it was: the assistant's writing is a reason only
/// for a file the assistant wrote.
#[test]
fn the_open_notes_name_the_path_and_say_when_the_folder_stood_in() {
    use crate::features::file_command::{FileProgress, OpenedInstead};
    let mut s = ChatScreen::new();
    s.set_file_progress(FileProgress::Opened {
        name: "chart.png".into(),
        path: "/data/files/c1/chart.png".into(),
    });
    s.set_file_progress(FileProgress::OpenedFolder {
        path: "/data/files/c1".into(),
        instead_of: Some(OpenedInstead {
            name: "run.bat".into(),
            by_a_call: true,
        }),
    });
    s.set_file_progress(FileProgress::OpenedFolder {
        path: "/data/files/c1".into(),
        instead_of: None,
    });
    s.set_file_progress(FileProgress::OpenedFolder {
        path: "/home/me".into(),
        instead_of: Some(OpenedInstead {
            name: "script.py".into(),
            by_a_call: false,
        }),
    });
    let notes: Vec<&str> = s
        .feed
        .iter()
        .filter(|m| m.role == FeedRole::Note)
        .map(|m| m.text.as_str())
        .collect();
    assert_eq!(notes.len(), 4, "{notes:?}");
    assert!(
        notes[0].contains("chart.png") && notes[0].contains("/data/files/c1/chart.png"),
        "{}",
        notes[0]
    );
    // The fallback has to be readable as a fallback, not as "the file opened".
    assert!(
        notes[1].contains("run.bat") && notes[1].contains("/data/files/c1"),
        "{}",
        notes[1]
    );
    assert!(!notes[2].contains("run.bat"), "{}", notes[2]);
    assert!(notes[2].contains("/data/files/c1"), "{}", notes[2]);
    // Whose file it was picks the reason. Compared whole, through the bundle, so the test
    // holds in any interface language and cannot pass on a shared prefix.
    let loc = crate::shared::i18n::locale(crate::shared::i18n::Lang::default());
    let args = |name, path| [("name", name), ("path", path)];
    assert_eq!(
        notes[1],
        loc.tf(
            "ui.file.opened_folder_instead",
            &args("run.bat", "/data/files/c1")
        )
    );
    assert_eq!(
        notes[3],
        loc.tf(
            "ui.file.opened_folder_instead_own",
            &args("script.py", "/home/me")
        )
    );
}

/// A staged-image card for the tests — `est_tokens` is what the chip and the
/// listing actually add up, so it is set explicitly rather than derived.
fn image_info(
    name: &str,
    bytes: usize,
    est_tokens: usize,
) -> crate::entities::message_image::ImageInfo {
    crate::entities::message_image::ImageInfo {
        name: name.into(),
        source: format!("D:\\pics\\{name}"),
        mime: "image/png".into(),
        width: 800,
        height: 600,
        bytes,
        est_tokens,
    }
}

/// docs/research/remove-by-shared-name.md F2a: the two lines of a shared name carry the
/// source `/file remove` accepts, a unique name's line is as it was, and a removal note
/// names the source only when it was given one.
#[test]
fn a_shared_file_name_is_told_apart_by_its_source() {
    use crate::entities::attachment::{AttachMode, AttachmentInfo};
    use crate::features::file_command::FileProgress;
    let note = |progress: FileProgress| {
        let mut s = ChatScreen::new();
        s.set_file_progress(progress);
        let note = s.feed.iter().find(|m| m.role == FeedRole::Note);
        note.expect("a note in the feed").text.clone()
    };
    let info = |name: &str, source: &str| AttachmentInfo {
        name: name.into(),
        source: source.into(),
        bytes: 13,
        est_tokens: 4,
        prompt_tokens: 4,
        mode: AttachMode::Inline,
        has_original: false,
    };
    let list = note(FileProgress::Listed {
        stored: Vec::new(),
        images: Vec::new(),
        dir: String::new(),
        items: vec![
            info("notes.md", "D:\\a\\notes.md"),
            info("Notes.md", "D:\\b\\notes.md"),
            info("todo.txt", "D:\\a\\todo.txt"),
        ],
    });
    assert!(
        list.contains("D:\\a\\notes.md") && list.contains("D:\\b\\notes.md"),
        "{list}"
    );
    assert!(!list.contains("D:\\a\\todo.txt"), "{list}");

    let removed = note(FileProgress::Removed {
        name: "notes.md".into(),
        source: Some("D:\\b\\notes.md".into()),
    });
    assert!(removed.contains("D:\\b\\notes.md"), "{removed}");
    let removed = note(FileProgress::Removed {
        name: "todo.txt".into(),
        source: None,
    });
    assert!(!removed.contains("D:\\"), "{removed}");
}

#[test]
fn a_shared_image_name_is_told_apart_by_its_source() {
    use crate::features::image_command::ImageProgress;
    let note = |progress: ImageProgress| {
        let mut s = ChatScreen::new();
        s.set_image_progress(progress);
        let note = s.feed.iter().find(|m| m.role == FeedRole::Note);
        note.expect("a note in the feed").text.clone()
    };
    let mut first = image_info("chart.png", 120_000, 1200);
    first.source = "D:\\a\\chart.png".into();
    let mut second = image_info("chart.png", 4096, 400);
    second.source = "D:\\b\\chart.png".into();
    let list = note(ImageProgress::Listed {
        items: vec![first, second, image_info("photo.jpg", 4096, 400)],
    });
    assert!(
        list.contains("D:\\a\\chart.png") && list.contains("D:\\b\\chart.png"),
        "{list}"
    );
    assert!(!list.contains("D:\\pics\\photo.jpg"), "{list}");

    let removed = note(ImageProgress::Removed {
        name: "chart.png".into(),
        source: Some("D:\\b\\chart.png".into()),
    });
    assert!(removed.contains("D:\\b\\chart.png"), "{removed}");
    let removed = note(ImageProgress::Removed {
        name: "photo.jpg".into(),
        source: None,
    });
    assert!(!removed.contains("D:\\"), "{removed}");
}

#[test]
fn image_command_intercepted_on_enter() {
    let mut s = ChatScreen::new();
    s.set_server_status(ready_statuses());
    // A path with spaces (quoted) — the whole thing is one argument.
    type_str(&mut s, "/image attach \"d:\\my pics\\chart 1.png\"");
    let intent = s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
    assert_eq!(
        intent,
        Some(ChatIntent::ImageAttach {
            path: "d:\\my pics\\chart 1.png".into(),
        })
    );
    assert!(s.input.is_empty(), "the field is cleared after the command");
    assert!(
        !s.feed.iter().any(|m| m.role == FeedRole::User),
        "a command must not go out as a message"
    );

    type_str(&mut s, "/image remove #2");
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)),
        Some(ChatIntent::ImageRemove {
            target: "#2".into()
        })
    );
    type_str(&mut s, "/image list");
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)),
        Some(ChatIntent::ImageList)
    );
    assert!(!s.feed.iter().any(|m| m.role == FeedRole::User));
}

/// The two ways to paste an image differ in exactly one thing, and it is the thing that
/// matters: `Ctrl+V` must still paste **text** when the clipboard holds no image (that is
/// what the help overlay has always promised the key does), while a typed `/image paste`
/// must not silently turn into a text paste — the user asked for an image.
#[test]
fn both_paste_routes_produce_an_intent_and_differ_only_in_the_text_fallback() {
    let mut s = ChatScreen::new();
    s.set_server_status(ready_statuses());

    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('v'), KeyModifiers::CONTROL)),
        Some(ChatIntent::PasteImage {
            text_fallback: true
        }),
        "Ctrl+V keeps working as a text paste when there is no image"
    );

    type_str(&mut s, "/image paste");
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)),
        Some(ChatIntent::PasteImage {
            text_fallback: false
        })
    );
    assert!(s.input.is_empty(), "the field is cleared after the command");
    assert!(
        !s.feed.iter().any(|m| m.role == FeedRole::User),
        "a command must not go out as a message"
    );
}

/// `/image paste` is a command like the others, so the input box must highlight it and
/// skip spellcheck on it — otherwise it reads as a misspelled sentence while being typed.
#[test]
fn image_paste_is_recognized_as_a_command_while_typing() {
    let mut s = ChatScreen::new();
    s.set_server_status(ready_statuses());
    type_str(&mut s, "/image paste");
    assert!(s.input_is_command());
}

#[test]
fn invalid_image_command_shows_note_and_does_not_send() {
    let mut s = ChatScreen::new();
    s.set_server_status(ready_statuses());
    type_str(&mut s, "/image attach");
    let intent = s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
    assert_eq!(intent, None, "an invalid command isn't sent as a message");
    // Errors go into the feed as a service note (there is no separate error role).
    assert!(s.feed.iter().any(|m| m.role == FeedRole::Note));
    assert!(!s.feed.iter().any(|m| m.role == FeedRole::User));
}

#[test]
fn image_input_is_recognized_as_command() {
    let mut s = ChatScreen::new();
    type_str(&mut s, "/image attach a.png");
    assert!(
        s.input_is_command(),
        "the input box highlights it and skips spellcheck"
    );
    // A malformed one is recognized too — it's still a command, not prose.
    s.input.clear();
    type_str(&mut s, "/image");
    assert!(s.input_is_command());
    // Prose that merely mentions the word isn't.
    s.input.clear();
    type_str(&mut s, "look at the image below");
    assert!(!s.input_is_command());
}

#[test]
fn staged_image_chip_shows_count_and_standing_cost() {
    let mut s = ChatScreen::new();
    assert_eq!(
        s.staged_images_hint(),
        None,
        "no chip without staged images"
    );

    s.set_staged_images(vec![
        image_info("chart.png", 120_000, 1200),
        image_info("photo.jpg", 300_000, 300),
    ]);
    let hint = s.staged_images_hint().expect("a chip with staged images");
    assert!(hint.contains('2'), "the count of images: {hint}");
    // The cost of the next turn: both images together (1200 + 300).
    assert!(hint.contains("1.5k"), "the cost of the next send: {hint}");
    // Sending clears the staging — the chip goes with it (the orchestrator
    // reports an empty set), unlike the attachments chip which stays.
    s.set_staged_images(Vec::new());
    assert_eq!(s.staged_images_hint(), None, "the chip goes with the send");
}

#[test]
fn image_list_note_numbers_items_for_removal() {
    use crate::features::image_command::ImageProgress;
    let mut s = ChatScreen::new();
    s.set_image_progress(ImageProgress::Listed {
        items: vec![
            image_info("chart.png", 120_000, 1200),
            image_info("photo.jpg", 4096, 400),
        ],
    });
    let note = s
        .feed
        .iter()
        .find(|m| m.role == FeedRole::Note)
        .expect("a note in the feed");
    // The `#N` handles are what `/image remove #N` accepts, in listing order.
    assert!(note.text.contains("#1 chart.png"), "{}", note.text);
    assert!(note.text.contains("#2 photo.jpg"), "{}", note.text);
    let first = note.text.find("#1").unwrap();
    let second = note.text.find("#2").unwrap();
    assert!(
        first < second,
        "numbering follows the listing: {}",
        note.text
    );
}

#[test]
fn empty_image_list_says_sent_images_are_not_staged() {
    use crate::features::image_command::ImageProgress;
    let mut s = ChatScreen::new();
    s.set_image_progress(ImageProgress::Listed { items: Vec::new() });
    let note = s
        .feed
        .iter()
        .find(|m| m.role == FeedRole::Note)
        .expect("a note in the feed");
    // The empty listing is not "you have no images" — it has to distinguish
    // staged-for-the-next-message from already sent, or `/image remove` looks
    // broken to whoever wants an image out of the conversation.
    assert_eq!(note.text, s.loc.t("ui.image.list_empty"), "{}", note.text);
}

#[test]
fn rag_progress_banner_lifecycle() {
    let mut s = ChatScreen::new();
    assert!(!s.is_rag_active());
    s.set_rag_progress(RagProgress::Started { total: 3 });
    assert!(s.is_rag_active());
    // The file has just started (chunks_total=0) — no chunk suffix in the banner.
    s.set_rag_progress(RagProgress::Indexing {
        index: 1,
        total: 3,
        name: "a.txt".into(),
        dir: "d:\\docs".into(),
        chunks_done: 0,
        chunks_total: 0,
    });
    assert!(s.is_rag_active());
    assert!(
        !s.rag.as_ref().unwrap().text().contains("чанки"),
        "with no chunks_total there must be no chunk suffix: {:?}",
        s.rag.as_ref().unwrap().text()
    );
    // Chunk progress (chunks_total>0) — the banner carries the chunk counter.
    s.set_rag_progress(RagProgress::Indexing {
        index: 1,
        total: 3,
        name: "a.txt".into(),
        dir: "d:\\docs".into(),
        chunks_done: 16,
        chunks_total: 42,
    });
    let banner = s.rag.as_ref().unwrap().text();
    assert!(
        banner.contains("16") && banner.contains("42"),
        "the banner must show chunks 16/42: {banner:?}"
    );
    s.set_rag_progress(RagProgress::Finished {
        files: 3,
        chunks: 9,
        errors: 0,
        cancelled: false,
    });
    assert!(!s.is_rag_active(), "the banner clears on completion");
    assert!(
        s.feed
            .iter()
            .any(|m| m.role == FeedRole::Note && m.text.contains("завершена"))
    );
}

#[test]
fn rag_banner_row_keeps_the_counters_on_a_narrow_screen() {
    use crate::features::file_command::FileProgress;
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    // The screenshot case: an attachment whose name is a web page's title,
    // indexed on a screen narrower than the banner's text. The widget used to
    // clip the row at the edge, and the counters — the only part that moves —
    // were the part that fell off.
    let mut s = ChatScreen::new();
    s.set_file_progress(FileProgress::Indexing {
        name: "GitHub - openai/gpt-oss: gpt-oss-120b and gpt-oss-20b are two open-weight models"
            .into(),
        done: 64,
        total: 128,
    });
    let mut term = Terminal::new(TestBackend::new(90, 16)).unwrap();
    term.draw(|f| s.render(f)).unwrap();
    let buf = term.backend().buffer().clone();
    let rows: Vec<String> = (0..buf.area.height)
        .map(|y| {
            (0..buf.area.width)
                .map(|x| buf[(x, y)].symbol())
                .collect::<String>()
        })
        .collect();
    let banner = rows
        .iter()
        .find(|r| r.contains("RAG:"))
        .unwrap_or_else(|| panic!("the banner row: {rows:#?}"));
    assert!(
        banner.trim_end().ends_with("64/128"),
        "the counters end the row: {banner:?}"
    );
    assert!(
        banner.contains('') && banner.contains("GitHub"),
        "the name gave way, from the middle: {banner:?}"
    );
}

#[test]
fn command_input_is_not_spellchecked() {
    let mut s = ChatScreen::new();
    s.set_spellchecker(mk_checker());
    // Plain text with an error → checked (underlines are present).
    type_str(&mut s, "helo");
    s.last_edit = None; // lift the debounce so the recheck runs immediately
    assert!(s.maybe_recheck_spelling());
    assert!(!s.input.misspelled_is_empty(), "plain text is spellchecked");
    // Turn the line into a command — spellcheck is lifted.
    s.input.clear();
    type_str(&mut s, "/rag add helo");
    s.last_edit = None;
    assert!(s.input_is_command());
    assert!(s.maybe_recheck_spelling());
    assert!(
        s.input.misspelled_is_empty(),
        "a command isn't spellchecked"
    );
}

/// The reported symptom, end to end (docs/research/spellcheck-stress-marks.md):
/// a stressed line typed into the chat input draws no underline. Read against
/// the **shipped** `ru_RU` rather than a fixture — the point is what the user
/// sees, and the debounce is lifted so the recheck runs on the spot.
#[test]
fn a_stressed_russian_line_is_not_underlined() {
    let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("dictionaries");
    let checker = crate::features::spellcheck::dict::load(
        &dir,
        None,
        &dir.join("does-not-exist.txt"),
        true,
        &["ru_RU".to_string()],
    );
    let mut s = ChatScreen::new();
    s.set_spellchecker(checker);
    type_str(&mut s, "И\u{301}стинно так");
    s.last_edit = None;
    assert!(s.maybe_recheck_spelling());
    assert!(
        s.input.misspelled_is_empty(),
        "a stressed word is not an error"
    );
    // …and the check is still on: a typo under a stress is still a typo.
    s.input.clear();
    type_str(&mut s, "харашо\u{301}");
    s.last_edit = None;
    assert!(s.maybe_recheck_spelling());
    assert!(!s.input.misspelled_is_empty(), "the typo is still flagged");
}

#[test]
fn rag_remove_command_intercepted_on_enter() {
    let mut s = ChatScreen::new();
    type_str(&mut s, "/rag remove d:\\dir\\file.txt");
    let intent = s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE));
    assert_eq!(
        intent,
        Some(ChatIntent::RagDelete {
            path: "d:\\dir\\file.txt".into(),
        })
    );
    assert!(s.input.is_empty());
}

#[test]
fn rag_removed_progress_pushes_note() {
    let mut s = ChatScreen::new();
    s.set_rag_progress(RagProgress::Removed { chunks: 5 });
    assert!(
        s.feed
            .iter()
            .any(|m| m.role == FeedRole::Note && m.text.contains("удалено фрагментов: 5"))
    );
    // Zero — a clear "nothing found" note.
    s.set_rag_progress(RagProgress::Removed { chunks: 0 });
    assert!(
        s.feed
            .iter()
            .any(|m| m.role == FeedRole::Note && m.text.contains("ничего не найдено"))
    );
}

#[test]
fn rag_list_and_rebuild_commands_intercepted_on_enter() {
    let mut s = ChatScreen::new();
    type_str(&mut s, "/rag list");
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)),
        Some(ChatIntent::RagList)
    );
    assert!(s.input.is_empty());

    type_str(&mut s, "/rag rebuild");
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)),
        Some(ChatIntent::RagRebuild)
    );
}

#[test]
fn rag_listed_progress_pushes_note() {
    use crate::entities::rag::RagSourceInfo;
    let mut s = ChatScreen::new();
    // An empty knowledge base — a clear note.
    s.set_rag_progress(RagProgress::Listed { sources: vec![] });
    assert!(
        s.feed
            .iter()
            .any(|m| m.role == FeedRole::Note && m.text.contains("база знаний пуста"))
    );
    // With sources — a chunk counter and the source name.
    s.set_rag_progress(RagProgress::Listed {
        sources: vec![RagSourceInfo {
            source: "spec.md".into(),
            chunks: 42,
            created_at: chrono::Utc::now(),
        }],
    });
    assert!(
        s.feed.iter().any(|m| m.role == FeedRole::Note
            && m.text.contains("spec.md")
            && m.text.contains("42"))
    );
}

#[test]
fn rag_failure_pushes_error_note() {
    let mut s = ChatScreen::new();
    s.set_rag_progress(RagProgress::Failed("эмбеддер недоступен".into()));
    assert!(!s.is_rag_active());
    assert!(
        s.feed
            .iter()
            .any(|m| m.role == FeedRole::Note && m.text.contains("эмбеддер недоступен"))
    );
}

#[test]
fn render_does_not_panic() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    let mut s = ChatScreen::new();
    s.push_user_message("привет".into());
    let id = gen_id();
    s.begin_generation(id, None);
    s.push_chunk(id, "# Ответ\n\nтекст");
    let mut term = Terminal::new(TestBackend::new(50, 16)).unwrap();
    term.draw(|f| s.render(f)).unwrap();
}

/// In-feed search must never touch the message being written — that is the whole
/// reason it lives in its own field (docs/history/in-feed-search.md §3, fork F3).
#[test]
fn ctrl_f_opens_feed_search_and_esc_closes_it_leaving_the_message_alone() {
    let mut s = ChatScreen::new();
    type_str(&mut s, "недописанное сообщение");

    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Char('f'), KeyModifiers::CONTROL)),
        None
    );
    assert!(s.search.is_some(), "Ctrl+F opens the search field");
    type_str(&mut s, "маркер");
    assert_eq!(
        s.input.text(),
        "недописанное сообщение",
        "typing a query must not reach the message box"
    );

    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE)),
        None
    );
    assert!(s.search.is_none(), "Esc closes it");
    assert_eq!(
        s.input.text(),
        "недописанное сообщение",
        "and leaves the message"
    );
}

/// Layout-independent, like every other Ctrl shortcut: physical F is `Ctrl+а`
/// on a Russian layout.
#[test]
fn ctrl_f_opens_feed_search_under_a_cyrillic_layout() {
    let mut s = ChatScreen::new();
    s.handle_key(KeyEvent::new(KeyCode::Char('а'), KeyModifiers::CONTROL));
    assert!(s.search.is_some());
}

/// **The trap that slow manual testing cannot see** (§1.5): a run of characters
/// arrives as one coalesced paste, and without its own target it would land in
/// the message the user was writing.
#[test]
fn fast_typing_reaches_the_search_field_not_the_message() {
    let mut s = ChatScreen::new();
    type_str(&mut s, "черновик");
    s.handle_key(KeyEvent::new(KeyCode::Char('f'), KeyModifiers::CONTROL));

    s.handle_paste("быстрый набор");
    assert_eq!(
        s.search.as_ref().map(|f| f.text()),
        Some("быстрый набор".to_string()),
        "a coalesced paste belongs to the search field while it is open"
    );
    assert_eq!(s.input.text(), "черновик", "and must not touch the message");
}

/// `Ctrl+E`/`Ctrl+R`/a rewrite round/a cross-chat jump all funnel through
/// `activate_chat`, which renumbers the feed — so a search left open would point
/// at messages that moved (§1.5).
#[test]
fn activating_a_chat_closes_the_search() {
    let mut s = ChatScreen::new();
    s.handle_key(KeyEvent::new(KeyCode::Char('f'), KeyModifiers::CONTROL));
    type_str(&mut s, "маркер");
    assert!(s.search.is_some());

    s.activate_chat(
        gen_id(),
        "Чат".into(),
        &[],
        "",
        FeedView::default(),
        None,
        None,
    );
    assert!(
        s.search.is_none(),
        "the search must not survive a feed rebuild"
    );
}

/// Reopening resumes the query, so a second `Ctrl+F` continues rather than
/// starting over (fork F5) — but only within the same chat.
#[test]
fn reopening_the_search_resumes_the_query() {
    let mut s = ChatScreen::new();
    s.handle_key(KeyEvent::new(KeyCode::Char('f'), KeyModifiers::CONTROL));
    type_str(&mut s, "маркер");
    s.handle_key(KeyEvent::new(KeyCode::Esc, KeyModifiers::NONE));

    s.handle_key(KeyEvent::new(KeyCode::Char('f'), KeyModifiers::CONTROL));
    assert_eq!(
        s.search.as_ref().map(|f| f.text()),
        Some("маркер".to_string())
    );
}

// ---------- history compaction (spec §6.7) ----------

/// The divider's localized label — how the boundary is found in a rendered feed.
fn compacted_label() -> &'static str {
    crate::shared::i18n::locale(crate::shared::i18n::Lang::default()).t("ui.feed.compacted")
}

/// Renders the screen and returns its rows as strings.
fn screen_rows(s: &mut ChatScreen, w: u16, h: u16) -> Vec<String> {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    let mut term = Terminal::new(TestBackend::new(w, h)).unwrap();
    term.draw(|f| s.render(f)).unwrap();
    let buf = term.backend().buffer().clone();
    let area = buf.area;
    (area.top()..area.bottom())
        .map(|y| {
            (area.left()..area.right())
                .map(|x| buf[(x, y)].symbol())
                .collect()
        })
        .collect()
}

/// Activation carries the chat's boundary into the feed: the compaction is a
/// per-chat rendering input, so switching chats has to bring it along or a
/// summary would linger from the previous one.
#[test]
fn activation_carries_the_compaction_boundary() {
    let messages: Vec<Message> = (0..4)
        .map(|i| Message::user(format!("реплика-{i}")))
        .collect();
    let boundary = messages[2].id;
    let mut s = ChatScreen::new();
    s.activate_chat(
        gen_id(),
        "Чат".into(),
        &messages,
        "",
        FeedView::default(),
        None,
        Some((boundary, "ранее обсудили X".into())),
    );
    let rows = screen_rows(&mut s, 60, 24);
    assert!(
        rows.iter().any(|r| r.contains(compacted_label())),
        "the divider must be drawn for the activated chat: {rows:?}"
    );

    // Switching to a chat with nothing folded takes it away again.
    s.activate_chat(
        gen_id(),
        "Другой".into(),
        &messages,
        "",
        FeedView::default(),
        None,
        None,
    );
    let rows = screen_rows(&mut s, 60, 24);
    assert!(
        !rows.iter().any(|r| r.contains(compacted_label())),
        "the previous chat's boundary must not linger: {rows:?}"
    );
}

/// A live compaction (`AppEvent::Compacted`) reaches the feed without a
/// reactivation — the boundary appears on the chat the user is looking at.
#[test]
fn a_live_compaction_reaches_the_feed() {
    let messages: Vec<Message> = (0..4)
        .map(|i| Message::user(format!("реплика-{i}")))
        .collect();
    let boundary = messages[2].id;
    let mut s = ChatScreen::new();
    s.activate_chat(
        gen_id(),
        "Чат".into(),
        &messages,
        "",
        FeedView::default(),
        None,
        None,
    );
    assert!(
        !screen_rows(&mut s, 60, 24)
            .iter()
            .any(|r| r.contains(compacted_label()))
    );

    s.set_compaction(Some((boundary, "ранее обсудили X".into())));
    let rows = screen_rows(&mut s, 60, 24);
    assert!(
        rows.iter().any(|r| r.contains(compacted_label())),
        "the divider must appear without reactivating the chat: {rows:?}"
    );
}

/// The quiet background indicator joins the same `·`-separated list the other
/// background tasks use — they can run at the same time, so it must compose
/// rather than replace.
#[test]
fn compaction_shows_a_quiet_background_indicator() {
    let mut s = ChatScreen::new();
    assert_eq!(s.background_hint(), None);

    s.set_compacting(true);
    let hint = s.background_hint().expect("the indicator must be shown");
    assert_eq!(hint, s.loc().t("ui.chat.bg.compact"));

    // Composes with a concurrent task rather than replacing it.
    s.set_reflecting(true);
    let hint = s.background_hint().unwrap();
    assert!(hint.contains(s.loc().t("ui.chat.bg.reflect")), "{hint}");
    assert!(hint.contains(s.loc().t("ui.chat.bg.compact")), "{hint}");
    assert!(hint.contains(" · "), "{hint}");

    s.set_compacting(false);
    let hint = s.background_hint().unwrap();
    assert!(!hint.contains(s.loc().t("ui.chat.bg.compact")), "{hint}");
}

/// The retry chip (spec §6.8): it carries the numbers, it composes with the
/// background tasks, and — the part that matters — it cannot outlive the wait it
/// describes.
#[test]
fn the_retry_chip_shows_the_numbers_and_is_cleared_by_what_ends_the_wait() {
    let mut s = ChatScreen::new();
    let gen_id = Uuid::new_v4();
    s.begin_generation(gen_id, None);
    assert_eq!(s.background_hint(), None);

    s.set_retrying(gen_id, 2, 3, 4);
    let hint = s.background_hint().expect("the chip must be shown");
    for part in ["2", "3", "4"] {
        assert!(
            hint.contains(part),
            "the numbers must reach the chip: {hint}"
        );
    }

    // Composes with a concurrent background task rather than replacing it.
    s.set_reflecting(true);
    let hint = s.background_hint().unwrap();
    assert!(hint.contains(s.loc().t("ui.chat.bg.reflect")), "{hint}");
    assert!(hint.contains(" · "), "{hint}");

    // Content ends the wait, so the chip goes.
    s.push_chunk(gen_id, "answer");
    let hint = s.background_hint().unwrap();
    assert!(
        !hint.contains('4'),
        "content must clear the chip, leaving only the background task: {hint}"
    );

    // And so does the turn finishing, from the other direction.
    s.set_retrying(gen_id, 3, 3, 2);
    assert!(s.background_hint().unwrap().contains('3'));
    s.finish_generation(gen_id, FinishReason::Error, false);
    let hint = s.background_hint().unwrap();
    assert!(
        !hint.contains('3'),
        "a finished turn must not leave a chip behind: {hint}"
    );
}

/// The running card (spec §11.3, docs/subagent-live.md §3.7): a started call
/// is a card at once, saying *running…* where the result goes; the result
/// completes that card in place rather than adding a second; a result with
/// no started card still gets one; and a turn that ends with a card still
/// running clears the mark.
#[test]
fn a_started_call_is_a_running_card_completed_in_place() {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    let mut s = ChatScreen::new();
    let id = gen_id();
    s.begin_generation(id, None);
    s.push_chunk(id, "Делегирую.");
    s.push_tool_call_started(
        id,
        "c1".into(),
        "call_subagent".into(),
        "{\"name\":\"Критик\",\"message\":\"оцени\"}".into(),
    );
    let card = &s.feed.last().unwrap().tools;
    assert_eq!(card.len(), 1);
    assert!(card[0].running && card[0].result.is_empty());
    let mut term = Terminal::new(TestBackend::new(80, 20)).unwrap();
    term.draw(|f| s.render(f)).unwrap();
    let dumped = format!("{:?}", term.backend().buffer());
    assert!(
        dumped.contains(s.loc().t("ui.feed.tool_running")),
        "{dumped}"
    );

    s.push_tool_call(
        id,
        "c1".into(),
        "call_subagent".into(),
        "{}".into(),
        "слабо".into(),
        0,
    );
    let card = &s.feed.last().unwrap().tools;
    assert_eq!(card.len(), 1, "completed in place, not added");
    assert!(!card[0].running);
    assert_eq!(card[0].result, "слабо");

    // No started card for this id — a plain push, as before.
    s.push_tool_call(
        id,
        "c2".into(),
        "web_search".into(),
        "{}".into(),
        "ок".into(),
        0,
    );
    assert_eq!(s.feed.last().unwrap().tools.len(), 2);

    // The turn ends mid-call: nothing stays running.
    s.push_tool_call_started(id, "c3".into(), "python_exec".into(), "{}".into());
    assert!(s.feed.last().unwrap().tools[2].running);
    s.finish_generation(id, FinishReason::Cancelled, false);
    let bubble = |s: &ChatScreen| {
        s.feed
            .iter()
            .rev()
            .find(|m| m.role == FeedRole::Assistant)
            .unwrap()
            .tools
            .clone()
    };
    assert!(bubble(&s).iter().all(|t| !t.running));

    // A stale generation opens nothing.
    s.push_tool_call_started(gen_id(), "c9".into(), "web_search".into(), "{}".into());
    assert_eq!(bubble(&s).len(), 3);
}

/// The running transcript on screen (docs/subagent-live.md §3.4): a filed
/// round rebuilds the feed stitched as a landed transcript would be, only for
/// the open transcript; the chip survives on the transcript through
/// `live_turn` while the parent's chunks do not land in it; and a finish of
/// that turn clears the chip without touching the feed.
#[test]
fn a_running_transcript_grows_by_rounds_and_keeps_the_chip_but_not_the_stream() {
    use crate::app::events::{ChildView, SubagentProgress};
    let mut s = ChatScreen::new();
    let run_id = Uuid::new_v4();
    let generation = Uuid::new_v4();
    let stream = Uuid::new_v4();
    s.set_child_view(Some(ChildView {
        parent: Uuid::new_v4(),
        parent_title: "Родитель".into(),
        system_message: "persona".into(),
    }));
    s.activate_chat(
        run_id,
        "Критик".into(),
        &[Message::user("задание")],
        "",
        FeedView::default(),
        None,
        None,
    );
    s.set_live_turn(Some(Box::new(crate::app::events::LiveTurn {
        role: MessageRole::Assistant,
        turn: generation,
        stream,
        partial: Some(crate::app::events::LivePartial {
            text: "начало".into(),
            thoughts: String::new(),
            tools: Vec::new(),
        }),
        continues: false,
        background: false,
    })));
    assert!(s.generating, "a transcript streams its own run");
    assert!(
        s.feed.last().unwrap().streaming && s.feed.last().unwrap().text == "начало",
        "seeded with the round so far"
    );

    s.set_subagent_progress(
        generation,
        Uuid::new_v4(),
        Some(SubagentProgress {
            kind: crate::app::events::RunProgressKind::Subagent,
            name: "Критик".into(),
            round: 2,
            tool: None,
        }),
    );
    assert!(s.background_hint().unwrap().contains("Критик"));

    // The parent's chunk is not for this feed; the run's own is.
    s.push_chunk(generation, "текст родителя");
    assert!(!s.feed.iter().any(|m| m.text.contains("текст родителя")));
    s.push_chunk(stream, " и дальше");
    assert_eq!(s.feed.last().unwrap().text, "начало и дальше");

    // A filed round: assistant + tool, then the next assistant — one bubble.
    let mut a = Message::assistant("");
    a.tool_calls = vec![crate::entities::subagent::SubagentRun::fixture("x", &["y"]).on_record()];
    s.grow_transcript(run_id, &[a, Message::assistant("вывод")]);
    let bubbles: Vec<_> = s
        .feed
        .iter()
        .filter(|m| m.role == FeedRole::Assistant)
        .collect();
    assert_eq!(bubbles.len(), 1, "rounds stitch into one bubble");
    assert!(bubbles[0].text.contains("вывод"));
    assert_eq!(bubbles[0].tools.len(), 1);
    assert_eq!(s.feed[0].role, FeedRole::System, "the persona stays on top");

    // Another conversation's round is ignored.
    s.grow_transcript(Uuid::new_v4(), &[Message::assistant("чужое")]);
    assert!(!s.feed.iter().any(|m| m.text.contains("чужое")));

    // The run's stream ends: its bubble closes and the chip goes (the run is
    // over — the turn's own clearing event usually precedes this anyway).
    s.finish_generation(stream, FinishReason::Stop, false);
    assert!(!s.generating);
    assert!(s.background_hint().is_none());
    assert!(!s.feed.iter().any(|m| m.role == FeedRole::Note));
}

/// A dialogue's line streams on its speaker's side (spec §9.13, stage 2):
/// a `LiveTurn` opened mid-line seeds a bubble of that side, the next line's
/// `TranscriptLine` retargets the stream, and a stale generation is ignored.
#[test]
fn a_dialogue_line_streams_on_its_speakers_side() {
    use crate::app::events::ChildView;
    let mut s = ChatScreen::new();
    let run_id = Uuid::new_v4();
    let stream = Uuid::new_v4();
    s.set_child_view(Some(ChildView {
        parent: Uuid::new_v4(),
        parent_title: "Родитель".into(),
        system_message: "personas".into(),
    }));
    s.activate_chat(
        run_id,
        "Mara ↔ Jonas".into(),
        &[Message::assistant("Your espresso!")],
        "",
        FeedView::default(),
        None,
        None,
    );
    // Opened mid-line: participant b is speaking — the seeded partial is a
    // **user-side** streaming bubble.
    s.set_live_turn(Some(Box::new(crate::app::events::LiveTurn {
        turn: Uuid::new_v4(),
        stream,
        role: MessageRole::User,
        partial: Some(crate::app::events::LivePartial {
            text: "hmm".into(),
            thoughts: String::new(),
            tools: Vec::new(),
        }),
        continues: false,
        background: false,
    })));
    let last = s.feed.last().unwrap();
    assert_eq!(
        last.role,
        FeedRole::User,
        "b's line streams on the user side"
    );
    assert!(last.streaming && last.text == "hmm");
    s.push_chunk(stream, ", that is a latte");
    assert_eq!(s.feed.last().unwrap().text, "hmm, that is a latte");

    // The line files; the next one is a's — the stream retargets.
    s.grow_transcript(run_id, &[Message::user("hmm, that is a latte")]);
    s.set_transcript_line(stream, MessageRole::Assistant);
    s.push_chunk(stream, "So sorry!");
    let last = s.feed.last().unwrap();
    assert_eq!(last.role, FeedRole::Assistant, "a's line is assistant-side");
    assert_eq!(last.text, "So sorry!");

    // A stale generation's line marker changes nothing.
    s.set_transcript_line(Uuid::new_v4(), MessageRole::User);
    s.push_chunk(stream, " Remaking it.");
    assert_eq!(s.feed.last().unwrap().role, FeedRole::Assistant);
}

/// A running dialogue edited its transcript (`reset_transcript`, spec §9.13):
/// the feed is rebuilt from the replacement — the persona bubble stays on
/// top, the discarded line is gone — and a foreign id is ignored.
#[test]
fn reset_transcript_replaces_the_feed_in_place() {
    use crate::app::events::ChildView;
    let mut s = ChatScreen::new();
    let run_id = Uuid::new_v4();
    s.set_child_view(Some(ChildView {
        parent: Uuid::new_v4(),
        parent_title: "Родитель".into(),
        system_message: "personas".into(),
    }));
    s.activate_chat(
        run_id,
        "Сцена".into(),
        &[
            Message::assistant("открывающая"),
            Message::user("плохая реплика"),
        ],
        "",
        FeedView::default(),
        None,
        None,
    );
    let replaced = vec![
        Message::assistant("открывающая"),
        Message::new(MessageRole::System, "Режиссёр попросил переписать"),
    ];
    s.reset_transcript(run_id, &replaced);
    assert!(!s.feed.iter().any(|m| m.text.contains("плохая")));
    assert_eq!(s.feed[0].role, FeedRole::System, "the persona stays on top");
    assert!(s.feed.iter().any(|m| m.role == FeedRole::Note));

    s.reset_transcript(Uuid::new_v4(), &[Message::user("чужое")]);
    assert!(!s.feed.iter().any(|m| m.text.contains("чужое")));
}

/// The dialogue chip's wordings (spec §9.13): the line being written and the
/// director judging the scene — keyed by the progress kind.
#[test]
fn the_dialogue_chip_names_the_line_and_the_director() {
    use crate::app::events::{RunProgressKind, SubagentProgress};
    let mut s = ChatScreen::new();
    let generation = gen_id();
    s.begin_generation(generation, None);
    s.set_subagent_progress(
        generation,
        Uuid::new_v4(),
        Some(SubagentProgress {
            name: "Mara ↔ Jonas".into(),
            round: 3,
            tool: None,
            kind: RunProgressKind::DialogueLine,
        }),
    );
    let hint = s.background_hint().unwrap();
    assert!(
        hint.contains("Mara ↔ Jonas") && hint.contains('3'),
        "{hint}"
    );
    s.set_subagent_progress(
        generation,
        Uuid::new_v4(),
        Some(SubagentProgress {
            name: "Mara ↔ Jonas".into(),
            round: 3,
            tool: None,
            kind: RunProgressKind::DialogueDirector,
        }),
    );
    let hint = s.background_hint().unwrap();
    assert!(
        hint.contains("режиссёр") || hint.contains("director"),
        "{hint}"
    );
}

/// Back on the running turn's chat (docs/subagent-live.md §3.5): `live_turn`
/// restores the generation, so the stream and the chip resume into its feed.
#[test]
fn returning_to_the_running_chat_resumes_its_generation() {
    let mut s = ChatScreen::new();
    let chat = Uuid::new_v4();
    let generation = Uuid::new_v4();
    s.set_child_view(None);
    s.activate_chat(
        chat,
        "Чат".into(),
        &[Message::user("вопрос")],
        "",
        FeedView::default(),
        None,
        None,
    );
    s.set_live_turn(Some(Box::new(crate::app::events::LiveTurn {
        role: MessageRole::Assistant,
        turn: generation,
        stream: generation,
        partial: Some(crate::app::events::LivePartial {
            text: "Смотрю".into(),
            thoughts: "план".into(),
            tools: vec![
                crate::app::events::LiveTool {
                    call_id: "c1".into(),
                    name: "web_search".into(),
                    arguments: "{}".into(),
                    result: Some(("ок".into(), 0)),
                },
                crate::app::events::LiveTool {
                    call_id: "c2".into(),
                    name: "call_subagent".into(),
                    arguments: "{}".into(),
                    result: None,
                },
            ],
        }),
        continues: false,
        background: false,
    })));
    assert!(s.generating);
    // The seed: one streaming bubble with the text, the thoughts and both
    // cards — the answered one complete, the other still running.
    let bubble = s.feed.last().unwrap();
    assert!(bubble.streaming);
    assert_eq!(bubble.text, "Смотрю");
    assert_eq!(bubble.thoughts, "план");
    assert_eq!(bubble.tools.len(), 2);
    assert!(!bubble.tools[0].running && bubble.tools[0].result == "ок");
    assert!(bubble.tools[1].running);
    // …and the rest of the stream continues into it: the card completes in
    // place, the text goes on.
    s.push_tool_call(
        generation,
        "c2".into(),
        "call_subagent".into(),
        "{}".into(),
        "итог".into(),
        0,
    );
    assert!(!s.feed.last().unwrap().tools[1].running);
    s.push_chunk(generation, "продолжение");
    assert!(s.feed.iter().any(|m| m.text.contains("продолжение")));
    s.finish_generation(generation, FinishReason::Stop, false);
    assert!(!s.generating);
}

/// The sub-agent chip (spec §9.3.2): worded from the raw progress, the tool
/// named when inside one, composing with the background tasks, cleared by
/// the run's end and by the turn's end, and dropped for a stale generation.
#[test]
fn the_subagent_chip_follows_the_run_and_cannot_outlive_the_turn() {
    use crate::app::events::SubagentProgress;
    let mut s = ChatScreen::new();
    let gen_id = Uuid::new_v4();
    let run = Uuid::new_v4();
    s.begin_generation(gen_id, None);
    let at = |round: u32, tool: Option<&str>| {
        Some(SubagentProgress {
            kind: crate::app::events::RunProgressKind::Subagent,
            name: "Критик".into(),
            round,
            tool: tool.map(str::to_string),
        })
    };

    s.set_subagent_progress(gen_id, run, at(1, None));
    let hint = s.background_hint().expect("the chip must be shown");
    assert!(hint.contains("Критик") && hint.contains('1'), "{hint}");
    s.set_subagent_progress(gen_id, run, at(2, Some("web_search")));
    let hint = s.background_hint().unwrap();
    assert!(hint.contains("web_search") && hint.contains('2'), "{hint}");

    s.set_reflecting(true);
    let hint = s.background_hint().unwrap();
    assert!(
        hint.contains(s.loc().t("ui.chat.bg.reflect")) && hint.contains("Критик"),
        "{hint}"
    );

    // The run ended; the turn goes on.
    s.set_subagent_progress(gen_id, run, None);
    assert!(!s.background_hint().unwrap().contains("Критик"));

    // The turn ends with a run still reported — the chip goes with it.
    s.set_subagent_progress(gen_id, run, at(3, None));
    s.finish_generation(gen_id, FinishReason::Cancelled, false);
    assert!(!s.background_hint().unwrap().contains("Критик"));

    // A stale generation's chip is dropped.
    s.begin_generation(Uuid::new_v4(), None);
    s.set_subagent_progress(gen_id, run, at(1, None));
    assert!(!s.background_hint().unwrap().contains("Критик"));
}

/// A chip from a turn the user has already cancelled must not appear on the next
/// one — the same staleness rule every streamed event follows (spec §4.4).
#[test]
fn a_retry_chip_from_a_stale_generation_is_dropped() {
    let mut s = ChatScreen::new();
    let current = Uuid::new_v4();
    s.begin_generation(current, None);

    s.set_retrying(Uuid::new_v4(), 2, 3, 9);
    assert_eq!(
        s.background_hint(),
        None,
        "a chip for another generation must be ignored"
    );
}

// ---------- the quit commands (`/exit`, `/quit`, spec §11.7) ----------

/// Both spellings quit from the input box — the third route out, for terminals
/// that keep `Ctrl+Q` and `F10` for themselves.
#[test]
fn exit_commands_quit_from_the_input_box() {
    for cmd in crate::features::exit_command::ALIASES {
        let mut s = ChatScreen::new();
        type_str(&mut s, cmd);
        assert_eq!(
            s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)),
            Some(ChatIntent::Quit),
            "{cmd}"
        );
        // The command must not survive as the chat's draft: it is cleared here,
        // and `app::runtime::run_loop` flushes this last state on its way out.
        assert!(s.input.is_empty(), "{cmd} left text in the box");
        assert!(
            s.take_dirty_draft().is_some_and(|d| d.is_empty()),
            "{cmd} must hand an empty draft back for saving"
        );
    }
}

/// Quitting during generation works, exactly as `Ctrl+Q`/`F10` do — the command
/// is checked before the `generating` gate that holds back ordinary messages.
#[test]
fn exit_command_quits_while_generating() {
    let mut s = ChatScreen::new();
    s.begin_generation(gen_id(), None);
    type_str(&mut s, "/exit");
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)),
        Some(ChatIntent::Quit)
    );

    // The control: an ordinary message IS held back while generating, so the
    // assertion above is about the command and not about a missing gate.
    let mut s = ChatScreen::new();
    s.begin_generation(gen_id(), None);
    type_str(&mut s, "/exit please");
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)),
        None
    );
}

/// A mistyped quit leaves a note instead of quitting — and instead of going out
/// to the model, which is what an unrecognized `/…` line would do.
#[test]
fn a_mistyped_quit_command_notes_and_does_not_send() {
    let mut s = ChatScreen::new();
    type_str(&mut s, "/quit now");
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)),
        None,
        "a malformed command is neither sent nor obeyed"
    );
    let note = s
        .feed
        .iter()
        .rev()
        .find(|m| m.role == FeedRole::Note)
        .expect("a note explaining the syntax");
    // The note has to close the door (docs/lessons.md §4): it names the route
    // that does work, not only the mistake.
    assert!(note.text.contains("/quit"), "{}", note.text);
    assert!(note.text.contains("Ctrl+Q"), "{}", note.text);
}

/// The words themselves are ordinary things to say to a model, and a longer
/// command that merely starts with one is not a quit command either.
#[test]
fn text_that_only_resembles_a_quit_command_is_sent() {
    for text in ["exit", "how do I quit vim?", "/exits"] {
        let mut s = ChatScreen::new();
        type_str(&mut s, text);
        assert_eq!(
            s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)),
            Some(ChatIntent::Send(text.to_string())),
            "{text}"
        );
    }
}

// ---------- `chat://` references (spec §11.3) ----------

/// A chat card for the address book the screen keeps.
fn card(id: Uuid, profile: Uuid, title: &str) -> ChatSummary {
    let mut c = ChatSummary::fixture(title);
    c.id = id;
    c.profile_id = profile;
    c.message_count = 1;
    c
}

/// Puts `s` on `here` with `others` also in the list, all of one profile, and
/// an assistant reply citing every id in `cited`.
fn screen_citing(here: Uuid, others: &[Uuid], cited: &[Uuid]) -> ChatScreen {
    let profile = Uuid::new_v4();
    let mut s = ChatScreen::new();
    let mut list = vec![card(here, profile, "Текущий")];
    list.extend(
        others
            .iter()
            .enumerate()
            .map(|(i, id)| card(*id, profile, &format!("Прошлый {i}"))),
    );
    let text = cited
        .iter()
        .map(|id| crate::features::chat_links::uri(*id))
        .collect::<Vec<_>>()
        .join(" и ");
    s.activate_chat(
        here,
        "Текущий".into(),
        &[Message::assistant(format!("см. {text}"))],
        "",
        FeedView::default(),
        None,
        None,
    );
    // After activation, as it arrives in practice: the orchestrator emits the
    // list on its own schedule.
    s.set_chat_list(list);
    s
}

/// The cache the picker reads is only built by a render, so every test that
/// asks for references draws a frame first — the same constraint the jump obeys.
fn draw(s: &mut ChatScreen) {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;
    let mut term = Terminal::new(TestBackend::new(60, 16)).unwrap();
    term.draw(|f| s.render(f)).unwrap();
}

fn ctrl(c: char) -> KeyEvent {
    KeyEvent::new(KeyCode::Char(c), KeyModifiers::CONTROL)
}

/// `Ctrl+L` opens the picker on the cited conversation, and `Enter` follows it.
#[test]
fn ctrl_l_follows_a_cited_conversation() {
    let (here, other) = (gen_id(), gen_id());
    let mut s = screen_citing(here, &[other], &[other]);
    draw(&mut s);

    assert_eq!(s.handle_key(ctrl('l')), None, "the picker opens in place");
    assert!(s.chat_links.is_some());
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)),
        Some(ChatIntent::OpenChatLink(other))
    );
    assert!(s.chat_links.is_none(), "the picker closes on a pick");
}

/// Nothing to follow is not an empty list — it says what a reference looks
/// like (docs/lessons.md §4).
#[test]
fn ctrl_l_with_no_references_explains_instead_of_opening() {
    let mut s = screen_citing(gen_id(), &[], &[]);
    draw(&mut s);

    assert_eq!(s.handle_key(ctrl('l')), None);
    assert!(s.chat_links.is_none(), "an empty popup is not opened");
    let note = s
        .feed
        .iter()
        .rev()
        .find(|m| m.role == FeedRole::Note)
        .expect("a note about references");
    assert!(note.text.contains("chat://"), "{}", note.text);
}

/// A reference back to the open conversation is listed (the model wrote it)
/// but following it says where you already are instead of switching.
#[test]
fn a_reference_to_the_open_chat_says_so() {
    let here = gen_id();
    let mut s = screen_citing(here, &[], &[here]);
    draw(&mut s);

    s.handle_key(ctrl('l'));
    assert!(s.chat_links.is_some(), "the open chat is still offered");
    assert_eq!(
        s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)),
        None,
        "no switch to the chat we are on"
    );
    let note = s
        .feed
        .iter()
        .rev()
        .find(|m| m.role == FeedRole::Note)
        .expect("a note saying we are already here");
    assert!(!note.text.is_empty());
}

/// The profile boundary (spec §9.5) holds in the feed too: an address for
/// another companion's conversation resolves to nothing, so it is neither
/// drawn as a link nor offered.
#[test]
fn another_profiles_conversation_is_not_a_reference() {
    let (here, foreign) = (gen_id(), gen_id());
    let mut s = ChatScreen::new();
    s.activate_chat(
        here,
        "Текущий".into(),
        &[Message::assistant(format!(
            "см. {}",
            crate::features::chat_links::uri(foreign)
        ))],
        "",
        FeedView::default(),
        None,
        None,
    );
    s.set_chat_list(vec![
        card(here, Uuid::new_v4(), "Текущий"),
        card(foreign, Uuid::new_v4(), "Чужой профиль"),
    ]);
    draw(&mut s);

    assert!(s.feed_view.chat_links().is_empty());
    assert_eq!(s.handle_key(ctrl('l')), None);
    assert!(s.chat_links.is_none());
}

/// Switching chats must not leave the previous conversation's picker open.
#[test]
fn switching_chats_closes_the_picker() {
    let (here, other) = (gen_id(), gen_id());
    let mut s = screen_citing(here, &[other], &[other]);
    draw(&mut s);
    s.handle_key(ctrl('l'));
    assert!(s.chat_links.is_some());

    s.activate_chat(
        other,
        "Прошлый".into(),
        &[Message::user("привет")],
        "",
        FeedView::default(),
        None,
        None,
    );
    assert!(s.chat_links.is_none());
}

/// Stage 2 (spec §11.3): a left click on a drawn `chat://` address follows it,
/// and a click a column past it is ordinary feed again.
#[test]
fn a_click_on_a_chat_reference_opens_that_conversation() {
    use ratatui::crossterm::event::MouseButton;
    let (here, other) = (gen_id(), gen_id());
    let mut s = screen_citing(here, &[other], &[other]);
    draw(&mut s);
    let at = s
        .feed_view
        .link_hit_for_test(0)
        .expect("the address is drawn");

    assert_eq!(
        s.handle_mouse(mouse_at(
            MouseEventKind::Down(MouseButton::Left),
            at.0,
            at.1
        )),
        Some(ChatIntent::OpenChatLink(other))
    );
    assert_eq!(
        s.handle_mouse(mouse_at(
            MouseEventKind::Down(MouseButton::Left),
            at.2,
            at.1
        )),
        None,
        "the cell past the address is not the address"
    );
}

/// Clicking a reference back to the open conversation says so rather than
/// switching — the same rule (and wording) the picker follows.
#[test]
fn clicking_a_reference_to_the_open_chat_says_so() {
    use ratatui::crossterm::event::MouseButton;
    let here = gen_id();
    let mut s = screen_citing(here, &[], &[here]);
    draw(&mut s);
    let at = s
        .feed_view
        .link_hit_for_test(0)
        .expect("the address is drawn");

    assert_eq!(
        s.handle_mouse(mouse_at(
            MouseEventKind::Down(MouseButton::Left),
            at.0,
            at.1
        )),
        None
    );
    assert!(
        s.feed
            .iter()
            .rev()
            .any(|m| m.role == FeedRole::Note && !m.text.is_empty()),
        "a note instead of a silent no-op"
    );
}

/// The click must not reach the feed while an overlay owns the screen — and the
/// reference picker is now one of them.
#[test]
fn a_click_is_ignored_while_the_picker_is_open() {
    use ratatui::crossterm::event::MouseButton;
    let (here, other) = (gen_id(), gen_id());
    let mut s = screen_citing(here, &[other], &[other]);
    draw(&mut s);
    let at = s
        .feed_view
        .link_hit_for_test(0)
        .expect("the address is drawn");
    s.handle_key(ctrl('l'));
    assert!(s.chat_links.is_some());

    assert_eq!(
        s.handle_mouse(mouse_at(
            MouseEventKind::Down(MouseButton::Left),
            at.0,
            at.1
        )),
        None
    );
}

// ---------- typed routes for the chords (docs/history/command-only-control.md) ----------

use crate::features::ui_command::{self, Arity, UiCommand};

/// A chat screen driven the way a user drives it — type a line, press `Enter` —
/// with the state the typed routes need: an open chat, two profiles and a
/// settings snapshot.
///
/// A harness rather than a prologue per test: six tests spelling out the same
/// four setup lines is the sliding self-duplication the gate keeps catching
/// (docs/lessons.md §2, where the previous recurrence was exactly this shape).
struct Cmd {
    s: ChatScreen,
    chat: Uuid,
}

impl Cmd {
    /// A furnished screen: the common case. The ids are **fixed** rather than
    /// fresh, so two screens built here are genuinely identical — the
    /// command-versus-chord comparison below is between two of them, and random
    /// ids would make every intent that carries one differ.
    fn new() -> Self {
        let mut s = ChatScreen::new();
        let chat = Uuid::from_u128(1);
        s.activate_chat(
            chat,
            "Про космос".into(),
            &[Message::user("привет")],
            "",
            FeedView::default(),
            None,
            None,
        );
        s.set_profile_list(vec![
            ProfileSummary {
                id: Uuid::from_u128(2),
                name: "Гайя".into(),
            },
            ProfileSummary {
                id: Uuid::from_u128(3),
                name: "Гелиос".into(),
            },
        ]);
        s.set_settings(
            AppConfig::default(),
            vec![Profile::new("P", "sys")],
            Vec::new(),
            Default::default(),
            Vec::new(),
        );
        Self { s, chat }
    }

    /// A bare screen: no chat, no profiles, no settings snapshot yet — the state
    /// the app is in for the first moments after launch.
    fn bare() -> Self {
        Self {
            s: ChatScreen::new(),
            chat: Uuid::nil(),
        }
    }

    /// Types a command line and presses `Enter`.
    fn run(&mut self, line: &str) -> Option<ChatIntent> {
        type_str(&mut self.s, line);
        self.s
            .handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE))
    }

    fn ctrl(&mut self, c: char) -> Option<ChatIntent> {
        self.s
            .handle_key(KeyEvent::new(KeyCode::Char(c), KeyModifiers::CONTROL))
    }

    fn key(&mut self, code: KeyCode) -> Option<ChatIntent> {
        self.s.handle_key(KeyEvent::new(code, KeyModifiers::NONE))
    }

    /// The last note in the feed (the answer a refused command leaves).
    fn last_note(&self) -> String {
        self.s
            .feed
            .iter()
            .rev()
            .find(|m| m.role == FeedRole::Note)
            .map(|m| m.text.clone())
            .unwrap_or_default()
    }
}

/// The heart of the track: a command reaches the action through the very
/// handler its chord uses, so the two cannot grow two behaviours. Each pair is
/// run on two identical screens and must produce the same intent — and, where
/// the effect is in-screen rather than an intent, the same visible state.
#[test]
fn every_command_agrees_with_the_chord_it_mirrors() {
    let ctrl_pairs = [
        ("/settings", 'p'),
        ("/regen", 'r'),
        ("/takeback", 'e'),
        ("/new", 'n'),
        ("/thoughts", 't'),
        ("/toolcalls", 'o'),
        ("/mouse", 'w'),
        ("/links", 'l'),
    ];
    for (line, chord) in ctrl_pairs {
        let (mut typed, mut pressed) = (Cmd::new(), Cmd::new());
        assert_eq!(
            typed.run(line),
            pressed.ctrl(chord),
            "{line} and Ctrl+{chord} disagree"
        );
    }
    // The function keys, and `Esc` for the chat list.
    for (line, code) in [
        ("/self", KeyCode::F(3)),
        ("/copy", KeyCode::F(5)),
        ("/chats", KeyCode::Esc),
    ] {
        let (mut typed, mut pressed) = (Cmd::new(), Cmd::new());
        assert_eq!(
            typed.run(line),
            pressed.key(code),
            "{line} and {code:?} disagree"
        );
    }
    // `/help` has no chord to compare against: `F1` never reaches the screen
    // (the runtime routes it above every screen and owns the overlay), and the
    // chat's `?` is gone — so the typed route is checked against the intent the
    // runtime opens the overlay on.
    let mut typed = Cmd::new();
    assert_eq!(typed.run("/help"), Some(ChatIntent::OpenHelp));
    // In-screen effects have no intent to compare, so compare the state each
    // route leaves behind.
    let (mut typed, mut pressed) = (Cmd::new(), Cmd::new());
    assert_eq!(typed.run("/emoji"), None);
    pressed.ctrl('b');
    assert_eq!(typed.s.emoji.is_some(), pressed.s.emoji.is_some());
    let (mut typed, mut pressed) = (Cmd::new(), Cmd::new());
    assert_eq!(typed.run("/find"), None);
    pressed.ctrl('f');
    assert_eq!(typed.s.search.is_some(), pressed.s.search.is_some());
}

/// Every command clears the input box and hands an **empty** draft back, or the
/// next launch would greet the user with the command they typed (the `/exit`
/// trap, journal ui-input.md). Driven off the registry, so a new row is covered
/// the moment it is added.
#[test]
fn every_command_clears_the_box_and_the_draft() {
    for spec in ui_command::COMMANDS {
        let mut c = Cmd::new();
        let line = match spec.arity {
            Arity::Required => format!("{} что-нибудь", spec.aliases[0]),
            _ => spec.aliases[0].to_string(),
        };
        c.run(&line);
        // `/rename` bare deliberately hands the current title back for editing;
        // it is the one command whose answer IS text in the box.
        if spec.command == UiCommand::Rename {
            continue;
        }
        assert!(c.s.input.is_empty(), "{line} left text in the box");
        assert!(
            c.s.take_dirty_draft().is_some_and(|d| d.is_empty()),
            "{line} must hand an empty draft back for saving"
        );
    }
}

/// Every registry command is highlighted while it is being typed — including a
/// malformed one, which is a command and not prose. What the box shows and what
/// `Enter` does come from the same parser, so they cannot disagree.
#[test]
fn commands_are_highlighted_as_they_are_typed() {
    for spec in ui_command::COMMANDS {
        for alias in spec.aliases {
            let mut c = Cmd::new();
            type_str(&mut c.s, alias);
            assert!(c.s.input_is_command(), "{alias} is not highlighted");
            // …and with a stray argument, which is still a command.
            type_str(&mut c.s, " nonsense");
            assert!(
                c.s.input_is_command(),
                "{alias} with an argument is not highlighted"
            );
        }
    }
    // A longer word that merely starts with a command name is prose, exactly as
    // it is on `Enter`.
    for text in ["/settings2", "/newest", "how do I stop this?"] {
        let mut c = Cmd::new();
        type_str(&mut c.s, text);
        assert!(!c.s.input_is_command(), "{text} must stay plain text");
    }
}

/// A command blocked by state answers; a chord in the same state is silent. That
/// asymmetry is the point (docs/lessons.md §4) — and each note has to name a
/// route out, so the answer is not a dead end.
#[test]
fn a_blocked_command_says_so_and_names_a_route() {
    // Nothing is generating: `/stop` has nothing to cancel.
    let mut c = Cmd::new();
    assert_eq!(c.run("/stop"), None);
    assert!(!c.last_note().is_empty(), "/stop said nothing");

    // A turn is running: the destructive pair and impersonation are held back —
    // by the command, with an answer, where the chord just no-ops.
    for line in ["/regen", "/retry", "/takeback", "/impersonate"] {
        let mut c = Cmd::new();
        c.s.begin_generation(gen_id(), None);
        assert_eq!(c.run(line), None, "{line} must not fire mid-turn");
        let note = c.last_note();
        assert!(note.contains(line), "{line}: the note must name it: {note}");
        assert!(
            note.contains("/stop"),
            "{line}: the note must name the way out: {note}"
        );
    }

    // No chat open yet: the chat-scoped commands say so instead of doing nothing.
    for line in ["/copy", "/clone", "/rename Новое"] {
        let mut c = Cmd::bare();
        assert_eq!(c.run(line), None, "{line} without a chat");
        assert!(!c.last_note().is_empty(), "{line} said nothing");
    }

    // The settings snapshot has not arrived yet (the first moments after launch).
    let mut c = Cmd::bare();
    assert_eq!(c.run("/settings"), None);
    assert!(!c.last_note().is_empty(), "/settings said nothing");
}

/// `/stop` is the explicit half of `Esc`: it cancels a running turn, while
/// `/chats` is the other half and opens the list whatever the turn is doing.
#[test]
fn stop_cancels_a_turn_and_chats_never_does() {
    let mut c = Cmd::new();
    c.s.begin_generation(gen_id(), None);
    assert_eq!(c.run("/stop"), Some(ChatIntent::Cancel));

    let mut c = Cmd::new();
    c.s.begin_generation(gen_id(), None);
    assert_eq!(c.run("/chats"), Some(ChatIntent::OpenChatList));
    // The control: `Esc` in that same state cancels instead — which is the
    // overload the two commands exist to resolve.
    let mut c = Cmd::new();
    c.s.begin_generation(gen_id(), None);
    assert_eq!(c.key(KeyCode::Esc), Some(ChatIntent::Cancel));
}

/// The confirmation popup belongs to the operation, not to the key: with
/// `confirm_destructive_keys` on, a typed `/regen` opens it too, and the intent
/// is only issued on `Enter`.
#[test]
fn a_typed_takeback_honours_the_confirmation_setting() {
    for (line, expected) in [
        ("/regen", ChatIntent::RegenerateLast),
        ("/takeback", ChatIntent::DeleteLastExchange),
    ] {
        let mut c = Cmd::new();
        let mut cfg = AppConfig::default();
        cfg.interface.confirm_destructive_keys = true;
        c.s.set_settings(cfg, Vec::new(), Vec::new(), Default::default(), Vec::new());
        assert_eq!(c.run(line), None, "{line} must ask first");
        assert!(c.s.confirm.is_some(), "{line} did not open the popup");
        assert_eq!(
            c.key(KeyCode::Enter),
            Some(expected),
            "{line} was not confirmed"
        );
    }
}

/// `/search <text>` goes straight to the message-level results screen — the
/// two-step journey (`Esc`, `Ctrl+F`, `Ctrl+G`) collapsed into one line. Bare, it
/// teaches its own syntax rather than opening an empty screen (fork F4).
#[test]
fn search_asks_the_orchestrator_and_bare_search_teaches_the_syntax() {
    let mut c = Cmd::new();
    assert_eq!(
        c.run("/search про космос"),
        Some(ChatIntent::SearchMessages {
            query: "про космос".into()
        })
    );

    let mut c = Cmd::new();
    assert_eq!(c.run("/search"), None);
    assert!(
        c.last_note().contains("/search"),
        "the usage line: {}",
        c.last_note()
    );
}

/// `/find <text>` seeds the in-feed query, so one line opens the search *and*
/// lands on a match.
#[test]
fn find_with_text_seeds_the_query() {
    let mut c = Cmd::new();
    assert_eq!(c.run("/find привет"), None);
    assert!(c.s.search.is_some(), "the search field is not open");
    assert_eq!(c.s.search_last, "привет");
}

/// `/rename <title>` renames the open chat. Bare, it hands the current title
/// back as an editable command line — the box is where a command is typed, so
/// the title is edited there rather than in a popup of its own.
#[test]
fn rename_takes_a_title_or_offers_the_current_one() {
    let mut c = Cmd::new();
    let chat = c.chat;
    assert_eq!(
        c.run("/rename Космос и мы"),
        Some(ChatIntent::RenameChat {
            id: chat,
            title: "Космос и мы".into()
        })
    );

    let mut c = Cmd::new();
    assert_eq!(c.run("/rename"), None);
    assert_eq!(c.s.input.text(), "/rename Про космос");
    // Pressing Enter on what it offered renames to the same title — a no-op
    // rename, not a crash and not a message going out to the model.
    let chat = c.chat;
    assert_eq!(
        c.s.handle_key(KeyEvent::new(KeyCode::Enter, KeyModifiers::NONE)),
        Some(ChatIntent::RenameChat {
            id: chat,
            title: "Про космос".into()
        })
    );
}

/// A long title is stored whole — the two routes to a title agree that the cut
/// belongs to whichever surface draws it, in columns and with a marker
/// (spec §11.2, the user's decision of 2026-09-06).
#[test]
fn a_renamed_title_is_kept_whole() {
    let mut c = Cmd::new();
    let long = "я".repeat(140);
    let Some(ChatIntent::RenameChat { title, .. }) = c.run(&format!("/rename {long}")) else {
        panic!("expected a rename");
    };
    assert_eq!(title, long);
}

/// `/new <profile>`: an exact name, then an unambiguous prefix (fork F3). A miss
/// and an ambiguity both name the candidates and the bare-`/new` route.
#[test]
fn new_chat_resolves_a_profile_name_or_says_why_not() {
    let mut c = Cmd::new();
    let expected = c.s.profiles[0].id;
    assert_eq!(
        c.run("/new гайя"), // exact, case-insensitively
        Some(ChatIntent::NewChat {
            profile_id: Some(expected)
        })
    );

    let mut c = Cmd::new();
    let helios = c.s.profiles[1].id;
    assert_eq!(
        c.run("/new гел"), // an unambiguous prefix
        Some(ChatIntent::NewChat {
            profile_id: Some(helios)
        })
    );

    // Ambiguous: both profile names share their first letter, so a one-letter
    // prefix matches them both — the note lists the candidates.
    let mut c = Cmd::new();
    assert_eq!(c.run("/new г"), None);
    let note = c.last_note();
    assert!(note.contains("Гайя") && note.contains("Гелиос"), "{note}");
    assert!(note.contains("/new"), "the route back: {note}");

    // A miss: the note lists what there is.
    let mut c = Cmd::new();
    assert_eq!(c.run("/new Прометей"), None);
    let note = c.last_note();
    assert!(note.contains("Гайя"), "{note}");

    // Bare `/new` with several profiles opens the picker, exactly as `Ctrl+N`.
    let mut c = Cmd::new();
    assert_eq!(c.run("/new"), None);
    assert!(c.s.profile_overlay.is_some(), "the picker did not open");
}

/// The registry does not swallow ordinary text: a message that merely looks like
/// a command still goes out to the model.
#[test]
fn text_that_only_resembles_a_command_is_sent() {
    for text in ["/settings2", "how do I /stop it?", "стоп"] {
        let mut c = Cmd::new();
        assert_eq!(
            c.run(text),
            Some(ChatIntent::Send(text.into())),
            "{text} must go out as a message"
        );
    }
}

/// Every registry command is answered by the screen — no row can be added
/// without deciding what it does. A command that neither returns an intent nor
/// changes anything visible would be exactly the silence this track exists to
/// remove.
#[test]
fn no_command_is_a_silent_no_op() {
    for spec in ui_command::COMMANDS {
        let mut c = Cmd::new();
        let line = match spec.arity {
            Arity::Required => format!("{} космос", spec.aliases[0]),
            _ => spec.aliases[0].to_string(),
        };
        let intent = c.run(&line);
        let visible = c.s.emoji.is_some()
            || c.s.search.is_some()
            || c.s.profile_overlay.is_some()
            || c.s.chat_links.is_some()
            || !c.last_note().is_empty()
            || !c.s.input.is_empty();
        assert!(
            intent.is_some() || visible,
            "{:?} ({line}) did nothing at all",
            spec.command
        );
    }
}

/// A modal owns the keyboard while it is open, so a command typed "through" one
/// cannot fire. This pins the routing order rather than the commands themselves.
/// The in-feed search field stands in for any chat-owned modal (the help
/// dialog, which this test used to open, is the runtime's overlay now — its
/// modality is pinned in the runtime's tests): it owns every typed character,
/// so the command line lands in the query, never in the parser.
#[test]
fn commands_do_not_fire_through_a_modal() {
    let mut c = Cmd::new();
    c.s.handle_key(KeyEvent::new(KeyCode::Char('f'), KeyModifiers::CONTROL));
    assert!(c.s.search.is_some());
    assert_eq!(c.run("/settings"), None, "a modal must keep the keyboard");
    assert!(c.s.search.is_some(), "the search field closed unexpectedly");
    assert!(
        c.s.input.is_empty(),
        "the command line leaked into the input box"
    );
}

/// The registry's `UiCommand` variants are all reachable from the table — a
/// variant nobody can type would be dead weight, and one claimed twice would
/// shadow the second row.
#[test]
fn every_ui_command_variant_has_exactly_one_row() {
    let mut seen: Vec<UiCommand> = Vec::new();
    for spec in ui_command::COMMANDS {
        assert!(
            !seen.contains(&spec.command),
            "{:?} has two rows",
            spec.command
        );
        seen.push(spec.command);
    }
    assert_eq!(seen.len(), ui_command::COMMANDS.len());
}

// ---------- stage 2: the screens' own leftovers (/profile, /self clear) ----------

/// `/profile list` answers from the snapshot the screen already holds, and marks
/// the open chat's own profile — the question behind it is usually "which one am
/// I talking to?".
#[test]
fn profile_list_names_them_and_marks_the_active_one() {
    let mut c = Cmd::new();
    // The open chat belongs to the first profile.
    c.s.set_chat_list(vec![card(c.chat, Uuid::from_u128(2), "Про космос")]);
    assert_eq!(c.run("/profile list"), None);
    let note = c.last_note();
    assert!(note.contains("Гайя") && note.contains("Гелиос"), "{note}");
    let marker = c.s.palette.glyphs().title_marker;
    assert!(
        note.contains(&format!("{marker} Гайя")),
        "the open chat's profile is not marked: {note}"
    );
    assert!(
        !note.contains(&format!("{marker} Гелиос")),
        "only one profile is the active one: {note}"
    );
}

/// `/profile new` creates through the same command the settings screen's
/// `Ctrl+N` sends, with the same default name; a given name is used verbatim.
/// The persona stays empty either way — that is the settings screen's job, and
/// the orchestrator's notice is what says so.
#[test]
fn profile_new_matches_the_settings_screen_default() {
    let mut c = Cmd::new();
    let default_name = c.s.loc.t("ui.settings.new_profile_name").to_string();
    assert_eq!(
        c.run("/profile new"),
        Some(ChatIntent::CreateProfile { name: default_name })
    );

    let mut c = Cmd::new();
    assert_eq!(
        c.run("/profile new Дневной помощник"),
        Some(ChatIntent::CreateProfile {
            name: "Дневной помощник".into()
        })
    );
}

/// Deleting a profile **always** asks, unlike the key it mirrors: a typed name
/// can resolve to a profile the user did not picture, where the settings screen
/// shows the row it is about to delete. The question names the profile and how
/// many conversations go with it.
#[test]
fn profile_delete_always_confirms_and_names_what_goes() {
    let mut c = Cmd::new();
    c.s.set_chat_list(vec![
        card(c.chat, Uuid::from_u128(2), "Про космос"),
        card(Uuid::from_u128(9), Uuid::from_u128(2), "Второй"),
    ]);
    // The setting that gates the two chat-level keys is OFF here — this popup
    // is not governed by it.
    assert!(!c.s.confirm_destructive);
    assert_eq!(c.run("/profile delete Гайя"), None, "it must ask first");
    let text = confirm_popup_text(&mut c.s);
    assert!(text.contains("Гайя"), "the profile is named: {text}");
    assert!(text.contains('2'), "the chat count is stated: {text}");
    // Answering yes sends the same command `Ctrl+D` sends.
    assert_eq!(
        c.key(KeyCode::Enter),
        Some(ChatIntent::DeleteProfile(Uuid::from_u128(2)))
    );

    // …and `Esc` answers no: nothing is sent and the popup closes.
    let mut c = Cmd::new();
    c.run("/profile delete Гайя");
    assert_eq!(c.key(KeyCode::Esc), None);
    assert!(c.s.confirm.is_none(), "the popup stayed open");
}

/// The last profile cannot go — there would be nothing to create chats from. The
/// orchestrator refuses too, but asking a question whose "yes" is then declined
/// is a worse way to say it.
#[test]
fn deleting_the_only_profile_is_refused_before_it_asks() {
    let mut c = Cmd::new();
    c.s.set_profile_list(vec![ProfileSummary {
        id: Uuid::from_u128(2),
        name: "Гайя".into(),
    }]);
    assert_eq!(c.run("/profile delete Гайя"), None);
    assert!(c.s.confirm.is_none(), "it must not ask");
    assert!(!c.last_note().is_empty(), "it said nothing");
    assert!(
        c.last_note().contains("/profile new"),
        "the note must name the way out: {}",
        c.last_note()
    );
}

/// A name that matches nothing or several profiles is answered by the **shared**
/// resolver, so `/profile delete` and `/new` agree on the prefix rule — but each
/// names the route that fits the command that was typed.
#[test]
fn profile_delete_uses_the_shared_name_resolver() {
    let mut c = Cmd::new();
    assert_eq!(c.run("/profile delete гел"), None, "an unambiguous prefix");
    assert!(
        matches!(&c.s.confirm, Some(ConfirmAction::DeleteProfile { name, .. }) if name == "Гелиос"),
        "the prefix must resolve like /new does"
    );

    let mut c = Cmd::new();
    assert_eq!(c.run("/profile delete г"), None);
    assert!(c.s.confirm.is_none(), "an ambiguous name must not ask");
    let note = c.last_note();
    assert!(note.contains("Гайя") && note.contains("Гелиос"), "{note}");
    assert!(
        note.contains("/profile list"),
        "the route fits the typed command: {note}"
    );

    // The other caller keeps its own route.
    let mut c = Cmd::new();
    c.run("/new г");
    assert!(
        c.last_note().contains("/new"),
        "/new keeps the picker route: {}",
        c.last_note()
    );
}

/// `/self clear` mirrors `Ctrl+K` twice in the self-model screen — a
/// confirmation there, a confirmation here — and reaches the same edit.
#[test]
fn self_clear_confirms_and_sends_the_same_edit() {
    let mut c = Cmd::new();
    assert_eq!(c.run("/self clear"), None, "it must ask first");
    assert!(c.s.confirm.is_some());
    assert_eq!(c.key(KeyCode::Enter), Some(ChatIntent::ClearSelfModel));

    // Bare `/self` still opens the screen — the subcommand did not shadow it.
    let mut c = Cmd::new();
    assert_eq!(c.run("/self"), Some(ChatIntent::OpenSelfModel));

    // An unknown word is reported by the parser, with the usage line.
    let mut c = Cmd::new();
    assert_eq!(c.run("/self wipe"), None);
    assert!(
        c.last_note().contains("/self"),
        "the usage line: {}",
        c.last_note()
    );

    // The model belongs to the active profile: with no chat open the
    // orchestrator would drop the edit, so the command says so instead.
    let mut c = Cmd::bare();
    assert_eq!(c.run("/self clear"), None);
    assert!(c.s.confirm.is_none(), "nothing to clear, nothing to ask");
    assert!(!c.last_note().is_empty(), "/self clear said nothing");
}

/// The stage-2 commands are highlighted while typed and never leak to the model,
/// exactly like the registry's.
#[test]
fn profile_commands_are_highlighted_and_never_sent() {
    for text in [
        "/profile",
        "/profile list",
        "/profile new Гайя",
        "/profile delete Гайя",
        "/profile renam x",
    ] {
        let mut c = Cmd::new();
        type_str(&mut c.s, text);
        assert!(c.s.input_is_command(), "{text} is not highlighted");
        let mut c = Cmd::new();
        assert!(
            !matches!(c.run(text), Some(ChatIntent::Send(_))),
            "{text} must not go out as a message"
        );
    }
    // Near-words stay prose.
    for text in ["/profiles", "/profile-new"] {
        let mut c = Cmd::new();
        type_str(&mut c.s, text);
        assert!(!c.s.input_is_command(), "{text} must stay plain text");
    }
}

/// Every command family the input chain dispatches is highlighted while it is
/// being typed. One line per family rather than per form: what this guards is
/// the *drift* between the dispatch chain in `submit` and the predicate the
/// renderer asks — `/project` was dispatched but not highlighted, so the command
/// worked and looked like prose the whole time it was typed.
#[test]
fn every_command_family_is_highlighted_as_it_is_typed() {
    for text in [
        "/rag list",
        "/reindex",
        "/compact",
        "/file attach x.txt",
        "/project attach .",
        "/project status",
        "/project test-cmd cargo test",
        "/image list",
        "/tts off",
        "/settings",
        "/profile list",
        "/export md",
        "/exit",
    ] {
        let mut c = Cmd::new();
        type_str(&mut c.s, text);
        assert!(c.s.input_is_command(), "{text} is not highlighted");
        let mut c = Cmd::new();
        assert!(
            !matches!(c.run(text), Some(ChatIntent::Send(_))),
            "{text} must not go out as a message"
        );
    }
    // A malformed member of a family is still a command, not prose — the box and
    // `Enter` read it through the same parser.
    for text in ["/project", "/project nonsense", "/rag"] {
        let mut c = Cmd::new();
        type_str(&mut c.s, text);
        assert!(c.s.input_is_command(), "{text} is not highlighted");
    }
    // Near-words stay prose.
    for text in ["/projects", "/project-attach", "how do I attach a project?"] {
        let mut c = Cmd::new();
        type_str(&mut c.s, text);
        assert!(!c.s.input_is_command(), "{text} must stay plain text");
    }
}

/// The confirmation popup, rendered to text — the question has to be readable,
/// not merely present (the popup wraps at 56 columns).
fn confirm_popup_text(s: &mut ChatScreen) -> String {
    use ratatui::Terminal;
    use ratatui::backend::TestBackend;

    let mut term = Terminal::new(TestBackend::new(80, 24)).unwrap();
    term.draw(|f| s.render(f)).unwrap();
    let buf = term.backend().buffer();
    let mut out = String::new();
    for y in buf.area.top()..buf.area.bottom() {
        for x in buf.area.left()..buf.area.right() {
            out.push_str(buf[(x, y)].symbol());
        }
        out.push('\n');
    }
    out
}

/// A sub-agent transcript opens read-only (spec §11.2, docs/research/subagent-chats.md
/// §3.8): the persona heads the feed as a system bubble, sending and every
/// chat-changing chord and command refuse with a note that names the parent,
/// the commands that make sense in a transcript still work, and a transcript's
/// `chat://` address resolves.
mod read_only_transcript {
    use super::*;
    use crate::app::events::ChildView;
    use crate::entities::chat::{ChatSummary, ChildSummary};

    fn transcript() -> Cmd {
        let mut c = Cmd::new();
        let child = Uuid::from_u128(77);
        c.s.set_child_view(Some(ChildView {
            parent: c.chat,
            parent_title: "Про космос".into(),
            system_message: "Ты — критик.".into(),
        }));
        c.s.activate_chat(
            child,
            "Критик".into(),
            &[Message::user("Оцени X."), Message::assistant("X слаб.")],
            "",
            FeedView::default(),
            None,
            None,
        );
        c.chat = child;
        c
    }

    #[test]
    fn opens_with_the_persona_as_a_system_bubble() {
        let c = transcript();
        assert!(c.s.read_only());
        assert_eq!(c.s.feed[0].role, FeedRole::System);
        assert_eq!(c.s.feed[0].text, "Ты — критик.");
        assert_eq!(c.s.feed[1].role, FeedRole::User);
        assert_eq!(c.s.feed.len(), 3);
        // A chat, by contrast, never shows its system message.
        let plain = Cmd::new();
        assert!(!plain.s.read_only());
        assert!(plain.s.feed.iter().all(|m| m.role != FeedRole::System));
    }

    #[test]
    fn sending_is_refused_with_a_note_and_the_text_stays() {
        let mut c = transcript();
        assert_eq!(c.run("привет"), None);
        let note = c.last_note();
        assert!(note.contains("Про космос"), "{note}");
        assert_eq!(c.s.input.text(), "привет", "the line is not swallowed");
    }

    #[test]
    fn chat_changing_chords_and_commands_refuse_with_the_note() {
        for ch in ['r', 'e', 'u'] {
            let mut c = transcript();
            assert_eq!(c.ctrl(ch), None, "Ctrl+{ch}");
            assert!(
                c.last_note().contains("Про космос"),
                "Ctrl+{ch} said nothing"
            );
            assert!(
                c.s.confirm.is_none(),
                "Ctrl+{ch} must not open a confirmation"
            );
        }
        for line in [
            "/regen",
            "/takeback",
            "/impersonate",
            "/clone",
            "/compact",
            "/file attach x.txt",
            "/image attach x.png",
            "/project attach .",
        ] {
            let mut c = transcript();
            assert_eq!(c.run(line), None, "{line}");
            assert!(c.last_note().contains("Про космос"), "{line} said nothing");
            assert!(c.s.input.is_empty(), "{line} stays in the box");
        }
    }

    #[test]
    fn the_transcript_commands_still_work() {
        let mut c = transcript();
        let id = c.chat;
        assert_eq!(c.run("/copy"), Some(ChatIntent::CopyChat(id)));
        assert!(matches!(
            c.run("/rename Критик X"),
            Some(ChatIntent::RenameChat { id: r, title }) if r == id && title == "Критик X"
        ));
        assert!(matches!(
            c.run("/export md"),
            Some(ChatIntent::ExportChat { id: e, .. }) if e == id
        ));
        assert!(matches!(
            c.run("/search слаб"),
            Some(ChatIntent::SearchMessages { .. })
        ));
        assert_eq!(c.run("/chats"), Some(ChatIntent::OpenChatList));
        assert_eq!(c.key(KeyCode::Esc), Some(ChatIntent::OpenChatList));
    }

    #[test]
    fn status_and_input_title_say_read_only() {
        let c = transcript();
        let model = c.s.status_model(None, None, None);
        assert!(model.read_only);
        assert!(!Cmd::new().s.status_model(None, None, None).read_only);
    }

    #[test]
    fn a_transcript_address_resolves_from_the_list_snapshot() {
        let mut c = Cmd::new();
        let child_id = Uuid::from_u128(77);
        c.s.set_chat_list(vec![ChatSummary {
            id: c.chat,
            profile_id: Uuid::from_u128(2),
            title: "Про космос".into(),
            created_at: chrono::Utc::now(),
            modified_at: chrono::Utc::now(),
            message_count: 1,
            children: vec![ChildSummary {
                id: child_id,
                title: "Критик".into(),
                created_at: chrono::Utc::now(),
                finished_at: None,
                message_count: 2,
                outcome: None,
                running: false,
                background: false,
            }],
            children_expanded: false,
            unread: false,
        }]);
        let card = c.s.summary_card(child_id).expect("the transcript's card");
        assert_eq!(card.title, "Критик");
        assert_eq!(card.profile_id, Uuid::from_u128(2));
        assert!(c.s.feed_view.known_chats().contains(&child_id));
    }
}

/// The open transcript of a run out in the **background** (spec §9.3.2,
/// §11.2): the screen streams it like a turn child's, but there is no turn
/// behind it — `Esc` goes back to the list and leaves the run out, `F6` stops
/// it through the `/subagents stop` route, and both the key and its footer
/// hint exist only while the run streams.
mod background_transcript {
    use super::*;
    use crate::app::events::{ChildView, LivePartial, LiveTurn};
    use crate::entities::chat::ChildSummary;

    const RUN: u128 = 77;
    const STREAM: u128 = 88;

    /// A streaming transcript under the open chat — a background run's when
    /// `background`, a turn child's otherwise. The list snapshot carries the
    /// run as `running` the way the orchestrator reports it, which is where
    /// the stop route resolves the id.
    fn streaming(background: bool) -> Cmd {
        let mut c = Cmd::new();
        let run = Uuid::from_u128(RUN);
        let mut chat = card(c.chat, Uuid::from_u128(2), "Про космос");
        chat.children = vec![ChildSummary {
            id: run,
            title: "Критик".into(),
            created_at: chrono::Utc::now(),
            finished_at: None,
            message_count: 1,
            outcome: None,
            running: true,
            background,
        }];
        chat.children_expanded = true;
        c.s.set_chat_list(vec![chat]);
        c.s.set_child_view(Some(ChildView {
            parent: c.chat,
            parent_title: "Про космос".into(),
            system_message: "Ты — критик.".into(),
        }));
        c.s.activate_chat(
            run,
            "Критик".into(),
            &[Message::user("Оцени X.")],
            "",
            FeedView::default(),
            None,
            None,
        );
        c.s.set_live_turn(Some(Box::new(LiveTurn {
            turn: Uuid::new_v4(),
            stream: Uuid::from_u128(STREAM),
            role: MessageRole::Assistant,
            partial: Some(LivePartial::default()),
            continues: false,
            background,
        })));
        c.chat = run;
        c
    }

    #[test]
    fn esc_goes_back_and_f6_stops_the_run() {
        let mut c = streaming(true);
        assert!(c.s.generating, "the run's stream is live in this feed");
        let model = c.s.status_model(None, None, None);
        assert!(model.background_run && model.read_only);
        assert_eq!(c.key(KeyCode::Esc), Some(ChatIntent::OpenChatList));
        assert_eq!(
            c.key(KeyCode::F(6)),
            Some(ChatIntent::StopSubagentRun {
                id: Uuid::from_u128(RUN)
            })
        );
        assert!(c.last_note().contains("Критик"), "{}", c.last_note());
    }

    #[test]
    fn once_the_run_ends_the_key_stops_nothing_and_the_hint_goes() {
        let mut c = streaming(true);
        c.s.finish_generation(Uuid::from_u128(STREAM), FinishReason::Cancelled, false);
        assert!(!c.s.generating);
        assert!(!c.s.status_model(None, None, None).background_run);
        assert_eq!(c.key(KeyCode::F(6)), None);
        assert_eq!(c.key(KeyCode::Esc), Some(ChatIntent::OpenChatList));
    }

    /// A turn child's transcript is the turn's: `Esc` cancels it, as on the
    /// parent, and the stop key is not there.
    #[test]
    fn a_turn_childs_transcript_keeps_esc_as_cancel_and_has_no_stop_key() {
        let mut c = streaming(false);
        assert!(c.s.generating);
        assert!(!c.s.status_model(None, None, None).background_run);
        assert_eq!(c.key(KeyCode::Esc), Some(ChatIntent::Cancel));
        assert_eq!(c.key(KeyCode::F(6)), None);
    }

    #[test]
    fn f6_on_a_chat_does_nothing_idle_or_generating() {
        let mut c = Cmd::new();
        assert_eq!(c.key(KeyCode::F(6)), None);
        c.s.begin_generation(gen_id(), None);
        assert!(!c.s.status_model(None, None, None).background_run);
        assert_eq!(c.key(KeyCode::F(6)), None);
        assert_eq!(c.key(KeyCode::Esc), Some(ChatIntent::Cancel));
    }

    /// The rendered footer says what the keys do here — `F6` and a
    /// navigation `Esc` — and drops the stop hint the moment the run ends
    /// (spec §11.2: an advertised key that is a no-op is worse than none).
    #[test]
    fn the_footer_advertises_f6_only_while_the_run_streams() {
        use ratatui::Terminal;
        use ratatui::backend::TestBackend;
        let mut c = streaming(true);
        c.s.set_server_status(ready_statuses());
        let mut term = Terminal::new(TestBackend::new(160, 20)).unwrap();
        let text = |c: &mut Cmd, term: &mut Terminal<TestBackend>| {
            term.draw(|f| c.s.render(f)).unwrap();
            let buf = term.backend().buffer();
            (0..buf.area.height)
                .map(|y| {
                    (0..buf.area.width)
                        .map(|x| buf[(x, y)].symbol().to_string())
                        .collect::<String>()
                })
                .collect::<Vec<_>>()
                .join("\n")
        };
        let live = text(&mut c, &mut term);
        assert!(live.contains("F6"), "{live}");
        assert!(
            live.contains(c.s.loc.t("ui.status.hotkey.stop_run")),
            "{live}"
        );
        assert!(
            !live.contains(c.s.loc.t("ui.status.hotkey.cancel")),
            "{live}"
        );
        c.s.finish_generation(Uuid::from_u128(STREAM), FinishReason::Stop, false);
        let ended = text(&mut c, &mut term);
        assert!(!ended.contains("F6"), "{ended}");
    }
}

/// Stage 3 of the command-only track (docs/history/commands-stage3.md): `/autotitle`,
/// the `/profile` text subcommands, and the `/impersonation` family.
mod stage3_commands {
    use super::*;
    use crate::features::profiles::ProfileEdit;
    use crate::shared::config::ImpersonationProfile;

    /// [`Cmd::new`] plus the state the stage-3 commands read: the open chat is
    /// in the list and belongs to a full profile the settings snapshot
    /// carries, and the config holds two personas — the first one linked.
    fn staffed() -> Cmd {
        let mut c = Cmd::new();
        let mut gaia = Profile::new("Гайя", "Ты — Гайя.");
        gaia.id = Uuid::from_u128(2);
        gaia.greeting = Some("Привет!".into());
        gaia.impersonation_profile_id = Some(Uuid::from_u128(10));
        let mut helios = Profile::new("Гелиос", "");
        helios.id = Uuid::from_u128(3);
        let cfg = AppConfig {
            impersonation_profiles: vec![
                ImpersonationProfile {
                    id: Uuid::from_u128(10),
                    name: "Владимир".into(),
                    system_message: "Ты — Владимир.".into(),
                },
                ImpersonationProfile {
                    id: Uuid::from_u128(11),
                    name: "Вера".into(),
                    system_message: String::new(),
                },
            ],
            ..Default::default()
        };
        c.s.set_settings(
            cfg,
            vec![gaia, helios],
            Vec::new(),
            Default::default(),
            Vec::new(),
        );
        c.s.set_chat_list(vec![card(c.chat, Uuid::from_u128(2), "Про космос")]);
        c
    }

    /// The same screen with the chat on the *other* profile — no greeting, an
    /// empty system message, no persona linked.
    fn staffed_on_helios() -> Cmd {
        let mut c = staffed();
        let chat = Uuid::from_u128(5);
        c.s.set_chat_list(vec![card(chat, Uuid::from_u128(3), "Второй")]);
        c.s.activate_chat(
            chat,
            "Второй".into(),
            &[],
            "",
            FeedView::default(),
            None,
            None,
        );
        c.chat = chat;
        c
    }

    #[test]
    fn autotitle_asks_the_model_and_answers_without_a_chat() {
        let mut c = Cmd::new();
        let chat = c.chat;
        assert_eq!(c.run("/autotitle"), Some(ChatIntent::AutoTitleChat(chat)));
        // The task takes seconds; a silent wait would read as a refusal.
        assert!(!c.last_note().is_empty(), "/autotitle said nothing");

        let mut c = Cmd::bare();
        assert_eq!(c.run("/autotitle"), None);
        assert!(!c.last_note().is_empty(), "no-chat /autotitle said nothing");
    }

    #[test]
    fn profile_text_commands_edit_the_active_profile() {
        // Set / clear, both fields; the intent goes to the chat's profile and
        // the note names it.
        let cases: [(&str, ProfileEdit); 4] = [
            (
                "/profile system Ты — Гея 2.0.",
                ProfileEdit {
                    system_message: Some("Ты — Гея 2.0.".into()),
                    ..Default::default()
                },
            ),
            (
                "/profile system clear",
                ProfileEdit {
                    system_message: Some(String::new()),
                    ..Default::default()
                },
            ),
            (
                "/profile greeting Здравствуй!",
                ProfileEdit {
                    greeting: Some(Some("Здравствуй!".into())),
                    ..Default::default()
                },
            ),
            (
                "/profile greeting clear",
                ProfileEdit {
                    greeting: Some(None),
                    ..Default::default()
                },
            ),
        ];
        for (line, expected) in cases {
            let mut c = staffed();
            let Some(ChatIntent::UpdateProfile { id, edit }) = c.run(line) else {
                panic!("{line}: expected an UpdateProfile intent");
            };
            assert_eq!(id, Uuid::from_u128(2), "{line}: the chat's profile");
            assert_eq!(*edit, expected, "{line}");
            assert!(c.last_note().contains("Гайя"), "{line}: {}", c.last_note());
            assert!(c.s.input.is_empty(), "{line} left text in the box");
        }
    }

    #[test]
    fn bare_profile_text_prefills_the_current_value_or_teaches() {
        let mut c = staffed();
        assert_eq!(c.run("/profile system"), None);
        assert_eq!(c.s.input.text(), "/profile system Ты — Гайя.");

        let mut c = staffed();
        assert_eq!(c.run("/profile greeting"), None);
        assert_eq!(c.s.input.text(), "/profile greeting Привет!");

        // Nothing set: an empty prefill would teach nothing — the note names
        // the syntax that sets one instead.
        let mut c = staffed_on_helios();
        assert_eq!(c.run("/profile system"), None);
        let note = c.last_note();
        assert!(note.contains("/profile system"), "{note}");
        assert!(c.s.input.is_empty());

        let mut c = staffed_on_helios();
        assert_eq!(c.run("/profile greeting"), None);
        assert!(c.last_note().contains("/profile greeting"));
    }

    #[test]
    fn profile_text_without_a_chat_or_snapshot_answers() {
        let mut c = Cmd::bare();
        assert_eq!(c.run("/profile system Текст"), None);
        assert!(!c.last_note().is_empty(), "no-chat edit said nothing");

        // `Cmd::new` has no chat-list snapshot, so the profile is unknowable.
        let mut c = Cmd::new();
        assert_eq!(c.run("/profile greeting Текст"), None);
        assert!(!c.last_note().is_empty());
    }

    #[test]
    fn impersonation_list_marks_the_used_persona() {
        let mut c = staffed();
        assert_eq!(c.run("/impersonation list"), None);
        let note = c.last_note();
        let marker = c.s.palette.glyphs().title_marker;
        assert!(
            note.contains(&format!("{marker} Владимир")),
            "the linked persona is marked: {note}"
        );
        assert!(note.contains("Вера"), "{note}");

        // No personas at all → the route that creates one, not an empty list.
        let mut c = Cmd::new();
        assert_eq!(c.run("/impersonation list"), None);
        assert!(
            c.last_note().contains("/impersonation new"),
            "{}",
            c.last_note()
        );
    }

    #[test]
    fn impersonation_new_creates_and_names_the_next_steps() {
        let mut c = staffed();
        let Some(ChatIntent::UpdateConfig(config)) = c.run("/impersonation new Тень") else {
            panic!("expected an UpdateConfig intent");
        };
        let created = config.impersonation_profiles.last().unwrap();
        assert_eq!(config.impersonation_profiles.len(), 3);
        assert_eq!(created.name, "Тень");
        assert!(created.system_message.is_empty());
        let note = c.last_note();
        assert!(note.contains("/impersonation use"), "the next step: {note}");
        assert!(
            note.contains("/impersonation system"),
            "the next step: {note}"
        );

        // The default name — the settings screen's `Ctrl+N` one.
        let mut c = staffed();
        let Some(ChatIntent::UpdateConfig(config)) = c.run("/impersonation new") else {
            panic!("expected an UpdateConfig intent");
        };
        let name = &config.impersonation_profiles.last().unwrap().name;
        assert_eq!(name, c.s.loc.t("ui.settings.new_profile_name"));

        // Before the snapshot arrives, the command answers instead of acting.
        let mut c = Cmd::bare();
        assert_eq!(c.run("/impersonation new Тень"), None);
        assert!(!c.last_note().is_empty());
    }

    #[test]
    fn impersonation_delete_always_confirms_and_enter_commits() {
        let mut c = staffed();
        assert_eq!(c.run("/impersonation delete Влад"), None);
        assert!(
            matches!(
                &c.s.confirm,
                Some(ConfirmAction::DeleteImpersonation { id, name })
                    if *id == Uuid::from_u128(10) && name == "Владимир"
            ),
            "the prefix resolves and the popup opens"
        );
        let Some(ChatIntent::UpdateConfig(config)) = c.key(KeyCode::Enter) else {
            panic!("Enter must commit the deletion");
        };
        let left: Vec<&str> = config
            .impersonation_profiles
            .iter()
            .map(|p| p.name.as_str())
            .collect();
        assert_eq!(left, ["Вера"]);
        assert!(c.last_note().contains("Владимир"), "{}", c.last_note());

        // Esc declines — nothing changes and nothing is sent.
        let mut c = staffed();
        assert_eq!(c.run("/impersonation delete Вера"), None);
        assert_eq!(c.key(KeyCode::Esc), None);
        assert!(c.s.confirm.is_none());
    }

    #[test]
    fn impersonation_use_links_and_default_unlinks() {
        let mut c = staffed();
        let Some(ChatIntent::UpdateProfile { id, edit }) = c.run("/impersonation use Вера")
        else {
            panic!("expected an UpdateProfile intent");
        };
        assert_eq!(id, Uuid::from_u128(2));
        assert_eq!(
            edit.impersonation_profile_id,
            Some(Some(Uuid::from_u128(11)))
        );

        let mut c = staffed();
        let Some(ChatIntent::UpdateProfile { edit, .. }) = c.run("/impersonation use default")
        else {
            panic!("expected an UpdateProfile intent");
        };
        assert_eq!(edit.impersonation_profile_id, Some(None));

        // A miss lists what there is and the route that shows it.
        let mut c = staffed();
        assert_eq!(c.run("/impersonation use Прометей"), None);
        let note = c.last_note();
        assert!(note.contains("Владимир") && note.contains("Вера"), "{note}");
        assert!(note.contains("/impersonation list"), "{note}");
    }

    #[test]
    fn impersonation_system_edits_the_linked_persona() {
        let mut c = staffed();
        let Some(ChatIntent::UpdateConfig(config)) = c.run("/impersonation system Новый текст")
        else {
            panic!("expected an UpdateConfig intent");
        };
        assert_eq!(
            config.impersonation_profiles[0].system_message,
            "Новый текст"
        );
        assert!(c.last_note().contains("Владимир"), "{}", c.last_note());

        // Bare — the current text comes back as an editable command line.
        let mut c = staffed();
        assert_eq!(c.run("/impersonation system"), None);
        assert_eq!(c.s.input.text(), "/impersonation system Ты — Владимир.");

        // `clear` empties it — impersonation falls back to the default text.
        let mut c = staffed();
        let Some(ChatIntent::UpdateConfig(config)) = c.run("/impersonation system clear") else {
            panic!("expected an UpdateConfig intent");
        };
        assert!(config.impersonation_profiles[0].system_message.is_empty());

        // No persona linked: the note names both routes that give the profile
        // one, instead of editing something invisible.
        let mut c = staffed_on_helios();
        assert_eq!(c.run("/impersonation system Текст"), None);
        let note = c.last_note();
        assert!(note.contains("/impersonation use"), "{note}");
        assert!(note.contains("/impersonation new"), "{note}");
    }

    /// The box and the highlighting: the family clears the box like every other
    /// command, and is colored as a command while typed — including malformed
    /// spellings, which are commands and not prose.
    #[test]
    fn impersonation_commands_clear_the_box_and_highlight() {
        for line in [
            "/impersonation list",
            "/impersonation new Тень",
            "/impersonation use Вера",
            "/impersonation system Текст",
            "/profile system Текст",
            "/profile greeting Текст",
        ] {
            let mut c = staffed();
            c.run(line);
            assert!(c.s.input.is_empty(), "{line} left text in the box");
            assert!(
                c.s.take_dirty_draft().is_some_and(|d| d.is_empty()),
                "{line} must hand an empty draft back"
            );
        }
        for text in [
            "/impersonation list",
            "/impersonation renam x",
            "/profile system",
            "/profile greeting clear",
        ] {
            let mut c = staffed();
            type_str(&mut c.s, text);
            assert!(c.s.input_is_command(), "{text} is not highlighted");
        }
        // Near-words stay prose, exactly as they are on Enter.
        let mut c = staffed();
        type_str(&mut c.s, "/impersonations list");
        assert!(!c.s.input_is_command());
    }
}

/// `/subagents` (spec §11.2): the chat list's `Ctrl+O` for the open chat —
/// flips the stored fold and answers **which way and where**, since the rows
/// it moves live on another screen.
mod subagents_command {
    use super::*;
    use crate::app::events::ChildView;
    use crate::entities::chat::ChildSummary;
    use crate::entities::subagent::SubagentRun;

    /// [`Cmd::new`] with the open chat in the list snapshot, carrying one
    /// transcript and the given stored fold.
    fn with_transcript(expanded: bool) -> Cmd {
        let mut c = Cmd::new();
        let mut chat = card(c.chat, Uuid::from_u128(2), "Про космос");
        chat.children = vec![ChildSummary::of(&SubagentRun::fixture(
            "Критик",
            &["Оцени X.", "X слаб."],
        ))];
        chat.children_expanded = expanded;
        c.s.set_chat_list(vec![chat]);
        c
    }

    #[test]
    fn subagents_toggles_the_stored_fold_and_says_which_way() {
        let mut c = with_transcript(false);
        let chat = c.chat;
        assert_eq!(
            c.run("/subagents"),
            Some(ChatIntent::SetChildrenExpanded {
                id: chat,
                expanded: true
            })
        );
        assert_eq!(c.last_note(), c.s.loc.t("ui.cmd.subagents_expanded"));

        let mut c = with_transcript(true);
        let chat = c.chat;
        assert_eq!(
            c.run("/subagents"),
            Some(ChatIntent::SetChildrenExpanded {
                id: chat,
                expanded: false
            })
        );
        assert_eq!(c.last_note(), c.s.loc.t("ui.cmd.subagents_collapsed"));
        assert!(c.s.input.is_empty(), "the command must clear the box");
    }

    /// The two words set the state outright — the hand that knows what it
    /// wants without checking first. Idempotent: `expand` on an expanded chat
    /// still answers, and the orchestrator no-ops on the equal value.
    #[test]
    fn explicit_subcommands_set_the_state_outright() {
        let mut c = with_transcript(true);
        let chat = c.chat;
        assert_eq!(
            c.run("/subagents expand"),
            Some(ChatIntent::SetChildrenExpanded {
                id: chat,
                expanded: true
            })
        );
        assert_eq!(c.last_note(), c.s.loc.t("ui.cmd.subagents_expanded"));

        let mut c = with_transcript(false);
        let chat = c.chat;
        assert_eq!(
            c.run("/subagents collapse"),
            Some(ChatIntent::SetChildrenExpanded {
                id: chat,
                expanded: false
            })
        );
        // A word outside the set is the parser's to report, with the usage
        // line — not silence, and not a message sent to the model.
        let mut c = with_transcript(false);
        assert_eq!(c.run("/subagents wide"), None);
        let note = c.last_note();
        assert!(note.contains("wide"), "{note}");
        assert!(note.contains("/subagents"), "{note}");
    }

    #[test]
    fn subagents_works_from_an_open_transcript_and_folds_the_parent() {
        // Read-only refuses what would change the conversation; folding the
        // parent's list changes neither, so the typed route stays open — and
        // the state it flips is the parent's.
        let mut c = with_transcript(true);
        let parent = c.chat;
        let child = c.s.chats[0].children[0].id;
        c.s.set_child_view(Some(ChildView {
            parent,
            parent_title: "Про космос".into(),
            system_message: "Ты — критик.".into(),
        }));
        c.s.activate_chat(
            child,
            "Критик".into(),
            &[],
            "",
            FeedView::default(),
            None,
            None,
        );
        c.chat = child;
        assert!(c.s.read_only());
        assert_eq!(
            c.run("/subagents"),
            Some(ChatIntent::SetChildrenExpanded {
                id: parent,
                expanded: false
            })
        );
    }

    #[test]
    fn subagents_without_transcripts_or_chat_answers() {
        // A chat with none: the note says where the rows come from, instead
        // of toggling something invisible (docs/lessons.md §4).
        let mut c = Cmd::new();
        c.s.set_chat_list(vec![card(c.chat, Uuid::from_u128(2), "Про космос")]);
        assert_eq!(c.run("/subagents"), None);
        assert_eq!(c.last_note(), c.s.loc.t("ui.cmd.subagents_none"));

        // Before the list snapshot arrives the answer is the same — the
        // screen knows of no transcripts to fold.
        let mut c = Cmd::new();
        assert_eq!(c.run("/subagents"), None);
        assert_eq!(c.last_note(), c.s.loc.t("ui.cmd.subagents_none"));

        // No chat at all.
        let mut c = Cmd::bare();
        assert_eq!(c.run("/subagents"), None);
        assert_eq!(c.last_note(), c.s.loc.t("ui.cmd.no_chat"));
    }
}

// ---------- /continue on the screen (spec §6.4) ----------

/// A continuation streams into the bubble it resumes: same bubble, no
/// separator, mid-word seam intact — the `"\n\n"` round-stitching must not
/// fire between the partial and its continuation.
#[test]
fn continuation_streams_into_the_resumed_bubble() {
    let mut s = ChatScreen::new();
    let id = Uuid::new_v4();
    let messages = vec![Message::user("вопрос"), Message::assistant("Нача")];
    s.activate_chat(
        id,
        "Чат".into(),
        &messages,
        "",
        FeedView::default(),
        None,
        None,
    );
    let g = gen_id();
    s.begin_continuation(g, None);
    s.push_chunk(g, "ло");
    let assistants: Vec<_> = s
        .feed
        .iter()
        .filter(|m| m.role == FeedRole::Assistant)
        .collect();
    assert_eq!(assistants.len(), 1, "no second bubble may open");
    assert_eq!(assistants[0].text, "Начало", "joined with no separator");
    assert!(assistants[0].streaming);
}

/// A note that landed after the partial (the "cut short" explanation) moves
/// above the resumed reply rather than splitting it: the bubble is re-opened
/// **past** the note, and the stream lands in the bubble, not the note.
#[test]
fn continuation_reopens_the_bubble_past_a_trailing_note() {
    let mut s = ChatScreen::new();
    let id = Uuid::new_v4();
    let messages = vec![Message::user("вопрос"), Message::assistant("Нача")];
    s.activate_chat(
        id,
        "Чат".into(),
        &messages,
        "",
        FeedView::default(),
        None,
        None,
    );
    s.push_note("ответ оборван");
    let g = gen_id();
    s.begin_continuation(g, None);
    s.push_chunk(g, "ло");
    let last = s.feed.last().unwrap();
    assert_eq!(last.role, FeedRole::Assistant);
    assert_eq!(last.text, "Начало");
    assert!(
        s.feed.iter().any(|m| m.role == FeedRole::Note),
        "the note stays, above the reply it described"
    );
}

/// The `Length` notes exist at all only since `/continue` made them actionable
/// (spec §6.4), and each names the route that works in the current mode
/// (fork F9): the resuming one when the mode can resume, the regenerating one
/// when it cannot.
#[test]
fn a_length_cut_gets_the_note_its_mode_deserves() {
    let loc = crate::shared::i18n::locale(crate::shared::i18n::Lang::default());
    for (continuable, key) in [
        (true, "ui.err.reply_truncated_continuable"),
        (false, "ui.err.reply_truncated"),
    ] {
        let mut s = ChatScreen::new();
        let g = gen_id();
        s.begin_generation(g, None);
        s.push_chunk(g, "полответа");
        s.finish_generation(g, FinishReason::Length, continuable);
        let last = s.feed.last().unwrap();
        assert_eq!(last.role, FeedRole::Note);
        assert_eq!(last.text, loc.t(key), "continuable={continuable}");
    }
}

/// A reply the provider's content filter stopped used to end with nothing on screen
/// saying so — the fragment read as a complete answer. It keeps the fragment and
/// gains one note, whatever `continuable` claims: resuming meets the same filter
/// (docs/research/content-filter-finish.md).
#[test]
fn a_filtered_reply_keeps_its_fragment_and_says_why() {
    let loc = crate::shared::i18n::locale(crate::shared::i18n::Lang::default());
    for continuable in [false, true] {
        let mut s = ChatScreen::new();
        let g = gen_id();
        s.begin_generation(g, None);
        s.push_chunk(g, "The first half");
        s.finish_generation(g, FinishReason::Filtered, continuable);
        let n = s.feed.len();
        assert_eq!(s.feed[n - 1].role, FeedRole::Note);
        assert_eq!(
            s.feed[n - 1].text,
            loc.t("ui.err.reply_filtered"),
            "continuable={continuable}"
        );
        assert_eq!(s.feed[n - 2].role, FeedRole::Assistant);
        assert_eq!(s.feed[n - 2].text, "The first half");
    }
}

/// The cancel note names `/continue` only when it would actually work.
#[test]
fn the_cancel_note_names_continue_only_when_it_works() {
    let loc = crate::shared::i18n::locale(crate::shared::i18n::Lang::default());
    for (continuable, key) in [
        (true, "ui.chat.gen_cancelled_continuable"),
        (false, "ui.chat.gen_cancelled"),
    ] {
        let mut s = ChatScreen::new();
        let g = gen_id();
        s.begin_generation(g, None);
        s.push_chunk(g, "полотв");
        s.finish_generation(g, FinishReason::Cancelled, continuable);
        assert_eq!(
            s.feed.last().unwrap().text,
            loc.t(key),
            "continuable={continuable}"
        );
    }
}

/// A mid-continuation switch away and back: the rebuilt feed seeds the live
/// partial **into** the bubble being continued (`LiveTurn::continues`), not
/// into a bubble of its own.
#[test]
fn a_rebuild_mid_continuation_appends_the_partial_into_the_seed_bubble() {
    let mut s = ChatScreen::new();
    let id = Uuid::new_v4();
    let generation = gen_id();
    let messages = vec![Message::user("вопрос"), Message::assistant("Нача")];
    s.activate_chat(
        id,
        "Чат".into(),
        &messages,
        "",
        FeedView::default(),
        None,
        None,
    );
    s.set_live_turn(Some(Box::new(crate::app::events::LiveTurn {
        role: MessageRole::Assistant,
        turn: generation,
        stream: generation,
        partial: Some(crate::app::events::LivePartial {
            text: "ло".into(),
            thoughts: String::new(),
            tools: Vec::new(),
        }),
        continues: true,
        background: false,
    })));
    assert!(s.generating);
    let assistants: Vec<_> = s
        .feed
        .iter()
        .filter(|m| m.role == FeedRole::Assistant)
        .collect();
    assert_eq!(assistants.len(), 1, "the partial joins the seed bubble");
    assert_eq!(assistants[0].text, "Начало");
    assert!(assistants[0].streaming);
}

/// The title's caption in `external` mode: a name in settings wins, a blank
/// field falls back to what the engine said it is running
/// (`AppEvent::EngineModel`), and neither still means no caption at all — the
/// header drawn before the engine was ever asked
/// (docs/research/external-model-name.md §4).
#[test]
fn the_caption_prefers_settings_and_falls_back_to_the_engine() {
    use crate::shared::config::{AppConfig, ServerMode};
    let settings = |s: &mut ChatScreen, named: Option<&str>| {
        let mut cfg = AppConfig::default();
        cfg.engine.mode = ServerMode::External;
        cfg.engine.external.url = Some("http://127.0.0.1:8000/v1".into());
        cfg.engine.external.model_name = named.map(String::from);
        s.set_settings(cfg, Vec::new(), Vec::new(), Default::default(), Vec::new());
    };

    let mut s = ChatScreen::new();
    settings(&mut s, None);
    assert_eq!(s.model_meta(), "", "nothing configured, nothing discovered");

    s.set_engine_model(Some("gemma-4-31B_q4_0-it".into()));
    assert_eq!(s.model_meta(), "gemma-4-31B_q4_0-it");

    settings(&mut s, Some("qwen-3.6-27b"));
    assert_eq!(
        s.model_meta(),
        "qwen-3.6-27b",
        "a name the user typed outranks the server's opinion"
    );

    // The engine was replaced: the caption is cleared at once rather than
    // keeping the previous server's model.
    settings(&mut s, None);
    s.set_engine_model(None);
    assert_eq!(s.model_meta(), "");
}

/// Several runs at once (spec §9.3.2): the chip counts them and names the
/// latest report; a run's `None` drops its line; one run left is one line.
#[test]
fn the_chip_counts_several_running_runs_and_names_the_latest() {
    use crate::app::events::{RunProgressKind, SubagentProgress};
    let mut s = ChatScreen::new();
    let generation = gen_id();
    s.begin_generation(generation, None);
    let at = |name: &str| {
        Some(SubagentProgress {
            kind: RunProgressKind::Subagent,
            name: name.into(),
            round: 1,
            tool: None,
        })
    };
    let (a, b) = (gen_id(), gen_id());
    s.set_subagent_progress(generation, a, at("Критик"));
    let one = s.background_hint().unwrap();
    assert!(one.contains("Критик") && !one.contains('2'), "{one}");
    s.set_subagent_progress(generation, b, at("Поэт"));
    let two = s.background_hint().unwrap();
    assert!(
        two.contains('2') && two.contains("Поэт") && !two.contains("Критик"),
        "{two}"
    );
    // A's next report makes it the latest again.
    s.set_subagent_progress(generation, a, at("Критик"));
    let again = s.background_hint().unwrap();
    assert!(again.contains('2') && again.contains("Критик"), "{again}");
    // B ends: one line, no count.
    s.set_subagent_progress(generation, b, None);
    let back = s.background_hint().unwrap();
    assert!(back.contains("Критик") && !back.contains("Поэт"), "{back}");
    s.set_subagent_progress(generation, a, None);
    assert!(!s.background_hint().unwrap_or_default().contains("Критик"));
}

// ---------- /tasks stop <kind>: the typed route to F6 on a task row ----------

/// `/tasks [stop <kind>]` (docs/research/tasks-stop-command.md §3.2): the
/// named task ends in the very intent the tasks screen's `F6` sends when the
/// screen's own flag says it is running, and every other shape leaves a note
/// that names a route — the idle task, no word while several run, a word the
/// command does not know. The note names the task with the tasks screen's
/// word for it, in the interface language.
mod tasks_stop {
    use super::*;
    use crate::app::events::BackgroundKind;
    use crate::shared::i18n::{Lang, locale};

    fn set_running(c: &mut Cmd, kind: BackgroundKind, on: bool) {
        match kind {
            BackgroundKind::Reflection => c.s.set_reflecting(on),
            BackgroundKind::Consolidation => c.s.set_consolidating(on),
            BackgroundKind::SelfConsolidation => c.s.set_self_consolidating(on),
            BackgroundKind::Compaction => c.s.set_compacting(on),
        }
    }

    #[test]
    fn bare_tasks_is_still_the_screen() {
        let mut c = Cmd::new();
        assert_eq!(c.run("/tasks"), Some(ChatIntent::OpenTasks));
    }

    #[test]
    fn a_running_kind_is_stopped_and_named() {
        let mut c = Cmd::new();
        set_running(&mut c, BackgroundKind::Reflection, true);
        assert_eq!(
            c.run("/tasks stop reflection"),
            Some(ChatIntent::StopBackgroundTasks {
                kinds: vec![BackgroundKind::Reflection]
            })
        );
        let note = c.last_note();
        assert!(
            note.contains(c.s.loc.t("ui.tasks.app.reflection")),
            "the note names the task: {note}"
        );
    }

    /// Each word reaches its kind, whatever the case it was typed in.
    #[test]
    fn every_word_maps_to_its_kind_without_case() {
        for (word, kind) in [
            ("reflection", BackgroundKind::Reflection),
            ("NOTES", BackgroundKind::Consolidation),
            ("Self", BackgroundKind::SelfConsolidation),
            ("compact", BackgroundKind::Compaction),
        ] {
            let mut c = Cmd::new();
            set_running(&mut c, kind, true);
            assert_eq!(
                c.run(&format!("/tasks stop {word}")),
                Some(ChatIntent::StopBackgroundTasks { kinds: vec![kind] }),
                "word {word:?}"
            );
        }
    }

    /// An idle task is not "stopped": the note says so and names the screen
    /// that shows what is running (docs/lessons.md §4).
    #[test]
    fn an_idle_kind_is_refused_with_the_route() {
        let mut c = Cmd::new();
        assert_eq!(c.run("/tasks stop compact"), None);
        let note = c.last_note();
        assert!(
            note.contains(c.s.loc.t("ui.tasks.app.compaction")) && note.contains("/tasks"),
            "{note}"
        );
    }

    /// Bare `stop` mirrors `/subagents stop`: the only running task is the one.
    #[test]
    fn bare_stop_takes_the_only_running_task() {
        let mut c = Cmd::new();
        set_running(&mut c, BackgroundKind::Compaction, true);
        assert_eq!(
            c.run("/tasks stop"),
            Some(ChatIntent::StopBackgroundTasks {
                kinds: vec![BackgroundKind::Compaction]
            })
        );
    }

    /// …and with several running it lists their words, so the answer is the
    /// next command rather than a guess.
    #[test]
    fn bare_stop_with_several_running_lists_their_words() {
        let mut c = Cmd::new();
        set_running(&mut c, BackgroundKind::Reflection, true);
        set_running(&mut c, BackgroundKind::Compaction, true);
        assert_eq!(c.run("/tasks stop"), None);
        let note = c.last_note();
        assert!(
            note.contains("/tasks stop")
                && note.contains("reflection")
                && note.contains("compact")
                && !note.contains("notes"),
            "{note}"
        );
    }

    #[test]
    fn bare_stop_with_none_running_says_so() {
        let mut c = Cmd::new();
        assert_eq!(c.run("/tasks stop"), None);
        let note = c.last_note();
        assert!(note.contains("/tasks"), "{note}");
    }

    /// A word outside the set is answered with the whole set.
    #[test]
    fn an_unknown_word_names_the_four() {
        let mut c = Cmd::new();
        set_running(&mut c, BackgroundKind::Reflection, true);
        assert_eq!(c.run("/tasks stop foo"), None);
        let note = c.last_note();
        for word in ["foo", "reflection", "notes", "self", "compact"] {
            assert!(note.contains(word), "{word:?} missing from: {note}");
        }
    }

    /// The tasks are the app's, not a chat's: the route works with no chat
    /// open, as `/tasks` itself does.
    #[test]
    fn the_route_needs_no_open_chat() {
        let mut c = Cmd::bare();
        set_running(&mut c, BackgroundKind::Consolidation, true);
        assert_eq!(
            c.run("/tasks stop notes"),
            Some(ChatIntent::StopBackgroundTasks {
                kinds: vec![BackgroundKind::Consolidation]
            })
        );
    }

    /// Per-locale gate (docs/history/i18n-ui.md §3.5): every note renders under
    /// every bundled language with no placeholder left, and each names a route.
    #[test]
    fn every_note_renders_in_both_locales() {
        for &lang in Lang::ALL {
            let loc = locale(lang);
            for (key, args) in [
                ("ui.cmd.tasks_stopping", vec![("task", "x")]),
                ("ui.cmd.tasks_stopping_all", vec![("tasks", "x")]),
                ("ui.cmd.tasks_not_running", vec![("task", "x")]),
                ("ui.cmd.tasks_none_running", vec![]),
                ("ui.cmd.tasks_stop_which", vec![("kinds", "a · b")]),
                (
                    "ui.cmd.tasks_bad_kind",
                    vec![("arg", "foo"), ("kinds", "a · b")],
                ),
            ] {
                let text = loc.tf(key, &args);
                assert!(
                    !text.contains('{') && !text.contains('}'),
                    "unsubstituted placeholder in {lang:?} {key}: {text}"
                );
                if !key.starts_with("ui.cmd.tasks_stopping") {
                    assert!(
                        text.contains("/tasks"),
                        "{lang:?} {key} names no route: {text}"
                    );
                }
            }
            assert!(loc.t("ui.help.k.tasks").starts_with("/tasks [stop"));
        }
    }

    // ---- /tasks stop all (docs/research/tasks-stop-all.md) ----

    /// `all` takes every running task in the table's order, in one intent,
    /// and one note names them all by the tasks screen's words.
    #[test]
    fn all_stops_every_running_task_in_the_tables_order() {
        let mut c = Cmd::new();
        set_running(&mut c, BackgroundKind::Compaction, true);
        set_running(&mut c, BackgroundKind::Reflection, true);
        assert_eq!(
            c.run("/tasks stop all"),
            Some(ChatIntent::StopBackgroundTasks {
                kinds: vec![BackgroundKind::Reflection, BackgroundKind::Compaction]
            })
        );
        let note = c.last_note();
        let loc = c.s.loc;
        assert!(
            note.contains(loc.t("ui.tasks.app.reflection"))
                && note.contains(loc.t("ui.tasks.app.compaction"))
                && !note.contains(loc.t("ui.tasks.app.consolidation")),
            "{note}"
        );
    }

    /// `all` with one task running is the same path with one name; the word
    /// is matched without case.
    #[test]
    fn all_with_one_running_names_that_one() {
        let mut c = Cmd::new();
        set_running(&mut c, BackgroundKind::Consolidation, true);
        assert_eq!(
            c.run("/tasks stop ALL"),
            Some(ChatIntent::StopBackgroundTasks {
                kinds: vec![BackgroundKind::Consolidation]
            })
        );
        let note = c.last_note();
        assert!(
            note.contains(c.s.loc.t("ui.tasks.app.consolidation")),
            "{note}"
        );
    }

    #[test]
    fn all_with_none_running_says_so() {
        let mut c = Cmd::new();
        assert_eq!(c.run("/tasks stop all"), None);
        let note = c.last_note();
        assert!(note.contains("/tasks"), "{note}");
    }

    /// The two notes that list the kinds name `all` as the route for the whole
    /// set (lessons §4: an answer that lists the choices and omits the one that
    /// takes them all is a dead end).
    #[test]
    fn the_kind_notes_name_all() {
        let mut c = Cmd::new();
        set_running(&mut c, BackgroundKind::Reflection, true);
        set_running(&mut c, BackgroundKind::Compaction, true);
        assert_eq!(c.run("/tasks stop"), None);
        assert!(c.last_note().contains("all"), "{}", c.last_note());
        assert_eq!(c.run("/tasks stop foo"), None);
        assert!(c.last_note().contains("all"), "{}", c.last_note());
    }
}