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