| 1 | #!/usr/bin/env python3 |
| 2 | """Touch input for Meteors: real touch sequences on Android- and iOS-like viewports.""" |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import json |
| 6 | import sys |
| 7 | import time |
| 8 | import subprocess |
| 9 | import socket |
| 10 | from pathlib import Path |
| 11 | |
| 12 | from playwright.sync_api import sync_playwright |
| 13 | |
| 14 | OUT = Path("/workspace/meteors-shots") |
| 15 | OUT.mkdir(parents=True, exist_ok=True) |
| 16 | ROOT = Path("/workspace/meteors") |
| 17 | CHROME = "/usr/bin/google-chrome" |
| 18 | CHROME_ARGS = [ |
| 19 | "--use-gl=angle", |
| 20 | "--use-angle=swiftshader", |
| 21 | "--no-sandbox", |
| 22 | "--disable-dev-shm-usage", |
| 23 | "--disable-gpu-sandbox", |
| 24 | ] |
| 25 | |
| 26 | ANDROID = { |
| 27 | "name": "android-chrome", |
| 28 | "viewport": {"width": 393, "height": 851}, |
| 29 | "device_scale_factor": 2, |
| 30 | "is_mobile": True, |
| 31 | "has_touch": True, |
| 32 | "user_agent": ( |
| 33 | "Mozilla/5.0 (Linux; Android 14; Pixel 7) AppleWebKit/537.36 " |
| 34 | "(KHTML, like Gecko) Chrome/120.0.0.0 Mobile Safari/537.36" |
| 35 | ), |
| 36 | } |
| 37 | IOS = { |
| 38 | "name": "ios-safari", |
| 39 | "viewport": {"width": 390, "height": 844}, |
| 40 | "device_scale_factor": 2, |
| 41 | "is_mobile": True, |
| 42 | "has_touch": True, |
| 43 | "user_agent": ( |
| 44 | "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) " |
| 45 | "AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 " |
| 46 | "Mobile/15E148 Safari/604.1" |
| 47 | ), |
| 48 | } |
| 49 | |
| 50 | FAILURES = [] |
| 51 | LOG = [] |
| 52 | |
| 53 | |
| 54 | def free_port(): |
| 55 | s = socket.socket() |
| 56 | s.bind(("127.0.0.1", 0)) |
| 57 | port = s.getsockname()[1] |
| 58 | s.close() |
| 59 | return port |
| 60 | |
| 61 | |
| 62 | def field_to_client(page, col, field_y): |
| 63 | return page.evaluate( |
| 64 | """([col, fieldY]) => { |
| 65 | const c = app.scene.canvas; |
| 66 | const r = c.getBoundingClientRect(); |
| 67 | const w = Planet.FIELD_WIDTH; |
| 68 | const h = Planet.FIELD_HEIGHT; |
| 69 | return { |
| 70 | x: r.left + ((col + 0.5) / w) * r.width, |
| 71 | y: r.top + (1 - fieldY / h) * r.height |
| 72 | }; |
| 73 | }""", |
| 74 | [col, field_y], |
| 75 | ) |
| 76 | |
| 77 | |
| 78 | def touch_start(cdp, x, y): |
| 79 | cdp.send( |
| 80 | "Input.dispatchTouchEvent", |
| 81 | {"type": "touchStart", "touchPoints": [{"x": x, "y": y, "id": 0, "force": 1.0}]}, |
| 82 | ) |
| 83 | |
| 84 | |
| 85 | def touch_move(cdp, x, y): |
| 86 | cdp.send( |
| 87 | "Input.dispatchTouchEvent", |
| 88 | {"type": "touchMove", "touchPoints": [{"x": x, "y": y, "id": 0, "force": 1.0}]}, |
| 89 | ) |
| 90 | |
| 91 | |
| 92 | def touch_end(cdp): |
| 93 | cdp.send("Input.dispatchTouchEvent", {"type": "touchEnd", "touchPoints": []}) |
| 94 | |
| 95 | |
| 96 | def touch_drag(cdp, x1, y1, x2, y2, steps=16): |
| 97 | touch_start(cdp, x1, y1) |
| 98 | for i in range(1, steps + 1): |
| 99 | t = i / steps |
| 100 | touch_move(cdp, x1 + (x2 - x1) * t, y1 + (y2 - y1) * t) |
| 101 | touch_end(cdp) |
| 102 | |
| 103 | |
| 104 | def reset_board(page, stacks): |
| 105 | """stacks: {col: [colors from bottom]}""" |
| 106 | page.evaluate( |
| 107 | """(stacks) => { |
| 108 | const g = app.core; |
| 109 | g.spawnInterval = 1e9; |
| 110 | g.spawnTimer = 0; |
| 111 | g.blocks.length = 0; |
| 112 | g.clusters = Object.create(null); |
| 113 | g.score = 0; |
| 114 | g.drag = null; |
| 115 | g.state = 'playing'; |
| 116 | g.nextBlockId = 1; |
| 117 | g.nextClusterId = 1; |
| 118 | app.selectTween = null; |
| 119 | for (const [col, colors] of Object.entries(stacks)) { |
| 120 | colors.forEach((color, row) => g.place(+col, row, color)); |
| 121 | } |
| 122 | }""", |
| 123 | stacks, |
| 124 | ) |
| 125 | page.evaluate("() => { if (typeof syncSprites === 'function') { /* maybe closed */ } }") |
| 126 | # Give the view one frame to draw. |
| 127 | page.wait_for_timeout(80) |
| 128 | |
| 129 | |
| 130 | def board(page): |
| 131 | return page.evaluate( |
| 132 | """() => app.core.blocks.map((b) => ({ |
| 133 | id: b.id, col: b.col, y: b.y, color: b.color, state: b.state |
| 134 | }))""" |
| 135 | ) |
| 136 | |
| 137 | |
| 138 | def grab_state(page): |
| 139 | return page.evaluate( |
| 140 | """() => ({ |
| 141 | drag: app.core.drag && { |
| 142 | blockId: app.core.drag.blockId, |
| 143 | accum: app.core.drag.accum, |
| 144 | startedAsTop: app.core.drag.startedAsTop |
| 145 | }, |
| 146 | tween: app.selectTween && { |
| 147 | phase: app.selectTween.phase, |
| 148 | scale: app.selectTween.scale, |
| 149 | t: app.selectTween.t, |
| 150 | blockId: app.selectTween.blockId |
| 151 | } |
| 152 | })""" |
| 153 | ) |
| 154 | |
| 155 | |
| 156 | def check(cond, msg, device): |
| 157 | if not cond: |
| 158 | FAILURES.append(f"[{device}] {msg}") |
| 159 | print(f"FAIL [{device}] {msg}") |
| 160 | else: |
| 161 | print(f"OK [{device}] {msg}") |
| 162 | return cond |
| 163 | |
| 164 | |
| 165 | def shot(page, name): |
| 166 | path = OUT / name |
| 167 | page.screenshot(path=str(path), full_page=False) |
| 168 | print(f"wrote {path} ({path.stat().st_size} bytes)") |
| 169 | return path |
| 170 | |
| 171 | |
| 172 | def start_play(page): |
| 173 | page.goto(URL, wait_until="networkidle", timeout=20000) |
| 174 | page.wait_for_timeout(400) |
| 175 | btn = page.locator("#btn-survival") |
| 176 | btn.wait_for(state="visible", timeout=5000) |
| 177 | # Real touch tap on the Play button (mobile browsers may swallow click). |
| 178 | box = btn.bounding_box() |
| 179 | cdp = page.context.new_cdp_session(page) |
| 180 | touch_drag( |
| 181 | cdp, |
| 182 | box["x"] + box["width"] / 2, |
| 183 | box["y"] + box["height"] / 2, |
| 184 | box["x"] + box["width"] / 2, |
| 185 | box["y"] + box["height"] / 2, |
| 186 | steps=1, |
| 187 | ) |
| 188 | page.wait_for_timeout(400) |
| 189 | # Stage 3: Spelen opens the planet picker first. |
| 190 | if page.evaluate("() => app && app.mode") == "planet-select": |
| 191 | go = page.locator("#btn-planet-go") |
| 192 | go.wait_for(state="visible", timeout=5000) |
| 193 | go.click() |
| 194 | page.wait_for_timeout(400) |
| 195 | # Fallback if the touch didn't start the game (desktop-like click path). |
| 196 | if page.evaluate("() => app.mode") != "playing": |
| 197 | if page.locator("#btn-survival").is_visible(): |
| 198 | btn.click() |
| 199 | page.wait_for_timeout(400) |
| 200 | if page.locator("#btn-planet-go").is_visible(): |
| 201 | page.locator("#btn-planet-go").click() |
| 202 | page.wait_for_timeout(400) |
| 203 | page.wait_for_function("() => app && app.mode === 'playing' && app.core && app.scene", timeout=8000) |
| 204 | page.wait_for_selector("canvas", state="visible", timeout=5000) |
| 205 | return cdp |
| 206 | |
| 207 | |
| 208 | def run_device(browser, device): |
| 209 | name = device["name"] |
| 210 | print(f"\n======== {name} ========") |
| 211 | context = browser.new_context( |
| 212 | viewport=device["viewport"], |
| 213 | device_scale_factor=device["device_scale_factor"], |
| 214 | is_mobile=device["is_mobile"], |
| 215 | has_touch=device["has_touch"], |
| 216 | user_agent=device["user_agent"], |
| 217 | ) |
| 218 | page = context.new_page() |
| 219 | bag = [] |
| 220 | page.on("console", lambda m: bag.append(f"{m.type}: {m.text}")) |
| 221 | page.on("pageerror", lambda e: bag.append(f"pageerror: {e}")) |
| 222 | cdp = start_play(page) |
| 223 | check(page.evaluate("() => app.mode") == "playing", "play started", name) |
| 224 | shot(page, f"t1102-touch-{name}-play.png") |
| 225 | |
| 226 | # --- Instant grab + click tween starts on pointerdown, no hold delay --- |
| 227 | reset_board(page, {"2": [0, 1, 2, 3, 4]}) |
| 228 | page.wait_for_timeout(120) |
| 229 | pos = field_to_client(page, 2, 1.5) # centre of the block at y=1 |
| 230 | touch_start(cdp, pos["x"], pos["y"]) |
| 231 | # No wait-for-hold: inspect immediately, then after one frame. |
| 232 | page.wait_for_timeout(16) |
| 233 | st = grab_state(page) |
| 234 | check(st.get("drag") is not None, f"instant grab on pointerdown, state={st}", name) |
| 235 | tw = st.get("tween") or {} |
| 236 | check( |
| 237 | tw.get("phase") in ("press", "hold") and tw.get("blockId") is not None, |
| 238 | f"click tween starts immediately on pointerdown, tween={tw}", |
| 239 | name, |
| 240 | ) |
| 241 | # Drag without any extra hold delay: swipe up two tiles. |
| 242 | dest = field_to_client(page, 2, 1.5 + 2) |
| 243 | for i in range(1, 9): |
| 244 | t = i / 8 |
| 245 | touch_move(cdp, pos["x"], pos["y"] + (dest["y"] - pos["y"]) * t) |
| 246 | page.wait_for_timeout(40) |
| 247 | mid = board(page) |
| 248 | # The grabbed block (color 1 at y=1) should have moved up. |
| 249 | grabbed = [b for b in mid if b["color"] == 1] |
| 250 | check( |
| 251 | grabbed and grabbed[0]["y"] >= 2.9, |
| 252 | f"immediate drag (no hold) swaps upward, grabbed={grabbed}", |
| 253 | name, |
| 254 | ) |
| 255 | touch_end(cdp) |
| 256 | page.wait_for_timeout(80) |
| 257 | |
| 258 | # --- Fast swipe: several tiles in a short sequence, no skip --- |
| 259 | reset_board(page, {"3": [0, 1, 2, 3, 4]}) |
| 260 | page.wait_for_timeout(80) |
| 261 | p0 = field_to_client(page, 3, 0.5) |
| 262 | p1 = field_to_client(page, 3, 0.5 + 4) |
| 263 | # Few steps: a fast swipe that jumps a long way between events. |
| 264 | touch_drag(cdp, p0["x"], p0["y"], p1["x"], p1["y"], steps=3) |
| 265 | page.wait_for_timeout(80) |
| 266 | after = {b["color"]: b["y"] for b in board(page) if b["col"] == 3} |
| 267 | check( |
| 268 | after.get(0) == 4, |
| 269 | f"fast swipe walks every tile (bottom ends at top), after={after}", |
| 270 | name, |
| 271 | ) |
| 272 | check( |
| 273 | after.get(1) == 0 and after.get(2) == 1 and after.get(3) == 2 and after.get(4) == 3, |
| 274 | f"fast swipe does not skip neighbours, after={after}", |
| 275 | name, |
| 276 | ) |
| 277 | shot(page, f"t1102-touch-{name}-after-swipe.png") |
| 278 | |
| 279 | # --- Horizontal slop: slightly diagonal swipe stays in column --- |
| 280 | reset_board(page, {"4": [0, 1, 2, 3], "5": [3, 4, 0, 1]}) |
| 281 | page.wait_for_timeout(80) |
| 282 | q0 = field_to_client(page, 4, 0.5) |
| 283 | q1 = field_to_client(page, 4.4, 0.5 + 3) # drift 0.4 tiles toward col 5 |
| 284 | # field_to_client uses (col+0.5)*tile, so 4.4 means 0.4 into the slop visually. |
| 285 | q1 = field_to_client(page, 4.4, 3.5) |
| 286 | colors_before_5 = [b["color"] for b in sorted(board(page), key=lambda b: b["y"]) if b["col"] == 5] |
| 287 | touch_drag(cdp, q0["x"], q0["y"], q1["x"], q1["y"], steps=10) |
| 288 | page.wait_for_timeout(80) |
| 289 | col4 = {b["color"]: (b["y"], b["col"]) for b in board(page) if b["color"] == 0 and b["state"] == "still"} |
| 290 | moved = [b for b in board(page) if b["color"] == 0 and b.get("id") is not None] |
| 291 | # The bottom block of col 4 (color 0) should be at the top of col 4, not in col 5. |
| 292 | bottoms = [b for b in board(page) if b["col"] == 4] |
| 293 | top4 = max(bottoms, key=lambda b: b["y"]) if bottoms else None |
| 294 | colors_after_5 = [b["color"] for b in sorted(board(page), key=lambda b: b["y"]) if b["col"] == 5] |
| 295 | check( |
| 296 | top4 and top4["color"] == 0 and top4["col"] == 4, |
| 297 | f"diagonal drag stays in starting column, top4={top4}, col5={colors_after_5}", |
| 298 | name, |
| 299 | ) |
| 300 | check( |
| 301 | colors_after_5 == colors_before_5, |
| 302 | f"neighbour column untouched, before={colors_before_5} after={colors_after_5}", |
| 303 | name, |
| 304 | ) |
| 305 | shot(page, f"t1102-touch-{name}-diagonal.png") |
| 306 | |
| 307 | # --- Snap: press slightly above the top block still grabs it --- |
| 308 | reset_board(page, {"1": [2, 3]}) |
| 309 | page.wait_for_timeout(80) |
| 310 | # Top block occupies [1, 2); press at 2.25 (0.25 above). |
| 311 | s = field_to_client(page, 1, 2.25) |
| 312 | touch_start(cdp, s["x"], s["y"]) |
| 313 | page.wait_for_timeout(16) |
| 314 | st = grab_state(page) |
| 315 | check(st.get("drag") is not None, f"snap-grab slightly above the block, state={st}", name) |
| 316 | touch_end(cdp) |
| 317 | page.wait_for_timeout(50) |
| 318 | |
| 319 | if bag: |
| 320 | print(f"console [{name}]: {bag[:8]}") |
| 321 | context.close() |
| 322 | |
| 323 | |
| 324 | def main(): |
| 325 | global URL |
| 326 | port = free_port() |
| 327 | URL = f"http://127.0.0.1:{port}/" |
| 328 | print("serving", URL, "from", ROOT / "build") |
| 329 | httpd = subprocess.Popen( |
| 330 | ["python3", "-m", "http.server", str(port), "--bind", "127.0.0.1", "--directory", str(ROOT / "build")], |
| 331 | stdout=subprocess.DEVNULL, |
| 332 | stderr=subprocess.DEVNULL, |
| 333 | ) |
| 334 | try: |
| 335 | time.sleep(0.4) |
| 336 | with sync_playwright() as p: |
| 337 | browser = p.chromium.launch( |
| 338 | executable_path=CHROME, |
| 339 | args=CHROME_ARGS, |
| 340 | headless=True, |
| 341 | ) |
| 342 | for device in (ANDROID, IOS): |
| 343 | run_device(browser, device) |
| 344 | browser.close() |
| 345 | finally: |
| 346 | httpd.terminate() |
| 347 | try: |
| 348 | httpd.wait(timeout=3) |
| 349 | except Exception: |
| 350 | httpd.kill() |
| 351 | |
| 352 | print("\n======== summary ========") |
| 353 | if FAILURES: |
| 354 | print(f"{len(FAILURES)} failure(s):") |
| 355 | for f in FAILURES: |
| 356 | print(" -", f) |
| 357 | return 1 |
| 358 | print("all touch tests passed") |
| 359 | return 0 |
| 360 | |
| 361 | |
| 362 | if __name__ == "__main__": |
| 363 | sys.exit(main()) |
projects-timfalken-com meteors