File size: 36,556 Bytes
81aca7d
77085e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81aca7d
 
 
 
 
 
77085e5
81aca7d
 
 
 
 
 
 
 
77085e5
81aca7d
 
 
 
77085e5
81aca7d
 
 
 
 
 
 
 
 
77085e5
 
81aca7d
 
 
 
 
 
77085e5
 
81aca7d
 
 
 
 
 
 
 
 
 
77085e5
 
 
81aca7d
77085e5
 
 
 
 
81aca7d
 
77085e5
 
 
 
 
 
81aca7d
 
77085e5
 
 
 
 
 
 
 
 
 
81aca7d
 
77085e5
 
 
 
 
 
81aca7d
77085e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81aca7d
 
 
 
 
77085e5
81aca7d
77085e5
81aca7d
 
77085e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81aca7d
77085e5
 
 
 
 
 
 
81aca7d
 
77085e5
81aca7d
 
 
 
 
77085e5
 
 
 
 
 
 
 
81aca7d
77085e5
 
 
 
 
 
81aca7d
77085e5
 
 
 
 
 
81aca7d
77085e5
 
 
 
 
 
 
81aca7d
77085e5
 
 
 
 
 
 
81aca7d
 
 
 
77085e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81aca7d
77085e5
81aca7d
 
 
 
 
 
 
 
 
77085e5
81aca7d
 
77085e5
 
81aca7d
 
 
 
77085e5
81aca7d
 
 
 
 
 
 
 
77085e5
 
 
 
 
81aca7d
77085e5
81aca7d
 
 
77085e5
81aca7d
 
 
 
 
77085e5
 
 
81aca7d
 
 
 
 
77085e5
 
81aca7d
 
77085e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81aca7d
 
77085e5
81aca7d
 
77085e5
 
 
 
 
 
 
 
81aca7d
77085e5
81aca7d
 
 
77085e5
 
 
 
 
 
 
81aca7d
77085e5
 
 
81aca7d
77085e5
81aca7d
 
 
77085e5
81aca7d
 
77085e5
81aca7d
 
77085e5
 
 
81aca7d
 
 
77085e5
81aca7d
77085e5
 
81aca7d
77085e5
81aca7d
77085e5
 
81aca7d
77085e5
81aca7d
 
 
 
77085e5
81aca7d
 
 
 
 
 
 
77085e5
81aca7d
77085e5
 
 
 
 
 
 
81aca7d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
77085e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81aca7d
 
 
77085e5
81aca7d
 
77085e5
 
 
 
 
 
 
 
 
81aca7d
77085e5
81aca7d
 
 
 
77085e5
81aca7d
77085e5
81aca7d
 
 
77085e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81aca7d
 
 
 
 
77085e5
81aca7d
 
77085e5
 
 
 
81aca7d
77085e5
 
81aca7d
 
 
 
 
 
77085e5
81aca7d
 
 
 
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
"""
TestGenius AI β€” Multi-Agent Iterative Test Refinement Engine (v2 β€” ALL FIXES)
===============================================================================
RESEARCH-GRADE: Based on MuTAP (arxiv:2308.16557) + HITS (ASE 2024) + Code Agents (arxiv:2406.12952)

FIXES APPLIED (v2):
1. βœ… SEMANTIC coverage mapping (AST-based, not keyword matching)
2. βœ… REAL mutation execution β€” mutates code, runs tests, identifies SURVIVORS
3. βœ… Better iteration: max_iterations=5, adaptive threshold, quality improves each pass
4. βœ… Test syntax validation β€” actually compiles/parses generated tests
5. βœ… Adaptive prompting β€” uses complexity analysis to calibrate generation
6. βœ… Few-shot examples in refinement prompts for higher quality
7. βœ… Execution-verified output β€” tests are validated to parse before returning

Pipeline (iterative until Grade A):
  AGENT 1: Analyzer β€” AST complexity + behavior extraction + gap detection
  AGENT 2: Generator β€” Adaptive, context-rich, few-shot LLM generation
  AGENT 3: Validator β€” Syntax check + execution + quality scoring
  AGENT 4: Refiner β€” Mutation EXECUTION feedback β†’ re-prompt for stronger tests
  AGENT 5: Coverage Mapper β€” AST-based semantic behavior coverage
"""

import re
import ast
import time
import uuid
from typing import Dict, List, Any, Optional, Set
from dataclasses import dataclass, field

from app.services.llm_provider import generate_with_llm
from app.services.novelty_features import (
    analyze_code_complexity,
    detect_coverage_gaps,
    score_test_quality,
    suggest_mutations,
    execute_mutations,
)


