Files
Agent-Workbench/scripts/verify-unified-model-ui.mjs
2026-11-06 14:08:53 +08:00

121 lines
11 KiB
JavaScript

import { openWorkbenchTool } from './ui-navigation.mjs';
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 { chromium, _electron } from 'playwright';
import { expect } from 'playwright/test';
import { fixture, launchWorkbench, until } from '../tests/helpers.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';
if (process.argv.includes('--help')) { console.log('node scripts/verify-unified-model-ui.mjs [--desktop] — isolated empty/select/edit/save/stale/pool checks, no real credentials or model requests. AGENT_WORKBENCH_QA_APP_ROOT selects a built app.'); process.exit(0); }
assert.ok(process.argv.slice(2).every(value => value === '--desktop'));
const desktop = process.argv.includes('--desktop'), appRoot = path.resolve(process.env.AGENT_WORKBENCH_QA_APP_ROOT || '.');
const directory = path.resolve('.qa/model-unification', (desktop ? 'electron-' : 'web-') + randomUUID());
fs.mkdirSync(directory, { recursive: true }); console.log('EVIDENCE ' + directory);
const disposers = [], t = { after: fn => disposers.push(fn) }, f = fixture(t, { modernDefaults: true });
f.store.close();
const marks = [], screenshots = [], errors = [], launches = [];
let host, controller, browser, app, page, window, result;
const dialog = () => page.getByRole('dialog', { name: '模型中心', exact: true });
const editor = () => dialog().getByRole('form', { name: '编辑模型连接', exact: true });
const card = () => dialog().locator('[data-connection-id="legacy-default"]');
const mark = message => { marks.push(message); console.log('PASS ' + message); };
async function open() { await openWorkbenchTool(page, '打开模型中心'); await expect(dialog()).toBeVisible(); }
async function shot(name) { const file = path.join(directory, name + '.png'); await page.screenshot({ path: file, fullPage: true }); screenshots.push(file); }
async function resize(width) {
if (window) { await window.evaluate((win, size) => win.setContentSize(size.width, size.height), { width, height: 1080 }); await until(() => page.evaluate(w => innerWidth === w, width)); }
else await page.setViewportSize({ width, height: 1080 });
assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1), 'page horizontal overflow');
}
try {
if (desktop) {
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);
controller = new HostController({ appRoot, dataRoot: f.data, port, nodePath: process.execPath, timeoutMs: 60000 }); await controller.start();
host = { api: (route, input) => controller.api(route, input === undefined ? {} : { method: 'POST', input }), bootstrap: await controller.api('/bootstrap') };
const executablePath = desktopExecutableIdentity(appRoot).path;
app = await _electron.launch({ executablePath, args: [...(executablePath.toLowerCase().endsWith('agentworkbench.exe') ? [] : [path.join(appRoot, 'dist-desktop/main.cjs')]),
'--app-root', appRoot, '--data-root', f.data, '--node', process.execPath, '--port', String(port)], env: projectEnvironment({ AGENT_WORKBENCH_QA: '1', AGENT_WORKBENCH_NODE: process.execPath }) });
page = await app.firstWindow(); window = await app.browserWindow(page);
} else {
host = await launchWorkbench(t, f.data, {}, { appRoot }); assert.notEqual(new URL(host.url).port, '4318');
browser = await chromium.launch({ channel: 'msedge', headless: true }); page = await browser.newPage(); await page.goto(host.url);
}
page.on('pageerror', error => errors.push(error.message));
page.on('request', request => { if (/\/sessions\/[^/]+\/messages$/.test(request.url())) launches.push(request.url()); });
await page.waitForLoadState('networkidle'); await resize(1440);
const goal = 'Keep my task text while I select a model.';
await page.getByLabel('任务内容', { exact: true }).fill(goal);
await page.getByRole('button', { name: '发送任务', exact: true }).click();
await expect(dialog()).toBeVisible(); await expect(dialog().locator('.mc-connections > .mc-connection')).toHaveCount(0);
await expect(dialog()).toContainText('还没有模型连接');
await expect(dialog().getByLabel('新任务默认模型', { exact: true })).toHaveValue('');
await expect(dialog().locator('option[value="demo"]')).toHaveCount(0);
assert.equal(launches.length, 0); assert.doesNotMatch(await page.locator('body').innerText(), /离线演示|原模型连接|原连接与演示设置/);
await shot('empty-1440');
await dialog().getByRole('button', { name: '关闭窗口', exact: true }).click(); await expect(page.getByLabel('任务内容', { exact: true })).toHaveValue(goal);
mark('unconfigured install has no demo or placeholder connection; send opens model selection without executing or losing the task');
await host.api('/config', { ...host.bootstrap.config, mode: 'compatible', model: 'owned-existing-model', baseUrl: 'http://127.0.0.1:9/v1', apiKey: 'owned-ui-credential' });
await page.reload(); await page.waitForLoadState('networkidle'); await open();
await expect(card().getByRole('heading', { name: 'owned-existing-model', exact: true })).toBeVisible();
await card().getByRole('button', { name: '编辑', exact: true }).click();
await expect(editor()).toBeVisible(); await expect(page.getByRole('dialog')).toHaveCount(1);
await expect(editor().getByLabel('请求模型名称', { exact: true })).toHaveValue('owned-existing-model');
await expect(editor().getByLabel('上下文上限', { exact: true })).toHaveCount(0); await expect(editor().getByLabel('输出上限', { exact: true })).toHaveCount(0);
await editor().getByLabel('连接名称', { exact: true }).fill('Owned upstream');
await editor().getByLabel('并发请求数(0 为不限)', { exact: true }).fill('0');
await shot('unified-editor-1440');
await editor().getByRole('button', { name: '保存模型连接', exact: true }).click(); await expect(editor()).toHaveCount(0);
let center = await host.api('/model-center');
assert.equal(center.connections.find(value => value.id === 'legacy-default').name, 'Owned upstream');
assert.equal(center.connections.find(value => value.id === 'legacy-default').hasKey, true);
assert.equal(center.selected.id, 'legacy-default');
assert.equal(JSON.stringify(center).includes('owned-ui-credential'), false);
mark('existing connection opens the shared editor in the same modal, saves zero concurrency and retains credential and identity');
await card().getByRole('button', { name: '编辑', exact: true }).click();
await editor().getByLabel('连接名称', { exact: true }).fill('Keep my unsaved edit');
const current = center.connections.find(value => value.id === 'legacy-default');
await host.api('/model-center/connections', { ...current, name: 'Externally updated' });
await editor().getByRole('button', { name: '保存模型连接', exact: true }).click();
await expect(dialog().getByRole('status')).toContainText('模型连接已修改');
await expect(editor().getByLabel('连接名称', { exact: true })).toHaveValue('Keep my unsaved edit');
await editor().getByRole('button', { name: '取消', exact: true }).click();
await dialog().getByRole('button', { name: '刷新模型中心', exact: true }).click();
await expect(card()).toContainText('Externally updated');
mark('stale save shows its actual error and preserves the unsaved draft; cancel and refresh recover the current connection');
const other = await host.api('/model-center/connections', { name: 'Owned pool member', model: 'owned-other', baseUrl: 'http://127.0.0.1:9/v1', requiresKey: false });
const pool = await host.api('/model-center/pools', { name: 'Owned selected pool', members: [{ connectionId: other.id }] });
await host.api('/model-center/select', { type: 'pool', id: pool.id });
await dialog().getByRole('button', { name: '刷新模型中心', exact: true }).click();
await expect(dialog().getByLabel('新任务默认模型', { exact: true })).toHaveValue('pool:' + pool.id);
await card().getByRole('button', { name: '编辑', exact: true }).click(); await editor().getByLabel('连接名称', { exact: true }).fill('Owned final name');
await editor().getByRole('button', { name: '保存模型连接', exact: true }).click(); await expect(editor()).toHaveCount(0);
assert.equal((await host.api('/model-center')).selected.id, pool.id);
await card().getByRole('button', { name: '停用Owned final name', exact: true }).click(); await expect(card()).toContainText('已停用');
await card().getByRole('button', { name: '启用Owned final name', exact: true }).click(); await expect(card()).toContainText('已配置');
assert.equal((await host.api('/model-center')).selected.id, pool.id);
mark('edit and enable/disable behave like every other connection and keep the selected model pool');
await dialog().getByRole('button', { name: '关闭窗口', exact: true }).click(); await page.reload(); await page.waitForLoadState('networkidle'); await open();
await expect(card()).toContainText('Owned final name'); await expect(dialog().getByLabel('新任务默认模型', { exact: true })).toHaveValue('pool:' + pool.id);
for (const width of [1440, 390]) {
await resize(width); await shot('connections-' + width);
await card().getByRole('button', { name: '编辑', exact: true }).click(); await expect(editor()).toBeVisible();
await editor().getByLabel('连接名称', { exact: true }).scrollIntoViewIfNeeded();
assert.equal(await page.getByRole('dialog').count(), 1); assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1));
assert.doesNotMatch(await page.locator('body').innerText(), /离线演示|原模型连接|原连接与演示设置/);
await shot('editor-' + width); await editor().getByRole('button', { name: '取消', exact: true }).click();
}
mark('saved selection and connection survive reload; 1440px and 390px keep one consistent modal and no horizontal overflow');
assert.deepEqual(errors, []); assert.equal(launches.length, 0); center = await host.api('/model-center'); assert.equal(center.attempts.length, 0);
result = { success: true, desktop, appRoot, marks, screenshots, errors, realModelRequests: 0, totalModelRequests: 0, userProjectsExecuted: 0 };
} catch (error) { if (page) await shot('failure').catch(() => {}); result = { success: false, desktop, error: error.stack, marks, screenshots, errors }; }
finally { await app?.close(); await browser?.close(); await controller?.stop(); for (const dispose of disposers.reverse()) await dispose(); fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify(result, null, 2)); }
console.log(JSON.stringify({ directory, ...result })); if (!result.success) process.exitCode = 1;