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
409
410
411
412
#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod cli_handlers_integration_tests {
    use crate::cli::handlers::*;
    use crate::stateless_server::StatelessTemplateServer;
    use crate::cli::{OutputFormat, ContextFormat};
    use std::sync::Arc;
    use tempfile::TempDir;
    use serde_json::json;
    use std::path::PathBuf;

    fn create_test_server() -> Arc<StatelessTemplateServer> {
        Arc::new(StatelessTemplateServer::new().unwrap())
    }

    fn create_test_project() -> TempDir {
        let test_dir = TempDir::new().unwrap();
        let src_dir = test_dir.path().join("src");
        std::fs::create_dir(&src_dir).unwrap();
        
        // Create main.rs
        std::fs::write(
            src_dir.join("main.rs"),
            r#"
            mod utils;
            use utils::helper;
            
            fn main() {
                println!("Hello, world!");
                let result = helper();
                complex_function(result);
            }
            
            fn complex_function(x: i32) -> i32 {
                match x {
                    0 => 0,
                    1..=10 => {
                        if x % 2 == 0 {
                            x * 2
                        } else {
                            x + 1
                        }
                    },
                    11..=100 => {
                        for i in 0..x {
                            if i % 3 == 0 {
                                println!("Fizz: {}", i);
                            }
                        }
                        x / 2
                    },
                    _ => -1
                }
            }
            "#
        ).unwrap();
        
        // Create utils.rs
        std::fs::write(
            src_dir.join("utils.rs"),
            r#"
            pub fn helper() -> i32 {
                42
            }
            
            pub fn duplicate_logic(x: i32) -> i32 {
                if x > 0 {
                    x * 2
                } else {
                    0
                }
            }
            
            pub fn similar_logic(x: i32) -> i32 {
                if x > 0 {
                    x * 2
                } else {
                    0
                }
            }
            "#
        ).unwrap();
        
        // Create Cargo.toml
        std::fs::write(
            test_dir.path().join("Cargo.toml"),
            r#"
            [package]
            name = "test-project"
            version = "0.1.0"
            edition = "2021"
            "#
        ).unwrap();
        
        // Create README.md
        std::fs::write(
            test_dir.path().join("README.md"),
            "# Test Project\nThis is a test project for CLI handlers."
        ).unwrap();
        
        test_dir
    }

    #[tokio::test]
    async fn test_handle_generate() {
        let server = create_test_server();
        let test_dir = TempDir::new().unwrap();
        let output_path = test_dir.path().join("test.gitignore");

        let result = handle_generate(
            server,
            "gitignore".to_string(),
            "rust/cli".to_string(),
            vec![("project_name".to_string(), json!("test-project"))],
            Some(output_path.clone()),
            true,
        ).await;

        assert!(result.is_ok(), "handle_generate failed: {:?}", result);
        assert!(output_path.exists(), "Output file was not created");
        
        let content = std::fs::read_to_string(&output_path).unwrap();
        assert!(content.contains("target/") || content.contains("Cargo.lock"));
    }

    #[tokio::test]
    async fn test_handle_scaffold() {
        let server = create_test_server();

        let result = handle_scaffold(
            server,
            "rust".to_string(),
            vec!["gitignore".to_string()],
            vec![("project_name".to_string(), json!("test-scaffold"))],
            1,
        ).await;

        assert!(result.is_ok(), "handle_scaffold failed: {:?}", result);
    }

    #[tokio::test]
    async fn test_handle_scaffold_parallel() {
        let server = create_test_server();

        let result = handle_scaffold(
            server,
            "rust".to_string(),
            vec!["gitignore".to_string(), "makefile".to_string()],
            vec![("project_name".to_string(), json!("test-scaffold"))],
            2, // parallel = 2
        ).await;

        assert!(result.is_ok(), "handle_scaffold with parallel failed: {:?}", result);
    }

    #[tokio::test]
    async fn test_handle_list() {
        let server = create_test_server();

        let result = handle_list(server, None, None, OutputFormat::Table).await;
        assert!(result.is_ok(), "handle_list failed: {:?}", result);
    }

    #[tokio::test]
    async fn test_handle_list_with_toolchain() {
        let server = create_test_server();

        let result = handle_list(
            server, 
            Some("rust".to_string()), 
            None, 
            OutputFormat::Json
        ).await;
        assert!(result.is_ok(), "handle_list with toolchain failed: {:?}", result);
    }

    #[tokio::test]
    async fn test_handle_list_with_category() {
        let server = create_test_server();

        let result = handle_list(
            server, 
            None, 
            Some("gitignore".to_string()), 
            OutputFormat::Yaml
        ).await;
        assert!(result.is_ok(), "handle_list with category failed: {:?}", result);
    }

    #[tokio::test]
    async fn test_handle_search() {
        let server = create_test_server();

        let result = handle_search(
            server,
            "rust".to_string(),
            None,
            10,
        ).await;

        assert!(result.is_ok(), "handle_search failed: {:?}", result);
    }

    #[tokio::test]
    async fn test_handle_search_with_toolchain() {
        let server = create_test_server();

        let result = handle_search(
            server,
            "cli".to_string(),
            Some("rust".to_string()),
            5,
        ).await;

        assert!(result.is_ok(), "handle_search with toolchain failed: {:?}", result);
    }

    #[tokio::test]
    async fn test_handle_validate() {
        let server = create_test_server();

        let result = handle_validate(
            server,
            "rust/cli/gitignore".to_string(),
            vec![("project_name".to_string(), json!("test"))],
        ).await;

        assert!(result.is_ok(), "handle_validate failed: {:?}", result);
    }

    #[tokio::test]
    async fn test_handle_validate_invalid_uri() {
        let server = create_test_server();

        let result = handle_validate(
            server,
            "invalid/template/uri".to_string(),
            vec![],
        ).await;

        // Should handle invalid URI gracefully
        assert!(result.is_err(), "Expected error for invalid URI");
    }

    #[tokio::test]
    async fn test_handle_context() {
        let test_dir = create_test_project();

        let result = handle_context(
            None,
            test_dir.path().to_path_buf(),
            None,
            ContextFormat::Markdown,
        ).await;

        assert!(result.is_ok(), "handle_context failed: {:?}", result);
    }

    #[tokio::test]
    async fn test_handle_context_with_output() {
        let test_dir = create_test_project();
        let output_dir = TempDir::new().unwrap();
        let output_path = output_dir.path().join("context.md");

        let result = handle_context(
            None,
            test_dir.path().to_path_buf(),
            Some(output_path.clone()),
            ContextFormat::Markdown,
        ).await;

        assert!(result.is_ok(), "handle_context with output failed: {:?}", result);
        assert!(output_path.exists(), "Context output file was not created");
    }

    #[tokio::test]
    async fn test_handle_context_json_format() {
        let test_dir = create_test_project();

        let result = handle_context(
            None,
            test_dir.path().to_path_buf(),
            None,
            ContextFormat::Json,
        ).await;

        assert!(result.is_ok(), "handle_context JSON format failed: {:?}", result);
    }

    #[tokio::test]
    async fn test_handle_context_with_toolchain() {
        let test_dir = create_test_project();

        let result = handle_context(
            Some("rust".to_string()),
            test_dir.path().to_path_buf(),
            None,
            ContextFormat::Markdown,
        ).await;

        assert!(result.is_ok(), "handle_context with toolchain failed: {:?}", result);
    }

    #[tokio::test]
    async fn test_error_handling_nonexistent_path() {
        let result = handle_context(
            None,
            PathBuf::from("/non/existent/path"),
            None,
            ContextFormat::Markdown,
        ).await;

        assert!(result.is_err(), "Expected error for non-existent path");
    }

    #[tokio::test]
    async fn test_generate_to_stdout() {
        let server = create_test_server();

        let result = handle_generate(
            server,
            "gitignore".to_string(),
            "rust/cli".to_string(),
            vec![("project_name".to_string(), json!("test-project"))],
            None, // output to stdout
            false,
        ).await;

        assert!(result.is_ok(), "handle_generate to stdout failed: {:?}", result);
    }

    #[tokio::test]
    async fn test_generate_with_create_dirs() {
        let server = create_test_server();
        let test_dir = TempDir::new().unwrap();
        let nested_path = test_dir.path().join("deep").join("nested").join("test.gitignore");

        let result = handle_generate(
            server,
            "gitignore".to_string(),
            "rust/cli".to_string(),
            vec![("project_name".to_string(), json!("test-project"))],
            Some(nested_path.clone()),
            true, // create_dirs = true
        ).await;

        assert!(result.is_ok(), "handle_generate with create_dirs failed: {:?}", result);
        assert!(nested_path.exists(), "Nested output file was not created");
    }

    #[tokio::test]
    async fn test_generate_without_create_dirs() {
        let server = create_test_server();
        let test_dir = TempDir::new().unwrap();
        let nested_path = test_dir.path().join("deep").join("nested").join("test.gitignore");

        let result = handle_generate(
            server,
            "gitignore".to_string(),
            "rust/cli".to_string(),
            vec![("project_name".to_string(), json!("test-project"))],
            Some(nested_path.clone()),
            false, // create_dirs = false
        ).await;

        // Should fail because parent directory doesn't exist
        assert!(result.is_err(), "Expected error when parent directory doesn't exist");
    }

    #[tokio::test]
    async fn test_concurrent_handler_operations() {
        use tokio::task;
        
        let server = create_test_server();
        let test_dir = create_test_project();
        
        let handles: Vec<_> = (0..3).map(|i| {
            let server_clone = Arc::clone(&server);
            let path = test_dir.path().to_path_buf();
            
            task::spawn(async move {
                match i % 3 {
                    0 => handle_list(server_clone, None, None, OutputFormat::Json).await,
                    1 => handle_search(server_clone, "rust".to_string(), None, 5).await,
                    _ => handle_context(None, path, None, ContextFormat::Json).await,
                }
            })
        }).collect();

        for handle in handles {
            let result = handle.await.unwrap();
            assert!(result.is_ok(), "Concurrent handler operation failed");
        }
    }

    #[tokio::test]
    async fn test_error_propagation() {
        let server = create_test_server();

        // Test with invalid template that should cause an error
        let result = handle_generate(
            server,
            "invalid_category".to_string(),
            "invalid/template".to_string(),
            vec![],
            None,
            false,
        ).await;

        assert!(result.is_err(), "Expected error for invalid template");
    }
}