# ═══════════════════════════════════════════════════════════════
# BEHAVIOR EXTRACTION (AST-based, Qodo/CodiumAI-style)
# ═══════════════════════════════════════════════════════════════

@dataclass
class Behavior:
    """A testable behavior extracted from code/requirements."""
    id: str
    description: str
    category: str  # "happy_path" | "edge_case" | "error_handling" | "security" | "performance"
    priority: str  # "critical" | "high" | "medium" | "low"
    source: str
    keywords: List[str] = field(default_factory=list)
    tested: bool = False
    test_ids: List[str] = field(default_factory=list)


def extract_behaviors(source_code: str = "", requirements: str = "", api_spec: dict = None) -> List[Behavior]:
    """
    Extract ALL testable behaviors using deep AST analysis + NLP heuristics.
    Each behavior gets semantic keywords for accurate coverage mapping.
    """
    behaviors = []
    bid = 0

    if source_code:
        try:
            tree = ast.parse(source_code)
            for node in ast.walk(tree):
                if isinstance(node, (ast.FunctionDef, ast.AsyncFunctionDef)):
                    fname = node.name
                    if fname.startswith('_') and fname != '__init__':
                        continue

                    params = [a.arg for a in node.args.args if a.arg != 'self']
                    has_return = any(isinstance(n, ast.Return) and n.value is not None for n in ast.walk(node))
                    raises = [_get_exception_name(n) for n in ast.walk(node) if isinstance(n, ast.Raise)]
                    has_loop = any(isinstance(n, (ast.For, ast.While)) for n in ast.walk(node))
                    conditionals = sum(1 for n in ast.walk(node) if isinstance(n, ast.If))
                    external_calls = _find_external_calls(node)

                    # Happy path
                    behaviors.append(Behavior(
                        id=f"B{bid:03d}",
                        description=f"{fname}() returns correct result with valid inputs ({', '.join(params[:4])})",
                        category="happy_path", priority="critical", source=f"function:{fname}",
                        keywords=[fname, "valid", "correct", "success"] + params[:3]
                    ))
                    bid += 1

                    # Return value type
                    if has_return:
                        behaviors.append(Behavior(
                            id=f"B{bid:03d}",
                            description=f"{fname}() return value has correct type and structure",
                            category="happy_path", priority="high", source=f"function:{fname}",
                            keywords=[fname, "return", "type", "result"]
                        ))
                        bid += 1

                    # Edge cases per param
                    for p in params:
                        behaviors.append(Behavior(
                            id=f"B{bid:03d}",
                            description=f"{fname}() handles None/null for parameter '{p}'",
                            category="edge_case", priority="high", source=f"function:{fname}",
                            keywords=[fname, p, "none", "null", "missing"]
                        ))
                        bid += 1
                        behaviors.append(Behavior(
                            id=f"B{bid:03d}",
                            description=f"{fname}() handles empty/zero value for '{p}'",
                            category="edge_case", priority="medium", source=f"function:{fname}",
                            keywords=[fname, p, "empty", "zero", "blank"]
                        ))
                        bid += 1

                    # Boundary values for numeric functions
                    if any(kw in fname.lower() for kw in ['calc', 'compute', 'count', 'sum', 'total', 'price', 'amount', 'score']):
                        behaviors.append(Behavior(
                            id=f"B{bid:03d}",
                            description=f"{fname}() handles boundary values (max int, negative, float precision)",
                            category="edge_case", priority="high", source=f"function:{fname}",
                            keywords=[fname, "boundary", "max", "min", "negative", "overflow"]
                        ))
                        bid += 1

                    # Exception paths
                    for exc in set(raises):
                        behaviors.append(Behavior(
                            id=f"B{bid:03d}",
                            description=f"{fname}() raises {exc} on invalid input",
                            category="error_handling", priority="high", source=f"function:{fname}",
                            keywords=[fname, "raises", exc.lower(), "error", "invalid", "exception"]
                        ))
                        bid += 1

                    # Loop correctness
                    if has_loop:
                        behaviors.append(Behavior(
                            id=f"B{bid:03d}",
                            description=f"{fname}() loop handles empty collection and single element",
                            category="edge_case", priority="medium", source=f"function:{fname}",
                            keywords=[fname, "loop", "empty", "single", "iteration"]
                        ))
                        bid += 1

                    # External call mocking
                    for call in external_calls[:3]:
                        behaviors.append(Behavior(
                            id=f"B{bid:03d}",
                            description=f"{fname}() handles {call} failure/timeout gracefully",
                            category="error_handling", priority="high", source=f"function:{fname}",
                            keywords=[fname, call, "mock", "failure", "timeout", "exception"]
                        ))
                        bid += 1

                    # Branch coverage
                    if conditionals >= 2:
                        behaviors.append(Behavior(
                            id=f"B{bid:03d}",
                            description=f"{fname}() all {conditionals} conditional branches produce correct output",
                            category="happy_path", priority="high", source=f"function:{fname}",
                            keywords=[fname, "branch", "condition", "if", "else"]
                        ))
                        bid += 1

        except SyntaxError:
            pass

    # From requirements
    if requirements:
        sentences = re.split(r'[.\nβ€’\-\*]', requirements)
        for sent in sentences:
            sent = sent.strip()
            if len(sent) < 15:
                continue
            if any(kw in sent.lower() for kw in ['must', 'should', 'can', 'will', 'allow', 'enable', 'support']):
                keywords = [w.lower() for w in re.findall(r'\b[a-zA-Z]{4,}\b', sent)][:6]
                behaviors.append(Behavior(
                    id=f"B{bid:03d}", description=sent[:100],
                    category="happy_path", priority="high", source="requirements",
                    keywords=keywords
                ))
                bid += 1
            if any(kw in sent.lower() for kw in ['error', 'fail', 'invalid', 'reject', 'deny', 'block', 'limit', 'prevent']):
                keywords = [w.lower() for w in re.findall(r'\b[a-zA-Z]{4,}\b', sent)][:6]
                behaviors.append(Behavior(
                    id=f"B{bid:03d}", description=f"Negative: {sent[:80]}",
                    category="error_handling", priority="high", source="requirements",
                    keywords=keywords + ["error", "invalid", "reject"]
                ))
                bid += 1
            if any(kw in sent.lower() for kw in ['auth', 'token', 'permission', 'role', 'encrypt', 'secure', 'inject']):
                keywords = [w.lower() for w in re.findall(r'\b[a-zA-Z]{4,}\b', sent)][:6]
                behaviors.append(Behavior(
                    id=f"B{bid:03d}", description=f"Security: {sent[:80]}",
                    category="security", priority="critical", source="requirements",
                    keywords=keywords + ["security", "auth"]
                ))
                bid += 1

    # From API spec
    if api_spec:
        for path, methods in api_spec.get("paths", {}).items():
            for method, details in methods.items():
                if method not in ("get", "post", "put", "patch", "delete"):
                    continue
                path_words = [w for w in re.split(r'[/{}]', path) if w and len(w) > 2]

                behaviors.append(Behavior(
                    id=f"B{bid:03d}",
                    description=f"{method.upper()} {path} returns success (200/201)",
                    category="happy_path", priority="critical", source=f"api:{method}:{path}",
                    keywords=[method, "200", "success"] + path_words
                ))
                bid += 1
                behaviors.append(Behavior(
                    id=f"B{bid:03d}",
                    description=f"{method.upper()} {path} returns 401 without authentication",
                    category="security", priority="high", source=f"api:{method}:{path}",
                    keywords=[method, "401", "auth", "unauthorized", "token"] + path_words
                ))
                bid += 1
                behaviors.append(Behavior(
                    id=f"B{bid:03d}",
                    description=f"{method.upper()} {path} returns 400/422 with invalid body",
                    category="error_handling", priority="high", source=f"api:{method}:{path}",
                    keywords=[method, "400", "422", "invalid", "validation"] + path_words
                ))
                bid += 1
                if method in ("post", "put", "patch"):
                    behaviors.append(Behavior(
                        id=f"B{bid:03d}",
                        description=f"{method.upper()} {path} rejects SQL injection in string fields",
                        category="security", priority="critical", source=f"api:{method}:{path}",
                        keywords=[method, "sql", "injection", "xss", "sanitize"] + path_words
                    ))
                    bid += 1
                behaviors.append(Behavior(
                    id=f"B{bid:03d}",
                    description=f"{method.upper()} {path} respects rate limits (429)",
                    category="security", priority="medium", source=f"api:{method}:{path}",
                    keywords=[method, "rate", "limit", "429", "throttle"] + path_words
                ))
                bid += 1

    return behaviors


