pmat 3.18.2

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
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
//! RED Phase Tests for DocumentationScorer (Sprint 2)
//!
//! Documentation Category: 15 points total
//! - Rustdoc Coverage (7pts): Public API documentation with examples
//! - README Quality (5pts): Comprehensive project README
//! - Changelog Presence (3pts): CHANGELOG.md with version history
//!
//! Evidence-based design: Well-documented projects have 30-40% fewer
//! support issues and faster onboarding (GitHub State of the Octoverse 2024).

use pmat::services::rust_project_score::{DocumentationScorer, Scorer};

// Test fixture: Create temporary test project
fn create_test_project() -> tempfile::TempDir {
    let temp = tempfile::tempdir().unwrap();
    let cargo_toml = temp.path().join("Cargo.toml");
    std::fs::write(
        &cargo_toml,
        r#"
[package]
name = "test-project"
version = "0.1.0"
edition = "2021"

[dependencies]
"#,
    )
    .unwrap();

    // Create src directory with main.rs
    let src_dir = temp.path().join("src");
    std::fs::create_dir(&src_dir).unwrap();
    std::fs::write(
        src_dir.join("main.rs"),
        r#"
fn main() {
    println!("Hello, world!");
}
"#,
    )
    .unwrap();

    temp
}

// ============================================================================
// Test 1: DocumentationScorer Creation
// ============================================================================

#[test]
fn test_documentation_scorer_creation() {
    let scorer = DocumentationScorer::new();
    assert_eq!(scorer.name(), "Documentation");
    assert_eq!(scorer.max_points(), 15.0);
}

// ============================================================================
// Test 2: Perfect Score (All Documentation Checks Pass)
// ============================================================================

#[test]
fn test_perfect_score_all_checks_pass() {
    let temp = create_test_project();
    let scorer = DocumentationScorer::new();

    let result = scorer.score(temp.path());
    assert!(result.is_ok());

    let score = result.unwrap();
    assert_eq!(score.max, 15.0);
    assert!(score.earned >= 0.0 && score.earned <= 15.0);
}

// ============================================================================
// Test 3: Rustdoc Coverage Scoring (7pts)
// ============================================================================

#[test]
fn test_rustdoc_coverage_well_documented() {
    let temp = create_test_project();

    // Create lib.rs with comprehensive documentation
    let src_lib = temp.path().join("src").join("lib.rs");
    std::fs::write(
        &src_lib,
        r#"
//! Test library with documentation
//!
//! This library demonstrates well-documented code.

/// Adds two numbers together.
///
/// # Examples
///
/// ```
/// let result = test_project::add(2, 3);
/// assert_eq!(result, 5);
/// ```
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

/// Subtracts two numbers.
///
/// # Examples
///
/// ```
/// let result = test_project::subtract(5, 3);
/// assert_eq!(result, 2);
/// ```
pub fn subtract(a: i32, b: i32) -> i32 {
    a - b
}
"#,
    )
    .unwrap();

    let scorer = DocumentationScorer::new();
    let result = scorer.score(temp.path());

    assert!(result.is_ok());
    let score = result.unwrap();

    // Should get high rustdoc coverage points
    assert!(score.earned >= 7.0 || score.earned == 0.0);
}

#[test]
fn test_rustdoc_coverage_poorly_documented() {
    let temp = create_test_project();

    // Create lib.rs WITHOUT documentation
    let src_lib = temp.path().join("src").join("lib.rs");
    std::fs::write(
        &src_lib,
        r#"
pub fn add(a: i32, b: i32) -> i32 {
    a + b
}

pub fn subtract(a: i32, b: i32) -> i32 {
    a - b
}
"#,
    )
    .unwrap();

    let scorer = DocumentationScorer::new();
    let result = scorer.score(temp.path());

    assert!(result.is_ok());
    let score = result.unwrap();

    // Should lose rustdoc coverage points
    assert!(score.earned < 15.0);
}

// ============================================================================
// Test 4: README Quality Scoring (5pts)
// ============================================================================

#[test]
fn test_readme_quality_comprehensive() {
    let temp = create_test_project();

    // Create comprehensive README
    std::fs::write(
        temp.path().join("README.md"),
        r#"
# Test Project

A comprehensive test project demonstrating README quality.

## Installation

```bash
cargo add test-project
```

## Usage

```rust
use test_project::add;

let result = add(2, 3);
```

## Features

- Feature 1
- Feature 2
- Feature 3

## License

MIT License
"#,
    )
    .unwrap();

    let scorer = DocumentationScorer::new();
    let result = scorer.score(temp.path());

    assert!(result.is_ok());
    let score = result.unwrap();

    // Should get high README quality points
    assert!(score.earned >= 5.0 || score.earned == 0.0);
}

#[test]
fn test_readme_quality_minimal() {
    let temp = create_test_project();

    // Create minimal README
    std::fs::write(
        temp.path().join("README.md"),
        r#"
# Test Project

A test project.
"#,
    )
    .unwrap();

    let scorer = DocumentationScorer::new();
    let result = scorer.score(temp.path());

    assert!(result.is_ok());
    let score = result.unwrap();

    // Should get partial README points
    assert!(score.earned < 15.0);
}

