fresh-editor 0.1.74

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
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
//! E2E tests for diff view cursor and navigation interactions
//!
//! These tests verify that cursor movement, pane switching, mouse clicks,
//! and selections work correctly in the side-by-side diff view.
//!
//! Test coverage includes all combinations of:
//! - Line length: empty, short (< pane width), long (> pane width)
//! - Vertical file length: short (no scroll), long (requires scroll)
//! - Cursor position: start/mid/end of line, start/mid/end of buffer
//! - Movement directions: left/right/up/down in all positions

use crate::common::git_test_helper::GitTestRepo;
use crate::common::harness::{copy_plugin, copy_plugin_lib, EditorTestHarness};
use crate::common::tracing::init_tracing_from_env;
use crossterm::event::{KeyCode, KeyModifiers, MouseButton, MouseEvent, MouseEventKind};
use fresh::config::Config;
use std::fs;
use tracing::info;

/// Helper to copy audit_mode plugin and its dependencies to the test repo
fn setup_audit_mode_plugin(repo: &GitTestRepo) {
    let plugins_dir = repo.path.join("plugins");
    fs::create_dir_all(&plugins_dir).expect("Failed to create plugins directory");
    copy_plugin(&plugins_dir, "audit_mode");
    copy_plugin_lib(&plugins_dir);
}

/// Check if we're in the diff view.
/// We check for "*Diff:" in tab bar and "OLD (HEAD)" header which are visible
/// at any viewport width. The "Side-by-side diff:" status message may be truncated
/// in narrow viewports.
fn is_in_diff_view(screen: &str) -> bool {
    let has_diff_tab = screen.contains("*Diff:");
    let has_old_header = screen.contains("OLD (HEAD)");
    let has_full_status = screen.contains("Side-by-side diff:");
    (has_diff_tab && has_old_header) || has_full_status
}

/// Helper to open the side-by-side diff view
fn open_side_by_side_diff(harness: &mut EditorTestHarness) {
    info!("open_side_by_side_diff: sending Ctrl+p");
    harness
        .send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
        .unwrap();
    info!("open_side_by_side_diff: waiting for prompt");
    harness.wait_for_prompt().unwrap();
    info!("open_side_by_side_diff: typing 'Side-by-Side Diff'");
    harness.type_text("Side-by-Side Diff").unwrap();
    harness.render().unwrap();
    info!("open_side_by_side_diff: sending Enter");
    harness
        .send_key(KeyCode::Enter, KeyModifiers::NONE)
        .unwrap();
    info!("open_side_by_side_diff: waiting for prompt closed");
    harness.wait_for_prompt_closed().unwrap();
    info!("open_side_by_side_diff: prompt closed, waiting for diff view to load");

    // Wait for side-by-side view to fully load
    harness
        .wait_until(|h| {
            let screen = h.screen_to_string();
            if screen.contains("TypeError")
                || screen.contains("Error:")
                || screen.contains("Failed")
            {
                panic!("Error loading side-by-side diff. Screen:\n{}", screen);
            }
            let still_loading = screen.contains("Loading side-by-side diff");
            !still_loading && is_in_diff_view(&screen)
        })
        .unwrap();
    info!("open_side_by_side_diff: diff view loaded");
}

/// Create a repo with various line lengths for comprehensive testing
/// Returns paths to original and modified content
fn create_repo_with_varied_lines(repo: &GitTestRepo) {
    // Create initial file with varied line lengths
    let file_path = repo.path.join("test.rs");
    let original_content = r#"
short
this is a medium length line for testing
this is a very long line that extends well beyond the visible viewport width and requires horizontal scrolling to see the entire content of this particular line which is intentionally made very long for testing purposes

another short
medium line here with some content
"#;
    fs::write(&file_path, original_content).expect("Failed to create file");
    repo.git_add_all();
    repo.git_commit("Initial commit");

    // Modify with changes to various line lengths
    let modified_content = r#"
short_modified
this is a MODIFIED medium length line for testing
this is a very long MODIFIED line that extends well beyond the visible viewport width and requires horizontal scrolling to see the entire content of this particular line which is intentionally made very long for testing purposes and now even longer

another short MOD
medium line here with some MODIFIED content
added new line
"#;
    fs::write(&file_path, modified_content).expect("Failed to modify file");
}

/// Create a repo with a long file that requires vertical scrolling
fn create_repo_with_long_file(repo: &GitTestRepo) {
    let file_path = repo.path.join("long.rs");

    // Create a file with many lines
    let mut original = String::new();
    for i in 0..100 {
        original.push_str(&format!("// Original line {}\n", i));
    }
    fs::write(&file_path, &original).expect("Failed to create file");
    repo.git_add_all();
    repo.git_commit("Initial commit");

    // Modify some lines throughout the file
    let mut modified = String::new();
    for i in 0..100 {
        if i == 10 || i == 50 || i == 90 {
            modified.push_str(&format!("// MODIFIED line {} with extra content\n", i));
        } else if i == 25 {
            // Add extra lines here
            modified.push_str("// Inserted line A\n");
            modified.push_str("// Inserted line B\n");
            modified.push_str(&format!("// Original line {}\n", i));
        } else {
            modified.push_str(&format!("// Original line {}\n", i));
        }
    }
    fs::write(&file_path, modified).expect("Failed to modify file");
}

/// Create a repo with empty lines for edge case testing
fn create_repo_with_empty_lines(repo: &GitTestRepo) {
    let file_path = repo.path.join("empty.rs");

    let original = "first line\n\n\nfourth line\n";
    fs::write(&file_path, original).expect("Failed to create file");
    repo.git_add_all();
    repo.git_commit("Initial commit");

    // Modify to add content to empty lines
    let modified = "first line\nsecond line added\n\nfourth line modified\n";
    fs::write(&file_path, modified).expect("Failed to modify file");
}

