patcher 0.2.1

patcher is a Rust library for generating and applying Git-style unified diff patches.
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
use tracing::warn;

use crate::{Chunk, Operation, Patch};

/// Change type used internally for the diffing algorithms
pub enum Change {
    Equal(usize, usize),  // (old_index, new_index)
    Delete(usize, usize), // (old_index, count)
    Insert(usize, usize), // (new_index, count)
}

/// Handle special cases for empty files
pub fn handle_empty_files(old_lines: &[&str], new_lines: &[&str]) -> Option<Patch> {
    // Special case for empty files
    if old_lines.is_empty() && !new_lines.is_empty() {
        // Adding content to an empty file
        let operations = new_lines
            .iter()
            .map(|&line| Operation::Add(line.to_string()))
            .collect();

        return Some(Patch {
            preamble: None,
            old_file: "original".to_string(),
            new_file: "modified".to_string(),
            chunks: vec![Chunk {
                old_start: 0,
                old_lines: 0,
                new_start: 0,
                new_lines: new_lines.len(),
                operations,
            }],
        });
    } else if !old_lines.is_empty() && new_lines.is_empty() {
        // Removing all content
        let operations = old_lines
            .iter()
            .map(|&line| Operation::Remove(line.to_string()))
            .collect();

        return Some(Patch {
            preamble: None,
            old_file: "original".to_string(),
            new_file: "modified".to_string(),
            chunks: vec![Chunk {
                old_start: 0,
                old_lines: old_lines.len(),
                new_start: 0,
                new_lines: 0,
                operations,
            }],
        });
    } else if old_lines.is_empty() && new_lines.is_empty() {
        // Both files are empty, no diff needed
        return Some(Patch {
            preamble: None,
            old_file: "original".to_string(),
            new_file: "modified".to_string(),
            chunks: Vec::new(),
        });
    }

    None
}

/// Finds the start and end indices of the next block of relevant changes.
/// Skips leading `Equal` changes and merges adjacent non-equal changes
/// separated by fewer than `context_lines * 2` equal changes.
/// Returns `None` if no more non-equal blocks are found.
fn find_next_block(
    changes: &[Change],
    start_index: usize,
    context_lines: usize,
) -> Option<(usize, usize)> {
    let merge_threshold = context_lines * 2;

    // 1. Find the start of the next non-Equal block using iterators
    let Some(relative_start_pos) = changes[start_index..]
        .iter()
        .position(|c| !matches!(c, Change::Equal(_, _)))
    else {
        return None; // No non-equal changes found from start_index onwards
    };
    let block_start_idx = start_index + relative_start_pos;

    // 2. Find the end of the block, merging across small gaps of Equal changes
    let mut block_end_idx = block_start_idx;
    let mut consecutive_equals = 0;

    while block_end_idx < changes.len() {
        match changes[block_end_idx] {
            Change::Equal(_, _) => {
                consecutive_equals += 1;
            }
            _ => {
                // Delete or Insert encountered
                // If the preceding gap of Equal changes was large enough, end the block before it.
                if consecutive_equals > merge_threshold {
                    // Use > not >= to keep context for both sides
                    block_end_idx = block_end_idx.saturating_sub(consecutive_equals);
                    // After adjusting for trailing equals, we have the correct end index.
                    // Return immediately to prevent issues seen in debug logs (spurious second trigger).
                    return Some((block_start_idx, block_end_idx));
                }
                consecutive_equals = 0; // Reset gap counter as we found a non-equal change
            }
        }
        block_end_idx += 1;

        // Post-loop check: Adjust end index if trailing equals exceed threshold
        if block_end_idx == changes.len() && consecutive_equals > merge_threshold {
            block_end_idx = block_end_idx.saturating_sub(consecutive_equals);
            return Some((block_start_idx, block_end_idx));
        }
    }

    // Final sanity check: block end should not be before block start
    if block_end_idx < block_start_idx {
        warn!(
            "Warning: find_next_block calculated block_end ({}) < block_start ({}). Returning None.",
            block_end_idx, block_start_idx
        );
        return None;
    }

    Some((block_start_idx, block_end_idx))
}

