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

151 lines
13 KiB
JavaScript

import { openWorkbenchTool } from './ui-navigation.mjs';
import fs from 'node:fs';
import path from 'node:path';
import http from 'node:http';
import assert from 'node:assert/strict';
import { createHash, randomUUID } from 'node:crypto';
import { chromium, expect } from 'playwright/test';
import { fixture, launchWorkbench, until } from '../tests/helpers.mjs';
import { Provider } from '../src/server/provider.mjs';
if (process.argv.includes('--help')) { console.log('node scripts/verify-model-selection-ui.mjs [--app-root ABS] — isolated compiled UI, two owned HTTP routes, add/switch/budget/reload/demo/locking at desktop and 390px; no configured-model calls'); process.exit(0); }
const args = process.argv.slice(2);
assert.ok(!args.length || args.length === 2 && args[0] === '--app-root' && path.isAbsolute(args[1]));
const appRoot = args[1] ?? process.cwd();
const directory = path.resolve('.qa/model-selection-k-' + randomUUID()); fs.mkdirSync(directory, { recursive: true });
const disposers = [], t = { after: fn => disposers.push(fn) }, f = fixture(t), requests = [];
let release, block = false;
const upstream = http.createServer(async (req, res) => {
const chunks = []; for await (const chunk of req) chunks.push(chunk);
const body = JSON.parse(Buffer.concat(chunks)); requests.push({ url: req.url, body });
if (block) await new Promise(resolve => { release = resolve; });
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ model: body.model, choices: [{ message: { role: 'assistant', content: 'Owned route receipt.' } }],
usage: { prompt_tokens: 13, completion_tokens: 7, total_tokens: 20 } }));
});
await new Promise(resolve => upstream.listen(0, '127.0.0.1', resolve));
const endpoint = 'http://127.0.0.1:' + upstream.address().port;
f.store.saveConfig({ mode: 'compatible', baseUrl: endpoint + '/legacy/v1', model: 'owned-legacy-gpt', maxSteps: 1 });
const provider = new Provider(), hub = provider.modelHub(f.store);
const selected = hub.upsert({ name: 'Claude · 自有接口', model: 'owned-claude', baseUrl: endpoint + '/selected/v1', requiresKey: false, modelIdentityPolicy: 'require-match' });
const noKey = hub.upsert({ name: '待填凭据', model: 'no-credential', baseUrl: endpoint + '/no-key/v1' });
const pool = hub.pool({ name: '工程模型池', members: [{ connectionId: selected.id, weight: 1 }], strategy: 'round-robin', sticky: true, maxAttempts: 1, queueTimeoutMs: 1000 });
hub.select({ type: 'pool', id: pool.id }); f.store.close();
const nodePath = fs.existsSync(path.join(appRoot, 'runtime/node.exe')) ? path.join(appRoot, 'runtime/node.exe') : process.execPath;
const host = await launchWorkbench(t, f.data, {}, { appRoot, nodePath });
assert.notEqual(new URL(host.url).port, '4318');
let browser, page, result; const checks = [], screenshots = [], errors = [];
const mark = name => { checks.push(name); console.log('PASS ' + name); };
const capture = async name => { const file = path.join(directory, name + '.png'); await page.screenshot({ path: file }); screenshots.push({ path: file, sha256: createHash('sha256').update(fs.readFileSync(file)).digest('hex') }); };
const dialog = () => page.getByRole('dialog', { name: '模型中心', exact: true });
const select = () => dialog().getByLabel('新任务默认模型', { exact: true });
const close = () => dialog().getByRole('button', { name: '关闭窗口', exact: true }).click();
try {
browser = await chromium.launch({ channel: 'msedge', headless: true }); page = await browser.newPage({ viewport: { width: 1440, height: 1000 } });
page.on('pageerror', error => errors.push(error.message));
await page.goto(host.url); await page.waitForLoadState('networkidle'); await page.getByLabel('当前项目').selectOption(f.project.id);
await page.getByRole('button', { name: '选择与管理模型', exact: true }).click();
await expect(dialog()).toBeVisible(); await expect(select()).toHaveValue('pool:' + pool.id);
// This Playwright build reports disabled <option> as enabled. Inspect the
// native option property instead; the original DOM/error is kept in QA.
await expect(select().locator('option[value="connection:' + noKey.id + '"]')).toHaveJSProperty('disabled', true);
await capture('unified-entry-desktop');
await select().selectOption('connection:' + selected.id);
await expect(dialog().locator('.mc-current')).toContainText(selected.name);
assert.equal((await host.api('/model-center/selection')).modelSelection.id, selected.id);
await close(); await expect(page.getByRole('button', { name: '选择与管理模型' })).toContainText(selected.name);
await expect(page.getByRole('button', { name: '切换当前模型' })).toContainText(selected.name);
mark('sidebar and header share the authoritative multi-model selector; selection persists without a request');
await page.getByRole('button', { name: '切换当前模型', exact: true }).click();
await dialog().getByRole('button', { name: '添加模型', exact: true }).click();
const form = dialog().getByRole('form', { name: '编辑模型连接', exact: true });
const longName = 'DeepSeek · 本机工程备用连接与长名称适配';
await form.getByLabel('连接名称', { exact: true }).fill(longName);
await form.getByLabel('接口根地址', { exact: true }).fill(endpoint + '/new/v1');
await form.getByLabel('请求模型名称', { exact: true }).fill('owned-new-model');
await form.getByLabel('服务需要 API Key', { exact: true }).uncheck();
await form.getByRole('button', { name: '保存模型连接', exact: true }).click(); await expect(form).toHaveCount(0);
assert.equal((await host.api('/model-center/selection')).modelSelection.id, selected.id);
const added = (await host.api('/model-center')).connections.find(c => c.name === longName); assert.ok(added);
await select().selectOption('connection:' + added.id); await expect(select()).toHaveValue('connection:' + added.id);
await close(); await page.reload(); await page.getByRole('button', { name: '选择与管理模型' }).click();
await expect(select()).toHaveValue('connection:' + added.id); assert.equal(requests.length, 0);
mark('add from primary entry keeps the previous default, supports a new selection and survives reload without paid probes');
await select().selectOption('pool:' + pool.id); await expect(select()).toHaveValue('pool:' + pool.id);
await dialog().getByRole('button', { name: '运行预算', exact: true }).click();
const budgets = dialog().getByRole('form', { name: '运行预算设置', exact: true });
await budgets.getByLabel('上下文预算', { exact: true }).fill('32000');
await budgets.getByLabel('单次运行 tokens', { exact: true }).fill('90000');
await budgets.getByRole('button', { name: '保存运行预算', exact: true }).click();
await expect(dialog().getByRole('status')).toContainText('当前模型选择保持');
assert.equal((await host.api('/model-center/selection')).modelSelection.id, pool.id);
await dialog().getByRole('button', { name: '模型连接', exact: true }).click();
await dialog().locator('.mc-connection').filter({ has: page.getByRole('heading', { name: '原模型连接', exact: true }) }).getByRole('button', { name: '编辑', exact: true }).click();
const legacy = page.getByRole('dialog', { name: '原连接与演示设置', exact: true });
await expect(legacy.getByRole('button', { name: '保存并切换原连接', exact: true })).toBeVisible();
await legacy.getByRole('button', { name: '取消', exact: true }).click();
assert.equal((await host.api('/model-center/selection')).modelSelection.id, pool.id);
mark('budget-only saves preserve the pool and original connection editing is explicitly secondary and cancellable');
await page.getByLabel('任务内容').fill('仅回复一次,用于自有接口路由验收,不执行工具。');
await page.getByRole('button', { name: '发送任务', exact: true }).click();
await until(async () => { const current = await host.api('/sessions/' + f.session.id); return current.status !== 'running' && current.execution.run?.consumption?.length === 1; });
const firstRun = (await host.api('/sessions/' + f.session.id)).execution.run; const frozen = structuredClone(firstRun);
assert.equal(requests.length, 1); assert.equal(requests[0].url, '/selected/v1/chat/completions'); assert.equal(requests[0].body.model, selected.model);
await page.getByRole('button', { name: '切换当前模型' }).click();
await select().selectOption('legacy:legacy-default'); await expect(select()).toHaveValue('legacy:legacy-default');
await expect(dialog().locator('.mc-run-model')).toContainText(selected.model);
assert.deepEqual((await host.api('/sessions/' + f.session.id)).execution.run, frozen);
await capture('default-versus-run-desktop'); await close();
mark('actual pool request reaches only the selected endpoint; changing defaults preserves the sealed old Run and shows both identities');
await host.api('/model-center/select', { type: 'connection', id: selected.id });
await openWorkbenchTool(page, '打开模型中心');
await expect(select()).toHaveValue('connection:' + selected.id); await close();
await expect(page.getByRole('button', { name: '切换当前模型' })).toContainText(selected.name);
await page.getByRole('button', { name: '切换当前模型' }).click(); await select().selectOption('demo'); await expect(select()).toHaveValue('demo');
assert.equal((await host.api('/model-center')).selected.id, selected.id);
await select().selectOption('legacy:legacy-default'); await expect(select()).toHaveValue('legacy:legacy-default');
assert.equal((await host.api('/model-center/selection')).config.mode, 'compatible');
await close();
mark('opening refreshes another tab selection and choosing a real connection exits demo without losing profiles');
await page.setViewportSize({ width: 390, height: 844 }); await page.getByRole('button', { name: '切换当前模型' }).click();
await expect(select()).toHaveValue('legacy:legacy-default');
await select().selectOption('connection:' + added.id); await expect(select()).toHaveValue('connection:' + added.id);
assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth));
await capture('selection-390'); await close();
await page.getByRole('button', { name: '展开项目栏' }).click();
await expect(page.getByRole('button', { name: '选择与管理模型' })).toContainText(longName);
await page.getByRole('button', { name: '选择与管理模型' }).click(); await expect(select()).toHaveValue('connection:' + added.id);
await capture('sidebar-entry-390'); await close();
mark('390px header and sidebar both add and switch with long model labels and no page overflow');
await page.setViewportSize({ width: 1440, height: 1000 }); await host.api('/model-center/select', { type: 'connection', id: selected.id });
block = true; const second = await host.api('/sessions', { projectId: f.project.id });
await host.api('/sessions/' + second.id + '/messages', { content: 'Owned held request.' });
await until(() => requests.length === 2 && release);
await page.reload(); await page.getByRole('button', { name: '切换当前模型' }).click();
// The host also rejects edits if the active task belongs to a different visible session.
const attempted = await fetch(host.url + '/api/model-center/runtime-budgets', { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Workbench-Token': host.bootstrap.token }, body: JSON.stringify({ maxSteps: 4 }) });
assert.equal(attempted.status, 409);
await expect(select()).toBeDisabled(); await expect(dialog().getByRole('button', { name: '添加模型', exact: true })).toBeDisabled();
await capture('active-request-lock'); release(); block = false;
await until(async () => (await host.api('/sessions/' + second.id)).status !== 'running');
assert.deepEqual(errors, []);
mark('active request locks route and add controls and server rejects budget overrides across sessions');
result = { success: true, appRoot, testedVersion: (await host.api('/health')).version, directory, checks, screenshots, errors,
requests: requests.map(r => ({ path: r.url, model: r.body.model })), realModelRequests: 0 };
} catch (error) {
if (page) fs.writeFileSync(path.join(directory, 'failure.html'), await page.content());
fs.writeFileSync(path.join(directory, 'failure-center.json'), JSON.stringify(await host.api('/model-center'), null, 2));
if (page) await capture('failure').catch(() => {}); result = { success: false, directory, error: error.stack, checks, screenshots, errors, realModelRequests: 0 }; throw error;
} finally {
release?.(); fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify(result, null, 2) + '\n');
console.log(JSON.stringify({ success: result.success, directory, checks: checks.length }));
await browser?.close(); upstream.closeAllConnections(); await new Promise(resolve => upstream.close(resolve));
for (const dispose of disposers.reverse()) await dispose();
}