177 lines
16 KiB
JavaScript
177 lines
16 KiB
JavaScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import http from 'node:http';
|
|
import net from 'node:net';
|
|
import assert from 'node:assert/strict';
|
|
import { randomUUID, createHash } from 'node:crypto';
|
|
import { chromium, _electron } from 'playwright';
|
|
import { expect } from 'playwright/test';
|
|
import { fixture, launchWorkbench, until, completion } from '../tests/helpers.mjs';
|
|
import { Provider } from '../src/server/provider.mjs';
|
|
import { Runtime } from '../src/server/runtime.mjs';
|
|
import { HostController } from '../src/core/desktop/host-controller.mjs';
|
|
import { desktopLaunchCommand } from './desktop-launch.mjs';
|
|
import { projectEnvironment } from '../src/core/process-environment.mjs';
|
|
import { openWorkbenchTool } from './ui-navigation.mjs';
|
|
|
|
const desktop = process.argv.includes('--desktop');
|
|
assert.ok(process.argv.slice(2).every(value => value === '--desktop'));
|
|
assert.ok(process.env.AGENT_WORKBENCH_QA_APP_ROOT, 'Use an independent candidate build');
|
|
const appRoot = fs.realpathSync.native(process.env.AGENT_WORKBENCH_QA_APP_ROOT);
|
|
const directory = path.resolve('.qa/m1-routing-permission-2026-11-06', (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 });
|
|
const requests = [], checks = [], screenshots = [], errors = [], externalBlocked = [];
|
|
let upstreamError = false;
|
|
const model = 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({ route: req.url, body });
|
|
res.writeHead(upstreamError ? 400 : 200, { 'Content-Type': 'application/json', 'x-request-id': 'owned-original-ui' });
|
|
res.end(JSON.stringify(upstreamError ? { error: { message: '自有上游原错:参数不被接受', type: 'owned_error' } }
|
|
: { ...completion([], '自有验证完成'), model: 'owned-reported-different' }));
|
|
});
|
|
await new Promise(resolve => model.listen(0, '127.0.0.1', resolve));
|
|
const provider = Object.assign(Object.create(Provider.prototype), { apiKey: '' }), hub = provider.modelHub(f.store);
|
|
const members = {};
|
|
for (const [key, tools] of [['negative', 'unsupported'], ['unknown', 'unknown'], ['known', 'supported']]) members[key] = hub.upsert({
|
|
name: '自有验证 · ' + key, model: 'owned-' + key, requiresKey: false, baseUrl: 'http://127.0.0.1:' + model.address().port + '/v1',
|
|
capabilityDeclarations: { tools }, modelIdentityPolicy: 'require-match',
|
|
});
|
|
const pool = hub.pool({ name: '自有自动适配池', members: Object.values(members).map(value => ({ connectionId: value.id })), sticky: true });
|
|
const negativePool = hub.pool({ name: '自有明确否定池', members: [{ connectionId: members.negative.id }] });
|
|
hub.select({ type: 'pool', id: pool.id });
|
|
const historical = { id: randomUUID(), model: 'owned-historical', connectionId: members.known.id, runId: 'owned-historical-run', ordinal: 1,
|
|
status: 'completed', startedAt: '2026-11-01T00:00:00Z', usage: null, usageStatus: 'unknown',
|
|
modelIdentity: { schemaVersion: 1, requestedModel: 'owned-historical', reportedModels: ['owned-old-report'], status: 'mismatch', complete: true,
|
|
invalidIdentifiers: false, policy: 'require-match', scope: 'Owned historical fixture; effective policy not saved.' } };
|
|
hub.attempts.push(historical); hub.saveAttempts();
|
|
const runtime = new Runtime(f.store, provider), registry = runtime.mcpRegistry(), entry = path.join(f.root, 'owned-mcp.mjs');
|
|
fs.copyFileSync('tests/fixtures/registered-mcp-server.mjs', entry);
|
|
let registration = registry.register(f.project.id, { name: '自有外部工具', executable: process.execPath, entry, timeoutMs: 2000 });
|
|
assert.equal((await registry.start(f.session, { id: registration.id, configurationRevision: registration.configurationRevision, confirm: true }).promise).status, 'completed');
|
|
registration = registry.find(f.project.id, registration.id);
|
|
registry.configure(f.project.id, { id: registration.id, configurationRevision: registration.configurationRevision, enabled: true, allowedTools: ['echo'] });
|
|
await runtime.stopAll(); f.store.close();
|
|
let host, browser, application, controller, page, window, result, desktopStdout = '', desktopStderr = '';
|
|
const mark = name => { checks.push(name); console.log('PASS ' + name); };
|
|
const center = () => page.getByRole('dialog', { name: '模型中心', exact: true });
|
|
async function openCenter() { if (!await center().count()) await openWorkbenchTool(page, '打开模型中心'); return center(); }
|
|
async function closeDialog() { await page.getByRole('dialog').getByRole('button', { name: '关闭窗口', exact: true }).click(); }
|
|
async function ledger() { await openCenter(); await center().getByRole('button', { name: '请求账本', exact: true }).click(); }
|
|
async function resize(width, height = 960) {
|
|
if (window) { await window.evaluate((win, size) => win.setContentSize(size.width, size.height), { width, height }); await until(() => page.evaluate(w => innerWidth === w, width)); }
|
|
else await page.setViewportSize({ width, height });
|
|
}
|
|
async function shot(name, locator) {
|
|
await locator.scrollIntoViewIfNeeded();
|
|
assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1), 'No page overflow');
|
|
const filename = path.join(directory, name + '.png'); await page.screenshot({ path: filename, scale: 'css' });
|
|
screenshots.push({ file: path.basename(filename), sha256: createHash('sha256').update(fs.readFileSync(filename)).digest('hex') });
|
|
fs.writeFileSync(path.join(directory, name + '.txt'), await page.getByRole('dialog').innerText());
|
|
}
|
|
async function request(title) {
|
|
const session = await host.api('/sessions', { projectId: f.project.id });
|
|
await host.api('/sessions/' + session.id + '/messages', { content: title });
|
|
await until(async () => !['running', 'awaiting_approval', 'stopping'].includes((await host.api('/sessions/' + session.id)).status), 30000);
|
|
const final = await host.api('/sessions/' + session.id);
|
|
fs.writeFileSync(path.join(directory, title + '-session.json'), JSON.stringify(final, null, 2)); return final;
|
|
}
|
|
async function permission(mode) {
|
|
await page.getByLabel('操作权限', { exact: true }).selectOption(mode);
|
|
await until(async () => (await host.api('/bootstrap')).config.approvalMode === mode);
|
|
}
|
|
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 = { url: controller.url, api: (route, input) => controller.api(route, input === undefined ? {} : { method: 'POST', input }) };
|
|
application = await _electron.launch({ ...desktopLaunchCommand(appRoot, ['--app-root', appRoot,
|
|
'--data-root', f.data, '--node', process.execPath, '--port', String(port), '--hidden-window']), cwd: appRoot, env: projectEnvironment(), timeout: 60000 });
|
|
application.process().stdout?.on('data', bytes => { desktopStdout += bytes.toString('utf8'); });
|
|
application.process().stderr?.on('data', bytes => { desktopStderr += bytes.toString('utf8'); });
|
|
page = await application.firstWindow(); window = await application.browserWindow(page); await window.evaluate(win => win.showInactive());
|
|
} 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();
|
|
}
|
|
page.on('pageerror', cause => errors.push(cause.message));
|
|
await page.route('**/*', route => {
|
|
const url = new URL(route.request().url());
|
|
if (url.origin === new URL(host.url).origin || ['data:', 'blob:'].includes(url.protocol)) return route.continue();
|
|
externalBlocked.push(url.origin); return route.abort();
|
|
});
|
|
await page.addInitScript(({ projectId, sessionId }) => { localStorage.setItem('awb-project', projectId); localStorage.setItem('awb-session', sessionId); }, { projectId: f.project.id, sessionId: f.session.id });
|
|
await resize(1280); await page.goto(host.url);
|
|
let session = await request('known'); assert.equal(session.status, 'completed'); assert.equal(requests.length, 1); assert.equal(requests[0].body.model, members.known.model);
|
|
await ledger(); let row = center().locator('.mc-attempts article').filter({ has: page.locator('strong').filter({ hasText: /^owned-known$/ }) });
|
|
await row.locator('.mc-routing-decision summary').click(); await expect(row).toContainText('自动选择排除'); await expect(row).toContainText('保留为未知后备');
|
|
await row.locator('.model-identity-details summary').click(); await expect(row).toContainText('严格核对型号'); await expect(row).toContainText('记录原结果和型号差异');
|
|
await shot('known-identity-1280', row.locator('.model-identity-details')); await resize(390, 844); await shot('known-selection-390', row.locator('.mc-routing-decision'));
|
|
mark('Automatic known member, expanded selection provenance and requested/effective identity render on 1280/390');
|
|
const old = center().locator('.mc-attempts article').filter({ hasText: 'owned-historical-run' });
|
|
await old.locator('.model-identity-details summary').click(); await expect(old).toContainText('原记录未保存实际策略');
|
|
const current = (await host.api('/model-center')).attempts.find(value => value.id === historical.id); assert.deepEqual(current, historical);
|
|
await shot('historical-policy-390', old); mark('Historical strict-policy record stays unchanged and actual policy is explicitly unrecorded');
|
|
|
|
await resize(1280); await center().getByRole('button', { name: '模型连接', exact: true }).click();
|
|
const knownCard = center().locator('[data-connection-id="' + members.known.id + '"]');
|
|
await knownCard.getByRole('button', { name: '停用' + members.known.name, exact: true }).click(); await expect(knownCard).toContainText('已停用'); await closeDialog();
|
|
session = await request('fallback'); assert.equal(session.status, 'completed'); assert.equal(requests.length, 2); assert.equal(requests[1].body.model, members.unknown.model);
|
|
await ledger(); row = center().locator('.mc-attempts article').filter({ has: page.locator('strong').filter({ hasText: /^owned-unknown$/ }) });
|
|
await row.locator('.mc-routing-decision summary').click(); await expect(row).toContainText('未知能力后备尝试(未验证)'); await expect(row).toContainText('当时不可用');
|
|
await shot('fallback-1280', row); mark('UI disabling the known member yields an explicitly unverified fallback and preserves actual requests');
|
|
await closeDialog(); await host.api('/model-center/select', { type: 'pool', id: negativePool.id });
|
|
session = await request('negative'); assert.equal(session.status, 'failed'); assert.equal(requests.length, 2);
|
|
await ledger(); row = center().locator('.mc-attempts article').filter({ hasText: '没有适配本次请求' });
|
|
await row.locator('.mc-routing-decision summary').click(); await expect(row).toContainText('未产生请求用量'); await expect(row).toContainText('没有可用适配成员');
|
|
await shot('all-negative-1280', row); mark('All-negative automatic pool displays local no-dispatch and zero invented usage with manual recovery');
|
|
|
|
await center().getByRole('button', { name: '模型连接', exact: true }).click();
|
|
const negativeCard = center().locator('[data-connection-id="' + members.negative.id + '"]');
|
|
await negativeCard.getByRole('button', { name: '用于新任务', exact: true }).click();
|
|
await until(async () => (await host.api('/model-center')).selected.id === members.negative.id); await closeDialog(); upstreamError = true;
|
|
session = await request('manual-error'); assert.equal(session.status, 'failed'); assert.equal(requests.length, 3); assert.equal(requests[2].body.model, members.negative.model);
|
|
await ledger(); row = center().locator('.mc-attempts article').filter({ has: page.locator('strong').filter({ hasText: /^owned-negative$/ }) });
|
|
await expect(row).toContainText('显式选择:按原配置透传'); await expect(row).toContainText('自有上游原错:参数不被接受'); await expect(row).toContainText('用量未知');
|
|
await resize(390, 844); await shot('manual-upstream-error-390', row); mark('Manual override actually sends the negative member once and shows original HTTP400/unknown usage');
|
|
await resize(1280); await center().getByRole('button', { name: '模型连接', exact: true }).click();
|
|
await negativeCard.getByRole('button', { name: '编辑', exact: true }).click();
|
|
const edit = page.getByRole('form', { name: '编辑模型连接', exact: true }); await expect(edit).toContainText('记录型号差异后继续');
|
|
await edit.getByLabel('型号核对', { exact: true }).selectOption('require-match'); await edit.getByRole('button', { name: '保存模型连接', exact: true }).click();
|
|
await until(async () => (await host.api('/model-center')).connections.find(value => value.id === members.negative.id).modelIdentityPolicy === 'require-match');
|
|
await closeDialog(); mark('Connection editor saves requested strict policy and explains actual open behavior');
|
|
|
|
for (const mode of ['ask', 'auto', 'full']) {
|
|
await permission(mode); await openWorkbenchTool(page, '项目MCP工具');
|
|
const panel = page.getByRole('dialog', { name: '项目 MCP 工具', exact: true });
|
|
await expect(panel.locator('.mcp-status-bar')).toContainText(mode === 'full' ? '完全访问权限 · 直接执行' : '请求批准');
|
|
await expect(panel).not.toContainText('调用逐次审批'); await expect(panel).not.toContainText('每次调用仍需单独确认');
|
|
const registrationRow = panel.locator('.mcp-registration').filter({ hasText: '自有外部工具' });
|
|
await registrationRow.getByRole('button', { name: '保存并启用所选工具', exact: true }).click();
|
|
await expect(panel.locator('.mcp-notice')).toContainText(mode === 'full' ? '直接执行' : '请求批准');
|
|
await resize(mode === 'full' ? 390 : 1280, mode === 'full' ? 844 : 960); await shot('mcp-' + mode, panel.locator('.mcp-heading'));
|
|
await closeDialog();
|
|
}
|
|
await page.reload(); await expect(page.getByLabel('操作权限', { exact: true })).toHaveValue('full');
|
|
await openWorkbenchTool(page, '项目MCP工具'); await expect(page.getByRole('dialog').locator('.mcp-status-bar')).toContainText('直接执行');
|
|
await shot('mcp-full-reload-390', page.locator('.mcp-heading'));
|
|
mark('Actual permission selector persists ask/auto/full; MCP header/badge/save notice and reload match each mode on web/Electron');
|
|
assert.equal(requests.length, 3); assert.deepEqual(errors, []); assert.deepEqual(externalBlocked, []);
|
|
const final = await host.api('/model-center'); assert.deepEqual(final.attempts.find(value => value.id === historical.id), historical);
|
|
fs.writeFileSync(path.join(directory, 'final-model-center.json'), JSON.stringify(final, null, 2));
|
|
result = { success: true, appRoot, actualElectron: desktop, checks, screenshots, errors, externalBlocked, hostUrl: host.url,
|
|
syntheticRequests: requests.length, realModelRequests: 0, realCredentialsRead: 0, productionWrites: 0, viewportWidths: [1280, 390] };
|
|
} catch (cause) {
|
|
if (page) { await page.screenshot({ path: path.join(directory, 'failure.png') }).catch(() => {}); fs.writeFileSync(path.join(directory, 'failure.txt'), await page.locator('body').innerText().catch(() => '')); }
|
|
result = { success: false, appRoot, actualElectron: desktop, checks, screenshots, errors, externalBlocked, error: cause.stack, syntheticRequests: requests.length, realModelRequests: 0 };
|
|
} finally {
|
|
fs.writeFileSync(path.join(directory, 'requests.json'), JSON.stringify(requests, null, 2));
|
|
if (application) await application.close(); if (browser) await browser.close();
|
|
if (controller && await controller.health()) await controller.stop();
|
|
await host?.close?.(); await new Promise(resolve => { model.closeAllConnections(); model.close(resolve); });
|
|
for (const dispose of disposers.reverse()) await dispose();
|
|
if (desktop) { fs.writeFileSync(path.join(directory, 'desktop-stdout.txt'), desktopStdout); fs.writeFileSync(path.join(directory, 'desktop-stderr.txt'), desktopStderr); }
|
|
fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify(result, null, 2));
|
|
}
|
|
console.log(JSON.stringify({ directory, ...result })); if (!result.success) process.exitCode = 1;
|