149 lines
20 KiB
JavaScript
149 lines
20 KiB
JavaScript
import { openWorkbenchTool } from './ui-navigation.mjs';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import assert from 'node:assert/strict';
|
|
import { createHash, randomUUID } from 'node:crypto';
|
|
import { chromium, expect } from 'playwright/test';
|
|
import { fixture } from '../tests/helpers.mjs';
|
|
import { workerHttp } from '../tests/fixtures/workers-http.mjs';
|
|
import { EngineRunner } from '../src/core/engines/engine-runner.mjs';
|
|
import { resolveControlledInstallation } from '../src/core/engines/controlled-installation.mjs';
|
|
import { ownerBytes } from '../tests/fixtures/workers-project.mjs';
|
|
import { desktopProjectQa } from './desktop-project-qa.mjs';
|
|
|
|
if (process.argv.includes('--help')) {
|
|
console.log('node scripts/verify-controlled-ui.mjs [--desktop] — real Edge or Electron desktop/390px, actual fixed Harness, owned in-memory model fixture, engine selection, reports, approvals, stop and explicit unknown recovery. No configured-model requests.'); process.exit(0);
|
|
}
|
|
assert.ok(process.argv.slice(2).every(value => value === '--desktop')); assert.ok(process.argv.length <= 3);
|
|
const desktopMode = process.argv.includes('--desktop');
|
|
const version = JSON.parse(fs.readFileSync('package.json', 'utf8')).version, directory = path.resolve('.qa', (desktopMode ? 'desktop-controlled-ui-' : 'controlled-ui-') + randomUUID());
|
|
const installation = resolveControlledInstallation(process.cwd(), process.env.AGENT_WORKBENCH_DEEPSEEK_ROOT); assert.equal(installation.controlledStatus, 'available', installation.message);
|
|
fs.mkdirSync(directory, { recursive: true });
|
|
const hash = bytes => createHash('sha256').update(bytes).digest('hex'), disposers = [], t = { after: value => disposers.push(value) };
|
|
const f = fixture(t), host = await workerHttp(f), original = ownerBytes(f), assertions = [], screenshots = [], pageErrors = [], requestFailures = [], expectedRejections = [];
|
|
f.store.saveConfig({ contextLimit: 64000 });
|
|
host.runtime.controlledEngines = new EngineRunner(f.store, host.provider, { directoryRoot: path.join(f.root, 'engine-runs') });
|
|
function inputs() {
|
|
const files = ['package.json', 'package-lock.json'];
|
|
const walk = root => { for (const entry of fs.readdirSync(root, { withFileTypes: true })) { const file = path.join(root, entry.name); if (entry.isDirectory()) walk(file); else if (entry.isFile()) files.push(file); } };
|
|
for (const root of ['src', 'scripts', 'dist-client', 'dist-server', ...(desktopMode ? ['dist-desktop'] : [])]) walk(root);
|
|
return files.sort().map(file => ({ path: file, sha256: hash(fs.readFileSync(file)) }));
|
|
}
|
|
const frozen = inputs(), desktop = desktopMode ? await desktopProjectQa({ store: f.store, provider: host.provider, runtime: host.runtime, sample: directory, kind: 'controlled-ui' }) : null;
|
|
const browser = desktopMode ? null : await chromium.launch({ channel: 'msedge', headless: true }), page = desktop?.page ?? await browser.newPage({ viewport: { width: 1440, height: 1000 } });
|
|
const resize = size => desktop ? desktop.resize(size.width, size.height) : page.setViewportSize(size);
|
|
page.on('pageerror', error => pageErrors.push(error.message)); page.on('requestfailed', request => requestFailures.push(request.url() + ': ' + request.failure()?.errorText));
|
|
const panel = () => page.getByLabel('隔离协作控制台', { exact: true }), runPanel = () => page.getByLabel('受控引擎工程运行', { exact: true });
|
|
const mark = text => { assertions.push(text); console.log('PASS ' + text); };
|
|
async function screenshot(name) { const file = path.join(directory, name + '.png'); await page.screenshot({ path: file, scale: 'css' }); screenshots.push({ file, sha256: hash(fs.readFileSync(file)) }); }
|
|
async function closeInspector() {
|
|
const close = page.getByRole('button', { name: '关闭检视区', exact: true });
|
|
if (await page.evaluate(() => innerWidth) > 940) { await expect(close).toHaveCount(0); return; }
|
|
await expect(close).toHaveCount(1);
|
|
if (await close.isVisible()) { await close.click(); await expect(page.locator('.inspector')).toBeHidden(); }
|
|
}
|
|
async function openWorkers() { await closeInspector(); await openWorkbenchTool(page, '隔离协作'); await expect(panel()).toBeVisible(); await expect(panel().getByRole('button', { name: '刷新', exact: true })).toBeEnabled(); }
|
|
async function openEngine() { await closeInspector(); await openWorkbenchTool(page, '本地代理引擎'); await expect(runPanel()).toBeVisible(); }
|
|
async function parentFromChild() { await openWorkers(); await panel().getByRole('button', { name: '返回父目标', exact: true }).click(); await expect.poll(() => page.evaluate(() => localStorage.getItem('awb-session'))).toBe(f.session.id); await openWorkers(); }
|
|
async function start() { await panel().getByRole('button', { name: /开始引擎任务|继续未完成子任务/, exact: true }).click(); await page.getByRole('dialog', { name: '确认受限子执行', exact: true }).getByRole('button', { name: '确认开始', exact: true }).click(); }
|
|
async function applyOne() {
|
|
if (await page.getByRole('button', { name: '查看改动', exact: true }).isVisible()) await page.getByRole('button', { name: '查看改动', exact: true }).click();
|
|
else await page.locator('.inspector-tabs').getByRole('button', { name: /^改动/ }).click();
|
|
await page.getByRole('button', { name: '组合审查', exact: true }).click();
|
|
await page.getByRole('dialog', { name: '选择要一起审查的提案', exact: true }).getByRole('button', { name: '封存审查范围', exact: true }).click();
|
|
const review = page.getByRole('dialog', { name: '多文件变更集审查', exact: true }); await review.getByRole('button', { name: '确认应用全部 1 项', exact: true }).click(); await expect(review).toContainText('全部应用完成'); await page.keyboard.press('Escape');
|
|
}
|
|
let result, releaseHeld;
|
|
try {
|
|
if (desktop) { await desktop.select(f.session); await resize({ width: 1440, height: 1000 }); }
|
|
else { await page.addInitScript(({ projectId, sessionId }) => { localStorage.setItem('awb-project', projectId); localStorage.setItem('awb-session', sessionId); }, { projectId: f.project.id, sessionId: f.session.id }); await page.goto(host.url); await page.waitForLoadState('networkidle'); }
|
|
await openWorkers();
|
|
await panel().getByLabel('执行方式', { exact: true }).selectOption('deepseek');
|
|
const rootField = panel().getByLabel('Harness 安装目录', { exact: true }); await rootField.fill(path.join(f.root, 'missing-installation'));
|
|
await panel().getByRole('button', { name: '检测受控安装', exact: true }).click(); await expect(panel()).toContainText('安装尚不可用');
|
|
await expect(panel().getByRole('button', { name: '封存分工计划', exact: true })).toBeDisabled();
|
|
await rootField.fill(installation.root); await panel().getByRole('button', { name: '检测受控安装', exact: true }).click(); await expect(panel()).toContainText('受控安装可用');
|
|
await expect(panel().locator('fieldset')).toHaveCount(1); assert.equal(host.requests.length, 0);
|
|
await panel().getByLabel('父目标', { exact: true }).fill('受控原生引擎与正式审查界面验收');
|
|
await panel().getByLabel('任务名称', { exact: true }).fill('engine-1'); await panel().getByLabel('独立子目标', { exact: true }).fill('准备范围内提案,审批工程检查并保留原运行记录');
|
|
await panel().getByLabel('允许修改的文件或目录', { exact: true }).fill('src/a.mjs');
|
|
await panel().getByRole('button', { name: '封存分工计划', exact: true }).click(); await expect(panel()).toContainText('计划已封存');
|
|
const group = [...f.store.core.collaboration.values()].at(-1), child = group.children[0]; assert.equal(group.children.length, 1); assert.equal(child.engine.controlledSha256, installation.controlledSha256);
|
|
assert.deepEqual(ownerBytes(f), original); mark('desktop engine selector rejects a missing source, pins the actual installation and seals one original scope without model calls');
|
|
await panel().getByRole('button', { name: '创建引擎工作区', exact: true }).click(); await expect(page.getByRole('dialog', { name: '确认创建隔离工作区', exact: true })).toContainText('1 个独立 Git 工作区');
|
|
await page.getByRole('dialog', { name: '确认创建隔离工作区', exact: true }).getByRole('button', { name: '确认创建', exact: true }).click(); await expect.poll(() => group.status, { timeout: 20000 }).toBe('ready');
|
|
await start(); await expect.poll(() => group.status, { timeout: 30000 }).toBe('awaiting_review');
|
|
const first = [...f.store.core.engine_run.values()].find(job => job.taskId === child.taskId); assert.equal(first.status, 'completed', first.error); assert.equal(first.native.cleanup.treeConfirmed, true);
|
|
await expect(panel().locator('.worker-lane')).toHaveCount(1); await expect(panel()).toContainText('DeepSeek Harness'); await screenshot('desktop-controlled-parent');
|
|
mark('actual Harness runs the owned bridge and returns reviewable proposals after confirmed process-tree closure');
|
|
await resize({ width: 390, height: 844 }); await panel().locator('.worker-console-heading').scrollIntoViewIfNeeded();
|
|
const refresh = panel().getByRole('button', { name: '刷新', exact: true });
|
|
const bounds = await refresh.boundingBox(); assert.ok(bounds.width >= 76 && bounds.height >= 42, JSON.stringify(bounds)); assert.equal(await refresh.evaluate(node => getComputedStyle(node).whiteSpace), 'nowrap');
|
|
assert.ok(await page.locator('body').evaluate(node => node.scrollWidth <= 390)); await screenshot('mobile-controlled-parent');
|
|
mark('390px keeps the real single-engine scope, header and a single-line 42px refresh action without horizontal overflow');
|
|
await panel().getByRole('button', { name: '审查子任务改动', exact: true }).click(); await expect.poll(() => page.evaluate(() => localStorage.getItem('awb-session'))).toBe(child.sessionId);
|
|
await openEngine(); await expect(runPanel()).toContainText('运行已核验');
|
|
await runPanel().getByRole('button', { name: '查看原输入', exact: true }).click(); const inputPreview = page.getByRole('dialog', { name: '引擎原执行输入', exact: true });
|
|
await expect(inputPreview).toContainText(first.inputSha256); await expect(inputPreview.getByLabel('引擎运行原记录', { exact: true })).toContainText(child.taskId); await screenshot('mobile-controlled-input');
|
|
await inputPreview.getByRole('button', { name: '下一页', exact: true }).click(); await expect(inputPreview.getByRole('button', { name: '回到首页', exact: true })).toBeEnabled(); await page.keyboard.press('Escape');
|
|
let reportFile;
|
|
if (desktop) {
|
|
reportFile = path.join(directory, 'agent-workbench-engine-run-' + first.id + '.json');
|
|
const download = await desktop.download(reportFile, () => runPanel().getByRole('button', { name: '下载完整报告', exact: true }).click());
|
|
assert.equal(download.suggestedFilename, path.basename(reportFile));
|
|
} else {
|
|
const downloading = page.waitForEvent('download'); await runPanel().getByRole('button', { name: '下载完整报告', exact: true }).click();
|
|
const download = await downloading; reportFile = path.join(directory, download.suggestedFilename()); await download.saveAs(reportFile);
|
|
}
|
|
assert.equal(hash(fs.readFileSync(reportFile)), first.reportSha256);
|
|
mark('mobile child run view shows actual counts and closure, pages original inputs and downloads the exact original report bytes');
|
|
let heldRoute; const latePattern = '**/api/sessions/' + child.sessionId + '/engine-runs/' + first.id + '/report**';
|
|
await page.route(latePattern, async route => { heldRoute = route; await new Promise(resolve => { releaseHeld = resolve; }); expectedRejections.push('owned previous-run 500 response'); await route.fulfill({ status: 500, contentType: 'application/json', body: '{"error":"Owned late engine record error"}' }); });
|
|
await runPanel().getByRole('button', { name: '查看原报告', exact: true }).click(); await expect.poll(() => !!heldRoute).toBe(true); await page.keyboard.press('Escape');
|
|
releaseHeld(); await expect.poll(() => expectedRejections.length).toBe(1); await page.unroute(latePattern); await expect(page.getByText('Owned late engine record error', { exact: true })).toHaveCount(0);
|
|
mark('closing a run view ignores an old delayed record failure and keeps the selected task unchanged');
|
|
await applyOne(); await parentFromChild(); await start(); await expect.poll(() => f.store.session(child.sessionId).status, { timeout: 30000 }).toBe('awaiting_approval');
|
|
await panel().getByRole('button', { name: '查看并审批检查', exact: true }).click(); await expect(page.locator('.approval-card')).toContainText('npm'); await expect(page.locator('.inspector')).toBeHidden(); await screenshot('mobile-controlled-approval'); await page.locator('.approval-card').getByRole('button', { name: '确认执行', exact: true }).click();
|
|
await expect.poll(() => group.status, { timeout: 30000 }).toBe('ready_to_collect'); await resize({ width: 1440, height: 1000 });
|
|
await openEngine(); await expect(runPanel().locator('[data-engine-run]')).toHaveCount(2); await screenshot('desktop-controlled-completed'); await page.keyboard.press('Escape');
|
|
assert.equal(f.store.core.task.get(child.taskId).usage.requests, host.requests.length); assert.equal(f.store.core.task.get(child.taskId).usage.totalTokens, host.requests.length * 168);
|
|
mark('explicit child file review and check approval create a new native Run, retain previous evidence and account actual model requests once');
|
|
await parentFromChild(); await panel().getByRole('button', { name: '核对成果', exact: true }).click(); await expect.poll(() => group.status, { timeout: 15000 }).toBe('collected');
|
|
await panel().getByRole('button', { name: '查看原成果记录', exact: true }).click(); await expect(page.getByRole('dialog', { name: '原成果核对记录', exact: true })).toContainText('src/a.mjs'); await page.keyboard.press('Escape');
|
|
await panel().getByRole('button', { name: '结束协作并保留现场', exact: true }).click(); await page.getByRole('dialog', { name: '确认保留现场', exact: true }).getByRole('button', { name: '确认保留现场结束', exact: true }).click(); await expect.poll(() => group.status).toBe('resolved_external');
|
|
mark('parent collection reads the original controlled-worker proof and explicit retention preserves every owner byte');
|
|
const stopped = await host.manager.plan(f.session, { goal: '原生引擎停止与原输入核对', engine: { id: 'deepseek-harness', root: installation.root, controlledSha256: installation.controlledSha256 },
|
|
children: [{ label: 'stopped-engine', goal: '停止之后不执行迟到工具', paths: ['src/stopped.mjs'] }] });
|
|
await (await host.manager.prepare(f.session, stopped.id, stopped.fingerprint)).promise;
|
|
const complete = host.provider.complete; let held = 0;
|
|
host.provider.complete = async (_config, _messages, _project, signal) => new Promise((_resolve, reject) => { held++; signal.addEventListener('abort', () => reject(Object.assign(new Error('Owned held native model stopped'), { name: 'AbortError' })), { once: true }); });
|
|
await panel().getByRole('button', { name: '刷新', exact: true }).click(); await panel().getByLabel('选择协作记录', { exact: true }).selectOption(stopped.id); await start(); await expect.poll(() => held, { timeout: 30000 }).toBe(1);
|
|
await panel().getByRole('button', { name: '停止全部', exact: true }).click(); await expect.poll(() => stopped.status, { timeout: 20000 }).toBe('paused'); host.provider.complete = complete;
|
|
const stoppedJob = [...f.store.core.engine_run.values()].find(job => job.taskId === stopped.children[0].taskId);
|
|
assert.equal(stoppedJob.status, 'cancelled'); assert.equal(stoppedJob.native.cleanup.treeConfirmed, true); assert.equal(stoppedJob.calls.length, 0); assert.equal(stopped.usage.unknownRequests, 1);
|
|
mark('actual native stop closes the owned process tree, preserves one unknown model attempt and dispatches no late tool');
|
|
stopped.status = 'unknown'; stopped.requiresVerification = true;
|
|
const stoppedWorker = f.store.core.worker.get(stopped.children[0].id); stoppedWorker.status = 'unknown'; stoppedWorker.requiresVerification = true; stoppedWorker.ownerPid = null;
|
|
stoppedJob.status = 'unknown'; stoppedJob.requiresVerification = true; stoppedJob.verified = false;
|
|
f.store.save(f.store.session(stoppedWorker.sessionId)); f.store.save(f.session);
|
|
const originalReport = f.store.artifactStore.read(f.store.core.artifact.get(stoppedJob.reportArtifactId)), originalOutcome = structuredClone(stoppedJob.originalOutcome), previousUsage = structuredClone(stopped.usage);
|
|
await resize({ width: 390, height: 844 }); await panel().getByRole('button', { name: '刷新', exact: true }).click(); await expect(panel()).toContainText('结果待核对');
|
|
await panel().getByRole('button', { name: '核对原输入与现场', exact: true }).click(); await expect.poll(() => !!stopped.evidence, { timeout: 15000 }).toBe(true);
|
|
await expect(panel().getByRole('button', { name: '确认核对并继续', exact: true })).toBeDisabled();
|
|
await panel().getByRole('button', { name: '查看核对记录', exact: true }).click(); await expect(page.getByRole('dialog', { name: '原输入与现场核对', exact: true })).toContainText(stoppedJob.inputSha256); await screenshot('mobile-controlled-recovery'); await page.keyboard.press('Escape');
|
|
await panel().getByRole('button', { name: '确认核对并继续', exact: true }).click(); await page.getByRole('dialog', { name: '确认原结果核对', exact: true }).getByRole('button', { name: '确认核对并继续', exact: true }).click(); await expect.poll(() => stopped.requiresVerification).toBe(false);
|
|
assert.equal(stoppedJob.status, 'resolved_external'); assert.deepEqual(stoppedJob.originalOutcome, originalOutcome); assert.equal(f.store.artifactStore.read(f.store.core.artifact.get(stoppedJob.reportArtifactId)), originalReport); assert.deepEqual(stopped.usage, previousUsage); assert.deepEqual(ownerBytes(f), original);
|
|
mark('390px recovery requires reading the owned observation before explicit review and preserves original outcome/report and cumulative budget');
|
|
assert.deepEqual(inputs(), frozen); assert.deepEqual(pageErrors, []); assert.deepEqual(requestFailures, []);
|
|
result = { passed: true, targetVersion: version, testedVersion: version, assertions, screenshots, pageErrors, requestFailures, expectedRejections, installation,
|
|
engineRuns: [...f.store.core.engine_run.values()], groups: [group.id, stopped.id], inputs: frozen, realModelRequests: 0, ...(desktop ? { desktopEvents: desktop.events } : {}),
|
|
scope: 'Actual native Harness and ' + (desktop ? 'Electron with fixed main/preload and own in-process Node host' : 'Edge') + ' with an owned predefined in-memory model fixture. Unknown recovery is a declared fixture transition; actual host-crash behavior is independently tested.' };
|
|
} catch (error) { result = { passed: false, targetVersion: version, testedVersion: version, error: error.stack, assertions, screenshots, pageErrors, requestFailures, realModelRequests: 0 }; await screenshot('controlled-ui-failure').catch(() => {}); throw error; }
|
|
finally {
|
|
releaseHeld?.(); await browser?.close(); await desktop?.close(); await host.runtime.stopAll(); f.store.storage.database.exec('PRAGMA wal_checkpoint(TRUNCATE)');
|
|
fs.cpSync(f.root, path.join(directory, 'original-fixture'), { recursive: true });
|
|
for (const session of f.store.sessions.values()) fs.writeFileSync(path.join(directory, 'session-' + session.id + '.json'), JSON.stringify(await host.api('/sessions/' + session.id + '/export'), null, 2));
|
|
fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify(result, null, 2)); fs.writeFileSync('.qa/' + (desktopMode ? 'desktop-controlled-ui' : 'controlled-ui') + '-location.json', JSON.stringify({ directory, result: path.join(directory, 'result.json') }));
|
|
for (const dispose of disposers.reverse()) await dispose(); console.log('EVIDENCE ' + directory);
|
|
}
|