import fs from 'node:fs'; import path from 'node:path'; import http from 'node:http'; import https from 'node:https'; import { X509Certificate, randomUUID, createHash } from 'node:crypto'; import assert from 'node:assert/strict'; import { chromium, expect } from 'playwright/test'; import { fixture, launchWorkbench, until } from '../tests/helpers.mjs'; if (process.argv.includes('--help')) { console.log('node scripts/verify-companion-ui.mjs [--app-root ] - isolated desktop/mobile UI; an explicit package runs its contained Node and actual server. Loopback relay verifies the gateway CA; does not modify OS trust or substitute for iPhone acceptance.'); process.exit(0); } const args = process.argv.slice(2); if (args.length && (args.length !== 2 || args[0] !== '--app-root' || !path.isAbsolute(args[1]))) throw new Error('Use --app-root with an absolute package directory, or no arguments.'); const appRoot = fs.realpathSync.native(args.length ? args[1] : process.cwd()); const nodePath = args.length ? path.join(appRoot, 'runtime', 'node.exe') : process.execPath; assert.ok(fs.existsSync(nodePath)); if (args.length) assert.ok(fs.existsSync(path.join(appRoot, 'runtime', 'node_modules', 'npm', 'bin', 'npm-cli.js')), 'The contained engineering runner requires bundled npm.'); const directory = path.resolve('.qa', 'companion-ui-' + randomUUID()); fs.mkdirSync(directory, { recursive: true }); const disposers = [], t = { after: action => disposers.push(action) }, f = fixture(t); fs.writeFileSync(path.join(f.projectRoot, 'package.json'), JSON.stringify({ name: 'owned-companion-ui', private: true, type: 'module', scripts: { test: 'node --test fixture.test.mjs' } })); fs.writeFileSync(path.join(f.projectRoot, 'fixture.test.mjs'), "import test from 'node:test';import assert from 'node:assert/strict';test('owned mobile fixture',()=>assert.equal(1+1,2));\n"); f.store.close(); const host = await launchWorkbench(t, f.data, { Path: path.dirname(nodePath) + path.delimiter + (process.env.Path || process.env.PATH || '') }, { appRoot, nodePath }); assert.equal(fs.realpathSync.native(host.bootstrap.runtimeRoot), appRoot, 'The isolated host must actually serve the selected package.'); await host.api('/sessions/' + f.session.id + '/messages', { content: '手机伴随端验收:读取项目、准备原差异并等待手机审批。明确为本地离线夹具。' }); await until(async () => (await host.api('/sessions/' + f.session.id)).status === 'awaiting_approval'); const before = await host.api('/sessions/' + f.session.id), browser = await chromium.launch({ channel: 'msedge', headless: true }); const desktop = await browser.newPage({ viewport: { width: 1440, height: 1000 } }), errors = [], passed = [], screenshots = []; desktop.on('pageerror', error => errors.push(error.message)); const mark = text => { passed.push(text); console.log('PASS ' + text); }; async function shot(page, name) { const file = path.join(directory, name + '.png'); await page.screenshot({ path: file, fullPage: !(await page.locator(':modal').count()) }); screenshots.push(file); } let mobile, relay, gatewayStatus, cookie = '', disconnected = false; try { await desktop.addInitScript(({ projectId, sessionId }) => { localStorage.setItem('awb-project', projectId); localStorage.setItem('awb-session', sessionId); }, { projectId: f.project.id, sessionId: f.session.id }); await desktop.goto(host.url); await desktop.getByRole('button', { name: '手机连接', exact: true }).click(); const panel = desktop.getByRole('dialog', { name: '手机连接', exact: true }); await expect(panel).toBeVisible(); await expect(panel).toContainText('默认关闭'); const initial = await host.api('/companion'); assert.ok(initial.addresses.length, 'Requires an actual owned private IPv4 interface.'); const reservation = http.createServer(); await new Promise(resolve => reservation.listen(0, initial.addresses[0].address, resolve)); const port = reservation.address().port; await new Promise(resolve => reservation.close(resolve)); await panel.getByLabel('手机连接端口', { exact: true }).fill(String(port)); await panel.getByRole('button', { name: '开启 HTTPS', exact: true }).click(); await expect(panel).toContainText('手机连接已开启', { timeout: 20000 }); await expect(panel).toContainText('证书信任设置'); await shot(desktop, 'desktop-certificate'); const downloadEvent = desktop.waitForEvent('download'); await panel.getByRole('button', { name: '下载手机根证书', exact: true }).click(); const download = await downloadEvent, certFile = path.join(directory, 'fixture-mobile-ca.cer'); await download.saveAs(certFile); const ca = new X509Certificate(fs.readFileSync(certFile)).toString(); assert.equal(new X509Certificate(ca).ca, true); mark('desktop explicitly enables HTTPS and downloads the actual public CA without changing OS trust'); await panel.getByLabel('允许审批命令', { exact: true }).check(); await panel.getByLabel('允许停止任务', { exact: true }).check(); await panel.getByRole('button', { name: '生成一次性配对码', exact: true }).click(); await expect(panel.getByAltText('手机一次性配对二维码')).toBeVisible(); const pairUrl = await panel.locator('.companion-pair-url').textContent(); await shot(desktop, 'desktop-pairing'); gatewayStatus = await host.api('/companion'); // Browser rendering uses an owned loopback relay because this test must not // silently install a root or bypass a TLS warning. The relay verifies the // real gateway certificate for every request and holds only its fixture // Secure cookie in memory. Native Safari cookie/trust/PWA remains device QA. const target = new URL(gatewayStatus.origin); relay = http.createServer((request, response) => { if (disconnected && request.url.startsWith('/mobile-api/')) { response.writeHead(503, { 'Content-Type': 'application/json' }); response.end(JSON.stringify({ error: '自有断线夹具' })); return; } const headers = { Host: target.host, Origin: target.origin, Connection: 'close', ...(cookie ? { Cookie: cookie } : {}), ...(request.headers['content-type'] ? { 'Content-Type': request.headers['content-type'] } : {}) }; const forwarded = https.request(new URL(request.url, target), { method: request.method, headers, ca, servername: '' }, incoming => { const set = incoming.headers['set-cookie']; if (set?.length) cookie = set[0].split(';')[0]; const outgoing = { ...incoming.headers }; delete outgoing['set-cookie']; response.writeHead(incoming.statusCode, outgoing); incoming.pipe(response); }); forwarded.on('error', () => { if (!response.headersSent) { response.writeHead(503, { 'Content-Type': 'application/json' }); response.end(JSON.stringify({ error: '自有TLS转发中断' })); } else response.destroy(); }); request.pipe(forwarded); }); await new Promise(resolve => relay.listen(0, '127.0.0.1', resolve)); const relayUrl = 'http://127.0.0.1:' + relay.address().port; const mobileContext = await browser.newContext({ viewport: { width: 390, height: 844 }, isMobile: true, hasTouch: true }); mobile = await mobileContext.newPage(); mobile.on('pageerror', error => errors.push(error.message)); await mobile.goto(relayUrl + '/#pair=' + new URLSearchParams(new URL(pairUrl).hash.slice(1)).get('pair')); await expect(mobile.getByRole('heading', { name: '接续电脑上的任务', exact: true })).toBeVisible(); assert.equal(new URL(mobile.url()).hash, ''); await mobile.getByLabel('设备名称', { exact: true }).fill('验收 iPhone 观察窗口'); await mobile.getByRole('button', { name: '连接这台电脑', exact: true }).click(); await expect(mobile.getByRole('heading', { name: '项目任务', exact: true })).toBeVisible(); await shot(mobile, 'mobile-task-list'); await expect(panel).toContainText('验收 iPhone 观察窗口', { timeout: 10000 }); mark('390px real gateway pairing clears fragment, displays scoped task list and registers the same device in desktop'); await mobile.locator('.mobile-task-row').filter({ hasText: before.title }).click(); await expect(mobile.getByRole('heading', { name: '等待你的确认', exact: true })).toBeVisible(); await mobile.getByText('原任务与运行', { exact: true }).click(); await expect(mobile.locator('.mobile-task-title code')).toContainText(before.taskId); assert.ok(await mobile.locator('body').evaluate(body => body.scrollWidth <= innerWidth)); await shot(mobile, 'mobile-original-approval'); await mobile.getByRole('button', { name: '核对并批准', exact: true }).click(); const confirmation = mobile.getByRole('dialog'); await expect(confirmation).toContainText(before.runId); await confirmation.getByRole('button', { name: '确认', exact: true }).click(); await until(async () => ['needs_review', 'completed'].includes((await host.api('/sessions/' + f.session.id)).status)); await expect(mobile.locator('.mobile-receipt')).toContainText('已批准原请求'); const after = await host.api('/sessions/' + f.session.id); assert.equal(after.taskId, before.taskId); assert.equal(after.runId, before.runId); assert.equal(after.events.filter(item => item.tool === 'run_command').length, 1); mark('phone UI confirms original Task/Run approval and the existing Runtime executes one command'); await mobile.getByRole('button', { name: /^文件差异/ }).click(); await mobile.getByRole('button', { name: /WORKBENCH-NOTES.md/ }).click(); await expect(mobile.getByRole('dialog')).toContainText('SHA-256 已核对'); await shot(mobile, 'mobile-original-diff'); await mobile.getByRole('button', { name: '返回任务', exact: true }).click(); await host.api('/sessions/' + f.session.id + '/changes/' + after.changes.at(-1).id + '/apply', {}); assert.equal(fs.existsSync(path.join(f.projectRoot, 'WORKBENCH-NOTES.md')), true); await host.api('/sessions/' + f.session.id + '/check-plans', { names: ['test'] }); const planned = await host.api('/sessions/' + f.session.id); await host.api('/sessions/' + f.session.id + '/check-jobs', { planId: planned.execution.plans.at(-1).id }); await until(async () => ['passed', 'failed', 'unverified'].includes((await host.api('/sessions/' + f.session.id)).execution.checks.at(-1)?.status), 30000); const checked = await host.api('/sessions/' + f.session.id); assert.equal(checked.execution.checks.at(-1).status, 'passed'); await host.api('/sessions/' + f.session.id + '/delivery-reports', {}); await mobile.getByRole('button', { name: '检查与报告', exact: true }).click(); await expect(mobile.locator('.mobile-checks')).toContainText('通过', { timeout: 12000 }); await mobile.getByRole('button', { name: /^交付报告 [\d,]+ bytes/ }).click(); await expect(mobile.getByRole('dialog')).toContainText('SHA-256 已核对'); await shot(mobile, 'mobile-original-report'); await mobile.getByRole('button', { name: '返回任务', exact: true }).click(); mark('original paginated diff and counted check/report display verified content from the same Task'); disconnected = true; await mobile.getByRole('button', { name: '刷新任务', exact: true }).click(); await expect(mobile.locator('.mobile-connection')).toContainText('连接中断'); await shot(mobile, 'mobile-disconnected'); disconnected = false; await mobile.getByRole('button', { name: '重新同步', exact: true }).click(); await expect(mobile.locator('.mobile-connection')).toContainText('已同步'); assert.equal((await host.api('/sessions/' + f.session.id)).events.filter(item => item.tool === 'run_command').length, 1); await mobile.reload(); await expect(mobile.getByRole('heading', { name: '项目任务', exact: true })).toBeVisible(); mark('disconnect and refresh recover from persistent seq without submitting a new command or Run'); const cacheEntries = await mobile.evaluate(async () => { const names = await caches.keys(); return (await Promise.all(names.map(async name => (await (await caches.open(name)).keys()).map(request => new URL(request.url).pathname)))).flat(); }); assert.ok(cacheEntries.length > 0 && cacheEntries.every(route => !route.startsWith('/mobile-api/'))); mark('PWA service worker stores the shell and no API/project response'); await mobile.setViewportSize({ width: 768, height: 1024 }); await shot(mobile, 'tablet-task-list'); await desktop.setViewportSize({ width: 390, height: 844 }); await panel.evaluate(node => { node.scrollTop = 0; }); await shot(desktop, 'desktop-management-narrow'); assert.ok(await desktop.locator('body').evaluate(body => body.scrollWidth <= innerWidth)); await panel.getByRole('button', { name: '撤销', exact: true }).click(); await mobile.getByRole('button', { name: '刷新任务', exact: true }).click(); await expect(mobile.getByRole('heading', { name: '接续电脑上的任务', exact: true })).toBeVisible(); await expect(mobile.locator('.mobile-task-row')).toHaveCount(0); await shot(mobile, 'mobile-revoked'); assert.equal((await host.api('/sessions/' + f.session.id)).events.filter(item => item.tool === 'run_command').length, 1); mark('desktop revocation removes mobile project content immediately on the next read while preserving the original completed tool'); assert.deepEqual(errors, []); const result = { success: true, version: host.bootstrap.version, appRoot, nodePath, nodeSha256: createHash('sha256').update(fs.readFileSync(nodePath)).digest('hex'), containedRuntime: args.length > 0, passed, screenshots, errors, viewports: [1440, 390, 768], browser: 'Microsoft Edge via Playwright; owned loopback QA relay with actual CA verification', nativeSafariTrustCookiePWA: 'pending-real-iPhone', osTrustChanged: false, certificateWarningsBypassed: false, realModelRequests: 0, taskId: before.taskId, runId: before.runId }; fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify(result, null, 2)); } catch (error) { if (mobile) await shot(mobile, 'failure-mobile').catch(() => {}); await shot(desktop, 'failure-desktop').catch(() => {}); fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify({ success: false, error: error.message, stack: error.stack, passed, screenshots, errors, realModelRequests: 0 }, null, 2)); throw error; } finally { console.log('EVIDENCE ' + directory); await browser.close(); if (relay) await new Promise(resolve => { relay.closeAllConnections(); relay.close(resolve); }); await host.api('/companion/close', {}).catch(() => {}); for (const dispose of disposers.reverse()) await dispose(); }