Files
Agent-Workbench/scripts/verify-requirements-desktop.mjs

56 lines
5.1 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 { _electron } from 'playwright';
import { fixture, 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 root = path.resolve('.qa/requirements-progress', 'electron-' + randomUUID()); fs.mkdirSync(root, { 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, '解释当前工程依据,保留用户原文和原读取。'); completeRead(f); f.session.title = 'Electron要求证据原任务'; f.store.save(f.session); f.store.close();
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);
const controller = new HostController({ appRoot, dataRoot: f.data, port, nodePath: process.execPath, timeoutMs: 60000 });
const passed = [], errors = [], screenshots = []; let application, result;
const mark = name => { passed.push(name); console.log('PASS ' + name); };
try {
await controller.start(); assert.equal((await controller.api('/bootstrap')).config.hasKey, false);
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 });
const page = await application.firstWindow(); page.on('pageerror', error => errors.push(error.message));
const window = await application.browserWindow(page); await window.evaluate(win => win.setSize(1440, 1080));
await page.locator('.connection.connected').waitFor(); assert.equal(await page.evaluate(() => typeof window.process), 'undefined');
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();
const panel = page.getByLabel('要求与证据', { exact: true }); await until(() => panel.getByRole('button', { name: '记录要求', exact: true }).isEnabled());
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('实际读取当前文件,保留原来源');
await editor.getByRole('button', { name: '保存要求', exact: true }).click(); await editor.waitFor({ state: 'hidden' }); mark('sandboxed actual Electron saves a sourced requirement');
const row = panel.locator('.requirement-list > li').first(); await row.getByRole('combobox').selectOption({ index: 1 });
await row.getByRole('button', { name: '复核所选证据', exact: true }).click(); await until(async () => (await row.locator('.requirement-state').textContent()) === '所选证据已核对');
mark('actual Electron verifies the original tool result');
await page.reload(); await page.locator('.connection.connected').waitFor();
if (!await panel.isVisible()) await page.getByRole('button', { name: '查看任务记录', exact: true }).click();
await until(async () => (await row.locator('.requirement-state').textContent()) === '所选证据已核对');
await window.evaluate(win => { win.showInactive(); win.setContentSize(1440, 1080); });
await until(() => page.evaluate(() => innerWidth === 1440));
await panel.scrollIntoViewIfNeeded(); const screenshot = path.join(root, 'verified.png'); await page.screenshot({ path: screenshot }); screenshots.push(screenshot);
mark('renderer reload restores the same Task and requirement proof'); assert.deepEqual(errors, []);
result = { success: true, version: (await controller.api('/bootstrap')).version, appRoot, passed, errors, screenshots, actualElectron: true, installationValidated: false, realModelRequests: 0, officialServiceTouched: false };
} catch (error) { result = { success: false, passed, errors, screenshots, error: error.stack, realModelRequests: 0 }; }
finally {
if (application) await application.close(); if (await controller.health()) await controller.stop();
for (const dispose of disposers.reverse()) await dispose();
fs.writeFileSync(path.join(root, 'result.json'), JSON.stringify(result, null, 2));
}
console.log(JSON.stringify({ root, ...result })); if (!result.success) process.exitCode = 1;