import fs from 'node:fs'; import path from 'node:path'; import assert from 'node:assert/strict'; import { randomUUID } from 'node:crypto'; import { chromium, _electron } from 'playwright'; import { expect } from 'playwright/test'; 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'; import { openDefaults } from '../src/shared/contracts/execution-policy.mjs'; if (process.argv.includes('--help')) { console.log('node scripts/verify-open-formal-ui.mjs [--desktop] — read-only formal installed UI inspection; no model requests, permission edits or user project execution.'); process.exit(0); } assert.ok(process.argv.slice(2).every(arg => arg === '--desktop')); const desktop = process.argv.includes('--desktop'), url = 'http://127.0.0.1:4318'; const root = path.resolve('.qa/unrestricted-1.25.5', (desktop ? 'formal-electron-' : 'formal-web-') + randomUUID()); fs.mkdirSync(root, { recursive: true }); const boot = await (await fetch(url + '/api/bootstrap', { headers: { Connection: 'close' } })).json(); assert.equal(boot.version, '1.25.5'); assert.equal(boot.config.approvalMode, 'full'); for (const [key, value] of Object.entries(openDefaults)) assert.equal(boot.config[key], value, key); const host = await (await fetch(url + '/api/host/status', { headers: { 'X-Workbench-Token': boot.token, Connection: 'close' } })).json(); assert.equal(host.activity.active, false); let browser, app, page, window, result; const screenshots = [], errors = []; try { if (desktop) { const controller = new HostController({ appRoot: boot.runtimeRoot, dataRoot: host.identity.dataRoot, port: 4318, timeoutMs: 60000 }); const connection = await controller.connect(); assert.equal(connection.identity.pid, host.identity.pid); const executablePath = desktopExecutableIdentity(boot.runtimeRoot).path; app = await _electron.launch({ executablePath, args: ['--app-root', boot.runtimeRoot, '--data-root', host.identity.dataRoot, '--port', '4318'], env: projectEnvironment({ AGENT_WORKBENCH_QA: '1' }) }); page = await app.firstWindow(); window = await app.browserWindow(page); } else { browser = await chromium.launch({ channel: 'msedge', headless: true }); page = await browser.newPage(); await page.goto(url); } page.on('pageerror', error => errors.push(error.message)); await page.waitForLoadState('networkidle'); for (const width of [1440, 390]) { if (window) { await window.evaluate((value, size) => value.setContentSize(size.width, size.height), { width, height: 1080 }); await expect.poll(() => page.evaluate(() => innerWidth)).toBe(width); } else await page.setViewportSize({ width, height: 1080 }); await expect(page.getByLabel('操作权限', { exact: true })).toHaveValue('full'); assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1)); const file = path.join(root, 'permission-' + width + '.png'); await page.screenshot({ path: file, fullPage: true }); screenshots.push(file); } await page.getByRole('button', { name: '打开模型中心', exact: true }).click(); const dialog = page.getByRole('dialog', { name: '模型中心', exact: true }); await dialog.getByRole('button', { name: '运行预算', exact: true }).click(); await expect(dialog.getByLabel('额度策略', { exact: true })).toHaveValue('unrestricted'); await expect(dialog.getByLabel('输出上限', { exact: true })).toHaveCount(0); const file = path.join(root, 'open-budget-390.png'); await page.screenshot({ path: file, fullPage: true }); screenshots.push(file); await dialog.getByRole('button', { name: '关闭窗口', exact: true }).click(); assert.deepEqual(errors, []); result = { success: true, version: boot.version, pid: host.identity.pid, runtimeRoot: boot.runtimeRoot, desktop, screenshots, errors, realModelRequests: 0, userProjectsExecuted: 0, writes: 0 }; } catch (error) { if (page) await page.screenshot({ path: path.join(root, 'failure.png'), fullPage: true }).catch(() => {}); result = { success: false, error: error.stack, desktop, screenshots, errors }; } finally { await app?.close(); await browser?.close(); fs.writeFileSync(path.join(root, 'result.json'), JSON.stringify(result, null, 2)); } console.log(JSON.stringify({ root, ...result })); if (!result.success) process.exitCode = 1;