chabeau 0.7.3

A full-screen terminal chat interface that connects to various AI APIs for real-time conversations
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
use super::*;
use crate::api::{ChatMessage, ChatToolCall, ChatToolCallFunction};
use crate::core::app::picker::{
    ModelPickerState, PickerData, PickerSession, ProviderPickerState, ThemePickerState,
};
use crate::core::app::session::{
    PendingToolCall, StreamContinuation, ToolCallRequest, ToolPayloadHistoryEntry,
    ToolResultRecord, ToolResultStatus,
};
use crate::core::app::ui_state::VerticalCursorDirection;
use crate::core::config::data::McpServerConfig;
use crate::core::message::{Message, TranscriptRole};
use crate::core::text_wrapping::{TextWrapper, WrapConfig};
use crate::ui::picker::{PickerItem, PickerState};
use crate::utils::test_utils::{
    create_test_app, create_test_message, create_test_message_with_role,
};
use rust_mcp_schema::{
    ListResourceTemplatesResult, ListResourcesResult, ListToolsResult, Resource, ResourceTemplate,
    Tool, ToolInputSchema,
};
use serde_json::{Map, Value};
use std::collections::HashMap;
use tokio_util::sync::CancellationToken;
use tui_textarea::{CursorMove, Input, Key};

#[test]
fn theme_picker_highlights_active_theme_over_default() {
    let mut app = create_test_app();
    // Simulate active theme is light, while default (config) remains None in tests
    app.ui.current_theme_id = Some("light".to_string());

    // Open the theme picker
    app.open_theme_picker().expect("theme picker opens");

    // After sorting and selection alignment, ensure selected item has id "light"
    if let Some(picker) = app.picker_state() {
        let idx = picker.selected;
        let selected_id = &picker.items[idx].id;
        assert_eq!(selected_id, "light");
    } else {
        panic!("picker not opened");
    }
}

#[test]
fn model_picker_title_uses_az_when_no_dates() {
    let mut app = create_test_app();
    // Build a model picker with no sort_key (no dates)
    let items = vec![
        PickerItem {
            id: "a-model".into(),
            label: "a-model".into(),
            metadata: None,
            inspect_metadata: None,
            sort_key: None,
        },
        PickerItem {
            id: "z-model".into(),
            label: "z-model".into(),
            metadata: None,
            inspect_metadata: None,
            sort_key: None,
        },
    ];
    let mut picker_state = PickerState::new("Pick Model", items.clone(), 0);
    picker_state.sort_mode = crate::ui::picker::SortMode::Name;
    app.picker.picker_session = Some(PickerSession {
        state: picker_state,
        data: PickerData::Model(Box::new(ModelPickerState {
            search_filter: String::new(),
            all_items: items,
            before_model: None,
            has_dates: false,
        })),
    });
    app.update_picker_title();
    let picker = app.picker_state().unwrap();
    assert!(picker.title.contains("Sort by: A-Z"));
}

#[test]
fn provider_model_cancel_reverts_base_url_and_state() {
    let mut app = create_test_app();
    // Set current state to some new provider context
    app.session.provider_name = "newprov".into();
    app.session.provider_display_name = "NewProv".into();
    app.session.model = "new-model".into();
    app.session.api_key = "new-key".into();
    app.session.base_url = "https://api.newprov.test/v1".into();

    // Simulate saved previous state for transition
    app.picker.in_provider_model_transition = true;
    app.picker.provider_model_transition_state = Some((
        "oldprov".into(),
        "OldProv".into(),
        "old-model".into(),
        "old-key".into(),
        "https://api.oldprov.test/v1".into(),
    ));

    // Cancelling model picker should revert provider/model/api_key/base_url
    app.picker.revert_model_preview(&mut app.session);

    assert_eq!(app.session.provider_name, "oldprov");
    assert_eq!(app.session.provider_display_name, "OldProv");
    assert_eq!(app.session.model, "old-model");
    assert_eq!(app.session.api_key, "old-key");
    assert_eq!(app.session.base_url, "https://api.oldprov.test/v1");
    assert!(!app.picker.in_provider_model_transition);
    assert!(app.picker.provider_model_transition_state.is_none());
}

#[test]
fn calculate_available_height_matches_expected_layout_rules() {
    let mut app = create_test_app();

    let cases = [
        (30, 5, 22), // 30 - (5 + 2) - 1
        (10, 8, 0),  // Saturating at zero when chat area would be negative
        (5, 0, 2),   // Just borders and title removed
    ];

    for (term_height, input_height, expected) in cases {
        assert_eq!(
            app.conversation()
                .calculate_available_height(term_height, input_height),
            expected
        );
    }
}

#[test]
fn clear_transcript_resets_transcript_state() {
    let mut app = create_test_app();
    app.ui
        .messages
        .push_back(create_test_message("user", "Hello"));
    app.ui
        .messages
        .push_back(create_test_message("assistant", "Response"));
    app.ui.current_response = "partial".to_string();
    app.session.retrying_message_index = Some(1);
    app.session.is_refining = true;
    app.session.original_refining_content = Some("original".to_string());
    app.session.last_refine_prompt = Some("prompt".to_string());
    app.session.has_received_assistant_message = true;
    app.session.character_greeting_shown = true;
    app.session.mcp_init.begin();
    app.session.mcp_init.deferred_message = Some("queued".to_string());

    app.get_prewrapped_lines_cached(80);
    assert!(app.ui.prewrap_cache.is_some());

    {
        let mut conversation = app.conversation();
        conversation.clear_transcript();
    }

    assert!(app.ui.messages.is_empty());
    assert!(app.ui.current_response.is_empty());
    assert!(app.ui.prewrap_cache.is_none());
    assert!(app.session.retrying_message_index.is_none());
    assert!(!app.session.is_refining);
    assert!(app.session.original_refining_content.is_none());
    assert!(app.session.last_refine_prompt.is_none());
    assert!(!app.session.has_received_assistant_message);
    assert!(!app.session.character_greeting_shown);
    assert!(app.session.mcp_init.in_progress);
    assert!(!app.session.mcp_init.complete);
    assert_eq!(
        app.session.mcp_init.deferred_message.as_deref(),
        Some("queued")
    );
}

#[test]
fn start_new_stream_preserves_tool_history_and_clears_transient_state() {
    let mut app = create_test_app();
    app.session.tool_pipeline.pending_tool_calls.insert(
        0,
        PendingToolCall {
            id: Some("call-1".to_string()),
            name: Some("lookup".to_string()),
            arguments: "{\"q\":\"now\"}".to_string(),
        },
    );
    app.session
        .tool_pipeline
        .pending_tool_queue
        .push_back(ToolCallRequest {
            server_id: "alpha".to_string(),
            tool_name: "lookup".to_string(),
            arguments: None,
            raw_arguments: "{\"q\":\"next\"}".to_string(),
            tool_call_id: Some("call-2".to_string()),
        });
    app.session.tool_pipeline.active_tool_request = Some(ToolCallRequest {
        server_id: "alpha".to_string(),
        tool_name: "lookup".to_string(),
        arguments: None,
        raw_arguments: "{\"q\":\"active\"}".to_string(),
        tool_call_id: Some("call-3".to_string()),
    });
    app.session
        .tool_pipeline
        .tool_call_records
        .push(ChatToolCall {
            id: "call-1".to_string(),
            kind: "function".to_string(),
            function: ChatToolCallFunction {
                name: "lookup".to_string(),
                arguments: "{\"q\":\"now\"}".to_string(),
            },
        });
    app.session.tool_pipeline.tool_results.push(ChatMessage {
        role: "tool".to_string(),
        content: "{\"ok\":true}".to_string(),
        name: None,
        tool_call_id: Some("call-1".to_string()),
        tool_calls: None,
    });
    app.session.tool_pipeline.continuation_messages = Some(StreamContinuation {
        api_messages: vec![ChatMessage {
            role: "assistant".to_string(),
            content: "partial".to_string(),
            name: None,
            tool_call_id: None,
            tool_calls: None,
        }],
        api_messages_base: vec![],
    });

    app.session
        .tool_pipeline
        .tool_result_history
        .push(ToolResultRecord {
            tool_name: "lookup".to_string(),
            server_name: Some("Alpha MCP".to_string()),
            server_id: Some("alpha".to_string()),
            status: ToolResultStatus::Success,
            failure_kind: None,
            content: "{\"ok\":true}".to_string(),
            summary: "lookup on Alpha MCP (success)".to_string(),
            tool_call_id: Some("call-1".to_string()),
            raw_arguments: Some("{\"q\":\"now\"}".to_string()),
            assistant_message_index: Some(0),
        });
    app.session
        .tool_pipeline
        .tool_payload_history
        .push(ToolPayloadHistoryEntry {
            server_id: Some("alpha".to_string()),
            tool_call_id: Some("call-1".to_string()),
            assistant_message: ChatMessage {
                role: "assistant".to_string(),
                content: String::new(),
                name: None,
                tool_call_id: None,
                tool_calls: Some(vec![ChatToolCall {
                    id: "call-1".to_string(),
                    kind: "function".to_string(),
                    function: ChatToolCallFunction {
                        name: "lookup".to_string(),
                        arguments: "{\"q\":\"now\"}".to_string(),
                    },
                }]),
            },
            tool_message: ChatMessage {
                role: "tool".to_string(),
                content: "{\"ok\":true}".to_string(),
                name: None,
                tool_call_id: Some("call-1".to_string()),
                tool_calls: None,
            },
            assistant_message_index: Some(0),
        });

    {
        let mut conversation = app.conversation();
        let (_token, stream_id) = conversation.start_new_stream();
        assert_eq!(stream_id, 1);
    }

    assert!(app.session.tool_pipeline.pending_tool_calls.is_empty());
    assert!(app.session.tool_pipeline.pending_tool_queue.is_empty());
    assert!(app.session.tool_pipeline.active_tool_request.is_none());
    assert!(app.session.tool_pipeline.tool_call_records.is_empty());
    assert!(app.session.tool_pipeline.tool_results.is_empty());
    assert!(app.session.tool_pipeline.continuation_messages.is_none());

    assert_eq!(app.session.tool_pipeline.tool_result_history.len(), 1);
    assert_eq!(app.session.tool_pipeline.tool_payload_history.len(), 1);
    assert_eq!(
        app.session.tool_pipeline.tool_result_history[0]
            .tool_call_id
            .as_deref(),
        Some("call-1")
    );
    assert_eq!(
        app.session.tool_pipeline.tool_payload_history[0]
            .tool_call_id
            .as_deref(),
        Some("call-1")
    );
}

