diffguard-diff 0.2.0

Unified diff parser for diffguard
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
//! Property-based tests for diffguard-diff
//!
//! Feature: diffguard-completion
//!
//! These tests verify the enhanced diff parsing functionality for handling
//! special cases like binary files, submodules, renames, and malformed content.
//!
//! Feature: comprehensive-test-coverage
//!
//! These tests also verify diff parsing consistency and correctness properties.

use proptest::prelude::*;

use diffguard_diff::parse_unified_diff;
use diffguard_types::Scope;

// ============================================================================
// Strategies for generating diff content
// ============================================================================

/// Strategy to generate valid file paths (alphanumeric with slashes)
fn file_path_strategy() -> impl Strategy<Value = String> {
    prop::collection::vec(
        prop::string::string_regex("[a-zA-Z][a-zA-Z0-9_]{0,15}").expect("valid regex"),
        1..4,
    )
    .prop_map(|parts| parts.join("/"))
    .prop_filter("path must not be empty", |p| !p.is_empty())
}

/// Strategy to generate file extensions
fn extension_strategy() -> impl Strategy<Value = String> {
    prop::sample::select(vec![
        "rs", "py", "js", "ts", "go", "java", "rb", "c", "cpp", "txt", "md",
    ])
    .prop_map(|s| s.to_string())
}

/// Strategy to generate a full file path with extension
fn full_path_strategy() -> impl Strategy<Value = String> {
    (file_path_strategy(), extension_strategy()).prop_map(|(path, ext)| format!("{}.{}", path, ext))
}

/// Strategy to generate valid line content (no special diff characters at start)
fn line_content_strategy() -> impl Strategy<Value = String> {
    prop::string::string_regex("[a-zA-Z0-9_(){}\\[\\];:,.<>=+\\-*/& ]{0,80}")
        .expect("valid regex")
        .prop_filter("must not start with diff markers", |s| {
            !s.starts_with('+')
                && !s.starts_with('-')
                && !s.starts_with('@')
                && !s.starts_with(' ')
                && !s.starts_with('\\')
        })
}

/// Strategy to generate a valid hunk header
fn hunk_header_strategy(new_start: u32, new_count: u32) -> String {
    format!("@@ -1,1 +{},{} @@", new_start, new_count)
}

/// Strategy to generate git commit hashes (40 hex chars)
fn commit_hash_strategy() -> impl Strategy<Value = String> {
    prop::string::string_regex("[0-9a-f]{40}").expect("valid regex")
}

/// Strategy to generate file modes
fn file_mode_strategy() -> impl Strategy<Value = String> {
    prop::sample::select(vec!["100644", "100755", "120000", "160000"]).prop_map(|s| s.to_string())
}

// ============================================================================
// Helper functions to generate diff content
// ============================================================================

/// Generate a standard diff header for a file
fn make_diff_header(path: &str) -> String {
    format!(
        "diff --git a/{path} b/{path}\n\
         index 0000000..1111111 100644\n\
         --- a/{path}\n\
         +++ b/{path}",
        path = path
    )
}

/// Generate a diff with added lines
fn make_diff_with_added_lines(path: &str, lines: &[&str]) -> String {
    let header = make_diff_header(path);
    let hunk = hunk_header_strategy(1, lines.len() as u32);
    let content: String = lines.iter().map(|l| format!("+{}\n", l)).collect();
    format!("{}\n{}\n{}", header, hunk, content)
}

/// Generate a binary file diff
fn make_binary_diff(path: &str) -> String {
    format!(
        "diff --git a/{path} b/{path}\n\
         index 0000000..1111111 100644\n\
         Binary files a/{path} and b/{path} differ",
        path = path
    )
}

/// Generate a submodule diff
fn make_submodule_diff(path: &str, old_commit: &str, new_commit: &str) -> String {
    format!(
        "diff --git a/{path} b/{path}\n\
         index {old_short}..{new_short} 160000\n\
         --- a/{path}\n\
         +++ b/{path}\n\
         @@ -1 +1 @@\n\
         -Subproject commit {old_commit}\n\
         +Subproject commit {new_commit}",
        path = path,
        old_short = &old_commit[..7],
        new_short = &new_commit[..7],
        old_commit = old_commit,
        new_commit = new_commit
    )
}

/// Generate a deleted file diff
fn make_deleted_file_diff(path: &str, mode: &str) -> String {
    format!(
        "diff --git a/{path} b/{path}\n\
         deleted file mode {mode}\n\
         index 1111111..0000000\n\
         --- a/{path}\n\
         +++ /dev/null\n\
         @@ -1,1 +0,0 @@\n\
         -fn deleted() {{}}",
        path = path,
        mode = mode
    )
}

/// Generate a mode-only change diff
fn make_mode_change_diff(path: &str, old_mode: &str, new_mode: &str) -> String {
    format!(
        "diff --git a/{path} b/{path}\n\
         old mode {old_mode}\n\
         new mode {new_mode}",
        path = path,
        old_mode = old_mode,
        new_mode = new_mode
    )
}

/// Generate a rename diff with content changes
fn make_rename_diff(old_path: &str, new_path: &str, added_lines: &[&str]) -> String {
    let hunk = hunk_header_strategy(1, added_lines.len() as u32 + 1);
    let content: String = added_lines.iter().map(|l| format!("+{}\n", l)).collect();
    format!(
        "diff --git a/{old_path} b/{new_path}\n\
         similarity index 90%\n\
         rename from {old_path}\n\
         rename to {new_path}\n\
         --- a/{old_path}\n\
         +++ b/{new_path}\n\
         {hunk}\n\
         fn existing() {{}}\n\
         {content}",
        old_path = old_path,
        new_path = new_path,
        hunk = hunk,
        content = content
    )
}

// ============================================================================
// Property 3: Diff Parsing Consistency
// ============================================================================
//
// Feature: comprehensive-test-coverage, Property 3: Diff Parsing Consistency
// For any well-formed unified diff string, calling `parse_unified_diff` twice
// with the same scope SHALL return identical results (same DiffLines in same order).
// **Validates: Requirements 2.1**

proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    // Feature: comprehensive-test-coverage, Property 3: Diff Parsing Consistency
    // Parsing the same diff twice with Scope::Added should return identical results
    // **Validates: Requirements 2.1**
    #[test]
    fn property_parse_consistency_added_scope(
        path in full_path_strategy(),
        lines in prop::collection::vec(line_content_strategy(), 1..5),
    ) {
        // Filter out empty lines
        let non_empty_lines: Vec<&str> = lines.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();

        prop_assume!(!non_empty_lines.is_empty());

        // Create a well-formed diff
        let diff = make_diff_with_added_lines(&path, &non_empty_lines);

        // Parse the diff twice with the same scope
        let result1 = parse_unified_diff(&diff, Scope::Added);
        let result2 = parse_unified_diff(&diff, Scope::Added);

        // Both parses should succeed
        prop_assert!(
            result1.is_ok(),
            "First parse should succeed, but got error: {:?}",
            result1.err()
        );
        prop_assert!(
            result2.is_ok(),
            "Second parse should succeed, but got error: {:?}",
            result2.err()
        );

        let (lines1, stats1) = result1.unwrap();
        let (lines2, stats2) = result2.unwrap();

        // Property: Both parses should return the same number of lines
        prop_assert_eq!(
            lines1.len(),
            lines2.len(),
            "Both parses should return the same number of lines, but got {} vs {}",
            lines1.len(),
            lines2.len()
        );

        // Property: Both parses should return identical DiffStats
        prop_assert_eq!(
            stats1.files,
            stats2.files,
            "Both parses should return the same file count, but got {} vs {}",
            stats1.files,
            stats2.files
        );
        prop_assert_eq!(
            stats1.lines,
            stats2.lines,
            "Both parses should return the same line count, but got {} vs {}",
            stats1.lines,
            stats2.lines
        );

        // Property: Both parses should return lines in the same order with identical content
        for (i, (line1, line2)) in lines1.iter().zip(lines2.iter()).enumerate() {
            prop_assert_eq!(
                &line1.path,
                &line2.path,
                "Line {} should have the same path, but got '{}' vs '{}'",
                i,
                line1.path,
                line2.path
            );
            prop_assert_eq!(
                line1.line,
                line2.line,
                "Line {} should have the same line number, but got {} vs {}",
                i,
                line1.line,
                line2.line
            );
            prop_assert_eq!(
                &line1.content,
                &line2.content,
                "Line {} should have the same content, but got '{}' vs '{}'",
                i,
                line1.content,
                line2.content
            );
        }
    }

    // Feature: comprehensive-test-coverage, Property 3: Diff Parsing Consistency
    // Parsing the same diff twice with Scope::Changed should return identical results
    // **Validates: Requirements 2.1**
    #[test]
    fn property_parse_consistency_changed_scope(
        path in full_path_strategy(),
        lines in prop::collection::vec(line_content_strategy(), 1..5),
    ) {
        // Filter out empty lines
        let non_empty_lines: Vec<&str> = lines.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();

        prop_assume!(!non_empty_lines.is_empty());

        // Create a well-formed diff
        let diff = make_diff_with_added_lines(&path, &non_empty_lines);

        // Parse the diff twice with the same scope
        let result1 = parse_unified_diff(&diff, Scope::Changed);
        let result2 = parse_unified_diff(&diff, Scope::Changed);

        // Both parses should succeed
        prop_assert!(
            result1.is_ok(),
            "First parse should succeed, but got error: {:?}",
            result1.err()
        );
        prop_assert!(
            result2.is_ok(),
            "Second parse should succeed, but got error: {:?}",
            result2.err()
        );

        let (lines1, stats1) = result1.unwrap();
        let (lines2, stats2) = result2.unwrap();

        // Property: Both parses should return the same number of lines
        prop_assert_eq!(
            lines1.len(),
            lines2.len(),
            "Both parses should return the same number of lines, but got {} vs {}",
            lines1.len(),
            lines2.len()
        );

        // Property: Both parses should return identical DiffStats
        prop_assert_eq!(
            stats1.files,
            stats2.files,
            "Both parses should return the same file count, but got {} vs {}",
            stats1.files,
            stats2.files
        );
        prop_assert_eq!(
            stats1.lines,
            stats2.lines,
            "Both parses should return the same line count, but got {} vs {}",
            stats1.lines,
            stats2.lines
        );

        // Property: Both parses should return lines in the same order with identical content
        for (i, (line1, line2)) in lines1.iter().zip(lines2.iter()).enumerate() {
            prop_assert_eq!(
                &line1.path,
                &line2.path,
                "Line {} should have the same path, but got '{}' vs '{}'",
                i,
                line1.path,
                line2.path
            );
            prop_assert_eq!(
                line1.line,
                line2.line,
                "Line {} should have the same line number, but got {} vs {}",
                i,
                line1.line,
                line2.line
            );
            prop_assert_eq!(
                &line1.content,
                &line2.content,
                "Line {} should have the same content, but got '{}' vs '{}'",
                i,
                line1.content,
                line2.content
            );
        }
    }

    // Feature: comprehensive-test-coverage, Property 3: Diff Parsing Consistency
    // Parsing a multi-file diff twice should return identical results
    // **Validates: Requirements 2.1**
    #[test]
    fn property_modified_scope_matches_changed_scope(
        path in full_path_strategy(),
        removed_lines in prop::collection::vec(line_content_strategy(), 1..3),
        added_lines in prop::collection::vec(line_content_strategy(), 1..3),
    ) {
        let non_empty_removed: Vec<&str> = removed_lines.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();
        let non_empty_added: Vec<&str> = added_lines.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();

        prop_assume!(!non_empty_removed.is_empty());
        prop_assume!(!non_empty_added.is_empty());

        let diff = make_changed_diff(&path, &non_empty_removed, &non_empty_added);
        let changed = parse_unified_diff(&diff, Scope::Changed);
        let modified = parse_unified_diff(&diff, Scope::Modified);

        prop_assert!(changed.is_ok(), "Changed parse should succeed");
        prop_assert!(modified.is_ok(), "Modified parse should succeed");
        prop_assert_eq!(changed.unwrap(), modified.unwrap());
    }

    // Feature: comprehensive-test-coverage, Property 3: Diff Parsing Consistency
    // Parsing a multi-file diff twice should return identical results
    // **Validates: Requirements 2.1**
    #[test]
    fn property_parse_consistency_multi_file(
        path1 in full_path_strategy(),
        path2 in full_path_strategy(),
        lines1 in prop::collection::vec(line_content_strategy(), 1..3),
        lines2 in prop::collection::vec(line_content_strategy(), 1..3),
    ) {
        // Ensure paths are different
        prop_assume!(path1 != path2);

        // Filter out empty lines
        let non_empty_lines1: Vec<&str> = lines1.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();
        let non_empty_lines2: Vec<&str> = lines2.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();

        prop_assume!(!non_empty_lines1.is_empty());
        prop_assume!(!non_empty_lines2.is_empty());

        // Create a well-formed multi-file diff
        let diff1 = make_diff_with_added_lines(&path1, &non_empty_lines1);
        let diff2 = make_diff_with_added_lines(&path2, &non_empty_lines2);
        let combined = format!("{}\n{}", diff1, diff2);

        // Parse the diff twice with the same scope
        let result1 = parse_unified_diff(&combined, Scope::Added);
        let result2 = parse_unified_diff(&combined, Scope::Added);

        // Both parses should succeed
        prop_assert!(
            result1.is_ok(),
            "First parse should succeed, but got error: {:?}",
            result1.err()
        );
        prop_assert!(
            result2.is_ok(),
            "Second parse should succeed, but got error: {:?}",
            result2.err()
        );

        let (parsed_lines1, stats1) = result1.unwrap();
        let (parsed_lines2, stats2) = result2.unwrap();

        // Property: Both parses should return the same number of lines
        prop_assert_eq!(
            parsed_lines1.len(),
            parsed_lines2.len(),
            "Both parses should return the same number of lines, but got {} vs {}",
            parsed_lines1.len(),
            parsed_lines2.len()
        );

        // Property: Both parses should return identical DiffStats
        prop_assert_eq!(
            stats1.files,
            stats2.files,
            "Both parses should return the same file count, but got {} vs {}",
            stats1.files,
            stats2.files
        );
        prop_assert_eq!(
            stats1.lines,
            stats2.lines,
            "Both parses should return the same line count, but got {} vs {}",
            stats1.lines,
            stats2.lines
        );

        // Property: Both parses should return lines in the same order with identical content
        for (i, (line1, line2)) in parsed_lines1.iter().zip(parsed_lines2.iter()).enumerate() {
            prop_assert_eq!(
                &line1.path,
                &line2.path,
                "Line {} should have the same path, but got '{}' vs '{}'",
                i,
                line1.path,
                line2.path
            );
            prop_assert_eq!(
                line1.line,
                line2.line,
                "Line {} should have the same line number, but got {} vs {}",
                i,
                line1.line,
                line2.line
            );
            prop_assert_eq!(
                &line1.content,
                &line2.content,
                "Line {} should have the same content, but got '{}' vs '{}'",
                i,
                line1.content,
                line2.content
            );
        }
    }
}

// ============================================================================
// Property 6: Diff Parser Skips Special Files
// ============================================================================
//
// Feature: diffguard-completion, Property 6: Diff Parser Skips Special Files
// For any unified diff containing binary file markers, submodule commits,
// mode-only changes, or deleted file markers, the `parse_unified_diff` function
// SHALL return successfully with no lines extracted from those special files.
// **Validates: Requirements 4.1, 4.2, 4.4, 4.5**

proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    // Feature: diffguard-completion, Property 6: Diff Parser Skips Special Files
    // Binary files should be skipped
    // **Validates: Requirements 4.1**
    #[test]
    fn property_binary_files_skipped(
        binary_path in full_path_strategy(),
        normal_path in full_path_strategy(),
        line_content in line_content_strategy(),
    ) {
        // Ensure paths are different
        prop_assume!(binary_path != normal_path);
        prop_assume!(!line_content.is_empty());

        // Create a diff with a binary file followed by a normal file
        let binary_diff = make_binary_diff(&binary_path);
        let normal_diff = make_diff_with_added_lines(&normal_path, &[&line_content]);
        let combined = format!("{}\n{}", binary_diff, normal_diff);

        let result = parse_unified_diff(&combined, Scope::Added);

        // Property: parsing should succeed
        prop_assert!(
            result.is_ok(),
            "Parsing should succeed, but got error: {:?}",
            result.err()
        );

        let (lines, _stats) = result.unwrap();

        // Property: no lines should be extracted from the binary file
        let binary_lines: Vec<_> = lines.iter().filter(|l| l.path == binary_path).collect();
        prop_assert!(
            binary_lines.is_empty(),
            "No lines should be extracted from binary file '{}', but found {:?}",
            binary_path,
            binary_lines
        );

        // Property: lines from normal file should still be extracted
        let normal_lines: Vec<_> = lines.iter().filter(|l| l.path == normal_path).collect();
        prop_assert!(
            !normal_lines.is_empty(),
            "Lines should be extracted from normal file '{}', but found none",
            normal_path
        );
    }


    // Feature: diffguard-completion, Property 6: Diff Parser Skips Special Files
    // Submodule changes should be skipped
    // **Validates: Requirements 4.2**
    #[test]
    fn property_submodule_changes_skipped(
        submodule_path in full_path_strategy(),
        normal_path in full_path_strategy(),
        old_commit in commit_hash_strategy(),
        new_commit in commit_hash_strategy(),
        line_content in line_content_strategy(),
    ) {
        // Ensure paths are different and commits are different
        prop_assume!(submodule_path != normal_path);
        prop_assume!(old_commit != new_commit);
        prop_assume!(!line_content.is_empty());

        // Create a diff with a submodule change followed by a normal file
        let submodule_diff = make_submodule_diff(&submodule_path, &old_commit, &new_commit);
        let normal_diff = make_diff_with_added_lines(&normal_path, &[&line_content]);
        let combined = format!("{}\n{}", submodule_diff, normal_diff);

        let result = parse_unified_diff(&combined, Scope::Added);

        // Property: parsing should succeed
        prop_assert!(
            result.is_ok(),
            "Parsing should succeed, but got error: {:?}",
            result.err()
        );

        let (lines, _stats) = result.unwrap();

        // Property: no lines should be extracted from the submodule
        let submodule_lines: Vec<_> = lines.iter().filter(|l| l.path == submodule_path).collect();
        prop_assert!(
            submodule_lines.is_empty(),
            "No lines should be extracted from submodule '{}', but found {:?}",
            submodule_path,
            submodule_lines
        );

        // Property: lines from normal file should still be extracted
        let normal_lines: Vec<_> = lines.iter().filter(|l| l.path == normal_path).collect();
        prop_assert!(
            !normal_lines.is_empty(),
            "Lines should be extracted from normal file '{}', but found none",
            normal_path
        );
    }


    // Feature: diffguard-completion, Property 6: Diff Parser Skips Special Files
    // Deleted files should be skipped
    // **Validates: Requirements 4.5**
    #[test]
    fn property_deleted_files_skipped(
        deleted_path in full_path_strategy(),
        normal_path in full_path_strategy(),
        mode in file_mode_strategy(),
        line_content in line_content_strategy(),
    ) {
        // Ensure paths are different
        prop_assume!(deleted_path != normal_path);
        prop_assume!(!line_content.is_empty());

        // Create a diff with a deleted file followed by a normal file
        let deleted_diff = make_deleted_file_diff(&deleted_path, &mode);
        let normal_diff = make_diff_with_added_lines(&normal_path, &[&line_content]);
        let combined = format!("{}\n{}", deleted_diff, normal_diff);

        let result = parse_unified_diff(&combined, Scope::Added);

        // Property: parsing should succeed
        prop_assert!(
            result.is_ok(),
            "Parsing should succeed, but got error: {:?}",
            result.err()
        );

        let (lines, _stats) = result.unwrap();

        // Property: no lines should be extracted from the deleted file
        let deleted_lines: Vec<_> = lines.iter().filter(|l| l.path == deleted_path).collect();
        prop_assert!(
            deleted_lines.is_empty(),
            "No lines should be extracted from deleted file '{}', but found {:?}",
            deleted_path,
            deleted_lines
        );

        // Property: lines from normal file should still be extracted
        let normal_lines: Vec<_> = lines.iter().filter(|l| l.path == normal_path).collect();
        prop_assert!(
            !normal_lines.is_empty(),
            "Lines should be extracted from normal file '{}', but found none",
            normal_path
        );
    }


    // Feature: diffguard-completion, Property 6: Diff Parser Skips Special Files
    // Mode-only changes should be skipped
    // **Validates: Requirements 4.4**
    #[test]
    fn property_mode_only_changes_skipped(
        mode_path in full_path_strategy(),
        normal_path in full_path_strategy(),
        line_content in line_content_strategy(),
    ) {
        // Ensure paths are different
        prop_assume!(mode_path != normal_path);
        prop_assume!(!line_content.is_empty());

        // Create a diff with a mode-only change followed by a normal file
        let mode_diff = make_mode_change_diff(&mode_path, "100644", "100755");
        let normal_diff = make_diff_with_added_lines(&normal_path, &[&line_content]);
        let combined = format!("{}\n{}", mode_diff, normal_diff);

        let result = parse_unified_diff(&combined, Scope::Added);

        // Property: parsing should succeed
        prop_assert!(
            result.is_ok(),
            "Parsing should succeed, but got error: {:?}",
            result.err()
        );

        let (lines, _stats) = result.unwrap();

        // Property: no lines should be extracted from the mode-only change
        let mode_lines: Vec<_> = lines.iter().filter(|l| l.path == mode_path).collect();
        prop_assert!(
            mode_lines.is_empty(),
            "No lines should be extracted from mode-only change '{}', but found {:?}",
            mode_path,
            mode_lines
        );

        // Property: lines from normal file should still be extracted
        let normal_lines: Vec<_> = lines.iter().filter(|l| l.path == normal_path).collect();
        prop_assert!(
            !normal_lines.is_empty(),
            "Lines should be extracted from normal file '{}', but found none",
            normal_path
        );
    }


    // Feature: diffguard-completion, Property 6: Diff Parser Skips Special Files
    // Combined test: multiple special file types in one diff
    // **Validates: Requirements 4.1, 4.2, 4.4, 4.5**
    #[test]
    fn property_multiple_special_files_all_skipped(
        binary_path in full_path_strategy(),
        deleted_path in full_path_strategy(),
        mode_path in full_path_strategy(),
        normal_path in full_path_strategy(),
        mode in file_mode_strategy(),
        line_content in line_content_strategy(),
    ) {
        // Ensure all paths are different
        prop_assume!(binary_path != deleted_path);
        prop_assume!(binary_path != mode_path);
        prop_assume!(binary_path != normal_path);
        prop_assume!(deleted_path != mode_path);
        prop_assume!(deleted_path != normal_path);
        prop_assume!(mode_path != normal_path);
        prop_assume!(!line_content.is_empty());

        // Create a diff with multiple special files followed by a normal file
        let binary_diff = make_binary_diff(&binary_path);
        let deleted_diff = make_deleted_file_diff(&deleted_path, &mode);
        let mode_diff = make_mode_change_diff(&mode_path, "100644", "100755");
        let normal_diff = make_diff_with_added_lines(&normal_path, &[&line_content]);
        let combined = format!(
            "{}\n{}\n{}\n{}",
            binary_diff, deleted_diff, mode_diff, normal_diff
        );

        let result = parse_unified_diff(&combined, Scope::Added);

        // Property: parsing should succeed
        prop_assert!(
            result.is_ok(),
            "Parsing should succeed, but got error: {:?}",
            result.err()
        );

        let (lines, _stats) = result.unwrap();

        // Property: no lines from any special file
        for special_path in [&binary_path, &deleted_path, &mode_path] {
            let special_lines: Vec<_> = lines.iter().filter(|l| &l.path == special_path).collect();
            prop_assert!(
                special_lines.is_empty(),
                "No lines should be extracted from special file '{}', but found {:?}",
                special_path,
                special_lines
            );
        }

        // Property: lines from normal file should still be extracted
        let normal_lines: Vec<_> = lines.iter().filter(|l| l.path == normal_path).collect();
        prop_assert!(
            !normal_lines.is_empty(),
            "Lines should be extracted from normal file '{}', but found none",
            normal_path
        );
    }
}

// ============================================================================
// Property 7: Diff Parser Handles Renames
// ============================================================================
//
// Feature: diffguard-completion, Property 7: Diff Parser Handles Renames
// For any unified diff containing a file rename, the `parse_unified_diff` function
// SHALL use the new (destination) path for all extracted lines from that file.
// **Validates: Requirements 4.3**

proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    // Feature: diffguard-completion, Property 7: Diff Parser Handles Renames
    // Renamed files should use the new path
    // **Validates: Requirements 4.3**
    #[test]
    fn property_renamed_files_use_new_path(
        old_path in full_path_strategy(),
        new_path in full_path_strategy(),
        line_content in line_content_strategy(),
    ) {
        // Ensure paths are different
        prop_assume!(old_path != new_path);
        prop_assume!(!line_content.is_empty());

        // Create a rename diff with added content
        let rename_diff = make_rename_diff(&old_path, &new_path, &[&line_content]);

        let result = parse_unified_diff(&rename_diff, Scope::Added);

        // Property: parsing should succeed
        prop_assert!(
            result.is_ok(),
            "Parsing should succeed, but got error: {:?}",
            result.err()
        );

        let (lines, _stats) = result.unwrap();

        // Property: no lines should use the old path
        let old_path_lines: Vec<_> = lines.iter().filter(|l| l.path == old_path).collect();
        prop_assert!(
            old_path_lines.is_empty(),
            "No lines should use old path '{}', but found {:?}",
            old_path,
            old_path_lines
        );

        // Property: all extracted lines should use the new path
        for line in &lines {
            prop_assert_eq!(
                &line.path,
                &new_path,
                "All lines should use new path '{}', but found line with path '{}'",
                new_path,
                line.path
            );
        }
    }


    // Feature: diffguard-completion, Property 7: Diff Parser Handles Renames
    // Renamed files with multiple added lines should all use the new path
    // **Validates: Requirements 4.3**
    #[test]
    fn property_renamed_files_multiple_lines_use_new_path(
        old_path in full_path_strategy(),
        new_path in full_path_strategy(),
        line1 in line_content_strategy(),
        line2 in line_content_strategy(),
        line3 in line_content_strategy(),
    ) {
        // Ensure paths are different and we have content
        prop_assume!(old_path != new_path);
        prop_assume!(!line1.is_empty() || !line2.is_empty() || !line3.is_empty());

        // Filter out empty lines
        let lines: Vec<&str> = [line1.as_str(), line2.as_str(), line3.as_str()]
            .into_iter()
            .filter(|l| !l.is_empty())
            .collect();

        if lines.is_empty() {
            return Ok(());
        }

        // Create a rename diff with multiple added lines
        let rename_diff = make_rename_diff(&old_path, &new_path, &lines);

        let result = parse_unified_diff(&rename_diff, Scope::Added);

        // Property: parsing should succeed
        prop_assert!(
            result.is_ok(),
            "Parsing should succeed, but got error: {:?}",
            result.err()
        );

        let (parsed_lines, _stats) = result.unwrap();

        // Property: all extracted lines should use the new path
        for line in &parsed_lines {
            prop_assert_eq!(
                &line.path,
                &new_path,
                "All lines should use new path '{}', but found line with path '{}'",
                new_path,
                line.path
            );
        }
    }

    // Feature: diffguard-completion, Property 7: Diff Parser Handles Renames
    // Renamed file followed by normal file should both be parsed correctly
    // **Validates: Requirements 4.3**
    #[test]
    fn property_renamed_and_normal_files_parsed_correctly(
        old_path in full_path_strategy(),
        new_path in full_path_strategy(),
        normal_path in full_path_strategy(),
        rename_content in line_content_strategy(),
        normal_content in line_content_strategy(),
    ) {
        // Ensure all paths are different
        prop_assume!(old_path != new_path);
        prop_assume!(old_path != normal_path);
        prop_assume!(new_path != normal_path);
        prop_assume!(!rename_content.is_empty());
        prop_assume!(!normal_content.is_empty());

        // Create a diff with a renamed file followed by a normal file
        let rename_diff = make_rename_diff(&old_path, &new_path, &[&rename_content]);
        let normal_diff = make_diff_with_added_lines(&normal_path, &[&normal_content]);
        let combined = format!("{}\n{}", rename_diff, normal_diff);

        let result = parse_unified_diff(&combined, Scope::Added);

        // Property: parsing should succeed
        prop_assert!(
            result.is_ok(),
            "Parsing should succeed, but got error: {:?}",
            result.err()
        );

        let (lines, _stats) = result.unwrap();

        // Property: renamed file lines should use new path
        let renamed_lines: Vec<_> = lines.iter().filter(|l| l.path == new_path).collect();
        prop_assert!(
            !renamed_lines.is_empty(),
            "Lines should be extracted from renamed file with new path '{}'",
            new_path
        );

        // Property: no lines should use old path
        let old_path_lines: Vec<_> = lines.iter().filter(|l| l.path == old_path).collect();
        prop_assert!(
            old_path_lines.is_empty(),
            "No lines should use old path '{}', but found {:?}",
            old_path,
            old_path_lines
        );

        // Property: normal file lines should be extracted
        let normal_lines: Vec<_> = lines.iter().filter(|l| l.path == normal_path).collect();
        prop_assert!(
            !normal_lines.is_empty(),
            "Lines should be extracted from normal file '{}'",
            normal_path
        );
    }
}

// ============================================================================
// Property 8: Diff Parser Resilience
// ============================================================================
//
// Feature: diffguard-completion, Property 8: Diff Parser Resilience
// For any unified diff where malformed content appears after a valid file header,
// the `parse_unified_diff` function SHALL continue processing and extract lines
// from subsequent valid files.
// **Validates: Requirements 4.6**

/// Generate a malformed hunk header
fn make_malformed_hunk_header() -> &'static str {
    "@@ malformed hunk header without proper format"
}

/// Generate a diff with a malformed hunk header
fn make_malformed_diff(path: &str) -> String {
    format!(
        "diff --git a/{path} b/{path}\n\
         index 0000000..1111111 100644\n\
         --- a/{path}\n\
         +++ b/{path}\n\
         {malformed}\n\
         +this line should be skipped",
        path = path,
        malformed = make_malformed_hunk_header()
    )
}

proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    // Feature: diffguard-completion, Property 8: Diff Parser Resilience
    // Malformed hunk headers should not stop processing of subsequent files
    // **Validates: Requirements 4.6**
    #[test]
    fn property_continues_after_malformed_hunk(
        malformed_path in full_path_strategy(),
        valid_path in full_path_strategy(),
        line_content in line_content_strategy(),
    ) {
        // Ensure paths are different
        prop_assume!(malformed_path != valid_path);
        prop_assume!(!line_content.is_empty());

        // Create a diff with a malformed file followed by a valid file
        let malformed_diff = make_malformed_diff(&malformed_path);
        let valid_diff = make_diff_with_added_lines(&valid_path, &[&line_content]);
        let combined = format!("{}\n{}", malformed_diff, valid_diff);

        let result = parse_unified_diff(&combined, Scope::Added);

        // Property: parsing should succeed (not return an error)
        prop_assert!(
            result.is_ok(),
            "Parsing should succeed despite malformed content, but got error: {:?}",
            result.err()
        );

        let (lines, _stats) = result.unwrap();

        // Property: lines from the valid file should be extracted
        let valid_lines: Vec<_> = lines.iter().filter(|l| l.path == valid_path).collect();
        prop_assert!(
            !valid_lines.is_empty(),
            "Lines should be extracted from valid file '{}' after malformed content, but found none",
            valid_path
        );
    }


    // Feature: diffguard-completion, Property 8: Diff Parser Resilience
    // Multiple malformed files should not stop processing of subsequent valid files
    // **Validates: Requirements 4.6**
    #[test]
    fn property_continues_after_multiple_malformed_hunks(
        malformed_path1 in full_path_strategy(),
        malformed_path2 in full_path_strategy(),
        valid_path in full_path_strategy(),
        line_content in line_content_strategy(),
    ) {
        // Ensure all paths are different
        prop_assume!(malformed_path1 != malformed_path2);
        prop_assume!(malformed_path1 != valid_path);
        prop_assume!(malformed_path2 != valid_path);
        prop_assume!(!line_content.is_empty());

        // Create a diff with multiple malformed files followed by a valid file
        let malformed_diff1 = make_malformed_diff(&malformed_path1);
        let malformed_diff2 = make_malformed_diff(&malformed_path2);
        let valid_diff = make_diff_with_added_lines(&valid_path, &[&line_content]);
        let combined = format!("{}\n{}\n{}", malformed_diff1, malformed_diff2, valid_diff);

        let result = parse_unified_diff(&combined, Scope::Added);

        // Property: parsing should succeed
        prop_assert!(
            result.is_ok(),
            "Parsing should succeed despite multiple malformed files, but got error: {:?}",
            result.err()
        );

        let (lines, _stats) = result.unwrap();

        // Property: lines from the valid file should be extracted
        let valid_lines: Vec<_> = lines.iter().filter(|l| l.path == valid_path).collect();
        prop_assert!(
            !valid_lines.is_empty(),
            "Lines should be extracted from valid file '{}' after malformed content, but found none",
            valid_path
        );
    }

    // Feature: diffguard-completion, Property 8: Diff Parser Resilience
    // Valid file before malformed file should still be parsed
    // **Validates: Requirements 4.6**
    #[test]
    fn property_valid_file_before_malformed_is_parsed(
        valid_path in full_path_strategy(),
        malformed_path in full_path_strategy(),
        line_content in line_content_strategy(),
    ) {
        // Ensure paths are different
        prop_assume!(valid_path != malformed_path);
        prop_assume!(!line_content.is_empty());

        // Create a diff with a valid file followed by a malformed file
        let valid_diff = make_diff_with_added_lines(&valid_path, &[&line_content]);
        let malformed_diff = make_malformed_diff(&malformed_path);
        let combined = format!("{}\n{}", valid_diff, malformed_diff);

        let result = parse_unified_diff(&combined, Scope::Added);

        // Property: parsing should succeed
        prop_assert!(
            result.is_ok(),
            "Parsing should succeed, but got error: {:?}",
            result.err()
        );

        let (lines, _stats) = result.unwrap();

        // Property: lines from the valid file should be extracted
        let valid_lines: Vec<_> = lines.iter().filter(|l| l.path == valid_path).collect();
        prop_assert!(
            !valid_lines.is_empty(),
            "Lines should be extracted from valid file '{}', but found none",
            valid_path
        );
    }


    // Feature: diffguard-completion, Property 8: Diff Parser Resilience
    // Interleaved valid and malformed files should all be handled correctly
    // **Validates: Requirements 4.6**
    #[test]
    fn property_interleaved_valid_and_malformed_files(
        valid_path1 in full_path_strategy(),
        malformed_path in full_path_strategy(),
        valid_path2 in full_path_strategy(),
        content1 in line_content_strategy(),
        content2 in line_content_strategy(),
    ) {
        // Ensure all paths are different
        prop_assume!(valid_path1 != malformed_path);
        prop_assume!(valid_path1 != valid_path2);
        prop_assume!(malformed_path != valid_path2);
        prop_assume!(!content1.is_empty());
        prop_assume!(!content2.is_empty());

        // Create a diff: valid -> malformed -> valid
        let valid_diff1 = make_diff_with_added_lines(&valid_path1, &[&content1]);
        let malformed_diff = make_malformed_diff(&malformed_path);
        let valid_diff2 = make_diff_with_added_lines(&valid_path2, &[&content2]);
        let combined = format!("{}\n{}\n{}", valid_diff1, malformed_diff, valid_diff2);

        let result = parse_unified_diff(&combined, Scope::Added);

        // Property: parsing should succeed
        prop_assert!(
            result.is_ok(),
            "Parsing should succeed, but got error: {:?}",
            result.err()
        );

        let (lines, stats) = result.unwrap();

        // Property: lines from both valid files should be extracted
        let valid1_lines: Vec<_> = lines.iter().filter(|l| l.path == valid_path1).collect();
        prop_assert!(
            !valid1_lines.is_empty(),
            "Lines should be extracted from first valid file '{}', but found none",
            valid_path1
        );

        let valid2_lines: Vec<_> = lines.iter().filter(|l| l.path == valid_path2).collect();
        prop_assert!(
            !valid2_lines.is_empty(),
            "Lines should be extracted from second valid file '{}', but found none",
            valid_path2
        );

        // Property: stats should reflect both valid files
        prop_assert_eq!(
            stats.files,
            2,
            "Stats should show 2 files, but got {}",
            stats.files
        );
    }
}

