fresh-editor 0.2.11

A lightweight, fast terminal-based text editor with LSP support and TypeScript plugins
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
//! E2E tests for the keybinding editor modal

use crate::common::harness::EditorTestHarness;
use crossterm::event::{KeyCode, KeyModifiers};
use portable_pty::{native_pty_system, PtySize};

/// Helper to open the keybinding editor directly
fn open_keybinding_editor(harness: &mut EditorTestHarness) {
    harness.editor_mut().open_keybinding_editor();
    harness.render().unwrap();
}

// ========================
// Opening and closing
// ========================

/// Test opening the keybinding editor modal
#[test]
fn test_open_keybinding_editor() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    harness.render().unwrap();
    harness.assert_screen_not_contains("Keybinding Editor");

    open_keybinding_editor(&mut harness);

    harness.assert_screen_contains("Keybinding Editor");
    harness.assert_screen_contains("bindings");
    harness.assert_screen_contains("Config:");
}

/// Test closing the keybinding editor with Escape
#[test]
fn test_close_keybinding_editor_with_escape() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);
    harness.assert_screen_contains("Keybinding Editor");

    harness.send_key(KeyCode::Esc, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    harness.assert_screen_not_contains("Keybinding Editor");
}

// ========================
// Navigation
// ========================

/// Test navigating the binding list with arrow keys
#[test]
fn test_navigate_bindings_with_arrows() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    let screen_before = harness.screen_to_string();

    // Navigate down several times
    for _ in 0..5 {
        harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    }
    harness.render().unwrap();

    let screen_after = harness.screen_to_string();
    // Selection indicator should have moved (screen should differ)
    assert_ne!(
        screen_before, screen_after,
        "Selection should have moved after pressing Down"
    );

    // Navigate back up
    for _ in 0..3 {
        harness.send_key(KeyCode::Up, KeyModifiers::NONE).unwrap();
    }
    harness.render().unwrap();

    // Should still show the editor
    harness.assert_screen_contains("Keybinding Editor");
}

/// Test Home and End keys jump to first/last binding
#[test]
fn test_home_end_navigation() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Go to end
    harness.send_key(KeyCode::End, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    let screen_end = harness.screen_to_string();

    // Go to beginning
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    let screen_home = harness.screen_to_string();

    assert_ne!(
        screen_end, screen_home,
        "Home and End should show different parts of the list"
    );
}

/// Test PageUp and PageDown navigation
#[test]
fn test_page_up_down_navigation() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    let screen_initial = harness.screen_to_string();

    // Page down
    harness
        .send_key(KeyCode::PageDown, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();
    let screen_page_down = harness.screen_to_string();

    assert_ne!(
        screen_initial, screen_page_down,
        "PageDown should scroll the list"
    );

    // Page up should go back
    harness
        .send_key(KeyCode::PageUp, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();
}

// ========================
// Text search
// ========================

/// Test text search filters the binding list
#[test]
fn test_text_search() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Activate search
    harness
        .send_key(KeyCode::Char('/'), KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Type "save" to filter bindings
    for ch in "save".chars() {
        harness
            .send_key(KeyCode::Char(ch), KeyModifiers::NONE)
            .unwrap();
    }
    harness.render().unwrap();

    // Should show the search query
    harness.assert_screen_contains("save");
    // Should show "save" action in results
    let screen = harness.screen_to_string();
    assert!(
        screen.contains("save") || screen.contains("Save"),
        "Search for 'save' should show matching bindings"
    );
}

/// Test search persists after pressing Enter (unfocuses but stays visible)
#[test]
fn test_search_persists_after_enter() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Activate search and type query
    harness
        .send_key(KeyCode::Char('/'), KeyModifiers::NONE)
        .unwrap();
    for ch in "undo".chars() {
        harness
            .send_key(KeyCode::Char(ch), KeyModifiers::NONE)
            .unwrap();
    }
    harness.render().unwrap();
    harness.assert_screen_contains("undo");

    // Press Enter to unfocus search
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Search query should still be visible
    harness.assert_screen_contains("undo");
}

/// Test Escape cancels search and shows all bindings
#[test]
fn test_escape_cancels_search() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Activate search and type query
    harness
        .send_key(KeyCode::Char('/'), KeyModifiers::NONE)
        .unwrap();
    for ch in "save".chars() {
        harness
            .send_key(KeyCode::Char(ch), KeyModifiers::NONE)
            .unwrap();
    }
    harness.render().unwrap();

    // Press Escape to cancel search
    harness.send_key(KeyCode::Esc, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Search bar should be gone, but editor should still be open
    harness.assert_screen_contains("Keybinding Editor");
    // The bindings count should reflect all bindings again
    harness.assert_screen_contains("bindings");
}

