Files
Agent-Workbench/scripts/verify-requirements-ui.mjs
2026-11-06 07:26:36 +08:00

91 lines
7.6 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert/strict';
import { randomUUID } from 'node:crypto';
import { chromium } from 'playwright';
import { fixture, launchWorkbench, until } from '../tests/helpers.mjs';
import { beginPlanTask, completeRead } from '../tests/fixtures/task-plan-fixture.mjs';
const directory = path.resolve('.qa/requirements-progress', 'ui-' + randomUUID()); fs.mkdirSync(directory, { recursive: true });
const appRoot = path.resolve(process.env.AGENT_WORKBENCH_QA_APP_ROOT || '.');
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), originalSession = f.session; f.session = other; beginPlanTask(f, '另一个独立研究目标');
other.title = '另一任务的独立目标'; f.store.save(other); f.session = originalSession; f.store.close();
const host = await launchWorkbench(t, f.data, {}, { appRoot }); assert.notEqual(new URL(host.url).port, '4318'); assert.equal(host.bootstrap.config.hasKey, false);
const browser = await chromium.launch({ channel: 'msedge', headless: true }), page = await browser.newPage({ viewport: { width: 1440, height: 1080 } });
const passed = [], screenshots = [], errors = [];
page.on('pageerror', error => errors.push(error.message));
const panel = () => page.getByLabel('要求与证据', { exact: true }), row = () => panel().locator('.requirement-list > li').first();
const mark = name => { passed.push(name); console.log('PASS ' + name); };
const shot = async name => { const file = path.join(directory, name + '.png'); await page.screenshot({ path: file, scale: 'css' }); screenshots.push(file); };
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 panel().getByRole('button', { name: '记录要求', exact: true }).waitFor();
await until(async () => await panel().getByRole('button', { name: '记录要求', exact: true }).isEnabled());
}
async function verify() {
await row().getByRole('combobox').selectOption(read.id);
await row().getByRole('button', { name: '复核所选证据', exact: true }).click();
await until(async () => (await row().locator('.requirement-state').textContent()) === '所选证据已核对');
}
let result;
try {
await page.goto(host.url); await select(f.session);
await panel().getByRole('button', { name: '记录要求', exact: true }).click();
const editor = page.getByRole('dialog', { name: '记录用户要求', exact: true });
await editor.getByLabel('要求名称', { exact: true }).fill('解释当前工程的实际依据');
await editor.getByLabel('要求验收条件', { exact: true }).fill('保留README原读取和可核对结论,参考文件保持原字节');
await editor.getByRole('button', { name: '保存要求', exact: true }).click(); await editor.waitFor({ state: 'hidden' });
assert.equal(await row().locator('.requirement-state').textContent(), '待复核'); mark('manual source selection creates pending requirement, never automatic acceptance');
await verify(); mark('actual original read proof verified through UI');
await panel().scrollIntoViewIfNeeded(); await shot('desktop-verified');
await page.reload(); await select(f.session); await until(async () => (await row().locator('.requirement-state').textContent()) === '所选证据已核对');
mark('saved requirement and proof survive page refresh');
await row().getByRole('button', { name: '修订此要求', exact: true }).click();
await editor.getByLabel('要求验收条件', { exact: true }).fill('追加核对当前说明,旧复核记录保留');
await editor.getByRole('button', { name: '保存要求', exact: true }).click(); await editor.waitFor({ state: 'hidden' });
await until(async () => (await row().locator('.requirement-state').textContent()) === '待复核');
assert.match(await panel().textContent(), /原复核历史.*1 条/s); mark('requirement revision invalidates its proof and preserves original history');
await verify();
const file = path.join(f.projectRoot, 'README.md'), original = fs.readFileSync(file); fs.appendFileSync(file, '\nChanged externally in owned fixture\n');
await panel().getByRole('button', { name: '刷新复核', exact: true }).click();
await until(async () => (await row().locator('.requirement-state').textContent()) === '依据已失效');
await row().getByRole('button', { name: '复核所选证据', exact: true }).click();
await until(async () => (await panel().textContent()).includes('原读取对应旧文件SHA'));
await panel().scrollIntoViewIfNeeded(); await shot('desktop-stale'); mark('source mutation and stale original read are visible failures with preserved history');
fs.writeFileSync(file, original);
await page.setViewportSize({ width: 390, height: 844 });
if (!await panel().isVisible()) await page.getByRole('button', { name: '查看任务记录', exact: true }).click();
await panel().scrollIntoViewIfNeeded(); await shot('narrow-stale');
assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1), true); mark('390px viewport keeps requirement controls usable without page overflow');
await page.setViewportSize({ width: 1440, height: 1080 });
// Drain the revision-triggered 120 ms refresh before deliberately holding the
// manual refresh. Otherwise interception can disable the button we are about
// to click and turn this ownership test into a test-created deadlock.
await page.waitForLoadState('networkidle');
let release = null, held = false;
await page.route('**/requirements?taskId=' + f.session.taskId, async route => {
if (held) { await route.continue(); return; }
held = true; await new Promise(resolve => { release = resolve; });
try { await route.continue(); } catch (error) { if (!/Route is already handled|Invalid InterceptionId/.test(error.message)) errors.push(error.message); }
});
await panel().getByRole('button', { name: '刷新复核', exact: true }).click(); await until(() => held);
await select(other); release(); await page.unroute('**/requirements?taskId=' + f.session.taskId);
await until(async () => (await panel().textContent()).includes('尚未逐项记录要求'));
assert.ok(!(await panel().textContent()).includes('解释当前工程的实际依据')); mark('late response for original Task cannot replace the newly selected task');
assert.deepEqual(errors, []);
const requirements = await host.api('/sessions/' + f.session.id + '/requirements?taskId=' + f.session.taskId);
result = { success: true, version: host.bootstrap.version, appRoot, passed, screenshots, errors, requirements, realModelRequests: 0, officialServiceTouched: false, companionWritePermissionsAdded: false };
} catch (error) {
await shot('failure').catch(() => {}); result = { success: false, passed, screenshots, errors, error: error.stack, realModelRequests: 0 };
} finally {
await browser.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, requirements: undefined })); if (!result.success) process.exitCode = 1;