#!/usr/bin/env python3 """tests/e2e.py - viewport checks for the plugin_crontab schedule inspector. Serves the plugin through a tiny CGI-ish HTTP shim on a loopback port, opens it with Playwright at 1440 / 1024 / 390 CSS pixels, and asserts there is no horizontal overflow, no console error, that the jobs table rendered from the fixture crontab, and that the playground explained its default expression. Skips cleanly (exit 0) when Playwright is not installed, so the offline suite stays dependency-free. python3 tests/e2e.py # run if Playwright is available python3 tests/e2e.py --strict # exit 2 when Playwright is missing """ import argparse import http.server import os import socketserver import subprocess import sys import tempfile import threading ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BIN = os.path.join(ROOT, "plugin_crontab") VIEWPORTS = [(1440, 900), (1024, 768), (390, 844)] FIXTURE = """# fixture crontab for the viewport suite SHELL=/bin/sh */5 * * * * /usr/bin/backup.sh --flag 0 3 * * * /usr/bin/logrotate @hourly /usr/local/bin/heartbeat 30 2 * * 0 /usr/local/bin/weekly 0 0 1 * * /usr/local/bin/monthly-report 0 9 * * mon-fri /usr/local/bin/standup """ class Handler(http.server.BaseHTTPRequestHandler): def do_GET(self): # noqa: N802 from urllib.parse import urlparse u = urlparse(self.path) env = dict(os.environ) env["QUERY_STRING"] = u.query env["REQUEST_METHOD"] = "GET" env.pop("BOT_PASSWORD", None) out = subprocess.run([BIN], capture_output=True, env=env, timeout=30).stdout sep = out.find(b"\r\n\r\n") headers = out[:sep].decode("utf-8", "replace") if sep >= 0 else "" body = out[sep + 4:] if sep >= 0 else out ctype = "text/html; charset=utf-8" for line in headers.splitlines(): if line.lower().startswith("content-type:"): ctype = line.split(":", 1)[1].strip() self.send_response(200) self.send_header("Content-Type", ctype) self.send_header("Cache-Control", "no-store") self.send_header("Content-Length", str(len(body))) self.end_headers() self.wfile.write(body) def log_message(self, *_a): pass def main(): ap = argparse.ArgumentParser() ap.add_argument("--strict", action="store_true") args = ap.parse_args() if not os.path.exists(BIN): print("plugin_crontab is not built; run make first", file=sys.stderr) return 1 try: from playwright.sync_api import sync_playwright except Exception: msg = "playwright not installed - skipping viewport e2e" if args.strict: print(msg, file=sys.stderr) return 2 print(msg) return 0 fixture = tempfile.NamedTemporaryFile("w", suffix=".crontab", delete=False) fixture.write(FIXTURE) fixture.close() os.environ["CRONTAB_FILE"] = fixture.name with socketserver.TCPServer(("127.0.0.1", 0), Handler) as srv: port = srv.server_address[1] threading.Thread(target=srv.serve_forever, daemon=True).start() url = "http://127.0.0.1:%d/plugin_crontab" % port failures = [] with sync_playwright() as p: browser = p.chromium.launch() for (w, h) in VIEWPORTS: page = browser.new_page(viewport={"width": w, "height": h}) errors = [] page.on("console", lambda m: errors.append(m.text) if m.type == "error" else None) page.goto(url, wait_until="load") page.wait_for_timeout(2200) sw = page.evaluate("document.documentElement.scrollWidth") iw = page.evaluate("window.innerWidth") if sw > iw + 1: failures.append("%dx%d horizontal overflow %d > %d" % (w, h, sw, iw)) rows = page.evaluate("document.querySelectorAll('#rows tr').length") if rows < 5: failures.append("%dx%d job table only has %d rows" % (w, h, rows)) total = page.evaluate("document.getElementById('k-total').textContent") if (total or "").strip() in ("", "\u2014"): failures.append("%dx%d job count KPI did not render (%r)" % (w, h, total)) runs = page.evaluate("document.querySelectorAll('#exRuns .run').length") if runs != 5: failures.append("%dx%d playground showed %d runs" % (w, h, runs)) note = page.evaluate("document.getElementById('exNote').textContent") if "invalid" in (note or "").lower() or "failed" in (note or "").lower(): failures.append("%dx%d playground failed: %r" % (w, h, note)) real = [e for e in errors if "favicon" not in e.lower()] if real: failures.append("%dx%d console errors: %s" % (w, h, real[:3])) print(" %dx%d sw=%d iw=%d rows=%d total=%s runs=%d" % (w, h, sw, iw, rows, total, runs)) page.close() browser.close() srv.shutdown() os.unlink(fixture.name) if failures: for f in failures: print(" FAIL " + f) return 1 print(" viewport e2e passed at %d sizes" % len(VIEWPORTS)) return 0 if __name__ == "__main__": sys.exit(main())