import { TestReporter } from './utils/test-reporter'; import { execSync } from 'child_process'; import * as http from 'http'; import * as dotenv from 'dotenv'; import * as path from 'path'; dotenv.config({ path: path.resolve(__dirname, '.env') }); const platform = process.env.PLATFORM || 'ios'; const appiumHost = process.env.APPIUM_HOST || 'localhost'; const appiumPort = Number(process.env.APPIUM_PORT) || 4723; async function ensureWDARunning(): Promise { const isReady = await checkWDA(); if (isReady) { console.log('[global-setup] WDA already running'); return; } console.log('[global-setup] WDA not responding, starting...'); try { execSync('bash ./scripts/start-wda.sh', { cwd: process.cwd(), timeout: 70000, stdio: 'inherit', }); } catch (e: any) { console.error('[global-setup] Failed to start WDA:', e.message); throw new Error('WDA failed to start. Please start it manually.'); } } function checkWDA(): Promise { return new Promise((resolve) => { const req = http.request( { hostname: 'localhost', port: 8100, path: '/status', method: 'GET', timeout: 3000 }, (res) => { let data = ''; res.on('data', (chunk) => (data += chunk)); res.on('end', () => { resolve(data.includes('"ready" : true')); }); } ); req.on('error', () => resolve(false)); req.on('timeout', () => { req.destroy(); resolve(false); }); req.end(); }); } function checkAppium(): Promise { return new Promise((resolve) => { const req = http.request( { hostname: appiumHost, port: appiumPort, path: '/status', method: 'GET', timeout: 3000 }, (res) => { let data = ''; res.on('data', (chunk) => (data += chunk)); res.on('end', () => { resolve(data.includes('"ready":true') || data.includes('"ready" : true')); }); } ); req.on('error', () => resolve(false)); req.on('timeout', () => { req.destroy(); resolve(false); }); req.end(); }); } export async function setup() { TestReporter.clearSharedResults(); console.log(`[global-setup] Platform: ${platform}`); if (platform === 'android') { const ready = await checkAppium(); if (ready) { console.log('[global-setup] Appium already running'); } else { throw new Error('Appium not running. Start with: appium --port 4723'); } } else { await ensureWDARunning(); } } export function teardown() { const p = process.env.PLATFORM || 'ios'; TestReporter.generateCombinedReport(`AIHub_${p.toUpperCase()}`); }