pmat 3.11.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
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
677
678
679
680
681
682
//! TDD tests for `pmat hooks` command implementation
//!
//! Following Toyota Way TDD approach:
//! 1. RED: Write failing tests first
//! 2. GREEN: Implement minimum code to pass
//! 3. REFACTOR: Keep complexity ≤30 cyclomatic, ≤25 cognitive

use anyhow::Result;
use std::fs;
use std::path::PathBuf;
use tempfile::TempDir;

/// Test fixture for hooks command testing
struct HooksTestFixture {
    #[allow(dead_code)]
    temp_dir: TempDir,
    #[allow(dead_code)]
    git_dir: PathBuf,
    hooks_dir: PathBuf,
    config_path: PathBuf,
}

impl HooksTestFixture {
    /// Create test fixture with git repository and pmat.toml
    fn new() -> Result<Self> {
        let temp_dir = tempfile::tempdir()?;
        let git_dir = temp_dir.path().join(".git");
        let hooks_dir = git_dir.join("hooks");
        let config_path = temp_dir.path().join("pmat.toml");

        // Create .git/hooks directory
        fs::create_dir_all(&hooks_dir)?;

        let sample_config = r#"
[hooks]
enabled = true
auto_install = true
backup_existing = true

[hooks.quality_gates]
max_cyclomatic_complexity = 30
max_cognitive_complexity = 25
max_satd_comments = 5
min_test_coverage = 80.0
max_clippy_warnings = 100

[hooks.documentation]
required_files = [
    "docs/execution/roadmap.md",
    "CHANGELOG.md"
]
task_id_pattern = "PMAT-[0-9]{4}"
        "#;

        std::fs::write(&config_path, sample_config)?;

        Ok(Self {
            temp_dir,
            git_dir,
            hooks_dir,
            config_path,
        })
    }

    /// Get path to git hooks directory
    fn hooks_dir(&self) -> &PathBuf {
        &self.hooks_dir
    }

    /// Get path to pre-commit hook
    fn pre_commit_hook(&self) -> PathBuf {
        self.hooks_dir.join("pre-commit")
    }

    /// Get path to config file
    fn config_path(&self) -> &PathBuf {
        &self.config_path
    }

    /// Create existing pre-commit hook for testing
    fn create_existing_hook(&self, content: &str) -> Result<()> {
        let hook_path = self.pre_commit_hook();
        std::fs::write(&hook_path, content)?;

        // Make executable on Unix
        #[cfg(unix)]
        {
            use std::os::unix::fs::PermissionsExt;
            let mut perms = fs::metadata(&hook_path)?.permissions();
            perms.set_mode(0o755);
            fs::set_permissions(&hook_path, perms)?;
        }

        Ok(())
    }
}

/// Hooks command interface (to be implemented)
struct HooksCommand {
    #[allow(dead_code)]
    hooks_dir: PathBuf,
    #[allow(dead_code)]
    config_path: PathBuf,
}

impl HooksCommand {
    /// Create new hooks command with specified directories
    fn new(hooks_dir: PathBuf, config_path: PathBuf) -> Self {
        Self {
            hooks_dir,
            config_path,
        }
    }

    /// Install or update pre-commit hooks
    async fn install(&self, _force: bool, _backup: bool) -> Result<HookInstallResult> {
        // TO BE IMPLEMENTED - this should make test fail (RED phase)
        todo!("Implement hooks install command")
    }

    /// Uninstall PMAT-managed hooks
    async fn uninstall(&self, _restore_backup: bool) -> Result<HookUninstallResult> {
        // TO BE IMPLEMENTED - this should make test fail (RED phase)
        todo!("Implement hooks uninstall command")
    }

    /// Show hook installation status
    async fn status(&self) -> Result<HookStatus> {
        // TO BE IMPLEMENTED - this should make test fail (RED phase)
        todo!("Implement hooks status command")
    }

    /// Verify hooks work with current configuration
    async fn verify(&self, _fix: bool) -> Result<HookVerificationResult> {
        // TO BE IMPLEMENTED - this should make test fail (RED phase)
        todo!("Implement hooks verify command")
    }

    /// Regenerate hooks from current configuration
    async fn refresh(&self) -> Result<HookRefreshResult> {
        // TO BE IMPLEMENTED - this should make test fail (RED phase)
        todo!("Implement hooks refresh command")
    }
}

