pmat 2.93.1

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
//! Comprehensive CLI Functional Test Harness
//!
//! CRITICAL: This test harness verifies EVERY command and option works.
//! Without this, the entire project is dead in the water.
//!
//! Principles:
//! - Functional programming: pure functions, immutable data
//! - Exhaustive testing: every command, every option combination
//! - Real execution: actually run the binary, don't mock
//! - Output validation: verify output is sensible, not just "no error"

use std::collections::HashMap;
use std::path::PathBuf;
use std::process::Command;
use tempfile::TempDir;

/// Test result for a single command execution
#[derive(Debug, Clone)]
struct CommandResult {
    command: String,
    args: Vec<String>,
    success: bool,
    stdout: String,
    stderr: String,
    exit_code: i32,
    validation_errors: Vec<String>,
}

/// Validation function type
type Validator = fn(&str) -> Result<(), String>;

/// Test specification for a command
#[derive(Clone)]
struct CommandSpec {
    command: String,
    subcommand: Option<String>,
    args: Vec<String>,
    options: HashMap<String, String>,
    should_succeed: bool,
    output_validators: Vec<Validator>,
}

/// The main test harness
struct CliTestHarness {
    binary_path: PathBuf,
    test_dir: TempDir,
    results: Vec<CommandResult>,
}

impl CliTestHarness {
    fn new() -> Self {
        let binary_path = PathBuf::from(env!("CARGO_BIN_EXE_pmat"));
        let test_dir = TempDir::new().expect("Failed to create temp dir");

        Self {
            binary_path,
            test_dir,
            results: Vec::new(),
        }
    }

    /// Execute a command and validate output
    fn execute_command(&mut self, spec: CommandSpec) -> CommandResult {
        let mut cmd = Command::new(&self.binary_path);

        // Add command and subcommand
        cmd.arg(&spec.command);
        if let Some(subcmd) = &spec.subcommand {
            cmd.arg(subcmd);
        }

        // Add args
        for arg in &spec.args {
            cmd.arg(arg);
        }

        // Add options
        for (key, value) in &spec.options {
            cmd.arg(format!("--{}", key));
            if !value.is_empty() {
                cmd.arg(value);
            }
        }

        // Execute with timeout to prevent hanging
        let cmd_child = cmd.spawn().expect("Failed to spawn command");
        let output = match cmd_child.wait_with_output() {
            Ok(output) => output,
            Err(e) => panic!("Command failed to complete: {}", e),
        };

        let stdout = String::from_utf8_lossy(&output.stdout).to_string();
        let stderr = String::from_utf8_lossy(&output.stderr).to_string();
        let exit_code = output.status.code().unwrap_or(-1);

        // Validate output
        let mut validation_errors = Vec::new();

        if spec.should_succeed && !output.status.success() {
            validation_errors.push(format!(
                "Command should succeed but failed with exit code {}",
                exit_code
            ));
        }

        // Run custom validators
        for validator in &spec.output_validators {
            if let Err(e) = validator(&stdout) {
                validation_errors.push(e);
            }
        }

        let result = CommandResult {
            command: format!(
                "{} {}",
                spec.command,
                spec.subcommand.as_ref().unwrap_or(&String::new())
            ),
            args: spec.args.clone(),
            success: output.status.success(),
            stdout: stdout.clone(),
            stderr: stderr.clone(),
            exit_code,
            validation_errors: validation_errors.clone(),
        };

        self.results.push(result.clone());
        result
    }

    /// Generate test report
    fn generate_report(&self) -> String {
        let total = self.results.len();
        let passed = self
            .results
            .iter()
            .filter(|r| r.validation_errors.is_empty())
            .count();
        let failed = total - passed;

        let mut report = "# CLI Functional Test Report\n\n".to_string();
        report.push_str(&format!("Total Commands Tested: {}\n", total));
        report.push_str(&format!("✅ Passed: {}\n", passed));
        report.push_str(&format!("❌ Failed: {}\n\n", failed));

        if failed > 0 {
            report.push_str("## Failed Commands\n\n");
            for result in &self.results {
                if !result.validation_errors.is_empty() {
                    report.push_str(&format!("### Command: `{}`\n", result.command));
                    report.push_str(&format!("Args: {:?}\n", result.args));
                    report.push_str(&format!("Exit Code: {}\n", result.exit_code));
                    report.push_str("Errors:\n");
                    for error in &result.validation_errors {
                        report.push_str(&format!("- {}\n", error));
                    }
                    if !result.stderr.is_empty() {
                        report.push_str(&format!("Stderr:\n```\n{}\n```\n", result.stderr));
                    }
                    report.push('\n');
                }
            }
        }

        report.push_str("## Working Commands\n\n");
        for result in &self.results {
            if result.validation_errors.is_empty() {
                report.push_str(&format!("✅ `pmat {}`", result.command));
                if !result.args.is_empty() {
                    report.push_str(&format!(" {}", result.args.join(" ")));
                }
                report.push('\n');
            }
        }

        report
    }
}

