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

113 lines
9.8 KiB
JavaScript

import fs from 'node:fs';
import path from 'node:path';
import assert from 'node:assert/strict';
import { randomUUID } from 'node:crypto';
import { chromium, expect } from 'playwright/test';
import { fixture, launchWorkbench } from '../tests/helpers.mjs';
if (process.argv.includes('--help')) { console.log('node scripts/verify-model-center-ui.mjs — isolated model connections, pool, import preview, local inspection and 390px layout'); process.exit(0); }
const evidenceDir = path.resolve('.qa', 'model-center-ui-' + randomUUID()); fs.mkdirSync(evidenceDir, { recursive: true });
const disposers = [], t = { after: dispose => disposers.push(dispose) };
const f = fixture(t), inspectionArtifacts = [];
for (let index = 0; index < 65; index++) { const value = f.store.artifactStore.write(f.session, null, { ownedInspection: index }); f.store.core.artifact.set(value.id, value); inspectionArtifacts.push(value); }
f.store.save(f.session); const oldestArtifact = inspectionArtifacts[0]; fs.appendFileSync(path.join(f.store.artifactStore.root, oldestArtifact.relative), 'owned-original-corruption'); f.store.close();
const importFile = path.join(evidenceDir, 'owned-import.json');
fs.writeFileSync(importFile, JSON.stringify({ schema: 1, connections: [{ name: 'Imported Fixture', brand: '自定义', model: 'fixture-imported',
baseUrl: 'http://127.0.0.1:9/v1', apiKey: 'ui-owned-import-key' }] }));
const host = await launchWorkbench(t, f.data);
assert.notEqual(new URL(host.url).port, '4318'); assert.equal(host.bootstrap.config.hasKey, false);
const browser = await chromium.launch({ channel: 'msedge', headless: true });
const page = await browser.newPage({ viewport: { width: 1440, height: 1000 } });
const pageErrors = [], requestFailures = [], marks = [];
page.on('pageerror', error => pageErrors.push(error.message));
page.on('requestfailed', request => requestFailures.push(request.url() + ': ' + request.failure()?.errorText));
const mark = value => { marks.push(value); console.log('PASS ' + value); };
try {
await page.goto(host.url); await page.waitForLoadState('networkidle');
await page.getByRole('button', { name: '选择与管理模型', exact: true }).click();
const dialog = page.getByRole('dialog', { name: '模型中心', exact: true }); await expect(dialog).toBeVisible();
await dialog.getByRole('button', { name: '手动填写(兼容)', exact: true }).click();
const editor = dialog.getByRole('form', { name: '编辑模型连接', exact: true });
await editor.getByLabel('连接名称', { exact: true }).fill('Fixture Primary');
await editor.getByLabel('接口根地址', { exact: true }).fill('http://127.0.0.1:9/v1');
await editor.getByLabel('请求模型名称', { exact: true }).fill('fixture-primary');
await editor.getByLabel('临时 API Key', { exact: true }).fill('ui-owned-primary-key');
await editor.getByRole('button', { name: '保存模型连接', exact: true }).click();
await expect(dialog.locator('.mc-connection').filter({ hasText: 'Fixture Primary' })).toContainText('已设置,重启需重新填写');
assert.equal(JSON.stringify(await host.api('/model-center')).includes('ui-owned-primary-key'), false);
assert.equal((await host.api('/model-center')).connections.find(value => value.name === 'Fixture Primary').hasKey, true);
mark('connection form stores only the volatile credential and renders configured status without exposing it');
await dialog.getByRole('button', { name: '手动填写(兼容)', exact: true }).click();
const second = dialog.getByRole('form', { name: '编辑模型连接', exact: true });
await second.getByLabel('连接名称', { exact: true }).fill('Fixture Reserve');
await second.getByLabel('接口根地址', { exact: true }).fill('http://127.0.0.1:9/v1');
await second.getByLabel('请求模型名称', { exact: true }).fill('fixture-reserve');
await second.getByRole('button', { name: '保存模型连接', exact: true }).click();
await dialog.getByRole('button', { name: '模型池', exact: true }).click();
await dialog.getByRole('button', { name: '新建模型池', exact: true }).click();
const poolForm = dialog.getByRole('form', { name: '编辑模型池', exact: true });
await poolForm.getByLabel('模型池名称', { exact: true }).fill('Fixture failover pool');
await poolForm.locator('.mc-member-choice').filter({ hasText: 'Fixture Primary' }).locator('input[type=checkbox]').check();
await poolForm.locator('.mc-member-choice').filter({ hasText: 'Fixture Reserve' }).locator('input[type=checkbox]').check();
await poolForm.getByLabel('Fixture Primary权重', { exact: true }).fill('2');
await poolForm.getByRole('button', { name: '保存模型池', exact: true }).click();
const poolCard = dialog.locator('.mc-pool').filter({ hasText: 'Fixture failover pool' }); await expect(poolCard).toContainText('2 个成员');
await poolCard.getByRole('button', { name: '使用此模型池', exact: true }).click();
const center = await host.api('/model-center'); assert.equal(center.selected.type, 'pool');
assert.equal(center.pools.find(value => value.id === center.selected.id).members.length, 2);
mark('pool editor saves weighted members and binds selection for new Runs');
await dialog.getByRole('button', { name: '配置导入', exact: true }).click();
await expect(dialog.locator('.mc-tabs button[aria-current]')).toContainText('配置导入');
await dialog.getByLabel('配置格式').selectOption('workbench-json');
await dialog.getByLabel('本地文件路径', { exact: true }).fill(importFile);
await dialog.getByRole('button', { name: '预览可导入连接', exact: true }).click();
await expect(dialog.locator('.mc-editor').filter({ hasText: '本次预览' })).toContainText('Imported Fixture');
assert.equal((await page.locator('body').innerText()).includes('ui-owned-import-key'), false);
await dialog.getByRole('button', { name: '导入这些连接', exact: true }).click();
assert.ok((await host.api('/model-center')).connections.some(value => value.name === 'Imported Fixture'));
const sourceCard = dialog.locator('.mc-import-source').filter({ hasText: 'workbench-json' });
await sourceCard.getByRole('button', { name: '启用自动同步', exact: true }).click();
let view = await host.api('/model-center'); assert.equal(view.imports[0].enabled, true);
fs.writeFileSync(importFile, JSON.stringify({ schema: 1, connections: [{ name: 'Imported Fixture', brand: '自定义', model: 'fixture-imported-v2',
baseUrl: 'http://127.0.0.1:9/v1', apiKey: 'ui-owned-import-key' }] }));
await dialog.getByRole('button', { name: '立即同步已启用来源', exact: true }).click();
await expect(dialog.locator('.mc-notice')).toContainText('更新 1 项');
view = await host.api('/model-center'); assert.equal(view.connections.find(value => value.name === 'Imported Fixture').model, 'fixture-imported-v2');
await sourceCard.getByRole('button', { name: '关闭自动同步', exact: true }).click();
assert.equal((await host.api('/model-center')).imports[0].enabled, false);
assert.equal(JSON.stringify(view).includes('ui-owned-import-key'), false);
mark('schema-bound import preview, opt-in automatic sync, revision update and sync shutdown keep credentials hidden');
await dialog.getByRole('button', { name: '运行巡检', exact: true }).click();
await dialog.getByRole('button', { name: '开始本地巡检', exact: true }).click();
await expect(dialog.locator('.mc-checks')).toContainText('SQLite quick_check通过');
const operations = await host.api('/operations'); assert.equal(operations.modelRequests, 0);
assert.equal(operations.artifacts.total, 65); assert.equal(operations.artifacts.checked, 50); assert.equal(operations.checks.find(value => value.id === 'artifacts').status, 'passed');
await dialog.getByRole('button', { name: '检查更早工件', exact: true }).click();
await expect(dialog.locator('.mc-operations-pages')).toContainText(oldestArtifact.id); await expect(dialog.getByRole('button', { name: '检查更早工件', exact: true })).toBeDisabled();
const olderInspection = await host.api('/operations?artifactOffset=0'); assert.equal(olderInspection.checks.find(value => value.id === 'artifacts').status, 'failed'); assert.equal(olderInspection.artifacts.results[0].id, oldestArtifact.id);
mark('real historical pagination detects the owned corrupted older artifact, retains its identity and leaves original bytes unchanged');
await page.screenshot({ path: path.join(evidenceDir, 'desktop-operations.png'), fullPage: false });
mark('local inspection shows real database and artifact checks and emits zero model requests');
await page.setViewportSize({ width: 390, height: 844 });
assert.ok(await page.locator('body').evaluate(node => node.scrollWidth <= 390), '390px viewport must not overflow horizontally');
await page.screenshot({ path: path.join(evidenceDir, 'mobile-operations.png'), fullPage: false });
await dialog.getByRole('button', { name: '模型连接', exact: true }).click();
await expect(dialog.locator('.mc-connection').filter({ hasText: 'Fixture Primary' })).toBeVisible();
assert.ok(await page.locator('body').evaluate(node => node.scrollWidth <= 390), '390px connections must not overflow horizontally');
await page.screenshot({ path: path.join(evidenceDir, 'mobile-connections.png'), fullPage: false });
mark('390px model-center modal and connection cards remain operable without horizontal page overflow');
assert.deepEqual(pageErrors, []); assert.deepEqual(requestFailures, []);
const report = { url: host.url, viewportWidths: [1440, 390], assertions: marks, pageErrors, requestFailures,
modelRequests: operations.modelRequests, screenshots: ['desktop-operations.png', 'mobile-operations.png', 'mobile-connections.png'] };
fs.writeFileSync(path.join(evidenceDir, 'result.json'), JSON.stringify(report, null, 2));
console.log('EVIDENCE ' + evidenceDir);
} finally {
await browser.close();
for (const dispose of disposers.reverse()) await dispose();
}