/// Hook installation result
#[derive(Debug, PartialEq)]
struct HookInstallResult {
    success: bool,
    hook_created: bool,
    backup_created: bool,
    message: String,
}

/// Hook uninstall result
#[derive(Debug, PartialEq)]
struct HookUninstallResult {
    success: bool,
    hook_removed: bool,
    backup_restored: bool,
    message: String,
}

/// Hook status information
#[derive(Debug, PartialEq)]
struct HookStatus {
    installed: bool,
    is_pmat_managed: bool,
    config_up_to_date: bool,
    last_updated: Option<String>,
    hook_content_preview: Option<String>,
}

/// Hook verification result
#[derive(Debug, PartialEq)]
struct HookVerificationResult {
    is_valid: bool,
    issues: Vec<String>,
    fixes_applied: Vec<String>,
}

/// Hook refresh result
#[derive(Debug, PartialEq)]
struct HookRefreshResult {
    success: bool,
    hook_updated: bool,
    config_changes_detected: bool,
    message: String,
}

// =============================================================================
// TDD TESTS (RED PHASE) - These should fail initially
// =============================================================================

#[tokio::test]
#[ignore = "RED phase TDD - hooks command not yet implemented"]
async fn test_hooks_install_basic() -> Result<()> {
    // ARRANGE
    let fixture = HooksTestFixture::new()?;
    let hooks_cmd = HooksCommand::new(fixture.hooks_dir().clone(), fixture.config_path().clone());

    // ACT
    let result = hooks_cmd.install(false, true).await?;

    // ASSERT
    assert!(result.success);
    assert!(result.hook_created);
    assert!(!result.backup_created); // No existing hook to backup

    // Hook file should exist and be executable
    let hook_path = fixture.pre_commit_hook();
    assert!(hook_path.exists());

    // Hook should contain PMAT-generated content
    let hook_content = std::fs::read_to_string(&hook_path)?;
    assert!(hook_content.contains("PMAT"));
    assert!(hook_content.contains("auto-generated"));
    assert!(hook_content.contains("DO NOT EDIT"));

    Ok(())
}

#[tokio::test]
#[ignore = "RED phase TDD - hooks command not yet implemented"]
async fn test_hooks_install_with_existing_hook() -> Result<()> {
    // ARRANGE
    let fixture = HooksTestFixture::new()?;
    fixture.create_existing_hook("#!/bin/bash\necho 'existing hook'")?;

    let hooks_cmd = HooksCommand::new(fixture.hooks_dir().clone(), fixture.config_path().clone());

    // ACT
    let result = hooks_cmd.install(false, true).await?;

    // ASSERT
    assert!(result.success);
    assert!(result.hook_created);
    assert!(result.backup_created);

    // Backup should exist
    let backup_path = fixture.hooks_dir().join("pre-commit.pmat-backup");
    assert!(backup_path.exists());

    let backup_content = std::fs::read_to_string(&backup_path)?;
    assert!(backup_content.contains("existing hook"));

    Ok(())
}

#[tokio::test]
#[ignore = "RED phase TDD - hooks command not yet implemented"]
async fn test_hooks_install_force_overwrite() -> Result<()> {
    // ARRANGE
    let fixture = HooksTestFixture::new()?;
    fixture.create_existing_hook("#!/bin/bash\necho 'existing hook'")?;

    let hooks_cmd = HooksCommand::new(fixture.hooks_dir().clone(), fixture.config_path().clone());

    // ACT
    let result = hooks_cmd.install(true, false).await?;

    // ASSERT
    assert!(result.success);
    assert!(result.hook_created);
    assert!(!result.backup_created); // Force install without backup

    // Hook should be replaced
    let hook_content = std::fs::read_to_string(fixture.pre_commit_hook())?;
    assert!(hook_content.contains("PMAT"));
    assert!(!hook_content.contains("existing hook"));

    Ok(())
}

