tldr-core 0.1.6

Core analysis engine for TLDR code analysis tool
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
//! Test module for dice CLI command (Session 8 spec)
//!
//! These tests define expected behavior BEFORE implementation.
//! Tests are designed to FAIL until the modules are implemented.
//!
//! # Test Categories
//!
//! ## 1. Token-Based Dice Coefficient (D1)
//! - Basic Dice computation
//! - Multiset handling
//! - Edge cases (empty, identical, disjoint)
//!
//! ## 2. Jaccard Coefficient (D2)
//! - Basic Jaccard computation
//! - Relationship to Dice
//!
//! ## 3. Cosine Similarity (D3)
//! - TF-IDF weighted similarity
//! - Term frequency computation
//!
//! ## 4. Function-Level Similarity (D4)
//! - file.py::function_name syntax
//! - Function extraction
//!
//! ## 5. File-Level Similarity (D5)
//! - Entire file comparison
//! - Auto-detection
//!
//! ## 6. Block-Level Similarity (D6)
//! - file.py:start:end syntax
//! - Line range extraction
//!
//! ## 7. N-gram Similarity (D10)
//! - Bigram and trigram comparison
//! - Configurable n
//!
//! ## 8. Pairwise Similarity Matrix (D11)
//! - All-pairs comparison
//! - Threshold filtering
//!
//! ## 9. Score Interpretation (D12)
//! - Human-readable descriptions
//! - Threshold boundaries
//!
//! Reference: session8-spec.md


// =============================================================================
// Test Fixture Setup Module
// =============================================================================

/// Test fixture utilities for similarity analysis tests
pub mod fixtures {
    use std::path::{Path, PathBuf};
    use tempfile::TempDir;

    /// A temporary directory for testing similarity analysis
    pub struct TestDir {
        pub dir: TempDir,
    }

    impl TestDir {
        /// Create a new empty temporary directory
        pub fn new() -> std::io::Result<Self> {
            let dir = TempDir::new()?;
            Ok(Self { dir })
        }

        /// Get the path to the directory
        pub fn path(&self) -> &Path {
            self.dir.path()
        }

        /// Add a file to the directory
        pub fn add_file(&self, name: &str, content: &str) -> std::io::Result<PathBuf> {
            let path = self.dir.path().join(name);
            if let Some(parent) = path.parent() {
                std::fs::create_dir_all(parent)?;
            }
            std::fs::write(&path, content)?;
            Ok(path)
        }
    }

    // -------------------------------------------------------------------------
    // Identical Code Fixtures
    // -------------------------------------------------------------------------

    pub const PYTHON_FUNC_A: &str = r#"
def process_data(items):
    result = []
    for item in items:
        processed = transform(item)
        result.append(processed)
    return result
"#;

    /// Identical to PYTHON_FUNC_A
    pub const PYTHON_FUNC_A_COPY: &str = r#"
def process_data(items):
    result = []
    for item in items:
        processed = transform(item)
        result.append(processed)
    return result
"#;

    // -------------------------------------------------------------------------
    // Similar Code Fixtures (for Type-2/3 comparison)
    // -------------------------------------------------------------------------

    /// Same structure, different names (~85% similar)
    pub const PYTHON_FUNC_B_SIMILAR: &str = r#"
def handle_items(data):
    output = []
    for element in data:
        converted = transform(element)
        output.append(converted)
    return output
"#;

    /// Same structure with added logging (~75% similar)
    pub const PYTHON_FUNC_C_WITH_LOGGING: &str = r#"
def process_data_logged(items):
    print("Starting processing")
    result = []
    for item in items:
        processed = transform(item)
        print(f"Processed: {processed}")
        result.append(processed)
    print("Done")
    return result
"#;

    // -------------------------------------------------------------------------
    // Completely Different Code Fixtures
    // -------------------------------------------------------------------------

    pub const PYTHON_FUNC_DIFFERENT: &str = r#"
def calculate_average(numbers):
    if not numbers:
        return 0
    total = sum(numbers)
    count = len(numbers)
    return total / count
"#;

    /// Another completely different function
    pub const PYTHON_FUNC_VERY_DIFFERENT: &str = r#"
class DatabaseConnection:
    def __init__(self, host, port):
        self.host = host
        self.port = port
        self.connected = False

    def connect(self):
        self.connected = True
        return self
"#;

    // -------------------------------------------------------------------------
    // File with Multiple Functions (for function extraction testing)
    // -------------------------------------------------------------------------

    pub const PYTHON_MULTI_FUNCTION_FILE: &str = r#"
def first_function(a, b):
    return a + b

def second_function(x, y, z):
    result = x * y
    result = result + z
    return result

def third_function(items):
    total = 0
    for item in items:
        total += item
    return total
"#;

    /// Another file with similar functions
    pub const PYTHON_MULTI_FUNCTION_FILE_B: &str = r#"
def add_numbers(a, b):
    return a + b

def multiply_and_add(x, y, z):
    product = x * y
    product = product + z
    return product

def sum_items(elements):
    total = 0
    for element in elements:
        total += element
    return total
"#;

    // -------------------------------------------------------------------------
    // TypeScript Fixtures
    // -------------------------------------------------------------------------

    pub const TS_FUNC_A: &str = r#"
export function processData(items: any[]): any[] {
    const result: any[] = [];
    for (const item of items) {
        const processed = transform(item);
        result.push(processed);
    }
    return result;
}
"#;

    pub const TS_FUNC_B_SIMILAR: &str = r#"
export function handleItems(data: any[]): any[] {
    const output: any[] = [];
    for (const element of data) {
        const converted = transform(element);
        output.push(converted);
    }
    return output;
}
"#;

    // -------------------------------------------------------------------------
    // Go Fixtures
    // -------------------------------------------------------------------------

    pub const GO_FUNC_A: &str = r#"
func ProcessData(items []interface{}) []interface{} {
    result := make([]interface{}, 0)
    for _, item := range items {
        processed := transform(item)
        result = append(result, processed)
    }
    return result
}
"#;

    pub const GO_FUNC_B_SIMILAR: &str = r#"
func HandleItems(data []interface{}) []interface{} {
    output := make([]interface{}, 0)
    for _, element := range data {
        converted := transform(element)
        output = append(output, converted)
    }
    return output
}
"#;