/// Test search with Down arrow moves focus to list
#[test]
fn test_search_down_arrow_moves_to_list() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Activate search and type query
    harness
        .send_key(KeyCode::Char('/'), KeyModifiers::NONE)
        .unwrap();
    for ch in "copy".chars() {
        harness
            .send_key(KeyCode::Char(ch), KeyModifiers::NONE)
            .unwrap();
    }
    harness.render().unwrap();

    // Press Down to unfocus and navigate list
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Search query should remain visible
    harness.assert_screen_contains("copy");

    // Further Down keys should navigate in the list (not type in search)
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    harness.assert_screen_contains("Keybinding Editor");
}

// ========================
// Context and source filters
// ========================

/// Test cycling context filter
#[test]
fn test_context_filter_cycle() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Initially showing all contexts
    harness.assert_screen_contains("[All]");

    // Press 'c' to cycle context filter
    harness
        .send_key(KeyCode::Char('c'), KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Should now show a specific context (not All anymore, or cycled to next)
    let screen = harness.screen_to_string();
    // After first press, should show first specific context
    assert!(
        screen.contains("Context:"),
        "Should still show the Context label"
    );
}

/// Test cycling source filter
#[test]
fn test_source_filter_cycle() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Initially showing all sources
    harness.assert_screen_contains("[All]");

    // Press 's' to cycle source filter
    harness
        .send_key(KeyCode::Char('s'), KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Should show a filtered source
    let screen = harness.screen_to_string();
    assert!(
        screen.contains("Source:"),
        "Should still show the Source label"
    );
}

// ========================
// Help overlay
// ========================

/// Test opening and closing the help overlay
#[test]
fn test_help_overlay() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Open help with ?
    harness
        .send_key(KeyCode::Char('?'), KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Help overlay should be visible
    harness.assert_screen_contains("Keyboard Shortcuts");
    harness.assert_screen_contains("Navigation");

    // Close help with Escape
    harness.send_key(KeyCode::Esc, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Help should be gone, editor should still be open
    harness.assert_screen_not_contains("Keyboard Shortcuts");
    harness.assert_screen_contains("Keybinding Editor");
}

// ========================
// Edit dialog
// ========================

/// Test opening the edit dialog with Enter
#[test]
fn test_open_edit_dialog() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Press Enter to edit the selected binding
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Edit dialog should be visible
    harness.assert_screen_contains("Edit Keybinding");
    harness.assert_screen_contains("Key:");
    harness.assert_screen_contains("Action:");
    harness.assert_screen_contains("Context:");
    harness.assert_screen_contains("Save");
    harness.assert_screen_contains("Cancel");
}

/// Test closing the edit dialog with Escape
#[test]
fn test_close_edit_dialog_with_escape() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Open edit dialog
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();
    harness.assert_screen_contains("Edit Keybinding");

    // Close with Escape
    harness.send_key(KeyCode::Esc, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Dialog should be closed, editor still open
    harness.assert_screen_not_contains("Edit Keybinding");
    harness.assert_screen_contains("Keybinding Editor");
}

/// Test switching focus areas in the edit dialog with Tab
#[test]
fn test_edit_dialog_tab_focus() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Open edit dialog
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Tab through the focus areas (Key -> Action -> Context -> Buttons)
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    // Should still show the dialog
    harness.assert_screen_contains("Edit Keybinding");

    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    harness.assert_screen_contains("Edit Keybinding");

    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    harness.assert_screen_contains("Edit Keybinding");

    // Close
    harness.send_key(KeyCode::Esc, KeyModifiers::NONE).unwrap();
}

/// Test that Tab in the edit dialog cycles through ALL controls including
/// both Save and Cancel buttons, not just the button area as a single stop.
/// Full cycle: Key -> Action -> Context -> Save -> Cancel -> Key
#[test]
fn test_edit_dialog_tab_cycles_through_cancel() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Open edit dialog
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();
    harness.assert_screen_contains("Edit Keybinding");

    // Tab: Key(0) -> Action(1)
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    // Tab: Action(1) -> Context(2)
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    // Tab: Context(2) -> Buttons/Save(3, btn=0)
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    harness.assert_screen_contains("Edit Keybinding");

    // Tab: Save(3, btn=0) -> Cancel(3, btn=1)
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    // Cancel button should now be highlighted — pressing Enter now should close dialog
    harness.assert_screen_contains("Edit Keybinding");

    // Press Enter on Cancel — should close the dialog without saving
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();
    harness.assert_screen_not_contains("Edit Keybinding");

    // Keybinding editor should still be open (dialog closed, not the editor)
    harness.assert_screen_contains("Keybinding Editor");
}

// ========================
// Add binding dialog
// ========================

/// Test opening the add binding dialog with 'a'
#[test]
fn test_open_add_dialog() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Press 'a' to add a new binding
    harness
        .send_key(KeyCode::Char('a'), KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Add dialog should be visible
    harness.assert_screen_contains("Add Keybinding");
    harness.assert_screen_contains("Key:");
    harness.assert_screen_contains("Action:");
    harness.assert_screen_contains("Context:");
}

