import assert from 'node:assert/strict'; import fs from 'node:fs'; import path from 'node:path'; import { randomUUID, createHash } from 'node:crypto'; import { spawn } from 'node:child_process'; const release = JSON.parse(fs.readFileSync('releases/latest.json', 'utf8')); const appRoot = release.portableRoot; const manifest = JSON.parse(fs.readFileSync(path.join(appRoot, 'MANIFEST.json'), 'utf8')); for (const file of manifest.files) { const absolute = path.resolve(appRoot, file.path); assert.ok(absolute.startsWith(appRoot + path.sep)); const bytes = fs.readFileSync(absolute); assert.equal(bytes.length, file.bytes); assert.equal(createHash('sha256').update(bytes).digest('hex'), file.sha256, file.path); } const data = path.resolve('.qa', 'release-' + randomUUID()); fs.mkdirSync(data, { recursive: true }); const env = { ...process.env, AGENT_WORKBENCH_DATA_DIR: data, AGENT_WORKBENCH_PORT: '4320' }; delete env.AGENT_WORKBENCH_API_KEY; for (const name of Object.keys(env)) if (name.toLowerCase() === 'path') delete env[name]; const windowsRoot = process.env.SystemRoot || 'C:\\Windows'; env.PATH = [windowsRoot, path.join(windowsRoot, 'System32'), path.join(windowsRoot, 'System32', 'WindowsPowerShell', 'v1.0')].join(path.delimiter); function runPowerShell(script, extra = []) { return new Promise((resolve, reject) => { const child = spawn('powershell.exe', ['-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', path.join(appRoot, 'scripts', script), '-Port', '4320', ...extra], { windowsHide: true, env, stdio: ['ignore', 'pipe', 'pipe'] }); let output = '', errors = ''; child.stdout.on('data', bytes => { output += bytes.toString('utf8'); }); child.stderr.on('data', bytes => { errors += bytes.toString('utf8'); }); const timeout = setTimeout(() => { child.kill(); reject(new Error('Launcher did not exit in time.')); }, 20000); child.on('error', error => { clearTimeout(timeout); reject(error); }); // Windows descendants can inherit pipe handles after the launcher exits. // Wait for the launcher itself, then close this test's readers. child.on('exit', code => { clearTimeout(timeout); child.stdout.destroy(); child.stderr.destroy(); if (code === 0) resolve(output); else reject(new Error('Launcher failed: ' + errors)); }); }); } const launch = () => runPowerShell('Start-Workbench.ps1', ['-NoBrowser']); const stop = () => runPowerShell('Stop-Workbench.ps1'); const url = 'http://127.0.0.1:4320'; const until = async predicate => { const deadline = Date.now() + 10000; while (!(await predicate())) { if (Date.now() > deadline) throw new Error('Release state timeout'); await new Promise(resolve => setTimeout(resolve, 50)); } }; let started = false; try { console.log((await launch()).trim()); started = true; const bootstrap = await (await fetch(url + '/api/bootstrap')).json(); const api = async (route, input) => { const response = await fetch(url + '/api' + route, { method: input === undefined ? 'GET' : 'POST', headers: { 'Content-Type': 'application/json', 'X-Workbench-Token': bootstrap.token }, ...(input === undefined ? {} : { body: JSON.stringify(input) }) }); const result = await response.json(); assert.ok(response.ok, result.error); return result; }; assert.equal(bootstrap.version, manifest.version); assert.equal(bootstrap.config.mode, 'demo'); const firstInfo = JSON.parse(fs.readFileSync(path.join(data, 'service-info.json'), 'utf8')); console.log((await launch()).trim()); assert.equal(JSON.parse(fs.readFileSync(path.join(data, 'service-info.json'), 'utf8')).pid, firstInfo.pid); const example = bootstrap.projects.find(project => project.id === bootstrap.exampleProjectId); assert.ok(example.root.toLowerCase().startsWith(appRoot.toLowerCase() + path.sep)); const session = await api('/sessions', { projectId: example.id }); await api('/sessions/' + session.id + '/messages', { content: '便携包工具与随附 Node 验证' }); let current; await until(async () => { current = await api('/sessions/' + session.id); return current.status === 'awaiting_approval'; }); assert.equal(current.approvals[0].command, 'node --version'); assert.equal(fs.existsSync(path.join(example.root, 'WORKBENCH-NOTES.md')), false); await api('/sessions/' + session.id + '/approvals/' + current.approvals[0].id, { allow: true }); await until(async () => { current = await api('/sessions/' + session.id); return current.status === 'needs_review'; }); const command = current.events.find(event => event.tool === 'run_command'); assert.equal(command.phase, 'completed'); assert.match(command.output, new RegExp(manifest.nodeVersion.replaceAll('.', '\\.'))); for (const proposal of current.changes.filter(change => change.status === 'pending')) await api('/sessions/' + session.id + '/changes/' + proposal.id + '/reject', {}); const planned = await api('/sessions/' + session.id + '/check-plans', { names: ['test'] }); const plan = planned.execution.plans.at(-1); assert.ok(plan.checks.some(check => check.name === 'test')); const accepted = await api('/sessions/' + session.id + '/check-jobs', { planId: plan.id }); const jobId = accepted.execution.checks.at(-1).id; let checked; await until(async () => { checked = await api('/sessions/' + session.id + '/check-jobs/' + jobId); return !['queued', 'running'].includes(checked.status); }); assert.equal(checked.status, 'passed'); assert.equal(checked.results[0].tests.total, 4); assert.equal(checked.results[0].tests.passed, 4); const checkLog = await api('/artifacts/' + checked.results[0].logArtifactId + '?session=' + session.id); assert.match(checkLog.content, /tests 4/); assert.ok(fs.existsSync(path.join(appRoot, 'runtime/node_modules/npm/bin/npm-cli.js'))); assert.equal((await api('/research')).length, 4); const page = await fetch(url); assert.equal(page.status, 200); assert.match(await page.text(), /Agent Workbench/); console.log((await stop()).trim()); started = false; await until(async () => { try { await fetch(url + '/api/health'); return false; } catch { return true; } }); const result = { success: true, date: new Date().toISOString(), appRoot, version: manifest.version, nodeVersion: manifest.nodeVersion, verifiedFiles: manifest.files.length, url, reusedPid: firstInfo.pid, checks: { jobId, status: checked.status, tests: checked.results[0].tests }, steps: ['manifest hashes', 'bundled Node launch', 'same-service reuse', 'packaged example and static page', 'explicit command consent', 'actual bundled Node version', 'actual bundled npm check without system Node/npm', 'check log artifact', 'report availability', 'graceful stop'], realModel: false }; fs.writeFileSync('.qa/release-validation.json', JSON.stringify(result, null, 2)); console.log(JSON.stringify(result, null, 2)); } finally { if (started) { try { await stop(); } catch {} } }