    // -------------------------------------------------------------------------
    // Rust Fixtures
    // -------------------------------------------------------------------------

    pub const RUST_FUNC_A: &str = r#"
pub fn process_data(items: &[Item]) -> Vec<Item> {
    let mut result = Vec::new();
    for item in items {
        let processed = transform(item);
        result.push(processed);
    }
    result
}
"#;

    pub const RUST_FUNC_B_SIMILAR: &str = r#"
pub fn handle_items(data: &[Item]) -> Vec<Item> {
    let mut output = Vec::new();
    for element in data {
        let converted = transform(element);
        output.push(converted);
    }
    output
}
"#;

    // -------------------------------------------------------------------------
    // Edge Case Fixtures
    // -------------------------------------------------------------------------

    /// Function with unique tokens only
    pub const UNIQUE_TOKENS_ONLY: &str = r#"
def unique_function_xyz():
    alpha_var = "unique_string_123"
    return alpha_var
"#;

    /// Different function with no shared tokens
    pub const NO_SHARED_TOKENS: &str = r#"
def another_beta_function():
    omega_value = "different_text_456"
    return omega_value
"#;

    // -------------------------------------------------------------------------
    // Block-Level Testing Fixtures
    // -------------------------------------------------------------------------

    /// File with distinct blocks for line-range testing
    pub const FILE_WITH_BLOCKS: &str = r#"
# Block A: lines 1-10
def block_a():
    x = 1
    y = 2
    z = 3
    result = x + y + z
    return result

# Block B: lines 12-21
def block_b():
    a = 1
    b = 2
    c = 3
    result = a + b + c
    return result

# Block C: lines 23-32 (different)
def block_c():
    items = [1, 2, 3]
    total = sum(items)
    average = total / len(items)
    return average
"#;
}

// =============================================================================
// Token-Based Dice Coefficient Tests (D1)
// =============================================================================

#[cfg(test)]
mod dice_coefficient_tests {
    use super::fixtures::*;
    use crate::analysis::similarity::{
        compute_similarity, SimilarityMetric, SimilarityOptions,
    };
    

    /// Test: Dice coefficient for identical code
    /// Contract: dice(A, A) = 1.0
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_dice_identical_code() {
        // GIVEN: Two files with identical code
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.py", PYTHON_FUNC_A_COPY).unwrap();

        // WHEN: Computing Dice similarity
        let options = crate::analysis::similarity::SimilarityOptions { metric: SimilarityMetric::Dice, ..Default::default() };

        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // THEN: Dice should be 1.0
        assert!(
            (report.similarity.dice - 1.0).abs() < 0.001,
            "Identical code should have Dice = 1.0, got {}",
            report.similarity.dice
        );
    }

    /// Test: Dice coefficient is symmetric
    /// Contract: dice(A, B) = dice(B, A)
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_dice_symmetry() {
        // GIVEN: Two different files
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.py", PYTHON_FUNC_B_SIMILAR).unwrap();

        // WHEN: Computing Dice in both directions
        let options = SimilarityOptions::default();
        let report_ab = compute_similarity(&path_a, &path_b, &options).unwrap();
        let report_ba = compute_similarity(&path_b, &path_a, &options).unwrap();

        // THEN: Should be symmetric
        assert!(
            (report_ab.similarity.dice - report_ba.similarity.dice).abs() < 0.001,
            "Dice should be symmetric"
        );
    }

    /// Test: Dice coefficient for disjoint code
    /// Contract: dice(A, B) = 0 if A and B share no tokens
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_dice_disjoint_code() {
        // GIVEN: Two files with no shared tokens
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", UNIQUE_TOKENS_ONLY).unwrap();
        let path_b = test_dir.add_file("b.py", NO_SHARED_TOKENS).unwrap();

        // WHEN: Computing Dice similarity
        let options = SimilarityOptions::default();
        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // THEN: Dice should be close to 0 (some keywords may overlap)
        assert!(
            report.similarity.dice < 0.3,
            "Disjoint code should have low Dice, got {}",
            report.similarity.dice
        );
    }

    /// Test: Dice handles empty input
    /// Contract: Empty fragment results in dice = 0.0
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_dice_empty_input() {
        // GIVEN: Empty file vs normal file
        let test_dir = TestDir::new().unwrap();
        let path_empty = test_dir.add_file("empty.py", "").unwrap();
        let path_normal = test_dir.add_file("normal.py", PYTHON_FUNC_A).unwrap();

        // WHEN: Computing Dice similarity
        let options = SimilarityOptions::default();
        let report = compute_similarity(&path_empty, &path_normal, &options).unwrap();

        // THEN: Dice should be 0.0
        assert!(
            (report.similarity.dice - 0.0).abs() < 0.001,
            "Empty input should give Dice = 0.0"
        );
    }

    /// Test: Dice coefficient for similar code
    /// Contract: Similar code has high Dice (0.7-0.9)
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_dice_similar_code() {
        // GIVEN: Two similar files
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.py", PYTHON_FUNC_B_SIMILAR).unwrap();

        // WHEN: Computing Dice similarity
        let options = SimilarityOptions::default();
        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // THEN: Should be moderately high
        assert!(
            report.similarity.dice >= 0.5 && report.similarity.dice <= 1.0,
            "Similar code should have moderate-high Dice, got {}",
            report.similarity.dice
        );
    }

    /// Test: Dice uses multiset (token counts matter)
    /// Contract: 2 * |intersection| / (|A| + |B|) with counts
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_dice_multiset_handling() {
        // GIVEN: File with repeated tokens vs file without
        let repeated = r#"
def func():
    x = 1
    x = 2
    x = 3
    return x
"#;
        let not_repeated = r#"
def func():
    a = 1
    b = 2
    c = 3
    return a
"#;
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", repeated).unwrap();
        let path_b = test_dir.add_file("b.py", not_repeated).unwrap();

        // WHEN: Computing similarity
        let options = SimilarityOptions::default();
        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // THEN: Token breakdown should reflect counts
        // The shared 'x' appears 4 times in A but not in B
        // This test verifies multiset behavior
        assert!(
            report.token_breakdown.unique_to_fragment1 > 0
                || report.token_breakdown.unique_to_fragment2 > 0
        );
    }

