Files
Agent-Workbench/scripts/verify-task-plans-ui.mjs
2026-10-30 20:34:54 +08:00

170 lines
16 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, expect } from 'playwright/test';
import { fixture, launchWorkbench } from '../tests/helpers.mjs';
import { beginPlanTask, completeRead } from '../tests/fixtures/task-plan-fixture.mjs';
if (process.argv.includes('--help')) { console.log('node scripts/verify-task-plans-ui.mjs — independent task dependencies, original evidence, stale checks, sealed revisions, desktop/mobile and late responses'); process.exit(0); }
const directory = path.resolve('.qa', 'task-plan-ui-' + randomUUID()); fs.mkdirSync(directory, { recursive: true });
const disposers = [], t = { after: dispose => disposers.push(dispose) }, f = fixture(t);
fs.mkdirSync(path.join(f.projectRoot, 'test'));
fs.writeFileSync(path.join(f.projectRoot, 'package.json'), JSON.stringify({ name: 'task-plan-ui-fixture', type: 'module', scripts: { test: 'node --test test/*.test.mjs', build: 'node --check src.mjs' } }));
fs.writeFileSync(path.join(f.projectRoot, 'src.mjs'), 'export const sum = (a, b) => a + b;\n');
fs.writeFileSync(path.join(f.projectRoot, 'test/sum.test.mjs'), "import test from 'node:test'; import assert from 'node:assert/strict'; import { sum } from '../src.mjs'; test('addition', () => assert.equal(sum(2, 3), 5)); test('negative', () => assert.equal(sum(-1, 2), 1));\n");
beginPlanTask(f, '读取工程并核验测试、构建和移动页面'); completeRead(f);
f.session.title = '有依赖的实际任务计划'; f.store.save(f.session); f.store.close();
let host = await launchWorkbench(t, f.data), deliberateNavigation = false, releaseHeld = null;
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: 1000 } });
const passed = [], pageErrors = [], requestFailures = [], screenshots = [];
const route = '/sessions/' + f.session.id, state = () => host.api(route), panel = () => page.getByLabel('执行计划', { exact: true });
page.on('pageerror', error => pageErrors.push(error.message));
page.on('requestfailed', request => { if (!deliberateNavigation) requestFailures.push(request.url() + ': ' + request.failure()?.errorText); });
const mark = text => { passed.push(text); console.log('PASS ' + text); };
const screenshot = async name => { const file = path.join(directory, name + '.png'); await page.screenshot({ path: file, scale: 'css' }); screenshots.push(file); };
async function selectTask() {
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: f.session.title }).click();
await page.getByRole('button', { name: '查看任务记录', exact: true }).click();
await expect(panel().getByRole('button', { name: /建立计划|补充步骤/ })).toBeEnabled();
}
const steps = () => panel().locator('.plan-step');
const editor = () => page.getByRole('dialog', { name: '编辑执行计划', exact: true });
async function expandEvidence(index) {
const details = steps().nth(index).locator('.plan-evidence');
if (!await details.evaluate(node => node.open)) await details.locator('summary').click();
}
async function verify(index) { await expandEvidence(index); await steps().nth(index).getByRole('button', { name: '核验此步骤', exact: true }).click(); }
async function newSession() {
if (!await page.getByRole('button', { name: '新建任务', exact: true }).first().isVisible()) await page.getByRole('button', { name: '展开项目栏', exact: true }).click();
await page.getByRole('button', { name: '新建任务', exact: true }).first().click();
await expect.poll(() => page.evaluate(() => localStorage.getItem('awb-session'))).not.toBe(f.session.id);
}
let result;
try {
await page.goto(host.url); await page.waitForLoadState('networkidle'); await selectTask();
await panel().getByRole('button', { name: '建立计划', exact: true }).click();
await editor().getByLabel('步骤1名称', { exact: true }).fill('读取原工程依据');
await editor().getByLabel('步骤1通过条件', { exact: true }).fill('实际读取本工程,保留原调用和结果');
await editor().getByLabel('识别工程', { exact: true }).uncheck(); await editor().getByLabel('读取文件', { exact: true }).check();
await editor().getByRole('button', { name: '追加步骤', exact: true }).click();
await editor().getByLabel('步骤2名称', { exact: true }).fill('测试和构建实际通过');
await editor().getByLabel('步骤2通过条件', { exact: true }).fill('当前源码两项测试及构建均实际通过');
await editor().getByLabel('步骤2类别', { exact: true }).selectOption('checks');
await editor().getByLabel('读取原工程依据', { exact: true }).check();
await screenshot('task-plan-desktop-editor');
await editor().getByRole('button', { name: '保存执行计划', exact: true }).click();
await expect(steps()).toHaveCount(2); await expect(steps().nth(1)).toContainText('先核验:读取原工程依据');
await expandEvidence(1); await expect(steps().nth(1).getByRole('button', { name: '核验此步骤', exact: true })).toBeDisabled();
const firstPlan = await host.api(route + '/task-plan'); assert.equal(firstPlan.revision, 1); assert.equal(firstPlan.all_verified, false);
assert.equal((await state()).execution.progress.totalTools, 1);
mark('structured editor seals two real steps and their dependency without running tools');
await verify(0); await expect(steps().nth(0).locator('.status')).toHaveText('已核验');
assert.equal((await state()).execution.progress.totalTools, 1);
await expandEvidence(0); await steps().nth(0).getByRole('button', { name: '查看原核验记录', exact: true }).click();
const artifact = page.getByRole('dialog', { name: '执行结果工件', exact: true });
await expect(artifact).toContainText('task-step-proof'); await expect(artifact).toContainText('SHA256');
await page.keyboard.press('Escape'); await expect(artifact).toHaveCount(0); await expect(panel()).toBeVisible();
mark('original owned read verifies the prerequisite and opens its hashed proof without replay');
const planned = await host.api(route + '/check-plans', {}), checkId = planned.execution.plans.at(-1).id;
await host.api(route + '/check-jobs', { planId: checkId });
await expect.poll(async () => (await state()).execution.checks.at(-1)?.status, { timeout: 15000 }).toBe('passed');
await panel().getByRole('button', { name: '核对当前计划', exact: true }).click();
await expect(steps().nth(1).getByRole('button', { name: '核验此步骤', exact: true })).toBeEnabled();
await verify(1); await expect(steps().nth(1).locator('.status')).toHaveText('已核验');
assert.equal((await host.api(route + '/task-plan')).all_verified, true);
assert.equal((await state()).execution.checks.at(-1).results[0].tests.total, 2);
await panel().evaluate(node => node.scrollIntoView({ block: 'start' }));
await screenshot('task-plan-desktop-verified');
mark('real two-test and build job supplies the current-source proof and completes the sealed plan');
fs.appendFileSync(path.join(f.projectRoot, 'src.mjs'), '\n// Outside edit after the verified check.\n');
await panel().getByRole('button', { name: '核对当前计划', exact: true }).click();
await expect(steps().nth(1).locator('.status')).toHaveText('需重新检查');
await verify(1); await expect(steps().nth(1)).toContainText('最近一次核验未通过');
const stale = await host.api(route + '/task-plan'); assert.equal(stale.all_verified, false);
assert.equal((await state()).execution.taskProofs.at(-1).status, 'rejected');
const reported = await host.api(route + '/delivery-reports', {});
const reportArtifact = reported.execution.artifacts.findLast(value => value.kind === 'delivery-report');
const reportView = await host.api('/artifacts/' + reportArtifact.id + '?session=' + f.session.id + '&limit=16000');
assert.equal(JSON.parse(reportView.content).status, 'needs_plan_verification');
mark('outside source edits invalidate the current proof, retain the rejection and block a passed delivery report');
await page.setViewportSize({ width: 390, height: 844 });
await page.getByRole('button', { name: '查看任务记录', exact: true }).click(); await expect(panel()).toBeVisible();
await panel().getByRole('button', { name: '补充步骤', exact: true }).click();
await expect(editor().getByLabel('步骤1通过条件', { exact: true })).toBeDisabled();
await editor().getByRole('button', { name: '追加步骤', exact: true }).click();
await editor().getByLabel('步骤3名称', { exact: true }).fill('手机页面操作与断言');
await editor().getByLabel('步骤3通过条件', { exact: true }).fill('在390像素页面实际填写、点击并核对结果,保留原截图');
await editor().getByLabel('步骤3类别', { exact: true }).selectOption('browser');
await editor().getByLabel('步骤3页面宽度', { exact: true }).selectOption('390');
await editor().getByLabel('填写', { exact: true }).check(); await editor().getByLabel('点击', { exact: true }).check();
await editor().getByLabel('测试和构建实际通过', { exact: true }).check();
await screenshot('task-plan-mobile-editor');
assert.ok(await page.locator('body').evaluate(node => node.scrollWidth <= 390));
await editor().getByRole('button', { name: '保存执行计划', exact: true }).click();
await expect(steps()).toHaveCount(3); await expect(steps().nth(2)).toContainText('390px');
await expandEvidence(2); await expect(steps().nth(2).getByRole('button', { name: '核验此步骤', exact: true })).toBeDisabled();
const revised = await host.api(route + '/task-plan'); assert.equal(revised.revision, 2); assert.equal(revised.all_verified, false);
assert.deepEqual(revised.steps.slice(0, 2).map(({ key, title, criterion }) => ({ key, title, criterion })), firstPlan.steps.map(({ key, title, criterion }) => ({ key, title, criterion })));
await verify(0); await expect(steps().nth(0).locator('.status')).toHaveText('已核验');
await panel().getByRole('button', { name: '查看原计划', exact: true }).click(); await expect(artifact).toContainText('task-plan-contract');
await screenshot('task-plan-mobile-artifact'); await page.keyboard.press('Escape'); await expect(panel()).toBeVisible();
await screenshot('task-plan-mobile-steps');
await panel().evaluate(node => node.scrollIntoView({ block: 'start' })); await screenshot('task-plan-mobile-overview');
assert.ok(await page.locator('body').evaluate(node => node.scrollWidth <= 390));
mark('390px immutable revision adds a mobile step, preserves old criteria and keeps evidence dialogs operable');
await page.setViewportSize({ width: 1440, height: 1000 });
let held; const gate = new Promise(resolve => { releaseHeld = resolve; });
const matcher = '**/task-plan?taskId=*';
await page.route(matcher, pending => { held = pending; return gate; });
await panel().getByRole('button', { name: '核对当前计划', exact: true }).click(); await expect.poll(() => !!held).toBeTruthy();
await newSession();
await held.fulfill({ status: 500, contentType: 'application/json', body: JSON.stringify({ error: '原任务计划迟到错误' }) }); releaseHeld(); releaseHeld = null; await page.unroute(matcher);
await expect(page.getByText('原任务计划迟到错误', { exact: false })).toHaveCount(0);
mark('a delayed plan-read failure stays with the original task after selection changes');
await selectTask(); await panel().getByRole('button', { name: '补充步骤', exact: true }).click();
await editor().getByRole('button', { name: '追加步骤', exact: true }).click();
await editor().getByLabel('步骤4名称', { exact: true }).fill('追加原工程读取'); await editor().getByLabel('步骤4通过条件', { exact: true }).fill('保存到原任务的第四步骤');
let heldSave, savedReply; const saveGate = new Promise(resolve => { releaseHeld = resolve; });
const saveMatcher = '**/sessions/' + f.session.id + '/task-plans';
await page.route(saveMatcher, async pending => { heldSave = pending; const response = await pending.fetch(); savedReply = await response.json(); return saveGate; });
await editor().getByRole('button', { name: '保存执行计划', exact: true }).click(); await expect.poll(() => !!savedReply).toBeTruthy();
// Controlled lifecycle fault: dispatch the real React tab action while the
// save dialog blocks pointer input. This is not a claim that its backdrop
// permits user clicks; it verifies response handling after component unmount.
await page.locator('.inspector-tabs').getByRole('button', { name: '轨迹', exact: true }).dispatchEvent('click');
await heldSave.fulfill({ status: 201, contentType: 'application/json', body: JSON.stringify(savedReply) }); releaseHeld(); releaseHeld = null; await page.unroute(saveMatcher);
await expect(page.getByRole('dialog', { name: '编辑执行计划', exact: true })).toHaveCount(0);
await page.getByRole('button', { name: '查看任务记录', exact: true }).click(); await expect(steps()).toHaveCount(4);
assert.equal((await host.api(route + '/task-plan')).revision, 3);
mark('controlled tab unmount during a delayed successful save retains the original task result without restoring the closed editor');
const before = await host.api(route + '/export'); deliberateNavigation = true; await host.close(); host = await launchWorkbench(t, f.data);
await page.goto(host.url); await page.waitForLoadState('networkidle'); deliberateNavigation = false; await selectTask();
await expect(steps()).toHaveCount(4); const after = await host.api(route + '/export');
assert.deepEqual(after.execution.taskPlans, before.execution.taskPlans); assert.deepEqual(after.execution.taskProofs, before.execution.taskProofs);
assert.deepEqual(after.session.providerMessages, before.session.providerMessages); assert.equal(after.execution.progress.totalTools, 1);
assert.equal(after.execution.task.usage.requests, 0); assert.equal(after.execution.checks.length, 1);
mark('real host restart retains all plan revisions, accepted/rejected proofs and the original operations without replay');
assert.deepEqual(pageErrors, []); assert.deepEqual(requestFailures, []);
result = { success: true, version: host.bootstrap.version, testedAt: new Date().toISOString(), directory, passed, pageErrors, requestFailures, screenshots,
taskId: f.session.taskId, sessionId: f.session.id, planRevisions: after.execution.taskPlans.length, proofs: after.execution.taskProofs.length,
scope: 'Independent real Edge desktop/390px Workbench UI, actual npm checks and owned read fixture. No real model credentials or production service access; semantic project acceptance is separate.' };
} catch (error) {
await screenshot('task-plan-ui-failure').catch(() => {}); fs.writeFileSync(path.join(directory, 'failure-dom.html'), await page.content().catch(() => ''));
result = { success: false, version: host.bootstrap.version, testedAt: new Date().toISOString(), directory, passed, pageErrors, requestFailures, screenshots, error: error.stack }; process.exitCode = 1;
} finally { releaseHeld?.(); deliberateNavigation = true; await browser.close(); for (const dispose of disposers.reverse()) await dispose(); }
fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify(result, null, 2) + '\n');
const proofPrefix = (await import('./qa-evidence.mjs')).releaseEvidencePrefix(result.version);
fs.writeFileSync(`docs/${proofPrefix}-task-ui-validation.json`, JSON.stringify(result, null, 2) + '\n');
console.log(JSON.stringify({ success: result.success, version: result.version, groups: passed.length, directory, error: result.error }));