kizu 0.7.0

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

fn render_buffer(app: &App, w: u16, h: u16) -> ratatui::buffer::Buffer {
    let backend = TestBackend::new(w, h);
    let mut terminal = Terminal::new(backend).expect("terminal");
    terminal.draw(|f| render(f, app)).expect("draw");
    terminal.backend().buffer().clone()
}

fn render_to_string(app: &App, w: u16, h: u16) -> String {
    let buffer = render_buffer(app, w, h);
    let mut out = String::new();
    for y in 0..buffer.area().height {
        out.push_str(&buffer_row_text(&buffer, y));
        out.push('\n');
    }
    out
}

fn render_footer_text(app: &App, w: u16, h: u16) -> String {
    let buffer = render_buffer(app, w, h);
    buffer_row_text(&buffer, buffer.area().height - 1)
}

fn buffer_row_text(buffer: &ratatui::buffer::Buffer, y: u16) -> String {
    let mut out = String::new();
    for x in 0..buffer.area().width {
        out.push_str(buffer[(x, y)].symbol());
    }
    out
}

fn first_cell_matching<F>(buffer: &ratatui::buffer::Buffer, f: F) -> Option<(u16, u16)>
where
    F: Fn(&ratatui::buffer::Cell) -> bool,
{
    for y in 0..buffer.area().height {
        for x in 0..buffer.area().width {
            if f(&buffer[(x, y)]) {
                return Some((x, y));
            }
        }
    }
    None
}

fn buffer_has_cell<F>(buffer: &ratatui::buffer::Buffer, f: F) -> bool
where
    F: Fn(&ratatui::buffer::Cell) -> bool,
{
    first_cell_matching(buffer, f).is_some()
}

#[test]
fn render_empty_state_when_no_files() {
    let app = app_with_files(Vec::new());
    let view = render_to_string(&app, 70, 6);
    assert!(
        view.contains("No changes since baseline (baseline: abcdef1)"),
        "expected empty state with short SHA, got:\n{view}"
    );
    assert!(view.contains("[follow]"));
}

// ---- v0.5 line-number gutter -------------------------------------

#[test]
fn render_diff_line_numbered_inserts_line_number_span_after_bar() {
    let line = diff_line(LineKind::Context, "hello");
    let gutter = LineNumberGutter::single(2);
    let base = render_diff_line(
        &line,
        false,
        false,
        40,
        None,
        None,
        Color::Reset,
        Color::Reset,
        &[],
    );
    let mut rendered = add_line_number_gutters(
        vec![base],
        diff_ln_span((Some(10), Some(10)), &gutter),
        &gutter,
    );
    let rendered = rendered.remove(0);
    // span 0: 5-cell cursor bar
    // span 1: " 10 " single-column line-number gutter
    // span 2..: body
    assert!(
        rendered.spans.len() >= 3,
        "expected at least 3 spans, got {}",
        rendered.spans.len()
    );
    let ln = rendered.spans[1].content.as_ref();
    assert!(ln.contains("10"), "line-number gutter text: {ln:?}");
    assert!(
        ln.starts_with(' ') && ln.ends_with(' '),
        "gutter must be padded with 1 leading / 1 trailing space: {ln:?}"
    );
    assert_eq!(ln.len(), gutter.total_width);
}

#[test]
fn render_diff_line_numbered_added_row_shows_new_side() {
    let line = diff_line(LineKind::Added, "x");
    let gutter = LineNumberGutter::single(2);
    let base = render_diff_line(
        &line,
        false,
        false,
        40,
        None,
        None,
        Color::Reset,
        Color::Reset,
        &[],
    );
    let mut rendered =
        add_line_number_gutters(vec![base], diff_ln_span((None, Some(11)), &gutter), &gutter);
    let rendered = rendered.remove(0);
    let ln = rendered.spans[1].content.as_ref();
    // Single column shows the new-side line number.
    assert!(ln.contains("11"), "Added row must show new number: {ln:?}");
    assert_eq!(ln.len(), gutter.total_width);
}

#[test]
fn render_diff_line_numbered_deleted_row_leaves_gutter_blank() {
    // Deleted rows no longer exist in the worktree, so there is no
    // "current" line number to print. The gutter must be blank.
    // See `diff_ln_span` docstring for the intuition / reasoning.
    let line = diff_line(LineKind::Deleted, "y");
    let gutter = LineNumberGutter::single(2);
    let base = render_diff_line(
        &line,
        false,
        false,
        40,
        None,
        None,
        Color::Reset,
        Color::Reset,
        &[],
    );
    let mut rendered =
        add_line_number_gutters(vec![base], diff_ln_span((Some(12), None), &gutter), &gutter);
    let rendered = rendered.remove(0);
    let ln = rendered.spans[1].content.as_ref();
    assert!(
        ln.chars().all(|c| c == ' '),
        "Deleted row gutter must be blank: {ln:?}"
    );
    assert_eq!(ln.len(), gutter.total_width);
}

#[test]
fn render_diff_line_wrapped_numbered_continuation_rows_blank_the_gutter() {
    // Long content that will wrap into at least 2 visual rows at
    // body_width=4. Continuation rows must not repeat the number.
    let line = diff_line(LineKind::Context, "aaaaaaaaaa");
    let gutter = LineNumberGutter::single(2);
    let base = render_diff_line_wrapped(
        &line,
        false,
        Some(0),
        4,
        None,
        None,
        Color::Reset,
        Color::Reset,
        &[],
    );
    let rendered =
        add_line_number_gutters(base, diff_ln_span((Some(10), Some(10)), &gutter), &gutter);
    assert!(rendered.len() >= 2, "content must wrap into 2+ rows");
    // First row has numbers.
    let first_ln = rendered[0].spans[1].content.as_ref();
    assert!(
        first_ln.contains("10"),
        "first row must show 10: {first_ln:?}"
    );
    // Continuation row has blank gutter.
    let cont_ln = rendered[1].spans[1].content.as_ref();
    assert!(
        cont_ln.chars().all(|c| c == ' '),
        "continuation row must be all spaces: {cont_ln:?}"
    );
    assert_eq!(cont_ln.len(), gutter.total_width);
}

#[test]
fn render_file_view_line_numbered_shows_single_column() {
    let gutter = LineNumberGutter::single(3);
    let base = render_file_view_line(
        "hello world",
        false,
        40,
        Style::default(),
        None,
        std::path::Path::new("foo.rs"),
        false,
    );
    let mut rendered = add_line_number_gutters(vec![base], file_ln_span(42, &gutter), &gutter);
    let rendered = rendered.remove(0);
    assert!(rendered.spans.len() >= 3);
    let ln = rendered.spans[1].content.as_ref();
    assert!(ln.contains("42"), "file-view gutter: {ln:?}");
    assert_eq!(ln.len(), gutter.total_width);
}

#[test]
fn render_file_view_line_wrapped_numbered_blanks_continuation() {
    let gutter = LineNumberGutter::single(3);
    let base = render_file_view_line_wrapped(
        "aaaaaaaaaa",
        Some(0),
        /*body_width*/ 4,
        Style::default(),
        None,
        std::path::Path::new("foo.rs"),
        false,
    );
    let rendered = add_line_number_gutters(base, file_ln_span(42, &gutter), &gutter);
    assert!(rendered.len() >= 2);
    let cont_ln = rendered[1].spans[1].content.as_ref();
    assert!(
        cont_ln.chars().all(|c| c == ' '),
        "continuation row must be blank: {cont_ln:?}"
    );
}

#[test]
fn sticky_hunk_header_also_reserves_ln_gutter() {
    // Codex 3rd-round Important-1: the sticky header is drawn
    // directly by render_scroll with render_hunk_header(),
    // bypassing render_row's insert_blank_gutter branch. Without
    // an explicit fix the pinned header sits 4 cells left of the
    // scrolling DiffLine bodies whenever LN is on and the cursor
    // is deep enough inside a hunk to activate stickiness.
    let mut app = single_hunk_app(
        "src/foo.rs",
        10,
        // Enough DiffLines that the cursor will scroll past
        // the hunk header and stickiness kicks in.
        prefixed_diff_lines(LineKind::Context, "line ", 30),
        100,
    );
    app.show_line_numbers = true;
    app.build_layout();
    // Move cursor deep into the hunk so the header becomes sticky.
    app.scroll = 20;
    let buffer = render_buffer(&app, 80, 10);

    // Sticky header lives at y=0. Its `@@` must share its x with
    // the DiffLine `line` bodies below.
    let top_row: String = (0..buffer.area().width)
        .map(|x| buffer[(x, 0)].symbol().chars().next().unwrap_or(' '))
        .collect();
    assert!(
        top_row.contains("@@"),
        "top row must hold the sticky header: {top_row:?}"
    );
    let header_x = top_row.find("@@").unwrap();
    let mut body_x: Option<usize> = None;
    for y in 1..buffer.area().height {
        let row: String = (0..buffer.area().width)
            .map(|x| buffer[(x, y)].symbol().chars().next().unwrap_or(' '))
            .collect();
        if let Some(col) = row.find("line ") {
            body_x = Some(col);
            break;
        }
    }
    let bx = body_x.expect("at least one DiffLine body must be visible");
    // `@@` sits 2 cells to the right of the DiffLine body because
    // render_hunk_header prefixes the label with a 2-cell seen_mark
    // pad ("  " for unseen hunks). Pin the relative offset so the
    // sticky-header alignment can't silently regress.
    assert_eq!(
        (header_x as isize) - (bx as isize),
        2,
        "sticky header '@@' must sit 2 cells right of DiffLine body (header_x={header_x}, body_x={bx})"
    );
}

