pmat 3.16.0

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
//! Duplicate detection analysis - finds duplicate code blocks

use anyhow::Result;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::{Path, PathBuf};

include!("duplicates_detection.rs");
include!("duplicates_extraction.rs");
include!("duplicates_output.rs");

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
    use super::*;
    use std::path::Path;

    fn strip_ansi(s: &str) -> String {
        let re = regex::Regex::new(r"\x1b\[[0-9;]*m").unwrap();
        re.replace_all(s, "").to_string()
    }

    #[test]
    fn test_normalize_block() {
        let lines = vec!["  fn test() {", "    // comment", "    let x = 1;", "  }"];
        let normalized = normalize_block(&lines);
        assert!(!normalized.contains("// comment"));
        assert!(normalized.contains("fn test()"));
        assert_eq!(normalized, "fn test() {\nlet x = 1;\n}");
    }

    #[test]
    fn test_count_tokens() {
        assert_eq!(count_tokens("fn test() { }"), 4);
        assert_eq!(count_tokens("let x = 1;"), 4);
        assert_eq!(count_tokens(""), 0);
        assert_eq!(count_tokens("  \n  \t  "), 0);
    }

    #[test]
    fn test_is_function_declaration() {
        assert!(is_function_declaration("fn main() {"));
        assert!(is_function_declaration("function test() {"));
        assert!(is_function_declaration("def calculate():"));
        assert!(!is_function_declaration("let x = 1;"));
    }

    #[test]
    fn test_is_type_declaration() {
        assert!(is_type_declaration("class Foo {"));
        assert!(is_type_declaration("struct Bar {"));
        assert!(is_type_declaration("impl Display for Foo {"));
        assert!(!is_type_declaration("let x = 1;"));
    }

    #[test]
    fn test_is_block_opening() {
        assert!(is_block_opening("fn main() {"));
        assert!(is_block_opening("if true {"));
        assert!(!is_block_opening("{ x: 1 }"));
        assert!(!is_block_opening("let x = 1;"));
    }

    #[test]
    fn test_is_block_start() {
        // Function declarations
        assert!(is_block_start("fn main() {"));
        assert!(is_block_start("function test() {"));
        assert!(is_block_start("def calculate():"));

        // Type declarations
        assert!(is_block_start("class Foo {"));
        assert!(is_block_start("struct Bar {"));
        assert!(is_block_start("impl Display for Foo {"));

        // Block openings
        assert!(is_block_start("if condition {"));

        // Not block starts
        assert!(!is_block_start("let x = 1;"));
        assert!(!is_block_start("{ x: 1 }"));
    }

    #[test]
    fn test_is_source_file() {
        assert!(is_source_file(Path::new("test.rs")));
        assert!(is_source_file(Path::new("test.js")));
        assert!(is_source_file(Path::new("test.ts")));
        assert!(is_source_file(Path::new("test.py")));
        assert!(is_source_file(Path::new("test.java")));
        assert!(is_source_file(Path::new("test.cpp")));
        assert!(is_source_file(Path::new("test.c")));
        assert!(is_source_file(Path::new("test.kt")));
        assert!(is_source_file(Path::new("test.kts")));
        assert!(!is_source_file(Path::new("test.txt")));
        assert!(!is_source_file(Path::new("README.md")));
    }

    #[test]
    fn test_should_process_file() {
        let path = Path::new("src/main.rs");

        // No filters
        assert!(should_process_file(path, &None, &None));

        // Include filter
        assert!(should_process_file(path, &Some("src".to_string()), &None));
        assert!(!should_process_file(
            path,
            &Some("tests".to_string()),
            &None
        ));

        // Exclude filter
        assert!(!should_process_file(path, &None, &Some("src".to_string())));
        assert!(should_process_file(path, &None, &Some("tests".to_string())));

        // Both filters (exclude takes precedence)
        assert!(!should_process_file(
            path,
            &Some("src".to_string()),
            &Some("src".to_string())
        ));
    }

    #[test]
    fn test_find_block_end() {
        let lines = vec![
            "fn test() {",
            "    let x = 1;",
            "    if true {",
            "        println!(\"hello\");",
            "    }",
            "}",
        ];

        assert_eq!(find_block_end(&lines), Some(6));

        let lines2 = vec!["fn test() {", "    let x = 1;"];
        assert_eq!(find_block_end(&lines2), None);
    }

    #[test]
    fn test_extract_exact_blocks() {
        let lines = vec![
            "fn test1() {",
            "    let x = 1;",
            "    println!(\"x = {}\", x);",
            "}",
            "",
            "fn test2() {",
            "    let y = 2;",
            "    println!(\"y = {}\", y);",
            "}",
        ];

        let mut blocks = Vec::new();
        extract_exact_blocks(&mut blocks, &lines, "test.rs", 3, 100);

        // Should find multiple sliding windows
        assert!(!blocks.is_empty());
        assert!(blocks.iter().all(|(_, file, _, _, _)| file == "test.rs"));
    }

    #[test]
    fn test_find_duplicate_blocks_no_duplicates() {
        let blocks = vec![
            (
                "hash1".to_string(),
                "file1.rs".to_string(),
                1,
                10,
                "content1".to_string(),
            ),
            (
                "hash2".to_string(),
                "file2.rs".to_string(),
                1,
                10,
                "content2".to_string(),
            ),
        ];

        let duplicates = find_duplicate_blocks(blocks, 0.8);
        assert!(duplicates.is_empty());
    }

    #[test]
    fn test_find_duplicate_blocks_with_duplicates() {
        let blocks = vec![
            (
                "hash1".to_string(),
                "file1.rs".to_string(),
                1,
                10,
                "content1".to_string(),
            ),
            (
                "hash1".to_string(),
                "file2.rs".to_string(),
                20,
                29,
                "content1".to_string(),
            ),
            (
                "hash2".to_string(),
                "file3.rs".to_string(),
                1,
                5,
                "content2".to_string(),
            ),
        ];

        let duplicates = find_duplicate_blocks(blocks, 0.8);
        assert_eq!(duplicates.len(), 1);
        assert_eq!(duplicates[0].hash, "hash1");
        assert_eq!(duplicates[0].locations.len(), 2);
        assert_eq!(duplicates[0].lines, 10);
    }

    #[test]
    fn test_file_stats_calculation() {
        let mut stats = FileStats {
            duplicate_lines: 20,
            total_lines: 100,
            duplication_percentage: 0.0,
        };

        // Calculate percentage
        stats.duplication_percentage =
            (stats.duplicate_lines as f32 / stats.total_lines as f32) * 100.0;
        assert_eq!(stats.duplication_percentage, 20.0);
    }

    #[tokio::test]
    async fn test_detect_duplicates_empty_project() {
        use tempfile::TempDir;

        let temp_dir = TempDir::new().unwrap();
        let result = detect_duplicates(
            temp_dir.path(),
            crate::cli::DuplicateType::Exact,
            0.8,
            5,
            100,
            &None,
            &None,
        )
        .await;

        assert!(result.is_ok());
        let report = result.unwrap();
        assert_eq!(report.total_duplicates, 0);
        assert_eq!(report.duplicate_lines, 0);
        assert_eq!(report.total_lines, 0);
        assert_eq!(report.duplication_percentage, 0.0);
    }

    #[test]
    fn test_format_json_output() {
        let report = DuplicateReport {
            total_duplicates: 1,
            duplicate_lines: 10,
            total_lines: 100,
            duplication_percentage: 10.0,
            duplicate_blocks: vec![],
            file_statistics: HashMap::new(),
        };

        let result = format_json_output(&report);
        assert!(result.is_ok());
        let json = result.unwrap();
        assert!(json.contains("\"total_duplicates\": 1"));
        assert!(json.contains("\"duplication_percentage\": 10.0"));
    }

    #[test]
    fn test_format_human_output() {
        let report = DuplicateReport {
            total_duplicates: 2,
            duplicate_lines: 20,
            total_lines: 100,
            duplication_percentage: 20.0,
            duplicate_blocks: vec![DuplicateBlock {
                hash: "hash1".to_string(),
                locations: vec![
                    DuplicateLocation {
                        file: "file1.rs".to_string(),
                        start_line: 10,
                        end_line: 20,
                        content_preview: "fn test() {".to_string(),
                    },
                    DuplicateLocation {
                        file: "file2.rs".to_string(),
                        start_line: 30,
                        end_line: 40,
                        content_preview: "fn test() {".to_string(),
                    },
                ],
                lines: 10,
                tokens: 20,
                similarity: 1.0,
            }],
            file_statistics: HashMap::new(),
        };

        let result = format_human_output(&report);
        assert!(result.is_ok());
        let output = strip_ansi(&result.unwrap());
        assert!(output.contains("Duplicate Code Analysis"));
        assert!(output.contains("Total duplicate blocks:"));
        assert!(output.contains("2"));
        assert!(output.contains("Block 1"));
        assert!(output.contains("10 lines, 2 locations"));
    }

    // ─────────────────────────────────────────────────────────────────────
    // duplicates_output.rs: format_output dispatcher + sarif/csv +
    // write_top_files_section + write_remaining_blocks_count + extract_filename
    // ─────────────────────────────────────────────────────────────────────

    fn populated_report_with_blocks(n_blocks: usize) -> DuplicateReport {
        let blocks = (0..n_blocks)
            .map(|i| DuplicateBlock {
                hash: format!("h{i}"),
                locations: vec![
                    DuplicateLocation {
                        file: format!("a-{i}.rs"),
                        start_line: 1,
                        end_line: 5,
                        content_preview: "preview".to_string(),
                    },
                    DuplicateLocation {
                        file: format!("b-{i}.rs"),
                        start_line: 1,
                        end_line: 5,
                        content_preview: "preview".to_string(),
                    },
                ],
                lines: 5,
                tokens: 10,
                similarity: 1.0,
            })
            .collect();
        let mut file_stats = HashMap::new();
        file_stats.insert(
            "src/x.rs".to_string(),
            FileStats {
                duplicate_lines: 10,
                total_lines: 50,
                duplication_percentage: 20.0,
            },
        );
        file_stats.insert(
            "src/y.rs".to_string(),
            FileStats {
                duplicate_lines: 5,
                total_lines: 100,
                duplication_percentage: 5.0,
            },
        );
        DuplicateReport {
            total_duplicates: n_blocks,
            duplicate_lines: n_blocks * 5,
            total_lines: 1000,
            duplication_percentage: 5.0,
            duplicate_blocks: blocks,
            file_statistics: file_stats,
        }
    }

    #[test]
    fn test_format_output_dispatcher_human_arm() {
        let r = populated_report_with_blocks(1);
        // Human/Summary/Detailed all go to format_human_output.
        let out = format_output(&r, crate::cli::DuplicateOutputFormat::Human).unwrap();
        let stripped = strip_ansi(&out);
        assert!(stripped.contains("Duplicate Code Analysis"));
    }

    #[test]
    fn test_format_output_dispatcher_summary_and_detailed_arms() {
        let r = populated_report_with_blocks(1);
        // Summary + Detailed share the human-output arm; verify both succeed.
        format_output(&r, crate::cli::DuplicateOutputFormat::Summary).unwrap();
        format_output(&r, crate::cli::DuplicateOutputFormat::Detailed).unwrap();
    }

    #[test]
    fn test_format_output_dispatcher_json_arm() {
        let r = populated_report_with_blocks(2);
        let out = format_output(&r, crate::cli::DuplicateOutputFormat::Json).unwrap();
        assert!(out.contains("\"total_duplicates\""));
        assert!(out.contains("\"entropy_analysis\""));
        assert!(out.contains("\"metrics\""));
    }

    #[test]
    fn test_format_output_dispatcher_sarif_arm() {
        let r = populated_report_with_blocks(2);
        let out = format_output(&r, crate::cli::DuplicateOutputFormat::Sarif).unwrap();
        assert!(out.contains("\"version\": \"2.1.0\""));
        assert!(out.contains("duplicate-code"));
    }

    #[test]
    fn test_format_output_dispatcher_csv_arm() {
        let r = populated_report_with_blocks(2);
        let out = format_output(&r, crate::cli::DuplicateOutputFormat::Csv).unwrap();
        assert!(out.starts_with("Type,File1,Start1,End1,File2,Start2,End2\n"));
        // 2 blocks → 2 data rows.
        assert_eq!(out.lines().filter(|l| l.starts_with("exact,")).count(), 2);
    }

    #[test]
    fn test_format_csv_skips_blocks_with_under_two_locations() {
        let mut r = populated_report_with_blocks(0);
        r.duplicate_blocks.push(DuplicateBlock {
            hash: "single".to_string(),
            locations: vec![DuplicateLocation {
                file: "only.rs".to_string(),
                start_line: 1,
                end_line: 2,
                content_preview: String::new(),
            }],
            lines: 1,
            tokens: 1,
            similarity: 1.0,
        });
        let out = format_csv_output(&r).unwrap();
        // Header only — single-location block dropped.
        assert_eq!(out, "Type,File1,Start1,End1,File2,Start2,End2\n");
    }

    #[test]
    fn test_human_output_with_file_stats_emits_top_files() {
        let r = populated_report_with_blocks(1);
        let out = strip_ansi(&format_human_output(&r).unwrap());
        // file_statistics non-empty → "Top Files by Duplication" section emitted.
        assert!(out.contains("Top Files by Duplication"));
        // Filenames extracted from full paths.
        assert!(out.contains("x.rs"));
        assert!(out.contains("y.rs"));
    }

    #[test]
    fn test_human_output_skips_top_files_when_stats_empty() {
        let mut r = populated_report_with_blocks(0);
        r.file_statistics.clear();
        let out = strip_ansi(&format_human_output(&r).unwrap());
        assert!(!out.contains("Top Files by Duplication"));
    }

    #[test]
    fn test_human_output_with_more_than_20_blocks_shows_remaining_count() {
        let r = populated_report_with_blocks(25);
        let out = strip_ansi(&format_human_output(&r).unwrap());
        // First 20 emitted; remainder shown as "... and 5 more blocks".
        assert!(out.contains("... and 5 more blocks"));
    }

    #[test]
    fn test_human_output_no_remaining_count_when_blocks_le_20() {
        let r = populated_report_with_blocks(15);
        let out = strip_ansi(&format_human_output(&r).unwrap());
        assert!(!out.contains("more blocks"));
    }

    #[test]
    fn test_extract_filename_handles_paths_and_bare_names() {
        // Bare name → returned unchanged.
        assert_eq!(extract_filename("foo.rs"), "foo.rs");
        // Full path → only basename returned.
        assert_eq!(extract_filename("src/cli/foo.rs"), "foo.rs");
        // No extension → still returns basename.
        assert_eq!(extract_filename("src/cli/Makefile"), "Makefile");
    }

    #[test]
    fn test_get_sorted_file_stats_sorts_by_dup_pct_desc() {
        let mut stats = HashMap::new();
        stats.insert(
            "low.rs".to_string(),
            FileStats {
                duplicate_lines: 1,
                total_lines: 100,
                duplication_percentage: 1.0,
            },
        );
        stats.insert(
            "high.rs".to_string(),
            FileStats {
                duplicate_lines: 50,
                total_lines: 100,
                duplication_percentage: 50.0,
            },
        );
        stats.insert(
            "mid.rs".to_string(),
            FileStats {
                duplicate_lines: 10,
                total_lines: 100,
                duplication_percentage: 10.0,
            },
        );
        let sorted = get_sorted_file_stats(&stats);
        // Descending order: high → mid → low.
        assert_eq!(sorted[0].0, "high.rs");
        assert_eq!(sorted[1].0, "mid.rs");
        assert_eq!(sorted[2].0, "low.rs");
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod property_tests {
    use proptest::prelude::*;

    proptest! {
        #[test]
        fn basic_property_stability(_input in ".*") {
            // Basic property test for coverage
            prop_assert!(true);
        }

        #[test]
        fn module_consistency_check(_x in 0u32..1000) {
            // Module consistency verification
            prop_assert!(_x < 1001);
        }
    }
}