/// Create a repo with a short file (no vertical scroll needed)
fn create_repo_short_file(repo: &GitTestRepo) {
    let file_path = repo.path.join("short.rs");

    let original = "line 1\nline 2\nline 3\n";
    fs::write(&file_path, original).expect("Failed to create file");
    repo.git_add_all();
    repo.git_commit("Initial commit");

    let modified = "line 1 modified\nline 2\nline 3 changed\n";
    fs::write(&file_path, modified).expect("Failed to modify file");
}

// =============================================================================
// COMPREHENSIVE MOVEMENT TESTS
// =============================================================================

/// Test cursor movement on empty lines (line length = 0)
#[test]
fn test_diff_cursor_empty_lines() {
    let repo = GitTestRepo::new();
    create_repo_with_empty_lines(&repo);
    setup_audit_mode_plugin(&repo);

    let file_path = repo.path.join("empty.rs");

    let mut harness = EditorTestHarness::with_config_and_working_dir(
        160,
        40,
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();

    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();

    open_side_by_side_diff(&mut harness);

    // Navigate to an empty line
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Try moving right on empty line - should stay at position 0
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Try End on empty line - should stay at position 0
    harness.send_key(KeyCode::End, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Try Home on empty line
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    let screen = harness.screen_to_string();
    assert!(
        is_in_diff_view(&screen),
        "Should still be in diff view after empty line navigation"
    );
}

/// Test cursor at start of line - moving left should not move, right should work
#[test]
fn test_diff_cursor_at_line_start() {
    let repo = GitTestRepo::new();
    create_repo_with_varied_lines(&repo);
    setup_audit_mode_plugin(&repo);

    let file_path = repo.path.join("test.rs");

    let mut harness = EditorTestHarness::with_config_and_working_dir(
        160,
        40,
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();

    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();
    harness
        .wait_until(|h| h.screen_to_string().contains("short"))
        .unwrap();

    open_side_by_side_diff(&mut harness);

    // Go to line with content
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Ensure we're at start of line
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Moving left at start should not crash or move cursor
    harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Moving right should work
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    let screen = harness.screen_to_string();
    assert!(is_in_diff_view(&screen), "Should still be in diff view");
}

/// Test cursor at end of line - moving right should not move past end
#[test]
fn test_diff_cursor_at_line_end() {
    let repo = GitTestRepo::new();
    create_repo_with_varied_lines(&repo);
    setup_audit_mode_plugin(&repo);

    let file_path = repo.path.join("test.rs");

    let mut harness = EditorTestHarness::with_config_and_working_dir(
        160,
        40,
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();

    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();
    harness
        .wait_until(|h| h.screen_to_string().contains("short"))
        .unwrap();

    open_side_by_side_diff(&mut harness);

    // Go to short line
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Go to end of line
    harness.send_key(KeyCode::End, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Moving right at end should not move cursor past end
    for _ in 0..5 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::NONE)
            .unwrap();
        harness.render().unwrap();
    }

    // Moving left from end should work
    harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    let screen = harness.screen_to_string();
    assert!(is_in_diff_view(&screen), "Should still be in diff view");
}

/// Test cursor at middle of line - both directions should work
#[test]
fn test_diff_cursor_at_line_middle() {
    let repo = GitTestRepo::new();
    create_repo_with_varied_lines(&repo);
    setup_audit_mode_plugin(&repo);

    let file_path = repo.path.join("test.rs");

    let mut harness = EditorTestHarness::with_config_and_working_dir(
        160,
        40,
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();

    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();
    harness
        .wait_until(|h| h.screen_to_string().contains("medium"))
        .unwrap();

    open_side_by_side_diff(&mut harness);

    // Go to medium length line
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Move to middle of line
    for _ in 0..10 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::NONE)
            .unwrap();
    }
    harness.render().unwrap();

    // Move left
    for _ in 0..3 {
        harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    }
    harness.render().unwrap();

    // Move right
    for _ in 0..5 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::NONE)
            .unwrap();
    }
    harness.render().unwrap();

    let screen = harness.screen_to_string();
    assert!(is_in_diff_view(&screen), "Should still be in diff view");
}

