import fs from 'node:fs'; import path from 'node:path'; import assert from 'node:assert/strict'; import { chromium, expect } from 'playwright/test'; import { _electron } from 'playwright'; import { HostController } from '../src/core/desktop/host-controller.mjs'; import { projectEnvironment } from '../src/core/process-environment.mjs'; import { publicSnapshot, compareOriginalState, hash } from './embedded-browser-d-state.mjs'; const deployed = JSON.parse(fs.readFileSync('.qa/embedded-browser-d-formal-verify.json')); assert.equal(deployed.success, true); const base = 'http://127.0.0.1:4318', directory = path.resolve('.qa/embedded-browser-d-native-ui'); assert.equal(fs.existsSync(directory), false); fs.mkdirSync(directory); const before = await publicSnapshot(base, deployed.dataRoot); assert.equal(before.version, '1.22.0'); assert.equal(before.pid, 36552); const bootstrap = await (await fetch(base + '/api/bootstrap')).json(), originalId = '22c90b01-6b82-43d9-b2b6-88a4b245c781'; const original = await (await fetch(base + '/api/sessions/' + originalId + '/export', { headers: { 'X-Workbench-Token': bootstrap.token } })).json(); const originalStatus = original.session.status; const selection = { projectId: original.project.id, sessionId: originalId }, errors = [], blockedWrites = [], screenshots = [], results = []; let browser, application, result; async function prepare(page) { page.on('pageerror', error => errors.push(error.message)); await page.route('**/api/**', route => { if (!['GET', 'HEAD', 'OPTIONS'].includes(route.request().method())) { blockedWrites.push(route.request().method() + ' ' + route.request().url()); return route.abort(); } return route.continue(); }); } async function capture(page, name) { const file = path.join(directory, name + '.png'); await page.screenshot({ path: file }); screenshots.push({ file, sha256: hash(fs.readFileSync(file)) }); } async function inspect(page, width, height, label) { await expect(page.locator('.connection small')).toHaveText('v1.22.0', { timeout: 30000 }); await expect(page.locator('.work-header .work-title')).toContainText(original.session.title); await capture(page, label + '-original-session'); await page.getByRole('button', { name: '打开模型中心', exact: true }).click(); const dialog = page.getByRole('dialog', { name: '模型中心', exact: true }); await expect(dialog).toBeVisible(); await dialog.locator('.mc-connection').first().getByRole('button', { name: '原生参数', exact: true }).click(); const form = dialog.getByRole('form', { name: '原生请求参数', exact: true }); await expect(form).toBeVisible(); const current = before.modelCenter.connections[0]; assert.deepEqual(JSON.parse(await form.getByLabel('原生参数 JSON', { exact: true }).inputValue()), current.nativeParameters ?? {}); await expect(form.getByLabel('输出上限字段', { exact: true })).toHaveValue(current.outputTokenField ?? (current.protocol === 'responses' ? 'max_output_tokens' : 'max_tokens')); const save = form.getByRole('button', { name: '保存原生参数', exact: true }); await save.scrollIntoViewIfNeeded(); const box = await save.boundingBox(); assert.ok(box && box.x >= 0 && box.y >= 0 && box.x + box.width <= width + 1 && box.y + box.height <= height + 1); assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth)); await capture(page, label + '-native-parameters'); await form.getByRole('button', { name: '取消', exact: true }).click(); await page.keyboard.press('Escape'); await page.getByRole('button',{name:'查看任务记录',exact:true}).click(); const environment=page.getByRole('region',{name:'工程环境与依赖',exact:true});await environment.scrollIntoViewIfNeeded();await expect(environment).toBeVisible(); await expect(environment.getByRole('button',{name:'诊断工程环境',exact:true})).toBeVisible(); await capture(page,label+'-environment-entry'); await page.locator('.inspector-tabs').getByRole('button', { name: '浏览', exact: true }).click(); const browsing = page.getByRole('region', { name: '内置浏览器', exact: true }); await expect(browsing.getByRole('heading', { name: '在任务旁核对真实页面', exact: true })).toBeVisible(); if (label.startsWith('electron-')) await expect(browsing).not.toContainText('桌面浏览器未连接'); else await expect(browsing).toContainText('桌面浏览器未连接'); await expect(browsing.getByRole('button', { name: '打开目标', exact: true })).toBeEnabled(); assert.ok(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth)); await capture(page, label + '-browser-entry'); await browsing.getByRole('button', { name: '返回任务', exact: true }).click(); results.push({ label, width, height, originalStatus, originalSessionPreserved: true, nativeSettingsMatchPublicConnection: true, saveFullyReachable: true, browserEntryObserved: true, desktopBrowserConnected: label.startsWith('electron-'), submitted: false }); } try { browser = await chromium.launch({ channel: 'msedge', headless: true }); for (const [width, height] of [[1440, 1000], [390, 844]]) { const context = await browser.newContext({ viewport: { width, height } }); await context.addInitScript(value => { localStorage.setItem('awb-project', value.projectId); localStorage.setItem('awb-session', value.sessionId); }, selection); const page = await context.newPage(); await prepare(page); await page.goto(base + '/?release=1.22.0'); await inspect(page, width, height, 'edge-' + width); await context.close(); } const controller = new HostController({ appRoot: deployed.candidate, dataRoot: deployed.dataRoot, port: 4318, nodePath: process.execPath }); assert.equal((await controller.status()).desktopWindows.count, 0); application = await _electron.launch({ executablePath: path.join(deployed.candidate, 'desktop/AgentWorkbench.exe'), args: ['--app-root', deployed.candidate, '--data-root', deployed.dataRoot, '--port', '4318'], cwd: deployed.candidate, env: projectEnvironment(), timeout: 90000 }); const page = await application.firstWindow(); await expect(page.locator('.connection small')).toHaveText('v1.22.0', { timeout: 30000 }); await prepare(page); await page.evaluate(value => { localStorage.setItem('awb-project', value.projectId); localStorage.setItem('awb-session', value.sessionId); }, selection); await page.reload({ waitUntil: 'domcontentloaded' }); assert.equal((await controller.status()).desktopWindows.count, 1); for (const [width, height] of [[1280, 900], [390, 844]]) { await page.setViewportSize({ width, height }); await inspect(page, width, height, 'electron-' + width); } await application.close(); application = null; assert.equal((await controller.status()).desktopWindows.count, 0); assert.deepEqual(errors, []); assert.deepEqual(blockedWrites, []); const after = await publicSnapshot(base, deployed.dataRoot), retained = compareOriginalState(before, after); result = { success: true, version: before.version, pid: before.pid, candidate: deployed.candidate, results, screenshots, errors, blockedWrites, retained, originalSession: originalId, actualInstalledElectron: true, realModelRequests: 0, scope: 'Read-only formal Edge and visible installed Electron at two viewports; actual original session/current status and native parameters/save reachability. Original historical session was already completed at deployment baseline; no invented failure display. No settings saved, models called or tasks retried.' }; } catch (error) { result = { success: false, results, screenshots, errors, blockedWrites, error: error.stack }; process.exitCode = 1; } finally { await application?.close().catch(() => {}); await browser?.close(); fs.writeFileSync('.qa/embedded-browser-d-native-ui.json', JSON.stringify(result, null, 2), { flag: 'wx' }); console.log(JSON.stringify({ success: result.success, results: results.length, screenshots: screenshots.length, error: result.error })); }