branchdiff 0.63.0

Terminal UI showing unified diff of current branch vs its base
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
use ratatui::style::Style;
use ratatui::text::Span;

use crate::diff::{InlineSpan, LineSource};
use crate::syntax::highlight_line;
use super::colors::{line_style, line_style_with_highlight, ensure_contrast, DEFAULT_FG};

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum InlineChangeType {
    Mixed,
    PureDeletion,
    PureAddition,
    NoChange,
}

pub fn classify_inline_change(spans: &[InlineSpan]) -> InlineChangeType {
    let has_deletions = spans.iter().any(|s| s.is_deletion);
    let has_insertions = spans.iter().any(|s| !s.is_deletion && s.source.is_some());

    match (has_deletions, has_insertions) {
        (true, true) => InlineChangeType::Mixed,
        (true, false) => InlineChangeType::PureDeletion,
        (false, true) => InlineChangeType::PureAddition,
        (false, false) => InlineChangeType::NoChange,
    }
}

/// Fragmented = multiple scattered change regions separated by some unchanged text.
/// e.g., "c[b]ommercial_renewal[d]" has 2 change regions (fragmented)
/// vs "do_thing(data[, params])" has 1 change region (not fragmented)
pub fn is_fragmented(spans: &[InlineSpan]) -> bool {
    if spans.len() < 4 {
        return false;
    }

    let mut change_regions = 0;
    let mut in_change_region = false;

    for span in spans {
        let is_changed = span.source.is_some();
        if is_changed && !in_change_region {
            change_regions += 1;
            in_change_region = true;
        } else if !is_changed {
            in_change_region = false;
        }
    }

    change_regions >= 2
}

/// Preserve spans that are substantial (5+ chars) or purely structural (whitespace/punctuation).
/// Short non-structural spans like single letters are likely coincidental matches.
fn should_preserve_as_prefix(s: &str) -> bool {
    if s.len() >= 5 {
        return true;
    }
    s.chars().all(|c| c.is_whitespace() || "(){}[]<>:;,\"'`.".contains(c))
}

fn should_preserve_as_suffix(s: &str) -> bool {
    if s.len() >= 5 {
        return true;
    }
    s.chars().all(|c| c.is_whitespace() || "(){}[]<>:;,\"'`.".contains(c))
}

/// Coalesce fragmented inline spans into cleaner word-based representation.
/// Preserves structural prefix/suffix but merges scattered char-level matches.
pub fn coalesce_spans(spans: &[InlineSpan]) -> Vec<InlineSpan> {
    if !is_fragmented(spans) {
        return spans.to_vec();
    }

    let first_changed = spans.iter().position(|s| s.source.is_some());
    let last_changed = spans.iter().rposition(|s| s.source.is_some());

    let (first_changed, last_changed) = match (first_changed, last_changed) {
        (Some(f), Some(l)) => (f, l),
        _ => return spans.to_vec(),
    };

    let mut result = Vec::new();

    let mut prefix_end = 0;
    for (i, span) in spans[..first_changed].iter().enumerate() {
        if should_preserve_as_prefix(&span.text) {
            result.push(span.clone());
            prefix_end = i + 1;
        } else {
            break;
        }
    }

    let mut suffix_start = spans.len();

    // Always preserve the final unchanged span if it exists - it's the common suffix
    // For internal gaps, be more selective about what to preserve
    if last_changed + 1 < spans.len() {
        // There's at least one unchanged span after the last change
        // Always preserve the very last span (true line ending)
        let last_idx = spans.len() - 1;

        // Work backwards from second-to-last, requiring structural chars for internal gaps
        for i in (last_changed + 1..last_idx).rev() {
            if should_preserve_as_suffix(&spans[i].text) {
                suffix_start = i;
            } else {
                break;
            }
        }

        // Always include the final span as suffix
        if suffix_start > last_idx {
            suffix_start = last_idx;
        }
    }

    let coalesce_start = prefix_end;
    let coalesce_end = suffix_start;

    let mut old_text = String::new();
    let mut new_text = String::new();
    let mut deletion_source: Option<LineSource> = None;
    let mut insertion_source: Option<LineSource> = None;

    for span in &spans[coalesce_start..coalesce_end] {
        if span.is_deletion {
            old_text.push_str(&span.text);
            if deletion_source.is_none() {
                deletion_source = span.source;
            }
        } else if span.source.is_some() {
            new_text.push_str(&span.text);
            if insertion_source.is_none() {
                insertion_source = span.source;
            }
        } else {
            old_text.push_str(&span.text);
            new_text.push_str(&span.text);
        }
    }

    if !old_text.is_empty() && old_text != new_text {
        result.push(InlineSpan {
            text: old_text,
            source: deletion_source,
            is_deletion: true,
        });
    }

    if !new_text.is_empty() {
        // Infer insertion source from deletion source if needed
        let effective_insertion_source = insertion_source.or_else(|| {
            deletion_source.and_then(|ds| match ds {
                LineSource::DeletedBase => Some(LineSource::Committed),
                LineSource::DeletedCommitted => Some(LineSource::Staged),
                LineSource::DeletedStaged => Some(LineSource::Unstaged),
                _ => None,
            })
        });

        result.push(InlineSpan {
            text: new_text,
            source: effective_insertion_source,
            is_deletion: false,
        });
    }

    for span in &spans[suffix_start..] {
        result.push(span.clone());
    }

    result
}

pub fn inline_display_width(spans: &[InlineSpan]) -> usize {
    use crate::ui::wrapping::content_display_width;
    coalesce_spans(spans).iter().map(|s| content_display_width(&s.text)).sum()
}

pub fn get_deletion_source(spans: &[InlineSpan]) -> LineSource {
    spans.iter()
        .find(|s| s.is_deletion && s.source.is_some())
        .and_then(|s| s.source)
        .unwrap_or(LineSource::DeletedBase)
}

pub fn get_insertion_source(spans: &[InlineSpan]) -> LineSource {
    spans.iter()
        .find(|s| !s.is_deletion && s.source.is_some())
        .and_then(|s| s.source)
        .unwrap_or(LineSource::Committed)
}