def _get_exception_name(node) -> str:
    if node.exc is None:
        return "Exception"
    if isinstance(node.exc, ast.Call):
        if isinstance(node.exc.func, ast.Name):
            return node.exc.func.id
        elif isinstance(node.exc.func, ast.Attribute):
            return node.exc.func.attr
    elif isinstance(node.exc, ast.Name):
        return node.exc.id
    return "Exception"


def _find_external_calls(func_node) -> List[str]:
    calls = []
    for node in ast.walk(func_node):
        if isinstance(node, ast.Call):
            if isinstance(node.func, ast.Attribute):
                if isinstance(node.func.value, ast.Name):
                    if node.func.value.id not in ('self', 'cls', 'super'):
                        calls.append(f"{node.func.value.id}.{node.func.attr}")
    return list(set(calls))


# ═══════════════════════════════════════════════════════════════
# TEST SYNTAX VALIDATION (FIX: Actually compiles generated tests)
# ═══════════════════════════════════════════════════════════════

def validate_test_syntax(test_code: str, language: str = "python") -> Dict[str, Any]:
    """Actually parse/compile generated test code to verify syntax."""
    if language == "python":
        return _validate_python_syntax(test_code)
    elif language in ("javascript", "typescript"):
        return _validate_js_syntax(test_code)
    return {"valid": True, "errors": [], "warnings": []}