    /// Test: Dice is non-negative
    /// Contract: dice >= 0.0
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_dice_non_negative() {
        // GIVEN: Any two files
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.py", PYTHON_FUNC_DIFFERENT).unwrap();

        // WHEN: Computing Dice
        let options = SimilarityOptions::default();
        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // THEN: Should be non-negative
        assert!(report.similarity.dice >= 0.0, "Dice must be non-negative");
    }
}

// =============================================================================
// Jaccard Coefficient Tests (D2)
// =============================================================================

#[cfg(test)]
mod jaccard_coefficient_tests {
    use super::fixtures::*;
    use crate::analysis::similarity::{compute_similarity, SimilarityMetric, SimilarityOptions};
    

    /// Test: Jaccard for identical code
    /// Contract: jaccard(A, A) = 1.0
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_jaccard_identical_code() {
        // GIVEN: Identical files
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.py", PYTHON_FUNC_A_COPY).unwrap();

        // WHEN: Computing Jaccard
        let options = crate::analysis::similarity::SimilarityOptions { metric: SimilarityMetric::Jaccard, ..Default::default() };

        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // THEN: Should be 1.0
        assert!((report.similarity.jaccard - 1.0).abs() < 0.001);
    }

    /// Test: Jaccard for disjoint code
    /// Contract: jaccard(A, B) = 0 if disjoint
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_jaccard_disjoint_code() {
        // GIVEN: Files with minimal overlap
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", UNIQUE_TOKENS_ONLY).unwrap();
        let path_b = test_dir.add_file("b.py", NO_SHARED_TOKENS).unwrap();

        // WHEN: Computing Jaccard
        let options = SimilarityOptions::default();
        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // THEN: Should be close to 0
        assert!(
            report.similarity.jaccard < 0.3,
            "Disjoint code should have low Jaccard"
        );
    }

    /// Test: Jaccard <= Dice (always more conservative)
    /// Contract: jaccard = dice / (2 - dice)
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_jaccard_less_than_dice() {
        // GIVEN: Any two files
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.py", PYTHON_FUNC_B_SIMILAR).unwrap();

        // WHEN: Computing both metrics
        let options = crate::analysis::similarity::SimilarityOptions { metric: SimilarityMetric::All, ..Default::default() };

        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // THEN: Jaccard should be <= Dice
        assert!(
            report.similarity.jaccard <= report.similarity.dice + 0.001,
            "Jaccard ({}) should be <= Dice ({})",
            report.similarity.jaccard,
            report.similarity.dice
        );
    }

    /// Test: Jaccard/Dice relationship formula
    /// Contract: jaccard = dice / (2 - dice)
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_jaccard_dice_relationship() {
        // GIVEN: Two files
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        let path_b = test_dir
            .add_file("b.py", PYTHON_FUNC_C_WITH_LOGGING)
            .unwrap();

        // WHEN: Computing both metrics
        let options = crate::analysis::similarity::SimilarityOptions { metric: SimilarityMetric::All, ..Default::default() };

        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // THEN: Relationship should hold
        let expected_jaccard = report.similarity.dice / (2.0 - report.similarity.dice);
        assert!(
            (report.similarity.jaccard - expected_jaccard).abs() < 0.01,
            "Jaccard/Dice relationship broken"
        );
    }

    /// Test: Jaccard is symmetric
    /// Contract: jaccard(A, B) = jaccard(B, A)
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_jaccard_symmetry() {
        // GIVEN: Two files
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.py", PYTHON_FUNC_B_SIMILAR).unwrap();

        // WHEN: Computing in both directions
        let options = SimilarityOptions::default();
        let report_ab = compute_similarity(&path_a, &path_b, &options).unwrap();
        let report_ba = compute_similarity(&path_b, &path_a, &options).unwrap();

        // THEN: Should be symmetric
        assert!((report_ab.similarity.jaccard - report_ba.similarity.jaccard).abs() < 0.001);
    }
}

// =============================================================================
// Cosine Similarity Tests (D3)
// =============================================================================

#[cfg(test)]
mod cosine_similarity_tests {
    use super::fixtures::*;
    use crate::analysis::similarity::{compute_similarity, SimilarityMetric};
    

    /// Test: Cosine for identical code
    /// Contract: cosine(A, A) = 1.0
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_cosine_identical_code() {
        // GIVEN: Identical files
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.py", PYTHON_FUNC_A_COPY).unwrap();

        // WHEN: Computing cosine
        let options = crate::analysis::similarity::SimilarityOptions { metric: SimilarityMetric::Cosine, ..Default::default() };

        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // THEN: Should be 1.0
        assert!(report.similarity.cosine.is_some());
        assert!((report.similarity.cosine.unwrap() - 1.0).abs() < 0.001);
    }

    /// Test: Cosine for disjoint code
    /// Contract: cosine(A, B) = 0 if no shared terms
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_cosine_disjoint_code() {
        // GIVEN: Files with no shared tokens
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", UNIQUE_TOKENS_ONLY).unwrap();
        let path_b = test_dir.add_file("b.py", NO_SHARED_TOKENS).unwrap();

        // WHEN: Computing cosine
        let options = crate::analysis::similarity::SimilarityOptions { metric: SimilarityMetric::Cosine, ..Default::default() };

        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // THEN: Should be close to 0
        assert!(report.similarity.cosine.unwrap() < 0.3);
    }

    /// Test: Cosine is in valid range
    /// Contract: 0 <= cosine <= 1 for non-negative vectors
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_cosine_valid_range() {
        // GIVEN: Any two files
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.py", PYTHON_FUNC_DIFFERENT).unwrap();

        // WHEN: Computing cosine
        let options = crate::analysis::similarity::SimilarityOptions { metric: SimilarityMetric::Cosine, ..Default::default() };

        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // THEN: Should be in [0, 1]
        let cosine = report.similarity.cosine.unwrap();
        assert!(
            (0.0..=1.0).contains(&cosine),
            "Cosine must be in [0,1], got {}",
            cosine
        );
    }

