103 lines
10 KiB
JavaScript
103 lines
10 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { createHash } from 'node:crypto';
|
|
import { fixture, launchWorkbench, until, compatibleFixture, call, completion, assertToolPairs, workspace } from './helpers.mjs';
|
|
import { createBrowserProject, browserSteps } from './fixtures/browser-project.mjs';
|
|
import { BrowserManager } from '../src/core/browser/browser-manager.mjs';
|
|
import { Store } from '../src/server/store.mjs';
|
|
import { requestMetrics } from '../src/shared/contracts/model-request.mjs';
|
|
|
|
test('real browser HTTP plans, original field pages and PNG bytes preserve ownership and survive host restart', async t => {
|
|
const f = createBrowserProject(fixture(t)); f.store.close(); let host = await launchWorkbench(t, f.data), route = '/sessions/' + f.session.id;
|
|
assert.equal(host.bootstrap.capabilities.browserChecks, 'sealed-static-build-and-real-edge-assertions');
|
|
assert.equal((await host.api(route + '/browser')).availability.status, 'available');
|
|
let session = await host.api(route + '/browser-plans', { steps: browserSteps, viewport: { width: 390, height: 844 } });
|
|
const plan = session.execution.browserPlans.at(-1), other = await host.api('/sessions', { projectId: f.project.id });
|
|
assert.equal(plan.build.files, 2); assert.equal(session.execution.browserJobs.length, 0);
|
|
await assert.rejects(host.api('/sessions/' + other.id + '/browser-jobs', { planId: plan.id }), /当前工程Task|不属于/);
|
|
await host.api(route + '/browser-jobs', { planId: plan.id });
|
|
await until(async () => { session = await host.api(route); return session.execution.browserJobs.at(-1)?.finishedAt; }, 20000);
|
|
const job = session.execution.browserJobs.at(-1); assert.equal(job.status, 'passed', job.summary); assert.equal(job.cleanup.exited, true);
|
|
let text = '', offset = 0, page;
|
|
do { page = await host.api(route + '/browser-jobs/' + job.id + '/record?offset=' + offset + '&limit=1700'); text += page.content; offset += page.content.length; } while (page.truncated);
|
|
assert.equal(createHash('sha256').update(text).digest('hex'), job.resultSha256); const original = JSON.parse(text);
|
|
assert.equal(original.pages.at(-1).viewport.width, 390); assert.equal(original.pages.at(-1).viewport.scrollWidth <= 390, true);
|
|
const image = job.screenshots.at(-1), imageRoute = '/api' + route + '/browser-jobs/' + job.id + '/images/' + image.artifactId;
|
|
const response = await fetch(host.url + imageRoute, { headers: { 'X-Workbench-Token': host.bootstrap.token } });
|
|
assert.equal(response.headers.get('content-type'), 'image/png'); const bytes = Buffer.from(await response.arrayBuffer());
|
|
assert.equal(bytes.length, image.bytes); assert.equal(createHash('sha256').update(bytes).digest('hex'), image.sha256);
|
|
assert.equal((await fetch(host.url + imageRoute)).status, 403);
|
|
await assert.rejects(host.api('/sessions/' + other.id + '/browser-jobs/' + job.id + '/record'), /不属于/);
|
|
const duplicate = await host.api(route + '/browser-jobs', { planId: plan.id }); assert.equal(duplicate.execution.browserJobs.length, 1);
|
|
await host.close(); host = await launchWorkbench(t, f.data);
|
|
assert.deepEqual((await host.api(route)).execution.browserJobs.at(-1), job);
|
|
assert.equal((await host.api(route + '/browser-jobs/' + job.id + '/record?limit=1700')).content, text.slice(0, 1700));
|
|
assert.equal((await host.api(route)).execution.task.usage.requests, 0);
|
|
});
|
|
|
|
test('self harness browser tools require approval and feed verified actual page fields back to the original Task', async t => {
|
|
const f = createBrowserProject(fixture(t)); f.store.close(); let phase = 0;
|
|
const model = await compatibleFixture(t, (url, input) => {
|
|
if (!input?.messages) return { data: [{ id: 'browser-protocol-fixture' }] };
|
|
assert.ok(requestMetrics(input).estimatedInputTokens <= 12352, 'The complete actual request must fit the fixed 16K/2048/1600 budget.');
|
|
assert.ok(input.tools.some(tool => tool.function.name === 'run_browser'));
|
|
const last = input.messages.filter(value => value.role === 'tool').at(-1);
|
|
const latest = last ? JSON.parse(last.content) : null;
|
|
if (phase++ === 0) return completion([call('browser-fixture-plan', 'plan_browser', { steps: browserSteps })]);
|
|
if (phase === 2) return completion([call('browser-fixture-run', 'run_browser', { plan_id: latest.id })]);
|
|
if (phase === 3) { assert.equal(latest.browser_status, 'passed'); assert.equal(latest.evidence_verified, true); return completion([call('browser-fixture-read', 'read_browser_result', { browser_job_id: latest.browser_job_id, section: 'page', limit: 6000 })]); }
|
|
assert.equal(latest.evidence_verified, true); assert.equal(latest.truncated, false, 'This small original page must fit without repeated ten-character reads.');
|
|
const page = JSON.parse(latest.content);
|
|
assert.ok(page.text.includes('已完成')); assert.equal(page.controls.find(control => control.id === 'query').value, '真实输入');
|
|
return completion([], '已核对当前Task实际页面和原断言。');
|
|
});
|
|
const host = await launchWorkbench(t, f.data), route = '/sessions/' + f.session.id;
|
|
await host.api('/config', { mode: 'compatible', baseUrl: model.baseUrl, model: 'browser-protocol-fixture', apiKey: 'SYNTHETIC-BROWSER-QA-ONLY', contextLimit: 16000, maxOutputTokens: 2048 });
|
|
await host.api(route + '/messages', { content: '请验证当前工程的填写、选择和状态切换,读取真实页面结果。' });
|
|
let session;
|
|
await until(async () => { session = await host.api(route); return session.status === 'awaiting_approval'; }, 15000);
|
|
const taskId = session.execution.task.id, approval = session.approvals.at(-1);
|
|
assert.ok(approval.browserPlanId); assert.equal(approval.checkPlanId, undefined); assert.equal(session.execution.browserJobs.length, 0);
|
|
assert.equal(approval.browserSummary.steps.length, browserSteps.length);
|
|
await host.api(route + '/approvals/' + approval.id, { allow: true });
|
|
await until(async () => { session = await host.api(route); return ['completed', 'failed', 'paused'].includes(session.status); }, 25000);
|
|
assert.equal(session.status, 'completed', session.failure?.message); assert.equal(session.execution.task.id, taskId);
|
|
assert.equal(session.execution.browserJobs.at(-1).status, 'passed'); assert.equal(session.execution.browserJobs.at(-1).taskId, taskId);
|
|
assert.equal(session.events.filter(value => value.kind === 'tool' && value.phase === 'completed').length, 3);
|
|
assert.equal(model.requests.filter(value => value.input?.messages).length, 4, 'No synthetic HTTP failure/retry or extra read is needed.');
|
|
assertToolPairs((await host.api(route + '/export')).session.providerMessages);
|
|
});
|
|
|
|
test('browser host recovery retains unknown root outcome, blocks replay, and requires an explicit owned acknowledgement', async t => {
|
|
const f = createBrowserProject(fixture(t)), manager = new BrowserManager(f.store, { appDirectory: workspace });
|
|
const plan = await manager.plan(f.session, { steps: browserSteps }), now = new Date().toISOString();
|
|
const job = { id: '42424242-4242-4242-4242-424242424242', sessionId: f.session.id, projectId: f.project.id, taskId: f.session.taskId, runId: f.session.runId,
|
|
planId: plan.id, planFingerprint: plan.fingerprint, sourceFingerprint: plan.sourceFingerprint, buildSha256: plan.build.sha256,
|
|
status: 'running', browserDispatched: true, requiresVerification: false, startedAt: now, finishedAt: null, steps: [{ index: 0, status: 'passed', actual: 'before host exit' }], screenshots: [] };
|
|
f.store.putRecord('browser_job', f.session, job, 'browser.test_crash'); f.store.close();
|
|
const reopened = new Store(f.data); f.onCleanup(() => reopened.close()); const session = reopened.session(f.session.id), restored = reopened.core.browser_job.get(job.id);
|
|
assert.equal(restored.status, 'unknown'); assert.equal(restored.requiresVerification, true); assert.equal(restored.steps[0].actual, 'before host exit');
|
|
const next = new BrowserManager(reopened, { appDirectory: workspace }); f.onCleanup(() => next.stopAll());
|
|
const newPlan = await next.plan(session, { steps: browserSteps }); assert.throws(() => next.start(session, newPlan.id), /原记录/);
|
|
assert.throws(() => next.acknowledge(session, restored.id, '0'.repeat(64)), /已经变化/);
|
|
next.acknowledge(session, restored.id, restored.planFingerprint); assert.equal(restored.status, 'unknown'); assert.equal(restored.requiresVerification, false);
|
|
const actual = await next.start(session, newPlan.id).promise; assert.equal(actual.status, 'passed', actual.summary); assert.equal(restored.status, 'unknown');
|
|
});
|
|
|
|
test('late HTTP stop of a finished browser job cannot interrupt a later job in the same Task', async t => {
|
|
const f = createBrowserProject(fixture(t)); f.store.close(); const host = await launchWorkbench(t, f.data), route = '/sessions/' + f.session.id;
|
|
const planned = await host.api(route + '/browser-plans', { steps: [{ action: 'assert_count', selector: 'button', count: 1 }] });
|
|
await host.api(route + '/browser-jobs', { planId: planned.execution.browserPlans.at(-1).id }); let first;
|
|
await until(async () => { first = (await host.api(route)).execution.browserJobs.at(-1); return !!first.finishedAt; }, 20000); assert.equal(first.status, 'passed');
|
|
const next = await host.api(route + '/browser-plans', { steps: [{ action: 'assert_visible', selector: '#state' }, { action: 'assert_count', selector: '.not-present', count: 1 }] });
|
|
const started = await host.api(route + '/browser-jobs', { planId: next.execution.browserPlans.at(-1).id }), jobId = started.execution.browserJobs.at(-1).id;
|
|
await until(async () => (await host.api(route)).execution.browserJobs.at(-1).steps.length === 1, 15000);
|
|
const late = await host.api(route + '/browser-jobs/' + first.id + '/stop', {});
|
|
assert.notEqual(late.execution.browserJobs.at(-1).status, 'interrupted', 'Stale stop affected a later job.');
|
|
assert.equal(late.execution.browserJobs[0].status, 'passed');
|
|
await host.api(route + '/browser-jobs/' + jobId + '/stop', {});
|
|
const finished = (await host.api(route)).execution.browserJobs.at(-1);
|
|
assert.equal(finished.status, 'interrupted'); assert.equal(finished.cleanup.exited, true);
|
|
assert.equal((await host.api(route)).execution.task.usage.requests, 0);
|
|
});
|