136 lines
17 KiB
JavaScript
136 lines
17 KiB
JavaScript
import { openWorkbenchTool } from './ui-navigation.mjs';
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import net from 'node:net';
|
|
import http from 'node:http';
|
|
import { randomUUID, createHash } from 'node:crypto';
|
|
import { DatabaseSync } from 'node:sqlite';
|
|
import { _electron } from 'playwright';
|
|
import { HostController } from '../src/core/desktop/host-controller.mjs';
|
|
import { observeWindowsProcess } from '../src/core/desktop/process-identity.mjs';
|
|
import { projectEnvironment } from '../src/core/process-environment.mjs';
|
|
import { desktopExecutableIdentity } from '../src/core/desktop/app-installation.mjs';
|
|
|
|
if (process.argv.includes('--help')) { console.log('node scripts/verify-desktop-window.mjs [--native-picker] — isolated actual Electron runtime; default uses explicitly identified native-dialog/shell fixtures, --native-picker waits for actual Windows UI selection. No existing credentials or configured model.'); process.exit(0); }
|
|
if (process.argv.slice(2).some(arg => arg !== '--native-picker')) throw new Error('Use --help.');
|
|
const native = process.argv.includes('--native-picker'), appRoot = process.cwd(), root = path.resolve('.qa', 'desktop-window-' + randomUUID());
|
|
const dataRoot = path.join(root, 'data'), projectRoot = path.join(root, 'owned-project'); fs.mkdirSync(projectRoot, { recursive: true });
|
|
fs.writeFileSync(path.join(projectRoot, 'README.md'), '# Owned desktop project\n\nOriginal source for desktop lifecycle acceptance.\n');
|
|
fs.writeFileSync(path.join(projectRoot, 'package.json'), JSON.stringify({ name: 'owned-desktop-project', type: 'module', scripts: { test: 'node --test case.test.mjs' } }));
|
|
fs.writeFileSync(path.join(projectRoot, 'case.test.mjs'), "import test from 'node:test';import assert from 'node:assert/strict';import fs from 'node:fs';test('original desktop source retained',()=>assert.match(fs.readFileSync('README.md','utf8'),/Original source/));\n");
|
|
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 controller = new HostController({ appRoot, dataRoot, port, nodePath: process.execPath, timeoutMs: 60000 });
|
|
let application, page, original, externalServer, stage = 'start', expectedNavigation = false; const results = [], errors = [], requests = [];
|
|
const record = (name, detail = {}) => { results.push({ name, passed: true, ...detail }); console.log(name + ': passed'); };
|
|
const until = async (action, timeout = 30000) => { const deadline = Date.now() + timeout; while (!await action()) { if (Date.now() > deadline) throw new Error('Timeout: ' + stage); await new Promise(resolve => setTimeout(resolve, 100)); } };
|
|
const save = result => { fs.writeFileSync(path.join(root, 'result.json'), JSON.stringify(result, null, 2)); fs.writeFileSync('.qa/desktop-window-' + (native ? 'native' : 'latest') + '.json', JSON.stringify(result, null, 2)); };
|
|
const completedTools = id => {
|
|
const db = new DatabaseSync(path.join(dataRoot, 'workbench.sqlite'), { readOnly: true });
|
|
try { return db.prepare("SELECT payload FROM records WHERE kind='tool' AND session_id=? ORDER BY id").all(id).map(row => JSON.parse(row.payload)).filter(value => value.state === 'completed'); } finally { db.close(); }
|
|
};
|
|
async function launch() {
|
|
const executable = desktopExecutableIdentity(appRoot).path, packaged = executable.toLowerCase().endsWith('agentworkbench.exe');
|
|
const current = await _electron.launch({ executablePath: executable,
|
|
args: [...(packaged ? [] : [path.join(appRoot, 'dist-desktop/main.cjs')]), '--app-root', appRoot, '--data-root', dataRoot, '--node', process.execPath, '--port', String(port), ...(native ? [] : ['--hidden-window'])],
|
|
cwd: appRoot, env: projectEnvironment(), timeout: 60000 });
|
|
const window = await current.firstWindow();
|
|
window.on('pageerror', error => errors.push(error.message)); window.on('requestfailed', request => { if (!expectedNavigation) requests.push({ url: request.url(), failure: request.failure()?.errorText }); });
|
|
await window.locator('.connection.connected').waitFor({ timeout: 30000 });
|
|
return { application: current, page: window };
|
|
}
|
|
async function snapshot(name, width) {
|
|
const window = await application.browserWindow(page); await window.evaluate((value, width) => { value.showInactive(); value.setContentSize(width, 900); }, width);
|
|
await until(() => page.evaluate(width => innerWidth === width, width));
|
|
await page.screenshot({ path: path.join(root, name + '.png') }); fs.writeFileSync(path.join(root, name + '.html'), await page.content());
|
|
const overflow = await page.evaluate(() => document.documentElement.scrollWidth > innerWidth);
|
|
assert.equal(overflow, false); return { name, width, png: path.join(root, name + '.png') };
|
|
}
|
|
try {
|
|
original = (await controller.start()).identity; const project = await controller.api('/projects', { method: 'POST', input: { root: projectRoot } });
|
|
const session = await controller.api('/sessions', { method: 'POST', input: { projectId: project.id } });
|
|
({ application, page } = await launch());
|
|
const pid = await application.evaluate(() => process.pid), launcherPid = application.process().pid, observed = observeWindowsProcess(pid), status = await controller.status();
|
|
assert.equal(status.identity.pid, original.pid); assert.equal(status.desktopWindows.count, 1); assert.equal(status.desktopWindows.entries[0].pid, pid); assert.equal(status.desktopWindows.entries[0].createdAt, observed.createdAt); record('actual Electron process registered and original host reused', { electronPid: pid, launcherPid, hostPid: original.pid });
|
|
await page.getByRole('combobox', { name: '当前项目', exact: true }).selectOption(project.id);
|
|
if (native) {
|
|
stage = 'actual native directory picker'; const chosenRoot = path.join(root, 'native-picked-project'); fs.mkdirSync(chosenRoot); fs.writeFileSync(path.join(chosenRoot, 'README.md'), '# Actual native picker project\n');
|
|
await openWorkbenchTool(page, '桌面宿主');
|
|
const pending = page.getByRole('button', { name: '选择项目文件夹', exact: true }).click();
|
|
console.log(JSON.stringify({ nativePickerReady: true, expectedPath: chosenRoot, electronPid: pid, hostPid: original.pid, root }));
|
|
await until(async () => (await controller.api('/bootstrap')).projects.some(value => value.root.toLowerCase() === chosenRoot.toLowerCase()), 1200000); await pending;
|
|
await until(async () => (await page.getByRole('combobox', { name: '当前项目', exact: true }).textContent()).includes('native-picked-project'));
|
|
record(stage, { directory: chosenRoot, dialog: 'Actual Windows openDirectory dialog; no dialog/shell replacement in this run.' });
|
|
await snapshot('native-picker-result', 1440);
|
|
} else {
|
|
stage = 'sandboxed renderer and fixed preload';
|
|
const surface = await page.evaluate(() => ({ methods: Object.keys(window.desktopWorkbench).sort(), require: typeof window.require, process: typeof window.process, ipc: typeof window.ipcRenderer, frozen: Object.isFrozen(window.desktopWorkbench) }));
|
|
// This verifier runs the current source and its current preload. Keep an
|
|
// explicit allowlist so later versions retain these reviewed capabilities
|
|
// while any unexpected privileged method still fails acceptance.
|
|
assert.deepEqual(surface.methods, ['browsingLayout', 'openFilePreview', 'pickProject', 'projectFolder', 'protocol', 'revealFile', 'reviewTask', 'status', 'stopHost'].sort());
|
|
assert.equal(surface.require, 'undefined'); assert.equal(surface.process, 'undefined'); assert.equal(surface.ipc, 'undefined'); assert.equal(surface.frozen, true);
|
|
const preferences = await (await application.browserWindow(page)).evaluate(value => value.webContents.getLastWebPreferences());
|
|
assert.equal(preferences.sandbox, true); assert.equal(preferences.contextIsolation, true); assert.equal(preferences.nodeIntegration, false); record(stage, { surface });
|
|
stage = 'external navigation, requests and popups rejected'; let outsideRequests = 0;
|
|
externalServer = http.createServer((_req, res) => { outsideRequests++; res.end('owned external fixture'); }); await new Promise(resolve => externalServer.listen(0, '127.0.0.1', resolve));
|
|
const externalUrl = 'http://127.0.0.1:' + externalServer.address().port + '/outside'; expectedNavigation = true;
|
|
await page.evaluate(url => { window.open(url); location.href = url; }, externalUrl); await new Promise(resolve => setTimeout(resolve, 300));
|
|
assert.equal(new URL(page.url()).origin, controller.url); assert.equal(application.windows().length, 1); assert.equal(outsideRequests, 0);
|
|
const navigation = await (await application.browserWindow(page)).evaluate(value => ({ url: value.webContents.getURL(), loading: value.webContents.isLoading() }));
|
|
assert.equal(navigation.loading, false); assert.equal(new URL(navigation.url).origin, controller.url);
|
|
// Chromium reports requestedNavigation to Playwright before Electron vetoes it.
|
|
// A real window refresh clears that automation-only pending document state.
|
|
const refreshed = page.waitForEvent('domcontentloaded'); await (await application.browserWindow(page)).evaluate(value => value.webContents.reload()); await refreshed;
|
|
await page.locator('.connection.connected').waitFor(); expectedNavigation = false; record(stage, { navigation, originalWindowRefreshed: true });
|
|
stage = 'offline original task and approval retained across refresh and window close';
|
|
await page.getByRole('textbox', { name: '任务内容', exact: true }).fill('Read this owned project and prepare the offline demonstration proposal.'); await page.getByRole('button', { name: '发送任务', exact: true }).click();
|
|
await page.getByRole('button', { name: '确认执行', exact: true }).waitFor({ timeout: 20000 });
|
|
const before = await controller.api('/sessions/' + session.id), tools = completedTools(session.id), hash = createHash('sha256').update(JSON.stringify(tools)).digest('hex'); assert.ok(tools.length >= 3);
|
|
await page.reload(); await page.getByRole('button', { name: '确认执行', exact: true }).waitFor({ timeout: 20000 }); assert.equal((await controller.api('/sessions/' + session.id)).approvals.at(-1).id, before.approvals.at(-1).id);
|
|
const closed = application.waitForEvent('close'); await (await application.browserWindow(page)).evaluate(value => value.close()); await closed; application = null;
|
|
assert.equal((await controller.connect()).identity.instanceId, original.instanceId); assert.equal(observeWindowsProcess(pid), null); assert.equal((await controller.status()).desktopWindows.count, 0);
|
|
({ application, page } = await launch()); await page.getByRole('button', { name: '确认执行', exact: true }).waitFor({ timeout: 20000 });
|
|
const reopened = await controller.api('/sessions/' + session.id); assert.equal(reopened.execution.task.id, before.execution.task.id); assert.equal(reopened.execution.runs.at(-1).id, before.execution.runs.at(-1).id); assert.equal(reopened.eventSeq, before.eventSeq); assert.equal(reopened.approvals.at(-1).id, before.approvals.at(-1).id);
|
|
assert.equal(createHash('sha256').update(JSON.stringify(completedTools(session.id))).digest('hex'), hash); record(stage, { taskId: before.execution.task.id, runId: before.execution.runs.at(-1).id, approvalId: before.approvals.at(-1).id, eventSeq: before.eventSeq, completedTools: tools.length });
|
|
stage = 'actual command approval, proposal and project check'; await page.getByRole('button', { name: '确认执行', exact: true }).click();
|
|
await until(async () => (await controller.api('/sessions/' + session.id)).status === 'needs_review');
|
|
const done = await controller.api('/sessions/' + session.id); const change = done.changes.at(-1); assert.equal(change.status, 'pending');
|
|
await controller.api('/sessions/' + session.id + '/changes/' + change.id + '/apply', { method: 'POST', input: {} });
|
|
const planned = await controller.api('/sessions/' + session.id + '/check-plans', { method: 'POST', input: {} });
|
|
const started = await controller.api('/sessions/' + session.id + '/check-jobs', { method: 'POST', input: { planId: planned.execution.plans.at(-1).id } }), checkId = started.execution.checks.at(-1).id;
|
|
await until(async () => !['queued', 'running'].includes((await controller.api('/sessions/' + session.id + '/check-jobs/' + checkId)).status));
|
|
const check = await controller.api('/sessions/' + session.id + '/check-jobs/' + checkId); assert.equal(check.status, 'passed'); record(stage, { changeId: change.id, checkId, checkStatus: check.status });
|
|
stage = 'current project, task and file desktop entry scope';
|
|
await application.evaluate(({ dialog, shell }, projectRoot) => { globalThis.desktopFixtureCalls = []; dialog.showOpenDialog = async () => ({ canceled: false, filePaths: [projectRoot] }); dialog.showMessageBox = async () => ({ response: 0 }); shell.openPath = async value => { globalThis.desktopFixtureCalls.push({ folder: value }); return ''; }; shell.showItemInFolder = value => globalThis.desktopFixtureCalls.push({ reveal: value }); }, projectRoot);
|
|
await page.getByRole('button', { name: 'REA 工件研究', exact: true }).waitFor();
|
|
await page.getByRole('button', { name: 'README.md', exact: true }).click(); await page.locator('.file-heading').getByText('README.md', { exact: true }).waitFor();
|
|
const file = await controller.api('/projects/' + project.id + '/file?path=README.md'), scope = { projectId: project.id, sessionId: session.id, taskId: done.execution.task.id };
|
|
const invalid = await page.evaluate(async input => { const rejected = []; for (const value of [{ ...input, path: '../outside' }, { ...input, taskId: '00000000-0000-0000-0000-000000000000' }, { ...input, sha256: '0'.repeat(64) }, { ...input, command: 'foreign' }]) { try { await window.desktopWorkbench.revealFile(value); rejected.push(false); } catch { rejected.push(true); } } return rejected; }, { ...scope, path: file.path, sha256: file.sha256 });
|
|
assert.deepEqual(invalid, [true, true, true, true]);
|
|
await openWorkbenchTool(page, '桌面宿主'); await page.getByRole('button', { name: '打开项目目录', exact: true }).click(); await page.getByRole('button', { name: '定位当前文件', exact: true }).click();
|
|
await until(() => application.evaluate(() => globalThis.desktopFixtureCalls.length === 2));
|
|
const fixtureCalls = await application.evaluate(() => globalThis.desktopFixtureCalls); assert.deepEqual(fixtureCalls, [{ folder: projectRoot }, { reveal: path.join(projectRoot, 'README.md') }]);
|
|
const screenshots = [await snapshot('desktop-host-1440', 1440), await snapshot('desktop-host-390', 390)];
|
|
await page.getByRole('button', { name: '审查当前任务', exact: true }).click(); await page.getByLabel('任务记录', { exact: true }).waitFor();
|
|
record(stage, { invalidRejected: invalid.length, fixtureCalls, screenshots, nativeOperations: 'Dialog/shell adapters are declared fixtures in this automated run; actual Windows picker separately verified.' });
|
|
stage = 'native stop cancelled, then original host explicitly stopped';
|
|
await page.getByRole('button', { name: '关闭检视区', exact: true }).click();
|
|
await openWorkbenchTool(page, '桌面宿主'); await page.getByRole('button', { name: '停止并退出', exact: true }).click();
|
|
await until(() => page.getByRole('button', { name: '停止并退出', exact: true }).isEnabled());
|
|
assert.equal((await controller.connect()).identity.instanceId, original.instanceId);
|
|
await application.evaluate(({ dialog }) => { dialog.showMessageBox = async () => ({ response: 1 }); });
|
|
const exited = application.waitForEvent('close'); expectedNavigation = true; await page.getByRole('button', { name: '停止并退出', exact: true }).click(); await exited; application = null;
|
|
assert.equal(observeWindowsProcess(original.pid), null); assert.equal(await controller.health(), null); expectedNavigation = false; record(stage, { hostPid: original.pid, exited: true, confirmation: 'Declared native confirmation fixtures: cancelled first, confirmed second.' });
|
|
}
|
|
assert.deepEqual(errors, []); assert.deepEqual(requests, []);
|
|
save({ success: true, root, appRoot, dataRoot, port, results, errors, requests, native, inference: 'Offline demo/local engineering checks only; no configured model or existing credentials.' });
|
|
} catch (error) {
|
|
if (page && !page.isClosed()) { try { fs.writeFileSync(path.join(root, 'failure.html'), await page.content()); await page.screenshot({ path: path.join(root, 'failure.png') }); } catch {} }
|
|
save({ success: false, root, appRoot, dataRoot, stage, results, errors, requests, message: error.message, stack: error.stack, native }); throw error;
|
|
} finally {
|
|
if (application) await application.close();
|
|
if (await controller.health()) await controller.stop();
|
|
if (externalServer) { externalServer.closeAllConnections(); await new Promise(resolve => externalServer.close(resolve)); }
|
|
}
|