anyrepair 0.2.4

A comprehensive Rust crate for repairing malformed structured data including JSON, YAML, XML, TOML, CSV, INI, Markdown, and Diff with format auto-detection
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
//! Comprehensive tests for diff/udiff repair functionality
//!
//! Test organization:
//! - Basic functionality tests
//! - Repair strategy tests (missing headers, incorrect prefixes, etc.)
//! - Edge case tests
//! - API tests (confidence, needs_repair, auto-detection)
//! - File-based tests (using example files)
//! - Batch/integration tests

use anyrepair::{diff::DiffRepairer, repair, Repair};
use std::fs;
use std::path::Path;

// ============================================================================
// Basic Functionality Tests
// ============================================================================

#[test]
fn test_valid_diff_passes_through() {
    let valid_diff = r#"--- a/src/main.rs
+++ b/src/main.rs
@@ -1,3 +1,3 @@
 fn main() {
-    println!("Hello");
+    println!("Hello World");
     println!("Done");
 }
"#;
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(valid_diff).unwrap();
    
    assert!(result.contains("@@"));
    assert!(result.contains("---"));
    assert!(result.contains("+++"));
    assert!(result.contains("-    println!(\"Hello\");"));
    assert!(result.contains("+    println!(\"Hello World\");"));
}

#[test]
fn test_multiple_hunks() {
    let multi_hunk = r#"--- a/file.txt
+++ b/file.txt
@@ -1,3 +1,3 @@
 line1
-line2
+line2_modified
 line3
@@ -5,2 +5,2 @@
 line5
-line6
+line6_modified
"#;
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(multi_hunk).unwrap();
    
    // Should preserve both hunks
    let hunk_count = result.lines()
        .filter(|line| line.starts_with("@@"))
        .count();
    assert_eq!(hunk_count, 2);
}

#[test]
fn test_context_lines() {
    let with_context = r#"--- a/file.txt
+++ b/file.txt
@@ -1,5 +1,5 @@
 unchanged1
 unchanged2
-old line
+new line
 unchanged3
 unchanged4
"#;
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(with_context).unwrap();
    
    // Should preserve context lines (starting with space)
    assert!(result.contains(" unchanged1"));
    assert!(result.contains(" unchanged2"));
    assert!(result.contains(" unchanged3"));
}

#[test]
fn test_hunk_with_only_additions() {
    let additions_only = r#"--- a/file.txt
+++ b/file.txt
@@ -1,0 +1,2 @@
+new line 1
+new line 2
"#;
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(additions_only).unwrap();
    
    assert!(result.contains("@@"));
    assert!(result.contains("+new line 1"));
    assert!(result.contains("+new line 2"));
}

#[test]
fn test_hunk_with_only_deletions() {
    let deletions_only = r#"--- a/file.txt
+++ b/file.txt
@@ -1,2 +1,0 @@
-old line 1
-old line 2
"#;
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(deletions_only).unwrap();
    
    assert!(result.contains("@@"));
    assert!(result.contains("-old line 1"));
    assert!(result.contains("-old line 2"));
}

#[test]
fn test_complex_diff_repair() {
    let complex = r#"--- a/src/lib.rs
+++ b/src/lib.rs
@@ -10,5 +10,5 @@
 pub fn example() {
     let x = 1;
-    let y = 2;
+    let y = 3;
     println!("{}", x + y);
 }
@@ -20,3 +20,3 @@
 pub fn another() {
-    return 1;
+    return 2;
 }
"#;
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(complex).unwrap();
    
    assert!(result.contains("@@"));
    assert!(result.contains("---"));
    assert!(result.contains("+++"));
    
    // Check both hunks are present
    let hunk_count = result.lines()
        .filter(|line| line.starts_with("@@"))
        .count();
    assert_eq!(hunk_count, 2);
}

// ============================================================================
// Repair Strategy Tests
// ============================================================================

#[test]
fn test_missing_hunk_header() {
    let malformed = r#"--- a/file.txt
+++ b/file.txt
-old line
+new line
"#;
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(malformed).unwrap();
    
    // Should add hunk header
    assert!(result.contains("@@"));
}

