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
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod coverage_tests {
    use super::*;
    use crate::contracts::{BaseAnalysisContract, OutputFormat, QualityProfile, SatdSeverity};
    use std::path::PathBuf;
    use tempfile::TempDir;

    /// Helper to create a temp directory with a valid path for testing
    fn create_test_dir() -> TempDir {
        tempfile::tempdir().expect("Failed to create temp directory")
    }

    /// Helper to create a temp file for refactor tests
    fn create_test_file(dir: &TempDir) -> PathBuf {
        let file_path = dir.path().join("test.rs");
        std::fs::write(&file_path, "fn main() { println!(\"hello\"); }").unwrap();
        file_path
    }

    /// Helper to create a base contract with valid path
    fn create_base_contract(path: PathBuf) -> BaseAnalysisContract {
        BaseAnalysisContract {
            path,
            format: OutputFormat::Json,
            output: None,
            top_files: Some(10),
            include_tests: false,
            timeout: 60,
        }
    }

    #[test]
    fn test_real_contract_service_new() {
        let service = RealContractService::new();
        assert!(service.is_ok(), "RealContractService::new() should succeed");
    }

    #[test]
    fn test_real_contract_service_default() {
        let service = RealContractService::default();
        // Default should create a working service
        assert!(Arc::strong_count(&service.inner) >= 1);
    }

    #[tokio::test]
    async fn test_analyze_complexity() {
        let temp_dir = create_test_dir();
        let service = RealContractService::new().unwrap();

        let contract = AnalyzeComplexityContract {
            base: create_base_contract(temp_dir.path().to_path_buf()),
            max_cyclomatic: Some(20),
            max_cognitive: Some(15),
            max_halstead: Some(50.0),
        };

        let result = service.analyze_complexity(contract).await;
        assert!(result.is_ok(), "analyze_complexity should succeed");

        let value = result.unwrap();
        assert!(value.get("summary").is_some());
        assert!(value.get("results").is_some());
        assert!(value.get("metadata").is_some());
    }

    #[tokio::test]
    async fn test_analyze_complexity_default_thresholds() {
        let temp_dir = create_test_dir();
        let service = RealContractService::new().unwrap();

        let contract = AnalyzeComplexityContract {
            base: create_base_contract(temp_dir.path().to_path_buf()),
            max_cyclomatic: None,
            max_cognitive: None,
            max_halstead: None,
        };

        let result = service.analyze_complexity(contract).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_analyze_satd() {
        let temp_dir = create_test_dir();
        let service = RealContractService::new().unwrap();

        let contract = AnalyzeSatdContract {
            base: create_base_contract(temp_dir.path().to_path_buf()),
            severity: Some(SatdSeverity::Medium),
            critical_only: false,
            strict: true,
            fail_on_violation: false,
        };

        let result = service.analyze_satd(contract).await;
        assert!(result.is_ok(), "analyze_satd should succeed");

        let value = result.unwrap();
        assert!(value.get("summary").is_some());
    }

    #[tokio::test]
    async fn test_analyze_satd_critical_only() {
        let temp_dir = create_test_dir();
        let service = RealContractService::new().unwrap();

        let contract = AnalyzeSatdContract {
            base: create_base_contract(temp_dir.path().to_path_buf()),
            severity: Some(SatdSeverity::Critical),
            critical_only: true,
            strict: false,
            fail_on_violation: false,
        };

        let result = service.analyze_satd(contract).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_analyze_dead_code() {
        let temp_dir = create_test_dir();
        let service = RealContractService::new().unwrap();

        let contract = AnalyzeDeadCodeContract {
            base: create_base_contract(temp_dir.path().to_path_buf()),
            include_unreachable: true,
            min_dead_lines: 5,
            max_percentage: 20.0,
            fail_on_violation: false,
        };

        let result = service.analyze_dead_code(contract).await;
        assert!(result.is_ok(), "analyze_dead_code should succeed");

        let value = result.unwrap();
        assert!(value.get("results").is_some());
    }

    #[tokio::test]
    async fn test_analyze_dead_code_no_unreachable() {
        let temp_dir = create_test_dir();
        let service = RealContractService::new().unwrap();

        let contract = AnalyzeDeadCodeContract {
            base: create_base_contract(temp_dir.path().to_path_buf()),
            include_unreachable: false,
            min_dead_lines: 1,
            max_percentage: 100.0,
            fail_on_violation: false,
        };

        let result = service.analyze_dead_code(contract).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_analyze_tdg() {
        let temp_dir = create_test_dir();
        let service = RealContractService::new().unwrap();

        let contract = AnalyzeTdgContract {
            base: create_base_contract(temp_dir.path().to_path_buf()),
            threshold: 1.5,
            include_components: true,
            critical_only: false,
        };

        let result = service.analyze_tdg(contract).await;
        assert!(result.is_ok(), "analyze_tdg should succeed");

        let value = result.unwrap();
        assert!(value.get("summary").is_some());
    }

    #[tokio::test]
    async fn test_analyze_tdg_without_components() {
        let temp_dir = create_test_dir();
        let service = RealContractService::new().unwrap();

        let contract = AnalyzeTdgContract {
            base: create_base_contract(temp_dir.path().to_path_buf()),
            threshold: 2.5,
            include_components: false,
            critical_only: true,
        };

        let result = service.analyze_tdg(contract).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_analyze_lint_hotspot() {
        let temp_dir = create_test_dir();
        let service = RealContractService::new().unwrap();

        let contract = AnalyzeLintHotspotContract {
            base: create_base_contract(temp_dir.path().to_path_buf()),
            file: None,
            max_density: 5.0,
            min_confidence: 0.8,
            enforce: false,
            dry_run: false,
        };

        let result = service.analyze_lint_hotspot(contract).await;
        assert!(result.is_ok(), "analyze_lint_hotspot should succeed");
    }

    #[tokio::test]
    async fn test_analyze_lint_hotspot_dry_run() {
        let temp_dir = create_test_dir();
        let service = RealContractService::new().unwrap();

        let contract = AnalyzeLintHotspotContract {
            base: create_base_contract(temp_dir.path().to_path_buf()),
            file: None,
            max_density: 3.0,
            min_confidence: 0.9,
            enforce: true,
            dry_run: true,
        };

        let result = service.analyze_lint_hotspot(contract).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_quality_gate_standard_profile() {
        let temp_dir = create_test_dir();
        let service = RealContractService::new().unwrap();

        let contract = QualityGateContract {
            base: create_base_contract(temp_dir.path().to_path_buf()),
            profile: QualityProfile::Standard,
            file: None,
            fail_on_violation: false,
            verbose: true,
        };

        let result = service.quality_gate(contract).await;
        assert!(result.is_ok(), "quality_gate should succeed");

        let value = result.unwrap();
        assert!(value.get("passed").is_some());
    }

    #[tokio::test]
    async fn test_quality_gate_strict_profile() {
        let temp_dir = create_test_dir();
        let service = RealContractService::new().unwrap();

        let contract = QualityGateContract {
            base: create_base_contract(temp_dir.path().to_path_buf()),
            profile: QualityProfile::Strict,
            file: None,
            fail_on_violation: false,
            verbose: false,
        };

        let result = service.quality_gate(contract).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_quality_gate_extreme_profile() {
        let temp_dir = create_test_dir();
        let service = RealContractService::new().unwrap();

        let contract = QualityGateContract {
            base: create_base_contract(temp_dir.path().to_path_buf()),
            profile: QualityProfile::Extreme,
            file: None,
            fail_on_violation: false,
            verbose: true,
        };

        let result = service.quality_gate(contract).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_quality_gate_toyota_profile() {
        let temp_dir = create_test_dir();
        let service = RealContractService::new().unwrap();

        let contract = QualityGateContract {
            base: create_base_contract(temp_dir.path().to_path_buf()),
            profile: QualityProfile::Toyota,
            file: None,
            fail_on_violation: false,
            verbose: false,
        };

        let result = service.quality_gate(contract).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_refactor_auto() {
        let temp_dir = create_test_dir();
        let test_file = create_test_file(&temp_dir);
        let service = RealContractService::new().unwrap();

        let contract = RefactorAutoContract {
            file: test_file,
            format: OutputFormat::Json,
            output: None,
            target_complexity: 10,
            dry_run: true,
            timeout: 60,
        };

        let result = service.refactor_auto(contract).await;
        assert!(result.is_ok(), "refactor_auto should succeed");

        let value = result.unwrap();
        assert!(value.get("plan").is_some());
        assert!(value.get("dry_run").is_some());
    }

    #[tokio::test]
    async fn test_refactor_auto_apply() {
        let temp_dir = create_test_dir();
        let test_file = create_test_file(&temp_dir);
        let service = RealContractService::new().unwrap();

        let contract = RefactorAutoContract {
            file: test_file,
            format: OutputFormat::Markdown,
            output: None,
            target_complexity: 5,
            dry_run: false,
            timeout: 120,
        };

        let result = service.refactor_auto(contract).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_output_formats() {
        let temp_dir = create_test_dir();
        let service = RealContractService::new().unwrap();

        // Test each output format
        for format in [
            OutputFormat::Table,
            OutputFormat::Json,
            OutputFormat::Yaml,
            OutputFormat::Markdown,
            OutputFormat::Csv,
            OutputFormat::Summary,
        ] {
            let mut base = create_base_contract(temp_dir.path().to_path_buf());
            base.format = format;

            let contract = AnalyzeComplexityContract {
                base,
                max_cyclomatic: None,
                max_cognitive: None,
                max_halstead: None,
            };

            let result = service.analyze_complexity(contract).await;
            assert!(result.is_ok(), "Should work with format {:?}", format);
        }
    }

    #[tokio::test]
    async fn test_with_output_file() {
        let temp_dir = create_test_dir();
        let service = RealContractService::new().unwrap();
        let output_path = temp_dir.path().join("output.json");

        let mut base = create_base_contract(temp_dir.path().to_path_buf());
        base.output = Some(output_path);

        let contract = AnalyzeComplexityContract {
            base,
            max_cyclomatic: Some(30),
            max_cognitive: None,
            max_halstead: None,
        };

        let result = service.analyze_complexity(contract).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_include_tests_flag() {
        let temp_dir = create_test_dir();
        let service = RealContractService::new().unwrap();

        let mut base = create_base_contract(temp_dir.path().to_path_buf());
        base.include_tests = true;

        let contract = AnalyzeSatdContract {
            base,
            severity: None,
            critical_only: false,
            strict: false,
            fail_on_violation: false,
        };

        let result = service.analyze_satd(contract).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_all_satd_severities() {
        let temp_dir = create_test_dir();
        let service = RealContractService::new().unwrap();

        for severity in [
            SatdSeverity::Low,
            SatdSeverity::Medium,
            SatdSeverity::High,
            SatdSeverity::Critical,
        ] {
            let contract = AnalyzeSatdContract {
                base: create_base_contract(temp_dir.path().to_path_buf()),
                severity: Some(severity),
                critical_only: false,
                strict: false,
                fail_on_violation: false,
            };

            let result = service.analyze_satd(contract).await;
            assert!(result.is_ok(), "Should work with severity {:?}", severity);
        }
    }

    #[test]
    fn test_service_inner_arc() {
        let service = RealContractService::new().unwrap();
        // Verify the Arc reference counting works correctly
        let arc_clone = Arc::clone(&service.inner);
        assert_eq!(Arc::strong_count(&service.inner), 2);
        drop(arc_clone);
        assert_eq!(Arc::strong_count(&service.inner), 1);
    }
}