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
/// Print validation result as text
fn print_validation_text(result: &QaValidationResult) {
    use crate::cli::colors as c;
    println!("Validating {}...\n", c::label(&result.task_id));

    for category in result.categories.values() {
        let status = if category.passed == category.total {
            c::pass("")
        } else if category.passed > 0 {
            c::warn("")
        } else {
            c::fail("")
        };

        println!(
            "{} {} ({}/{})",
            status, c::subheader(&category.name), category.passed, category.total
        );

        for item in &category.items {
            let item_status = match item.status {
                ValidationStatus::Passed => format!("  {}{}", c::GREEN, c::RESET),
                ValidationStatus::Failed => format!("  {}{}", c::RED, c::RESET),
                ValidationStatus::Warning => format!("  {}{}", c::YELLOW, c::RESET),
                ValidationStatus::Skipped => format!("  {}-{}", c::DIM, c::RESET),
                ValidationStatus::Manual => format!("  {}?{}", c::BLUE, c::RESET),
            };
            println!("{}  {}: {}", item_status, item.id, item.description);
        }
        println!();
    }

    println!("{}: {:.1}%", c::label("Overall Score"), result.overall_score);
    println!();

    if !result.manual_checks_required.is_empty() {
        println!("{}:", c::warn("Manual Checks Required"));
        for check in &result.manual_checks_required {
            println!("  - {}", check);
        }
        println!();
    }

    if result.passed {
        println!("{}", c::pass("QA Validation PASSED"));
    } else {
        println!("{}", c::fail("QA Validation FAILED"));
    }
}

/// Print validation result as markdown
fn print_validation_markdown(result: &QaValidationResult) {
    println!("# QA Validation Report: {}\n", result.task_id);
    println!(
        "**Date**: {}",
        result.timestamp.format("%Y-%m-%d %H:%M:%S UTC")
    );
    println!("**Score**: {:.1}%\n", result.overall_score);

    for category in result.categories.values() {
        println!(
            "## {} ({}/{})\n",
            category.name, category.passed, category.total
        );

        for item in &category.items {
            let checkbox = match item.status {
                ValidationStatus::Passed => "[x]",
                ValidationStatus::Failed => "[ ] **FAILED**",
                ValidationStatus::Warning => "[ ] *warning*",
                ValidationStatus::Skipped => "[ ] *(skipped)*",
                ValidationStatus::Manual => "[ ] *(manual)*",
            };
            println!("- {} {}: {}", checkbox, item.id, item.description);
        }
        println!();
    }

    if !result.manual_checks_required.is_empty() {
        println!("## Manual Checks Required\n");
        for check in &result.manual_checks_required {
            println!("- [ ] {}", check);
        }
    }
}

