Files
Agent-Workbench/scripts/verify-release-efficiency-ui.mjs
T
2026-11-02 09:50:08 +08:00

82 lines
11 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, createHash } from 'node:crypto';
import { chromium, _electron, expect } from 'playwright/test';
import { HostController } from '../src/core/desktop/host-controller.mjs';
import { desktopExecutableIdentity } from '../src/core/desktop/app-installation.mjs';
import { InstallationProgress } from '../src/core/desktop/installation-progress.mjs';
import { writeState } from '../src/core/desktop/file-state.mjs';
import { projectEnvironment } from '../src/core/process-environment.mjs';
import { REQUIRED_REPORTS } from './documentation-bundle.mjs';
if (process.argv.includes('--help')) { console.log('node scripts/verify-release-efficiency-ui.mjs — isolated actual Edge/Electron state UI using explicitly owned installation-progress fixtures; real installer acceptance is separate.'); process.exit(0); }
assert.equal(process.argv.length, 2);
const source = path.resolve(process.env.AGENT_WORKBENCH_QA_APP_ROOT || '.'), root = path.resolve('.qa', 'release-efficiency-ui-' + randomUUID());
const appRoot = path.join(root, '程序 含中文长路径', 'versions', 'owned-ui'), installRoot = path.dirname(path.dirname(appRoot)), dataRoot = path.join(root, 'data');
const version = JSON.parse(fs.readFileSync(path.join(source, fs.existsSync(path.join(source, 'MANIFEST.json')) ? 'MANIFEST.json' : 'package.json'))).version;
const results = [], screenshots = [], errors = [], hash = bytes => createHash('sha256').update(bytes).digest('hex');
fs.mkdirSync(appRoot, { recursive: true });
for (const file of ['dist-client', 'dist-server', 'dist-desktop', 'public', 'examples']) fs.cpSync(path.join(source, file), path.join(appRoot, file), { recursive: true });
for (const file of REQUIRED_REPORTS) { const target = path.join(appRoot, file); fs.mkdirSync(path.dirname(target), { recursive: true }); fs.copyFileSync(path.join(source, file), target); }
fs.writeFileSync(path.join(appRoot, 'package.json'), JSON.stringify({ version }));
const executable = desktopExecutableIdentity(source).path, packaged = path.basename(executable).toLowerCase() === 'agentworkbench.exe';
const electronRoot = path.join(appRoot, packaged ? 'desktop' : 'runtime/electron');
fs.cpSync(path.dirname(executable), electronRoot, { recursive: true });
const electron = path.join(electronRoot, path.basename(executable));
const server = net.createServer(); await new Promise(resolve => server.listen(0, '127.0.0.1', resolve)); const port = server.address().port; await new Promise(resolve => server.close(resolve));
const stateRoot = path.join(installRoot, '.installation'); fs.mkdirSync(stateRoot, { recursive: true });
// Explicit UI fixtures only; none of these records claims a real package install.
writeState(path.join(installRoot, 'current.json'), { schema: 1, app: 'agent-workbench-installation', installId: randomUUID(), revision: 1, dataRoot, port, active: { directory: 'owned-ui', version }, previous: null });
const progress = command => new InstallationProgress({ stateRoot, installRoot, dataRoot, command, expectedManifestSha256: 'a'.repeat(64) });
const controller = new HostController({ appRoot, dataRoot, port, nodePath: process.execPath });
let browser, application, page, stage = 'start';
const record = name => { results.push({ name, passed: true }); console.log('PASS ' + name); };
const capture = async name => { const file = path.join(root, name + '.png'); await page.screenshot({ path: file }); screenshots.push({ file, sha256: hash(fs.readFileSync(file)) }); };
const open = async () => { const version = page.getByRole('button', { name: '版本与更新', exact: true }); if (await page.evaluate(() => innerWidth <= 620) || !await version.isVisible()) await page.getByRole('button', { name: '展开项目栏', exact: true }).click(); await expect(version).toBeVisible(); await version.click(); };
const refresh = async () => { await page.getByRole('button', { name: '刷新状态', exact: true }).click(); };
const close = async () => { await page.getByRole('dialog').getByRole('button', { name: '关闭窗口', exact: true }).click(); };
const overflow = async () => assert.equal(await page.evaluate(() => document.documentElement.scrollWidth > innerWidth), false);
try {
const original = await controller.start(); browser = await chromium.launch({ channel: 'msedge', headless: true });
page = await browser.newPage({ viewport: { width: 1440, height: 960 } }); page.on('pageerror', error => errors.push(error.message));
await page.goto(controller.url); await page.locator('.connection.connected').waitFor(); await open();
await expect(page.getByText('还没有新版安装进度记录。', { exact: false })).toBeVisible(); record('actual authenticated status endpoint: registered version and legacy empty progress');
stage = 'original reports';
for (const [kind, file] of [['research', REQUIRED_REPORTS[0]], ['cursor', REQUIRED_REPORTS[1]], ['context', REQUIRED_REPORTS[2]], ['tools', REQUIRED_REPORTS[3]]]) {
// Follow the server's fixed public report names, not a substitute placeholder.
const name = ({ research: 'overview', cursor: 'cursor', context: 'context', tools: 'tools' })[kind];
const response = await controller.api('/research/' + name); const bytes = fs.readFileSync(path.join(appRoot, file));
assert.equal(response.content, bytes.toString('utf8')); assert.equal(response.sha256, hash(bytes));
}
record('four runtime report APIs return original source text and SHA');
stage = 'running progress'; const running = progress('preflight'); running.phase('verify-current');
running.count('verify-current', { scope: 'full-manifest', root: appRoot, completedFiles: 17, totalFiles: 100, completedBytes: 2048, totalBytes: 12000, currentPath: 'docs/中文长路径/当前核对的原文件.json' }); running.save();
await refresh(); await expect(page.getByText('17 / 100 文件', { exact: true })).toBeVisible();
await expect(page.getByRole('progressbar', { name: '当前核对范围' })).toHaveAttribute('value', '17'); await capture('web-running-wide'); await overflow(); record('actual persisted progress shows current range and real counts with no invented total percentage');
running.finish('ready-to-copy'); await page.reload(); await open(); await expect(page.getByText('预检已通过,写入进度由安装器显示;新版本尚未激活。')).toBeVisible(); record('reload restores original ready preparation without replaying it');
stage = 'failure and mobile'; const failed = progress('activate'); failed.finish('failed', { error: { code: 'OWNED_UI_FAILURE', message: '自有验收夹具:新候选原字节已变化,请保留原记录。' }, receiptFile: path.join(stateRoot, 'owned-receipt.json') });
await refresh(); await expect(page.getByText('OWNED_UI_FAILURE', { exact: true })).toBeVisible(); await capture('web-failure-wide');
await close(); await page.setViewportSize({ width: 390, height: 844 }); await open(); await expect(page.getByText('OWNED_UI_FAILURE', { exact: true })).toBeVisible(); await overflow(); await capture('web-failure-390'); record('390px Chinese paths, original failure and recovery guidance remain usable');
stage = 'unknown and offline'; const unknown = progress('activate'), value = JSON.parse(fs.readFileSync(unknown.file)); value.process.createdAt = 'owned-lost-process-fixture'; fs.writeFileSync(unknown.file, JSON.stringify(value));
await refresh(); await expect(page.getByText('原安装进程已结束,结果待核对', { exact: true })).toBeVisible();
await page.context().setOffline(true); await expect(page.getByRole('alert').filter({ hasText: '以下保留上次读取结果' })).toBeVisible({ timeout: 10000 });
await page.context().setOffline(false); await expect(page.getByRole('alert').filter({ hasText: '以下保留上次读取结果' })).toHaveCount(0, { timeout: 10000 }); await capture('web-unknown-390'); record('lost process is unknown; offline retains explicitly stale progress and reconnect refreshes');
await page.getByRole('combobox', { name: '查看安装操作' }).selectOption(failed.value.id); await expect(page.getByText('OWNED_UI_FAILURE', { exact: true })).toBeVisible();
await page.getByRole('combobox', { name: '查看安装操作' }).selectOption(''); await expect(page.getByText('原安装进程已结束,结果待核对', { exact: true })).toBeVisible();
stage = 'actual Electron'; await browser.close(); browser = null;
application = await _electron.launch({ executablePath: electron, args: [...(packaged ? [] : [path.join(appRoot, 'dist-desktop/main.cjs')]), '--app-root', appRoot, '--data-root', dataRoot, '--node', process.execPath, '--port', String(port), '--hidden-window'], cwd: appRoot, env: projectEnvironment(), timeout: 60000 });
page = await application.firstWindow(); page.on('pageerror', error => errors.push(error.message));
const native = await application.browserWindow(page); await native.evaluate(window => { window.showInactive(); window.setContentSize(1440, 960); });
await page.locator('.connection.connected').waitFor(); await open(); await expect(page.getByText('原安装进程已结束,结果待核对', { exact: true })).toBeVisible(); await capture('electron-unknown-wide'); await overflow();
await close(); await native.evaluate(window => window.setContentSize(390, 844)); await expect.poll(() => page.evaluate(() => innerWidth)).toBe(390); await open(); await expect(page.getByText('原安装进程已结束,结果待核对', { exact: true })).toBeVisible(); await overflow(); await capture('electron-unknown-390');
record('actual Electron wide and 390px display original update state; window close retains host');
await application.close(); application = null; assert.equal((await controller.health()).pid, original.identity.pid); assert.deepEqual(errors, []); stage = 'complete';
fs.writeFileSync(path.join(root, 'result.json'), JSON.stringify({ passed: true, success: true, version, source, root, stage, results, screenshots, fixtureScope: 'Owned progress fixtures through real authenticated API and actual Edge/Electron; installer process acceptance is separate.', modelRequests: 0 }, null, 2));
console.log('EVIDENCE ' + root); console.log(JSON.stringify({ passed: true, root, groups: results.length }));
} catch (error) {
if (page) await capture('original-failure').catch(() => {});
fs.writeFileSync(path.join(root, 'result.json'), JSON.stringify({ passed: false, version, source, root, stage, results, screenshots, error: error.stack }, null, 2)); console.error(JSON.stringify({ passed: false, root, stage, error: error.message })); process.exitCode = 1;
} finally { if (browser) await browser.close().catch(() => {}); if (application) await application.close().catch(() => {}); if (await controller.health()) await controller.stop(); }