/// Builds the list of operations for a chunk, including context,
/// and calculates the old and new line counts for the chunk.
/// Returns the list of operations, the old line count, the new line count,
/// and the index in `changes` after adding trailing context.
fn build_chunk_operations<'a>(
    changes: &[Change],
    old_lines: &'a [&'a str],
    new_lines: &'a [&'a str],
    context_lines: usize,
    context_start_change_idx: usize,
    block_start_idx: usize,
    block_end_idx: usize,
) -> (Vec<Operation>, usize, usize, usize) {
    let mut operations = Vec::new();
    let mut chunk_old_lines_count = 0;
    let mut chunk_new_lines_count = 0;

    // Add context before the block
    for idx in context_start_change_idx..block_start_idx {
        if let Change::Equal(o, _) = changes[idx] {
            // Use get for safety, though indices should be valid based on how changes are generated
            if let Some(line) = old_lines.get(o) {
                operations.push(Operation::Context(line.to_string()));
                chunk_old_lines_count += 1;
                chunk_new_lines_count += 1;
            }
        }
    }

    // Add operations from the core block
    for idx in block_start_idx..block_end_idx {
        match changes[idx] {
            Change::Equal(o, _) => {
                if let Some(line) = old_lines.get(o) {
                    operations.push(Operation::Context(line.to_string()));
                    chunk_old_lines_count += 1;
                    chunk_new_lines_count += 1;
                }
            }
            Change::Delete(o, count) => {
                for j in 0..count {
                    if let Some(line) = old_lines.get(o + j) {
                        operations.push(Operation::Remove(line.to_string()));
                        chunk_old_lines_count += 1;
                    }
                }
            }
            Change::Insert(n, count) => {
                for j in 0..count {
                    if let Some(line) = new_lines.get(n + j) {
                        operations.push(Operation::Add(line.to_string()));
                        chunk_new_lines_count += 1;
                    }
                }
            }
        }
    }

    // Add context after the block
    let mut context_scan_idx = block_end_idx;
    let mut context_added_after = 0;
    while context_added_after < context_lines && context_scan_idx < changes.len() {
        if let Change::Equal(o, _) = changes[context_scan_idx] {
            if let Some(line) = old_lines.get(o) {
                operations.push(Operation::Context(line.to_string()));
                chunk_old_lines_count += 1;
                chunk_new_lines_count += 1;
                context_added_after += 1;
            } else {
                break; // Index out of bounds, stop adding context
            }
        } else {
            break; // Stop adding context if a non-Equal change is encountered
        }
        context_scan_idx += 1;
    }

    (
        operations,
        chunk_old_lines_count,
        chunk_new_lines_count,
        context_scan_idx, // Return the index after scanning for trailing context
    )
}

