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
// Provability handler tests - Part 1: Config, prepare_filtered, format tests
// Included from provability_handler.rs mod coverage_tests

fn strip_ansi(s: &str) -> String {
    let re = regex::Regex::new(r"\x1b\[[0-9;]*m").unwrap();
    re.replace_all(s, "").to_string()
}

// ============================================================
// ProvabilityConfig tests
// ============================================================

#[test]
fn test_provability_config_creation() {
    let config = ProvabilityConfig {
        project_path: PathBuf::from("/test/project"),
        functions: vec!["main".to_string(), "test".to_string()],
        analysis_depth: 5,
        format: ProvabilityOutputFormat::Json,
        high_confidence_only: true,
        include_evidence: true,
        output: Some(PathBuf::from("/test/output.json")),
        top_files: 10,
    };

    assert_eq!(config.project_path, PathBuf::from("/test/project"));
    assert_eq!(config.functions.len(), 2);
    assert_eq!(config.analysis_depth, 5);
    assert_eq!(config.format, ProvabilityOutputFormat::Json);
    assert!(config.high_confidence_only);
    assert!(config.include_evidence);
    assert!(config.output.is_some());
    assert_eq!(config.top_files, 10);
}

#[test]
fn test_provability_config_debug_derive() {
    let config = ProvabilityConfig {
        project_path: PathBuf::from("/test"),
        functions: vec![],
        analysis_depth: 3,
        format: ProvabilityOutputFormat::Summary,
        high_confidence_only: false,
        include_evidence: false,
        output: None,
        top_files: 5,
    };

    let debug_output = format!("{:?}", config);
    assert!(debug_output.contains("ProvabilityConfig"));
    assert!(debug_output.contains("/test"));
}

#[test]
fn test_provability_config_clone() {
    let config = ProvabilityConfig {
        project_path: PathBuf::from("/original"),
        functions: vec!["fn1".to_string()],
        analysis_depth: 7,
        format: ProvabilityOutputFormat::Full,
        high_confidence_only: true,
        include_evidence: true,
        output: Some(PathBuf::from("/output.md")),
        top_files: 15,
    };

    let cloned = config.clone();
    assert_eq!(cloned.project_path, config.project_path);
    assert_eq!(cloned.functions, config.functions);
    assert_eq!(cloned.analysis_depth, config.analysis_depth);
    assert_eq!(cloned.format, config.format);
    assert_eq!(cloned.high_confidence_only, config.high_confidence_only);
    assert_eq!(cloned.include_evidence, config.include_evidence);
    assert_eq!(cloned.output, config.output);
    assert_eq!(cloned.top_files, config.top_files);
}

// ============================================================
// prepare_filtered_summaries tests
// ============================================================

fn create_test_summary(score: f64) -> ProofSummary {
    ProofSummary {
        provability_score: score,
        analysis_time_us: 100,
        verified_properties: vec![],
        version: 1,
    }
}

fn create_test_summary_with_properties(
    score: f64,
    props: Vec<VerifiedProperty>,
) -> ProofSummary {
    ProofSummary {
        provability_score: score,
        analysis_time_us: 200,
        verified_properties: props,
        version: 1,
    }
}

#[test]
fn test_prepare_filtered_summaries_no_filter() {
    let summaries = vec![
        create_test_summary(0.9),
        create_test_summary(0.5),
        create_test_summary(0.3),
    ];

    let filtered = prepare_filtered_summaries(&summaries, false);

    assert_eq!(filtered.len(), 3);
    assert_eq!(filtered[0].provability_score, 0.9);
    assert_eq!(filtered[1].provability_score, 0.5);
    assert_eq!(filtered[2].provability_score, 0.3);
}

#[test]
fn test_prepare_filtered_summaries_high_confidence_only() {
    let summaries = vec![
        create_test_summary(0.95), // High confidence - included
        create_test_summary(0.85), // High confidence - included
        create_test_summary(0.79), // Below threshold - excluded
        create_test_summary(0.5),  // Below threshold - excluded
        create_test_summary(0.2),  // Below threshold - excluded
    ];

    let filtered = prepare_filtered_summaries(&summaries, true);

    assert_eq!(filtered.len(), 2);
    assert!(filtered.iter().all(|s| s.provability_score >= 0.8));
}

#[test]
fn test_prepare_filtered_summaries_empty_input() {
    let summaries: Vec<ProofSummary> = vec![];
    let filtered = prepare_filtered_summaries(&summaries, true);
    assert!(filtered.is_empty());
}

