"""Community browser regressions; external requests are mocked or blocked.""" import json from pathlib import Path from playwright.sync_api import sync_playwright BASE = 'http://127.0.0.1:10086' OUT = Path(__file__).resolve().parents[1] / 'tmp' / 'social-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, writes = [], [] state = {'roster': None, 'fail_save': False, 'fail_send': True, 'messages': [], 'fail_apply': True, 'reviews': []} def route_request(route): request = route.request url = request.url if '/api/social/v1/' in url: payload, status = {}, 200 if '/realtime/token' in url: payload = {'ok': True, 'enabled': False} elif '/roster/me' in url: if request.method == 'PUT': writes.append(('roster', request.post_data_json)) if state['fail_save']: status = 503 else: state['roster'] = request.post_data_json payload = state['roster'] or {} else: payload = {'item': state['roster']} elif '/messages' in url: if request.method == 'POST': body = request.post_data_json writes.append(('message', body)) if state['fail_send']: status = 503 payload = {**body, 'id': 'msg_saved', 'channelId': 'general', 'guestId': request.headers.get('x-social-guest-id'), 'at': '2026-11-02T00:00:00.000Z'} if status == 200: state['messages'] = [payload] else: payload = {'items': state['messages']} elif '/reviews' in url: if request.method == 'POST': payload = {**request.post_data_json, 'id': 'review_saved', 'at': '2026-11-02T00:00:00.000Z'} state['reviews'] = [payload] else: payload = {'items': state['reviews']} elif '/apply' in url: writes.append(('apply', request.post_data_json)) payload = {'id': 'application_saved'} if state['fail_apply']: status = 503 return route.fulfill(status=status, content_type='application/json', body=json.dumps(payload)) if not url.startswith(BASE + '/'): return route.abort() if '/api/' in url or '/ebook-api' in url: payload = {} if '/api/config' in url: payload = {'config': {'version': 99, 'modules': {'social': {'enabled': True}}, 'flags': {'socialFollow': False}}} elif '/social-join-status' in url: payload = {'ok': True, 'isMember': False, 'enabled': False, 'priceFen': 4900} return route.fulfill(status=200, content_type='application/json', body=json.dumps(payload)) return route.continue_() context.route('**/*', route_request) page = context.new_page() page.on('pageerror', lambda error: errors.append(str(error))) routes = ['index', 'people/index', 'people/index?mode=cafe', 'people/profile/index?id=m01', 'date/index', 'team/index', 'roster/index', 'chat/index', 'meetup/index', 'meetup-live/index'] for index, route in enumerate(routes): page.goto(BASE + '/#/pages/social/' + route) page.locator('.soc-taro-root').wait_for() page.wait_for_timeout(250) assert page.locator('.soc-taro-root').inner_text().strip(), route assert page.evaluate('document.documentElement.scrollWidth <= innerWidth + 1'), (width, route, 'overflow') page.screenshot(path=str(OUT / f'{width}-{index}.png'), full_page=True) if page.locator('.social-theme-control').count(): page.locator('.social-theme-control').click() page.wait_for_function("document.querySelector('.soc-taro-root').dataset.theme === 'light'") page.wait_for_timeout(500) if index == 1: assert page.locator('.social-people-chip').first.evaluate("el => getComputedStyle(el).backgroundColor === 'rgb(255, 255, 255)'") page.screenshot(path=str(OUT / f'{width}-{index}-light.png'), full_page=True) page.locator('.social-theme-control').click() page.goto(BASE + '/#/pages/social/people/index') page.get_by_role('button', name='关注', exact=True).first.click() page.reload() page.get_by_role('button', name='取消关注', exact=True).first.wait_for() page.locator('.social-search-row input').fill('no-such-person') assert page.locator('.social-people-chip').count() == 0 page.get_by_role('button', name='清空筛选', exact=True).click() assert page.locator('.social-people-chip').count() > 0 page.goto(BASE + '/#/pages/social/date/index') page.locator('.social-date-act.like').click() page.wait_for_timeout(350) page.locator('.social-date-act.undo').click() page.goto(BASE + '/#/pages/social/team/index') page.get_by_role('button', name='我想参与 ↗', exact=True).first.click() page.locator('.social-apply-sheet input').nth(0).fill('Applicant') page.locator('.social-apply-sheet input').nth(1).fill('contact@example.invalid') page.get_by_role('button', name='发送参与申请', exact=True).click() page.get_by_role('button', name='重试提交', exact=True).wait_for() state['fail_apply'] = False page.get_by_role('button', name='重试提交', exact=True).click() page.get_by_text('已表达兴趣', exact=True).wait_for() apply_writes = [body for kind, body in writes if kind == 'apply'] assert apply_writes[0]['requestId'] == apply_writes[1]['requestId'] page.goto(BASE + '/#/pages/social/people/index?mode=cafe') page.get_by_role('button', name='查看与分享评价', exact=True).first.click() page.locator('.social-sheet-panel textarea').fill('Useful workspace') page.get_by_role('button', name='发表评价', exact=True).click() page.get_by_text('Useful workspace', exact=True).wait_for() page.get_by_role('button', name='关闭评价', exact=True).click() page.goto(BASE + '/#/pages/social/index') page.get_by_role('tab', name='共创', exact=True).click() page.locator('.social-ugc-card').first.click() page.get_by_role('dialog').wait_for() page.get_by_role('button', name='关闭介绍', exact=True).click() page.get_by_role('button', name='社区故事', exact=False).click() page.get_by_role('dialog', name='社区故事').wait_for() page.locator('.connection-video-content header').click() assert page.get_by_role('dialog', name='社区故事').is_visible() page.get_by_role('dialog', name='社区故事').get_by_role('button', name='关闭', exact=True).click() page.locator('.social-join-btn').count() # Joining must never send a mock payment order. page.get_by_role('button', name='加入社区', exact=True).first.click() page.get_by_role('dialog', name='加入社区').wait_for() page.locator('.social-join-sheet-submit').wait_for() assert page.locator('.social-join-sheet-submit').is_disabled() page.get_by_role('dialog', name='加入社区').get_by_role('button', name='关闭', exact=True).click() page.goto(BASE + '/#/pages/social/roster/index') page.locator('#profile_name input').wait_for() page.locator('#profile_name input').fill('Test Nomad') page.locator('#profile_city input').fill('Dali') state['fail_save'] = True page.get_by_role('button', name='保存到社区', exact=True).click() page.get_by_text('同步未完成,草稿已保留,可以稍后重试', exact=True).wait_for() page.reload() page.wait_for_timeout(400) assert page.locator('#profile_name input').input_value() == 'Test Nomad' state['fail_save'] = False page.get_by_role('button', name='保存到社区', exact=True).click() page.get_by_text('已同步到社区', exact=True).wait_for() assert state['roster']['city'] == 'Dali' page.goto(BASE + '/#/pages/social/chat/index') page.locator('.im-composer input').fill('Hello from test') page.get_by_role('button', name='发送消息', exact=True).click() page.get_by_text('未送达 · 点重试', exact=True).wait_for() state['fail_send'] = False page.get_by_text('未送达 · 点重试', exact=True).click() page.get_by_text('已送达', exact=True).wait_for() message_writes = [body for kind, body in writes if kind == 'message'] assert len(message_writes) == 2 assert message_writes[0]['clientId'] == message_writes[1]['clientId'] assert not errors, errors results.append({'width': width, 'routes': len(routes), 'writes': len(writes), 'ok': True}) context.close() browser.close() print(json.dumps(results)) if __name__ == '__main__': run()