rigg-diff 0.17.0

Semantic JSON diffing with identity-key-based array matching
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
//! Line-level text diffing for long text properties.
//!
//! Produces structured diffs with context lines for human-readable
//! comparison of instructions, descriptions, and other long text fields.
//! Supports word-level highlighting within modified paragraphs.

use similar::{ChangeTag, TextDiff};

/// Minimum string length to trigger line-level diffing.
pub const LONG_TEXT_THRESHOLD: usize = 120;

/// Per-line threshold: lines longer than this are split at sentence boundaries.
const SENTENCE_SPLIT_THRESHOLD: usize = 120;

/// Target width when word-wrapping lines with no sentence boundaries.
const WORD_WRAP_WIDTH: usize = 80;

/// Maximum display length for context lines before truncation.
pub const CONTEXT_TRUNCATE_LEN: usize = 80;

/// A segment of a word-level diff within a single line.
#[derive(Debug, Clone, PartialEq)]
pub struct WordSegment {
    pub text: String,
    pub kind: WordSegmentKind,
}

/// Whether a word segment is unchanged or changed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum WordSegmentKind {
    Equal,
    Changed,
}

/// A single line in a text diff.
#[derive(Debug, Clone, PartialEq)]
pub enum DiffLine {
    /// Unchanged context line.
    Equal(String),
    /// Deleted line (only in old text).
    Delete(String),
    /// Inserted line (only in new text).
    Insert(String),
    /// A modified line with word-level diff segments.
    /// Contains old line segments and new line segments.
    Modified {
        old_segments: Vec<WordSegment>,
        new_segments: Vec<WordSegment>,
    },
}

/// A group of contiguous diff lines with surrounding context.
#[derive(Debug, Clone)]
pub struct TextDiffHunk {
    pub lines: Vec<DiffLine>,
}

/// Result of a line-level text diff.
#[derive(Debug, Clone)]
pub struct TextDiffResult {
    pub hunks: Vec<TextDiffHunk>,
    pub deletions: usize,
    pub insertions: usize,
}

/// Returns `true` if the string is long enough or multi-line to warrant
/// line-level diffing instead of inline comparison.
pub fn is_long_text(s: &str) -> bool {
    s.len() >= LONG_TEXT_THRESHOLD || s.contains('\n')
}

/// Compute a line-level diff between two strings.
///
/// Returns hunks with 1 line of surrounding context per change group.
/// Consecutive Delete+Insert pairs are converted to Modified entries
/// with word-level diff segments.
pub fn diff_text(old: &str, new: &str) -> TextDiffResult {
    let diff = TextDiff::from_lines(old, new);
    let mut deletions = 0;
    let mut insertions = 0;
    let mut hunks = Vec::new();

    for group in diff.grouped_ops(1) {
        let mut lines = Vec::new();
        for op in &group {
            for change in diff.iter_changes(op) {
                let text = change.value().to_string();
                match change.tag() {
                    ChangeTag::Equal => lines.push(DiffLine::Equal(text)),
                    ChangeTag::Delete => {
                        deletions += 1;
                        lines.push(DiffLine::Delete(text));
                    }
                    ChangeTag::Insert => {
                        insertions += 1;
                        lines.push(DiffLine::Insert(text));
                    }
                }
            }
        }
        if !lines.is_empty() {
            lines = pair_modifications(lines);
            hunks.push(TextDiffHunk { lines });
        }
    }

    TextDiffResult {
        hunks,
        deletions,
        insertions,
    }
}

/// Convert consecutive Delete+Insert runs into Modified entries with word-level diffs.
///
/// Pairs them 1:1 — if there are 3 deletes and 2 inserts, the first 2 become
/// Modified pairs and the 3rd remains a standalone Delete.
fn pair_modifications(lines: Vec<DiffLine>) -> Vec<DiffLine> {
    let mut result = Vec::with_capacity(lines.len());
    let mut i = 0;

    while i < lines.len() {
        // Look for a run of Delete lines followed by Insert lines
        if matches!(&lines[i], DiffLine::Delete(_)) {
            let delete_start = i;
            while i < lines.len() && matches!(&lines[i], DiffLine::Delete(_)) {
                i += 1;
            }
            let delete_end = i;

            let insert_start = i;
            while i < lines.len() && matches!(&lines[i], DiffLine::Insert(_)) {
                i += 1;
            }
            let insert_end = i;

            let delete_count = delete_end - delete_start;
            let insert_count = insert_end - insert_start;
            let pair_count = delete_count.min(insert_count);

            // Pair deletes with inserts as Modified entries
            for j in 0..pair_count {
                let old_text = match &lines[delete_start + j] {
                    DiffLine::Delete(s) => s.clone(),
                    _ => unreachable!(),
                };
                let new_text = match &lines[insert_start + j] {
                    DiffLine::Insert(s) => s.clone(),
                    _ => unreachable!(),
                };

                let (old_segments, new_segments) = diff_words(&old_text, &new_text);
                result.push(DiffLine::Modified {
                    old_segments,
                    new_segments,
                });
            }

            // Emit remaining unpaired deletes
            for j in pair_count..delete_count {
                result.push(lines[delete_start + j].clone());
            }

            // Emit remaining unpaired inserts
            for j in pair_count..insert_count {
                result.push(lines[insert_start + j].clone());
            }
        } else {
            result.push(lines[i].clone());
            i += 1;
        }
    }

    result
}