/// Generate QA report for audit trail
async fn handle_report(
    task_id: &str,
    project_path: &Path,
    with_evidence: bool,
    output: Option<&Path>,
    format: QaOutputFormat,
) -> Result<()> {
    use crate::cli::colors as c;
    println!("{} {}", c::label("Generating QA report for task:"), task_id);

    // First run validation
    let mut result = QaValidationResult {
        task_id: task_id.to_string(),
        timestamp: Utc::now(),
        categories: HashMap::new(),
        overall_score: 0.0,
        passed: true,
        manual_checks_required: vec![],
    };

    result.categories.insert(
        "code_quality".into(),
        run_code_quality_checks(project_path).await,
    );
    result
        .categories
        .insert("testing".into(), run_testing_checks(project_path).await);
    result.categories.insert(
        "documentation".into(),
        run_documentation_checks(project_path, task_id).await,
    );
    result.categories.insert(
        "process".into(),
        run_process_checks(project_path, task_id).await,
    );

    // Calculate score
    let (total_passed, total_items) = result
        .categories
        .values()
        .fold((0, 0), |(p, t), cat| (p + cat.passed, t + cat.total));
    result.overall_score = if total_items > 0 {
        (total_passed as f64 / total_items as f64) * 100.0
    } else {
        0.0
    };

    // Generate report content
    let report = match format {
        QaOutputFormat::Json => serde_json::to_string_pretty(&result)?,
        QaOutputFormat::Yaml => serde_yaml_ng::to_string(&result)?,
        QaOutputFormat::Markdown | QaOutputFormat::Text => {
            let mut md = String::new();
            md.push_str(&format!("# QA Report: {}\n\n", task_id));
            md.push_str("## Summary\n\n");
            md.push_str(&format!("- **Task**: {}\n", task_id));
            md.push_str(&format!(
                "- **Status**: {}\n",
                if result.passed {
                    "PASSED"
                } else {
                    "NEEDS ATTENTION"
                }
            ));
            md.push_str(&format!("- **Score**: {:.1}%\n", result.overall_score));
            md.push_str(&format!(
                "- **Date**: {}\n\n",
                result.timestamp.format("%Y-%m-%d")
            ));

            md.push_str("## Checklist Results\n\n");
            for category in result.categories.values() {
                md.push_str(&format!(
                    "### {} ({}/{})\n\n",
                    category.name, category.passed, category.total
                ));
                for item in &category.items {
                    let status_icon = match item.status {
                        ValidationStatus::Passed => "",
                        ValidationStatus::Failed => "",
                        ValidationStatus::Warning => "⚠️",
                        ValidationStatus::Skipped => "⏭️",
                        ValidationStatus::Manual => "📝",
                    };
                    md.push_str(&format!(
                        "- {} **{}**: {}\n",
                        status_icon, item.id, item.description
                    ));
                }
                md.push('\n');
            }

            if with_evidence {
                md.push_str("## Evidence\n\n");
                md.push_str("- Coverage Report: `target/llvm-cov/html/index.html`\n");
                md.push_str("- Test Results: See CI/CD logs\n");
                md.push_str("- Complexity: Run `pmat analyze complexity`\n");
            }

            md
        }
    };

    // Output
    if let Some(output_path) = output {
        fs::write(output_path, &report)?;
        println!("{} Report saved to: {}", c::pass(""), c::path(&output_path.display().to_string()));
    } else {
        println!("{}", report);
    }

    Ok(())
}

/// Show QA status summary
async fn handle_summary(
    task_id: Option<&str>,
    project_path: &Path,
    epic_id: Option<&str>,
) -> anyhow::Result<()> {
    use crate::cli::colors as c;
    let qa_dir = project_path.join(".pmat-qa");

    if !qa_dir.exists() {
        println!("{}", c::dim("No QA data found. Run 'pmat qa-work generate-checklist <TASK-ID>' first."));
        return Ok(());
    }

    // Handle epic summary
    if let Some(epic) = epic_id {
        return handle_epic_summary(epic, &qa_dir);
    }

    println!("{}\n", c::header("QA Status Summary"));
    println!(
        "{:<15} {:<12} {:<10}",
        c::dim("Task ID"),
        c::dim("Status"),
        c::dim("Score")
    );
    println!("{}", c::separator());

    if let Some(id) = task_id {
        // Show specific task
        let task_dir = qa_dir.join(id);
        if task_dir.exists() {
            print_task_status(id, &task_dir)?;
        } else {
            println!("{} No QA data found for task: {}", c::warn(""), id);
        }
    } else {
        // Show all tasks
        for entry in fs::read_dir(&qa_dir)? {
            let entry = entry?;
            if entry.file_type()?.is_dir() {
                let task_id = entry.file_name().to_string_lossy().to_string();
                print_task_status(&task_id, &entry.path())?;
            }
        }
    }

    Ok(())
}

#[cfg(test)]
mod print_tests {
    //! Covers print_validation_text + print_validation_markdown in
    //! qa_work_handler/impl_print.rs (178 uncov on broad, 0% cov).
    //! Skips async handle_report / handle_summary (require run_*_checks
    //! infrastructure + project fixtures).
    use super::*;

    fn item(id: &str, desc: &str, status: ValidationStatus) -> ValidationItem {
        ValidationItem {
            id: id.to_string(),
            description: desc.to_string(),
            status,
            value: None,
            threshold: None,
            evidence: None,
        }
    }