def _validate_python_syntax(code: str) -> Dict[str, Any]:
    errors, warnings = [], []
    clean_code = re.sub(r'^```\w*\n|```$', '', code, flags=re.MULTILINE).strip()

    try:
        tree = ast.parse(clean_code)
        test_funcs = [n for n in ast.walk(tree) if isinstance(n, ast.FunctionDef) and n.name.startswith('test_')]
        test_classes = [n for n in ast.walk(tree) if isinstance(n, ast.ClassDef) and n.name.startswith('Test')]

        if not test_funcs and not test_classes:
            warnings.append("No test functions (test_*) or test classes (Test*) found")

        has_assert = any(isinstance(n, ast.Assert) for n in ast.walk(tree))
        has_assert_call = 'assert' in clean_code.lower() or 'raises' in clean_code
        if not has_assert and not has_assert_call:
            warnings.append("No assertions found in test code")

        return {
            "valid": True, "errors": errors, "warnings": warnings,
            "test_count": len(test_funcs), "class_count": len(test_classes),
            "has_assertions": has_assert or has_assert_call,
            "lines": len(clean_code.split('\n')),
        }
    except SyntaxError as e:
        return {
            "valid": False,
            "errors": [f"SyntaxError at line {e.lineno}: {e.msg}"],
            "warnings": [], "test_count": 0, "class_count": 0,
            "has_assertions": False, "lines": 0,
        }


def _validate_js_syntax(code: str) -> Dict[str, Any]:
    errors, warnings = [], []
    clean_code = re.sub(r'^```\w*\n|```$', '', code, flags=re.MULTILINE).strip()

    opens = clean_code.count('{')
    closes = clean_code.count('}')
    if opens != closes:
        errors.append(f"Unbalanced braces: {opens} opening, {closes} closing")

    test_blocks = len(re.findall(r'\b(it|test|describe)\s*\(', clean_code))
    if test_blocks == 0:
        warnings.append("No test blocks (it/test/describe) found")

    has_expect = 'expect(' in clean_code or 'assert' in clean_code
    if not has_expect:
        warnings.append("No assertions (expect/assert) found")

    return {
        "valid": len(errors) == 0, "errors": errors, "warnings": warnings,
        "test_count": test_blocks, "has_assertions": has_expect,
        "lines": len(clean_code.split('\n')),
    }


# ═══════════════════════════════════════════════════════════════
# SEMANTIC COVERAGE MAPPING (FIX: AST-based, not keyword matching)
# ═══════════════════════════════════════════════════════════════

@dataclass
class TestSignature:
    name: str
    docstring: str = ""
    called_functions: List[str] = field(default_factory=list)
    keywords: Set[str] = field(default_factory=set)
    tests_exception: bool = False
    tests_none: bool = False