/// Compute word-level diff segments between two strings.
///
/// Returns (old_segments, new_segments) where each segment indicates
/// whether it's equal or changed relative to the other side.
pub fn diff_words(old: &str, new: &str) -> (Vec<WordSegment>, Vec<WordSegment>) {
    let diff = TextDiff::from_words(old, new);
    let mut old_segments = Vec::new();
    let mut new_segments = Vec::new();

    for change in diff.iter_all_changes() {
        let text = change.value().to_string();
        match change.tag() {
            ChangeTag::Equal => {
                old_segments.push(WordSegment {
                    text: text.clone(),
                    kind: WordSegmentKind::Equal,
                });
                new_segments.push(WordSegment {
                    text,
                    kind: WordSegmentKind::Equal,
                });
            }
            ChangeTag::Delete => {
                old_segments.push(WordSegment {
                    text,
                    kind: WordSegmentKind::Changed,
                });
            }
            ChangeTag::Insert => {
                new_segments.push(WordSegment {
                    text,
                    kind: WordSegmentKind::Changed,
                });
            }
        }
    }

    (old_segments, new_segments)
}

/// Truncate a string to approximately `max_len` characters with `" ... "` in the middle.
///
/// If the string is shorter than or equal to `max_len`, returns it unchanged.
/// Uses `char_indices()` for Unicode safety.
pub fn truncate_context(s: &str, max_len: usize) -> String {
    if s.len() <= max_len {
        return s.to_string();
    }

    let ellipsis = " ... ";
    let ellipsis_len = ellipsis.len();

    if max_len <= ellipsis_len + 4 {
        // Too short for meaningful truncation
        return s.chars().take(max_len).collect();
    }

    let half = (max_len - ellipsis_len) / 2;

    // Find the byte index for the prefix (first `half` chars)
    let prefix_end = s
        .char_indices()
        .nth(half)
        .map(|(i, _)| i)
        .unwrap_or(s.len());

    // Find the byte index for the suffix (last `half` chars)
    let total_chars = s.chars().count();
    let suffix_start_char = total_chars.saturating_sub(half);
    let suffix_start = s
        .char_indices()
        .nth(suffix_start_char)
        .map(|(i, _)| i)
        .unwrap_or(s.len());

    format!("{}{}{}", &s[..prefix_end], ellipsis, &s[suffix_start..])
}

/// Normalize text for more readable line-level diffs.
///
/// Long lines (single paragraphs without newlines) are split at sentence
/// boundaries so that the diff can show which sentence changed, rather than
/// showing the entire paragraph as one changed block.
///
/// Lines shorter than the threshold are left unchanged. When no sentence
/// boundaries are found in a long line, it falls back to word-wrapping.
pub fn normalize_for_diff(text: &str) -> String {
    let mut result = String::new();
    let parts: Vec<&str> = text.split('\n').collect();
    let line_count = parts.len();
    for (idx, line) in parts.into_iter().enumerate() {
        // Preserve trailing newline semantics: if the original text ended with
        // \n, the split produces a final empty element that we should skip.
        if line.is_empty() && idx == line_count - 1 {
            break;
        }
        if line.len() <= SENTENCE_SPLIT_THRESHOLD {
            result.push_str(line);
            result.push('\n');
        } else {
            let sentences = split_at_sentence_boundaries(line);
            if sentences.len() <= 1 {
                // No sentence boundaries found — word-wrap instead
                let wrapped = word_wrap(line.trim(), WORD_WRAP_WIDTH);
                for wrap_line in &wrapped {
                    result.push_str(wrap_line);
                    result.push('\n');
                }
            } else {
                for sentence in &sentences {
                    let trimmed = sentence.trim();
                    if trimmed.is_empty() {
                        continue;
                    }
                    if trimmed.len() > SENTENCE_SPLIT_THRESHOLD {
                        let wrapped = word_wrap(trimmed, WORD_WRAP_WIDTH);
                        for wrap_line in &wrapped {
                            result.push_str(wrap_line);
                            result.push('\n');
                        }
                    } else {
                        result.push_str(trimmed);
                        result.push('\n');
                    }
                }
            }
        }
    }
    result
}

