#!/usr/bin/env python3 """tests/e2e.py - viewport + rendering checks for the plugin_nvtop dashboard. 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: * no horizontal overflow, * no console errors, * the overview KPIs rendered numbers, * the per-GPU cards rendered (mock 4-GPU fixture), each with two sparklines, * the compute process table rendered rows. The fixture is injected through NVTOP_MOCK_CSV so the checks are deterministic on any host, GPU or not. Skips cleanly (exit 0) when Playwright is missing, so `make test` stays offline-only. 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 subprocess import socketserver import sys import threading ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BIN = os.path.join(ROOT, "plugin_nvtop") FIXTURE = os.path.join(ROOT, "tests", "fixtures", "mock-4gpu.csv") VIEWPORTS = [(1440, 900), (1024, 768), (390, 844)] 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) env["NVTOP_MOCK_CSV"] = FIXTURE out = subprocess.run([BIN], capture_output=True, env=env, timeout=60).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") # no Content-Length from the plugin; the shim sets its own framing 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_nvtop 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 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_nvtop" % 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(3200) 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)) kpis = page.evaluate( "['kGpus','kUtil','kMem','kProcs'].map(function(i){" "var e=document.getElementById(i);" "return e?e.textContent.trim():'';})") cards = page.evaluate( "document.querySelectorAll('#gpuGrid .gpu').length") sparks = page.evaluate( "document.querySelectorAll('#gpuGrid svg.spark').length") rows = page.evaluate( "document.querySelectorAll('#procRows tr').length") chips = page.evaluate( "document.querySelectorAll('#gpuGrid .chip').length") empty_hidden = page.evaluate( "document.getElementById('emptyState')" ".classList.contains('hide')") if kpis[0] != "4": failures.append("%dx%d GPU KPI did not render 4 (%r)" % (w, h, kpis[0])) if not kpis[1].endswith("%"): failures.append("%dx%d compute KPI missing %% (%r)" % (w, h, kpis[1])) if "GB" not in kpis[2] and "MB" not in kpis[2]: failures.append("%dx%d VRAM KPI missing units (%r)" % (w, h, kpis[2])) if kpis[3] != "4": failures.append("%dx%d process KPI did not render 4 (%r)" % (w, h, kpis[3])) if cards != 4: failures.append("%dx%d expected 4 GPU cards, got %d" % (w, h, cards)) if sparks < 8: failures.append("%dx%d expected >=8 sparklines, got %d" % (w, h, sparks)) if chips < 12: failures.append("%dx%d expected >=12 chips, got %d" % (w, h, chips)) if rows != 4: failures.append("%dx%d expected 4 process rows, got %d" % (w, h, rows)) if not empty_hidden: failures.append("%dx%d empty state still visible with GPUs" % (w, h)) 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 scroll=%d/%d cards=%d sparks=%d rows=%d kpis=%s" % (w, h, sw, iw, cards, sparks, rows, ",".join(kpis))) page.close() browser.close() srv.shutdown() 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())