#[tokio::test]
#[ignore = "RED phase TDD - hooks command not yet implemented"]
async fn test_hooks_uninstall_basic() -> Result<()> {
    // ARRANGE
    let fixture = HooksTestFixture::new()?;
    let hooks_cmd = HooksCommand::new(fixture.hooks_dir().clone(), fixture.config_path().clone());

    // Install first
    let _ = hooks_cmd.install(false, true).await?;

    // ACT
    let result = hooks_cmd.uninstall(false).await?;

    // ASSERT
    assert!(result.success);
    assert!(result.hook_removed);
    assert!(!result.backup_restored);

    // Hook should be removed
    assert!(!fixture.pre_commit_hook().exists());

    Ok(())
}

#[tokio::test]
#[ignore = "RED phase TDD - hooks command not yet implemented"]
async fn test_hooks_uninstall_with_backup_restore() -> Result<()> {
    // ARRANGE
    let fixture = HooksTestFixture::new()?;
    fixture.create_existing_hook("#!/bin/bash\necho 'original hook'")?;

    let hooks_cmd = HooksCommand::new(fixture.hooks_dir().clone(), fixture.config_path().clone());

    // Install (creates backup)
    let _ = hooks_cmd.install(false, true).await?;

    // ACT
    let result = hooks_cmd.uninstall(true).await?;

    // ASSERT
    assert!(result.success);
    assert!(result.hook_removed);
    assert!(result.backup_restored);

    // Original hook should be restored
    let hook_content = std::fs::read_to_string(fixture.pre_commit_hook())?;
    assert!(hook_content.contains("original hook"));
    assert!(!hook_content.contains("PMAT"));

    Ok(())
}

#[tokio::test]
#[ignore = "RED phase TDD - hooks command not yet implemented"]
async fn test_hooks_status_not_installed() -> Result<()> {
    // ARRANGE
    let fixture = HooksTestFixture::new()?;
    let hooks_cmd = HooksCommand::new(fixture.hooks_dir().clone(), fixture.config_path().clone());

    // ACT
    let result = hooks_cmd.status().await?;

    // ASSERT
    assert!(!result.installed);
    assert!(!result.is_pmat_managed);
    assert!(!result.config_up_to_date);
    assert_eq!(result.last_updated, None);
    assert_eq!(result.hook_content_preview, None);

    Ok(())
}

#[tokio::test]
#[ignore = "RED phase TDD - hooks command not yet implemented"]
async fn test_hooks_status_installed() -> Result<()> {
    // ARRANGE
    let fixture = HooksTestFixture::new()?;
    let hooks_cmd = HooksCommand::new(fixture.hooks_dir().clone(), fixture.config_path().clone());

    // Install first
    let _ = hooks_cmd.install(false, true).await?;

    // ACT
    let result = hooks_cmd.status().await?;

    // ASSERT
    assert!(result.installed);
    assert!(result.is_pmat_managed);
    assert!(result.config_up_to_date);
    assert!(result.last_updated.is_some());
    assert!(result.hook_content_preview.is_some());

    let preview = result.hook_content_preview.unwrap();
    assert!(preview.contains("PMAT"));

    Ok(())
}

#[tokio::test]
#[ignore = "RED phase TDD - hooks command not yet implemented"]
async fn test_hooks_verify_valid() -> Result<()> {
    // ARRANGE
    let fixture = HooksTestFixture::new()?;
    let hooks_cmd = HooksCommand::new(fixture.hooks_dir().clone(), fixture.config_path().clone());

    // Install first
    let _ = hooks_cmd.install(false, true).await?;

    // ACT
    let result = hooks_cmd.verify(false).await?;

    // ASSERT
    assert!(result.is_valid);
    assert!(result.issues.is_empty());
    assert!(result.fixes_applied.is_empty());

    Ok(())
}

#[tokio::test]
#[ignore = "RED phase TDD - hooks command not yet implemented"]
async fn test_hooks_verify_with_fix() -> Result<()> {
    // ARRANGE
    let fixture = HooksTestFixture::new()?;
    let hooks_cmd = HooksCommand::new(fixture.hooks_dir().clone(), fixture.config_path().clone());

    // Install hook
    let _ = hooks_cmd.install(false, true).await?;

    // Corrupt the hook to test fix
    std::fs::write(fixture.pre_commit_hook(), "#!/bin/bash\necho 'corrupted'")?;

    // ACT
    let result = hooks_cmd.verify(true).await?;

    // ASSERT
    assert!(result.is_valid); // Should be valid after fix
    assert!(!result.issues.is_empty()); // Should have detected issues
    assert!(!result.fixes_applied.is_empty()); // Should have applied fixes

    // Hook should be fixed
    let hook_content = std::fs::read_to_string(fixture.pre_commit_hook())?;
    assert!(hook_content.contains("PMAT"));
    assert!(!hook_content.contains("corrupted"));

    Ok(())
}

