Files
Agent-Workbench/tests/upgrade.test.mjs
T
2026-10-29 13:11:30 +08:00

47 lines
4.0 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import path from 'node:path';
import fs from 'node:fs';
import { spawn } from 'node:child_process';
import { fixture, compatibleFixture, until, workspace } from './helpers.mjs';
import { upgradeRunning } from '../scripts/upgrade-running.mjs';
test('the running 0.1.0 process upgrades in place and retains its project, session, files and volatile model connection', { timeout: 30000 }, async t => {
const f = fixture(t);
const legacyData = path.join(f.root, 'legacy-data'); f.store.exportLegacyData(legacyData);
const oldRoot = path.join(workspace, 'tests', 'fixtures', 'legacy-workbench-0.1');
const env = { ...process.env, AGENT_WORKBENCH_APP_DIR: oldRoot, AGENT_WORKBENCH_DATA_DIR: legacyData, AGENT_WORKBENCH_PORT: '0' };
delete env.AGENT_WORKBENCH_API_KEY;
const child = spawn(process.execPath, [path.join(oldRoot, 'dist-server', 'server.mjs')], { cwd: oldRoot, env, windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
let url = '', output = '', stderr = '';
child.stdout.on('data', bytes => { output += bytes.toString('utf8'); for (const line of output.split('\n')) { try { const result = JSON.parse(line); if (result.ready) url = result.url; } catch {} } });
child.stderr.on('data', bytes => { stderr += bytes.toString('utf8'); });
await until(() => url || child.exitCode !== null); assert.ok(url, stderr);
let 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 value = await response.json(); assert.ok(response.ok, value.error); return value;
};
f.onCleanup(async () => { try { if (child.exitCode === null && child.signalCode === null) { await api('/shutdown', {}); await until(() => child.exitCode !== null, 5000); } } finally { if (child.exitCode === null && child.signalCode === null) child.kill(); } });
const model = await compatibleFixture(t, () => ({ data: [{ id: 'upgrade-fixture' }] }));
await api('/config', { mode: 'compatible', model: 'upgrade-fixture', baseUrl: model.baseUrl, apiKey: 'fixture-upgrade-memory-key' });
await api('/sessions/' + f.session.id + '/proposals', { path: 'UPGRADE-NOTE.md', content: '# Preserve this verified change\n', reason: 'Fixture before in-place upgrade' });
const proposed = await api('/sessions/' + f.session.id); await api('/sessions/' + f.session.id + '/changes/' + proposed.changes[0].id + '/apply', {});
const beforeBytes = fs.readFileSync(path.join(f.projectRoot, 'UPGRADE-NOTE.md'));
const result = await upgradeRunning({ url, pid: child.pid, currentRoot: oldRoot, newRoot: workspace });
assert.equal(result.version, '1.0.0'); assert.equal(result.pid, child.pid); assert.equal(result.verified, true);
assert.equal(result.hasKey, true); bootstrap = await (await fetch(url + '/api/bootstrap')).json();
assert.equal(bootstrap.config.hasKey, true); assert.equal(bootstrap.config.model, 'upgrade-fixture');
assert.deepEqual(fs.readFileSync(path.join(f.projectRoot, 'UPGRADE-NOTE.md')), beforeBytes);
assert.equal((await api('/sessions/' + f.session.id)).changes[0].status, 'applied');
assert.equal((await api('/diagnostics?session=' + f.session.id)).version, '1.0.0');
assert.deepEqual((await api('/config/models', {})).models, ['upgrade-fixture']);
assert.equal(model.requests[0].headers.authorization, 'Bearer fixture-upgrade-memory-key');
assert.ok(!fs.readFileSync(path.join(legacyData, 'config.json'), 'utf8').includes('fixture-upgrade-memory-key'));
assert.equal(bootstrap.storage.engine, 'sqlite');
assert.equal((await api('/sessions/' + f.session.id)).execution.task.origin, 'legacy-import');
const health = await (await fetch(url + '/api/health')).json(); assert.equal(health.pid, child.pid);
await assert.rejects(fetch('http://127.0.0.1:9229/json/list', { signal: AbortSignal.timeout(500) }));
});