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
// =============================================================================
// OIP Tarantula patterns check tests (CB-120 through CB-124)
// =============================================================================

#[test]
fn test_check_oip_tarantula_patterns_clean() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    std::fs::create_dir_all(&src_dir).unwrap();
    std::fs::write(src_dir.join("lib.rs"), "pub fn safe_fn() {}").unwrap();

    let check =
        crate::cli::handlers::comply_handlers::check_oip_tarantula_patterns(temp_dir.path());
    assert_eq!(check.status, CheckStatus::Pass);
}

// =============================================================================
// Coverage quality patterns check tests (CB-125 through CB-127)
// =============================================================================

#[test]
fn test_check_coverage_quality_patterns_clean() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    std::fs::create_dir_all(&src_dir).unwrap();
    std::fs::write(src_dir.join("lib.rs"), "pub fn clean() {}").unwrap();

    let check =
        crate::cli::handlers::comply_handlers::check_coverage_quality_patterns(temp_dir.path());
    assert_eq!(check.status, CheckStatus::Pass);
}

// =============================================================================
// Dead code percentage check tests (CB-304)
// =============================================================================

#[test]
fn test_check_dead_code_percentage_no_src() {
    let temp_dir = TempDir::new().unwrap();

    let check = crate::cli::handlers::comply_handlers::check_dead_code_percentage(temp_dir.path());
    assert_eq!(check.status, CheckStatus::Skip);
}

#[test]
fn test_check_dead_code_percentage_clean() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    std::fs::create_dir_all(&src_dir).unwrap();
    std::fs::write(
        src_dir.join("lib.rs"),
        "pub fn active_fn() { println!(\"used\"); }",
    )
    .unwrap();

    let check = crate::cli::handlers::comply_handlers::check_dead_code_percentage(temp_dir.path());
    // Clean code should pass
    assert!(check.status == CheckStatus::Pass || check.status == CheckStatus::Skip);
}

#[test]
fn test_check_dead_code_percentage_with_dead_code() {
    let temp_dir = TempDir::new().unwrap();
    let src_dir = temp_dir.path().join("src");
    std::fs::create_dir_all(&src_dir).unwrap();
    // Create file with dead code annotations
    std::fs::write(
        src_dir.join("lib.rs"),
        r#"
#[allow(dead_code)]
fn dead_fn1() {}
#[allow(dead_code)]
fn dead_fn2() {}
#[allow(dead_code)]
fn dead_fn3() {}
pub fn active_fn() {}
"#,
    )
    .unwrap();

    let check = crate::cli::handlers::comply_handlers::check_dead_code_percentage(temp_dir.path());
    // Should detect dead code
    assert!(check.name.contains("Dead Code"));
}

// =============================================================================
// Dead code analysis helper tests
// =============================================================================

#[test]
fn test_is_heavily_cfg_gated_false() {
    let content = "pub fn normal_fn() {}";
    let result = crate::cli::handlers::comply_handlers::is_heavily_cfg_gated(content);
    assert!(!result);
}

#[test]
fn test_is_heavily_cfg_gated_true() {
    let content = r#"
#[cfg(target_arch = "x86_64")]
fn simd_fn1() {}
#[cfg(target_feature = "avx2")]
fn simd_fn2() {}
#[cfg(feature = "simd")]
fn simd_fn3() {}
#[target_feature(enable = "sse2")]
fn simd_fn4() {}
"#;
    let result = crate::cli::handlers::comply_handlers::is_heavily_cfg_gated(content);
    assert!(result);
}

#[test]
fn test_is_dead_code_annotation_true() {
    assert!(crate::cli::handlers::comply_handlers::is_dead_code_annotation("#[allow(dead_code)]"));
    assert!(crate::cli::handlers::comply_handlers::is_dead_code_annotation("#[allow(unused)]"));
}

#[test]
fn test_is_dead_code_annotation_false() {
    assert!(!crate::cli::handlers::comply_handlers::is_dead_code_annotation("pub fn active() {}"));
    assert!(!crate::cli::handlers::comply_handlers::is_dead_code_annotation("#[derive(Debug)]"));
}

#[test]
fn test_is_code_item_declaration_true() {
    assert!(crate::cli::handlers::comply_handlers::is_code_item_declaration("pub fn test() {}"));
    assert!(crate::cli::handlers::comply_handlers::is_code_item_declaration("fn private() {}"));
    assert!(
        crate::cli::handlers::comply_handlers::is_code_item_declaration("pub struct MyStruct {")
    );
    assert!(crate::cli::handlers::comply_handlers::is_code_item_declaration("pub enum MyEnum {"));
    assert!(crate::cli::handlers::comply_handlers::is_code_item_declaration("pub trait MyTrait {"));
    assert!(
        crate::cli::handlers::comply_handlers::is_code_item_declaration(
            "pub const VALUE: i32 = 42;"
        )
    );
    assert!(
        crate::cli::handlers::comply_handlers::is_code_item_declaration(
            "pub async fn async_fn() {}"
        )
    );
}

