review-mobile-ui-r10.mjs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/usr/bin/env node
  2. /** r10 真机移动端只读 UI 巡检:不提交表单、不改设置、不发送消息。 */
  3. import fs from 'node:fs';
  4. import path from 'node:path';
  5. import assert from 'node:assert/strict';
  6. import { connectRealAccountWebView } from './lib/real-account-webview.mjs';
  7. import { captureNativeScreenshot } from './android-webview-cdp.mjs';
  8. const out = 'test-results/mobile-ui-review-20261202';
  9. if (fs.existsSync(out)) throw Error('本轮证据目录已存在;先只读核实,不覆盖');
  10. fs.mkdirSync(path.join(out, 'screenshots'), { recursive: true });
  11. fs.mkdirSync(path.join(out, 'dom'), { recursive: true });
  12. const { cdp, session } = await connectRealAccountWebView();
  13. assert.equal(session.revision, 'r10', '当前真机包不是 r10');
  14. const manifest = { revision: session.revision, packageId: session.packageId, serial: session.serial, startedAt: new Date().toISOString(), screenshots: [], interactions: [] };
  15. const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
  16. const writeManifest = () => fs.writeFileSync(path.join(out, 'manifest.json'), JSON.stringify(manifest, null, 2) + '\n');
  17. async function snapshot(id, requestedPath = null, note = '') {
  18. await sleep(700);
  19. const dom = await cdp.evaluate(() => {
  20. 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) }; };
  21. 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'; };
  22. const controls = [...document.querySelectorAll('button,a,input,textarea,select,[role="button"],[role="tab"],[role="switch"],[role="radio"]')].filter(visible).map(el => {
  23. const r = rect(el), cls = String(el.className || ''), row = /m-row|m-tree-item/.test(cls);
  24. const text = row ? '[列表项内容隐去]' : (el.tagName === 'INPUT' || el.tagName === 'TEXTAREA' ? '' : (el.innerText || '').trim().replace(/\s+/g, ' ').slice(0, 70));
  25. 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) };
  26. });
  27. 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);
  28. const scroller = document.scrollingElement;
  29. 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 };
  30. });
  31. const shot = path.join(out, 'screenshots', id + '.png');
  32. const metadata = path.join(out, 'dom', id + '.json');
  33. if (fs.existsSync(shot) || fs.existsSync(metadata)) throw Error('证据文件已存在:' + id);
  34. captureNativeScreenshot(shot, session.serial);
  35. fs.writeFileSync(metadata, JSON.stringify(dom, null, 2) + '\n', { flag: 'wx' });
  36. manifest.screenshots.push({ id, requestedPath, actualPath: dom.path, screenshot: shot, dom: metadata, overflowX: dom.overflowX, smallControls: dom.smallControls.length, note, at: new Date().toISOString() });
  37. writeManifest();
  38. return dom;
  39. }
  40. async function go(route, id = route.replace(/[^a-z0-9]+/gi, '-').replace(/^-|-$/g, '')) {
  41. await cdp.send('Page.navigate', { url: 'https://localhost' + route });
  42. await sleep(1600);
  43. return snapshot(id, route);
  44. }
  45. async function tapText(text, selector = 'button') {
  46. const target = await cdp.evaluate(({ text, selector }) => {
  47. document.querySelectorAll('[data-r10-ui-target]').forEach(el => el.removeAttribute('data-r10-ui-target'));
  48. const matches = [...document.querySelectorAll(selector)].filter(el => {
  49. const r = el.getBoundingClientRect();
  50. return r.width > 0 && r.height > 0 && (el.innerText || '').replace(/\s+/g, ' ').trim() === text;
  51. });
  52. if (matches.length !== 1) return false;
  53. matches[0].setAttribute('data-r10-ui-target', 'current');
  54. return true;
  55. }, { text, selector });
  56. if (!target) return false;
  57. await cdp.tap('[data-r10-ui-target="current"]');
  58. await sleep(550);
  59. return true;
  60. }
  61. try {
  62. await go('/mobile/messages', 'messages');
  63. const row = await cdp.evaluate(() => {
  64. 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; });
  65. const safe = rows.find(el => !el.querySelector('app-m-badge'));
  66. if (!safe) return null;
  67. safe.setAttribute('data-r10-ui-target', 'chat');
  68. return { rowCount: rows.length, selectedHasUnreadBadge: false };
  69. });
  70. if (row) {
  71. await cdp.tap('[data-r10-ui-target="chat"]');
  72. await sleep(1300);
  73. const route = await cdp.evaluate(() => location.pathname);
  74. if (/^\/mobile\/chat\//.test(route)) await snapshot('chat-existing-read-row', route, '打开已有且未见未读徽标的会话,仅检查界面');
  75. manifest.interactions.push({ action: 'open_existing_chat_without_visible_unread_badge', resultPath: route, readOnlyIntent: true });
  76. writeManifest();
  77. }
  78. await go('/mobile/messages', 'messages-restored');
  79. const drawer = await cdp.evaluate(() => !!document.querySelector('button[aria-label="打开导航"]'));
  80. if (drawer) {
  81. await cdp.tap('button[aria-label="打开导航"]'); await sleep(350);
  82. if (await cdp.evaluate(() => !!document.querySelector('.m-drawer.show'))) await snapshot('drawer-open', '/mobile/messages', '仅打开导航抽屉');
  83. const close = await cdp.evaluate(() => !!document.querySelector('.m-drawer.show button[aria-label*="关闭"]'));
  84. if (close) await cdp.tap('.m-drawer.show button[aria-label*="关闭"]');
  85. else await cdp.evaluate(() => document.querySelector('.m-drawer.show .m-drawer-mask')?.click());
  86. manifest.interactions.push({ action: 'open_close_navigation_drawer', result: 'closed' }); writeManifest();
  87. }
  88. const newChat = await cdp.evaluate(() => !!document.querySelector('button[aria-label="新建对话"]'));
  89. if (newChat) {
  90. await cdp.tap('button[aria-label="新建对话"]'); await sleep(450);
  91. if (await cdp.evaluate(() => !!document.querySelector('[role="dialog"]'))) await snapshot('new-chat-dialog', '/mobile/messages', '打开新建对话弹层,不创建');
  92. const cancelled = await tapText('取消', '[role="dialog"] button');
  93. if (!cancelled) await cdp.evaluate(() => document.querySelector('[role="dialog"] button[aria-label*="关闭"]')?.click());
  94. manifest.interactions.push({ action: 'open_cancel_new_chat', cancelled }); writeManifest();
  95. }
  96. await go('/mobile/contacts', 'contacts-all');
  97. for (const [label, id] of [['数字生命', 'contacts-digital-lives'], ['同事', 'contacts-colleagues'], ['全部', 'contacts-all-restored']]) {
  98. if (await tapText(label, '.m-segmented button')) await snapshot(id, '/mobile/contacts', '切换本地筛选后复原;未修改联系人');
  99. }
  100. const agentTarget = await cdp.evaluate(() => {
  101. const el = document.querySelector('button.m-row--sub');
  102. if (!el) return false;
  103. el.setAttribute('data-r10-ui-target', 'agent'); return true;
  104. });
  105. if (agentTarget) {
  106. await cdp.tap('[data-r10-ui-target="agent"]'); await sleep(1200);
  107. const route = await cdp.evaluate(() => location.pathname);
  108. if (/^\/mobile\/agent\//.test(route)) await snapshot('agent-settings-existing-container', route, '打开已有容器设置页,只读查看');
  109. manifest.interactions.push({ action: 'open_existing_container_settings', resultPath: route, noRenameSubmitted: true }); writeManifest();
  110. }
  111. await go('/mobile/workbench', 'workbench-all');
  112. for (const [label, id] of [['在线', 'workbench-online'], ['离线', 'workbench-offline'], ['全部', 'workbench-all-restored']]) {
  113. if (await tapText(label, '.m-segmented button')) await snapshot(id, '/mobile/workbench', '切换本地筛选后复原;未修改容器');
  114. }
  115. await go('/mobile/mine', 'mine-top');
  116. await cdp.evaluate(() => document.scrollingElement?.scrollTo({ top: document.scrollingElement.scrollHeight, behavior: 'instant' }));
  117. await snapshot('mine-bottom', '/mobile/mine', '检查设置与账号区域;未切换或保存设置');
  118. await cdp.evaluate(() => document.scrollingElement?.scrollTo({ top: 0, behavior: 'instant' }));
  119. await go('/mobile/my-containers', 'my-containers');
  120. const access = await cdp.evaluate(() => {
  121. const el = [...document.querySelectorAll('button.container-btn')].find(b => b.innerText.trim() === '查看接入信息' && b.getBoundingClientRect().width > 0);
  122. if (!el) return false;
  123. el.setAttribute('data-r10-ui-target', 'access'); return true;
  124. });
  125. if (access) {
  126. await cdp.tap('[data-r10-ui-target="access"]'); await sleep(1000);
  127. const route = await cdp.evaluate(() => location.pathname);
  128. if (/\/plan-access$/.test(route)) await snapshot('plan-access-existing-container', route, '从已有容器打开接入信息;未展开敏感值');
  129. } else {
  130. await go('/mobile/plan-access', 'plan-access-empty', '无容器选择态');
  131. }
  132. await go('/plans', 'plans');
  133. await cdp.evaluate(() => document.scrollingElement?.scrollTo({ top: document.scrollingElement.scrollHeight, behavior: 'instant' }));
  134. await snapshot('plans-bottom', '/plans', '检查套餐页下半部;未提交申请');
  135. await go('/provision', 'provision');
  136. await go('/mobile/admin/users', 'admin-users');
  137. await go('/mobile/messages', 'messages-final');
  138. manifest.completedAt = new Date().toISOString();
  139. writeManifest();
  140. console.log(JSON.stringify({ revision: manifest.revision, screenshots: manifest.screenshots.length, interactions: manifest.interactions.length, directory: out }, null, 2));
  141. } catch (error) {
  142. manifest.failure = { message: error.message, at: new Date().toISOString() };
  143. writeManifest();
  144. console.error(error.stack || error.message);
  145. process.exitCode = 1;
  146. } finally {
  147. cdp.close();
  148. }