import { releaseEvidencePrefix } from './qa-evidence.mjs'; import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import { randomUUID, createHash } from 'node:crypto'; import { chromium, expect } from 'playwright/test'; import { Store } from '../src/server/store.mjs'; import { launchWorkbench } from '../tests/helpers.mjs'; import { createResearchSamples, ownedAsar } from './research-fixtures.mjs'; const proofPrefix = releaseEvidencePrefix(JSON.parse(fs.readFileSync('package.json', 'utf8')).version); if (process.argv.includes('--help')) { console.log('node scripts/verify-research-ui.mjs — independent owned projects, real REA static operations and a declared stdio stop fixture'); process.exit(0); } const root = path.resolve('.qa', 'research-ui-' + randomUUID()), data = path.join(root, 'data'), projectRoot = path.join(root, 'project'); fs.mkdirSync(projectRoot, { recursive: true }); fs.mkdirSync('.qa/screenshots', { recursive: true }); const projectBytes = Buffer.from('# Owned REA UI project\n'); fs.writeFileSync(path.join(projectRoot, 'README.md'), projectBytes); const store = new Store(data), project = store.addProject(projectRoot), original = store.createSession(project.id); original.title = 'REA工件与原证据验收'; store.save(original); store.close(); const samples = createResearchSamples(path.join(root, 'samples')), disposers = [], t = { after: fn => disposers.push(fn) }; let host = await launchWorkbench(t, data), route = '/sessions/' + original.id, deliberateNavigation = false; const browser = await chromium.launch({ channel: 'msedge', headless: true }), page = await browser.newPage({ viewport: { width: 1440, height: 1050 } }); const passed = [], pageErrors = [], requestFailures = [], expectedRequestFailures = [], screenshots = [], analysisRequests = []; const sha = bytes => createHash('sha256').update(bytes).digest('hex'), state = () => host.api(route); const panel = () => page.getByRole('dialog', { name: 'REA 工件研究', exact: true }); const view = () => page.getByRole('dialog', { name: '原 Evidence 字段', exact: true }); const jobCard = job => panel().locator('[data-research-job="' + job.id + '"]'); const mark = text => { passed.push(text); console.log('PASS ' + text); }; page.on('pageerror', error => pageErrors.push(error.message)); page.on('requestfailed', request => { const failure = { url: request.url(), error: request.failure()?.errorText }; (deliberateNavigation && failure.error?.includes('ERR_ABORTED') ? expectedRequestFailures : requestFailures).push(failure); }); page.on('request', request => { if (request.method() === 'POST' && request.url().endsWith('/research/jobs')) analysisRequests.push({ url: request.url(), input: request.postDataJSON() }); }); async function screenshot(name) { const filename = '.qa/screenshots/' + proofPrefix + '-' + name + '.png'; await page.screenshot({ path: filename, scale: 'css' }); screenshots.push(filename); } async function openProject(projectId = project.id, title = original.title) { await page.goto(host.url); await page.waitForLoadState('networkidle'); await page.getByLabel('当前项目').selectOption(projectId); await page.locator('.session-list button').filter({ hasText: title }).click(); await page.getByRole('button', { name: 'REA 工件研究', exact: true }).click(); await expect(panel()).toBeVisible(); } async function register(filename, label) { const before = (await state()).execution.researchTargets.length; await panel().getByLabel('REA工件路径').fill(filename); await panel().getByLabel('REA工件名称').fill(label); await panel().getByRole('button', { name: '登记工件', exact: true }).click(); await expect.poll(async () => (await state()).execution.researchTargets.length).toBe(before + 1); const target = (await state()).execution.researchTargets.at(-1); await expect(panel().locator('.rea-target').filter({ hasText: label })).toBeVisible(); return target; } async function analyze(target, operation) { const before = (await state()).execution.researchJobs.length, card = panel().locator('.rea-target').filter({ hasText: target.label }); await card.getByRole('combobox').selectOption(operation); await card.getByRole('button', { name: '分析', exact: true }).click(); await expect.poll(async () => (await state()).execution.researchJobs.length).toBe(before + 1); await expect.poll(async () => (await state()).execution.researchJobs.at(-1).finishedAt, { timeout: 45000 }).toBeTruthy(); const job = (await state()).execution.researchJobs.at(-1); await expect(jobCard(job)).toContainText(job.status === 'completed' ? '原证据已核验' : '工件版本变化'); return job; } async function downloaded(job) { const pending = page.waitForEvent('download'); await jobCard(job).getByRole('button', { name: '完整证据', exact: true }).click(); const download = await pending, filename = path.join(root, job.id + '-bundle.json'); await download.saveAs(filename); const bytes = fs.readFileSync(filename); assert.equal(sha(bytes), job.bundle.documentSha256); const bundle = JSON.parse(bytes.toString('utf8')), evidence = bundle.records.find(value => value.evidence_id === job.evidenceId); assert.equal(evidence.subject.digest.sha256, job.targetSha256); return evidence; } let result; try { assert.notEqual(new URL(host.url).port, '4318'); assert.equal(host.bootstrap.config.hasKey, false); await openProject(); await expect(panel()).toContainText('已找到 REA 6.8.0'); mark('independent desktop research entry detects the installed fixed REA version'); const target = await register(samples.asar, '自有 Electron 代理工件'); assert.equal(target.sha256, sha(fs.readFileSync(samples.asar))); assert.equal((await state()).execution.task.usage.requests, 0); assert.equal(target.sessionId, original.id); assert.equal(target.projectId, project.id); mark('UI registration seals the artifact identity in the current project and creates no model request'); const first = await analyze(target, 'javascript'); assert.equal(first.status, 'completed'); assert.equal(first.cleanup.rootExited, true); await expect(jobCard(first).getByLabel('已核验原证据来源')).toContainText(target.sha256.slice(0, 12)); const firstEvidence = await downloaded(first); assert.equal(firstEvidence.normalized_result.statistics.parsed_javascript_files, 2); mark('actual REA ASAR analysis renders the verified source chain and downloads the exact original Bundle'); await jobCard(first).getByRole('button', { name: '查看原字段', exact: true }).click(); await expect(view()).toBeVisible(); await view().getByRole('button', { name: 'statistics', exact: true }).click(); await expect(view().locator('.rea-pointer > code')).toHaveText('/normalized_result/statistics'); await expect.poll(async () => JSON.parse(await view().getByLabel('原Evidence内容').innerText()).parsed_javascript_files).toBe(2); await page.keyboard.press('Escape'); await expect(view()).toHaveCount(0); await expect(panel()).toBeVisible(); await jobCard(first).getByRole('button', { name: '查看原字段', exact: true }).click(); await expect(view()).toBeVisible(); const firstPage = await host.api(route + '/research/jobs/' + first.id + '/evidence?offset=0&limit=4000'); assert.equal(await view().getByLabel('原Evidence内容').innerText(), firstPage.content); if (firstPage.truncated) { await view().getByRole('button', { name: '下一页', exact: true }).click(); const secondPage = await host.api(route + '/research/jobs/' + first.id + '/evidence?offset=' + firstPage.next_offset + '&limit=4000'); await expect(view().getByLabel('原Evidence内容')).toHaveText(secondPage.content); await view().getByRole('button', { name: '回到首页', exact: true }).click(); await expect(view().getByLabel('原Evidence内容')).toHaveText(firstPage.content); } await view().getByRole('button', { name: '关闭窗口', exact: true }).click(); await jobCard(first).getByRole('button', { name: '原限制', exact: true }).click(); await expect(view().locator('.rea-pointer > code')).toHaveText('/limitations'); await expect.poll(async () => JSON.parse(await view().getByLabel('原Evidence内容').innerText())).toEqual(firstEvidence.limitations ?? []); await view().getByRole('button', { name: '关闭窗口', exact: true }).click(); mark('original field selection, paging and limitations retain exact bytes; Escape closes only the top dialog'); for (const [name, title] of [['原响应', 'REA 原响应'], ['连接记录', 'REA 连接记录']]) { await jobCard(first).getByRole('button', { name, exact: true }).click(); const dialog = page.getByRole('dialog', { name: title, exact: true }); await expect(dialog).toBeVisible(); await expect(dialog.locator('pre')).not.toHaveText(''); await dialog.getByRole('button', { name: '关闭窗口', exact: true }).click(); } const reused = await analyze(target, 'javascript'); assert.equal(reused.reusedFromJobId, first.id); assert.equal(reused.traceArtifactId, undefined); await expect(jobCard(reused)).toContainText('本次没有再次调用 REA'); await downloaded(reused); mark('raw responses and connection records are readable; repeat analysis reuses the verified original without dispatch'); const pe = await register(samples.pe, '自有 PE 导入与资源工件'); const imports = await analyze(pe, 'pe-imports'), importsEvidence = await downloaded(imports); assert.match(JSON.stringify(importsEvidence.normalized_result), /KERNEL32\.dll/); assert.match(JSON.stringify(importsEvidence.normalized_result), /ExitProcess/); const resources = await analyze(pe, 'pe-resources'), resourceEvidence = await downloaded(resources); assert.match(JSON.stringify(resourceEvidence.normalized_result), new RegExp(sha(Buffer.from('REA-DATA')))); assert.equal(imports.cleanup.rootExited, true); assert.equal(resources.cleanup.rootExited, true); await screenshot('research-desktop'); mark('UI PE operation selection performs actual import and resource analysis with independently verified target hashes'); fs.writeFileSync(samples.asar, ownedAsar({ 'package.json': '{"name":"owned-new-version","main":"main.js"}', 'main.js': 'exports.contract = { observed: "new static version" };' })); const stale = await analyze(target, 'javascript'); assert.equal(stale.status, 'stale'); assert.equal(stale.analysisDispatched, undefined); await expect(jobCard(stale).getByLabel('已核验原证据来源')).toHaveCount(0); await jobCard(stale).getByRole('button', { name: '重新分析', exact: true }).click(); const retry = page.getByRole('dialog', { name: '重新运行静态分析', exact: true }); await expect(retry).toBeVisible(); await retry.getByRole('button', { name: '返回原记录', exact: true }).click(); const updated = await register(samples.asar, '自有代理新版本'); assert.notEqual(updated.sha256, target.sha256); const updatedJob = await analyze(updated, 'javascript'); assert.equal(updatedJob.status, 'completed'); assert.ok((await state()).execution.researchJobs.some(value => value.id === stale.id && value.status === 'stale')); mark('changed artifact versions refuse dispatch, retain the stale record and require a separately registered version'); await page.setViewportSize({ width: 390, height: 844 }); await jobCard(updatedJob).scrollIntoViewIfNeeded(); assert.equal(await panel().evaluate(element => element.scrollWidth <= element.clientWidth), true); await screenshot('research-mobile'); await jobCard(updatedJob).getByRole('button', { name: '查看原字段', exact: true }).click(); await expect(view()).toBeVisible(); assert.equal(await view().evaluate(element => element.scrollWidth <= element.clientWidth), true); await screenshot('research-evidence-mobile'); await page.keyboard.press('Escape'); await expect(panel()).toBeVisible(); mark('390px research, long identities and original evidence dialogs fit the viewport and retain usable close controls'); const recordBeforeRestart = (await state()).execution.researchJobs.map(value => ({ id: value.id, status: value.status, evidenceId: value.evidenceId })); deliberateNavigation = true; await page.goto('about:blank'); await host.close(); host = await launchWorkbench(t, data); await page.setViewportSize({ width: 1440, height: 1050 }); await openProject(); deliberateNavigation = false; assert.deepEqual((await state()).execution.researchJobs.map(value => ({ id: value.id, status: value.status, evidenceId: value.evidenceId })), recordBeforeRestart); await expect(jobCard(updatedJob)).toContainText('原证据已核验'); await downloaded(updatedJob); mark('a real host restart preserves exact target/job identities and serves sealed Evidence without producer replay'); let releaseView; const gate = new Promise(resolve => { releaseView = resolve; }); await page.route('**/research/jobs/' + updatedJob.id + '/evidence?*', async intercepted => { const response = await intercepted.fetch(); await gate; await intercepted.fulfill({ response }); }); await jobCard(updatedJob).getByRole('button', { name: '查看原字段', exact: true }).click(); await panel().getByRole('button', { name: '关闭窗口', exact: true }).click(); releaseView(); await page.unrouteAll({ behavior: 'wait' }); await expect(view()).toHaveCount(0); await expect(panel()).toHaveCount(0); mark('a delayed evidence response cannot reopen a closed research panel or change another task'); assert.deepEqual(fs.readFileSync(path.join(projectRoot, 'README.md')), projectBytes); assert.equal(fs.existsSync(samples.marker), false); assert.equal((await state()).execution.task.usage.requests, 0); // Declared fault fixture: no claim that this second MCP process is the actual REA implementation. const faultRoot = path.join(root, 'stdio-stop-fixture'); fs.mkdirSync(path.join(faultRoot, 'scripts'), { recursive: true }); fs.writeFileSync(path.join(faultRoot, 'package.json'), '{"name":"rea-agents","version":"6.8.0","type":"module"}'); fs.writeFileSync(path.join(faultRoot, 'scripts/rea.mjs'), 'import readline from "node:readline"; readline.createInterface({input:process.stdin}).on("line",()=>{});'); const faultData = path.join(root, 'fault-data'), faultStore = new Store(faultData), faultProject = faultStore.addProject(projectRoot), faultSession = faultStore.createSession(faultProject.id); faultSession.title = '声明的stdio停止故障验收'; faultStore.save(faultSession); faultStore.close(); deliberateNavigation = true; await page.goto('about:blank'); await host.close(); host = await launchWorkbench(t, faultData, { AGENT_WORKBENCH_REA_PACKAGE_ROOT: faultRoot }); route = '/sessions/' + faultSession.id; await openProject(faultProject.id, faultSession.title); deliberateNavigation = false; const faultTarget = await register(samples.asar, '停止握手的自有工件'), before = (await state()).execution.researchJobs.length; await panel().locator('.rea-target').filter({ hasText: faultTarget.label }).getByRole('button', { name: '分析', exact: true }).click(); await expect.poll(async () => (await state()).execution.researchJobs.at(-1)?.connection?.pid).toBeTruthy(); await expect(panel().getByRole('button', { name: '停止分析', exact: true })).toBeEnabled(); await panel().getByRole('button', { name: '停止分析', exact: true }).click(); await expect.poll(async () => (await state()).execution.researchJobs.at(-1)?.status).toBe('cancelled'); await expect.poll(async () => (await state()).execution.researchJobs.at(-1)?.finishedAt).toBeTruthy(); const stopped = (await state()).execution.researchJobs.at(-1); assert.equal(stopped.cleanup.rootExited, true); await expect(jobCard(stopped)).toContainText('已停止'); await jobCard(stopped).getByRole('button', { name: '重新分析', exact: true }).click(); await page.getByRole('dialog', { name: '重新运行静态分析', exact: true }).getByRole('button', { name: '确认重新分析', exact: true }).click(); await expect.poll(async () => (await state()).execution.researchJobs.length).toBe(before + 2); await expect.poll(async () => (await state()).execution.researchJobs.at(-1)?.connection?.pid).toBeTruthy(); await expect(panel().getByRole('button', { name: '停止分析', exact: true })).toBeEnabled(); await panel().getByRole('button', { name: '停止分析', exact: true }).click(); await expect.poll(async () => (await state()).execution.researchJobs.at(-1).status).toBe('cancelled'); await expect.poll(async () => (await state()).execution.researchJobs.at(-1)?.finishedAt).toBeTruthy(); assert.equal((await state()).execution.researchJobs.find(value => value.id === stopped.id).status, 'cancelled'); assert.equal((await state()).execution.task.usage.requests, 0); await screenshot('research-stop-desktop'); mark('declared owned stdio fault fixture stops its root process and explicit retry adds a new record without changing history'); assert.equal(pageErrors.length, 0); assert.equal(requestFailures.length, 0); result = { success: true, version: host.bootstrap.version, at: new Date().toISOString(), browser: browser.version(), passed, pageErrors, requestFailures, expectedRequestFailures, root, actualRea: { version: '6.8.0', operations: ['javascript', 'pe-imports', 'pe-resources'], jobs: recordBeforeRestart, targetExecution: false, modelRequests: 0 }, stopScope: 'explicitly declared private stdio handshake fault fixture', screenshots, analysisRequests, scope: 'Independent ports/data and self-owned inert artifacts. No production service, configured-model request or real key read/copied.' }; } catch (error) { await screenshot('research-ui-failure').catch(() => {}); fs.writeFileSync(path.join(root, 'failure-dom.html'), await page.content().catch(() => '')); result = { success: false, version: host.bootstrap.version, at: new Date().toISOString(), error: error.stack, root, passed, pageErrors, requestFailures, expectedRequestFailures, screenshots }; process.exitCode = 1; } finally { deliberateNavigation = true; await browser.close(); for (const dispose of disposers.reverse()) await dispose(); } fs.writeFileSync(path.join(root, 'result.json'), JSON.stringify(result, null, 2) + '\n'); fs.writeFileSync('docs/' + proofPrefix + '-research-ui-validation.json', JSON.stringify(result, null, 2) + '\n'); console.log(JSON.stringify({ success: result.success, version: result.version, groups: passed.length, root, pageErrors, requestFailures, error: result.error }, null, 2));