125 lines
7.0 KiB
JavaScript
125 lines
7.0 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import http from 'node:http';
|
|
import { spawn } from 'node:child_process';
|
|
import { fileURLToPath } from 'node:url';
|
|
import { Store } from '../src/server/store.mjs';
|
|
import { DEFAULT_CONFIG } from '../src/core/storage/legacy-import.mjs';
|
|
|
|
export const workspace = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');
|
|
const fixtureRoot = path.join(workspace, '.qa');
|
|
const disposalLists = new WeakMap();
|
|
|
|
export function fixture(t, { modernDefaults = false } = {}) {
|
|
fs.mkdirSync(fixtureRoot, { recursive: true });
|
|
const root = fs.mkdtempSync(path.join(fixtureRoot, 'test-'));
|
|
const projectRoot = path.join(root, 'project');
|
|
fs.mkdirSync(projectRoot);
|
|
fs.writeFileSync(path.join(projectRoot, 'README.md'), '# Fixture project\n\nA local test project with TODO work.\n');
|
|
const data = path.join(root, 'data');
|
|
// Existing regression scenarios intentionally exercise the original finite
|
|
// budget/compaction contract. New-default coverage opts into a fresh install.
|
|
if (!modernDefaults) {
|
|
fs.mkdirSync(data);
|
|
fs.writeFileSync(path.join(data, 'config.json'), JSON.stringify({ ...DEFAULT_CONFIG, contextLimit: 16000, maxOutputTokens: 2048, capacityMode: 'manual', historyPolicy: 'compact', maxRunTokens: 200000, maxTaskTokens: 1000000, maxSteps: 12, maxRunDurationMs: 600000, modelResponseTimeoutMs: 300000, modelStreamIdleTimeoutMs: 120000, executionMode: 'restricted' }));
|
|
}
|
|
const store = new Store(data);
|
|
const project = store.addProject(projectRoot);
|
|
const session = store.createSession(project.id);
|
|
const disposers = [() => store.close()];
|
|
disposalLists.set(t, disposers);
|
|
t.after(async () => {
|
|
for (const dispose of disposers.reverse()) await dispose();
|
|
const resolved = path.resolve(root);
|
|
assert.ok(resolved.startsWith(path.resolve(fixtureRoot) + path.sep) && path.basename(resolved).startsWith('test-'));
|
|
// Yield while Windows releases handles from owned child processes. A
|
|
// persistent lock still fails teardown; resource disposal must run first.
|
|
await fs.promises.rm(resolved, { recursive: true, force: true, maxRetries: 10, retryDelay: 100 });
|
|
});
|
|
return { root, projectRoot, data, store, project, session, onCleanup: dispose => disposers.push(dispose) };
|
|
}
|
|
|
|
export async function until(predicate, timeout = 8000) {
|
|
const deadline = Date.now() + timeout;
|
|
while (!(await predicate())) {
|
|
if (Date.now() > deadline) throw new Error('Timed out waiting for test state.');
|
|
await new Promise(resolve => setTimeout(resolve, 20));
|
|
}
|
|
}
|
|
|
|
export async function compatibleFixture(t, respond) {
|
|
const requests = [];
|
|
const server = http.createServer(async (request, response) => {
|
|
try {
|
|
const chunks = [];
|
|
for await (const chunk of request) chunks.push(chunk);
|
|
const input = chunks.length ? JSON.parse(Buffer.concat(chunks).toString('utf8')) : null;
|
|
requests.push({ url: request.url, headers: request.headers, input });
|
|
const result = await respond(request.url, input, requests);
|
|
response.writeHead(result.status ?? 200, { 'Content-Type': 'application/json', ...result.headers });
|
|
response.end(JSON.stringify(result.body ?? result));
|
|
} catch (error) {
|
|
response.writeHead(500, { 'Content-Type': 'application/json' });
|
|
response.end(JSON.stringify({ error: { message: error.message } }));
|
|
}
|
|
});
|
|
await new Promise(resolve => server.listen(0, '127.0.0.1', resolve));
|
|
t.after(() => new Promise(resolve => { server.closeAllConnections(); server.close(resolve); }));
|
|
return { baseUrl: 'http://127.0.0.1:' + server.address().port + '/v1', requests };
|
|
}
|
|
|
|
export const call = (id, name, args) => ({ id, type: 'function', function: { name, arguments: typeof args === 'string' ? args : JSON.stringify(args) } });
|
|
export const completion = (calls = [], content = '') => ({ choices: [{ message: { role: 'assistant', content, ...(calls.length ? { tool_calls: calls } : {}) }, finish_reason: calls.length ? 'tool_calls' : 'stop' }], usage: { prompt_tokens: 123, completion_tokens: 45 } });
|
|
|
|
export function assertToolPairs(messages) {
|
|
for (let index = 0; index < messages.length; index++) {
|
|
const message = messages[index];
|
|
assert.notEqual(message.role, 'tool', 'Orphan tool message');
|
|
if (!message.tool_calls?.length) continue;
|
|
const expected = new Set(message.tool_calls.map(call => call.id));
|
|
for (let count = 0; count < message.tool_calls.length; count++) {
|
|
const result = messages[++index];
|
|
assert.equal(result?.role, 'tool');
|
|
assert.ok(expected.delete(result.tool_call_id), 'Tool result ID mismatch or duplicate');
|
|
}
|
|
assert.equal(expected.size, 0);
|
|
}
|
|
}
|
|
|
|
export async function launchWorkbench(t, data, extraEnv = {}, { appRoot = process.env.AGENT_WORKBENCH_TEST_APP_ROOT ?? workspace, nodePath = process.execPath } = {}) {
|
|
const env = { ...process.env, AGENT_WORKBENCH_APP_DIR: appRoot, AGENT_WORKBENCH_DATA_DIR: data, AGENT_WORKBENCH_PORT: '0', ...extraEnv };
|
|
delete env.AGENT_WORKBENCH_API_KEY;
|
|
const child = spawn(nodePath, [path.join(appRoot, 'dist-server', 'server.mjs')], { cwd: appRoot, env, windowsHide: true, stdio: ['ignore', 'pipe', 'pipe'] });
|
|
let output = '', errors = '', url = '';
|
|
child.stdout.on('data', bytes => {
|
|
output += bytes.toString('utf8');
|
|
for (const line of output.split('\n')) { try { const parsed = JSON.parse(line); if (parsed.ready) url = parsed.url; } catch {} }
|
|
});
|
|
child.stderr.on('data', bytes => { errors += bytes.toString('utf8'); });
|
|
await until(() => url || child.exitCode !== null);
|
|
assert.ok(url, 'Workbench launch failed: ' + errors);
|
|
const bootstrap = await (await fetch(url + '/api/bootstrap', { headers: { Connection: 'close' } })).json();
|
|
const api = async (route, input) => {
|
|
const method = input === undefined ? 'GET' : 'POST';
|
|
let response;
|
|
try {
|
|
response = await fetch(url + '/api' + route, { method,
|
|
headers: { 'Content-Type': 'application/json', 'X-Workbench-Token': bootstrap.token, Connection: 'close' },
|
|
...(input === undefined ? {} : { body: JSON.stringify(input) }) });
|
|
} catch (cause) { throw Object.assign(new Error('QA API transport failed before response headers: ' + method + ' ' + route, { cause }), { stage: 'response_headers', method, route }); }
|
|
let result;
|
|
try { result = await response.json(); }
|
|
catch (cause) { throw Object.assign(new Error('QA API transport failed while reading response: ' + method + ' ' + route, { cause }), { stage: 'response_body', method, route }); }
|
|
if (!response.ok) throw Object.assign(new Error(result.error), { status: response.status });
|
|
return result;
|
|
};
|
|
const close = async () => {
|
|
if (child.exitCode !== null || child.signalCode !== null) return;
|
|
try { await api('/shutdown', {}); await until(() => child.exitCode !== null || child.signalCode !== null, 5000); }
|
|
finally { if (child.exitCode === null && child.signalCode === null) child.kill(); }
|
|
};
|
|
if (disposalLists.has(t)) disposalLists.get(t).push(close); else t.after(close);
|
|
return { child, url, bootstrap, api, close };
|
|
}
|