nomadro/scripts/check-social-overhaul.py
Eric e177b50d2e ARCHIVE test12 Codex social overhaul — NEVER DEPLOY to .com/.cn.
Freeze experimental social/admin UX on test12 for reference only; see docs/archive/test12-NEVER-DEPLOY.md.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-05 19:22:12 +08:00

157 lines
13 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Browser acceptance of community navigation, drafts, recovery and admin publishing.
All mutation requests are intercepted; no community content or configuration is published.
"""
import asyncio,json,os,sys
from pathlib import Path
from playwright.async_api import async_playwright,expect
sys.stdout.reconfigure(encoding='utf-8')
BASE=os.environ.get('SOCIAL_BASE_URL','http://localhost:3010')
ADMIN=os.environ.get('SOCIAL_ADMIN_URL','http://localhost:5174')
OUT=Path('artifacts/social-overhaul/acceptance');OUT.mkdir(parents=True,exist_ok=True)
async def main():
results=[];errors=[]
async with async_playwright() as p:
browser=await p.chromium.launch()
ctx=await browser.new_context(locale='en-US',timezone_id='America/New_York',viewport={'width':1440,'height':1000},reduced_motion='reduce')
await ctx.add_init_script('''if(!localStorage.getItem('nomadro-ui-locale'))localStorage.setItem('nomadro-ui-locale','en');
const measure=CanvasRenderingContext2D.prototype.measureText;
CanvasRenderingContext2D.prototype.measureText=function(t){if(t==='mmmmmmmmmmlli中文测试')return {width:100};return measure.call(this,t)};''')
state={'fail':False,'skills':[],'posts':[],'hostsFail':False}
async def mock(route):
req=route.request;url=req.url
if '/api/config' in url:
return await route.fulfill(json={'version':1,'config':{'version':1,'modules':{'social':{'enabled':True}},'copy':{}},'updatedAt':''})
if '/api/coffee/v1/hosts' in url:
if state['hostsFail']:return await route.abort()
return await route.fulfill(json={'hosts':[{'id':'h1','username':'test-host','name':'Test Host','bio':'Design and travel','city':'Lisbon','timezone':'Europe/Lisbon','event_count':1}]})
if '/api/social/v1' in url:
if req.method=='GET':
if '/skills' in url:return await route.fulfill(json={'items':state['skills']})
if '/realtime/' in url:return await route.fulfill(json={'enabled':False})
return await route.fulfill(json={'items':[],'peers':[],'online':0,'item':None,'count':0})
if req.method in ['POST','PUT','PATCH','DELETE']:
state['posts'].append(url)
if state['fail'] and ('/skills' in url or '/apply' in url):return await route.abort()
if url.endswith('/skills'):
body=req.post_data_json;state['skills'].append({'id':'test-skill','guestId':req.headers.get('x-social-guest-id'),'author':'Test Author','createdAt':'2026-11-05T00:00:00Z',**body})
if url.endswith('/close'):state['skills']=[]
return await route.fulfill(json={'ok':True,'id':'test-ack','peers':[],'online':0})
return await route.fulfill(json={'ok':True})
await ctx.route('**/api/**',mock)
page=await ctx.new_page();page.on('pageerror',lambda e:errors.append(str(e)))
async def go(path):
response=await page.goto(BASE+'/social'+path);assert response.status==200
await expect(page.locator('.social-language-toggle')).to_have_attribute('aria-label','切换中文',timeout=20000)
async def check(name,fn):
try:
await fn();results.append({'name':name,'passed':True});print('PASS',name,flush=True)
except Exception as e:
results.append({'name':name,'passed':False,'error':str(e)[:1800]});print('FAIL',name,str(e)[:350],flush=True)
await page.screenshot(path=str(OUT/f'failure-{len(results)}.png'),full_page=True)
if await page.locator('dialog[open]').count():await page.keyboard.press('Escape')
async def navigation():
await go('')
for i in range(3):
button=page.locator('.nav-group-btn').nth(i);await button.hover();await expect(button).to_have_attribute('aria-expanded','true')
panel=page.locator('.nav-group').nth(i).locator('.nav-group-panel');assert await panel.locator('a').count() in [3,4,6]
await button.focus();await page.keyboard.press('ArrowDown');await expect(panel.locator('a').first).to_be_focused()
await page.keyboard.press('Escape');await expect(button).to_be_focused()
await page.keyboard.press('Control+k');await expect(page.locator('dialog[open]')).to_be_visible()
await page.get_by_role('searchbox',name='Search pages and features').fill('Skill exchange')
await page.keyboard.press('ArrowDown');await page.keyboard.press('Enter');await expect(page).to_have_url(BASE+'/social/skills')
await page.set_viewport_size({'width':390,'height':844})
menu=page.locator('.mobile-menu-btn');await menu.click();await expect(page.locator('.nav-links')).to_have_class('nav-links open')
await page.keyboard.press('Escape');await expect(menu).to_be_focused()
assert await page.evaluate('document.body.style.overflow')!='hidden'
await page.set_viewport_size({'width':1440,'height':1000})
await check('navigation, keyboard command search and mobile focus',navigation)
async def theme_language():
await go('');before=await page.locator('html').get_attribute('data-audience')
await page.locator('.social-theme-toggle').click();await expect(page.locator('html')).to_have_attribute('data-theme','light')
await page.reload();await expect(page.locator('html')).to_have_attribute('data-theme','light')
await page.locator('.social-language-toggle').click();await expect(page.locator('html')).to_have_attribute('lang','zh-CN')
assert await page.locator('html').get_attribute('data-audience')==before
# Reload with user preference; remove init-script override for this explicit persistence check.
await page.evaluate("localStorage.setItem('nomadro-ui-locale','zh')")
await page.goto(BASE+'/social/people');await expect(page.get_by_role('button',name='更多筛选',exact=True)).to_be_visible()
await page.locator('.social-language-toggle').click();await expect(page.locator('.social-language-toggle')).to_have_attribute('aria-label','切换中文')
await page.locator('.social-theme-toggle').click()
await check('day/night persistence and language independent of audience',theme_language)
async def home_search():
await go('');await page.get_by_role('searchbox',name='Search cities, interests or people').fill('Lisbon')
await page.locator('.community-hero-search button').click();await expect(page).to_have_url(BASE+'/social/people?q=Lisbon')
await expect(page.get_by_role('searchbox')).to_have_value('Lisbon')
await check('home search opens filtered people directory',home_search)
async def skills():
await go('/skills');await page.get_by_role('button',name='Post an exchange',exact=True).click()
dialog=page.locator('dialog[open]');await dialog.get_by_label('I can offer',exact=True).fill('Playwright coaching');await dialog.get_by_label('I’d love to learn',exact=True).fill('Photography')
await page.keyboard.press('Escape');await page.get_by_role('button',name='Post an exchange',exact=True).click();await expect(dialog.get_by_label('I can offer',exact=True)).to_have_value('Playwright coaching')
state['fail']=True;await dialog.get_by_role('button',name='Publish exchange',exact=True).click();await expect(dialog.get_by_role('alert')).to_be_visible()
state['fail']=False;await dialog.get_by_role('button',name='Publish exchange',exact=True).click();await expect(dialog).to_have_count(0)
await expect(page.locator('.social-skills-card').filter(has_text='Playwright coaching')).to_have_count(1)
await page.get_by_role('button',name='My exchanges',exact=True).click();await expect(page.locator('.social-skills-card')).to_have_count(1)
await page.get_by_role('button',name='Close exchange',exact=True).click();await page.locator('dialog[open]').get_by_role('button',name='Close exchange',exact=True).click()
await expect(page.locator('.social-skills-card')).to_have_count(0)
await check('skill draft, failed submission, retry, refresh and close',skills)
async def roles():
await go('/team#vol-welcome');dialog=page.locator('dialog[open]');await expect(dialog).to_be_visible()
await dialog.locator('input').nth(0).fill('Test applicant');await dialog.locator('input').nth(1).fill('test@example.invalid')
await page.keyboard.press('Escape');assert '#' not in page.url
await page.locator('.social-role-card button.social-btn').first.click();await expect(dialog.locator('input').nth(0)).to_have_value('Test applicant')
state['fail']=True;await dialog.locator('button[type=submit]').click();await expect(dialog.locator('.social-status.warn')).to_be_visible()
state['fail']=False;await dialog.locator('button[type=submit]').click();await expect(dialog.locator('.social-success')).to_be_visible()
await page.keyboard.press('Escape')
await check('role deep link, separate persistent draft and retry',roles)
async def coffee():
await go('/coffeechat');await expect(page.locator('.cf-card')).to_have_count(1)
await page.locator('.cf-host-search input').fill('not-a-topic');await expect(page.locator('.cf-card')).to_have_count(0)
await page.get_by_role('button',name='Reset filters',exact=True).click();await expect(page.locator('.cf-card')).to_have_count(1)
state['hostsFail']=True;await page.reload();await expect(page.locator('.cf-error')).to_contain_text('previously viewed')
await expect(page.locator('.cf-card')).to_have_count(1);state['hostsFail']=False
await check('coffee host search, empty reset and offline cache',coffee)
async def map_test():
await go('/map');await page.set_viewport_size({'width':390,'height':844})
await page.get_by_role('searchbox',name='Search cities',exact=True).fill('lisbon');await expect(page.locator('.community-map-directory > button')).to_have_count(1)
await page.locator('.community-map-directory > button').click();await expect(page.locator('.community-map-selection')).to_be_visible()
await expect(page.locator('.community-map-selection a')).to_have_attribute('href','/social/places/lisbon')
await page.get_by_role('button',name='Globe',exact=True).click();await expect(page).to_have_url(BASE+'/social/map?city=lisbon&mode=globe')
await page.get_by_role('button',name='Map',exact=True).click();await expect(page.locator('.community-map-stage .social-map-canvas-wrap')).to_be_visible(timeout=20000)
await page.get_by_role('button',name='Clear city',exact=True).click();await expect(page.locator('.community-map-selection')).to_have_count(0)
assert await page.evaluate('document.documentElement.scrollWidth-innerWidth')<=1
await page.set_viewport_size({'width':1440,'height':1000})
await check('mobile map search, city selection and flat/globe switching',map_test)
await ctx.close()
admin=await browser.new_context(viewport={'width':1440,'height':1000},reduced_motion='reduce');await admin.add_init_script("localStorage.setItem('nomadro_admin_com_token','mock-acceptance-token')")
state_admin={'version':7,'experience':{},'conflict':False,'writes':[]}
async def admin_mock(route):
url=route.request.url
if '/src/' in url:return await route.continue_()
if '/experience' in url:
if route.request.method=='PUT':
if state_admin['conflict']:return await route.fulfill(status=409,json={'detail':'配置已被其他操作更新,请先加载最新配置;当前草稿可保留'})
body=route.request.post_data_json;assert body['expectedVersion']==state_admin['version'];state_admin['writes'].append(body)
state_admin['version']+=1;state_admin['experience']=body['experience']
return await route.fulfill(json={'ok':True,**{k:state_admin[k] for k in ['version','experience']}})
if '/config/meta' in url:return await route.fulfill(json={'version':state_admin['version'],'updatedAt':''})
return await route.fulfill(json={'ok':True,'items':[],'peers':[],'online':0})
await admin.route('**/api/**',admin_mock);page=await admin.new_page();page.on('pageerror',lambda e:errors.append(str(e)))
async def admin_test():
await page.goto(ADMIN+'/cn/social/experience');await expect(page.get_by_role('heading',name='社区体验设计台')).to_be_visible()
fields=page.locator('.com-experience-copy-grid input');await fields.nth(1).fill('Acceptance home title')
await page.locator('.com-experience-preview').scroll_into_view_if_needed();await expect(page.locator('.com-experience-preview')).to_contain_text('Acceptance home title')
await page.get_by_role('link',name='打开目录配置 ↗',exact=True).click();await expect(page.locator('.com-unsaved-dialog')).to_be_visible();await page.get_by_role('button',name='继续编辑',exact=True).click()
state_admin['conflict']=True;await page.get_by_role('button',name='发布社区体验',exact=True).click();await expect(page.get_by_role('alert')).to_contain_text('配置已被其他操作更新')
await expect(fields.nth(1)).to_have_value('Acceptance home title')
state_admin['conflict']=False;await page.get_by_role('button',name='发布社区体验',exact=True).click();await expect(page.get_by_role('status').filter(has_text='已发布')).to_be_visible()
assert len(state_admin['writes'])==1
await page.set_viewport_size({'width':390,'height':844});await page.screenshot(path=str(OUT/'admin-390.png'),full_page=True)
assert await page.evaluate('document.documentElement.scrollWidth-innerWidth')<=1
await check('admin preview, unsaved protection, version conflict and publish',admin_test)
await browser.close()
report={'checks':results,'pageErrors':errors};(OUT/'report.json').write_text(json.dumps(report,indent=2,ensure_ascii=False),encoding='utf-8')
assert all(r['passed'] for r in results) and not errors, 'Acceptance failed; inspect report.json'
print(json.dumps({'passed':len(results),'pageErrors':len(errors)}))
asyncio.run(main())