rskim 2.3.1

The most intelligent context optimization engine for coding agents. Code-aware AST parsing, command rewriting, output compression.
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
//! Unified diff parsing — hunk extraction and file status detection.

use std::sync::LazyLock;

use regex::Regex;

use super::types::{DiffHunk, FileChange, FileDiff, FileMetadata};
use crate::output::canonical::DiffFileStatus;

/// Matches hunk headers: `@@ -N,M +N,M @@ optional context`
static HUNK_RE: LazyLock<Regex> = LazyLock::new(|| {
    Regex::new(r"^@@\s+-(\d+)(?:,(\d+))?\s+\+(\d+)(?:,(\d+))?\s+@@").expect("valid regex")
});

/// Parse a hunk header line: `@@ -N,M +N,M @@`
///
/// Returns `(old_start, old_count, new_start, new_count)` on success.
pub(super) fn parse_hunk_header(line: &str) -> Option<(usize, usize, usize, usize)> {
    let caps = HUNK_RE.captures(line)?;
    let old_start: usize = caps.get(1)?.as_str().parse().ok()?;
    let old_count: usize = caps.get(2).map_or(1, |m| m.as_str().parse().unwrap_or(1));
    let new_start: usize = caps.get(3)?.as_str().parse().ok()?;
    let new_count: usize = caps.get(4).map_or(1, |m| m.as_str().parse().unwrap_or(1));
    Some((old_start, old_count, new_start, new_count))
}

/// Scan extended headers from a `diff --git` block.
///
/// Starting at `start`, reads lines until a hunk header (`@@`) or the next
/// `diff --git` header. Returns the collected metadata and the index of
/// the next unprocessed line.
pub(super) fn scan_extended_headers(lines: &[&str], start: usize) -> (FileMetadata, usize) {
    let mut meta = FileMetadata {
        change: FileChange::Modified,
        file_minus: String::new(),
        file_plus: String::new(),
    };

    let mut i = start;
    while i < lines.len() && !lines[i].starts_with("diff --git ") {
        let line = lines[i];

        if line.starts_with("new file mode") {
            meta.change = FileChange::New;
        } else if line.starts_with("deleted file mode") {
            meta.change = FileChange::Deleted;
        } else if line.starts_with("rename from ") {
            let from = line
                .strip_prefix("rename from ")
                .unwrap_or_default()
                .to_string();
            meta.change = FileChange::Renamed { from: Some(from) };
        } else if line.starts_with("rename to ") {
            // Only update if not already set to Renamed (rename from comes first)
            if !matches!(meta.change, FileChange::Renamed { .. }) {
                meta.change = FileChange::Renamed { from: None };
            }
        } else if line.starts_with("Binary files") && line.contains("differ") {
            meta.change = FileChange::Binary;
        } else if line.starts_with("--- ") {
            meta.file_minus = line.strip_prefix("--- ").unwrap_or_default().to_string();
        } else if line.starts_with("+++ ") {
            meta.file_plus = line.strip_prefix("+++ ").unwrap_or_default().to_string();
        } else if line.starts_with("@@") {
            // Hunk header — extended headers are done, stop before consuming it
            break;
        }

        i += 1;
    }

    (meta, i)
}

/// Collect hunks from diff lines starting at `start`.
///
/// Reads hunk headers (`@@`) and their patch lines until the next `diff --git`
/// header or end of input. Returns the hunks and the index of the next
/// unprocessed line.
pub(super) fn collect_hunks<'a>(lines: &[&'a str], start: usize) -> (Vec<DiffHunk<'a>>, usize) {
    let mut hunks: Vec<DiffHunk<'a>> = Vec::new();
    let mut i = start;

    while i < lines.len() && !lines[i].starts_with("diff --git ") {
        let line = lines[i];

        if line.starts_with("@@") {
            if let Some((old_start, old_count, new_start, new_count)) = parse_hunk_header(line) {
                let mut patch_lines: Vec<&'a str> = Vec::new();
                i += 1;

                while i < lines.len() {
                    let patch_line = lines[i];
                    if patch_line.starts_with("diff --git ") || patch_line.starts_with("@@") {
                        break;
                    }
                    // Only keep actual patch lines (+, -, space, or \ no newline)
                    if patch_line.starts_with('+')
                        || patch_line.starts_with('-')
                        || patch_line.starts_with(' ')
                        || patch_line.starts_with('\\')
                    {
                        patch_lines.push(patch_line);
                    }
                    i += 1;
                }

                hunks.push(DiffHunk {
                    old_start,
                    old_count,
                    new_start,
                    new_count,
                    patch_lines,
                });
                continue;
            }
        }
        i += 1;
    }

    (hunks, i)
}