/// Test adding a new keybinding end-to-end
#[test]
fn test_add_new_binding() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Open add dialog
    harness
        .send_key(KeyCode::Char('a'), KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();
    harness.assert_screen_contains("Add Keybinding");

    // Record a key: press Ctrl+K (the dialog starts in RecordingKey mode)
    harness
        .send_key(KeyCode::Char('k'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();
    // The key should be shown
    harness.assert_screen_contains("Ctrl+K");

    // Tab to Action field
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Type action name "save"
    for ch in "save".chars() {
        harness
            .send_key(KeyCode::Char(ch), KeyModifiers::NONE)
            .unwrap();
    }
    harness.render().unwrap();

    // Accept autocomplete with Enter
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Tab to context, then to buttons
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Press Enter on Save button
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Should show modified indicator since we added a binding
    harness.assert_screen_contains("modified");
}

// ========================
// Delete binding
// ========================

/// Test that deleting a keymap binding creates a noop override
/// (disabling the binding rather than removing it from the keymap),
/// and that the original action appears as unbound in the table.
#[test]
fn test_delete_keymap_binding_creates_noop_override() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Search for "save" to find the Ctrl+S keymap binding
    harness
        .send_key(KeyCode::Char('/'), KeyModifiers::NONE)
        .unwrap();
    for ch in "save".chars() {
        harness
            .send_key(KeyCode::Char(ch), KeyModifiers::NONE)
            .unwrap();
    }
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Navigate to the keymap "save" binding (the one with Ctrl+S)
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    let mut found_keymap = false;
    for _ in 0..20 {
        let screen = harness.screen_to_string();
        for line in screen.lines() {
            if line.contains(">") && line.contains("keymap") && line.contains("Ctrl+S") {
                found_keymap = true;
                break;
            }
        }
        if found_keymap {
            break;
        }
        harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
        harness.render().unwrap();
    }
    assert!(
        found_keymap,
        "Should find the Ctrl+S save keymap binding in search results"
    );

    // Delete (override) the keymap binding
    harness
        .send_key(KeyCode::Char('d'), KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Should show status about keymap being overridden
    let screen = harness.screen_to_string();
    assert!(
        screen.contains("disabled") || screen.contains("override") || screen.contains("noop"),
        "Should show a status message about the keymap binding being disabled.\nScreen:\n{}",
        screen,
    );

    // The editor should be marked as modified
    assert!(
        screen.contains("modified"),
        "Editor should show [modified] after overriding a keymap binding"
    );

    // Cancel the "save" search so we can see all results.
    // Pressing Escape cancels the search.
    harness.send_key(KeyCode::Esc, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Now search for Ctrl+S (via record-key search) to find the noop override
    harness
        .send_key(KeyCode::Char('r'), KeyModifiers::NONE)
        .unwrap();
    harness
        .send_key(KeyCode::Char('s'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // The noop override should appear with "custom" source
    let screen = harness.screen_to_string();
    assert!(
        screen.contains("noop"),
        "After overriding keymap binding, Ctrl+S should show 'noop' action.\nScreen:\n{}",
        screen,
    );
    assert!(
        screen.contains("custom"),
        "The noop override should have 'custom' source.\nScreen:\n{}",
        screen,
    );

    // Cancel the record-key search and search for "save" again to verify
    // the original "save" action still appears as unbound (no key).
    harness.send_key(KeyCode::Esc, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    harness
        .send_key(KeyCode::Char('/'), KeyModifiers::NONE)
        .unwrap();
    // Search for "^save" specifically
    for ch in "save".chars() {
        harness
            .send_key(KeyCode::Char(ch), KeyModifiers::NONE)
            .unwrap();
    }
    harness.render().unwrap();

    // "save" action should still appear (now unbound — no key, no "keymap" source)
    let screen = harness.screen_to_string();
    assert!(
        screen.contains("save"),
        "The 'save' action should still appear in search results.\nScreen:\n{}",
        screen,
    );
    // There should be no "keymap" source for "save" (the keymap entry was overridden)
    // and Ctrl+S should not appear next to "save" (it's now bound to noop)
    // Check that a row has "save" as action but no key
    let has_unbound_save = screen
        .lines()
        .any(|line| line.contains("save") && !line.contains("Ctrl+S") && !line.contains("keymap"));
    assert!(
        has_unbound_save,
        "The 'save' action should appear without Ctrl+S key (unbound).\nScreen:\n{}",
        screen,
    );

    // Save and close
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();
    harness
        .send_key(KeyCode::Char('s'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();
    harness.assert_screen_not_contains("Keybinding Editor");

    // Ctrl+S should now have no effect (noop override is active).
    // Type something so we can verify save doesn't trigger.
    harness.type_text("x").unwrap();
    harness.render().unwrap();

    let buffer_before = harness.get_buffer_content().unwrap();

    // Press Ctrl+S — if the noop override works, this does nothing
    harness
        .send_key(KeyCode::Char('s'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    let buffer_after = harness.get_buffer_content().unwrap();
    assert_eq!(
        buffer_after, buffer_before,
        "Buffer content should be unchanged — Ctrl+S should be a noop now"
    );
}

/// Test that deleting an unbound action shows an error
#[test]
fn test_cannot_delete_unbound_action() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // The first visible row in the default sort is an unbound action
    // (unbound actions have empty context which sorts first)
    harness
        .send_key(KeyCode::Char('d'), KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Should show error about not being able to delete
    let screen = harness.screen_to_string();
    assert!(
        screen.contains("Cannot") || screen.contains("cannot") || screen.contains("delete"),
        "Should show a message about not being able to delete unbound actions.\nScreen:\n{}",
        screen,
    );
}

// ========================
// Unsaved changes confirmation
// ========================

/// Test unsaved changes confirmation dialog appears
#[test]
fn test_unsaved_changes_confirm_dialog() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Add a binding to create unsaved changes
    harness
        .send_key(KeyCode::Char('a'), KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Record key
    harness
        .send_key(KeyCode::Char('k'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Tab to action
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    for ch in "save".chars() {
        harness
            .send_key(KeyCode::Char(ch), KeyModifiers::NONE)
            .unwrap();
    }
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();

    // Tab to buttons and save
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Now press Esc - should show confirm dialog
    harness.send_key(KeyCode::Esc, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    harness.assert_screen_contains("Unsaved Changes");
    harness.assert_screen_contains("Save");
    harness.assert_screen_contains("Discard");
    harness.assert_screen_contains("Cancel");
}

/// Test canceling the confirm dialog returns to editor
#[test]
fn test_confirm_dialog_cancel() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Add a binding to create unsaved changes
    harness
        .send_key(KeyCode::Char('a'), KeyModifiers::NONE)
        .unwrap();
    harness
        .send_key(KeyCode::Char('k'), KeyModifiers::CONTROL)
        .unwrap();
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    for ch in "save".chars() {
        harness
            .send_key(KeyCode::Char(ch), KeyModifiers::NONE)
            .unwrap();
    }
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Press Esc to show confirm dialog
    harness.send_key(KeyCode::Esc, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    harness.assert_screen_contains("Unsaved Changes");

    // Press Esc again (or navigate to Cancel) to cancel
    harness.send_key(KeyCode::Esc, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Should be back in the editor
    harness.assert_screen_contains("Keybinding Editor");
    harness.assert_screen_not_contains("Unsaved Changes");
}

/// Test discarding changes via confirm dialog
#[test]
fn test_confirm_dialog_discard() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Add a binding to create unsaved changes
    harness
        .send_key(KeyCode::Char('a'), KeyModifiers::NONE)
        .unwrap();
    harness
        .send_key(KeyCode::Char('k'), KeyModifiers::CONTROL)
        .unwrap();
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    for ch in "save".chars() {
        harness
            .send_key(KeyCode::Char(ch), KeyModifiers::NONE)
            .unwrap();
    }
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Press Esc to show confirm dialog
    harness.send_key(KeyCode::Esc, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    harness.assert_screen_contains("Unsaved Changes");

    // Navigate to Discard button (Right from Save) and press Enter
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Editor should be closed
    harness.assert_screen_not_contains("Keybinding Editor");
}

// ========================
// Mouse interactions
// ========================

/// Test mouse scroll moves the selection
#[test]
fn test_mouse_scroll() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    let screen_before = harness.screen_to_string();

    // Scroll down in the center of the modal
    harness.mouse_scroll_down(60, 20).unwrap();
    harness.mouse_scroll_down(60, 20).unwrap();
    harness.mouse_scroll_down(60, 20).unwrap();

    let screen_after = harness.screen_to_string();
    assert_ne!(
        screen_before, screen_after,
        "Mouse scroll should move the selection"
    );
}

/// Test mouse click selects a table row
#[test]
fn test_mouse_click_selects_row() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    let screen_before = harness.screen_to_string();

    // Click on a row in the table area (approximately row 15 for a row in the middle)
    harness.mouse_click(60, 15).unwrap();
    harness.render().unwrap();

    let screen_after = harness.screen_to_string();
    assert_ne!(
        screen_before, screen_after,
        "Mouse click should select a different row"
    );
}

/// Test mouse events are masked (don't leak to underlying editor)
#[test]
fn test_mouse_events_masked() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();

    // Type some content in the editor first
    harness.type_text("Hello world").unwrap();
    harness.render().unwrap();

    open_keybinding_editor(&mut harness);

    // Click at position (5, 2) which would normally position cursor in the editor
    harness.mouse_click(5, 2).unwrap();
    harness.render().unwrap();

    // Keybinding editor should still be open (event was captured)
    harness.assert_screen_contains("Keybinding Editor");

    // Scroll at the same position
    harness.mouse_scroll_down(5, 2).unwrap();

    // Still in keybinding editor
    harness.assert_screen_contains("Keybinding Editor");
}

// ========================
// Record key search
// ========================

/// Test record key search mode
#[test]
fn test_record_key_search() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Press 'r' to start record key search
    harness
        .send_key(KeyCode::Char('r'), KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Should show record key search mode
    harness.assert_screen_contains("Record Key:");

    // Record a key combination (Ctrl+S)
    harness
        .send_key(KeyCode::Char('s'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Should show the recorded key and filter results
    harness.assert_screen_contains("Ctrl+S");
}

// ========================
// Saving changes
// ========================

/// Test saving changes with Ctrl+S
#[test]
fn test_save_changes_with_ctrl_s() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Add a binding to create changes
    harness
        .send_key(KeyCode::Char('a'), KeyModifiers::NONE)
        .unwrap();
    harness
        .send_key(KeyCode::Char('k'), KeyModifiers::CONTROL)
        .unwrap();
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    for ch in "save".chars() {
        harness
            .send_key(KeyCode::Char(ch), KeyModifiers::NONE)
            .unwrap();
    }
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();
    harness.assert_screen_contains("modified");

    // Save with Ctrl+S
    harness
        .send_key(KeyCode::Char('s'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Editor should close after saving
    harness.assert_screen_not_contains("Keybinding Editor");
}

// ========================
// Edit dialog field editing
// ========================

/// Test typing in the action field with autocomplete
#[test]
fn test_action_field_autocomplete() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Open add dialog
    harness
        .send_key(KeyCode::Char('a'), KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Record a key first
    harness
        .send_key(KeyCode::Char('k'), KeyModifiers::CONTROL)
        .unwrap();

    // Tab to action field
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Type partial action name
    for ch in "und".chars() {
        harness
            .send_key(KeyCode::Char(ch), KeyModifiers::NONE)
            .unwrap();
    }
    harness.render().unwrap();

    // Autocomplete suggestions should be visible
    let screen = harness.screen_to_string();
    assert!(
        screen.contains("undo"),
        "Autocomplete should show 'undo' suggestion for 'und'"
    );
}

/// Test context field cycling in edit dialog
#[test]
fn test_edit_dialog_context_cycling() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Open add dialog
    harness
        .send_key(KeyCode::Char('a'), KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Tab past key and action to context
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Default context should be "normal"
    harness.assert_screen_contains("normal");

    // Press Right to cycle context
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Should show a different context now
    let screen = harness.screen_to_string();
    assert!(
        screen.contains("prompt") || screen.contains("popup") || screen.contains("global"),
        "Context should have cycled to a different value"
    );

    // Close
    harness.send_key(KeyCode::Esc, KeyModifiers::NONE).unwrap();
}

// ========================
// Table content
// ========================

/// Test that the table shows expected columns
#[test]
fn test_table_shows_columns() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Check column headers
    harness.assert_screen_contains("Key");
    harness.assert_screen_contains("Action");
    harness.assert_screen_contains("Description");
    harness.assert_screen_contains("Context");
    harness.assert_screen_contains("Source");
}

/// Test that bindings count is displayed
#[test]
fn test_bindings_count_displayed() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Should show binding count
    harness.assert_screen_contains("bindings");
}

/// Test that footer hints are displayed
#[test]
fn test_footer_hints_displayed() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Check footer hints
    harness.assert_screen_contains("Edit");
    harness.assert_screen_contains("Add");
    harness.assert_screen_contains("Delete");
    harness.assert_screen_contains("Search");
    harness.assert_screen_contains("Help");
    harness.assert_screen_contains("Close");
}

// ========================
// Unicode / narrow terminal
// ========================

/// Test that the keybinding editor renders correctly at narrow widths
/// where key display strings containing multi-byte Unicode characters
/// (e.g. "Alt+Shift+↓") may be truncated by column width.
/// Regression test: pad_right used byte indexing which panics on
/// multi-byte char boundaries.
#[test]
fn test_render_narrow_terminal_unicode_keys() {
    // At width 80, key_col_width = floor(78 * 0.16) = 12.
    // "Alt+Shift+↓" is 13 bytes but ↓ spans bytes 10..13,
    // so byte index 12 is not a char boundary and would panic.
    let mut harness = EditorTestHarness::new(80, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Search for "block_select" to filter to bindings with Alt+Shift+arrow
    // keys (e.g. Alt+Shift+↓), ensuring they are in the visible viewport
    // when rendered with narrow column widths.
    harness
        .send_key(KeyCode::Char('/'), KeyModifiers::NONE)
        .unwrap();
    for ch in "block_select".chars() {
        harness
            .send_key(KeyCode::Char(ch), KeyModifiers::NONE)
            .unwrap();
    }
    // This render triggers pad_right on the filtered bindings which include
    // "Alt+Shift+↓" — at key_col_width=12 this panics on the byte boundary.
    harness.render().unwrap();

    // Should not panic and should display the editor
    harness.assert_screen_contains("Keybinding Editor");
}

// ========================
// Scroll / selection visibility
// ========================

/// Test that the selected item (">" indicator) stays visible no matter how
/// many times we press Down or Up. Regression test: editor.visible_rows was
/// hardcoded to 20 and never synced from the actual rendered viewport, so on
/// shorter terminals the selection would scroll out of view.
#[test]
fn test_selected_item_stays_visible_when_scrolling() {
    // Use a short terminal where visible rows in the table (~13) is much
    // less than the total number of bindings, forcing scroll to kick in.
    let mut harness = EditorTestHarness::new(120, 24).unwrap();
    open_keybinding_editor(&mut harness);

    // The ">" indicator marks the selected row. It must always be visible.
    harness.assert_screen_contains(">");

    // Press Down 40 times — well past the visible area.
    // After every key press the ">" indicator must remain on screen.
    for i in 0..40 {
        harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
        harness.render().unwrap();
        assert!(
            harness.screen_to_string().contains(">"),
            "Selection indicator '>' not visible after pressing Down {} times",
            i + 1,
        );
    }

    // Now press Up all the way back to the top.
    for i in 0..40 {
        harness.send_key(KeyCode::Up, KeyModifiers::NONE).unwrap();
        harness.render().unwrap();
        assert!(
            harness.screen_to_string().contains(">"),
            "Selection indicator '>' not visible after pressing Up {} times",
            i + 1,
        );
    }
}

// ========================
// Unbound actions
// ========================

/// Test that actions without a keybinding appear in the editor list
#[test]
fn test_unbound_actions_are_listed() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Search for "duplicate_line" which has no default keybinding
    harness
        .send_key(KeyCode::Char('/'), KeyModifiers::NONE)
        .unwrap();
    for ch in "duplicate_line".chars() {
        harness
            .send_key(KeyCode::Char(ch), KeyModifiers::NONE)
            .unwrap();
    }
    harness.render().unwrap();

    // The unbound action should appear in search results
    let screen = harness.screen_to_string();
    assert!(
        screen.contains("duplicate_line") || screen.contains("Duplicate"),
        "Unbound action 'duplicate_line' should be listed in the keybinding editor"
    );
}

/// Test that an unbound action can be edited (assign a key to it)
#[test]
fn test_edit_unbound_action() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Search for "duplicate_line" (unbound action)
    harness
        .send_key(KeyCode::Char('/'), KeyModifiers::NONE)
        .unwrap();
    for ch in "duplicate_line".chars() {
        harness
            .send_key(KeyCode::Char(ch), KeyModifiers::NONE)
            .unwrap();
    }
    // Press Enter to unfocus search, then navigate to first result
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Press Enter to open edit dialog on the unbound action
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Edit dialog should open
    harness.assert_screen_contains("Edit Keybinding");
    harness.assert_screen_contains("Key:");
    harness.assert_screen_contains("Action:");

    // The action field should show "duplicate_line"
    harness.assert_screen_contains("duplicate_line");

    // Record a key (dialog starts in RecordingKey mode)
    harness
        .send_key(
            KeyCode::Char('d'),
            KeyModifiers::CONTROL | KeyModifiers::SHIFT,
        )
        .unwrap();
    harness.render().unwrap();

    // Tab to context, then to Save button
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Press Enter to save
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Dialog should be closed and editor should show modified
    harness.assert_screen_not_contains("Edit Keybinding");
    harness.assert_screen_contains("modified");
}

/// Test that deleting a custom binding makes the action appear as unbound
#[test]
fn test_deleted_binding_appears_as_unbound() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // First, add a custom binding for "duplicate_line"
    harness
        .send_key(KeyCode::Char('a'), KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();
    harness.assert_screen_contains("Add Keybinding");

    // Record key: Ctrl+Shift+D
    harness
        .send_key(
            KeyCode::Char('d'),
            KeyModifiers::CONTROL | KeyModifiers::SHIFT,
        )
        .unwrap();
    harness.render().unwrap();

    // Tab to action field and type "duplicate_line"
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    for ch in "duplicate_line".chars() {
        harness
            .send_key(KeyCode::Char(ch), KeyModifiers::NONE)
            .unwrap();
    }
    // Accept autocomplete
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Tab to context, then to Save button
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Press Enter to save
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Should show modified
    harness.assert_screen_contains("modified");

    // Now search for "duplicate_line" and filter to custom source
    harness
        .send_key(KeyCode::Char('/'), KeyModifiers::NONE)
        .unwrap();
    for ch in "duplicate_line".chars() {
        harness
            .send_key(KeyCode::Char(ch), KeyModifiers::NONE)
            .unwrap();
    }
    // Enter to unfocus search
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // The custom binding should be visible
    let screen = harness.screen_to_string();
    assert!(
        screen.contains("custom"),
        "Custom binding for duplicate_line should show 'custom' source"
    );

    // Navigate to the custom binding row (it should be one of the filtered results)
    // Go to the first result which should be the custom one (or the unbound one)
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Find the custom one - cycle through results to find the "custom" row
    // Look for the line with "custom" to know which row has the custom binding
    let mut found_custom = false;
    for _ in 0..5 {
        let current_screen = harness.screen_to_string();
        // Check if the selected row (marked with ">") contains "custom"
        for line in current_screen.lines() {
            if line.contains(">") && line.contains("custom") {
                found_custom = true;
                break;
            }
        }
        if found_custom {
            break;
        }
        harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
        harness.render().unwrap();
    }

    // Delete the custom binding
    harness
        .send_key(KeyCode::Char('d'), KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Now clear the search to see all results for duplicate_line
    harness.send_key(KeyCode::Esc, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Search again to find duplicate_line
    harness
        .send_key(KeyCode::Char('/'), KeyModifiers::NONE)
        .unwrap();
    for ch in "duplicate_line".chars() {
        harness
            .send_key(KeyCode::Char(ch), KeyModifiers::NONE)
            .unwrap();
    }
    harness.render().unwrap();

    // duplicate_line should still appear (now as unbound - no "custom" or "keymap" source)
    let screen = harness.screen_to_string();
    assert!(
        screen.contains("duplicate_line") || screen.contains("Duplicate"),
        "After deleting the custom binding, duplicate_line should still appear as unbound"
    );
    // The "custom" source label should be gone (it's now unbound with empty source)
    assert!(
        !screen.contains("custom"),
        "After deleting the custom binding, there should be no 'custom' source for duplicate_line"
    );
}

/// Test the full delete flow end-to-end:
/// 1. Add a custom binding (Ctrl+Shift+D → duplicate_line), save
/// 2. Verify the binding works (key performs the action) and appears in the table
/// 3. Delete the binding, save
/// 4. Verify the table shows the action without a bound key
/// 5. Verify the key no longer performs the action
#[test]
fn test_delete_binding_full_flow() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();

    // === Phase 1: Add a custom binding Ctrl+Shift+D → duplicate_line ===
    open_keybinding_editor(&mut harness);

    // Press 'a' to open Add dialog
    harness
        .send_key(KeyCode::Char('a'), KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();
    harness.assert_screen_contains("Add Keybinding");

    // Record key: Ctrl+Shift+D
    harness
        .send_key(
            KeyCode::Char('d'),
            KeyModifiers::CONTROL | KeyModifiers::SHIFT,
        )
        .unwrap();
    harness.render().unwrap();
    harness.assert_screen_contains("Ctrl+Shift+D");

    // Tab to Action field
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();

    // Type "duplicate_line" and accept autocomplete
    for ch in "duplicate_line".chars() {
        harness
            .send_key(KeyCode::Char(ch), KeyModifiers::NONE)
            .unwrap();
    }
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Tab to context, then to Save button, press Enter to save the dialog
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();
    harness.assert_screen_contains("modified");

    // Save and close keybinding editor with Ctrl+S
    harness
        .send_key(KeyCode::Char('s'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();
    harness.assert_screen_not_contains("Keybinding Editor");

    // === Phase 2: Verify binding works (before delete) ===
    // Type some content
    harness.type_text("aaa").unwrap();
    harness.render().unwrap();

    // Move cursor to start of buffer
    harness
        .send_key(KeyCode::Home, KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Press Ctrl+Shift+D to duplicate the line
    harness
        .send_key(
            KeyCode::Char('d'),
            KeyModifiers::CONTROL | KeyModifiers::SHIFT,
        )
        .unwrap();
    harness.render().unwrap();

    let buffer_content = harness.get_buffer_content().unwrap();
    assert_eq!(
        buffer_content, "aaa\naaa",
        "Before delete: Ctrl+Shift+D should duplicate the line (binding is active)"
    );

    // === Phase 3: Verify binding in table, then delete it ===
    open_keybinding_editor(&mut harness);

    // Search for "duplicate_line"
    harness
        .send_key(KeyCode::Char('/'), KeyModifiers::NONE)
        .unwrap();
    for ch in "duplicate_line".chars() {
        harness
            .send_key(KeyCode::Char(ch), KeyModifiers::NONE)
            .unwrap();
    }
    // Enter to unfocus search
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Verify the custom binding appears in the table
    let screen = harness.screen_to_string();
    assert!(
        screen.contains("Ctrl+Shift+D"),
        "Before delete: table should show Ctrl+Shift+D for the binding"
    );
    assert!(
        screen.contains("custom"),
        "Before delete: table should show 'custom' source"
    );

    // Navigate to the custom binding row
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    let mut found_custom = false;
    for _ in 0..10 {
        let current_screen = harness.screen_to_string();
        for line in current_screen.lines() {
            if line.contains(">") && line.contains("custom") {
                found_custom = true;
                break;
            }
        }
        if found_custom {
            break;
        }
        harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
        harness.render().unwrap();
    }
    assert!(found_custom, "Should find the custom binding row to delete");

    // Delete the custom binding
    harness
        .send_key(KeyCode::Char('d'), KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Immediately after delete (before saving/closing): the table should
    // already reflect the removal — the action appears as unbound with no key.
    let screen = harness.screen_to_string();
    assert!(
        screen.contains("duplicate_line") || screen.contains("Duplicate"),
        "After delete (before save): action should still be listed"
    );
    assert!(
        !screen.contains("Ctrl+Shift+D"),
        "After delete (before save): Ctrl+Shift+D should be gone from the table immediately"
    );
    assert!(
        !screen.contains("custom"),
        "After delete (before save): 'custom' source should be gone immediately"
    );

    // Save and close keybinding editor with Ctrl+S
    harness
        .send_key(KeyCode::Char('s'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();
    harness.assert_screen_not_contains("Keybinding Editor");

    // === Phase 4: After delete - verify table shows action without key ===
    open_keybinding_editor(&mut harness);

    // Search for "duplicate_line"
    harness
        .send_key(KeyCode::Char('/'), KeyModifiers::NONE)
        .unwrap();
    for ch in "duplicate_line".chars() {
        harness
            .send_key(KeyCode::Char(ch), KeyModifiers::NONE)
            .unwrap();
    }
    harness.render().unwrap();

    let screen = harness.screen_to_string();
    // The action should still appear (as unbound)
    assert!(
        screen.contains("duplicate_line") || screen.contains("Duplicate"),
        "After delete: action should still be listed in the table"
    );
    // The key should NOT appear in the table
    assert!(
        !screen.contains("Ctrl+Shift+D"),
        "After delete: Ctrl+Shift+D should NOT appear in the table (binding was deleted)"
    );
    // The source should NOT show "custom"
    assert!(
        !screen.contains("custom"),
        "After delete: 'custom' source should NOT appear"
    );

    // Close keybinding editor
    harness.send_key(KeyCode::Esc, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // === Phase 5: After delete - verify key has no effect ===
    // Buffer still has "aaa\naaa" from Phase 2. Record it, then press the
    // key and verify the buffer is unchanged (binding no longer active).
    let buffer_before = harness.get_buffer_content().unwrap();

    // Press Ctrl+Shift+D - should have NO effect since the binding was deleted
    harness
        .send_key(
            KeyCode::Char('d'),
            KeyModifiers::CONTROL | KeyModifiers::SHIFT,
        )
        .unwrap();
    harness.render().unwrap();

    let buffer_after = harness.get_buffer_content().unwrap();
    assert_eq!(
        buffer_after, buffer_before,
        "After delete: Ctrl+Shift+D should have no effect (binding was removed)"
    );
}

/// Test that adding a binding with a key that's already used shows a conflict warning
#[test]
fn test_add_binding_conflict_warning_for_existing_key() {
    let mut harness = EditorTestHarness::new(120, 40).unwrap();
    open_keybinding_editor(&mut harness);

    // Press 'a' to open Add dialog
    harness
        .send_key(KeyCode::Char('a'), KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();
    harness.assert_screen_contains("Add Keybinding");

    // Record a key that is already in use: Ctrl+S (bound to "save" in the keymap)
    harness
        .send_key(KeyCode::Char('s'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Should show the recorded key
    harness.assert_screen_contains("Ctrl+S");

    // Should show a conflict warning since Ctrl+S is already bound
    let screen = harness.screen_to_string();
    assert!(
        screen.contains("Conflict"),
        "Should show conflict warning when using a key already bound to another action.\nScreen:\n{}",
        screen
    );
}

// ========================
// Terminal mode interaction
// ========================

/// Test that when terminal mode is active, opening the keybinding editor
/// captures key input — keys go to the editor, not the terminal PTY.
/// Regression test: dispatch_terminal_input's in_modal check didn't include
/// keybinding_editor.is_some(), so keys were swallowed by terminal mode.
#[test]
fn test_keybinding_editor_captures_keys_over_terminal_mode() {
    // Skip if PTY not available
    if native_pty_system()
        .openpty(PtySize {
            rows: 1,
            cols: 1,
            pixel_width: 0,
            pixel_height: 0,
        })
        .is_err()
    {
        eprintln!("Skipping test: PTY not available");
        return;
    }

    let mut harness = EditorTestHarness::new(120, 40).unwrap();

    // Open a terminal — this enters terminal mode automatically
    harness.editor_mut().open_terminal();
    harness.render().unwrap();
    assert!(
        harness.editor().is_terminal_mode(),
        "Should be in terminal mode after opening terminal"
    );

    // Now open the keybinding editor modal
    open_keybinding_editor(&mut harness);
    harness.assert_screen_contains("Keybinding Editor");

    // The ">" selection indicator should be visible at the first row
    harness.assert_screen_contains(">");

    // Press Down several times — these keys should go to the keybinding editor
    // (moving the selection), NOT to the terminal PTY.
    for _ in 0..3 {
        harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    }
    harness.render().unwrap();

    // The editor should still be visible and the selection should have moved
    harness.assert_screen_contains("Keybinding Editor");
    // The ">" indicator must still be on screen (moved down)
    harness.assert_screen_contains(">");

    // Press Escape — should close the editor, not be eaten by terminal
    harness.send_key(KeyCode::Esc, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Keybinding editor should be closed now
    harness.assert_screen_not_contains("Keybinding Editor");
}