#[test]
fn default_sort_mode_helper_behaviour() {
    let mut app = create_test_app();
    // Theme picker prefers alphabetical → Name
    app.picker.picker_session = Some(PickerSession {
        state: PickerState::new("Pick Theme", vec![], 0),
        data: PickerData::Theme(Box::new(ThemePickerState {
            search_filter: String::new(),
            all_items: Vec::new(),
            before_theme: None,
            before_theme_id: None,
        })),
    });
    assert!(matches!(
        app.picker_session().unwrap().default_sort_mode(),
        crate::ui::picker::SortMode::Name
    ));
    // Provider picker prefers alphabetical → Name
    app.picker.picker_session = Some(PickerSession {
        state: PickerState::new("Pick Provider", vec![], 0),
        data: PickerData::Provider(Box::new(ProviderPickerState {
            search_filter: String::new(),
            all_items: Vec::new(),
            before_provider: None,
        })),
    });
    assert!(matches!(
        app.picker_session().unwrap().default_sort_mode(),
        crate::ui::picker::SortMode::Name
    ));
    // Model picker with dates → Date
    app.picker.picker_session = Some(PickerSession {
        state: PickerState::new("Pick Model", vec![], 0),
        data: PickerData::Model(Box::new(ModelPickerState {
            search_filter: String::new(),
            all_items: Vec::new(),
            before_model: None,
            has_dates: true,
        })),
    });
    assert!(matches!(
        app.picker_session().unwrap().default_sort_mode(),
        crate::ui::picker::SortMode::Date
    ));
    // Model picker without dates → Name
    if let Some(PickerSession {
        data: PickerData::Model(state),
        ..
    }) = app.picker_session_mut()
    {
        state.has_dates = false;
    }
    assert!(matches!(
        app.picker_session().unwrap().default_sort_mode(),
        crate::ui::picker::SortMode::Name
    ));
}

#[test]
fn build_stream_params_includes_mcp_tools() {
    let mut app = create_test_app();

    app.config
        .mcp_servers
        .push(crate::core::config::data::McpServerConfig {
            id: "alpha".to_string(),
            display_name: "Alpha MCP".to_string(),
            base_url: Some("https://mcp.example.com".to_string()),
            command: None,
            args: None,
            env: None,
            headers: None,
            transport: Some("streamable-http".to_string()),
            allowed_tools: Some(vec!["search".to_string()]),
            protocol_version: None,
            enabled: Some(true),
            tool_payloads: None,
            tool_payload_window: None,
            yolo: None,
        });
    app.mcp = crate::mcp::client::McpClientManager::from_config(&app.config);

    let mut prop_map = Map::new();
    prop_map.insert("type".to_string(), Value::String("string".to_string()));
    let mut properties = HashMap::new();
    properties.insert("query".to_string(), prop_map);
    let input_schema = ToolInputSchema::new(vec!["query".to_string()], Some(properties), None);

    let tool_allowed = Tool {
        annotations: None,
        description: Some("Search the index".to_string()),
        execution: None,
        icons: Vec::new(),
        input_schema: input_schema.clone(),
        meta: None,
        name: "search".to_string(),
        output_schema: None,
        title: None,
    };

    let tool_blocked = Tool {
        annotations: None,
        description: Some("Ignore me".to_string()),
        execution: None,
        icons: Vec::new(),
        input_schema,
        meta: None,
        name: "hidden".to_string(),
        output_schema: None,
        title: None,
    };

    let list = ListToolsResult {
        meta: None,
        next_cursor: None,
        tools: vec![tool_allowed, tool_blocked],
    };

    if let Some(server) = app.mcp.server_mut("alpha") {
        server.cached_tools = Some(list);
    } else {
        panic!("missing MCP server state");
    }

    let params = app.build_stream_params(
        vec![ChatMessage {
            role: "user".to_string(),
            content: "Hello".to_string(),
            name: None,
            tool_call_id: None,
            tool_calls: None,
        }],
        CancellationToken::new(),
        1,
    );

    let tools = params.tools.expect("expected MCP tools");
    assert_eq!(tools.len(), 3);
    assert!(tools.iter().any(|tool| tool.function.name == "search"));
    assert!(tools
        .iter()
        .any(|tool| { tool.function.name == crate::mcp::MCP_INSTANT_RECALL_TOOL }));
    assert!(tools
        .iter()
        .any(|tool| { tool.function.name == crate::mcp::MCP_LIST_RESOURCES_TOOL }));
    let description = tools
        .iter()
        .find(|tool| tool.function.name == "search")
        .and_then(|tool| tool.function.description.as_ref())
        .expect("missing tool description");
    assert!(description.contains("Alpha MCP"));
}

#[test]
fn build_stream_params_includes_mcp_resources() {
    let mut app = create_test_app();

    app.config
        .mcp_servers
        .push(crate::core::config::data::McpServerConfig {
            id: "alpha".to_string(),
            display_name: "Alpha MCP".to_string(),
            base_url: Some("https://mcp.example.com".to_string()),
            command: None,
            args: None,
            env: None,
            headers: None,
            transport: Some("streamable-http".to_string()),
            allowed_tools: None,
            protocol_version: None,
            enabled: Some(true),
            tool_payloads: None,
            tool_payload_window: None,
            yolo: None,
        });
    app.mcp = crate::mcp::client::McpClientManager::from_config(&app.config);

    let resources = ListResourcesResult {
        meta: None,
        next_cursor: Some("cursor-res".to_string()),
        resources: vec![Resource {
            annotations: None,
            description: Some("Alpha resource".to_string()),
            icons: Vec::new(),
            meta: None,
            mime_type: None,
            name: "alpha-doc".to_string(),
            size: None,
            title: Some("Alpha Doc".to_string()),
            uri: "mcp://alpha/doc".to_string(),
        }],
    };
    let resource_templates = ListResourceTemplatesResult {
        meta: None,
        next_cursor: Some("cursor-templates".to_string()),
        resource_templates: vec![ResourceTemplate {
            annotations: None,
            description: Some("Alpha template".to_string()),
            icons: Vec::new(),
            meta: None,
            mime_type: None,
            name: "alpha-template".to_string(),
            title: Some("Alpha Template".to_string()),
            uri_template: "mcp://alpha/{doc}".to_string(),
        }],
    };

    if let Some(server) = app.mcp.server_mut("alpha") {
        server.cached_resources = Some(resources);
        server.cached_resource_templates = Some(resource_templates);
    } else {
        panic!("missing MCP server state");
    }

    let params = app.build_stream_params(
        vec![ChatMessage {
            role: "user".to_string(),
            content: "Hello".to_string(),
            name: None,
            tool_call_id: None,
            tool_calls: None,
        }],
        CancellationToken::new(),
        1,
    );

    let tools = params.tools.expect("expected MCP tools");
    assert!(tools
        .iter()
        .any(|tool| tool.function.name == crate::mcp::MCP_READ_RESOURCE_TOOL));
    assert!(tools
        .iter()
        .any(|tool| tool.function.name == crate::mcp::MCP_LIST_RESOURCES_TOOL));

    let system_message = params
        .api_messages
        .iter()
        .find(|msg| msg.role == "system")
        .expect("missing system message");
    assert!(system_message
        .content
        .contains("MCP resources and templates (by server id):"));
    assert!(system_message.content.contains("mcp://alpha/doc"));
    assert!(system_message.content.contains("mcp://alpha/{doc}"));
    assert!(system_message.content.contains("cursor-res"));
    assert!(system_message.content.contains("cursor-templates"));
}

