|
| 1 | +import subprocess |
| 2 | +import time |
| 3 | +import sys |
| 4 | +import os |
| 5 | + |
| 6 | + |
| 7 | +def run_integration_test(): |
| 8 | + print("🚀 Starting Integration Test for 'edit' tool...") |
| 9 | + |
| 10 | + # Ensure current directory is in PYTHONPATH for the subprocess |
| 11 | + env = os.environ.copy() |
| 12 | + env["PYTHONPATH"] = os.getcwd() + ":" + env.get("PYTHONPATH", "") |
| 13 | + |
| 14 | + # Create a test file |
| 15 | + test_filename = "test_edit_integration.txt" |
| 16 | + with open(test_filename, "w") as f: |
| 17 | + f.write("Line 1\nLine 2\nLine 3\n") |
| 18 | + |
| 19 | + # Start the mini-copilot process |
| 20 | + process = subprocess.Popen( |
| 21 | + [sys.executable, "-m", "mini_copilot.main"], |
| 22 | + stdin=subprocess.PIPE, |
| 23 | + stdout=subprocess.PIPE, |
| 24 | + stderr=subprocess.STDOUT, |
| 25 | + text=True, |
| 26 | + bufsize=1, |
| 27 | + cwd=os.getcwd(), |
| 28 | + env=env, |
| 29 | + ) |
| 30 | + |
| 31 | + try: |
| 32 | + # 1. Wait for prompt |
| 33 | + print("Waiting for prompt...") |
| 34 | + output = "" |
| 35 | + start_time = time.time() |
| 36 | + while time.time() - start_time < 30: |
| 37 | + char = process.stdout.read(1) |
| 38 | + if not char: |
| 39 | + break |
| 40 | + output += char |
| 41 | + if "> " in output: |
| 42 | + print("✅ Prompt detected.") |
| 43 | + break |
| 44 | + |
| 45 | + if "> " not in output: |
| 46 | + print(f"❌ Timed out waiting for prompt. Last output: {output}") |
| 47 | + return False |
| 48 | + |
| 49 | + # 2. Trigger tool |
| 50 | + print(f"Sending message to trigger 'edit' tool on {test_filename}...") |
| 51 | + edit_request = f"Edit the file {test_filename}. Change 'Line 2' to 'Line Two'.\n" |
| 52 | + process.stdin.write(edit_request) |
| 53 | + process.stdin.flush() |
| 54 | + |
| 55 | + # 3. Look for tool execution |
| 56 | + print("Monitoring for tool invocation and response...") |
| 57 | + found_tool = False |
| 58 | + found_success = False |
| 59 | + start_time = time.time() |
| 60 | + output = "" |
| 61 | + while time.time() - start_time < 60: |
| 62 | + char = process.stdout.read(1) |
| 63 | + if not char: |
| 64 | + break |
| 65 | + output += char |
| 66 | + sys.stdout.write(char) |
| 67 | + sys.stdout.flush() |
| 68 | + |
| 69 | + if f"Successfully edited {test_filename}" in output: |
| 70 | + found_tool = True |
| 71 | + |
| 72 | + # If the assistant replies and we found the tool message, we are good |
| 73 | + if found_tool and "> " in output: |
| 74 | + found_success = True |
| 75 | + break |
| 76 | + |
| 77 | + if found_tool: |
| 78 | + # Verify file content |
| 79 | + with open(test_filename, "r") as f: |
| 80 | + content = f.read() |
| 81 | + if "Line Two" in content and "Line 2" not in content: |
| 82 | + print(f"\n✅ File content verified: {test_filename}") |
| 83 | + print("\n🎉 Integration Test PASSED!") |
| 84 | + return True |
| 85 | + else: |
| 86 | + print(f"\n❌ File content mismatch. Content: {content}") |
| 87 | + return False |
| 88 | + else: |
| 89 | + print("\n❌ Integration Test FAILED (Tool not triggered or response not found).") |
| 90 | + return False |
| 91 | + |
| 92 | + finally: |
| 93 | + process.terminate() |
| 94 | + if os.path.exists(test_filename): |
| 95 | + os.remove(test_filename) |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + if run_integration_test(): |
| 100 | + sys.exit(0) |
| 101 | + else: |
| 102 | + sys.exit(1) |
0 commit comments