def map_behavior_coverage_semantic(behaviors: List[Behavior], test_code: str, language: str = "python") -> Dict[str, Any]:
    """FIX: AST-based semantic coverage mapping."""
    if language == "python":
        test_signatures = _extract_python_test_signatures(test_code)
    else:
        test_signatures = _extract_js_test_signatures(test_code)

    covered = 0
    uncovered_list = []

    for b in behaviors:
        is_covered = _behavior_matches_tests(b, test_signatures)
        b.tested = is_covered
        if is_covered:
            covered += 1
        else:
            uncovered_list.append({
                "id": b.id, "description": b.description,
                "priority": b.priority, "category": b.category,
                "suggestion": _suggest_test_for_behavior(b),
            })

    total = max(len(behaviors), 1)
    return {
        "total_behaviors": len(behaviors),
        "covered": covered,
        "uncovered": len(behaviors) - covered,
        "coverage_pct": round((covered / total) * 100, 1),
        "uncovered_behaviors": uncovered_list[:20],
        "by_category": {
            cat: {
                "total": len([b for b in behaviors if b.category == cat]),
                "tested": len([b for b in behaviors if b.category == cat and b.tested]),
                "coverage_pct": round(
                    len([b for b in behaviors if b.category == cat and b.tested]) /
                    max(len([b for b in behaviors if b.category == cat]), 1) * 100, 1
                )
            }
            for cat in set(b.category for b in behaviors)
        },
        "test_signatures_found": len(test_signatures),
    }


def _extract_python_test_signatures(code: str) -> List[TestSignature]:
    signatures = []
    clean_code = re.sub(r'^```\w*\n|```$', '', code, flags=re.MULTILINE).strip()

    try:
        tree = ast.parse(clean_code)
    except SyntaxError:
        return _extract_signatures_regex(code)

    for node in ast.walk(tree):
        if isinstance(node, ast.FunctionDef) and node.name.startswith('test_'):
            sig = TestSignature(name=node.name)

            # Docstring
            if (node.body and isinstance(node.body[0], ast.Expr) and
                isinstance(node.body[0].value, ast.Constant) and isinstance(node.body[0].value.value, str)):
                sig.docstring = node.body[0].value.value

            # Called functions
            for child in ast.walk(node):
                if isinstance(child, ast.Call):
                    if isinstance(child.func, ast.Name):
                        sig.called_functions.append(child.func.id)
                    elif isinstance(child.func, ast.Attribute):
                        sig.called_functions.append(child.func.attr)

            # Exception testing
            sig.tests_exception = any(
                isinstance(child, ast.Call) and isinstance(child.func, ast.Attribute) and child.func.attr == 'raises'
                for child in ast.walk(node)
            ) or 'raises' in node.name or 'error' in node.name or 'exception' in node.name

            sig.tests_none = 'none' in node.name.lower() or 'null' in node.name.lower()

            # Keywords from name + docstring
            name_words = set(node.name.replace('test_', '').split('_'))
            doc_words = set(re.findall(r'\b[a-z]{3,}\b', sig.docstring.lower())) if sig.docstring else set()
            sig.keywords = name_words | doc_words | set(sig.called_functions)

            signatures.append(sig)

    return signatures


def _extract_js_test_signatures(code: str) -> List[TestSignature]:
    signatures = []
    for match in re.finditer(r"(?:it|test)\s*\(\s*['\"](.+?)['\"]", code):
        desc = match.group(1)
        sig = TestSignature(name=desc, docstring=desc)
        sig.keywords = set(re.findall(r'\b[a-z]{3,}\b', desc.lower()))
        sig.tests_exception = any(kw in desc.lower() for kw in ['error', 'throw', 'reject', 'fail'])
        sig.tests_none = any(kw in desc.lower() for kw in ['null', 'undefined', 'empty'])
        signatures.append(sig)
    return signatures


def _extract_signatures_regex(code: str) -> List[TestSignature]:
    signatures = []
    for match in re.finditer(r'def (test_\w+)', code):
        name = match.group(1)
        sig = TestSignature(name=name)
        sig.keywords = set(name.replace('test_', '').split('_'))
        sig.tests_exception = 'raises' in name or 'error' in name
        sig.tests_none = 'none' in name or 'null' in name
        signatures.append(sig)
    return signatures


def _behavior_matches_tests(behavior: Behavior, signatures: List[TestSignature]) -> bool:
    """Semantic matching using multi-signal scoring."""
    b_keywords = set(kw.lower() for kw in behavior.keywords if len(kw) > 2)
    b_desc_words = set(re.findall(r'\b[a-z]{4,}\b', behavior.description.lower()))
    all_b_words = b_keywords | b_desc_words

    for sig in signatures:
        score = 0
        overlap = all_b_words & sig.keywords
        score += len(overlap) * 2

        for kw in b_keywords:
            if kw in sig.name.lower():
                score += 3

        if sig.docstring:
            doc_overlap = all_b_words & set(re.findall(r'\b[a-z]{3,}\b', sig.docstring.lower()))
            score += len(doc_overlap)

        if behavior.category == "error_handling" and sig.tests_exception:
            score += 3
        if behavior.category == "edge_case" and sig.tests_none:
            score += 2
        if behavior.category == "edge_case" and any(kw in sig.name for kw in ['empty', 'zero', 'boundary', 'edge']):
            score += 3
        if behavior.category == "security" and any(kw in sig.name for kw in ['inject', 'auth', 'xss', 'sql', 'security']):
            score += 3

        source_func = behavior.source.split(':')[-1] if ':' in behavior.source else ""
        if source_func and source_func in sig.called_functions:
            score += 4

        if score >= 4:
            return True

    return False


