Files
Agent-Workbench/scripts/verify-python-ui.mjs

132 lines
11 KiB
JavaScript

import { releaseEvidencePrefix } from './qa-evidence.mjs';
import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import { chromium, expect } from 'playwright/test';
import { fixture, launchWorkbench } from '../tests/helpers.mjs';
import { createPythonDeliveryProject, pythonFixes, assertPythonProtected } from '../tests/fixtures/python-project.mjs';
import { captureWorkspaceSource } from '../tests/fixtures/workspace-project.mjs';
import { deliveryGit } from '../tests/fixtures/delivery-project.mjs';
const disposers = [], t = { after: fn => disposers.push(fn) }, f = fixture(t);
const protectedFiles = createPythonDeliveryProject(f), source = captureWorkspaceSource(f);
f.session.title = 'Python工程检查与交付'; f.store.core.task.get(f.session.taskId).goal = '修复三个Python模块,保护测试与声明,核对实际检查并交付本地Git报告。';
f.store.save(f.session); f.store.close();
const host = await launchWorkbench(t, f.data, process.env.AGENT_WORKBENCH_TEST_PYTHON ? { AGENT_WORKBENCH_PYTHON_PATH: process.env.AGENT_WORKBENCH_TEST_PYTHON } : {});
const proofPrefix = releaseEvidencePrefix(host.bootstrap.version);
assert.notEqual(new URL(host.url).port, '4318'); assert.equal(host.bootstrap.config.hasKey, false);
fs.mkdirSync('.qa/screenshots', { recursive: true });
const browser = await chromium.launch({ channel: 'msedge', headless: true }), page = await browser.newPage({ viewport: { width: 1440, height: 1000 } });
const passed = [], pageErrors = [], requestFailures = [], route = '/sessions/' + f.session.id;
page.on('pageerror', error => pageErrors.push(error.message));
page.on('requestfailed', request => requestFailures.push(request.url() + ': ' + request.failure()?.errorText));
const state = () => host.api(route), panel = () => page.getByLabel('任务记录', { exact: true });
const artifact = () => page.getByRole('dialog', { name: '执行结果工件', exact: true });
const mark = text => { passed.push(text); console.log('PASS ' + text); };
const screenshot = name => page.screenshot({ path: '.qa/screenshots/' + proofPrefix + '-python-' + name + '.png', scale: 'css' });
const choose = (name, selected) => panel().getByLabel('启用检查 ' + name, { exact: true }).setChecked(selected);
async function executeChecks() {
await panel().getByRole('button', { name: '执行这些检查', exact: true }).click();
const confirm = page.getByRole('dialog', { name: '执行工程检查', exact: true });
await confirm.getByRole('button', { name: '确认执行检查', exact: true }).click();
await expect.poll(async () => (await state()).execution.checks.at(-1)?.finishedAt, { timeout: 20000 }).toBeTruthy();
}
try {
await page.goto(host.url); await page.waitForLoadState('networkidle');
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 panel().getByRole('button', { name: '识别工程与检查', exact: true }).click();
await expect.poll(async () => (await state()).execution.plans.at(-1)?.checks.map(check => check.name)).toEqual(['test', 'build']);
await choose('test', false); await choose('build', false); await choose('python:test', true); await choose('python:compile', true);
await expect(panel().getByRole('button', { name: '执行这些检查', exact: true })).toBeDisabled();
await panel().getByRole('button', { name: '重新识别所选检查', exact: true }).click();
await expect.poll(async () => (await state()).execution.plans.at(-1).checks.map(check => check.name)).toEqual(['python:test', 'python:compile']);
await expect(panel().locator('.check-plan')).toContainText('Python');
assert.equal((await state()).execution.checks.length, 0);
mark('mixed projects retain npm defaults and explicitly seal the selected Python checks');
await panel().getByRole('button', { name: '执行这些检查', exact: true }).click();
const confirm = page.getByRole('dialog', { name: '执行工程检查', exact: true });
await expect(confirm).toContainText('unittest'); await expect(confirm).toContainText('Python'); await expect(confirm).toContainText('python.exe');
await confirm.getByRole('button', { name: '取消', exact: true }).click();
assert.equal((await state()).execution.checks.length, 0);
mark('the confirmation shows the actual interpreter and cancellation executes no checks');
await executeChecks();
const failed = (await state()).execution.checks.at(-1);
assert.equal(failed.status, 'failed'); assert.equal(failed.results[0].tests.failed, 5);
await expect(panel()).toContainText('测试 1/6 通过'); await expect(panel()).toContainText('失败 5');
await expect(panel()).toContainText('语法编译 6/6 个源码文件');
await screenshot('checks-desktop-failed');
mark('actual unittest failures and the separately passed compilation display their counted results');
await panel().getByRole('button', { name: '查看检查日志', exact: true }).first().click();
await expect(artifact()).toContainText('FAILED (failures=5)'); await expect(artifact()).toContainText('SHA256');
await artifact().getByRole('button', { name: '关闭窗口', exact: true }).click();
await panel().getByRole('button', { name: '查看实际测试依据', exact: true }).first().click();
await expect.poll(async () => JSON.parse(await artifact().locator('.artifact-preview').textContent()).tests.failed).toBe(5); await expect(artifact()).toContainText('python-check-result');
await artifact().getByRole('button', { name: '关闭窗口', exact: true }).click();
mark('the log and original structured result are independently viewable and hash-bound');
for (const [relative, content] of Object.entries(pythonFixes)) await host.api(route + '/proposals', { path: relative, content, reason: 'Verify the actual reviewed Python repair' });
const changes = (await state()).changes.filter(change => change.status === 'pending');
let session = await host.api(route + '/change-sets', { changeIds: changes.map(change => change.id) }), batch = session.execution.changeSets.at(-1);
await host.api(route + '/change-sets/' + batch.id + '/apply', { fingerprint: batch.fingerprint });
await expect.poll(async () => (await state()).execution.changeSets.at(-1).status).toBe('completed');
await panel().getByRole('button', { name: '重新识别检查计划', exact: true }).click();
await expect.poll(async () => (await state()).execution.plans.at(-1).checks.map(check => check.name)).toEqual(['test', 'build']);
await choose('test', false); await choose('build', false); await choose('python:test', true); await choose('python:compile', true);
await panel().getByRole('button', { name: '重新识别所选检查', exact: true }).click();
await expect.poll(async () => (await state()).execution.plans.at(-1).checks.map(check => check.name)).toEqual(['python:test', 'python:compile']);
await executeChecks();
assert.equal((await state()).execution.checks.at(-1).status, 'passed');
await expect(panel()).toContainText('测试 6/6 通过'); assertPythonProtected(assert, f.projectRoot, protectedFiles);
await panel().locator('.project-checks').scrollIntoViewIfNeeded(); await screenshot('checks-desktop-passed');
mark('a reviewed three-file repair runs new checks while retaining the original failed job and protected files');
await page.setViewportSize({ width: 390, height: 844 }); await page.getByRole('button', { name: '查看任务记录', exact: true }).click();
await expect(panel()).toBeVisible(); await panel().locator('.project-checks').scrollIntoViewIfNeeded();
await screenshot('checks-mobile');
await panel().getByRole('button', { name: '查看实际测试依据', exact: true }).first().click();
await expect.poll(async () => JSON.parse(await artifact().locator('.artifact-preview').textContent()).tests.passed).toBe(6); await screenshot('metrics-mobile');
await artifact().getByRole('button', { name: '关闭窗口', exact: true }).click();
await page.getByRole('button', { name: '关闭检视区', exact: true }).click();
const composer = await page.getByLabel('任务内容').boundingBox();
assert.ok(composer && composer.y >= 0 && composer.y + composer.height <= 844);
assert.ok(await page.locator('body').evaluate(node => node.scrollWidth <= 390));
mark('390px checks, interpreter paths, original result dialog and composer remain operable without page overflow');
await page.setViewportSize({ width: 1440, height: 1000 }); await page.getByRole('button', { name: '查看任务记录', exact: true }).click();
const delivery = panel().getByLabel('Git交付', { exact: true });
await delivery.getByLabel('提交说明', { exact: true }).fill('fix: deliver the reviewed Python workflow');
await delivery.getByRole('button', { name: '预览Git提交', exact: true }).click();
const gitConfirm = page.getByRole('dialog', { name: '确认本地Git提交', exact: true });
await expect(gitConfirm).toContainText(/6\s*\/\s*6/); await expect(gitConfirm).toContainText('src/workflow/normalize.py');
await gitConfirm.getByRole('button', { name: '确认创建本地提交', exact: true }).click();
await expect.poll(async () => (await state()).execution.deliveries.at(-1)?.reportArtifactId, { timeout: 20000 }).toBeTruthy();
session = await state(); const job = session.execution.deliveries.at(-1);
assert.deepEqual(job.paths.slice().sort(), Object.keys(pythonFixes).sort());
await delivery.getByRole('button', { name: '查看交付报告', exact: true }).click();
await expect(artifact()).toContainText('Python实际执行'); await expect(artifact()).toContainText('6/6');
await screenshot('delivery-desktop'); await artifact().getByRole('button', { name: '关闭窗口', exact: true }).click();
const after = captureWorkspaceSource(f);
assert.deepEqual(after.files, source.files);
const ownerIndex = deliveryGit(f, ['show', ':owner-staged.txt']); assert.match(ownerIndex, /preserve the index/);
assertPythonProtected(assert, f.projectRoot, protectedFiles);
assert.equal(session.execution.checks[0].status, 'failed');
mark('Git confirmation and the Python execution report deliver only the three reviewed sources and preserve owner changes');
assert.deepEqual(pageErrors, []); assert.deepEqual(requestFailures, []);
const result = { success: true, version: host.bootstrap.version, at: new Date().toISOString(), browser: browser.version(), passed, pageErrors, requestFailures,
checks: { before: failed.results[0].tests, after: session.execution.checks.at(-1).results[0].tests }, commit: job.commit,
scope: 'Independent host and owned fixture. Real Python processes, reviewed API repairs and actual local Git. No configured model requests or production credentials.' };
fs.writeFileSync('docs/' + proofPrefix + '-python-ui-validation.json', JSON.stringify(result, null, 2) + '\n'); console.log(JSON.stringify(result, null, 2));
} catch (error) {
await screenshot('python-ui-failure').catch(() => {});
fs.writeFileSync('.qa/python-ui-failure-dom.html', await page.content().catch(() => ''));
fs.writeFileSync('.qa/python-ui-failure.json', JSON.stringify({ error: error.message, passed, pageErrors, requestFailures }, null, 2)); throw error;
} finally {
await browser.close(); for (const dispose of disposers.reverse()) await dispose();
}