#[test]
fn ln_gutter_preserves_hunk_header_vs_diff_body_alignment() {
    // Bug (user-reported 2026-04-21): HunkHeader / BinaryNotice
    // must reserve the gutter column when LN is on, otherwise
    // their bodies slide left relative to DiffLine bodies. Pin
    // the invariant: the relative offset between `@@` and the
    // first body glyph must match between LN OFF and LN ON —
    // turning the gutter on shifts *both* by the same amount.
    let make_app = |show_ln: bool| {
        let mut app = single_hunk_app(
            "src/foo.rs",
            10,
            vec![
                diff_line(LineKind::Context, "fn ok()"),
                diff_line(LineKind::Added, "x"),
            ],
            100,
        );
        app.show_line_numbers = show_ln;
        app.build_layout();
        app
    };
    let probe = |app: &App| -> (usize, usize) {
        let buffer = render_buffer(app, 80, 12);
        (
            first_text_run(&buffer, "@@").0 as usize,
            first_text_run(&buffer, "fn ok()").0 as usize,
        )
    };
    let (off_h, off_b) = probe(&make_app(false));
    let (on_h, on_b) = probe(&make_app(true));
    // Both rows must shift by the same gutter width — the relative
    // offset between `@@` and `fn` is invariant under the toggle.
    assert_eq!(
        (on_h as isize - off_h as isize),
        (on_b as isize - off_b as isize),
        "gutter toggle must shift hunk header and diff body by the same amount (off: @@={off_h}, fn={off_b}; on: @@={on_h}, fn={on_b})"
    );
    // Sanity: LN ON actually adds width (otherwise the invariant
    // holds trivially).
    assert!(on_b > off_b, "LN ON must widen the left gutter");
}

#[test]
fn render_scroll_shows_line_numbers_when_enabled() {
    // v0.5 end-to-end: `show_line_numbers=true` must put a
    // right-aligned worktree line number in the gutter of every
    // Context / Added row.
    let mut app = single_hunk_app(
        "src/foo.rs",
        10,
        vec![
            diff_line(LineKind::Context, "fn ok()"),
            diff_line(LineKind::Added, "let x = 1;"),
        ],
        100,
    );
    app.show_line_numbers = true;
    app.build_layout();
    let view = render_to_string(&app, 80, 12);
    // Context row: new=10 → " 10 " in the gutter.
    // Added row: new=10 as well (new_count starts at new_start for
    // the first Added when old_count=0 → see git.rs:line_numbers_for).
    assert!(
        view.contains(" 10 "),
        "Context/Added row must show the worktree line number:\n{view}"
    );
}

#[test]
fn render_scroll_omits_line_numbers_in_stream_mode_even_when_enabled() {
    // Codex review §Critical-2: Stream mode FileDiffs carry
    // synthetic old_start/new_start values that are not real file
    // line numbers. The renderer must suppress the gutter.
    let mut app = added_hunk_app("src/foo.rs", 100, &["x"], 100);
    app.show_line_numbers = true;
    app.view_mode = crate::app::ViewMode::Stream;
    app.build_layout();
    let view = render_to_string(&app, 80, 12);
    // No "100" glyph should appear as a line-number gutter (it
    // might still appear in the hunk header `L100` — let's pin
    // something more specific: no `"100"` as a right-aligned
    // gutter value with a trailing separator).
    assert!(
        !view.contains(" 100 "),
        "Stream mode must not render line-number gutter:\n{view}"
    );
}

#[test]
fn render_scroll_drops_gutter_when_viewport_is_extremely_narrow() {
    // Codex review §Critical-2: at widths where 5 (cursor bar)
    // + gutter + 4 (min body) cannot fit, the renderer must
    // silently fall back to the no-gutter layout so the user
    // still sees diff content.
    let mut app = app_with_file(single_added_hunk_file("src/foo.rs", 10, "xyz", 100));
    app.show_line_numbers = true;
    app.build_layout();
    // Width 9 is too small: 5 + 9 (gutter) + 4 > 9. Fallback
    // forces ln off, body_width = 9 - 5 = 4.
    let view = render_to_string(&app, 12, 4);
    // No "10" as a gutter number should appear in this narrow view.
    // The fixture's new_start is 10 so we'd see it only if the
    // fallback failed.
    assert!(
        !view.contains(" 10 "),
        "narrow viewport must drop the gutter:\n{view}"
    );
}

#[test]
fn line_number_digits_clamps_to_lower_bound_of_two() {
    assert_eq!(line_number_digits(0), 2);
    assert_eq!(line_number_digits(1), 2);
    assert_eq!(line_number_digits(9), 2);
    assert_eq!(line_number_digits(10), 2);
    assert_eq!(line_number_digits(99), 2);
    assert_eq!(line_number_digits(100), 3);
    assert_eq!(line_number_digits(9999), 4);
}

#[test]
fn render_scroll_shows_file_header_hunk_header_and_diff_line() {
    let app = single_hunk_app(
        "src/foo.rs",
        10,
        vec![
            diff_line(LineKind::Context, "fn ok()"),
            diff_line(LineKind::Added, "let x = 1;"),
            diff_line(LineKind::Deleted, "let y = 2;"),
        ],
        100,
    );
    let view = render_to_string(&app, 80, 12);
    assert!(view.contains("src/foo.rs"), "missing file header:\n{view}");
    // New hunk header format: @@ L<range> +N/-M
    assert!(
        view.contains("@@ L10"),
        "missing hunk header line range:\n{view}"
    );
    assert!(
        view.contains("+1/-1"),
        "missing hunk header counts:\n{view}"
    );
    // ADR-0014: no `+`/`-` prefix; the body text appears bare on
    // the row and the add/delete signal lives in the background.
    assert!(view.contains("let x = 1;"), "missing added line:\n{view}");
    assert!(view.contains("let y = 2;"), "missing deleted line:\n{view}");
}

#[test]
fn render_scroll_lines_use_background_color_for_added_and_deleted() {
    // ADR-0014: delta-style background color for diff rows.
    //
    // The `+`/`-` prefix column is gone; added/deleted rows are
    // identified purely by their background color. We assert that
    // (a) the body text carries a green or red `bg` Style and
    // (b) no literal `+`/`-` prefix cell appears on the row body.
    let app = single_hunk_app(
        "src/foo.rs",
        1,
        vec![
            diff_line(LineKind::Added, "x"),
            diff_line(LineKind::Deleted, "y"),
        ],
        100,
    );
    let buffer = render_buffer(&app, 80, 12);

    let found_added_bg = buffer_has_cell(&buffer, |cell| {
        cell.symbol() == "x" && cell.style().bg == Some(BG_ADDED)
    });
    let found_deleted_bg = buffer_has_cell(&buffer, |cell| {
        cell.symbol() == "y" && cell.style().bg == Some(BG_DELETED)
    });
    assert!(
        found_added_bg,
        "expected an added 'x' cell with green background"
    );
    assert!(
        found_deleted_bg,
        "expected a deleted 'y' cell with red background"
    );
}