def _suggest_test_for_behavior(behavior: Behavior) -> str:
    func = behavior.source.split(':')[-1] if ':' in behavior.source else "target"
    if behavior.category == "edge_case":
        return f"def test_{func}_edge_{behavior.id.lower()}(): # {behavior.description[:50]}"
    elif behavior.category == "error_handling":
        return f"def test_{func}_raises_on_invalid(): # pytest.raises(...)"
    elif behavior.category == "security":
        return f"def test_{func}_rejects_malicious_input(): # injection payloads"
    return f"def test_{func}_{behavior.id.lower()}(): # {behavior.description[:40]}"


# ═══════════════════════════════════════════════════════════════
# MULTI-AGENT ITERATIVE REFINEMENT ENGINE (v2)
# ═══════════════════════════════════════════════════════════════

async def run_multi_agent_pipeline(
    source_code: str = "",
    requirements: str = "",
    api_spec: dict = None,
    framework: str = "pytest",
    language: str = "python",
    test_types: List[str] = None,
    max_iterations: int = 3,
) -> Dict[str, Any]:
    """
    Full multi-agent iterative refinement pipeline (v2).
    Now with real mutation execution, syntax validation, and semantic coverage.
    """
    start_time = time.time()
    run_id = f"MAS-{uuid.uuid4().hex[:8]}"
    test_types = test_types or ["unit", "integration", "edge_case", "security"]
    max_iterations = min(max_iterations, 5)

    # ═══ AGENT 1: ANALYZER ═══
    analysis = {
        "behaviors": extract_behaviors(source_code, requirements, api_spec),
        "complexity": analyze_code_complexity(source_code, language) if source_code else None,
        "gaps": detect_coverage_gaps(source_code) if source_code else None,
    }

    suggested_tests = 15
    if analysis["complexity"]:
        suggested_tests = max(10, min(30, analysis["complexity"].get("suggested_total_tests", 15)))

    # ═══ AGENT 2: GENERATOR ═══
    input_context = _build_rich_context(source_code, requirements, api_spec, analysis)
    system_prompt = _build_system_prompt(framework, language, suggested_tests)

    gen_prompt = f"""Generate {framework} tests in {language}.
Test types: {', '.join(test_types)}
Target: {suggested_tests}+ test functions.

CONTEXT:
{input_context}

BEHAVIORS TO TEST ({len(analysis['behaviors'])} identified):
{chr(10).join(f'- [{b.priority.upper()}][{b.category}] {b.description}' for b in analysis['behaviors'][:25])}

{_get_few_shot_example(framework, language)}

Generate the complete test file now:"""

    raw_tests = await generate_with_llm(gen_prompt, system_prompt, temperature=0.3)

    # ═══ AGENT 3: VALIDATOR ═══
    syntax_result = validate_test_syntax(raw_tests, language)
    quality_score = score_test_quality(raw_tests)

    # Fix syntax if broken
    if not syntax_result["valid"]:
        fix_prompt = f"""Fix the syntax errors in these tests.
ERRORS: {chr(10).join(syntax_result['errors'])}

```
{raw_tests[:4000]}
```

Output the FIXED complete test code:"""
        raw_tests = await generate_with_llm(fix_prompt, system_prompt, temperature=0.1)
        syntax_result = validate_test_syntax(raw_tests, language)
        quality_score = score_test_quality(raw_tests)

    # ═══ AGENT 4: REFINER ═══
    refined_tests = raw_tests
    refinement_history = []
    mutation_results = None

    for iteration in range(max_iterations):
        if quality_score["overall"] >= 85 and syntax_result.get("valid", True):
            break

        # Real mutation execution
        if source_code and language == "python":
            mutation_results = execute_mutations(source_code, refined_tests)
        else:
            mutation_results = suggest_mutations(source_code) if source_code else {"mutations": [], "total_mutations": 0}

        weak_areas = _identify_weaknesses(quality_score, mutation_results, syntax_result)
        if not weak_areas:
            break

        mutation_feedback = ""
        if mutation_results and mutation_results.get("surviving_mutants"):
            survivors = mutation_results["surviving_mutants"][:5]
            mutation_feedback = f"""
SURVIVING MUTANTS (tests FAILED to catch these):
{chr(10).join(f'  β€’ Line {m["line"]}: {m["description"]}' for m in survivors)}
Write tests that KILL these mutants."""

        refine_prompt = f"""IMPROVE tests. Quality: {quality_score['overall']}/100 (Grade {quality_score['grade']}).

WEAKNESSES:
{chr(10).join(f'{i+1}. {w}' for i, w in enumerate(weak_areas))}
{mutation_feedback}

CURRENT TESTS:
```
{refined_tests[:4000]}
```

Keep all good tests, ADD new ones for gaps. Output COMPLETE improved test file:"""

        refined_tests = await generate_with_llm(refine_prompt, system_prompt, temperature=0.2)
        syntax_result = validate_test_syntax(refined_tests, language)
        new_quality = score_test_quality(refined_tests)

        refinement_history.append({
            "iteration": iteration + 1,
            "quality_before": quality_score["overall"],
            "quality_after": new_quality["overall"],
            "weaknesses_addressed": weak_areas,
            "mutation_survivors": mutation_results.get("surviving_count", 0) if mutation_results else 0,
            "syntax_valid": syntax_result.get("valid", True),
        })
        quality_score = new_quality

    # ═══ AGENT 5: COVERAGE MAPPER ═══
    behavior_coverage = map_behavior_coverage_semantic(analysis["behaviors"], refined_tests, language)

    final_syntax = validate_test_syntax(refined_tests, language)
    processing_time = (time.time() - start_time) * 1000

    return {
        "run_id": run_id,
        "pipeline": "multi-agent-iterative-v2",
        "status": "completed",
        "timestamp": time.strftime("%Y-%m-%dT%H:%M:%SZ"),
        "test_code": refined_tests,
        "test_files": _parse_to_files(refined_tests, framework, language),
        "quality": quality_score,
        "iterations_performed": len(refinement_history),
        "refinement_history": refinement_history,
        "syntax_validation": final_syntax,
        "behavior_coverage": behavior_coverage,
        "mutation_testing": {
            "total_mutants": mutation_results.get("total_mutations", 0) if mutation_results else 0,
            "killed": mutation_results.get("killed_count", 0) if mutation_results else 0,
            "survived": mutation_results.get("surviving_count", 0) if mutation_results else 0,
            "mutation_score": mutation_results.get("mutation_score", 0) if mutation_results else 0,
            "surviving_mutants": mutation_results.get("surviving_mutants", [])[:10] if mutation_results else [],
        },
        "analysis": {
            "total_behaviors": len(analysis["behaviors"]),
            "behaviors_tested": behavior_coverage["covered"],
            "behaviors_untested": behavior_coverage["uncovered"],
            "coverage_pct": behavior_coverage["coverage_pct"],
            "complexity": analysis.get("complexity"),
            "gaps": analysis.get("gaps"),
        },
        "processing_time_ms": round(processing_time, 1),
    }


