Files
Agent-Workbench/scripts/verify-network-diagnostics-ui.mjs
T
2026-11-03 23:11:33 +08:00

103 lines
11 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 { 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-network-diagnostics-ui.mjs [--app-root ABSOLUTE_ROOT] — owned TCP/TLS-only diagnostics, stop/reload/revision/proxy scopes, desktop and 390px. Zero 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.length ? fs.realpathSync.native(args[1]) : process.cwd();
const targetVersion = JSON.parse(fs.readFileSync(path.join(appRoot, fs.existsSync(path.join(appRoot, 'MANIFEST.json')) ? 'MANIFEST.json' : 'package.json'))).version;
const root = path.resolve('.qa', 'network-q-ui-' + randomUUID()); fs.mkdirSync(root);
const disposers = [], f = fixture({ after: action => disposers.push(action) }), checks = [], screenshots = [], errors = [], traffic = [];
const sockets = new Set(); let plainBytes = 0, tlsOpen = 0;
const listen = async server => { server.on('connection', socket => { sockets.add(socket); socket.on('close', () => sockets.delete(socket)); });
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); return server.address().port; };
const plain = net.createServer(socket => socket.on('data', chunk => { plainBytes += chunk.length; }));
const stalled = net.createServer(socket => { tlsOpen++; socket.on('data', chunk => { traffic.push({ bytes: chunk.length, tls: chunk[0] === 22 }); }); socket.on('close', () => tlsOpen--); });
const plainPort = await listen(plain), tlsPort = await listen(stalled), refused = net.createServer(), refusedPort = await listen(refused);
await new Promise(resolve => refused.close(resolve));
const provider = new Provider(), hub = provider.modelHub(f.store);
const add = (name, scheme, port) => hub.upsert({ name, model: 'owned-no-model', baseUrl: scheme + '://127.0.0.1:' + port + '/v1', requiresKey: true });
const good = add('本机 TCP · 不需要模型凭据', 'http', plainPort), bad = add('本机拒绝连接 · 失败样例', 'https', refusedPort), waiting = add('本机 TLS 等待 · 可停止样例', 'https', tlsPort);
const initialConfig = structuredClone(f.store.config), initialCenter = hub.view(); 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({ after: action => disposers.push(action) }, f.data,
{ HTTPS_PROXY: 'http://owned-proxy-user:owned-proxy-secret@127.0.0.1:9', NO_PROXY: '127.0.0.1' }, { appRoot, nodePath });
assert.notEqual(new URL(host.url).port, '4318');
const 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));
let result;
const mark = value => { checks.push(value); console.log('PASS ' + value); };
const capture = async name => { const file = path.join(root, name + '.png'); await page.screenshot({ path: file, fullPage: true }); screenshots.push(file); };
const open = async () => { await page.getByRole('button', { name: '打开模型中心', exact: true }).click(); const dialog = page.getByRole('dialog', { name: '模型中心', exact: true }); await expect(dialog).toBeVisible(); return dialog; };
const tab = dialog => dialog.getByRole('navigation', { name: '模型中心分类' }).getByRole('button', { name: '连接诊断', exact: true });
const panel = () => page.getByRole('region', { name: '模型连接诊断', exact: true });
const start = async () => { await panel().getByRole('button', { name: '开始直连检查', exact: true }).click(); };
try {
await page.goto(host.url); await page.waitForLoadState('networkidle'); let dialog = await open();
const card = dialog.locator('.mc-connection').filter({ hasText: good.name }); await card.getByRole('button', { name: '连接诊断', exact: true }).click();
await expect(panel().getByLabel('诊断连接', { exact: true })).toHaveValue(good.id); await expect(panel()).toContainText('这条连接还没有检查记录');
await expect(panel().getByRole('button', { name: '开始直连检查', exact: true })).toBeEnabled(); await capture('empty-desktop');
mark('connection-card entry binds the exact saved target, works without a Key and shows an honest empty state');
await start(); await expect(panel().getByRole('status')).toContainText('直连握手通过', { timeout: 10000 });
await expect(panel()).toContainText('HTTP 端点无需 TLS'); await expect(panel()).toContainText('DNS 未返回此地址族');
await expect(panel()).toContainText('实际 API 路线尚未核验'); await panel().getByText('查看原诊断范围与运输参数', { exact: true }).click();
await expect(panel()).toContainText('未请求;能力与连接健康结论保持原记录'); await capture('passed-desktop');
mark('real loopback TCP success separates missing IPv6, HTTPS proxy scope and untested model API capability');
let view = await host.api('/model-center/network-diagnostics'); const goodId = view.jobs[0].id;
await page.reload(); dialog = await open(); await tab(dialog).click();
await panel().getByLabel('诊断连接', { exact: true }).selectOption(good.id);
await expect(panel().getByRole('status')).toContainText('直连握手通过');
assert.equal((await host.api('/model-center/network-diagnostics')).jobs[0].id, goodId); mark('refresh restores the original saved result without repeating network work');
await panel().getByLabel('诊断连接', { exact: true }).selectOption(bad.id); await start();
await expect(panel().getByRole('status')).toContainText('未通过', { timeout: 10000 }); await expect(panel()).toContainText('ECONNREFUSED'); await capture('refused-desktop');
mark('connection failure stays attached to the selected endpoint and shows its real TCP error code');
await panel().getByLabel('诊断连接', { exact: true }).selectOption(waiting.id); await start();
await expect(panel()).toContainText('TLS 握手中'); await expect(panel().getByRole('button', { name: '开始直连检查', exact: true })).toBeDisabled();
assert.equal((await host.api('/host/status')).activity.counts.networkDiagnostics, 1);
await panel().getByRole('button', { name: '停止诊断', exact: true }).click(); await expect(panel().getByRole('status')).toContainText('已停止'); await until(() => tlsOpen === 0);
mark('running TLS check disables duplicate start and explicit stop closes owned sockets before reporting stopped');
await start(); await expect(panel()).toContainText('TLS 握手中'); view = await host.api('/model-center/network-diagnostics'); const runningId = view.jobs[0].id;
await dialog.locator('.modal-heading .icon-button').click(); dialog = await open(); await tab(dialog).click();
await expect(panel()).toContainText('正在检查'); await panel().getByRole('button', { name: '查看正在运行的诊断', exact: true }).click();
await panel().getByRole('button', { name: '停止诊断', exact: true }).click(); await expect(panel().getByRole('status')).toContainText('已停止');
assert.equal((await host.api('/model-center/network-diagnostics')).jobs[0].id, runningId); mark('closing and reopening the model center recovers the same active check and retains explicit stop');
await host.api('/model-center/toggle', { id: good.id, enabled: false }); await panel().getByLabel('诊断连接', { exact: true }).selectOption(good.id);
await panel().getByRole('button', { name: '刷新连接诊断', exact: true }).click(); await expect(panel()).toContainText('连接配置已变化');
await start(); await expect(panel().getByRole('alert')).toContainText('连接已变化');
await dialog.getByRole('button', { name: '刷新模型中心', exact: true }).click();
await start();
await expect(panel().locator('.mn-result-heading .mn-revision')).toHaveText('修订 2');
await expect(panel().getByRole('status')).toContainText('直连握手通过');
await until(async () => (await host.api('/model-center/network-diagnostics')).active === 0);
mark('old revisions remain visible, stale starts reject before any socket, and refreshing permits a new bounded check');
await page.setViewportSize({ width: 390, height: 844 }); await capture('passed-390');
await panel().locator('.mn-result-heading').scrollIntoViewIfNeeded(); await capture('passed-result-390');
await panel().locator('.mn-paths').scrollIntoViewIfNeeded(); await capture('passed-paths-390');
await panel().getByRole('complementary', { name: '连接诊断历史' }).scrollIntoViewIfNeeded(); await capture('history-390');
assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth));
assert.ok(await panel().evaluate(element => element.scrollWidth <= element.clientWidth + 1));
const startBox = await panel().getByRole('button', { name: '开始直连检查', exact: true }).boundingBox(); assert.ok(startBox.height >= 44);
await panel().getByLabel('诊断连接', { exact: true }).selectOption(bad.id); await expect(panel()).toContainText('ECONNREFUSED');
await panel().locator('.mn-result-heading').scrollIntoViewIfNeeded(); await capture('refused-390');
mark('390px success and failure states fit without page or panel overflow and retain touch-sized actions');
view = await host.api('/model-center/network-diagnostics'); const center = await host.api('/model-center'), boot = await host.api('/bootstrap');
assert.deepEqual(center.selected, initialCenter.selected); assert.deepEqual(center.pools, initialCenter.pools); assert.deepEqual(boot.config, { ...initialConfig, hasKey: false });
assert.deepEqual(center.attempts, []); assert.ok(center.connections.every(value => Object.values(value.capabilities).every(state => state !== 'verified')));
assert.equal(plainBytes, 0); assert.ok(traffic.length > 0 && traffic.every(value => value.tls)); assert.equal(JSON.stringify(view).includes('owned-proxy-secret'), false);
assert.equal((await page.locator('body').innerText()).includes('owned-proxy-secret'), false); assert.deepEqual(errors, []);
mark('original routing, budgets, model ledger and capability results remain unchanged; no HTTP or credential text is sent');
result = { success: true, appRoot, targetVersion, checks, screenshots, errors, httpRequests: 0, realModelRequests: 0,
plainBytes, tlsFrames: traffic.length, jobs: view.jobs, originalSelectionPreserved: true, originalBudgetsPreserved: true };
} catch (error) { await capture('failure').catch(() => {}); result = { success: false, error: error.stack, checks, screenshots, errors, realModelRequests: 0 }; throw error; }
finally {
fs.writeFileSync(path.join(root, 'result.json'), JSON.stringify(result, null, 2)); console.log(JSON.stringify({ success: result.success, root, checks: checks.length }));
await browser.close(); for (const socket of sockets) socket.destroy();
for (const server of [plain, stalled]) await new Promise(resolve => server.close(resolve));
for (const dispose of disposers.reverse()) await dispose();
}