#[tokio::test]
#[ignore = "RED phase TDD - hooks command not yet implemented"]
async fn test_hooks_refresh() -> Result<()> {
    // ARRANGE
    let fixture = HooksTestFixture::new()?;
    let hooks_cmd = HooksCommand::new(fixture.hooks_dir().clone(), fixture.config_path().clone());

    // Install first
    let _ = hooks_cmd.install(false, true).await?;

    // Modify config to trigger refresh
    let new_config = r#"
[hooks]
enabled = true
auto_install = true

[hooks.quality_gates]
max_cyclomatic_complexity = 25
max_cognitive_complexity = 20
    "#;
    std::fs::write(&fixture.config_path, new_config)?;

    // ACT
    let result = hooks_cmd.refresh().await?;

    // ASSERT
    assert!(result.success);
    assert!(result.hook_updated);
    assert!(result.config_changes_detected);

    Ok(())
}

#[tokio::test]
#[ignore = "RED phase TDD - hooks command not yet implemented"]
async fn test_hooks_template_generation() -> Result<()> {
    // ARRANGE
    let fixture = HooksTestFixture::new()?;
    let hooks_cmd = HooksCommand::new(fixture.hooks_dir().clone(), fixture.config_path().clone());

    // ACT
    let _ = hooks_cmd.install(false, true).await?;

    // ASSERT
    let hook_content = std::fs::read_to_string(fixture.pre_commit_hook())?;

    // Should contain configuration values from pmat.toml
    assert!(hook_content.contains("30")); // max_cyclomatic_complexity
    assert!(hook_content.contains("25")); // max_cognitive_complexity
    assert!(hook_content.contains("80")); // min_test_coverage
    assert!(hook_content.contains("PMAT-[0-9]{4}")); // task_id_pattern

    // Should contain quality gate enforcement
    assert!(hook_content.contains("pmat") || hook_content.contains("analyze"));
    assert!(hook_content.contains("complexity"));

    Ok(())
}

// =============================================================================
// PROPERTY-BASED TESTS
// =============================================================================

#[tokio::test]
#[ignore = "RED phase TDD - hooks command not yet implemented"]
async fn test_hooks_idempotent_install() -> Result<()> {
    // Property: Installing hooks multiple times should be idempotent

    // ARRANGE
    let fixture = HooksTestFixture::new()?;
    let hooks_cmd = HooksCommand::new(fixture.hooks_dir().clone(), fixture.config_path().clone());

    // ACT
    let result1 = hooks_cmd.install(false, true).await?;
    let result2 = hooks_cmd.install(false, true).await?;
    let result3 = hooks_cmd.install(false, true).await?;

    // ASSERT
    assert!(result1.success);
    assert!(result2.success);
    assert!(result3.success);

    // Hook content should be identical
    let hook_content = std::fs::read_to_string(fixture.pre_commit_hook())?;
    assert!(hook_content.contains("PMAT"));

    Ok(())
}

// =============================================================================
// INTEGRATION TESTS
// =============================================================================

#[tokio::test]
#[ignore = "RED phase TDD - hooks command not yet implemented"]
async fn test_hooks_integration_with_config() -> Result<()> {
    // Integration test: hooks should reflect config changes

    // ARRANGE
    let fixture = HooksTestFixture::new()?;
    let hooks_cmd = HooksCommand::new(fixture.hooks_dir().clone(), fixture.config_path().clone());

    // Install with initial config
    let _ = hooks_cmd.install(false, true).await?;
    let initial_content = std::fs::read_to_string(fixture.pre_commit_hook())?;

    // Change config
    let updated_config = r#"
[hooks]
enabled = true

[hooks.quality_gates]
max_cyclomatic_complexity = 35
max_cognitive_complexity = 30
    "#;
    std::fs::write(&fixture.config_path, updated_config)?;

    // ACT
    let _ = hooks_cmd.refresh().await?;
    let updated_content = std::fs::read_to_string(fixture.pre_commit_hook())?;

    // ASSERT
    assert_ne!(initial_content, updated_content);
    assert!(updated_content.contains("35")); // New max_cyclomatic_complexity
    assert!(updated_content.contains("30")); // New max_cognitive_complexity

    Ok(())
}

