pmat 3.16.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
// Tests for Python tree-sitter mutation operators
// include!()'d from python_tree_sitter_mutations.rs

#[cfg_attr(coverage_nightly, coverage(off))]
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_python_arithmetic_mutation() {
        let source = b"result = a + b";
        let operator = PythonBinaryOpMutation;

        // Parse with tree-sitter-python
        let mut parser = tree_sitter::Parser::new();
        parser
            .set_language(&tree_sitter_python::LANGUAGE.into())
            .expect("Failed to set Python language");

        let tree = parser.parse(source, None).expect("Failed to parse");
        let root = tree.root_node();

        // Recursively search for binary_operator node
        fn find_and_test(
            node: &tree_sitter::Node,
            source: &[u8],
            operator: &PythonBinaryOpMutation,
        ) -> bool {
            if operator.can_mutate(node, source) {
                let mutations = operator.mutate(node, source);
                assert!(
                    !mutations.is_empty(),
                    "Should generate mutations for '+' operator"
                );

                // Verify mutations replace + with -, *, /, //, %, **
                let expected_ops = ["-", "*", "/", "//", "%", "**"];
                assert_eq!(mutations.len(), expected_ops.len());

                for (i, mutation) in mutations.iter().enumerate() {
                    assert!(mutation.source.contains(expected_ops[i]));
                    assert!(mutation.description.contains("+ →"));
                }
                return true;
            }

            let mut cursor = node.walk();
            for child in node.children(&mut cursor) {
                if find_and_test(&child, source, operator) {
                    return true;
                }
            }
            false
        }

        assert!(
            find_and_test(&root, source, &operator),
            "Should find binary_operator node"
        );
    }

    #[test]
    fn test_python_relational_mutation() {
        let source = b"return a > b";
        let operator = PythonRelationalOpMutation;

        let mut parser = tree_sitter::Parser::new();
        parser
            .set_language(&tree_sitter_python::LANGUAGE.into())
            .expect("Failed to set Python language");

        let tree = parser.parse(source, None).expect("Failed to parse");
        let root = tree.root_node();

        fn find_and_test(
            node: &tree_sitter::Node,
            source: &[u8],
            operator: &PythonRelationalOpMutation,
        ) -> bool {
            if operator.can_mutate(node, source) {
                let mutations = operator.mutate(node, source);
                assert!(
                    !mutations.is_empty(),
                    "Should generate mutations for '>' operator"
                );

                // Verify mutations replace > with <, >=, <=, ==, !=
                let expected_ops = ["<", ">=", "<=", "==", "!="];
                assert_eq!(mutations.len(), expected_ops.len());
                return true;
            }

            let mut cursor = node.walk();
            for child in node.children(&mut cursor) {
                if find_and_test(&child, source, operator) {
                    return true;
                }
            }
            false
        }

        assert!(
            find_and_test(&root, source, &operator),
            "Should find comparison_operator node"
        );
    }

    #[test]
    fn test_python_logical_mutation() {
        let source = b"return a and b";
        let operator = PythonLogicalOpMutation;

        let mut parser = tree_sitter::Parser::new();
        parser
            .set_language(&tree_sitter_python::LANGUAGE.into())
            .expect("Failed to set Python language");

        let tree = parser.parse(source, None).expect("Failed to parse");
        let root = tree.root_node();

        fn find_and_test(
            node: &tree_sitter::Node,
            source: &[u8],
            operator: &PythonLogicalOpMutation,
        ) -> bool {
            if operator.can_mutate(node, source) {
                let mutations = operator.mutate(node, source);
                assert!(
                    !mutations.is_empty(),
                    "Should generate mutations for 'and' operator"
                );

                // Verify mutations replace 'and' with 'or'
                assert!(mutations.iter().any(|m| m.source.contains("or")));
                assert!(mutations.iter().any(|m| m.description.contains("and →")));
                return true;
            }

            let mut cursor = node.walk();
            for child in node.children(&mut cursor) {
                if find_and_test(&child, source, operator) {
                    return true;
                }
            }
            false
        }

        assert!(
            find_and_test(&root, source, &operator),
            "Should find boolean_operator node"
        );
    }

    #[test]
    fn test_python_identity_mutation() {
        let source = b"return value is None";
        let operator = PythonIdentityOpMutation;

        let mut parser = tree_sitter::Parser::new();
        parser
            .set_language(&tree_sitter_python::LANGUAGE.into())
            .expect("Failed to set Python language");

        let tree = parser.parse(source, None).expect("Failed to parse");
        let root = tree.root_node();

        fn find_and_test(
            node: &tree_sitter::Node,
            source: &[u8],
            operator: &PythonIdentityOpMutation,
        ) -> bool {
            if operator.can_mutate(node, source) {
                let mutations = operator.mutate(node, source);
                assert!(
                    !mutations.is_empty(),
                    "Should generate mutations for 'is' operator"
                );

                // Verify mutations replace 'is' with 'is not' and '=='
                assert!(mutations.iter().any(|m| m.source.contains("is not")));
                assert!(mutations.iter().any(|m| m.source.contains("==")));
                return true;
            }

            let mut cursor = node.walk();
            for child in node.children(&mut cursor) {
                if find_and_test(&child, source, operator) {
                    return true;
                }
            }
            false
        }

        assert!(
            find_and_test(&root, source, &operator),
            "Should find 'is' operator"
        );
    }

    #[test]
    fn test_python_membership_mutation() {
        let source = b"return item in collection";
        let operator = PythonMembershipOpMutation;

        let mut parser = tree_sitter::Parser::new();
        parser
            .set_language(&tree_sitter_python::LANGUAGE.into())
            .expect("Failed to set Python language");

        let tree = parser.parse(source, None).expect("Failed to parse");
        let root = tree.root_node();

        fn find_and_test(
            node: &tree_sitter::Node,
            source: &[u8],
            operator: &PythonMembershipOpMutation,
        ) -> bool {
            if operator.can_mutate(node, source) {
                let mutations = operator.mutate(node, source);
                assert!(
                    !mutations.is_empty(),
                    "Should generate mutations for 'in' operator"
                );

                // Verify mutation replaces 'in' with 'not in'
                assert!(mutations.iter().any(|m| m.source.contains("not in")));
                assert!(mutations.iter().any(|m| m.description.contains("in →")));
                return true;
            }

            let mut cursor = node.walk();
            for child in node.children(&mut cursor) {
                if find_and_test(&child, source, operator) {
                    return true;
                }
            }
            false
        }

        assert!(
            find_and_test(&root, source, &operator),
            "Should find 'in' operator"
        );
    }

    /// Test UTF-8 validity after mutation (validates expect() calls in operator impls)
    #[test]
    fn test_utf8_validity_after_mutation() {
        // Test case 1: Simple ASCII operators (most common)
        let source = b"result = a + b - c * d / e";

        let mut parser = tree_sitter::Parser::new();
        parser
            .set_language(&tree_sitter_python::LANGUAGE.into())
            .expect("Failed to set Python language");

        let tree = parser.parse(source, None).expect("Failed to parse");
        let root = tree.root_node();

        // Find and mutate all operators
        fn collect_mutations(node: &tree_sitter::Node, source: &[u8]) -> Vec<MutatedSource> {
            let mut all_mutations = Vec::new();

            if let "binary_operator" = node.kind() {
                let operator = PythonBinaryOpMutation;
                if operator.can_mutate(node, source) {
                    all_mutations.extend(operator.mutate(node, source));
                }
            }

            let mut cursor = node.walk();
            for child in node.children(&mut cursor) {
                all_mutations.extend(collect_mutations(&child, source));
            }

            all_mutations
        }

        let mutations = collect_mutations(&root, source);

        // Verify all mutations produce valid UTF-8
        for mutation in &mutations {
            // This validates the expect() doesn't panic
            assert!(!mutation.source.is_empty());
            // Verify it's valid UTF-8 by checking it can be parsed again
            assert!(mutation.source.is_ascii() || mutation.source.chars().count() > 0);
        }

        assert!(!mutations.is_empty(), "Should generate mutations");
    }

    /// Test UTF-8 validity with Unicode identifiers (Python 3 supports Unicode)
    #[test]
    fn test_utf8_validity_with_unicode_identifiers() {
        // Python 3 allows Unicode identifiers
        let source = "résultat = α + β".as_bytes();

        let mut parser = tree_sitter::Parser::new();
        parser
            .set_language(&tree_sitter_python::LANGUAGE.into())
            .expect("Failed to set Python language");

        let tree = parser.parse(source, None).expect("Failed to parse");
        let root = tree.root_node();

        fn find_and_mutate(node: &tree_sitter::Node, source: &[u8]) -> Vec<MutatedSource> {
            let operator = PythonBinaryOpMutation;
            if operator.can_mutate(node, source) {
                return operator.mutate(node, source);
            }

            let mut cursor = node.walk();
            for child in node.children(&mut cursor) {
                let mutations = find_and_mutate(&child, source);
                if !mutations.is_empty() {
                    return mutations;
                }
            }
            vec![]
        }

        let mutations = find_and_mutate(&root, source);

        // All mutations should preserve Unicode and remain valid UTF-8
        for mutation in &mutations {
            assert!(mutation.source.contains("résultat"));
            assert!(mutation.source.contains("α"));
            assert!(mutation.source.contains("β"));
            // Verify valid UTF-8 by ensuring chars() doesn't panic
            assert!(mutation.source.chars().count() > 0);
        }

        assert!(
            !mutations.is_empty(),
            "Should generate mutations with Unicode"
        );
    }

    /// Test all mutation operators produce valid UTF-8
    #[test]
    fn test_all_operators_produce_valid_utf8() {
        // Test with various operators
        let test_cases = vec![
            b"a + b".as_slice(),
            b"x == y".as_slice(),
            b"a and b".as_slice(),
            b"x in y".as_slice(),
        ];

        for source in test_cases {
            let mut parser = tree_sitter::Parser::new();
            parser
                .set_language(&tree_sitter_python::LANGUAGE.into())
                .expect("Failed to set Python language");

            let tree = parser.parse(source, None).expect("Failed to parse");
            let root = tree.root_node();

            // Collect all possible mutations
            fn collect_all_mutations(
                node: &tree_sitter::Node,
                source: &[u8],
            ) -> Vec<MutatedSource> {
                let mut mutations = Vec::new();

                // Try all operators
                let operators: Vec<Box<dyn TreeSitterMutationOperator>> = vec![
                    Box::new(PythonBinaryOpMutation),
                    Box::new(PythonRelationalOpMutation),
                    Box::new(PythonLogicalOpMutation),
                    Box::new(PythonMembershipOpMutation),
                ];

                for op in operators {
                    if op.can_mutate(node, source) {
                        mutations.extend(op.mutate(node, source));
                    }
                }

                let mut cursor = node.walk();
                for child in node.children(&mut cursor) {
                    mutations.extend(collect_all_mutations(&child, source));
                }

                mutations
            }

            let mutations = collect_all_mutations(&root, source);

            // Every mutation must be valid UTF-8
            for mutation in &mutations {
                // This is the key test - if expect() were to panic, this would fail
                assert!(!mutation.source.is_empty());
                // Verify UTF-8 validity explicitly
                assert!(
                    std::str::from_utf8(mutation.source.as_bytes()).is_ok(),
                    "Mutation should produce valid UTF-8: {}",
                    mutation.description
                );
            }
        }
    }

    /// Test edge case: Empty operator replacement still produces valid UTF-8
    #[test]
    fn test_edge_cases_utf8() {
        // Test with minimal source
        let source = b"a+b";

        let mut parser = tree_sitter::Parser::new();
        parser
            .set_language(&tree_sitter_python::LANGUAGE.into())
            .expect("Failed to set Python language");

        let tree = parser.parse(source, None).expect("Failed to parse");
        let root = tree.root_node();

        fn find_operator(node: &tree_sitter::Node, source: &[u8]) -> Vec<MutatedSource> {
            let operator = PythonBinaryOpMutation;
            if operator.can_mutate(node, source) {
                return operator.mutate(node, source);
            }

            let mut cursor = node.walk();
            for child in node.children(&mut cursor) {
                let result = find_operator(&child, source);
                if !result.is_empty() {
                    return result;
                }
            }
            vec![]
        }

        let mutations = find_operator(&root, source);

        // All mutations valid UTF-8
        for mutation in &mutations {
            assert!(mutation.source.len() >= 3); // At least "a?b" where ? is operator
            assert!(std::str::from_utf8(mutation.source.as_bytes()).is_ok());
        }

        assert!(!mutations.is_empty());
    }
}