"""Capability detection, title notices and the required-WebGPU startup gate.""" from pathlib import Path import sys sys.path.insert(0, str(Path(__file__).resolve().parent.parent / 'tools')) from use_venv import ensure_playwright ensure_playwright() from browser import capabilities, consume_gpu_option, launch consume_gpu_option() for case in ['native', 'absent', 'null', 'reject', 'firefox', 'limits']: with launch() as (_, page, url): errors = [] page.on('pageerror', lambda e: errors.append(str(e))) if case != 'native': gpu = 'undefined' if case == 'absent' else "{requestAdapter: async () => null}" if case == 'reject': gpu = "{requestAdapter: async () => {throw new Error('adapter denied')}}" if case == 'limits': gpu="{requestAdapter:async()=>({limits:{maxStorageTexturesPerShaderStage:3}})}" page.add_init_script(f"Object.defineProperty(navigator, 'gpu', {{value: {gpu}}});") page.add_init_script("Object.defineProperty(self, 'crossOriginIsolated', {value: false});") if case == 'firefox': page.add_init_script("Object.defineProperty(navigator, 'userAgent', {value: 'Firefox/157.0'});") page.goto(url) caps = capabilities(page) page.wait_for_function("!$('title').hidden") if case == 'native': assert caps['isolated'] assert page.locator('#cap-notice').is_hidden() == caps['webgpu'] else: assert not caps['webgpu'] and caps['adapterInfo'] is None notice = page.inner_text('#cap-notice') assert 'python3 tools/serve.py' in notice and "opened from a file it can't" in notice assert ('Firefox Nightly' in notice and 'dom.webgpu.enabled' in notice) if case == 'firefox' else 'Chrome or Edge' in notice if case != 'native': assert not page.evaluate('canStartGame()') page.evaluate('startNewWorld(424242, 512, 384)') assert not page.evaluate('GAME.running || GAME.loading') assert not page.evaluate('startFromSave({})') if case == 'absent': Path('shots').mkdir(exist_ok=True) page.screenshot(path='shots/t88-p1-notice.png') assert not errors, errors print(f'PASS capabilities: {case}')