#[tokio::test]
#[ignore = "RED phase TDD - hooks command not yet implemented"]
async fn test_hooks_performance_requirements() -> Result<()> {
    // Performance test: hook installation should be <5 seconds

    // ARRANGE
    let fixture = HooksTestFixture::new()?;
    let hooks_cmd = HooksCommand::new(fixture.hooks_dir().clone(), fixture.config_path().clone());

    // ACT
    let start = std::time::Instant::now();
    let _ = hooks_cmd.install(false, true).await?;
    let elapsed = start.elapsed();

    // ASSERT
    assert!(
        elapsed.as_secs() < 5,
        "Hook installation took {}s (should be <5s)",
        elapsed.as_secs()
    );

    Ok(())
}

// =============================================================================
// NEW COMMANDS TESTS (Sprint 61) - init and run
// =============================================================================

#[tokio::test]
#[ignore = "RED phase TDD - hooks init command not yet tested"]
async fn test_hooks_init_basic() -> Result<()> {
    // Test that `init` command works as alias for `install`

    // ARRANGE
    let fixture = HooksTestFixture::new()?;
    let hooks_cmd = HooksCommand::new(fixture.hooks_dir().clone(), fixture.config_path().clone());

    // ACT
    let result = hooks_cmd.install(false, true).await?;

    // ASSERT
    assert!(result.success);
    assert!(result.hook_created);
    assert!(fixture.pre_commit_hook().exists());

    Ok(())
}

#[tokio::test]
#[ignore = "RED phase TDD - hooks run command not yet implemented"]
async fn test_hooks_run_with_installed_hooks() -> Result<()> {
    // Test running hooks via `run` command

    // ARRANGE
    let fixture = HooksTestFixture::new()?;

    // Create a simple test hook that succeeds
    let test_hook = r#"#!/bin/bash
echo "✅ Test check passed"
exit 0
"#;
    fixture.create_existing_hook(test_hook)?;

    let _hooks_cmd = HooksCommand::new(fixture.hooks_dir().clone(), fixture.config_path().clone());

    // ACT - this would need to be implemented
    // let result = hooks_cmd.run(false, true).await?;

    // ASSERT
    // assert!(result.success);
    // assert_eq!(result.checks_passed, 1);
    // assert_eq!(result.checks_failed, 0);

    Ok(())
}

#[tokio::test]
#[ignore = "RED phase TDD - hooks run command not yet implemented"]
async fn test_hooks_run_all_files() -> Result<()> {
    // Test running hooks on all files (CI/CD mode)

    // ARRANGE
    let fixture = HooksTestFixture::new()?;

    // Create test hook
    let test_hook = r#"#!/bin/bash
echo "🔍 Running on all files..."
echo "✅ Check 1 passed"
echo "✅ Check 2 passed"
exit 0
"#;
    fixture.create_existing_hook(test_hook)?;

    let _hooks_cmd = HooksCommand::new(fixture.hooks_dir().clone(), fixture.config_path().clone());

    // ACT - would test all_files flag
    // let result = hooks_cmd.run(true, true).await?;

    // ASSERT
    // assert!(result.success);
    // assert_eq!(result.checks_passed, 2);

    Ok(())
}

#[tokio::test]
#[ignore = "RED phase TDD - hooks run command not yet implemented"]
async fn test_hooks_run_fails_on_error() -> Result<()> {
    // Test that run command fails when hook exits with error

    // ARRANGE
    let fixture = HooksTestFixture::new()?;

    // Create hook that fails
    let test_hook = r#"#!/bin/bash
echo "✅ Check 1 passed"
echo "❌ Check 2 failed"
exit 1
"#;
    fixture.create_existing_hook(test_hook)?;

    let _hooks_cmd = HooksCommand::new(fixture.hooks_dir().clone(), fixture.config_path().clone());

    // ACT & ASSERT
    // let result = hooks_cmd.run(false, false).await?;
    // assert!(!result.success);
    // assert_eq!(result.checks_passed, 1);
    // assert_eq!(result.checks_failed, 1);

    Ok(())
}