#!/usr/bin/env node /* 真机复验(raw CDP 版):注入登录态 → 移动端 6 项验证。连 App WebView 不能用 playwright connectOverCDP。 */ import { spawnSync } from 'node:child_process'; import { readFileSync, mkdirSync } from 'node:fs'; import { resolve, dirname } from 'node:path'; import { fileURLToPath } from 'node:url'; import { connectWebView, captureNativeScreenshot } from './android-webview-cdp.mjs'; const here = dirname(fileURLToPath(import.meta.url)); const outDir = resolve(here, '../test-results/ui-experience-20261202'); mkdirSync(outDir, { recursive: true }); const ADB = 'E:/coding-environment/AndroidSDK/platform-tools/adb.exe'; const SERIAL = '3e529a34'; const PORT = 9444; const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); const adb = (...args) => { const r = spawnSync(ADB, ['-s', SERIAL, ...args], { encoding: 'utf8', timeout: 30000, windowsHide: true }); return (r.stdout || '').trim(); }; /* 1. 启动 App 并转发 WebView 端口 */ adb('shell', 'am', 'start', '-n', 'cn.fmode.app/.MainActivity'); await sleep(6000); let page; for (let i = 0; i < 20; i++) { const pid = adb('shell', 'pidof', 'cn.fmode.app').split(/\s+/)[0]; if (pid && adb('shell', 'cat', '/proc/net/unix').includes(`webview_devtools_remote_${pid}`)) { adb('forward', '--remove', `tcp:${PORT}`); adb('forward', `tcp:${PORT}`, `localabstract:webview_devtools_remote_${pid}`); break; } await sleep(1000); } page = await connectWebView(PORT); console.log('已连入 WebView, URL =', await page.evaluate(() => location.href)); /* 2. 注入登录态并刷新 */ const login = JSON.parse(readFileSync(resolve(here, '../.claude/dev-login.json'), 'utf8')); await page.evaluate((d) => { localStorage.setItem('fmode_session_token', d.token); localStorage.setItem('fmode_me', JSON.stringify(d.me)); }, login); await page.evaluate(() => { location.href = 'https://localhost/mobile/messages'; }); await sleep(9000); console.log('登录后 URL =', await page.evaluate(() => location.href)); console.log('列表文本 =', await page.evaluate(() => document.body.innerText.slice(0, 100).replace(/\s+/g, ' '))); /* 3. 进入「技能锁定测试」会话 */ const found = await page.evaluate(() => { const row = [...document.querySelectorAll('button, a, li')].find((e) => (e.textContent || '').includes('技能锁定测试')); if (!row) return false; row.scrollIntoView({ block: 'center' }); (row.closest('button') || row.querySelector('button') || row).click(); return true; }); if (!found) { console.log('FAIL: 列表里找不到「技能锁定测试」'); await page.screenshot(resolve(outDir, 'r-00-nolist.png')); process.exit(1); } await sleep(5000); console.log('会话页 URL =', await page.evaluate(() => location.href)); await page.screenshot(resolve(outDir, 'r-01-chat.png')); /* 4. 发多步任务 */ await page.fill('textarea', '请用 terminal 依次执行 echo r-step-1、sleep 15、echo r-step-2,每步之间报告进度。'); await page.evaluate(() => document.querySelector('.m-send').click()); console.log('已发送多步任务'); let sawWait = false, sawLive = false, doneShot = false; for (let i = 0; i < 25; i++) { const probe = await page.evaluate(() => { const turns = [...document.querySelectorAll('app-assistant-turn')]; const last = turns[turns.length - 1]; return { lastTurn: last ? (last.textContent || '').replace(/\s+/g, ' ').trim().slice(0, 100) : null, wait: [...document.querySelectorAll('.card-wait')].map((e) => e.textContent.trim()), liveVisible: !!document.querySelector('app-live-activity'), }; }); console.log(`[+${i * 2}s]`, JSON.stringify(probe)); if (probe.wait.length) { if (!sawWait) await page.screenshot(resolve(outDir, 'r-02-wait.png')); sawWait = true; } if (probe.liveVisible && !sawLive) { sawLive = true; await page.screenshot(resolve(outDir, 'r-03-live.png')); } if (probe.lastTurn && probe.lastTurn.includes('已完成') && !doneShot) { doneShot = true; await page.screenshot(resolve(outDir, 'r-04-done.png')); break; } await sleep(2000); } /* 5. 输入区样式实测 */ const style = await page.evaluate(() => { const area = document.querySelector('.m-composer-area'); const dock = document.querySelector('app-m-dock'); const cs = area ? getComputedStyle(area) : null; const dr = dock ? dock.getBoundingClientRect() : null; return { composerBg: cs?.backgroundColor, dockLeft: dr ? Math.round(dr.left) : null, viewport: innerWidth, placeholder: document.querySelector('textarea')?.placeholder, subtitle: (document.querySelector('.m-subtitle, [class*=subtitle]')?.textContent || '').trim(), }; }); console.log('输入区/命名样式:', JSON.stringify(style)); /* 6. 回列表看忙碌环(任务可能已结束,尽力而为) */ await page.evaluate(() => { location.href = 'https://localhost/mobile/messages'; }); await sleep(4000); const busy = await page.evaluate(() => document.querySelectorAll('.m-presence--busy').length); await page.screenshot(resolve(outDir, 'r-05-list.png')); console.log('=== 真机结论 ==='); console.log('① 等待提示:', sawWait ? 'OK' : 'FAIL'); console.log('② 实时步骤区:', sawLive ? 'OK' : 'FAIL'); console.log('③ 输入区透明:', style.composerBg === 'rgba(0, 0, 0, 0)' ? 'OK' : `FAIL (${style.composerBg})`); console.log('④ 输入框贴边:', style.dockLeft !== null && style.dockLeft <= 18 ? `OK (left=${style.dockLeft})` : `FAIL (left=${style.dockLeft})`); console.log('⑤ 无容器命名:', style.placeholder === '发消息…' ? 'OK' : `检查 (${style.placeholder})`); console.log('⑥ 列表忙碌环:', busy > 0 ? 'OK' : '任务已结束未捕捉到(浏览器端已验证)'); page.close();