139 lines
16 KiB
JavaScript
139 lines
16 KiB
JavaScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import assert from 'node:assert/strict';
|
|
import net from 'node:net';
|
|
import http from 'node:http';
|
|
import { execFile, spawn } from 'node:child_process';
|
|
import { promisify } from 'node:util';
|
|
import { randomUUID } from 'node:crypto';
|
|
import { HostController } from '../src/core/desktop/host-controller.mjs';
|
|
import { validateInstalledVersion, snapshotPersistentData } from '../src/core/desktop/install-manager.mjs';
|
|
import { observeWindowsProcess } from '../src/core/desktop/process-identity.mjs';
|
|
import { projectEnvironment } from '../src/core/process-environment.mjs';
|
|
import { hash } from './desktop-f-state.mjs';
|
|
const release = JSON.parse(fs.readFileSync('releases/latest.json'));
|
|
const installer = JSON.parse(fs.readFileSync('.qa/embedded-browser-d-qa-installer.json')); assert.equal(installer.version, '1.22.0'); assert.equal(installer.qaFixture.fault, 'none');
|
|
assert.equal(hash(fs.readFileSync(installer.path)), installer.sha256);
|
|
// Keep the actual cross-drive install, with an exclusive short directory so
|
|
// inherited TEMP depth does not exceed the installer's Windows path limit.
|
|
const qaParent = 'C:\\AWQA22DI';
|
|
fs.mkdirSync(qaParent, { recursive: true });
|
|
assert.equal(fs.lstatSync(qaParent).isSymbolicLink(), false);
|
|
const root = path.join(qaParent, 'i-' + randomUUID().replaceAll('-', '')), installRoot = path.join(root, 'program'), dataRoot = path.join(root, 'data');
|
|
assert.equal(fs.existsSync(root), false); fs.mkdirSync(root);
|
|
const run = promisify(execFile), env = projectEnvironment(), cases = [];
|
|
const registryKey = 'HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Uninstall\\{a56fa7df-8a1f-415c-8fb1-d0a84e21ea3c}_is1';
|
|
let registryExists = false; try { await run('reg.exe', ['query', registryKey], { windowsHide: true, env, timeout: 10000 }); registryExists = true; } catch (error) { assert.equal(error.code, 1); }
|
|
assert.equal(registryExists, false, 'The isolated QA registration must be absent; never replace the user product registration.');
|
|
const probe = net.createServer(); await new Promise(resolve => probe.listen(0, '127.0.0.1', resolve)); const port = probe.address().port; await new Promise(resolve => probe.close(resolve));
|
|
const candidate = path.join(installRoot, 'versions', installer.candidateDirectory);
|
|
let stage = 'exact final installer', controller, model, report, original, authenticated = 0;
|
|
const record = (name, detail = {}) => { cases.push({ name, success: true, ...detail }); console.log('PASS ' + name); };
|
|
async function until(action, timeout = 90000) { const end = Date.now() + timeout; while (!await action()) { if (Date.now() > end) throw Error('Timeout: ' + stage); await new Promise(resolve => setTimeout(resolve, 100)); } }
|
|
async function processRun(binary, args, label, timeout = 900000) {
|
|
const bootstrap = path.basename(binary).toLowerCase() === 'agentworkbench.exe';
|
|
const nativeOutput = bootstrap ? fs.openSync(path.join(root, label + '-native-output.txt'), 'wx') : null;
|
|
const chunks = [], child = spawn(binary, args, { cwd: path.dirname(binary), env, windowsHide: true, stdio: bootstrap ? ['ignore', nativeOutput, nativeOutput] : ['ignore', 'pipe', 'pipe'] });
|
|
if (nativeOutput !== null) fs.closeSync(nativeOutput);
|
|
child.stdout?.on('data', bytes => chunks.push(bytes)); child.stderr?.on('data', bytes => chunks.push(bytes));
|
|
const timer = setTimeout(() => child.kill(), timeout);
|
|
const code = await new Promise((resolve, reject) => { child.once('error', reject); child.once(bootstrap ? 'exit' : 'close', resolve); }); clearTimeout(timer);
|
|
const bytes = Buffer.concat(chunks), file = path.join(root, label + '-process.txt'); fs.writeFileSync(file, bytes);
|
|
assert.equal(code, 0, label + ' process exit'); return { exitCode: code, processLog: file, processLogSha256: hash(bytes) };
|
|
}
|
|
async function nativeStart(label) {
|
|
await processRun(path.join(installRoot, 'AgentWorkbench.exe'), ['--hidden-window'], label, 30000);
|
|
controller = new HostController({ appRoot: candidate, dataRoot, port, nodePath: path.join(candidate, 'runtime/node.exe') });
|
|
await until(async () => { try { const value = await controller.connect(); return !!value; } catch { return false; } });
|
|
await until(async () => (await controller.status()).desktopWindows.count === 1);
|
|
return (await controller.status()).identity;
|
|
}
|
|
async function crashOwnedWindow() {
|
|
const status = await controller.status(); assert.equal(status.desktopWindows.count, 1);
|
|
const entry = status.desktopWindows.entries[0], actual = observeWindowsProcess(entry.pid);
|
|
assert.ok(actual); assert.equal(actual.createdAt, entry.createdAt);
|
|
assert.equal(fs.realpathSync.native(actual.executable), fs.realpathSync.native(path.join(candidate, 'desktop/AgentWorkbench.exe')));
|
|
// Simulate this authenticated Electron process crashing. /T would explicitly
|
|
// kill the detached backend descendant as well, a different failure mode.
|
|
const result = await run('taskkill.exe', ['/PID', String(entry.pid), '/F'], { windowsHide: true, env, timeout: 30000 });
|
|
fs.writeFileSync(path.join(root, 'owned-window-crash-' + entry.pid + '.txt'), result.stdout + result.stderr);
|
|
await until(async () => (await controller.status()).desktopWindows.count === 0);
|
|
assert.equal(observeWindowsProcess(entry.pid), null); return { pid: entry.pid, createdAt: entry.createdAt, scope: 'Explicit owned Electron process crash fixture; authenticated original PID/path/creation verified before taskkill /PID /F, without /T. Forced termination of the entire process tree is not window-process crash isolation.' };
|
|
}
|
|
try {
|
|
const setupLog = path.join(root, 'exact-setup.log');
|
|
const installed = await processRun(installer.path, ['/VERYSILENT', '/SUPPRESSMSGBOXES', '/NORESTART', '/NOICONS', '/DIR=' + installRoot, '/DATA_ROOT=' + dataRoot, '/PORT=' + port, '/LOG=' + setupLog], 'exact-setup');
|
|
const identity = validateInstalledVersion(candidate), pointer = JSON.parse(fs.readFileSync(path.join(installRoot, 'current.json')));
|
|
assert.equal(pointer.active.directory, installer.candidateDirectory); assert.equal(pointer.active.runtimeIdentity, identity.identitySha256);
|
|
assert.equal(hash(fs.readFileSync(path.join(installRoot, 'AgentWorkbench.exe'))), hash(fs.readFileSync(path.join(release.portableRoot, 'AgentWorkbench.exe'))));
|
|
record(stage, { installerSha256: installer.sha256, ...installed, setupLog, setupLogSha256: hash(fs.readFileSync(setupLog)), manifestSha256: identity.manifestSha256, loadedIdentity: identity.identitySha256 });
|
|
stage = 'actual installed native bootstrap and bundled execution host'; original = await nativeStart('native-start');
|
|
assert.equal(original.version, '1.22.0'); assert.equal(original.runtimeRoot, candidate); assert.equal(original.executable.path, path.join(candidate, 'runtime/node.exe'));
|
|
const detectedEngine = await controller.api('/engines/deepseek/availability'), detectedPurge = await controller.api('/engines/purge/source');
|
|
assert.equal(detectedEngine.status, 'available'); assert.equal(detectedPurge.status, 'available'); assert.equal(detectedPurge.defaultEnabled, false);
|
|
record(stage, { identity: original, desktopWindow: (await controller.status()).desktopWindows.entries[0],
|
|
relocatedSources: { engine: { root: detectedEngine.root, version: detectedEngine.version, identitySha256: detectedEngine.identitySha256 },
|
|
purge: { root: detectedPurge.root, version: detectedPurge.version, sourceSha256: detectedPurge.sourceSha256 } },
|
|
sourceEnvironmentOverrides: false, sourceScope: 'Read-only installed default lookup in a source-neutral child environment; actual third-party critical inputs checked, no SDK/plugin/model execution.' });
|
|
const key = 'owned-final-install-key-' + randomUUID();
|
|
model = http.createServer(async (request, response) => {
|
|
for await (const bytes of request) void bytes;
|
|
if (request.headers.authorization !== 'Bearer ' + key) { response.writeHead(401); response.end('{}'); return; }
|
|
authenticated++; response.writeHead(200, { 'Content-Type': 'application/json' }); response.end(JSON.stringify({ choices: [{ message: { role: 'assistant', content: 'Owned final installer connection' }, finish_reason: 'stop' }], usage: { prompt_tokens: 4, completion_tokens: 1, total_tokens: 5 } }));
|
|
}); await new Promise(resolve => model.listen(0, '127.0.0.1', resolve));
|
|
stage = 'actual installed owned DPAPI credential and HTTP authentication';
|
|
const created = await controller.api('/model-center/connections', { method: 'POST', input: { name: 'Owned final installer', baseUrl: 'http://127.0.0.1:' + model.address().port + '/v1', model: 'owned', apiKey: key, persistKey: true } });
|
|
const connectionId = created.connection?.id ?? created.id ?? created.center?.connections.at(-1).id; assert.ok(connectionId);
|
|
await controller.api('/model-center/probe', { method: 'POST', input: { id: connectionId } }); assert.equal(authenticated, 1);
|
|
const saved = (await controller.api('/model-center')).connections.find(value => value.id === connectionId); assert.equal(saved.hasKey, true);
|
|
record(stage, { connectionId, authenticatedRequests: authenticated, savedCredential: saved.savedCredential });
|
|
stage = 'actual installed owned approval and bundled npm project check';
|
|
const bootstrap = await controller.api('/bootstrap'), project = bootstrap.projects.find(value => value.id === bootstrap.exampleProjectId);
|
|
assert.equal(project.root, path.join(dataRoot, 'projects', 'starter-project'));
|
|
const session = await controller.api('/sessions', { method: 'POST', input: { projectId: project.id } });
|
|
await controller.api('/sessions/' + session.id + '/messages', { method: 'POST', input: { content: 'Owned exact installer offline demonstration and bundled Node check' } });
|
|
let current; await until(async () => { current = await controller.api('/sessions/' + session.id); return current.status === 'awaiting_approval'; });
|
|
const approval = current.approvals.find(value => value.status === 'pending'); assert.equal(approval.command, 'node --version');
|
|
await controller.api('/sessions/' + session.id + '/approvals/' + approval.id, { method: 'POST', input: { allow: true } });
|
|
await until(async () => { current = await controller.api('/sessions/' + session.id); return current.status === 'needs_review'; });
|
|
for (const change of current.changes.filter(value => value.status === 'pending')) await controller.api('/sessions/' + session.id + '/changes/' + change.id + '/reject', { method: 'POST', input: {} });
|
|
const planned = await controller.api('/sessions/' + session.id + '/check-plans', { method: 'POST', input: { names: ['test'] } }), plan = planned.execution.plans.at(-1);
|
|
const started = await controller.api('/sessions/' + session.id + '/check-jobs', { method: 'POST', input: { planId: plan.id } }), checkId = started.execution.checks.at(-1).id;
|
|
let checked; await until(async () => { checked = await controller.api('/sessions/' + session.id + '/check-jobs/' + checkId); return !['queued', 'running'].includes(checked.status); });
|
|
assert.equal(checked.status, 'passed'); assert.equal(checked.results[0].tests.passed, 4); assert.equal(checked.results[0].tests.total, 4);
|
|
|
|
const browserLog=path.join(root,'installed-browser.log'),browserStream=fs.createWriteStream(browserLog,{flags:'wx'});
|
|
const browserChild=spawn(process.execPath,['scripts/verify-embedded-browser.mjs','--app-root',candidate],{cwd:process.cwd(),env,windowsHide:true,stdio:['ignore','pipe','pipe']});
|
|
browserChild.stdout.pipe(browserStream,{end:false});browserChild.stderr.pipe(browserStream,{end:false});
|
|
const browserExit=await new Promise((resolve,reject)=>{browserChild.once('error',reject);browserChild.once('close',resolve);});await new Promise(resolve=>browserStream.end(resolve));
|
|
assert.equal(browserExit,0,'Actual installed browser verifier failed: '+browserLog);
|
|
const browserProof=JSON.parse(fs.readFileSync('.qa/embedded-browser-latest.json'));assert.equal(browserProof.success,true);assert.equal(browserProof.appRoot,candidate);assert.equal(browserProof.checks.length,17);assert.equal(browserProof.realModelRequests,0);
|
|
record(stage, { sessionId: session.id, originalApprovalId: approval.id, checkId, actualTests: checked.results[0].tests, embeddedBrowser: {groups:browserProof.checks.length,directory:browserProof.root,resultSha256:hash(fs.readFileSync(path.join(browserProof.root,'result.json'))),log:browserLog,logSha256:hash(fs.readFileSync(browserLog))}, originalLogArtifactId: checked.results[0].logArtifactId });
|
|
stage = 'actual owned Electron crash preserves final installed host and data';
|
|
const beforeCrash = snapshotPersistentData(dataRoot), crash = await crashOwnedWindow();
|
|
assert.equal((await controller.status()).identity.pid, original.pid); assert.deepEqual(snapshotPersistentData(dataRoot), beforeCrash);
|
|
record(stage, { ...crash, originalHostPid: original.pid, originalDataRetained: true });
|
|
stage = 'actual installed host restart preserves every row and loads owned saved connection';
|
|
const beforeRestart = snapshotPersistentData(dataRoot); await controller.stop(); assert.equal(observeWindowsProcess(original.pid), null);
|
|
const restarted = await nativeStart('native-restart'); assert.notEqual(restarted.pid, original.pid);
|
|
assert.deepEqual(snapshotPersistentData(dataRoot), beforeRestart);
|
|
const restored = (await controller.api('/model-center')).connections.find(value => value.id === connectionId);
|
|
assert.equal(restored.hasKey, true); assert.equal(restored.revision, saved.revision); assert.equal(restored.credentialRevision, saved.credentialRevision);
|
|
await controller.api('/model-center/probe', { method: 'POST', input: { id: connectionId } }); assert.equal(authenticated, 2);
|
|
record(stage, { previousPid: original.pid, pid: restarted.pid, originalPersistentDataRetained: true, ownedCredentialAuthenticatedAfterRestart: true });
|
|
await crashOwnedWindow();
|
|
stage = 'actual exact final installer uninstall preserves all owned user data';
|
|
const beforeUninstall = snapshotPersistentData(dataRoot), uninstallLog = path.join(root, 'uninstall.log');
|
|
const uninstalled = await processRun(path.join(installRoot, 'unins000.exe'), ['/VERYSILENT', '/SUPPRESSMSGBOXES', '/NORESTART', '/LOG=' + uninstallLog], 'uninstall');
|
|
assert.equal(observeWindowsProcess(restarted.pid), null); assert.deepEqual(snapshotPersistentData(dataRoot), beforeUninstall);
|
|
assert.equal(fs.existsSync(path.join(installRoot, 'AgentWorkbench.exe')), false); assert.equal(fs.existsSync(path.join(installRoot, 'current.json')), false);
|
|
record(stage, { ...uninstalled, uninstallLog, uninstallLogSha256: hash(fs.readFileSync(uninstallLog)), originalDataRoot: dataRoot, originalDataRetained: true });
|
|
report = { success: true, version: '1.22.0', root, installRoot, dataRoot, installer, cases, ownedModelRequests: authenticated, realModelRequests: 0, isolatedQaRegistrationWasAbsent: true, scope: 'Final candidate payload and installer logic, using an isolated QA AppId (explicitly different setup SHA). The product setup is separately staged on the formal installation. Exclusive owned install/data/port, /NOICONS and an absent isolated QA registration prevent replacing user installation or shortcuts. Actual native bootstrap, host/owned credential/check/restart/uninstall; crash fixture stated explicitly.' };
|
|
} catch (error) { report = { success: false, version: '1.22.0', root, installRoot, dataRoot, stage, error: error.stack, cases, installer, ownedModelRequests: authenticated, realModelRequests: 0 }; process.exitCode = 1; }
|
|
finally {
|
|
if (!report?.success && controller) try { const status = await controller.status(); if (!status.desktopWindows.count && !status.activity.active) await controller.stop(); } catch {}
|
|
model?.closeAllConnections?.(); if (model) await new Promise(resolve => model.close(resolve));
|
|
fs.writeFileSync(path.join(root, 'result.json'), JSON.stringify(report, null, 2)); fs.writeFileSync('.qa/embedded-browser-d-install-location.json', JSON.stringify({ root, result: path.join(root, 'result.json') }));
|
|
console.log(JSON.stringify({ success: report.success, version: report.version, root, cases: cases.length, ...(report.error ? { stage, error: report.error } : {}) }));
|
|
}
|