import fs from 'node:fs'; import path from 'node:path'; import net from 'node:net'; import http from 'node:http'; import https from 'node:https'; import { randomUUID, createHash, X509Certificate } from 'node:crypto'; import assert from 'node:assert/strict'; import { chromium, _electron } from 'playwright'; import { expect } from 'playwright/test'; import { fixture, launchWorkbench, until } from '../tests/helpers.mjs'; import { Provider } from '../src/server/provider.mjs'; import { Runtime } from '../src/server/runtime.mjs'; import { HostController } from '../src/core/desktop/host-controller.mjs'; import { desktopExecutableIdentity } from '../src/core/desktop/app-installation.mjs'; import { projectEnvironment } from '../src/core/process-environment.mjs'; import { openWorkbenchTool } from './ui-navigation.mjs'; const desktop = process.argv.includes('--desktop'); assert.ok(process.env.AGENT_WORKBENCH_QA_APP_ROOT, 'Use a separate candidate build'); const appRoot = fs.realpathSync.native(process.env.AGENT_WORKBENCH_QA_APP_ROOT); const directory = path.resolve('.qa/m1-open-records-2026-11-07', (desktop ? 'electron-' : 'web-') + randomUUID()); fs.mkdirSync(directory); console.log('EVIDENCE ' + directory); const disposers = [], t = { after: action => disposers.push(action) }, f = fixture(t, { modernDefaults: true }), errors = [], checks = [], screenshots = []; const runtime = new Runtime(f.store, new Provider()), run = f.store.beginRun(f.session, '自有完整历史与恢复界面样例', false, f.store.config); f.store.message(f.session, 'user', '自有完整历史与恢复界面样例'); f.session.providerMessages.push({ role: 'user', content: '自有完整历史与恢复界面样例' }); f.session.status = 'paused'; f.store.finishRun(f.session, run, 'owned_ui_fixture'); const oldInput = { scope: 'Synthetic failed MCP display fixture; no process launched', call: null }; for (let index = 0; index < 61; index++) { const artifact = f.store.artifactStore.write(f.session, run, { ...oldInput, ordinal: index }, { kind: 'mcp-input' }); f.store.core.artifact.set(artifact.id, artifact); const job = { id: randomUUID(), sessionId: f.session.id, projectId: f.project.id, taskId: run.taskId, runId: run.id, operation: 'probe', status: 'failed', startedAt: new Date(Date.now() + index).toISOString(), finishedAt: new Date().toISOString(), requiresVerification: false, processStarted: false, input: { ...oldInput, ordinal: index }, inputArtifactId: artifact.id, inputSha256: artifact.sha256, error: { code: 'OWNED_DISPLAY_FIXTURE', message: '自有失败记录 ' + index + ';未启动程序,不补造结果。' } }; f.store.core.mcp_job.set(job.id, job); } f.store.save(f.session); const checkpoints = []; for (let index = 0; index < 61; index++) checkpoints.push(await runtime.checkpointManager().create(f.session)); await runtime.stopAll(); f.store.close(); let host, controller, browser, application, page, window, mobile, relay, cookie = '', result; const mark = text => { checks.push(text); console.log('PASS ' + text); }; async function resize(width, height = 960) { if (window) { await window.evaluate((win, size) => win.setContentSize(size.width, size.height), { width, height }); await until(() => page.evaluate(w => innerWidth === w, width)); } else await page.setViewportSize({ width, height }); } async function shot(name, target = page) { assert.ok(await target.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1), 'No page overflow'); const file = path.join(directory, name + '.png'); await target.screenshot({ path: file, scale: 'css' }); screenshots.push({ file: path.basename(file), sha256: createHash('sha256').update(fs.readFileSync(file)).digest('hex') }); fs.writeFileSync(path.join(directory, name + '.txt'), await target.locator('body').innerText()); } async function closeDialog() { await page.getByRole('dialog').getByRole('button', { name: '关闭窗口', exact: true }).click(); } try { if (desktop) { const server = net.createServer(); await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); const port = server.address().port; await new Promise(resolve => server.close(resolve)); assert.notEqual(port, 4318); controller = new HostController({ appRoot, dataRoot: f.data, port, nodePath: process.execPath, timeoutMs: 60000 }); await controller.start(); host = { url: controller.url, api: (route, input) => controller.api(route, input === undefined ? {} : { method: 'POST', input }) }; application = await _electron.launch({ executablePath: desktopExecutableIdentity(appRoot).path, args: [path.join(appRoot, 'dist-desktop/main.cjs'), '--app-root', appRoot, '--data-root', f.data, '--node', process.execPath, '--port', String(port), '--hidden-window'], cwd: appRoot, env: projectEnvironment(), timeout: 60000 }); page = await application.firstWindow(); window = await application.browserWindow(page); await window.evaluate(win => win.showInactive()); } else { host = await launchWorkbench(t, f.data, {}, { appRoot }); assert.notEqual(new URL(host.url).port, '4318'); } browser = await chromium.launch({ channel: 'msedge', headless: true }); if (!page) page = await browser.newPage(); page.on('pageerror', error => errors.push(error.message)); await page.addInitScript(({ projectId, sessionId }) => { localStorage.setItem('awb-project', projectId); localStorage.setItem('awb-session', sessionId); }, { projectId: f.project.id, sessionId: f.session.id }); await resize(1280); await page.goto(host.url); await page.getByRole('button', { name: '查看任务记录', exact: true }).click(); const checkpointPanel = page.getByLabel('任务恢复检查点', { exact: true }); await expect(checkpointPanel).toContainText('已加载 50 / 61'); const inlineMcp = page.getByLabel('MCP运行记录', { exact: true }); await expect(inlineMcp).toContainText('已加载 50 / 61'); await inlineMcp.getByRole('button', { name: '加载更早的 MCP 记录', exact: true }).click(); await expect(inlineMcp.locator('.mcp-history-row')).toHaveCount(61); await checkpointPanel.getByRole('button', { name: '加载更早的检查点', exact: true }).click(); await expect(checkpointPanel.locator('.checkpoint-card')).toHaveCount(61); await checkpointPanel.getByRole('button', { name: '保存恢复检查点', exact: true }).click(); await expect(checkpointPanel).toContainText('已加载 62 / 62'); await checkpointPanel.getByRole('button', { name: '核对恢复条件', exact: true }).last().click(); await expect(checkpointPanel).toContainText('当前可以续接原任务'); await shot('checkpoints-1280'); mark('Checkpoint pagination retains 61 old records when a new checkpoint is saved and verifies the oldest original'); await checkpointPanel.getByRole('button', { name: '查看原恢复包', exact: true }).last().click(); const original = page.getByRole('dialog', { name: '执行结果工件', exact: true }); await expect(original).toContainText('toolIndex'); await page.keyboard.press('Escape'); const readme = path.join(f.projectRoot, 'README.md'), before = fs.readFileSync(readme); fs.appendFileSync(readme, '\nOwned outside edit.\n'); await checkpointPanel.getByRole('button', { name: '核对恢复条件', exact: true }).last().click(); await expect(checkpointPanel).toContainText('工程源码已变化'); fs.writeFileSync(readme, before); await resize(390, 844); await shot('checkpoint-stale-390'); mark('Original v2 index is visible; changed source reports the true recovery failure at 390px'); await resize(1280); await openWorkbenchTool(page, '项目MCP工具'); const mcp = page.getByRole('dialog', { name: '项目 MCP 工具', exact: true }); await expect(mcp).toContainText('已加载 50 / 61'); await mcp.getByRole('button', { name: '加载更早的 MCP 记录', exact: true }).click(); await expect(mcp.locator('.mcp-history-row')).toHaveCount(61); await mcp.getByRole('button', { name: '刷新', exact: true }).click(); await expect(mcp.locator('.mcp-history-row')).toHaveCount(61); await mcp.getByRole('button', { name: '查看原始输入', exact: true }).last().click(); await expect(mcp.getByLabel('MCP原报告')).toContainText('"ordinal": 0'); await shot('mcp-history-1280'); await resize(390, 844); await shot('mcp-history-390'); const history = await host.api('/sessions/' + f.session.id + '/mcp/jobs'); assert.equal(history.length, 61); assert.equal((await host.api('/sessions/' + f.session.id + '/task-checkpoints')).length, 62); mark('All 61 MCP failures and earliest sealed input stay accessible after refresh; legacy unpaged APIs retain all rows'); await closeDialog(); await resize(1280); await openWorkbenchTool(page, '手机连接'); const panel = page.getByRole('dialog', { name: '手机连接', exact: true }), initial = await host.api('/companion'); assert.ok(initial.addresses.length); const socket = net.createServer(); await new Promise(resolve => socket.listen(0, initial.addresses[0].address, resolve)); const port = socket.address().port; await new Promise(resolve => socket.close(resolve)); await panel.getByLabel('手机连接端口', { exact: true }).fill(String(port)); await panel.getByRole('button', { name: '开启 HTTPS', exact: true }).click(); await expect(panel).toContainText('手机连接已开启', { timeout: 25000 }); const certificate = path.join(directory, 'owned-ca.cer'); if (application) { await application.evaluate(({ BrowserWindow }, filename) => { BrowserWindow.getAllWindows()[0].webContents.session.once('will-download', (_event, item) => item.setSavePath(filename)); }, certificate); await panel.getByRole('button', { name: '下载手机根证书', exact: true }).click(); await until(() => fs.existsSync(certificate) && fs.statSync(certificate).size > 0, 15000); } else { const downloaded = page.waitForEvent('download'); await panel.getByRole('button', { name: '下载手机根证书', exact: true }).click(); await (await downloaded).saveAs(certificate); } const ca = new X509Certificate(fs.readFileSync(certificate)).toString(); await panel.getByLabel('允许停止任务', { exact: true }).check(); await panel.getByRole('button', { name: '生成一次性配对码', exact: true }).click(); await expect(panel.locator('.companion-pair-url')).toBeVisible(); const pairUrl = await panel.locator('.companion-pair-url').textContent(), target = new URL((await host.api('/companion')).origin); relay = http.createServer((request, response) => { const headers = { Host: target.host, Origin: target.origin, Connection: 'close', ...(cookie ? { Cookie: cookie } : {}), ...(request.headers['content-type'] ? { 'Content-Type': request.headers['content-type'] } : {}) }; const outgoing = https.request(new URL(request.url, target), { method: request.method, headers, ca, servername: '' }, incoming => { if (incoming.headers['set-cookie']?.length) cookie = incoming.headers['set-cookie'][0].split(';')[0]; const received = { ...incoming.headers }; delete received['set-cookie']; response.writeHead(incoming.statusCode, received); incoming.pipe(response); }); outgoing.on('error', () => { if (!response.headersSent) response.writeHead(503); response.end(); }); request.pipe(outgoing); }); await new Promise(resolve => relay.listen(0, '127.0.0.1', resolve)); const relayUrl = 'http://127.0.0.1:' + relay.address().port; mobile = await browser.newPage({ viewport: { width: 390, height: 844 }, isMobile: true, hasTouch: true }); mobile.on('pageerror', error => errors.push(error.message)); await mobile.goto(relayUrl + '/#pair=' + new URLSearchParams(new URL(pairUrl).hash.slice(1)).get('pair')); await mobile.getByLabel('设备名称', { exact: true }).fill('自有操作历史窗口'); await mobile.getByRole('button', { name: '连接这台电脑', exact: true }).click(); await expect(mobile.getByRole('heading', { name: '项目任务', exact: true })).toBeVisible(); await mobile.goto('about:blank'); // Seed while polling is stopped, then verify a deterministic first page. for (let index = 0; index < 61; index++) { const session = await host.api('/sessions/' + f.session.id); const response = await fetch(relayUrl + '/mobile-api/sessions/' + f.session.id + '/stop', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ requestId: randomUUID(), taskId: session.taskId, runId: session.runId, revision: session.revision }) }); assert.equal(response.status, 200, await response.text()); } await mobile.goto(relayUrl); await expect(mobile.getByRole('heading', { name: '项目任务', exact: true })).toBeVisible(); const operations = mobile.locator('.mobile-operation-history'); await expect(operations).toContainText('已加载 50 / 61'); await operations.locator('summary').click(); await operations.getByRole('button', { name: '加载更早的手机操作', exact: true }).click(); await expect(operations).toContainText('已加载 61 / 61'); await mobile.getByRole('button', { name: '刷新任务', exact: true }).click(); await expect(operations).toContainText('已加载 61 / 61'); await shot('mobile-operations-390', mobile); mark('CA-verified real gateway pairing and 61 durable stop receipts paginate and remain expanded on mobile refresh; no task or model was started'); await closeDialog(); await openWorkbenchTool(page, '手机连接'); // Start a fresh page view after the burst of new audit rows. const audit = panel.locator('.companion-audit'); await expect(audit).toContainText('已加载 50 / 123', { timeout: 15000 }); await audit.locator('summary').click(); await audit.getByRole('button', { name: '加载更早的设备操作', exact: true }).click(); await expect(audit).toContainText('已加载 100 / 123'); await audit.getByRole('button', { name: '加载更早的设备操作', exact: true }).click(); await expect(audit).toContainText('已加载 123 / 123'); await audit.scrollIntoViewIfNeeded(); await shot('companion-audit-1280'); await resize(390, 844); await audit.scrollIntoViewIfNeeded(); await shot('companion-audit-390'); mark('Desktop audit exposes all 123 original actions with accurate loaded/total counts at 1280 and 390px'); 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-operation-history')).toHaveCount(0); mark('Revocation clears mobile operation history and retained authority is rejected'); assert.deepEqual(errors, []); result = { success: true, appRoot, actualElectron: desktop, checks, screenshots, errors, viewportWidths: [1280, 390], syntheticMcpDisplayRows: 61, actualCompanionStopReceipts: 61, realModelRequests: 0, productionWrites: 0, nativePhone: 'not-tested; CA-verified local relay only' }; } catch (error) { if (page) await shot('failure').catch(() => {}); if (mobile) await shot('failure-mobile', mobile).catch(() => {}); result = { success: false, appRoot, actualElectron: desktop, checks, screenshots, errors, error: error.stack, realModelRequests: 0 }; } finally { if (host) await host.api('/companion/close', {}).catch(() => {}); if (relay) await new Promise(resolve => { relay.closeAllConnections(); relay.close(resolve); }); if (browser) await browser.close(); if (application) await application.close(); if (controller && await controller.health()) await controller.stop(); await host?.close?.(); for (const dispose of disposers.reverse()) await dispose(); fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify(result, null, 2)); } console.log(JSON.stringify({ directory, ...result })); if (!result.success) process.exitCode = 1;