#[test]
fn test_is_code_item_declaration_false() {
    assert!(!crate::cli::handlers::comply_handlers::is_code_item_declaration("let x = 5;"));
    assert!(!crate::cli::handlers::comply_handlers::is_code_item_declaration("// comment"));
    assert!(!crate::cli::handlers::comply_handlers::is_code_item_declaration("#[derive(Debug)]"));
}

#[test]
fn test_has_code_markers_true() {
    assert!(crate::cli::handlers::comply_handlers::has_code_markers(
        "fn test() {"
    ));
    assert!(crate::cli::handlers::comply_handlers::has_code_markers(
        "let x = 5;"
    ));
    assert!(crate::cli::handlers::comply_handlers::has_code_markers(
        "return value;"
    ));
    assert!(crate::cli::handlers::comply_handlers::has_code_markers(
        "struct Foo {"
    ));
}

#[test]
fn test_has_code_markers_false() {
    assert!(!crate::cli::handlers::comply_handlers::has_code_markers(
        "just a comment"
    ));
    assert!(!crate::cli::handlers::comply_handlers::has_code_markers(
        "empty line"
    ));
}

#[test]
fn test_is_commented_out_code_true() {
    assert!(crate::cli::handlers::comply_handlers::is_commented_out_code("// fn test() {"));
    assert!(crate::cli::handlers::comply_handlers::is_commented_out_code("// let x = 5;"));
    assert!(crate::cli::handlers::comply_handlers::is_commented_out_code("// return foo;"));
}

#[test]
fn test_is_commented_out_code_false() {
    // Just a regular comment
    assert!(
        !crate::cli::handlers::comply_handlers::is_commented_out_code(
            "// This is a regular comment"
        )
    );
    // Not a comment
    assert!(!crate::cli::handlers::comply_handlers::is_commented_out_code("fn test() {}"));
}

#[test]
fn test_flush_comment_run_threshold() {
    // Run of 3 or more counts
    assert_eq!(
        crate::cli::handlers::comply_handlers::flush_comment_run(3),
        3
    );
    assert_eq!(
        crate::cli::handlers::comply_handlers::flush_comment_run(5),
        5
    );
    // Run of less than 3 doesn't count
    assert_eq!(
        crate::cli::handlers::comply_handlers::flush_comment_run(2),
        0
    );
    assert_eq!(
        crate::cli::handlers::comply_handlers::flush_comment_run(0),
        0
    );
}

#[test]
fn test_update_macro_depth_enter() {
    let depth =
        crate::cli::handlers::comply_handlers::update_macro_depth("macro_rules! my_macro {", None);
    assert!(depth.is_some());
}

#[test]
fn test_update_macro_depth_exit() {
    let depth = crate::cli::handlers::comply_handlers::update_macro_depth("}", Some(1));
    assert!(depth.is_none());
}

#[test]
fn test_update_macro_depth_nested() {
    // Start macro
    let depth =
        crate::cli::handlers::comply_handlers::update_macro_depth("macro_rules! test {", None);
    assert!(depth.is_some());

    // Inside macro, nested brace
    let depth = crate::cli::handlers::comply_handlers::update_macro_depth("() => { {", depth);
    assert!(depth.is_some());
}

#[test]
fn test_filter_production_lines() {
    let lines = vec![
        "pub fn main() {}",
        "#[cfg(test)]",
        "mod tests {",
        "    #[test]",
        "    fn test_something() {}",
        "}",
    ];
    let prod_lines = crate::cli::handlers::comply_handlers::filter_production_lines(&lines);
    assert_eq!(prod_lines.len(), 1);
    assert_eq!(prod_lines[0], "pub fn main() {}");
}

#[test]
fn test_count_dead_items_no_dead_code() {
    let lines = vec![
        "pub fn active() {}",
        "pub struct Active {}",
        "impl Active {}",
    ];
    let refs: Vec<&str> = lines.iter().map(|s| *s).collect();
    let (total, dead, annotations) = crate::cli::handlers::comply_handlers::count_dead_items(&refs);
    assert_eq!(total, 2); // fn + struct
    assert_eq!(dead, 0);
    assert_eq!(annotations, 0);
}