/// Determine the file status, display path, and optional old_path from diff
/// header paths and extended header metadata.
pub(super) fn resolve_file_info(
    a_path: &str,
    b_path: &str,
    meta: &FileMetadata,
) -> (DiffFileStatus, String, Option<String>) {
    let status = match &meta.change {
        FileChange::Binary => DiffFileStatus::Binary,
        FileChange::New => DiffFileStatus::Added,
        FileChange::Deleted => DiffFileStatus::Deleted,
        FileChange::Renamed { .. } => DiffFileStatus::Renamed,
        FileChange::Modified => {
            // Also check --- / +++ paths as fallback for diffs without explicit headers
            if meta.file_minus == "/dev/null" || meta.file_minus == "a//dev/null" {
                DiffFileStatus::Added
            } else if meta.file_plus == "/dev/null" || meta.file_plus == "b//dev/null" {
                DiffFileStatus::Deleted
            } else {
                DiffFileStatus::Modified
            }
        }
    };

    let path = if status == DiffFileStatus::Deleted {
        strip_ab_prefix(a_path)
    } else {
        strip_ab_prefix(b_path)
    };

    let old_path = if let FileChange::Renamed { from } = &meta.change {
        from.clone().or_else(|| Some(strip_ab_prefix(a_path)))
    } else {
        None
    };

    (status, path, old_path)
}

/// Parse unified diff output into a list of per-file diffs.
///
/// Handles standard `git diff --no-color` output including:
/// - New files (`--- /dev/null`)
/// - Deleted files (`+++ /dev/null`)
/// - Renamed files (`rename from` / `rename to`)
/// - Binary files (`Binary files ... differ`)
pub(super) fn parse_unified_diff<'a>(output: &'a str) -> Vec<FileDiff<'a>> {
    let mut files: Vec<FileDiff<'a>> = Vec::new();
    let lines: Vec<&str> = output.lines().collect();
    let mut i = 0;

    while i < lines.len() {
        // Skip lines until a diff header
        if !lines[i].starts_with("diff --git ") {
            i += 1;
            continue;
        }

        let (a_path, b_path) = parse_diff_git_header(lines[i]);
        i += 1;

        let (meta, next_i) = scan_extended_headers(&lines, i);
        i = next_i;

        let (status, path, old_path) = resolve_file_info(&a_path, &b_path, &meta);

        let hunks = if meta.change == FileChange::Binary {
            // Skip remaining lines for binary files
            while i < lines.len() && !lines[i].starts_with("diff --git ") {
                i += 1;
            }
            Vec::new()
        } else {
            let (h, next_i) = collect_hunks(&lines, i);
            i = next_i;
            h
        };

        files.push(FileDiff {
            path,
            old_path,
            status,
            hunks,
        });
    }

    files
}

/// Parse the `diff --git a/path b/path` header to extract both paths.
pub(super) fn parse_diff_git_header(line: &str) -> (String, String) {
    // Format: "diff --git a/path b/path"
    // Handle paths with spaces by splitting on " b/"
    let rest = line.strip_prefix("diff --git ").unwrap_or(line);

    // Find the boundary between a/path and b/path.
    // We use rfind so that paths containing " b/" in a directory name
    // are split at the *last* occurrence (which is the real separator).
    // Try " b/" first, then " b\" (Windows), then fall back to last space.
    let sep = rest
        .rfind(" b/")
        .or_else(|| rest.rfind(" b\\"))
        .or_else(|| rest.rfind(' '));
    if let Some(pos) = sep {
        (rest[..pos].to_string(), rest[pos + 1..].to_string())
    } else {
        // No separator found — treat entire string as b-path
        (rest.to_string(), rest.to_string())
    }
}

/// Strip the `a/` or `b/` prefix from a diff path.
pub(super) fn strip_ab_prefix(path: &str) -> String {
    path.strip_prefix("a/")
        .or_else(|| path.strip_prefix("b/"))
        .unwrap_or(path)
        .to_string()
}

