| 1 | #!/usr/bin/env python3 |
| 2 | """t1246: startup login prompt with Play as Guest. |
| 3 | |
| 4 | Usage: python3 test/t1246-login-prompt-playwright.py URL TAG |
| 5 | Logged-out checks run against the live site too. With a local PHP |
| 6 | server (auth.php?test=1) the logged-in path is also checked. |
| 7 | """ |
| 8 | import sys |
| 9 | from pathlib import Path |
| 10 | from playwright.sync_api import sync_playwright |
| 11 | |
| 12 | URL = sys.argv[1] if len(sys.argv) > 1 else "https://projects.timfalken.com/meteors/" |
| 13 | if not URL.endswith("/"): |
| 14 | URL += "/" |
| 15 | TAG = sys.argv[2] if len(sys.argv) > 2 else "live" |
| 16 | LOCAL = "127.0.0.1" in URL or "localhost" in URL |
| 17 | SHOTS = Path("/workspace/meteors-shots") |
| 18 | SHOTS.mkdir(parents=True, exist_ok=True) |
| 19 | fails = [] |
| 20 | |
| 21 | def check(c, m): |
| 22 | print(("OK " if c else "FAIL ") + m) |
| 23 | if not c: |
| 24 | fails.append(m) |
| 25 | |
| 26 | def shot(page, name): |
| 27 | page.screenshot(path=str(SHOTS / f"t1246-{TAG}-{name}.png")) |
| 28 | |
| 29 | def wait_ready(page): |
| 30 | page.wait_for_function("() => window.app && app.loginPromptReady === true", timeout=20000) |
| 31 | |
| 32 | def prompt_visible(page): |
| 33 | return page.evaluate("() => { const e = document.getElementById('login-prompt'); return !!e && !e.hidden; }") |
| 34 | |
| 35 | def logged_out(br, name, vp, mobile): |
| 36 | ctx = br.new_context(viewport=vp, is_mobile=mobile, has_touch=mobile, device_scale_factor=2 if mobile else 1) |
| 37 | page = ctx.new_page() |
| 38 | errs = [] |
| 39 | page.on("pageerror", lambda e: errs.append(str(e))) |
| 40 | page.goto(URL + "index.html", wait_until="networkidle") |
| 41 | wait_ready(page) |
| 42 | check(prompt_visible(page), f"{name}: prompt visible for a logged-out player") |
| 43 | title = page.locator("#login-prompt-title").inner_text().strip() |
| 44 | guest = page.locator("#btn-play-guest").inner_text().strip() |
| 45 | login_href = page.locator("#btn-login-prompt").get_attribute("href") |
| 46 | check(title.lower() in ("log in?", "inloggen?"), f"{name}: title is a login question ({title!r})") |
| 47 | check(guest.lower() in ("play as guest", "speel als gast"), f"{name}: guest button {guest!r}") |
| 48 | check(login_href == "auth.php", f"{name}: Log in uses auth.php (menu return), got {login_href!r}") |
| 49 | box = page.locator("#login-prompt").bounding_box() |
| 50 | btn = page.locator("#btn-login-prompt").bounding_box() |
| 51 | guest_box = page.locator("#btn-play-guest").bounding_box() |
| 52 | if box and btn and guest_box: |
| 53 | check(btn["y"] < guest_box["y"], f"{name}: Play as Guest sits under Log in") |
| 54 | check(guest_box["height"] >= 40 and btn["height"] >= 40, f"{name}: touch targets tall enough ({btn['height']:.0f}/{guest_box['height']:.0f})") |
| 55 | check(btn["x"] + btn["width"] <= vp["width"] + 1 and btn["x"] >= -1, f"{name}: Log in fits the viewport") |
| 56 | check(guest_box["x"] + guest_box["width"] <= vp["width"] + 1, f"{name}: guest button fits the viewport") |
| 57 | shot(page, f"{name}-prompt-en") |
| 58 | |
| 59 | # Language follows the existing picker / I18n. |
| 60 | page.evaluate("() => I18n.setLocale('nl')") |
| 61 | page.wait_for_timeout(200) |
| 62 | check(page.locator("#login-prompt-title").inner_text().strip() == "Inloggen?", f"{name}: Dutch title") |
| 63 | check(page.locator("#btn-play-guest").inner_text().strip() == "Speel als gast", f"{name}: Dutch guest button") |
| 64 | login_nl = page.locator("#btn-login-prompt").inner_text().strip() |
| 65 | check(login_nl.lower() == "inloggen", f"{name}: Dutch login button ({login_nl!r})") |
| 66 | shot(page, f"{name}-prompt-nl") |
| 67 | page.evaluate("() => I18n.setLocale('en')") |
| 68 | page.wait_for_timeout(200) |
| 69 | |
| 70 | # Clicking Log in remembers the session choice before leaving. |
| 71 | 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 }); }") |
| 72 | if mobile: |
| 73 | page.locator("#btn-login-prompt").tap() |
| 74 | else: |
| 75 | page.locator("#btn-login-prompt").click() |
| 76 | remembered = page.evaluate("() => sessionStorage.getItem('meteors-login-prompt')") |
| 77 | nav = page.evaluate("() => window.__loginNav") |
| 78 | check(remembered == "1", f"{name}: clicking Log in stores the session flag ({remembered!r})") |
| 79 | check(nav == "auth.php", f"{name}: Log in would go to auth.php ({nav!r})") |
| 80 | |
| 81 | # Play as Guest closes the prompt and shows the main menu. |
| 82 | # Flag is already set from the login click; guest just hides it. |
| 83 | if mobile: |
| 84 | page.locator("#btn-play-guest").tap() |
| 85 | else: |
| 86 | page.locator("#btn-play-guest").click() |
| 87 | page.wait_for_function("() => { const e = document.getElementById('login-prompt'); return !e || e.hidden; }", timeout=3000) |
| 88 | check(not prompt_visible(page), f"{name}: prompt hidden after Play as Guest") |
| 89 | check(page.evaluate("() => !document.getElementById('menu').hidden && app.mode === 'menu'"), f"{name}: main menu visible after guest") |
| 90 | shot(page, f"{name}-menu-guest") |
| 91 | |
| 92 | # Same session (reload): do not ask again. |
| 93 | page.reload(wait_until="networkidle") |
| 94 | wait_ready(page) |
| 95 | check(not prompt_visible(page), f"{name}: reload in the same tab does not show the prompt again") |
| 96 | check(page.evaluate("() => !document.getElementById('menu').hidden"), f"{name}: menu still visible after reload") |
| 97 | |
| 98 | # Returning from the auth callback URL must not show the prompt. |
| 99 | page.goto(URL + "index.html?online=1", wait_until="networkidle") |
| 100 | wait_ready(page) |
| 101 | check(not prompt_visible(page), f"{name}: ?online=1 (auth return) does not show the prompt") |
| 102 | check("online=1" not in page.url, f"{name}: ?online=1 still stripped from the address") |
| 103 | |
| 104 | check(not errs, f"{name}: no page errors {errs[:2]}") |
| 105 | ctx.close() |
| 106 | |
| 107 | # Fresh session (new context) asks again. |
| 108 | ctx2 = br.new_context(viewport=vp, is_mobile=mobile, has_touch=mobile, device_scale_factor=2 if mobile else 1) |
| 109 | page2 = ctx2.new_page() |
| 110 | page2.goto(URL + "index.html", wait_until="networkidle") |
| 111 | wait_ready(page2) |
| 112 | check(prompt_visible(page2), f"{name}: a fresh session shows the prompt again") |
| 113 | shot(page2, f"{name}-fresh-session") |
| 114 | ctx2.close() |
| 115 | |
| 116 | def logged_in(br, name, vp, mobile): |
| 117 | ctx = br.new_context(viewport=vp, is_mobile=mobile, has_touch=mobile, device_scale_factor=2 if mobile else 1) |
| 118 | page = ctx.new_page() |
| 119 | errs = [] |
| 120 | page.on("pageerror", lambda e: errs.append(str(e))) |
| 121 | page.goto(URL + "auth.php?test=1&next=index.html", wait_until="domcontentloaded") |
| 122 | page.goto(URL + "index.html", wait_until="networkidle") |
| 123 | page.wait_for_function("() => typeof Online !== 'undefined' && Online.me", timeout=15000) |
| 124 | wait_ready(page) |
| 125 | check(not prompt_visible(page), f"{name}: logged-in player never sees the prompt") |
| 126 | check(page.evaluate("() => !document.getElementById('menu').hidden"), f"{name}: logged-in player lands on the main menu") |
| 127 | name_txt = page.locator("#menu-name").inner_text().strip() |
| 128 | check("test_user_1" in name_txt, f"{name}: menu shows the logged-in name ({name_txt!r})") |
| 129 | shot(page, f"{name}-logged-in") |
| 130 | check(not errs, f"{name}: logged-in no page errors {errs[:2]}") |
| 131 | ctx.close() |
| 132 | |
| 133 | with sync_playwright() as pw: |
| 134 | br = pw.chromium.launch(args=["--use-gl=angle", "--use-angle=swiftshader"]) |
| 135 | for name, vp, mobile in (("desktop", {"width": 1280, "height": 800}, False), ("mobile", {"width": 390, "height": 844}, True)): |
| 136 | logged_out(br, name, vp, mobile) |
| 137 | if LOCAL: |
| 138 | logged_in(br, name, vp, mobile) |
| 139 | br.close() |
| 140 | |
| 141 | print("ALL OK" if not fails else f"{len(fails)} FAIL") |
| 142 | for f in fails: |
| 143 | print(" -", f) |
| 144 | sys.exit(1 if fails else 0) |
projects-timfalken-com meteors