#[test]
fn test_readme_absent() {
    let temp = create_test_project();

    // No README created

    let scorer = DocumentationScorer::new();
    let result = scorer.score(temp.path());

    assert!(result.is_ok());
    let score = result.unwrap();

    // Should lose README points
    assert!(score.earned <= 10.0);
}

// ============================================================================
// Test 5: Changelog Presence Scoring (3pts)
// ============================================================================

#[test]
fn test_changelog_present() {
    let temp = create_test_project();

    // Create CHANGELOG.md
    std::fs::write(
        temp.path().join("CHANGELOG.md"),
        r#"
# Changelog

## [0.1.0] - 2024-01-01

### Added
- Initial release
- Feature 1
- Feature 2
"#,
    )
    .unwrap();

    let scorer = DocumentationScorer::new();
    let result = scorer.score(temp.path());

    assert!(result.is_ok());
    let score = result.unwrap();

    // Should get changelog points
    assert!(score.earned >= 3.0 || score.earned == 0.0);
}

#[test]
fn test_changelog_absent() {
    let temp = create_test_project();

    // No CHANGELOG created

    let scorer = DocumentationScorer::new();
    let result = scorer.score(temp.path());

    assert!(result.is_ok());
    let score = result.unwrap();

    // Should lose changelog points
    assert!(score.earned <= 12.0);
}

// ============================================================================
// Test 6: Rustdoc Missing Items
// ============================================================================

#[test]
fn test_rustdoc_missing_items() {
    let temp = create_test_project();

    // Create lib.rs with some documented and some undocumented items
    let src_lib = temp.path().join("src").join("lib.rs");
    std::fs::write(
        &src_lib,
        r#"
/// Documented function
pub fn documented() -> i32 {
    42
}

pub fn undocumented() -> i32 {
    99
}
"#,
    )
    .unwrap();

    let scorer = DocumentationScorer::new();
    let result = scorer.score(temp.path());

    assert!(result.is_ok());
    let score = result.unwrap();

    // Should get partial rustdoc points
    assert!(score.earned < 15.0);
}

// ============================================================================
// Test 7: README Section Detection
// ============================================================================

#[test]
fn test_readme_section_detection() {
    let temp = create_test_project();

    // Create README with multiple sections
    std::fs::write(
        temp.path().join("README.md"),
        r#"
# Test Project

## Installation
Install instructions here.

## Usage
Usage examples here.

## API Documentation
API docs here.

## Contributing
Contribution guidelines here.

## License
License information here.
"#,
    )
    .unwrap();

    let scorer = DocumentationScorer::new();
    let result = scorer.score(temp.path());

    assert!(result.is_ok());
    let score = result.unwrap();

    // Should get full README points for comprehensive sections
    assert!(score.earned >= 5.0 || score.earned == 0.0);
}

// ============================================================================
// Test 8: Changelog Version Detection
// ============================================================================

#[test]
fn test_changelog_version_detection() {
    let temp = create_test_project();

    // Create CHANGELOG with multiple versions
    std::fs::write(
        temp.path().join("CHANGELOG.md"),
        r#"
# Changelog

## [0.2.0] - 2024-02-01
### Added
- New feature

## [0.1.0] - 2024-01-01
### Added
- Initial release
"#,
    )
    .unwrap();

    let scorer = DocumentationScorer::new();
    let result = scorer.score(temp.path());

    assert!(result.is_ok());
    let score = result.unwrap();

    // Should get full changelog points for version history
    assert!(score.earned >= 3.0 || score.earned == 0.0);
}

// ============================================================================
// Test 9: Recommendations Generation
// ============================================================================

#[test]
fn test_recommendations_for_documentation_issues() {
    let temp = create_test_project();

    // Create project with NO documentation
    let scorer = DocumentationScorer::new();
    let recommendations = scorer.recommendations(temp.path());

    // Should provide specific recommendations
    assert!(!recommendations.is_empty());

    let rec_text = recommendations.join(" ");
    assert!(
        rec_text.contains("documentation")
            || rec_text.contains("README")
            || rec_text.contains("CHANGELOG")
            || rec_text.contains("rustdoc")
    );
}

// ============================================================================
// Test 10: Scorer Implements Send + Sync
// ============================================================================

#[test]
fn test_scorer_is_send_sync() {
    fn assert_send<T: Send>() {}
    fn assert_sync<T: Sync>() {}

    assert_send::<DocumentationScorer>();
    assert_sync::<DocumentationScorer>();
}

// ============================================================================
// Test 11: Scoring is Deterministic
// ============================================================================

#[test]
fn test_scoring_is_deterministic() {
    let temp = create_test_project();
    let scorer = DocumentationScorer::new();

    // Score same project twice
    let result1 = scorer.score(temp.path()).unwrap();
    let result2 = scorer.score(temp.path()).unwrap();

    // Should get identical scores
    assert_eq!(result1.earned, result2.earned);
    assert_eq!(result1.max, result2.max);
}

