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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
#!/usr/bin/env python3

"""
Property Test Upgrade Script
Sprint 88 Follow-up: Upgrade placeholder tests to meaningful property tests

This script identifies placeholder property tests and provides templates
for upgrading them to meaningful tests based on the module context.
"""

import os
import re
import sys
import argparse
from pathlib import Path
from typing import List, Dict, Tuple, Optional
from dataclasses import dataclass
import json

@dataclass
class TestUpgradeCandidate:
    """Represents a file with placeholder tests that can be upgraded"""
    file_path: Path
    module_type: str
    current_test: str
    suggested_upgrade: str
    priority: int  # 1-5, with 1 being highest priority

class PropertyTestUpgrader:
    """Upgrades placeholder property tests to meaningful ones"""
    
    def __init__(self, base_path: str = "server/src"):
        self.base_path = Path(base_path)
        self.placeholder_pattern = re.compile(r'prop_assert!\(true\)')
        self.module_patterns = self.load_module_patterns()
        
    def load_module_patterns(self) -> Dict[str, Dict]:
        """Load patterns for different module types"""
        return {
            "parser": {
                "keywords": ["parse", "lexer", "tokenize", "syntax"],
                "template": self.get_parser_template(),
                "priority": 1
            },
            "analyzer": {
                "keywords": ["analyze", "detect", "calculate", "measure"],
                "template": self.get_analyzer_template(),
                "priority": 1
            },
            "serializer": {
                "keywords": ["serialize", "deserialize", "encode", "decode", "json", "toml"],
                "template": self.get_serializer_template(),
                "priority": 2
            },
            "validator": {
                "keywords": ["validate", "check", "verify", "ensure"],
                "template": self.get_validator_template(),
                "priority": 2
            },
            "transformer": {
                "keywords": ["transform", "convert", "refactor", "modify"],
                "template": self.get_transformer_template(),
                "priority": 3
            },
            "builder": {
                "keywords": ["build", "create", "construct", "generate"],
                "template": self.get_builder_template(),
                "priority": 3
            },
            "model": {
                "keywords": ["model", "struct", "enum", "type"],
                "template": self.get_model_template(),
                "priority": 4
            },
            "service": {
                "keywords": ["service", "handler", "manager", "controller"],
                "template": self.get_service_template(),
                "priority": 4
            },
            "utility": {
                "keywords": ["util", "helper", "common", "shared"],
                "template": self.get_utility_template(),
                "priority": 5
            }
        }
    
    def find_placeholder_tests(self) -> List[Path]:
        """Find all files with placeholder property tests"""
        placeholder_files = []
        
        for rust_file in self.base_path.rglob("*.rs"):
            if rust_file.is_file():
                content = rust_file.read_text()
                if self.placeholder_pattern.search(content):
                    placeholder_files.append(rust_file)
        
        return placeholder_files
    
    def identify_module_type(self, file_path: Path) -> str:
        """Identify the type of module based on file name and content"""
        file_name = file_path.stem.lower()
        content = file_path.read_text().lower()[:1000]  # Check first 1000 chars
        
        # Check each module pattern
        best_match = "utility"  # Default
        best_score = 0
        
        for module_type, pattern_info in self.module_patterns.items():
            score = 0
            for keyword in pattern_info["keywords"]:
                if keyword in file_name:
                    score += 2  # File name match is stronger
                if keyword in content:
                    score += 1
            
            if score > best_score:
                best_score = score
                best_match = module_type
        
        return best_match
    
    def generate_upgrade_suggestion(self, file_path: Path) -> TestUpgradeCandidate:
        """Generate an upgrade suggestion for a file with placeholder tests"""
        module_type = self.identify_module_type(file_path)
        pattern_info = self.module_patterns[module_type]
        
        # Extract current test structure
        content = file_path.read_text()
        current_test = self.extract_current_test(content)
        
        # Generate suggested upgrade based on module type
        suggested_upgrade = pattern_info["template"].format(
            module_name=file_path.stem
        )
        
        return TestUpgradeCandidate(
            file_path=file_path,
            module_type=module_type,
            current_test=current_test,
            suggested_upgrade=suggested_upgrade,
            priority=pattern_info["priority"]
        )
    
    def extract_current_test(self, content: str) -> str:
        """Extract the current placeholder test from file content"""
        # Find the property test module
        match = re.search(
            r'(proptest!\s*\{[^}]*prop_assert!\(true\)[^}]*\})',
            content,
            re.DOTALL
        )
        
        if match:
            return match.group(1)
        
        return "// No property test found"
    
    def apply_upgrade(self, candidate: TestUpgradeCandidate, dry_run: bool = True) -> bool:
        """Apply the upgrade to the file"""
        if dry_run:
            print(f"Would upgrade: {candidate.file_path}")
            return True
        
        try:
            content = candidate.file_path.read_text()
            
            # Replace placeholder test with meaningful test
            updated_content = content.replace(
                candidate.current_test,
                candidate.suggested_upgrade
            )
            
            candidate.file_path.write_text(updated_content)
            return True
        except Exception as e:
            print(f"Error upgrading {candidate.file_path}: {e}")
            return False
    
    # Template methods for different module types
    def get_parser_template(self) -> str:
        return '''proptest! {{
        #[test]
        fn parse_valid_input_succeeds(
            input in valid_{module_name}_input()
        ) {{
            let result = parse_{module_name}(&input);
            prop_assert!(result.is_ok());
        }}

        #[test]
        fn parse_format_roundtrip(
            input in valid_{module_name}_input()
        ) {{
            let parsed = parse_{module_name}(&input).unwrap();
            let formatted = format_{module_name}(&parsed);
            let reparsed = parse_{module_name}(&formatted).unwrap();
            prop_assert_eq!(parsed, reparsed);
        }}

        #[test]
        fn parser_never_panics(input in ".*") {{
            // Parser should handle any input without panicking
            let _ = parse_{module_name}_safe(&input);
        }}
    }}

    fn valid_{module_name}_input() -> impl Strategy<Value = String> {{
        "[a-zA-Z][a-zA-Z0-9_]*"
    }}'''
    
    def get_analyzer_template(self) -> str:
        return '''proptest! {{
        #[test]
        fn analysis_deterministic(
            input in arbitrary_input()
        ) {{
            let result1 = analyze(&input);
            let result2 = analyze(&input);
            prop_assert_eq!(result1, result2);
        }}

        #[test]
        fn analysis_bounds_check(
            input in arbitrary_input()
        ) {{
            let result = analyze(&input);
            prop_assert!(result.score >= 0.0);
            prop_assert!(result.score <= 100.0);
        }}

        #[test]
        fn analysis_never_panics(input in ".*") {{
            let _ = analyze_safe(&input);
        }}
    }}'''
    
    def get_serializer_template(self) -> str:
        return '''proptest! {{
        #[test]
        fn serialize_deserialize_roundtrip(
            value in arbitrary_{module_name}()
        ) {{
            let serialized = serde_json::to_string(&value)?;
            let deserialized = serde_json::from_str(&serialized)?;
            prop_assert_eq!(value, deserialized);
        }}

        #[test]
        fn serialization_never_panics(
            value in arbitrary_{module_name}()
        ) {{
            let _ = serde_json::to_string(&value);
        }}
    }}'''
    
    def get_validator_template(self) -> str:
        return '''proptest! {{
        #[test]
        fn valid_input_passes_validation(
            input in valid_{module_name}_input()
        ) {{
            prop_assert!(validate_{module_name}(&input).is_ok());
        }}

        #[test]
        fn invalid_input_fails_validation(
            input in invalid_{module_name}_input()
        ) {{
            prop_assert!(validate_{module_name}(&input).is_err());
        }}

        #[test]
        fn validator_never_panics(input in ".*") {{
            let _ = validate_{module_name}(&input);
        }}
    }}'''
    
    def get_transformer_template(self) -> str:
        return '''proptest! {{
        #[test]
        fn transform_preserves_invariants(
            input in arbitrary_input()
        ) {{
            let transformed = transform(&input);
            prop_assert!(verify_invariants(&input, &transformed));
        }}

        #[test]
        fn transform_idempotent(
            input in arbitrary_input()
        ) {{
            let once = transform(&input);
            let twice = transform(&once);
            prop_assert_eq!(once, twice);
        }}
    }}'''
    
    def get_builder_template(self) -> str:
        return '''proptest! {{
        #[test]
        fn builder_creates_valid_objects(
            params in arbitrary_build_params()
        ) {{
            let result = Builder::new()
                .with_params(params)
                .build();
            prop_assert!(result.is_ok());
            prop_assert!(result.unwrap().is_valid());
        }}

        #[test]
        fn builder_defaults_are_valid() {{
            let result = Builder::default().build();
            prop_assert!(result.is_ok());
        }}
    }}'''
    
    def get_model_template(self) -> str:
        return '''proptest! {{
        #[test]
        fn model_equality_is_reflexive(
            model in arbitrary_{module_name}()
        ) {{
            prop_assert_eq!(&model, &model);
        }}

        #[test]
        fn model_clone_equals_original(
            model in arbitrary_{module_name}()
        ) {{
            let cloned = model.clone();
            prop_assert_eq!(model, cloned);
        }}

        #[test]
        fn model_hash_deterministic(
            model in arbitrary_{module_name}()
        ) {{
            use std::collections::hash_map::DefaultHasher;
            use std::hash::{{Hash, Hasher}};
            
            let mut hasher1 = DefaultHasher::new();
            model.hash(&mut hasher1);
            let hash1 = hasher1.finish();
            
            let mut hasher2 = DefaultHasher::new();
            model.hash(&mut hasher2);
            let hash2 = hasher2.finish();
            
            prop_assert_eq!(hash1, hash2);
        }}
    }}'''
    
    def get_service_template(self) -> str:
        return '''proptest! {{
        #[test]
        fn service_handles_concurrent_requests(
            requests in prop::collection::vec(arbitrary_request(), 1..10)
        ) {{
            let service = Service::new();
            let results: Vec<_> = requests.iter()
                .map(|req| service.handle(req))
                .collect();
            
            for result in results {{
                prop_assert!(result.is_ok());
            }}
        }}

        #[test]
        fn service_state_consistent(
            operations in prop::collection::vec(arbitrary_operation(), 1..20)
        ) {{
            let mut service = Service::new();
            
            for op in operations {{
                service.execute(op);
                prop_assert!(service.verify_state_consistency());
            }}
        }}
    }}'''
    
    def get_utility_template(self) -> str:
        return '''proptest! {{
        #[test]
        fn utility_function_deterministic(
            input in arbitrary_input()
        ) {{
            let result1 = utility_function(&input);
            let result2 = utility_function(&input);
            prop_assert_eq!(result1, result2);
        }}

        #[test]
        fn utility_handles_edge_cases(
            size in 0usize..10000
        ) {{
            let input = generate_input(size);
            let result = utility_function(&input);
            prop_assert!(result.is_valid());
        }}
    }}'''
    
    def generate_report(self, candidates: List[TestUpgradeCandidate], format: str = "terminal"):
        """Generate a report of upgrade candidates"""
        if format == "json":
            self.generate_json_report(candidates)
        elif format == "markdown":
            self.generate_markdown_report(candidates)
        else:
            self.generate_terminal_report(candidates)
    
    def generate_terminal_report(self, candidates: List[TestUpgradeCandidate]):
        """Generate terminal-formatted report"""
        print("\n" + "="*60)
        print("PROPERTY TEST UPGRADE CANDIDATES")
        print("="*60)
        print(f"\nFound {len(candidates)} files with placeholder tests\n")
        
        # Group by priority
        by_priority = {}
        for candidate in candidates:
            priority = candidate.priority
            if priority not in by_priority:
                by_priority[priority] = []
            by_priority[priority].append(candidate)
        
        for priority in sorted(by_priority.keys()):
            priority_candidates = by_priority[priority]
            print(f"\n{'' * (6 - priority)} Priority {priority} ({len(priority_candidates)} files)")
            print("-" * 40)
            
            for candidate in priority_candidates[:5]:  # Show top 5 per priority
                rel_path = candidate.file_path.relative_to(self.base_path)
                print(f"  📁 {rel_path}")
                print(f"     Type: {candidate.module_type}")
                print(f"     Upgrade: Add {candidate.module_type}-specific property tests")
            
            if len(priority_candidates) > 5:
                print(f"  ... and {len(priority_candidates) - 5} more")
    
    def generate_markdown_report(self, candidates: List[TestUpgradeCandidate]):
        """Generate markdown-formatted report"""
        print("# Property Test Upgrade Report\n")
        print(f"**Total Files**: {len(candidates)}\n")
        print(f"**Date**: {__import__('datetime').datetime.now().isoformat()}\n")
        
        # Statistics by module type
        by_type = {}
        for candidate in candidates:
            module_type = candidate.module_type
            if module_type not in by_type:
                by_type[module_type] = 0
            by_type[module_type] += 1
        
        print("## Statistics by Module Type\n")
        print("| Module Type | Count | Priority |")
        print("|------------|-------|----------|")
        for module_type in sorted(by_type.keys()):
            priority = self.module_patterns[module_type]["priority"]
            print(f"| {module_type} | {by_type[module_type]} | {priority} |")
        
        print("\n## Top Priority Upgrades\n")
        
        # Show top candidates
        sorted_candidates = sorted(candidates, key=lambda c: (c.priority, str(c.file_path)))
        for candidate in sorted_candidates[:20]:
            rel_path = candidate.file_path.relative_to(self.base_path)
            print(f"- [ ] `{rel_path}` ({candidate.module_type})")
    
    def generate_json_report(self, candidates: List[TestUpgradeCandidate]):
        """Generate JSON-formatted report"""
        report = {
            "total_files": len(candidates),
            "timestamp": __import__('datetime').datetime.now().isoformat(),
            "candidates": []
        }
        
        for candidate in candidates:
            report["candidates"].append({
                "file": str(candidate.file_path.relative_to(self.base_path)),
                "module_type": candidate.module_type,
                "priority": candidate.priority
            })
        
        print(json.dumps(report, indent=2))