#[test]
fn diff_view_uses_document_highlight_for_tsx() {
    let tmp = tempfile::tempdir().expect("tmp");
    let rel = "src/Button.tsx";
    let abs = tmp.path().join(rel);
    std::fs::create_dir_all(abs.parent().unwrap()).expect("mkdir");
    let content = "export const button = (\n  <Button\n    kind=\"primary\"\n    onClick={() => save()}\n  />\n);\n";
    std::fs::write(&abs, content).expect("write tsx");

    let mut app = app_with_file(make_file(
        rel,
        vec![added_hunk(
            1,
            &[
                "export const button = (",
                "  <Button",
                "    kind=\"primary\"",
                "    onClick={() => save()}",
                "  />",
                ");",
            ],
        )],
        100,
    ));
    app.root = tmp.path().to_path_buf();
    app.scroll_to(0);

    let buffer = render_buffer(&app, 100, 14);
    let (x, y, _) = first_text_run(&buffer, "kind");

    assert_eq!(
        buffer[(x, y)].style().fg,
        Some(Color::Cyan),
        "tsx attribute in diff view should use tree-sitter document highlight"
    );
}

#[test]
fn diff_view_uses_baseline_document_highlight_for_deleted_tsx() {
    let tmp = tempfile::tempdir().expect("tmp");
    let root = tmp.path();
    std::process::Command::new("git")
        .args(["init", "--quiet", "--initial-branch=main"])
        .current_dir(root)
        .status()
        .expect("git init");
    std::process::Command::new("git")
        .args(["config", "user.email", "test@example.com"])
        .current_dir(root)
        .status()
        .expect("git config email");
    std::process::Command::new("git")
        .args(["config", "user.name", "kizu test"])
        .current_dir(root)
        .status()
        .expect("git config name");

    let rel = "src/Button.tsx";
    let abs = root.join(rel);
    std::fs::create_dir_all(abs.parent().unwrap()).expect("mkdir");
    let before = "export const button = (\n  <Button\n    kind=\"primary\"\n    onClick={() => save()}\n  />\n);\n";
    let after = "export const button = (\n  <Button\n    onClick={() => save()}\n  />\n);\n";
    std::fs::write(&abs, before).expect("write before");
    std::process::Command::new("git")
        .args(["add", rel])
        .current_dir(root)
        .status()
        .expect("git add");
    std::process::Command::new("git")
        .args(["commit", "--quiet", "-m", "seed"])
        .current_dir(root)
        .status()
        .expect("git commit");
    let baseline = crate::git::head_sha(root).expect("head");
    std::fs::write(&abs, after).expect("write after");
    let files = crate::git::compute_diff(root, &baseline).expect("diff");
    let mut app = app_with_files(files);
    app.root = root.to_path_buf();
    app.baseline_sha = baseline;
    app.scroll_to(0);

    let buffer = render_buffer(&app, 100, 14);
    let (x, y, _) = first_text_run(&buffer, "kind");

    assert_eq!(
        buffer[(x, y)].style().fg,
        Some(Color::Cyan),
        "deleted tsx attribute should use baseline document highlight"
    );
}

#[test]
fn diff_view_does_not_document_highlight_non_js_files() {
    let tmp = tempfile::tempdir().expect("tmp");
    let rel = "src/foo.rs";
    let abs = tmp.path().join(rel);
    std::fs::create_dir_all(abs.parent().unwrap()).expect("mkdir");
    std::fs::write(&abs, "pub fn answer() -> i32 { 42 }\n").expect("write rust");

    let mut app = app_with_file(make_file(
        rel,
        vec![added_hunk(1, &["pub fn answer() -> i32 { 42 }"])],
        100,
    ));
    app.root = tmp.path().to_path_buf();

    let _buffer = render_buffer(&app, 100, 10);
    let highlighter = app.highlighter.get().expect("highlighter initialized");

    assert_eq!(
        highlighter.document_cache_len(),
        0,
        "diff view should not build whole-document highlights for non-JS/TS files"
    );
}

#[test]
fn file_view_uses_document_highlight_for_tsx() {
    let content = "export const button = (\n  <Button\n    kind=\"primary\"\n    onClick={() => save()}\n  />\n);\n";
    let mut app = app_with_files(Vec::new());
    app.file_view = Some(file_view_state(
        "src/Button.tsx",
        content.lines().map(String::from).collect(),
        2,
        true,
    ));

    let buffer = render_buffer(&app, 100, 10);
    let (x, y, _) = first_text_run(&buffer, "kind");

    assert_eq!(
        buffer[(x, y)].style().fg,
        Some(Color::Cyan),
        "tsx attribute in file view should use tree-sitter document highlight"
    );
}

#[test]
fn nowrap_added_row_background_extends_to_viewport_edge() {
    // ADR-0014: the delta-style coloured band must run from the
    // first body cell to the last cell of the viewport, even
    // when the diff content is shorter than the width. This
    // tests nowrap mode (the default). If the padding logic
    // breaks, the right edge of a short added row will fall
    // back to the terminal default background.
    let app = single_added_app("a.rs", "tiny");
    let width: u16 = 40;
    let buffer = render_buffer(&app, width, 12);

    // Find the row that contains the "tiny" body text.
    let y = row_containing(&buffer, "tiny");

    // Every cell from the start of the body region (x = 5) to
    // the last column must carry BG_ADDED.
    for x in 5..width {
        let cell = &buffer[(x, y)];
        assert_eq!(
            cell.style().bg,
            Some(BG_ADDED),
            "cell (x={x}, y={y}) lost the added background; symbol = {:?}",
            cell.symbol()
        );
    }
}

#[test]
fn render_scroll_lines_omit_plus_minus_prefix() {
    // ADR-0014: there must be no `+`/`-` prefix cells in the diff
    // body region (rows past the 5-char bar margin). The background
    // color encodes add/delete instead.
    let app = single_hunk_app(
        "src/foo.rs",
        1,
        vec![
            diff_line(LineKind::Added, "ADDED_LINE"),
            diff_line(LineKind::Deleted, "DELETED_LINE"),
        ],
        100,
    );
    let view = render_to_string(&app, 80, 12);
    assert!(
        !view.contains("+ADDED_LINE"),
        "must not carry a `+` prefix next to added body:\n{view}"
    );
    assert!(
        !view.contains("-DELETED_LINE"),
        "must not carry a `-` prefix next to deleted body:\n{view}"
    );
}

#[test]
fn wrap_mode_does_not_show_any_marker_when_terminal_newline_present() {
    // v0.5 M2 (plan v0.5-newline-marker.md): the former `¶` marker
    // drew on every row with has_trailing_newline=true, which is
    // ~99% of rows. Now common rows carry no marker at all; only
    // EOF-no-newline rows get a Yellow `∅`. Pin the new default:
    // normal rows must be glyph-free.
    let long_content: String = (0..120u8).map(|i| (b'a' + (i % 26)) as char).collect();
    let mut app = single_added_app("a.rs", &long_content);
    app.wrap_lines = true;

    let view = render_to_string(&app, 80, 14);
    assert!(
        !view.contains(""),
        "v0.5 M2: `¶` must no longer appear on normal wrap rows:\n{view}"
    );
    assert!(
        !view.contains(""),
        "no EOF marker when has_trailing_newline=true:\n{view}"
    );
    // The second half of the content must still be visible.
    assert!(
        view.contains(&long_content[90..110]),
        "expected wrapped continuation to be visible:\n{view}"
    );
}

#[test]
fn render_diff_line_nowrap_cjk_pads_to_cell_width_not_char_count() {
    // Fallback (no highlighter) path: 5 kanji = 5 chars = 10 cells.
    // At body_width=20 cells the padded body must be exactly 20
    // cells wide — not 20-5=15 pad chars tacked on (which would
    // produce a 25-cell body and bleed past the viewport).
    use unicode_width::UnicodeWidthStr;
    let line = diff_line(LineKind::Added, "あいうえお");
    let rendered = super::render_diff_line(
        &line,
        false,
        false,
        20,
        None,
        None,
        Color::Rgb(10, 50, 10),
        Color::Rgb(60, 10, 10),
        &[],
    );
    // Skip the 5-cell left bar; the remaining spans make up the body.
    let body_cells: usize = rendered
        .spans
        .iter()
        .skip(1)
        .map(|s| UnicodeWidthStr::width(s.content.as_ref()))
        .sum();
    assert_eq!(
        body_cells, 20,
        "nowrap CJK body must pad to body_width in cells, got {body_cells} cells",
    );
}

#[test]
fn wrap_at_chars_respects_cjk_display_width() {
    // Each kanji in `日本語テスト` has display width 2. At a body
    // width of 4 cells we must emit 2 kanji per chunk — NOT 4
    // kanji (which would overflow to 8 cells and let the chunk
    // spill beyond the viewport in wrap mode).
    let chunks = super::wrap_at_chars("日本語テスト", 4);
    assert_eq!(chunks, vec!["日本", "語テ", "スト"]);
}