#[test]
fn test_missing_file_headers() {
    let malformed = r#"@@ -1,1 +1,1 @@
-old
+new
"#;
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(malformed).unwrap();
    
    // Should add file headers
    assert!(result.contains("---") || result.contains("+++"));
}

#[test]
fn test_incorrect_line_prefixes() {
    let malformed = r#"@@ -1,2 +1,2 @@
old line
new line
"#;
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(malformed).unwrap();
    
    // Lines in hunk should have proper prefixes
    let lines: Vec<&str> = result.lines().collect();
    let mut in_hunk = false;
    for line in lines {
        if line.starts_with("@@") {
            in_hunk = true;
        } else if in_hunk && !line.trim().is_empty() {
            // Should have +, -, or space prefix
            assert!(line.starts_with('+') || line.starts_with('-') || line.starts_with(' '),
                   "Line should have diff prefix: {}", line);
        }
    }
}

#[test]
fn test_malformed_hunk_range() {
    let malformed = r#"--- a/file.txt
+++ b/file.txt
@@ -1 +1 @@
-old
+new
"#;
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(malformed).unwrap();
    
    // Should fix hunk range format
    assert!(result.contains("@@"));
    // Should have proper format with commas
    let hunk_lines: Vec<&str> = result.lines()
        .filter(|line| line.starts_with("@@"))
        .collect();
    assert!(!hunk_lines.is_empty());
}

#[test]
fn test_malformed_hunk_header_numbers() {
    let malformed = r#"--- a/file.txt
+++ b/file.txt
@@ abc def ghi jkl @@
-old
+new
"#;
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(malformed).unwrap();
    
    // Should fix hunk header to have proper numbers
    let hunk_lines: Vec<&str> = result.lines()
        .filter(|line| line.starts_with("@@"))
        .collect();
    
    assert!(!hunk_lines.is_empty());
    // Should have numeric values
    for hunk in hunk_lines {
        assert!(hunk.contains(|c: char| c.is_ascii_digit()));
    }
}

#[test]
fn test_inconsistent_spacing_in_hunk_header() {
    let inconsistent = r#"--- a/file.txt
+++ b/file.txt
@@  -1,2  +1,2  @@
-old
+new
"#;
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(inconsistent).unwrap();
    
    // Should normalize spacing
    let hunk_lines: Vec<&str> = result.lines()
        .filter(|line| line.starts_with("@@"))
        .collect();
    
    for hunk in hunk_lines {
        // Should not have excessive spaces
        assert!(!hunk.contains("  "));
    }
}

#[test]
fn test_missing_newline_at_end() {
    let malformed = r#"--- a/file.txt
+++ b/file.txt
@@ -1,1 +1,1 @@
-old
+new"#;
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(malformed).unwrap();
    
    // Should end with newline
    assert!(result.ends_with('\n') || result.ends_with("\r\n"));
}

// ============================================================================
// Edge Case Tests
// ============================================================================

#[test]
fn test_empty_content() {
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair("").unwrap();
    assert_eq!(result, "");
}

#[test]
fn test_whitespace_only() {
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair("   \n  \t  ").unwrap();
    // Should handle whitespace gracefully
    assert!(!result.contains("@@"));
}

#[test]
fn test_partial_diff_with_missing_parts() {
    let partial = r#"@@ -1,2 +1,2 @@
 line1
-line2
+line2_new
"#;
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(partial).unwrap();
    
    // Should add file headers
    assert!(result.contains("---") || result.contains("+++"));
}

#[test]
fn test_udiff_format() {
    // Unified diff format test
    let udiff = r#"--- a/src/main.rs	2024-01-01 10:00:00.000000000 +0000
+++ b/src/main.rs	2024-01-01 11:00:00.000000000 +0000
@@ -1,3 +1,3 @@
 fn main() {
-    old();
+    new();
 }
"#;
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(udiff).unwrap();
    
    assert!(result.contains("@@"));
    assert!(result.contains("---"));
    assert!(result.contains("+++"));
}