#[test]
fn test_count_dead_items_with_dead_code() {
    let lines = vec![
        "#[allow(dead_code)]",
        "fn dead_fn() {}",
        "pub fn active() {}",
    ];
    let refs: Vec<&str> = lines.iter().map(|s| *s).collect();
    let (total, dead, annotations) = crate::cli::handlers::comply_handlers::count_dead_items(&refs);
    assert_eq!(total, 2);
    assert_eq!(dead, 1);
    assert_eq!(annotations, 1);
}

#[test]
fn test_count_block_comment_code_lines() {
    let lines = vec![
        "/* fn old_function() {",
        "    let x = 5;",
        "    return x;",
        "} */",
    ];
    let refs: Vec<&str> = lines.iter().map(|s| *s).collect();
    let dead_lines = crate::cli::handlers::comply_handlers::count_block_comment_code_lines(&refs);
    // Should detect code-like content in block comment
    assert!(dead_lines >= 2);
}

#[test]
fn test_count_commented_code_lines() {
    let lines = vec![
        "// fn old_function() {",
        "//     let x = 5;",
        "//     return x;",
        "// }",
        "pub fn new_function() {}",
    ];
    let refs: Vec<&str> = lines.iter().map(|s| *s).collect();
    let dead_lines = crate::cli::handlers::comply_handlers::count_commented_code_lines(&refs);
    // Should detect 4 consecutive commented code lines
    assert_eq!(dead_lines, 4);
}

#[test]
fn test_analyze_file_dead_code() {
    let lines = vec![
        "#[allow(dead_code)]",
        "fn dead() {}",
        "pub fn active() {}",
        "#[cfg(test)]",
        "mod tests {}",
    ];
    let refs: Vec<&str> = lines.iter().map(|s| *s).collect();
    let (total, dead, prod_lines, estimated_dead) =
        crate::cli::handlers::comply_handlers::analyze_file_dead_code(&refs);

    assert!(total >= 2);
    assert!(dead >= 1);
    assert!(prod_lines >= 3);
    assert!(estimated_dead >= 10); // ~10 per annotation
}

// =============================================================================
// format_violation_list tests
// =============================================================================

#[test]
fn test_format_violation_list_empty() {
    let issues: Vec<String> = vec![];
    let result = crate::cli::handlers::comply_handlers::format_violation_list(&issues);
    assert!(result.is_empty());
}

#[test]
fn test_format_violation_list_single() {
    let issues = vec!["CB-001: Test issue".to_string()];
    let result = crate::cli::handlers::comply_handlers::format_violation_list(&issues);
    assert_eq!(result, "    - CB-001: Test issue");
}

#[test]
fn test_format_violation_list_multiple() {
    let issues = vec![
        "Issue 1".to_string(),
        "Issue 2".to_string(),
        "Issue 3".to_string(),
    ];
    let result = crate::cli::handlers::comply_handlers::format_violation_list(&issues);
    assert!(result.contains("    - Issue 1"));
    assert!(result.contains("    - Issue 2"));
    assert!(result.contains("    - Issue 3"));
}

// =============================================================================
// collect_cb_violations tests
// =============================================================================

#[test]
fn test_collect_cb_violations_empty_project() {
    let temp_dir = TempDir::new().unwrap();
    let (issues, critical, warning) =
        crate::cli::handlers::comply_handlers::collect_cb_violations(temp_dir.path(), false, false);

    // Empty project should have no violations
    assert!(issues.is_empty());
    assert_eq!(critical, 0);
    assert_eq!(warning, 0);
}

// =============================================================================
// build_cb_result tests
// =============================================================================

#[test]
fn test_build_cb_result_no_issues() {
    let result = crate::cli::handlers::comply_handlers::build_cb_result(vec![], 0, 0);
    assert_eq!(result.status, CheckStatus::Pass);
    assert!(result.message.contains("no violations"));
}

#[test]
fn test_build_cb_result_warnings_only() {
    let issues = vec!["Warning 1".to_string(), "Warning 2".to_string()];
    let result = crate::cli::handlers::comply_handlers::build_cb_result(issues, 0, 2);
    assert_eq!(result.status, CheckStatus::Warn);
    assert!(result.message.contains("2 warnings"));
}

#[test]
fn test_build_cb_result_critical() {
    let issues = vec!["Critical 1".to_string(), "Warning 1".to_string()];
    let result = crate::cli::handlers::comply_handlers::build_cb_result(issues, 1, 1);
    assert_eq!(result.status, CheckStatus::Fail);
    assert_eq!(result.severity, Severity::Critical);
}