| 1 | #!/usr/bin/env python3 |
| 2 | """t1143: custom-planet solo after ?online=1 login, WebGL start failure, lifetime points.""" |
| 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 | |
| 30 | def check(cond, msg): |
| 31 | print(("OK " if cond else "FAIL") + " " + msg) |
| 32 | if not cond: |
| 33 | FAILURES.append(msg) |
| 34 | return cond |
| 35 | |
| 36 | |
| 37 | def shot(page, name): |
| 38 | path = SHOTS / name |
| 39 | page.screenshot(path=str(path), full_page=False) |
| 40 | print(f"wrote {path} ({path.stat().st_size})") |
| 41 | |
| 42 | |
| 43 | def free_port(): |
| 44 | s = socket.socket() |
| 45 | s.bind(("127.0.0.1", 0)) |
| 46 | port = s.getsockname()[1] |
| 47 | s.close() |
| 48 | return port |
| 49 | |
| 50 | |
| 51 | def start_php(port, data, log_path): |
| 52 | env = os.environ.copy() |
| 53 | env["METEORS_DATA_DIR"] = str(data) |
| 54 | log = open(log_path, "w") |
| 55 | proc = subprocess.Popen( |
| 56 | [PHP, "-S", f"127.0.0.1:{port}", "-t", str(ROOT / "build"), str(ROOT / "build" / "router.php")], |
| 57 | cwd=str(ROOT / "build"), |
| 58 | env=env, |
| 59 | stdout=log, |
| 60 | stderr=log, |
| 61 | ) |
| 62 | time.sleep(0.5) |
| 63 | if proc.poll() is not None: |
| 64 | raise RuntimeError("php failed: " + Path(log_path).read_text(errors="replace")) |
| 65 | return proc |
| 66 | |
| 67 | |
| 68 | def login(page, next_url="index.html?online=1"): |
| 69 | page.goto(f"{URL}auth.php?test=1&next={next_url}", wait_until="domcontentloaded", timeout=20000) |
| 70 | page.wait_for_function("() => typeof Online !== 'undefined' && Online.me", timeout=15000) |
| 71 | page.wait_for_timeout(400) |
| 72 | |
| 73 | |
| 74 | def back_to_menu(page): |
| 75 | vis = page.locator("#online-home [data-back-menu]") |
| 76 | if vis.count() and vis.first.is_visible(): |
| 77 | vis.first.click() |
| 78 | else: |
| 79 | page.evaluate( |
| 80 | """() => { |
| 81 | document.getElementById('online-panel').hidden = true; |
| 82 | document.getElementById('menu').hidden = false; |
| 83 | if (window.app) { app.mode = 'menu'; app.online = false; } |
| 84 | if (typeof Online !== 'undefined' && Online.stopPolling) Online.stopPolling(); |
| 85 | }""" |
| 86 | ) |
| 87 | page.wait_for_selector("#btn-play", timeout=8000) |
| 88 | page.wait_for_timeout(200) |
| 89 | |
| 90 | |
| 91 | def set_lang(page, loc): |
| 92 | page.locator("#lang-current").click() |
| 93 | page.wait_for_selector("#lang-list:not([hidden])", timeout=4000) |
| 94 | page.locator(f'#lang-list .lang-option[data-locale="{loc}"]').click() |
| 95 | page.wait_for_timeout(250) |
| 96 | |
| 97 | |
| 98 | def field_ok(page): |
| 99 | return page.evaluate( |
| 100 | """() => { |
| 101 | const c = document.querySelector('body > canvas'); |
| 102 | const r = c ? c.getBoundingClientRect() : null; |
| 103 | return { |
| 104 | mode: window.app && app.mode, |
| 105 | cells: window.app && app.cells && app.cells.length, |
| 106 | canvasW: r && r.width, |
| 107 | canvasH: r && r.height, |
| 108 | planetHud: (document.getElementById('planet-name')||{}).textContent, |
| 109 | planetId: window.app && app.planet && app.planet.id, |
| 110 | planetName: window.app && app.planet && app.planet.name, |
| 111 | hudHidden: document.getElementById('hud') && document.getElementById('hud').hidden, |
| 112 | menuHidden: document.getElementById('menu') && document.getElementById('menu').hidden, |
| 113 | token: window.app && app.playToken, |
| 114 | }; |
| 115 | }""" |
| 116 | ) |
| 117 | |
| 118 | |
| 119 | def test_custom_and_points(page): |
| 120 | login(page, "index.html?online=1") |
| 121 | check("online=1" in page.url, f"landed with ?online=1 ({page.url})") |
| 122 | back_to_menu(page) |
| 123 | set_lang(page, "nl") |
| 124 | check(page.locator("#btn-designer").is_visible(), "designer visible") |
| 125 | |
| 126 | page.locator("#btn-designer").click() |
| 127 | page.locator("#designer").wait_for(state="visible", timeout=8000) |
| 128 | page.locator("#btn-designer-new").click() |
| 129 | page.locator("#designer-edit").wait_for(state="visible", timeout=8000) |
| 130 | page.locator("#des-name").fill("Testplaneet Ada") |
| 131 | page.locator("#des-people").fill("Adanen") |
| 132 | page.locator("#btn-designer-save").click() |
| 133 | page.wait_for_function( |
| 134 | "() => (document.getElementById('designer-list')||{}).innerText.includes('Ada')", |
| 135 | timeout=10000, |
| 136 | ) |
| 137 | page.locator("#btn-designer-back").click() |
| 138 | page.wait_for_selector("#btn-play", timeout=8000) |
| 139 | |
| 140 | page.locator("#btn-play").click() |
| 141 | page.wait_for_selector("#planet-select:not([hidden])", timeout=8000) |
| 142 | custom = page.locator("#planet-grid .planet-card.custom") |
| 143 | check(custom.count() >= 1, "custom planet in Spelen picker") |
| 144 | custom.first.scroll_into_view_if_needed() |
| 145 | custom.first.click() |
| 146 | page.wait_for_timeout(200) |
| 147 | page.locator("#btn-planet-go").click() |
| 148 | page.wait_for_timeout(1500) |
| 149 | st = field_ok(page) |
| 150 | print("custom play", st) |
| 151 | check(st["mode"] == "playing", f"mode playing, got {st['mode']}") |
| 152 | check((st["cells"] or 0) >= 130, f"field cells {st['cells']}") |
| 153 | check((st["canvasW"] or 0) > 100 and (st["canvasH"] or 0) > 100, f"canvas {st['canvasW']}x{st['canvasH']}") |
| 154 | check("Testplaneet Ada" in (st["planetHud"] or ""), f"HUD is custom planet, got {st['planetHud']!r}") |
| 155 | check("Terra" not in (st["planetHud"] or "").replace("Testplaneet", ""), f"HUD is not Terra: {st['planetHud']!r}") |
| 156 | shot(page, "t1143-local-custom-solo-field.png") |
| 157 | |
| 158 | # Lifetime points: wait for play_begin token, min 3s, then force game over. |
| 159 | page.wait_for_function("() => window.app && app.playToken", timeout=8000) |
| 160 | page.wait_for_timeout(3500) |
| 161 | page.evaluate( |
| 162 | """() => { |
| 163 | app.core.score = 12; |
| 164 | app.core.state = 'gameover'; |
| 165 | app.core.gameOverTime = 0; |
| 166 | }""" |
| 167 | ) |
| 168 | # finishPlaySession runs on first gameover tick; then returnToMenu after 2s |
| 169 | page.wait_for_function( |
| 170 | "() => document.getElementById('menu') && !document.getElementById('menu').hidden", |
| 171 | timeout=8000, |
| 172 | ) |
| 173 | page.wait_for_timeout(800) |
| 174 | pts = page.locator("#menu-points").inner_text().strip() |
| 175 | print("lifetime after game over", pts) |
| 176 | check(int(pts) >= 12, f"solo game-over awards lifetime points, got {pts}") |
| 177 | shot(page, "t1143-local-points-after-gameover.png") |
| 178 | |
| 179 | |
| 180 | def test_webgl_failure(page): |
| 181 | # Fresh logged-out (or stay logged in — doesn't matter). Reload menu. |
| 182 | page.goto(f"{URL}index.html", wait_until="networkidle", timeout=20000) |
| 183 | page.wait_for_selector("#btn-play", timeout=10000) |
| 184 | page.wait_for_timeout(400) |
| 185 | page.evaluate( |
| 186 | """() => { |
| 187 | // ES6 `class Scene` is lexical, not window.Scene — hook getContext |
| 188 | // so GLContext throws the same 'WebGL is not supported' as a GPU blocklist. |
| 189 | const proto = HTMLCanvasElement.prototype; |
| 190 | if (!window.__OrigGetContext) window.__OrigGetContext = proto.getContext; |
| 191 | proto.getContext = function (type) { |
| 192 | const t = String(type || ''); |
| 193 | if (t.indexOf('webgl') !== -1 || t === 'experimental-webgl') |
| 194 | { return null; } |
| 195 | return window.__OrigGetContext.apply(this, arguments); |
| 196 | }; |
| 197 | }""" |
| 198 | ) |
| 199 | page.locator("#btn-play").click() |
| 200 | page.wait_for_selector("#planet-select:not([hidden])", timeout=8000) |
| 201 | page.locator("#btn-planet-go").click() |
| 202 | page.wait_for_timeout(600) |
| 203 | info = page.evaluate( |
| 204 | """() => ({ |
| 205 | menuHidden: document.getElementById('menu').hidden, |
| 206 | menuDisplay: getComputedStyle(document.getElementById('menu')).display, |
| 207 | hudHidden: document.getElementById('hud').hidden, |
| 208 | hudPlanet: document.getElementById('planet-name').textContent, |
| 209 | err: (document.getElementById('start-error')||{}).textContent, |
| 210 | errHidden: document.getElementById('start-error').hidden, |
| 211 | orphan: !!document.getElementById('canvas_orphan_t1143'), |
| 212 | bodyCanvas: document.querySelectorAll('body > canvas').length, |
| 213 | mode: window.app && app.mode, |
| 214 | })""" |
| 215 | ) |
| 216 | print("webgl fail state", info) |
| 217 | check(info["menuHidden"] is False and info["menuDisplay"] != "none", "returned to menu on WebGL fail") |
| 218 | check(info["hudHidden"] is True, "HUD not left visible with default Terra") |
| 219 | check(info["errHidden"] is False and "WebGL" in (info["err"] or ""), f"WebGL error shown: {info['err']!r}") |
| 220 | check(info["bodyCanvas"] == 0, f"orphan canvas removed, body canvases={info['bodyCanvas']}") |
| 221 | check(info["mode"] == "menu", f"app.mode menu, got {info['mode']}") |
| 222 | shot(page, "t1143-local-webgl-error.png") |
| 223 | page.evaluate("""() => { |
| 224 | if (window.__OrigGetContext) |
| 225 | { HTMLCanvasElement.prototype.getContext = window.__OrigGetContext; } |
| 226 | }""") |
| 227 | |
| 228 | |
| 229 | def main(): |
| 230 | global URL |
| 231 | port = free_port() |
| 232 | URL = f"http://127.0.0.1:{port}/" |
| 233 | data = Path(tempfile.mkdtemp(prefix="meteors-t1143-")) |
| 234 | proc = start_php(port, data, "/tmp/t1143-test-php.log") |
| 235 | print("PHP", URL, data) |
| 236 | try: |
| 237 | with sync_playwright() as p: |
| 238 | browser = p.chromium.launch(executable_path=CHROME, args=CHROME_ARGS, headless=True) |
| 239 | ctx = browser.new_context(viewport={"width": 1280, "height": 800}) |
| 240 | page = ctx.new_page() |
| 241 | errs = [] |
| 242 | page.on("pageerror", lambda e: errs.append(str(e))) |
| 243 | test_custom_and_points(page) |
| 244 | test_webgl_failure(page) |
| 245 | print("pageerrors", errs) |
| 246 | # WebGL mock throws — startPlay catches it, should not be an unhandled? |
| 247 | # abortStartPlay console.errors but the throw is caught. Good. |
| 248 | ctx.close() |
| 249 | browser.close() |
| 250 | finally: |
| 251 | proc.terminate() |
| 252 | try: |
| 253 | proc.wait(timeout=3) |
| 254 | except Exception: |
| 255 | proc.kill() |
| 256 | if FAILURES: |
| 257 | print("FAILURES:") |
| 258 | for f in FAILURES: |
| 259 | print(" -", f) |
| 260 | return 1 |
| 261 | print("ALL OK") |
| 262 | return 0 |
| 263 | |
| 264 | |
| 265 | if __name__ == "__main__": |
| 266 | sys.exit(main()) |
projects-timfalken-com meteors