#[test]
fn wrap_at_chars_handles_mixed_ascii_and_cjk() {
    // `ab漢字` = 1+1+2+2 = 6 cells. At width 4, the first chunk
    // fits `ab漢` (1+1+2=4 cells exactly); `字` starts a new chunk.
    let chunks = super::wrap_at_chars("ab漢字cd", 4);
    assert_eq!(chunks, vec!["ab漢", "字cd"]);
}

#[test]
fn wrap_mode_cjk_line_wraps_within_viewport() {
    // 40 kanji at width 40 (viewport cell width): the line must
    // wrap to 2 rows since each kanji is 2 cells wide. Previously
    // this rendered as a single over-wide row that ratatui
    // silently truncated or that bled past the viewport.
    let forty_kanji: String = "あいうえおかきくけこ".repeat(4);
    assert_eq!(forty_kanji.chars().count(), 40);
    let mut app = single_added_app("a.rs", &forty_kanji);
    app.wrap_lines = true;

    // Viewport: 45 cols (5 for left bar + 40 for body).
    let view = render_to_string(&app, 45, 10);
    // Both the first kanji and a kanji past the 20th position
    // must be visible: if wrap worked correctly, both halves of
    // the content appear on separate visual rows.
    assert!(view.contains(""), "first kanji must be visible:\n{view}");
    // The 35th kanji (well past the 20-kanji midpoint) must also
    // land in the viewport — only possible if the line actually
    // wrapped rather than being truncated at column 40.
    let late_kanji: char = forty_kanji.chars().nth(35).unwrap();
    assert!(
        view.contains(late_kanji),
        "late CJK char {late_kanji:?} must survive wrap:\n{view}"
    );
}

#[test]
fn wrap_mode_shows_eof_marker_when_no_terminal_newline() {
    // v0.5 M2: has_trailing_newline=false is the git `\ No newline
    // at end of file` case. The new EOF marker is `∅` in Yellow.
    // Pin the *presence* of `∅` and the *absence* of the legacy `¶`.
    let long_content: String = (0..40u8).map(|i| (b'a' + (i % 26)) as char).collect();
    let mut file = single_added_file("a.rs", &long_content, 100);
    let DiffContent::Text(hunks) = &mut file.content else {
        panic!("expected text diff");
    };
    hunks[0].lines[0].has_trailing_newline = false;

    let mut app = app_with_file(file);
    app.wrap_lines = true;
    let view = render_to_string(&app, 40, 10);
    assert!(
        view.contains(""),
        "EOF-no-newline must render a `∅` marker in wrap mode:\n{view}"
    );
    assert!(!view.contains(""), "legacy `¶` must be gone:\n{view}");
}

#[test]
fn nowrap_mode_shows_eof_marker_when_no_terminal_newline() {
    // v0.5 M2: EOF-no-newline information is independent of wrap
    // mode. The marker must appear in nowrap as well.
    let mut file = single_added_file("a.rs", "short", 100);
    let DiffContent::Text(hunks) = &mut file.content else {
        panic!("expected text diff");
    };
    hunks[0].lines[0].has_trailing_newline = false;

    let mut app = app_with_file(file);
    app.wrap_lines = false;
    let view = render_to_string(&app, 80, 10);
    assert!(
        view.contains(""),
        "EOF-no-newline must render a `∅` marker in nowrap mode:\n{view}"
    );
}

#[test]
fn nowrap_mode_omits_marker_for_normal_line() {
    // Symmetric to the wrap "normal row" test: no marker on a
    // has_trailing_newline=true nowrap row.
    let mut app = single_added_app("a.rs", "short");
    app.wrap_lines = false;
    let view = render_to_string(&app, 80, 10);
    assert!(
        !view.contains("") && !view.contains(""),
        "normal nowrap row must carry no end-of-line marker:\n{view}"
    );
}

#[test]
fn line_numbers_hint_appears_in_footer_and_marks_state() {
    // v0.5 plan §Important-4 (Codex): wrap/`z` hints are already
    // visible in the footer, so the LN toggle must be too.
    let mut app = single_added_app("a.rs", "x");
    // OFF by default → still shows a hint (just without bold).
    let off_view = render_to_string(&app, 80, 8);
    assert!(
        off_view.contains("nums off"),
        "footer must spell out the disabled LN state:\n{off_view}"
    );

    // ON → state must be visible in text, not only in bold styling.
    app.show_line_numbers = true;
    app.build_layout();
    let on_view = render_to_string(&app, 80, 8);
    assert!(
        on_view.contains("nums on"),
        "footer must spell out the enabled LN state:\n{on_view}"
    );
}

#[test]
fn line_numbers_hint_marks_stream_mode_as_off() {
    // Stream mode always suppresses the gutter, so the footer
    // should make that explicit with an `(off)` marker no matter
    // what `show_line_numbers` is.
    let mut app = single_added_app("a.rs", "x");
    app.show_line_numbers = true;
    app.view_mode = crate::app::ViewMode::Stream;
    app.build_layout();
    let view = render_to_string(&app, 80, 8);
    assert!(
        view.contains("nums") && view.contains("off"),
        "stream footer must flag LN as disabled:\n{view}"
    );
}

#[test]
fn wrap_nowrap_indicator_appears_in_footer() {
    let mut app = single_added_app("a.rs", "x");
    let nowrap_view = render_to_string(&app, 80, 8);
    assert!(nowrap_view.contains("nowrap"));

    app.wrap_lines = true;
    let wrap_view = render_to_string(&app, 80, 8);
    assert!(wrap_view.contains("wrap"));
    assert!(!wrap_view.contains("nowrap"));
}

#[test]
fn responsive_footer_keeps_state_not_keymap_when_normal_mode_is_narrow() {
    let mut app = single_added_app(
        "src/extremely/long/path/that/pushes/status/content/out/of/sight/component.rs",
        "x",
    );
    app.follow_mode = false;
    app.wrap_lines = true;
    app.show_line_numbers = true;
    app.head_dirty = true;
    app.build_layout();

    let footer = render_footer_text(&app, 64, 8);
    assert!(footer.contains("[manual]"), "missing mode:\n{footer}");
    assert!(
        footer.contains("wrap"),
        "narrow footer must keep wrap state visible without relying on key labels:\n{footer}"
    );
    assert!(
        footer.contains("nums"),
        "narrow footer must keep line-number state visible without relying on key labels:\n{footer}"
    );
    assert!(
        !footer.contains("w wrap"),
        "footer should not carry keymap:\n{footer}"
    );
    assert!(
        !footer.contains("# nums"),
        "footer should not carry keymap:\n{footer}"
    );
    assert!(
        !footer.contains("picker"),
        "narrow footer should drop verbose low-priority labels first:\n{footer}"
    );
}

#[test]
fn responsive_footer_keeps_back_hint_when_file_view_path_is_long() {
    let mut app = app_with_files(Vec::new());
    app.file_view = Some(file_view_state(
        "src/extremely/long/path/that/would/otherwise/hide/the/back/hint/demo.rs",
        vec!["first".into(), "second".into(), "third".into()],
        1,
        true,
    ));
    app.wrap_lines = true;
    app.show_line_numbers = true;

    let footer = render_footer_text(&app, 56, 8);
    assert!(
        footer.contains("[file") || footer.contains("[file view]"),
        "missing file-view mode:\n{footer}"
    );
    assert!(
        footer.contains("wrap"),
        "file-view footer must keep wrap state visible:\n{footer}"
    );
    assert!(
        footer.contains("nums"),
        "file-view footer must keep line-number state visible:\n{footer}"
    );
    assert!(
        footer.contains("Esc") || footer.contains("back"),
        "file-view footer must keep the back hint visible:\n{footer}"
    );
}

#[test]
fn help_overlay_uses_configured_key_labels() {
    let mut app = single_added_app("src/foo.rs", "x");
    app.follow_mode = false;
    app.config.keys.cursor_placement = 'Z';
    app.config.keys.wrap_toggle = 'W';
    app.config.keys.line_numbers_toggle = 'L';
    app.config.keys.picker = 'p';
    app.help_overlay = true;

    let view = render_to_string(&app, 100, 24);
    assert!(
        view.contains("Z") && view.contains("center"),
        "help overlay must show remapped cursor-placement key:\n{view}"
    );
    assert!(
        view.contains("W") && view.contains("wrap"),
        "help overlay must show remapped wrap key:\n{view}"
    );
    assert!(
        view.contains("L") && view.contains("line numbers"),
        "help overlay must show remapped line-number key:\n{view}"
    );
    assert!(
        view.contains("p") && view.contains("picker"),
        "help overlay must show remapped picker key:\n{view}"
    );
}

