| 1 | #!/usr/bin/env python3 |
| 2 | """tests/e2e.py - viewport checks for the plugin_ports inspector. |
| 3 | |
| 4 | Serves the plugin through a tiny CGI-ish HTTP shim on a loopback port, opens it |
| 5 | with Playwright at 1440 / 1024 / 390 CSS pixels, and asserts there is no |
| 6 | horizontal overflow, no console error, that the listener table rendered from the |
| 7 | live kernel, and that the probe box answered. Skips cleanly (exit 0) when |
| 8 | Playwright is not installed, so the offline suite stays dependency-free. |
| 9 | |
| 10 | python3 tests/e2e.py # run if Playwright is available |
| 11 | python3 tests/e2e.py --strict # exit 2 when Playwright is missing |
| 12 | """ |
| 13 | |
| 14 | import argparse |
| 15 | import http.server |
| 16 | import os |
| 17 | import socketserver |
| 18 | import subprocess |
| 19 | import sys |
| 20 | import threading |
| 21 | |
| 22 | ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 23 | BIN = os.path.join(ROOT, "plugin_ports") |
| 24 | VIEWPORTS = [(1440, 900), (1024, 768), (390, 844)] |
| 25 | |
| 26 | |
| 27 | class Handler(http.server.BaseHTTPRequestHandler): |
| 28 | def do_GET(self): # noqa: N802 |
| 29 | from urllib.parse import urlparse |
| 30 | u = urlparse(self.path) |
| 31 | env = dict(os.environ) |
| 32 | env["QUERY_STRING"] = u.query |
| 33 | env["REQUEST_METHOD"] = "GET" |
| 34 | env.pop("BOT_PASSWORD", None) |
| 35 | out = subprocess.run([BIN], capture_output=True, env=env, timeout=30).stdout |
| 36 | sep = out.find(b"\r\n\r\n") |
| 37 | headers = out[:sep].decode("utf-8", "replace") if sep >= 0 else "" |
| 38 | body = out[sep + 4:] if sep >= 0 else out |
| 39 | ctype = "text/html; charset=utf-8" |
| 40 | for line in headers.splitlines(): |
| 41 | if line.lower().startswith("content-type:"): |
| 42 | ctype = line.split(":", 1)[1].strip() |
| 43 | self.send_response(200) |
| 44 | self.send_header("Content-Type", ctype) |
| 45 | self.send_header("Cache-Control", "no-store") |
| 46 | self.send_header("Content-Length", str(len(body))) |
| 47 | self.end_headers() |
| 48 | self.wfile.write(body) |
| 49 | |
| 50 | def log_message(self, *_a): |
| 51 | pass |
| 52 | |
| 53 | |
| 54 | def main(): |
| 55 | ap = argparse.ArgumentParser() |
| 56 | ap.add_argument("--strict", action="store_true") |
| 57 | args = ap.parse_args() |
| 58 | |
| 59 | if not os.path.exists(BIN): |
| 60 | print("plugin_ports is not built; run make first", file=sys.stderr) |
| 61 | return 1 |
| 62 | |
| 63 | try: |
| 64 | from playwright.sync_api import sync_playwright |
| 65 | except Exception: |
| 66 | msg = "playwright not installed - skipping viewport e2e" |
| 67 | if args.strict: |
| 68 | print(msg, file=sys.stderr) |
| 69 | return 2 |
| 70 | print(msg) |
| 71 | return 0 |
| 72 | |
| 73 | with socketserver.TCPServer(("127.0.0.1", 0), Handler) as srv: |
| 74 | port = srv.server_address[1] |
| 75 | threading.Thread(target=srv.serve_forever, daemon=True).start() |
| 76 | url = "http://127.0.0.1:%d/plugin_ports" % port |
| 77 | |
| 78 | failures = [] |
| 79 | with sync_playwright() as p: |
| 80 | browser = p.chromium.launch() |
| 81 | for (w, h) in VIEWPORTS: |
| 82 | page = browser.new_page(viewport={"width": w, "height": h}) |
| 83 | errors = [] |
| 84 | page.on("console", lambda m: errors.append(m.text) |
| 85 | if m.type == "error" else None) |
| 86 | page.goto(url, wait_until="load") |
| 87 | page.wait_for_timeout(1800) |
| 88 | |
| 89 | sw = page.evaluate("document.documentElement.scrollWidth") |
| 90 | iw = page.evaluate("window.innerWidth") |
| 91 | if sw > iw + 1: |
| 92 | failures.append("%dx%d horizontal overflow %d > %d" % (w, h, sw, iw)) |
| 93 | |
| 94 | total = page.evaluate( |
| 95 | "document.getElementById('kTotal').textContent") |
| 96 | if not total.strip().isdigit(): |
| 97 | failures.append("%dx%d total KPI did not render (%r)" |
| 98 | % (w, h, total)) |
| 99 | |
| 100 | rows = page.evaluate( |
| 101 | "document.querySelectorAll('#rows tr').length") |
| 102 | if rows < 1: |
| 103 | failures.append("%dx%d listener table is empty" % (w, h)) |
| 104 | |
| 105 | # The probe button must exist on at least one row. |
| 106 | probed = page.evaluate( |
| 107 | "document.querySelectorAll('#rows button[data-p]').length") |
| 108 | if probed < 1: |
| 109 | failures.append("%dx%d no probe button rendered" % (w, h)) |
| 110 | |
| 111 | if errors: |
| 112 | failures.append("%dx%d console errors: %s" % (w, h, errors[:3])) |
| 113 | |
| 114 | page.close() |
| 115 | browser.close() |
| 116 | |
| 117 | if failures: |
| 118 | print("FAIL") |
| 119 | for f in failures: |
| 120 | print(" " + f) |
| 121 | return 1 |
| 122 | |
| 123 | print("ok - %d viewports clean (no overflow, no console error, table rendered)" |
| 124 | % len(VIEWPORTS)) |
| 125 | return 0 |
| 126 | |
| 127 | |
| 128 | if __name__ == "__main__": |
| 129 | sys.exit(main()) |