#!/usr/bin/env python3 """1:1 drag tracking (t1104): mouse and real touch, including a high-DPR mobile viewport.""" from __future__ import annotations import socket import subprocess import sys import time from pathlib import Path from playwright.sync_api import sync_playwright OUT = Path("/workspace/meteors-shots") OUT.mkdir(parents=True, exist_ok=True) ROOT = Path("/workspace/meteors") CHROME = "/usr/bin/google-chrome" CHROME_ARGS = [ "--use-gl=angle", "--use-angle=swiftshader", "--no-sandbox", "--disable-dev-shm-usage", "--disable-gpu-sandbox", ] DESKTOP = { "name": "desktop-mouse", "viewport": {"width": 1280, "height": 900}, "device_scale_factor": 1, "is_mobile": False, "has_touch": False, "user_agent": None, "input": "mouse", } ANDROID = { "name": "android-touch", "viewport": {"width": 393, "height": 851}, "device_scale_factor": 2, "is_mobile": True, "has_touch": True, "user_agent": ( "Mozilla/5.0 (Linux; Android 14; Pixel 7) AppleWebKit/537.36 " "(KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36" ), "input": "touch", } ANDROID_DPR3 = { "name": "android-dpr3", "viewport": {"width": 393, "height": 851}, "device_scale_factor": 3, "is_mobile": True, "has_touch": True, "user_agent": ( "Mozilla/5.0 (Linux; Android 14; Pixel 7) AppleWebKit/537.36 " "(KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36" ), "input": "touch", } IOS = { "name": "ios-touch", "viewport": {"width": 390, "height": 844}, "device_scale_factor": 2, "is_mobile": True, "has_touch": True, "user_agent": ( "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) " "AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 " "Mobile/15E148 Safari/604.1" ), "input": "touch", } FAILURES = [] URL = None def free_port(): s = socket.socket() s.bind(("127.0.0.1", 0)) port = s.getsockname()[1] s.close() return port def field_to_client(page, col, field_y): return page.evaluate( """([col, fieldY]) => { const c = app.scene.canvas; const r = c.getBoundingClientRect(); const w = Planet.FIELD_WIDTH; const h = Planet.FIELD_HEIGHT; return { x: r.left + ((col + 0.5) / w) * r.width, y: r.top + (1 - fieldY / h) * r.height }; }""", [col, field_y], ) def touch_start(cdp, x, y): cdp.send( "Input.dispatchTouchEvent", {"type": "touchStart", "touchPoints": [{"x": x, "y": y, "id": 1}]}, ) def touch_move(cdp, x, y): cdp.send( "Input.dispatchTouchEvent", {"type": "touchMove", "touchPoints": [{"x": x, "y": y, "id": 1}]}, ) def touch_end(cdp): cdp.send("Input.dispatchTouchEvent", {"type": "touchEnd", "touchPoints": []}) def pointer_down(page, cdp, kind, x, y): if kind == "mouse": page.mouse.move(x, y) page.mouse.down() else: touch_start(cdp, x, y) def pointer_move(page, cdp, kind, x, y): if kind == "mouse": page.mouse.move(x, y) else: touch_move(cdp, x, y) def pointer_up(page, cdp, kind): if kind == "mouse": page.mouse.up() else: touch_end(cdp) def drag_to(page, cdp, kind, x1, y1, x2, y2, steps=12, end=True): pointer_down(page, cdp, kind, x1, y1) for i in range(1, steps + 1): t = i / steps pointer_move(page, cdp, kind, x1 + (x2 - x1) * t, y1 + (y2 - y1) * t) if end: pointer_up(page, cdp, kind) def reset_board(page, stacks): page.evaluate( """(stacks) => { const g = app.core; g.spawnInterval = 1e9; g.spawnTimer = 0; g.blocks.length = 0; g.clusters = Object.create(null); g.score = 0; g.drag = null; g.state = 'playing'; g.nextBlockId = 1; g.nextClusterId = 1; app.selectTween = null; for (const [col, colors] of Object.entries(stacks)) { colors.forEach((color, row) => g.place(+col, row, color)); } }""", stacks, ) page.wait_for_timeout(80) def board(page): return page.evaluate( """() => app.core.blocks.map((b) => ({ id: b.id, col: b.col, y: b.y, color: b.color, state: b.state }))""" ) def col_ys(page, col): return {b["color"]: b["y"] for b in board(page) if b["col"] == col} def check(cond, msg, device): if not cond: FAILURES.append(f"[{device}] {msg}") print(f"FAIL [{device}] {msg}") else: print(f"OK [{device}] {msg}") return cond def shot(page, name): path = OUT / name page.screenshot(path=str(path), full_page=False) print(f"wrote {path} ({path.stat().st_size} bytes)") def start_play(page, kind): page.goto(URL, wait_until="networkidle", timeout=20000) page.wait_for_timeout(400) btn = page.locator("#btn-play") btn.wait_for(state="visible", timeout=5000) if kind == "touch": box = btn.bounding_box() cdp = page.context.new_cdp_session(page) touch_start(cdp, box["x"] + box["width"] / 2, box["y"] + box["height"] / 2) touch_end(cdp) page.wait_for_timeout(400) else: cdp = None btn.click() page.wait_for_timeout(400) if page.evaluate("() => app && app.mode") == "planet-select": go = page.locator("#btn-planet-go") go.wait_for(state="visible", timeout=5000) go.click() page.wait_for_timeout(400) if page.evaluate("() => app.mode") != "playing": if page.locator("#btn-play").is_visible(): page.locator("#btn-play").click() page.wait_for_timeout(400) if page.locator("#btn-planet-go").is_visible(): page.locator("#btn-planet-go").click() page.wait_for_timeout(400) page.wait_for_function("() => app && app.mode === 'playing' && app.core && app.scene", timeout=8000) page.wait_for_selector("canvas", state="visible", timeout=5000) if cdp is None: cdp = page.context.new_cdp_session(page) return cdp def nearly(a, b, eps=0.15): return abs(a - b) <= eps def run_device(browser, device): name = device["name"] kind = device["input"] print(f"\n======== {name} ({kind}, dpr={device['device_scale_factor']}) ========") kw = dict( viewport=device["viewport"], device_scale_factor=device["device_scale_factor"], is_mobile=device["is_mobile"], has_touch=device["has_touch"], ) if device["user_agent"]: kw["user_agent"] = device["user_agent"] context = browser.new_context(**kw) page = context.new_page() bag = [] page.on("console", lambda m: bag.append(f"{m.type}: {m.text}")) page.on("pageerror", lambda e: bag.append(f"pageerror: {e}")) cdp = start_play(page, kind) check(page.evaluate("() => app.mode") == "playing", "play started", name) # --- 1 tile down --- reset_board(page, {"2": [0, 1, 2, 3, 4]}) p0 = field_to_client(page, 2, 1.5) p1 = field_to_client(page, 2, 0.5) drag_to(page, cdp, kind, p0["x"], p0["y"], p1["x"], p1["y"], steps=10, end=True) page.wait_for_timeout(60) ys = col_ys(page, 2) check(nearly(ys.get(1, -99), 0), f"1 tile down: grabbed color 1 at y=0, ys={ys}", name) check(nearly(ys.get(0, -99), 1), f"1 tile down: swapped neighbour to y=1, ys={ys}", name) check(nearly(ys.get(2, -99), 2) and nearly(ys.get(3, -99), 3) and nearly(ys.get(4, -99), 4), f"1 tile down: rest of the stack unmoved, ys={ys}", name) # --- 1 tile up --- reset_board(page, {"3": [0, 1, 2, 3, 4]}) p0 = field_to_client(page, 3, 0.5) p1 = field_to_client(page, 3, 1.5) drag_to(page, cdp, kind, p0["x"], p0["y"], p1["x"], p1["y"], steps=10, end=True) page.wait_for_timeout(60) ys = col_ys(page, 3) check(nearly(ys.get(0, -99), 1), f"1 tile up: grabbed color 0 at y=1, ys={ys}", name) check(nearly(ys.get(1, -99), 0), f"1 tile up: neighbour at y=0, ys={ys}", name) check(nearly(ys.get(2, -99), 2), f"1 tile up: rest unmoved, ys={ys}", name) # --- 2 tiles, then stop: pointer held still, nothing else moves --- reset_board(page, {"4": [0, 1, 2, 3, 4]}) p0 = field_to_client(page, 4, 0.5) p1 = field_to_client(page, 4, 2.5) drag_to(page, cdp, kind, p0["x"], p0["y"], p1["x"], p1["y"], steps=14, end=False) page.wait_for_timeout(50) mid = col_ys(page, 4) check(nearly(mid.get(0, -99), 2), f"2 tiles: grabbed color 0 at y=2, ys={mid}", name) check(nearly(mid.get(1, -99), 0) and nearly(mid.get(2, -99), 1) and nearly(mid.get(3, -99), 3), f"2 tiles: walked both neighbours, ys={mid}", name) # Pointer stops: more moves at the same point, then a short wait. for _ in range(6): pointer_move(page, cdp, kind, p1["x"], p1["y"]) page.wait_for_timeout(30) after = col_ys(page, 4) check(after == mid, f"pointer stopped: stack stays put, mid={mid} after={after}", name) pointer_up(page, cdp, kind) page.wait_for_timeout(80) end = col_ys(page, 4) check(end == mid, f"after release: still at the pointer row, mid={mid} end={end}", name) shot(page, f"t1104-drag-{name}.png") if bag: print(f"console [{name}]: {bag[:8]}") context.close() def main(): global URL port = free_port() URL = f"http://127.0.0.1:{port}/" print("serving", URL, "from", ROOT / "build") httpd = subprocess.Popen( ["python3", "-m", "http.server", str(port), "--bind", "127.0.0.1", "--directory", str(ROOT / "build")], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, ) try: time.sleep(0.4) with sync_playwright() as p: browser = p.chromium.launch( executable_path=CHROME, args=CHROME_ARGS, headless=True, ) for device in (DESKTOP, ANDROID, ANDROID_DPR3, IOS): run_device(browser, device) browser.close() finally: httpd.terminate() try: httpd.wait(timeout=3) except Exception: httpd.kill() print("\n======== summary ========") if FAILURES: print(f"{len(FAILURES)} failure(s):") for f in FAILURES: print(" -", f) return 1 print("all 1:1 drag tests passed") return 0 if __name__ == "__main__": sys.exit(main())