inferno-ai 0.10.3

Enterprise AI/ML model runner with automatic updates, real-time monitoring, and multi-interface support
#!/usr/bin/env python3
"""
Test script to validate the HTTP server functionality.
This tests the server endpoints and API integration.
"""

import requests
import json
import time
import subprocess
import sys
import os
import signal
from urllib.parse import urljoin

class InfernoServerTester:
    def __init__(self, base_url="http://127.0.0.1:8080"):
        self.base_url = base_url
        self.server_process = None

    def start_server(self):
        """Start the Inferno server in the background"""
        print("🚀 Starting Inferno server...")
        try:
            # Try to start the server
            self.server_process = subprocess.Popen(
                ["cargo", "run", "--", "serve", "--bind", "127.0.0.1:8080"],
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                cwd="/Users/ryanrobson/git/inferno"
            )

            # Wait a bit for the server to start
            time.sleep(5)

            # Check if the process is still running
            if self.server_process.poll() is None:
                print("✅ Server started successfully")
                return True
            else:
                print("❌ Server failed to start")
                stdout, stderr = self.server_process.communicate()
                print(f"STDOUT: {stdout.decode()}")
                print(f"STDERR: {stderr.decode()}")
                return False

        except Exception as e:
            print(f"❌ Failed to start server: {e}")
            return False

    def stop_server(self):
        """Stop the Inferno server"""
        if self.server_process:
            print("🛑 Stopping server...")
            self.server_process.terminate()
            try:
                self.server_process.wait(timeout=5)
            except subprocess.TimeoutExpired:
                self.server_process.kill()
                self.server_process.wait()
            print("✅ Server stopped")

    def test_endpoint(self, endpoint, method="GET", data=None, expected_status=200):
        """Test a specific endpoint"""
        url = urljoin(self.base_url, endpoint)

        try:
            if method == "GET":
                response = requests.get(url, timeout=10)
            elif method == "POST":
                response = requests.post(url, json=data, timeout=10)
            else:
                raise ValueError(f"Unsupported method: {method}")

            if response.status_code == expected_status:
                print(f"{method} {endpoint} - Status: {response.status_code}")
                return True, response
            else:
                print(f"{method} {endpoint} - Expected: {expected_status}, Got: {response.status_code}")
                return False, response

        except requests.exceptions.RequestException as e:
            print(f"{method} {endpoint} - Connection error: {e}")
            return False, None

    def test_health_check(self):
        """Test the health check endpoint"""
        print("\n🏥 Testing health check endpoint...")
        success, response = self.test_endpoint("/health")
        if success and response:
            try:
                print(f"  Response: {response.text}")
                return True
            except:
                pass
        return success

    def test_root_endpoint(self):
        """Test the root endpoint"""
        print("\n🏠 Testing root endpoint...")
        success, response = self.test_endpoint("/")
        if success and response:
            try:
                print(f"  Response: {response.text[:200]}...")
                return True
            except:
                pass
        return success

    def test_metrics_endpoints(self):
        """Test metrics endpoints"""
        print("\n📊 Testing metrics endpoints...")

        endpoints = [
            "/metrics",
            "/metrics/json",
            "/metrics/snapshot"
        ]

        results = []
        for endpoint in endpoints:
            success, response = self.test_endpoint(endpoint)
            results.append(success)
            if success and response:
                content_type = response.headers.get('content-type', '')
                print(f"  Content-Type: {content_type}")

        return all(results)

    def test_openai_endpoints(self):
        """Test OpenAI-compatible API endpoints"""
        print("\n🤖 Testing OpenAI-compatible endpoints...")

        # Test models endpoint
        success, response = self.test_endpoint("/v1/models")
        if not success:
            return False

        if response:
            try:
                models_data = response.json()
                print(f"  Models available: {len(models_data.get('data', []))}")
            except:
                print("  Models endpoint returned non-JSON response")

        # Test chat completions (this might fail without a loaded model)
        chat_request = {
            "model": "test-model",
            "messages": [{"role": "user", "content": "Hello!"}],
            "max_tokens": 10
        }

        print("  Testing chat completions endpoint...")
        success, response = self.test_endpoint("/v1/chat/completions", "POST", chat_request, expected_status=None)
        if response:
            print(f"    Status: {response.status_code}")
            if response.status_code in [200, 400, 404, 500]:  # Any reasonable response is ok for now
                return True

        return False

    def test_websocket_endpoint(self):
        """Test WebSocket endpoint availability"""
        print("\n🔗 Testing WebSocket endpoint...")
        # For WebSocket, we'll just check if the endpoint exists (returns proper error)
        success, response = self.test_endpoint("/ws/stream", expected_status=None)
        if response:
            # WebSocket endpoints often return 400 or 426 for regular HTTP requests
            if response.status_code in [400, 426, 404]:
                print(f"✅ WebSocket endpoint exists (Status: {response.status_code})")
                return True
        return False

    def run_tests(self):
        """Run all server functionality tests"""
        print("🔥 Testing Inferno HTTP Server Functionality")
        print("=" * 50)

        results = []

        # Test if server is accessible
        print("\n📡 Testing server connectivity...")
        try:
            response = requests.get(self.base_url, timeout=5)
            print(f"✅ Server is accessible at {self.base_url}")
        except requests.exceptions.RequestException:
            print(f"❌ Server is not accessible at {self.base_url}")
            print("   Make sure to start the server first with: cargo run -- serve")
            return False

        # Run individual tests
        results.append(self.test_health_check())
        results.append(self.test_root_endpoint())
        results.append(self.test_metrics_endpoints())
        results.append(self.test_openai_endpoints())
        results.append(self.test_websocket_endpoint())

        # Summary
        print("\n" + "=" * 50)
        print("📊 Test Results Summary:")
        passed = sum(results)
        total = len(results)
        print(f"  ✅ Passed: {passed}/{total}")
        print(f"  ❌ Failed: {total - passed}/{total}")

        if passed == total:
            print("🎉 All tests passed! Server is working correctly.")
        else:
            print("⚠️  Some tests failed. Check the output above for details.")

        return passed == total

