"""Loads a published WebGL2 site in headless Chrome and fails if the page reports an error. Pass the zip PackageForItch.py wrote, or a folder holding index.html. The site is served over HTTP from a temporary folder, as itch serves it, so a file missing from the zip shows up as a failed load. No arguments checks the newest zip in Installs. Needs Chrome or Edge, and Node 22 or later for its WebSocket. """ import argparse import functools import glob import http.server import json import os import socket import subprocess import tempfile import threading import time import zipfile REPO_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) CONSOLE_WATCHER = os.path.join(os.path.dirname(os.path.abspath(__file__)), "browsersmoke_console.js") BROWSER_PATHS = (r"C:\Program Files\Google\Chrome\Application\chrome.exe", r"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", r"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe") class SiteHandler(http.server.SimpleHTTPRequestHandler): extensions_map = {**http.server.SimpleHTTPRequestHandler.extensions_map, ".wasm": "application/wasm", ".js": "text/javascript"} def log_message(self, format, *args): pass def log_request(self, code="-", size="-"): self.server.requests.append((int(code), self.path)) def handle(self): # Chrome drops connections it no longer wants when the check ends. try: super().handle() except ConnectionError: pass def find_browser(): for path in BROWSER_PATHS: if os.path.isfile(path): return path raise SystemExit("Couldn't find Chrome or Edge.") def newest_zip(): zips = glob.glob(os.path.join(REPO_ROOT, "Installs", "*.zip")) if not zips: raise SystemExit("No zip in Installs. Pass a zip or a site folder.") return max(zips, key=os.path.getmtime) def free_port(): with socket.socket() as probe: probe.bind(("127.0.0.1", 0)) return probe.getsockname()[1] def serve(directory): server = http.server.ThreadingHTTPServer(("127.0.0.1", 0), functools.partial(SiteHandler, directory=directory)) server.requests = [] threading.Thread(target=server.serve_forever, daemon=True).start() return server def watch_page(url, profile, seconds): debug_port = free_port() browser = subprocess.Popen([find_browser(), "--headless=new", "--use-angle=swiftshader", "--enable-unsafe-swiftshader", "--no-first-run", f"--user-data-dir={profile}", f"--remote-debugging-port={debug_port}", "about:blank"], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) try: # The debugging port opens a moment after launch. time.sleep(2) watcher = subprocess.run(["node", CONSOLE_WATCHER, str(debug_port), url, str(seconds)], capture_output=True, text=True, timeout=seconds + 60) finally: subprocess.run(["taskkill", "/F", "/T", "/PID", str(browser.pid)], capture_output=True) browser.wait() return [json.loads(line) for line in watcher.stdout.splitlines() if line.startswith("{")] def check(site, seconds): with tempfile.TemporaryDirectory(ignore_cleanup_errors=True) as work: directory = site if os.path.isfile(site): directory = os.path.join(work, "site") with zipfile.ZipFile(site) as archive: archive.extractall(directory) server = serve(directory) try: events = watch_page(f"http://127.0.0.1:{server.server_address[1]}/index.html", os.path.join(work, "profile"), seconds) finally: server.shutdown() problems = [f"{event['kind']}: {event['text']}" for event in events if event["kind"] != "loaded" and "favicon" not in event["text"]] problems += [f"{code} {path}" for code, path in server.requests if code != 200 and "favicon" not in path] if not any(kind == "loaded" for kind in (event["kind"] for event in events)): problems.append("The page never finished loading.") if not any(path.endswith(".wasm") for _code, path in server.requests): problems.append("The page never asked for a .wasm file.") return problems def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("site", nargs="?", help="A published zip, or a folder with an index.html.") parser.add_argument("--seconds", type=int, default=15, help="How long the page runs before it is judged.") args = parser.parse_args() site = args.site or newest_zip() print(f"Checking {site}") problems = check(site, args.seconds) if problems: print("FAILED") for problem in problems: print(" " + problem) raise SystemExit(1) print("OK") if __name__ == "__main__": main()