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
//! EXTREME TDD: Multi-Language Deep Context Tests
//!
//! These tests ensure that deep_context.md properly analyzes and displays
//! AST information for ALL supported languages.

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod red_phase_tests {
    use std::fs;
    use std::path::PathBuf;
    use tempfile::TempDir;

    /// RED TEST: Go files MUST be analyzed with AST extraction
    ///
    /// This test will FAIL until Go AST extraction is properly wired into
    /// the context generation pipeline.
    #[tokio::test]
    #[cfg(feature = "go-ast")]
    async fn red_test_go_file_gets_analyzed() {
        // Create temp directory with Go file
        let temp_dir = TempDir::new().unwrap();
        let go_file = temp_dir.path().join("test.go");

        fs::write(
            &go_file,
            r#"
package main

import "fmt"

type Message struct {
    Type  string
    Value int
}

func ProcessMessage(msg Message) string {
    if msg.Type == "ping" {
        return fmt.Sprintf("pong: %d", msg.Value)
    }
    return "unknown"
}

func main() {
    msg := Message{Type: "ping", Value: 42}
    result := ProcessMessage(msg)
    fmt.Println(result)
}
"#,
        )
        .unwrap();

        // Run context generation
        use crate::services::context::analyze_project;
        let context = analyze_project(temp_dir.path(), "stable").await.unwrap();

        // Go file MUST be in the context
        let go_file_found = context.files.iter().any(|f| f.path.contains("test.go"));
        assert!(go_file_found, "Go file must be discovered and analyzed");

        // Find the Go file context
        let go_context = context
            .files
            .iter()
            .find(|f| f.path.contains("test.go"))
            .expect("Must find Go file in context");

        // MUST contain Go function names in the items
        let functions: Vec<String> = go_context
            .items
            .iter()
            .filter_map(|item| {
                if let crate::services::context::AstItem::Function { name, .. } = item {
                    Some(name.clone())
                } else {
                    None
                }
            })
            .collect();

        let has_process_message = functions.iter().any(|f| f.contains("ProcessMessage"));
        let has_main = functions.iter().any(|f| f.contains("main"));

        assert!(
            has_process_message,
            "Go function 'ProcessMessage' must be extracted, found functions: {:?}",
            functions
        );
        assert!(
            has_main,
            "Go function 'main' must be extracted, found functions: {:?}",
            functions
        );

        // MUST NOT be empty
        assert!(
            !functions.is_empty(),
            "Go file must have functions extracted"
        );
    }

    /// RED TEST: TypeScript files MUST be analyzed correctly (not as Rust)
    ///
    /// This test will FAIL until TypeScript is correctly identified and parsed
    /// by its own AST analyzer (not incorrectly parsed as Rust).
    #[tokio::test]
    async fn red_test_typescript_file_analyzed_correctly() {
        // Create temp directory with TypeScript file
        let temp_dir = TempDir::new().unwrap();
        let ts_file = temp_dir.path().join("test.ts");

        fs::write(
            &ts_file,
            r#"
export interface Message {
    type: "ping" | "pong";
    value: number;
}

class MessageProcessor {
    private count: number = 0;

    process(msg: Message): string {
        this.count++;
        if (msg.type === "ping") {
            return `pong: ${msg.value}`;
        }
        return "unknown";
    }

    getCount(): number {
        return this.count;
    }
}

export function createProcessor(): MessageProcessor {
    return new MessageProcessor();
}
"#,
        )
        .unwrap();

        // Run context generation
        use crate::services::context::analyze_project;
        let context = analyze_project(temp_dir.path(), "stable").await.unwrap();

        // TypeScript file MUST be in the context
        let ts_file_found = context.files.iter().any(|f| f.path.contains("test.ts"));
        assert!(
            ts_file_found,
            "TypeScript file must be discovered and analyzed"
        );

        // Find the TypeScript file context
        let ts_context = context
            .files
            .iter()
            .find(|f| f.path.contains("test.ts"))
            .expect("Must find TypeScript file in context");

        // Language MUST be detected as typescript/javascript, NOT rust
        assert_ne!(
            ts_context.language.to_lowercase(),
            "rust",
            "TypeScript file must NOT be detected as Rust, got: {}",
            ts_context.language
        );

        // MUST contain TypeScript function/method names in the items
        let functions: Vec<String> = ts_context
            .items
            .iter()
            .filter_map(|item| {
                if let crate::services::context::AstItem::Function { name, .. } = item {
                    Some(name.clone())
                } else {
                    None
                }
            })
            .collect();

        let has_process = functions
            .iter()
            .any(|f| f.contains("process") || f.contains("MessageProcessor"));
        let has_create_processor = functions.iter().any(|f| f.contains("createProcessor"));

        assert!(
            has_process || has_create_processor || !functions.is_empty(),
            "TypeScript methods/functions must be extracted, found functions: {:?}",
            functions
        );
    }

    /// RED TEST: JavaScript files MUST show function analysis
    #[tokio::test]
    async fn red_test_javascript_file_analyzed() {
        let temp_dir = TempDir::new().unwrap();
        let js_file = temp_dir.path().join("test.js");

        fs::write(
            &js_file,
            r#"
function calculateSum(a, b) {
    if (a < 0 || b < 0) {
        throw new Error("Negative numbers not allowed");
    }
    return a + b;
}

class Calculator {
    constructor() {
        this.history = [];
    }

    add(a, b) {
        const result = calculateSum(a, b);
        this.history.push({op: 'add', a, b, result});
        return result;
    }
}

module.exports = { Calculator, calculateSum };
"#,
        )
        .unwrap();

        use crate::services::context::analyze_project;
        let context = analyze_project(temp_dir.path(), "stable").await.unwrap();

        let js_file_found = context.files.iter().any(|f| f.path.contains("test.js"));
        assert!(js_file_found, "JavaScript file must be discovered");

        let js_context = context
            .files
            .iter()
            .find(|f| f.path.contains("test.js"))
            .expect("Must find JavaScript file in context");

        // Should have some functions extracted
        let function_count = js_context
            .items
            .iter()
            .filter(|item| matches!(item, crate::services::context::AstItem::Function { .. }))
            .count();

        assert!(
            function_count > 0 || js_context.language.to_lowercase().contains("javascript"),
            "JavaScript file must be analyzed, found {} functions with language: {}",
            function_count,
            js_context.language
        );
    }

    /// RED TEST: Multi-language project MUST analyze all languages
    #[tokio::test]
    #[cfg(feature = "go-ast")]
    async fn red_test_multi_language_project_analyzes_all() {
        let temp_dir = TempDir::new().unwrap();

        // Create Rust file
        fs::write(
            temp_dir.path().join("lib.rs"),
            r#"
/// Rust func.
pub fn rust_func(x: i32) -> i32 {
    x * 2
}
"#,
        )
        .unwrap();

        // Create Go file
        fs::write(
            temp_dir.path().join("main.go"),
            r#"
package main

func GoFunc(x int) int {
    return x * 2
}
"#,
        )
        .unwrap();

        // Create TypeScript file
        fs::write(
            temp_dir.path().join("app.ts"),
            r#"
export function tsFunc(x: number): number {
    return x * 2;
}
"#,
        )
        .unwrap();

        use crate::services::context::analyze_project;
        let context = analyze_project(temp_dir.path(), "stable").await.unwrap();

        // ALL files must be discovered
        assert!(
            context.files.iter().any(|f| f.path.contains("lib.rs")),
            "Rust file must be discovered"
        );
        assert!(
            context.files.iter().any(|f| f.path.contains("main.go")),
            "Go file must be discovered"
        );
        assert!(
            context.files.iter().any(|f| f.path.contains("app.ts")),
            "TypeScript file must be discovered"
        );

        // At minimum, files should be present even if analysis is incomplete
        assert!(
            context.files.len() >= 3,
            "Should have at least 3 files, got {}",
            context.files.len()
        );
    }

    /// RED TEST: Language detection must correctly identify file types
    #[test]
    fn red_test_language_detection_correctness() {
        use crate::services::language_registry::LanguageRegistry;

        let registry = LanguageRegistry::new();

        // Test Go detection
        let go_lang = registry.detect_language(&PathBuf::from("test.go"));
        assert_eq!(
            go_lang.name().to_lowercase(),
            "go",
            "Go files must be detected as 'go', got '{}'",
            go_lang.name()
        );

        // Test TypeScript detection
        let ts_lang = registry.detect_language(&PathBuf::from("test.ts"));
        assert_eq!(
            ts_lang.name().to_lowercase(),
            "typescript",
            "TypeScript files must be detected as 'typescript', got '{}'",
            ts_lang.name()
        );

        // Test JavaScript detection
        let js_lang = registry.detect_language(&PathBuf::from("test.js"));
        assert_eq!(
            js_lang.name().to_lowercase(),
            "javascript",
            "JavaScript files must be detected as 'javascript', got '{}'",
            js_lang.name()
        );
    }
}

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod integration_tests {
    use std::fs;
    use tempfile::TempDir;

    /// Integration test: End-to-end Go analysis
    #[tokio::test]
    #[cfg(feature = "go-ast")]
    async fn test_go_analysis_end_to_end() {
        let temp_dir = TempDir::new().unwrap();
        let go_file = temp_dir.path().join("complex.go");

        fs::write(
            &go_file,
            r#"
package main

func ComplexFunc(x, y, z int) int {
    if x > 0 {
        if y > 0 {
            if z > 0 {
                return x + y + z
            }
            return x + y
        }
        return x
    }
    return 0
}
"#,
        )
        .unwrap();

        use crate::services::context::analyze_project;
        let context = analyze_project(temp_dir.path(), "stable").await.unwrap();

        let has_go_file = context.files.iter().any(|f| f.path.contains("complex.go"));
        assert!(has_go_file, "Go file should be in context");
    }

    /// Integration test: End-to-end TypeScript analysis
    #[tokio::test]
    async fn test_typescript_analysis_end_to_end() {
        let temp_dir = TempDir::new().unwrap();
        let ts_file = temp_dir.path().join("complex.ts");

        fs::write(
            &ts_file,
            r#"
class Complex {
    method(x: number, y: number): number {
        if (x > 0) {
            if (y > 0) {
                return x + y;
            }
            return x;
        }
        return 0;
    }
}
"#,
        )
        .unwrap();

        use crate::services::context::analyze_project;
        let context = analyze_project(temp_dir.path(), "stable").await.unwrap();

        let has_ts_file = context.files.iter().any(|f| f.path.contains("complex.ts"));
        assert!(has_ts_file, "TypeScript file should be in context");
    }
}