/// Process changes to generate chunks with proper context
pub fn process_changes_to_chunks(
    changes: &[Change],
    old_lines: &[&str],
    new_lines: &[&str],
    context_lines: usize,
) -> Vec<Chunk> {
    let mut chunks = Vec::new();
    if changes.is_empty() {
        return chunks;
    }

    let mut current_change_idx = 0;
    while current_change_idx < changes.len() {
        // Find the next block of changes to process
        let Some((block_start_idx, block_end_idx)) =
            find_next_block(changes, current_change_idx, context_lines)
        else {
            break; // No more blocks found
        };

        // Safeguard against infinite loops by ensuring we always advance
        if block_end_idx <= current_change_idx {
            // This should ideally not happen if find_next_block is correct, but as a safety measure.
            warn!(
                "Warning: find_next_block did not advance index. current={}, block_end={}. Forcing advance.",
                current_change_idx, block_end_idx
            );
            current_change_idx += 1;
            continue;
        }

        // Calculate context boundaries needed before the block
        let context_start_change_idx = block_start_idx.saturating_sub(context_lines);

        // Determine the actual start line numbers for the chunk based on the first Equal change
        // in the context preceding the block. Fallback to the first change in the block if no context.
        let (chunk_old_start, chunk_new_start) =
            determine_chunk_start_indices(changes, context_start_change_idx, block_start_idx);

        // Build the operations and calculate line counts for the chunk
        let (operations, chunk_old_lines_count, chunk_new_lines_count, next_change_idx) =
            build_chunk_operations(
                changes,
                old_lines,
                new_lines,
                context_lines,
                context_start_change_idx,
                block_start_idx,
                block_end_idx,
            );

        // Create the chunk if it contains operations
        if !operations.is_empty() {
            warn!(
                "[Debug] Creating chunk: old_start={}, new_start={}, old_lines={}, new_lines={}",
                chunk_old_start, chunk_new_start, chunk_old_lines_count, chunk_new_lines_count
            );
            let chunk = Chunk {
                old_start: chunk_old_start,
                old_lines: chunk_old_lines_count,
                new_start: chunk_new_start,
                new_lines: chunk_new_lines_count,
                operations,
            };
            chunks.push(chunk);
        }

        // Continue scanning from where the context scan stopped
        // Ensure we're making progress
        if next_change_idx <= current_change_idx {
            warn!(
                "Warning: next_change_idx did not advance index after building chunk. current={}, next={}. Forcing advance.",
                current_change_idx, next_change_idx
            );
            current_change_idx += 1; // Force advancement if we're stuck
        } else {
            current_change_idx = next_change_idx;
        }
    }

    chunks
}

/// Determines the starting line indices (old, new) for a chunk.
/// It looks for the first `Equal` change within the preceding context window.
/// If no `Equal` change is found in the context, it infers the start based on the first change in the block.
fn determine_chunk_start_indices(
    changes: &[Change],
    context_start_idx: usize,
    block_start_idx: usize,
) -> (usize, usize) {
    // Find the first Equal change in the context window before the block
    let context_start = changes[context_start_idx..block_start_idx]
        .iter()
        .find_map(|c| match c {
            Change::Equal(o, n) => Some((*o, *n)),
            _ => None,
        });

    if let Some((old_start, new_start)) = context_start {
        (old_start, new_start)
    } else {
        // If no Equal context before block, base start on the block's first change
        // This logic might need adjustment if the first block change isn't Equal
        match changes.get(block_start_idx) {
            Some(Change::Equal(o, n)) => (*o, *n),
            Some(Change::Delete(o, _)) => (*o, infer_previous_new_index(changes, block_start_idx)), // Need helper to infer previous state
            Some(Change::Insert(_, n)) => (infer_previous_old_index(changes, block_start_idx), *n), // Need helper to infer previous state
            None => (0, 0), // Should not happen if block_start_idx is valid
        }
    }
}

// Helper function to infer the new index before a Delete, if no preceding Equal context exists.
// This is a simplified inference; might not cover all edge cases perfectly without full state tracking.
fn infer_previous_new_index(changes: &[Change], current_idx: usize) -> usize {
    if current_idx == 0 {
        return 0;
    }
    // Look backwards for the state just before current_idx
    match changes[current_idx - 1] {
        Change::Equal(_, n_prev) => n_prev + 1,
        Change::Insert(n_prev, count) => n_prev + count,
        Change::Delete(_, _) => infer_previous_new_index(changes, current_idx - 1), // Recurse if previous was also delete
    }
}

// Helper function to infer the old index before an Insert, if no preceding Equal context exists.
fn infer_previous_old_index(changes: &[Change], current_idx: usize) -> usize {
    if current_idx == 0 {
        return 0;
    }
    // Look backwards for the state just before current_idx
    match changes[current_idx - 1] {
        Change::Equal(o_prev, _) => o_prev + 1,
        Change::Delete(o_prev, count) => o_prev + count,
        Change::Insert(_, _) => infer_previous_old_index(changes, current_idx - 1), // Recurse if previous was also insert
    }
}

