53 lines
2.7 KiB
Python
53 lines
2.7 KiB
Python
"""Capability detection, title warnings and the future 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', 'required']:
|
|
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')}}"
|
|
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'});")
|
|
if case == 'required':
|
|
def require(route):
|
|
response = route.fetch()
|
|
route.fulfill(response=response, body=response.text().replace(
|
|
'const REQUIRE_GPU_AND_THREADS = false;', 'const REQUIRE_GPU_AND_THREADS = true;'))
|
|
page.route('**/index.html', require)
|
|
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 == 'required':
|
|
assert not page.evaluate('canStartGame()')
|
|
page.evaluate('startNewWorld(424242, 512, 384)')
|
|
assert not page.evaluate('GAME.running || GAME.loading')
|
|
assert not page.evaluate('startFromSave({})')
|
|
else:
|
|
assert page.evaluate('canStartGame()')
|
|
if case == 'absent':
|
|
Path('shots').mkdir(exist_ok=True)
|
|
page.screenshot(path='shots/t88-p1-notice.png')
|
|
page.evaluate("SET.look='pixel'; startNewWorld(424242, 512, 384)")
|
|
page.wait_for_function('GAME.running && !GAME.loading', timeout=60000)
|
|
assert not errors, errors
|
|
print(f'PASS capabilities: {case}')
|