def test_server_configuration():
    """Test server configuration and setup"""
    print("🔧 Testing server configuration...")

    # Check if serve.rs has the expected endpoints
    serve_file = "/Users/ryanrobson/git/inferno/src/cli/serve.rs"
    if os.path.exists(serve_file):
        with open(serve_file, 'r') as f:
            content = f.read()

        endpoints = [
            "/health",
            "/metrics",
            "/v1/models",
            "/v1/chat/completions",
            "/ws/stream"
        ]

        for endpoint in endpoints:
            if endpoint in content:
                print(f"{endpoint} endpoint configured")
            else:
                print(f"{endpoint} endpoint missing")

        # Check for important middleware
        middleware = ["CorsLayer", "TraceLayer"]
        for mw in middleware:
            if mw in content:
                print(f"{mw} middleware configured")
            else:
                print(f"{mw} middleware missing")

        print("  ✅ Server configuration looks good")
        return True
    else:
        print("  ❌ serve.rs file not found")
        return False

def main():
    """Main test function"""
    print("🔥 Inferno HTTP Server Functionality Test")
    print("=" * 45)

    # Test configuration first
    test_server_configuration()

    # Check if user wants to test with a running server
    print("\n" + "=" * 45)
    print("🚀 To test the live server functionality:")
    print("1. Start the server in another terminal:")
    print("   cd /Users/ryanrobson/git/inferno")
    print("   cargo run -- serve")
    print("2. Then run this test script again with --live flag")
    print("   python3 test_server_functionality.py --live")

    if len(sys.argv) > 1 and sys.argv[1] == "--live":
        print("\n📡 Testing live server...")
        tester = InfernoServerTester()
        return tester.run_tests()
    else:
        print("\n✅ Configuration test completed.")
        print("📋 Server endpoints that should be available:")
        print("  • GET  /health - Health check")
        print("  • GET  / - Server information")
        print("  • GET  /metrics - Prometheus metrics")
        print("  • GET  /v1/models - List models (OpenAI compatible)")
        print("  • POST /v1/chat/completions - Chat API (OpenAI compatible)")
        print("  • GET  /ws/stream - WebSocket streaming")
        return True

if __name__ == "__main__":
    success = main()
    sys.exit(0 if success else 1)