/// Test cursor at first row of buffer - up should not crash
#[test]
fn test_diff_cursor_at_buffer_start() {
    let repo = GitTestRepo::new();
    create_repo_short_file(&repo);
    setup_audit_mode_plugin(&repo);

    let file_path = repo.path.join("short.rs");

    let mut harness = EditorTestHarness::with_config_and_working_dir(
        160,
        40,
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();

    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();

    open_side_by_side_diff(&mut harness);

    // Cursor should be at first row
    // Try moving up - should stay at first row
    harness.send_key(KeyCode::Up, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Move down should work
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Ctrl+Home should go to start
    harness
        .send_key(KeyCode::Home, KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    let screen = harness.screen_to_string();
    assert!(is_in_diff_view(&screen), "Should still be in diff view");
}

/// Test cursor at last row of buffer - down should not crash
#[test]
fn test_diff_cursor_at_buffer_end() {
    let repo = GitTestRepo::new();
    create_repo_short_file(&repo);
    setup_audit_mode_plugin(&repo);

    let file_path = repo.path.join("short.rs");

    let mut harness = EditorTestHarness::with_config_and_working_dir(
        160,
        40,
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();

    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();

    open_side_by_side_diff(&mut harness);

    // Go to end of buffer
    harness
        .send_key(KeyCode::End, KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Try moving down - should stay at last row
    for _ in 0..5 {
        harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    }
    harness.render().unwrap();

    // Move up should work
    harness.send_key(KeyCode::Up, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    let screen = harness.screen_to_string();
    assert!(is_in_diff_view(&screen), "Should still be in diff view");
}

/// Test horizontal scroll on long lines (line > pane width)
#[test]
fn test_diff_horizontal_scroll_long_line() {
    init_tracing_from_env();
    info!("Starting test_diff_horizontal_scroll_long_line");

    let repo = GitTestRepo::new();
    info!("Created git test repo");
    create_repo_with_varied_lines(&repo);
    info!("Created repo with varied lines");
    setup_audit_mode_plugin(&repo);
    info!("Set up audit_mode plugin");

    let file_path = repo.path.join("test.rs");

    let mut harness = EditorTestHarness::with_config_and_working_dir(
        80, // Narrower to trigger horizontal scroll
        40,
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();
    info!("Created editor test harness");

    harness.open_file(&file_path).unwrap();
    info!("Opened file");
    harness.render().unwrap();
    info!("Rendered");
    harness
        .wait_until(|h| h.screen_to_string().contains("long"))
        .unwrap();
    info!("File content visible with 'long'");

    open_side_by_side_diff(&mut harness);
    info!("Opened side-by-side diff");

    // Go to the very long line
    for _ in 0..3 {
        harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    }
    harness.render().unwrap();

    let screen_before = harness.screen_to_string();
    println!("Before horizontal scroll:\n{}", screen_before);

    // Move right many times to trigger horizontal scroll
    for _ in 0..50 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::NONE)
            .unwrap();
    }
    harness.render().unwrap();

    let screen_after_right = harness.screen_to_string();
    println!("After scrolling right:\n{}", screen_after_right);

    // Move to end of line
    harness.send_key(KeyCode::End, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    let screen_at_end = harness.screen_to_string();
    println!("At end of line:\n{}", screen_at_end);

    // Move back to start
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    let screen_at_home = harness.screen_to_string();
    println!("Back at home:\n{}", screen_at_home);

    assert!(
        is_in_diff_view(&screen_at_home),
        "Should still be in diff view"
    );
}

/// Test vertical scroll with long file
#[test]
fn test_diff_vertical_scroll_long_file() {
    let repo = GitTestRepo::new();
    create_repo_with_long_file(&repo);
    setup_audit_mode_plugin(&repo);

    let file_path = repo.path.join("long.rs");

    let mut harness = EditorTestHarness::with_config_and_working_dir(
        160,
        30, // Shorter to trigger vertical scroll
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();

    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();

    open_side_by_side_diff(&mut harness);

    let screen_initial = harness.screen_to_string();
    println!("Initial view:\n{}", screen_initial);

    // Page down to scroll
    harness
        .send_key(KeyCode::PageDown, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    let screen_after_pagedown = harness.screen_to_string();
    println!("After PageDown:\n{}", screen_after_pagedown);

    // Go to end of buffer
    harness
        .send_key(KeyCode::End, KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    let screen_at_end = harness.screen_to_string();
    println!("At buffer end:\n{}", screen_at_end);

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

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

    let screen_at_start = harness.screen_to_string();
    println!("Back at buffer start:\n{}", screen_at_start);

    assert!(
        is_in_diff_view(&screen_at_start),
        "Should still be in diff view"
    );
}

/// Test cursor movement in viewport middle (not at edge)
#[test]
fn test_diff_cursor_viewport_middle() {
    let repo = GitTestRepo::new();
    create_repo_with_long_file(&repo);
    setup_audit_mode_plugin(&repo);

    let file_path = repo.path.join("long.rs");

    let mut harness = EditorTestHarness::with_config_and_working_dir(
        160,
        40,
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();

    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();

    open_side_by_side_diff(&mut harness);

    // Move to middle of viewport
    for _ in 0..10 {
        harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    }
    harness.render().unwrap();

    // Move in all directions from middle
    for _ in 0..3 {
        harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    }
    harness.render().unwrap();

    for _ in 0..2 {
        harness.send_key(KeyCode::Up, KeyModifiers::NONE).unwrap();
    }
    harness.render().unwrap();

    for _ in 0..5 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::NONE)
            .unwrap();
    }
    harness.render().unwrap();

    for _ in 0..3 {
        harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    }
    harness.render().unwrap();

    let screen = harness.screen_to_string();
    assert!(is_in_diff_view(&screen), "Should still be in diff view");
}

/// Test word movement (Ctrl+Left/Right) with various line content
#[test]
fn test_diff_word_movement_comprehensive() {
    let repo = GitTestRepo::new();
    create_repo_with_varied_lines(&repo);
    setup_audit_mode_plugin(&repo);

    let file_path = repo.path.join("test.rs");

    let mut harness = EditorTestHarness::with_config_and_working_dir(
        160,
        40,
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();

    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();
    harness
        .wait_until(|h| h.screen_to_string().contains("medium"))
        .unwrap();

    open_side_by_side_diff(&mut harness);

    // Go to medium length line
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Word right from start
    for _ in 0..4 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::CONTROL)
            .unwrap();
        harness.render().unwrap();
    }

    // Word left back
    for _ in 0..2 {
        harness
            .send_key(KeyCode::Left, KeyModifiers::CONTROL)
            .unwrap();
        harness.render().unwrap();
    }

    // Go to end with Ctrl+Right repeatedly
    for _ in 0..20 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::CONTROL)
            .unwrap();
    }
    harness.render().unwrap();

    // Word left from end
    harness
        .send_key(KeyCode::Left, KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Go to start with Ctrl+Left repeatedly
    for _ in 0..20 {
        harness
            .send_key(KeyCode::Left, KeyModifiers::CONTROL)
            .unwrap();
    }
    harness.render().unwrap();

    let screen = harness.screen_to_string();
    assert!(is_in_diff_view(&screen), "Should still be in diff view");
}

/// Test Tab key switches between panes
#[test]
fn test_diff_pane_switching_with_tab() {
    let repo = GitTestRepo::new();
    create_repo_with_varied_lines(&repo);
    setup_audit_mode_plugin(&repo);

    let file_path = repo.path.join("test.rs");

    let mut harness = EditorTestHarness::with_config_and_working_dir(
        160,
        40,
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();

    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();

    open_side_by_side_diff(&mut harness);

    // Move to a line with content
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Switch to NEW pane with Tab
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Move cursor in NEW pane
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Switch back to OLD pane
    harness.send_key(KeyCode::Tab, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Move cursor in OLD pane
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    let screen = harness.screen_to_string();
    assert!(is_in_diff_view(&screen), "Should still be in diff view");
}

/// Test mouse click places cursor correctly in both panes
#[test]
fn test_diff_mouse_click_both_panes() {
    let repo = GitTestRepo::new();
    create_repo_with_varied_lines(&repo);
    setup_audit_mode_plugin(&repo);

    let file_path = repo.path.join("test.rs");

    let mut harness = EditorTestHarness::with_config_and_working_dir(
        160,
        40,
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();

    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();

    open_side_by_side_diff(&mut harness);

    // Click on left pane (OLD) at various positions
    harness
        .send_mouse(MouseEvent {
            kind: MouseEventKind::Down(MouseButton::Left),
            column: 10,
            row: 5,
            modifiers: KeyModifiers::NONE,
        })
        .unwrap();
    harness.render().unwrap();
    harness
        .send_mouse(MouseEvent {
            kind: MouseEventKind::Up(MouseButton::Left),
            column: 10,
            row: 5,
            modifiers: KeyModifiers::NONE,
        })
        .unwrap();
    harness.render().unwrap();

    // Click on right pane (NEW)
    harness
        .send_mouse(MouseEvent {
            kind: MouseEventKind::Down(MouseButton::Left),
            column: 100,
            row: 5,
            modifiers: KeyModifiers::NONE,
        })
        .unwrap();
    harness.render().unwrap();
    harness
        .send_mouse(MouseEvent {
            kind: MouseEventKind::Up(MouseButton::Left),
            column: 100,
            row: 5,
            modifiers: KeyModifiers::NONE,
        })
        .unwrap();
    harness.render().unwrap();

    // Click past end of line (should clamp to line end)
    harness
        .send_mouse(MouseEvent {
            kind: MouseEventKind::Down(MouseButton::Left),
            column: 70, // Past end of short line on left pane
            row: 3,
            modifiers: KeyModifiers::NONE,
        })
        .unwrap();
    harness.render().unwrap();
    harness
        .send_mouse(MouseEvent {
            kind: MouseEventKind::Up(MouseButton::Left),
            column: 70,
            row: 3,
            modifiers: KeyModifiers::NONE,
        })
        .unwrap();
    harness.render().unwrap();

    // Click on empty line
    harness
        .send_mouse(MouseEvent {
            kind: MouseEventKind::Down(MouseButton::Left),
            column: 20,
            row: 2, // Might be an empty line
            modifiers: KeyModifiers::NONE,
        })
        .unwrap();
    harness.render().unwrap();
    harness
        .send_mouse(MouseEvent {
            kind: MouseEventKind::Up(MouseButton::Left),
            column: 20,
            row: 2,
            modifiers: KeyModifiers::NONE,
        })
        .unwrap();
    harness.render().unwrap();

    let screen = harness.screen_to_string();
    assert!(is_in_diff_view(&screen), "Should still be in diff view");
}

/// Test selection with Shift+Arrow in various positions
#[test]
fn test_diff_selection_comprehensive() {
    let repo = GitTestRepo::new();
    create_repo_with_varied_lines(&repo);
    setup_audit_mode_plugin(&repo);

    let file_path = repo.path.join("test.rs");

    let mut harness = EditorTestHarness::with_config_and_working_dir(
        160,
        40,
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();

    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();

    open_side_by_side_diff(&mut harness);

    // Go to line with content
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Select right from start of line
    for _ in 0..5 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::SHIFT)
            .unwrap();
    }
    harness.render().unwrap();

    // Extend selection down
    harness
        .send_key(KeyCode::Down, KeyModifiers::SHIFT)
        .unwrap();
    harness.render().unwrap();

    // Extend selection left
    for _ in 0..3 {
        harness
            .send_key(KeyCode::Left, KeyModifiers::SHIFT)
            .unwrap();
    }
    harness.render().unwrap();

    // Clear selection by moving without shift
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Select word right
    harness
        .send_key(KeyCode::Right, KeyModifiers::SHIFT | KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Select to end of line
    harness.send_key(KeyCode::End, KeyModifiers::SHIFT).unwrap();
    harness.render().unwrap();

    // Select to start of line
    harness
        .send_key(KeyCode::Home, KeyModifiers::SHIFT)
        .unwrap();
    harness.render().unwrap();

    let screen = harness.screen_to_string();
    assert!(is_in_diff_view(&screen), "Should still be in diff view");
}

/// Test combined movement: down then right, up then left, etc.
#[test]
fn test_diff_combined_movement() {
    let repo = GitTestRepo::new();
    create_repo_with_varied_lines(&repo);
    setup_audit_mode_plugin(&repo);

    let file_path = repo.path.join("test.rs");

    let mut harness = EditorTestHarness::with_config_and_working_dir(
        160,
        40,
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();

    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();

    open_side_by_side_diff(&mut harness);

    // Move diagonally: down-right
    for _ in 0..5 {
        harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
        harness
            .send_key(KeyCode::Right, KeyModifiers::NONE)
            .unwrap();
        harness.render().unwrap();
    }

    // Move diagonally: up-left
    for _ in 0..3 {
        harness.send_key(KeyCode::Up, KeyModifiers::NONE).unwrap();
        harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
        harness.render().unwrap();
    }

    // End then down
    harness.send_key(KeyCode::End, KeyModifiers::NONE).unwrap();
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Home then up
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness.send_key(KeyCode::Up, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    let screen = harness.screen_to_string();
    assert!(is_in_diff_view(&screen), "Should still be in diff view");
}

/// Test no scroll needed (short file that fits in viewport)
#[test]
fn test_diff_no_scroll_needed() {
    let repo = GitTestRepo::new();
    create_repo_short_file(&repo);
    setup_audit_mode_plugin(&repo);

    let file_path = repo.path.join("short.rs");

    let mut harness = EditorTestHarness::with_config_and_working_dir(
        160,
        40, // Tall enough that short file doesn't need scroll
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();

    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();

    open_side_by_side_diff(&mut harness);

    // Move through entire file
    harness
        .send_key(KeyCode::End, KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    harness
        .send_key(KeyCode::Home, KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // PageDown/Up should work even if no scroll needed
    harness
        .send_key(KeyCode::PageDown, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

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

    let screen = harness.screen_to_string();
    assert!(is_in_diff_view(&screen), "Should still be in diff view");
}

/// Test Home/End keys on each line type (empty, short, long)
#[test]
fn test_diff_home_end_all_line_types() {
    init_tracing_from_env();
    info!("Starting test_diff_home_end_all_line_types");

    let repo = GitTestRepo::new();
    info!("Created git test repo");
    create_repo_with_varied_lines(&repo);
    info!("Created repo with varied lines");
    setup_audit_mode_plugin(&repo);
    info!("Set up audit_mode plugin");

    let file_path = repo.path.join("test.rs");

    let mut harness = EditorTestHarness::with_config_and_working_dir(
        80, // Narrow enough to require horizontal scroll on long lines
        40,
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();
    info!("Created editor test harness");

    harness.open_file(&file_path).unwrap();
    info!("Opened file");
    harness.render().unwrap();
    info!("Rendered");

    open_side_by_side_diff(&mut harness);
    info!("Opened side-by-side diff");

    // Test 1: Empty line (first line might be empty in our test file)
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();
    harness.send_key(KeyCode::End, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Test 2: Short line (line 2: "short")
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Home on short line
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // End on short line
    harness.send_key(KeyCode::End, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Home again to verify
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Test 3: Medium line
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

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

    // Test 4: Very long line (requires horizontal scroll)
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    let screen_at_start = harness.screen_to_string();
    println!("Long line at start:\n{}", screen_at_start);

    // End on long line - should scroll horizontally
    harness.send_key(KeyCode::End, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    let screen_at_end = harness.screen_to_string();
    println!("Long line at end:\n{}", screen_at_end);

    // Home on long line - should scroll back
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    let screen_back_home = harness.screen_to_string();
    println!("Long line back at home:\n{}", screen_back_home);

    // Test 5: Empty line again
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

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

    let screen = harness.screen_to_string();
    assert!(is_in_diff_view(&screen), "Should still be in diff view");
}

/// Test cursor visibility: cursor should always be visible after movement
#[test]
fn test_diff_cursor_always_visible() {
    init_tracing_from_env();
    info!("Starting test_diff_cursor_always_visible");

    let repo = GitTestRepo::new();
    info!("Created git test repo");
    create_repo_with_long_file(&repo);
    info!("Created repo with long file");
    setup_audit_mode_plugin(&repo);
    info!("Set up audit_mode plugin");

    let file_path = repo.path.join("long.rs");

    let mut harness = EditorTestHarness::with_config_and_working_dir(
        80, // Narrow to test horizontal visibility
        20, // Short to test vertical visibility
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();
    info!("Created editor test harness");

    harness.open_file(&file_path).unwrap();
    info!("Opened file");
    harness.render().unwrap();
    info!("Rendered");

    open_side_by_side_diff(&mut harness);
    info!("Opened side-by-side diff");

    // Go to end of buffer - cursor should be visible
    harness
        .send_key(KeyCode::End, KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();
    let screen = harness.screen_to_string();
    assert!(
        is_in_diff_view(&screen),
        "Should still be in diff view at buffer end"
    );

    // Go to start of buffer - cursor should be visible
    harness
        .send_key(KeyCode::Home, KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Move right many times - cursor should remain visible (viewport scrolls)
    for _ in 0..30 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::NONE)
            .unwrap();
    }
    harness.render().unwrap();
    let screen = harness.screen_to_string();
    assert!(
        is_in_diff_view(&screen),
        "Should still be in diff view after horizontal scroll"
    );

    // Move left back to start - cursor should remain visible
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    let screen = harness.screen_to_string();
    assert!(is_in_diff_view(&screen), "Should still be in diff view");
}

// =============================================================================
// Line Wrap Tests - cursor should wrap to next/prev line at boundaries
// =============================================================================

/// Create a test repo with simple multi-line content for wrap testing
fn create_repo_for_wrap_test(repo: &GitTestRepo) {
    let file_path = repo.path.join("wrap.txt");
    // OLD version: 3 lines with specific lengths
    let old_content = "first\nsecond\nthird\n";
    fs::write(&file_path, old_content).unwrap();

    repo.git_add(&["wrap.txt"]);
    repo.git_commit("Initial");

    // NEW version: same lines but modified
    let new_content = "first_mod\nsecond_mod\nthird_mod\n";
    fs::write(&file_path, new_content).unwrap();
}

/// Test that pressing Right at end of line moves to start of next line
#[test]
fn test_diff_cursor_wrap_right_to_next_line() {
    init_tracing_from_env();

    let repo = GitTestRepo::new();
    create_repo_for_wrap_test(&repo);
    setup_audit_mode_plugin(&repo);

    let file_path = repo.path.join("wrap.txt");

    let mut harness = EditorTestHarness::with_config_and_working_dir(
        120,
        40,
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();

    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();

    open_side_by_side_diff(&mut harness);

    // Go to first content line (row 0 is header, row 1 is "first")
    // Ctrl+Home goes to row 0, Down goes to row 1 (first content)
    harness
        .send_key(KeyCode::Home, KeyModifiers::CONTROL)
        .unwrap();
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Go to end of first content line
    harness.send_key(KeyCode::End, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Verify we're at line 2 (row 1 displays as Ln 2)
    let screen = harness.screen_to_string();
    assert!(
        screen.contains("Ln 2,") || screen.contains("Ln 2 "),
        "Should be on line 2, got: {}",
        screen
    );

    // Press Right - should wrap to start of line 3 (row 2, "second")
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    let screen = harness.screen_to_string();
    assert!(
        screen.contains("Ln 3,") || screen.contains("Ln 3 "),
        "Should be on line 3 after wrapping, got: {}",
        screen
    );

    // Should be at column 1 (start of line)
    assert!(
        screen.contains("Col 1") || screen.contains(", Col 1"),
        "Should be at column 1 after wrapping, got: {}",
        screen
    );
}

/// Test that pressing Left at start of line moves to end of previous line
#[test]
fn test_diff_cursor_wrap_left_to_prev_line() {
    init_tracing_from_env();

    let repo = GitTestRepo::new();
    create_repo_for_wrap_test(&repo);
    setup_audit_mode_plugin(&repo);

    let file_path = repo.path.join("wrap.txt");

    let mut harness = EditorTestHarness::with_config_and_working_dir(
        120,
        40,
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();

    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();

    open_side_by_side_diff(&mut harness);

    // Go to third line (row 0 is header, row 1 is "first", row 2 is "second")
    // We need to go Down twice to get to "second" content line
    harness
        .send_key(KeyCode::Home, KeyModifiers::CONTROL)
        .unwrap();
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Go to start of line (column 1)
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Verify we're at line 3 (row 2 displays as Ln 3)
    let screen = harness.screen_to_string();
    assert!(
        screen.contains("Ln 3,") || screen.contains("Ln 3 "),
        "Should be on line 3, got: {}",
        screen
    );

    // Press Left - should wrap to end of line 2 (the "first" line, displayed as Ln 2)
    harness.send_key(KeyCode::Left, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    let screen = harness.screen_to_string();
    assert!(
        screen.contains("Ln 2,") || screen.contains("Ln 2 "),
        "Should be on line 2 after wrapping, got: {}",
        screen
    );

    // Should be at end of line 2 (column 6 for "first" which has 5 chars, cursor at end is col 6)
    // Note: Column might be displayed as 5 or 6 depending on 0-indexed vs 1-indexed
    assert!(
        screen.contains("Col 5") || screen.contains("Col 6"),
        "Should be at end of line (col 5 or 6), got: {}",
        screen
    );
}

/// Test that word movement (Ctrl+Right/Left) wraps at line boundaries
#[test]
fn test_diff_cursor_word_wrap_at_boundaries() {
    init_tracing_from_env();

    let repo = GitTestRepo::new();
    create_repo_for_wrap_test(&repo);
    setup_audit_mode_plugin(&repo);

    let file_path = repo.path.join("wrap.txt");

    let mut harness = EditorTestHarness::with_config_and_working_dir(
        120,
        40,
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();

    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();

    open_side_by_side_diff(&mut harness);

    // Go to first content line (row 1), end
    harness
        .send_key(KeyCode::Home, KeyModifiers::CONTROL)
        .unwrap();
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.send_key(KeyCode::End, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Verify we're at line 2 (row 1 = Ln 2)
    let screen = harness.screen_to_string();
    assert!(
        screen.contains("Ln 2,") || screen.contains("Ln 2 "),
        "Should be on line 2, got: {}",
        screen
    );

    // Ctrl+Right at end of line should go to next line (line 3)
    harness
        .send_key(KeyCode::Right, KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    let screen = harness.screen_to_string();
    assert!(
        screen.contains("Ln 3,") || screen.contains("Ln 3 "),
        "Ctrl+Right at end should go to line 3, got: {}",
        screen
    );

    // Go back to line 3 start
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Ctrl+Left at start of line should go to previous line (line 2)
    harness
        .send_key(KeyCode::Left, KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    let screen = harness.screen_to_string();
    assert!(
        screen.contains("Ln 2,") || screen.contains("Ln 2 "),
        "Ctrl+Left at start should go to line 2, got: {}",
        screen
    );
}

// =============================================================================
// Horizontal Scroll Tests - viewport should scroll to keep cursor visible
// =============================================================================

/// Test that moving character-by-character along a long line keeps cursor visible
/// by scrolling the viewport horizontally
#[test]
fn test_diff_horizontal_scroll_keeps_cursor_visible() {
    init_tracing_from_env();

    let repo = GitTestRepo::new();
    create_repo_with_varied_lines(&repo);
    setup_audit_mode_plugin(&repo);

    let file_path = repo.path.join("test.rs");

    // Use narrow viewport (80 cols) to ensure scrolling is needed
    // Each pane is roughly 40 columns wide minus gutter
    let mut harness = EditorTestHarness::with_config_and_working_dir(
        80,
        30,
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();

    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();

    open_side_by_side_diff(&mut harness);

    // Navigate to the long line (line 4 in gutter = row 3 in display alignment)
    // The line is: "this is a very long MODIFIED line that extends..."
    // Content lines in original: empty, short, medium, long, empty, another short, medium
    // So the long line is at source line 3 (0-indexed), displayed as line 4 (1-indexed)
    harness
        .send_key(KeyCode::Home, KeyModifiers::CONTROL)
        .unwrap();

    // After Ctrl+Home, cursor is on first content line (Ln 1, empty in old file)
    // Content lines: Ln 1 (empty), Ln 2 (short), Ln 3 (medium), Ln 4 (long)
    // Move down 3 times: Ln 1 -> Ln 2 -> Ln 3 -> Ln 4 (the long line)
    for _ in 0..3 {
        harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    }
    harness.render().unwrap();

    // Check we're on the right line by looking at the status bar
    let screen = harness.screen_to_string();
    assert!(
        screen.contains("Ln 4,") || screen.contains("Ln 4 "),
        "Should be on line 4 (the long line). Screen:\n{}",
        screen
    );

    // Now move right character by character and verify cursor stays visible
    // After moving past the visible width, the start of line should scroll off
    // We'll check that "MODIFIED" becomes visible as we scroll right

    // First, move to start of line
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Verify we're still on line 4 after pressing Home
    let screen = harness.screen_to_string();
    assert!(
        screen.contains("Ln 4,") || screen.contains("Ln 4 "),
        "After Home, should still be on line 4. Screen:\n{}",
        screen
    );

    // Initial state: "this is a very long MODIFIED" - we can see "this" at start
    assert!(
        screen.contains("this"),
        "At line start, should see 'this'. Screen:\n{}",
        screen
    );

    // Move right past the visible portion - "MODIFIED" is around column 20+
    // Moving right 40+ times should scroll the view
    // Note: Use fewer iterations to stay on the same line (don't wrap to next line)
    for _ in 0..40 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::NONE)
            .unwrap();
    }
    harness.render().unwrap();

    // Verify we're still on line 4
    let screen = harness.screen_to_string();
    assert!(
        screen.contains("Ln 4,"),
        "After 40 right moves, should still be on line 4. Screen:\n{}",
        screen
    );

    assert!(
        is_in_diff_view(&screen),
        "Should still be in diff view after right moves. Screen:\n{}",
        screen
    );

    // After moving 50 chars right, "MODIFIED" should be visible
    let screen = harness.screen_to_string();
    assert!(
        screen.contains("MODIFIED"),
        "After scrolling right, should see 'MODIFIED'. Screen:\n{}",
        screen
    );

    // The start of line "this" should have scrolled off
    assert!(
        !screen.contains("this is a very"),
        "After scrolling right, 'this is a very' should have scrolled off. Screen:\n{}",
        screen
    );

    // Move to end of line - this goes to end of focused pane's line (OLD pane)
    // which is shorter than the NEW pane's line
    harness.send_key(KeyCode::End, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    let screen = harness.screen_to_string();
    // At end of OLD line (~216 chars), we should see content around that position
    // The screen should show content from around position 180-216 (visible_width ~35)
    // This includes "testing purposes" which appears in both lines around that position
    assert!(
        screen.contains("testing purposes") || screen.contains("testing"),
        "At line end, should see 'testing purposes' (near end of line). Screen:\n{}",
        screen
    );

    // Move back to start
    harness.send_key(KeyCode::Home, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    let screen = harness.screen_to_string();
    // Back at start, should see "this" again
    assert!(
        screen.contains("this"),
        "Back at line start, should see 'this'. Screen:\n{}",
        screen
    );
}

// =============================================================================
// Selection and Copy Tests
// =============================================================================

/// Test that moving without shift clears the selection
/// We verify this by: select text, copy, move without shift, copy again,
/// then paste into a prompt. If selection was cleared, the second copy should
/// produce nothing new.
#[test]
fn test_diff_move_without_shift_clears_selection() {
    init_tracing_from_env();

    let repo = GitTestRepo::new();
    create_repo_for_wrap_test(&repo);
    setup_audit_mode_plugin(&repo);

    let file_path = repo.path.join("wrap.txt");

    let mut harness = EditorTestHarness::with_config_and_working_dir(
        120,
        40,
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();

    // Isolate clipboard to prevent parallel test interference
    harness.editor_mut().set_clipboard_for_test("".to_string());

    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();

    open_side_by_side_diff(&mut harness);

    // Navigate to content line
    harness
        .send_key(KeyCode::Home, KeyModifiers::CONTROL)
        .unwrap();
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Select some text with Shift+Right (select "fir" - 3 chars from "first")
    for _ in 0..3 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::SHIFT)
            .unwrap();
    }
    harness.render().unwrap();

    // Copy selection - should copy "fir"
    harness
        .send_key(KeyCode::Char('c'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Now move without shift - this should clear the selection
    harness
        .send_key(KeyCode::Right, KeyModifiers::NONE)
        .unwrap();
    harness.render().unwrap();

    // Clear clipboard and try to copy again - should copy nothing since selection is cleared
    harness.editor_mut().set_clipboard_for_test("".to_string());
    harness
        .send_key(KeyCode::Char('c'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Close diff view with 'q' and wait until it's closed
    harness
        .send_key(KeyCode::Char('q'), KeyModifiers::NONE)
        .unwrap();
    harness
        .wait_until(|h| !is_in_diff_view(&h.screen_to_string()))
        .unwrap();

    // Open command palette and paste
    harness
        .send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
        .unwrap();
    harness.wait_for_prompt().unwrap();

    harness
        .send_key(KeyCode::Char('v'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // The second copy should have copied nothing (selection was cleared)
    // So the paste should result in empty prompt
    let screen = harness.screen_to_string();
    let prompt_line = screen
        .lines()
        .find(|l| l.contains("Command:"))
        .unwrap_or("");
    assert!(
        !prompt_line.contains("fir"),
        "After move without shift, selection should be cleared. Prompt: {}",
        prompt_line
    );
}

/// Test that copy in diff view doesn't include extra empty lines between lines
/// We verify by copying multiple lines from diff view, then pasting into prompt
#[test]
fn test_diff_copy_no_empty_lines() {
    init_tracing_from_env();

    let repo = GitTestRepo::new();
    // Create file with multiple lines
    let file_path = repo.path.join("multiline.txt");
    let original_content = "line one\nline two\nline three\n";
    fs::write(&file_path, original_content).expect("Failed to create file");
    repo.git_add_all();
    repo.git_commit("Initial commit");

    let modified_content = "line one modified\nline two modified\nline three modified\n";
    fs::write(&file_path, modified_content).expect("Failed to modify file");

    setup_audit_mode_plugin(&repo);

    let mut harness = EditorTestHarness::with_config_and_working_dir(
        120,
        40,
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();

    // Isolate clipboard
    harness.editor_mut().set_clipboard_for_test("".to_string());

    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();

    open_side_by_side_diff(&mut harness);

    // Go to first content line
    harness
        .send_key(KeyCode::Home, KeyModifiers::CONTROL)
        .unwrap();
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Select multiple lines with Shift+Down
    harness
        .send_key(KeyCode::Down, KeyModifiers::SHIFT)
        .unwrap();
    harness
        .send_key(KeyCode::Down, KeyModifiers::SHIFT)
        .unwrap();
    harness.render().unwrap();

    // Copy with Ctrl+C
    harness
        .send_key(KeyCode::Char('c'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Close diff view with 'q' and wait until it's closed
    harness
        .send_key(KeyCode::Char('q'), KeyModifiers::NONE)
        .unwrap();
    harness
        .wait_until(|h| !is_in_diff_view(&h.screen_to_string()))
        .unwrap();

    // Paste into prompt to verify content
    harness
        .send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
        .unwrap();
    harness.wait_for_prompt().unwrap();

    harness
        .send_key(KeyCode::Char('v'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Check the prompt shows the pasted content
    // The content should have "line one" and "line two" without extra blank lines between
    let screen = harness.screen_to_string();
    let prompt_line = screen
        .lines()
        .find(|l| l.contains("Command:"))
        .unwrap_or("");

    // Should contain line content (verifies copy worked)
    assert!(
        prompt_line.contains("line"),
        "Should contain copied line content. Prompt: {}",
        prompt_line
    );
}

/// Test that copy in diff view doesn't clear the selection
/// We verify by: select, copy, extend selection with Shift, copy again
/// If selection was preserved, the second copy should have more content
#[test]
fn test_diff_copy_preserves_selection() {
    init_tracing_from_env();

    let repo = GitTestRepo::new();
    create_repo_for_wrap_test(&repo);
    setup_audit_mode_plugin(&repo);

    let file_path = repo.path.join("wrap.txt");

    let mut harness = EditorTestHarness::with_config_and_working_dir(
        120,
        40,
        Config::default(),
        repo.path.clone(),
    )
    .unwrap();

    // Isolate clipboard
    harness.editor_mut().set_clipboard_for_test("".to_string());

    harness.open_file(&file_path).unwrap();
    harness.render().unwrap();

    open_side_by_side_diff(&mut harness);

    // Navigate to content line
    harness
        .send_key(KeyCode::Home, KeyModifiers::CONTROL)
        .unwrap();
    harness.send_key(KeyCode::Down, KeyModifiers::NONE).unwrap();
    harness.render().unwrap();

    // Select 3 characters with Shift+Right (select "fir")
    for _ in 0..3 {
        harness
            .send_key(KeyCode::Right, KeyModifiers::SHIFT)
            .unwrap();
    }
    harness.render().unwrap();

    // Copy with Ctrl+C
    harness
        .send_key(KeyCode::Char('c'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Extend selection with Shift+Right (now selecting "firs")
    harness
        .send_key(KeyCode::Right, KeyModifiers::SHIFT)
        .unwrap();
    harness.render().unwrap();

    // Copy again - should now have 4 characters if selection was preserved
    harness
        .send_key(KeyCode::Char('c'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Close diff view with 'q' and wait until it's closed
    harness
        .send_key(KeyCode::Char('q'), KeyModifiers::NONE)
        .unwrap();
    harness
        .wait_until(|h| !is_in_diff_view(&h.screen_to_string()))
        .unwrap();

    // Paste into prompt to verify what was copied
    harness
        .send_key(KeyCode::Char('p'), KeyModifiers::CONTROL)
        .unwrap();
    harness.wait_for_prompt().unwrap();

    harness
        .send_key(KeyCode::Char('v'), KeyModifiers::CONTROL)
        .unwrap();
    harness.render().unwrap();

    // Should have copied 4 characters: "firs" (first 4 chars of "first")
    let screen = harness.screen_to_string();
    let prompt_line = screen
        .lines()
        .find(|l| l.contains("Command:"))
        .unwrap_or("");
    assert!(
        prompt_line.contains("firs"),
        "Should have 4 chars after extending selection post-copy. Prompt: {}",
        prompt_line
    );
}