| 1 | #!/usr/bin/env python3 |
| 2 | """tests/e2e.py - viewport + rendering checks for the plugin_nvtop 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: |
| 6 | |
| 7 | * no horizontal overflow, |
| 8 | * no console errors, |
| 9 | * the overview KPIs rendered numbers, |
| 10 | * the per-GPU cards rendered (mock 4-GPU fixture), each with two sparklines, |
| 11 | * the compute process table rendered rows. |
| 12 | |
| 13 | The fixture is injected through NVTOP_MOCK_CSV so the checks are deterministic |
| 14 | on any host, GPU or not. Skips cleanly (exit 0) when Playwright is missing, so |
| 15 | `make test` stays offline-only. |
| 16 | |
| 17 | python3 tests/e2e.py # run if Playwright is available |
| 18 | python3 tests/e2e.py --strict # exit 2 when Playwright is missing |
| 19 | """ |
| 20 | |
| 21 | import argparse |
| 22 | import http.server |
| 23 | import os |
| 24 | import subprocess |
| 25 | import socketserver |
| 26 | import sys |
| 27 | import threading |
| 28 | |
| 29 | ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) |
| 30 | BIN = os.path.join(ROOT, "plugin_nvtop") |
| 31 | FIXTURE = os.path.join(ROOT, "tests", "fixtures", "mock-4gpu.csv") |
| 32 | VIEWPORTS = [(1440, 900), (1024, 768), (390, 844)] |
| 33 | |
| 34 | |
| 35 | class Handler(http.server.BaseHTTPRequestHandler): |
| 36 | def do_GET(self): # noqa: N802 |
| 37 | from urllib.parse import urlparse |
| 38 | u = urlparse(self.path) |
| 39 | env = dict(os.environ) |
| 40 | env["QUERY_STRING"] = u.query |
| 41 | env["REQUEST_METHOD"] = "GET" |
| 42 | env.pop("BOT_PASSWORD", None) |
| 43 | env["NVTOP_MOCK_CSV"] = FIXTURE |
| 44 | out = subprocess.run([BIN], capture_output=True, env=env, timeout=60).stdout |
| 45 | sep = out.find(b"\r\n\r\n") |
| 46 | headers = out[:sep].decode("utf-8", "replace") if sep >= 0 else "" |
| 47 | body = out[sep + 4:] if sep >= 0 else out |
| 48 | ctype = "text/html; charset=utf-8" |
| 49 | for line in headers.splitlines(): |
| 50 | if line.lower().startswith("content-type:"): |
| 51 | ctype = line.split(":", 1)[1].strip() |
| 52 | self.send_response(200) |
| 53 | self.send_header("Content-Type", ctype) |
| 54 | self.send_header("Cache-Control", "no-store") |
| 55 | # no Content-Length from the plugin; the shim sets its own framing |
| 56 | self.send_header("Content-Length", str(len(body))) |
| 57 | self.end_headers() |
| 58 | self.wfile.write(body) |
| 59 | |
| 60 | def log_message(self, *_a): |
| 61 | pass |
| 62 | |
| 63 | |
| 64 | def main(): |
| 65 | ap = argparse.ArgumentParser() |
| 66 | ap.add_argument("--strict", action="store_true") |
| 67 | args = ap.parse_args() |
| 68 | |
| 69 | if not os.path.exists(BIN): |
| 70 | print("plugin_nvtop is not built; run make first", file=sys.stderr) |
| 71 | return 1 |
| 72 | |
| 73 | try: |
| 74 | from playwright.sync_api import sync_playwright |
| 75 | except Exception: |
| 76 | msg = "playwright not installed - skipping viewport e2e" |
| 77 | if args.strict: |
| 78 | print(msg, file=sys.stderr) |
| 79 | return 2 |
| 80 | print(msg) |
| 81 | return 0 |
| 82 | |
| 83 | with socketserver.TCPServer(("127.0.0.1", 0), Handler) as srv: |
| 84 | port = srv.server_address[1] |
| 85 | threading.Thread(target=srv.serve_forever, daemon=True).start() |
| 86 | url = "http://127.0.0.1:%d/plugin_nvtop" % port |
| 87 | |
| 88 | failures = [] |
| 89 | with sync_playwright() as p: |
| 90 | browser = p.chromium.launch() |
| 91 | for (w, h) in VIEWPORTS: |
| 92 | page = browser.new_page(viewport={"width": w, "height": h}) |
| 93 | errors = [] |
| 94 | page.on("console", lambda m: errors.append(m.text) |
| 95 | if m.type == "error" else None) |
| 96 | page.goto(url, wait_until="load") |
| 97 | page.wait_for_timeout(3200) |
| 98 | |
| 99 | sw = page.evaluate("document.documentElement.scrollWidth") |
| 100 | iw = page.evaluate("window.innerWidth") |
| 101 | if sw > iw + 1: |
| 102 | failures.append("%dx%d horizontal overflow %d > %d" % (w, h, sw, iw)) |
| 103 | |
| 104 | kpis = page.evaluate( |
| 105 | "['kGpus','kUtil','kMem','kProcs'].map(function(i){" |
| 106 | "var e=document.getElementById(i);" |
| 107 | "return e?e.textContent.trim():'';})") |
| 108 | cards = page.evaluate( |
| 109 | "document.querySelectorAll('#gpuGrid .gpu').length") |
| 110 | sparks = page.evaluate( |
| 111 | "document.querySelectorAll('#gpuGrid svg.spark').length") |
| 112 | rows = page.evaluate( |
| 113 | "document.querySelectorAll('#procRows tr').length") |
| 114 | chips = page.evaluate( |
| 115 | "document.querySelectorAll('#gpuGrid .chip').length") |
| 116 | empty_hidden = page.evaluate( |
| 117 | "document.getElementById('emptyState')" |
| 118 | ".classList.contains('hide')") |
| 119 | |
| 120 | if kpis[0] != "4": |
| 121 | failures.append("%dx%d GPU KPI did not render 4 (%r)" |
| 122 | % (w, h, kpis[0])) |
| 123 | if not kpis[1].endswith("%"): |
| 124 | failures.append("%dx%d compute KPI missing %% (%r)" |
| 125 | % (w, h, kpis[1])) |
| 126 | if "GB" not in kpis[2] and "MB" not in kpis[2]: |
| 127 | failures.append("%dx%d VRAM KPI missing units (%r)" |
| 128 | % (w, h, kpis[2])) |
| 129 | if kpis[3] != "4": |
| 130 | failures.append("%dx%d process KPI did not render 4 (%r)" |
| 131 | % (w, h, kpis[3])) |
| 132 | if cards != 4: |
| 133 | failures.append("%dx%d expected 4 GPU cards, got %d" |
| 134 | % (w, h, cards)) |
| 135 | if sparks < 8: |
| 136 | failures.append("%dx%d expected >=8 sparklines, got %d" |
| 137 | % (w, h, sparks)) |
| 138 | if chips < 12: |
| 139 | failures.append("%dx%d expected >=12 chips, got %d" |
| 140 | % (w, h, chips)) |
| 141 | if rows != 4: |
| 142 | failures.append("%dx%d expected 4 process rows, got %d" |
| 143 | % (w, h, rows)) |
| 144 | if not empty_hidden: |
| 145 | failures.append("%dx%d empty state still visible with GPUs" |
| 146 | % (w, h)) |
| 147 | |
| 148 | real = [e for e in errors if "favicon" not in e.lower()] |
| 149 | if real: |
| 150 | failures.append("%dx%d console errors: %s" % (w, h, real[:3])) |
| 151 | |
| 152 | print(" %dx%d scroll=%d/%d cards=%d sparks=%d rows=%d kpis=%s" |
| 153 | % (w, h, sw, iw, cards, sparks, rows, ",".join(kpis))) |
| 154 | page.close() |
| 155 | browser.close() |
| 156 | srv.shutdown() |
| 157 | |
| 158 | if failures: |
| 159 | for f in failures: |
| 160 | print(" FAIL " + f) |
| 161 | return 1 |
| 162 | print(" viewport e2e passed at %d sizes" % len(VIEWPORTS)) |
| 163 | return 0 |
| 164 | |
| 165 | |
| 166 | if __name__ == "__main__": |
| 167 | sys.exit(main()) |