| 1 | #!/usr/bin/env python3 |
| 2 | """1:1 drag tracking (t1104): mouse and real touch, including a high-DPR mobile viewport.""" |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import socket |
| 6 | import subprocess |
| 7 | import sys |
| 8 | import time |
| 9 | from pathlib import Path |
| 10 | |
| 11 | from playwright.sync_api import sync_playwright |
| 12 | |
| 13 | OUT = Path("/workspace/meteors-shots") |
| 14 | OUT.mkdir(parents=True, exist_ok=True) |
| 15 | ROOT = Path("/workspace/meteors") |
| 16 | CHROME = "/usr/bin/google-chrome" |
| 17 | CHROME_ARGS = [ |
| 18 | "--use-gl=angle", |
| 19 | "--use-angle=swiftshader", |
| 20 | "--no-sandbox", |
| 21 | "--disable-dev-shm-usage", |
| 22 | "--disable-gpu-sandbox", |
| 23 | ] |
| 24 | |
| 25 | DESKTOP = { |
| 26 | "name": "desktop-mouse", |
| 27 | "viewport": {"width": 1280, "height": 900}, |
| 28 | "device_scale_factor": 1, |
| 29 | "is_mobile": False, |
| 30 | "has_touch": False, |
| 31 | "user_agent": None, |
| 32 | "input": "mouse", |
| 33 | } |
| 34 | ANDROID = { |
| 35 | "name": "android-touch", |
| 36 | "viewport": {"width": 393, "height": 851}, |
| 37 | "device_scale_factor": 2, |
| 38 | "is_mobile": True, |
| 39 | "has_touch": True, |
| 40 | "user_agent": ( |
| 41 | "Mozilla/5.0 (Linux; Android 14; Pixel 7) AppleWebKit/537.36 " |
| 42 | "(KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36" |
| 43 | ), |
| 44 | "input": "touch", |
| 45 | } |
| 46 | ANDROID_DPR3 = { |
| 47 | "name": "android-dpr3", |
| 48 | "viewport": {"width": 393, "height": 851}, |
| 49 | "device_scale_factor": 3, |
| 50 | "is_mobile": True, |
| 51 | "has_touch": True, |
| 52 | "user_agent": ( |
| 53 | "Mozilla/5.0 (Linux; Android 14; Pixel 7) AppleWebKit/537.36 " |
| 54 | "(KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36" |
| 55 | ), |
| 56 | "input": "touch", |
| 57 | } |
| 58 | IOS = { |
| 59 | "name": "ios-touch", |
| 60 | "viewport": {"width": 390, "height": 844}, |
| 61 | "device_scale_factor": 2, |
| 62 | "is_mobile": True, |
| 63 | "has_touch": True, |
| 64 | "user_agent": ( |
| 65 | "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) " |
| 66 | "AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 " |
| 67 | "Mobile/15E148 Safari/604.1" |
| 68 | ), |
| 69 | "input": "touch", |
| 70 | } |
| 71 | |
| 72 | FAILURES = [] |
| 73 | URL = None |
| 74 | |
| 75 | |
| 76 | def free_port(): |
| 77 | s = socket.socket() |
| 78 | s.bind(("127.0.0.1", 0)) |
| 79 | port = s.getsockname()[1] |
| 80 | s.close() |
| 81 | return port |
| 82 | |
| 83 | |
| 84 | def field_to_client(page, col, field_y): |
| 85 | return page.evaluate( |
| 86 | """([col, fieldY]) => { |
| 87 | const c = app.scene.canvas; |
| 88 | const r = c.getBoundingClientRect(); |
| 89 | const w = Planet.FIELD_WIDTH; |
| 90 | const h = Planet.FIELD_HEIGHT; |
| 91 | return { |
| 92 | x: r.left + ((col + 0.5) / w) * r.width, |
| 93 | y: r.top + (1 - fieldY / h) * r.height |
| 94 | }; |
| 95 | }""", |
| 96 | [col, field_y], |
| 97 | ) |
| 98 | |
| 99 | |
| 100 | def touch_start(cdp, x, y): |
| 101 | cdp.send( |
| 102 | "Input.dispatchTouchEvent", |
| 103 | {"type": "touchStart", "touchPoints": [{"x": x, "y": y, "id": 1}]}, |
| 104 | ) |
| 105 | |
| 106 | |
| 107 | def touch_move(cdp, x, y): |
| 108 | cdp.send( |
| 109 | "Input.dispatchTouchEvent", |
| 110 | {"type": "touchMove", "touchPoints": [{"x": x, "y": y, "id": 1}]}, |
| 111 | ) |
| 112 | |
| 113 | |
| 114 | def touch_end(cdp): |
| 115 | cdp.send("Input.dispatchTouchEvent", {"type": "touchEnd", "touchPoints": []}) |
| 116 | |
| 117 | |
| 118 | def pointer_down(page, cdp, kind, x, y): |
| 119 | if kind == "mouse": |
| 120 | page.mouse.move(x, y) |
| 121 | page.mouse.down() |
| 122 | else: |
| 123 | touch_start(cdp, x, y) |
| 124 | |
| 125 | |
| 126 | def pointer_move(page, cdp, kind, x, y): |
| 127 | if kind == "mouse": |
| 128 | page.mouse.move(x, y) |
| 129 | else: |
| 130 | touch_move(cdp, x, y) |
| 131 | |
| 132 | |
| 133 | def pointer_up(page, cdp, kind): |
| 134 | if kind == "mouse": |
| 135 | page.mouse.up() |
| 136 | else: |
| 137 | touch_end(cdp) |
| 138 | |
| 139 | |
| 140 | def drag_to(page, cdp, kind, x1, y1, x2, y2, steps=12, end=True): |
| 141 | pointer_down(page, cdp, kind, x1, y1) |
| 142 | for i in range(1, steps + 1): |
| 143 | t = i / steps |
| 144 | pointer_move(page, cdp, kind, x1 + (x2 - x1) * t, y1 + (y2 - y1) * t) |
| 145 | if end: |
| 146 | pointer_up(page, cdp, kind) |
| 147 | |
| 148 | |
| 149 | def reset_board(page, stacks): |
| 150 | page.evaluate( |
| 151 | """(stacks) => { |
| 152 | const g = app.core; |
| 153 | g.spawnInterval = 1e9; |
| 154 | g.spawnTimer = 0; |
| 155 | g.blocks.length = 0; |
| 156 | g.clusters = Object.create(null); |
| 157 | g.score = 0; |
| 158 | g.drag = null; |
| 159 | g.state = 'playing'; |
| 160 | g.nextBlockId = 1; |
| 161 | g.nextClusterId = 1; |
| 162 | app.selectTween = null; |
| 163 | for (const [col, colors] of Object.entries(stacks)) { |
| 164 | colors.forEach((color, row) => g.place(+col, row, color)); |
| 165 | } |
| 166 | }""", |
| 167 | stacks, |
| 168 | ) |
| 169 | page.wait_for_timeout(80) |
| 170 | |
| 171 | |
| 172 | def board(page): |
| 173 | return page.evaluate( |
| 174 | """() => app.core.blocks.map((b) => ({ |
| 175 | id: b.id, col: b.col, y: b.y, color: b.color, state: b.state |
| 176 | }))""" |
| 177 | ) |
| 178 | |
| 179 | |
| 180 | def col_ys(page, col): |
| 181 | return {b["color"]: b["y"] for b in board(page) if b["col"] == col} |
| 182 | |
| 183 | |
| 184 | def check(cond, msg, device): |
| 185 | if not cond: |
| 186 | FAILURES.append(f"[{device}] {msg}") |
| 187 | print(f"FAIL [{device}] {msg}") |
| 188 | else: |
| 189 | print(f"OK [{device}] {msg}") |
| 190 | return cond |
| 191 | |
| 192 | |
| 193 | def shot(page, name): |
| 194 | path = OUT / name |
| 195 | page.screenshot(path=str(path), full_page=False) |
| 196 | print(f"wrote {path} ({path.stat().st_size} bytes)") |
| 197 | |
| 198 | |
| 199 | def start_play(page, kind): |
| 200 | page.goto(URL, wait_until="networkidle", timeout=20000) |
| 201 | page.wait_for_timeout(400) |
| 202 | btn = page.locator("#btn-play") |
| 203 | btn.wait_for(state="visible", timeout=5000) |
| 204 | if kind == "touch": |
| 205 | box = btn.bounding_box() |
| 206 | cdp = page.context.new_cdp_session(page) |
| 207 | touch_start(cdp, box["x"] + box["width"] / 2, box["y"] + box["height"] / 2) |
| 208 | touch_end(cdp) |
| 209 | page.wait_for_timeout(400) |
| 210 | else: |
| 211 | cdp = None |
| 212 | btn.click() |
| 213 | page.wait_for_timeout(400) |
| 214 | if page.evaluate("() => app && app.mode") == "planet-select": |
| 215 | go = page.locator("#btn-planet-go") |
| 216 | go.wait_for(state="visible", timeout=5000) |
| 217 | go.click() |
| 218 | page.wait_for_timeout(400) |
| 219 | if page.evaluate("() => app.mode") != "playing": |
| 220 | if page.locator("#btn-play").is_visible(): |
| 221 | page.locator("#btn-play").click() |
| 222 | page.wait_for_timeout(400) |
| 223 | if page.locator("#btn-planet-go").is_visible(): |
| 224 | page.locator("#btn-planet-go").click() |
| 225 | page.wait_for_timeout(400) |
| 226 | page.wait_for_function("() => app && app.mode === 'playing' && app.core && app.scene", timeout=8000) |
| 227 | page.wait_for_selector("canvas", state="visible", timeout=5000) |
| 228 | if cdp is None: |
| 229 | cdp = page.context.new_cdp_session(page) |
| 230 | return cdp |
| 231 | |
| 232 | |
| 233 | def nearly(a, b, eps=0.15): |
| 234 | return abs(a - b) <= eps |
| 235 | |
| 236 | |
| 237 | def run_device(browser, device): |
| 238 | name = device["name"] |
| 239 | kind = device["input"] |
| 240 | print(f"\n======== {name} ({kind}, dpr={device['device_scale_factor']}) ========") |
| 241 | kw = dict( |
| 242 | viewport=device["viewport"], |
| 243 | device_scale_factor=device["device_scale_factor"], |
| 244 | is_mobile=device["is_mobile"], |
| 245 | has_touch=device["has_touch"], |
| 246 | ) |
| 247 | if device["user_agent"]: |
| 248 | kw["user_agent"] = device["user_agent"] |
| 249 | context = browser.new_context(**kw) |
| 250 | page = context.new_page() |
| 251 | bag = [] |
| 252 | page.on("console", lambda m: bag.append(f"{m.type}: {m.text}")) |
| 253 | page.on("pageerror", lambda e: bag.append(f"pageerror: {e}")) |
| 254 | cdp = start_play(page, kind) |
| 255 | check(page.evaluate("() => app.mode") == "playing", "play started", name) |
| 256 | |
| 257 | # --- 1 tile down --- |
| 258 | reset_board(page, {"2": [0, 1, 2, 3, 4]}) |
| 259 | p0 = field_to_client(page, 2, 1.5) |
| 260 | p1 = field_to_client(page, 2, 0.5) |
| 261 | drag_to(page, cdp, kind, p0["x"], p0["y"], p1["x"], p1["y"], steps=10, end=True) |
| 262 | page.wait_for_timeout(60) |
| 263 | ys = col_ys(page, 2) |
| 264 | check(nearly(ys.get(1, -99), 0), f"1 tile down: grabbed color 1 at y=0, ys={ys}", name) |
| 265 | check(nearly(ys.get(0, -99), 1), f"1 tile down: swapped neighbour to y=1, ys={ys}", name) |
| 266 | check(nearly(ys.get(2, -99), 2) and nearly(ys.get(3, -99), 3) and nearly(ys.get(4, -99), 4), |
| 267 | f"1 tile down: rest of the stack unmoved, ys={ys}", name) |
| 268 | |
| 269 | # --- 1 tile up --- |
| 270 | reset_board(page, {"3": [0, 1, 2, 3, 4]}) |
| 271 | p0 = field_to_client(page, 3, 0.5) |
| 272 | p1 = field_to_client(page, 3, 1.5) |
| 273 | drag_to(page, cdp, kind, p0["x"], p0["y"], p1["x"], p1["y"], steps=10, end=True) |
| 274 | page.wait_for_timeout(60) |
| 275 | ys = col_ys(page, 3) |
| 276 | check(nearly(ys.get(0, -99), 1), f"1 tile up: grabbed color 0 at y=1, ys={ys}", name) |
| 277 | check(nearly(ys.get(1, -99), 0), f"1 tile up: neighbour at y=0, ys={ys}", name) |
| 278 | check(nearly(ys.get(2, -99), 2), f"1 tile up: rest unmoved, ys={ys}", name) |
| 279 | |
| 280 | # --- 2 tiles, then stop: pointer held still, nothing else moves --- |
| 281 | reset_board(page, {"4": [0, 1, 2, 3, 4]}) |
| 282 | p0 = field_to_client(page, 4, 0.5) |
| 283 | p1 = field_to_client(page, 4, 2.5) |
| 284 | drag_to(page, cdp, kind, p0["x"], p0["y"], p1["x"], p1["y"], steps=14, end=False) |
| 285 | page.wait_for_timeout(50) |
| 286 | mid = col_ys(page, 4) |
| 287 | check(nearly(mid.get(0, -99), 2), f"2 tiles: grabbed color 0 at y=2, ys={mid}", name) |
| 288 | check(nearly(mid.get(1, -99), 0) and nearly(mid.get(2, -99), 1) and nearly(mid.get(3, -99), 3), |
| 289 | f"2 tiles: walked both neighbours, ys={mid}", name) |
| 290 | # Pointer stops: more moves at the same point, then a short wait. |
| 291 | for _ in range(6): |
| 292 | pointer_move(page, cdp, kind, p1["x"], p1["y"]) |
| 293 | page.wait_for_timeout(30) |
| 294 | after = col_ys(page, 4) |
| 295 | check(after == mid, f"pointer stopped: stack stays put, mid={mid} after={after}", name) |
| 296 | pointer_up(page, cdp, kind) |
| 297 | page.wait_for_timeout(80) |
| 298 | end = col_ys(page, 4) |
| 299 | check(end == mid, f"after release: still at the pointer row, mid={mid} end={end}", name) |
| 300 | shot(page, f"t1104-drag-{name}.png") |
| 301 | |
| 302 | if bag: |
| 303 | print(f"console [{name}]: {bag[:8]}") |
| 304 | context.close() |
| 305 | |
| 306 | |
| 307 | def main(): |
| 308 | global URL |
| 309 | port = free_port() |
| 310 | URL = f"http://127.0.0.1:{port}/" |
| 311 | print("serving", URL, "from", ROOT / "build") |
| 312 | httpd = subprocess.Popen( |
| 313 | ["python3", "-m", "http.server", str(port), "--bind", "127.0.0.1", "--directory", str(ROOT / "build")], |
| 314 | stdout=subprocess.DEVNULL, |
| 315 | stderr=subprocess.DEVNULL, |
| 316 | ) |
| 317 | try: |
| 318 | time.sleep(0.4) |
| 319 | with sync_playwright() as p: |
| 320 | browser = p.chromium.launch( |
| 321 | executable_path=CHROME, |
| 322 | args=CHROME_ARGS, |
| 323 | headless=True, |
| 324 | ) |
| 325 | for device in (DESKTOP, ANDROID, ANDROID_DPR3, IOS): |
| 326 | run_device(browser, device) |
| 327 | browser.close() |
| 328 | finally: |
| 329 | httpd.terminate() |
| 330 | try: |
| 331 | httpd.wait(timeout=3) |
| 332 | except Exception: |
| 333 | httpd.kill() |
| 334 | |
| 335 | print("\n======== summary ========") |
| 336 | if FAILURES: |
| 337 | print(f"{len(FAILURES)} failure(s):") |
| 338 | for f in FAILURES: |
| 339 | print(" -", f) |
| 340 | return 1 |
| 341 | print("all 1:1 drag tests passed") |
| 342 | return 0 |
| 343 | |
| 344 | |
| 345 | if __name__ == "__main__": |
| 346 | sys.exit(main()) |
projects-timfalken-com meteors