| 1 | #!/usr/bin/env python3 |
| 2 | """t1139 / t1140: Planet Designer UI, locked modal, custom planets in every picker.""" |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | import os |
| 6 | import socket |
| 7 | import subprocess |
| 8 | import sys |
| 9 | import tempfile |
| 10 | import time |
| 11 | from pathlib import Path |
| 12 | |
| 13 | from playwright.sync_api import sync_playwright |
| 14 | |
| 15 | ROOT = Path("/workspace/meteors") |
| 16 | SHOTS = Path("/workspace/meteors-shots") |
| 17 | SHOTS.mkdir(parents=True, exist_ok=True) |
| 18 | PHP = os.environ.get("PHP", "/home/box/.local/bin/php") |
| 19 | CHROME = "/usr/bin/google-chrome" |
| 20 | CHROME_ARGS = [ |
| 21 | "--use-gl=angle", |
| 22 | "--use-angle=swiftshader", |
| 23 | "--no-sandbox", |
| 24 | "--disable-dev-shm-usage", |
| 25 | ] |
| 26 | FAILURES = [] |
| 27 | URL = None |
| 28 | |
| 29 | MOBILE = { |
| 30 | "viewport": {"width": 390, "height": 844}, |
| 31 | "device_scale_factor": 2, |
| 32 | "is_mobile": True, |
| 33 | "has_touch": True, |
| 34 | "user_agent": ( |
| 35 | "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) " |
| 36 | "AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 " |
| 37 | "Mobile/15E148 Safari/604.1" |
| 38 | ), |
| 39 | } |
| 40 | |
| 41 | |
| 42 | def check(cond, msg): |
| 43 | if cond: |
| 44 | print("OK ", msg) |
| 45 | else: |
| 46 | print("FAIL", msg) |
| 47 | FAILURES.append(msg) |
| 48 | return cond |
| 49 | |
| 50 | |
| 51 | def shot(page, name): |
| 52 | path = SHOTS / name |
| 53 | page.screenshot(path=str(path), full_page=False) |
| 54 | print(f"wrote {path} ({path.stat().st_size} bytes)") |
| 55 | return path |
| 56 | |
| 57 | |
| 58 | def free_port(): |
| 59 | s = socket.socket() |
| 60 | s.bind(("127.0.0.1", 0)) |
| 61 | port = s.getsockname()[1] |
| 62 | s.close() |
| 63 | return port |
| 64 | |
| 65 | |
| 66 | def start_php(port, data, log_path): |
| 67 | env = os.environ.copy() |
| 68 | env["METEORS_DATA_DIR"] = str(data) |
| 69 | log = open(log_path, "w") |
| 70 | proc = subprocess.Popen( |
| 71 | [PHP, "-S", f"127.0.0.1:{port}", "-t", str(ROOT / "build"), str(ROOT / "build" / "router.php")], |
| 72 | cwd=str(ROOT / "build"), |
| 73 | env=env, |
| 74 | stdout=log, |
| 75 | stderr=log, |
| 76 | ) |
| 77 | time.sleep(0.5) |
| 78 | if proc.poll() is not None: |
| 79 | err = Path(log_path).read_text(errors="replace") |
| 80 | raise RuntimeError("php -S failed: " + err) |
| 81 | return proc |
| 82 | |
| 83 | |
| 84 | def seed_points(data, user="local:test_user_1", points=2000): |
| 85 | script = Path(data) / "seed.php" |
| 86 | script.write_text(f"""<?php |
| 87 | define('METEORS_NO_SESSION', true); |
| 88 | putenv('METEORS_DATA_DIR={data}'); |
| 89 | require '{ROOT}/server/lib/bootstrap.php'; |
| 90 | require '{ROOT}/server/lib/Store.php'; |
| 91 | $s = Store::open('{data}'); |
| 92 | echo $s->awardPoints('{user}', {points}, 'seed-playwright', 'solo'); |
| 93 | """) |
| 94 | r = subprocess.run([PHP, str(script)], capture_output=True, text=True) |
| 95 | if r.returncode != 0: |
| 96 | raise RuntimeError("seed failed: " + r.stdout + "\n" + r.stderr) |
| 97 | print("seeded points, lifetime=", r.stdout.strip()) |
| 98 | return r.stdout.strip() |
| 99 | |
| 100 | |
| 101 | def login(page, n=1): |
| 102 | page.goto(f"{URL}auth.php?test={n}&next=index.html", wait_until="domcontentloaded", timeout=20000) |
| 103 | page.wait_for_timeout(200) |
| 104 | page.goto(f"{URL}index.html", wait_until="networkidle", timeout=20000) |
| 105 | page.wait_for_function( |
| 106 | "() => { const u = document.getElementById('menu-user'); return u && !u.hidden; }", |
| 107 | timeout=10000, |
| 108 | ) |
| 109 | page.wait_for_timeout(300) |
| 110 | |
| 111 | |
| 112 | |
| 113 | def show_custom(page, grid=None): |
| 114 | try: |
| 115 | page.evaluate( |
| 116 | """() => { |
| 117 | const c = document.querySelector('%s .planet-card.custom, .planet-card.custom'); |
| 118 | if (c) c.scrollIntoView({block: 'center', behavior: 'instant'}); |
| 119 | }""" % (grid or '#planet-select') |
| 120 | ) |
| 121 | page.wait_for_timeout(250) |
| 122 | except Exception as e: |
| 123 | print("show_custom:", e) |
| 124 | |
| 125 | def wait_vis(page, sel, timeout=8000): |
| 126 | page.locator(sel).wait_for(state="visible", timeout=timeout) |
| 127 | |
| 128 | |
| 129 | def fill_new_planet(page, name="Aurora", people="Aurorans"): |
| 130 | wait_vis(page, "#designer-edit") |
| 131 | page.locator("#des-name").fill(name) |
| 132 | page.locator("#des-people").fill(people) |
| 133 | page.locator("#des-gravity").fill("2.5") |
| 134 | page.locator("#des-air").fill("2") |
| 135 | page.locator("#des-mass").fill("1.2") |
| 136 | page.wait_for_timeout(150) |
| 137 | |
| 138 | |
| 139 | def main(): |
| 140 | global URL |
| 141 | port = free_port() |
| 142 | URL = f"http://127.0.0.1:{port}/" |
| 143 | data = Path(tempfile.mkdtemp(prefix="meteors-designer-")) |
| 144 | log_path = data / "php.log" |
| 145 | print("data", data, "url", URL) |
| 146 | proc = start_php(port, data, log_path) |
| 147 | try: |
| 148 | with sync_playwright() as p: |
| 149 | browser = p.chromium.launch( |
| 150 | executable_path=CHROME, args=CHROME_ARGS, headless=True |
| 151 | ) |
| 152 | desktop = browser.new_context(viewport={"width": 1280, "height": 800}) |
| 153 | page = desktop.new_page() |
| 154 | errors = [] |
| 155 | page.on("pageerror", lambda e: errors.append(str(e))) |
| 156 | page.on( |
| 157 | "console", |
| 158 | lambda m: errors.append(m.text) if m.type == "error" else None, |
| 159 | ) |
| 160 | |
| 161 | login(page) |
| 162 | check(page.locator("#btn-designer").is_visible(), "designer button on menu when logged in") |
| 163 | check("test_user_1" in page.locator("#menu-name").inner_text(), "menu shows login name") |
| 164 | shot(page, "t1139-menu-logged-in-1280.png") |
| 165 | |
| 166 | page.locator("#btn-designer").click() |
| 167 | wait_vis(page, "#designer") |
| 168 | page.wait_for_timeout(200) |
| 169 | shot(page, "t1139-designer-empty-1280.png") |
| 170 | |
| 171 | page.locator("#btn-designer-new").click() |
| 172 | fill_new_planet(page) |
| 173 | power = page.locator("#des-power").inner_text() |
| 174 | check("Power" in power, f"live Power preview: {power!r}") |
| 175 | check(page.locator(".des-slot").count() == 5, "five block slots") |
| 176 | shot(page, "t1139-designer-edit-1280.png") |
| 177 | |
| 178 | # Modal: defaults are Terra, so opening slot 0: other four Terra used (grey), rest locked. |
| 179 | page.locator(".des-slot").nth(0).click() |
| 180 | wait_vis(page, "#des-modal") |
| 181 | page.wait_for_timeout(200) |
| 182 | locked = page.locator(".des-block.locked").count() |
| 183 | used = page.locator(".des-block.used").count() |
| 184 | locks = page.locator(".des-lock").count() |
| 185 | check(locked > 20, f"many locked blocks ({locked})") |
| 186 | check(locks == locked, f"lock icon on every locked block ({locks} vs {locked})") |
| 187 | check(used == 4, f"other four Terra slots greyed as used ({used})") |
| 188 | check(page.locator(".des-block:not(.locked):not(.used)").count() >= 1, "current terra is selectable") |
| 189 | shot(page, "t1139-modal-locked-grey-1280.png") |
| 190 | page.locator("#des-modal-close").click() |
| 191 | page.wait_for_timeout(150) |
| 192 | |
| 193 | page.locator("#btn-designer-save").click() |
| 194 | wait_vis(page, "#designer") |
| 195 | page.wait_for_timeout(300) |
| 196 | cards = page.locator("#designer-list .designer-card") |
| 197 | check(cards.count() >= 1, "saved planet appears in the list") |
| 198 | check("Aurora" in page.locator("#designer-list").inner_text(), "list shows planet name") |
| 199 | shot(page, "t1139-designer-list-1280.png") |
| 200 | |
| 201 | # Seed points so some later blocks unlock, then reopen the modal. |
| 202 | seed_points(data, points=2000) |
| 203 | page.reload(wait_until="networkidle") |
| 204 | page.wait_for_function( |
| 205 | "() => { const u = document.getElementById('menu-user'); return u && !u.hidden; }", |
| 206 | timeout=10000, |
| 207 | ) |
| 208 | pts = page.locator("#menu-points").inner_text().strip() |
| 209 | check(pts == "2000" or int(pts) >= 2000, f"menu points after seed ({pts})") |
| 210 | page.locator("#btn-designer").click() |
| 211 | wait_vis(page, "#designer") |
| 212 | page.locator("[data-edit]").first.click() |
| 213 | wait_vis(page, "#designer-edit") |
| 214 | page.locator(".des-slot").nth(4).click() |
| 215 | wait_vis(page, "#des-modal") |
| 216 | page.wait_for_timeout(250) |
| 217 | unlocked = page.locator(".des-block:not(.locked)").count() |
| 218 | locked = page.locator(".des-block.locked").count() |
| 219 | check(unlocked > 8, f"after 2000 points, several blocks unlocked ({unlocked})") |
| 220 | check(locked > 10, f"later blocks still locked ({locked})") |
| 221 | # Pick the first unlocked unused non-current block (Fehu etc.) |
| 222 | pick = page.locator(".des-block:not(.locked):not(.used):not(.current)").first |
| 223 | check(pick.count() == 1, "an unlocked unused block is clickable") |
| 224 | pick.click() |
| 225 | page.wait_for_timeout(200) |
| 226 | check(page.locator("#des-modal").is_hidden(), "modal closes after pick") |
| 227 | page.locator("#btn-designer-save").click() |
| 228 | wait_vis(page, "#designer") |
| 229 | page.wait_for_timeout(200) |
| 230 | shot(page, "t1139-modal-mixed-unlocked-1280.png") # will re-open for a clean shot |
| 231 | |
| 232 | # Re-open modal just for a clear mixed locked/unlocked screenshot |
| 233 | page.locator("[data-edit]").first.click() |
| 234 | wait_vis(page, "#designer-edit") |
| 235 | page.locator(".des-slot").nth(0).click() |
| 236 | wait_vis(page, "#des-modal") |
| 237 | page.wait_for_timeout(200) |
| 238 | shot(page, "t1139-modal-mixed-unlocked-1280.png") |
| 239 | page.locator("#des-modal-close").click() |
| 240 | page.locator("#btn-designer-cancel").click() |
| 241 | page.locator("#btn-designer-back").click() |
| 242 | wait_vis(page, "#menu") |
| 243 | |
| 244 | # --- pickers in all three modes --- |
| 245 | page.locator("#btn-survival").click() |
| 246 | wait_vis(page, "#planet-select") |
| 247 | page.wait_for_timeout(250) |
| 248 | check(page.locator(".planet-card.custom").count() >= 1, "solo picker shows custom planet") |
| 249 | check("Aurora" in page.locator("#planet-grid").inner_text(), "solo picker lists Aurora") |
| 250 | check("Eigen" in page.locator(".custom-badge").first.inner_text(), "custom badge in solo picker") |
| 251 | shot(page, "t1139-picker-solo-1280.png") |
| 252 | page.locator("#btn-planet-back").click() |
| 253 | wait_vis(page, "#menu") |
| 254 | |
| 255 | page.locator("#btn-play").click() |
| 256 | wait_vis(page, "#planet-select") |
| 257 | page.wait_for_timeout(250) |
| 258 | check(page.locator(".planet-card.custom").count() >= 1, "offline picker shows custom planet") |
| 259 | shot(page, "t1139-picker-offline-1280.png") |
| 260 | page.locator("#btn-planet-back").click() |
| 261 | wait_vis(page, "#menu") |
| 262 | |
| 263 | page.locator("#btn-online").click() |
| 264 | wait_vis(page, "#online-panel") |
| 265 | page.wait_for_timeout(400) |
| 266 | if page.locator("#btn-create").is_visible(): |
| 267 | page.locator("#btn-create").click() |
| 268 | wait_vis(page, "#online-lobby") |
| 269 | page.wait_for_timeout(400) |
| 270 | check(page.locator("#lobby-planet-grid .planet-card.custom").count() >= 1, |
| 271 | "online lobby picker shows custom planet") |
| 272 | shot(page, "t1139-picker-online-1280.png") |
| 273 | if page.locator("#btn-leave-lobby").is_visible(): |
| 274 | page.locator("#btn-leave-lobby").click() |
| 275 | page.wait_for_timeout(300) |
| 276 | # back to menu |
| 277 | back = page.locator("[data-back-menu]") |
| 278 | if back.count() and back.first.is_visible(): |
| 279 | back.first.click() |
| 280 | else: |
| 281 | page.goto(f"{URL}index.html", wait_until="networkidle") |
| 282 | page.wait_for_timeout(400) |
| 283 | |
| 284 | # Custom planet in play (solo) |
| 285 | wait_vis(page, "#menu") |
| 286 | page.locator("#btn-survival").click() |
| 287 | wait_vis(page, "#planet-select") |
| 288 | page.locator(".planet-card.custom").first.click() |
| 289 | page.wait_for_timeout(150) |
| 290 | page.locator("#btn-planet-go").click() |
| 291 | page.wait_for_function( |
| 292 | "() => typeof app !== 'undefined' && app.mode === 'playing' && app.core", |
| 293 | timeout=10000, |
| 294 | ) |
| 295 | page.wait_for_timeout(5500) |
| 296 | name = page.evaluate("() => app.planet && app.planet.name") |
| 297 | custom = page.evaluate("() => !!(app.planet && app.planet.custom)") |
| 298 | check(name == "Aurora", f"playing on custom planet ({name!r})") |
| 299 | check(custom is True, "planet.custom is true in play") |
| 300 | shot(page, "t1139-custom-in-play-1280.png") |
| 301 | |
| 302 | # 1920 play + menu for consistency |
| 303 | page2 = desktop.new_page() # same context, still logged in? new page shares cookies |
| 304 | page2.set_viewport_size({"width": 1920, "height": 1080}) |
| 305 | # already mid-play on page; start fresh on page2 |
| 306 | page2.goto(f"{URL}index.html", wait_until="networkidle") |
| 307 | page2.wait_for_function( |
| 308 | "() => { const u = document.getElementById('menu-user'); return u && !u.hidden; }", |
| 309 | timeout=10000, |
| 310 | ) |
| 311 | shot(page2, "t1139-menu-logged-in-1920x1080.png") |
| 312 | page2.locator("#btn-survival").click() |
| 313 | wait_vis(page2, "#planet-select") |
| 314 | page2.wait_for_timeout(200) |
| 315 | shot(page2, "t1139-picker-solo-1920x1080.png") |
| 316 | page2.locator(".planet-card.custom").first.click() |
| 317 | page2.locator("#btn-planet-go").click() |
| 318 | page2.wait_for_function( |
| 319 | "() => typeof app !== 'undefined' && app.mode === 'playing'", |
| 320 | timeout=10000, |
| 321 | ) |
| 322 | page2.wait_for_timeout(5500) |
| 323 | shot(page2, "t1139-custom-in-play-1920x1080.png") |
| 324 | page2.close() |
| 325 | |
| 326 | # --- mobile --- |
| 327 | mobile = browser.new_context(**MOBILE) |
| 328 | m = mobile.new_page() |
| 329 | login(m) |
| 330 | shot(m, "t1139-menu-logged-in-390.png") |
| 331 | m.locator("#btn-designer").click() |
| 332 | wait_vis(m, "#designer") |
| 333 | m.wait_for_timeout(200) |
| 334 | shot(m, "t1139-designer-list-390.png") |
| 335 | m.locator("[data-edit]").first.click() |
| 336 | wait_vis(m, "#designer-edit") |
| 337 | m.wait_for_timeout(200) |
| 338 | shot(m, "t1139-designer-edit-390.png") |
| 339 | m.locator(".des-slot").nth(0).click() |
| 340 | wait_vis(m, "#des-modal") |
| 341 | m.wait_for_timeout(200) |
| 342 | check(m.locator(".des-lock").count() > 0, "mobile modal shows lock icons") |
| 343 | shot(m, "t1139-modal-locked-grey-390.png") |
| 344 | m.locator("#des-modal-close").click() |
| 345 | m.wait_for_timeout(150) |
| 346 | m.evaluate("() => { if (typeof Designer !== 'undefined') Designer.close(); }") |
| 347 | m.wait_for_timeout(200) |
| 348 | if not m.locator("#menu").is_visible(): |
| 349 | m.goto(f"{URL}index.html", wait_until="networkidle") |
| 350 | m.wait_for_function( |
| 351 | "() => { const u = document.getElementById('menu-user'); return u && !u.hidden; }", |
| 352 | timeout=10000, |
| 353 | ) |
| 354 | wait_vis(m, "#menu") |
| 355 | m.locator("#btn-survival").click() |
| 356 | wait_vis(m, "#planet-select") |
| 357 | m.wait_for_timeout(200) |
| 358 | check(m.locator(".planet-card.custom").count() >= 1, "mobile solo picker shows custom") |
| 359 | show_custom(m) |
| 360 | shot(m, "t1139-picker-solo-390.png") |
| 361 | m.locator("#btn-planet-back").click() |
| 362 | wait_vis(m, "#menu") |
| 363 | m.locator("#btn-play").click() |
| 364 | wait_vis(m, "#planet-select") |
| 365 | m.wait_for_timeout(200) |
| 366 | show_custom(m) |
| 367 | shot(m, "t1139-picker-offline-390.png") |
| 368 | m.locator("#btn-planet-back").click() |
| 369 | m.locator("#btn-online").click() |
| 370 | wait_vis(m, "#online-panel") |
| 371 | m.wait_for_timeout(400) |
| 372 | if m.locator("#btn-create").is_visible(): |
| 373 | m.locator("#btn-create").click() |
| 374 | wait_vis(m, "#online-lobby") |
| 375 | m.wait_for_timeout(400) |
| 376 | check(m.locator("#lobby-planet-grid .planet-card.custom").count() >= 1, |
| 377 | "mobile online picker shows custom") |
| 378 | show_custom(m) |
| 379 | shot(m, "t1139-picker-online-390.png") |
| 380 | |
| 381 | if errors: |
| 382 | print("page errors:", errors) |
| 383 | # fail only on real exceptions, not leftover console |
| 384 | real = [e for e in errors if "Failed to load" not in e] |
| 385 | if real: |
| 386 | FAILURES.append("page errors: " + "; ".join(real[:5])) |
| 387 | |
| 388 | browser.close() |
| 389 | finally: |
| 390 | proc.terminate() |
| 391 | try: |
| 392 | proc.wait(timeout=3) |
| 393 | except Exception: |
| 394 | proc.kill() |
| 395 | print("php log:\n", log_path.read_text(errors="replace")[-2000:]) |
| 396 | |
| 397 | print("\n{} failures".format(len(FAILURES))) |
| 398 | for f in FAILURES: |
| 399 | print(" -", f) |
| 400 | return 0 if not FAILURES else 1 |
| 401 | |
| 402 | |
| 403 | if __name__ == "__main__": |
| 404 | sys.exit(main()) |
projects-timfalken-com meteors