// Validator functions
fn validate_help_has_usage(output: &str) -> Result<(), String> {
    if output.contains("Usage:") {
        Ok(())
    } else {
        Err("Help output missing 'Usage:' section".to_string())
    }
}

fn validate_help_has_commands(output: &str) -> Result<(), String> {
    if output.contains("Commands:") {
        Ok(())
    } else {
        Err("Help output missing 'Commands:' section".to_string())
    }
}

fn validate_has_version(output: &str) -> Result<(), String> {
    if output.contains("pmat") {
        Ok(())
    } else {
        Err("Version output doesn't contain 'pmat'".to_string())
    }
}

fn validate_complexity_output(output: &str) -> Result<(), String> {
    if output.contains("Complexity Analysis") || output.contains("Files analyzed") {
        Ok(())
    } else {
        Err("Complexity output missing expected sections".to_string())
    }
}

fn validate_satd_output(output: &str) -> Result<(), String> {
    if output.contains("SATD Analysis") || output.contains("Files analyzed") {
        Ok(())
    } else {
        Err("SATD output missing expected sections".to_string())
    }
}

fn validate_dead_code_output(output: &str) -> Result<(), String> {
    if output.contains("Dead Code Analysis") || output.contains("Files analyzed") {
        Ok(())
    } else {
        Err("Dead code output missing expected sections".to_string())
    }
}

fn validate_quality_gate_output(output: &str) -> Result<(), String> {
    if output.contains("Quality Gate") || output.contains("Checking") {
        Ok(())
    } else {
        Err("Quality gate output missing expected sections".to_string())
    }
}

/// Create all command specifications to test
fn generate_command_specs() -> Vec<CommandSpec> {
    let mut specs = Vec::new();

    // Help commands - these MUST work
    specs.push(CommandSpec {
        command: "--help".to_string(),
        subcommand: None,
        args: vec![],
        options: HashMap::new(),
        should_succeed: true,
        output_validators: vec![validate_help_has_usage, validate_help_has_commands],
    });

    // Version command
    specs.push(CommandSpec {
        command: "--version".to_string(),
        subcommand: None,
        args: vec![],
        options: HashMap::new(),
        should_succeed: true,
        output_validators: vec![validate_has_version],
    });

    // Analyze complexity - various forms
    specs.push(CommandSpec {
        command: "analyze".to_string(),
        subcommand: Some("complexity".to_string()),
        args: vec![],
        options: HashMap::new(),
        should_succeed: true,
        output_validators: vec![validate_complexity_output],
    });

    // Analyze complexity with file
    specs.push(CommandSpec {
        command: "analyze".to_string(),
        subcommand: Some("complexity".to_string()),
        args: vec![],
        options: {
            let mut opts = HashMap::new();
            opts.insert("file".to_string(), "src/lib.rs".to_string());
            opts
        },
        should_succeed: true,
        output_validators: vec![],
    });

    // Analyze SATD
    specs.push(CommandSpec {
        command: "analyze".to_string(),
        subcommand: Some("satd".to_string()),
        args: vec![],
        options: HashMap::new(),
        should_succeed: true,
        output_validators: vec![validate_satd_output],
    });

    // Analyze dead-code - FIXED: Should no longer hang
    specs.push(CommandSpec {
        command: "analyze".to_string(),
        subcommand: Some("dead-code".to_string()),
        args: vec![],
        options: {
            let mut opts = HashMap::new();
            opts.insert("path".to_string(), ".".to_string());
            opts
        },
        should_succeed: true,
        output_validators: vec![validate_dead_code_output],
    });

    // Quality gate
    specs.push(CommandSpec {
        command: "quality-gate".to_string(),
        subcommand: None,
        args: vec![],
        options: HashMap::new(),
        should_succeed: true, // Should succeed even if quality fails
        output_validators: vec![validate_quality_gate_output],
    });

    // Demo command
    specs.push(CommandSpec {
        command: "demo".to_string(),
        subcommand: None,
        args: vec!["--help".to_string()],
        options: HashMap::new(),
        should_succeed: true,
        output_validators: vec![],
    });

    // Agent command
    specs.push(CommandSpec {
        command: "agent".to_string(),
        subcommand: None,
        args: vec!["--help".to_string()],
        options: HashMap::new(),
        should_succeed: true,
        output_validators: vec![],
    });

    // TDG analysis
    specs.push(CommandSpec {
        command: "analyze".to_string(),
        subcommand: Some("tdg".to_string()),
        args: vec![],
        options: HashMap::new(),
        should_succeed: true,
        output_validators: vec![],
    });

    // Context generation
    specs.push(CommandSpec {
        command: "context".to_string(),
        subcommand: None,
        args: vec![],
        options: HashMap::new(),
        should_succeed: true,
        output_validators: vec![],
    });

    // Refactor commands
    specs.push(CommandSpec {
        command: "refactor".to_string(),
        subcommand: None,
        args: vec!["--help".to_string()],
        options: HashMap::new(),
        should_succeed: true,
        output_validators: vec![],
    });

    specs
}

