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 } from 'playwright'; import { Store } from '../src/server/store.mjs'; import { launchWorkbench, until } from '../tests/helpers.mjs'; import { createEngineFixture } from './engine-fixtures.mjs'; import { resolveDeepSeekInstallation } from '../src/core/engines/deepseek-installation.mjs'; if (process.argv.includes('--help')) { console.log('node scripts/verify-engines-ui.mjs — isolated browser flows, declared stop fixture and actual installed SDK'); process.exit(0); } const root = path.resolve('.qa', 'engines-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 engine UI validation project\n'); fs.writeFileSync(path.join(projectRoot, 'README.md'), projectBytes); const store = new Store(data), project = store.addProject(projectRoot), session = store.createSession(project.id), other = store.createSession(project.id); session.title = '引擎连接与原记录验收'; other.title = '引擎归属对照'; store.save(session); store.save(other); store.close(); const fixtureRoot = createEngineFixture(path.join(root, '本地 SDK #')), native = resolveDeepSeekInstallation(); const disposers = [], t = { after: fn => disposers.push(fn) }; let host = await launchWorkbench(t, data, { AGENT_WORKBENCH_DEEPSEEK_ROOT: fixtureRoot }), selectedRoute = '/sessions/' + session.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 = [], starts = []; const sha = value => createHash('sha256').update(value).digest('hex'), state = () => host.api(selectedRoute); const panel = () => page.getByRole('dialog', { name: '本地代理引擎', exact: true }); const view = () => page.getByRole('dialog', { name: '引擎原检查记录', exact: true }); const card = job => panel().locator('[data-engine-job="' + job.id + '"]'); const textContains = async (locator, text) => { await locator.waitFor({ state: 'visible' }); await until(async () => (await locator.innerText()).includes(text)); }; const enabled = async locator => { await locator.waitFor({ state: 'visible' }); await until(() => locator.isEnabled()); }; 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('/engines/jobs')) starts.push(request.postDataJSON()); }); async function screenshot(name) { const directory = path.join(root, 'screenshots'); fs.mkdirSync(directory, { recursive: true }); const filename = path.join(directory, 'engines-' + name + '.png'); await page.screenshot({ path: filename, scale: 'css' }); screenshots.push(filename); } async function open(title = session.title) { deliberateNavigation = true; await page.goto(host.url); await page.waitForLoadState('networkidle'); deliberateNavigation = false; if (!await page.getByLabel('当前项目').isVisible()) await page.getByRole('button', { name: '展开项目栏', exact: true }).click(); await page.getByLabel('当前项目').selectOption(project.id); await page.locator('.session-list button').filter({ hasText: title }).click(); await page.getByRole('button', { name: '本地代理引擎', exact: true }).click(); await panel().waitFor({ state: 'visible' }); await textContains(panel(), '已找到 0.2.1-rc.2'); } async function detect(installationRoot) { await panel().getByLabel('DeepSeek安装目录').fill(installationRoot); await textContains(panel(), '目录已改变'); assert.equal(await panel().getByRole('button', { name: '核验连接', exact: true }).isEnabled(), false); await panel().getByRole('button', { name: '检测安装', exact: true }).click(); await enabled(panel().getByRole('button', { name: '核验连接', exact: true })); } async function waitJob(before) { await until(async () => (await state()).execution.engineJobs.length === before + 1); await until(async () => (await state()).execution.engineJobs.at(-1).reportArtifactId, 20000); const job = (await state()).execution.engineJobs.at(-1); await textContains(card(job), job.status === 'completed' ? '连接通过' : '已停止'); return job; } let result; try { assert.notEqual(new URL(host.url).port, '4318'); assert.equal(host.bootstrap.config.hasKey, false); await open(); assert.equal((await state()).execution.task, null); await textContains(panel(), '本次检查不发送模型任务'); mark('independent desktop engine entry detects the declared installation and clearly separates connection from project execution'); await panel().getByLabel('DeepSeek安装目录').fill(path.join(root, '不存在的安装')); await panel().getByRole('button', { name: '检测安装', exact: true }).click(); await textContains(panel(), '本地引擎关键输入不完整'); assert.equal(await panel().getByRole('button', { name: '核验连接', exact: true }).isEnabled(), false); assert.equal((await state()).execution.engineJobs.length, 0); await detect(fixtureRoot); mark('missing installations and edited directories cannot launch a check until detection succeeds'); await panel().getByRole('button', { name: '核验连接', exact: true }).click(); const first = await waitJob(0); assert.equal(first.taskId, (await state()).execution.task.id); assert.equal(first.reportSummary.modelRequests, 0); assert.equal(first.reportSummary.rootExited, true); await textContains(card(first).getByLabel('引擎实际检查依据'), first.installation.identitySha256.slice(0, 12)); assert.ok(starts[0].identitySha256); assert.equal(starts[0].identitySha256, first.installation.identitySha256); mark('UI launch seals the selected installation into the current Task/Run and renders actual startup and process-close facts'); await card(first).getByRole('button', { name: '查看原记录', exact: true }).click(); await view().waitFor({ state: 'visible' }); const firstPage = await host.api(selectedRoute + '/engines/jobs/' + first.id + '/report?limit=4000'); await until(async () => (await view().getByLabel('引擎原记录内容').innerText()) === firstPage.content); assert.equal(firstPage.truncated, true); await view().getByRole('button', { name: '下一页', exact: true }).click(); const secondPage = await host.api(selectedRoute + '/engines/jobs/' + first.id + '/report?offset=4000&limit=4000'); await until(async () => (await view().getByLabel('引擎原记录内容').innerText()) === secondPage.content); await view().getByRole('button', { name: '回到首页', exact: true }).click(); await textContains(view(), first.reportSha256); const pendingDownload = page.waitForEvent('download'); await view().getByRole('button', { name: '下载完整记录', exact: true }).click(); const download = await pendingDownload, filename = path.join(root, 'downloaded-report.json'); await download.saveAs(filename); assert.equal(sha(fs.readFileSync(filename)), first.reportSha256); assert.equal(JSON.parse(fs.readFileSync(filename, 'utf8')).report.responses.length, 6); await page.keyboard.press('Escape'); await view().waitFor({ state: 'hidden' }); assert.equal(await panel().isVisible(), true); mark('original record paging, full download SHA256 and nested-dialog keyboard close preserve the exact evidence'); await card(first).getByRole('button', { name: '重新核验', exact: true }).click(); const retry = page.getByRole('dialog', { name: '重新核验引擎连接', exact: true }); await retry.waitFor({ state: 'visible' }); assert.equal((await state()).execution.engineJobs.length, 1); await retry.getByRole('button', { name: '开始新的核验', exact: true }).click(); const second = await waitJob(1); assert.notEqual(second.pid, first.pid); assert.notEqual(second.id, first.id); assert.equal((await state()).execution.engineJobs.find(job => job.id === first.id).reportSha256, first.reportSha256); mark('an explicit new check creates a different process and record while retaining the original check unchanged'); fs.writeFileSync(path.join(fixtureRoot, 'fixture-options.json'), JSON.stringify({ mode: 'hang-init' })); await panel().getByRole('button', { name: '核验连接', exact: true }).click(); await until(async () => (await state()).execution.engineJobs.at(-1).status === 'initializing'); await enabled(panel().getByRole('button', { name: '停止检查', exact: true })); await panel().getByRole('button', { name: '停止检查', exact: true }).click(); const stopped = await waitJob(2); assert.equal(stopped.status, 'cancelled'); assert.equal(stopped.reportSummary.rootExited, true); assert.equal(fs.readFileSync(path.join(fixtureRoot, 'wire.jsonl'), 'utf8').includes('notifications/cancelled'), false); fs.writeFileSync(path.join(fixtureRoot, 'fixture-options.json'), '{}'); await card(stopped).getByRole('button', { name: '重新核验', exact: true }).click(); await page.getByRole('dialog', { name: '重新核验引擎连接', exact: true }).getByRole('button', { name: '开始新的核验', exact: true }).click(); const resumed = await waitJob(3); assert.equal(resumed.status, 'completed'); mark('declared pending-initialize fixture stops its owned process and explicit retry preserves the stopped result'); assert.equal(native.status, 'available', native.message); await detect(native.root); await panel().getByRole('button', { name: '核验连接', exact: true }).click(); const nativeJob = await waitJob(4); assert.equal(nativeJob.status, 'completed'); assert.equal(nativeJob.installation.root, native.root); assert.equal(nativeJob.reportSummary.observedResponses, 6); assert.equal(nativeJob.reportSummary.modelRequests, 0); assert.equal(nativeJob.reportSummary.rootExited, true); await screenshot('native-desktop'); mark('the same product UI verifies the actual installed DeepSeek SDK without a prompt or configured-model call'); await page.setViewportSize({ width: 390, height: 844 }); await textContains(card(nativeJob), '连接通过'); assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth), true); await card(nativeJob).getByRole('button', { name: '查看原记录', exact: true }).click(); await view().waitFor({ state: 'visible' }); await textContains(view(), nativeJob.reportSha256); assert.equal(await page.evaluate(() => document.documentElement.scrollWidth <= innerWidth), true); await screenshot('record-mobile'); await page.keyboard.press('Escape'); await view().waitFor({ state: 'hidden' }); await panel().getByLabel('DeepSeek安装目录').scrollIntoViewIfNeeded(); await screenshot('native-mobile'); mark('390px installation paths, source chain, original record, paging and keyboard close remain readable without page overflow'); await page.setViewportSize({ width: 1440, height: 1050 }); let held = false, release; const delay = new Promise(resolve => { release = resolve; }); const pattern = '**/api/engines/deepseek/availability*'; await page.route(pattern, async route => { held = true; await delay; await route.fulfill({ status: 500, contentType: 'application/json', body: JSON.stringify({ error: 'owned-late-engine-error-must-stay-in-original-view' }) }); }); await panel().getByRole('button', { name: '检测安装', exact: true }).click(); await until(() => held); await panel().getByRole('button', { name: '关闭窗口', exact: true }).click(); await panel().waitFor({ state: 'hidden' }); if (!await page.locator('.session-list').isVisible()) await page.getByRole('button', { name: '展开项目栏', exact: true }).click(); await page.locator('.session-list button').filter({ hasText: other.title }).click(); selectedRoute = '/sessions/' + other.id; const lateResponse = page.waitForResponse(response => response.url().includes('/engines/deepseek/availability') && response.status() === 500); release(); await lateResponse; await page.unroute(pattern); await page.getByRole('button', { name: '本地代理引擎', exact: true }).click(); await panel().waitFor({ state: 'visible' }); await textContains(panel(), '已找到 0.2.1-rc.2'); await textContains(panel(), '先验证这个安装能否连接'); assert.equal((await state()).execution.engineJobs.length, 0); assert.equal(await page.getByText('owned-late-engine-error-must-stay-in-original-view').count(), 0); mark('late detection failure from a closed panel cannot appear in a different task or reveal its prior engine records'); const wireBefore = fs.readFileSync(path.join(fixtureRoot, 'wire.jsonl')), saved = await host.api('/sessions/' + session.id); deliberateNavigation = true; await host.close(); host = await launchWorkbench(t, data, { AGENT_WORKBENCH_DEEPSEEK_ROOT: fixtureRoot }); selectedRoute = '/sessions/' + session.id; await open(); assert.deepEqual((await state()).execution.engineJobs, saved.execution.engineJobs); assert.deepEqual(fs.readFileSync(path.join(fixtureRoot, 'wire.jsonl')), wireBefore); await card(nativeJob).getByRole('button', { name: '查看原记录', exact: true }).click(); await view().waitFor({ state: 'visible' }); await textContains(view(), nativeJob.reportSha256); assert.deepEqual(fs.readFileSync(path.join(projectRoot, 'README.md')), projectBytes); assert.equal((await state()).execution.task.usage.requests, 0); mark('actual host restart preserves all native and declared-fixture records with no protocol replay or project modification'); assert.equal(pageErrors.length, 0); assert.equal(requestFailures.length, 0); result = { success: true, version: host.bootstrap.version, testedAt: new Date().toISOString(), root, browser: browser.version(), passed, pageErrors, requestFailures, expectedRequestFailures, screenshots, starts, nativeJob, actualSdk: { version: native.version, identitySha256: nativeJob.installation.identitySha256, nativeMethods: 6, modelRequests: 0, rootExited: true }, stopScope: 'declared private SDK pending-initialize fixture', scope: 'Independent service and data. Original 4318 Provider, tasks and credentials were not accessed.' }; } catch (error) { await screenshot('initial-failure').catch(() => {}); fs.writeFileSync(path.join(root, 'failure-dom.html'), await page.content().catch(() => '')); result = { success: false, version: host.bootstrap.version, testedAt: new Date().toISOString(), root, passed, pageErrors, requestFailures, expectedRequestFailures, screenshots, error: error.stack }; 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(result.version === '1.9.0' ? 'docs/engines-ui-validation.json' : `docs/${releaseEvidencePrefix(result.version)}-engines-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 }));