110 lines
10 KiB
JavaScript
110 lines
10 KiB
JavaScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import assert from 'node:assert/strict';
|
|
import { createHash, randomUUID } from 'node:crypto';
|
|
import { spawn } from 'node:child_process';
|
|
import { fileURLToPath } from 'node:url';
|
|
|
|
const sha = value => createHash('sha256').update(value).digest('hex');
|
|
async function until(predicate, timeout = 20000) {
|
|
const deadline = Date.now() + timeout;
|
|
while (!await predicate()) {
|
|
if (Date.now() >= deadline) throw new Error('Owned browser verification timed out.');
|
|
await new Promise(resolve => setTimeout(resolve, 30));
|
|
}
|
|
}
|
|
|
|
export async function verifyBrowserApi({ api, url, token, directory, appRoot, contained = false }) {
|
|
const projectRoot = path.join(directory, 'browser-project'); fs.mkdirSync(path.join(projectRoot, 'dist'), { recursive: true });
|
|
fs.writeFileSync(path.join(projectRoot, 'README.md'), '# Owned portable browser acceptance\n');
|
|
fs.writeFileSync(path.join(projectRoot, 'dist', 'index.html'), '<!doctype html><html lang="zh-CN"><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1"><title>随包页面验收</title>'
|
|
+ '<style>body{font:16px sans-serif;margin:20px}input,select,button{display:block;max-width:100%;margin:12px 0}</style><h1>实际随包浏览器</h1>'
|
|
+ '<input id="query" aria-label="搜索"><select id="filter"><option value="all">全部</option><option value="pending">待办</option></select>'
|
|
+ '<button id="toggle">切换</button><p id="state">待办</p><script src="/app.js"></script></html>');
|
|
fs.writeFileSync(path.join(projectRoot, 'dist', 'app.js'), 'document.querySelector("#toggle").addEventListener("click",()=>document.querySelector("#state").textContent="已完成");');
|
|
const project = await api('/projects', { root: projectRoot }), session = await api('/sessions', { projectId: project.id }), route = '/sessions/' + session.id;
|
|
await api(route + '/messages', { content: '为自有静态工程创建独立验收任务。' }); let current;
|
|
await until(async () => { current = await api(route); return current.status === 'awaiting_approval'; });
|
|
const approval = current.approvals.find(value => value.status === 'pending'); assert.equal(approval.command, 'node --version');
|
|
await api(route + '/approvals/' + approval.id, { allow: true });
|
|
await until(async () => { current = await api(route); return current.status === 'needs_review'; });
|
|
for (const change of current.changes.filter(value => value.status === 'pending')) await api(route + '/changes/' + change.id + '/reject', {});
|
|
const taskId = (await api(route)).execution.task.id, availability = (await api(route + '/browser')).availability;
|
|
assert.equal(availability.status, 'available', availability.message); assert.equal(availability.playwrightVersion, '1.64.1');
|
|
const steps = [{ action: 'fill', selector: '#query', value: '本地页面验收' }, { action: 'select', selector: '#filter', value: 'pending' },
|
|
{ action: 'assert_count', selector: 'button', count: 1 }, { action: 'assert_text', selector: '#state', text: '待办' },
|
|
{ action: 'click', selector: '#toggle' }, { action: 'assert_text', selector: '#state', text: '已完成' }];
|
|
const results = [];
|
|
for (const viewport of [{ width: 1280, height: 900 }, { width: 390, height: 844 }]) {
|
|
const planned = await api(route + '/browser-plans', { steps, viewport }), plan = planned.execution.browserPlans.at(-1);
|
|
const contract = await api('/artifacts/' + plan.contractArtifactId + '?session=' + session.id + '&limit=16000');
|
|
assert.equal(contract.truncated, false); assert.equal(sha(contract.content), contract.sha256);
|
|
const sealed = JSON.parse(contract.content); assert.equal(sealed.id, plan.id); assert.equal(sealed.taskId, taskId);
|
|
assert.equal(sealed.installation.playwrightVersion, '1.64.1');
|
|
if (contained) assert.ok(sealed.installation.modulePath.startsWith(fs.realpathSync.native(path.join(appRoot, 'runtime', 'node_modules')) + path.sep));
|
|
await api(route + '/browser-jobs', { planId: plan.id }); let job;
|
|
await until(async () => { job = (await api(route)).execution.browserJobs.at(-1); return !!job?.finishedAt; }, 25000);
|
|
assert.equal(job.status, 'passed', job.summary); assert.equal(job.taskId, taskId); assert.equal(job.assertions.passed, 3);
|
|
assert.equal(job.cleanup.exited, true); assert.equal(job.cleanup.previewClosed, true); assert.equal(job.cleanup.snapshotDisposed, true);
|
|
let text = '', offset = 0, originalPage;
|
|
do { originalPage = await api(route + '/browser-jobs/' + job.id + '/record?offset=' + offset + '&limit=4000'); text += originalPage.content; offset += originalPage.content.length; } while (originalPage.truncated);
|
|
assert.equal(sha(text), job.resultSha256); const original = JSON.parse(text), actualPage = original.pages.at(-1);
|
|
assert.equal(original.steps[0].actual, '本地页面验收'); assert.deepEqual(original.steps[1].actual, ['pending']);
|
|
assert.deepEqual(original.pageErrors, []); assert.deepEqual(original.blockedRequests, []); assert.equal(actualPage.viewport.width, viewport.width);
|
|
assert.ok(actualPage.text.includes('已完成')); if (viewport.width === 390) assert.ok(actualPage.viewport.scrollWidth <= 390);
|
|
fs.writeFileSync(path.join(directory, 'browser-original-' + viewport.width + '.json'), text);
|
|
const image = job.screenshots.at(-1), response = await fetch(url + '/api' + route + '/browser-jobs/' + job.id + '/images/' + image.artifactId,
|
|
{ headers: { 'X-Workbench-Token': token, Connection: 'close' }, signal: AbortSignal.timeout(15000) });
|
|
assert.equal(response.status, 200); assert.equal(response.headers.get('content-type'), 'image/png'); const bytes = Buffer.from(await response.arrayBuffer());
|
|
assert.equal(sha(bytes), image.sha256); assert.equal(bytes.length, image.bytes); assert.equal(bytes.readUInt32BE(16), viewport.width); assert.equal(bytes.readUInt32BE(20), viewport.height);
|
|
fs.writeFileSync(path.join(directory, 'browser-original-' + viewport.width + '.png'), bytes);
|
|
const reused = await api(route + '/browser-jobs', { planId: plan.id }); assert.equal(reused.execution.browserJobs.at(-1).id, job.id);
|
|
assert.equal(reused.execution.browserJobs.length, results.length + 1);
|
|
results.push({ jobId: job.id, taskId, viewport, status: job.status, assertions: job.assertions, resultSha256: job.resultSha256,
|
|
buildSha256: job.buildSha256, screenshotSha256: image.sha256, browserExited: job.cleanup.exited, reusedOriginalJob: true });
|
|
}
|
|
return { success: true, installation: availability, results, modelRequests: 0,
|
|
scope: 'Actual API, locked browser runtime, two isolated Edge processes, CSS interactions, original DOM and PNG bytes. The task is created through the offline demonstration; no remote model is used.' };
|
|
}
|
|
|
|
async function main() {
|
|
if (process.argv.includes('--help')) { console.log('node scripts/verify-browser.mjs [--app-root <source-or-portable-directory>] [--contained]'); return; }
|
|
const index = process.argv.indexOf('--app-root'); if (index >= 0 && !process.argv[index + 1]) throw new Error('--app-root needs an absolute or relative directory.');
|
|
const appRoot = fs.realpathSync.native(path.resolve(index >= 0 ? process.argv[index + 1] : '.'));
|
|
const directory = path.resolve('.qa', 'browser-native-' + randomUUID()), data = path.join(directory, 'data'); fs.mkdirSync(data, { recursive: true });
|
|
const allowed = /^(path|pathext|systemroot|systemdrive|windir|comspec|temp|tmp|userprofile|appdata|localappdata|programfiles(?:\(x86\))?|commonprogramfiles(?:\(x86\))?|lang|lc_all)$/i;
|
|
const env = { ...Object.fromEntries(Object.entries(process.env).filter(([name]) => allowed.test(name))),
|
|
AGENT_WORKBENCH_APP_DIR: appRoot, AGENT_WORKBENCH_DATA_DIR: data, AGENT_WORKBENCH_PORT: '0' };
|
|
const runtime = path.join(appRoot, 'runtime', 'node.exe');
|
|
const child = spawn(fs.existsSync(runtime) ? runtime : process.execPath, [path.join(appRoot, 'dist-server', 'server.mjs')],
|
|
{ cwd: appRoot, env, windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
let url = '', stdout = '', stderr = '', api, result;
|
|
child.stdout.on('data', bytes => { stdout += bytes.toString('utf8'); for (const line of stdout.split('\n')) { try { const value = JSON.parse(line); if (value.ready) url = value.url; } catch {} } });
|
|
child.stderr.on('data', bytes => { stderr += bytes.toString('utf8'); });
|
|
try {
|
|
await until(() => url || child.exitCode !== null, 10000); assert.ok(url, 'Owned host failed: ' + stderr); assert.notEqual(new URL(url).port, '4318');
|
|
const bootstrap = await (await fetch(url + '/api/bootstrap')).json(); assert.equal(bootstrap.config.mode, 'demo'); assert.equal(bootstrap.config.hasKey, false);
|
|
const health = await (await fetch(url + '/api/health')).json(); assert.equal(health.pid, child.pid);
|
|
api = async (route, input) => {
|
|
const response = await fetch(url + '/api' + route, { method: input === undefined ? 'GET' : 'POST', headers: { 'Content-Type': 'application/json', 'X-Workbench-Token': bootstrap.token, Connection: 'close' },
|
|
...(input === undefined ? {} : { body: JSON.stringify(input) }), signal: AbortSignal.timeout(20000) });
|
|
const value = await response.json(); assert.ok(response.ok, value.error); return value;
|
|
};
|
|
result = { ...await verifyBrowserApi({ api, url, token: bootstrap.token, directory, appRoot, contained: process.argv.includes('--contained') }),
|
|
version: bootstrap.version, directory, appRoot, testedAt: new Date().toISOString(), ownedHostPid: child.pid };
|
|
} catch (error) { result = { success: false, directory, appRoot, error: error.stack }; process.exitCode = 1; }
|
|
finally {
|
|
if (api && child.exitCode === null) {
|
|
try { await api('/shutdown', {}); await until(() => child.exitCode !== null || child.signalCode !== null, 10000); }
|
|
catch (error) { result.success = false; result.shutdownError = error.message; process.exitCode = 1; }
|
|
}
|
|
if (child.exitCode === null && child.signalCode === null) child.kill();
|
|
await until(() => child.exitCode !== null || child.signalCode !== null, 5000); result.rootExited = true;
|
|
fs.writeFileSync(path.join(directory, 'host.log'), stdout + '\n' + stderr);
|
|
}
|
|
fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify(result, null, 2) + '\n');
|
|
fs.writeFileSync('.qa/browser-native-validation.json', JSON.stringify(result, null, 2) + '\n');
|
|
console.log(JSON.stringify({ success: result.success, version: result.version, directory, results: result.results, modelRequests: 0, rootExited: result.rootExited, error: result.error, shutdownError: result.shutdownError }));
|
|
}
|
|
if (process.argv[1] && path.resolve(process.argv[1]) === fileURLToPath(import.meta.url)) await main();
|