#!/usr/bin/env python3 """ Test client for Bzzz Chat API integration This script simulates the N8N workflow calling the Bzzz API """ import json import requests import time import sys # API endpoint API_URL = "http://localhost:8080/bzzz/api" def test_health_check(): """Test the health check endpoint""" print("๐Ÿ” Testing health check endpoint...") try: response = requests.get(f"{API_URL}/health", timeout=5) if response.status_code == 200: print("โœ… Health check passed:", response.json()) return True else: print(f"โŒ Health check failed: {response.status_code}") return False except Exception as e: print(f"โŒ Health check error: {e}") return False def create_test_task(): """Create a simple test task""" return { "method": "execute_task_in_sandbox", "task": { "task_id": 9999, "number": 9999, "title": "Chat API Test Task", "description": "Create a simple Python hello world function and save it to hello.py", "repository": { "owner": "test", "repository": "chat-test" }, "git_url": "", # No git repo for simple test "task_type": "development", "priority": "medium", "requirements": [], "deliverables": ["hello.py with hello_world() function"], "context": "This is a test task from the chat API integration" }, "execution_options": { "sandbox_image": "registry.home.deepblack.cloud/tony/bzzz-sandbox:latest", "timeout": "300s", "max_iterations": 5, "return_full_log": True, "cleanup_on_complete": True }, "callback": { "webhook_url": "http://localhost:8080/test-callback", "include_artifacts": True } } def test_task_execution(): """Test task execution endpoint""" print("\n๐Ÿš€ Testing task execution...") task_request = create_test_task() try: print("๐Ÿ“ค Sending task request...") print(f"Task: {task_request['task']['description']}") response = requests.post( f"{API_URL}/execute-task", json=task_request, headers={"Content-Type": "application/json"}, timeout=30 ) if response.status_code == 200: result = response.json() print("โœ… Task accepted:", result) print(f" Task ID: {result.get('task_id')}") print(f" Status: {result.get('status')}") print(f" Message: {result.get('message')}") return True else: print(f"โŒ Task execution failed: {response.status_code}") print(f" Response: {response.text}") return False except Exception as e: print(f"โŒ Task execution error: {e}") return False def create_complex_task(): """Create a more complex test task""" return { "method": "execute_task_in_sandbox", "task": { "task_id": 9998, "number": 9998, "title": "Complex Chat API Test", "description": "Create a Python script that implements a simple calculator with add, subtract, multiply, and divide functions. Include basic error handling and save to calculator.py", "repository": { "owner": "test", "repository": "calculator-test" }, "git_url": "", "task_type": "development", "priority": "high", "requirements": [ "Python functions for basic math operations", "Error handling for division by zero", "Simple command-line interface" ], "deliverables": ["calculator.py with Calculator class"], "context": "Complex test task to validate full execution pipeline" }, "execution_options": { "sandbox_image": "registry.home.deepblack.cloud/tony/bzzz-sandbox:latest", "timeout": "600s", "max_iterations": 10, "return_full_log": True, "cleanup_on_complete": False # Keep sandbox for inspection }, "callback": { "webhook_url": "http://localhost:8080/test-callback", "include_artifacts": True } } def test_complex_execution(): """Test complex task execution""" print("\n๐Ÿง  Testing complex task execution...") task_request = create_complex_task() try: print("๐Ÿ“ค Sending complex task request...") print(f"Task: {task_request['task']['description']}") response = requests.post( f"{API_URL}/execute-task", json=task_request, headers={"Content-Type": "application/json"}, timeout=30 ) if response.status_code == 200: result = response.json() print("โœ… Complex task accepted:", result) return True else: print(f"โŒ Complex task failed: {response.status_code}") print(f" Response: {response.text}") return False except Exception as e: print(f"โŒ Complex task error: {e}") return False def main(): """Run all tests""" print("๐Ÿงช Bzzz Chat API Test Suite") print("=" * 40) # Test health check if not test_health_check(): print("โŒ Health check failed, is the server running?") print(" Start with: ./test/run_chat_api.sh") sys.exit(1) # Test simple task execution if not test_task_execution(): print("โŒ Simple task execution failed") sys.exit(1) # Test complex task execution if not test_complex_execution(): print("โŒ Complex task execution failed") sys.exit(1) print("\nโœ… All tests passed!") print("\n๐Ÿ“‹ Next steps:") print("1. Import the N8N workflow from chat-to-code-integration.json") print("2. Configure webhook URLs to point to your N8N instance") print("3. Test with actual chat interface") print("4. Monitor execution logs in ./data/chat-api-logs/") print("\n๐Ÿ’ฌ Example chat messages to try:") print(' "Create a simple hello world function in Python"') print(' "Task: Build a REST API endpoint\\nRepo: https://github.com/myorg/api.git\\nLanguage: Python"') print(' "Fix the memory leak in the session handler"') if __name__ == "__main__": main()