Files
Agent-Workbench/scripts/verify-task-agreement-ui.mjs
T
2026-11-04 13:38:08 +08:00

118 lines
12 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 { 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 ? 'electron-' : 'web-') + randomUUID()); fs.mkdirSync(directory, { recursive: true });
const disposers = [], t = { after: fn => disposers.push(fn) }, f = fixture(t);
beginPlanTask(f, '说明当前工程;参考 README.md;禁止修改原说明;后续范围待确认。无需测试。');
const read = completeRead(f); f.session.title = '草案与范围审阅'; f.store.save(f.session);
const other = f.store.createSession(f.project.id), original = f.session; f.session = other; beginPlanTask(f, '另一独立目标'); other.title = '草案隔离的另一任务'; f.store.save(other); f.session = original;
const originalMessages = structuredClone(f.session.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 });
const editor = () => page.getByRole('dialog', { name: '审阅原文草案', 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();
await until(() => panel().getByRole('button', { name: '从原文整理草案', exact: true }).isEnabled());
}
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 }); assert.notEqual(new URL(host.url).port, '4318'); browser = await chromium.launch({ channel: 'msedge', headless: true }); page = await browser.newPage({ viewport: { width: 1440, height: 1080 } }); await page.goto(host.url); }
page.on('pageerror', error => errors.push(error.message)); assert.equal(host.bootstrap.config.hasKey, false);
await page.locator('.connection.connected').waitFor(); await select(f.session);
await panel().getByRole('button', { name: '从原文整理草案', exact: true }).click(); await editor().waitFor();
assert.equal(await editor().getByLabel('草案1 内容用途', { exact: true }).inputValue(), 'unknown');
assert.equal(await editor().getByRole('button', { name: '审阅并保存', exact: true }).isEnabled(), false);
await editor().getByRole('button', { name: '取消', exact: true }).click();
assert.equal((await host.api('/sessions/' + f.session.id + '/requirements?taskId=' + f.session.taskId)).configured, false);
mark('generated and cancelled draft stays unconfigured, unresolved and independent of model history');
await panel().getByRole('button', { name: '从原文整理草案', exact: true }).click(); await editor().waitFor();
const roles = ['requirement', 'reference', 'prohibition', 'unknown', 'requirement'];
for (const [i, role] of roles.entries()) await editor().getByLabel(`草案${i + 1} 内容用途`, { exact: true }).selectOption(role);
await editor().getByLabel('草案1 要求证据类别', { exact: true }).selectOption('results');
assert.equal(await editor().getByLabel('草案1 要求文件范围', { exact: true }).inputValue(), '');
await editor().getByLabel('草案1 要求证据类别', { exact: true }).selectOption('analysis');
await editor().getByLabel('草案5 要求复核方式', { exact: true }).selectOption('not_requested');
await shot('desktop-draft'); await resize(390, 844);
await editor().getByLabel('草案2 内容用途', { exact: true }).scrollIntoViewIfNeeded();
assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1), true); await shot('narrow-draft');
await editor().getByRole('checkbox').check(); await editor().getByRole('button', { name: '审阅并保存', exact: true }).click(); await editor().waitFor({ state: 'hidden' });
await until(async () => await panel().locator('.requirement-list > li').count() === 5);
assert.deepEqual(await panel().locator('.requirement-state').allTextContents(), ['待复核', '参考已记录', '边界已记录', '范围待澄清', '按约定未执行']);
mark('reviewed classification preserves reference, boundary, unknown and no-test semantics without inferring file paths');
await resize(1440, 1080); await page.reload(); await page.locator('.connection.connected').waitFor(); await select(f.session);
const rows = panel().locator('.requirement-list > li'); await rows.nth(0).getByRole('combobox').selectOption(read.id);
await rows.nth(0).getByRole('button', { name: '复核所选证据', exact: true }).click(); await until(async () => (await rows.nth(0).locator('.requirement-state').textContent()) === '所选证据已核对');
assert.equal((await host.api('/sessions/' + f.session.id + '/requirements?taskId=' + f.session.taskId)).all_resolved, false);
mark('refresh restores saved scope, original read is separately verified and unknown still blocks resolution');
await rows.nth(3).getByRole('button', { name: '修订此要求', exact: true }).click();
const manual = page.getByRole('dialog', { name: '记录用户要求', exact: true });
await manual.getByLabel('内容用途', { exact: true }).selectOption('requirement');
await manual.getByLabel('要求复核方式', { exact: true }).selectOption('user');
await manual.getByLabel('要求验收条件', { exact: true }).fill('用户实际体验后确认说明范围');
await manual.getByRole('button', { name: '保存要求', exact: true }).click(); await manual.waitFor({ state: 'hidden' });
await rows.nth(3).getByRole('textbox').fill('实际阅读了当前说明,确认范围符合本次原目标。');
await rows.nth(3).getByRole('button', { name: '记录用户确认', exact: true }).click();
await until(async () => (await rows.nth(3).locator('.requirement-state').textContent()) === '用户已确认');
assert.equal((await host.api('/sessions/' + f.session.id + '/requirements?taskId=' + f.session.taskId)).all_resolved, true);
await panel().scrollIntoViewIfNeeded(); await shot('desktop-reviewed');
mark('explicit clarification and actual user confirmation resolve only the selected revision');
await panel().getByRole('button', { name: '从原文整理草案', exact: true }).click(); await editor().waitFor();
const externalView = await host.api('/sessions/' + f.session.id + '/requirements?taskId=' + f.session.taskId);
const clean = row => ({ key: row.key, role: row.role, title: row.title, criterion: row.criterion, method: row.method, kind: row.kind,
source: { kind: row.source.kind, id: row.source.id, start: row.source.start, end: row.source.end },
...(row.method === 'scope' ? {} : row.kind === 'analysis' ? { tools: row.tools } : { checks: row.checks }) });
const externalRows = externalView.items.map(clean); externalRows[0].criterion += '(另一窗口追加)';
await host.api('/sessions/' + f.session.id + '/requirements', { taskId: f.session.taskId, runId: f.session.runId, expected_revision: externalView.revision, requirements: externalRows });
await editor().getByRole('checkbox').check(); await editor().getByRole('button', { name: '审阅并保存', exact: true }).click();
await until(async () => /修订|旧来源/.test(await page.locator('body').innerText()));
assert.equal(await editor().isVisible(), true); await shot('desktop-stale-draft'); await editor().getByRole('button', { name: '取消', exact: true }).click();
mark('concurrent revision rejects stale draft while preserving its review fields and original ledger');
let release, finished, held = false; const handled = new Promise(resolve => { finished = resolve; });
await page.route('**/requirement-drafts', async route => {
held = true; await new Promise(resolve => { release = resolve; });
try { await route.continue(); } catch (error) { errors.push(error.message); } finally { finished(); }
});
await panel().getByRole('button', { name: '从原文整理草案', exact: true }).click(); await until(() => held); await select(other); release(); await handled; await page.unroute('**/requirement-drafts');
await until(async () => /尚未逐项记录/.test(await panel().textContent())); assert.equal(await editor().count(), 0);
mark('late draft response does not open a modal or overwrite another selected Task');
assert.deepEqual(errors, []);
const exported = await host.api('/sessions/' + f.session.id + '/export'); assert.deepEqual(exported.session.providerMessages, originalMessages);
fs.writeFileSync(path.join(directory, 'session.json'), JSON.stringify(exported, null, 2));
result = { success: true, version: host.bootstrap.version, appRoot, actualElectron: desktop, passed, screenshots, errors, realModelRequests: 0, officialServiceTouched: false };
} catch (error) {
if (page) { await page.screenshot({ path: path.join(directory, 'failure.png') }).catch(() => {}); fs.writeFileSync(path.join(directory, 'failure.txt'), await page.locator('body').innerText().catch(() => 'Unavailable')); }
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;