Files
Agent-Workbench/scripts/verify-browser-ui.mjs
T

142 lines
14 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { releaseEvidencePrefix } from './qa-evidence.mjs';
import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert/strict';
import { createHash, randomUUID } from 'node:crypto';
import { chromium, expect } from 'playwright/test';
import { fixture, launchWorkbench } from '../tests/helpers.mjs';
import { createBrowserProject } from '../tests/fixtures/browser-project.mjs';
if (process.argv.includes('--help')) { console.log('node scripts/verify-browser-ui.mjs — independent real Edge browser plans, review, results, images, mobile, stop, late response and restart'); process.exit(0); }
const directory = path.resolve('.qa', 'browser-ui-' + randomUUID()); fs.mkdirSync(directory, { recursive: true });
const disposers = [], t = { after: value => disposers.push(value) }, f = createBrowserProject(fixture(t));
f.session.title = '工程实际页面验收';
const indexFile = path.join(f.projectRoot, 'dist', 'index.html'); fs.writeFileSync(indexFile, fs.readFileSync(indexFile, 'utf8').replace('<h1>', '<p>' + '页面证据 '.repeat(1800) + '</p><h1>'));
f.store.save(f.session); f.store.close(); let host = await launchWorkbench(t, f.data), route = '/sessions/' + f.session.id;
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 = [], actualJobs = []; let result, deliberateNavigation = false;
page.on('pageerror', error => pageErrors.push(error.message));
page.on('requestfailed', request => { if (!deliberateNavigation) requestFailures.push(request.url() + ': ' + request.failure()?.errorText); });
const panel = () => page.getByLabel('工程页面验收', { exact: true }), state = () => host.api(route);
const mark = message => { passed.push(message); console.log('PASS ' + message); };
const screenshot = async name => { const file = path.join(directory, name + '.png'); await page.screenshot({ path: file, scale: 'css' }); screenshots.push(file); };
const sha = value => createHash('sha256').update(value).digest('hex');
async function selectTask() {
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: '封存页面计划', exact: true })).toBeEnabled();
}
async function seal() {
const old = (await state()).execution.browserPlans.at(-1)?.id;
await panel().getByRole('button', { name: '封存页面计划', exact: true }).click();
await expect.poll(async () => (await state()).execution.browserPlans.at(-1)?.id).not.toBe(old);
return (await state()).execution.browserPlans.at(-1);
}
async function start() {
await panel().getByRole('button', { name: '运行这份计划', exact: true }).click();
await page.getByRole('dialog', { name: '执行页面检查', exact: true }).getByRole('button', { name: '确认运行页面检查', exact: true }).click();
}
async function finished() {
await expect.poll(async () => (await state()).execution.browserJobs.at(-1)?.finishedAt, { timeout: 20000 }).toBeTruthy();
const job = (await state()).execution.browserJobs.at(-1); actualJobs.push(job); return job;
}
async function original(job) {
let text = '', offset = 0, value;
do { value = await host.api(route + '/browser-jobs/' + job.id + '/record?offset=' + offset + '&limit=8000'); text += value.content; offset += value.content.length; } while (value.truncated);
assert.equal(sha(text), job.resultSha256); fs.writeFileSync(path.join(directory, 'original-' + job.id + '.json'), text); return JSON.parse(text);
}
try {
await page.goto(host.url); await page.waitForLoadState('networkidle'); await selectTask();
await expect(panel()).toContainText('Edge · 实际浏览器'); assert.equal((await state()).execution.browserJobs.length, 0);
for (let index = 1; index <= 3; index++) await panel().getByRole('button', { name: '添加步骤', exact: true }).click();
await panel().getByLabel('步骤 1 操作').selectOption('click'); await panel().getByLabel('步骤 1 元素').fill('#toggle');
await panel().getByLabel('步骤 2 操作').selectOption('assert_text'); await panel().getByLabel('步骤 2 元素').fill('#state'); await panel().getByLabel('步骤 2 预期文字').fill('已完成');
await panel().getByLabel('步骤 3 操作').selectOption('assert_count'); await panel().getByLabel('步骤 3 元素').fill('button'); await panel().getByLabel('步骤 3 预期数量').fill('1');
const plan = await seal(); assert.equal(plan.steps.length, 3); assert.equal(plan.build.files, 2); assert.equal((await state()).execution.browserJobs.length, 0);
mark('actual step editor seals the chosen build, viewport and three CSS interactions without opening the page');
await panel().getByRole('button', { name: '运行这份计划', exact: true }).click();
const confirm = page.getByRole('dialog', { name: '执行页面检查', exact: true }); await expect(confirm).toContainText('1280×900'); await expect(confirm).toContainText('#state'); await expect(confirm).toContainText(plan.build.sha256);
await confirm.getByRole('button', { name: '取消', exact: true }).click(); assert.equal((await state()).execution.browserJobs.length, 0);
mark('review shows exact build identity and planned assertions; cancelling executes no browser work');
await start(); const success = await finished(); assert.equal(success.status, 'passed', success.summary); assert.equal(success.assertions.total, 2); assert.equal(success.cleanup.exited, true);
await expect(panel()).toContainText('实际断言 2/2'); const succeeded = await original(success); assert.equal(succeeded.steps[2].actual, 1);
assert.ok(succeeded.pages.at(-1).text.includes('已完成')); await screenshot('browser-desktop-results');
mark('real inner Edge clicks the intended element and the two counted assertions display with original Task/Run/build identity');
await panel().getByRole('button', { name: '查看原执行记录', exact: true }).first().click();
const record = () => page.getByRole('dialog', { name: '浏览器原执行记录', exact: true }); await expect(record()).toContainText(success.resultSha256);
await expect(record().getByRole('button', { name: '下一段', exact: true })).toBeEnabled();
const first = await record().locator('.artifact-preview').textContent(); await record().getByRole('button', { name: '下一段', exact: true }).click();
await expect.poll(async () => await record().locator('.artifact-preview').textContent()).not.toBe(first); await expect(record()).toContainText('字符 8000');
await record().getByRole('button', { name: '关闭窗口', exact: true }).click();
const downloading = page.waitForEvent('download'); await panel().getByRole('button', { name: '下载原结果', exact: true }).first().click();
const download = await downloading, downloadPath = await download.path(); assert.equal(sha(fs.readFileSync(downloadPath)), success.resultSha256);
mark('original large JSON is paged in the UI and downloads the complete hash-identical result');
await panel().getByRole('button', { name: '最终截图', exact: true }).first().click(); const image = () => page.getByRole('dialog', { name: '原始页面截图', exact: true });
await expect.poll(async () => image().locator('img').evaluate(node => node.naturalWidth)).toBe(1280); await expect(image()).toContainText(success.screenshots.at(-1).sha256);
const imageDownload = page.waitForEvent('download'); await image().getByRole('button', { name: '下载原始PNG', exact: true }).click();
const png = await imageDownload, pngPath = await png.path(); assert.equal(sha(fs.readFileSync(pngPath)), success.screenshots.at(-1).sha256);
await screenshot('browser-desktop-original-image'); await image().getByRole('button', { name: '关闭窗口', exact: true }).click();
mark('the original screenshot renders at its actual resolution and downloads identical PNG bytes');
await panel().getByLabel('步骤 3 预期数量').fill('2'); await seal(); await start(); const failure = await finished();
assert.equal(failure.status, 'failed'); const failed = await original(failure); assert.equal(failed.steps[2].actual, 1); assert.equal(failed.assertions.failed, 1);
await expect(panel()).toContainText('第3步失败'); await expect(panel().getByRole('button', { name: '运行这份计划', exact: true })).toBeDisabled();
mark('a real failed assertion retains actual count one and requires a newly reviewed plan instead of replaying');
await page.setViewportSize({ width: 390, height: 844 }); await expect(page.locator('.workbench')).toHaveClass(/sidebar-hidden/);
await page.getByRole('button', { name: '查看任务记录', exact: true }).click();
await panel().getByLabel('浏览器视口').selectOption('mobile'); await panel().getByLabel('步骤 3 预期数量').fill('1'); await seal(); await start(); const mobile = await finished();
assert.equal(mobile.status, 'passed', mobile.summary); assert.equal(mobile.screenshots.at(-1).width, 390);
assert.ok(await page.locator('body').evaluate(node => node.scrollWidth <= 390)); await screenshot('browser-mobile-results');
await panel().getByRole('button', { name: '最终截图', exact: true }).first().click(); await expect.poll(async () => image().locator('img').evaluate(node => node.naturalWidth)).toBe(390);
await screenshot('browser-mobile-original-image'); await image().getByRole('button', { name: '关闭窗口', exact: true }).click();
mark('390px forms and result records remain usable and the inner browser captures its real 390px layout');
await panel().getByRole('button', { name: '添加步骤', exact: true }).click(); await panel().getByLabel('步骤 4 操作').selectOption('assert_count');
await panel().getByLabel('步骤 4 元素').fill('.never-present'); await panel().getByLabel('步骤 4 预期数量').fill('1'); await seal(); await start();
await expect.poll(async () => (await state()).execution.browserJobs.at(-1)?.steps.length, { timeout: 15000 }).toBe(3);
await panel().getByRole('button', { name: '停止浏览器检查', exact: true }).click(); const stopped = await finished();
assert.equal(stopped.status, 'interrupted'); assert.equal(stopped.cleanup.exited, true); assert.equal(stopped.steps[0].status, 'passed');
assert.equal((await state()).execution.browserJobs.length, 4);
mark('stopping an actual in-flight assertion closes its owned browser and preserves prior successful and failed jobs');
await page.setViewportSize({ width: 1440, height: 1000 });
if (await page.getByRole('button', { name: '展开项目栏', exact: true }).count()) await page.getByRole('button', { name: '展开项目栏', exact: true }).click();
let held, release; const delivered = new Promise(resolve => { release = resolve; });
const matcher = '**/browser-jobs/' + mobile.id + '/record?*';
await page.route(matcher, pending => { held = pending; return delivered; });
// Locate by original sequence: interrupted job, mobile success, failed job, desktop success.
await panel().locator('.browser-result').nth(1).getByRole('button', { name: '查看原执行记录', exact: true }).click();
await expect.poll(() => !!held).toBeTruthy(); const originalSession = (await state()).id;
await page.getByRole('button', { name: '新建任务', exact: true }).first().click();
await expect.poll(async () => (await host.api('/sessions?project=' + f.project.id)).length).toBe(2);
await expect.poll(async () => page.evaluate(() => localStorage.getItem('awb-session'))).not.toBe(originalSession);
await held.fulfill({ status: 500, contentType: 'application/json', body: JSON.stringify({ error: '原任务迟到错误' }) }); release(); await page.unroute(matcher);
await expect(page.getByText('原任务迟到错误', { exact: false })).toHaveCount(0); assert.equal((await state()).id, originalSession);
mark('a delayed original-record failure stays with its source and does not appear in a newly selected task');
const before = (await state()).execution.browserJobs; deliberateNavigation = true; await host.close(); host = await launchWorkbench(t, f.data);
await page.goto(host.url); await page.waitForLoadState('networkidle'); deliberateNavigation = false; await selectTask();
assert.deepEqual((await state()).execution.browserJobs, before); assert.equal((await state()).execution.task.usage.requests, 0);
await panel().getByRole('button', { name: '最终截图', exact: true }).first().click(); await expect.poll(async () => image().locator('img').evaluate(node => node.naturalWidth)).toBe(390);
await image().getByRole('button', { name: '关闭窗口', exact: true }).click();
mark('actual host restart preserves the four original browser jobs and PNGs without model requests or execution replay');
assert.deepEqual(pageErrors, []); assert.deepEqual(requestFailures, []);
result = { success: true, version: host.bootstrap.version, testedAt: new Date().toISOString(), directory, passed, pageErrors, requestFailures,
screenshots, taskId: actualJobs[0].taskId, sessionId: f.session.id, jobs: actualJobs.map(job => ({ id: job.id, status: job.status, planId: job.planId,
assertions: job.assertions, resultSha256: job.resultSha256, cleanup: job.cleanup, screenshots: job.screenshots })),
scope: 'Actual independent Workbench UI plus real Edge static-build executions. The configured production Provider, other projects and keys are not accessed.' };
} catch (error) {
await screenshot('browser-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 { 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');
fs.writeFileSync(`docs/${releaseEvidencePrefix(host.bootstrap.version)}-browser-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 }));