#[test]
fn test_prepare_filtered_summaries_all_below_threshold() {
    let summaries = vec![
        create_test_summary(0.5),
        create_test_summary(0.6),
        create_test_summary(0.7),
    ];

    let filtered = prepare_filtered_summaries(&summaries, true);
    assert!(filtered.is_empty());
}

#[test]
fn test_prepare_filtered_summaries_boundary_score() {
    // Test exact 0.8 boundary (should be included since >= 0.8)
    let summaries = vec![create_test_summary(0.8), create_test_summary(0.7999)];

    let filtered = prepare_filtered_summaries(&summaries, true);
    assert_eq!(filtered.len(), 1);
    assert_eq!(filtered[0].provability_score, 0.8);
}

// ============================================================
// format_provability_output tests
// ============================================================

fn create_test_function_id(file: &str, name: &str, line: usize) -> FunctionId {
    FunctionId {
        file_path: file.to_string(),
        function_name: name.to_string(),
        line_number: line,
    }
}

#[test]
fn test_format_provability_output_json() {
    let config = ProvabilityConfig {
        project_path: PathBuf::from("/test"),
        functions: vec![],
        analysis_depth: 3,
        format: ProvabilityOutputFormat::Json,
        high_confidence_only: false,
        include_evidence: true,
        output: None,
        top_files: 5,
    };

    let function_ids = vec![create_test_function_id("src/main.rs", "main", 10)];
    let summaries = vec![create_test_summary(0.85)];

    let result = format_provability_output(&function_ids, &summaries, &config);
    assert!(result.is_ok());

    let content = result.unwrap();
    assert!(content.contains("provability_analysis"));
    assert!(content.contains("main"));
}

#[test]
fn test_format_provability_output_summary() {
    let config = ProvabilityConfig {
        project_path: PathBuf::from("/test"),
        functions: vec![],
        analysis_depth: 3,
        format: ProvabilityOutputFormat::Summary,
        high_confidence_only: false,
        include_evidence: false,
        output: None,
        top_files: 5,
    };

    let function_ids = vec![
        create_test_function_id("src/main.rs", "main", 10),
        create_test_function_id("src/lib.rs", "helper", 20),
    ];
    let summaries = vec![create_test_summary(0.9), create_test_summary(0.6)];

    let result = format_provability_output(&function_ids, &summaries, &config);
    assert!(result.is_ok());

    let content = result.unwrap();
    assert!(content.contains("Provability Analysis Summary"));
    assert!(content.contains("Total functions analyzed:"));
}

#[test]
fn test_format_provability_output_full() {
    let config = ProvabilityConfig {
        project_path: PathBuf::from("/test"),
        functions: vec![],
        analysis_depth: 3,
        format: ProvabilityOutputFormat::Full,
        high_confidence_only: false,
        include_evidence: true,
        output: None,
        top_files: 5,
    };

    let function_ids = vec![create_test_function_id("src/main.rs", "main", 10)];
    let summaries = vec![create_test_summary_with_properties(
        0.85,
        vec![VerifiedProperty {
            property_type: PropertyType::NullSafety,
            confidence: 0.9,
            evidence: "No null references".to_string(),
        }],
    )];

    let result = format_provability_output(&function_ids, &summaries, &config);
    assert!(result.is_ok());

    let content = result.unwrap();
    assert!(content.contains("Detailed Provability Analysis"));
}

#[test]
fn test_format_provability_output_sarif() {
    let config = ProvabilityConfig {
        project_path: PathBuf::from("/test"),
        functions: vec![],
        analysis_depth: 3,
        format: ProvabilityOutputFormat::Sarif,
        high_confidence_only: false,
        include_evidence: false,
        output: None,
        top_files: 5,
    };

    let function_ids = vec![
        create_test_function_id("src/main.rs", "high_score", 10),
        create_test_function_id("src/main.rs", "medium_score", 20),
        create_test_function_id("src/main.rs", "low_score", 30),
    ];
    let summaries = vec![
        create_test_summary(0.9), // High
        create_test_summary(0.6), // Medium
        create_test_summary(0.3), // Low
    ];

    let result = format_provability_output(&function_ids, &summaries, &config);
    assert!(result.is_ok());

    let content = result.unwrap();
    assert!(content.contains("sarif-schema-2.1.0"));
    assert!(content.contains("paiml-provability-analyzer"));
}