#[test]
fn test_all_cli_commands_work() {
    let mut harness = CliTestHarness::new();
    let specs = generate_command_specs();

    println!("Testing {} command variations...", specs.len());

    let mut failed = 0;
    for spec in specs {
        let result = harness.execute_command(spec.clone());
        if !result.validation_errors.is_empty() {
            failed += 1;
            eprintln!(
                "❌ Failed: {} {}",
                spec.command,
                spec.subcommand.unwrap_or_default()
            );
            for error in &result.validation_errors {
                eprintln!("   {}", error);
            }
        } else {
            println!(
                "✅ Passed: {} {}",
                spec.command,
                spec.subcommand.unwrap_or_default()
            );
        }
    }

    // Generate report
    let report = harness.generate_report();
    std::fs::write("cli_test_report.md", &report).expect("Failed to write report");

    if failed > 0 {
        panic!(
            "{} commands failed! See cli_test_report.md for details",
            failed
        );
    }
}

#[test]
fn test_help_is_actually_helpful() {
    let output = Command::new(env!("CARGO_BIN_EXE_pmat"))
        .arg("--help")
        .output()
        .expect("Failed to run help");

    let help_text = String::from_utf8_lossy(&output.stdout);

    // Help should show actual examples
    assert!(help_text.contains("Usage:"), "Help should show usage");
    assert!(help_text.contains("Commands:"), "Help should list commands");

    // Help should be organized
    assert!(help_text.contains("analyze"), "Should show analyze command");
    assert!(
        help_text.contains("quality-gate"),
        "Should show quality-gate command"
    );
}

#[test]
fn test_analyze_complexity_actually_finds_files() {
    let output = Command::new(env!("CARGO_BIN_EXE_pmat"))
        .arg("analyze")
        .arg("complexity")
        .arg("--project-path")
        .arg(".")
        .output()
        .expect("Failed to run complexity analysis");

    let stdout = String::from_utf8_lossy(&output.stdout);

    // Should actually find some files
    assert!(
        !stdout.contains("Files analyzed: 0"),
        "Complexity analysis should find files, but got:\n{}",
        stdout
    );
}

#[test]
fn test_error_messages_are_helpful() {
    // Test invalid command
    let output = Command::new(env!("CARGO_BIN_EXE_pmat"))
        .arg("agent")
        .arg("analyze") // This is wrong - should be separate commands
        .output()
        .expect("Failed to run command");

    let stderr = String::from_utf8_lossy(&output.stderr);

    // Error should suggest correct usage
    assert!(
        stderr.contains("unrecognized") || stderr.contains("error"),
        "Should show clear error message"
    );

    // Ideally should suggest: "Did you mean 'pmat analyze'?"
    // This is what we need to add!
}