#[test]
fn file_view_marks_eof_no_newline_on_last_line_nowrap() {
    // v0.5 M2: when the on-disk file lacks a trailing LF, the
    // file view must draw `∅` at the end of the final line.
    // Non-last lines never get the marker, regardless of
    // `last_line_has_trailing_newline`.
    let mut app = app_with_files(Vec::new());
    app.file_view = Some(file_view_state(
        "foo.rs",
        vec!["first".into(), "tail-no-newline".into()],
        1,
        false,
    ));
    app.wrap_lines = false;
    let view = render_to_string(&app, 40, 8);
    assert!(
        view.contains(""),
        "file view must mark EOF-no-newline on the last line:\n{view}"
    );
}

#[test]
fn file_view_omits_marker_when_last_line_has_newline() {
    let mut app = app_with_files(Vec::new());
    app.file_view = Some(file_view_state(
        "foo.rs",
        vec!["first".into(), "last".into()],
        1,
        true,
    ));
    app.wrap_lines = false;
    let view = render_to_string(&app, 40, 8);
    assert!(
        !view.contains("") && !view.contains(""),
        "normal file must carry no EOF marker:\n{view}"
    );
}

#[test]
fn file_view_wrap_mode_renders_late_content_and_footer_indicator() {
    let long = format!("const DATA: &str = {:?};", "0123456789".repeat(12));
    let mut app = app_with_files(Vec::new());
    app.file_view = Some(file_view_state("src/demo.rs", vec![long.clone()], 0, true));
    app.wrap_lines = true;

    let view = render_to_string(&app, 40, 8);
    assert!(
        view.contains(&long[45..65]),
        "wrapped file view must surface a later slice of the long line:\n{view}"
    );
    assert!(
        view.contains("[file view]"),
        "file view footer missing:\n{view}"
    );
    assert!(view.contains("wrap"), "wrap indicator missing:\n{view}");
}

#[test]
fn render_scroll_marks_binary_file_with_notice() {
    let app = app_with_file(timed_binary_file("assets/icon.png", 0));
    let view = render_to_string(&app, 80, 8);
    assert!(view.contains("assets/icon.png"));
    assert!(view.contains("[binary file - diff suppressed]"));
    assert!(view.contains("bin"));
}

#[test]
fn render_picker_overlays_a_box_with_query_and_filtered_list() {
    let mut app = app_with_files(vec![
        single_added_file("src/auth.rs", "x", 300),
        single_added_file("src/handler.rs", "y", 200),
        single_added_file("tests/auth_test.rs", "z", 100),
    ]);
    app.picker = Some(PickerState {
        query: "auth".into(),
        cursor: 0,
    });

    let view = render_to_string(&app, 90, 14);
    // Query input rendered at the top of the popup
    assert!(view.contains("> auth"), "missing query line:\n{view}");
    // Two filtered files are visible
    assert!(view.contains("src/auth.rs"), "missing src/auth.rs:\n{view}");
    assert!(
        view.contains("tests/auth_test.rs"),
        "missing tests/auth_test.rs:\n{view}"
    );
    // The non-matching file should NOT be inside the picker popup.
    // It might still appear in the underlying scroll view; the popup
    // header should advertise the filtered count.
    assert!(view.contains("Files 2/3"), "missing files counter:\n{view}");
    // Footer switches to picker hint copy
    assert!(view.contains("[picker]"));
    assert!(view.contains("type to filter"));
    assert!(view.contains("Esc"));
}

#[test]
fn render_footer_shows_last_error_in_red_when_set() {
    let mut app = single_added_app("src/foo.rs", "x");
    app.last_error = Some("git diff exploded".into());

    // Wide enough that the footer's body + error message both fit.
    let buffer = render_buffer(&app, 140, 6);

    let footer_y = buffer.area().height - 1;
    let footer_text = buffer_row_text(&buffer, footer_y);
    let had_red_x = (0..buffer.area().width).any(|x| {
        let cell = &buffer[(x, footer_y)];
        cell.symbol() == "×" && cell.style().fg == Some(Color::Red)
    });
    assert!(
        footer_text.contains("git diff exploded"),
        "footer:\n{footer_text}"
    );
    assert!(
        had_red_x,
        "expected red '×' marker before the error message"
    );
}

#[test]
fn render_footer_shows_source_aware_watcher_failures() {
    let mut app = single_added_app("src/foo.rs", "x");
    app.watcher_health.record_failure(
        crate::watcher::WatchSource::GitRefs,
        "watcher [git.refs]: refs watcher dead".into(),
    );
    app.watcher_health.record_failure(
        crate::watcher::WatchSource::Worktree,
        "watcher [worktree]: worktree watcher dead".into(),
    );

    // v0.5: the footer grew a `# nums` segment, so the viewport
    // needs a little more room before the worktree warning is
    // truncated at the right edge.
    let view = render_to_string(&app, 200, 6);
    assert!(
        view.contains("⚠ WATCHER"),
        "missing watcher warning:\n{view}"
    );
    assert!(
        view.contains("watcher [git.refs]: refs watcher dead"),
        "missing git watcher message:\n{view}"
    );
    assert!(
        view.contains("watcher [worktree]: worktree"),
        "missing worktree watcher message:\n{view}"
    );
}

#[test]
fn render_footer_shows_input_health_warning() {
    let mut app = single_added_app("src/foo.rs", "x");
    app.input_health = Some("input: stream hiccup".into());

    let view = render_to_string(&app, 140, 6);
    assert!(view.contains("⚠ INPUT"), "missing input warning:\n{view}");
    assert!(
        view.contains("input: stream hiccup"),
        "missing input health message:\n{view}"
    );
}

#[test]
fn render_footer_switches_to_manual_when_follow_mode_off() {
    let mut app = single_added_app("src/foo.rs", "x");
    app.follow_mode = false;
    let view = render_to_string(&app, 80, 6);
    assert!(view.contains("[manual]"), "expected [manual]:\n{view}");
}

fn hunk_with_context(old_start: usize, ctx: &str, lines: Vec<DiffLine>) -> Hunk {
    let mut hunk = hunk(old_start, lines);
    hunk.context = Some(ctx.to_string());
    hunk
}

fn modified_file_status(name: &str, status: FileStatus, secs: u64) -> FileDiff {
    let mut file = make_file(name, vec![added_hunk(1, &["x"])], secs);
    file.status = status;
    file
}

#[test]
fn file_header_path_color_encodes_status() {
    let mut app = app_with_files(vec![
        modified_file_status("a.rs", FileStatus::Modified, 100),
        modified_file_status("b.rs", FileStatus::Added, 200),
        modified_file_status("c.rs", FileStatus::Deleted, 300),
        modified_file_status("d.rs", FileStatus::Untracked, 400),
    ]);
    // Bootstrap parks scroll on the follow target (bottom). Reset to 0
    // so every file header sits inside the viewport for inspection.
    app.scroll_to(0);
    let buffer = render_buffer(&app, 80, 30);

    // For each path, find its first cell (the 'a'/'b'/'c'/'d') and
    // assert the foreground color matches the status mapping.
    let want = [
        ("a.rs", Color::Cyan),
        ("b.rs", Color::Green),
        ("c.rs", Color::Red),
        ("d.rs", Color::Yellow),
    ];
    for (name, expected) in want {
        let (x, y, _) = first_text_run(&buffer, name);
        assert_eq!(
            buffer[(x, y)].style().fg,
            Some(expected),
            "{name} should be in {expected:?}"
        );
    }
}

#[test]
fn file_header_shows_prefix_when_set() {
    let mut file = single_added_file("src/auth.rs", "x", 100);
    file.header_prefix = Some("14:03:22 Write".to_string());
    let mut app = app_with_file(file);
    app.scroll = 0;

    let buffer = render_buffer(&app, 60, 10);

    // The prefix "14:03:22 Write" should appear in the header line.
    let first_line = buffer_row_text(&buffer, 0);
    assert!(
        first_line.contains("14:03:22 Write"),
        "header should contain prefix, got: {first_line:?}"
    );
}

#[test]
fn hunk_header_uses_function_context_when_available() {
    let app = app_with_hunks(
        "src/auth.rs",
        vec![hunk_with_context(
            10,
            "fn verify_token(claims: &Claims) -> Result<bool> {",
            vec![diff_line(LineKind::Added, "let x = 1;")],
        )],
        100,
    );
    let view = render_to_string(&app, 100, 14);
    assert!(
        view.contains("@@ fn verify_token(claims: &Claims) -> Result<bool> {"),
        "expected xfuncname header, got:\n{view}"
    );
    // The literal `@@ -10,X +10,Y @@` form should NOT appear once
    // context is available.
    assert!(
        !view.contains("@@ -10,0 +10,1 @@"),
        "old hunk-range header leaked through:\n{view}"
    );
}