    /// Test: Cosine weights rare tokens higher
    /// Contract: IDF gives more weight to distinctive terms
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_cosine_weights_rare_tokens() {
        // This test would ideally show that rare tokens have more impact
        // For now, just verify cosine is computed
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.py", PYTHON_FUNC_B_SIMILAR).unwrap();

        let options = crate::analysis::similarity::SimilarityOptions { metric: SimilarityMetric::Cosine, ..Default::default() };

        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // Verify cosine is computed and reasonable
        assert!(report.similarity.cosine.is_some());
    }

    /// Test: Cosine handles empty input
    /// Contract: cosine = 0 for empty document
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_cosine_empty_input() {
        // GIVEN: Empty vs normal file
        let test_dir = TestDir::new().unwrap();
        let path_empty = test_dir.add_file("empty.py", "").unwrap();
        let path_normal = test_dir.add_file("normal.py", PYTHON_FUNC_A).unwrap();

        // WHEN: Computing cosine
        let options = crate::analysis::similarity::SimilarityOptions { metric: SimilarityMetric::Cosine, ..Default::default() };

        let report = compute_similarity(&path_empty, &path_normal, &options).unwrap();

        // THEN: Should be 0.0
        assert!((report.similarity.cosine.unwrap() - 0.0).abs() < 0.001);
    }
}

// =============================================================================
// Function-Level Similarity Tests (D4)
// =============================================================================

#[cfg(test)]
mod function_level_tests {
    use super::fixtures::*;
    use crate::analysis::similarity::{
        compute_similarity, parse_target, ComparisonLevel, SimilarityOptions,
    };
    use std::path::PathBuf;

    /// Test: Parse function target syntax
    /// Contract: file.py::function_name is valid
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_parse_function_target() {
        // GIVEN: Function target string
        let target = "src/auth.py::login";

        // WHEN: Parsing target
        let parsed = parse_target(target).unwrap();

        // THEN: Should extract file and function
        assert_eq!(parsed.file, PathBuf::from("src/auth.py"));
        assert_eq!(parsed.function, Some("login".to_string()));
        assert!(parsed.line_range.is_none());
    }

    /// Test: Function-level comparison
    /// Contract: Compare only function bodies
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_function_level_comparison() {
        // GIVEN: Files with multiple functions
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir
            .add_file("a.py", PYTHON_MULTI_FUNCTION_FILE)
            .unwrap();
        let path_b = test_dir
            .add_file("b.py", PYTHON_MULTI_FUNCTION_FILE_B)
            .unwrap();

        // WHEN: Comparing specific functions
        let options = SimilarityOptions {
            level: Some(ComparisonLevel::Function),
            ..Default::default()
        };

        let target_a = format!("{}::third_function", path_a.display());
        let target_b = format!("{}::sum_items", path_b.display());

        let report = compute_similarity(
            &PathBuf::from(&target_a),
            &PathBuf::from(&target_b),
            &options,
        )
        .unwrap();

        // THEN: Should compare only those functions (high similarity)
        assert!(
            report.similarity.dice > 0.7,
            "Similar functions should have high Dice"
        );
    }

    /// Test: Function not found error
    /// Contract: Return error if function doesn't exist
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_function_not_found_error() {
        // GIVEN: File without target function
        let test_dir = TestDir::new().unwrap();
        let path = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();

        // WHEN: Trying to compare non-existent function
        let options = SimilarityOptions::default();
        let target = format!("{}::nonexistent_function", path.display());

        let result = compute_similarity(&PathBuf::from(&target), &path, &options);

        // THEN: Should return error
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("not found") || err_msg.contains("Function"),
            "Error should mention function not found"
        );
    }

    /// Test: Fragment info includes function name
    /// Contract: SimilarityFragment.function is set
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_fragment_includes_function_name() {
        // GIVEN: Function comparison
        let test_dir = TestDir::new().unwrap();
        let path = test_dir
            .add_file("a.py", PYTHON_MULTI_FUNCTION_FILE)
            .unwrap();

        // WHEN: Comparing function
        let options = SimilarityOptions::default();
        let target = format!("{}::first_function", path.display());

        let report =
            compute_similarity(&PathBuf::from(&target), &PathBuf::from(&target), &options).unwrap();

        // THEN: Fragment should have function name
        assert_eq!(
            report.fragment1.function,
            Some("first_function".to_string())
        );
    }
}

// =============================================================================
// File-Level Similarity Tests (D5)
// =============================================================================

#[cfg(test)]
mod file_level_tests {
    use super::fixtures::*;
    use crate::analysis::similarity::{compute_similarity, SimilarityOptions};
    

    /// Test: File-level comparison (default)
    /// Contract: Entire file content is compared
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_file_level_comparison() {
        // GIVEN: Two files
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.py", PYTHON_FUNC_B_SIMILAR).unwrap();

        // WHEN: Comparing files (default level)
        let options = SimilarityOptions::default();
        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // THEN: Should compare entire files
        assert!(
            report.fragment1.function.is_none(),
            "File-level should not have function"
        );
        assert!(
            report.fragment1.line_range.is_none(),
            "File-level should not have line range"
        );
    }

    /// Test: File token count is correct
    /// Contract: fragment.tokens reflects entire file
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_file_token_count() {
        // GIVEN: File
        let test_dir = TestDir::new().unwrap();
        let path = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();

        // WHEN: Computing similarity (file-level)
        let options = SimilarityOptions::default();
        let report = compute_similarity(&path, &path, &options).unwrap();

        // THEN: Token count should be positive and reasonable
        assert!(report.fragment1.tokens > 0);
        assert!(report.fragment1.lines > 0);
    }

    /// Test: File line count is correct
    /// Contract: fragment.lines reflects file line count
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_file_line_count() {
        // GIVEN: File with known line count
        let content = "line1\nline2\nline3\nline4\nline5\n";
        let test_dir = TestDir::new().unwrap();
        let path = test_dir.add_file("a.py", content).unwrap();

        // WHEN: Computing similarity
        let options = SimilarityOptions::default();
        let report = compute_similarity(&path, &path, &options).unwrap();

        // THEN: Line count should be correct
        assert!(report.fragment1.lines >= 5, "Should count at least 5 lines");
    }

