105 lines
12 KiB
JavaScript
105 lines
12 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, _electron } from 'playwright';
|
|
import { expect } from 'playwright/test';
|
|
import { fixture, compatibleFixture, completion, call, launchWorkbench, until } from '../tests/helpers.mjs';
|
|
import { Provider } from '../src/server/provider.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-model-capacity-ui.mjs [--desktop] — isolated actual capacity/history save, upstream adoption, stale-edit recovery and reported-cache UI. Optional AGENT_WORKBENCH_QA_APP_ROOT selects the immutable app. No real-model keys.'); 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/user-project-errors-20261106', (desktop ? 'capacity-electron-' : 'capacity-web-') + randomUUID());
|
|
fs.mkdirSync(directory, { recursive: true }); console.log('EVIDENCE ' + directory);
|
|
const disposers = [], t = { after: fn => disposers.push(fn) }, f = fixture(t), checks = [], screenshots = [], errors = [];
|
|
let host, controller, browser, application, page, window, result;
|
|
const dialog = () => page.getByRole('dialog', { name: '模型中心', exact: true });
|
|
const mark = name => { checks.push(name); console.log('PASS ' + name); };
|
|
async function open() { if (!await dialog().count()) await page.getByRole('button', { name: '打开模型中心', exact: true }).click(); }
|
|
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 });
|
|
const compactControl = page.locator('.task-toolbar').getByRole('button', { name: '查看文件', exact: true });
|
|
if (width <= 940) await expect(compactControl).toBeVisible(); else await expect(compactControl).toHaveCount(0);
|
|
}
|
|
async function shot(name) {
|
|
const file = path.join(directory, name + '.png'); await page.screenshot({ path: file, fullPage: true }); screenshots.push(file);
|
|
fs.writeFileSync(path.join(directory, name + '.txt'), await page.locator('body').innerText());
|
|
}
|
|
try {
|
|
const model = await compatibleFixture(t, (url, body) => url.endsWith('/models') ? { data: [{ id: 'owned-capacity', context_length: 131072, max_output_tokens: 32768, capabilities: { text: true, tools: true } }] }
|
|
: { ...completion(body.messages.some(row => row.role === 'tool') ? [] : [call('owned-list', 'list_files', { path: '' })], 'Owned reference answer.'),
|
|
usage: { prompt_tokens: 1000, completion_tokens: 20, prompt_tokens_details: { cached_tokens: 700 } } });
|
|
const hub = new Provider().modelHub(f.store), source = hub.upsert({ name: 'Owned source', model: 'source', baseUrl: model.baseUrl, requiresKey: false });
|
|
const preview = await hub.modelCatalog().fetch({ id: source.id, revision: source.revision });
|
|
const applied = hub.modelCatalog().apply({ previewId: preview.id, models: ['owned-capacity'], routing: 'manual', defaultModel: 'owned-capacity' });
|
|
const connection = hub.upsert({ ...hub.connection(applied.added[0]), contextLimit: 16000, maxOutputTokens: 2048 });
|
|
assert.equal(connection.upstreamModel.maxOutputTokens, 32768); f.store.close();
|
|
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 ? { method: 'POST', input } : {}), bootstrap: await controller.api('/bootstrap') };
|
|
const executable = desktopExecutableIdentity(appRoot).path;
|
|
application = await _electron.launch({ executablePath: executable, args: [...(executable.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 application.firstWindow(); window = await application.browserWindow(page); await page.waitForLoadState();
|
|
} 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)); await page.waitForLoadState('networkidle'); await resize(1440); await open();
|
|
await dialog().getByRole('button', { name: '运行预算', exact: true }).click();
|
|
const budgets = () => dialog().getByRole('form', { name: '运行预算设置', exact: true });
|
|
await expect(budgets().getByLabel('容量策略', { exact: true })).toHaveValue('manual');
|
|
await budgets().getByLabel('容量策略', { exact: true }).selectOption('model'); await budgets().getByLabel('历史传递', { exact: true }).selectOption('preserve');
|
|
await expect(budgets().getByLabel('输出上限', { exact: true })).toHaveCount(0);
|
|
await budgets().getByLabel('容量策略', { exact: true }).selectOption('manual'); await expect(budgets().getByLabel('输出上限', { exact: true })).toHaveValue('2048');
|
|
await budgets().getByLabel('容量策略', { exact: true }).selectOption('model');
|
|
await budgets().getByRole('button', { name: '保存运行预算', exact: true }).click(); await expect(dialog().getByRole('status')).toContainText('运行预算已保存');
|
|
await page.reload(); await open(); await dialog().getByRole('button', { name: '运行预算', exact: true }).click();
|
|
await expect(budgets().getByLabel('容量策略', { exact: true })).toHaveValue('model'); await expect(budgets().getByLabel('历史传递', { exact: true })).toHaveValue('preserve');
|
|
await shot('capacity-history-1440'); mark('capacity and complete-history choices save and survive reload without model or credential changes');
|
|
await dialog().getByRole('button', { name: '模型连接', exact: true }).click();
|
|
const card = () => dialog().locator('.mc-connection').filter({ has: page.getByRole('heading', { name: connection.name, exact: true }) });
|
|
await card().getByRole('button', { name: '编辑', exact: true }).click();
|
|
const editor = () => dialog().getByRole('form', { name: '编辑模型连接', exact: true });
|
|
await expect(editor().getByLabel('输出上限', { exact: true })).toHaveValue('2048');
|
|
await editor().getByRole('button', { name: '采用上游容量', exact: true }).click(); await expect(editor().getByLabel('输出上限', { exact: true })).toHaveValue('32768');
|
|
const center = await host.api('/model-center'), current = center.connections.find(row => row.id === connection.id);
|
|
await host.api('/model-center/connections', { ...current, maxOutputTokens: 8192 });
|
|
await editor().getByRole('button', { name: '保存模型连接', exact: true }).click(); await expect(dialog().getByRole('status')).toContainText('刷新');
|
|
await expect(editor().getByLabel('输出上限', { exact: true })).toHaveValue('32768'); await shot('stale-edit-retained');
|
|
assert.equal((await host.api('/model-center')).connections.find(row => row.id === connection.id).maxOutputTokens, 8192);
|
|
await editor().getByRole('button', { name: '取消', exact: true }).click(); await page.reload(); await open();
|
|
await card().getByRole('button', { name: '编辑', exact: true }).click(); await editor().getByRole('button', { name: '采用上游容量', exact: true }).click();
|
|
await editor().getByRole('button', { name: '保存模型连接', exact: true }).click(); await expect(editor()).toHaveCount(0);
|
|
assert.equal((await host.api('/model-center')).connections.find(row => row.id === connection.id).maxOutputTokens, 32768);
|
|
await page.reload(); await open(); await card().getByRole('button', { name: '编辑', exact: true }).click(); await expect(editor().getByLabel('输出上限', { exact: true })).toHaveValue('32768');
|
|
for (const width of [1440, 390]) { await resize(width); await editor().getByLabel('输出上限', { exact: true }).scrollIntoViewIfNeeded(); assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1)); await shot('upstream-capacity-' + width); }
|
|
mark('old 2048 stays visible until upstream adoption; 32768 saves and persists; stale revision rejects while retaining the edit; both viewport widths fit');
|
|
await dialog().getByRole('button', { name: '关闭窗口', exact: true }).click();
|
|
await host.api('/sessions/' + f.session.id + '/messages', { content: 'Inspect the directory once and answer only. Do not modify files or run checks.' });
|
|
let state; await until(async () => { state = await host.api('/sessions/' + f.session.id); return !['running', 'awaiting_approval', 'stopping'].includes(state.status); }, 20000);
|
|
assert.equal(state.status, 'completed'); assert.equal(model.requests.filter(row => row.input).length, 2);
|
|
assert.ok(model.requests.filter(row => row.input).every(row => row.input.max_tokens === 32768));
|
|
await page.reload();
|
|
const project = page.getByLabel('当前项目', { exact: true }); if (!await project.isVisible()) await page.getByRole('button', { name: '展开项目栏', exact: true }).click(); await project.selectOption(f.project.id);
|
|
await page.getByRole('button', { name: '查看任务记录', exact: true }).click();
|
|
const context = page.getByLabel('请求上下文', { exact: true }); await expect(context).toContainText('700 tokens'); await expect(context).toContainText('共同前缀'); await expect(context).toContainText('不等于上游命中');
|
|
for (const width of [1440, 390]) { await resize(width); if (!await context.isVisible()) await page.getByRole('button', { name: '查看任务记录', exact: true }).click(); await expect(context).toBeVisible(); await context.scrollIntoViewIfNeeded(); assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1)); await shot('reported-cache-' + width); }
|
|
assert.deepEqual(errors, []); fs.writeFileSync(path.join(directory, 'session.json'), JSON.stringify(state, null, 2));
|
|
mark('actual owned HTTP sends 32768; reported 700 cached tokens and prefix bytes remain separate and persist in the request record');
|
|
result = { success: true, version: host.bootstrap.version, appRoot, actualElectron: desktop, checks, screenshots, errors, realModelRequests: 0, ownedModelRequests: 2, directoryGets: 1, officialServiceTouched: false };
|
|
} catch (error) { if (page) await shot('failure').catch(() => {}); if (host) fs.writeFileSync(path.join(directory, 'failed-session.json'), JSON.stringify(await host.api('/sessions/' + f.session.id).catch(cause => ({ error: cause.message })), null, 2)); result = { success: false, appRoot, actualElectron: desktop, checks, screenshots, errors, error: error.stack, realModelRequests: 0 }; }
|
|
finally {
|
|
if (application) await application.close(); if (browser) await browser.close(); if (controller && await controller.health()) await controller.stop();
|
|
await host?.close?.(); 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;
|