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
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
441
442
443
444
445
446
447
448
449
450
#![cfg(feature = "red-phase-tests")]
// RED Phase: Write failing tests first

// NOTE (Sprint 47): Use assetsearch (../../assetsearch) for MCP-based semantic search.
// All tests in this file marked #[ignore] pending migration to assetsearch.

// PMAT-SEARCH-001: AST-Aware Code Chunker
// Test count: 20 tests (5 per language + 5 edge cases)

use pmat::services::semantic::chunker::*;

// ============================================================================
// Rust Language Tests (4 tests)
// ============================================================================

#[test]
fn test_chunk_rust_functions() {
    let source = r#"
        /// Calculate sum
        fn add(a: i32, b: i32) -> i32 { a + b }

        /// Calculate product
        fn multiply(a: i32, b: i32) -> i32 { a * b }
    "#;

    let chunks = chunk_code(source, Language::Rust).unwrap();
    assert_eq!(chunks.len(), 2);
    assert_eq!(chunks[0].chunk_type, ChunkType::Function);
    assert_eq!(chunks[0].chunk_name, "add");
    assert!(chunks[0].content.contains("Calculate sum"));
    assert_eq!(chunks[1].chunk_type, ChunkType::Function);
    assert_eq!(chunks[1].chunk_name, "multiply");
}

#[test]
fn test_chunk_rust_impl_blocks() {
    let source = r#"
        struct Calculator;

        impl Calculator {
            fn add(&self, a: i32, b: i32) -> i32 { a + b }
            fn sub(&self, a: i32, b: i32) -> i32 { a - b }
        }
    "#;

    let chunks = chunk_code(source, Language::Rust).unwrap();
    assert!(!chunks.is_empty());
    // Should extract impl block or individual methods
    let has_impl_or_methods = chunks
        .iter()
        .any(|c| c.chunk_type == ChunkType::Class || c.chunk_type == ChunkType::Function);
    assert!(has_impl_or_methods);
}

#[test]
fn test_chunk_rust_modules() {
    let source = r#"
        mod calculator {
            pub fn add(a: i32, b: i32) -> i32 { a + b }
        }
    "#;

    let chunks = chunk_code(source, Language::Rust).unwrap();
    assert!(!chunks.is_empty());
    // Should extract module or functions within
    let has_content = chunks
        .iter()
        .any(|c| c.chunk_type == ChunkType::Module || c.chunk_type == ChunkType::Function);
    assert!(has_content);
}

#[test]
fn test_chunk_rust_with_docstrings() {
    let source = r#"
        /// This function adds two numbers
        /// # Examples
        /// ```
        /// let result = add(2, 3);
        /// assert_eq!(result, 5);
        /// ```
        fn add(a: i32, b: i32) -> i32 { a + b }
    "#;

    let chunks = chunk_code(source, Language::Rust).unwrap();
    assert_eq!(chunks.len(), 1);
    assert!(chunks[0].content.contains("This function adds two numbers"));
    assert!(chunks[0].content.contains("# Examples"));
}

// ============================================================================
// TypeScript Language Tests (4 tests)
// ============================================================================

#[test]
fn test_chunk_typescript_class() {
    let source = r#"
        class Calculator {
            add(a: number, b: number): number { return a + b; }
            multiply(a: number, b: number): number { return a * b; }
        }
    "#;

    let chunks = chunk_code(source, Language::TypeScript).unwrap();
    assert_eq!(chunks.len(), 1);
    assert_eq!(chunks[0].chunk_type, ChunkType::Class);
    assert_eq!(chunks[0].chunk_name, "Calculator");
}

#[test]
fn test_chunk_typescript_functions() {
    let source = r#"
        function add(a: number, b: number): number {
            return a + b;
        }

        const multiply = (a: number, b: number): number => a * b;
    "#;

    let chunks = chunk_code(source, Language::TypeScript).unwrap();
    assert_eq!(chunks.len(), 2);
    assert!(chunks.iter().any(|c| c.chunk_name == "add"));
    assert!(chunks.iter().any(|c| c.chunk_name == "multiply"));
}

