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

89 lines
7.8 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';
const args = process.argv.slice(2);
assert.ok(args.length === 0 || args.length === 2 && args[0] === '--app-root' && path.isAbsolute(args[1]));
const appRoot = args[1] ?? process.cwd();
const directory = path.resolve('.qa', 'model-wait-ui-' + randomUUID()); fs.mkdirSync(directory, { recursive: true });
const disposers = [], t = { after: action => disposers.push(action) }, f = fixture(t), requests = [], sockets = new Set();
const model = http.createServer(async (request, response) => {
const pieces = []; for await (const piece of request) pieces.push(piece); requests.push(JSON.parse(Buffer.concat(pieces)));
response.writeHead(200, { 'Content-Type': 'text/event-stream' });
response.write('data: ' + JSON.stringify({ choices: [{ index: 0, delta: { tool_calls: [{ index: 0, id: 'owned-incomplete', type: 'function', function: { name: 'propose_file', arguments: '{"path":"incomplete.html",' } }] }, finish_reason: null }] }) + '\n\n');
});
model.on('connection', socket => { sockets.add(socket); socket.once('close', () => sockets.delete(socket)); });
await new Promise(resolve => model.listen(0, '127.0.0.1', resolve));
const provider = new Provider(), hub = provider.modelHub(f.store);
const member = hub.upsert({ name: '本地受控流', model: 'owned-wait-model', baseUrl: 'http://127.0.0.1:' + model.address().port + '/v1', requiresKey: false, streaming: true, capabilityDeclarations: { streaming: 'supported' } });
const pool = hub.pool({ name: '保持当前模型池', members: [{ connectionId: member.id }] }); hub.select({ type: 'pool', id: pool.id }); f.store.close();
const host = await launchWorkbench(t, f.data, {}, { appRoot, nodePath: fs.existsSync(path.join(appRoot, 'runtime/node.exe')) ? path.join(appRoot, 'runtime/node.exe') : process.execPath });
const checks = [], screenshots = [], pageErrors = [];
let browser, page;
const mark = text => { checks.push(text); console.log('PASS ' + text); };
const capture = async name => {
const file = path.join(directory, name + '.png'); await page.screenshot({ path: file });
fs.writeFileSync(path.join(directory, name + '.html'), await page.content());
screenshots.push({ path: file, sha256: createHash('sha256').update(fs.readFileSync(file)).digest('hex') });
};
const openWaits = async () => {
await openWorkbenchTool(page, '打开模型中心');
await page.getByRole('button', { name: '等待时限', exact: true }).click();
await expect(page.getByRole('form', { name: '模型等待时限' })).toBeVisible();
};
try {
browser = await chromium.launch({ channel: 'msedge', headless: true });
page = await browser.newPage({ viewport: { width: 1440, height: 1000 } }); page.on('pageerror', error => pageErrors.push(error.message));
await page.goto(host.url); await page.getByLabel('当前项目').selectOption(f.project.id); await openWaits();
await expect(page.getByLabel('单次模型响应上限(秒)', { exact: true })).toHaveValue('300');
await expect(page.getByLabel('流式无数据上限(秒)', { exact: true })).toHaveValue('120');
await page.getByLabel('单次模型响应上限(秒)', { exact: true }).fill('480');
await page.getByLabel('流式无数据上限(秒)', { exact: true }).fill('180');
await page.getByRole('button', { name: '保存等待时限', exact: true }).click();
await expect(page.getByRole('status')).toContainText('等待时限已保存');
let center = await host.api('/model-center'); assert.equal(center.selected.id, pool.id);
assert.deepEqual(center.waitSettings, { modelResponseTimeoutMs: 480000, modelStreamIdleTimeoutMs: 180000 });
assert.equal(center.connections.find(value => value.id === member.id).revision, member.revision); assert.equal(requests.length, 0);
await capture('settings-desktop'); mark('desktop saves seconds, preserves original pool/member and sends no model request');
await assert.rejects(host.api('/model-center/wait-settings', { modelStreamIdleTimeoutMs: 0 }), error => error.status === 400);
assert.deepEqual((await host.api('/model-center')).waitSettings, center.waitSettings); mark('invalid timeout rejected without saving');
await page.reload(); await openWaits();
await expect(page.getByLabel('单次模型响应上限(秒)', { exact: true })).toHaveValue('480');
await page.setViewportSize({ width: 390, height: 844 });
const narrowSave = page.getByRole('button', { name: '保存等待时限', exact: true });
await narrowSave.scrollIntoViewIfNeeded(); await expect(narrowSave).toBeVisible();
const saveBounds = await narrowSave.boundingBox();
assert.ok(saveBounds && saveBounds.x >= 0 && saveBounds.y >= 0 && saveBounds.x + saveBounds.width <= 390 && saveBounds.y + saveBounds.height <= 844, 'Full narrow save button is reachable within viewport');
assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth)); await capture('settings-390');
mark('saved values survive reload; narrow screen remains usable');
await page.keyboard.press('Escape'); await page.setViewportSize({ width: 1440, height: 1000 });
await host.api('/model-center/wait-settings', { modelResponseTimeoutMs: 15000, modelStreamIdleTimeoutMs: 5000 });
await page.reload();
await page.getByLabel('任务内容').fill('读取当前项目,受控超时验收'); await page.getByRole('button', { name: '发送任务', exact: true }).click();
let sessionId; await until(async () => { sessionId = (await host.api('/sessions')).find(value => value.title === '读取当前项目,受控超时验收')?.id; return !!sessionId && requests.length === 1; });
await assert.rejects(host.api('/model-center/wait-settings', { modelResponseTimeoutMs: 60000 }), error => error.status === 409);
await openWaits(); await expect(page.getByRole('button', { name: '保存等待时限', exact: true })).toBeDisabled();
await page.keyboard.press('Escape'); mark('active request locks wait changes in both API and UI');
await until(async () => (await host.api('/sessions/' + sessionId)).status === 'failed', 20000);
const session = await host.api('/sessions/' + sessionId); assert.equal(session.failure.timeout.kind, 'idle');
assert.equal(session.failure.timeout.limitMs, 5000); assert.equal(session.changes.length, 0); assert.equal(requests.length, 1);
assert.equal(session.execution.run.usage.unknownRequests, 1); assert.equal(session.execution.run.model.modelResponseTimeoutMs, 15000);
await expect(page.getByText(/流式响应连续 5 秒未收到新数据/).first()).toBeVisible();
await capture('idle-error-desktop'); await page.setViewportSize({ width: 390, height: 844 }); await capture('idle-error-390');
assert.equal(pageErrors.length, 0); mark('actual stalled stream shows exact idle reason, saves unknown use, never writes or retries');
fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify({ success: true, checks, screenshots, pageErrors, requests: requests.length, realModelRequests: 0, appRoot }, null, 2));
console.log(JSON.stringify({ success: true, directory, checks: checks.length, screenshots: screenshots.length }));
} catch (error) {
if (page) await capture('failure').catch(() => {});
fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify({ success: false, error: error.stack, checks, screenshots, pageErrors, requests: requests.length, appRoot }, null, 2)); throw error;
} finally {
await browser?.close(); for (const socket of sockets) socket.destroy(); await new Promise(resolve => model.close(resolve));
for (const action of disposers.reverse()) await action();
}