// ============================================================================
// Tests
// ============================================================================

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

    // ========================================================================
    // Hunk header parsing tests (#103)
    // ========================================================================

    #[test]
    fn test_parse_hunk_header_basic() {
        let result = parse_hunk_header("@@ -1,5 +1,8 @@ function foo() {");
        assert_eq!(result, Some((1, 5, 1, 8)));
    }

    #[test]
    fn test_parse_hunk_header_single_line() {
        let result = parse_hunk_header("@@ -1 +1 @@");
        assert_eq!(result, Some((1, 1, 1, 1)));
    }

    #[test]
    fn test_parse_hunk_header_with_context_label() {
        let result = parse_hunk_header("@@ -10,3 +12,5 @@ impl MyStruct {");
        assert_eq!(result, Some((10, 3, 12, 5)));
    }

    #[test]
    fn test_parse_hunk_header_malformed() {
        assert!(parse_hunk_header("not a hunk header").is_none());
        assert!(parse_hunk_header("@@ invalid @@").is_none());
        assert!(parse_hunk_header("--- a/file.rs").is_none());
    }

    #[test]
    fn test_parse_hunk_header_zero_count() {
        // New file with no old content
        let result = parse_hunk_header("@@ -0,0 +1,12 @@");
        assert_eq!(result, Some((0, 0, 1, 12)));
    }

    #[test]
    fn test_parse_hunk_header_large_line_numbers() {
        let result = parse_hunk_header("@@ -1000,50 +1050,60 @@");
        assert_eq!(result, Some((1000, 50, 1050, 60)));
    }

    // ========================================================================
    // Unified diff parsing tests (#103)
    // ========================================================================

    #[test]
    fn test_parse_unified_diff_single_file() {
        let input = include_str!("../../../../tests/fixtures/cmd/diff/single_file.diff");
        let files = parse_unified_diff(input);

        assert_eq!(files.len(), 1, "expected 1 file");
        assert_eq!(files[0].path, "src/auth/middleware.ts");
        assert_eq!(files[0].status, DiffFileStatus::Modified);
        assert_eq!(files[0].hunks.len(), 1, "expected 1 hunk");
    }

    #[test]
    fn test_parse_unified_diff_multi_file() {
        let input = include_str!("../../../../tests/fixtures/cmd/diff/multi_file.diff");
        let files = parse_unified_diff(input);

        assert_eq!(files.len(), 2, "expected 2 files");
        assert_eq!(files[0].path, "src/api/routes.ts");
        assert_eq!(files[0].status, DiffFileStatus::Modified);
        assert_eq!(files[1].path, "src/api/handlers.ts");
        assert_eq!(files[1].status, DiffFileStatus::Modified);
        assert_eq!(files[1].hunks.len(), 2, "expected 2 hunks for handlers.ts");
    }

    #[test]
    fn test_parse_unified_diff_new_file() {
        let input = include_str!("../../../../tests/fixtures/cmd/diff/new_file.diff");
        let files = parse_unified_diff(input);

        assert_eq!(files.len(), 1, "expected 1 file");
        assert_eq!(files[0].path, "src/utils/validator.ts");
        assert_eq!(files[0].status, DiffFileStatus::Added);
        assert_eq!(files[0].hunks.len(), 1, "expected 1 hunk");
        // All lines should be additions
        assert!(
            files[0].hunks[0]
                .patch_lines
                .iter()
                .all(|l| l.starts_with('+')),
            "all lines in new file should be additions"
        );
    }

    #[test]
    fn test_parse_unified_diff_deleted_file() {
        let input = include_str!("../../../../tests/fixtures/cmd/diff/deleted_file.diff");
        let files = parse_unified_diff(input);

        assert_eq!(files.len(), 1, "expected 1 file");
        assert_eq!(files[0].path, "src/legacy/old_auth.ts");
        assert_eq!(files[0].status, DiffFileStatus::Deleted);
        // All lines should be deletions
        assert!(
            files[0].hunks[0]
                .patch_lines
                .iter()
                .all(|l| l.starts_with('-')),
            "all lines in deleted file should be deletions"
        );
    }

    #[test]
    fn test_parse_unified_diff_renamed_file() {
        let input = include_str!("../../../../tests/fixtures/cmd/diff/renamed_file.diff");
        let files = parse_unified_diff(input);

        assert_eq!(files.len(), 1, "expected 1 file");
        assert_eq!(files[0].path, "src/utils/format.ts");
        assert_eq!(files[0].status, DiffFileStatus::Renamed);
        assert_eq!(
            files[0].old_path.as_deref(),
            Some("src/utils/helpers.ts"),
            "expected old path for rename"
        );
    }

    #[test]
    fn test_parse_unified_diff_binary_file() {
        let input = include_str!("../../../../tests/fixtures/cmd/diff/binary_file.diff");
        let files = parse_unified_diff(input);

        assert_eq!(files.len(), 1, "expected 1 file");
        assert_eq!(files[0].path, "assets/logo.png");
        assert_eq!(files[0].status, DiffFileStatus::Binary);
        assert!(
            files[0].hunks.is_empty(),
            "binary files should have no hunks"
        );
    }

    // ========================================================================
    // File status detection tests (#103)
    // ========================================================================

    #[test]
    fn test_file_status_from_new_file() {
        let diff = "diff --git a/new.ts b/new.ts\nnew file mode 100644\nindex 0000000..abc1234\n--- /dev/null\n+++ b/new.ts\n@@ -0,0 +1,3 @@\n+line 1\n+line 2\n+line 3\n";
        let files = parse_unified_diff(diff);
        assert_eq!(files[0].status, DiffFileStatus::Added);
    }

    #[test]
    fn test_file_status_from_deleted_file() {
        let diff = "diff --git a/old.ts b/old.ts\ndeleted file mode 100644\nindex abc1234..0000000\n--- a/old.ts\n+++ /dev/null\n@@ -1,3 +0,0 @@\n-line 1\n-line 2\n-line 3\n";
        let files = parse_unified_diff(diff);
        assert_eq!(files[0].status, DiffFileStatus::Deleted);
    }

    #[test]
    fn test_file_status_modified() {
        let diff = "diff --git a/mod.ts b/mod.ts\nindex abc..def 100644\n--- a/mod.ts\n+++ b/mod.ts\n@@ -1,3 +1,4 @@\n line 1\n-line 2\n+line 2 modified\n+line 2b\n line 3\n";
        let files = parse_unified_diff(diff);
        assert_eq!(files[0].status, DiffFileStatus::Modified);
    }

    // ========================================================================
    // Hunk content extraction tests (#103)
    // ========================================================================

    #[test]
    fn test_hunk_content_single_file() {
        let input = include_str!("../../../../tests/fixtures/cmd/diff/single_file.diff");
        let files = parse_unified_diff(input);

        let hunk = &files[0].hunks[0];
        assert_eq!(hunk.old_start, 5);
        assert_eq!(hunk.old_count, 7);
        assert_eq!(hunk.new_start, 5);
        assert_eq!(hunk.new_count, 10);

        // Should contain both + and - lines
        let has_additions = hunk.patch_lines.iter().any(|l| l.starts_with('+'));
        let has_deletions = hunk.patch_lines.iter().any(|l| l.starts_with('-'));
        assert!(has_additions, "expected additions in hunk");
        assert!(has_deletions, "expected deletions in hunk");
    }

    #[test]
    fn test_hunk_content_new_file() {
        let input = include_str!("../../../../tests/fixtures/cmd/diff/new_file.diff");
        let files = parse_unified_diff(input);

        let hunk = &files[0].hunks[0];
        assert_eq!(hunk.old_start, 0);
        assert_eq!(hunk.old_count, 0);
        assert_eq!(hunk.new_start, 1);
        assert_eq!(hunk.new_count, 12);
    }

    // ========================================================================
    // Empty diff edge case (#103)
    // ========================================================================

    #[test]
    fn test_parse_unified_diff_empty() {
        let files = parse_unified_diff("");
        assert!(files.is_empty());
    }

    #[test]
    fn test_parse_unified_diff_whitespace_only() {
        let files = parse_unified_diff("  \n\n  \n");
        assert!(files.is_empty());
    }

    // ========================================================================
    // strip_ab_prefix tests (#103)
    // ========================================================================

    #[test]
    fn test_strip_ab_prefix() {
        assert_eq!(strip_ab_prefix("a/src/main.rs"), "src/main.rs");
        assert_eq!(strip_ab_prefix("b/src/main.rs"), "src/main.rs");
        assert_eq!(strip_ab_prefix("src/main.rs"), "src/main.rs");
        assert_eq!(strip_ab_prefix("/dev/null"), "/dev/null");
    }

    // ========================================================================
    // parse_diff_git_header tests (#103)
    // ========================================================================

    #[test]
    fn test_parse_diff_git_header_simple() {
        let (a, b) = parse_diff_git_header("diff --git a/src/main.rs b/src/main.rs");
        assert_eq!(a, "a/src/main.rs");
        assert_eq!(b, "b/src/main.rs");
    }

    #[test]
    fn test_parse_diff_git_header_different_paths() {
        let (a, b) = parse_diff_git_header("diff --git a/old/path.ts b/new/path.ts");
        assert_eq!(a, "a/old/path.ts");
        assert_eq!(b, "b/new/path.ts");
    }

    #[test]
    fn test_parse_diff_git_header_fallback_no_b_prefix() {
        // Unusual format without " b/" — falls back to last-space split
        let (a, b) = parse_diff_git_header("diff --git a-path b-path");
        assert_eq!(a, "a-path");
        assert_eq!(b, "b-path");
    }

    #[test]
    fn test_parse_diff_git_header_no_separator() {
        // Degenerate input with no space after stripping prefix
        let (a, b) = parse_diff_git_header("diff --git noseparator");
        // Both should be the same — no split possible
        assert_eq!(a, "noseparator");
        assert_eq!(b, "noseparator");
    }

    // ========================================================================
    // scan_extended_headers direct unit tests (parse:51:testing)
    // ========================================================================

    #[test]
    fn test_scan_extended_headers_new_file() {
        let lines = vec![
            "new file mode 100644",
            "index 0000000..abc1234",
            "--- /dev/null",
            "+++ b/src/new.rs",
        ];
        let (meta, idx) = scan_extended_headers(&lines, 0);
        assert_eq!(meta.change, FileChange::New);
        assert_eq!(meta.file_minus, "/dev/null");
        assert_eq!(meta.file_plus, "b/src/new.rs");
        assert_eq!(idx, lines.len(), "should consume all lines");
    }

    #[test]
    fn test_scan_extended_headers_deleted_file() {
        let lines = vec![
            "deleted file mode 100644",
            "index abc1234..0000000",
            "--- a/src/old.rs",
            "+++ /dev/null",
        ];
        let (meta, idx) = scan_extended_headers(&lines, 0);
        assert_eq!(meta.change, FileChange::Deleted);
        assert_eq!(meta.file_minus, "a/src/old.rs");
        assert_eq!(meta.file_plus, "/dev/null");
        assert_eq!(idx, lines.len());
    }

    #[test]
    fn test_scan_extended_headers_rename_with_from() {
        let lines = vec![
            "similarity index 95%",
            "rename from src/utils/helpers.ts",
            "rename to src/utils/format.ts",
            "index abc..def 100644",
            "--- a/src/utils/helpers.ts",
            "+++ b/src/utils/format.ts",
        ];
        let (meta, idx) = scan_extended_headers(&lines, 0);
        assert_eq!(
            meta.change,
            FileChange::Renamed {
                from: Some("src/utils/helpers.ts".to_string())
            }
        );
        assert_eq!(idx, lines.len());
    }

    #[test]
    fn test_scan_extended_headers_rename_to_only() {
        // `rename to` without a preceding `rename from` — sets Renamed { from: None }
        let lines = vec!["rename to src/new.rs"];
        let (meta, _) = scan_extended_headers(&lines, 0);
        assert_eq!(meta.change, FileChange::Renamed { from: None });
    }

    #[test]
    fn test_scan_extended_headers_binary() {
        let lines = vec!["Binary files a/img.png and b/img.png differ"];
        let (meta, idx) = scan_extended_headers(&lines, 0);
        assert_eq!(meta.change, FileChange::Binary);
        assert_eq!(idx, 1);
    }

    #[test]
    fn test_scan_extended_headers_stops_at_hunk_header() {
        let lines = vec![
            "index abc..def 100644",
            "--- a/src/main.rs",
            "+++ b/src/main.rs",
            "@@ -1,3 +1,4 @@",
            " context",
        ];
        let (meta, idx) = scan_extended_headers(&lines, 0);
        assert_eq!(meta.change, FileChange::Modified);
        // Must stop before consuming the @@ line
        assert_eq!(idx, 3, "should stop at the @@ line");
    }

    #[test]
    fn test_scan_extended_headers_stops_at_next_diff_header() {
        let lines = vec!["index abc..def 100644", "diff --git a/other.rs b/other.rs"];
        let (meta, idx) = scan_extended_headers(&lines, 0);
        assert_eq!(meta.change, FileChange::Modified);
        assert_eq!(idx, 1, "should stop at the diff --git line");
    }

    #[test]
    fn test_scan_extended_headers_start_offset() {
        // Verify the `start` parameter is respected
        let lines = vec![
            "diff --git a/ignore.rs b/ignore.rs",
            "new file mode 100644",
            "--- /dev/null",
            "+++ b/ignore.rs",
        ];
        let (meta, idx) = scan_extended_headers(&lines, 1);
        assert_eq!(meta.change, FileChange::New);
        assert_eq!(idx, lines.len());
    }

    // ========================================================================
    // collect_hunks direct unit tests (parse:97:testing)
    // ========================================================================

    #[test]
    fn test_collect_hunks_single_hunk() {
        let lines = vec![
            "@@ -1,3 +1,4 @@",
            " context",
            "-old line",
            "+new line",
            "+added line",
            " context",
        ];
        let (hunks, idx) = collect_hunks(&lines, 0);
        assert_eq!(hunks.len(), 1);
        let h = &hunks[0];
        assert_eq!(h.old_start, 1);
        assert_eq!(h.old_count, 3);
        assert_eq!(h.new_start, 1);
        assert_eq!(h.new_count, 4);
        assert_eq!(h.patch_lines.len(), 5);
        assert_eq!(idx, lines.len());
    }

    #[test]
    fn test_collect_hunks_two_hunks() {
        let lines = vec![
            "@@ -1,2 +1,2 @@",
            "-a",
            "+b",
            "@@ -10,2 +10,3 @@",
            " ctx",
            "+new",
            " ctx",
        ];
        let (hunks, idx) = collect_hunks(&lines, 0);
        assert_eq!(hunks.len(), 2);
        assert_eq!(hunks[0].old_start, 1);
        assert_eq!(hunks[1].old_start, 10);
        assert_eq!(hunks[1].new_count, 3);
        assert_eq!(idx, lines.len());
    }

    #[test]
    fn test_collect_hunks_stops_at_next_diff_header() {
        let lines = vec![
            "@@ -1,1 +1,1 @@",
            "-x",
            "+y",
            "diff --git a/other.rs b/other.rs",
            "@@ -5,1 +5,1 @@",
        ];
        let (hunks, idx) = collect_hunks(&lines, 0);
        assert_eq!(hunks.len(), 1, "should stop before next diff --git header");
        assert_eq!(idx, 3, "should point at the diff --git line");
    }

    #[test]
    fn test_collect_hunks_skips_non_patch_lines() {
        // Lines without +/-/space/\ prefix should be silently skipped
        let lines = vec!["@@ -1,1 +1,1 @@", "index abc..def", "+added"];
        let (hunks, _) = collect_hunks(&lines, 0);
        assert_eq!(hunks[0].patch_lines.len(), 1);
        assert_eq!(hunks[0].patch_lines[0], "+added");
    }

    #[test]
    fn test_collect_hunks_empty_input() {
        let (hunks, idx) = collect_hunks(&[], 0);
        assert!(hunks.is_empty());
        assert_eq!(idx, 0);
    }

    // ========================================================================
    // resolve_file_info direct unit tests (parse:143:testing)
    // ========================================================================

    #[test]
    fn test_resolve_file_info_binary() {
        let meta = FileMetadata {
            change: FileChange::Binary,
            file_minus: String::new(),
            file_plus: String::new(),
        };
        let (status, path, old_path) = resolve_file_info("a/img.png", "b/img.png", &meta);
        assert_eq!(status, DiffFileStatus::Binary);
        assert_eq!(path, "img.png");
        assert!(old_path.is_none());
    }

    #[test]
    fn test_resolve_file_info_new_file() {
        let meta = FileMetadata {
            change: FileChange::New,
            file_minus: "/dev/null".to_string(),
            file_plus: "b/src/new.rs".to_string(),
        };
        let (status, path, old_path) = resolve_file_info("a/src/new.rs", "b/src/new.rs", &meta);
        assert_eq!(status, DiffFileStatus::Added);
        assert_eq!(path, "src/new.rs");
        assert!(old_path.is_none());
    }

    #[test]
    fn test_resolve_file_info_deleted_file_uses_a_path() {
        let meta = FileMetadata {
            change: FileChange::Deleted,
            file_minus: "a/src/old.rs".to_string(),
            file_plus: "/dev/null".to_string(),
        };
        let (status, path, old_path) = resolve_file_info("a/src/old.rs", "b/src/old.rs", &meta);
        assert_eq!(status, DiffFileStatus::Deleted);
        assert_eq!(path, "src/old.rs");
        assert!(old_path.is_none());
    }

    #[test]
    fn test_resolve_file_info_renamed_with_from() {
        let meta = FileMetadata {
            change: FileChange::Renamed {
                from: Some("src/utils/helpers.ts".to_string()),
            },
            file_minus: "a/src/utils/helpers.ts".to_string(),
            file_plus: "b/src/utils/format.ts".to_string(),
        };
        let (status, path, old_path) =
            resolve_file_info("a/src/utils/helpers.ts", "b/src/utils/format.ts", &meta);
        assert_eq!(status, DiffFileStatus::Renamed);
        assert_eq!(path, "src/utils/format.ts");
        assert_eq!(old_path.as_deref(), Some("src/utils/helpers.ts"));
    }

    #[test]
    fn test_resolve_file_info_renamed_without_from_falls_back_to_a_path() {
        // `rename to` only — no `rename from` header captured
        let meta = FileMetadata {
            change: FileChange::Renamed { from: None },
            file_minus: "a/src/old.rs".to_string(),
            file_plus: "b/src/new.rs".to_string(),
        };
        let (status, path, old_path) = resolve_file_info("a/src/old.rs", "b/src/new.rs", &meta);
        assert_eq!(status, DiffFileStatus::Renamed);
        assert_eq!(path, "src/new.rs");
        // Falls back to stripping a_path
        assert_eq!(old_path.as_deref(), Some("src/old.rs"));
    }

    #[test]
    fn test_resolve_file_info_modified_fallback_dev_null_plus() {
        // No mode header, but +++ /dev/null signals deletion
        let meta = FileMetadata {
            change: FileChange::Modified,
            file_minus: "a/src/old.rs".to_string(),
            file_plus: "/dev/null".to_string(),
        };
        let (status, path, _) = resolve_file_info("a/src/old.rs", "b/src/old.rs", &meta);
        assert_eq!(status, DiffFileStatus::Deleted);
        assert_eq!(path, "src/old.rs");
    }

    #[test]
    fn test_resolve_file_info_modified_fallback_dev_null_minus() {
        // No mode header, but --- /dev/null signals addition
        let meta = FileMetadata {
            change: FileChange::Modified,
            file_minus: "/dev/null".to_string(),
            file_plus: "b/src/new.rs".to_string(),
        };
        let (status, path, _) = resolve_file_info("a/src/new.rs", "b/src/new.rs", &meta);
        assert_eq!(status, DiffFileStatus::Added);
        assert_eq!(path, "src/new.rs");
    }

    // ========================================================================
    // parse_diff_git_header rfind edge case (parse:205:regression)
    // ========================================================================

    /// Regression test: a path where a directory component is literally named "b"
    /// (e.g. `src/b/foo.rs`). With `find` the first " b/" occurrence would split
    /// incorrectly; `rfind` always finds the true a/b separator.
    #[test]
    fn test_parse_diff_git_header_path_with_b_directory() {
        // Both sides have "src/b/" as a directory — the separator is the *last* " b/"
        let (a, b) = parse_diff_git_header("diff --git a/src/b/foo.rs b/src/b/foo.rs");
        assert_eq!(a, "a/src/b/foo.rs");
        assert_eq!(b, "b/src/b/foo.rs");
    }

    #[test]
    fn test_parse_diff_git_header_multiple_b_components() {
        // Deeper nesting: a/b/b/file.rs  b/b/b/file.rs
        let (a, b) = parse_diff_git_header("diff --git a/b/b/file.rs b/b/b/file.rs");
        assert_eq!(a, "a/b/b/file.rs");
        assert_eq!(b, "b/b/b/file.rs");
    }
}