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
#![allow(deprecated)]
//! CLI Documentation Enforcement Tests (EXTREME TDD - RED Phase)
//!
//! TICKET: PMAT-7001
//! Phase: RED (All tests should FAIL)
//! Status: 🔴 Tests written but not implemented
//!
//! This test suite verifies that all CLI commands have complete, accurate,
//! and non-generic documentation. Tests use assert_cmd to validate actual
//! CLI behavior.
//!
//! ## Test Categories:
//! 1. Help text existence
//! 2. Flag documentation completeness
//! 3. Description quality (non-generic)
//! 4. Examples presence
//! 5. Documentation drift detection

use assert_cmd::Command;
use predicates::prelude::*;

// ============================================================================
// Category 1: Help Text Existence
// ============================================================================

/// RED: All commands must have --help that returns success
///
/// Verifies that every PMAT command has a working --help flag that
/// displays usage information.
#[test]
#[ignore] // Remove #[ignore] when implementing
fn red_test_all_commands_have_help() {
    let commands = vec![
        // Analyze commands
        "analyze complexity",
        "analyze satd",
        "analyze dead-code",
        "analyze churn",
        "analyze deep-context",
        // Maintain commands
        "maintain health",
        "maintain roadmap",
        // Scaffold commands
        "scaffold agent",
        // Hooks commands
        "hooks install",
        "hooks verify",
        "hooks refresh",
    ];

    for cmd in commands {
        Command::cargo_bin("pmat")
            .unwrap()
            .args(cmd.split_whitespace())
            .arg("--help")
            .assert()
            .success()
            .stdout(predicate::str::contains("Usage:"));
    }
}

