Files
Agent-Workbench/scripts/verify-open-formal-ui.mjs
T
2026-11-06 14:08:53 +08:00

75 lines
6.9 KiB
JavaScript

import { openWorkbenchTool } from './ui-navigation.mjs';
import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert/strict';
import { randomUUID } from 'node:crypto';
import { chromium, _electron } from 'playwright';
import { expect } from 'playwright/test';
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';
import { openDefaults } from '../src/shared/contracts/execution-policy.mjs';
if (process.argv.includes('--help')) { console.log('node scripts/verify-open-formal-ui.mjs [--desktop] — read-only formal installed UI inspection; no model requests, permission edits or user project execution.'); process.exit(0); }
assert.ok(process.argv.slice(2).every(arg => arg === '--desktop'));
const desktop = process.argv.includes('--desktop'), url = 'http://127.0.0.1:4318';
const expectedVersion = JSON.parse(fs.readFileSync('package.json', 'utf8')).version;
const root = path.resolve('.qa/open-formal-ui-' + expectedVersion, (desktop ? 'formal-electron-' : 'formal-web-') + randomUUID()); fs.mkdirSync(root, { recursive: true });
const boot = await (await fetch(url + '/api/bootstrap', { headers: { Connection: 'close' } })).json();
assert.equal(boot.version, expectedVersion); assert.equal(boot.config.approvalMode, 'full');
for (const [key, value] of Object.entries(openDefaults)) assert.equal(boot.config[key], value, key);
const host = await (await fetch(url + '/api/host/status', { headers: { 'X-Workbench-Token': boot.token, Connection: 'close' } })).json(); assert.equal(host.activity.active, false);
let browser, app, page, window, result; const screenshots = [], errors = [];
try {
if (desktop) {
const controller = new HostController({ appRoot: boot.runtimeRoot, dataRoot: host.identity.dataRoot, port: 4318, timeoutMs: 60000 });
const connection = await controller.connect(); assert.equal(connection.identity.pid, host.identity.pid);
const executablePath = desktopExecutableIdentity(boot.runtimeRoot).path;
app = await _electron.launch({ executablePath, args: ['--app-root', boot.runtimeRoot, '--data-root', host.identity.dataRoot, '--port', '4318'], env: projectEnvironment({ AGENT_WORKBENCH_QA: '1' }) });
page = await app.firstWindow(); window = await app.browserWindow(page);
} else { browser = await chromium.launch({ channel: 'msedge', headless: true }); page = await browser.newPage(); await page.goto(url); }
page.on('pageerror', error => errors.push(error.message)); await page.waitForLoadState('networkidle');
for (const width of [1440, 390]) {
if (window) { await window.evaluate((value, size) => value.setContentSize(size.width, size.height), { width, height: 1080 }); await expect.poll(() => page.evaluate(() => innerWidth)).toBe(width); }
else await page.setViewportSize({ width, height: 1080 });
await expect(page.getByLabel('操作权限', { exact: true })).toHaveValue('full');
assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1));
const file = path.join(root, 'permission-' + width + '.png'); await page.screenshot({ path: file, fullPage: true }); screenshots.push(file);
}
await openWorkbenchTool(page, '打开模型中心');
const dialog = page.getByRole('dialog', { name: '模型中心', exact: true }); await dialog.getByRole('button', { name: '运行预算', exact: true }).click();
await expect(dialog.getByLabel('额度策略', { exact: true })).toHaveValue('unrestricted');
await expect(dialog.getByLabel('输出上限', { exact: true })).toHaveCount(0);
const file = path.join(root, 'open-budget-390.png'); await page.screenshot({ path: file, fullPage: true }); screenshots.push(file);
await dialog.getByRole('button', { name: '模型连接', exact: true }).click();
const center = await (await fetch(url + '/api/model-center', { headers: { 'X-Workbench-Token': boot.token, Connection: 'close' } })).json();
const connection = center.connections.find(value => value.id === center.selected.id && value.id !== 'legacy-default' && value.enabled)
?? center.connections.find(value => value.id !== 'legacy-default' && value.enabled);
const existing = center.connections.find(value => value.id === 'legacy-default' && value.model.trim());
const targets = [...new Map([connection, existing].filter(Boolean).map(value => [value.id, value])).values()];
await expect(dialog.locator('option[value="demo"]')).toHaveCount(0);
for (const connection of targets) {
const cards = dialog.locator('.mc-connections > .mc-connection');
await expect(cards).toHaveCount(center.connections.filter(value => value.id !== 'legacy-default' || value.model.trim()).length);
const card = dialog.locator('.mc-connections > [data-connection-id="' + connection.id + '"]');
await expect(card.getByRole('heading', { name: connection.name, exact: true })).toBeVisible();
await card.getByRole('button', { name: '编辑', exact: true }).click();
const editor = dialog.getByRole('form', { name: '编辑模型连接', exact: true });
await expect(page.getByRole('dialog')).toHaveCount(1);
await expect(editor.getByLabel('连接名称', { exact: true })).toHaveValue(connection.name);
await expect(editor.getByLabel('上下文上限', { exact: true })).toHaveCount(0);
await expect(editor.getByLabel('输出上限', { exact: true })).toHaveCount(0);
const upstream = connection.upstreamModel?.id === connection.model ? connection.upstreamModel : {};
for (const [field, label] of [['contextLimit', '模型最大上下文'], ['maxOutputTokens', '模型最大输出']]) {
await expect(editor.getByLabel(label, { exact: true })).toHaveText(upstream[field] ? Number(upstream[field]).toLocaleString() + ' tokens' : '本地无限制 · 由上游决定');
}
await editor.getByLabel('连接容量策略', { exact: true }).scrollIntoViewIfNeeded();
const capacityFile = path.join(root, 'model-capacity-' + connection.id + '-390.png'); await page.screenshot({ path: capacityFile, fullPage: true }); screenshots.push(capacityFile);
await editor.getByRole('button', { name: '取消', exact: true }).click();
}
await dialog.getByRole('button', { name: '关闭窗口', exact: true }).click();
assert.deepEqual(errors, []); result = { success: true, version: boot.version, pid: host.identity.pid, runtimeRoot: boot.runtimeRoot, desktop, screenshots, errors, realModelRequests: 0, userProjectsExecuted: 0, writes: 0 };
} catch (error) { if (page) await page.screenshot({ path: path.join(root, 'failure.png'), fullPage: true }).catch(() => {}); result = { success: false, error: error.stack, desktop, screenshots, errors }; }
finally { await app?.close(); await browser?.close(); fs.writeFileSync(path.join(root, 'result.json'), JSON.stringify(result, null, 2)); }
console.log(JSON.stringify({ root, ...result })); if (!result.success) process.exitCode = 1;