173 lines
19 KiB
JavaScript
173 lines
19 KiB
JavaScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import http from 'node:http';
|
|
import https from 'node:https';
|
|
import assert from 'node:assert/strict';
|
|
import { randomUUID, createHash } from 'node:crypto';
|
|
import { X509Certificate } from 'node:crypto';
|
|
import { chromium, _electron } from 'playwright';
|
|
import { expect } from 'playwright/test';
|
|
import { Store } from '../src/server/store.mjs';
|
|
import { Provider } from '../src/server/provider.mjs';
|
|
import { launchWorkbench, until } from '../tests/helpers.mjs';
|
|
import { HostController } from '../src/core/desktop/host-controller.mjs';
|
|
import { desktopLaunchCommand } from './desktop-launch.mjs';
|
|
import { projectEnvironment } from '../src/core/process-environment.mjs';
|
|
import { openWorkbenchTool } from './ui-navigation.mjs';
|
|
import { verifyPlanEvidence } from '../src/core/context/request-plan.mjs';
|
|
|
|
if (process.argv.includes('--help')) { console.log('node scripts/verify-mnemo-ui.mjs --app-root <independent candidate> --connection <new synthetic connection.private.json> --output <new evidence directory> [--desktop]'); process.exit(0); }
|
|
const args = process.argv.slice(2), opt = name => { assert.ok(args.includes(name)); return args[args.indexOf(name) + 1]; };
|
|
const appRoot = fs.realpathSync.native(opt('--app-root')), output = path.resolve(opt('--output')), desktop = args.includes('--desktop');
|
|
const c = JSON.parse(fs.readFileSync(path.resolve(opt('--connection')))); assert.equal(c.syntheticOnly, true); assert.equal(new URL(c.origin).hostname, '127.0.0.1');
|
|
fs.mkdirSync(output); const data = path.join(output, 'data'), projectRoot = path.join(output, 'project'); fs.mkdirSync(projectRoot);
|
|
fs.writeFileSync(path.join(projectRoot, 'README.md'), '# Newly owned UI fixture\n');
|
|
const store = new Store(data), project = store.addProject(projectRoot), session = store.createSession(project.id, '记忆桥原生界面验收');
|
|
const provider = new Provider(); provider.setKey(''); const hub = provider.modelHub(store);
|
|
const connection = hub.upsert({ name: '新本机合成接收器', model: 'mnemo-joint-ui', baseUrl: c.providerBaseUrl + '/v1', requiresKey: true }); hub.select({ type: 'connection', id: connection.id });
|
|
// Only the new controlled provider capability is re-entered explicitly below.
|
|
store.close();
|
|
const checks = [], screenshots = [], errors = [], blocked = [], disposers = [], t = { after: callback => disposers.push(callback) };
|
|
let host, controller, browser, application, page, window, relay, mobileContext, result;
|
|
const sha = bytes => createHash('sha256').update(bytes).digest('hex');
|
|
const mark = text => { checks.push(text); console.log('PASS ' + text); };
|
|
const secretValues = [c.fixtureToken];
|
|
const clean = input => { let text = JSON.stringify(input); for (const token of secretValues) if (token) text = text.split(token).join('[SYNTHETIC_TOKEN]'); return JSON.parse(text); };
|
|
async function control(route, body) {
|
|
const r = await fetch(c.origin + '/__fixture/control/' + route, { method: body === undefined ? 'GET' : 'POST', headers: { Authorization: 'Bearer ' + c.fixtureToken, 'Content-Type': 'application/json' },
|
|
...(body === undefined ? {} : { body: JSON.stringify(body) }), redirect: 'error' }); const value = await r.json(); assert.ok(r.ok); return value;
|
|
}
|
|
async function resize(width, height = 960) {
|
|
if (window) { await window.evaluate((win, size) => win.setContentSize(size.width, size.height), { width, height }); await until(() => page.evaluate(w => innerWidth === w, width)); }
|
|
else await page.setViewportSize({ width, height });
|
|
}
|
|
async function shot(name, anchor, target = page) {
|
|
if (anchor) await anchor.scrollIntoViewIfNeeded(); await target.evaluate(() => new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve))));
|
|
assert.ok(await target.evaluate(() => document.documentElement.scrollWidth <= innerWidth + 1), 'Page must fit the viewport');
|
|
const filename = path.join(output, name + '.png'); let capture = 'playwright';
|
|
if (window && target === page) {
|
|
const picture = await window.evaluate(async win => { const [width, height] = win.getContentSize(); const image = await win.capturePage({ x: 0, y: 0, width, height }, { stayHidden: true, stayAwake: true });
|
|
return { data: image.toPNG().toString('base64'), hidden: !win.isVisible(), empty: image.isEmpty(), size: image.getSize() }; });
|
|
assert.equal(picture.hidden, true); assert.equal(picture.empty, false); fs.writeFileSync(filename, Buffer.from(picture.data, 'base64')); capture = 'native-hidden-electron';
|
|
} else await target.screenshot({ path: filename, scale: 'css' });
|
|
screenshots.push({ file: path.basename(filename), sha256: sha(fs.readFileSync(filename)), capture, width: await target.evaluate(() => innerWidth) });
|
|
}
|
|
async function memoryPanel() { await openWorkbenchTool(page, '项目记忆'); const dialog = page.getByRole('dialog', { name: '项目记忆', exact: true }); await expect(dialog).toBeVisible(); return dialog; }
|
|
async function execute(label) {
|
|
await page.keyboard.press('Escape'); const before = await control('observations');
|
|
await page.getByRole('textbox', { name: '任务内容', exact: true }).fill(label);
|
|
await page.getByRole('button', { name: '发送任务', exact: true }).click();
|
|
await until(async () => { const s = await host.api('/sessions/' + session.id); return s.runId && s.status === 'completed' && (s.messages ?? []).some(m => m.role === 'user' && m.content === label); }, 45000);
|
|
const current = await host.api('/sessions/' + session.id), attempts = await host.api('/model-center/attempts?limit=100'), after = await control('observations');
|
|
assert.equal(after.physical.length, before.physical.length + 1); const physical = after.physical.at(-1), attempt = attempts.items.find(a => a.upstreamResponse?.requestId === physical.physicalRequestId);
|
|
assert.ok(attempt); assert.equal(attempt.runId, current.runId); assert.equal(attempt.requestSha256, physical.bodySha256);
|
|
fs.copyFileSync(path.join(c.directory, physical.bodyFile), path.join(output, physical.bodyFile));
|
|
return { session: current, physical, attempt };
|
|
}
|
|
try {
|
|
if (desktop) {
|
|
const socket = http.createServer(); await new Promise(resolve => socket.listen(0, '127.0.0.1', resolve)); const port = socket.address().port; await new Promise(resolve => socket.close(resolve));
|
|
controller = new HostController({ appRoot, dataRoot: data, port, nodePath: process.execPath, timeoutMs: 60000 }); await controller.start();
|
|
host = { url: controller.url, api: (route, input) => controller.api(route, input === undefined ? {} : { method: 'POST', input }) };
|
|
application = await _electron.launch({ ...desktopLaunchCommand(appRoot, ['--app-root', appRoot, '--data-root', data, '--node', process.execPath, '--port', String(port), '--hidden-window']),
|
|
cwd: appRoot, env: projectEnvironment({ AGENT_WORKBENCH_API_KEY: '' }), timeout: 60000 });
|
|
page = await application.firstWindow(); window = await application.browserWindow(page);
|
|
} else { host = await launchWorkbench(t, data, {}, { appRoot }); browser = await chromium.launch({ channel: 'msedge', headless: true }); page = await browser.newPage(); }
|
|
assert.notEqual(new URL(host.url).port, '4318');
|
|
page.setDefaultTimeout(15000); page.on('pageerror', cause => errors.push(cause.message));
|
|
await page.route('**/*', route => { const url = new URL(route.request().url()); if (url.origin === new URL(host.url).origin || ['data:', 'blob:'].includes(url.protocol)) return route.continue(); blocked.push(url.origin); return route.abort(); });
|
|
const saved = (await host.api('/model-center')).connections.find(v => v.id === connection.id);
|
|
await host.api('/model-center/connections', { ...saved, apiKey: c.providerToken });
|
|
await page.addInitScript(({ projectId, sessionId }) => { localStorage.setItem('awb-project', projectId); localStorage.setItem('awb-session', sessionId); }, { projectId: project.id, sessionId: session.id });
|
|
await resize(1280); await page.goto(host.url); await page.waitForLoadState('networkidle');
|
|
let dialog = await memoryPanel(); const editor = dialog.locator('.memory-editor');
|
|
await editor.getByLabel('标题', { exact: true }).fill('引航合成记忆'); await editor.getByLabel('内容', { exact: true }).fill('引航合成记忆:这是新本机来源的完整中文正文。\n保留换行和来源修订,不代表真实工程已交付。');
|
|
await editor.getByRole('button', { name: '保存记忆', exact: true }).click(); await expect(dialog.locator('.memory-card')).toContainText('引航合成记忆');
|
|
const bridge = dialog.getByRole('region', { name: 'Mnemo 项目记忆桥', exact: true }); await bridge.locator('summary').first().click();
|
|
await expect(bridge).toContainText('默认关闭'); await shot('bridge-default-off-1280', bridge);
|
|
const view = await host.api('/projects/' + project.id + '/memory-bridge'), auth = await control('bind', { producerId: view.producerId, projectId: project.id, projectName: '新界面合成项目', requestKey: randomUUID(), confirmed: true });
|
|
secretValues.push(auth.token);
|
|
await bridge.getByLabel('Mnemo 根地址', { exact: true }).fill(c.origin); await bridge.getByLabel('项目绑定 ID', { exact: true }).fill(auth.binding.id);
|
|
await bridge.getByLabel('本次项目凭据', { exact: true }).fill(auth.token); await bridge.getByLabel('启用本项目记忆桥', { exact: true }).check();
|
|
await bridge.getByRole('button', { name: '保存连接', exact: true }).click(); await expect(bridge).toContainText('本次已连接'); await expect(bridge.getByLabel('本次项目凭据', { exact: true })).toHaveValue('');
|
|
await bridge.getByRole('button', { name: /引航合成记忆/ }).click();
|
|
const review = bridge.locator('.mnemo-review'), fullRevision = review.locator('details').filter({ hasText: '查看完整来源修订与摘要' });
|
|
await fullRevision.locator('summary').click();
|
|
const displayed = JSON.parse(await fullRevision.locator('pre').innerText()), currentView = await host.api('/projects/' + project.id + '/memory-bridge');
|
|
assert.deepEqual(displayed, currentView.sources[0].review); assert.equal(sha(JSON.stringify(displayed)), currentView.sources[0].confirmationDigest);
|
|
await shot('full-source-revision-1280', fullRevision); await fullRevision.locator('summary').click();
|
|
await expect(bridge.getByRole('button', { name: '确认并共享', exact: true })).toBeDisabled();
|
|
await bridge.getByLabel('我已核对以上完整来源修订', { exact: true }).check(); await bridge.getByRole('button', { name: '提交候选', exact: true }).click();
|
|
await expect(bridge.locator('.mnemo-sources')).toContainText('候选 · 未确认'); await shot('candidate-1280', bridge.locator('.mnemo-review'));
|
|
await bridge.getByLabel('我已核对以上完整来源修订', { exact: true }).check(); await bridge.getByRole('button', { name: '确认并共享', exact: true }).click();
|
|
await expect(bridge.locator('.mnemo-sources')).toContainText('已确认 · 修订 2');
|
|
await page.reload(); await page.waitForLoadState('networkidle'); dialog = await memoryPanel();
|
|
const reloaded = dialog.getByRole('region', { name: 'Mnemo 项目记忆桥', exact: true }); await reloaded.locator('summary').first().click(); await expect(reloaded).toContainText('本次已连接');
|
|
await resize(390, 844); await reloaded.getByRole('button', { name: /引航合成记忆/ }).click(); await shot('confirmed-source-390', reloaded.locator('.mnemo-review'));
|
|
mark('Real form saves a default-off connection, explicit candidate and exact full confirmation; refresh and 390px retain revision without showing credentials');
|
|
await resize(1280); const first = await execute('引航合成记忆');
|
|
await page.reload(); await page.waitForLoadState('networkidle'); await page.getByRole('button', { name: '查看任务记录', exact: true }).click();
|
|
const context = page.getByLabel('请求上下文', { exact: true }), receipt = context.locator('.mnemo-plan-receipt');
|
|
await expect(receipt).toBeVisible(); await receipt.locator('summary').click(); await expect(receipt).toContainText('已收到响应'); await expect(receipt).toContainText('来源序号 2');
|
|
await shot('physical-receipt-1280', receipt); await receipt.getByRole('button', { name: '查看整条选入记忆', exact: true }).click();
|
|
const artifact = page.getByRole('dialog', { name: '执行结果工件', exact: true }); await expect(artifact.locator('pre')).toContainText('mnemo-authorized-project-memory'); await expect(artifact.locator('pre')).toContainText('引航合成记忆');
|
|
await shot('selected-artifact-1280', artifact); await page.keyboard.press('Escape'); await resize(390, 844);
|
|
// Responsive layout closes the inspector; reopen its actual narrow entry.
|
|
await page.getByRole('button', { name: '查看任务记录', exact: true }).click(); await expect(receipt).toBeVisible();
|
|
await shot('physical-receipt-390', receipt); await page.keyboard.press('Escape');
|
|
mark('Same physical request ID/SHA and Task/Run are bound to full selected source and per-attempt validation; desktop and narrow receipts open original artifacts');
|
|
|
|
// Actual companion gateway over certificate-verified HTTPS, with a loopback
|
|
// browser relay; no OS certificate installation or TLS bypass.
|
|
const status = await host.api('/companion'); assert.ok(status.addresses.length, 'Owned private interface required for companion');
|
|
const reservation = http.createServer(); await new Promise(resolve => reservation.listen(0, status.addresses[0].address, resolve)); const port = reservation.address().port; await new Promise(resolve => reservation.close(resolve));
|
|
const gateway = await host.api('/companion/start', { address: status.addresses[0].address, port });
|
|
const bootstrap = await (await fetch(host.url + '/api/bootstrap')).json();
|
|
const certResponse = await fetch(host.url + '/api/companion/certificate', { headers: { 'X-Workbench-Token': bootstrap.token } }); assert.ok(certResponse.ok);
|
|
const ca = new X509Certificate(Buffer.from(await certResponse.arrayBuffer())).toString();
|
|
const pairing = await host.api('/companion/pair', { projectIds: [project.id], permissions: { observe: true, approve: false, stop: false }, expiresInHours: 1 });
|
|
const target = new URL(gateway.origin); let cookie = '';
|
|
relay = http.createServer((request, response) => {
|
|
const forwarded = https.request(new URL(request.url, target), { method: request.method, headers: { Host: target.host, Origin: target.origin, Connection: 'close', ...(cookie ? { Cookie: cookie } : {}),
|
|
...(request.headers['content-type'] ? { 'Content-Type': request.headers['content-type'] } : {}) }, ca, servername: '' }, incoming => {
|
|
if (incoming.headers['set-cookie']?.length) cookie = incoming.headers['set-cookie'][0].split(';')[0]; const headers = { ...incoming.headers }; delete headers['set-cookie']; response.writeHead(incoming.statusCode, headers); incoming.pipe(response);
|
|
}); forwarded.on('error', cause => { errors.push('Synthetic TLS relay: ' + cause.code); if (!response.headersSent) response.writeHead(503); response.end(); }); request.pipe(forwarded);
|
|
});
|
|
await new Promise(resolve => relay.listen(0, '127.0.0.1', resolve));
|
|
if (!browser) browser = await chromium.launch({ channel: 'msedge', headless: true });
|
|
mobileContext = await browser.newContext({ viewport: { width: 390, height: 844 }, isMobile: true, hasTouch: true }); const mobile = await mobileContext.newPage(); mobile.on('pageerror', cause => errors.push(cause.message));
|
|
await mobile.goto('http://127.0.0.1:' + relay.address().port + '/#pair=' + new URLSearchParams(new URL(pairing.url).hash.slice(1)).get('pair'));
|
|
await mobile.getByLabel('设备名称', { exact: true }).fill('本机记忆回执窄屏'); await mobile.getByRole('button', { name: '连接这台电脑', exact: true }).click();
|
|
await expect(mobile.getByRole('heading', { name: '项目任务', exact: true })).toBeVisible(); await mobile.getByRole('button').filter({ hasText: '记忆桥原生界面验收' }).click();
|
|
const mobileReceipt = mobile.locator('.mnemo-plan-receipt'); await expect(mobileReceipt).toBeVisible(); await mobileReceipt.locator('summary').click();
|
|
await expect(mobileReceipt).toContainText(first.session.runId); await expect(mobileReceipt).toContainText('来源序号 2'); await expect(mobileReceipt).toContainText('已收到响应');
|
|
await shot('companion-same-run-390', mobileReceipt, mobile);
|
|
assert.equal((await mobile.locator('body').innerText()).includes(auth.token), false); assert.equal((await mobile.locator('body').innerText()).includes(projectRoot), false);
|
|
mark('Actual project-authorized companion projection shows the same Run/source/revision/physical outcome through certificate-verified HTTPS; physical iPhone remains untested');
|
|
|
|
await page.keyboard.press('Escape'); dialog = await memoryPanel(); const lastBridge = dialog.getByRole('region', { name: 'Mnemo 项目记忆桥', exact: true }); await lastBridge.locator('summary').first().click();
|
|
await control('revoke', { bindingId: auth.binding.id }); await resize(1280); const second = await execute('引航合成记忆撤权后的新请求');
|
|
assert.equal(fs.readFileSync(path.join(output, second.physical.bodyFile), 'utf8').includes('"mnemo_project_memory"'), false);
|
|
await page.reload(); await page.waitForLoadState('networkidle'); await page.getByRole('button', { name: '查看任务记录', exact: true }).click();
|
|
const invalid = page.getByLabel('请求上下文', { exact: true }).locator('.mnemo-plan-receipt'); await invalid.locator('summary').click(); await expect(invalid).toContainText('服务拒绝');
|
|
await shot('unavailable-original-task-1280', invalid);
|
|
await mobile.reload(); await expect(mobile.getByRole('heading', { name: '项目任务', exact: true })).toBeVisible(); await mobile.getByRole('button').filter({ hasText: '记忆桥原生界面验收' }).click();
|
|
await mobile.locator('.mnemo-plan-receipt').last().locator('summary').click(); await expect(mobile.locator('.mnemo-plan-receipt').last()).toContainText('0 条选入'); await shot('companion-unavailable-390', mobile.locator('.mnemo-plan-receipt').last(), mobile);
|
|
mark('Revoked bridge is omitted with a visible reason while the original task still completes once; previous receipt stays available');
|
|
fs.writeFileSync(path.join(output, 'requests.json'), JSON.stringify(clean({ first, second }), null, 2));
|
|
assert.equal(errors.length, 0); assert.equal(blocked.length, 0);
|
|
result = { success: true, appRoot, actualElectron: desktop, checks, screenshots, errors, blocked, projectId: project.id, sessionId: session.id, runIds: [first.session.runId, second.session.runId],
|
|
realModelCalls: 0, realKeysRead: 0, productionAccessed: false, companionHttpsVerified: true, physicalIPhoneVerified: false };
|
|
} catch (cause) {
|
|
if (page) { await shot('failure').catch(() => {}); fs.writeFileSync(path.join(output, 'failure-dom.txt'), await page.locator('body').innerText().catch(() => '')); }
|
|
result = { success: false, appRoot, actualElectron: desktop, checks, screenshots, errors, blocked, error: cause.stack, realModelCalls: 0, productionAccessed: false };
|
|
} finally {
|
|
if (mobileContext) await mobileContext.close(); if (relay) await new Promise(resolve => { relay.closeAllConnections(); relay.close(resolve); });
|
|
if (application) await application.close(); if (browser) await browser.close();
|
|
await host?.api('/companion/close', {}).catch(() => {});
|
|
if (controller && await controller.health()) await controller.stop(); await host?.close?.();
|
|
for (const dispose of disposers.reverse()) await dispose();
|
|
fs.writeFileSync(path.join(output, 'result.json'), JSON.stringify(clean(result), null, 2));
|
|
}
|
|
console.log(JSON.stringify({ output, success: result.success, error: result.error })); if (!result.success) process.exitCode = 1;
|