pmat 3.15.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
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
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
    use crate::services::file_classifier::*;
    use proptest::prelude::*;
    use std::path::PathBuf;

    // Strategy for generating file paths (avoiding vendor patterns)
    prop_compose! {
        fn arb_file_path()
            (
                segments in prop::collection::vec("[a-zA-Z0-9_]+", 1..3),
                extension in prop::option::of("[a-zA-Z0-9]+")
            )
            -> PathBuf
        {
            let mut path = segments.join("/");
            if let Some(ext) = extension {
                path.push('.');
                path.push_str(&ext);
            }
            // Ensure path doesn't accidentally match vendor patterns
            if path.contains("min") || path.contains("vendor") || path.contains("node_modules") {
                return PathBuf::from("src/safe_test_file.rs");
            }
            PathBuf::from(path)
        }
    }

    // Strategy for generating vendor-like paths
    prop_compose! {
        fn arb_vendor_path()
            (
                prefix in prop::option::of("[a-zA-Z0-9_/]+"),
                vendor_dir in prop::sample::select(vec![
                    "vendor", "node_modules", "third_party", "external",
                    ".yarn", "bower_components"
                ]),
                suffix in "[a-zA-Z0-9_/.-]+",
            )
            -> PathBuf
        {
            let mut path = String::new();
            if let Some(p) = prefix {
                path.push_str(&p);
                path.push('/');
            }
            path.push_str(vendor_dir);
            path.push('/');
            path.push_str(&suffix);
            PathBuf::from(path)
        }
    }

    // Strategy for generating build artifact paths
    prop_compose! {
        fn arb_build_artifact_path()
            (
                prefix in prop::option::of("[a-zA-Z0-9_/]+"),
                build_dir in prop::sample::select(vec![
                    "target/debug", "target/release", "build", "dist",
                    "__pycache__", "venv", ".tox", "cmake-build-debug", ".gradle"
                ]),
                suffix in "[a-zA-Z0-9_/.-]+",
            )
            -> PathBuf
        {
            let mut path = String::new();
            if let Some(p) = prefix {
                path.push_str(&p);
                path.push('/');
            }
            path.push_str(build_dir);
            path.push('/');
            path.push_str(&suffix);
            PathBuf::from(path)
        }
    }

    // Strategy for generating file content (reduced size for faster tests)
    prop_compose! {
        fn arb_file_content()
            (
                content_type in prop::sample::select(vec!["text", "binary", "mixed"]),
                size in 0usize..1000,  // Reduced from 10000
            )
            -> Vec<u8>
        {
            match content_type {
                "text" => {
                    let mut content = Vec::with_capacity(size);
                    for i in 0..size {
                        if i % 80 == 79 {
                            content.push(b'\n');
                        } else {
                            content.push(b'a' + ((i % 26) as u8));
                        }
                    }
                    content
                }
                "binary" => {
                    let mut content = Vec::with_capacity(size);
                    for i in 0..size {
                        content.push((i % 256) as u8);
                    }
                    content
                }
                _ => {
                    let mut content = Vec::with_capacity(size);
                    for i in 0..size {
                        if i % 100 < 80 {
                            content.push(b'a' + ((i % 26) as u8));
                        } else {
                            content.push((i % 256) as u8);
                        }
                    }
                    content
                }
            }
        }
    }

    // Strategy for generating minified-like content
    prop_compose! {
        fn arb_minified_content()
            (
                size in 100usize..500,  // Reduced from 5000
                has_signature in any::<bool>(),
            )
            -> Vec<u8>
        {
            let mut content = Vec::with_capacity(size);

            // Add minified signature if requested
            if has_signature {
                let signatures: Vec<&[u8]> = vec![
                    b"/*! jQuery",
                    b"/*! * Bootstrap",
                    b"!function(e,t){",
                    b"/*! For license information",
                    b"/** @license React",
                ];
                let sig = signatures[size % signatures.len()];
                content.extend_from_slice(sig);
            }

            // Generate high-entropy content with few newlines
            for i in 0..size {
                if i % 1000 == 999 {
                    content.push(b'\n');
                } else {
                    // High variety of characters for high entropy
                    let chars = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()_+-=[]{}|;:,.<>?";
                    content.push(chars[i % chars.len()]);
                }
            }

            content
        }
    }

    // Strategy for generating file classifier config
    prop_compose! {
        fn arb_file_classifier_config()
            (
                skip_vendor in any::<bool>(),
                max_line_length in 100usize..2000,  // Reduced from 20000
                max_file_size in 1000usize..50000,  // Reduced from 5000000
            )
            -> FileClassifierConfig
        {
            FileClassifierConfig {
                skip_vendor,
                max_line_length,
                max_file_size,
            }
        }
    }

    proptest! {
        /// Property: Empty files are always skipped
        #[test]
        fn empty_files_always_skipped(path in arb_file_path()) {
            let classifier = FileClassifier::default();
            let decision = classifier.should_parse(&path, b"");
            prop_assert_eq!(decision, ParseDecision::Skip(SkipReason::EmptyFile));
        }

        /// Property: Files over max size are always skipped
        #[test]
        fn oversized_files_always_skipped(
            path in arb_file_path(),
            extra_bytes in 1usize..10000
        ) {
            let classifier = FileClassifier::default();
            let content = vec![b'a'; classifier.max_file_size + extra_bytes];
            let decision = classifier.should_parse(&path, &content);
            prop_assert_eq!(decision, ParseDecision::Skip(SkipReason::FileTooLarge));
        }

        /// Property: Vendor paths are skipped when skip_vendor is true
        #[test]
        fn vendor_paths_skipped_when_configured(
            vendor_path in arb_vendor_path(),
            content in arb_file_content()
        ) {
            let classifier = FileClassifier {
                skip_vendor: true,
                ..Default::default()
            };

            // Skip if content triggers other skip reasons
            if !content.is_empty() && content.len() < LARGE_FILE_THRESHOLD {
                let decision = classifier.should_parse(&vendor_path, &content);

                // Should be skipped, but might be for other reasons (binary, minified, etc)
                prop_assert!(matches!(decision, ParseDecision::Skip(_)));
            }
        }

        /// Property: Build artifacts are always skipped
        #[test]
        fn build_artifacts_always_skipped(
            build_path in arb_build_artifact_path(),
            content_size in 1usize..1000
        ) {
            let classifier = FileClassifier::default();
            let content = vec![b'a'; content_size];
            let decision = classifier.should_parse(&build_path, &content);
            prop_assert_eq!(decision, ParseDecision::Skip(SkipReason::BuildArtifact));
        }

        /// Property: Binary content is detected and skipped
        #[test]
        fn binary_content_detected(
            binary_ratio in 0.4f64..1.0
        ) {
            let classifier = FileClassifier::default();
            let size = 1000;
            let mut content = Vec::with_capacity(size);

            // Generate content with specified ratio of non-printable chars
            for i in 0..size {
                if (i as f64 / size as f64) < binary_ratio {
                    content.push((i % 32) as u8); // Non-printable
                } else {
                    content.push(b'a' + ((i % 26) as u8)); // Printable
                }
            }

            // Use a safe path that won't trigger vendor detection
            let safe_path = PathBuf::from("src/test_file.dat");
            let decision = classifier.should_parse(&safe_path, &content);

            // Should be skipped as binary
            prop_assert!(matches!(decision, ParseDecision::Skip(SkipReason::BinaryContent)));
        }

        /// Property: Files with null bytes are binary
        #[test]
        fn null_bytes_mean_binary(
            prefix in prop::collection::vec(32u8..127, 0..100),
            suffix in prop::collection::vec(32u8..127, 0..100),
        ) {
            let classifier = FileClassifier::default();
            let mut content = prefix;
            content.push(0); // Add null byte
            content.extend(suffix);

            // Use a safe path that won't trigger vendor detection
            let safe_path = PathBuf::from("src/test_file.dat");
            let decision = classifier.should_parse(&safe_path, &content);
            prop_assert_eq!(decision, ParseDecision::Skip(SkipReason::BinaryContent));
        }

        /// Property: Lines over max length trigger skip
        #[test]
        fn long_lines_trigger_skip(
            line_length in 10100usize..10500,  // Reduced range for faster tests
        ) {
            let classifier = FileClassifier::default();

            // Create content with a very long line
            let mut content = String::new();
            content.push_str("normal line\n");
            content.push_str(&"a".repeat(line_length));
            content.push_str("\nanother normal line");

            // Use a safe path that won't trigger vendor detection
            let safe_path = PathBuf::from("src/test_file.js");
            let decision = classifier.should_parse(&safe_path, content.as_bytes());
            prop_assert_eq!(decision, ParseDecision::Skip(SkipReason::LineTooLong));
        }

        /// Property: File classification is deterministic for same data
        #[test]
        fn classification_deterministic_for_same_data(data in prop::collection::vec(any::<u8>(), 1..1000)) {
            // We can't test entropy directly, but we can test that classification is deterministic
            let classifier = FileClassifier::default();
            let path = PathBuf::from("test.dat");

            let decision1 = classifier.should_parse(&path, &data);
            let decision2 = classifier.should_parse(&path, &data);

            prop_assert_eq!(decision1, decision2);
        }

        /// Property: High entropy content is detected as minified
        #[test]
        fn high_entropy_detected_as_minified(
            minified_content in arb_minified_content()
        ) {
            let classifier = FileClassifier::default();

            // Only test if content isn't too large or empty
            if !minified_content.is_empty() && minified_content.len() < LARGE_FILE_THRESHOLD {
                // Use a safe path that won't trigger vendor detection
                let safe_path = PathBuf::from("src/test_file.js");
                let decision = classifier.should_parse(&safe_path, &minified_content);

                // Should be skipped as minified or for line length
                prop_assert!(matches!(
                    decision,
                    ParseDecision::Skip(SkipReason::MinifiedContent) |
                    ParseDecision::Skip(SkipReason::LineTooLong)
                ));
            }
        }

        /// Property: Include large files flag works correctly
        #[test]
        fn include_large_files_flag_behavior(
            size in (LARGE_FILE_THRESHOLD + 1)..DEFAULT_MAX_FILE_SIZE
        ) {
            // Create a safe path that won't trigger vendor/build artifact detection
            let path = PathBuf::from("src/large_test_file.rs");

            let classifier = FileClassifier::default();
            let mut content = String::new();

            // Generate content with newlines to avoid LineTooLong
            // Each line is 99 chars + newline = 100 bytes
            let num_lines = size / 100;
            for _i in 0..num_lines {
                content.push_str(&"a".repeat(99));
                content.push('\n');
            }
            // Pad to exact size to ensure we're above threshold
            let remaining = size - content.len();
            if remaining > 0 {
                // Add remaining as short lines to avoid LineTooLong
                for _ in 0..remaining {
                    content.push('b');
                }
            }
            let content_bytes = content.as_bytes();

            // Verify content is actually above threshold
            prop_assert!(content_bytes.len() > LARGE_FILE_THRESHOLD,
                "Content size {} should be > threshold {}",
                content_bytes.len(), LARGE_FILE_THRESHOLD);

            // Without flag - should skip large files
            let decision = classifier.should_parse_with_options(&path, content_bytes, false);
            prop_assert_eq!(decision, ParseDecision::Skip(SkipReason::LargeFile));

            // With flag - should parse large files (unless other skip conditions apply)
            let decision = classifier.should_parse_with_options(&path, content_bytes, true);
            // Check that it's not skipped for LargeFile reason
            prop_assert_ne!(decision, ParseDecision::Skip(SkipReason::LargeFile));

            // It should either parse or skip for a different reason (not LargeFile)
            match decision {
                ParseDecision::Parse => {},
                ParseDecision::Skip(reason) => {
                    prop_assert_ne!(reason, SkipReason::LargeFile);
                }
            }
        }

        /// Property: Decision is deterministic for same input
        #[test]
        fn decision_deterministic(
            path in arb_file_path(),
            content in arb_file_content()
        ) {
            let classifier = FileClassifier::default();

            let decision1 = classifier.should_parse(&path, &content);
            let decision2 = classifier.should_parse(&path, &content);
            let decision3 = classifier.should_parse(&path, &content);

            prop_assert_eq!(decision1, decision2);
            prop_assert_eq!(decision2, decision3);
        }

        /// Property: Skip reasons have correct priority
        #[test]
        fn skip_reason_priority_correct(
            size_factor in 0.0f64..3.0,
            _has_null in any::<bool>(),
            _has_long_line in any::<bool>(),
        ) {
            let classifier = FileClassifier::default();

            // Empty file has highest priority
            let empty_decision = classifier.should_parse(&PathBuf::from("test.js"), b"");
            prop_assert_eq!(empty_decision, ParseDecision::Skip(SkipReason::EmptyFile));

            // File too large has second priority
            if size_factor > 2.0 {
                let large_content = vec![b'a'; (DEFAULT_MAX_FILE_SIZE as f64 * size_factor) as usize];
                let large_decision = classifier.should_parse(&PathBuf::from("test.js"), &large_content);
                prop_assert_eq!(large_decision, ParseDecision::Skip(SkipReason::FileTooLarge));
            }
        }

        /// Property: Normal source files are parsed
        #[test]
        fn normal_source_files_parsed(
            segments in prop::collection::vec("[a-zA-Z0-9_-]+", 1..3),
            extension in prop::sample::select(vec!["rs", "js", "py", "java", "cpp", "go"]),
            lines in prop::collection::vec("[a-zA-Z ]+(\\{|\\}|\\(|\\)|;|,|\\.|_|-){0,5}[a-zA-Z ]*", 5..50)
        ) {
            let classifier = FileClassifier::default();

            let mut path = segments.join("/");
            path.push_str("/file.");
            path.push_str(extension);

            // Ensure content has reasonable structure (not just numbers or single chars)
            let mut content = String::new();
            for (i, line) in lines.iter().enumerate() {
                if i > 0 {
                    content.push('\n');
                }
                // Add some whitespace to make it look like code
                if i % 4 == 0 {
                    content.push_str("    "); // Indentation
                }
                content.push_str(line);
            }

            // Only test if content has multiple lines and isn't too short
            if lines.len() >= 5 && content.len() > 50 {
                let decision = classifier.should_parse(&PathBuf::from(path), content.as_bytes());
                prop_assert_eq!(decision, ParseDecision::Parse);
            }
        }

        /// Property: Uniform data is not detected as minified
        #[test]
        fn uniform_data_not_minified(
            byte_value in b'a'..=b'z',
            size in 100usize..1000
        ) {
            let classifier = FileClassifier::default();
            let data = vec![byte_value; size];

            // Add newlines to avoid line too long
            let mut content = Vec::new();
            for chunk in data.chunks(80) {
                content.extend_from_slice(chunk);
                content.push(b'\n');
            }

            let decision = classifier.should_parse(&PathBuf::from("test.txt"), &content);
            // Uniform data should parse fine (not minified)
            prop_assert_eq!(decision, ParseDecision::Parse);
        }

        /// Property: Mixed character data may be detected as minified
        #[test]
        fn mixed_data_minification_detection(
            data in prop::collection::vec(any::<u8>(), 100..1000)
        ) {
            let classifier = FileClassifier::default();

            // Only test valid UTF-8 data to avoid binary detection
            if std::str::from_utf8(&data).is_ok() {
                let decision = classifier.should_parse(&PathBuf::from("test.js"), &data);
                // We can't predict the exact decision, but it should be consistent
                let decision2 = classifier.should_parse(&PathBuf::from("test.js"), &data);
                prop_assert_eq!(decision, decision2);
            }
        }

        /// Property: File classifier config fields are respected
        #[test]
        fn config_fields_respected(config in arb_file_classifier_config()) {
            let classifier = FileClassifier {
                skip_vendor: config.skip_vendor,
                max_line_length: config.max_line_length,
                max_file_size: config.max_file_size,
                ..Default::default()
            };

            // Test max file size is respected
            let oversized = vec![b'a'; config.max_file_size + 1];
            let decision = classifier.should_parse(&PathBuf::from("test.txt"), &oversized);
            prop_assert_eq!(decision, ParseDecision::Skip(SkipReason::FileTooLarge));

            // Test vendor skip is respected
            if config.skip_vendor {
                let vendor_path = PathBuf::from("vendor/lib.js");
                let decision = classifier.should_parse(&vendor_path, b"content");
                prop_assert_eq!(decision, ParseDecision::Skip(SkipReason::VendorDirectory));
            }
        }

        /// Property: Minified file signatures are detected
        #[test]
        fn minified_signatures_detected(
            signature in prop::sample::select(vec![
                "/*! jQuery",
                "/*! * Bootstrap",
                "!function(e,t){",
                "/*! For license information",
                "/** @license React",
            ]),
            suffix in "[a-zA-Z0-9 ]{0,100}"
        ) {
            let classifier = FileClassifier::default();

            let mut content = String::from(signature);
            content.push_str(&suffix);

            let decision = classifier.should_parse(&PathBuf::from("lib.js"), content.as_bytes());
            prop_assert_eq!(decision, ParseDecision::Skip(SkipReason::MinifiedContent));
        }
    }

    #[test]
    fn test_basic_classifier_invariants() {
        let classifier = FileClassifier::default();

        // Test default values
        assert_eq!(classifier.max_line_length, 10_000); // DEFAULT_MAX_LINE_LENGTH value
        assert_eq!(classifier.max_file_size, DEFAULT_MAX_FILE_SIZE);
        assert!(classifier.skip_vendor);

        // Test empty file
        assert_eq!(
            classifier.should_parse(&PathBuf::from("test.rs"), b""),
            ParseDecision::Skip(SkipReason::EmptyFile)
        );

        // Test normal file
        let normal_content = b"fn main() {\n    println!(\"Hello, world!\");\n}";
        assert_eq!(
            classifier.should_parse(&PathBuf::from("main.rs"), normal_content),
            ParseDecision::Parse
        );
    }

    #[test]
    fn test_vendor_detection_patterns() {
        let classifier = FileClassifier::default();

        let vendor_paths = vec![
            "vendor/jquery.js",
            "node_modules/react/index.js",
            "third_party/lib.rs",
            "external/dependency.py",
            ".yarn/cache/package.js",
            "bower_components/angular.js",
            "lib.min.js",
            "app.bundle.js",
        ];

        for path in vendor_paths {
            let decision = classifier.should_parse(&PathBuf::from(path), b"content");
            assert!(
                matches!(decision, ParseDecision::Skip(SkipReason::VendorDirectory)),
                "Failed to detect vendor path: {}",
                path
            );
        }
    }

    #[test]
    fn test_build_artifact_detection() {
        let classifier = FileClassifier::default();

        let build_paths = vec![
            "target/debug/deps/lib.rlib",
            "target/release/myapp",
            "build/output.js",
            "dist/bundle.js",
            "__pycache__/module.pyc",
            "venv/lib/python3.9/site-packages/pkg.py",
            ".tox/py39/lib/python3.9/site-packages/test.py",
            "cmake-build-debug/CMakeFiles/app.dir/main.cpp.o",
            ".gradle/caches/modules-2/files-2.1/lib.jar",
        ];

        for path in build_paths {
            let decision = classifier.should_parse(&PathBuf::from(path), b"content");
            assert!(
                matches!(decision, ParseDecision::Skip(SkipReason::BuildArtifact)),
                "Failed to detect build artifact: {}",
                path
            );
        }
    }

    #[test]
    fn test_minification_detection_edge_cases() {
        let classifier = FileClassifier::default();

        // Test that minified signatures are detected
        let minified_signatures: Vec<&[u8]> = vec![
            b"/*! jQuery",
            b"/*! * Bootstrap",
            b"!function(e,t){",
            b"/*! For license information",
            b"/** @license React",
        ];

        for sig in minified_signatures {
            let decision = classifier.should_parse(&PathBuf::from("lib.js"), sig);
            assert_eq!(
                decision,
                ParseDecision::Skip(SkipReason::MinifiedContent),
                "Failed to detect minified signature"
            );
        }

        // Test high-entropy content detection
        let high_entropy = b"a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6";
        let decision = classifier.should_parse(&PathBuf::from("data.js"), high_entropy);
        // Should be detected as minified due to lack of newlines
        assert_eq!(decision, ParseDecision::Skip(SkipReason::MinifiedContent));
    }
}