Files
Agent-Workbench/scripts/verify-mcp-recovery-ui.mjs
T
2026-11-06 14:08:53 +08:00

72 lines
6.2 KiB
JavaScript

import { openWorkbenchTool } from './ui-navigation.mjs';
import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert/strict';
import { randomUUID } from 'node:crypto';
import { execFile } from 'node:child_process';
import { promisify } from 'node:util';
import { chromium, expect } from 'playwright/test';
import { fixture, launchWorkbench, compatibleFixture, completion, call } from '../tests/helpers.mjs';
import { Provider } from '../src/server/provider.mjs';
import { Runtime } from '../src/server/runtime.mjs';
import { gitEnvironment } from '../src/core/git-process.mjs';
const directory = path.resolve('.qa', 'mcp-recovery-ui-' + randomUUID()); fs.mkdirSync(directory, { recursive: true });
const disposers = [], t = { after: dispose => disposers.push(dispose) }, f = fixture(t);
const runtime = new Runtime(f.store, new Provider()), registry = runtime.mcpRegistry();
const entry = path.join(f.root, 'owned-recovery-mcp.mjs'); fs.copyFileSync(path.resolve('tests/fixtures/registered-mcp-server.mjs'), entry);
const saved = registry.register(f.project.id, { name: '中断恢复工具', executable: process.execPath, entry, timeoutMs: 45000 });
await registry.start(f.session, { id: saved.id, configurationRevision: 1, confirm: true }).promise;
const value = registry.find(f.project.id, saved.id);
const enabled = registry.configure(f.project.id, { id: saved.id, configurationRevision: value.configurationRevision, enabled: true, allowedTools: ['hold'] });
const model = await compatibleFixture(t, () => completion([call('ui-recovery-crash', 'call_registered_mcp', {
registration_id: enabled.id, configuration_revision: enabled.configurationRevision, contract_sha256: enabled.contract.sha256,
tool: 'hold', arguments: { text: 'owned-ui-crash-effect' },
})]));
f.store.saveConfig({ mode: 'compatible', model: 'owned-recovery-ui', baseUrl: model.baseUrl, contextLimit: 24000 }); f.store.close();
const crash = await promisify(execFile)(process.execPath, [path.resolve('tests/fixtures/mcp-host-crash.mjs'), f.data, f.session.id],
{ env: gitEnvironment(), windowsHide: true, timeout: 18000 })
.then(() => assert.fail('Expected owned host exit.'), error => { assert.equal(error.code, 85, error.stderr); return JSON.parse(error.stdout.trim().split('\n').at(-1)); });
const host = await launchWorkbench(t, f.data), browser = await chromium.launch({ channel: 'msedge', headless: true });
const page = await browser.newPage({ viewport: { width: 1440, height: 1000 } });
const pageErrors = [], requestFailures = [], assertions = [];
page.on('pageerror', error => pageErrors.push(error.message));
page.on('requestfailed', request => requestFailures.push(request.url() + ': ' + request.failure()?.errorText));
const mark = value => { assertions.push(value); console.log('PASS ' + value); };
try {
await page.addInitScript(({ projectId, sessionId }) => { localStorage.setItem('awb-project', projectId); localStorage.setItem('awb-session', sessionId); }, { projectId: f.project.id, sessionId: f.session.id });
await page.goto(host.url); await page.waitForLoadState('networkidle');
await openWorkbenchTool(page, '项目MCP工具');
const dialog = page.getByRole('dialog', { name: '项目 MCP 工具', exact: true }), history = dialog.getByLabel('MCP运行记录', { exact: true });
const row = history.locator('.mcp-history-row.state-unknown'); await expect(row).toHaveCount(1);
await row.getByRole('button', { name: '查看原始输入', exact: true }).click();
const evidence = history.getByLabel('MCP原报告'); await expect(evidence).toContainText('没有完整原报告');
await evidence.locator('summary').click(); await expect(evidence).toContainText('owned-ui-crash-effect');
await evidence.scrollIntoViewIfNeeded(); await page.screenshot({ path: path.join(directory, 'desktop-mcp-interrupted-input.png') });
mark('actual host crash with no result report exposes its original verified input and unknown status');
await page.setViewportSize({ width: 390, height: 844 }); await evidence.scrollIntoViewIfNeeded();
assert.ok(await page.locator('body').evaluate(node => node.scrollWidth <= 390));
await page.screenshot({ path: path.join(directory, 'mobile-mcp-interrupted-input.png') });
await row.getByRole('button', { name: '人工核对结果', exact: true }).click();
const review = page.getByRole('dialog', { name: '核对未知的外部结果', exact: true });
await expect(review).toContainText('没有完整原报告'); await review.locator('summary').click(); await expect(review).toContainText('owned-ui-crash-effect');
assert.ok(await page.locator('body').evaluate(node => node.scrollWidth <= 390));
assert.ok(await review.evaluate(node => node.scrollWidth <= node.clientWidth));
await page.screenshot({ path: path.join(directory, 'mobile-mcp-review.png') });
await review.getByRole('button', { name: '我已核对原输入和进程', exact: true }).click(); await expect(review).toBeHidden();
const job = (await host.api('/sessions/' + f.session.id + '/mcp/jobs')).find(value => value.id === crash.jobId);
assert.equal(job.status, 'unknown'); assert.equal(job.requiresVerification, false); assert.equal(job.reportArtifactId, undefined);
const session = await host.api('/sessions/' + f.session.id); assert.deepEqual(session.execution.progress.requiresVerification, []);
assert.equal(model.requests.length, 1);
mark('390px original-input review clears the job and invocation gates while retaining unknown, no report and no automatic model replay');
assert.deepEqual(pageErrors, []); assert.deepEqual(requestFailures, []);
fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify({ success: true, assertions, viewportWidths: [1440, 390],
pageErrors, requestFailures, localFixtureModelRequests: model.requests.length, realModelRequests: 0, jobId: job.id }, null, 2));
console.log('EVIDENCE ' + directory);
} catch (error) {
await page.screenshot({ path: path.join(directory, 'failure.png') }).catch(() => {});
fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify({ success: false, error: error.message, assertions, pageErrors, requestFailures }, null, 2));
console.error('EVIDENCE ' + directory); throw error;
} finally { await browser.close(); await host.close(); for (const dispose of disposers.reverse()) await dispose(); }