/// RED: Help text must include basic structure
#[test]
#[ignore = "requires docs validation setup"]
fn red_test_help_has_basic_structure() {
    let output = Command::cargo_bin("pmat")
        .unwrap()
        .args(["maintain", "roadmap", "--help"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let help = String::from_utf8(output).unwrap();

    assert!(help.contains("Usage:"), "Missing Usage section");
    assert!(
        help.contains("Options:") || help.contains("FLAGS:"),
        "Missing Options/FLAGS section"
    );
}

// ============================================================================
// Category 2: Flag Documentation Completeness
// ============================================================================

/// RED: maintain roadmap must document ALL flags
///
/// From PMAT-6012, maintain roadmap has these flags:
/// - --validate
/// - --health
/// - --fix
/// - --generate-tickets (added in PMAT-6012)
/// - --dry-run
/// - --format
#[test]
#[ignore = "requires docs validation setup"]
fn red_test_maintain_roadmap_flags_complete() {
    let output = Command::cargo_bin("pmat")
        .unwrap()
        .args(["maintain", "roadmap", "--help"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let help = String::from_utf8(output).unwrap();

    // All flags from code must appear in help
    assert!(
        help.contains("--validate"),
        "Missing --validate flag documentation"
    );
    assert!(
        help.contains("--health"),
        "Missing --health flag documentation"
    );
    assert!(help.contains("--fix"), "Missing --fix flag documentation");
    assert!(
        help.contains("--generate-tickets"),
        "Missing --generate-tickets flag documentation (PMAT-6012)"
    );
    assert!(
        help.contains("--dry-run"),
        "Missing --dry-run flag documentation"
    );
    assert!(
        help.contains("--format"),
        "Missing --format flag documentation"
    );
}

/// RED: scaffold agent must document ALL flags
#[test]
#[ignore = "requires docs validation setup"]
fn red_test_scaffold_agent_flags_complete() {
    let output = Command::cargo_bin("pmat")
        .unwrap()
        .args(["scaffold", "agent", "--help"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let help = String::from_utf8(output).unwrap();

    assert!(
        help.contains("--template") || help.contains("-t"),
        "Missing --template flag"
    );
    assert!(
        help.contains("--quality-level") || help.contains("--quality"),
        "Missing --quality-level flag"
    );
    assert!(
        help.contains("--output") || help.contains("-o"),
        "Missing --output flag"
    );
    assert!(
        help.contains("--features") || help.contains("-f"),
        "Missing --features flag"
    );
}

/// RED: maintain health must document ALL flags
///
/// From PMAT-6010, health has parallel check flags
#[test]
#[ignore = "requires docs validation setup"]
fn red_test_maintain_health_flags_complete() {
    let output = Command::cargo_bin("pmat")
        .unwrap()
        .args(["maintain", "health", "--help"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let help = String::from_utf8(output).unwrap();

    assert!(help.contains("--quick"), "Missing --quick flag");
    assert!(help.contains("--all"), "Missing --all flag");
    assert!(help.contains("--check-build"), "Missing --check-build flag");
    assert!(help.contains("--check-tests"), "Missing --check-tests flag");
    assert!(
        help.contains("--check-coverage"),
        "Missing --check-coverage flag"
    );
    assert!(
        help.contains("--check-complexity"),
        "Missing --check-complexity flag"
    );
    assert!(help.contains("--check-satd"), "Missing --check-satd flag");
}

// ============================================================================
// Category 3: Description Quality (Non-Generic)
// ============================================================================

/// RED: Help text must have DESCRIPTIVE text, not just flag names
///
/// Bad:  "--validate    Validate"
/// Good: "--validate    Validate roadmap structure and ticket consistency"
#[test]
#[ignore = "requires docs validation setup"]
fn red_test_help_has_descriptive_text() {
    let output = Command::cargo_bin("pmat")
        .unwrap()
        .args(["maintain", "roadmap", "--help"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let help = String::from_utf8(output).unwrap();

    // Each flag should have description >10 chars
    // (more than just repeating the flag name)

    // Check for meaningful --validate description
    assert!(
        help.contains("Validate roadmap structure")
            || help.contains("Check roadmap consistency")
            || help.contains("Verify roadmap and tickets"),
        "Missing descriptive text for --validate"
    );

    // Check for meaningful --generate-tickets description
    assert!(
        help.contains("missing ticket files")
            || help.contains("Create ticket files from roadmap")
            || help.contains("Auto-generate"),
        "Missing descriptive text for --generate-tickets"
    );
}

/// RED: No generic descriptions allowed
///
/// Forbidden patterns:
/// - "The X parameter"
/// - "Input value"
/// - "Output value"
/// - Just the parameter name repeated
#[test]
#[ignore = "requires docs validation setup"]
fn red_test_no_generic_descriptions_cli() {
    let commands = vec!["scaffold agent", "maintain roadmap", "maintain health"];

    for cmd in commands {
        let output = Command::cargo_bin("pmat")
            .unwrap()
            .args(cmd.split_whitespace())
            .arg("--help")
            .assert()
            .success()
            .get_output()
            .stdout
            .clone();

        let help = String::from_utf8(output).unwrap();

        // Generic patterns that should NOT appear
        let forbidden = vec![
            "The name parameter",
            "The template parameter",
            "The output parameter",
            "Input value",
            "Output value",
        ];

        for pattern in &forbidden {
            assert!(
                !help.contains(pattern),
                "Command '{}' contains forbidden generic pattern: '{}'",
                cmd,
                pattern
            );
        }
    }
}

// ============================================================================
// Category 4: Examples Presence
// ============================================================================

/// RED: Help must include EXAMPLES section
///
/// Users learn best from examples. Every command should show
/// at least one example of common usage.
#[test]
#[ignore = "requires docs validation setup"]
fn red_test_help_includes_examples() {
    let commands = vec![
        "scaffold agent",
        "maintain roadmap",
        "maintain health",
        "analyze complexity",
    ];

    for cmd in commands {
        let output = Command::cargo_bin("pmat")
            .unwrap()
            .args(cmd.split_whitespace())
            .arg("--help")
            .assert()
            .success()
            .get_output()
            .stdout
            .clone();

        let help = String::from_utf8(output).unwrap();

        assert!(
            help.contains("EXAMPLE")
                || help.contains("Example")
                || help.contains("example")
                || help.contains("EXAMPLES")
                || help.contains("Examples"),
            "Command '{}' missing examples section",
            cmd
        );
    }
}

/// RED: Examples should show actual command syntax
#[test]
#[ignore = "requires docs validation setup"]
fn red_test_examples_show_command_syntax() {
    let output = Command::cargo_bin("pmat")
        .unwrap()
        .args(["scaffold", "agent", "--help"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let help = String::from_utf8(output).unwrap();

    // Example should show actual pmat command
    assert!(
        help.contains("pmat scaffold") || help.contains("$ pmat scaffold"),
        "Examples should show actual 'pmat scaffold' commands"
    );
}

// ============================================================================
// Category 5: Documentation Drift Detection
// ============================================================================

/// RED: All flags in code must appear in help
///
/// This test would extract flags from clap definitions and verify
/// they all appear in --help output. This prevents documentation drift
/// where code adds flags but help text isn't updated.
///
/// Note: This test requires helper functions to parse clap definitions
/// from Rust source code using the syn crate. This is deferred to Phase 3
/// (REFACTOR) as it requires complex AST parsing.
///
/// **Status**: Deferred to Phase 3 - requires syn crate integration
#[test]
#[ignore = "requires docs validation setup"]
fn red_test_no_undocumented_flags() {
    // TODO: Phase 3 - Implement automated drift detection
    // Requires:
    // 1. Parse clap definitions from source using syn crate
    // 2. Extract flags from parsed AST
    // 3. Compare with --help output
    //
    // For now, other tests manually verify major commands
    unimplemented!("Automated drift detection deferred to Phase 3 (REFACTOR)");
}

/// RED: Required vs optional should be clear
#[test]
#[ignore = "requires docs validation setup"]
fn red_test_required_vs_optional_clear() {
    let output = Command::cargo_bin("pmat")
        .unwrap()
        .args(["scaffold", "agent", "--help"])
        .assert()
        .success()
        .get_output()
        .stdout
        .clone();

    let help = String::from_utf8(output).unwrap();

    // Should indicate which arguments are required
    // Common patterns: [required], <required>, or prose description
    assert!(
        help.contains("[required]") ||
        help.contains("<") || // angle brackets often mean required
        help.contains("Required:") ||
        help.contains("Arguments:"), // clap typically shows this
        "Help should clearly indicate required vs optional arguments"
    );
}

// ============================================================================
// Category 6: Command-Specific Validations
// ============================================================================

/// RED: hooks commands must be documented
#[test]
#[ignore = "requires docs validation setup"]
fn red_test_hooks_commands_documented() {
    let hook_commands = vec!["hooks install", "hooks verify", "hooks refresh"];

    for cmd in hook_commands {
        Command::cargo_bin("pmat")
            .unwrap()
            .args(cmd.split_whitespace())
            .arg("--help")
            .assert()
            .success()
            .stdout(predicate::str::contains("Usage:"));
    }
}

/// RED: analyze commands must be documented
#[test]
#[ignore = "requires docs validation setup"]
fn red_test_analyze_commands_documented() {
    let analyze_commands = vec![
        "analyze complexity",
        "analyze satd",
        "analyze dead-code",
        "analyze churn",
        "analyze deep-context",
    ];

    for cmd in analyze_commands {
        Command::cargo_bin("pmat")
            .unwrap()
            .args(cmd.split_whitespace())
            .arg("--help")
            .assert()
            .success()
            .stdout(predicate::str::contains("Usage:"));
    }
}

// ============================================================================
// Helper Functions (To be implemented in Phase 2)
// ============================================================================

// These functions will be implemented when we move to GREEN phase

/// Extract all flags from help text
#[allow(dead_code)]
fn extract_flags_from_help(_command: &str) -> Vec<String> {
    // TODO: Parse --help output and extract flag names
    // This will use regex or parsing to find all --flag-name entries
    unimplemented!("Will implement in Phase 2")
}

/// Extract all flags from clap command definitions
#[allow(dead_code)]
fn extract_flags_from_clap_definitions(_command: &str) -> Vec<String> {
    // TODO: Parse clap #[arg(long)] definitions from source
    // This might use syn crate to parse Rust code
    unimplemented!("Will implement in Phase 2")
}

/// Check if description is generic/placeholder
#[allow(dead_code)]
fn is_generic_description(_desc: &str) -> bool {
    // TODO: Implement generic description detection
    // Check for patterns like "The X parameter", length, etc.
    unimplemented!("Will implement in Phase 2")
}

// ============================================================================
// Test Documentation
// ============================================================================

// Expected Failures:
//
// PHASE 1 (RED) - All tests should FAIL because:
// 1. Some commands missing --help text
// 2. Some flags not documented in help
// 3. Some descriptions are generic
// 4. Some commands missing examples
// 5. Documentation drift exists (code vs help mismatch)
//
// PHASE 2 (GREEN) - After implementation:
// 1. All commands will have complete help
// 2. All flags will be documented
// 3. All descriptions will be descriptive
// 4. All commands will have examples
// 5. Automated drift detection will catch mismatches
//
// PHASE 3 (REFACTOR) - After optimization:
// 1. Helper functions will be optimized
// 2. Tests will run faster (<1 second total)
// 3. Integration with quality gates complete