#[test]
fn test_chunk_typescript_interface() {
    let source = r#"
        interface Calculator {
            add(a: number, b: number): number;
            multiply(a: number, b: number): number;
        }
    "#;

    let chunks = chunk_code(source, Language::TypeScript).unwrap();
    assert_eq!(chunks.len(), 1);
    assert_eq!(chunks[0].chunk_type, ChunkType::Class); // Interfaces treated as class-like
    assert_eq!(chunks[0].chunk_name, "Calculator");
}

#[test]
fn test_chunk_typescript_with_jsdoc() {
    let source = r#"
        /**
         * Adds two numbers together
         * @param a First number
         * @param b Second number
         * @returns Sum of a and b
         */
        function add(a: number, b: number): number {
            return a + b;
        }
    "#;

    let chunks = chunk_code(source, Language::TypeScript).unwrap();
    assert_eq!(chunks.len(), 1);
    assert!(chunks[0].content.contains("Adds two numbers together"));
    assert!(chunks[0].content.contains("@param"));
}

// ============================================================================
// Python Language Tests (4 tests)
// ============================================================================

#[test]
fn test_chunk_python_functions() {
    let source = r#"
def add(a, b):
    """Calculate sum"""
    return a + b

def multiply(a, b):
    """Calculate product"""
    return a * b
    "#;

    let chunks = chunk_code(source, Language::Python).unwrap();
    assert_eq!(chunks.len(), 2);
    assert_eq!(chunks[0].chunk_type, ChunkType::Function);
    assert_eq!(chunks[0].chunk_name, "add");
    assert!(chunks[0].content.contains("Calculate sum"));
}

#[test]
fn test_chunk_python_class() {
    let source = r#"
class Calculator:
    """A simple calculator class"""

    def add(self, a, b):
        return a + b

    def multiply(self, a, b):
        return a * b
    "#;

    let chunks = chunk_code(source, Language::Python).unwrap();
    assert_eq!(chunks.len(), 1);
    assert_eq!(chunks[0].chunk_type, ChunkType::Class);
    assert_eq!(chunks[0].chunk_name, "Calculator");
    assert!(chunks[0].content.contains("simple calculator"));
}

#[test]
fn test_chunk_python_with_decorators() {
    let source = r#"
@property
def value(self):
    """Get the value"""
    return self._value

@staticmethod
def create():
    """Factory method"""
    return Calculator()
    "#;

    let chunks = chunk_code(source, Language::Python).unwrap();
    assert_eq!(chunks.len(), 2);
    assert!(chunks.iter().any(|c| c.chunk_name == "value"));
    assert!(chunks.iter().any(|c| c.chunk_name == "create"));
}

#[test]
fn test_chunk_python_nested_functions() {
    let source = r#"
def outer(x):
    """Outer function"""
    def inner(y):
        """Inner function"""
        return x + y
    return inner
    "#;

    let chunks = chunk_code(source, Language::Python).unwrap();
    // Should extract outer function (may or may not extract inner)
    assert!(!chunks.is_empty());
    assert!(chunks.iter().any(|c| c.chunk_name == "outer"));
}

// ============================================================================
// C/C++ Language Tests (4 tests)
// ============================================================================

#[test]
fn test_chunk_c_functions() {
    let source = r#"
        // Calculate sum
        int add(int a, int b) {
            return a + b;
        }

        // Calculate product
        int multiply(int a, int b) {
            return a * b;
        }
    "#;

    let chunks = chunk_code(source, Language::C).unwrap();
    assert_eq!(chunks.len(), 2);
    assert_eq!(chunks[0].chunk_type, ChunkType::Function);
    assert_eq!(chunks[0].chunk_name, "add");
}

#[test]
fn test_chunk_cpp_class() {
    let source = r#"
        class Calculator {
        public:
            int add(int a, int b) { return a + b; }
            int multiply(int a, int b) { return a * b; }
        };
    "#;

    let chunks = chunk_code(source, Language::Cpp).unwrap();
    assert_eq!(chunks.len(), 1);
    assert_eq!(chunks[0].chunk_type, ChunkType::Class);
    assert_eq!(chunks[0].chunk_name, "Calculator");
}

#[test]
fn test_chunk_cpp_template() {
    let source = r#"
        template<typename T>
        T add(T a, T b) {
            return a + b;
        }
    "#;

    let chunks = chunk_code(source, Language::Cpp).unwrap();
    assert_eq!(chunks.len(), 1);
    assert_eq!(chunks[0].chunk_name, "add");
}