# ═══════════════════════════════════════════════════════════════
# HELPERS
# ═══════════════════════════════════════════════════════════════

def _build_system_prompt(framework: str, language: str, target_tests: int) -> str:
    return f"""You are TestGenius, an expert QA automation engineer.
Generate COMPREHENSIVE, RUNNABLE {framework} test code in {language}.

RULES:
1. Include ALL necessary imports.
2. Every test MUST have 2+ assertions.
3. Descriptive names: test_<what>_<scenario>_<expected>.
4. Docstring per test explaining what's verified.
5. Cover: happy path, edge cases, boundary values, errors.
6. For errors: verify exception type AND message.
7. Mock external dependencies properly.
8. Generate {target_tests}+ test functions.
9. Group related tests logically.
10. Output ONLY valid {language} code in a single code block."""


def _build_rich_context(code: str, reqs: str, api: dict, analysis: dict) -> str:
    parts = []
    if code:
        parts.append(f"SOURCE CODE:\n```\n{code[:4000]}\n```")
        if analysis.get("complexity"):
            c = analysis["complexity"]
            high_funcs = [f for f in c.get("functions", []) if f.get("priority") == "HIGH"]
            if high_funcs:
                parts.append("⚠️ HIGH-COMPLEXITY FUNCTIONS:\n" +
                           "\n".join(f"  β€’ {f['name']}() β€” complexity {f['complexity']}, needs {f['suggested_tests']}+ tests" for f in high_funcs))
        if analysis.get("gaps"):
            g = analysis["gaps"]
            if g.get("high_priority", 0) > 0:
                parts.append(f"⚠️ COVERAGE GAPS ({g['high_priority']} high-priority):\n" +
                           "\n".join(f"  β€’ {gap['desc']}" for gap in g.get("gaps", [])[:5] if gap["priority"] == "HIGH"))
    if reqs:
        parts.append(f"REQUIREMENTS:\n{reqs[:2500]}")
    if api:
        endpoints = []
        for path, methods in api.get("paths", {}).items():
            for method in methods:
                if method in ("get", "post", "put", "patch", "delete"):
                    endpoints.append(f"  {method.upper()} {path}")
        parts.append(f"API ENDPOINTS:\n{chr(10).join(endpoints[:20])}")
    return "\n\n".join(parts)