#[test]
fn parse_resource_list_kind_defaults_to_resources_and_passes_cursor() {
    let mut args = serde_json::Map::new();
    args.insert(
        "cursor".to_string(),
        serde_json::Value::String("cursor-1".to_string()),
    );
    let (kind, cursor) = crate::core::app::actions::parse_resource_list_kind(&args)
        .expect("expected kind parsing to succeed");

    assert_eq!(kind, crate::core::app::actions::ResourceListKind::Resources);
    assert_eq!(cursor.as_deref(), Some("cursor-1"));
}

#[test]
fn parse_resource_list_kind_rejects_empty_cursor() {
    let mut args = serde_json::Map::new();
    args.insert(
        "cursor".to_string(),
        serde_json::Value::String("   ".to_string()),
    );
    let err = crate::core::app::actions::parse_resource_list_kind(&args)
        .expect_err("expected empty cursor to error");

    assert!(err.contains("cursor cannot be empty"));
}

#[test]
fn parse_resource_list_kind_accepts_templates() {
    let mut args = serde_json::Map::new();
    args.insert(
        "kind".to_string(),
        serde_json::Value::String("templates".to_string()),
    );
    let (kind, cursor) = crate::core::app::actions::parse_resource_list_kind(&args)
        .expect("expected kind parsing to succeed");

    assert_eq!(kind, crate::core::app::actions::ResourceListKind::Templates);
    assert!(cursor.is_none());
}

#[test]
fn build_stream_params_includes_tool_history_and_payloads() {
    let mut app = create_test_app();
    app.config.mcp_servers.push(McpServerConfig {
        id: "alpha".to_string(),
        display_name: "Alpha MCP".to_string(),
        base_url: Some("https://mcp.example.com".to_string()),
        command: None,
        args: None,
        env: None,
        headers: None,
        transport: Some("streamable-http".to_string()),
        allowed_tools: None,
        protocol_version: None,
        enabled: Some(true),
        tool_payloads: None,
        tool_payload_window: None,
        yolo: None,
    });
    app.mcp = crate::mcp::client::McpClientManager::from_config(&app.config);
    app.session
        .tool_pipeline
        .tool_result_history
        .push(ToolResultRecord {
            tool_name: "lookup".to_string(),
            server_name: Some("Alpha MCP".to_string()),
            server_id: Some("alpha".to_string()),
            status: ToolResultStatus::Success,
            failure_kind: None,
            content: "{\"ok\":true}".to_string(),
            summary: "lookup on Alpha MCP (success)".to_string(),
            tool_call_id: Some("call-1".to_string()),
            raw_arguments: Some("{\"q\":\"test\"}".to_string()),
            assistant_message_index: None,
        });
    app.session
        .tool_pipeline
        .tool_result_history
        .push(ToolResultRecord {
            tool_name: "lookup".to_string(),
            server_name: Some("Alpha MCP".to_string()),
            server_id: Some("alpha".to_string()),
            status: ToolResultStatus::Success,
            failure_kind: None,
            content: "{\"ok\":true}".to_string(),
            summary: "lookup on Alpha MCP (success) args: {\"q\":\"missing\"}".to_string(),
            tool_call_id: Some("call-2".to_string()),
            raw_arguments: Some("{\"q\":\"missing\"}".to_string()),
            assistant_message_index: None,
        });

    let assistant_message = ChatMessage {
        role: "assistant".to_string(),
        content: String::new(),
        name: None,
        tool_call_id: None,
        tool_calls: Some(vec![ChatToolCall {
            id: "call-1".to_string(),
            kind: "function".to_string(),
            function: ChatToolCallFunction {
                name: "lookup".to_string(),
                arguments: "{\"q\":\"test\"}".to_string(),
            },
        }]),
    };
    let tool_message = ChatMessage {
        role: "tool".to_string(),
        content: "{\"ok\":true}".to_string(),
        name: None,
        tool_call_id: Some("call-1".to_string()),
        tool_calls: None,
    };
    app.session
        .tool_pipeline
        .tool_payload_history
        .push(ToolPayloadHistoryEntry {
            server_id: Some("alpha".to_string()),
            tool_call_id: Some("call-1".to_string()),
            assistant_message,
            tool_message,
            assistant_message_index: None,
        });

    let params = app.build_stream_params(
        vec![ChatMessage {
            role: "user".to_string(),
            content: "Hello".to_string(),
            name: None,
            tool_call_id: None,
            tool_calls: None,
        }],
        CancellationToken::new(),
        1,
    );

    let system = params
        .api_messages
        .iter()
        .find(|msg| msg.role == "system")
        .expect("missing system message");
    assert!(system.content.contains(
        "MCP tool payload retention note: Default MCP tool output policy: only the current turn's raw outputs stay in chat context to save tokens; older outputs are summarized. Full payloads remain available via chabeau_instant_recall using call_id, which reinserts earlier outputs from system memory (NO software limit on retention)."
    ));
    assert!(system.content.contains(
        "Configure per server in config.toml with tool_payloads: turn (current turn only), window (last N raw outputs; set tool_payload_window), all (keep all raw outputs in context; token-expensive)."
    ));
    assert!(system
        .content
        .contains("MCP tool payload policy by server: alpha: default (turn)"));
    assert!(!system
        .content
        .contains("SESSION TOOL LEDGER (call_id • tool • args • status):"));
    assert!(!system.content.contains("SESSION MEMORY HINT:"));
    assert!(!system.content.contains("Tool history (session summaries):"));
    assert!(!system.content.contains("lookup on Alpha MCP (success)"));

    let last_user_idx = params
        .api_messages
        .iter()
        .rposition(|msg| msg.role == "user")
        .expect("missing user message");
    let assistant_idx = params
        .api_messages
        .iter()
        .position(|msg| msg.role == "assistant" && msg.tool_calls.is_some())
        .expect("missing assistant tool call");
    let tool_idx = params
        .api_messages
        .iter()
        .position(|msg| msg.role == "tool" && msg.tool_call_id.as_deref() == Some("call-1"))
        .expect("missing tool message");
    let summary_idx = params
        .api_messages
        .iter()
        .position(|msg| {
            msg.role == "assistant"
                && msg
                    .content
                    .starts_with("TOOL SUMMARY (system-added per MCP payload policy):")
        })
        .expect("missing summary message");
    let summary_message = &params.api_messages[summary_idx].content;
    assert!(summary_message.contains("missing"));
    assert!(summary_message.contains("call_id=call-2"));
    assert!(assistant_idx < tool_idx);
    assert!(tool_idx < summary_idx);
    assert!(summary_idx < last_user_idx);
}

