#!/usr/bin/env python3 """t1246: startup login prompt with Play as Guest. Usage: python3 test/t1246-login-prompt-playwright.py URL TAG Logged-out checks run against the live site too. With a local PHP server (auth.php?test=1) the logged-in path is also checked. """ import sys from pathlib import Path from playwright.sync_api import sync_playwright URL = sys.argv[1] if len(sys.argv) > 1 else "https://projects.timfalken.com/meteors/" if not URL.endswith("/"): URL += "/" TAG = sys.argv[2] if len(sys.argv) > 2 else "live" LOCAL = "127.0.0.1" in URL or "localhost" in URL SHOTS = Path("/workspace/meteors-shots") SHOTS.mkdir(parents=True, exist_ok=True) fails = [] def check(c, m): print(("OK " if c else "FAIL ") + m) if not c: fails.append(m) def shot(page, name): page.screenshot(path=str(SHOTS / f"t1246-{TAG}-{name}.png")) def wait_ready(page): page.wait_for_function("() => window.app && app.loginPromptReady === true", timeout=20000) def prompt_visible(page): return page.evaluate("() => { const e = document.getElementById('login-prompt'); return !!e && !e.hidden; }") def logged_out(br, name, vp, mobile): ctx = br.new_context(viewport=vp, is_mobile=mobile, has_touch=mobile, device_scale_factor=2 if mobile else 1) page = ctx.new_page() errs = [] page.on("pageerror", lambda e: errs.append(str(e))) page.goto(URL + "index.html", wait_until="networkidle") wait_ready(page) check(prompt_visible(page), f"{name}: prompt visible for a logged-out player") title = page.locator("#login-prompt-title").inner_text().strip() guest = page.locator("#btn-play-guest").inner_text().strip() login_href = page.locator("#btn-login-prompt").get_attribute("href") check(title.lower() in ("log in?", "inloggen?"), f"{name}: title is a login question ({title!r})") check(guest.lower() in ("play as guest", "speel als gast"), f"{name}: guest button {guest!r}") check(login_href == "auth.php", f"{name}: Log in uses auth.php (menu return), got {login_href!r}") box = page.locator("#login-prompt").bounding_box() btn = page.locator("#btn-login-prompt").bounding_box() guest_box = page.locator("#btn-play-guest").bounding_box() if box and btn and guest_box: check(btn["y"] < guest_box["y"], f"{name}: Play as Guest sits under Log in") check(guest_box["height"] >= 40 and btn["height"] >= 40, f"{name}: touch targets tall enough ({btn['height']:.0f}/{guest_box['height']:.0f})") check(btn["x"] + btn["width"] <= vp["width"] + 1 and btn["x"] >= -1, f"{name}: Log in fits the viewport") check(guest_box["x"] + guest_box["width"] <= vp["width"] + 1, f"{name}: guest button fits the viewport") shot(page, f"{name}-prompt-en") # Language follows the existing picker / I18n. page.evaluate("() => I18n.setLocale('nl')") page.wait_for_timeout(200) check(page.locator("#login-prompt-title").inner_text().strip() == "Inloggen?", f"{name}: Dutch title") check(page.locator("#btn-play-guest").inner_text().strip() == "Speel als gast", f"{name}: Dutch guest button") login_nl = page.locator("#btn-login-prompt").inner_text().strip() check(login_nl.lower() == "inloggen", f"{name}: Dutch login button ({login_nl!r})") shot(page, f"{name}-prompt-nl") page.evaluate("() => I18n.setLocale('en')") page.wait_for_timeout(200) # Clicking Log in remembers the session choice before leaving. page.evaluate("() => { window.__loginNav = null; document.getElementById('btn-login-prompt').addEventListener('click', (e) => { e.preventDefault(); window.__loginNav = document.getElementById('btn-login-prompt').getAttribute('href'); }, { once: true }); }") if mobile: page.locator("#btn-login-prompt").tap() else: page.locator("#btn-login-prompt").click() remembered = page.evaluate("() => sessionStorage.getItem('meteors-login-prompt')") nav = page.evaluate("() => window.__loginNav") check(remembered == "1", f"{name}: clicking Log in stores the session flag ({remembered!r})") check(nav == "auth.php", f"{name}: Log in would go to auth.php ({nav!r})") # Play as Guest closes the prompt and shows the main menu. # Flag is already set from the login click; guest just hides it. if mobile: page.locator("#btn-play-guest").tap() else: page.locator("#btn-play-guest").click() page.wait_for_function("() => { const e = document.getElementById('login-prompt'); return !e || e.hidden; }", timeout=3000) check(not prompt_visible(page), f"{name}: prompt hidden after Play as Guest") check(page.evaluate("() => !document.getElementById('menu').hidden && app.mode === 'menu'"), f"{name}: main menu visible after guest") shot(page, f"{name}-menu-guest") # Same session (reload): do not ask again. page.reload(wait_until="networkidle") wait_ready(page) check(not prompt_visible(page), f"{name}: reload in the same tab does not show the prompt again") check(page.evaluate("() => !document.getElementById('menu').hidden"), f"{name}: menu still visible after reload") # Returning from the auth callback URL must not show the prompt. page.goto(URL + "index.html?online=1", wait_until="networkidle") wait_ready(page) check(not prompt_visible(page), f"{name}: ?online=1 (auth return) does not show the prompt") check("online=1" not in page.url, f"{name}: ?online=1 still stripped from the address") check(not errs, f"{name}: no page errors {errs[:2]}") ctx.close() # Fresh session (new context) asks again. ctx2 = br.new_context(viewport=vp, is_mobile=mobile, has_touch=mobile, device_scale_factor=2 if mobile else 1) page2 = ctx2.new_page() page2.goto(URL + "index.html", wait_until="networkidle") wait_ready(page2) check(prompt_visible(page2), f"{name}: a fresh session shows the prompt again") shot(page2, f"{name}-fresh-session") ctx2.close() def logged_in(br, name, vp, mobile): ctx = br.new_context(viewport=vp, is_mobile=mobile, has_touch=mobile, device_scale_factor=2 if mobile else 1) page = ctx.new_page() errs = [] page.on("pageerror", lambda e: errs.append(str(e))) page.goto(URL + "auth.php?test=1&next=index.html", wait_until="domcontentloaded") page.goto(URL + "index.html", wait_until="networkidle") page.wait_for_function("() => typeof Online !== 'undefined' && Online.me", timeout=15000) wait_ready(page) check(not prompt_visible(page), f"{name}: logged-in player never sees the prompt") check(page.evaluate("() => !document.getElementById('menu').hidden"), f"{name}: logged-in player lands on the main menu") name_txt = page.locator("#menu-name").inner_text().strip() check("test_user_1" in name_txt, f"{name}: menu shows the logged-in name ({name_txt!r})") shot(page, f"{name}-logged-in") check(not errs, f"{name}: logged-in no page errors {errs[:2]}") ctx.close() with sync_playwright() as pw: br = pw.chromium.launch(args=["--use-gl=angle", "--use-angle=swiftshader"]) for name, vp, mobile in (("desktop", {"width": 1280, "height": 800}, False), ("mobile", {"width": 390, "height": 844}, True)): logged_out(br, name, vp, mobile) if LOCAL: logged_in(br, name, vp, mobile) br.close() print("ALL OK" if not fails else f"{len(fails)} FAIL") for f in fails: print(" -", f) sys.exit(1 if fails else 0)