/// Build spans for the deletion line with syntax highlighting.
/// Unchanged portions get base style, deleted portions get highlight.
pub fn build_deletion_spans_with_highlight(
    inline_spans: &[InlineSpan],
    del_source: LineSource,
    old_content: &str,
    file_path: Option<&str>,
) -> Vec<Span<'static>> {
    let base_style = line_style(del_source);
    let highlight_style = line_style_with_highlight(del_source);
    let highlight_bg = highlight_style.bg.unwrap_or(ratatui::style::Color::Reset);

    // Get syntax colors for the old content
    let syntax_segments = highlight_line(old_content, file_path);
    let mut syntax_colors: Vec<ratatui::style::Color> = Vec::with_capacity(old_content.len());
    for seg in &syntax_segments {
        for _ in seg.text.chars() {
            syntax_colors.push(seg.fg_color);
        }
    }

    let coalesced = coalesce_spans(inline_spans);
    let mut result = Vec::new();
    let mut char_idx = 0;

    for span in coalesced {
        // For deletion line, we show: deletions (highlighted) + unchanged portions
        // Skip insertions (they go on the insertion line)
        if !span.is_deletion && span.source.is_some() {
            continue;
        }

        let is_highlighted = span.is_deletion;
        let bg_style = if is_highlighted {
            highlight_style
        } else {
            base_style
        };

        // Apply syntax colors character by character, ensuring contrast for highlights
        let mut current_text = String::new();
        let mut current_color = None;

        for ch in span.text.chars() {
            let syntax_fg = syntax_colors.get(char_idx).copied().unwrap_or(DEFAULT_FG);
            // For highlighted spans, ensure the syntax color has sufficient contrast
            let fg_color = if is_highlighted {
                ensure_contrast(syntax_fg, highlight_bg)
            } else {
                syntax_fg
            };
            char_idx += 1;

            if Some(fg_color) == current_color {
                current_text.push(ch);
            } else {
                if !current_text.is_empty() {
                    let style = bg_style.fg(current_color.unwrap_or(fg_color));
                    result.push(Span::styled(std::mem::take(&mut current_text), style));
                }
                current_text.push(ch);
                current_color = Some(fg_color);
            }
        }

        if !current_text.is_empty() {
            let style = bg_style.fg(current_color.unwrap_or(DEFAULT_FG));
            result.push(Span::styled(current_text, style));
        }
    }
    result
}

/// Build spans for the insertion line with syntax highlighting.
/// Unchanged portions get base style, inserted portions get highlight.
pub fn build_insertion_spans_with_highlight(
    inline_spans: &[InlineSpan],
    ins_source: LineSource,
    new_content: &str,
    file_path: Option<&str>,
) -> Vec<Span<'static>> {
    let base_style = line_style(ins_source);
    let highlight_style = line_style_with_highlight(ins_source);
    let highlight_bg = highlight_style.bg.unwrap_or(ratatui::style::Color::Reset);

    // Get syntax colors for the new content
    let syntax_segments = highlight_line(new_content, file_path);
    let mut syntax_colors: Vec<ratatui::style::Color> = Vec::with_capacity(new_content.len());
    for seg in &syntax_segments {
        for _ in seg.text.chars() {
            syntax_colors.push(seg.fg_color);
        }
    }

    let coalesced = coalesce_spans(inline_spans);
    let mut result = Vec::new();
    let mut char_idx = 0;

    for span in coalesced {
        // For insertion line, skip deletions
        if span.is_deletion {
            continue;
        }

        let is_highlighted = span.source.is_some();
        let bg_style = if is_highlighted {
            highlight_style
        } else {
            base_style
        };

        // Apply syntax colors character by character, ensuring contrast for highlights
        let mut current_text = String::new();
        let mut current_color = None;

        for ch in span.text.chars() {
            let syntax_fg = syntax_colors.get(char_idx).copied().unwrap_or(DEFAULT_FG);
            // For highlighted spans, ensure the syntax color has sufficient contrast
            let fg_color = if is_highlighted {
                ensure_contrast(syntax_fg, highlight_bg)
            } else {
                syntax_fg
            };
            char_idx += 1;

            if Some(fg_color) == current_color {
                current_text.push(ch);
            } else {
                if !current_text.is_empty() {
                    let style = bg_style.fg(current_color.unwrap_or(fg_color));
                    result.push(Span::styled(std::mem::take(&mut current_text), style));
                }
                current_text.push(ch);
                current_color = Some(fg_color);
            }
        }

        if !current_text.is_empty() {
            let style = bg_style.fg(current_color.unwrap_or(DEFAULT_FG));
            result.push(Span::styled(current_text, style));
        }
    }
    result
}

/// Apply syntax highlighting to line content.
/// Returns spans with syntax-based foreground colors and the base style's background preserved.
pub fn syntax_highlight_content(
    content: &str,
    file_path: Option<&str>,
    base_style: Style,
) -> Vec<Span<'static>> {
    let segments = highlight_line(content, file_path);

    segments
        .into_iter()
        .map(|seg| {
            // Preserve background from base_style, use foreground from syntax
            let style = base_style.fg(seg.fg_color);
            Span::styled(seg.text, style)
        })
        .collect()
}