#[test]
fn hunk_header_shows_line_range_and_counts() {
    // Hunk header should display line number range and +/-
    // counts alongside the function context.
    let app = app_with_hunks(
        "a.rs",
        vec![Hunk {
            old_start: 10,
            old_count: 2,
            new_start: 10,
            new_count: 5,
            lines: vec![
                diff_line(LineKind::Context, "ok"),
                diff_line(LineKind::Added, "new1"),
                diff_line(LineKind::Added, "new2"),
                diff_line(LineKind::Added, "new3"),
                diff_line(LineKind::Deleted, "old1"),
            ],
            context: Some("fn example()".to_string()),
        }],
        100,
    );
    let view = render_to_string(&app, 100, 14);
    // Should contain the line range.
    assert!(
        view.contains("L10"),
        "expected line range L10, got:\n{view}"
    );
    // Should contain the change counts.
    assert!(
        view.contains("+3/-1"),
        "expected +3/-1 counts, got:\n{view}"
    );
}

#[test]
fn hunk_header_pure_deletion_uses_baseline_range_not_l0() {
    // Codex 3rd-round Important-3: a hunk that removes lines from
    // the top of the file ends up with new_start=0 / new_count=0.
    // The previous range formula `L{new_start}-{new_start + new_count - 1}`
    // would render "L0-?" (underflow or nonsense) and, now that
    // Deleted DiffLine rows have a blank gutter, the header was
    // the only positional signal left. Fall back to the baseline
    // range so the reader can still locate the removal.
    let app = app_with_hunks(
        "a.rs",
        vec![Hunk {
            old_start: 1,
            old_count: 3,
            new_start: 0,
            new_count: 0,
            lines: vec![
                diff_line(LineKind::Deleted, "gone1"),
                diff_line(LineKind::Deleted, "gone2"),
                diff_line(LineKind::Deleted, "gone3"),
            ],
            context: None,
        }],
        100,
    );
    let view = render_to_string(&app, 100, 14);
    assert!(
        !view.contains("L0"),
        "pure deletion must not render L0 as the header range:\n{view}"
    );
    // Baseline range should appear instead (old_start .. old_start+old_count-1).
    assert!(
        view.contains("L1-3") || view.contains("L1"),
        "pure deletion header must fall back to baseline range:\n{view}"
    );
}

#[test]
fn hunk_header_shows_range_in_fallback_format() {
    // When no function context is available, the header should
    // still show line range and counts.
    let app = app_with_hunks(
        "a.rs",
        vec![Hunk {
            old_start: 5,
            old_count: 1,
            new_start: 5,
            new_count: 3,
            lines: vec![
                diff_line(LineKind::Added, "x"),
                diff_line(LineKind::Added, "y"),
                diff_line(LineKind::Deleted, "z"),
            ],
            context: None,
        }],
        100,
    );
    let view = render_to_string(&app, 100, 14);
    assert!(view.contains("L5"), "expected line range L5, got:\n{view}");
    assert!(
        view.contains("+2/-1"),
        "expected +2/-1 counts, got:\n{view}"
    );
}

#[test]
fn selected_hunk_is_bright_and_unselected_hunk_is_dim() {
    // ADR-0014: the delta-style bg color is the same for both
    // focused and unfocused add rows — the contrast comes from
    // `Modifier::DIM` on the unfocused hunk (plus the left bar
    // `▎` / `▶` on the focused one). This pins both halves of
    // that contract so a future refactor can't silently flatten
    // the focus signal.
    //
    // We use content-only characters ('~', '!') that don't appear
    // in file paths, hunk headers (`@@`), or scroll chrome so the
    // cell lookup doesn't collide.
    let mut app = app_with_hunks(
        "a.rs",
        vec![added_hunk(1, &["~~~~"]), added_hunk(20, &["!!!!"])],
        100,
    );
    // Snap to the first hunk so one hunk is focused and the other
    // is not.
    app.scroll_to(app.layout.hunk_starts[0]);
    let buffer = render_buffer(&app, 100, 14);

    // Both body characters must sit on BG_ADDED (the colored
    // band never disappears), but the unfocused row must also
    // carry `Modifier::DIM` while the focused row must not.
    let mut focused_cells: Vec<(Option<Color>, Modifier)> = Vec::new();
    let mut unfocused_cells: Vec<(Option<Color>, Modifier)> = Vec::new();
    for y in 0..buffer.area().height {
        for x in 5..buffer.area().width {
            let cell = &buffer[(x, y)];
            let style = cell.style();
            if cell.symbol() == "~" {
                focused_cells.push((style.bg, style.add_modifier));
            }
            if cell.symbol() == "!" {
                unfocused_cells.push((style.bg, style.add_modifier));
            }
        }
    }
    assert!(
        !focused_cells.is_empty(),
        "focused hunk body '~' never rendered"
    );
    assert!(
        !unfocused_cells.is_empty(),
        "unfocused hunk body '!' never rendered"
    );
    // Focused: BG_ADDED, no DIM.
    assert!(
        focused_cells
            .iter()
            .all(|(bg, m)| *bg == Some(BG_ADDED) && !m.contains(Modifier::DIM)),
        "focused hunk must be BG_ADDED without DIM, got {focused_cells:?}"
    );
    // Unfocused: BG_ADDED, with DIM.
    assert!(
        unfocused_cells
            .iter()
            .all(|(bg, m)| *bg == Some(BG_ADDED) && m.contains(Modifier::DIM)),
        "unfocused hunk must be BG_ADDED with DIM, got {unfocused_cells:?}"
    );
}

#[test]
fn selected_hunk_displays_yellow_left_bar() {
    // Multi-line hunk so that *some* row exists that's selected but
    // not the cursor — proving the `▎` ribbon still gets drawn for
    // the rest of the hunk while only one row gets the `▶` arrow.
    let mut app = added_hunk_app("src/foo.rs", 1, &["first", "second"], 100);
    // Place the cursor on the hunk header so the `▎` (not `▶`) bar
    // covers both diff line rows.
    app.scroll_to(app.layout.hunk_starts[0]);
    let buffer = render_buffer(&app, 80, 14);

    let had_yellow_bar = buffer_has_cell(&buffer, |cell| {
        cell.symbol() == "" && cell.style().fg == Some(Color::Yellow)
    });
    assert!(
        had_yellow_bar,
        "expected a yellow '▎' on the selected hunk row"
    );
}

#[test]
fn cursor_row_displays_arrow_marker_distinct_from_hunk_bar() {
    // Two-line hunk: park the cursor on the first diff line. That row
    // should render `▶` in the left margin while the *other* diff row
    // of the same hunk still uses the plain `▎` ribbon.
    let mut app = added_hunk_app("src/foo.rs", 1, &["first", "second"], 100);
    // Layout: FileHeader, HunkHeader, DiffLine(0), DiffLine(1), Spacer
    // hunk_starts[0] = 1 (HunkHeader). First DiffLine is at row 2.
    app.scroll_to(app.layout.hunk_starts[0] + 1);
    let buffer = render_buffer(&app, 80, 14);

    let had_arrow = buffer_has_cell(&buffer, |cell| {
        cell.symbol() == "" && cell.style().fg == Some(Color::Yellow)
    });
    let had_plain_bar = buffer_has_cell(&buffer, |cell| {
        cell.symbol() == "" && cell.style().fg == Some(Color::Yellow)
    });
    assert!(had_arrow, "expected a yellow '▶' arrow at the cursor row");
    assert!(
        had_plain_bar,
        "expected a yellow '▎' ribbon on the other selected row"
    );
}

#[test]
fn hunk_header_cursor_displays_arrow_marker() {
    let mut app = single_added_app("src/foo.rs", "first");
    app.scroll_to(app.layout.hunk_starts[0]);

    let view = render_to_string(&app, 80, 10);
    assert!(
        view.contains(""),
        "cursor parked on a hunk header must still be visible:\n{view}"
    );
}

#[test]
fn hunk_header_cursor_arrow_is_yellow_and_bold() {
    // v0.4: when a hunk collapses under the seen mark the only
    // on-screen anchor the reader has is the hunk header row.
    // Paint its `▶` with the same Yellow + Bold style DiffLine
    // rows use so the cursor stays visible across the hand-off
    // between expanded and collapsed hunks.
    let mut app = single_added_app("src/foo.rs", "first");
    app.scroll_to(app.layout.hunk_starts[0]);

    let buffer = render_buffer(&app, 80, 10);

    let found = buffer_has_cell(&buffer, |cell| {
        let st = cell.style();
        cell.symbol() == ""
            && st.fg == Some(Color::Yellow)
            && st.add_modifier.contains(Modifier::BOLD)
    });
    assert!(
        found,
        "cursor `▶` on a hunk header must be Yellow + Bold, not Cyan"
    );
}

