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()