#!/usr/bin/env python3 """tests/e2e.py - viewport, console and gameplay checks for the plugin_tetris_3d UI. Serves the plugin through a tiny CGI-style HTTP shim on a loopback port and opens it with Playwright at 1440 / 1024 / 390 CSS pixels. Asserts there is no horizontal overflow, no console error, that the HUD and the three canvases render, and that a real game runs: a piece exists, it moves left on a keyboard press, and a hard drop changes the score. 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 threading ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BIN = os.path.join(ROOT, "plugin_tetris_3d") 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) 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") 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_tetris_3d 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_tetris_3d?embed=1" % 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 = [] def on_console(m, _e=errors): if m.type == "error": _e.append(m.text) page.on("console", on_console) page.on("pageerror", lambda e, _e=errors: _e.append(str(e))) page.goto(url, wait_until="load") page.wait_for_timeout(900) 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)) sh = page.evaluate("document.documentElement.scrollHeight") ih = page.evaluate("window.innerHeight") if sh > ih + 1: failures.append("%dx%d vertical overflow %d > %d" % (w, h, sh, ih)) for cid in ("cv", "cvNext", "cvHold"): box = page.evaluate( "(()=>{const c=document.getElementById('%s');" "if(!c)return null;const r=c.getBoundingClientRect();" "return [Math.round(r.width),Math.round(r.height),c.width,c.height];})()" % cid) if box is None: failures.append("%dx%d canvas %s missing" % (w, h, cid)) continue if box[0] < 20 or box[1] < 20: failures.append("%dx%d canvas %s collapsed %r" % (w, h, cid, box)) if box[2] < 10 or box[3] < 10: failures.append("%dx%d canvas %s has no backing pixels %r" % (w, h, cid, box)) score = page.evaluate("document.getElementById('vScore').textContent") if not score.strip().isdigit(): failures.append("%dx%d score HUD did not render (%r)" % (w, h, score)) # the canvas must actually have painted something painted = page.evaluate( "(()=>{const c=document.getElementById('cv');" "const g=c.getContext('2d');" "const d=g.getImageData(0,0,c.width,c.height).data;" "let n=0;for(let i=3;i %s)" % (w, h, before, after)) if w == 390: # mobile: the touch pad must be reachable and on screen for bid in ("bLeft", "bDrop", "bHold", "bPause"): vis = page.evaluate( "(()=>{const b=document.getElementById('%s');" "if(!b)return false;const r=b.getBoundingClientRect();" "return r.width>0&&r.height>0&&r.bottom<=window.innerHeight+1;})()" % bid) if not vis: failures.append("390x844 touch button %s not visible" % bid) page.click("#bRot") page.click("#bDrop") page.wait_for_timeout(150) if errors: failures.append("%dx%d console errors: %s" % (w, h, "; ".join(errors[:3]))) page.screenshot(path="/tmp/sx_tetris_3d_%dx%d.png" % (w, h)) page.close() browser.close() # a dark and a light render must both paint with sync_playwright() as p: browser = p.chromium.launch() for theme in ("dark", "light"): page = browser.new_page(viewport={"width": 1024, "height": 768}, color_scheme="dark" if theme == "dark" else "light") page.goto(url, wait_until="load") page.wait_for_timeout(600) page.click("#mBtn") page.wait_for_timeout(400) bg = page.evaluate("getComputedStyle(document.body).backgroundColor") painted = page.evaluate( "(()=>{const c=document.getElementById('cv');" "const g=c.getContext('2d');" "const d=g.getImageData(0,0,c.width,c.height).data;" "let n=0;for(let i=3;i