    /// Test: Comparing same file
    /// Contract: File compared to itself has similarity 1.0
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_file_compared_to_self() {
        // GIVEN: Single file
        let test_dir = TestDir::new().unwrap();
        let path = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();

        // WHEN: Comparing file to itself
        let options = SimilarityOptions::default();
        let report = compute_similarity(&path, &path, &options).unwrap();

        // THEN: Should be 1.0
        assert!((report.similarity.dice - 1.0).abs() < 0.001);
    }
}

// =============================================================================
// Block-Level Similarity Tests (D6)
// =============================================================================

#[cfg(test)]
mod block_level_tests {
    use super::fixtures::*;
    use crate::analysis::similarity::{
        compute_similarity, parse_target, SimilarityOptions,
    };
    use std::path::PathBuf;

    /// Test: Parse block target syntax
    /// Contract: file.py:start:end is valid
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_parse_block_target() {
        // GIVEN: Block target string
        let target = "src/code.py:10:50";

        // WHEN: Parsing target
        let parsed = parse_target(target).unwrap();

        // THEN: Should extract file and line range
        assert_eq!(parsed.file, PathBuf::from("src/code.py"));
        assert_eq!(parsed.line_range, Some((10, 50)));
        assert!(parsed.function.is_none());
    }

    /// Test: Block-level comparison
    /// Contract: Compare only specified lines
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_block_level_comparison() {
        // GIVEN: File with distinct blocks
        let test_dir = TestDir::new().unwrap();
        let path = test_dir.add_file("code.py", FILE_WITH_BLOCKS).unwrap();

        // WHEN: Comparing similar blocks (A and B)
        let options = SimilarityOptions::default();
        let target_a = format!("{}:2:8", path.display()); // Block A
        let target_b = format!("{}:12:18", path.display()); // Block B

        let report = compute_similarity(
            &PathBuf::from(&target_a),
            &PathBuf::from(&target_b),
            &options,
        )
        .unwrap();

        // THEN: Should have high similarity (blocks A and B are similar)
        assert!(
            report.similarity.dice > 0.6,
            "Similar blocks should have high similarity"
        );
    }

    /// Test: Block with different content has low similarity
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_block_different_content() {
        // GIVEN: File with distinct blocks
        let test_dir = TestDir::new().unwrap();
        let path = test_dir.add_file("code.py", FILE_WITH_BLOCKS).unwrap();

        // WHEN: Comparing different blocks (A and C)
        let options = SimilarityOptions::default();
        let target_a = format!("{}:2:8", path.display()); // Block A
        let target_c = format!("{}:23:30", path.display()); // Block C (different)

        let report = compute_similarity(
            &PathBuf::from(&target_a),
            &PathBuf::from(&target_c),
            &options,
        )
        .unwrap();

        // THEN: Should have lower similarity
        assert!(
            report.similarity.dice < 0.8,
            "Different blocks should have lower similarity"
        );
    }

    /// Test: Invalid line range error
    /// Contract: start > end returns error
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_invalid_line_range() {
        // GIVEN: Invalid line range (start > end)
        let target = "file.py:50:10"; // 50 > 10

        // WHEN: Parsing
        let result = parse_target(target);

        // THEN: Should return error
        assert!(result.is_err());
        let err_msg = result.unwrap_err().to_string();
        assert!(
            err_msg.contains("Invalid") || err_msg.contains("range"),
            "Should indicate invalid range"
        );
    }

    /// Test: Line range out of bounds is clamped
    /// Contract: end > file_lines is clamped with warning
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_line_range_clamped() {
        // GIVEN: File with fewer lines than requested
        let short_file = "line1\nline2\nline3\n";
        let test_dir = TestDir::new().unwrap();
        let path = test_dir.add_file("short.py", short_file).unwrap();

        // WHEN: Requesting lines beyond file end
        let options = SimilarityOptions::default();
        let target = format!("{}:1:100", path.display());

        let result = compute_similarity(&PathBuf::from(&target), &path, &options);

        // THEN: Should succeed (clamped) or warn
        // Implementation choice: either clamp or error
        // For robustness, we accept either
        if let Ok(report) = result {
            assert!(report.fragment1.lines <= 5); // Can't have more lines than file
        }
        // Error is also acceptable
    }

    /// Test: Fragment includes line range
    /// Contract: SimilarityFragment.line_range is set
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_fragment_includes_line_range() {
        // GIVEN: Block comparison
        let test_dir = TestDir::new().unwrap();
        let path = test_dir.add_file("code.py", FILE_WITH_BLOCKS).unwrap();

        // WHEN: Comparing block
        let options = SimilarityOptions::default();
        let target = format!("{}:2:8", path.display());

        let report =
            compute_similarity(&PathBuf::from(&target), &PathBuf::from(&target), &options).unwrap();

        // THEN: Fragment should have line range
        assert_eq!(report.fragment1.line_range, Some((2, 8)));
    }
}

// =============================================================================
// N-gram Similarity Tests (D10)
// =============================================================================

#[cfg(test)]
mod ngram_tests {
    use super::fixtures::*;
    use crate::analysis::similarity::{compute_similarity, SimilarityOptions};
    

    /// Test: Default n-gram size is 1 (unigrams)
    /// Contract: ngram_size = 1 by default
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_default_ngram_size() {
        // GIVEN: Default options
        let options = SimilarityOptions::default();

        // THEN: Default ngram_size should be 1
        assert_eq!(options.ngram_size, 1);
    }

    /// Test: Bigram similarity (n=2)
    /// Contract: Captures token pairs
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_bigram_similarity() {
        // GIVEN: Two files
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.py", PYTHON_FUNC_B_SIMILAR).unwrap();

        // WHEN: Computing with bigrams
        let options = SimilarityOptions {
            ngram_size: 2,
            ..Default::default()
        };

        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // THEN: Should compute valid similarity
        assert!(report.similarity.dice >= 0.0 && report.similarity.dice <= 1.0);
    }

    /// Test: Higher n = stricter matching
    /// Contract: Larger n-grams capture more context, may lower similarity
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_higher_n_stricter() {
        // GIVEN: Two similar files
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.py", PYTHON_FUNC_B_SIMILAR).unwrap();

        // WHEN: Computing with unigrams and trigrams
        let options_1 = SimilarityOptions {
            ngram_size: 1,
            ..Default::default()
        };
        let report_1 = compute_similarity(&path_a, &path_b, &options_1).unwrap();

        let options_3 = SimilarityOptions {
            ngram_size: 3,
            ..Default::default()
        };
        let report_3 = compute_similarity(&path_a, &path_b, &options_3).unwrap();

        // THEN: Higher n typically gives same or lower similarity
        // (unless code is very similar)
        assert!(
            report_3.similarity.dice <= report_1.similarity.dice + 0.1,
            "Higher n should not dramatically increase similarity"
        );
    }