// ============================================================================
// Property 4: Scope Filtering Correctness
// ============================================================================
//
// Feature: comprehensive-test-coverage, Property 4: Scope Filtering Correctness
// For any unified diff, the set of lines returned by `Scope::Changed` SHALL be
// a subset of lines returned by `Scope::Added`, and for pure additions (no removed
// lines), `Scope::Changed` SHALL return empty.
// **Validates: Requirements 2.2, 2.3**

/// Generate a diff with only added lines (no removed lines)
fn make_pure_addition_diff(path: &str, lines: &[&str]) -> String {
    let header = make_diff_header(path);
    let hunk = hunk_header_strategy(1, lines.len() as u32);
    let content: String = lines.iter().map(|l| format!("+{}\n", l)).collect();
    format!("{}\n{}\n{}", header, hunk, content)
}

/// Generate a diff with changed lines (removed followed by added)
fn make_changed_diff(path: &str, removed_lines: &[&str], added_lines: &[&str]) -> String {
    let header = make_diff_header(path);
    let total_new_lines = added_lines.len() as u32;
    let total_old_lines = removed_lines.len() as u32;
    let hunk = format!("@@ -1,{} +1,{} @@", total_old_lines, total_new_lines);
    let removed: String = removed_lines.iter().map(|l| format!("-{}\n", l)).collect();
    let added: String = added_lines.iter().map(|l| format!("+{}\n", l)).collect();
    format!("{}\n{}\n{}{}", header, hunk, removed, added)
}

/// Generate a diff with mixed content: context, removed, and added lines
fn make_mixed_diff(
    path: &str,
    context_before: &[&str],
    removed_lines: &[&str],
    added_lines: &[&str],
    context_after: &[&str],
) -> String {
    let header = make_diff_header(path);
    let old_count = context_before.len() + removed_lines.len() + context_after.len();
    let new_count = context_before.len() + added_lines.len() + context_after.len();
    let hunk = format!("@@ -1,{} +1,{} @@", old_count, new_count);

    let ctx_before: String = context_before.iter().map(|l| format!(" {}\n", l)).collect();
    let removed: String = removed_lines.iter().map(|l| format!("-{}\n", l)).collect();
    let added: String = added_lines.iter().map(|l| format!("+{}\n", l)).collect();
    let ctx_after: String = context_after.iter().map(|l| format!(" {}\n", l)).collect();

    format!(
        "{}\n{}\n{}{}{}{}",
        header, hunk, ctx_before, removed, added, ctx_after
    )
}

/// Generate a diff with interleaved additions (some after removals, some not)
fn make_interleaved_diff(
    path: &str,
    pure_added: &[&str],
    removed: &[&str],
    changed_added: &[&str],
) -> String {
    let header = make_diff_header(path);
    let old_count = removed.len();
    let new_count = pure_added.len() + changed_added.len();
    let hunk = format!("@@ -1,{} +1,{} @@", old_count, new_count);

    // First add pure additions (not preceded by removals)
    let pure: String = pure_added.iter().map(|l| format!("+{}\n", l)).collect();
    // Then removals
    let rem: String = removed.iter().map(|l| format!("-{}\n", l)).collect();
    // Then changed additions (preceded by removals)
    let changed: String = changed_added.iter().map(|l| format!("+{}\n", l)).collect();

    format!("{}\n{}\n{}{}{}", header, hunk, pure, rem, changed)
}

proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    // Feature: comprehensive-test-coverage, Property 4: Scope Filtering Correctness
    // For pure additions (no removed lines), Scope::Changed SHALL return empty
    // **Validates: Requirements 2.2**
    #[test]
    fn property_pure_additions_return_empty_changed(
        path in full_path_strategy(),
        lines in prop::collection::vec(line_content_strategy(), 1..5),
    ) {
        // Filter out empty lines
        let non_empty_lines: Vec<&str> = lines.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();

        prop_assume!(!non_empty_lines.is_empty());

        // Create a diff with only added lines (no removals)
        let diff = make_pure_addition_diff(&path, &non_empty_lines);

        // Parse with Scope::Added - should return all lines
        let result_added = parse_unified_diff(&diff, Scope::Added);
        prop_assert!(
            result_added.is_ok(),
            "Parsing with Scope::Added should succeed, but got error: {:?}",
            result_added.err()
        );
        let (added_lines, _) = result_added.unwrap();

        // Parse with Scope::Changed - should return empty
        let result_changed = parse_unified_diff(&diff, Scope::Changed);
        prop_assert!(
            result_changed.is_ok(),
            "Parsing with Scope::Changed should succeed, but got error: {:?}",
            result_changed.err()
        );
        let (changed_lines, _) = result_changed.unwrap();

        // Property: Scope::Added should return all added lines
        prop_assert!(
            !added_lines.is_empty(),
            "Scope::Added should return lines for pure additions, but got empty"
        );

        // Property: Scope::Changed should return empty for pure additions
        prop_assert!(
            changed_lines.is_empty(),
            "Scope::Changed should return empty for pure additions (no removed lines), but got {} lines: {:?}",
            changed_lines.len(),
            changed_lines.iter().map(|l| &l.content).collect::<Vec<_>>()
        );
    }

    // Feature: comprehensive-test-coverage, Property 4: Scope Filtering Correctness
    // Scope::Changed lines are always a subset of Scope::Added lines
    // **Validates: Requirements 2.3**
    #[test]
    fn property_changed_is_subset_of_added(
        path in full_path_strategy(),
        removed_lines in prop::collection::vec(line_content_strategy(), 1..3),
        added_lines in prop::collection::vec(line_content_strategy(), 1..5),
    ) {
        // Filter out empty lines
        let non_empty_removed: Vec<&str> = removed_lines.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();
        let non_empty_added: Vec<&str> = added_lines.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();

        prop_assume!(!non_empty_removed.is_empty());
        prop_assume!(!non_empty_added.is_empty());

        // Create a diff with removed and added lines (changed content)
        let diff = make_changed_diff(&path, &non_empty_removed, &non_empty_added);

        // Parse with both scopes
        let result_added = parse_unified_diff(&diff, Scope::Added);
        let result_changed = parse_unified_diff(&diff, Scope::Changed);

        prop_assert!(result_added.is_ok(), "Parsing with Scope::Added should succeed");
        prop_assert!(result_changed.is_ok(), "Parsing with Scope::Changed should succeed");

        let (added_result, _) = result_added.unwrap();
        let (changed_result, _) = result_changed.unwrap();

        // Property: Every line in changed_result must also be in added_result
        // We compare by (path, line number, content) tuple
        let added_set: std::collections::HashSet<_> = added_result
            .iter()
            .map(|l| (&l.path, l.line, &l.content))
            .collect();

        for changed_line in &changed_result {
            let key = (&changed_line.path, changed_line.line, &changed_line.content);
            prop_assert!(
                added_set.contains(&key),
                "Changed line {:?} at line {} should be in Added results, but was not found",
                changed_line.content,
                changed_line.line
            );
        }

        // Property: Changed count should be <= Added count
        prop_assert!(
            changed_result.len() <= added_result.len(),
            "Changed count ({}) should be <= Added count ({})",
            changed_result.len(),
            added_result.len()
        );
    }

    // Feature: comprehensive-test-coverage, Property 4: Scope Filtering Correctness
    // Multi-file diff: Changed is subset of Added across all files
    // **Validates: Requirements 2.2, 2.3**
    #[test]
    fn property_changed_subset_multi_file(
        path1 in full_path_strategy(),
        path2 in full_path_strategy(),
        lines1 in prop::collection::vec(line_content_strategy(), 1..3),
        removed2 in prop::collection::vec(line_content_strategy(), 1..2),
        added2 in prop::collection::vec(line_content_strategy(), 1..3),
    ) {
        // Ensure paths are different
        prop_assume!(path1 != path2);

        // Filter out empty lines
        let non_empty_lines1: Vec<&str> = lines1.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();
        let non_empty_removed2: Vec<&str> = removed2.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();
        let non_empty_added2: Vec<&str> = added2.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();

        prop_assume!(!non_empty_lines1.is_empty());
        prop_assume!(!non_empty_removed2.is_empty());
        prop_assume!(!non_empty_added2.is_empty());

        // File 1: pure additions (no removals)
        // File 2: changed content (removals + additions)
        let diff1 = make_pure_addition_diff(&path1, &non_empty_lines1);
        let diff2 = make_changed_diff(&path2, &non_empty_removed2, &non_empty_added2);
        let combined = format!("{}\n{}", diff1, diff2);

        // Parse with both scopes
        let result_added = parse_unified_diff(&combined, Scope::Added);
        let result_changed = parse_unified_diff(&combined, Scope::Changed);

        prop_assert!(result_added.is_ok(), "Parsing with Scope::Added should succeed");
        prop_assert!(result_changed.is_ok(), "Parsing with Scope::Changed should succeed");

        let (added_result, _) = result_added.unwrap();
        let (changed_result, _) = result_changed.unwrap();

        // Property: Every line in changed_result must also be in added_result
        let added_set: std::collections::HashSet<_> = added_result
            .iter()
            .map(|l| (&l.path, l.line, &l.content))
            .collect();

        for changed_line in &changed_result {
            let key = (&changed_line.path, changed_line.line, &changed_line.content);
            prop_assert!(
                added_set.contains(&key),
                "Changed line {:?} at line {} in file {} should be in Added results",
                changed_line.content,
                changed_line.line,
                changed_line.path
            );
        }

        // Property: File 1 (pure additions) should have no lines in Changed result
        let file1_changed: Vec<_> = changed_result.iter().filter(|l| l.path == path1).collect();
        prop_assert!(
            file1_changed.is_empty(),
            "File with pure additions ({}) should have no Changed lines, but found {:?}",
            path1,
            file1_changed
        );

        // Property: File 2 (with removals) may have Changed lines
        // (This is just a sanity check - the main property is subset)
        let file2_changed: Vec<_> = changed_result.iter().filter(|l| l.path == path2).collect();
        let file2_added: Vec<_> = added_result.iter().filter(|l| l.path == path2).collect();
        prop_assert!(
            file2_changed.len() <= file2_added.len(),
            "File 2 Changed count ({}) should be <= Added count ({})",
            file2_changed.len(),
            file2_added.len()
        );
    }

    // Feature: comprehensive-test-coverage, Property 4: Scope Filtering Correctness
    // Mixed diff with context lines: Changed is still subset of Added
    // **Validates: Requirements 2.2, 2.3**
    #[test]
    fn property_changed_subset_with_context(
        path in full_path_strategy(),
        ctx_before in prop::collection::vec(line_content_strategy(), 0..2),
        removed in prop::collection::vec(line_content_strategy(), 1..3),
        added in prop::collection::vec(line_content_strategy(), 1..3),
        ctx_after in prop::collection::vec(line_content_strategy(), 0..2),
    ) {
        // Filter out empty lines
        let non_empty_ctx_before: Vec<&str> = ctx_before.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();
        let non_empty_removed: Vec<&str> = removed.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();
        let non_empty_added: Vec<&str> = added.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();
        let non_empty_ctx_after: Vec<&str> = ctx_after.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();

        prop_assume!(!non_empty_removed.is_empty());
        prop_assume!(!non_empty_added.is_empty());

        // Create a diff with context, removed, and added lines
        let diff = make_mixed_diff(
            &path,
            &non_empty_ctx_before,
            &non_empty_removed,
            &non_empty_added,
            &non_empty_ctx_after,
        );

        // Parse with both scopes
        let result_added = parse_unified_diff(&diff, Scope::Added);
        let result_changed = parse_unified_diff(&diff, Scope::Changed);

        prop_assert!(result_added.is_ok(), "Parsing with Scope::Added should succeed");
        prop_assert!(result_changed.is_ok(), "Parsing with Scope::Changed should succeed");

        let (added_result, _) = result_added.unwrap();
        let (changed_result, _) = result_changed.unwrap();

        // Property: Every line in changed_result must also be in added_result
        let added_set: std::collections::HashSet<_> = added_result
            .iter()
            .map(|l| (&l.path, l.line, &l.content))
            .collect();

        for changed_line in &changed_result {
            let key = (&changed_line.path, changed_line.line, &changed_line.content);
            prop_assert!(
                added_set.contains(&key),
                "Changed line {:?} at line {} should be in Added results",
                changed_line.content,
                changed_line.line
            );
        }

        // Property: Changed count should be <= Added count
        prop_assert!(
            changed_result.len() <= added_result.len(),
            "Changed count ({}) should be <= Added count ({})",
            changed_result.len(),
            added_result.len()
        );
    }

    // Feature: comprehensive-test-coverage, Property 4: Scope Filtering Correctness
    // Interleaved additions: only additions after removals are in Changed
    // **Validates: Requirements 2.2, 2.3**
    #[test]
    fn property_interleaved_additions_correct_scope(
        path in full_path_strategy(),
        pure_added in prop::collection::vec(line_content_strategy(), 1..3),
        removed in prop::collection::vec(line_content_strategy(), 1..2),
        changed_added in prop::collection::vec(line_content_strategy(), 1..3),
    ) {
        // Filter out empty lines
        let non_empty_pure: Vec<&str> = pure_added.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();
        let non_empty_removed: Vec<&str> = removed.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();
        let non_empty_changed: Vec<&str> = changed_added.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();

        prop_assume!(!non_empty_pure.is_empty());
        prop_assume!(!non_empty_removed.is_empty());
        prop_assume!(!non_empty_changed.is_empty());

        // Create a diff with interleaved content:
        // First pure additions, then removals, then changed additions
        let diff = make_interleaved_diff(
            &path,
            &non_empty_pure,
            &non_empty_removed,
            &non_empty_changed,
        );

        // Parse with both scopes
        let result_added = parse_unified_diff(&diff, Scope::Added);
        let result_changed = parse_unified_diff(&diff, Scope::Changed);

        prop_assert!(result_added.is_ok(), "Parsing with Scope::Added should succeed");
        prop_assert!(result_changed.is_ok(), "Parsing with Scope::Changed should succeed");

        let (added_result, _) = result_added.unwrap();
        let (changed_result, _) = result_changed.unwrap();

        // Property: Added should contain all added lines (pure + changed)
        let expected_added_count = non_empty_pure.len() + non_empty_changed.len();
        prop_assert_eq!(
            added_result.len(),
            expected_added_count,
            "Scope::Added should return {} lines (pure + changed), but got {}",
            expected_added_count,
            added_result.len()
        );

        // Property: Changed should only contain lines that followed removals
        prop_assert_eq!(
            changed_result.len(),
            non_empty_changed.len(),
            "Scope::Changed should return {} lines (only those after removals), but got {}",
            non_empty_changed.len(),
            changed_result.len()
        );

        // Property: Every line in changed_result must also be in added_result
        let added_set: std::collections::HashSet<_> = added_result
            .iter()
            .map(|l| (&l.path, l.line, &l.content))
            .collect();

        for changed_line in &changed_result {
            let key = (&changed_line.path, changed_line.line, &changed_line.content);
            prop_assert!(
                added_set.contains(&key),
                "Changed line {:?} at line {} should be in Added results",
                changed_line.content,
                changed_line.line
            );
        }
    }

    // Feature: comprehensive-test-coverage, Property 4: Scope Filtering Correctness
    // Empty diff: both scopes return empty
    // **Validates: Requirements 2.2, 2.3**
    #[test]
    fn property_empty_diff_both_scopes_empty(
        path in full_path_strategy(),
    ) {
        // Create a diff header with no hunks
        let diff = format!(
            "diff --git a/{path} b/{path}\n\
             index 0000000..1111111 100644\n\
             --- a/{path}\n\
             +++ b/{path}",
            path = path
        );

        // Parse with both scopes
        let result_added = parse_unified_diff(&diff, Scope::Added);
        let result_changed = parse_unified_diff(&diff, Scope::Changed);

        prop_assert!(result_added.is_ok(), "Parsing with Scope::Added should succeed");
        prop_assert!(result_changed.is_ok(), "Parsing with Scope::Changed should succeed");

        let (added_result, _) = result_added.unwrap();
        let (changed_result, _) = result_changed.unwrap();

        // Property: Both scopes should return empty for a diff with no hunks
        prop_assert!(
            added_result.is_empty(),
            "Scope::Added should return empty for diff with no hunks, but got {} lines",
            added_result.len()
        );
        prop_assert!(
            changed_result.is_empty(),
            "Scope::Changed should return empty for diff with no hunks, but got {} lines",
            changed_result.len()
        );
    }
}