#[test]
fn test_prewrap_cache_reuse_when_unchanged() {
    let mut app = create_test_app();
    for i in 0..50 {
        app.ui.messages.push_back(Message {
            role: if i % 2 == 0 {
                TranscriptRole::User
            } else {
                TranscriptRole::Assistant
            },
            content: "lorem ipsum dolor sit amet consectetur adipiscing elit".into(),
        });
    }
    let w = 100u16;

    // Test lines cache reuse
    let lines_ptr1 = {
        let p1 = app.get_prewrapped_lines_cached(w);
        assert!(!p1.is_empty());
        p1.as_ptr()
    };
    let lines_ptr2 = {
        let p2 = app.get_prewrapped_lines_cached(w);
        p2.as_ptr()
    };
    assert_eq!(
        lines_ptr1, lines_ptr2,
        "lines cache should be reused when nothing changed"
    );

    // Test metadata cache reuse
    let meta_ptr1 = app.get_prewrapped_span_metadata_cached(w) as *const _;
    let meta_ptr2 = app.get_prewrapped_span_metadata_cached(w) as *const _;
    let meta_ptr3 = app.get_prewrapped_span_metadata_cached(w) as *const _;

    assert_eq!(
        meta_ptr1, meta_ptr2,
        "metadata cache should be reused when nothing changed"
    );
    assert_eq!(
        meta_ptr2, meta_ptr3,
        "metadata cache should be reused across multiple calls"
    );
}

#[test]
fn test_prewrap_cache_invalidates_on_width_change() {
    use crate::ui::markdown::test_fixtures;

    let mut app = create_test_app();
    app.ui.messages.push_back(test_fixtures::single_block());

    let width1 = 80u16;
    let width2 = 120u16;

    // Test lines cache invalidation
    let lines_ptr1 = {
        let p1 = app.get_prewrapped_lines_cached(width1);
        p1.as_ptr()
    };
    let lines_ptr2 = {
        let p2 = app.get_prewrapped_lines_cached(width2);
        p2.as_ptr()
    };
    assert_ne!(
        lines_ptr1, lines_ptr2,
        "lines cache should invalidate on width change"
    );

    // Test metadata cache invalidation
    let metadata1 = app.get_prewrapped_span_metadata_cached(width1);
    let has_code1 = metadata1
        .iter()
        .flat_map(|line| line.iter())
        .any(|k| k.is_code_block());

    let metadata2 = app.get_prewrapped_span_metadata_cached(width2);
    let has_code2 = metadata2
        .iter()
        .flat_map(|line| line.iter())
        .any(|k| k.is_code_block());

    // Both widths should have code block metadata
    assert!(has_code1, "Width1 metadata should have code blocks");
    assert!(has_code2, "Width2 metadata should have code blocks");

    // Verify cache was rebuilt by checking at width1 again
    let metadata1_again = app.get_prewrapped_span_metadata_cached(width1);
    let has_code1_again = metadata1_again
        .iter()
        .flat_map(|line| line.iter())
        .any(|k| k.is_code_block());

    assert!(
        has_code1_again,
        "Width1 again should still have code blocks after cache rebuild"
    );
}

#[test]
fn prewrap_cache_updates_metadata_for_markdown_last_message() {
    let mut app = create_test_app();
    app.ui
        .messages
        .push_back(create_test_message("user", "This is the opening line."));
    app.ui.messages.push_back(create_test_message(
        "assistant",
        "Initial response that will be replaced.",
    ));

    let width = 72;
    let initial_lines = app.get_prewrapped_lines_cached(width).clone();
    let initial_meta = app.get_prewrapped_span_metadata_cached(width).clone();
    assert_eq!(initial_lines.len(), initial_meta.len());

    if let Some(last) = app.ui.messages.back_mut() {
        last.content = "Here's an updated reply with a [link](https://example.com).".into();
    }

    let updated_lines = app.get_prewrapped_lines_cached(width).clone();
    let updated_meta = app.get_prewrapped_span_metadata_cached(width).clone();
    assert_eq!(updated_lines.len(), updated_meta.len());
    assert!(updated_meta
        .iter()
        .any(|kinds| kinds.iter().any(|kind| kind.is_link())));
}

#[test]
fn prewrap_cache_updates_metadata_for_plain_text_last_message() {
    let mut app = create_test_app();
    app.ui.markdown_enabled = false;
    app.ui.syntax_enabled = false;
    app.ui
        .messages
        .push_back(create_test_message("user", "Plain intro from the user."));
    app.ui.messages.push_back(create_test_message(
        "assistant",
        "A short reply that will expand into a much longer paragraph after the update.",
    ));

    let width = 40;
    let initial_lines = app.get_prewrapped_lines_cached(width).clone();
    let initial_meta = app.get_prewrapped_span_metadata_cached(width).clone();
    assert_eq!(initial_lines.len(), initial_meta.len());

    if let Some(last) = app.ui.messages.back_mut() {
        last.content = "Now the assistant responds with a deliberately long piece of plain text that should wrap across multiple terminal lines once re-rendered.".into();
    }

    let updated_lines = app.get_prewrapped_lines_cached(width).clone();
    let updated_meta = app.get_prewrapped_span_metadata_cached(width).clone();
    assert_eq!(updated_lines.len(), updated_meta.len());
    let mut saw_prefix = false;
    for kind in updated_meta.iter().flat_map(|kinds| kinds.iter()) {
        assert!(kind.is_text() || kind.is_prefix());
        if kind.is_prefix() {
            saw_prefix = true;
        }
    }
    assert!(
        saw_prefix,
        "expected plain text metadata to include a prefix span"
    );
}

#[test]
fn prewrap_cache_plain_text_last_message_wrapping() {
    // Reproduce the fast-path tail update and ensure plain-text wrapping is preserved
    let mut app = crate::utils::test_utils::create_test_app();
    app.ui.markdown_enabled = false;
    let theme = app.ui.theme.clone();

    // Start with two assistant messages
    app.ui.messages.push_back(Message {
        role: TranscriptRole::Assistant,
        content: "Short".into(),
    });
    app.ui.messages.push_back(Message {
        role: TranscriptRole::Assistant,
        content: "This is a very long plain text line that should wrap when width is small".into(),
    });

    let width = 20u16;
    app.get_prewrapped_lines_cached(width);

    // Update only the last message content to trigger the fast path
    if let Some(last) = app.ui.messages.back_mut() {
        last.content.push_str(" and now it changed");
    }
    let second = app.get_prewrapped_lines_cached(width).clone();
    // Convert to strings and check for wrapping (no line exceeds width)
    let rendered: Vec<String> = second.iter().map(|l| l.to_string()).collect();
    let content_lines: Vec<&String> = rendered.iter().filter(|s| !s.is_empty()).collect();
    assert!(
        content_lines.len() > 2,
        "Expected multiple wrapped content lines"
    );
    for (i, s) in content_lines.iter().enumerate() {
        assert!(
            s.chars().count() <= width as usize,
            "Line {} exceeds width: '{}' (len={})",
            i,
            s,
            s.len()
        );
    }

    // Silence unused warning
    let _ = theme;
}

#[test]
fn test_sync_cursor_mapping_single_and_multi_line() {
    let mut app = create_test_app();

    // Single line: move to end
    app.ui.set_input_text("hello world".to_string());
    app.ui
        .apply_textarea_edit(|ta| ta.move_cursor(CursorMove::End));
    assert_eq!(app.ui.get_input_text(), "hello world");
    assert_eq!(app.ui.get_input_cursor_position(), 11);

    // Multi-line: jump to (row=1, col=3) => after "wor" on second line
    app.ui.set_input_text("hello\nworld".to_string());
    app.ui
        .apply_textarea_edit(|ta| ta.move_cursor(CursorMove::Jump(1, 3)));
    // 5 (hello) + 1 (\n) + 3 = 9
    assert_eq!(app.ui.get_input_cursor_position(), 9);
}

#[test]
fn test_backspace_at_start_noop() {
    let mut app = create_test_app();
    app.ui.set_input_text("abc".to_string());
    // Move to head of line
    app.ui
        .apply_textarea_edit(|ta| ta.move_cursor(CursorMove::Head));
    // Simulate backspace (always single-char via input_without_shortcuts)
    app.ui.apply_textarea_edit(|ta| {
        ta.input_without_shortcuts(Input {
            key: Key::Backspace,
            ctrl: false,
            alt: false,
            shift: false,
        });
    });
    assert_eq!(app.ui.get_input_text(), "abc");
    assert_eq!(app.ui.get_input_cursor_position(), 0);
}

#[test]
fn test_backspace_at_line_start_joins_lines() {
    let mut app = create_test_app();
    app.ui.set_input_text("hello\nworld".to_string());
    // Move to start of second line
    app.ui
        .apply_textarea_edit(|ta| ta.move_cursor(CursorMove::Jump(1, 0)));
    // Backspace should join lines; use input_without_shortcuts to ensure single-char delete
    app.ui.apply_textarea_edit(|ta| {
        ta.input_without_shortcuts(Input {
            key: Key::Backspace,
            ctrl: false,
            alt: false,
            shift: false,
        });
    });
    assert_eq!(app.ui.get_input_text(), "helloworld");
    // Cursor should be at end of former first line (index 5)
    assert_eq!(app.ui.get_input_cursor_position(), 5);
}