    /// Test: N-gram with short input
    /// Contract: If tokens < n, return 0 similarity
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_ngram_short_input() {
        // GIVEN: Very short files
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", "x").unwrap();
        let path_b = test_dir.add_file("b.py", "y").unwrap();

        // WHEN: Computing with trigrams (n=3)
        let options = SimilarityOptions {
            ngram_size: 3,
            ..Default::default()
        };

        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // THEN: Should handle gracefully (0 similarity)
        assert!(
            (report.similarity.dice - 0.0).abs() < 0.001,
            "Short input with large n should give 0 similarity"
        );
    }

    /// Test: Config includes ngram_size
    /// Contract: Report reflects ngram_size used
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_ngram_in_config() {
        // GIVEN: Custom ngram size
        let test_dir = TestDir::new().unwrap();
        let path = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();

        // WHEN: Computing with n=2
        let options = SimilarityOptions {
            ngram_size: 2,
            ..Default::default()
        };

        let report = compute_similarity(&path, &path, &options).unwrap();

        // THEN: Config should reflect n=2
        assert_eq!(report.config.ngram_size, 2);
    }
}

// =============================================================================
// Pairwise Similarity Matrix Tests (D11)
// =============================================================================

#[cfg(test)]
mod pairwise_matrix_tests {
    use super::fixtures::*;
    use crate::analysis::similarity::{compute_pairwise_similarity, SimilarityOptions};
    

    /// Test: Compute all pairwise similarities
    /// Contract: Returns n*(n-1)/2 unique pairs
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_pairwise_all_pairs() {
        // GIVEN: Directory with 3 files
        let test_dir = TestDir::new().unwrap();
        test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        test_dir.add_file("b.py", PYTHON_FUNC_B_SIMILAR).unwrap();
        test_dir.add_file("c.py", PYTHON_FUNC_DIFFERENT).unwrap();

        // WHEN: Computing pairwise matrix
        let options = SimilarityOptions::default();
        let matrix = compute_pairwise_similarity(test_dir.path(), &options).unwrap();

        // THEN: Should have 3 pairs (a-b, a-c, b-c)
        assert_eq!(matrix.pairs.len(), 3);
    }

    /// Test: Matrix entries are valid
    /// Contract: All scores in [0, 1]
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_pairwise_valid_scores() {
        // GIVEN: Directory with files
        let test_dir = TestDir::new().unwrap();
        test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        test_dir.add_file("b.py", PYTHON_FUNC_B_SIMILAR).unwrap();

        // WHEN: Computing pairwise matrix
        let options = SimilarityOptions::default();
        let matrix = compute_pairwise_similarity(test_dir.path(), &options).unwrap();

        // THEN: All scores should be valid
        for pair in &matrix.pairs {
            assert!(pair.dice >= 0.0 && pair.dice <= 1.0);
            assert!(pair.jaccard >= 0.0 && pair.jaccard <= 1.0);
        }
    }

    /// Test: Threshold filtering
    /// Contract: Only pairs above threshold are included
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_pairwise_threshold_filter() {
        // GIVEN: Files with varying similarity
        let test_dir = TestDir::new().unwrap();
        test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        test_dir.add_file("b.py", PYTHON_FUNC_A_COPY).unwrap(); // High sim
        test_dir
            .add_file("c.py", PYTHON_FUNC_VERY_DIFFERENT)
            .unwrap(); // Low sim

        // WHEN: Computing with high threshold
        let options = SimilarityOptions::default();
        // Assuming options has a threshold field for filtering
        // options.min_similarity = Some(0.8);

        let matrix_all = compute_pairwise_similarity(test_dir.path(), &options).unwrap();

        // THEN: Should have at least the high-similarity pair
        assert!(!matrix_all.pairs.is_empty());
    }

    /// Test: Single file returns empty matrix
    /// Contract: Need 2+ files for pairs
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_pairwise_single_file() {
        // GIVEN: Single file
        let test_dir = TestDir::new().unwrap();
        test_dir.add_file("only.py", PYTHON_FUNC_A).unwrap();

        // WHEN: Computing pairwise
        let options = SimilarityOptions::default();
        let matrix = compute_pairwise_similarity(test_dir.path(), &options).unwrap();

        // THEN: Should be empty
        assert!(matrix.pairs.is_empty());
    }

    /// Test: Pairs are sorted by similarity
    /// Contract: Most similar first (optional but nice)
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_pairwise_sorted() {
        // GIVEN: Multiple files
        let test_dir = TestDir::new().unwrap();
        test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        test_dir.add_file("b.py", PYTHON_FUNC_A_COPY).unwrap();
        test_dir.add_file("c.py", PYTHON_FUNC_DIFFERENT).unwrap();

        // WHEN: Computing pairwise
        let options = SimilarityOptions::default();
        let matrix = compute_pairwise_similarity(test_dir.path(), &options).unwrap();

        // THEN: Should be sorted (highest first)
        for i in 1..matrix.pairs.len() {
            assert!(
                matrix.pairs[i - 1].dice >= matrix.pairs[i].dice,
                "Pairs should be sorted by similarity descending"
            );
        }
    }
}

// =============================================================================
// Score Interpretation Tests (D12)
// =============================================================================

#[cfg(test)]
mod score_interpretation_tests {
    use super::fixtures::*;
    use crate::analysis::similarity::{
        compute_similarity, interpret_similarity_score, SimilarityOptions,
    };
    

    /// Test: Identical code interpretation
    /// Contract: >= 0.95 = "Near-identical"
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_interpretation_near_identical() {
        // GIVEN: Score >= 0.95
        let interpretation = interpret_similarity_score(0.98);

        // THEN: Should indicate near-identical
        assert!(
            interpretation.to_lowercase().contains("identical")
                || interpretation.to_lowercase().contains("near"),
            "High score should indicate near-identical"
        );
    }

