| 1 | #!/usr/bin/env python3 |
| 2 | """t1141: flag picker, translations, persistence, no overflow.""" |
| 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 | # Expected Play-button text per locale (menu.play) |
| 42 | PLAY = { |
| 43 | "en": "Play", |
| 44 | "nl": "Spelen", |
| 45 | "de": "Spielen", |
| 46 | "fr": "Jouer", |
| 47 | "ja": "プレイ", |
| 48 | "zh": "开始游戏", |
| 49 | "ko": "플레이", |
| 50 | "pl": "Graj", |
| 51 | "ru": "Играть", |
| 52 | "es": "Jugar", |
| 53 | } |
| 54 | FLAGS = { |
| 55 | "en": "gb", "nl": "nl", "de": "de", "fr": "fr", "ja": "jp", |
| 56 | "zh": "cn", "ko": "kr", "pl": "pl", "ru": "ru", "es": "es", |
| 57 | } |
| 58 | |
| 59 | |
| 60 | def check(cond, msg): |
| 61 | if cond: |
| 62 | print("OK ", msg) |
| 63 | else: |
| 64 | print("FAIL", msg) |
| 65 | FAILURES.append(msg) |
| 66 | return cond |
| 67 | |
| 68 | |
| 69 | def shot(page, name): |
| 70 | path = SHOTS / name |
| 71 | page.screenshot(path=str(path), full_page=False) |
| 72 | print(f"wrote {path} ({path.stat().st_size} bytes)") |
| 73 | return path |
| 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 start_php(port, data, log_path): |
| 85 | env = os.environ.copy() |
| 86 | env["METEORS_DATA_DIR"] = str(data) |
| 87 | log = open(log_path, "w") |
| 88 | proc = subprocess.Popen( |
| 89 | [PHP, "-S", f"127.0.0.1:{port}", "-t", str(ROOT / "build"), str(ROOT / "build" / "router.php")], |
| 90 | cwd=str(ROOT / "build"), |
| 91 | env=env, |
| 92 | stdout=log, |
| 93 | stderr=log, |
| 94 | ) |
| 95 | time.sleep(0.5) |
| 96 | if proc.poll() is not None: |
| 97 | err = Path(log_path).read_text(errors="replace") |
| 98 | raise RuntimeError("php -S failed: " + err) |
| 99 | return proc |
| 100 | |
| 101 | |
| 102 | def login(page, n=1): |
| 103 | page.goto(f"{URL}auth.php?test={n}&next=index.html", wait_until="domcontentloaded", timeout=20000) |
| 104 | page.wait_for_timeout(200) |
| 105 | page.goto(f"{URL}index.html", wait_until="networkidle", timeout=20000) |
| 106 | page.wait_for_function( |
| 107 | "() => { const u = document.getElementById('menu-user'); return u && !u.hidden; }", |
| 108 | timeout=10000, |
| 109 | ) |
| 110 | page.wait_for_timeout(400) |
| 111 | |
| 112 | |
| 113 | def no_overflow(page, label): |
| 114 | info = page.evaluate( |
| 115 | """() => ({ |
| 116 | sw: document.documentElement.scrollWidth, |
| 117 | iw: window.innerWidth, |
| 118 | sh: document.documentElement.scrollHeight, |
| 119 | ih: window.innerHeight, |
| 120 | bodyOverflow: document.body.scrollWidth, |
| 121 | })""" |
| 122 | ) |
| 123 | ok = info["sw"] <= info["iw"] + 2 and info["bodyOverflow"] <= info["iw"] + 2 |
| 124 | check(ok, f"no overflow {label}: scrollW={info['sw']} innerW={info['iw']}") |
| 125 | return ok |
| 126 | |
| 127 | |
| 128 | def goto_fresh(page, clear=True): |
| 129 | if clear: |
| 130 | page.goto(f"{URL}index.html", wait_until="domcontentloaded", timeout=20000) |
| 131 | page.evaluate( |
| 132 | """() => { |
| 133 | try { localStorage.removeItem('meteors-locale'); } catch (e) {} |
| 134 | document.cookie = 'meteors-locale=; path=/; max-age=0'; |
| 135 | }""" |
| 136 | ) |
| 137 | page.reload(wait_until="networkidle", timeout=20000) |
| 138 | else: |
| 139 | page.goto(f"{URL}index.html", wait_until="networkidle", timeout=20000) |
| 140 | page.wait_for_selector("#btn-play", timeout=10000) |
| 141 | page.wait_for_timeout(200) |
| 142 | |
| 143 | |
| 144 | def current_play(page): |
| 145 | # CSS text-transform: uppercase on the Play button |
| 146 | return page.locator("#btn-play").inner_text().strip() |
| 147 | |
| 148 | def play_eq(got, expected, msg): |
| 149 | check(got.casefold() == expected.casefold(), msg) |
| 150 | |
| 151 | |
| 152 | def set_lang(page, loc): |
| 153 | page.locator("#lang-current").click() |
| 154 | page.wait_for_selector("#lang-list:not([hidden])", timeout=4000) |
| 155 | page.locator(f'#lang-list .lang-option[data-locale="{loc}"]').click() |
| 156 | page.wait_for_timeout(250) |
| 157 | page.wait_for_function( |
| 158 | f"() => document.documentElement.getAttribute('data-locale') === '{loc}'", |
| 159 | timeout=4000, |
| 160 | ) |
| 161 | |
| 162 | |
| 163 | def run_desktop(browser, ctx_kwargs=None): |
| 164 | ctx = browser.new_context( |
| 165 | viewport={"width": 1280, "height": 800}, |
| 166 | device_scale_factor=1, |
| 167 | **(ctx_kwargs or {}), |
| 168 | ) |
| 169 | page = ctx.new_page() |
| 170 | goto_fresh(page) |
| 171 | |
| 172 | # Default English |
| 173 | play = current_play(page) |
| 174 | play_eq(play, "Play", f"default Play is English, got {play!r}") |
| 175 | html_lang = page.evaluate("() => document.documentElement.lang") |
| 176 | check(html_lang == "en", f"html lang default en, got {html_lang}") |
| 177 | stored = page.evaluate("() => localStorage.getItem('meteors-locale')") |
| 178 | # After init, default en is written; that's ok. Fresh clear then reload should still be en. |
| 179 | src = page.locator("#lang-current img").get_attribute("src") or "" |
| 180 | check("gb.svg" in src, f"UK flag on current, src={src}") |
| 181 | no_overflow(page, "desktop-en-closed") |
| 182 | shot(page, "t1141-desktop-menu-en.png") |
| 183 | |
| 184 | # Open picker: 10 flags |
| 185 | page.locator("#lang-current").click() |
| 186 | page.wait_for_selector("#lang-list:not([hidden])", timeout=4000) |
| 187 | n = page.locator("#lang-list .lang-option").count() |
| 188 | check(n == 10, f"picker shows 10 flags, got {n}") |
| 189 | for loc, flag in FLAGS.items(): |
| 190 | el = page.locator(f'#lang-list .lang-option[data-locale="{loc}"] img') |
| 191 | check(el.count() == 1, f"flag for {loc}") |
| 192 | if el.count(): |
| 193 | s = el.get_attribute("src") or "" |
| 194 | check(flag + ".svg" in s, f"{loc} uses {flag}.svg, src={s}") |
| 195 | no_overflow(page, "desktop-picker-open") |
| 196 | shot(page, "t1141-desktop-picker-open.png") |
| 197 | # close by clicking current again or Escape |
| 198 | page.keyboard.press("Escape") |
| 199 | page.wait_for_timeout(150) |
| 200 | |
| 201 | # Cycle a few languages |
| 202 | for loc in ("nl", "de", "fr", "ja", "zh", "ko", "pl", "ru", "es"): |
| 203 | set_lang(page, loc) |
| 204 | txt = current_play(page) |
| 205 | play_eq(txt, PLAY[loc], f"Play in {loc} is {PLAY[loc]!r}, got {txt!r}") |
| 206 | src = page.locator("#lang-current img").get_attribute("src") or "" |
| 207 | check(FLAGS[loc] + ".svg" in src, f"current flag {loc} = {FLAGS[loc]}") |
| 208 | no_overflow(page, f"desktop-{loc}") |
| 209 | shot(page, f"t1141-desktop-menu-{loc}.png") |
| 210 | |
| 211 | # Persistence via localStorage |
| 212 | set_lang(page, "nl") |
| 213 | stored = page.evaluate("() => localStorage.getItem('meteors-locale')") |
| 214 | check(stored == "nl", f"localStorage meteors-locale=nl, got {stored!r}") |
| 215 | page.reload(wait_until="networkidle", timeout=20000) |
| 216 | page.wait_for_selector("#btn-play", timeout=10000) |
| 217 | page.wait_for_timeout(250) |
| 218 | txt = current_play(page) |
| 219 | play_eq(txt, "Spelen", f"after reload still Dutch, got {txt!r}") |
| 220 | shot(page, "t1141-desktop-persist-nl.png") |
| 221 | |
| 222 | # Login: server locale wins if set; first login pushes local |
| 223 | login(page, 1) |
| 224 | # After login applyFromProfile: if server null, client keeps nl and syncs |
| 225 | page.wait_for_timeout(500) |
| 226 | txt = current_play(page) |
| 227 | play_eq(txt, "Spelen", f"logged in keeps Dutch (or applies profile), got {txt!r}") |
| 228 | # Switch to Japanese while logged in — should POST set_locale |
| 229 | set_lang(page, "ja") |
| 230 | txt = current_play(page) |
| 231 | play_eq(txt, PLAY["ja"], f"logged-in Japanese Play, got {txt!r}") |
| 232 | shot(page, "t1141-desktop-logged-in-ja.png") |
| 233 | # New context: no localStorage, login should load ja from profile |
| 234 | ctx2 = browser.new_context(viewport={"width": 1280, "height": 800}, device_scale_factor=1) |
| 235 | p2 = ctx2.new_page() |
| 236 | login(p2, 1) |
| 237 | p2.wait_for_timeout(600) |
| 238 | txt2 = current_play(p2) |
| 239 | play_eq(txt2, PLAY["ja"], f"fresh context login applies server ja, got {txt2!r}") |
| 240 | shot(p2, "t1141-desktop-relogin-ja.png") |
| 241 | ctx2.close() |
| 242 | ctx.close() |
| 243 | |
| 244 | |
| 245 | def run_mobile(browser): |
| 246 | ctx = browser.new_context(**MOBILE) |
| 247 | page = ctx.new_page() |
| 248 | goto_fresh(page) |
| 249 | play = current_play(page) |
| 250 | play_eq(play, "Play", f"mobile default English, got {play!r}") |
| 251 | no_overflow(page, "mobile-en") |
| 252 | shot(page, "t1141-mobile-menu-en.png") |
| 253 | |
| 254 | page.locator("#lang-current").click() |
| 255 | page.wait_for_selector("#lang-list:not([hidden])", timeout=4000) |
| 256 | n = page.locator("#lang-list .lang-option").count() |
| 257 | check(n == 10, f"mobile picker 10 flags, got {n}") |
| 258 | # list should be on screen (opens upward) |
| 259 | box = page.locator("#lang-list").bounding_box() |
| 260 | vp = page.viewport_size |
| 261 | if box: |
| 262 | on = box["y"] >= -2 and box["y"] + box["height"] <= vp["height"] + 4 |
| 263 | check(on, f"mobile picker on-screen y={box['y']} h={box['height']} vp={vp['height']}") |
| 264 | no_overflow(page, "mobile-picker-open") |
| 265 | shot(page, "t1141-mobile-picker-open.png") |
| 266 | page.keyboard.press("Escape") |
| 267 | page.wait_for_timeout(150) |
| 268 | |
| 269 | for loc in ("nl", "de", "ja", "zh", "ru"): |
| 270 | set_lang(page, loc) |
| 271 | txt = current_play(page) |
| 272 | play_eq(txt, PLAY[loc], f"mobile Play {loc} = {PLAY[loc]!r}, got {txt!r}") |
| 273 | no_overflow(page, f"mobile-{loc}") |
| 274 | shot(page, f"t1141-mobile-menu-{loc}.png") |
| 275 | ctx.close() |
| 276 | |
| 277 | |
| 278 | def main(): |
| 279 | global URL |
| 280 | port = free_port() |
| 281 | URL = f"http://127.0.0.1:{port}/" |
| 282 | data = Path(tempfile.mkdtemp(prefix="meteors-i18n-")) |
| 283 | log_path = "/tmp/t1141-php.log" |
| 284 | proc = start_php(port, data, log_path) |
| 285 | print("PHP at", URL, "data", data) |
| 286 | try: |
| 287 | with sync_playwright() as p: |
| 288 | browser = p.chromium.launch(executable_path=CHROME, args=CHROME_ARGS, headless=True) |
| 289 | run_desktop(browser) |
| 290 | run_mobile(browser) |
| 291 | browser.close() |
| 292 | finally: |
| 293 | proc.terminate() |
| 294 | try: |
| 295 | proc.wait(timeout=3) |
| 296 | except Exception: |
| 297 | proc.kill() |
| 298 | |
| 299 | if FAILURES: |
| 300 | print("\nFAILURES:") |
| 301 | for f in FAILURES: |
| 302 | print(" -", f) |
| 303 | return 1 |
| 304 | print("\nALL OK") |
| 305 | return 0 |
| 306 | |
| 307 | |
| 308 | if __name__ == "__main__": |
| 309 | sys.exit(main()) |
projects-timfalken-com meteors