#[test]
fn test_backspace_with_alt_modifier_deletes_single_char() {
    let mut app = create_test_app();
    app.ui.set_input_text("hello world".to_string());
    app.ui
        .apply_textarea_edit(|ta| ta.move_cursor(CursorMove::End));
    // Simulate Alt+Backspace; with input_without_shortcuts it should still delete one char
    app.ui.apply_textarea_edit(|ta| {
        ta.input_without_shortcuts(Input {
            key: Key::Backspace,
            ctrl: false,
            alt: true,
            shift: false,
        });
    });
    assert_eq!(app.ui.get_input_text(), "hello worl");
    assert_eq!(
        app.ui.get_input_cursor_position(),
        "hello worl".chars().count()
    );
}

#[test]
fn test_update_input_scroll_keeps_cursor_visible() {
    let mut app = create_test_app();
    // Long line that wraps at width 10 into multiple lines
    let text = "one two three four five six seven eight nine ten";
    app.ui.set_input_text(text.to_string());
    // Simulate small input area: width=20 total => inner available width accounts in method
    let width: u16 = 10; // small terminal width to force wrapping (inner ~4)
    let input_area_height: u16 = 2; // only 2 lines visible
                                    // Place cursor near end
    app.ui
        .set_cursor_position(text.chars().count().saturating_sub(1));
    app.ui.update_input_scroll(input_area_height, width);
    // With cursor near end, scroll offset should be > 0 to bring cursor into view
    assert!(app.ui.input_scroll_offset > 0);
}

#[test]
fn test_shift_like_up_down_moves_one_line_on_many_newlines() {
    let mut app = create_test_app();
    // Build text with many blank lines
    let text = "top\n\n\n\n\n\n\n\n\n\nbottom";
    app.ui.set_input_text(text.to_string());
    // Jump to bottom line, col=3 (after 'bot')
    let bottom_row_usize = app.ui.get_textarea_line_count().saturating_sub(1);
    let bottom_row = bottom_row_usize as u16;
    app.ui
        .apply_textarea_edit(|ta| ta.move_cursor(CursorMove::Jump(bottom_row, 3)));
    let (row_before, col_before) = app.ui.get_textarea_cursor();
    assert_eq!(row_before, bottom_row as usize);
    assert!(col_before <= app.ui.get_textarea_line_len(bottom_row_usize));

    // Move up exactly one line
    app.ui
        .apply_textarea_edit(|ta| ta.move_cursor(CursorMove::Up));
    let (row_after_up, col_after_up) = app.ui.get_textarea_cursor();
    assert_eq!(row_after_up, bottom_row_usize.saturating_sub(1));
    // Column should clamp reasonably; we just assert it's within line bounds
    assert!(col_after_up <= app.ui.get_textarea_line_len(8));

    // Move down exactly one line
    app.ui
        .apply_textarea_edit(|ta| ta.move_cursor(CursorMove::Down));
    let (row_after_down, _col_after_down) = app.ui.get_textarea_cursor();
    assert_eq!(row_after_down, bottom_row_usize);
}

#[test]
fn test_wrapped_vertical_navigation_preserves_visual_column() {
    let mut app = create_test_app();
    app.ui.set_input_text_with_cursor("abcdefgh".to_string(), 6);

    let moved_up = app
        .ui
        .move_cursor_in_wrapped_input(8, VerticalCursorDirection::Up);
    assert!(moved_up);
    assert_eq!(app.ui.get_input_cursor_position(), 3);

    let moved_down = app
        .ui
        .move_cursor_in_wrapped_input(8, VerticalCursorDirection::Down);
    assert!(moved_down);
    assert_eq!(app.ui.get_input_cursor_position(), 6);
}

#[test]
fn test_wrapped_vertical_navigation_clamps_to_shorter_line() {
    let mut app = create_test_app();
    app.ui.set_input_text_with_cursor("abcdefgh".to_string(), 8);

    let moved_up = app
        .ui
        .move_cursor_in_wrapped_input(8, VerticalCursorDirection::Up);
    assert!(moved_up);
    assert_eq!(app.ui.get_input_cursor_position(), 5);

    let moved_down = app
        .ui
        .move_cursor_in_wrapped_input(8, VerticalCursorDirection::Down);
    assert!(moved_down);
    assert_eq!(app.ui.get_input_cursor_position(), 8);
}

#[test]
fn test_wrapped_vertical_navigation_handles_multiple_paragraphs() {
    let mut app = create_test_app();
    let text = "aaaaa bbbbb ccccc ddddd\neeeee fffff ggggg hhhhh";
    app.ui
        .set_input_text_with_cursor(text.to_string(), text.chars().count());

    let newline_idx = text.find('\n').unwrap();
    let mut saw_above_newline = false;

    loop {
        let moved = app
            .ui
            .move_cursor_in_wrapped_input(15, VerticalCursorDirection::Up);
        if !moved {
            break;
        }
        if app.ui.get_input_cursor_position() <= newline_idx {
            saw_above_newline = true;
        }
    }

    assert!(
        saw_above_newline,
        "cursor should cross the hard newline boundary"
    );
    let (row, _) = app.ui.get_textarea_cursor();
    assert_eq!(row, 0);
}

#[test]
fn test_wrapped_vertical_navigation_keeps_column_zero_on_descend() {
    let mut app = create_test_app();
    app.ui.set_input_text_with_cursor("abcdefgh".to_string(), 0);

    let moved_down = app
        .ui
        .move_cursor_in_wrapped_input(9, VerticalCursorDirection::Down);
    assert!(moved_down);
    assert_eq!(app.ui.get_input_cursor_position(), 4);

    let moved_up = app
        .ui
        .move_cursor_in_wrapped_input(9, VerticalCursorDirection::Up);
    assert!(moved_up);
    assert_eq!(app.ui.get_input_cursor_position(), 0);
}

#[test]
fn test_shift_like_left_right_moves_one_char() {
    let mut app = create_test_app();
    app.ui.set_input_text("hello".to_string());
    // Move to end, then back by one, then forward by one
    app.ui
        .apply_textarea_edit(|ta| ta.move_cursor(CursorMove::End));
    let end_pos = app.ui.get_input_cursor_position();
    app.ui
        .apply_textarea_edit(|ta| ta.move_cursor(CursorMove::Back));
    let back_pos = app.ui.get_input_cursor_position();
    assert_eq!(back_pos, end_pos.saturating_sub(1));
    app.ui
        .apply_textarea_edit(|ta| ta.move_cursor(CursorMove::Forward));
    let forward_pos = app.ui.get_input_cursor_position();
    assert_eq!(forward_pos, end_pos);
}

#[test]
fn paste_inserts_cursor_at_end_of_insert() {
    let mut app = create_test_app();
    let term_width = 80u16;
    let text = "this is a long paragraph that should wrap softly when rendered";

    app.insert_into_input(text, term_width);

    assert_eq!(app.ui.get_input_text(), text);
    assert_eq!(app.ui.get_input_cursor_position(), text.chars().count());
}

#[test]
fn visual_line_start_end_track_wrapped_columns() {
    let mut app = create_test_app();
    let text = "alpha beta gamma delta epsilon zeta eta".to_string();
    let cursor_pos = text.find("gamma").unwrap() + 2; // inside "gamma"
    let term_width = 20u16;
    let wrap_width = term_width.saturating_sub(5) as usize;

    app.ui.set_input_text_with_cursor(text.clone(), cursor_pos);

    let layout = TextWrapper::cursor_layout(&text, &WrapConfig::new(wrap_width));
    let line = layout
        .coordinates_for_index(app.ui.get_input_cursor_position())
        .0;
    let (line_start, line_end) = layout
        .line_bounds(line)
        .expect("line bounds available for wrapped line");

    assert!(app.ui.move_cursor_to_visual_line_start(term_width));
    assert_eq!(app.ui.get_input_cursor_position(), line_start);

    assert!(app.ui.move_cursor_to_visual_line_end(term_width));
    assert_eq!(app.ui.get_input_cursor_position(), line_end);
}