    /// Test: High similarity interpretation
    /// Contract: 0.85-0.95 = "High similarity"
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_interpretation_high_similarity() {
        // GIVEN: Score in [0.85, 0.95)
        let interpretation = interpret_similarity_score(0.88);

        // THEN: Should indicate high similarity
        assert!(
            interpretation.to_lowercase().contains("high")
                || interpretation.to_lowercase().contains("similar"),
            "Score 0.88 should indicate high similarity"
        );
    }

    /// Test: Moderate similarity interpretation
    /// Contract: 0.70-0.85 = "Moderate similarity"
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_interpretation_moderate_similarity() {
        // GIVEN: Score in [0.70, 0.85)
        let interpretation = interpret_similarity_score(0.75);

        // THEN: Should indicate moderate similarity
        assert!(
            interpretation.to_lowercase().contains("moderate")
                || interpretation.to_lowercase().contains("possible"),
            "Score 0.75 should indicate moderate similarity"
        );
    }

    /// Test: Low similarity interpretation
    /// Contract: 0.50-0.70 = "Some similarity"
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_interpretation_some_similarity() {
        // GIVEN: Score in [0.50, 0.70)
        let interpretation = interpret_similarity_score(0.55);

        // THEN: Should indicate some similarity
        assert!(
            interpretation.to_lowercase().contains("some")
                || interpretation.to_lowercase().contains("shared"),
            "Score 0.55 should indicate some similarity"
        );
    }

    /// Test: Very low similarity interpretation
    /// Contract: < 0.30 = "Very different"
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_interpretation_very_different() {
        // GIVEN: Score < 0.30
        let interpretation = interpret_similarity_score(0.15);

        // THEN: Should indicate very different
        assert!(
            interpretation.to_lowercase().contains("different")
                || interpretation.to_lowercase().contains("low"),
            "Score 0.15 should indicate very different"
        );
    }

    /// Test: Interpretation included in report
    /// Contract: Report has interpretation field
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_interpretation_in_report() {
        // GIVEN: Similarity computation
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.py", PYTHON_FUNC_B_SIMILAR).unwrap();

        // WHEN: Computing similarity
        let options = SimilarityOptions::default();
        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // THEN: Should have interpretation
        assert!(
            !report.similarity.interpretation.is_empty(),
            "Report should include interpretation"
        );
    }

    /// Test: Boundary condition at 0.95
    /// Contract: 0.95 exactly should be "near-identical"
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_interpretation_boundary_095() {
        let interpretation = interpret_similarity_score(0.95);
        assert!(
            interpretation.to_lowercase().contains("identical")
                || interpretation.to_lowercase().contains("near")
        );
    }

    /// Test: Boundary condition at 0.70
    /// Contract: 0.70 exactly should be "moderate"
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_interpretation_boundary_070() {
        let interpretation = interpret_similarity_score(0.70);
        assert!(
            interpretation.to_lowercase().contains("moderate")
                || interpretation.to_lowercase().contains("possible")
        );
    }
}

// =============================================================================
// Token Breakdown Tests
// =============================================================================

#[cfg(test)]
mod token_breakdown_tests {
    use super::fixtures::*;
    use crate::analysis::similarity::{compute_similarity, SimilarityOptions};
    

    /// Test: Token breakdown is computed
    /// Contract: Report includes shared/unique token counts
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_token_breakdown_computed() {
        // GIVEN: Two files
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.py", PYTHON_FUNC_B_SIMILAR).unwrap();

        // WHEN: Computing similarity
        let options = SimilarityOptions::default();
        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // THEN: Token breakdown should be present
        assert!(report.token_breakdown.total_unique > 0);
    }

    /// Test: Shared tokens count
    /// Contract: shared_tokens <= min(tokens1, tokens2)
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_shared_tokens_valid() {
        // GIVEN: Two files
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.py", PYTHON_FUNC_B_SIMILAR).unwrap();

        // WHEN: Computing similarity
        let options = SimilarityOptions::default();
        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // THEN: Shared tokens should be valid
        let min_tokens = report.fragment1.tokens.min(report.fragment2.tokens);
        assert!(
            report.token_breakdown.shared_tokens <= min_tokens,
            "Shared tokens can't exceed smaller fragment"
        );
    }

    /// Test: Unique tokens formula
    /// Contract: total_unique = shared + unique1 + unique2
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_unique_tokens_formula() {
        // GIVEN: Two files
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.py", PYTHON_FUNC_DIFFERENT).unwrap();

        // WHEN: Computing similarity
        let options = SimilarityOptions::default();
        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // THEN: Formula should hold
        let expected_total = report.token_breakdown.shared_tokens
            + report.token_breakdown.unique_to_fragment1
            + report.token_breakdown.unique_to_fragment2;
        assert_eq!(report.token_breakdown.total_unique, expected_total);
    }

    /// Test: Identical files have no unique tokens
    /// Contract: unique_to_fragment1 = unique_to_fragment2 = 0
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_identical_no_unique() {
        // GIVEN: Identical files
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.py", PYTHON_FUNC_A_COPY).unwrap();

        // WHEN: Computing similarity
        let options = SimilarityOptions::default();
        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // THEN: No unique tokens (or very few due to normalization)
        assert!(
            report.token_breakdown.unique_to_fragment1 == 0
                || report.token_breakdown.unique_to_fragment1 < 3,
            "Identical files should have no/minimal unique tokens"
        );
    }
}

// =============================================================================
// Multi-Language Tests
// =============================================================================

#[cfg(test)]
mod multi_language_similarity_tests {
    use super::fixtures::*;
    use crate::analysis::similarity::{compute_similarity, SimilarityOptions};
    

    /// Test: Python similarity
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_python_similarity() {
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.py", PYTHON_FUNC_B_SIMILAR).unwrap();

        let options = crate::analysis::similarity::SimilarityOptions { language: Some("python".to_string()), ..Default::default() };

        let report = compute_similarity(&path_a, &path_b, &options).unwrap();
        assert!(report.similarity.dice > 0.5);
    }

    /// Test: TypeScript similarity
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_typescript_similarity() {
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.ts", TS_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.ts", TS_FUNC_B_SIMILAR).unwrap();