#[test]
fn seen_hunk_header_shows_fold_glyph() {
    // v0.4: seen hunks render with a ▸ fold glyph in the hunk
    // header so the reader can tell "this hunk is collapsed,
    // not empty" at a glance. Unseen hunks have no such glyph.
    let mut app = single_added_app("src/foo.rs", "first");
    app.scroll_to(app.layout.hunk_starts[0] + 1); // onto the DiffLine
    app.handle_key(crossterm::event::KeyEvent::new(
        crossterm::event::KeyCode::Char(' '),
        crossterm::event::KeyModifiers::NONE,
    ));

    let view = render_to_string(&app, 80, 10);
    assert!(
        view.contains(""),
        "seen hunk header must display a ▸ fold glyph:\n{view}"
    );
}

#[test]
fn file_header_cursor_displays_arrow_marker() {
    let app = single_added_app("src/foo.rs", "first");

    let view = render_to_string(&app, 80, 10);
    assert!(
        view.contains(""),
        "cursor parked on a file header must still be visible:\n{view}"
    );
}

#[test]
fn binary_notice_cursor_displays_arrow_marker() {
    let mut app = app_with_file(timed_binary_file("assets/icon.png", 0));
    app.scroll_to(1);

    let view = render_to_string(&app, 80, 8);
    assert!(
        view.contains(""),
        "cursor parked on a binary notice row must still be visible:\n{view}"
    );
}

#[test]
fn centered_cursor_renders_arrow_near_viewport_middle() {
    // 40-row hunk, 12-row viewport, cursor parked deep inside the
    // hunk. In centered mode the cursor row should land at roughly
    // viewport_height / 2.
    let lines = numbered_added_lines(40);
    let mut app = app_with_context_hunk("src/foo.rs", "fn long_function() {", lines, 100);
    let header = app.layout.hunk_starts[0];
    // Park the cursor 20 rows past the hunk header (well inside the
    // hunk). Settle the scroll animation so this test asserts on
    // the final viewport, not a mid-tween sample.
    app.scroll_to(header + 20);
    app.anim = None;

    let buffer = render_buffer(&app, 80, 12);

    // Find the row that holds the yellow `▶` marker.
    let (_, y) = first_cell_matching(&buffer, |cell| {
        cell.symbol() == "" && cell.style().fg == Some(Color::Yellow)
    })
    .expect("expected the cursor `▶` to be drawn");
    // Sticky takes row 0, so the body height is 11. We expect the
    // cursor near the middle of the body — between rows 4 and 8 of
    // the full buffer, well within tolerance.
    assert!(
        (4..=8).contains(&y),
        "expected cursor near viewport middle, was at row {y}"
    );
}

#[test]
fn top_cursor_renders_arrow_near_viewport_top() {
    // Same fixture, toggled to Top placement. The arrow should
    // sit at the body's top row, just below the pinned sticky
    // hunk header.
    let lines = numbered_added_lines(40);
    let mut app = app_with_context_hunk("src/foo.rs", "fn long_function() {", lines, 100);
    let header = app.layout.hunk_starts[0];
    app.scroll_to(header + 20);
    app.anim = None;
    app.cursor_placement = crate::app::CursorPlacement::Top;

    let buffer = render_buffer(&app, 80, 12);

    let (_, y) = first_cell_matching(&buffer, |cell| {
        cell.symbol() == "" && cell.style().fg == Some(Color::Yellow)
    })
    .expect("expected the cursor `▶` to be drawn");
    // Top mode + sticky header (row 0): the cursor should sit at
    // the first body row, which is y=1 (right below the sticky
    // header).
    assert_eq!(y, 1, "expected cursor at viewport ceiling, was at row {y}");
}

#[test]
fn sticky_header_decision_agrees_with_final_body_height() {
    // Regression for Codex round-4 finding #3: the previous
    // decision flow computed a provisional top with the full
    // area height, decided stickiness from it, and only then
    // shrank the body for the sticky banner. At the boundary
    // where `header_row == provisional_top`, a placement
    // recomputed against the (unused) reduced body would have
    // moved the top down by one row, but the sticky decision
    // had already been made with the stale top → the header
    // disappeared.
    //
    // The new flow pessimistically peeks at the reduced body
    // FIRST. If the header would fall off that peeked top,
    // sticky kicks in and the render uses the same reduced
    // body. Otherwise render uses the full body. The two are
    // now self-consistent.
    //
    // We exercise the boundary by positioning the cursor so
    // that the full-body placement puts the header JUST at the
    // top (not sticky-worthy) but any reduction would push it
    // off. A 20-line hunk with a tight viewport hits this.
    let lines = numbered_added_lines(20);
    let mut app = app_with_context_hunk("src/foo.rs", "fn boundary() {", lines, 100);
    // Jump into the middle of the hunk so any sticky reservation
    // would definitely push the header off-screen.
    let header_row = app.layout.hunk_starts[0];
    app.scroll_to(header_row + 5);
    app.anim = None;

    let buffer = render_buffer(&app, 80, 8);

    // Pin: whichever branch the new decision flow picks, row 0
    // must either be the sticky header (contains `boundary`) OR
    // be the actual hunk header / a row on or after header_row
    // but never a row from later in the hunk with the header
    // silently dropped.
    let row0 = buffer_row_text(&buffer, 0);
    assert!(
        row0.contains("boundary") || row0.contains("@@"),
        "row 0 must show the hunk header (sticky or inline), got:\n{row0}"
    );
}

#[test]
fn sticky_hunk_header_appears_when_cursor_is_below_it() {
    // Build a single hunk tall enough that scrolling past the header
    // pushes it off the top of a small viewport. The renderer should
    // pin the header on viewport row 0.
    let lines = numbered_added_lines(40);
    let mut app = app_with_context_hunk("src/foo.rs", "fn long_function() {", lines, 100);
    // Skip past the hunk header so the renderer has to pin it.
    let header_row = app.layout.hunk_starts[0];
    app.scroll_to(header_row + 10);
    app.anim = None;

    // Tight viewport so the original header row really is off-screen.
    let buffer = render_buffer(&app, 80, 8);

    // The very first row of the main area must contain the function
    // name from the sticky header.
    let row0 = buffer_row_text(&buffer, 0);
    assert!(
        row0.contains("long_function"),
        "row 0 should be the pinned hunk header, got:\n{row0}"
    );
}

fn app_with_context_hunk(name: &str, ctx: &str, lines: Vec<DiffLine>, secs: u64) -> App {
    app_with_hunks(name, vec![hunk_with_context(1, ctx, lines)], secs)
}

// ---- v0.4 slash-search in-body highlight ------------------------

/// Find the cells whose symbol matches `needle` in `buf` and return
/// the starting cell of each run plus its length in cells. Used by
/// the search-highlight tests to locate the rendered `foo` runs
/// without depending on concrete x offsets (which shift when the
/// left gutter evolves).
///
/// Skips the last row — the footer echoes the confirmed `/query`
/// so a naive scan would double-count matches from the status bar.
fn find_text_runs(buf: &ratatui::buffer::Buffer, needle: &str) -> Vec<(u16, u16, usize)> {
    let mut out = Vec::new();
    let width = buf.area().width;
    let height = buf.area().height;
    let body_height = height.saturating_sub(1);
    for y in 0..body_height {
        let row: String = (0..width)
            .map(|x| buf[(x, y)].symbol().to_string())
            .collect();
        let mut search_from = 0;
        while let Some(off) = row[search_from..].find(needle) {
            let start_byte = search_from + off;
            // Map byte offset to cell x index by walking symbols.
            let mut x = 0u16;
            let mut cum_bytes = 0usize;
            for xi in 0..width {
                let sym = buf[(xi, y)].symbol();
                if cum_bytes == start_byte {
                    x = xi;
                    break;
                }
                cum_bytes += sym.len();
            }
            out.push((x, y, needle.chars().count()));
            search_from = start_byte + needle.len();
        }
    }
    out
}

fn first_text_run(buf: &ratatui::buffer::Buffer, needle: &str) -> (u16, u16, usize) {
    find_text_runs(buf, needle)
        .into_iter()
        .next()
        .unwrap_or_else(|| panic!("{needle} not found in buffer"))
}