#[test]
fn wrapped_cursor_crosses_paragraph_boundaries() {
    let mut app = create_test_app();
    let text = "one two three four five six seven eight nine ten\nalpha beta gamma delta epsilon zeta eta theta".to_string();
    let newline_index = text.find('\n').unwrap();
    let cursor_pos = newline_index + 4; // inside the second paragraph
    let term_width = 22u16;

    app.ui.set_input_text_with_cursor(text.clone(), cursor_pos);

    assert!(app
        .ui
        .move_cursor_in_wrapped_input(term_width, VerticalCursorDirection::Up));
    assert!(app.ui.get_input_cursor_position() <= newline_index);

    assert!(app
        .ui
        .move_cursor_in_wrapped_input(term_width, VerticalCursorDirection::Down));
    assert!(app.ui.get_input_cursor_position() > newline_index);
}

#[test]
fn wrapped_cursor_moves_through_blank_lines() {
    let mut app = create_test_app();
    let text = "line one\n\n\nline two content that wraps across multiple words".to_string();
    let term_width = 32u16;
    app.ui
        .set_input_text_with_cursor(text.clone(), text.chars().count());

    let top_boundary = text.find('\n').unwrap();
    let mut crossed = false;
    for _ in 0..6 {
        if !app
            .ui
            .move_cursor_in_wrapped_input(term_width, VerticalCursorDirection::Up)
        {
            break;
        }
        if app.ui.get_input_cursor_position() <= top_boundary {
            crossed = true;
            break;
        }
    }

    assert!(crossed, "cursor should move across consecutive blank lines");
}

#[test]
fn visual_line_controls_handle_blank_lines() {
    let mut app = create_test_app();
    let text = "alpha beta gamma\n\nsecond paragraph".to_string();
    let term_width = 28u16;
    app.ui
        .set_input_text_with_cursor(text.clone(), text.chars().count());

    // Move cursor onto the blank line between paragraphs.
    assert!(app
        .ui
        .move_cursor_in_wrapped_input(term_width, VerticalCursorDirection::Up));
    let blank_line_start = text.find('\n').unwrap() + 1;
    assert_eq!(app.ui.get_input_cursor_position(), blank_line_start);

    // Home should stay on the blank line (no-op but returns false because already there).
    assert!(!app.ui.move_cursor_to_visual_line_start(term_width));
    assert_eq!(app.ui.get_input_cursor_position(), blank_line_start);

    // End should also be a no-op but leave the preferred column at zero.
    assert!(!app.ui.move_cursor_to_visual_line_end(term_width));
    assert_eq!(app.ui.get_input_cursor_position(), blank_line_start);
    assert_eq!(app.ui.input_cursor_preferred_column, Some(0));
}

#[test]
fn page_cursor_movement_skips_multiple_wrapped_lines() {
    let mut app = create_test_app();
    let text = "lorem ipsum dolor sit amet consectetur adipiscing elit sed do eiusmod tempor incididunt ut labore et dolore magna aliqua".to_string();
    let term_width = 24u16;

    app.ui
        .set_input_text_with_cursor(text.clone(), text.chars().count());

    let before = app.ui.get_input_cursor_position();
    let moved =
        app.ui
            .move_cursor_page_in_wrapped_input(term_width, VerticalCursorDirection::Up, 3);

    assert!(moved);
    assert!(app.ui.get_input_cursor_position() < before);
}

#[test]
fn test_cursor_mapping_blankline_insert_no_desync() {
    let mut app = create_test_app();
    let text = "asdf\n\nasdf\n\nasdf";
    app.ui.set_input_text(text.to_string());
    // Jump to blank line 2 (0-based row 3), column 0
    app.ui
        .apply_textarea_edit(|ta| ta.move_cursor(CursorMove::Jump(3, 0)));
    // Insert a character on the blank line
    app.ui.apply_textarea_edit(|ta| {
        ta.insert_str("x");
    });

    // Compute wrapped position using same wrapper logic (no wrapping with wide width)
    let config = WrapConfig::new(120);
    let (line, col) = TextWrapper::calculate_cursor_position_in_wrapped_text(
        app.ui.get_input_text(),
        app.ui.get_input_cursor_position(),
        &config,
    );
    // Compare to textarea's cursor row/col
    let (row, c) = app.ui.get_textarea_cursor();
    assert_eq!(line, row);
    assert_eq!(col, c);
}

#[test]
fn test_recompute_input_layout_after_edit_updates_scroll() {
    let mut app = create_test_app();
    // Make text long enough to wrap
    let text = "one two three four five six seven eight nine ten";
    app.ui.set_input_text(text.to_string());
    // Place cursor near end
    app.ui
        .set_cursor_position(text.chars().count().saturating_sub(1));
    // Very small terminal width to force heavy wrapping; method accounts for borders and margin
    let width: u16 = 6;
    app.ui.recompute_input_layout_after_edit(width);
    // With cursor near end on a heavily wrapped input, expect some scroll
    assert!(app.ui.input_scroll_offset > 0);
    // Changing cursor position to start should reduce or reset scroll
    app.ui.set_cursor_position(0);
    app.ui.recompute_input_layout_after_edit(width);
    assert_eq!(app.ui.input_scroll_offset, 0);
}

#[test]
fn complete_slash_command_fills_unique_match() {
    let mut app = create_test_app();
    app.ui.set_input_text("/he".into());

    let handled = app.complete_slash_command(80);
    assert!(handled);
    assert_eq!(app.ui.get_input_text(), "/help ");
    assert_eq!(app.ui.get_input_cursor_position(), "/help ".chars().count());
    assert!(app.ui.is_input_focused());
}

#[test]
fn complete_slash_command_lists_multiple_matches() {
    let mut app = create_test_app();
    app.ui.set_input_text("/p".into());

    let handled = app.complete_slash_command(80);
    assert!(handled);
    assert_eq!(app.ui.get_input_text(), "/p");
    assert_eq!(
        app.ui.status.as_deref(),
        Some("Commands: /persona, /preset, /provider")
    );
}

#[test]
fn complete_slash_command_reports_unknown_prefix() {
    let mut app = create_test_app();
    app.ui.set_input_text("/zzz".into());

    let handled = app.complete_slash_command(80);
    assert!(handled);
    assert_eq!(app.ui.get_input_text(), "/zzz");
    assert_eq!(app.ui.status.as_deref(), Some("No command matches '/zzz'"));
}

#[test]
fn complete_slash_command_completes_mcp_server() {
    let mut app = create_test_app();
    app.config.mcp_servers.push(McpServerConfig {
        id: "agpedia".to_string(),
        display_name: "Agpedia".to_string(),
        base_url: Some("https://mcp.example.com".to_string()),
        command: None,
        args: None,
        env: None,
        headers: None,
        transport: Some("streamable-http".to_string()),
        allowed_tools: None,
        protocol_version: None,
        enabled: Some(true),
        tool_payloads: None,
        tool_payload_window: None,
        yolo: None,
    });
    app.mcp = crate::mcp::client::McpClientManager::from_config(&app.config);

    app.ui.set_input_text("/mcp agp".into());
    app.ui.set_cursor_position("/mcp agp".chars().count());

    let handled = app.complete_slash_command(80);
    assert!(handled);
    assert_eq!(app.ui.get_input_text(), "/mcp agpedia ");
    assert_eq!(
        app.ui.get_input_cursor_position(),
        "/mcp agpedia ".chars().count()
    );
}

#[test]
fn complete_slash_command_completes_yolo_server() {
    let mut app = create_test_app();
    app.config.mcp_servers.push(McpServerConfig {
        id: "agpedia".to_string(),
        display_name: "Agpedia".to_string(),
        base_url: Some("https://mcp.example.com".to_string()),
        command: None,
        args: None,
        env: None,
        headers: None,
        transport: Some("streamable-http".to_string()),
        allowed_tools: None,
        protocol_version: None,
        enabled: Some(true),
        tool_payloads: None,
        tool_payload_window: None,
        yolo: None,
    });
    app.mcp = crate::mcp::client::McpClientManager::from_config(&app.config);

    app.ui.set_input_text("/yolo agp".into());
    app.ui.set_cursor_position("/yolo agp".chars().count());

    let handled = app.complete_slash_command(80);
    assert!(handled);
    assert_eq!(app.ui.get_input_text(), "/yolo agpedia ");
    assert_eq!(
        app.ui.get_input_cursor_position(),
        "/yolo agpedia ".chars().count()
    );
}