        let options = crate::analysis::similarity::SimilarityOptions { language: Some("typescript".to_string()), ..Default::default() };

        let report = compute_similarity(&path_a, &path_b, &options).unwrap();
        assert!(report.similarity.dice > 0.5);
    }

    /// Test: Go similarity
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_go_similarity() {
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.go", GO_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.go", GO_FUNC_B_SIMILAR).unwrap();

        let options = crate::analysis::similarity::SimilarityOptions { language: Some("go".to_string()), ..Default::default() };

        let report = compute_similarity(&path_a, &path_b, &options).unwrap();
        assert!(report.similarity.dice > 0.5);
    }

    /// Test: Rust similarity
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_rust_similarity() {
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.rs", RUST_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.rs", RUST_FUNC_B_SIMILAR).unwrap();

        let options = crate::analysis::similarity::SimilarityOptions { language: Some("rust".to_string()), ..Default::default() };

        let report = compute_similarity(&path_a, &path_b, &options).unwrap();
        assert!(report.similarity.dice > 0.5);
    }

    /// Test: Auto-detect language
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_auto_detect_language() {
        let test_dir = TestDir::new().unwrap();
        let path = test_dir.add_file("code.py", PYTHON_FUNC_A).unwrap();

        let options = SimilarityOptions::default(); // No language specified

        let report = compute_similarity(&path, &path, &options).unwrap();
        // Should succeed with auto-detection
        assert!((report.similarity.dice - 1.0).abs() < 0.001);
    }
}

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

#[cfg(test)]
mod edge_case_similarity_tests {
    use super::fixtures::*;
    use crate::analysis::similarity::{compute_similarity, SimilarityOptions};
    use std::path::PathBuf;

    /// Test: Empty file similarity
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_empty_file() {
        let test_dir = TestDir::new().unwrap();
        let path_empty = test_dir.add_file("empty.py", "").unwrap();
        let path_normal = test_dir.add_file("normal.py", PYTHON_FUNC_A).unwrap();

        let options = SimilarityOptions::default();
        let report = compute_similarity(&path_empty, &path_normal, &options).unwrap();

        assert!((report.similarity.dice - 0.0).abs() < 0.001);
    }

    /// Test: Both files empty
    /// Contract: Both empty = similarity undefined or 1.0
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_both_files_empty() {
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", "").unwrap();
        let path_b = test_dir.add_file("b.py", "").unwrap();

        let options = SimilarityOptions::default();
        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // Two empty files could be considered identical (1.0) or undefined
        // Accept either 0.0 or 1.0
        assert!(report.similarity.dice == 0.0 || report.similarity.dice == 1.0);
    }

    /// Test: File not found error
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_file_not_found() {
        let test_dir = TestDir::new().unwrap();
        let path_existing = test_dir.add_file("exists.py", PYTHON_FUNC_A).unwrap();
        let path_missing = PathBuf::from("nonexistent.py");

        let options = SimilarityOptions::default();
        let result = compute_similarity(&path_existing, &path_missing, &options);

        assert!(result.is_err());
    }

    /// Test: Binary file handling
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_binary_file() {
        let test_dir = TestDir::new().unwrap();
        let path_normal = test_dir.add_file("code.py", PYTHON_FUNC_A).unwrap();
        let binary_path = test_dir.dir.path().join("binary.bin");
        std::fs::write(&binary_path, [0u8, 159, 146, 150]).unwrap();

        let options = SimilarityOptions::default();
        let result = compute_similarity(&path_normal, &binary_path, &options);

        // Should either error or return 0 similarity
        if let Ok(report) = result {
            assert!(report.similarity.dice < 0.1);
        }
    }

    /// Test: Very long file
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_long_file_performance() {
        // Generate a long file
        let long_content: String = (0..1000)
            .map(|i| format!("def func{}(): return {}\n", i, i))
            .collect();

        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("long_a.py", &long_content).unwrap();
        let path_b = test_dir.add_file("long_b.py", &long_content).unwrap();

        let options = SimilarityOptions::default();
        let start = std::time::Instant::now();
        let report = compute_similarity(&path_a, &path_b, &options).unwrap();
        let duration = start.elapsed();

        // Should complete in reasonable time (< 5s)
        assert!(duration.as_secs() < 5, "Long file comparison took too long");
        assert!((report.similarity.dice - 1.0).abs() < 0.001);
    }
}

// =============================================================================
// JSON Serialization Tests
// =============================================================================

#[cfg(test)]
mod serialization_tests {
    use super::fixtures::*;
    use crate::analysis::similarity::{compute_similarity, SimilarityOptions, SimilarityReport};
    

    /// Test: Report serializes to JSON
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_json_serialization() {
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.py", PYTHON_FUNC_B_SIMILAR).unwrap();

        let options = SimilarityOptions::default();
        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // Should serialize without error
        let json = serde_json::to_string(&report);
        assert!(json.is_ok());
    }

    /// Test: Report deserializes from JSON
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_json_deserialization() {
        let test_dir = TestDir::new().unwrap();
        let path_a = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();
        let path_b = test_dir.add_file("b.py", PYTHON_FUNC_B_SIMILAR).unwrap();

        let options = SimilarityOptions::default();
        let report = compute_similarity(&path_a, &path_b, &options).unwrap();

        // Serialize then deserialize
        let json = serde_json::to_string(&report).unwrap();
        let deserialized: Result<SimilarityReport, _> = serde_json::from_str(&json);

        assert!(deserialized.is_ok());
        let restored = deserialized.unwrap();
        assert!((restored.similarity.dice - report.similarity.dice).abs() < 0.001);
    }

    /// Test: Config is included in JSON
    #[test]
    #[ignore = "similarity module not yet implemented"]
    fn test_config_in_json() {
        let test_dir = TestDir::new().unwrap();
        let path = test_dir.add_file("a.py", PYTHON_FUNC_A).unwrap();

        let options = SimilarityOptions {
            ngram_size: 3,
            ..Default::default()
        };

        let report = compute_similarity(&path, &path, &options).unwrap();
        let json = serde_json::to_string(&report).unwrap();

        // JSON should contain config
        assert!(
            json.contains("ngram_size") || json.contains("ngram"),
            "JSON should include config"
        );
    }
}