// ============================================================================
// Property: Line Count Consistency
// ============================================================================
//
// Feature: comprehensive-test-coverage, Property: Line Count Consistency
// For any well-formed diff, the DiffStats.lines count SHALL equal the number
// of DiffLine items returned.
// **Validates: Requirements 2.4**

proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn property_line_count_matches_stats(
        path in full_path_strategy(),
        lines in prop::collection::vec(line_content_strategy(), 1..10),
    ) {
        // Filter out empty lines
        let non_empty_lines: Vec<&str> = lines.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();

        prop_assume!(!non_empty_lines.is_empty());

        // Create a well-formed diff
        let diff = make_diff_with_added_lines(&path, &non_empty_lines);

        // Parse the diff
        let result = parse_unified_diff(&diff, Scope::Added);
        prop_assert!(result.is_ok(), "Parsing should succeed");

        let (diff_lines, stats) = result.unwrap();

        // Property: stats.lines should equal the number of DiffLine items
        prop_assert_eq!(
            stats.lines as usize,
            diff_lines.len(),
            "DiffStats.lines ({}) should equal number of DiffLine items ({})",
            stats.lines,
            diff_lines.len()
        );
    }

    #[test]
    fn property_file_count_matches_unique_paths(
        path1 in full_path_strategy(),
        path2 in full_path_strategy(),
        lines1 in prop::collection::vec(line_content_strategy(), 1..3),
        lines2 in prop::collection::vec(line_content_strategy(), 1..3),
    ) {
        // Ensure paths are different
        prop_assume!(path1 != path2);

        // Filter out empty lines
        let non_empty_lines1: Vec<&str> = lines1.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();
        let non_empty_lines2: Vec<&str> = lines2.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();

        prop_assume!(!non_empty_lines1.is_empty());
        prop_assume!(!non_empty_lines2.is_empty());

        // Create a multi-file diff
        let diff1 = make_diff_with_added_lines(&path1, &non_empty_lines1);
        let diff2 = make_diff_with_added_lines(&path2, &non_empty_lines2);
        let combined = format!("{}\n{}", diff1, diff2);

        // Parse the diff
        let result = parse_unified_diff(&combined, Scope::Added);
        prop_assert!(result.is_ok(), "Parsing should succeed");

        let (diff_lines, stats) = result.unwrap();

        // Count unique paths in diff_lines
        let unique_paths: std::collections::BTreeSet<&str> = diff_lines
            .iter()
            .map(|l| l.path.as_str())
            .collect();

        // Property: stats.files should equal number of unique paths
        prop_assert_eq!(
            stats.files as usize,
            unique_paths.len(),
            "DiffStats.files ({}) should equal number of unique paths ({})",
            stats.files,
            unique_paths.len()
        );
    }
}

// ============================================================================
// Property: No Panic on Valid UTF-8 Input
// ============================================================================
//
// Feature: comprehensive-test-coverage, Property: Parser Robustness
// For any valid UTF-8 string input, `parse_unified_diff` SHALL not panic.
// **Validates: Requirements 2.5**

proptest! {
    #![proptest_config(ProptestConfig::with_cases(200))]

    #[test]
    fn property_no_panic_on_arbitrary_utf8(
        input in prop::string::string_regex("[\\x00-\\x7F\\u{0080}-\\u{FFFF}]{0,500}").expect("valid regex"),
    ) {
        // Parse arbitrary UTF-8 input - should never panic
        let _ = parse_unified_diff(&input, Scope::Added);
        let _ = parse_unified_diff(&input, Scope::Changed);
        let _ = parse_unified_diff(&input, Scope::Modified);
        let _ = parse_unified_diff(&input, Scope::Deleted);
        // If we reach here without panicking, the test passes
    }

    #[test]
    fn property_no_panic_on_unicode_content(
        prefix in prop::string::string_regex("[a-zA-Z0-9_]{0,20}").expect("valid regex"),
        unicode_chars in prop::string::string_regex("[\\u{4E00}-\\u{9FFF}\\u{1F600}-\\u{1F64F}]{0,10}").expect("valid regex"),
        suffix in prop::string::string_regex("[a-zA-Z0-9_]{0,20}").expect("valid regex"),
    ) {
        // Create input with Unicode characters
        let input = format!("{}{}{}", prefix, unicode_chars, suffix);

        // Parse should not panic
        let _ = parse_unified_diff(&input, Scope::Added);
        let _ = parse_unified_diff(&input, Scope::Changed);
        let _ = parse_unified_diff(&input, Scope::Modified);
        let _ = parse_unified_diff(&input, Scope::Deleted);
    }

    #[test]
    fn property_no_panic_on_special_characters(
        special in prop::sample::select(&[
            "\n", "\r", "\r\n", "\t", "\x00", "\\", "\"", "'", "`",
            "@@", "+++", "---", "diff", "Binary", "Subproject",
        ]),
        count in 1..20usize,
    ) {
        // Create input with repeated special characters
        let input = special.repeat(count);

        // Parse should not panic
        let _ = parse_unified_diff(&input, Scope::Added);
        let _ = parse_unified_diff(&input, Scope::Changed);
        let _ = parse_unified_diff(&input, Scope::Modified);
        let _ = parse_unified_diff(&input, Scope::Deleted);
    }

    #[test]
    fn property_no_panic_on_malformed_hunk_headers(
        prefix in prop::string::string_regex("@@[^@]{0,50}").expect("valid regex"),
    ) {
        // Create potentially malformed hunk headers
        let input = format!(
            "diff --git a/file b/file\n\
             --- a/file\n\
             +++ b/file\n\
             {}\n\
             +some content",
            prefix
        );

        // Parse should not panic (may return error, but not panic)
        let _ = parse_unified_diff(&input, Scope::Added);
        let _ = parse_unified_diff(&input, Scope::Changed);
        let _ = parse_unified_diff(&input, Scope::Modified);
        let _ = parse_unified_diff(&input, Scope::Deleted);
    }
}

// ============================================================================
// Property: Line Numbers are Monotonically Increasing Within Files
// ============================================================================
//
// Feature: comprehensive-test-coverage, Property: Line Number Validity
// For any parsed diff, line numbers within the same file SHALL be in
// the order they appear in the diff (not necessarily strictly increasing
// but consistent with hunk structure).
// **Validates: Requirements 2.6**

proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn property_line_numbers_are_positive(
        path in full_path_strategy(),
        lines in prop::collection::vec(line_content_strategy(), 1..5),
    ) {
        let non_empty_lines: Vec<&str> = lines.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();

        prop_assume!(!non_empty_lines.is_empty());

        let diff = make_diff_with_added_lines(&path, &non_empty_lines);
        let result = parse_unified_diff(&diff, Scope::Added);
        prop_assert!(result.is_ok(), "Parsing should succeed");

        let (diff_lines, _) = result.unwrap();

        // Property: All line numbers should be positive (>= 1)
        for line in &diff_lines {
            prop_assert!(
                line.line >= 1,
                "Line number should be >= 1, but got {} for path '{}'",
                line.line,
                line.path
            );
        }
    }

    #[test]
    fn property_paths_are_non_empty(
        path in full_path_strategy(),
        lines in prop::collection::vec(line_content_strategy(), 1..5),
    ) {
        let non_empty_lines: Vec<&str> = lines.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();

        prop_assume!(!non_empty_lines.is_empty());

        let diff = make_diff_with_added_lines(&path, &non_empty_lines);
        let result = parse_unified_diff(&diff, Scope::Added);
        prop_assert!(result.is_ok(), "Parsing should succeed");

        let (diff_lines, _) = result.unwrap();

        // Property: All paths should be non-empty
        for line in &diff_lines {
            prop_assert!(
                !line.path.is_empty(),
                "Path should not be empty"
            );
        }
    }
}

// ============================================================================
// Property: ChangeKind Consistency
// ============================================================================
//
// Feature: comprehensive-test-coverage, Property: ChangeKind Validity
// For any diff line, the ChangeKind should be consistent with the scope used.
// **Validates: Requirements 2.7**

use diffguard_diff::ChangeKind;

proptest! {
    #![proptest_config(ProptestConfig::with_cases(100))]

    #[test]
    fn property_changed_scope_only_has_changed_kind(
        path in full_path_strategy(),
        removed_lines in prop::collection::vec(line_content_strategy(), 1..3),
        added_lines in prop::collection::vec(line_content_strategy(), 1..3),
    ) {
        let non_empty_removed: Vec<&str> = removed_lines.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();
        let non_empty_added: Vec<&str> = added_lines.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();

        prop_assume!(!non_empty_removed.is_empty());
        prop_assume!(!non_empty_added.is_empty());

        let diff = make_changed_diff(&path, &non_empty_removed, &non_empty_added);
        let result = parse_unified_diff(&diff, Scope::Changed);
        prop_assert!(result.is_ok(), "Parsing should succeed");

        let (diff_lines, _) = result.unwrap();

        // Property: All lines from Scope::Changed should have ChangeKind::Changed
        for line in &diff_lines {
            prop_assert_eq!(
                line.kind,
                ChangeKind::Changed,
                "Lines from Scope::Changed should have ChangeKind::Changed"
            );
        }
    }

    #[test]
    fn property_pure_additions_have_added_kind(
        path in full_path_strategy(),
        lines in prop::collection::vec(line_content_strategy(), 1..5),
    ) {
        let non_empty_lines: Vec<&str> = lines.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();

        prop_assume!(!non_empty_lines.is_empty());

        let diff = make_pure_addition_diff(&path, &non_empty_lines);
        let result = parse_unified_diff(&diff, Scope::Added);
        prop_assert!(result.is_ok(), "Parsing should succeed");

        let (diff_lines, _) = result.unwrap();

        // Property: Pure additions should have ChangeKind::Added
        for line in &diff_lines {
            prop_assert_eq!(
                line.kind,
                ChangeKind::Added,
                "Pure additions should have ChangeKind::Added"
            );
        }
    }

    #[test]
    fn property_deleted_scope_only_has_deleted_kind(
        path in full_path_strategy(),
        removed_lines in prop::collection::vec(line_content_strategy(), 1..5),
    ) {
        let non_empty_removed: Vec<&str> = removed_lines.iter()
            .filter(|l| !l.is_empty())
            .map(|s| s.as_str())
            .collect();

        prop_assume!(!non_empty_removed.is_empty());

        let removed_count = non_empty_removed.len();
        let removed_content: String = non_empty_removed
            .iter()
            .map(|line| format!("-{}\n", line))
            .collect();
        let diff = format!(
            "diff --git a/{path} b/{path}\n\
             --- a/{path}\n\
             +++ b/{path}\n\
             @@ -1,{removed_count} +1,0 @@\n\
             {removed_content}",
            path = path,
            removed_count = removed_count,
            removed_content = removed_content
        );

        let result = parse_unified_diff(&diff, Scope::Deleted);
        prop_assert!(result.is_ok(), "Parsing should succeed");

        let (diff_lines, _) = result.unwrap();
        prop_assert_eq!(
            diff_lines.len(),
            removed_count,
            "Deleted scope should return all removed lines"
        );
        for line in &diff_lines {
            prop_assert_eq!(
                line.kind,
                ChangeKind::Deleted,
                "Lines from Scope::Deleted should have ChangeKind::Deleted"
            );
        }
    }
}

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

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

    /// Empty string input returns empty results.
    #[test]
    fn empty_string_returns_empty() {
        let result = parse_unified_diff("", Scope::Added);
        assert!(result.is_ok());
        let (lines, stats) = result.unwrap();
        assert!(lines.is_empty());
        assert_eq!(stats.files, 0);
        assert_eq!(stats.lines, 0);
    }

    /// Diff header with no hunks returns empty.
    #[test]
    fn header_only_no_hunks_returns_empty() {
        let diff = "diff --git a/file.rs b/file.rs\n\
                    index abc1234..def5678 100644\n\
                    --- a/file.rs\n\
                    +++ b/file.rs";

        let result = parse_unified_diff(diff, Scope::Added);
        assert!(result.is_ok());
        let (lines, _stats) = result.unwrap();
        assert!(lines.is_empty(), "No hunks should produce no lines");
    }

    /// Context-only hunk returns no added lines.
    #[test]
    fn context_only_hunk_no_added_lines() {
        let diff = "diff --git a/file.rs b/file.rs\n\
                    index abc1234..def5678 100644\n\
                    --- a/file.rs\n\
                    +++ b/file.rs\n\
                    @@ -1,3 +1,3 @@\n\
                     fn existing() {}\n\
                     fn another() {}\n\
                     fn third() {}";

        let result = parse_unified_diff(diff, Scope::Added);
        assert!(result.is_ok());
        let (lines, _stats) = result.unwrap();
        assert!(
            lines.is_empty(),
            "Context-only hunk should produce no added lines"
        );
    }

    /// DiffStats accuracy: files count equals unique paths.
    #[test]
    fn stats_files_equals_unique_paths() {
        let diff = "diff --git a/file1.rs b/file1.rs\n\
                    --- a/file1.rs\n\
                    +++ b/file1.rs\n\
                    @@ -1,1 +1,2 @@\n\
                     existing\n\
                    +added1\n\
                    diff --git a/file2.rs b/file2.rs\n\
                    --- a/file2.rs\n\
                    +++ b/file2.rs\n\
                    @@ -1,1 +1,2 @@\n\
                     existing\n\
                    +added2";

        let result = parse_unified_diff(diff, Scope::Added);
        assert!(result.is_ok());
        let (lines, stats) = result.unwrap();

        // Count unique paths
        let unique_paths: std::collections::BTreeSet<_> =
            lines.iter().map(|l| l.path.as_str()).collect();

        assert_eq!(stats.files as usize, unique_paths.len());
        assert_eq!(stats.files, 2);
    }

    /// DiffStats accuracy: lines count equals DiffLine items count.
    #[test]
    fn stats_lines_equals_diff_line_count() {
        let diff = "diff --git a/file.rs b/file.rs\n\
                    --- a/file.rs\n\
                    +++ b/file.rs\n\
                    @@ -1,1 +1,4 @@\n\
                     existing\n\
                    +line1\n\
                    +line2\n\
                    +line3";

        let result = parse_unified_diff(diff, Scope::Added);
        assert!(result.is_ok());
        let (lines, stats) = result.unwrap();

        assert_eq!(stats.lines as usize, lines.len());
        assert_eq!(stats.lines, 3);
    }

    /// Whitespace-only content parses correctly.
    #[test]
    fn whitespace_only_input() {
        let result = parse_unified_diff("   \n\n\t\t\n", Scope::Added);
        assert!(result.is_ok());
        let (lines, _) = result.unwrap();
        assert!(lines.is_empty());
    }

    /// Diff with only deleted lines returns empty for Added scope.
    #[test]
    fn only_deleted_lines_returns_empty_for_added() {
        let diff = "diff --git a/file.rs b/file.rs\n\
                    --- a/file.rs\n\
                    +++ b/file.rs\n\
                    @@ -1,3 +1,1 @@\n\
                    -removed1\n\
                    -removed2\n\
                     kept";

        let result = parse_unified_diff(diff, Scope::Added);
        assert!(result.is_ok());
        let (lines, _) = result.unwrap();
        assert!(
            lines.is_empty(),
            "Only deleted lines should return empty for Added scope"
        );
    }
}