#[test]
fn complete_slash_command_lists_mcp_servers() {
    let mut app = create_test_app();
    app.config.mcp_servers.push(McpServerConfig {
        id: "agpedia".to_string(),
        display_name: "Agpedia".to_string(),
        base_url: Some("https://mcp.example.com".to_string()),
        command: None,
        args: None,
        env: None,
        headers: None,
        transport: Some("streamable-http".to_string()),
        allowed_tools: None,
        protocol_version: None,
        enabled: Some(true),
        tool_payloads: None,
        tool_payload_window: None,
        yolo: None,
    });
    app.config.mcp_servers.push(McpServerConfig {
        id: "alpha".to_string(),
        display_name: "Alpha".to_string(),
        base_url: Some("https://mcp.example.com".to_string()),
        command: None,
        args: None,
        env: None,
        headers: None,
        transport: Some("streamable-http".to_string()),
        allowed_tools: None,
        protocol_version: None,
        enabled: Some(true),
        tool_payloads: None,
        tool_payload_window: None,
        yolo: None,
    });
    app.mcp = crate::mcp::client::McpClientManager::from_config(&app.config);

    app.ui.set_input_text("/mcp a".into());
    app.ui.set_cursor_position("/mcp a".chars().count());

    let handled = app.complete_slash_command(80);
    assert!(handled);
    assert_eq!(app.ui.get_input_text(), "/mcp a");
    assert_eq!(
        app.ui.status.as_deref(),
        Some("MCP servers: agpedia, alpha")
    );
}

#[test]
fn test_last_and_first_user_message_index() {
    let mut app = create_test_app();
    // No messages
    assert_eq!(app.ui.last_user_message_index(), None);
    assert_eq!(app.ui.first_user_message_index(), None);

    // Add messages: user, assistant, user
    app.ui.messages.push_back(create_test_message("user", "u1"));
    app.ui
        .messages
        .push_back(create_test_message("assistant", "a1"));
    app.ui.messages.push_back(create_test_message("user", "u2"));

    assert_eq!(app.ui.first_user_message_index(), Some(0));
    assert_eq!(app.ui.last_user_message_index(), Some(2));
}

#[test]
fn test_scroll_height_consistency_with_tables_regression() {
    // Regression test: Ensures scroll height calculations match renderer height with tables.
    // Previously, scroll calculations could diverge from actual rendered height, causing
    // scroll targeting issues where the viewport wouldn't scroll to the correct position.
    let mut app = create_test_app();

    // Add a message with a large table that will trigger width-dependent wrapping
    let table_content = r#"| Government System | Definition | Key Properties |
|-------------------|------------|----------------|
| Democracy | A system where power is vested in the people, who rule either directly or through freely elected representatives. | Universal suffrage, Free and fair elections, Protection of civil liberties |
| Dictatorship | A form of government where a single person or a small group holds absolute power. | Centralized authority, Limited or no political opposition |
| Monarchy | A form of government in which a single person, known as a monarch, rules until death or abdication. | Hereditary succession, Often ceremonial with limited political power |
"#;

    app.ui.messages.push_back(Message {
        role: TranscriptRole::Assistant,
        content: table_content.to_string(),
    });

    let width = 80u16;

    // Get the height that the renderer will actually use (prewrapped with width)
    let renderer_height = {
        let lines = app.get_prewrapped_lines_cached(width);
        lines.len() as u16
    };

    // Get the height that scroll calculations currently use
    let scroll_height = app.ui.calculate_wrapped_line_count(width);

    // Verify heights match - mismatch would cause scroll targeting to be off
    assert_eq!(
        renderer_height, scroll_height,
        "Renderer height ({}) should match scroll calculation height ({})",
        renderer_height, scroll_height
    );
}

#[test]
fn streaming_table_autoscroll_stays_consistent() {
    // Test that autoscroll stays at bottom when streaming table content
    let mut app = create_test_app();

    // Start with a user message
    let width = 80u16;
    let available_height = 20u16;

    {
        let mut conversation = app.conversation();
        conversation.add_user_message("Generate a table".to_string());

        // Start streaming a table in chunks
        let table_start = "Here's a government systems table:\n\n";
        conversation.append_to_response(table_start, available_height, width);

        let table_header =
            "| Government System | Definition | Key Properties |\n|-------------------|------------|----------------|\n";
        conversation.append_to_response(table_header, available_height, width);

        // Add table rows that will cause wrapping and potentially height changes
        let row1 = "| Democracy | A system where power is vested in the people, who rule either directly or through freely elected representatives. | Universal suffrage, Free and fair elections |\n";
        conversation.append_to_response(row1, available_height, width);

        let row2 = "| Dictatorship | A form of government where a single person or a small group holds absolute power. | Centralized authority, Limited or no political opposition |\n";
        conversation.append_to_response(row2, available_height, width);
    }

    // After each append, if we're auto-scrolling, we should be at the bottom
    if app.ui.auto_scroll {
        let expected_max_scroll = app.ui.calculate_max_scroll_offset(available_height, width);
        assert_eq!(
            app.ui.scroll_offset, expected_max_scroll,
            "Auto-scroll should keep us at bottom. Current offset: {}, Expected max: {}",
            app.ui.scroll_offset, expected_max_scroll
        );
    }
}

#[test]
fn test_scroll_height_consistency_narrow_terminal_regression() {
    // Regression test: Verifies scroll height calculations in narrow terminals with tables.
    // Narrow terminals (40 chars) force aggressive table column rebalancing, which previously
    // caused scroll height calculations to diverge significantly from actual rendered height.
    // This edge case is critical for maintaining scroll accuracy in constrained environments.
    let mut app = create_test_app();

    // Add a wide table that will need significant rebalancing in narrow terminals
    let wide_table = r#"| Very Long Government System Name | Very Detailed Definition That Goes On And On | Extremely Detailed Key Properties That Include Many Words |
|-----------------------------------|-----------------------------------------------|------------------------------------------------------------|
| Constitutional Democratic Republic | A complex system where power is distributed among elected representatives who operate within a constitutional framework with checks and balances | Multi-party elections, separation of powers, constitutional limits, judicial review, civil liberties protection |
| Authoritarian Single-Party State | A centralized system where one political party maintains exclusive control over government institutions and suppresses opposition | Centralized control, restricted freedoms, state propaganda, limited political participation, strict social control |

Some additional text after the table."#;

    app.ui.messages.push_back(Message {
        role: TranscriptRole::Assistant,
        content: wide_table.to_string(),
    });

    // Use very narrow width that will force aggressive table column rebalancing
    let width = 40u16;

    // Get the height that the renderer will actually use (prewrapped with narrow width)
    let renderer_height = {
        let lines = app.get_prewrapped_lines_cached(width);
        lines.len() as u16
    };

    // Get the height that scroll calculations currently use (widthless, then scroll heuristic)
    let scroll_height = app.ui.calculate_wrapped_line_count(width);

    // Verify the fix is still in place - heights must match to ensure correct scroll positioning
    assert_eq!(
        renderer_height, scroll_height,
        "Narrow terminal: Renderer height ({}) should match scroll calculation height ({})",
        renderer_height, scroll_height
    );
}

#[test]
fn streaming_table_with_cache_invalidation_consistency() {
    // Test the exact scenario: streaming table generation with cache invalidation
    let mut app = create_test_app();

    let width = 80u16;
    let available_height = 20u16;

    // Start with user message and empty assistant response
    {
        let mut conversation = app.conversation();
        conversation.add_user_message("Generate a large comparison table".to_string());
    }

    // Simulate streaming a large table piece by piece, with cache invalidation
    let table_chunks = vec![
        "Here's a detailed comparison table:\n\n",
        "| Feature | Option A | Option B | Option C |\n",
        "|---------|----------|----------|----------|\n",
        "| Performance | Very fast execution with optimized algorithms | Moderate speed with good balance | Slower but more flexible |
",
        "| Memory Usage | Low memory footprint, efficient data structures | Medium usage with some overhead | Higher memory requirements |
",
        "| Ease of Use | Complex setup but powerful once configured | User-friendly with good documentation | Simple and intuitive interface |
",
        "| Cost | Enterprise pricing with volume discounts available | Reasonable pricing for small to medium teams | Free with optional premium features |
",
    ];

    for chunk in table_chunks {
        // Before append: get current scroll state
        let _scroll_before = app.ui.scroll_offset;
        let _max_scroll_before = app.ui.calculate_max_scroll_offset(available_height, width);

        // Append content (this invalidates prewrap cache)
        {
            let mut conversation = app.conversation();
            conversation.append_to_response(chunk, available_height, width);
        }

        // After append: check scroll consistency
        let scroll_after = app.ui.scroll_offset;
        let max_scroll_after = app.ui.calculate_max_scroll_offset(available_height, width);

        // During streaming with auto_scroll=true, we should always be at bottom
        if app.ui.auto_scroll {
            assert_eq!(
                scroll_after, max_scroll_after,
                "Auto-scroll should keep us at bottom after streaming chunk"
            );
        }

        // The key test: prewrap cache and scroll calculation should give same height
        let prewrap_height = app.get_prewrapped_lines_cached(width).len() as u16;
        let scroll_calc_height = app.ui.calculate_wrapped_line_count(width);

        assert_eq!(
            prewrap_height, scroll_calc_height,
            "After streaming chunk, prewrap height ({}) should match scroll calc height ({})",
            prewrap_height, scroll_calc_height
        );
    }
}