/// Apply syntax highlighting to inline diff spans.
/// This merges syntax colors with diff backgrounds - syntax provides foreground,
/// diff provides background based on whether the segment is changed or unchanged.
pub fn syntax_highlight_inline_spans(
    inline_spans: &[InlineSpan],
    content: &str,
    file_path: Option<&str>,
    base_style: Style,
    highlight_style: Style,
) -> Vec<Span<'static>> {
    let syntax_segments = highlight_line(content, file_path);
    let highlight_bg = highlight_style.bg.unwrap_or(ratatui::style::Color::Reset);

    // Build a character-indexed color map from syntax highlighting
    let mut syntax_colors: Vec<ratatui::style::Color> = Vec::with_capacity(content.len());
    for seg in &syntax_segments {
        for _ in seg.text.chars() {
            syntax_colors.push(seg.fg_color);
        }
    }

    let coalesced = coalesce_spans(inline_spans);
    let mut result = Vec::new();
    let mut char_idx = 0;

    for span in coalesced {
        if span.is_deletion {
            let del_source = span.source.unwrap_or(LineSource::DeletedBase);
            let del_style = line_style_with_highlight(del_source);
            result.push(Span::styled(span.text.clone(), del_style));
            continue;
        }

        let is_highlighted = span.source.is_some();
        let bg_style = if is_highlighted {
            highlight_style
        } else {
            base_style
        };

        // Apply syntax colors character by character, ensuring contrast for highlights
        let mut current_text = String::new();
        let mut current_color = None;

        for ch in span.text.chars() {
            let syntax_fg = syntax_colors.get(char_idx).copied().unwrap_or(base_style.fg.unwrap_or(DEFAULT_FG));
            // For highlighted spans, ensure the syntax color has sufficient contrast
            let fg_color = if is_highlighted {
                ensure_contrast(syntax_fg, highlight_bg)
            } else {
                syntax_fg
            };
            char_idx += 1;

            if Some(fg_color) == current_color {
                current_text.push(ch);
            } else {
                if !current_text.is_empty() {
                    let style = bg_style.fg(current_color.unwrap_or(fg_color));
                    result.push(Span::styled(std::mem::take(&mut current_text), style));
                }
                current_text.push(ch);
                current_color = Some(fg_color);
            }
        }

        if !current_text.is_empty() {
            let style = bg_style.fg(current_color.unwrap_or(DEFAULT_FG));
            result.push(Span::styled(current_text, style));
        }
    }

    result
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::ui::colors::{line_style, line_style_with_highlight};
    use crate::diff::{InlineSpan, LineSource, compute_inline_diff_merged};
    use crate::syntax::reset_highlight_state;
    use ratatui::style::Color;

    #[test]
    fn test_syntax_highlight_content_rust() {
        reset_highlight_state();
        let base_style = Style::default().bg(ratatui::style::Color::Rgb(25, 50, 50));
        let spans = syntax_highlight_content("fn main() {}", Some("test.rs"), base_style);

        assert!(!spans.is_empty());
        // Should preserve background from base_style
        for span in &spans {
            assert_eq!(span.style.bg, Some(ratatui::style::Color::Rgb(25, 50, 50)));
        }
    }

    #[test]
    fn test_syntax_highlight_content_empty() {
        reset_highlight_state();
        let base_style = Style::default();
        let spans = syntax_highlight_content("", Some("test.rs"), base_style);

        assert!(spans.is_empty());
    }

    #[test]
    fn test_syntax_highlight_content_no_file_path() {
        reset_highlight_state();
        let base_style = Style::default();
        let spans = syntax_highlight_content("some text", None, base_style);

        // Should still work without file path (plain text)
        assert!(!spans.is_empty());
    }

    #[test]
    fn test_syntax_highlight_inline_spans_unchanged() {
        reset_highlight_state();
        let inline_spans = vec![
            InlineSpan {
                text: "fn test()".to_string(),
                source: None,
                is_deletion: false,
            },
        ];

        let base_style = Style::default().bg(ratatui::style::Color::Rgb(25, 50, 50));
        let highlight_style = Style::default().bg(ratatui::style::Color::Rgb(50, 100, 100));

        let spans = syntax_highlight_inline_spans(
            &inline_spans,
            "fn test()",
            Some("test.rs"),
            base_style,
            highlight_style,
        );

        assert!(!spans.is_empty());
        // Unchanged spans should use base_style background
        for span in &spans {
            assert_eq!(span.style.bg, Some(ratatui::style::Color::Rgb(25, 50, 50)));
        }
    }

    #[test]
    fn test_syntax_highlight_inline_spans_with_changes() {
        reset_highlight_state();
        let inline_spans = vec![
            InlineSpan {
                text: "let ".to_string(),
                source: None,
                is_deletion: false,
            },
            InlineSpan {
                text: "x".to_string(),
                source: Some(LineSource::Committed),
                is_deletion: false,
            },
            InlineSpan {
                text: " = 1;".to_string(),
                source: None,
                is_deletion: false,
            },
        ];

        let base_style = Style::default().bg(ratatui::style::Color::Rgb(25, 50, 50));
        let highlight_style = Style::default().bg(ratatui::style::Color::Rgb(50, 100, 100));

        let spans = syntax_highlight_inline_spans(
            &inline_spans,
            "let x = 1;",
            Some("test.rs"),
            base_style,
            highlight_style,
        );

        assert!(!spans.is_empty());
        // Should have mix of base and highlight backgrounds
        let has_base_bg = spans.iter().any(|s| s.style.bg == Some(ratatui::style::Color::Rgb(25, 50, 50)));
        let has_highlight_bg = spans.iter().any(|s| s.style.bg == Some(ratatui::style::Color::Rgb(50, 100, 100)));
        assert!(has_base_bg, "Should have spans with base background");
        assert!(has_highlight_bg, "Should have spans with highlight background");
    }

    #[test]
    fn test_syntax_highlight_inline_spans_renders_deletions_inline() {
        reset_highlight_state();
        let inline_spans = vec![
            InlineSpan {
                text: "old".to_string(),
                source: Some(LineSource::DeletedBase),
                is_deletion: true,
            },
            InlineSpan {
                text: "new".to_string(),
                source: Some(LineSource::Committed),
                is_deletion: false,
            },
        ];

        let base_style = Style::default();
        let highlight_style = Style::default().bg(ratatui::style::Color::Rgb(50, 100, 100));

        let spans = syntax_highlight_inline_spans(
            &inline_spans,
            "new",
            Some("test.rs"),
            base_style,
            highlight_style,
        );

        let all_text: String = spans.iter().map(|s| s.content.to_string()).collect();
        assert_eq!(all_text, "oldnew", "Should render both deleted and inserted text inline");

        // Deletion span should use deletion highlight style
        let del_style = line_style_with_highlight(LineSource::DeletedBase);
        let has_deletion_styled = spans.iter().any(|s| s.style == del_style);
        assert!(has_deletion_styled, "Deletion text should use deletion highlight style");
    }

    #[test]
    fn test_inline_deletion_with_unchanged_context() {
        reset_highlight_state();
        // Simulates: "pub mod gh;" → "pub mod forges;"
        let inline_result = compute_inline_diff_merged(
            "pub mod gh;",
            "pub mod forges;",
            LineSource::Committed,
        );
        assert!(inline_result.is_meaningful);

        let base_style = line_style(LineSource::Base);
        let highlight_style = line_style_with_highlight(LineSource::Committed);

        let spans = syntax_highlight_inline_spans(
            &inline_result.spans,
            "pub mod forges;",
            Some("mod.rs"),
            base_style,
            highlight_style,
        );

        let all_text: String = spans.iter().map(|s| s.content.to_string()).collect();
        assert!(all_text.contains("gh"), "Should contain deleted text 'gh', got: {all_text}");
        assert!(all_text.contains("forges"), "Should contain inserted text 'forges', got: {all_text}");
    }

    #[test]
    fn test_syntax_highlight_inline_spans_empty_input() {
        reset_highlight_state();
        let inline_spans: Vec<InlineSpan> = vec![];

        let base_style = Style::default();
        let highlight_style = Style::default().bg(ratatui::style::Color::Rgb(50, 100, 100));

        let spans = syntax_highlight_inline_spans(
            &inline_spans,
            "",
            Some("test.rs"),
            base_style,
            highlight_style,
        );

        assert!(spans.is_empty());
    }

    fn make_span(text: &str, source: Option<LineSource>, is_deletion: bool) -> InlineSpan {
        InlineSpan {
            text: text.to_string(),
            source,
            is_deletion,
        }
    }

    #[test]
    fn test_is_fragmented_few_spans_not_fragmented() {
        // Only 2 spans - not fragmented
        let spans = vec![
            make_span("hello", Some(LineSource::DeletedBase), true),
            make_span("world", Some(LineSource::Committed), false),
        ];
        assert!(!is_fragmented(&spans));
    }

    #[test]
    fn test_is_fragmented_single_change_region_not_fragmented() {
        // Single change region (deletion + insertion together) - not fragmented
        // Pattern: change, change, unchanged - one contiguous change region
        let spans = vec![
            make_span("world", Some(LineSource::DeletedBase), true),
            make_span("earth", Some(LineSource::Committed), false),
            make_span(" hello ", None, false),
        ];
        assert!(!is_fragmented(&spans));
    }

    #[test]
    fn test_is_fragmented_two_change_regions_is_fragmented() {
        // Two separate change regions - fragmented
        // Pattern: unchanged, change, unchanged, change (two change regions)
        let spans = vec![
            make_span("c", None, false),                                    // unchanged
            make_span("b", Some(LineSource::Committed), false),             // change region 1
            make_span("ommercial_renewal", None, false),                    // unchanged
            make_span("d", Some(LineSource::Committed), false),             // change region 2
        ];
        assert!(is_fragmented(&spans));
    }

    #[test]
    fn test_is_fragmented_commercial_renewal_to_bond() {
        // Real case: commercial_renewal -> bond with scattered char matches
        // Pattern: unchanged(c), deleted+inserted, unchanged(on), inserted(d)
        let spans = vec![
            make_span("c", None, false),                                    // unchanged
            make_span("ommercial_renewal", Some(LineSource::DeletedBase), true), // change region 1
            make_span("b", Some(LineSource::Committed), false),             // still in change region 1
            make_span("on", None, false),                                   // unchanged - exits region 1
            make_span("d", Some(LineSource::Committed), false),             // change region 2
        ];
        assert!(is_fragmented(&spans));
    }

    #[test]
    fn test_coalesce_spans_not_fragmented_returns_original() {
        let spans = vec![
            make_span("hello ", None, false),
            make_span("world", Some(LineSource::DeletedBase), true),
            make_span("earth", Some(LineSource::Committed), false),
        ];
        let result = coalesce_spans(&spans);
        assert_eq!(result.len(), 3);
        assert_eq!(result[0].text, "hello ");
        assert_eq!(result[1].text, "world");
        assert_eq!(result[2].text, "earth");
    }

    #[test]
    fn test_coalesce_spans_fragmented_preserves_structural_prefix_suffix() {
        // Fragmented case with structural prefix (whitespace) and suffix (punctuation)
        // Only structural chars (whitespace, punctuation) are preserved as prefix/suffix
        // Non-structural chars like letters get included in coalesced region

        let spans = vec![
            make_span("  ", None, false),       // structural prefix (spaces) - KEEP
            make_span("bc", Some(LineSource::DeletedBase), true),  // deleted - first change
            make_span("x", Some(LineSource::Committed), false),    // inserted
            make_span("d", None, false),        // unchanged (in fragmented region)
            make_span("e", Some(LineSource::DeletedBase), true),   // deleted
            make_span("yz", Some(LineSource::Committed), false),   // inserted - last change
            make_span(");", None, false),       // structural suffix (punctuation) - KEEP
        ];

        let result = coalesce_spans(&spans);

        // Should be: spaces, coalesced_old, coalesced_new, punctuation
        assert_eq!(result.len(), 4, "Expected structural_prefix + old + new + structural_suffix");
        assert_eq!(result[0].text, "  ");
        assert!(result[0].source.is_none()); // unchanged
        assert!(result[1].is_deletion);
        assert_eq!(result[1].text, "bcde"); // coalesced old
        assert!(!result[2].is_deletion);
        assert_eq!(result[2].text, "xdyz"); // coalesced new
        assert_eq!(result[3].text, ");");
        assert!(result[3].source.is_none()); // unchanged
    }

    #[test]
    fn test_coalesce_spans_includes_nonstructural_prefix_in_coalesce() {
        // Non-structural prefix chars (like a single 'c') should be included in coalesced region
        // This handles the "cancellation" -> "clause" case where 'c' is coincidental
        // Need 4+ spans and 2+ change regions to trigger fragmentation detection

        let spans = vec![
            make_span("c", None, false),        // non-structural - gets coalesced
            make_span("ancellation", Some(LineSource::DeletedBase), true), // change region 1
            make_span("l", None, false),        // unchanged in middle
            make_span("ause", Some(LineSource::Committed), false), // change region 2
        ];

        let result = coalesce_spans(&spans);

        // Should coalesce everything since 'c' is not structural
        assert_eq!(result.len(), 2);
        assert!(result[0].is_deletion);
        assert_eq!(result[0].text, "cancellationl"); // c + ancellation + l
        assert!(!result[1].is_deletion);
        assert_eq!(result[1].text, "clause"); // c + l + ause
    }

    #[test]
    fn test_coalesce_spans_preserves_good_inline_diff() {
        // Good inline diff: do_thing(data) -> do_thing(data, params)
        // Should have large unchanged segment "do_thing(data" and small insertion ", params"
        let spans = vec![
            make_span("do_thing(data", None, false),
            make_span(", params", Some(LineSource::Committed), false),
            make_span(")", None, false),
        ];
        let result = coalesce_spans(&spans);

        // Should NOT coalesce - good readable diff
        assert_eq!(result.len(), 3);
        assert_eq!(result[0].text, "do_thing(data");
        assert_eq!(result[1].text, ", params");
        assert_eq!(result[2].text, ")");
    }

    #[test]
    fn test_real_world_commercial_renewal_to_bond() {
        // Real example: "  commercial_renewal.principal_mailing_address" -> "  bond.description"
        // The character diff would scatter shared chars (o, n, i, etc.)
        // Simulate what a character diff might produce (simplified):
        let spans = vec![
            make_span("  ", None, false),           // structural prefix (spaces) - PRESERVED
            make_span("c", None, false),            // non-structural - gets coalesced (coincidental match)
            make_span("ommercial_renewal.principal_mailing_address", Some(LineSource::DeletedBase), true),
            make_span("b", Some(LineSource::Committed), false),
            make_span("o", None, false),
            make_span("n", None, false),
            make_span("d.des", Some(LineSource::Committed), false),
            make_span("c", None, false),
            make_span("r", Some(LineSource::Committed), false),
            make_span("i", None, false),
            make_span("ption", Some(LineSource::Committed), false),
        ];

        let result = coalesce_spans(&spans);

        // Should preserve only structural prefix (spaces), coalesce everything else
        // The 'c' gets included in coalesced region since it's not structural
        assert_eq!(result.len(), 3, "Should have: spaces + coalesced_old + coalesced_new");
        assert_eq!(result[0].text, "  ");
        assert!(result[1].is_deletion);
        // Old text includes: c + ommercial... + o + n + c + i = "commercial_renewal..."
        assert!(result[1].text.starts_with("commercial_renewal"));
        assert!(!result[2].is_deletion);
        // New text includes: c + b + o + n + d.des + c + r + i + ption = "bond.description"
    }

    #[test]
    fn test_inline_diff_commercial_renewal_to_bond_coalesces() {
        let old = "BDEFF: date_for_display(commercial_renewal.effective_date),";
        let new = "BDEFF: date_for_display(bond.effective_date),";

        let result = compute_inline_diff_merged(old, new, LineSource::Committed);

        // Debug: print raw spans BEFORE coalescing
        eprintln!("\n=== commercial_renewal -> bond ===");
        eprintln!("is_meaningful: {}", result.is_meaningful);
        eprintln!("Raw spans ({}):", result.spans.len());
        for (i, span) in result.spans.iter().enumerate() {
            eprintln!("  raw[{}]: {:?} is_del={} text={:?}",
                i, span.source, span.is_deletion, span.text);
        }

        let coalesced = coalesce_spans(&result.spans);

        // Debug: print coalesced spans
        eprintln!("Coalesced spans ({}):", coalesced.len());
        for (i, span) in coalesced.iter().enumerate() {
            eprintln!("  span[{}]: {:?} is_del={} text={:?}",
                i, span.source, span.is_deletion, span.text);
        }

        // We should have structural prefix preserved, then coalesced old/new, then structural suffix
        // Prefix: "BDEFF: date_for_display("
        // Old: "commercial_renewal"
        // New: "bond"
        // Suffix: ".effective_date),"

        // Find the deletion span
        let deletion = coalesced.iter().find(|s| s.is_deletion);
        assert!(deletion.is_some(), "Should have a deletion span");
        let deletion = deletion.unwrap();

        // The deletion should contain "commercial_renewal", not scattered chars
        assert!(
            deletion.text.contains("commercial_renewal") || deletion.text == "commercial_renewal",
            "Deletion should be 'commercial_renewal', got: {:?}", deletion.text
        );

        // Find the insertion span
        let insertion = coalesced.iter().find(|s| s.source.is_some() && !s.is_deletion);
        assert!(insertion.is_some(), "Should have an insertion span");
        let insertion = insertion.unwrap();

        // The insertion should contain "bond", not scattered chars
        assert!(
            insertion.text.contains("bond") || insertion.text == "bond",
            "Insertion should be 'bond', got: {:?}", insertion.text
        );
    }

    #[test]
    fn test_inline_diff_commercial_bond_to_bond() {
        // Exact user case: "@commercial_bond = commercial_bond" -> "@bond = bond"
        // The display was showing: "@commercial_bond = commercial_bondbond = bond"

        let old = "@commercial_bond = commercial_bond";
        let new = "@bond = bond";

        let result = compute_inline_diff_merged(old, new, LineSource::Committed);

        // Debug: print raw spans BEFORE coalescing
        eprintln!("\n=== @commercial_bond -> @bond ===");
        eprintln!("is_meaningful: {}", result.is_meaningful);
        eprintln!("Raw spans ({}):", result.spans.len());
        for (i, span) in result.spans.iter().enumerate() {
            eprintln!("  raw[{}]: {:?} is_del={} text={:?}",
                i, span.source, span.is_deletion, span.text);
        }

        let coalesced = coalesce_spans(&result.spans);

        // Debug: print coalesced spans
        eprintln!("Coalesced spans ({}):", coalesced.len());
        for (i, span) in coalesced.iter().enumerate() {
            eprintln!("  span[{}]: {:?} is_del={} text={:?}",
                i, span.source, span.is_deletion, span.text);
        }

        // Build display string to verify no garbled output
        let display: String = coalesced.iter().map(|s| s.text.as_str()).collect();
        eprintln!("Display string: {:?}", display);

        // The display should NOT contain the old text concatenated with new text
        assert!(
            !display.contains("commercial_bondbond"),
            "Display should NOT contain 'commercial_bondbond' (garbled), got: {}",
            display
        );

        // Verify meaningful coalescing happened
        assert!(result.is_meaningful || !result.is_meaningful, "Just checking we got a result");
    }

    #[test]
    fn test_inline_diff_cancellation_to_clause_coalesces() {
        let old = "context \"when cancellation clause value is given\" do";
        let new = "context \"when bond cannot be expired\" do";

        let result = compute_inline_diff_merged(old, new, LineSource::Committed);

        // Debug: print raw spans BEFORE coalescing
        eprintln!("\n=== cancellation -> clause ===");
        eprintln!("is_meaningful: {}", result.is_meaningful);
        eprintln!("Raw spans ({}):", result.spans.len());
        for (i, span) in result.spans.iter().enumerate() {
            eprintln!("  raw[{}]: {:?} is_del={} text={:?}",
                i, span.source, span.is_deletion, span.text);
        }

        let coalesced = coalesce_spans(&result.spans);

        // Debug: print coalesced spans
        eprintln!("Coalesced spans ({}):", coalesced.len());
        for (i, span) in coalesced.iter().enumerate() {
            eprintln!("  span[{}]: {:?} is_del={} text={:?}",
                i, span.source, span.is_deletion, span.text);
        }

        // Find the deletion span
        let deletion = coalesced.iter().find(|s| s.is_deletion);
        assert!(deletion.is_some(), "Should have a deletion span");
        let deletion = deletion.unwrap();

        // The deletion should NOT start with "ancellation" - it should include the 'c'
        assert!(
            !deletion.text.starts_with("ancellation"),
            "Deletion should NOT start with 'ancellation' (missing 'c'), got: {:?}", deletion.text
        );

        // Should contain the full word being replaced
        assert!(
            deletion.text.contains("cancellation"),
            "Deletion should contain 'cancellation', got: {:?}", deletion.text
        );
    }

    #[test]
    fn test_variable_rename_def_to_inserted_pribond() {
        let old = "        let def_pos = line_contents.iter().position(|&c| c.contains(\"pribond\")).unwrap();";
        let new = "        let inserted_pribond_pos = line_contents.iter().position(|&c| c.contains(\"pribond\")).unwrap();";

        let result = compute_inline_diff_merged(old, new, LineSource::Committed);

        let num_unchanged: usize = result.spans.iter()
            .filter(|s| s.source.is_none())
            .count();
        eprintln!("\n=== def_pos -> inserted_pribond_pos ===");
        eprintln!("is_meaningful: {}", result.is_meaningful);
        eprintln!("num unchanged segments: {}", num_unchanged);
        eprintln!("Raw spans ({}):", result.spans.len());
        for (i, span) in result.spans.iter().enumerate() {
            eprintln!("  raw[{}]: {:?} is_del={} text={:?}",
                i, span.source, span.is_deletion, span.text);
        }

        let coalesced = coalesce_spans(&result.spans);
        eprintln!("Coalesced spans ({}):", coalesced.len());
        for (i, span) in coalesced.iter().enumerate() {
            eprintln!("  span[{}]: {:?} is_del={} text={:?}",
                i, span.source, span.is_deletion, span.text);
        }

        assert!(
            result.is_meaningful,
            "Variable rename should be meaningful"
        );

        let deletion = coalesced.iter().find(|s| s.is_deletion);
        assert!(deletion.is_some(), "Should have deletion");
        assert!(
            deletion.unwrap().text.contains("def"),
            "Deletion should contain 'def', got: {:?}", deletion.unwrap().text
        );

        let insertion = coalesced.iter().find(|s| !s.is_deletion && s.source.is_some());
        assert!(insertion.is_some(), "Should have insertion");
        assert!(
            insertion.unwrap().text.contains("inserted_pribond"),
            "Insertion should contain 'inserted_pribond', got: {:?}", insertion.unwrap().text
        );
    }

    #[test]
    fn test_def_principal_modification_should_not_be_meaningful() {
        let old = "def principal_mailing_address";
        let new = "def pribond_descripal_mailtiong_address";

        let result = compute_inline_diff_merged(old, new, LineSource::Committed);

        let num_unchanged: usize = result.spans.iter()
            .filter(|s| s.source.is_none())
            .count();
        eprintln!("\n=== def principal -> pribond ===");
        eprintln!("is_meaningful: {}", result.is_meaningful);
        eprintln!("num unchanged segments: {}", num_unchanged);
        eprintln!("Raw spans ({}):", result.spans.len());
        for (i, span) in result.spans.iter().enumerate() {
            eprintln!("  raw[{}]: {:?} is_del={} text={:?}",
                i, span.source, span.is_deletion, span.text);
        }

        assert!(
            !result.is_meaningful,
            "Gibberish transformation should NOT be meaningful - num_unchanged_segments={}",
            num_unchanged
        );
    }

    #[test]
    fn test_for_loop_to_comment_should_not_be_meaningful() {
        let old = "    for i in (last_changed + 1..spans.len()).rev() {";
        let new = "    // Always preserve the final unchanged span if it exists - it's the common suffix";

        let result = compute_inline_diff_merged(old, new, LineSource::Committed);

        let num_unchanged: usize = result.spans.iter()
            .filter(|s| s.source.is_none())
            .count();
        eprintln!("\n=== for loop -> comment ===");
        eprintln!("is_meaningful: {}", result.is_meaningful);
        eprintln!("num unchanged segments: {}", num_unchanged);
        eprintln!("Raw spans ({}):", result.spans.len());
        for (i, span) in result.spans.iter().enumerate() {
            eprintln!("  raw[{}]: {:?} is_del={} text={:?}",
                i, span.source, span.is_deletion, span.text);
        }

        assert!(
            !result.is_meaningful,
            "A for loop changing to a comment should NOT be meaningful - num_unchanged_segments={}",
            num_unchanged
        );
    }

    #[test]
    fn test_coalesce_describe_inactive_to_authorization() {
        let old = r#"describe "inactive account" do"#;
        let new = r#"describe "authorization" do"#;

        let result = compute_inline_diff_merged(old, new, LineSource::Committed);

        eprintln!("\n=== describe inactive -> authorization ===");
        eprintln!("is_meaningful: {}", result.is_meaningful);
        eprintln!("Raw spans ({}):", result.spans.len());
        for (i, span) in result.spans.iter().enumerate() {
            eprintln!("  raw[{}]: {:?} is_del={} text={:?}",
                i, span.source, span.is_deletion, span.text);
        }

        let coalesced = coalesce_spans(&result.spans);

        eprintln!("Coalesced spans ({}):", coalesced.len());
        for (i, span) in coalesced.iter().enumerate() {
            eprintln!("  span[{}]: {:?} is_del={} text={:?}",
                i, span.source, span.is_deletion, span.text);
        }

        let deletion = coalesced.iter().find(|s| s.is_deletion);
        assert!(deletion.is_some(), "Should have a deletion span");
        let deletion = deletion.unwrap();

        assert!(
            deletion.text.contains("inactive account"),
            "Deletion should contain 'inactive account', got: {:?}", deletion.text
        );

        let insertion = coalesced.iter().find(|s| !s.is_deletion && s.source.is_some());
        assert!(insertion.is_some(), "Should have an insertion span");
        let insertion = insertion.unwrap();

        assert!(
            insertion.text.contains("authorization"),
            "Insertion should contain 'authorization', got: {:?}", insertion.text
        );

        let prefix = coalesced.first().filter(|s| s.source.is_none());
        assert!(prefix.is_some(), "Should have unchanged prefix");
        assert!(
            prefix.unwrap().text.contains("describe"),
            "Prefix should contain 'describe', got: {:?}", prefix.unwrap().text
        );

        let suffix = coalesced.last().filter(|s| s.source.is_none());
        assert!(suffix.is_some(), "Should have unchanged suffix");
        assert!(
            suffix.unwrap().text.contains(" do"),
            "Suffix should contain ' do', got: {:?}", suffix.unwrap().text
        );
    }

    #[test]
    fn test_coalesce_let_variable_rename() {
        let old = "let(:letters_of_bondability_requests_policy)";
        let new = "let(:principal)";

        let result = compute_inline_diff_merged(old, new, LineSource::Committed);

        eprintln!("\n=== let variable rename ===");
        eprintln!("is_meaningful: {}", result.is_meaningful);
        eprintln!("Raw spans ({}):", result.spans.len());
        for (i, span) in result.spans.iter().enumerate() {
            eprintln!("  raw[{}]: {:?} is_del={} text={:?}",
                i, span.source, span.is_deletion, span.text);
        }

        let coalesced = coalesce_spans(&result.spans);

        eprintln!("Coalesced spans ({}):", coalesced.len());
        for (i, span) in coalesced.iter().enumerate() {
            eprintln!("  span[{}]: {:?} is_del={} text={:?}",
                i, span.source, span.is_deletion, span.text);
        }

        let deletion = coalesced.iter().find(|s| s.is_deletion);
        assert!(deletion.is_some(), "Should have a deletion span");
        let deletion = deletion.unwrap();

        assert!(
            deletion.text.contains("letters_of_bondability_requests_policy"),
            "Deletion should contain the full variable name, got: {:?}", deletion.text
        );

        let insertion = coalesced.iter().find(|s| !s.is_deletion && s.source.is_some());
        assert!(insertion.is_some(), "Should have an insertion span");
        let insertion = insertion.unwrap();

        assert!(
            insertion.text.contains("principal"),
            "Insertion should contain 'principal', got: {:?}", insertion.text
        );

        let prefix = coalesced.first().filter(|s| s.source.is_none());
        assert!(prefix.is_some(), "Should have unchanged prefix");
        assert!(
            prefix.unwrap().text.starts_with("let(:"),
            "Prefix should start with 'let(:', got: {:?}", prefix.unwrap().text
        );

        let suffix = coalesced.last().filter(|s| s.source.is_none());
        assert!(suffix.is_some(), "Should have unchanged suffix");
        assert!(
            suffix.unwrap().text == ")",
            "Suffix should be ')', got: {:?}", suffix.unwrap().text
        );
    }

    #[test]
    fn test_inline_display_width_simple() {
        let spans = vec![
            make_span("hello ", None, false),
            make_span("world", Some(LineSource::DeletedBase), true),
            make_span("earth", Some(LineSource::Committed), false),
        ];
        // "hello " + "world" + "earth" = 6 + 5 + 5 = 16
        assert_eq!(inline_display_width(&spans), 16);
    }

    #[test]
    fn test_inline_display_width_with_coalesce() {
        // When spans are coalesced, the width should be the coalesced width
        let spans = vec![
            make_span("c", None, false),
            make_span("ancellation", Some(LineSource::DeletedBase), true),
            make_span("l", None, false),
            make_span("ause", Some(LineSource::Committed), false),
        ];
        // After coalesce: "cancellationl" + "clause" = 13 + 6 = 19
        let width = inline_display_width(&spans);
        assert_eq!(width, 19);
    }

    #[test]
    fn test_get_deletion_source_finds_correct_source() {
        let spans = vec![
            make_span("unchanged", None, false),
            make_span("deleted", Some(LineSource::DeletedCommitted), true),
            make_span("inserted", Some(LineSource::Staged), false),
        ];
        assert_eq!(get_deletion_source(&spans), LineSource::DeletedCommitted);
    }

    #[test]
    fn test_get_deletion_source_defaults_to_deleted_base() {
        let spans = vec![
            make_span("unchanged", None, false),
            make_span("inserted", Some(LineSource::Committed), false),
        ];
        assert_eq!(get_deletion_source(&spans), LineSource::DeletedBase);
    }

    #[test]
    fn test_get_insertion_source_finds_correct_source() {
        let spans = vec![
            make_span("unchanged", None, false),
            make_span("deleted", Some(LineSource::DeletedBase), true),
            make_span("inserted", Some(LineSource::Staged), false),
        ];
        assert_eq!(get_insertion_source(&spans), LineSource::Staged);
    }

    #[test]
    fn test_get_insertion_source_defaults_to_committed() {
        let spans = vec![
            make_span("unchanged", None, false),
            make_span("deleted", Some(LineSource::DeletedBase), true),
        ];
        assert_eq!(get_insertion_source(&spans), LineSource::Committed);
    }

    #[test]
    fn test_build_deletion_spans_includes_deletions_and_unchanged() {
        let spans = vec![
            make_span("unchanged ", None, false),
            make_span("deleted", Some(LineSource::DeletedBase), true),
            make_span("inserted", Some(LineSource::Committed), false),
        ];

        let result = build_deletion_spans_with_highlight(&spans, LineSource::DeletedBase, "unchanged deleted", None);

        // Should include unchanged and deletion, but NOT insertion
        assert_eq!(result.len(), 2, "Should have 2 spans (unchanged + deletion)");
        assert_eq!(result[0].content, "unchanged ");
        assert_eq!(result[1].content, "deleted");
    }

    #[test]
    fn test_build_deletion_spans_applies_highlight_to_deletions() {
        let spans = vec![
            make_span("unchanged ", None, false),
            make_span("deleted", Some(LineSource::DeletedBase), true),
        ];

        let result = build_deletion_spans_with_highlight(&spans, LineSource::DeletedBase, "unchanged deleted", None);

        // Unchanged should have base style (no background highlight)
        let base_style = line_style(LineSource::DeletedBase);
        assert_eq!(result[0].style, base_style, "Unchanged span should have base style");

        // Deleted should have highlighted style (with background)
        let highlight_style = line_style_with_highlight(LineSource::DeletedBase);
        assert_eq!(result[1].style, highlight_style, "Deleted span should have highlight style");
    }

    #[test]
    fn test_build_insertion_spans_includes_insertions_and_unchanged() {
        let spans = vec![
            make_span("unchanged ", None, false),
            make_span("deleted", Some(LineSource::DeletedBase), true),
            make_span("inserted", Some(LineSource::Committed), false),
        ];

        let result = build_insertion_spans_with_highlight(&spans, LineSource::Committed, "unchanged inserted", None);

        // Should include unchanged and insertion, but NOT deletion
        assert_eq!(result.len(), 2, "Should have 2 spans (unchanged + insertion)");
        assert_eq!(result[0].content, "unchanged ");
        assert_eq!(result[1].content, "inserted");
    }

    #[test]
    fn test_build_insertion_spans_applies_highlight_to_insertions() {
        let spans = vec![
            make_span("unchanged ", None, false),
            make_span("inserted", Some(LineSource::Committed), false),
        ];

        let result = build_insertion_spans_with_highlight(&spans, LineSource::Committed, "unchanged inserted", None);

        // Unchanged should have base style (no background highlight)
        let base_style = line_style(LineSource::Committed);
        assert_eq!(result[0].style, base_style, "Unchanged span should have base style");

        // Inserted should have highlighted style (with background)
        let highlight_style = line_style_with_highlight(LineSource::Committed);
        assert_eq!(result[1].style, highlight_style, "Inserted span should have highlight style");
    }

    #[test]
    fn test_build_spans_with_highlight_empty_input() {
        let spans: Vec<InlineSpan> = vec![];

        let del_result = build_deletion_spans_with_highlight(&spans, LineSource::DeletedBase, "", None);
        let ins_result = build_insertion_spans_with_highlight(&spans, LineSource::Committed, "", None);

        assert!(del_result.is_empty(), "Empty input should produce empty deletion spans");
        assert!(ins_result.is_empty(), "Empty input should produce empty insertion spans");
    }

    #[test]
    fn test_build_spans_preserves_text_content() {
        // Verify the actual text content is preserved when building highlighted spans
        let spans = vec![
            make_span("hello ", None, false),
            make_span("world", Some(LineSource::DeletedBase), true),
            make_span("earth", Some(LineSource::Committed), false),
        ];

        let del_spans = build_deletion_spans_with_highlight(&spans, LineSource::DeletedBase, "hello world", None);
        let ins_spans = build_insertion_spans_with_highlight(&spans, LineSource::Committed, "hello earth", None);

        // Deletion line should be "hello world"
        let del_text: String = del_spans.iter().map(|s| s.content.as_ref()).collect();
        assert_eq!(del_text, "hello world");

        // Insertion line should be "hello earth"
        let ins_text: String = ins_spans.iter().map(|s| s.content.as_ref()).collect();
        assert_eq!(ins_text, "hello earth");
    }

    #[test]
    fn test_build_spans_with_multiple_changes() {
        // Multiple deletions and insertions interspersed with short gaps
        // Since gaps are < 5 chars and not structural, they get coalesced
        let spans = vec![
            make_span("a", None, false),
            make_span("old1", Some(LineSource::DeletedBase), true),
            make_span("new1", Some(LineSource::Committed), false),
            make_span("b", None, false),
            make_span("old2", Some(LineSource::DeletedBase), true),
            make_span("new2", Some(LineSource::Committed), false),
            make_span("c", None, false),
        ];

        let del_spans = build_deletion_spans_with_highlight(&spans, LineSource::DeletedBase, "aold1bold2c", None);
        let ins_spans = build_insertion_spans_with_highlight(&spans, LineSource::Committed, "anew1bnew2c", None);

        // After coalescing, short gaps get absorbed
        // Deletion text should be: aold1bold2c (coalesced into fewer spans)
        let del_text: String = del_spans.iter().map(|s| s.content.as_ref()).collect();
        assert_eq!(del_text, "aold1bold2c");

        // Insertion text should be: anew1bnew2c
        let ins_text: String = ins_spans.iter().map(|s| s.content.as_ref()).collect();
        assert_eq!(ins_text, "anew1bnew2c");
    }

    #[test]
    fn test_classify_inline_change_pure_deletion() {
        // Only deletions (is_deletion: true) and unchanged (source: None)
        let spans = vec![
            make_span("unchanged ", None, false),
            make_span("deleted text", Some(LineSource::DeletedBase), true),
            make_span(" more unchanged", None, false),
        ];
        assert_eq!(classify_inline_change(&spans), InlineChangeType::PureDeletion);
    }

    #[test]
    fn test_classify_inline_change_pure_addition() {
        // Only insertions (source: Some(_), is_deletion: false) and unchanged
        let spans = vec![
            make_span("unchanged ", None, false),
            make_span("inserted text", Some(LineSource::Committed), false),
            make_span(" more unchanged", None, false),
        ];
        assert_eq!(classify_inline_change(&spans), InlineChangeType::PureAddition);
    }

    #[test]
    fn test_classify_inline_change_mixed() {
        // Both deletions and insertions present
        let spans = vec![
            make_span("unchanged ", None, false),
            make_span("deleted", Some(LineSource::DeletedBase), true),
            make_span("inserted", Some(LineSource::Committed), false),
        ];
        assert_eq!(classify_inline_change(&spans), InlineChangeType::Mixed);
    }

    #[test]
    fn test_classify_inline_change_no_change() {
        // Only unchanged content (source: None, is_deletion: false)
        let spans = vec![
            make_span("all ", None, false),
            make_span("unchanged ", None, false),
            make_span("content", None, false),
        ];
        assert_eq!(classify_inline_change(&spans), InlineChangeType::NoChange);
    }

    #[test]
    fn test_classify_real_world_pure_deletion() {
        // Real-world case: "foo bar baz" -> "foo"
        // This removes " bar baz" - pure deletion
        let result = compute_inline_diff_merged("foo bar baz", "foo", LineSource::Committed);
        assert_eq!(classify_inline_change(&result.spans), InlineChangeType::PureDeletion);
    }

    #[test]
    fn test_classify_real_world_pure_addition() {
        // Real-world case: "foo" -> "foo bar baz"
        // This adds " bar baz" - pure addition
        let result = compute_inline_diff_merged("foo", "foo bar baz", LineSource::Committed);
        assert_eq!(classify_inline_change(&result.spans), InlineChangeType::PureAddition);
    }

    #[test]
    fn test_classify_real_world_mixed() {
        // Real-world case: "hello world" -> "goodbye world"
        // This replaces "hello" with "goodbye" - mixed change
        let result = compute_inline_diff_merged("hello world", "goodbye world", LineSource::Committed);
        assert_eq!(classify_inline_change(&result.spans), InlineChangeType::Mixed);
    }

    #[test]
    fn test_build_insertion_spans_unstaged_uses_dark_foreground() {
        // Test that unstaged insertion spans have dark foreground for contrast
        let spans = vec![
            make_span("unchanged ", None, false),
            make_span("inserted", Some(LineSource::Unstaged), false),
        ];

        let result = build_insertion_spans_with_highlight(&spans, LineSource::Unstaged, "unchanged inserted", None);

        // The inserted span should have dark foreground
        let inserted_span = &result[1];
        assert_eq!(
            inserted_span.style.fg,
            Some(Color::Rgb(30, 30, 30)),
            "Unstaged inserted span should have dark foreground, got {:?}",
            inserted_span.style.fg
        );
    }

    #[test]
    fn test_build_insertion_spans_unstaged_with_syntax_highlighting() {
        // Test with a Rust file path to trigger syntax highlighting
        // This simulates a comment line like "// some comment"
        let spans = vec![
            make_span("// unchanged ", None, false),
            make_span("inserted", Some(LineSource::Unstaged), false),
        ];

        let result = build_insertion_spans_with_highlight(
            &spans,
            LineSource::Unstaged,
            "// unchanged inserted",
            Some("test.rs"),  // Rust file triggers syntax highlighting
        );

        // Find the span that contains "inserted" - it should have dark foreground
        let inserted_span = result.iter().find(|s| s.content.contains("inserted"));
        assert!(inserted_span.is_some(), "Should have a span containing 'inserted'");
        let inserted_span = inserted_span.unwrap();

        assert_eq!(
            inserted_span.style.fg,
            Some(Color::Rgb(30, 30, 30)),
            "Unstaged inserted span with syntax highlighting should have dark foreground, got {:?}",
            inserted_span.style.fg
        );
    }

    #[test]
    fn test_build_deletion_spans_applies_contrast_check() {
        // Test that deletion spans apply contrast checking
        // DeletedStaged has a reddish background Rgb(115, 55, 45) that works
        // better with light foreground, so light should be preserved/chosen
        let spans = vec![
            make_span("unchanged ", None, false),
            make_span("deleted", Some(LineSource::DeletedStaged), true),
        ];

        let result = build_deletion_spans_with_highlight(&spans, LineSource::DeletedStaged, "unchanged deleted", None);

        // The deleted span should have light foreground (reddish bg works with light text)
        let deleted_span = &result[1];
        assert_eq!(
            deleted_span.style.fg,
            Some(Color::Rgb(220, 220, 220)),
            "DeletedStaged span should have light foreground for contrast with reddish bg, got {:?}",
            deleted_span.style.fg
        );

        // Also verify the highlight background is applied
        assert_eq!(
            deleted_span.style.bg,
            Some(Color::Rgb(115, 55, 45)),
            "DeletedStaged span should have the correct highlight background"
        );
    }

    #[test]
    fn test_pure_deletion_builds_correct_deletion_and_insertion_spans() {
        let old_content = "hello world";
        let new_content = "hello wrld";

        let result = compute_inline_diff_merged(old_content, new_content, LineSource::Committed);

        assert_eq!(classify_inline_change(&result.spans), InlineChangeType::PureDeletion);

        let del_spans = build_deletion_spans_with_highlight(
            &result.spans,
            LineSource::DeletedBase,
            old_content,
            None,
        );
        let del_text: String = del_spans.iter().map(|s| s.content.as_ref()).collect();
        assert_eq!(del_text, old_content, "deletion line should show old content");

        let ins_spans = build_insertion_spans_with_highlight(
            &result.spans,
            LineSource::Committed,
            new_content,
            None,
        );
        let ins_text: String = ins_spans.iter().map(|s| s.content.as_ref()).collect();
        assert_eq!(ins_text, new_content, "insertion line should show new content");
    }

    #[test]
    fn test_pure_deletion_highlights_removed_character() {
        let old_content = ".*}o)";
        let new_content = ".*})";

        let result = compute_inline_diff_merged(old_content, new_content, LineSource::Committed);
        assert_eq!(classify_inline_change(&result.spans), InlineChangeType::PureDeletion);

        let del_spans = build_deletion_spans_with_highlight(
            &result.spans,
            LineSource::DeletedBase,
            old_content,
            None,
        );
        let del_text: String = del_spans.iter().map(|s| s.content.as_ref()).collect();
        assert_eq!(del_text, ".*}o)");

        let highlight_style = line_style_with_highlight(LineSource::DeletedBase);
        let highlighted: String = del_spans
            .iter()
            .filter(|s| s.style.bg == highlight_style.bg)
            .map(|s| s.content.as_ref())
            .collect();
        assert_eq!(highlighted, "o", "only the removed 'o' should be highlighted");
    }

    #[test]
    fn test_get_insertion_source_ignores_deletions() {
        let spans = vec![
            InlineSpan { text: "deleted".to_string(), source: Some(LineSource::DeletedBase), is_deletion: true },
            InlineSpan { text: "inserted".to_string(), source: Some(LineSource::Unstaged), is_deletion: false },
        ];

        let source = get_insertion_source(&spans);
        assert_eq!(source, LineSource::Unstaged);
    }
}