File size: 3,182 Bytes
0ccacbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys
import json
import subprocess
import tempfile
from dotenv import load_dotenv

# Add src to sys.path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from src.providers import GeminiBrain

def main():
    load_dotenv()
    api_key = os.getenv("GOOGLE_API_KEY")
    if not api_key:
        print("Error: GOOGLE_API_KEY not found in .env")
        return

    brain = GeminiBrain(api_key)

    # Test Case 1: Pure Functions (calculator.py)
    print("\n--- Testing with calculator.py ---")
    with open("c:/Users/daksh/OneDrive/Desktop/Hackathons/gitlab-ai-hackathon/context-brain-test/calculator.py", "r") as f:
        calc_content = f.read()
    
    mr_data = {
        "title": "Add calculator logic",
        "diff": f"File: calculator.py\nDiff:\n+ {calc_content.replace(chr(10), chr(10)+'+')}"
    }
    
    test_code = brain.generate_tests(mr_data)
    print("Generated Test Code:")
    print("-" * 20)
    print(test_code)
    print("-" * 20)

    if "patch" in test_code.lower() or "mock" in test_code.lower():
        print("❌ FAIL: Generated code contains forbidden keywords (patch/mock)!")
    else:
        print("✅ SUCCESS: No forbidden keywords found.")

    # Test Case 2: Business Logic (business_logic.py)
    print("\n--- Testing with business_logic.py ---")
    with open("c:/Users/daksh/OneDrive/Desktop/Hackathons/gitlab-ai-hackathon/context-brain-test/business_logic.py", "r") as f:
        bl_content = f.read()
    
    mr_data_bl = {
        "title": "Fix billing logic",
        "diff": f"File: business_logic.py\nDiff:\n+ {bl_content.replace(chr(10), chr(10)+'+')}"
    }
    
    test_code_bl = brain.generate_tests(mr_data_bl)
    print("Generated Test Code:")
    print("-" * 20)
    print(test_code_bl)
    print("-" * 20)

    if "patch" in test_code_bl.lower() or "mock" in test_code_bl.lower():
        print("❌ FAIL: Generated code contains forbidden keywords (patch/mock)!")
    else:
        print("✅ SUCCESS: No forbidden keywords found.")

    # Run one of them if successful
    if "NO_TESTS_NEEDED" not in test_code and "patch" not in test_code.lower():
        fd, test_filename = tempfile.mkstemp(suffix=".py", prefix="test_verify_")
        try:
            with os.fdopen(fd, "w", encoding="utf-8") as f:
                f.write(test_code)
            
            print(f"\nRunning pytest on generated code: {test_filename}")
            env = os.environ.copy()
            psep = ";" if os.name == "nt" else ":"
            # Point PYTHONPATH to the directory containing the modules to be tested
            env["PYTHONPATH"] = f"{env.get('PYTHONPATH', '')}{psep}c:/Users/daksh/OneDrive/Desktop/Hackathons/gitlab-ai-hackathon/context-brain-test"
            
            result = subprocess.run(["pytest", test_filename, "-v"], capture_output=True, text=True, env=env)
            print(result.stdout)
            print(result.stderr)
            if result.returncode == 0:
                print("✅ Pytest passed!")
            else:
                print("❌ Pytest failed!")
        finally:
            os.remove(test_filename)

if __name__ == "__main__":
    main()