import { exec as execCallback } from 'node:child_process'; import { readFile } from 'node:fs/promises'; import { dirname, resolve } from 'node:path'; import { fileURLToPath } from 'node:url'; import { promisify } from 'node:util'; const exec = promisify(execCallback); const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); const repoRoot = resolve(__dirname, '..', '..'); const DEFAULT_RPC_URL = process.env.STACK_RPC_URL ?? 'http://127.0.0.1:8545'; const DEFAULT_WEBAPP_URL = process.env.STACK_WEBAPP_URL ?? 'http://localhost:5173'; const DEFAULT_GRAPHQL_URL = process.env.STACK_GRAPHQL_URL ?? 'http://localhost:42069/graphql'; let stackStarted = false; async function cleanupContainers(): Promise { try { await run("podman pod ps -q --filter name=harb | xargs -r podman pod rm -f || true"); await run("podman ps -aq --filter name=harb | xargs -r podman rm -f || true"); } catch (error) { console.warn('[stack] Failed to cleanup containers', error); } } function delay(ms: number): Promise { return new Promise(resolveDelay => setTimeout(resolveDelay, ms)); } async function run(command: string): Promise { await exec(command, { cwd: repoRoot, shell: true }); } async function checkRpcReady(rpcUrl: string): Promise { const response = await fetch(rpcUrl, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ jsonrpc: '2.0', id: 1, method: 'eth_chainId', params: [] }), }); if (!response.ok) { throw new Error(`RPC health check failed with status ${response.status}`); } const payload = await response.json(); if (!payload?.result) { throw new Error('RPC health check returned no result'); } } async function checkWebAppReady(webAppUrl: string): Promise { const url = webAppUrl.endsWith('/') ? `${webAppUrl}app/` : `${webAppUrl}/app/`; const response = await fetch(url, { method: 'GET', redirect: 'manual', }); if (!response.ok && response.status !== 308) { throw new Error(`Web app health check failed with status ${response.status}`); } } async function checkGraphqlReady(graphqlUrl: string): Promise { const response = await fetch(graphqlUrl, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify({ query: '{ __typename }' }), }); if (!response.ok) { throw new Error(`GraphQL health check failed with status ${response.status}`); } } export async function startStack(): Promise { if (stackStarted) { return; } await cleanupContainers(); await run('nohup ./scripts/dev.sh start > ./tests/.stack.log 2>&1 &'); stackStarted = true; } export async function waitForStackReady(options: { rpcUrl?: string; webAppUrl?: string; graphqlUrl?: string; timeoutMs?: number; pollIntervalMs?: number; } = {}): Promise { const rpcUrl = options.rpcUrl ?? DEFAULT_RPC_URL; const webAppUrl = options.webAppUrl ?? DEFAULT_WEBAPP_URL; const graphqlUrl = options.graphqlUrl ?? DEFAULT_GRAPHQL_URL; const timeoutMs = options.timeoutMs ?? 180_000; const pollIntervalMs = options.pollIntervalMs ?? 2_000; const start = Date.now(); const errors = new Map(); while (Date.now() - start < timeoutMs) { try { await Promise.all([ checkRpcReady(rpcUrl), checkWebAppReady(webAppUrl), checkGraphqlReady(graphqlUrl), ]); return; } catch (error) { const message = error instanceof Error ? error.message : String(error); errors.set('lastError', message); await delay(pollIntervalMs); } } const logPath = resolve(repoRoot, 'tests', '.stack.log'); let logTail = ''; try { const contents = await readFile(logPath, 'utf-8'); logTail = contents.split('\n').slice(-40).join('\n'); } catch (readError) { logTail = `Unable to read stack log: ${readError}`; } throw new Error(`Stack failed to become ready within ${timeoutMs}ms\nLast error: ${errors.get('lastError')}\nLog tail:\n${logTail}`); } export async function stopStack(): Promise { if (!stackStarted) { return; } try { await run('./scripts/dev.sh stop'); } finally { stackStarted = false; } }