90 lines
10 KiB
JavaScript
90 lines
10 KiB
JavaScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import net from 'node:net';
|
|
import assert from 'node:assert/strict';
|
|
import { randomUUID } from 'node:crypto';
|
|
import { chromium, _electron } from 'playwright';
|
|
import { fixture, launchWorkbench, until } from '../tests/helpers.mjs';
|
|
import { beginPlanTask, completeRead } from '../tests/fixtures/task-plan-fixture.mjs';
|
|
import { RequirementReview } from '../src/core/tasks/requirement-review.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';
|
|
|
|
const desktop = process.argv.includes('--desktop'), appRoot = path.resolve(process.env.AGENT_WORKBENCH_QA_APP_ROOT || '.');
|
|
const directory = path.resolve('.qa/task-agreement', (desktop ? 'closure-electron-' : 'closure-web-') + randomUUID()); fs.mkdirSync(directory, { recursive: true });
|
|
const disposers = [], t = { after: fn => disposers.push(fn) }, f = fixture(t);
|
|
const originalRun = beginPlanTask(f, '修复原工程并通过测试,保留原失败。'); f.session.title = '原目标与诊断闭环';
|
|
const failure = { id: randomUUID(), source: 'model_connection', kind: 'network', stage: 'model_request', title: '隔离夹具原连接错误', message: '隔离夹具:原POST结果未知,不得重放。', runId: originalRun.id };
|
|
f.store.event(f.session, { kind: 'error', phase: 'failed', label: failure.title, diagnostic: failure });
|
|
f.session.lastFailure = failure; f.store.core.task.get(f.session.taskId).verification.status = 'failed';
|
|
const diagnostic = f.store.beginRun(f.session, '前面的错误是怎么回事?', false, f.store.config);
|
|
f.session.status = 'paused'; f.store.finishRun(f.session, diagnostic, 'diagnostic_answered', { status: 'completed' });
|
|
const row = (session, extra = {}) => ({ key: 'scope', role: 'unknown', title: '确认原目标范围', criterion: '保留未知范围', kind: 'analysis', method: 'scope',
|
|
source: { kind: 'goal', id: session.taskId, start: 0, end: f.store.core.task.get(session.taskId).goal.length }, ...extra });
|
|
const manager = new RequirementReview(f.store); await manager.save(f.session, { expected_revision: 0, requirements: [row(f.session)] });
|
|
const original = f.session, other = f.store.createSession(f.project.id); f.session = other;
|
|
beginPlanTask(f, '仅研究README.md,不修改文件。无需测试。'); other.title = '按约定完成的研究范围'; const read = completeRead(f);
|
|
await manager.save(other, { expected_revision: 0, requirements: [row(other, { role: 'requirement', method: 'evidence', tools: ['read_file'] })] });
|
|
await manager.verify(other, { expected_revision: 1, key: 'scope', evidence_id: read.id }); f.session = original;
|
|
const originalMessages = structuredClone(original.providerMessages); f.store.close();
|
|
const passed = [], screenshots = [], errors = []; let browser, application, controller, host, page, window, result;
|
|
const mark = name => { passed.push(name); console.log('PASS ' + name); };
|
|
const panel = () => page.getByLabel('当前缺口与下一步', { exact: true });
|
|
async function select(session) {
|
|
if (!await page.getByLabel('当前项目').isVisible()) await page.getByRole('button', { name: '展开项目栏', exact: true }).click();
|
|
await page.getByLabel('当前项目').selectOption(f.project.id); await page.locator('.session-list button').filter({ hasText: session.title }).click();
|
|
if (!await panel().isVisible()) await page.getByRole('button', { name: '查看任务记录', exact: true }).click();
|
|
}
|
|
async function resize(width, height) {
|
|
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) { const file = path.join(directory, name + '.png'); await page.screenshot({ path: file, scale: 'css' }); screenshots.push(file); fs.writeFileSync(path.join(directory, name + '.txt'), await page.locator('body').innerText()); }
|
|
try {
|
|
if (desktop) {
|
|
const socket = net.createServer(); await new Promise(resolve => socket.listen(0, '127.0.0.1', resolve)); const port = socket.address().port; await new Promise(resolve => socket.close(resolve)); assert.notEqual(port, 4318);
|
|
controller = new HostController({ appRoot, dataRoot: f.data, port, nodePath: process.execPath, timeoutMs: 60000 }); await controller.start();
|
|
host = { api: (route, input) => controller.api(route, input ? { method: 'POST', input } : {}), bootstrap: await controller.api('/bootstrap') };
|
|
const executable = desktopExecutableIdentity(appRoot).path;
|
|
application = await _electron.launch({ executablePath: executable, args: [...(executable.toLowerCase().endsWith('agentworkbench.exe') ? [] : [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()); await resize(1440, 1080);
|
|
} else { host = await launchWorkbench(t, f.data, {}, { appRoot }); browser = await chromium.launch({ channel: 'msedge', headless: true }); page = await browser.newPage({ viewport: { width: 1440, height: 1080 } }); await page.goto(host.url); }
|
|
assert.equal(host.bootstrap.config.hasKey, false); page.on('pageerror', error => errors.push(error.message));
|
|
await page.locator('.connection.connected').waitFor(); await select(original);
|
|
await until(async () => (await panel().innerText()).includes(failure.message));
|
|
assert.match(await panel().innerText(), /澄清原文用途/); assert.match(await panel().innerText(), /答复已完成/);
|
|
assert.match(await page.getByLabel('任务记录', { exact: true }).innerText(), /修复原工程并通过测试/);
|
|
await panel().scrollIntoViewIfNeeded(); await shot('desktop-diagnostic');
|
|
mark('completed read-only diagnostic shows the original execution error, unfinished goal and actionable unknown scope');
|
|
await panel().getByRole('button', { name: '查看诊断原绑定', exact: true }).click();
|
|
const artifact = page.getByRole('dialog', { name: '执行结果工件', exact: true }); await artifact.waitFor(); assert.match(await artifact.innerText(), new RegExp(originalRun.id));
|
|
await artifact.getByRole('button', { name: '关闭窗口', exact: true }).click();
|
|
await page.getByRole('button', { name: '生成交付报告', exact: true }).click(); await artifact.waitFor();
|
|
assert.match(await artifact.innerText(), /原POST结果未知/); assert.match(await artifact.innerText(), /澄清原文用途/); await artifact.getByRole('button', { name: '关闭窗口', exact: true }).click();
|
|
const statusUrl = '/sessions/' + original.id + '/delivery-status?taskId=' + original.taskId + '&runId=' + diagnostic.id;
|
|
const current = await host.api(statusUrl), saved = await host.api('/sessions/' + original.id);
|
|
const reportId = saved.execution.artifacts.findLast(value => value.kind === 'delivery-report').id;
|
|
const report = JSON.parse((await host.api('/artifacts/' + reportId + '?session=' + original.id + '&limit=64000')).content); assert.deepEqual(report.followUp, current.followUp);
|
|
mark('original diagnostic artifact opens and generated report uses the same current gaps and next step');
|
|
await page.reload(); await page.locator('.connection.connected').waitFor(); await select(original); await until(async () => (await panel().innerText()).includes(failure.message));
|
|
await resize(390, 844); if (!await panel().isVisible()) await page.getByRole('button', { name: '查看任务记录', exact: true }).click();
|
|
await panel().scrollIntoViewIfNeeded(); assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1), true); await shot('narrow-diagnostic');
|
|
mark('refresh and narrow layout retain the original Task, error and next action');
|
|
await resize(1440, 1080); await select(other); await until(async () => (await panel().innerText()).includes('约定范围已核对,按约定未测试'));
|
|
assert.doesNotMatch(await panel().innerText(), /原POST结果未知/); const research = await host.api('/sessions/' + other.id);
|
|
assert.equal(research.execution.checks.length, 0); assert.equal(research.execution.task.contract.checks, 'skip'); await panel().scrollIntoViewIfNeeded(); await shot('research-no-test');
|
|
mark('separately verified research closes its agreed scope without a fake test or another Task error');
|
|
await select(original); await until(async () => (await panel().innerText()).includes(failure.message));
|
|
let held = false, release, finished; const handled = new Promise(resolve => { finished = resolve; });
|
|
await page.route('**/delivery-status?*', async route => { held = true; await new Promise(resolve => { release = resolve; }); try { await route.continue(); } finally { finished(); } });
|
|
await panel().getByRole('button', { name: '刷新当前结果', exact: true }).click(); await until(() => held); await select(other); release(); await handled; await page.unroute('**/delivery-status?*');
|
|
await until(async () => (await panel().innerText()).includes('约定范围已核对,按约定未测试')); assert.doesNotMatch(await panel().innerText(), /原POST结果未知/);
|
|
mark('late status response cannot overwrite another selected Task');
|
|
const exported = await host.api('/sessions/' + original.id + '/export'); assert.deepEqual(exported.session.providerMessages, originalMessages); assert.deepEqual(errors, []);
|
|
fs.writeFileSync(path.join(directory, 'session.json'), JSON.stringify(exported, null, 2)); fs.writeFileSync(path.join(directory, 'report.json'), JSON.stringify(report, null, 2));
|
|
result = { success: true, version: host.bootstrap.version, appRoot, actualElectron: desktop, passed, screenshots, errors, realModelRequests: 0, officialServiceTouched: false };
|
|
} catch (error) { if (page) { await shot('failure').catch(() => {}); } result = { success: false, passed, screenshots, errors, error: error.stack, realModelRequests: 0 }; }
|
|
finally { if (application) await application.close(); if (browser) await browser.close(); if (controller && await controller.health()) await controller.stop(); 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;
|