#[test]
fn test_real_world_git_diff() {
    // Simulate a real git diff output
    let git_diff = r#"diff --git a/src/lib.rs b/src/lib.rs
index 1234567..abcdefg 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -10,5 +10,5 @@ pub fn example() {
     let x = 1;
-    let y = 2;
+    let y = 3;
     println!("{}", x + y);
 }
"#;
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(git_diff).unwrap();
    
    // Should preserve the unified diff part
    assert!(result.contains("@@"));
    assert!(result.contains("---"));
    assert!(result.contains("+++"));
}

// ============================================================================
// API Tests (Confidence, Needs Repair, Auto-Detection)
// ============================================================================

#[test]
fn test_auto_detection_via_repair() {
    let diff_content = r#"--- a/file.txt
+++ b/file.txt
@@ -1,1 +1,1 @@
-old
+new
"#;
    
    // Test that main repair function detects diff format
    let result = repair(diff_content).unwrap();
    assert!(result.contains("@@"));
}

#[test]
fn test_confidence_scoring() {
    let repairer = DiffRepairer::new();
    
    // Valid diff should have high confidence
    let valid = r#"--- a/file.txt
+++ b/file.txt
@@ -1,1 +1,1 @@
-old
+new
"#;
    let confidence = repairer.confidence(valid);
    assert!(confidence > 0.5);
    
    // Invalid diff should have lower confidence
    let invalid = "just some text";
    let low_confidence = repairer.confidence(invalid);
    assert!(low_confidence < confidence);
}

#[test]
fn test_needs_repair() {
    let repairer = DiffRepairer::new();
    
    // Valid diff should not need repair
    let valid = r#"--- a/file.txt
+++ b/file.txt
@@ -1,1 +1,1 @@
-old
+new
"#;
    assert!(!repairer.needs_repair(valid));
    
    // Invalid diff should need repair
    let invalid = r#"--- a/file.txt
+++ b/file.txt
old line
new line
"#;
    assert!(repairer.needs_repair(invalid));
}

// ============================================================================
// File-Based Tests - Sample Files
// ============================================================================

#[test]
fn test_repair_sample_diff() {
    let sample_path = Path::new("examples/data/diff/sample/sample.diff");
    if !sample_path.exists() {
        // Skip if examples directory not available (e.g., in published crate)
        return;
    }
    
    let content = fs::read_to_string(sample_path)
        .expect("Failed to read sample.diff");
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(&content).unwrap();
    
    // Valid diff should pass through with minimal changes
    assert!(result.contains("@@"));
    assert!(result.contains("---"));
    assert!(result.contains("+++"));
}

// ============================================================================
// File-Based Tests - Malformed Files
// ============================================================================

#[test]
fn test_repair_malformed_diff() {
    let malformed_path = Path::new("examples/data/diff/malformed/malformed.diff");
    if !malformed_path.exists() {
        return;
    }
    
    let content = fs::read_to_string(malformed_path)
        .expect("Failed to read malformed.diff");
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(&content).unwrap();
    
    // Should add file headers
    assert!(result.contains("---") || result.contains("+++"));
    assert!(result.contains("@@"));
}

#[test]
fn test_repair_missing_hunk_header() {
    let malformed_path = Path::new("examples/data/diff/malformed/malformed_missing_hunk_header.diff");
    if !malformed_path.exists() {
        return;
    }
    
    let content = fs::read_to_string(malformed_path)
        .expect("Failed to read malformed_missing_hunk_header.diff");
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(&content).unwrap();
    
    // Should add hunk header
    assert!(result.contains("@@"));
    assert!(result.contains("---"));
    assert!(result.contains("+++"));
}

#[test]
fn test_repair_missing_file_headers() {
    let malformed_path = Path::new("examples/data/diff/malformed/malformed_missing_file_headers.diff");
    if !malformed_path.exists() {
        return;
    }
    
    let content = fs::read_to_string(malformed_path)
        .expect("Failed to read malformed_missing_file_headers.diff");
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(&content).unwrap();
    
    // Should add file headers
    assert!(result.contains("---"));
    assert!(result.contains("+++"));
    assert!(result.contains("@@"));
}

