168 lines
18 KiB
JavaScript
168 lines
18 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, X509Certificate } from 'node:crypto';
|
|
import { chromium, _electron, expect } from 'playwright/test';
|
|
import { fixture, launchWorkbench } from '../tests/helpers.mjs';
|
|
import { desktopExecutableIdentity } from '../src/core/desktop/app-installation.mjs';
|
|
import { projectEnvironment } from '../src/core/process-environment.mjs';
|
|
import { FileResults } from '../src/core/tasks/file-results.mjs';
|
|
import { readText } from '../src/server/files.mjs';
|
|
|
|
if (process.argv.includes('--help')) { console.log('node scripts/verify-file-results-ui.mjs — isolated Edge/Electron existing-file receipts, changed bytes, explicit adoption, no-change delivery and late-response scope. Optional AGENT_WORKBENCH_QA_APP_ROOT. No model requests.'); process.exit(0); }
|
|
if (process.argv.length > 2) throw new Error('Use --help.');
|
|
const appRoot = path.resolve(process.env.AGENT_WORKBENCH_QA_APP_ROOT || '.'), directory = path.resolve('.qa/file-results-ui-' + randomUUID()); fs.mkdirSync(directory, { recursive: true });
|
|
const versionFile = path.join(appRoot, fs.existsSync(path.join(appRoot, 'package.json')) ? 'package.json' : 'MANIFEST.json');
|
|
const version = JSON.parse(fs.readFileSync(versionFile, 'utf8')).version;
|
|
const checks = [], screenshots = [], errors = [];
|
|
const mark = name => { checks.push(name); console.log('PASS ' + name); };
|
|
let stage = 'setup', result;
|
|
const shot = async (page, name) => { const file = path.join(directory, name + '.png'); await page.screenshot({ path: file, scale: 'css' }); screenshots.push(file); };
|
|
const overflow = page => page.evaluate(() => ({ width: innerWidth, document: document.documentElement.scrollWidth })).then(value => assert.ok(value.document <= value.width + 1, JSON.stringify(value)));
|
|
|
|
async function mobileFlow(browser, host, f, htmlPath) {
|
|
const initial = await host.api('/companion'); assert.ok(initial.addresses.length);
|
|
const address = initial.addresses[0].address, reservation = http.createServer(); await new Promise(resolve => reservation.listen(0, address, resolve));
|
|
const port = reservation.address().port; await new Promise(resolve => reservation.close(resolve));
|
|
const gateway = await host.api('/companion/start', { address, port }), target = new URL(gateway.origin);
|
|
const cert = await fetch(host.url + '/api/companion/certificate', { headers: { 'X-Workbench-Token': host.bootstrap.token } }); assert.equal(cert.status, 200);
|
|
const ca = new X509Certificate(Buffer.from(await cert.arrayBuffer())).toString(); assert.equal(new X509Certificate(ca).ca, true);
|
|
const pair = await host.api('/companion/pair', { projectIds: [f.project.id], permissions: { observe: true, approve: false, stop: false }, expiresInHours: 24 });
|
|
let cookie = '';
|
|
const relay = http.createServer((request, response) => {
|
|
const forwarded = https.request(new URL(request.url, target), { method: request.method, ca, servername: '', headers: { Host: target.host, Origin: target.origin, Connection: 'close', ...(cookie ? { Cookie: cookie } : {}), ...(request.headers['content-type'] ? { 'Content-Type': request.headers['content-type'] } : {}) } }, 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', () => { if (!response.headersSent) response.writeHead(503); response.end(); }); request.pipe(forwarded);
|
|
});
|
|
await new Promise(resolve => relay.listen(0, '127.0.0.1', resolve));
|
|
const context = await browser.newContext({ viewport: { width: 390, height: 844 }, isMobile: true, hasTouch: true }), mobile = await context.newPage();
|
|
mobile.on('pageerror', error => errors.push('mobile: ' + error.message));
|
|
try {
|
|
await mobile.goto('http://127.0.0.1:' + relay.address().port + '/#pair=' + new URLSearchParams(new URL(pair.url).hash.slice(1)).get('pair'));
|
|
await mobile.getByLabel('设备名称', { exact: true }).fill('当前成果观察夹具'); await mobile.getByRole('button', { name: '连接这台电脑', exact: true }).click();
|
|
await mobile.locator('.mobile-task-row').filter({ hasText: f.session.title }).click();
|
|
const panel = mobile.getByLabel('当前文件成果', { exact: true }); await expect(panel).toContainText('当前文件已核对');
|
|
const row = (await host.api('/sessions/' + f.session.id + '/file-results')).results[0]; await expect(panel).toContainText(row.files[0].sha256);
|
|
await expect(panel).toContainText(row.taskId.slice(0, 8)); await expect(panel).toContainText(row.runId.slice(0, 8)); await overflow(mobile); await shot(mobile, 'mobile-current-results');
|
|
await expect(mobile.getByRole('button', { name: '按当前成果结束任务', exact: true })).toHaveCount(0);
|
|
mark('mobile 390px: actual paired TLS gateway exposes current Task/Run and file SHA through observation only; no OS trust changes');
|
|
fs.appendFileSync(htmlPath, '<!-- 手机观察期间用户编辑 -->\n'); await mobile.reload();
|
|
await expect(mobile.getByRole('heading', { name: '项目任务', exact: true })).toBeVisible();
|
|
await mobile.locator('.mobile-task-row').filter({ hasText: f.session.title }).click();
|
|
await expect(mobile.getByLabel('当前文件成果', { exact: true })).toContainText('文件或要求已变化'); await overflow(mobile); await shot(mobile, 'mobile-stale-results');
|
|
mark('mobile 390px: authenticated refresh reports stale bytes and retains original SHA without granting file/finish permissions');
|
|
} catch (error) { await shot(mobile, 'mobile-failure').catch(() => {}); fs.writeFileSync(path.join(directory, 'mobile-failure.html'), await mobile.content().catch(() => 'unavailable')); throw error; }
|
|
finally { await context.close(); relay.closeAllConnections(); await new Promise(resolve => relay.close(resolve)); await host.api('/companion/close', {}); }
|
|
}
|
|
|
|
async function surface(kind) {
|
|
const disposers = [], t = { after: dispose => disposers.push(dispose) }, f = fixture(t);
|
|
const htmlPath = path.join(f.projectRoot, 'result.html');
|
|
fs.writeFileSync(htmlPath, '<!doctype html><html lang="zh"><meta charset="UTF-8"><title>已有成果</title><h1>当前已有成果</h1><p>用户保存的内容</p></html>\n');
|
|
const begin = (session, goal, title, forceNewTask = false) => {
|
|
const run = f.store.beginRun(session, goal, false, f.store.config, { forceNewTask }); session.status = 'paused'; session.title = title; f.store.finishRun(session, run, 'delivery_incomplete'); return run;
|
|
};
|
|
const goal = '创建 result.html 文件,已有内容满足时沿用,无需测试';
|
|
const originalRun = begin(f.session, goal, '核对已有HTML');
|
|
const sourceSession = f.store.createSession(f.project.id);
|
|
begin(sourceSession, goal, '显式采用旧成果');
|
|
const source = new FileResults(f.store).record(sourceSession, { files: [{ path: 'result.html', sha256: readText(f.project, 'result.html').sha256 }], reason: '原任务已保存HTML成果,作为后续显式采用的来源。' }, { source: 'user' });
|
|
begin(sourceSession, goal, '显式采用旧成果', true);
|
|
const blocked = f.store.createSession(f.project.id); begin(blocked, '修改 result.html 文件标题,无需测试', '修改要求不能绕过');
|
|
f.store.close();
|
|
const host = await launchWorkbench(t, f.data, {}, { appRoot });
|
|
assert.notEqual(new URL(host.url).port, '4318'); assert.equal(host.bootstrap.config.hasKey, false);
|
|
assert.equal(host.bootstrap.version, version);
|
|
let browser, application, page;
|
|
try {
|
|
if (kind === 'edge') { browser = await chromium.launch({ channel: 'msedge', headless: true }); page = await browser.newPage({ viewport: { width: 1440, height: 1000 } }); await page.goto(host.url); }
|
|
else {
|
|
const executable = desktopExecutableIdentity(appRoot).path, packaged = executable.toLowerCase().endsWith('agentworkbench.exe');
|
|
application = await _electron.launch({ executablePath: executable, args: [...(packaged ? [] : [path.join(appRoot, 'dist-desktop/main.cjs')]), '--app-root', appRoot, '--data-root', f.data, '--node', process.execPath, '--port', new URL(host.url).port, '--hidden-window'], cwd: appRoot, env: projectEnvironment(), timeout: 60000 });
|
|
page = await application.firstWindow(); await (await application.browserWindow(page)).evaluate(window => { window.showInactive(); window.setContentSize(1440, 1000); });
|
|
await expect.poll(() => page.evaluate(() => innerWidth)).toBe(1440);
|
|
}
|
|
page.on('pageerror', error => errors.push(kind + ': ' + error.message));
|
|
await page.locator('.connection.connected').waitFor();
|
|
const panel = () => page.getByLabel('当前文件成果', { exact: true });
|
|
const select = async session => {
|
|
if (!await page.getByLabel('当前项目', { exact: true }).isVisible()) await page.getByRole('button', { name: '展开项目栏', exact: true }).click();
|
|
await page.getByLabel('当前项目', { exact: true }).selectOption(f.project.id);
|
|
await page.locator('.session-list button').filter({ hasText: session.title }).click();
|
|
await page.getByRole('button', { name: '查看任务记录', exact: true }).click(); await expect(panel()).toBeVisible();
|
|
};
|
|
const form = async () => { const details = panel().locator('.file-result-form'); if (!await details.evaluate(node => node.open)) await details.locator('summary').click(); };
|
|
const choose = async () => { await form(); await panel().getByLabel('成果文件', { exact: true }).selectOption('result.html'); await expect(panel().getByLabel('成果文件内容')).toContainText('当前已有成果'); };
|
|
const route = '/sessions/' + f.session.id;
|
|
stage = kind + ': stale-save recovery'; await select(f.session); await choose();
|
|
const reason = '核对已有HTML的标题与完整内容,沿用用户已有成果。'; await panel().getByLabel('成果采用原因').fill(reason);
|
|
fs.appendFileSync(htmlPath, '<!-- 保存前由用户修改 -->\n');
|
|
await panel().getByRole('button', { name: '核对并采用此文件', exact: true }).click();
|
|
await expect(panel().getByRole('status')).toContainText('变化'); await expect(panel().getByLabel('成果采用原因')).toHaveValue(reason);
|
|
assert.equal((await host.api(route + '/file-results')).results.length, 0);
|
|
await panel().getByRole('button', { name: '重新读取当前文件', exact: true }).click(); await expect(panel().getByLabel('成果文件内容')).toContainText('保存前由用户修改');
|
|
await panel().getByRole('button', { name: '核对并采用此文件', exact: true }).click();
|
|
await expect(panel().locator('.file-result-row')).toHaveCount(1); await expect(panel()).toContainText('当前文件已核对');
|
|
mark(kind + ': changed SHA refuses stale save, retains input, rereads actual bytes and records owned receipt'); await shot(page, kind + '-current-results');
|
|
await panel().getByRole('button', { name: '查看原成果凭据', exact: true }).click();
|
|
const modal = page.getByRole('dialog', { name: '执行结果工件', exact: true }); await expect(modal).toContainText('file-result'); await expect(modal).toContainText('SHA256'); await page.keyboard.press('Escape');
|
|
await panel().getByRole('button', { name: '按当前成果结束任务', exact: true }).click();
|
|
await expect(panel().getByRole('status')).toContainText('任务已按当前成果结束');
|
|
const completed = await host.api(route); assert.equal(completed.status, 'completed'); assert.equal(completed.execution.task.delivery.status, 'existing_verified');
|
|
assert.equal(completed.changes.length, 0); assert.equal(completed.execution.checks.length, 0); assert.equal(completed.execution.deliveries.length, 0); assert.equal(completed.execution.task.usage.requests, 0);
|
|
assert.deepEqual(completed.execution.run, originalRun);
|
|
mark(kind + ': current receipt ends no-test task, preserves old failed run and creates no file change/check/commit/model request');
|
|
await page.reload(); await page.locator('.connection.connected').waitFor(); await select(f.session); await expect(panel()).toContainText('结束时采用已有成果');
|
|
fs.appendFileSync(htmlPath, '<!-- 交付后用户继续编辑 -->\n'); await panel().getByRole('button', { name: '刷新成果状态', exact: true }).click(); await expect(panel()).toContainText('需要重新核对');
|
|
await panel().getByRole('button', { name: '重新打开此文件核对', exact: true }).click(); await form(); await expect(panel().getByLabel('成果采用原因')).toHaveValue(reason);
|
|
await expect(panel().getByLabel('成果文件内容')).toContainText('交付后用户继续编辑');
|
|
mark(kind + ': refresh retains receipt and later file edits produce specific stale recovery without discarding reason');
|
|
stage = kind + ': narrow current results';
|
|
if (application) await (await application.browserWindow(page)).evaluate(window => { window.showInactive(); window.setContentSize(390, 844); }); else await page.setViewportSize({ width: 390, height: 844 });
|
|
await expect.poll(() => page.evaluate(() => innerWidth)).toBe(390);
|
|
await page.getByRole('button', { name: '查看任务记录', exact: true }).click(); await expect(panel()).toBeVisible(); await panel().scrollIntoViewIfNeeded(); await overflow(page); await shot(page, kind + '-390-results');
|
|
mark(kind + ': 390px task entry displays readable paths/hashes and actions without page overflow');
|
|
if (application) await (await application.browserWindow(page)).evaluate(window => window.setContentSize(1440, 1000)); else await page.setViewportSize({ width: 1440, height: 1000 });
|
|
await expect.poll(() => page.evaluate(() => innerWidth)).toBe(1440);
|
|
stage = kind + ': explicit adoption'; await select(sourceSession); await choose(); await panel().getByLabel('成果采用原因').fill('按当前要求再次核对完整HTML,显式采用原任务成果。');
|
|
await expect(panel().getByRole('button', { name: '核对并采用此文件', exact: true })).toBeDisabled();
|
|
await panel().getByLabel('成果来源').selectOption(source.result_id); await panel().getByRole('button', { name: '核对并采用此文件', exact: true }).click();
|
|
await expect(panel().locator('.file-result-row')).toHaveCount(1); await expect(panel()).toContainText('采用自 Task');
|
|
const adopted = (await host.api('/sessions/' + sourceSession.id + '/file-results')).results[0]; assert.equal(adopted.files[0].origin.resultId, source.result_id); assert.notEqual(adopted.files[0].origin.sha256, adopted.files[0].sha256);
|
|
mark(kind + ': new task explicitly selects original source, preserving old/current SHA and Task/Run'); await shot(page, kind + '-adopted');
|
|
stage = kind + ': change guard'; await select(blocked); await choose(); await panel().getByLabel('成果采用原因').fill('只核对现有文件,不能替代本次要求的标题修改。');
|
|
await panel().getByRole('button', { name: '核对并采用此文件', exact: true }).click(); await expect(panel().locator('.file-result-row')).toHaveCount(1);
|
|
await panel().getByRole('button', { name: '按当前成果结束任务', exact: true }).click(); await expect(panel().getByRole('status')).toContainText('包含修改');
|
|
assert.equal((await host.api('/sessions/' + blocked.id)).status, 'paused');
|
|
mark(kind + ': unchanged adoption cannot satisfy explicit modification, error retains current task and form');
|
|
stage = kind + ': late response'; await select(f.session); await choose();
|
|
const delayedReason = '延迟回执仍须归属原任务,不能写入后来选择的任务。'; await panel().getByLabel('成果采用原因').fill(delayedReason);
|
|
let entered, release, finished, didEnter = false; const held = new Promise(resolve => { entered = resolve; }), released = new Promise(resolve => { release = resolve; }), responded = new Promise(resolve => { finished = resolve; });
|
|
const routeErrors = [];
|
|
const matcher = '**/api/sessions/' + f.session.id + '/file-results';
|
|
await page.route(matcher, async route => {
|
|
if (route.request().method() !== 'POST') return route.continue();
|
|
try { const response = await route.fetch(); didEnter = true; entered(); await released; await route.fulfill({ response }); }
|
|
catch (error) { routeErrors.push(error.message); entered(); }
|
|
finally { finished(); }
|
|
});
|
|
try {
|
|
await panel().getByRole('button', { name: '核对并采用此文件', exact: true }).click(); await held;
|
|
await select(blocked); release(); await responded; await page.unroute(matcher); assert.deepEqual(routeErrors, []);
|
|
await expect(panel()).not.toContainText(delayedReason); assert.equal((await host.api('/sessions/' + blocked.id)).status, 'paused');
|
|
assert.equal((await host.api(route + '/file-results')).results[0].reason, delayedReason);
|
|
mark(kind + ': delayed successful save remains with original Task after UI selection changes');
|
|
} finally { release(); if (didEnter) await responded; await page.unroute(matcher); }
|
|
if (kind === 'edge') { stage = 'mobile current files'; await mobileFlow(browser, host, f, htmlPath); }
|
|
assert.equal((await host.api('/bootstrap')).config.hasKey, false);
|
|
} catch (error) { if (page) { await shot(page, kind + '-failure').catch(() => {}); fs.writeFileSync(path.join(directory, kind + '-failure.html'), await page.content().catch(() => 'unavailable')); } throw error; }
|
|
finally { await browser?.close(); await application?.close(); for (const dispose of disposers.reverse()) await dispose(); }
|
|
}
|
|
try { await surface('edge'); await surface('electron'); assert.deepEqual(errors, []); result = { success: true, version, appRoot, directory, checks, screenshots, errors, realModelRequests: 0, scope: 'Actual isolated Edge and Electron UI; 390px is desktop narrow validation, not a new iPhone test.' }; }
|
|
catch (error) { result = { success: false, stage, version, appRoot, directory, checks, screenshots, errors, error: error.stack }; process.exitCode = 1; }
|
|
fs.writeFileSync(path.join(directory, 'result.json'), JSON.stringify(result, null, 2)); fs.writeFileSync('.qa/file-results-ui-latest.json', JSON.stringify(result, null, 2)); console.log('EVIDENCE ' + directory); console.log(JSON.stringify(result));
|