#[test]
fn test_format_provability_output_markdown() {
    let config = ProvabilityConfig {
        project_path: PathBuf::from("/test"),
        functions: vec![],
        analysis_depth: 3,
        format: ProvabilityOutputFormat::Markdown,
        high_confidence_only: false,
        include_evidence: true,
        output: None,
        top_files: 5,
    };

    let function_ids = vec![create_test_function_id("src/main.rs", "main", 10)];
    let summaries = vec![create_test_summary(0.85)];

    let result = format_provability_output(&function_ids, &summaries, &config);
    assert!(result.is_ok());

    // Markdown format uses format_provability_detailed
    let content = result.unwrap();
    assert!(content.contains("Detailed Provability Analysis"));
}

// ============================================================
// write_provability_output tests
// ============================================================

#[tokio::test]
async fn test_write_provability_output_to_stdout() {
    let content = "Test output content";

    // When output is None, it prints to stdout
    let result = write_provability_output(content, &None).await;
    assert!(result.is_ok());
}

#[tokio::test]
async fn test_write_provability_output_to_file() {
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().join("output.txt");

    let content = "Test file output content";
    let result = write_provability_output(content, &Some(output_path.clone())).await;

    assert!(result.is_ok());
    assert!(output_path.exists());

    let file_content = std::fs::read_to_string(&output_path).unwrap();
    assert_eq!(file_content, content);
}

#[tokio::test]
async fn test_write_provability_output_to_file_with_json() {
    let temp_dir = TempDir::new().unwrap();
    let output_path = temp_dir.path().join("provability.json");

    let content = r#"{"provability_analysis": {"total_functions": 1}}"#;
    let result = write_provability_output(content, &Some(output_path.clone())).await;

    assert!(result.is_ok());
    assert!(output_path.exists());
}

// ============================================================
// resolve_function_targets tests (via integration patterns)
// ============================================================

#[tokio::test]
async fn test_resolve_function_targets_with_empty_functions() {
    // This tests the branch where config.functions.is_empty() is true
    let temp_dir = TempDir::new().unwrap();
    let config = ProvabilityConfig {
        project_path: temp_dir.path().to_path_buf(),
        functions: vec![], // Empty - will discover functions
        analysis_depth: 3,
        format: ProvabilityOutputFormat::Summary,
        high_confidence_only: false,
        include_evidence: false,
        output: None,
        top_files: 5,
    };

    let result = resolve_function_targets(&config).await;
    // May succeed or fail depending on directory state, but exercises the code path
    // If it succeeds, we get function IDs; if it fails, we handle error
    assert!(result.is_ok() || result.is_err());
}

#[tokio::test]
async fn test_resolve_function_targets_with_specific_functions() {
    // This tests the branch where specific functions are provided
    let temp_dir = TempDir::new().unwrap();
    let config = ProvabilityConfig {
        project_path: temp_dir.path().to_path_buf(),
        functions: vec!["main".to_string(), "src/lib.rs:helper".to_string()],
        analysis_depth: 3,
        format: ProvabilityOutputFormat::Summary,
        high_confidence_only: false,
        include_evidence: false,
        output: None,
        top_files: 5,
    };

    let result = resolve_function_targets(&config).await;
    assert!(result.is_ok());

    let ids = result.unwrap();
    assert_eq!(ids.len(), 2);
    assert_eq!(ids[0].function_name, "main");
    assert_eq!(ids[1].function_name, "helper");
}

// ============================================================
// run_provability_analysis tests
// ============================================================

#[tokio::test]
async fn test_run_provability_analysis_basic() {
    use crate::services::lightweight_provability_analyzer::LightweightProvabilityAnalyzer;

    let analyzer = LightweightProvabilityAnalyzer::new();
    let function_ids = vec![
        create_test_function_id("src/main.rs", "main", 10),
        create_test_function_id("src/lib.rs", "helper", 20),
    ];

    let result = run_provability_analysis(&analyzer, &function_ids).await;
    assert!(result.is_ok());

    let summaries = result.unwrap();
    assert_eq!(summaries.len(), 2);
}

#[tokio::test]
async fn test_run_provability_analysis_empty_input() {
    use crate::services::lightweight_provability_analyzer::LightweightProvabilityAnalyzer;

    let analyzer = LightweightProvabilityAnalyzer::new();
    let function_ids: Vec<FunctionId> = vec![];

    let result = run_provability_analysis(&analyzer, &function_ids).await;
    assert!(result.is_ok());

    let summaries = result.unwrap();
    assert!(summaries.is_empty());
}