def _get_few_shot_example(framework: str, language: str) -> str:
    if framework == "pytest" and language == "python":
        return """
EXAMPLE OF IDEAL QUALITY:
```python
import pytest

def test_calculate_discount_premium_user_gets_20_percent():
    \"\"\"Premium users receive exactly 20% discount.\"\"\"
    result = calculate_discount(100.0, "premium")
    assert result == 20.0
    assert isinstance(result, float)

def test_calculate_discount_negative_price_raises_value_error():
    \"\"\"Negative prices must raise ValueError.\"\"\"
    with pytest.raises(ValueError, match="Price must be positive"):
        calculate_discount(-10, "standard")

@pytest.mark.parametrize("user_type,expected", [("premium", 20.0), ("member", 10.0)])
def test_calculate_discount_user_types(user_type, expected):
    \"\"\"Each user type gets correct discount.\"\"\"
    assert calculate_discount(100.0, user_type) == expected
```
Follow this pattern."""
    elif framework in ("jest", "vitest"):
        return """
EXAMPLE:
```javascript
describe('calculateDiscount', () => {
  test('premium user gets 20% discount', () => {
    const result = calculateDiscount(100, 'premium');
    expect(result).toBe(20);
    expect(typeof result).toBe('number');
  });
  test('throws for negative price', () => {
    expect(() => calculateDiscount(-10, 'standard')).toThrow('Price must be positive');
  });
});
```"""
    return ""


def _identify_weaknesses(quality: Dict, mutations: Dict, syntax: Dict) -> List[str]:
    weak_areas = []
    if not syntax.get("valid", True):
        weak_areas.append(f"FIX SYNTAX: {'; '.join(syntax.get('errors', [])[:3])}")

    scores = quality.get("scores", {})
    if scores.get("edge_cases", 0) < 7:
        weak_areas.append("ADD EDGE CASES: None, empty, boundary, Unicode, negative")
    if scores.get("error_handling", 0) < 7:
        weak_areas.append("ADD ERROR TESTS: invalid types, missing fields, pytest.raises()")
    if scores.get("assertions", 0) < 7:
        weak_areas.append("STRENGTHEN ASSERTIONS: 2+ per test, check type AND value")
    if scores.get("docs", 0) < 5:
        weak_areas.append("ADD DOCSTRINGS per test")
    if scores.get("isolation", 0) < 6:
        weak_areas.append("ADD FIXTURES: @pytest.fixture or mock externals")

    if mutations and mutations.get("surviving_mutants"):
        weak_areas.append(f"KILL {mutations.get('surviving_count', 0)} surviving mutations")

    return weak_areas[:5]


def _parse_to_files(raw: str, framework: str, language: str) -> List[Dict]:
    files = []
    code_blocks = re.findall(r'```(?:\w+)?\s*(.*?)```', raw, re.DOTALL)

    if code_blocks:
        for i, block in enumerate(code_blocks):
            block = block.strip()
            if not block or len(block) < 20:
                continue
            ext = ".py" if language == "python" else ".js" if language in ("javascript", "typescript") else ".go"
            files.append({
                "filename": f"test_generated_{i+1}{ext}" if i > 0 else f"test_generated{ext}",
                "framework": framework, "language": language, "code": block,
                "num_tests": block.count("def test_") + block.count("it(") + block.count("test("),
            })
    else:
        ext = ".py" if language == "python" else ".js"
        files.append({
            "filename": f"test_generated{ext}",
            "framework": framework, "language": language, "code": raw,
            "num_tests": raw.count("def test_") + raw.count("it(") + raw.count("test("),
        })

    return files