/// Split text at sentence boundaries (`. `, `? `, `! ` followed by an
/// uppercase letter). The punctuation stays with the preceding sentence.
fn split_at_sentence_boundaries(text: &str) -> Vec<&str> {
    let bytes = text.as_bytes();
    let mut result = Vec::new();
    let mut start = 0;

    let mut i = 0;
    while i + 2 < bytes.len() {
        if (bytes[i] == b'.' || bytes[i] == b'?' || bytes[i] == b'!')
            && bytes[i + 1] == b' '
            && bytes[i + 2].is_ascii_uppercase()
        {
            // Include the punctuation on this segment, skip the space
            result.push(&text[start..=i]);
            start = i + 2;
            i = start;
        } else {
            i += 1;
        }
    }
    if start < text.len() {
        result.push(&text[start..]);
    }
    result
}

/// Word-wrap text at the given width, breaking at space boundaries.
fn word_wrap(text: &str, width: usize) -> Vec<String> {
    let mut lines = Vec::new();
    let mut current = String::new();

    for word in text.split(' ') {
        if word.is_empty() {
            continue;
        }
        if current.is_empty() {
            current.push_str(word);
        } else if current.len() + 1 + word.len() > width {
            lines.push(current);
            current = word.to_string();
        } else {
            current.push(' ');
            current.push_str(word);
        }
    }
    if !current.is_empty() {
        lines.push(current);
    }
    lines
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_is_long_text_short() {
        assert!(!is_long_text("hello"));
    }

    #[test]
    fn test_is_long_text_by_length() {
        let s = "a".repeat(LONG_TEXT_THRESHOLD);
        assert!(is_long_text(&s));
    }

    #[test]
    fn test_is_long_text_multiline() {
        assert!(is_long_text("line one\nline two"));
    }

    #[test]
    fn test_diff_identical() {
        let result = diff_text("hello\n", "hello\n");
        assert_eq!(result.deletions, 0);
        assert_eq!(result.insertions, 0);
        assert!(result.hunks.is_empty());
    }

    #[test]
    fn test_diff_single_line_change() {
        let old = "line one\nline two\nline three\n";
        let new = "line one\nline TWO\nline three\n";
        let result = diff_text(old, new);
        assert_eq!(result.deletions, 1);
        assert_eq!(result.insertions, 1);
        assert_eq!(result.hunks.len(), 1);

        // Should produce a Modified entry (paired delete+insert)
        let lines = &result.hunks[0].lines;
        assert!(lines.iter().any(|l| matches!(l, DiffLine::Modified { .. })));
    }

    #[test]
    fn test_diff_addition() {
        let old = "line one\nline three\n";
        let new = "line one\nline two\nline three\n";
        let result = diff_text(old, new);
        assert_eq!(result.deletions, 0);
        assert_eq!(result.insertions, 1);
    }

    #[test]
    fn test_diff_deletion() {
        let old = "line one\nline two\nline three\n";
        let new = "line one\nline three\n";
        let result = diff_text(old, new);
        assert_eq!(result.deletions, 1);
        assert_eq!(result.insertions, 0);
    }

    #[test]
    fn test_diff_multiple_hunks() {
        let old = "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\n";
        let new = "a\nB\nc\nd\ne\nf\ng\nH\ni\nj\n";
        let result = diff_text(old, new);
        assert_eq!(result.deletions, 2);
        assert_eq!(result.insertions, 2);
        // With 1 context line, changes at positions 2 and 8 (0-indexed)
        // should form separate hunks since they're >2 lines apart
        assert_eq!(result.hunks.len(), 2);
    }

    #[test]
    fn test_diff_context_lines() {
        let old = "a\nb\nc\nd\ne\n";
        let new = "a\nb\nC\nd\ne\n";
        let result = diff_text(old, new);
        assert_eq!(result.hunks.len(), 1);

        let lines = &result.hunks[0].lines;
        // With 1 context line, we should have: b (context), Modified(c->C), d (context)
        assert!(
            lines
                .iter()
                .any(|l| matches!(l, DiffLine::Equal(s) if s.starts_with('b')))
        );
        assert!(lines.iter().any(|l| matches!(l, DiffLine::Modified { .. })));
        assert!(
            lines
                .iter()
                .any(|l| matches!(l, DiffLine::Equal(s) if s.starts_with('d')))
        );
    }

    // === Word-level diff tests ===

    #[test]
    fn test_diff_words_single_word_change() {
        let (old_segs, new_segs) = diff_words("hello world", "hello earth");
        // "hello " is equal, "world"/"earth" is changed
        assert!(
            old_segs
                .iter()
                .any(|s| s.kind == WordSegmentKind::Changed && s.text == "world")
        );
        assert!(
            new_segs
                .iter()
                .any(|s| s.kind == WordSegmentKind::Changed && s.text == "earth")
        );
        assert!(
            old_segs
                .iter()
                .any(|s| s.kind == WordSegmentKind::Equal && s.text.contains("hello"))
        );
    }

    #[test]
    fn test_diff_words_no_change() {
        let (old_segs, new_segs) = diff_words("same text", "same text");
        assert!(old_segs.iter().all(|s| s.kind == WordSegmentKind::Equal));
        assert!(new_segs.iter().all(|s| s.kind == WordSegmentKind::Equal));
    }

    #[test]
    fn test_diff_words_completely_different() {
        let (old_segs, new_segs) = diff_words("foo bar", "baz qux");
        assert!(old_segs.iter().any(|s| s.kind == WordSegmentKind::Changed));
        assert!(new_segs.iter().any(|s| s.kind == WordSegmentKind::Changed));
    }

    // === pair_modifications tests ===

    #[test]
    fn test_pair_modifications_basic() {
        let lines = vec![
            DiffLine::Equal("context\n".to_string()),
            DiffLine::Delete("old line\n".to_string()),
            DiffLine::Insert("new line\n".to_string()),
            DiffLine::Equal("context\n".to_string()),
        ];
        let result = pair_modifications(lines);
        assert_eq!(result.len(), 3); // Equal, Modified, Equal
        assert!(matches!(&result[1], DiffLine::Modified { .. }));
    }

    #[test]
    fn test_pair_modifications_uneven_more_deletes() {
        let lines = vec![
            DiffLine::Delete("old 1\n".to_string()),
            DiffLine::Delete("old 2\n".to_string()),
            DiffLine::Delete("old 3\n".to_string()),
            DiffLine::Insert("new 1\n".to_string()),
            DiffLine::Insert("new 2\n".to_string()),
        ];
        let result = pair_modifications(lines);
        // 2 Modified + 1 leftover Delete
        assert_eq!(result.len(), 3);
        assert!(matches!(&result[0], DiffLine::Modified { .. }));
        assert!(matches!(&result[1], DiffLine::Modified { .. }));
        assert!(matches!(&result[2], DiffLine::Delete(_)));
    }

    #[test]
    fn test_pair_modifications_uneven_more_inserts() {
        let lines = vec![
            DiffLine::Delete("old 1\n".to_string()),
            DiffLine::Insert("new 1\n".to_string()),
            DiffLine::Insert("new 2\n".to_string()),
        ];
        let result = pair_modifications(lines);
        // 1 Modified + 1 leftover Insert
        assert_eq!(result.len(), 2);
        assert!(matches!(&result[0], DiffLine::Modified { .. }));
        assert!(matches!(&result[1], DiffLine::Insert(_)));
    }

    #[test]
    fn test_pair_modifications_no_pairs() {
        let lines = vec![
            DiffLine::Equal("context\n".to_string()),
            DiffLine::Insert("new\n".to_string()),
            DiffLine::Equal("context\n".to_string()),
        ];
        let result = pair_modifications(lines);
        assert_eq!(result.len(), 3);
        assert!(matches!(&result[1], DiffLine::Insert(_)));
    }

    // === truncate_context tests ===

    #[test]
    fn test_truncate_context_short_string() {
        let s = "short string";
        assert_eq!(truncate_context(s, 80), s);
    }

    #[test]
    fn test_truncate_context_exact_length() {
        let s = "a".repeat(80);
        assert_eq!(truncate_context(&s, 80), s);
    }

    #[test]
    fn test_truncate_context_long_string() {
        let s = "a".repeat(200);
        let truncated = truncate_context(&s, 80);
        assert!(truncated.len() <= 85); // allow slight overshoot from char boundaries
        assert!(truncated.contains(" ... "));
    }

    #[test]
    fn test_truncate_context_preserves_ends() {
        let s = "START middle padding that is quite long and should be truncated away END";
        let truncated = truncate_context(&s, 40);
        assert!(truncated.starts_with("START"));
        assert!(truncated.ends_with("END"));
        assert!(truncated.contains(" ... "));
    }

    #[test]
    fn test_truncate_context_unicode() {
        let s = "aaa\u{00e9}\u{00e9}\u{00e9}".repeat(20);
        let truncated = truncate_context(&s, 40);
        // Should not panic on unicode boundaries
        assert!(truncated.len() < s.len());
    }

    // === Modified variant in diff output ===

    #[test]
    fn test_diff_produces_modified_for_paired_changes() {
        let old = "line one\nold paragraph content here\nline three\n";
        let new = "line one\nnew paragraph content here\nline three\n";
        let result = diff_text(old, new);

        let has_modified = result.hunks.iter().any(|h| {
            h.lines
                .iter()
                .any(|l| matches!(l, DiffLine::Modified { .. }))
        });
        assert!(has_modified, "Paired delete+insert should become Modified");
    }

    #[test]
    fn test_modified_has_word_segments() {
        let old = "The IRMA framework requires validation.\n";
        let new = "The revised IRMA framework v2 requires validation.\n";
        let result = diff_text(old, new);

        for hunk in &result.hunks {
            for line in &hunk.lines {
                if let DiffLine::Modified {
                    old_segments,
                    new_segments,
                } = line
                {
                    // Old side should have equal segments and no "revised"/"v2"
                    assert!(
                        old_segments
                            .iter()
                            .any(|s| s.kind == WordSegmentKind::Equal)
                    );
                    // New side should have "revised" and "v2" as changed
                    let new_changed: String = new_segments
                        .iter()
                        .filter(|s| s.kind == WordSegmentKind::Changed)
                        .map(|s| s.text.as_str())
                        .collect();
                    assert!(
                        new_changed.contains("revised") || new_changed.contains("v2"),
                        "Expected changed words in new_segments, got: {}",
                        new_changed
                    );
                    return; // Found and verified
                }
            }
        }
        panic!("Expected a Modified DiffLine");
    }

    // === normalize_for_diff tests ===

    #[test]
    fn test_normalize_short_lines_unchanged() {
        let text = "Line one.\nLine two.\nLine three.\n";
        assert_eq!(normalize_for_diff(text), text);
    }

    #[test]
    fn test_normalize_splits_long_paragraph_at_sentences() {
        let text = "You are a helpful assistant. You should always respond politely. Use formal language when speaking to customers. Never share personal information. Always verify the caller's identity before proceeding.";
        let normalized = normalize_for_diff(text);
        let lines: Vec<&str> = normalized.lines().collect();
        assert!(
            lines.len() >= 4,
            "Expected at least 4 sentence lines, got {}: {:?}",
            lines.len(),
            lines
        );
        assert!(lines[0].ends_with('.'));
        assert!(lines[1].ends_with('.'));
    }

    #[test]
    fn test_normalize_preserves_existing_multiline() {
        let text = "Short line one.\nShort line two.\nShort line three.\n";
        assert_eq!(normalize_for_diff(text), text);
    }

    #[test]
    fn test_normalize_word_wraps_when_no_sentences() {
        // A long line with no sentence boundaries (no ". A" pattern)
        let text = "this is a very long line without any sentence boundaries or uppercase letters after periods and it just keeps going and going and going and going for a really long time";
        let normalized = normalize_for_diff(text);
        let lines: Vec<&str> = normalized.lines().collect();
        assert!(
            lines.len() > 1,
            "Expected word-wrap to split into multiple lines, got: {:?}",
            lines
        );
        for line in &lines {
            assert!(
                line.len() <= WORD_WRAP_WIDTH + 20,
                "Line too long after wrapping: {} chars",
                line.len()
            );
        }
    }

    #[test]
    fn test_normalize_improves_diff_granularity() {
        let old = "You are a helpful assistant. Always respond politely. Use formal language. Never share secrets.";
        let new = "You are a helpful assistant. Always respond very politely. Use formal language. Never share secrets.";
        // Without normalization: 1 delete + 1 insert (whole paragraph)
        let raw_result = diff_text(old, new);
        // With normalization: only the changed sentence differs
        let norm_old = normalize_for_diff(old);
        let norm_new = normalize_for_diff(new);
        let norm_result = diff_text(&norm_old, &norm_new);
        // The normalized version should have fewer changed lines
        assert!(
            norm_result.deletions <= raw_result.deletions,
            "Normalized diff should have same or fewer deletions"
        );
        assert_eq!(norm_result.deletions, 1);
        assert_eq!(norm_result.insertions, 1);
    }
}