Files
Agent-Workbench/scripts/verify-reminders-ui.mjs
T

123 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 { createHash, randomUUID } from 'node:crypto';
import { chromium, _electron } from 'playwright';
import { fixture, compatibleFixture, launchWorkbench, until, call, completion } from '../tests/helpers.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';
const actualElectron = process.argv.includes('--desktop'), appRoot = path.resolve(process.env.AGENT_WORKBENCH_QA_APP_ROOT || '.');
const directory = path.resolve(process.env.REMINDER_QA_OUTPUT || '.qa/reminder-readback-d', (actualElectron ? 'electron-' : 'web-') + randomUUID());
fs.mkdirSync(directory, { recursive: true });
const disposers = [], t = { after: fn => disposers.push(fn) }, f = fixture(t, { modernDefaults: true });
const passed = [], errors = [], screenshots = [], marker = '工作台可选进展建议';
let n = 0, browser, application, controller, host, page, window, result;
const model = await compatibleFixture(t, () => {
const step = ++n;
return { body: step < 4 ? completion([call('repeat-' + step, 'read_file', { path: 'README.md', offset: 0, limit: 10 })])
: step === 4 ? completion([call('ack', 'read_file', { path: 'README.md', offset: 0, limit: 10 })], '明白。')
: step === 5 ? completion([call('new-page', 'read_file', { path: 'README.md', offset: 10, limit: 10 })])
: completion([], '已核对读取过程,未修改文件。') };
});
f.session.title = 'D 提醒与回读本机验收'; f.store.save(f.session);
f.store.saveConfig({ mode: 'compatible', model: 'owned-reminder-ui', baseUrl: model.baseUrl, approvalMode: 'full' }); f.store.close();
const mark = name => { passed.push(name); console.log('PASS ' + name); };
const save = (name, value) => fs.writeFileSync(path.join(directory, name), JSON.stringify(value, null, 2));
async function shot(name) { const file = path.join(directory, name + '.png'); await page.screenshot({ path: file, scale: 'css' }); screenshots.push(file); }
async function settings() {
await page.getByRole('button', { name: '打开工具箱', exact: true }).click();
await page.getByLabel('搜索工作台工具').fill('进展提醒'); await page.getByRole('button', { name: '进展提醒', exact: true }).click();
const modal = page.getByRole('dialog', { name: '进展提醒', exact: true });
await modal.getByRole('switch', { name: '开启进展提醒', exact: true }).waitFor(); return modal;
}
async function visiblePanel() {
if (!await page.getByLabel('选择运行记录', { exact: true }).isVisible()) await page.getByRole('button', { name: '查看任务记录', exact: true }).click();
const observation = page.getByLabel('单轮观察', { exact: true }); await observation.waitFor();
if (!await observation.evaluate(element => element.open)) await observation.locator('summary').click();
const panel = page.getByLabel('提醒与回读', { exact: true }); await panel.waitFor(); await panel.scrollIntoViewIfNeeded(); return panel;
}
async function resize(width, height) {
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 });
}
try {
if (actualElectron) {
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, bootstrap: await controller.api('/bootstrap'), api: (route, input) => controller.api(route, input === undefined ? {} : { method: 'POST', input }) };
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), '--hidden-window'],
cwd: appRoot, env: projectEnvironment(), timeout: 60000 });
page = await application.firstWindow(); window = await application.browserWindow(page);
await window.evaluate(win => { win.showInactive(); win.setContentSize(1440, 1080); }); await until(() => page.evaluate(() => innerWidth === 1440));
assert.equal(await page.evaluate(() => typeof globalThis.process), 'undefined');
} 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({ viewport: { width: 1440, height: 1080 } }); await page.goto(host.url);
}
page.on('pageerror', error => errors.push(error.message)); await page.locator('.connection.connected').waitFor();
assert.equal(host.bootstrap.config.hasKey, false); assert.equal(host.bootstrap.config.progressRemindersEnabled, false);
await page.getByLabel('当前项目').selectOption(f.project.id); await page.locator('.session-list button').filter({ hasText: f.session.title }).click();
let modal = await settings(); const toggle = () => modal.getByRole('switch', { name: '开启进展提醒', exact: true });
assert.equal(await toggle().isChecked(), false); await shot('default-off'); await toggle().click();
await until(async () => (await host.api('/progress-reminders/settings')).enabled === true);
await until(async () => (await modal.textContent()).includes('已开启')); assert.equal(n, 0);
await modal.getByRole('button', { name: '关闭窗口', exact: true }).click(); await page.reload(); await page.locator('.connection.connected').waitFor();
modal = await settings(); assert.equal(await toggle().isChecked(), true); mark('toolbox search opens a default-off setting; real save and refresh restore the preference without model calls');
const failSave = route => route.request().method() === 'POST' ? route.fulfill({ status: 503, contentType: 'application/json', body: JSON.stringify({ error: 'Owned settings failure' }) }) : route.continue();
await page.route('**/api/progress-reminders/settings', failSave); await toggle().click();
await modal.getByRole('alert').waitFor(); assert.equal(await toggle().isChecked(), true); await shot('save-error');
await page.unroute('**/api/progress-reminders/settings', failSave); assert.equal((await host.api('/progress-reminders/settings')).enabled, true);
await modal.getByRole('button', { name: '关闭窗口', exact: true }).click(); mark('a failed save preserves the real setting and shows a recoverable error');
await host.api('/sessions/' + f.session.id + '/messages', { content: '修改README.md,先读取核对原内容;无需测试。本机固定HTTP验收。' });
await until(async () => (await host.api('/sessions/' + f.session.id)).status === 'completed', 30000);
const snapshot = await host.api('/sessions/' + f.session.id + '/export'); save('session.json', snapshot);
const run = snapshot.execution.run, inspected = await host.api('/sessions/' + f.session.id + '/progress-reminders?run=' + run.id); save('readback.json', inspected);
assert.equal(n, 6); assert.equal(inspected.evidenceVerified, true); assert.equal(inspected.items[0].status, 'resolved');
assert.deepEqual(model.requests.map(row => JSON.stringify(row.input).includes(marker)), [false, false, false, true, false, false]);
let panel = await visiblePanel(); await until(async () => (await panel.textContent()).includes('已观察到进展'));
assert.match(await panel.textContent(), /已随 1 次请求发出/); assert.match(await panel.textContent(), /因果关系未证实/); await shot('observed-readback');
mark('six original HTTP requests show one dispatch, ignore acknowledgement and resolve only on the later new range');
for (const name of ['原观察 1', '实际请求 1', '回读依据']) {
await panel.getByRole('button', { name, exact: true }).click();
const artifact = page.getByRole('dialog', { name: '执行结果工件', exact: true }); await artifact.waitFor();
assert.ok((await artifact.textContent()).length > 100); await artifact.getByRole('button', { name: '关闭窗口', exact: true }).click();
}
mark('observation, actual request and readback evidence open their real artifact viewers');
await resize(390, 844); panel = await visiblePanel();
assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1), true);
assert.ok((await panel.getByRole('button', { name: '实际请求 1', exact: true }).boundingBox()).height >= 44); await shot('narrow-readback');
mark('390px keeps readback facts and source actions reachable without page overflow');
await resize(1440, 1080);
let failReadback = true;
const readbackFailure = route => failReadback ? route.fulfill({ status: 503, contentType: 'application/json', body: JSON.stringify({ error: 'Owned readback failure' }) }) : route.continue();
await page.route('**/api/sessions/*/progress-reminders?*', readbackFailure); await page.reload(); await page.locator('.connection.connected').waitFor(); panel = await visiblePanel();
await panel.getByRole('alert').waitFor(); assert.match(await panel.textContent(), /暂时无法核对/); await shot('readback-error');
failReadback = false; await panel.getByRole('button', { name: '重新核对', exact: true }).click();
await until(async () => (await panel.textContent()).includes('已观察到进展')); await page.unroute('**/api/sessions/*/progress-reminders?*', readbackFailure);
mark('readback failure and retry recover from real API responses without changing the run or calling the model');
modal = await settings(); await toggle().click(); await until(async () => (await host.api('/progress-reminders/settings')).enabled === false);
await modal.getByRole('button', { name: '关闭窗口', exact: true }).click();
if (application) { await application.close(); application = null; if (controller && await controller.health()) await controller.stop(); controller = null; }
else { await browser.close(); browser = null; await host.close(); }
const restarted = await launchWorkbench(t, f.data, {}, { appRoot });
assert.equal((await restarted.api('/progress-reminders/settings')).enabled, false);
assert.equal((await restarted.api('/sessions/' + f.session.id + '/progress-reminders?run=' + run.id)).evidenceVerified, true); await restarted.close();
assert.equal(n, 6); assert.deepEqual(errors, []); mark('closing and restarting the actual host preserves off preference and original verified readback');
result = { success: true, appRoot, actualElectron, passed, screenshots, errors, realModelRequests: 0, localFixtureRequests: n, officialServiceTouched: false,
serverSha256: createHash('sha256').update(fs.readFileSync(path.join(appRoot, 'dist-server/server.mjs'))).digest('hex') };
} catch (error) {
if (page) try { await shot('failure'); fs.writeFileSync(path.join(directory, 'failure-dom.txt'), await page.locator('body').innerText()); } catch {}
result = { success: false, appRoot, actualElectron, passed, screenshots, errors, error: error.stack, realModelRequests: 0, localFixtureRequests: n, officialServiceTouched: false };
} finally {
save('fixture-requests.json', model.requests.map(({ url, input }) => ({ url, input })));
if (application) await application.close(); if (browser) await browser.close(); if (controller && await controller.health()) await controller.stop();
for (const dispose of disposers.reverse()) await dispose(); save('result.json', result);
}
console.log(JSON.stringify({ directory, ...result }, null, 2)); if (!result.success) process.exitCode = 1;