#!/usr/bin/env python3
import sys, os, threading, time, urllib.request

sys.path.insert(0, os.path.expanduser("~/addon-audit"))
import search_web

def start():
    try:
        srv = search_web.HTTPServer(('0.0.0.0', 8804), search_web.Handler)
        print("SERVER STARTED", flush=True)
        srv.serve_forever()
    except Exception as e:
        print(f"SERVER ERROR: {e}", flush=True)

t = threading.Thread(target=start, daemon=True)
t.start()
time.sleep(2)

try:
    r = urllib.request.urlopen('http://localhost:8804/', timeout=5)
    html = r.read().decode()
    print(f"OK status={r.status} len={len(html)}", flush=True)
    print(f"Has form: {'search-box' in html}")
    print(f"Has title: {'Ultimate Search' in html}")
    print(f"HTML preview: {html[:200]}")
except Exception as e:
    print(f"REQUEST FAILED: {e}", flush=True)
