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
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
    use super::*;

    // ==================== JavaAnalysisTool Tests ====================

    #[test]
    fn test_java_analysis_tool_metadata() {
        let registry = Arc::new(crate::agents::registry::AgentRegistry::new());
        let tool = JavaAnalysisTool::new(registry);
        let metadata = tool.metadata();

        assert_eq!(metadata.name, "analyze_java");
        assert!(metadata.description.contains("Java"));
        assert!(metadata.description.contains("complexity"));
    }

    #[test]
    fn test_java_analysis_tool_input_schema() {
        let registry = Arc::new(crate::agents::registry::AgentRegistry::new());
        let tool = JavaAnalysisTool::new(registry);
        let metadata = tool.metadata();

        let schema = metadata.input_schema;
        assert!(schema["properties"]["path"].is_object());
        assert!(schema["properties"]["max_depth"].is_object());
        assert!(schema["properties"]["include_metrics"].is_object());
        assert!(schema["properties"]["include_ast"].is_object());
        assert!(schema["required"]
            .as_array()
            .unwrap()
            .contains(&json!("path")));
    }

    // ==================== JavaMutationTool Tests ====================

    #[test]
    fn test_java_mutation_tool_metadata() {
        let registry = Arc::new(crate::agents::registry::AgentRegistry::new());
        let tool = JavaMutationTool::new(registry);
        let metadata = tool.metadata();

        assert_eq!(metadata.name, "mutation_test_java");
        assert!(metadata.description.contains("mutation"));
        assert!(metadata.description.contains("Java"));
    }

    #[test]
    fn test_java_mutation_tool_input_schema() {
        let registry = Arc::new(crate::agents::registry::AgentRegistry::new());
        let tool = JavaMutationTool::new(registry);
        let metadata = tool.metadata();

        let schema = metadata.input_schema;
        assert!(schema["properties"]["project_path"].is_object());
        assert!(schema["properties"]["source_path"].is_object());
        assert!(schema["properties"]["test_command"].is_object());
        assert!(schema["properties"]["mutation_operators"].is_object());
        assert!(schema["properties"]["timeout"].is_object());
    }

    // ==================== find_java_files Tests ====================

    #[test]
    fn test_find_java_files_empty_dir() {
        use tempfile::tempdir;

        let dir = tempdir().unwrap();
        let files = find_java_files(dir.path(), 5).unwrap();

        assert!(files.is_empty());
    }

    #[test]
    fn test_find_java_files_with_java() {
        use std::fs;
        use tempfile::tempdir;

        let dir = tempdir().unwrap();
        let java_file = dir.path().join("Test.java");
        fs::write(&java_file, "public class Test {}").unwrap();

        let files = find_java_files(dir.path(), 5).unwrap();

        assert_eq!(files.len(), 1);
        assert!(files[0].ends_with("Test.java"));
    }

    #[test]
    fn test_find_java_files_max_depth() {
        use std::fs;
        use tempfile::tempdir;

        let dir = tempdir().unwrap();
        let subdir = dir.path().join("deep").join("nested");
        fs::create_dir_all(&subdir).unwrap();

        let java_file = subdir.join("Deep.java");
        fs::write(&java_file, "public class Deep {}").unwrap();

        // With max_depth 1, shouldn't find nested file
        let files_shallow = find_java_files(dir.path(), 1).unwrap();
        assert!(
            files_shallow.is_empty() || !files_shallow.iter().any(|f| f.ends_with("Deep.java"))
        );

        // With max_depth 5, should find it
        let files_deep = find_java_files(dir.path(), 5).unwrap();
        assert!(files_deep.iter().any(|f| f.ends_with("Deep.java")));
    }

    #[test]
    fn test_find_java_files_ignores_other_extensions() {
        use std::fs;
        use tempfile::tempdir;

        let dir = tempdir().unwrap();
        fs::write(dir.path().join("test.rs"), "fn main() {}").unwrap();
        fs::write(dir.path().join("test.scala"), "object Test {}").unwrap();
        fs::write(dir.path().join("test.kt"), "class Test").unwrap();

        let files = find_java_files(dir.path(), 5).unwrap();
        assert!(files.is_empty());
    }

    #[test]
    fn test_find_java_files_zero_depth() {
        use std::fs;
        use tempfile::tempdir;

        let dir = tempdir().unwrap();
        let subdir = dir.path().join("sub");
        fs::create_dir(&subdir).unwrap();
        fs::write(subdir.join("Test.java"), "class Test {}").unwrap();

        // With depth 0, should only include the starting path itself
        let files = find_java_files(dir.path(), 0).unwrap();
        // Files in subdirectories shouldn't be found
        assert!(files.is_empty());
    }

    #[test]
    fn test_find_java_files_multiple() {
        use std::fs;
        use tempfile::tempdir;

        let dir = tempdir().unwrap();
        fs::write(dir.path().join("A.java"), "class A {}").unwrap();
        fs::write(dir.path().join("B.java"), "class B {}").unwrap();
        fs::write(dir.path().join("C.java"), "class C {}").unwrap();

        let files = find_java_files(dir.path(), 5).unwrap();
        assert_eq!(files.len(), 3);
    }

    // ==================== Tool Execute Tests (Error Cases) ====================

    #[tokio::test]
    async fn test_java_analysis_tool_missing_path() {
        let registry = Arc::new(crate::agents::registry::AgentRegistry::new());
        let tool = JavaAnalysisTool::new(registry);

        let params = json!({});
        let result = tool.execute(params).await;

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.message.contains("path"));
    }

    #[tokio::test]
    async fn test_java_analysis_tool_nonexistent_path() {
        let registry = Arc::new(crate::agents::registry::AgentRegistry::new());
        let tool = JavaAnalysisTool::new(registry);

        let params = json!({
            "path": "/nonexistent/path/to/java"
        });
        let result = tool.execute(params).await;

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.message.contains("does not exist") || err.message.contains("Path"));
    }

    #[tokio::test]
    async fn test_java_analysis_tool_wrong_extension() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        let mut file = NamedTempFile::with_suffix(".rs").unwrap();
        writeln!(file, "fn main() {{}}").unwrap();

        let registry = Arc::new(crate::agents::registry::AgentRegistry::new());
        let tool = JavaAnalysisTool::new(registry);

        let params = json!({
            "path": file.path().to_str().unwrap()
        });
        let result = tool.execute(params).await;

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.message.contains("Java") || err.message.contains(".java"));
    }

    #[tokio::test]
    async fn test_java_mutation_tool_missing_project_path() {
        let registry = Arc::new(crate::agents::registry::AgentRegistry::new());
        let tool = JavaMutationTool::new(registry);

        let params = json!({
            "source_path": "/path/to/source"
        });
        let result = tool.execute(params).await;

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.message.contains("project_path"));
    }

    #[tokio::test]
    async fn test_java_mutation_tool_missing_source_path() {
        let registry = Arc::new(crate::agents::registry::AgentRegistry::new());
        let tool = JavaMutationTool::new(registry);

        let params = json!({
            "project_path": "/path/to/project"
        });
        let result = tool.execute(params).await;

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.message.contains("source_path"));
    }

    #[tokio::test]
    async fn test_java_mutation_tool_complete_params() {
        let registry = Arc::new(crate::agents::registry::AgentRegistry::new());
        let tool = JavaMutationTool::new(registry);

        let params = json!({
            "project_path": "/path/to/project",
            "source_path": "/path/to/source"
        });
        let result = tool.execute(params).await;

        // Should succeed with placeholder response
        assert!(result.is_ok());
        let value = result.unwrap();
        assert_eq!(value["status"], "completed");
    }

    #[tokio::test]
    async fn test_java_mutation_tool_custom_params() {
        let registry = Arc::new(crate::agents::registry::AgentRegistry::new());
        let tool = JavaMutationTool::new(registry);

        let params = json!({
            "project_path": "/path/to/project",
            "source_path": "/path/to/source",
            "test_command": "mvn clean test",
            "timeout": 60,
            "mutation_operators": ["arithmetic", "conditional"]
        });
        let result = tool.execute(params).await;

        assert!(result.is_ok());
        let value = result.unwrap();
        assert_eq!(value["test_command"], "mvn clean test");
        assert_eq!(value["timeout"], 60);
        assert_eq!(
            value["mutation_operators"],
            json!(["arithmetic", "conditional"])
        );
    }

    #[tokio::test]
    async fn test_java_mutation_tool_default_operators() {
        let registry = Arc::new(crate::agents::registry::AgentRegistry::new());
        let tool = JavaMutationTool::new(registry);

        let params = json!({
            "project_path": "/path/to/project",
            "source_path": "/path/to/source"
        });
        let result = tool.execute(params).await;

        assert!(result.is_ok());
        let value = result.unwrap();
        let operators = value["mutation_operators"].as_array().unwrap();
        assert!(operators.contains(&json!("arithmetic")));
        assert!(operators.contains(&json!("conditional")));
        assert!(operators.contains(&json!("method")));
        assert!(operators.contains(&json!("assignment")));
    }

    // ==================== Integration-like Tests ====================

    #[tokio::test]
    async fn test_analyze_java_file_valid() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        let mut file = NamedTempFile::with_suffix(".java").unwrap();
        writeln!(
            file,
            r#"
            public class Test {{
                public String hello() {{
                    return "Hello";
                }}
            }}
            "#
        )
        .unwrap();

        let result = analyze_java_file(file.path(), true, false).await;
        assert!(result.is_ok());

        let value = result.unwrap();
        assert!(value["status"] == "completed" || value["status"] == "error");
    }

    #[tokio::test]
    async fn test_analyze_java_directory_empty() {
        use tempfile::tempdir;

        let dir = tempdir().unwrap();
        let result = analyze_java_directory(dir.path(), 3, true, false).await;

        assert!(result.is_ok());
        let value = result.unwrap();
        assert_eq!(value["status"], "completed");
        assert_eq!(value["summary"]["file_count"], 0);
    }

    #[tokio::test]
    async fn test_analyze_java_directory_with_file() {
        use std::fs;
        use tempfile::tempdir;

        let dir = tempdir().unwrap();
        fs::write(
            dir.path().join("Test.java"),
            "public class Test { public void run() {} }",
        )
        .unwrap();

        let result = analyze_java_directory(dir.path(), 3, true, false).await;

        assert!(result.is_ok());
        let value = result.unwrap();
        assert_eq!(value["status"], "completed");
        assert_eq!(value["summary"]["file_count"], 1);
    }

    #[tokio::test]
    async fn test_analyze_java_file_with_metrics() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        let mut file = NamedTempFile::with_suffix(".java").unwrap();
        writeln!(
            file,
            r#"
            public class Test {{
                public int calculate(int x, int y) {{
                    if (x > y) {{
                        return x;
                    }} else {{
                        return y;
                    }}
                }}
            }}
            "#
        )
        .unwrap();

        let result = analyze_java_file(file.path(), true, false).await;
        assert!(result.is_ok());

        let value = result.unwrap();
        // Should have metrics if the analysis succeeded
        if value["status"] == "completed" {
            assert!(value.get("metrics").is_some());
        }
    }

    #[tokio::test]
    async fn test_analyze_java_file_without_metrics() {
        use std::io::Write;
        use tempfile::NamedTempFile;

        let mut file = NamedTempFile::with_suffix(".java").unwrap();
        writeln!(file, "public class Test {{}}").unwrap();

        let result = analyze_java_file(file.path(), false, false).await;
        assert!(result.is_ok());

        let value = result.unwrap();
        // Should not have metrics when include_metrics is false
        assert!(value.get("metrics").is_none());
    }

    // ==================== Edge Cases ====================

    #[tokio::test]
    async fn test_analyze_java_directory_with_nested_files() {
        use std::fs;
        use tempfile::tempdir;

        let dir = tempdir().unwrap();
        let subdir = dir.path().join("src").join("main").join("java");
        fs::create_dir_all(&subdir).unwrap();

        fs::write(subdir.join("Main.java"), "public class Main {}").unwrap();
        fs::write(subdir.join("Helper.java"), "public class Helper {}").unwrap();

        let result = analyze_java_directory(dir.path(), 10, true, false).await;
        assert!(result.is_ok());

        let value = result.unwrap();
        assert_eq!(value["status"], "completed");
        assert_eq!(value["summary"]["file_count"], 2);
    }

    #[test]
    fn test_tool_creation() {
        let registry = Arc::new(crate::agents::registry::AgentRegistry::new());

        let analysis_tool = JavaAnalysisTool::new(Arc::clone(&registry));
        let mutation_tool = JavaMutationTool::new(Arc::clone(&registry));

        // Just verify they can be created without panicking
        assert_eq!(analysis_tool.metadata().name, "analyze_java");
        assert_eq!(mutation_tool.metadata().name, "mutation_test_java");
    }
}