#!/usr/bin/env node /** r10 真机移动端只读 UI 巡检:不提交表单、不改设置、不发送消息。 */ import fs from 'node:fs'; import path from 'node:path'; import assert from 'node:assert/strict'; import { connectRealAccountWebView } from './lib/real-account-webview.mjs'; import { captureNativeScreenshot } from './android-webview-cdp.mjs'; const out = 'test-results/mobile-ui-review-20261202'; if (fs.existsSync(out)) throw Error('本轮证据目录已存在;先只读核实,不覆盖'); fs.mkdirSync(path.join(out, 'screenshots'), { recursive: true }); fs.mkdirSync(path.join(out, 'dom'), { recursive: true }); const { cdp, session } = await connectRealAccountWebView(); assert.equal(session.revision, 'r10', '当前真机包不是 r10'); const manifest = { revision: session.revision, packageId: session.packageId, serial: session.serial, startedAt: new Date().toISOString(), screenshots: [], interactions: [] }; const sleep = ms => new Promise(resolve => setTimeout(resolve, ms)); const writeManifest = () => fs.writeFileSync(path.join(out, 'manifest.json'), JSON.stringify(manifest, null, 2) + '\n'); async function snapshot(id, requestedPath = null, note = '') { await sleep(700); const dom = await cdp.evaluate(() => { const rect = el => { if (!el) return null; const r = el.getBoundingClientRect(); return { x: Math.round(r.x), y: Math.round(r.y), width: Math.round(r.width), height: Math.round(r.height), right: Math.round(r.right), bottom: Math.round(r.bottom) }; }; const visible = el => { const r = el.getBoundingClientRect(), s = getComputedStyle(el); return r.width > 0 && r.height > 0 && r.right > 0 && r.bottom > 0 && r.x < innerWidth && r.y < innerHeight && s.display !== 'none' && s.visibility !== 'hidden'; }; const controls = [...document.querySelectorAll('button,a,input,textarea,select,[role="button"],[role="tab"],[role="switch"],[role="radio"]')].filter(visible).map(el => { const r = rect(el), cls = String(el.className || ''), row = /m-row|m-tree-item/.test(cls); const text = row ? '[列表项内容隐去]' : (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA' ? '' : (el.innerText || '').trim().replace(/\s+/g, ' ').slice(0, 70)); return { tag: el.tagName.toLowerCase(), role: el.getAttribute('role'), label: row ? '' : (el.getAttribute('aria-label') || ''), text, placeholder: el.getAttribute('placeholder') || '', rect: r, small: r.width < 44 || r.height < 44, disabled: !!el.disabled || el.getAttribute('aria-disabled') === 'true', className: cls.slice(0, 90) }; }); const tinyText = [...document.querySelectorAll('body *')].filter(el => el.childElementCount === 0 && visible(el) && el.innerText?.trim()).map(el => ({ text: el.innerText.trim().replace(/\s+/g, ' ').slice(0, 32), fontSize: getComputedStyle(el).fontSize })).filter(x => parseFloat(x.fontSize) < 12).slice(0, 30); const scroller = document.scrollingElement; return { path: location.pathname, viewport: { width: innerWidth, height: innerHeight }, scrollWidth: document.documentElement.scrollWidth, scrollHeight: scroller?.scrollHeight || 0, scrollTop: scroller?.scrollTop || 0, overflowX: document.documentElement.scrollWidth > innerWidth + 1, dark: document.documentElement.classList.contains('dark'), headings: [...document.querySelectorAll('h1,h2,h3,.m-title-main')].filter(visible).map(el => el.innerText.trim().replace(/\s+/g, ' ').slice(0, 80)), topbar: rect(document.querySelector('.m-app-bar,app-m-app-bar,.m-bar')), tabbar: rect(document.querySelector('.m-tabbar')), dock: rect(document.querySelector('app-m-dock')), dialogs: [...document.querySelectorAll('[role="dialog"]')].filter(visible).map(el => ({ rect: rect(el), label: el.getAttribute('aria-label') || el.getAttribute('aria-labelledby') || '' })), controls, smallControls: controls.filter(x => x.small).map(({ tag, label, text, rect, className }) => ({ tag, label, text, rect, className })), tinyText }; }); const shot = path.join(out, 'screenshots', id + '.png'); const metadata = path.join(out, 'dom', id + '.json'); if (fs.existsSync(shot) || fs.existsSync(metadata)) throw Error('证据文件已存在:' + id); captureNativeScreenshot(shot, session.serial); fs.writeFileSync(metadata, JSON.stringify(dom, null, 2) + '\n', { flag: 'wx' }); manifest.screenshots.push({ id, requestedPath, actualPath: dom.path, screenshot: shot, dom: metadata, overflowX: dom.overflowX, smallControls: dom.smallControls.length, note, at: new Date().toISOString() }); writeManifest(); return dom; } async function go(route, id = route.replace(/[^a-z0-9]+/gi, '-').replace(/^-|-$/g, '')) { await cdp.send('Page.navigate', { url: 'https://localhost' + route }); await sleep(1600); return snapshot(id, route); } async function tapText(text, selector = 'button') { const target = await cdp.evaluate(({ text, selector }) => { document.querySelectorAll('[data-r10-ui-target]').forEach(el => el.removeAttribute('data-r10-ui-target')); const matches = [...document.querySelectorAll(selector)].filter(el => { const r = el.getBoundingClientRect(); return r.width > 0 && r.height > 0 && (el.innerText || '').replace(/\s+/g, ' ').trim() === text; }); if (matches.length !== 1) return false; matches[0].setAttribute('data-r10-ui-target', 'current'); return true; }, { text, selector }); if (!target) return false; await cdp.tap('[data-r10-ui-target="current"]'); await sleep(550); return true; } try { await go('/mobile/messages', 'messages'); const row = await cdp.evaluate(() => { const rows = [...document.querySelectorAll('button.m-row')].filter(el => { const r = el.getBoundingClientRect(); return r.width > 0 && r.height > 0 && r.bottom > 0 && r.top < innerHeight; }); const safe = rows.find(el => !el.querySelector('app-m-badge')); if (!safe) return null; safe.setAttribute('data-r10-ui-target', 'chat'); return { rowCount: rows.length, selectedHasUnreadBadge: false }; }); if (row) { await cdp.tap('[data-r10-ui-target="chat"]'); await sleep(1300); const route = await cdp.evaluate(() => location.pathname); if (/^\/mobile\/chat\//.test(route)) await snapshot('chat-existing-read-row', route, '打开已有且未见未读徽标的会话,仅检查界面'); manifest.interactions.push({ action: 'open_existing_chat_without_visible_unread_badge', resultPath: route, readOnlyIntent: true }); writeManifest(); } await go('/mobile/messages', 'messages-restored'); const drawer = await cdp.evaluate(() => !!document.querySelector('button[aria-label="打开导航"]')); if (drawer) { await cdp.tap('button[aria-label="打开导航"]'); await sleep(350); if (await cdp.evaluate(() => !!document.querySelector('.m-drawer.show'))) await snapshot('drawer-open', '/mobile/messages', '仅打开导航抽屉'); const close = await cdp.evaluate(() => !!document.querySelector('.m-drawer.show button[aria-label*="关闭"]')); if (close) await cdp.tap('.m-drawer.show button[aria-label*="关闭"]'); else await cdp.evaluate(() => document.querySelector('.m-drawer.show .m-drawer-mask')?.click()); manifest.interactions.push({ action: 'open_close_navigation_drawer', result: 'closed' }); writeManifest(); } const newChat = await cdp.evaluate(() => !!document.querySelector('button[aria-label="新建对话"]')); if (newChat) { await cdp.tap('button[aria-label="新建对话"]'); await sleep(450); if (await cdp.evaluate(() => !!document.querySelector('[role="dialog"]'))) await snapshot('new-chat-dialog', '/mobile/messages', '打开新建对话弹层,不创建'); const cancelled = await tapText('取消', '[role="dialog"] button'); if (!cancelled) await cdp.evaluate(() => document.querySelector('[role="dialog"] button[aria-label*="关闭"]')?.click()); manifest.interactions.push({ action: 'open_cancel_new_chat', cancelled }); writeManifest(); } await go('/mobile/contacts', 'contacts-all'); for (const [label, id] of [['数字生命', 'contacts-digital-lives'], ['同事', 'contacts-colleagues'], ['全部', 'contacts-all-restored']]) { if (await tapText(label, '.m-segmented button')) await snapshot(id, '/mobile/contacts', '切换本地筛选后复原;未修改联系人'); } const agentTarget = await cdp.evaluate(() => { const el = document.querySelector('button.m-row--sub'); if (!el) return false; el.setAttribute('data-r10-ui-target', 'agent'); return true; }); if (agentTarget) { await cdp.tap('[data-r10-ui-target="agent"]'); await sleep(1200); const route = await cdp.evaluate(() => location.pathname); if (/^\/mobile\/agent\//.test(route)) await snapshot('agent-settings-existing-container', route, '打开已有容器设置页,只读查看'); manifest.interactions.push({ action: 'open_existing_container_settings', resultPath: route, noRenameSubmitted: true }); writeManifest(); } await go('/mobile/workbench', 'workbench-all'); for (const [label, id] of [['在线', 'workbench-online'], ['离线', 'workbench-offline'], ['全部', 'workbench-all-restored']]) { if (await tapText(label, '.m-segmented button')) await snapshot(id, '/mobile/workbench', '切换本地筛选后复原;未修改容器'); } await go('/mobile/mine', 'mine-top'); await cdp.evaluate(() => document.scrollingElement?.scrollTo({ top: document.scrollingElement.scrollHeight, behavior: 'instant' })); await snapshot('mine-bottom', '/mobile/mine', '检查设置与账号区域;未切换或保存设置'); await cdp.evaluate(() => document.scrollingElement?.scrollTo({ top: 0, behavior: 'instant' })); await go('/mobile/my-containers', 'my-containers'); const access = await cdp.evaluate(() => { const el = [...document.querySelectorAll('button.container-btn')].find(b => b.innerText.trim() === '查看接入信息' && b.getBoundingClientRect().width > 0); if (!el) return false; el.setAttribute('data-r10-ui-target', 'access'); return true; }); if (access) { await cdp.tap('[data-r10-ui-target="access"]'); await sleep(1000); const route = await cdp.evaluate(() => location.pathname); if (/\/plan-access$/.test(route)) await snapshot('plan-access-existing-container', route, '从已有容器打开接入信息;未展开敏感值'); } else { await go('/mobile/plan-access', 'plan-access-empty', '无容器选择态'); } await go('/plans', 'plans'); await cdp.evaluate(() => document.scrollingElement?.scrollTo({ top: document.scrollingElement.scrollHeight, behavior: 'instant' })); await snapshot('plans-bottom', '/plans', '检查套餐页下半部;未提交申请'); await go('/provision', 'provision'); await go('/mobile/admin/users', 'admin-users'); await go('/mobile/messages', 'messages-final'); manifest.completedAt = new Date().toISOString(); writeManifest(); console.log(JSON.stringify({ revision: manifest.revision, screenshots: manifest.screenshots.length, interactions: manifest.interactions.length, directory: out }, null, 2)); } catch (error) { manifest.failure = { message: error.message, at: new Date().toISOString() }; writeManifest(); console.error(error.stack || error.message); process.exitCode = 1; } finally { cdp.close(); }