﻿"""Real HTTP integration tests for workers endpoints"""
import requests
import subprocess
import time
import sys
import signal

def test_workers_http_integration():
    """Test workers endpoints via real HTTP"""
    # Start server
    server = subprocess.Popen(
        [sys.executable, "run.py"],
        stdout=subprocess.PIPE,
        stderr=subprocess.PIPE
    )
    
    time.sleep(5)  # Wait for server to start
    
    try:
        # Test workers HTML endpoint
        response = requests.get("http://localhost:8080/workers", timeout=5)
        assert response.status_code == 200, f"Workers HTML failed: {response.status_code}"
        assert len(response.text) > 1000, "Workers HTML too short"
        print(f"✅ Workers HTML: {response.status_code}")
        
        # Test workers API endpoint
        response = requests.get("http://localhost:8080/api/workers/", timeout=5)
        assert response.status_code == 200, f"Workers API failed: {response.status_code}"
        data = response.json()
        assert "workers" in data, "No workers in API response"
        print(f"✅ Workers API: {response.status_code}")
        
        # Test with role filter
        response = requests.get("http://localhost:8080/api/workers/?role=worker", timeout=5)
        assert response.status_code == 200, f"Role filter failed: {response.status_code}"
        print(f"✅ Role filter: {response.status_code}")
        
        print("All HTTP integration tests passed!")
        
    finally:
        # Clean shutdown
        server.terminate()
        server.wait(timeout=5)

if __name__ == "__main__":
    test_workers_http_integration()
