| 1 | #!/usr/bin/env python3 |
| 2 | """tests/e2e.py - viewport checks for the plugin_disks 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 mount table and the folder |
| 7 | breakdown both rendered real numbers. Skips cleanly (exit 0) when Playwright is |
| 8 | not installed, so `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 os |
| 17 | import subprocess |
| 18 | import socketserver |
| 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_disks") |
| 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=60).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 | # no Content-Length from the plugin; the shim sets its own framing |
| 47 | self.send_header("Content-Length", str(len(body))) |
| 48 | self.end_headers() |
| 49 | self.wfile.write(body) |
| 50 | |
| 51 | def log_message(self, *_a): |
| 52 | pass |
| 53 | |
| 54 | |
| 55 | def main(): |
| 56 | ap = argparse.ArgumentParser() |
| 57 | ap.add_argument("--strict", action="store_true") |
| 58 | args = ap.parse_args() |
| 59 | |
| 60 | if not os.path.exists(BIN): |
| 61 | print("plugin_disks is not built; run make first", file=sys.stderr) |
| 62 | return 1 |
| 63 | |
| 64 | try: |
| 65 | from playwright.sync_api import sync_playwright |
| 66 | except Exception: |
| 67 | msg = "playwright not installed - skipping viewport e2e" |
| 68 | if args.strict: |
| 69 | print(msg, file=sys.stderr) |
| 70 | return 2 |
| 71 | print(msg) |
| 72 | return 0 |
| 73 | |
| 74 | with socketserver.TCPServer(("127.0.0.1", 0), Handler) as srv: |
| 75 | port = srv.server_address[1] |
| 76 | threading.Thread(target=srv.serve_forever, daemon=True).start() |
| 77 | url = "http://127.0.0.1:%d/plugin_disks" % port |
| 78 | |
| 79 | failures = [] |
| 80 | with sync_playwright() as p: |
| 81 | browser = p.chromium.launch() |
| 82 | for (w, h) in VIEWPORTS: |
| 83 | page = browser.new_page(viewport={"width": w, "height": h}) |
| 84 | errors = [] |
| 85 | page.on("console", lambda m: errors.append(m.text) |
| 86 | if m.type == "error" else None) |
| 87 | page.goto(url, wait_until="load") |
| 88 | page.wait_for_timeout(3000) |
| 89 | |
| 90 | sw = page.evaluate("document.documentElement.scrollWidth") |
| 91 | iw = page.evaluate("window.innerWidth") |
| 92 | if sw > iw + 1: |
| 93 | failures.append("%dx%d horizontal overflow %d > %d" % (w, h, sw, iw)) |
| 94 | |
| 95 | count = page.evaluate( |
| 96 | "document.getElementById('kCount').textContent") |
| 97 | full = page.evaluate( |
| 98 | "document.getElementById('kFull').textContent") |
| 99 | mounts = page.evaluate( |
| 100 | "document.querySelectorAll('#mounts .mnt').length") |
| 101 | rows = page.evaluate( |
| 102 | "document.querySelectorAll('#rows tr').length") |
| 103 | note = page.evaluate( |
| 104 | "document.getElementById('dirNote').textContent") |
| 105 | |
| 106 | if not (count or "").strip().isdigit(): |
| 107 | failures.append("%dx%d mount count KPI did not render (%r)" |
| 108 | % (w, h, count)) |
| 109 | if mounts < 1: |
| 110 | failures.append("%dx%d no mount cards rendered" % (w, h)) |
| 111 | if "%" not in (full or ""): |
| 112 | failures.append("%dx%d fullness KPI did not render (%r)" |
| 113 | % (w, h, full)) |
| 114 | if not (note or "").strip(): |
| 115 | failures.append("%dx%d folder note empty (%r)" % (w, h, note)) |
| 116 | |
| 117 | real = [e for e in errors if "favicon" not in e.lower()] |
| 118 | if real: |
| 119 | failures.append("%dx%d console errors: %s" % (w, h, real[:3])) |
| 120 | |
| 121 | print(" %dx%d scrollWidth=%d innerWidth=%d mounts=%d rows=%d count=%s full=%s" |
| 122 | % (w, h, sw, iw, mounts, rows, (count or "").strip(), |
| 123 | (full or "").strip())) |
| 124 | page.close() |
| 125 | browser.close() |
| 126 | srv.shutdown() |
| 127 | |
| 128 | if failures: |
| 129 | for f in failures: |
| 130 | print(" FAIL " + f) |
| 131 | return 1 |
| 132 | print(" viewport e2e passed at %d sizes" % len(VIEWPORTS)) |
| 133 | return 0 |
| 134 | |
| 135 | |
| 136 | if __name__ == "__main__": |
| 137 | sys.exit(main()) |