    fn category(name: &str, items: Vec<ValidationItem>) -> CategoryResult {
        let passed = items
            .iter()
            .filter(|i| i.status == ValidationStatus::Passed)
            .count() as u32;
        let total = items.len() as u32;
        CategoryResult {
            name: name.to_string(),
            passed,
            total,
            items,
        }
    }

    fn make_result(categories: HashMap<String, CategoryResult>) -> QaValidationResult {
        QaValidationResult {
            task_id: "PMAT-100".to_string(),
            timestamp: Utc::now(),
            categories,
            overall_score: 80.0,
            passed: true,
            manual_checks_required: vec![],
        }
    }

    // ── print_validation_text ──

    #[test]
    fn test_print_validation_text_empty_categories() {
        let r = make_result(HashMap::new());
        // No panic on empty.
        print_validation_text(&r);
    }

    #[test]
    fn test_print_validation_text_all_passed_category() {
        let mut cats = HashMap::new();
        cats.insert(
            "tests".to_string(),
            category(
                "Tests",
                vec![
                    item("T1", "passes", ValidationStatus::Passed),
                    item("T2", "passes", ValidationStatus::Passed),
                ],
            ),
        );
        print_validation_text(&make_result(cats));
    }

    #[test]
    fn test_print_validation_text_partial_passed_category() {
        let mut cats = HashMap::new();
        cats.insert(
            "tests".to_string(),
            category(
                "Tests",
                vec![
                    item("T1", "passes", ValidationStatus::Passed),
                    item("T2", "fails", ValidationStatus::Failed),
                ],
            ),
        );
        print_validation_text(&make_result(cats));
    }

    #[test]
    fn test_print_validation_text_all_failed_category() {
        let mut cats = HashMap::new();
        cats.insert(
            "tests".to_string(),
            category(
                "Tests",
                vec![
                    item("T1", "fails", ValidationStatus::Failed),
                    item("T2", "fails", ValidationStatus::Failed),
                ],
            ),
        );
        print_validation_text(&make_result(cats));
    }

    #[test]
    fn test_print_validation_text_all_5_validation_statuses() {
        let mut cats = HashMap::new();
        cats.insert(
            "all".to_string(),
            category(
                "All",
                vec![
                    item("P", "passed", ValidationStatus::Passed),
                    item("F", "failed", ValidationStatus::Failed),
                    item("W", "warning", ValidationStatus::Warning),
                    item("S", "skipped", ValidationStatus::Skipped),
                    item("M", "manual", ValidationStatus::Manual),
                ],
            ),
        );
        print_validation_text(&make_result(cats));
    }

    // ── print_validation_markdown ──

    #[test]
    fn test_print_validation_markdown_empty_no_panic() {
        let r = make_result(HashMap::new());
        print_validation_markdown(&r);
    }

    #[test]
    fn test_print_validation_markdown_with_all_5_status_arms() {
        let mut cats = HashMap::new();
        cats.insert(
            "tests".to_string(),
            category(
                "Tests",
                vec![
                    item("P", "passed", ValidationStatus::Passed),
                    item("F", "failed", ValidationStatus::Failed),
                    item("W", "warning", ValidationStatus::Warning),
                    item("S", "skipped", ValidationStatus::Skipped),
                    item("M", "manual", ValidationStatus::Manual),
                ],
            ),
        );
        print_validation_markdown(&make_result(cats));
    }

    #[test]
    fn test_print_validation_markdown_with_manual_checks() {
        let r = QaValidationResult {
            task_id: "X".to_string(),
            timestamp: Utc::now(),
            categories: HashMap::new(),
            overall_score: 0.0,
            passed: false,
            manual_checks_required: vec![
                "Review feature flag rollout".to_string(),
                "Verify production logs".to_string(),
            ],
        };
        print_validation_markdown(&r);
    }

    #[test]
    fn test_print_validation_markdown_no_manual_checks_section_when_empty() {
        let r = QaValidationResult {
            task_id: "X".to_string(),
            timestamp: Utc::now(),
            categories: HashMap::new(),
            overall_score: 100.0,
            passed: true,
            manual_checks_required: vec![],
        };
        // Empty manual_checks_required → "Manual Checks Required" section skipped.
        // Just verify no panic.
        print_validation_markdown(&r);
    }
}