pmat 3.15.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
    #[test]
    fn test_print_compliance_text_all_status_types() {
        let report = ComplianceReport {
            project_version: PMAT_VERSION.to_string(),
            current_version: PMAT_VERSION.to_string(),
            is_compliant: true,
            versions_behind: 0,
            checks: vec![
                ComplianceCheck {
                    name: "Pass".to_string(),
                    status: CheckStatus::Pass,
                    message: "Good".to_string(),
                    severity: Severity::Info,
                },
                ComplianceCheck {
                    name: "Warn".to_string(),
                    status: CheckStatus::Warn,
                    message: "Warning".to_string(),
                    severity: Severity::Warning,
                },
                ComplianceCheck {
                    name: "Fail".to_string(),
                    status: CheckStatus::Fail,
                    message: "Failed".to_string(),
                    severity: Severity::Error,
                },
                ComplianceCheck {
                    name: "Skip".to_string(),
                    status: CheckStatus::Skip,
                    message: "Skipped".to_string(),
                    severity: Severity::Info,
                },
            ],
            breaking_changes: vec![],
            recommendations: vec![],
            timestamp: Utc::now(),
        };
        print_compliance_text(&report);
    }

    // print_compliance_markdown Tests

    #[test]
    fn test_print_compliance_markdown_compliant() {
        let report = ComplianceReport {
            project_version: PMAT_VERSION.to_string(),
            current_version: PMAT_VERSION.to_string(),
            is_compliant: true,
            versions_behind: 0,
            checks: vec![],
            breaking_changes: vec![],
            recommendations: vec![],
            timestamp: Utc::now(),
        };
        print_compliance_markdown(&report);
    }

    #[test]
    fn test_print_compliance_markdown_non_compliant() {
        let report = ComplianceReport {
            project_version: "1.0.0".to_string(),
            current_version: PMAT_VERSION.to_string(),
            is_compliant: false,
            versions_behind: 5,
            checks: vec![ComplianceCheck {
                name: "Check".to_string(),
                status: CheckStatus::Fail,
                message: "Failed".to_string(),
                severity: Severity::Error,
            }],
            breaking_changes: vec![],
            recommendations: vec![],
            timestamp: Utc::now(),
        };
        print_compliance_markdown(&report);
    }

    // Async Handler Tests (using tokio::test)

    #[tokio::test]
    async fn test_handle_init_new_project() {
        let temp = create_temp_project();
        let result = handle_init(temp.path(), false).await;
        assert!(result.is_ok());

        // Verify project.toml created
        assert!(temp.path().join(".pmat").join("project.toml").exists());
    }

    #[tokio::test]
    async fn test_handle_init_existing_no_force() {
        let temp = create_pmat_project("1.0.0");
        let result = handle_init(temp.path(), false).await;
        assert!(result.is_ok());

        // Version should remain unchanged
        let config = load_or_create_project_config(temp.path()).expect("Failed to load config");
        assert_eq!(config.pmat.version, "1.0.0");
    }

    #[tokio::test]
    async fn test_handle_init_existing_with_force() {
        let temp = create_pmat_project("1.0.0");
        let result = handle_init(temp.path(), true).await;
        assert!(result.is_ok());

        // Version should be updated to current
        let config = load_or_create_project_config(temp.path()).expect("Failed to load config");
        assert_eq!(config.pmat.version, PMAT_VERSION);
    }

    #[tokio::test]
    async fn test_handle_update_both() {
        let temp = create_pmat_project("1.0.0");
        let result = handle_update(temp.path(), false, false, false).await;
        assert!(result.is_ok());

        let config = load_or_create_project_config(temp.path()).expect("Failed to load config");
        assert_eq!(config.pmat.version, PMAT_VERSION);
    }

    #[tokio::test]
    async fn test_handle_update_dry_run() {
        let temp = create_pmat_project("1.0.0");
        let result = handle_update(temp.path(), false, false, true).await;
        assert!(result.is_ok());

        let config = load_or_create_project_config(temp.path()).expect("Failed to load config");
        assert_eq!(config.pmat.version, "1.0.0"); // Unchanged
    }

    #[tokio::test]
    async fn test_handle_update_hooks_only() {
        let temp = create_pmat_project("1.0.0");
        let result = handle_update(temp.path(), true, false, false).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_handle_update_config_only() {
        let temp = create_pmat_project("1.0.0");
        let result = handle_update(temp.path(), false, true, false).await;
        assert!(result.is_ok());

        let config = load_or_create_project_config(temp.path()).expect("Failed to load config");
        assert_eq!(config.pmat.version, PMAT_VERSION);
    }

    #[tokio::test]
    async fn test_handle_diff_default_versions() {
        let temp = create_pmat_project("1.0.0");
        let result = handle_diff(temp.path(), None, None, false).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_handle_diff_specific_versions() {
        let temp = create_pmat_project("1.0.0");
        let result = handle_diff(temp.path(), Some("1.0.0"), Some("2.0.0"), false).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_handle_diff_breaking_only() {
        let temp = create_pmat_project("1.0.0");
        let result = handle_diff(temp.path(), None, None, true).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_handle_migrate_dry_run() {
        let temp = create_pmat_project("1.0.0");
        let result = handle_migrate(temp.path(), None, true, false, false).await;
        assert!(result.is_ok());

        let config = load_or_create_project_config(temp.path()).expect("Failed to load config");
        assert_eq!(config.pmat.version, "1.0.0"); // Unchanged
    }

    #[tokio::test]
    async fn test_handle_migrate_with_target() {
        let temp = create_pmat_project("1.0.0");
        let result = handle_migrate(temp.path(), Some("2.0.0"), false, true, true).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_handle_migrate_no_backup() {
        let temp = create_pmat_project("1.0.0");
        let result = handle_migrate(temp.path(), None, false, true, true).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_handle_migrate_with_backup() {
        let temp = create_pmat_project("1.0.0");
        let result = handle_migrate(temp.path(), None, false, false, true).await;
        assert!(result.is_ok());

        // Verify backup directory created
        assert!(temp.path().join(".pmat").join("backup").exists());
    }

    #[tokio::test]
    async fn test_handle_enforce_no_git() {
        let temp = create_temp_project();
        let result = handle_enforce(temp.path(), true, false, ComplyOutputFormat::Text).await;
        assert!(result.is_err());
        let err = result.err().unwrap();
        assert!(err.to_string().contains("Not a git repository"));
    }

    #[tokio::test]
    async fn test_handle_enforce_install() {
        let temp = create_git_repo();
        let result = handle_enforce(temp.path(), true, false, ComplyOutputFormat::Text).await;
        assert!(result.is_ok());

        // Verify hook created
        let hook_path = temp.path().join(".git").join("hooks").join("pre-commit");
        assert!(hook_path.exists());
        let content = fs::read_to_string(&hook_path).expect("Failed to read hook");
        assert!(content.contains("PMAT"));
    }

    #[tokio::test]
    async fn test_handle_enforce_disable() {
        let temp = create_git_repo();
        // First install
        handle_enforce(temp.path(), true, false, ComplyOutputFormat::Text)
            .await
            .expect("Failed to install");

        // Then disable
        let result = handle_enforce(temp.path(), true, true, ComplyOutputFormat::Text).await;
        assert!(result.is_ok());

        // Verify hook removed
        let hook_path = temp.path().join(".git").join("hooks").join("pre-commit");
        assert!(!hook_path.exists());
    }

    #[tokio::test]
    async fn test_handle_enforce_disable_non_pmat_hook() {
        let temp = create_git_repo();
        let hook_path = temp.path().join(".git").join("hooks").join("pre-commit");
        fs::write(&hook_path, "#!/bin/sh\necho 'other hook'").expect("Failed to write hook");

        let result = handle_enforce(temp.path(), true, true, ComplyOutputFormat::Text).await;
        assert!(result.is_ok());

        // Non-PMAT hook should NOT be removed
        assert!(hook_path.exists());
    }

    #[tokio::test]
    async fn test_handle_enforce_json_format() {
        let temp = create_git_repo();
        let result = handle_enforce(temp.path(), true, false, ComplyOutputFormat::Json).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_handle_enforce_markdown_format() {
        let temp = create_git_repo();
        let result = handle_enforce(temp.path(), true, false, ComplyOutputFormat::Markdown).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_handle_report_text() {
        let temp = create_pmat_project(PMAT_VERSION);
        let result = handle_report(temp.path(), false, ComplyOutputFormat::Text, None).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_handle_report_json() {
        let temp = create_pmat_project(PMAT_VERSION);
        let result = handle_report(temp.path(), false, ComplyOutputFormat::Json, None).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_handle_report_markdown() {
        let temp = create_pmat_project(PMAT_VERSION);
        let result = handle_report(temp.path(), false, ComplyOutputFormat::Markdown, None).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_handle_report_with_history() {
        let temp = create_pmat_project(PMAT_VERSION);
        let result = handle_report(temp.path(), true, ComplyOutputFormat::Text, None).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_handle_report_to_file() {
        let temp = create_pmat_project(PMAT_VERSION);
        let output_file = temp.path().join("report.md");
        let result = handle_report(
            temp.path(),
            false,
            ComplyOutputFormat::Markdown,
            Some(&output_file),
        )
        .await;
        assert!(result.is_ok());
        assert!(output_file.exists());
    }

    // handle_comply_command Tests

    #[tokio::test]
    async fn test_handle_comply_command_init() {
        let temp = create_temp_project();
        let command = ComplyCommands::Init {
            path: temp.path().to_path_buf(),
            force: false,
        };
        let result = handle_comply_command(command).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_handle_comply_command_update() {
        let temp = create_pmat_project("1.0.0");
        let command = ComplyCommands::Update {
            path: temp.path().to_path_buf(),
            hooks: false,
            config: false,
            dry_run: true,
        };
        let result = handle_comply_command(command).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_handle_comply_command_diff() {
        let temp = create_pmat_project("1.0.0");
        let command = ComplyCommands::Diff {
            path: temp.path().to_path_buf(),
            from: None,
            to: None,
            breaking_only: false,
        };
        let result = handle_comply_command(command).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_handle_comply_command_migrate() {
        let temp = create_pmat_project("1.0.0");
        let command = ComplyCommands::Migrate {
            path: temp.path().to_path_buf(),
            version: None,
            dry_run: true,
            no_backup: true,
            force: true,
        };
        let result = handle_comply_command(command).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_handle_comply_command_enforce() {
        let temp = create_git_repo();
        let command = ComplyCommands::Enforce {
            path: temp.path().to_path_buf(),
            yes: true,
            disable: false,
            format: ComplyOutputFormat::Text,
        };
        let result = handle_comply_command(command).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_handle_comply_command_report() {
        let temp = create_pmat_project(PMAT_VERSION);
        let command = ComplyCommands::Report {
            path: temp.path().to_path_buf(),
            include_history: false,
            format: ComplyOutputFormat::Text,
            output: None,
        };
        let result = handle_comply_command(command).await;
        assert!(result.is_ok());
    }

    // Edge Cases and Error Paths

    #[test]
    fn test_version_parsing_with_prerelease() {
        let behind = calculate_versions_behind("2.0.0-alpha.1");
        // Should handle prerelease gracefully
        assert!(behind >= 0);
    }

    #[test]
    fn test_version_parsing_with_build_metadata() {
        let behind = calculate_versions_behind("2.0.0+build.123");
        assert!(behind >= 0);
    }

    #[test]
    fn test_compliance_check_debug_impl() {
        let check = ComplianceCheck {
            name: "Test".to_string(),
            status: CheckStatus::Pass,
            message: "OK".to_string(),
            severity: Severity::Info,
        };
        let debug_str = format!("{:?}", check);
        assert!(debug_str.contains("ComplianceCheck"));
        assert!(debug_str.contains("Pass"));
    }

    #[test]
    fn test_project_config_debug_impl() {
        let config = ProjectConfig::default();
        let debug_str = format!("{:?}", config);
        assert!(debug_str.contains("ProjectConfig"));
    }

    #[test]
    fn test_breaking_change_debug_impl() {
        let change = BreakingChange {
            version: "1.0.0".to_string(),
            description: "Test".to_string(),
            migration_guide: None,
        };
        let debug_str = format!("{:?}", change);
        assert!(debug_str.contains("BreakingChange"));
    }

    #[test]
    fn test_compliance_report_debug_impl() {
        let report = ComplianceReport {
            project_version: "1.0.0".to_string(),
            current_version: "2.0.0".to_string(),
            is_compliant: true,
            versions_behind: 0,
            checks: vec![],
            breaking_changes: vec![],
            recommendations: vec![],
            timestamp: Utc::now(),
        };
        let debug_str = format!("{:?}", report);
        assert!(debug_str.contains("ComplianceReport"));
    }

    #[tokio::test]
    async fn test_handle_check_with_nonexistent_path() {
        let temp = create_temp_project();
        let nonexistent = temp.path().join("nonexistent");
        // This should create the config directory
        let result = load_or_create_project_config(&nonexistent);
        // May fail due to parent directory not existing
        // Just verify it handles the error gracefully
        let _ = result;
    }

    #[test]
    fn test_changelog_entry_struct() {
        // Test the ChangelogEntry struct directly
        let entry = ChangelogEntry {
            version: "1.0.0".to_string(),
            description: "Test change".to_string(),
            breaking: true,
        };
        assert_eq!(entry.version, "1.0.0");
        assert!(entry.breaking);

        // Test clone
        let cloned = entry.clone();
        assert_eq!(cloned.version, entry.version);
        assert_eq!(cloned.breaking, entry.breaking);
    }

    #[test]
    fn test_pmat_version_constant() {
        // Verify PMAT_VERSION is set from Cargo.toml
        assert!(!PMAT_VERSION.is_empty());
        // Should be a valid semver-ish format
        let parts: Vec<&str> = PMAT_VERSION.split('.').collect();
        assert!(parts.len() >= 2, "Version should have at least major.minor");
    }

    // Integration-style Tests