import fs from 'node:fs'; import path from 'node:path'; import net from 'node:net'; import assert from 'node:assert/strict'; import { createHash, randomUUID } from 'node:crypto'; import { chromium, _electron } from 'playwright'; import { fixture, compatibleFixture, completion, launchWorkbench, until } from '../tests/helpers.mjs'; import { beginPlanTask } from '../tests/fixtures/task-plan-fixture.mjs'; import { DeliveryReporter } from '../src/core/delivery-report.mjs'; import { HostController } from '../src/core/desktop/host-controller.mjs'; import { desktopExecutableIdentity } from '../src/core/desktop/app-installation.mjs'; import { projectEnvironment } from '../src/core/process-environment.mjs'; const desktop = process.argv.includes('--desktop'), appRoot = path.resolve(process.env.AGENT_WORKBENCH_QA_APP_ROOT || '.'); const directory = path.resolve(process.env.EXPERIENCE_QA_OUTPUT || '.qa/experience-drafts-e', (desktop ? 'electron-' : 'web-') + randomUUID()); fs.mkdirSync(directory, { recursive: true }); const disposers = [], t = { after: fn => disposers.push(fn) }, f = fixture(t, { modernDefaults: true }); const passed = [], screenshots = [], errors = [], contrast = []; let browser, application, controller, host, page, window, result; const model = await compatibleFixture(t, () => completion([], '本机固定HTTP验收完成;未修改工程,不证明经验语义收益。')); f.store.saveConfig({ mode: 'compatible', baseUrl: model.baseUrl, model: 'owned-experience-ui', approvalMode: 'full' }); f.session.title = 'E 经验来源本机验收'; const run = beginPlanTask(f, '核对README,无需测试,不修改文件。'); run.stopReason = 'network_error'; run.usage.unknownRequests = 1; run.usage.estimatedUnknownTokens = 19; f.store.save(f.session); const firstReport = await new DeliveryReporter(f.store).create(f.session); const secondReport = await new DeliveryReporter(f.store).create(f.session); const consumer = f.store.createSession(f.project.id, 'E 请求差异本机验收'); f.store.close(); const save = (name, value) => fs.writeFileSync(path.join(directory, name), JSON.stringify(value, null, 2)); const mark = name => { passed.push(name); console.log('PASS ' + name); }; async function shot(name) { const file = path.join(directory, name + '.png'); await page.screenshot({ path: file, scale: 'css' }); screenshots.push(file); } async function resize(width, height) { 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 }); } const documents = body => body.messages.filter(value => ['user', 'system'].includes(value.role) && value.content.includes('"project_memory":')) .map(value => JSON.parse(value.content.slice(value.content.lastIndexOf('\n') + 1)).project_memory); async function send() { await host.api('/sessions/' + consumer.id + '/messages', { content: '说明本工程已有证据;无需测试,不修改文件。' }); await until(async () => (await host.api('/sessions/' + consumer.id)).status === 'completed', 30000); return documents(model.requests.at(-1).input); } async function openMemory() { await page.getByRole('button', { name: '打开工具箱', exact: true }).click(); const toolbox = page.getByRole('dialog', { name: '工具箱', exact: true }); await toolbox.getByLabel('搜索工作台工具').fill('项目记忆'); await toolbox.getByRole('button', { name: '项目记忆', exact: true }).click(); const modal = page.getByRole('dialog', { name: '项目记忆', exact: true }); await modal.getByLabel('经验来源', { exact: true }).waitFor(); await modal.locator('.memory-summary').waitFor(); return modal; } async function sampleContrast() { const values = await page.locator('.experience-source h4, .experience-source p, .experience-boundary, .experience-scope dt, .experience-scope dd, .experience-stages span').evaluateAll(nodes => { const rgb = text => text.match(/[\d.]+/g)?.map(Number) ?? []; const luminance = color => { const [r, g, b] = color.map(v => { const c = v / 255; return c <= .04045 ? c / 12.92 : ((c + .055) / 1.055) ** 2.4; }); return .2126 * r + .7152 * g + .0722 * b; }; return nodes.filter(node => node.getBoundingClientRect().height > 0).map(node => { const style = getComputedStyle(node), color = rgb(style.color); let parent = node, background; while (parent) { const c = rgb(getComputedStyle(parent).backgroundColor); if (c.length === 3 || c[3] === 1) { background = c; break; } parent = parent.parentElement; } const a = luminance(color), b = luminance(background ?? [255, 255, 255]); return { text: node.textContent.slice(0, 45), foreground: style.color, background, ratio: (Math.max(a, b) + .05) / (Math.min(a, b) + .05) }; }); }); contrast.push(...values); assert.ok(values.length > 5); assert.ok(values.every(row => row.ratio >= 4.5), JSON.stringify(values.filter(row => row.ratio < 4.5))); } try { if (desktop) { const socket = net.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)); assert.notEqual(port, 4318); controller = new HostController({ appRoot, dataRoot: f.data, port, nodePath: process.execPath, timeoutMs: 60000 }); await controller.start(); host = { url: controller.url, bootstrap: await controller.api('/bootstrap'), api: (route, input) => controller.api(route, input === undefined ? {} : { method: 'POST', input }) }; const executable = desktopExecutableIdentity(appRoot).path; application = await _electron.launch({ executablePath: executable, args: [...(executable.toLowerCase().endsWith('agentworkbench.exe') ? [] : [path.join(appRoot, 'dist-desktop/main.cjs')]), '--app-root', appRoot, '--data-root', f.data, '--node', process.execPath, '--port', String(port), '--hidden-window'], cwd: appRoot, env: projectEnvironment(), timeout: 60000 }); page = await application.firstWindow(); window = await application.browserWindow(page); await window.evaluate(win => { win.showInactive(); win.setContentSize(1440, 1080); }); await until(() => page.evaluate(() => innerWidth === 1440)); assert.equal(await page.evaluate(() => typeof globalThis.process), 'undefined'); } else { host = await launchWorkbench(t, f.data, {}, { appRoot }); assert.notEqual(new URL(host.url).port, '4318'); browser = await chromium.launch({ channel: 'msedge', headless: true }); page = await browser.newPage({ viewport: { width: 1440, height: 1080 } }); await page.goto(host.url); } page.on('pageerror', error => errors.push(error.message)); await page.locator('.connection.connected').waitFor(); await host.api('/config', { mode: 'compatible', baseUrl: model.baseUrl, model: 'owned-experience-ui', apiKey: 'owned-experience-ui-key', approvalMode: 'full' }); await page.getByLabel('当前项目').selectOption(f.project.id); await page.locator('.session-list button').filter({ hasText: f.session.title }).click(); let modal = await openMemory(); const card = () => modal.locator('.memory-experience').filter({ hasText: '原失败仍需核对' }); await modal.getByLabel('经验来源', { exact: true }).selectOption('delivery_report:' + firstReport.artifactId); await modal.getByRole('button', { name: '生成经验草稿', exact: true }).click(); let draft = modal.locator('.memory-experience').first(); await until(async () => (await draft.textContent()).includes('草稿 · 未采用')); await draft.getByText('来源与修订(1)', { exact: true }).click(); await draft.getByText('所选原始事实', { exact: true }).click(); assert.match(await draft.locator('pre').innerText(), /network_error/); assert.match(await draft.locator('pre').innerText(), /unknownRequests/); assert.equal(model.requests.length, 0); await shot('source-draft'); assert.equal((await send()).length, 0); mark('explicit original report creates a default-off draft preserving network failure and unknown usage; actual HTTP omits it'); await draft.getByRole('button', { name: '修订草稿', exact: true }).click(); await draft.getByLabel('经验标题', { exact: true }).fill('原失败仍需核对'); await draft.getByLabel('经验内容', { exact: true }).fill('原报告记录网络失败,原因未知;后续成功不能解释原失败。'); await draft.getByLabel('适用范围', { exact: true }).fill('当前工程、所选报告与相同源码'); await draft.getByLabel('失效条件', { exact: true }).fill('源码或证据变化,以及新反例'); const failSave = route => route.fulfill({ status: 503, contentType: 'application/json', body: JSON.stringify({ error: '本机验收:修订保存暂不可用' }) }); await page.route('**/memories/experiences/*/edit', failSave); await draft.getByRole('button', { name: '保存草稿修订', exact: true }).click(); await draft.getByRole('alert').waitFor(); assert.equal(await draft.getByLabel('经验内容', { exact: true }).inputValue(), '原报告记录网络失败,原因未知;后续成功不能解释原失败。'); await shot('save-error'); await page.unroute('**/memories/experiences/*/edit', failSave); await draft.getByRole('button', { name: '保存草稿修订', exact: true }).click(); draft = card(); await until(async () => !(await draft.getByRole('button', { name: '保存草稿修订', exact: true }).count())); await draft.getByRole('button', { name: '采用到本项目', exact: true }).click(); await draft.getByRole('button', { name: '撤销采用', exact: true }).waitFor(); const [sent] = await send(); assert.equal(sent.entries[0].title, '原失败仍需核对'); assert.equal(sent.entries[0].experience.adoption, 'active'); assert.equal(sent.entries[0].experience.engineering.conclusionVerified, false); const envelopeMessage = model.requests.at(-1).input.messages.find(value => value.content?.includes('"memory_artifact":')); const envelope = JSON.parse(envelopeMessage.content.slice(envelopeMessage.content.lastIndexOf('\n') + 1)); const sentBefore = await host.api('/artifacts/' + envelope.memory_artifact.id + '?session=' + consumer.id); save('sent-memory-before-withdrawal.json', sentBefore); const originalSent = await host.api('/sessions/' + consumer.id + '/export'); save('adopted-consumer.json', originalSent); await shot('adopted'); mark('failed editing retains input; retry saves an immutable revision, explicit adoption reaches actual HTTP without certifying a conclusion'); for (const palette of ['green', 'blue', 'purple']) { await page.evaluate(value => localStorage.setItem('orven-palette', value), palette); await page.reload(); await page.locator('.connection.connected').waitFor(); modal = await openMemory(); assert.equal(await page.locator('html').getAttribute('data-palette'), palette); await sampleContrast(); await shot('palette-' + palette); } await resize(390, 844); await card().getByText('来源与修订(3)', { exact: true }).click(); await card().getByRole('button', { name: '撤销采用', exact: true }).scrollIntoViewIfNeeded(); assert.ok(await page.locator('body').evaluate(node => node.scrollWidth <= 390)); assert.ok(await modal.evaluate(node => node.scrollWidth <= node.clientWidth + 1)); await shot('mobile-adopted'); await card().getByRole('button', { name: '撤销采用', exact: true }).click(); await card().getByRole('button', { name: '采用到本项目', exact: true }).waitFor(); assert.equal((await send()).length, 0); const view = await host.api('/projects/' + f.project.id + '/memories'); assert.equal(view.memories[0].experience.revisions.length, 4); const sentAfter = await host.api('/artifacts/' + envelope.memory_artifact.id + '?session=' + consumer.id); assert.deepEqual(sentAfter, sentBefore); save('sent-memory-after-withdrawal.json', sentAfter); mark('three palettes retain readable experience content; 390px adoption and withdrawal fit and future HTTP omits revoked content'); await card().getByRole('button', { name: '记录反例', exact: true }).click(); await card().getByLabel('新反例', { exact: true }).fill('同条件后来仍发生网络错误;旧原因尚未验证。'); await card().getByRole('button', { name: '记录反例并停用', exact: true }).click(); await until(async () => (await card().textContent()).includes('已过期')); assert.match(await card().innerText(), /工程验证未确认/); await shot('mobile-counterexample'); await resize(1440, 1080); await modal.getByLabel('经验来源', { exact: true }).selectOption('delivery_report:' + secondReport.artifactId); await modal.getByRole('button', { name: '生成经验草稿', exact: true }).click(); const second = modal.locator('.memory-experience').filter({ hasText: '交付报告经验草稿' }).first(); await second.getByRole('button', { name: '采用到本项目', exact: true }).click(); await second.getByRole('button', { name: '撤销采用', exact: true }).waitFor(); fs.appendFileSync(path.join(f.projectRoot, 'README.md'), '\nOwned source changed after adoption.\n'); await modal.getByRole('button', { name: '重新核对', exact: true }).click(); await until(async () => (await second.textContent()).includes('工程源码指纹已变化')); assert.equal((await send()).length, 0); await shot('source-expired'); mark('a new counterexample and a real source edit invalidate adoption while retaining the original facts and revisions'); const editor = modal.locator('.memory-editor'); await editor.getByLabel('标题', { exact: true }).fill('手动项目约定'); await editor.getByLabel('内容', { exact: true }).fill('手写记忆沿用原入口和来源语义。'); await editor.getByRole('button', { name: '保存记忆', exact: true }).click(); await modal.locator('.memory-card').filter({ hasText: '手动项目约定' }).waitFor(); await page.reload(); await page.locator('.connection.connected').waitFor(); modal = await openMemory(); assert.equal((await host.api('/projects/' + f.project.id + '/memories')).summary.current, 1); assert.equal((await host.api('/model-center/selection')).config.capacityMode, 'passthrough'); assert.equal((await host.api('/model-center/selection')).config.approvalMode, 'full'); assert.equal(model.requests.length, 4); assert.deepEqual(errors, []); mark('manual memory remains compatible; refresh preserves drafts, withdrawal, expiry, full access and passthrough settings'); await modal.getByRole('button', { name: '关闭窗口', exact: true }).click(); await page.locator('.session-list button').filter({ hasText: consumer.title }).click(); modal = await openMemory(); await modal.getByRole('button', { name: '前往来源会话管理', exact: true }).first().click(); await modal.getByLabel('经验来源', { exact: true }).selectOption('delivery_report:' + firstReport.artifactId); assert.equal((await host.api('/projects/' + f.project.id + '/memories')).summary.current, 1); mark('shared project memory links back to its actual source session and cannot be edited from another session'); const beforeRestart = await host.api('/projects/' + f.project.id + '/memories'); if (application) { await application.close(); application = null; if (controller && await controller.health()) await controller.stop(); controller = null; } else { await browser.close(); browser = null; await host.close(); } const restarted = await launchWorkbench(t, f.data, {}, { appRoot }); assert.deepEqual((await restarted.api('/projects/' + f.project.id + '/memories')).memories, beforeRestart.memories); assert.equal((await restarted.api('/model-center/selection')).config.approvalMode, 'full'); await restarted.close(); assert.equal(model.requests.length, 4); mark('an actual host restart preserves all memory texts, source identities, revision chains, expiry and chosen access mode'); result = { success: true, appRoot, actualElectron: desktop, passed, screenshots, errors, contrast, realModelRequests: 0, localFixtureRequests: model.requests.length, officialServiceTouched: false, serverSha256: createHash('sha256').update(fs.readFileSync(path.join(appRoot, 'dist-server/server.mjs'))).digest('hex') }; } catch (error) { if (page) try { await shot('failure'); fs.writeFileSync(path.join(directory, 'failure-dom.txt'), await page.locator('body').innerText()); } catch {} result = { success: false, appRoot, actualElectron: desktop, passed, screenshots, errors, contrast, error: error.stack, realModelRequests: 0, localFixtureRequests: model.requests.length, officialServiceTouched: false }; } finally { save('local-requests.json', model.requests.map(({ url, input }) => ({ url, input }))); if (application) await application.close(); if (browser) await browser.close(); if (controller && await controller.health()) await controller.stop(); for (const dispose of disposers.reverse()) await dispose(); save('result.json', result); } console.log(JSON.stringify({ directory, ...result }, null, 2)); if (!result.success) process.exitCode = 1;