"""Check built shared pages using mocked APIs only; no production network access. Serve h5/dist on localhost:10086 first. Requires Python Playwright + Chromium. """ from pathlib import Path import json from playwright.sync_api import sync_playwright BASE = "http://127.0.0.1:10086" OUT = Path(__file__).resolve().parents[1] / "tmp" / "account-ux" OUT.mkdir(parents=True, exist_ok=True) def run(): results = [] with sync_playwright() as p: browser = p.chromium.launch(headless=True) for width in (320, 390, 430): context = browser.new_context(viewport={"width": width, "height": 844}, is_mobile=True) errors = [] state = {"offline": False, "favorites_error": True} def route_request(route): url = route.request.url if not url.startswith(BASE + "/"): return route.abort() if not any(prefix in url for prefix in ("/api/", "/ebook-api", "/community-api")): return route.continue_() payload = {} status = 200 if url.endswith("/api/config"): payload = {"config": {"version": 99, "flags": {"offlineBadge": True}}} elif url.endswith("/api/config/meta"): payload = {"version": 99} elif url.endswith("/auth/presence"): payload = {"openid": "ux-test", "isVip": True} status = 503 if state["offline"] else 200 elif url.endswith("/auth/profile/stats"): payload = {"favorites_count": 0, "destinations_explored": 3, "member_since": "2026-01", "nomad_level": "探索者"} elif url.endswith("/auth/favorites/detail"): payload = [] status = 503 if state["favorites_error"] else 200 elif "/config/stream" in url: status = 503 route.fulfill(status=status, content_type="application/json", body=json.dumps(payload)) context.route("**/*", route_request) page = context.new_page() page.on("pageerror", lambda error: errors.append(str(error))) page.goto(BASE + "/#/pages/home/index") toggle = page.locator(".hub-theme-toggle") toggle.wait_for() assert toggle.bounding_box()["height"] >= 44 assert page.locator(".hub-portal-card").count() > 0 toggle.click() assert page.locator(".hub-root").get_attribute("data-theme") == "light" page.wait_for_function("getComputedStyle(document.querySelector('.hub-root')).backgroundColor === 'rgb(248, 250, 252)'") page.screenshot(path=str(OUT / f"home-light-{width}.png"), full_page=True) page.goto(BASE + "/#/pages/mine/index") page.get_by_text("未登录", exact=True).wait_for() assert page.locator(".profile-header .btn").bounding_box()["height"] >= 44 page.evaluate("""() => { localStorage.setItem('nomadro_token', JSON.stringify({data:'mock-token'})); localStorage.setItem('nomadro_openid', JSON.stringify({data:'ux-test'})); }""") page.reload() page.get_by_text("收藏暂未同步,请刷新重试", exact=True).wait_for() assert page.get_by_text("探索目的地", exact=True).count() == 0 page.screenshot(path=str(OUT / f"account-partial-{width}.png"), full_page=True) state["offline"] = True page.get_by_text("刷新", exact=True).click() page.get_by_text("同步失败,请检查网络后重试", exact=True).wait_for() assert page.get_by_text("退出登录", exact=True).count() == 1 assert page.evaluate("localStorage.getItem('nomadro_token')") is not None state["offline"] = False state["favorites_error"] = False page.get_by_text("重试同步", exact=True).click() page.get_by_text("探索目的地", exact=True).wait_for() assert page.locator(".profile-stats").evaluate("el => el.scrollWidth <= el.clientWidth") assert page.evaluate("document.documentElement.scrollWidth <= innerWidth") page.screenshot(path=str(OUT / f"account-restored-{width}.png"), full_page=True) page.get_by_text("退出登录", exact=True).click() page.get_by_text("未登录", exact=True).wait_for() assert page.evaluate("localStorage.getItem('nomadro_token')") is None assert not errors, errors results.append({"width": width, "ok": True}) context.close() browser.close() print(json.dumps(results)) if __name__ == "__main__": run()