Files
CyberGril/web/tests/browser_check.py
T
2026-10-19 22:50:29 +08:00

181 lines
13 KiB
Python

"""Foreground browser regression using preinstalled Python Playwright; no npm test dependency."""
import json
import os
from pathlib import Path
import socket
import subprocess
import time
import urllib.request
from playwright.sync_api import sync_playwright
ROOT = Path(__file__).resolve().parents[1]
OUT = Path(os.environ.get('CYBERGRIL_ARTIFACT_DIR', ROOT / 'qa-artifacts'))
OUT.mkdir(parents=True, exist_ok=True)
with socket.socket() as sock:
sock.bind(('127.0.0.1', 0))
PORT = sock.getsockname()[1]
BASE = f'http://127.0.0.1:{PORT}'
log = open(OUT / 'vite.log', 'w')
server = subprocess.Popen(['node', 'node_modules/vite/bin/vite.js', '--host', '127.0.0.1', '--port', str(PORT), '--strictPort'], cwd=ROOT, stdout=log, stderr=log)
report = {'checks': [], 'errors': [], 'browser': '', 'samples': {}}
def check(name, condition):
assert condition, name
report['checks'].append(name)
print('PASS', name, flush=True)
def screenshot(page, name):
page.screenshot(path=str(OUT / (name + '.png')), full_page=True)
try:
for _ in range(60):
try:
urllib.request.urlopen(BASE, timeout=.5)
break
except Exception:
time.sleep(.1)
with sync_playwright() as pw:
browser = pw.chromium.launch(args=['--no-sandbox', '--enable-unsafe-swiftshader'])
report['browser'] = browser.version
page = browser.new_page(viewport={'width': 1600, 'height': 900}, device_scale_factor=1)
page.on('pageerror', lambda e: report['errors'].append(str(e)))
# Unreachable probe is a deterministic offline case, not a test-only app mode.
page.route('**/v1/models', lambda route: route.abort())
page.goto(BASE)
page.wait_for_function("document.querySelector('#connection').textContent.includes('非实时模型')")
page.wait_for_timeout(5100)
check('unreachable probe selects labelled demo', '演示' in page.locator('#connection').inner_text())
check('real DOM + permanent engine disclosure', page.locator('.badge').is_visible() and page.locator('.message p').count() == 2)
check('renderer starts', page.locator('body').get_attribute('data-renderer') in ['webgl', 'canvas'])
screenshot(page, 'desktop-1600')
page.locator('#input').fill('中文输入法')
page.locator('#input').dispatch_event('compositionstart')
page.locator('#input').press('Enter')
check('IME Enter does not submit', page.locator('.message.user').count() == 0)
page.locator('#input').dispatch_event('compositionend')
page.locator('#input').press('Shift+Enter')
check('Shift Enter preserves newline', '\n' in page.locator('#input').input_value())
page.locator('#input').fill('今天有点累')
page.locator('#input').press('Enter')
page.locator('#cancel').wait_for(state='visible')
page.locator('#input').fill('流式中继续快速输入,不应丢字')
screenshot(page, 'desktop-stream-focus')
check('input stays editable during stream', page.locator('#input').input_value() == '流式中继续快速输入,不应丢字')
page.locator('#cancel').click()
page.locator('.error').wait_for()
check('cancel keeps partial and offers retry', '已取消' in page.locator('.error').inner_text())
screenshot(page, 'desktop-cancel-retry')
page.locator('.error button').click()
page.locator('#cancel').wait_for(state='hidden', timeout=10000)
check('manual retry succeeds without duplicate user turn', page.locator('.message.user').count() == 1 and page.locator('.error').count() == 0)
page.locator('#input').fill('长' * 500)
check('500 character input available', page.locator('#input').input_value() == '长' * 500)
page.locator('#input').fill('')
page.locator('#voice').click(force=True)
check('unavailable voice explains itself', '不会启动麦克风' in page.locator('#toast').inner_text())
page.locator('#collapse').click()
check('collapse restores conversation entry', page.locator('#restore').is_visible() and not page.locator('.panel').is_visible())
page.locator('#restore').click()
page.locator('[data-view=daily]').click()
check('daily has explicitly marked demo moments', page.locator('#preview-content small').count() == 3)
screenshot(page, 'daily-preview')
page.locator('[data-view=fate]').click()
check('fate is preview only', '未接入排盘引擎' in page.locator('#preview-content').inner_text())
page.locator('[data-view=chat]').click()
page.locator('#settings-open').click()
original = page.evaluate("getComputedStyle(document.documentElement).getPropertyValue('--accent')")
page.get_by_role('button', name='静听蓝城', exact=True).click()
check('preset keeps warm UI accent', original == page.evaluate("getComputedStyle(document.documentElement).getPropertyValue('--accent')"))
page.locator('#quality').select_option('static')
page.wait_for_function("document.body.dataset.renderer === 'static'")
check('static stops scene', page.locator('#effects').get_attribute('data-running') == 'false')
page.locator('#quality').select_option('lite')
page.wait_for_function("document.body.dataset.renderer === 'canvas'")
check('lite uses independent Canvas2D', page.locator('#effects canvas').count() == 1)
page.locator('#rain-sound').check()
check('rain only starts on user action', '已开启' in page.locator('#audio-status').inner_text())
page.locator('#volume').fill('50')
page.locator('#rain-sound').uncheck()
page.locator('#settings-close').click()
page.set_viewport_size({'width':1280,'height':800})
screenshot(page,'desktop-1280')
page.set_viewport_size({'width':390,'height':844})
page.evaluate("document.body.classList.remove('expanded')")
screenshot(page,'mobile-collapsed')
check('mobile default drawer near 30 percent', abs(page.locator('.panel').bounding_box()['height'] / 844 - .30) < .02)
page.locator('#drawer').click()
screenshot(page,'mobile-expanded')
check('mobile expansion at or below 55 percent', page.locator('.panel').bounding_box()['y'] >= 844*.45-1)
page.locator('#input').focus()
screenshot(page,'mobile-focus')
page.set_viewport_size({'width':844,'height':390})
screenshot(page,'mobile-landscape')
check('landscape is scrollable scene above chat', page.locator('.panel').bounding_box()['y'] >= 300)
check('no horizontal overflow', page.evaluate('document.documentElement.scrollWidth <= innerWidth'))
page.set_viewport_size({'width':1600,'height':900})
page.emulate_media(reduced_motion='reduce')
page.wait_for_function("document.body.dataset.renderer === 'static'")
screenshot(page,'reduced-motion')
check('reduced motion enforces static', page.locator('#effects').get_attribute('data-running') == 'false')
page.emulate_media(reduced_motion='no-preference')
page.wait_for_function("document.querySelector('#effects').dataset.running === 'true'")
# Visibility lifecycle event injected because headless tabs do not reliably hide.
page.evaluate("Object.defineProperty(document, 'hidden', {configurable:true,get:()=>true}); document.dispatchEvent(new Event('visibilitychange'))")
check('visibility handler pauses scene and CSS', page.locator('#effects').get_attribute('data-running') == 'false' and 'paused' in page.locator('body').get_attribute('class'))
page.evaluate("Object.defineProperty(document, 'hidden', {configurable:true,get:()=>false}); document.dispatchEvent(new Event('visibilitychange'))")
check('visibility handler restores selected animation', page.locator('#effects').get_attribute('data-running') == 'true')
# True API availability failure injection, no app query override.
for name, blocked, expected in [('webgl-fallback', ['webgl','webgl2','experimental-webgl'], 'canvas'), ('canvas-fallback', ['webgl','webgl2','experimental-webgl','2d'], 'css')]:
ctx = browser.new_context(viewport={'width':1280,'height':800})
ctx.add_init_script("const original=HTMLCanvasElement.prototype.getContext; HTMLCanvasElement.prototype.getContext=function(type,...args){if("+json.dumps(blocked)+".includes(type))return null;return original.call(this,type,...args)}")
p = ctx.new_page(); p.route('**/v1/models', lambda route: route.abort()); p.goto(BASE)
p.wait_for_function(f"document.body.dataset.renderer === '{expected}'",timeout=12000)
check(name, True); screenshot(p,name); ctx.close()
# Live mode cases use model-compatible SSE fixtures.
page.unroute('**/v1/models')
page.route('**/v1/models', lambda route: route.fulfill(json={'data':[{'id':'halogen-qwen3.8-flash-next'}]}))
page.locator('#mode').click()
page.wait_for_function("document.querySelector('#connection').textContent.includes('已连接')")
def completion(route):
report['last_request'] = route.request.post_data_json
route.fulfill(content_type='text/event-stream', body='data: {"choices":[{"delta":{"content":"<script>模型文本</script>中文"}}]}\r\n\r\ndata: [DONE]\r\n\r\n')
page.route('**/v1/chat/completions', completion)
page.locator('#input').fill('实时测试'); page.locator('#send').click()
page.wait_for_function("document.querySelector('#chat-status').textContent === '回应已完成'")
check('live SSE renders unsafe-looking text as text', '<script>模型文本</script>' in page.locator('.message.assistant').last.inner_text() and page.locator('.message script').count()==0)
check('live request excludes demo replies', all('窗外又下雨' not in m['content'] for m in report['last_request']['messages']))
page.unroute('**/v1/chat/completions')
page.route('**/v1/chat/completions', lambda route: route.fulfill(status=503,body='Unavailable'))
page.locator('#input').fill('失败测试'); page.locator('#send').click(); page.locator('.error').wait_for()
check('upstream error visible with retry', '503' in page.locator('.error').inner_text())
screenshot(page,'live-failure')
page.unroute('**/v1/chat/completions')
page.route('**/v1/chat/completions', completion)
page.locator('.error button').click(); page.wait_for_function("document.querySelector('#chat-status').textContent === '回应已完成'")
check('live retry clears failure', page.locator('.error').count()==0)
page.unroute('**/v1/chat/completions')
page.route('**/v1/chat/completions', lambda route: route.fulfill(content_type='text/event-stream',body='data: {"choices":[{"delta":{"content":"已经收到的一部分"}}]}\n\n'))
page.locator('#input').fill('中断测试'); page.locator('#send').click(); page.locator('.error').wait_for()
check('truncated SSE keeps partial text', '已经收到的一部分' in page.locator('.message.assistant').last.inner_text() and '中断' in page.locator('.error').inner_text())
# Browser scheduling sample with chat input during independent animation.
page.locator('#settings-open').click(); page.locator('#quality').select_option('full'); page.locator('#settings-close').click()
page.wait_for_timeout(1000)
sample = page.evaluate("""async () => {const gaps=[],tasks=[]; const po=new PerformanceObserver(l=>tasks.push(...l.getEntries().map(e=>e.duration)));po.observe({type:'longtask'});let last=performance.now();await new Promise(resolve=>{const end=last+4000;function frame(t){gaps.push(t-last);last=t;if(t<end)requestAnimationFrame(frame);else resolve()}requestAnimationFrame(frame)});po.disconnect();gaps.sort((a,b)=>a-b);return {frames:gaps.length,intervalP95:gaps[Math.floor(gaps.length*.95)],longTasks:tasks,renderer:document.body.dataset.renderer}}""")
report['samples']['chromium1600'] = sample
check('no unhandled page errors', not report['errors'])
browser.close()
# Firefox scope is reported accurately if the installed browser cannot launch.
try:
firefox = pw.firefox.launch()
fp = firefox.new_page(viewport={'width':1280,'height':800})
fp.route('**/v1/models',lambda route:route.abort());fp.goto(BASE); fp.wait_for_timeout(1000)
screenshot(fp,'firefox-1280'); report['firefox'] = firefox.version
check('Firefox shell usable',fp.locator('#input').is_visible());firefox.close()
except Exception as exc:
report['firefox_unavailable'] = str(exc)[:500]
finally:
server.terminate()
server.wait(timeout=10)
log.close()
(OUT/'browser-report.json').write_text(json.dumps(report,ensure_ascii=False,indent=2))