fn row_containing(buf: &ratatui::buffer::Buffer, needle: &str) -> u16 {
    first_text_run(buf, needle).1
}

#[test]
fn search_current_match_renders_with_yellow_background() {
    // `/foo<Enter>` on a single Added line containing one `foo` must
    // paint the 3 cells of `foo` with Yellow bg + Black fg + Bold.
    // Before this slice the matched cells inherit the diff bg_added
    // green, so the test fails loudly until the overlay lands.
    let mut app = single_added_app("a.rs", "let foo = 1;");
    let match_count = install_search(&mut app, "foo", 0);
    assert_eq!(match_count, 1, "test fixture precondition");

    let buffer = render_buffer(&app, 80, 6);

    let runs = find_text_runs(&buffer, "foo");
    assert_eq!(runs.len(), 1, "rendered buffer should contain one `foo`");
    let (x, y, len) = runs[0];
    for dx in 0..len as u16 {
        let cell = &buffer[(x + dx, y)];
        let style = cell.style();
        assert_eq!(
            style.bg,
            Some(Color::Yellow),
            "current-match cell at ({},{}) must carry Yellow bg, got {:?}",
            x + dx,
            y,
            style,
        );
        assert_eq!(
            style.fg,
            Some(Color::Black),
            "current-match cell at ({},{}) must carry Black fg, got {:?}",
            x + dx,
            y,
            style,
        );
        assert!(
            style.add_modifier.contains(Modifier::BOLD),
            "current-match cell at ({},{}) must be bold, got {:?}",
            x + dx,
            y,
            style,
        );
    }
}

#[test]
fn search_other_matches_render_with_underline_and_preserve_diff_bg() {
    // Two `foo` matches on a single Added line. `current=0` (= first
    // match) wears the Yellow reversal; the second one still gets a
    // visual cue (UNDERLINED + BOLD) while keeping the diff bg_added
    // green so the add/delete signal survives the overlay.
    let mut app = single_added_app("a.rs", "foo bar foo");
    let match_count = install_search(&mut app, "foo", 0);
    assert_eq!(match_count, 2, "test fixture precondition");

    let buffer = render_buffer(&app, 80, 6);

    let runs = find_text_runs(&buffer, "foo");
    assert_eq!(runs.len(), 2, "expected two `foo` runs in the buffer");

    // First run = current. Yellow bg already asserted by the other
    // test; here just sanity-check it is NOT underlined (it is
    // reversed instead so underline would be redundant + distracting).
    let (x0, y0, len0) = runs[0];
    let first_cell = &buffer[(x0, y0)].style();
    assert_eq!(first_cell.bg, Some(Color::Yellow));

    // Second run = non-current. Must carry UNDERLINED + BOLD, and
    // must keep the diff bg_added green (NOT Yellow).
    let (x1, y1, len1) = runs[1];
    for dx in 0..len1 as u16 {
        let style = buffer[(x1 + dx, y1)].style();
        assert_eq!(
            style.bg,
            Some(BG_ADDED),
            "non-current match cell at ({},{}) must keep bg_added green, got {:?}",
            x1 + dx,
            y1,
            style,
        );
        assert!(
            style.add_modifier.contains(Modifier::UNDERLINED),
            "non-current match cell at ({},{}) must be underlined, got {:?}",
            x1 + dx,
            y1,
            style,
        );
        assert!(
            style.add_modifier.contains(Modifier::BOLD),
            "non-current match cell at ({},{}) must be bold, got {:?}",
            x1 + dx,
            y1,
            style,
        );
    }

    // Also assert that non-match cells in the same row keep bg_added
    // and do NOT carry UNDERLINED (no accidental whole-line underline).
    // The ` bar ` space between matches sits in (x0+len0 .. x1).
    for xi in (x0 + len0 as u16)..x1 {
        let style = buffer[(xi, y0)].style();
        assert_eq!(
            style.bg,
            Some(BG_ADDED),
            "inter-match cell at ({},{}) must keep bg_added green, got {:?}",
            xi,
            y0,
            style,
        );
        assert!(
            !style.add_modifier.contains(Modifier::UNDERLINED),
            "inter-match cell at ({},{}) must NOT be underlined, got {:?}",
            xi,
            y0,
            style,
        );
    }
}

#[test]
fn search_current_match_on_cursor_row_retains_yellow_background() {
    // `commit_search_input` and `search_jump_next`/`_prev` both call
    // `scroll_to(row)`, so the cursor almost always sits on top of
    // the current match. `apply_cursor_gutter_tint` darkens every
    // cell bg on the cursor row; without a carve-out for search
    // colors, the Yellow reversal collapses into `DEFAULT_DIM`
    // (Rgb(30,30,36)) and the user perceives the highlight as
    // "dark". This test pins the carve-out.
    let mut app = single_added_app("a.rs", "let foo = 1;");
    install_search(&mut app, "foo", 0);
    let match_row = app.search.as_ref().unwrap().matches[0].row;
    // Force the cursor onto the match row (mirrors the
    // post-`n`/`N` state where `scroll_to(match.row)` runs).
    app.scroll = match_row;
    app.follow_mode = false;

    let buffer = render_buffer(&app, 80, 6);

    let runs = find_text_runs(&buffer, "foo");
    assert_eq!(runs.len(), 1, "rendered buffer should contain one `foo`");
    let (x, y, len) = runs[0];
    for dx in 0..len as u16 {
        let cell = &buffer[(x + dx, y)];
        let style = cell.style();
        assert_eq!(
            style.bg,
            Some(Color::Yellow),
            "current-match cell at ({},{}) must keep Yellow bg even on the cursor row, got {:?}",
            x + dx,
            y,
            style,
        );
        assert_eq!(
            style.fg,
            Some(Color::Black),
            "current-match cell at ({},{}) must keep Black fg on the cursor row, got {:?}",
            x + dx,
            y,
            style,
        );
    }
}

#[test]
fn footer_shows_search_query_and_position() {
    // With an active `SearchState`, the footer must echo the query
    // and a `[current/total]` counter so the user can tell which
    // hit `n`/`N` is about to jump to without counting visually.
    let mut app = added_hunk_app("a.rs", 1, &["foo one", "foo two", "foo three"], 100);
    let match_count = install_search(&mut app, "foo", 1); // 2/3
    assert_eq!(match_count, 3);
    app.follow_mode = false;

    let view = render_to_string(&app, 120, 8);
    assert!(
        view.contains("/foo"),
        "footer must echo the confirmed search query, got:\n{view}"
    );
    assert!(
        view.contains("[2/3]"),
        "footer must show [current/total] position, got:\n{view}"
    );
}

#[test]
fn search_other_match_in_unfocused_hunk_is_not_dimmed() {
    // When matches land in a hunk that is NOT currently selected,
    // `base_style` carries `Modifier::DIM` (the rest of the hunk
    // body dims so the focused hunk stands out). The search
    // overlay must strip DIM so matches stay loud regardless of
    // which hunk the cursor is in — otherwise every non-focus
    // match looks "dark" even though underline + bold are set.
    let mut app = app_with_hunks(
        "a.rs",
        vec![
            added_hunk(1, &["first foo"]),
            added_hunk(50, &["second foo"]),
        ],
        100,
    );
    let match_count = install_search(&mut app, "foo", 0);
    assert_eq!(match_count, 2, "test fixture precondition");
    // `current = 0` -> first hunk's `foo` is current; the cursor
    // sits on the first match row so the second hunk is unfocused.
    let first_row = app.search.as_ref().unwrap().matches[0].row;
    app.follow_mode = false;
    // Place the cursor explicitly on the first hunk's DiffLine so
    // `current_hunk()` resolves to hunk 0 and hunk 1 is unfocused.
    app.scroll = first_row;

    let buffer = render_buffer(&app, 80, 20);

    let runs = find_text_runs(&buffer, "foo");
    assert_eq!(runs.len(), 2, "both `foo` runs must render in the viewport");
    // The second run is in the unfocused hunk — `apply_search_overlay`
    // must have stripped `DIM` from `base_style` so the highlight
    // does not look washed out.
    let (x1, y1, len1) = runs[1];
    for dx in 0..len1 as u16 {
        let style = buffer[(x1 + dx, y1)].style();
        assert!(
            !style.add_modifier.contains(Modifier::DIM),
            "non-current match in unfocused hunk at ({},{}) must NOT carry DIM, got {:?}",
            x1 + dx,
            y1,
            style,
        );
        assert!(
            style.add_modifier.contains(Modifier::UNDERLINED),
            "non-current match in unfocused hunk at ({},{}) must be underlined, got {:?}",
            x1 + dx,
            y1,
            style,
        );
    }
}