#[test]
fn test_chunk_c_with_comments() {
    let source = r#"
        /**
         * Adds two integers
         * @param a First integer
         * @param b Second integer
         * @return Sum of a and b
         */
        int add(int a, int b) {
            return a + b;
        }
    "#;

    let chunks = chunk_code(source, Language::C).unwrap();
    assert_eq!(chunks.len(), 1);
    assert!(chunks[0].content.contains("Adds two integers"));
}

// ============================================================================
// Go Language Tests (4 tests)
// ============================================================================

#[test]
fn test_chunk_go_functions() {
    let source = r#"
        // Add calculates sum
        func Add(a, b int) int {
            return a + b
        }

        // Multiply calculates product
        func Multiply(a, b int) int {
            return a * b
        }
    "#;

    let chunks = chunk_code(source, Language::Go).unwrap();
    assert_eq!(chunks.len(), 2);
    assert_eq!(chunks[0].chunk_type, ChunkType::Function);
    assert_eq!(chunks[0].chunk_name, "Add");
    assert!(chunks[0].content.contains("calculates sum"));
}

#[test]
fn test_chunk_go_struct() {
    let source = r#"
        type Calculator struct {
            value int
        }

        func (c *Calculator) Add(a, b int) int {
            return a + b
        }
    "#;

    let chunks = chunk_code(source, Language::Go).unwrap();
    // Should extract struct and/or methods
    assert!(!chunks.is_empty());
    let has_struct_or_method = chunks
        .iter()
        .any(|c| c.chunk_name == "Calculator" || c.chunk_name == "Add");
    assert!(has_struct_or_method);
}

#[test]
fn test_chunk_go_interface() {
    let source = r#"
        // Calculator interface for arithmetic operations
        type Calculator interface {
            Add(a, b int) int
            Multiply(a, b int) int
        }
    "#;

    let chunks = chunk_code(source, Language::Go).unwrap();
    assert_eq!(chunks.len(), 1);
    assert_eq!(chunks[0].chunk_name, "Calculator");
}

#[test]
fn test_chunk_go_package() {
    let source = r#"
        package calculator

        // Add calculates sum
        func Add(a, b int) int {
            return a + b
        }
    "#;

    let chunks = chunk_code(source, Language::Go).unwrap();
    assert!(!chunks.is_empty());
    assert!(chunks.iter().any(|c| c.chunk_name == "Add"));
}

// ============================================================================
// Edge Case Tests (5 tests)
// ============================================================================

#[test]
fn test_chunk_empty_file() {
    let source = "";
    let chunks = chunk_code(source, Language::Rust).unwrap();
    assert_eq!(chunks.len(), 0);
}

#[test]
fn test_chunk_whitespace_only() {
    let source = "   \n\n   \t\t   \n   ";
    let chunks = chunk_code(source, Language::Rust).unwrap();
    assert_eq!(chunks.len(), 0);
}

#[test]
fn test_chunk_comments_only() {
    let source = r#"
        // This is a comment
        /* This is a block comment */
        /// This is a doc comment
    "#;
    let chunks = chunk_code(source, Language::Rust).unwrap();
    assert_eq!(chunks.len(), 0);
}

#[test]
fn test_chunk_checksum_deterministic() {
    let source = "fn foo() { }";
    let chunks1 = chunk_code(source, Language::Rust).unwrap();
    let chunks2 = chunk_code(source, Language::Rust).unwrap();

    assert_eq!(chunks1.len(), chunks2.len());
    assert_eq!(chunks1[0].content_checksum, chunks2[0].content_checksum);
}

#[test]
fn test_chunk_metadata_complete() {
    let source = r#"
        /// Test function
        fn test_func() {
            println!("test");
        }
    "#;

    let chunks = chunk_code(source, Language::Rust).unwrap();
    assert_eq!(chunks.len(), 1);

    let chunk = &chunks[0];
    assert_eq!(chunk.chunk_type, ChunkType::Function);
    assert_eq!(chunk.chunk_name, "test_func");
    assert_eq!(chunk.language, "rust");
    assert!(chunk.start_line > 0);
    assert!(chunk.end_line >= chunk.start_line);
    assert!(!chunk.content.is_empty());
    assert!(!chunk.content_checksum.is_empty());
    assert_eq!(chunk.content_checksum.len(), 64); // SHA256 hex length
}