#[test]
fn test_page_up_down_and_home_end_behavior() {
    let mut app = create_test_app();
    // Create enough messages to require scrolling
    for _ in 0..50 {
        app.ui
            .messages
            .push_back(create_test_message("assistant", "line content"));
    }

    let width: u16 = 80;
    let input_area_height = 3u16; // pretend a small input area
    let term_height = 24u16;
    let available_height = {
        let conversation = app.conversation();
        conversation.calculate_available_height(term_height, input_area_height)
    };

    // Sanity: have some scrollable height
    let max_scroll = app.ui.calculate_max_scroll_offset(available_height, width);
    assert!(max_scroll > 0);

    // Start in the middle
    let step = available_height.saturating_sub(1);
    app.ui.scroll_offset = (step * 2).min(max_scroll);

    // Page up reduces by step, not below 0
    let before = app.ui.scroll_offset;
    app.ui.page_up(available_height);
    let after_up = app.ui.scroll_offset;
    assert_eq!(after_up, before.saturating_sub(step));
    assert!(!app.ui.auto_scroll);

    // Page down increases by step, clamped to max
    app.ui.page_down(available_height, width);
    let after_down = app.ui.scroll_offset;
    assert!(after_down >= after_up);
    assert!(after_down <= max_scroll);
    assert!(!app.ui.auto_scroll);

    // Home goes to top and disables auto-scroll
    app.ui.scroll_to_top();
    assert_eq!(app.ui.scroll_offset, 0);
    assert!(!app.ui.auto_scroll);

    // End goes to bottom and enables auto-scroll
    app.ui.scroll_to_bottom_view(available_height, width);
    assert_eq!(app.ui.scroll_offset, max_scroll);
    assert!(app.ui.auto_scroll);
}

#[test]
fn test_prev_next_user_message_index_navigation() {
    let mut app = create_test_app();
    // indices: 0 user, 1 assistant, 2 app, 3 user
    app.ui.messages.push_back(create_test_message("user", "u1"));
    app.ui
        .messages
        .push_back(create_test_message("assistant", "a1"));
    app.ui.messages.push_back(create_test_message_with_role(
        crate::core::message::TranscriptRole::AppInfo,
        "s1",
    ));
    app.ui.messages.push_back(create_test_message("user", "u2"));

    // From index 3 (user) prev should be 0 (skipping non-user)
    assert_eq!(app.ui.prev_user_message_index(3), Some(0));
    // From index 0 next should be 3 (skipping non-user)
    assert_eq!(app.ui.next_user_message_index(0), Some(3));
    // From index 1 prev should be 0
    assert_eq!(app.ui.prev_user_message_index(1), Some(0));
    // From index 1 next should be 3
    assert_eq!(app.ui.next_user_message_index(1), Some(3));
}

#[test]
fn test_set_input_text_places_cursor_at_end() {
    let mut app = create_test_app();
    let text = String::from("line1\nline2");
    app.ui.set_input_text(text.clone());
    // Linear cursor at end
    assert_eq!(app.ui.get_input_cursor_position(), text.chars().count());
    // Textarea cursor at end (last row/col)
    let (row, col) = app.ui.get_textarea_cursor();
    let lines_len = app.ui.get_textarea_line_count();
    assert_eq!(row, lines_len - 1);
    assert_eq!(col, app.ui.get_textarea_line_len(lines_len - 1));
}

#[test]
fn test_turn_off_character_mode_from_picker() {
    use crate::character::card::{CharacterCard, CharacterData};

    let mut app = create_test_app();

    let character = CharacterCard {
        spec: "chara_card_v2".to_string(),
        spec_version: "2.0".to_string(),
        data: CharacterData {
            name: "TestChar".to_string(),
            description: "Test".to_string(),
            personality: "Friendly".to_string(),
            scenario: "Testing".to_string(),
            first_mes: "Hello!".to_string(),
            mes_example: String::new(),
            creator_notes: None,
            system_prompt: None,
            post_history_instructions: None,
            alternate_greetings: None,
            tags: None,
            creator: None,
            character_version: None,
        },
    };

    app.session.set_character(character);
    assert!(app.session.active_character.is_some());

    app.picker.picker_session = Some(picker::PickerSession {
        state: PickerState::new(
            "Pick Character",
            vec![PickerItem {
                id: picker::TURN_OFF_CHARACTER_ID.to_string(),
                label: "[Turn off character mode]".to_string(),
                metadata: Some("Disable character".to_string()),
                inspect_metadata: Some("Disable character".to_string()),
                sort_key: None,
            }],
            0,
        ),
        data: picker::PickerData::Character(picker::CharacterPickerState {
            search_filter: String::new(),
            all_items: vec![],
        }),
    });

    app.apply_selected_character(false);

    assert!(app.session.active_character.is_none());
    assert_eq!(app.ui.status.as_deref(), Some("Character mode disabled"));
}

/// Helper to count code blocks in span metadata.
fn count_code_blocks_in_metadata(metadata: &[Vec<crate::ui::span::SpanKind>]) -> usize {
    let mut indices = std::collections::HashSet::new();
    for line_meta in metadata {
        for kind in line_meta {
            if let Some(meta) = kind.code_block_meta() {
                indices.insert(meta.block_index());
            }
        }
    }
    indices.len()
}

#[test]

fn block_selection_uses_cached_metadata() {
    use crate::ui::markdown::test_fixtures;

    let mut app = create_test_app();
    app.ui.messages.push_back(test_fixtures::multiple_blocks());

    // First render caches metadata
    let width = 80u16;
    let _lines = app.get_prewrapped_lines_cached(width);
    let ptr_before = app.get_prewrapped_span_metadata_cached(width) as *const _;

    // Count code block spans in cache
    let cached_blocks =
        count_code_blocks_in_metadata(app.get_prewrapped_span_metadata_cached(width));
    assert_eq!(cached_blocks, 3, "Should cache 3 code blocks");

    // Enter block select mode
    app.ui.enter_block_select_mode(0);

    // Navigation should not invalidate cache
    let ptr_after = app.get_prewrapped_span_metadata_cached(width) as *const _;
    assert_eq!(
        ptr_before, ptr_after,
        "Block navigation should reuse cached metadata"
    );
}

#[test]

fn cache_invalidates_on_message_change() {
    use crate::ui::markdown::test_fixtures;

    let mut app = create_test_app();
    app.ui.messages.push_back(test_fixtures::single_block());

    let width = 80u16;
    let metadata_before = app.get_prewrapped_span_metadata_cached(width);
    let lines_before = metadata_before.len();

    // Modify messages - add a message with multiple blocks
    app.ui.messages.push_back(test_fixtures::multiple_blocks());
    app.invalidate_prewrap_cache();

    let metadata_after = app.get_prewrapped_span_metadata_cached(width);
    let lines_after = metadata_after.len();

    // Cache should reflect the new messages (more lines)
    assert!(
        lines_after > lines_before,
        "Should have more lines after adding message: {} -> {}",
        lines_before,
        lines_after
    );
}

#[test]

fn metadata_contains_code_blocks_after_cache() {
    use crate::ui::markdown::test_fixtures;

    let mut app = create_test_app();
    app.ui.messages.push_back(test_fixtures::multiple_blocks());

    let width = 80u16;
    let metadata = app.get_prewrapped_span_metadata_cached(width);

    // Cached metadata should include code block metadata
    let has_code_blocks = metadata
        .iter()
        .flat_map(|line| line.iter())
        .any(|k| k.is_code_block());

    assert!(
        has_code_blocks,
        "Cached metadata should include code blocks"
    );
}