#[test]
fn test_repair_incorrect_prefixes() {
    let malformed_path = Path::new("examples/data/diff/malformed/malformed_incorrect_prefixes.diff");
    if !malformed_path.exists() {
        return;
    }
    
    let content = fs::read_to_string(malformed_path)
        .expect("Failed to read malformed_incorrect_prefixes.diff");
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(&content).unwrap();
    
    // Should fix line prefixes
    let lines: Vec<&str> = result.lines().collect();
    let mut in_hunk = false;
    for line in lines {
        if line.starts_with("@@") {
            in_hunk = true;
        } else if in_hunk && !line.trim().is_empty() {
            // Lines in hunk should have proper prefixes
            assert!(line.starts_with('+') || line.starts_with('-') || 
                   line.starts_with(' ') || line.starts_with("---") || 
                   line.starts_with("+++"),
                   "Line should have diff prefix: {}", line);
        }
    }
}

#[test]
fn test_repair_malformed_hunk_range() {
    let malformed_path = Path::new("examples/data/diff/malformed/malformed_malformed_hunk_range.diff");
    if !malformed_path.exists() {
        return;
    }
    
    let content = fs::read_to_string(malformed_path)
        .expect("Failed to read malformed_malformed_hunk_range.diff");
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(&content).unwrap();
    
    // Should fix hunk range to have proper numbers
    let hunk_lines: Vec<&str> = result.lines()
        .filter(|line| line.starts_with("@@"))
        .collect();
    
    assert!(!hunk_lines.is_empty());
    for hunk in hunk_lines {
        // Should contain numeric values
        assert!(hunk.contains(|c: char| c.is_ascii_digit()));
    }
}

#[test]
fn test_repair_inconsistent_spacing() {
    let malformed_path = Path::new("examples/data/diff/malformed/malformed_inconsistent_spacing.diff");
    if !malformed_path.exists() {
        return;
    }
    
    let content = fs::read_to_string(malformed_path)
        .expect("Failed to read malformed_inconsistent_spacing.diff");
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(&content).unwrap();
    
    // Should normalize spacing
    let hunk_lines: Vec<&str> = result.lines()
        .filter(|line| line.starts_with("@@"))
        .collect();
    
    for hunk in hunk_lines {
        // Should not have excessive spaces
        assert!(!hunk.contains("  "), "Hunk should not have double spaces: {}", hunk);
    }
}

#[test]
fn test_repair_missing_newline() {
    let malformed_path = Path::new("examples/data/diff/malformed/malformed_missing_newline.diff");
    if !malformed_path.exists() {
        return;
    }
    
    let content = fs::read_to_string(malformed_path)
        .expect("Failed to read malformed_missing_newline.diff");
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(&content).unwrap();
    
    // Should end with newline
    assert!(result.ends_with('\n') || result.ends_with("\r\n"));
}

// ============================================================================
// File-Based Tests - Complex Files
// ============================================================================

#[test]
fn test_repair_complex_diff() {
    let malformed_path = Path::new("examples/data/diff/complex/malformed_complex.diff");
    if !malformed_path.exists() {
        return;
    }
    
    let content = fs::read_to_string(malformed_path)
        .expect("Failed to read malformed_complex.diff");
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(&content).unwrap();
    
    // Should repair complex diff with multiple hunks
    assert!(result.contains("@@"));
    assert!(result.contains("---"));
    assert!(result.contains("+++"));
    
    // Should have multiple hunks
    let hunk_count = result.lines()
        .filter(|line| line.starts_with("@@"))
        .count();
    assert!(hunk_count >= 2);
}

#[test]
fn test_repair_multi_file_complex_diff() {
    let multi_file_path = Path::new("examples/data/diff/complex/multi_file_complex.diff");
    if !multi_file_path.exists() {
        return;
    }
    
    let content = fs::read_to_string(multi_file_path)
        .expect("Failed to read multi_file_complex.diff");
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(&content).unwrap();
    
    // Should handle multi-file diff
    assert!(result.contains("@@"));
    assert!(result.contains("diff --git"));
    
    // Should have multiple file sections
    let file_header_count = result.lines()
        .filter(|line| line.starts_with("---") || line.starts_with("+++"))
        .count();
    assert!(file_header_count >= 4); // At least 2 files (2 headers each)
}