// ============================================================================
// Test 12: Invalid Project Error
// ============================================================================

#[test]
fn test_invalid_project_no_cargo_toml() {
    let temp = tempfile::tempdir().unwrap();
    let scorer = DocumentationScorer::new();

    let result = scorer.score(temp.path());

    // Should return error for invalid project
    assert!(result.is_err());
}

// ============================================================================
// Test 13: Performance (<5 seconds)
// ============================================================================

#[test]
fn test_scoring_performance() {
    use std::time::Instant;

    let temp = create_test_project();
    let scorer = DocumentationScorer::new();

    let start = Instant::now();
    let result = scorer.score(temp.path());
    let duration = start.elapsed();

    assert!(result.is_ok());

    // Should complete in <5 seconds per specification
    assert!(
        duration.as_secs() < 5,
        "Scoring took {:?}, expected <5s",
        duration
    );
}

// ============================================================================
// Test 14: CategoryScore Structure
// ============================================================================

#[test]
fn test_category_score_structure() {
    let temp = create_test_project();
    let scorer = DocumentationScorer::new();

    let result = scorer.score(temp.path()).unwrap();

    // Verify CategoryScore has correct structure
    assert_eq!(result.max, 15.0);
    assert!(result.earned >= 0.0);
    assert!(result.earned <= result.max);

    // Test percentage calculation
    let percentage = result.percentage();
    assert!((0.0..=100.0).contains(&percentage));
}

// ============================================================================
// Property-Based Test 15: Score Bounds
// ============================================================================

#[test]
fn test_score_bounds_property() {
    let temp = create_test_project();
    let scorer = DocumentationScorer::new();

    let result = scorer.score(temp.path()).unwrap();

    // Property: Score must be in [0, max]
    assert!(result.earned >= 0.0);
    assert!(result.earned <= result.max);
    assert_eq!(result.max, 15.0);
}

// ============================================================================
// Property-Based Test 16: Score Monotonicity
// ============================================================================

#[test]
fn test_score_monotonicity_property() {
    // Property: Adding documentation should never decrease score

    let temp1 = create_test_project();

    let temp2 = create_test_project();
    std::fs::write(
        temp2.path().join("README.md"),
        "# Test Project\n\nA documented project.",
    )
    .unwrap();

    let scorer = DocumentationScorer::new();

    let no_docs_score = scorer.score(temp1.path()).unwrap();
    let with_docs_score = scorer.score(temp2.path()).unwrap();

    // Code with documentation should score >= code without
    assert!(with_docs_score.earned >= no_docs_score.earned);
}

// ============================================================================
// Test 17: Rustdoc Coverage Calculation
// ============================================================================

#[test]
fn test_rustdoc_coverage_calculation() {
    let temp = create_test_project();

    // Create lib.rs with 50% documentation coverage
    let src_lib = temp.path().join("src").join("lib.rs");
    std::fs::write(
        &src_lib,
        r#"
/// Documented function 1
pub fn fn1() -> i32 { 1 }

pub fn fn2() -> i32 { 2 }

/// Documented function 3
pub fn fn3() -> i32 { 3 }

pub fn fn4() -> i32 { 4 }
"#,
    )
    .unwrap();

    let scorer = DocumentationScorer::new();
    let result = scorer.score(temp.path()).unwrap();

    // Should get partial rustdoc coverage points
    assert!(result.earned > 0.0 && result.earned < 15.0);
}

// ============================================================================
// Test 18: README Word Count
// ============================================================================

#[test]
fn test_readme_word_count() {
    let temp = create_test_project();

    // Create README with substantial content (>200 words)
    let readme_content = vec!["word"; 250].join(" ");
    std::fs::write(
        temp.path().join("README.md"),
        format!("# Test Project\n\n{}", readme_content),
    )
    .unwrap();

    let scorer = DocumentationScorer::new();
    let result = scorer.score(temp.path()).unwrap();

    // Should get points for substantial README
    assert!(result.earned >= 0.0);
}

// ============================================================================
// Test 19: Evidence-Based Weight Allocation
// ============================================================================

#[test]
fn test_evidence_based_weights() {
    let scorer = DocumentationScorer::new();

    // Verify evidence-based weight allocation
    assert_eq!(scorer.max_points(), 15.0);

    // Rustdoc (7pts): Highest weight - API documentation critical
    // README (5pts): Project overview and onboarding
    // Changelog (3pts): Version history and upgrade guidance
}

// ============================================================================
// Test 20: Module-Level Documentation
// ============================================================================

#[test]
fn test_module_level_documentation() {
    let temp = create_test_project();

    // Create lib.rs with module-level documentation
    let src_lib = temp.path().join("src").join("lib.rs");
    std::fs::write(
        &src_lib,
        r#"
//! # Test Project
//!
//! This is a comprehensive module-level documentation
//! that describes the entire library.

/// Public function
pub fn example() -> i32 {
    42
}
"#,
    )
    .unwrap();

    let scorer = DocumentationScorer::new();
    let result = scorer.score(temp.path()).unwrap();

    // Should get points for module-level documentation
    assert!(result.earned >= 0.0);
}