#!/usr/bin/env python3
"""
Axiom Zero — Real-World Physical Process Test Harness
Launches actual physical browser/client processes installed on this host (cURL, Python Requests, Headless Firefox)
against the local Axiom Zero detection intake engine to evaluate real physical hardware & HTTP client signatures.
"""

import os
import sys
import subprocess
import time
import json

SERVER_PORT = 8085
TELEMETRY_URL = f"http://127.0.0.1:{SERVER_PORT}/api/telemetry/submit"

def run_curl_test():
    print("[1/3] Launching REAL Physical cURL Process...")
    cmd = [
        "curl", "-s", "-X", "POST", TELEMETRY_URL,
        "-H", "Content-Type: application/json",
        "-H", "User-Agent: curl/7.88.1",
        "-d", json.dumps({
            "client_type": "cURL Physical Binary",
            "userAgent": "curl/7.88.1",
            "mathSinTail": 0.0, # Missing browser WebGL/FPU execution
            "hasWebDriver": False
        })
    ]
    res = subprocess.run(cmd, capture_output=True, text=True)
    print(f"   └─ Response: {res.stdout.strip()}")

def run_python_test():
    print("[2/3] Launching REAL Physical Python Requests Client...")
    import urllib.request
    req = urllib.request.Request(
        TELEMETRY_URL,
        data=json.dumps({
            "client_type": "Python urllib Client",
            "userAgent": "Python-urllib/3.12",
            "mathSinTail": 0.0,
            "hasWebDriver": False
        }).encode('utf-8'),
        headers={"Content-Type": "application/json", "User-Agent": "Python-urllib/3.12"}
    )
    try:
        with urllib.request.urlopen(req) as resp:
            print(f"   └─ Response: {resp.read().decode('utf-8').strip()}")
    except Exception as e:
        print(f"   └─ Response Exception: {e}")

def run_firefox_headless_test():
    print("[3/3] Launching REAL Physical Firefox Process (Headless)...")
    firefox_script = f"""
    const {{ exec }} = require('child_process');
    const http = require('http');

    // Launch Firefox headless against local endpoint
    const child = exec('firefox --headless --screenshot /tmp/firefox_test.png {TELEMETRY_URL}');
    setTimeout(() => {{
        child.kill();
        console.log("   └─ Physical Firefox Headless execution completed.");
    }}, 4000);
    """
    script_path = "/tmp/run_firefox_test.js"
    with open(script_path, "w") as f:
        f.write(firefox_script)

    res = subprocess.run(["node", script_path], capture_output=True, text=True)
    print(res.stdout.strip())

if __name__ == "__main__":
    print("======================================================================")
    print("  AXIOM ZERO — REAL-WORLD PHYSICAL PROCESS TEST HARNESS")
    print("======================================================================")
    print(f"[*] Connecting to local intake engine at: {TELEMETRY_URL}")
    
    run_curl_test()
    run_python_test()
    run_firefox_headless_test()
    print("======================================================================")