def main():
    parser = argparse.ArgumentParser(
        description="Upgrade placeholder property tests to meaningful ones"
    )
    parser.add_argument(
        "--path",
        default="server/src",
        help="Base path to search for Rust files"
    )
    parser.add_argument(
        "--format",
        choices=["terminal", "markdown", "json"],
        default="terminal",
        help="Output format for the report"
    )
    parser.add_argument(
        "--apply",
        action="store_true",
        help="Actually apply the upgrades (default is dry-run)"
    )
    parser.add_argument(
        "--limit",
        type=int,
        default=0,
        help="Limit number of files to upgrade (0 = no limit)"
    )
    parser.add_argument(
        "--priority",
        type=int,
        choices=[1, 2, 3, 4, 5],
        help="Only upgrade files with this priority or higher"
    )
    
    args = parser.parse_args()
    
    upgrader = PropertyTestUpgrader(args.path)
    
    # Find placeholder tests
    placeholder_files = upgrader.find_placeholder_tests()
    
    if not placeholder_files:
        print("No placeholder tests found!")
        return
    
    # Generate upgrade candidates
    candidates = []
    for file_path in placeholder_files:
        candidate = upgrader.generate_upgrade_suggestion(file_path)
        
        # Filter by priority if specified
        if args.priority and candidate.priority > args.priority:
            continue
        
        candidates.append(candidate)
    
    # Sort by priority
    candidates.sort(key=lambda c: (c.priority, str(c.file_path)))
    
    # Apply limit if specified
    if args.limit > 0:
        candidates = candidates[:args.limit]
    
    # Generate report
    upgrader.generate_report(candidates, args.format)
    
    # Apply upgrades if requested
    if args.apply:
        print(f"\nApplying upgrades to {len(candidates)} files...")
        success_count = 0
        
        for candidate in candidates:
            if upgrader.apply_upgrade(candidate, dry_run=False):
                success_count += 1
                print(f"✅ Upgraded: {candidate.file_path}")
            else:
                print(f"❌ Failed: {candidate.file_path}")
        
        print(f"\nSuccessfully upgraded {success_count}/{len(candidates)} files")

if __name__ == "__main__":
    main()