| 1 | #!/usr/bin/env python3 |
| 2 | """tests/e2e.py - viewport checks for the plugin_crontab schedule 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 jobs table rendered from the |
| 7 | fixture crontab, and that the playground explained its default expression. |
| 8 | Skips cleanly (exit 0) when Playwright is not installed, so the offline suite |
| 9 | stays dependency-free. |
| 10 | |
| 11 | python3 tests/e2e.py # run if Playwright is available |
| 12 | python3 tests/e2e.py --strict # exit 2 when Playwright is missing |
| 13 | """ |
| 14 | |
| 15 | import argparse |
| 16 | import http.server |
| 17 | import os |
| 18 | import socketserver |
| 19 | import subprocess |
| 20 | import sys |
| 21 | import tempfile |
| 22 | import threading |
| 23 | |
| 24 | ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 25 | BIN = os.path.join(ROOT, "plugin_crontab") |
| 26 | VIEWPORTS = [(1440, 900), (1024, 768), (390, 844)] |
| 27 | |
| 28 | FIXTURE = """# fixture crontab for the viewport suite |
| 29 | SHELL=/bin/sh |
| 30 | */5 * * * * /usr/bin/backup.sh --flag |
| 31 | 0 3 * * * /usr/bin/logrotate |
| 32 | @hourly /usr/local/bin/heartbeat |
| 33 | 30 2 * * 0 /usr/local/bin/weekly |
| 34 | 0 0 1 * * /usr/local/bin/monthly-report |
| 35 | 0 9 * * mon-fri /usr/local/bin/standup |
| 36 | """ |
| 37 | |
| 38 | |
| 39 | class Handler(http.server.BaseHTTPRequestHandler): |
| 40 | def do_GET(self): # noqa: N802 |
| 41 | from urllib.parse import urlparse |
| 42 | u = urlparse(self.path) |
| 43 | env = dict(os.environ) |
| 44 | env["QUERY_STRING"] = u.query |
| 45 | env["REQUEST_METHOD"] = "GET" |
| 46 | env.pop("BOT_PASSWORD", None) |
| 47 | out = subprocess.run([BIN], capture_output=True, env=env, timeout=30).stdout |
| 48 | sep = out.find(b"\r\n\r\n") |
| 49 | headers = out[:sep].decode("utf-8", "replace") if sep >= 0 else "" |
| 50 | body = out[sep + 4:] if sep >= 0 else out |
| 51 | ctype = "text/html; charset=utf-8" |
| 52 | for line in headers.splitlines(): |
| 53 | if line.lower().startswith("content-type:"): |
| 54 | ctype = line.split(":", 1)[1].strip() |
| 55 | self.send_response(200) |
| 56 | self.send_header("Content-Type", ctype) |
| 57 | self.send_header("Cache-Control", "no-store") |
| 58 | self.send_header("Content-Length", str(len(body))) |
| 59 | self.end_headers() |
| 60 | self.wfile.write(body) |
| 61 | |
| 62 | def log_message(self, *_a): |
| 63 | pass |
| 64 | |
| 65 | |
| 66 | def main(): |
| 67 | ap = argparse.ArgumentParser() |
| 68 | ap.add_argument("--strict", action="store_true") |
| 69 | args = ap.parse_args() |
| 70 | |
| 71 | if not os.path.exists(BIN): |
| 72 | print("plugin_crontab is not built; run make first", file=sys.stderr) |
| 73 | return 1 |
| 74 | |
| 75 | try: |
| 76 | from playwright.sync_api import sync_playwright |
| 77 | except Exception: |
| 78 | msg = "playwright not installed - skipping viewport e2e" |
| 79 | if args.strict: |
| 80 | print(msg, file=sys.stderr) |
| 81 | return 2 |
| 82 | print(msg) |
| 83 | return 0 |
| 84 | |
| 85 | fixture = tempfile.NamedTemporaryFile("w", suffix=".crontab", delete=False) |
| 86 | fixture.write(FIXTURE) |
| 87 | fixture.close() |
| 88 | os.environ["CRONTAB_FILE"] = fixture.name |
| 89 | |
| 90 | with socketserver.TCPServer(("127.0.0.1", 0), Handler) as srv: |
| 91 | port = srv.server_address[1] |
| 92 | threading.Thread(target=srv.serve_forever, daemon=True).start() |
| 93 | url = "http://127.0.0.1:%d/plugin_crontab" % port |
| 94 | |
| 95 | failures = [] |
| 96 | with sync_playwright() as p: |
| 97 | browser = p.chromium.launch() |
| 98 | for (w, h) in VIEWPORTS: |
| 99 | page = browser.new_page(viewport={"width": w, "height": h}) |
| 100 | errors = [] |
| 101 | page.on("console", lambda m: errors.append(m.text) |
| 102 | if m.type == "error" else None) |
| 103 | page.goto(url, wait_until="load") |
| 104 | page.wait_for_timeout(2200) |
| 105 | |
| 106 | sw = page.evaluate("document.documentElement.scrollWidth") |
| 107 | iw = page.evaluate("window.innerWidth") |
| 108 | if sw > iw + 1: |
| 109 | failures.append("%dx%d horizontal overflow %d > %d" % (w, h, sw, iw)) |
| 110 | |
| 111 | rows = page.evaluate("document.querySelectorAll('#rows tr').length") |
| 112 | if rows < 5: |
| 113 | failures.append("%dx%d job table only has %d rows" % (w, h, rows)) |
| 114 | |
| 115 | total = page.evaluate("document.getElementById('k-total').textContent") |
| 116 | if (total or "").strip() in ("", "\u2014"): |
| 117 | failures.append("%dx%d job count KPI did not render (%r)" % (w, h, total)) |
| 118 | |
| 119 | runs = page.evaluate("document.querySelectorAll('#exRuns .run').length") |
| 120 | if runs != 5: |
| 121 | failures.append("%dx%d playground showed %d runs" % (w, h, runs)) |
| 122 | |
| 123 | note = page.evaluate("document.getElementById('exNote').textContent") |
| 124 | if "invalid" in (note or "").lower() or "failed" in (note or "").lower(): |
| 125 | failures.append("%dx%d playground failed: %r" % (w, h, note)) |
| 126 | |
| 127 | real = [e for e in errors if "favicon" not in e.lower()] |
| 128 | if real: |
| 129 | failures.append("%dx%d console errors: %s" % (w, h, real[:3])) |
| 130 | |
| 131 | print(" %dx%d sw=%d iw=%d rows=%d total=%s runs=%d" |
| 132 | % (w, h, sw, iw, rows, total, runs)) |
| 133 | page.close() |
| 134 | browser.close() |
| 135 | srv.shutdown() |
| 136 | |
| 137 | os.unlink(fixture.name) |
| 138 | |
| 139 | if failures: |
| 140 | for f in failures: |
| 141 | print(" FAIL " + f) |
| 142 | return 1 |
| 143 | print(" viewport e2e passed at %d sizes" % len(VIEWPORTS)) |
| 144 | return 0 |
| 145 | |
| 146 | |
| 147 | if __name__ == "__main__": |
| 148 | sys.exit(main()) |