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'; 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) { 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'); 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-')); fs.rmSync(resolved, { recursive: true, force: true, maxRetries: 3, 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 = {}) { const env = { ...process.env, AGENT_WORKBENCH_APP_DIR: workspace, AGENT_WORKBENCH_DATA_DIR: data, AGENT_WORKBENCH_PORT: '0', ...extraEnv }; delete env.AGENT_WORKBENCH_API_KEY; const child = spawn(process.execPath, [path.join(workspace, 'dist-server', 'server.mjs')], { cwd: workspace, 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')).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(); 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 }; }