/// Create a patch with the specified chunks
pub fn create_patch(chunks: Vec<Chunk>) -> Patch {
    Patch {
        preamble: None,
        old_file: "original".to_string(),
        new_file: "modified".to_string(),
        chunks,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Chunk, Operation, Patch};

    #[test]
    fn test_handle_empty_files_add_to_empty() {
        let old_lines: Vec<&str> = vec![];
        let new_lines = vec!["a", "b"];
        let expected_patch = Patch {
            preamble: None,
            old_file: "original".to_string(),
            new_file: "modified".to_string(),
            chunks: vec![Chunk {
                old_start: 0,
                old_lines: 0,
                new_start: 0,
                new_lines: 2,
                operations: vec![
                    Operation::Add("a".to_string()),
                    Operation::Add("b".to_string()),
                ],
            }],
        };
        assert_eq!(
            handle_empty_files(&old_lines, &new_lines),
            Some(expected_patch)
        );
    }

    #[test]
    fn test_handle_empty_files_remove_all() {
        let old_lines = vec!["a", "b"];
        let new_lines: Vec<&str> = vec![];
        let expected_patch = Patch {
            preamble: None,
            old_file: "original".to_string(),
            new_file: "modified".to_string(),
            chunks: vec![Chunk {
                old_start: 0,
                old_lines: 2,
                new_start: 0,
                new_lines: 0,
                operations: vec![
                    Operation::Remove("a".to_string()),
                    Operation::Remove("b".to_string()),
                ],
            }],
        };
        assert_eq!(
            handle_empty_files(&old_lines, &new_lines),
            Some(expected_patch)
        );
    }

    #[test]
    fn test_handle_empty_files_both_empty() {
        let old_lines: Vec<&str> = vec![];
        let new_lines: Vec<&str> = vec![];
        let expected_patch = Patch {
            preamble: None,
            old_file: "original".to_string(),
            new_file: "modified".to_string(),
            chunks: Vec::new(),
        };
        assert_eq!(
            handle_empty_files(&old_lines, &new_lines),
            Some(expected_patch)
        );
    }

    #[test]
    fn test_handle_empty_files_no_change() {
        let old_lines = vec!["a"];
        let new_lines = vec!["a"];
        assert_eq!(handle_empty_files(&old_lines, &new_lines), None);
    }

    // --- Tests for process_changes_to_chunks ---

    #[test]
    fn test_process_chunks_basic_insert() {
        let old_lines = vec!["a", "b"];
        let new_lines = vec!["a", "x", "y", "b"];
        let changes = vec![
            Change::Equal(0, 0),  // a
            Change::Insert(1, 2), // x, y
            Change::Equal(1, 3),  // b
        ];
        let context_lines = 1;
        let chunks = process_changes_to_chunks(&changes, &old_lines, &new_lines, context_lines);

        assert_eq!(chunks.len(), 1);
        let chunk = &chunks[0];
        assert_eq!(chunk.old_start, 0);
        assert_eq!(chunk.new_start, 0);
        assert_eq!(chunk.old_lines, 2);
        assert_eq!(chunk.new_lines, 4);
        assert_eq!(
            chunk.operations,
            vec![
                Operation::Context("a".to_string()),
                Operation::Add("x".to_string()),
                Operation::Add("y".to_string()),
                Operation::Context("b".to_string()),
            ]
        );
    }

    #[test]
    fn test_process_chunks_basic_delete() {
        let old_lines = vec!["a", "x", "y", "b"];
        let new_lines = vec!["a", "b"];
        let changes = vec![
            Change::Equal(0, 0),  // a
            Change::Delete(1, 2), // x, y
            Change::Equal(3, 1),  // b
        ];
        let context_lines = 1;
        let chunks = process_changes_to_chunks(&changes, &old_lines, &new_lines, context_lines);

        assert_eq!(chunks.len(), 1);
        let chunk = &chunks[0];
        assert_eq!(chunk.old_start, 0);
        assert_eq!(chunk.new_start, 0);
        assert_eq!(chunk.old_lines, 4);
        assert_eq!(chunk.new_lines, 2);
        assert_eq!(
            chunk.operations,
            vec![
                Operation::Context("a".to_string()),
                Operation::Remove("x".to_string()),
                Operation::Remove("y".to_string()),
                Operation::Context("b".to_string()),
            ]
        );
    }

    #[test]
    fn test_process_chunks_basic_replace() {
        let old_lines = vec!["a", "b", "c"];
        let new_lines = vec!["a", "x", "c"];
        let changes = vec![
            Change::Equal(0, 0),  // a
            Change::Delete(1, 1), // b
            Change::Insert(1, 1), // x
            Change::Equal(2, 2),  // c
        ];
        let context_lines = 1;
        let chunks = process_changes_to_chunks(&changes, &old_lines, &new_lines, context_lines);

        assert_eq!(chunks.len(), 1);
        let chunk = &chunks[0];
        assert_eq!(chunk.old_start, 0);
        assert_eq!(chunk.new_start, 0);
        assert_eq!(chunk.old_lines, 3);
        assert_eq!(chunk.new_lines, 3);
        assert_eq!(
            chunk.operations,
            vec![
                Operation::Context("a".to_string()),
                Operation::Remove("b".to_string()),
                Operation::Add("x".to_string()),
                Operation::Context("c".to_string()),
            ]
        );
    }

    #[test]
    fn test_process_chunks_multiple_blocks() {
        let old_lines = vec!["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]; // 10
        let new_lines = vec!["a", "X", "c", "d", "e", "f", "g", "Y", "i", "j"]; // 10
        let changes = vec![
            Change::Equal(0, 0),  // a
            Change::Delete(1, 1), // b
            Change::Insert(1, 1), // X
            Change::Equal(2, 2),  // c
            Change::Equal(3, 3),  // d
            Change::Equal(4, 4),  // e (context=1, merge_threshold=2 -> need >2 equals to split)
            Change::Equal(5, 5),  // f
            Change::Equal(6, 6),  // g
            Change::Delete(7, 1), // h
            Change::Insert(7, 1), // Y
            Change::Equal(8, 8),  // i
            Change::Equal(9, 9),  // j
        ];
        let context_lines = 1;
        let chunks = process_changes_to_chunks(&changes, &old_lines, &new_lines, context_lines);

        // With context=1, the 4 Equal changes (c,d,e,f) > context*2 (2), so it should split
        assert_eq!(chunks.len(), 2);

        // Chunk 1: Replace b with X
        let chunk1 = &chunks[0];
        assert_eq!(chunk1.old_start, 0);
        assert_eq!(chunk1.new_start, 0);
        assert_eq!(chunk1.old_lines, 3); // a, b, c
        assert_eq!(chunk1.new_lines, 3); // a, X, c
        assert_eq!(
            chunk1.operations,
            vec![
                Operation::Context("a".to_string()),
                Operation::Remove("b".to_string()),
                Operation::Add("X".to_string()),
                Operation::Context("c".to_string()), // Trailing context
            ]
        );

        // Chunk 2: Replace h with Y
        let chunk2 = &chunks[1];
        assert_eq!(chunk2.old_start, 6); // Starts at g (context)
        assert_eq!(chunk2.new_start, 6); // Starts at g (context)
        assert_eq!(chunk2.old_lines, 4); // g, h, i, j
        assert_eq!(chunk2.new_lines, 4); // g, Y, i, j
        assert_eq!(
            chunk2.operations,
            vec![
                Operation::Context("g".to_string()), // Leading context
                Operation::Remove("h".to_string()),
                Operation::Add("Y".to_string()),
                Operation::Context("i".to_string()),
                Operation::Context("j".to_string()), // Trailing context
            ]
        );
    }

    #[test]
    fn test_process_chunks_zero_context() {
        let old_lines = vec!["a", "b", "c"];
        let new_lines = vec!["a", "x", "c"];
        let changes = vec![
            Change::Equal(0, 0),  // a
            Change::Delete(1, 1), // b
            Change::Insert(1, 1), // x
            Change::Equal(2, 2),  // c
        ];
        let context_lines = 0;
        let chunks = process_changes_to_chunks(&changes, &old_lines, &new_lines, context_lines);

        assert_eq!(chunks.len(), 1);
        let chunk = &chunks[0];
        assert_eq!(chunk.old_start, 1); // Starts at b (no context)
        assert_eq!(chunk.new_start, 1); // Starts at x (no context)
        assert_eq!(chunk.old_lines, 1); // b
        assert_eq!(chunk.new_lines, 1); // x
        assert_eq!(
            chunk.operations,
            vec![
                Operation::Remove("b".to_string()),
                Operation::Add("x".to_string()),
            ]
        );
    }

    #[test]
    fn test_process_chunks_context_at_ends() {
        let old_lines = vec!["a", "b", "c", "d", "e"];
        let new_lines = vec!["x", "b", "c", "d", "y"];
        let changes = vec![
            Change::Delete(0, 1), // a
            Change::Insert(0, 1), // x
            Change::Equal(1, 1),  // b (context=1)
            Change::Equal(2, 2),  // c
            Change::Equal(3, 3),  // d (context=1)
            Change::Delete(4, 1), // e
            Change::Insert(4, 1), // y
        ];
        let context_lines = 1;
        let chunks = process_changes_to_chunks(&changes, &old_lines, &new_lines, context_lines);

        assert_eq!(chunks.len(), 2);

        // Chunk 1: Replace a with x
        let chunk1 = &chunks[0];
        assert_eq!(chunk1.old_start, 0); // Starts at a
        assert_eq!(chunk1.new_start, 0); // Starts at x
        assert_eq!(chunk1.old_lines, 2); // a, b
        assert_eq!(chunk1.new_lines, 2); // x, b
        assert_eq!(
            chunk1.operations,
            vec![
                Operation::Remove("a".to_string()),
                Operation::Add("x".to_string()),
                Operation::Context("b".to_string()), // Trailing context
            ]
        );

        // Chunk 2: Replace e with y
        let chunk2 = &chunks[1];
        assert_eq!(chunk2.old_start, 3); // Starts at d (context)
        assert_eq!(chunk2.new_start, 3); // Starts at d (context)
        assert_eq!(chunk2.old_lines, 2); // d, e
        assert_eq!(chunk2.new_lines, 2); // d, y
        assert_eq!(
            chunk2.operations,
            vec![
                Operation::Context("d".to_string()), // Leading context
                Operation::Remove("e".to_string()),
                Operation::Add("y".to_string()),
            ]
        );
    }

    // --- Tests for find_next_block ---

    #[test]
    fn test_find_next_block_all_equal() {
        let changes = vec![Change::Equal(0, 0), Change::Equal(1, 1)];
        assert_eq!(find_next_block(&changes, 0, 1), None);
    }

    #[test]
    fn test_find_next_block_single_block_start() {
        let changes = vec![
            Change::Delete(0, 1),
            Change::Equal(1, 0),
            Change::Equal(2, 1),
        ];
        // Block starts at 0 (Delete). Encounters 2 Equals. Threshold is 1*2=2.
        // Since 2 is not > 2, the equals are merged.
        assert_eq!(find_next_block(&changes, 0, 1), Some((0, 3))); // Corrected assertion
    }

    #[test]
    fn test_find_next_block_single_block_middle() {
        let changes = vec![
            Change::Equal(0, 0),
            Change::Insert(1, 1),
            Change::Equal(1, 2),
        ];
        assert_eq!(find_next_block(&changes, 0, 1), Some((1, 3))); // Corrected end index
        assert_eq!(find_next_block(&changes, 3, 1), None); // Corrected start scan index
    }

    #[test]
    fn test_find_next_block_merges_small_gap() {
        let changes = vec![
            Change::Delete(0, 1),
            Change::Equal(1, 0), // 1 equal change, context=1, merge_threshold=2. 1 <= 2, so merge
            Change::Insert(1, 1),
            Change::Equal(2, 2),
        ];
        assert_eq!(find_next_block(&changes, 0, 1), Some((0, 4))); // Corrected end index
    }

    #[test]
    fn test_find_next_block_splits_large_gap() {
        let changes = vec![
            Change::Delete(0, 1),
            Change::Equal(1, 0),
            Change::Equal(2, 1),
            Change::Equal(3, 2), // 3 equal changes, context=1, merge_threshold=2. 3 > 2, so split
            Change::Insert(4, 1),
        ];
        assert_eq!(find_next_block(&changes, 0, 1), Some((0, 1))); // First block
        assert_eq!(find_next_block(&changes, 1, 1), Some((4, 5))); // Second block (scan starts after first block)
    }

    #[test]
    fn test_find_next_block_trailing_equals_split() {
        let changes = vec![
            Change::Delete(0, 1),
            Change::Equal(1, 0),
            Change::Equal(2, 1),
            Change::Equal(3, 2), // 3 trailing equals > merge_threshold=2
        ];
        assert_eq!(find_next_block(&changes, 0, 1), Some((0, 1))); // Block ends before trailing equals
    }

    #[test]
    fn test_find_next_block_trailing_equals_merge() {
        let changes = vec![
            Change::Delete(0, 1),
            Change::Equal(1, 0), // 1 trailing equal <= merge_threshold=2
        ];
        assert_eq!(find_next_block(&changes, 0, 1), Some((0, 2))); // Block includes trailing equal
    }

    // --- Tests for determine_chunk_start_indices ---

    #[test]
    fn test_determine_start_indices_with_context() {
        let changes = vec![
            Change::Equal(5, 5), // context_start_idx = 0
            Change::Equal(6, 6),
            Change::Delete(7, 1), // block_start_idx = 2
        ];
        assert_eq!(determine_chunk_start_indices(&changes, 0, 2), (5, 5));
    }

    #[test]
    fn test_determine_start_indices_no_context_equal_start() {
        let changes = vec![
            Change::Equal(7, 7), // block_start_idx = 0
            Change::Delete(8, 1),
        ];
        // context_start_idx = block_start_idx = 0
        assert_eq!(determine_chunk_start_indices(&changes, 0, 0), (7, 7));
    }

    #[test]
    fn test_determine_start_indices_no_context_delete_start() {
        let changes = vec![
            Change::Equal(5, 5),  // Change before block
            Change::Delete(6, 1), // block_start_idx = 1
        ];
        // context_start_idx = block_start_idx = 1
        assert_eq!(determine_chunk_start_indices(&changes, 1, 1), (6, 6)); // Infers new index from previous Equal
    }

    #[test]
    fn test_determine_start_indices_no_context_insert_start() {
        let changes = vec![
            Change::Equal(5, 5),  // Change before block
            Change::Insert(6, 1), // block_start_idx = 1
        ];
        // context_start_idx = block_start_idx = 1
        assert_eq!(determine_chunk_start_indices(&changes, 1, 1), (6, 1)); // Infers old index from previous Equal
    }

    #[test]
    fn test_determine_start_indices_no_context_delete_start_at_file_start() {
        let changes = vec![
            Change::Delete(0, 1), // block_start_idx = 0
        ];
        // context_start_idx = block_start_idx = 0
        assert_eq!(determine_chunk_start_indices(&changes, 0, 0), (0, 0)); // Infers 0 for new index
    }

    #[test]
    fn test_determine_start_indices_no_context_insert_start_at_file_start() {
        let changes = vec![
            Change::Insert(0, 1), // block_start_idx = 0
        ];
        // context_start_idx = block_start_idx = 0
        // 0-based logic yields (0, 0). Check if 1-based (1, 1) is expected.
        assert_eq!(determine_chunk_start_indices(&changes, 0, 0), (0, 1));
    }
}