#[test]
fn test_repair_large_hunk_complex_diff() {
    let large_hunk_path = Path::new("examples/data/diff/complex/large_hunk_complex.diff");
    if !large_hunk_path.exists() {
        return;
    }
    
    let content = fs::read_to_string(large_hunk_path)
        .expect("Failed to read large_hunk_complex.diff");
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(&content).unwrap();
    
    // Should handle large hunk
    assert!(result.contains("@@"));
    assert!(result.contains("---"));
    assert!(result.contains("+++"));
    
    // Large hunk should have substantial changes
    let added_lines = result.lines()
        .filter(|line| line.starts_with('+') && !line.starts_with("+++"))
        .count();
    let removed_lines = result.lines()
        .filter(|line| line.starts_with('-') && !line.starts_with("---"))
        .count();
    
    assert!(added_lines > 10 || removed_lines > 10, 
           "Large hunk should have substantial changes");
}

#[test]
fn test_repair_mixed_changes_complex_diff() {
    let mixed_path = Path::new("examples/data/diff/complex/mixed_changes_complex.diff");
    if !mixed_path.exists() {
        return;
    }
    
    let content = fs::read_to_string(mixed_path)
        .expect("Failed to read mixed_changes_complex.diff");
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(&content).unwrap();
    
    // Should handle mixed additions and deletions
    assert!(result.contains("@@"));
    
    // Should have both additions and deletions
    let has_additions = result.lines().any(|line| line.starts_with('+') && !line.starts_with("+++"));
    let has_deletions = result.lines().any(|line| line.starts_with('-') && !line.starts_with("---"));
    
    assert!(has_additions || has_deletions, 
           "Mixed changes diff should have additions or deletions");
}

#[test]
fn test_repair_real_world_git_diff() {
    let git_path = Path::new("examples/data/diff/complex/real_world_git.diff");
    if !git_path.exists() {
        return;
    }
    
    let content = fs::read_to_string(git_path)
        .expect("Failed to read real_world_git.diff");
    
    let mut repairer = DiffRepairer::new();
    let result = repairer.repair(&content).unwrap();
    
    // Should handle real git diff format
    // Note: repairer may normalize the format, so we check for key elements
    assert!(result.contains("@@"), "Should contain hunk headers");
    
    // Should have file headers (--- and +++)
    let file_header_count = result.lines()
        .filter(|line| line.starts_with("---") || line.starts_with("+++"))
        .count();
    assert!(file_header_count >= 2, "Should have at least one file with headers");
    
    // Should have hunks
    let hunk_count = result.lines()
        .filter(|line| line.starts_with("@@"))
        .count();
    assert!(hunk_count > 0, "Should have at least one hunk");
}

// ============================================================================
// Batch/Integration Tests
// ============================================================================

#[test]
fn test_repair_all_malformed_diffs() {
    // Test all malformed diff files in examples/data/diff
    let malformed_files = vec![
        ("malformed", "malformed.diff"),
        ("malformed", "malformed_missing_hunk_header.diff"),
        ("malformed", "malformed_missing_file_headers.diff"),
        ("malformed", "malformed_incorrect_prefixes.diff"),
        ("malformed", "malformed_malformed_hunk_range.diff"),
        ("malformed", "malformed_inconsistent_spacing.diff"),
        ("malformed", "malformed_missing_newline.diff"),
        ("complex", "malformed_complex.diff"),
        ("complex", "multi_file_complex.diff"),
        ("complex", "large_hunk_complex.diff"),
        ("complex", "mixed_changes_complex.diff"),
        ("complex", "real_world_git.diff"),
    ];
    
    for (subdir, filename) in malformed_files {
        let file_path = Path::new("examples/data/diff").join(subdir).join(filename);
        if !file_path.exists() {
            continue;
        }
        
        let content = fs::read_to_string(&file_path)
            .unwrap_or_else(|_| panic!("Failed to read {}", filename));
        
        let mut repairer = DiffRepairer::new();
        let result = repairer.repair(&content);
        
        assert!(result.is_ok(), "Failed to repair {}", filename);
        
        let repaired = result.unwrap();
        // Repaired content should have at least a hunk header or file headers
        assert!(
            repaired.contains("@@") || repaired.contains("---") || repaired.contains("+++"),
            "Repaired {} should contain diff markers", filename
        );
    }
}