/** * Global setup for real API integration tests * Ensures SSH tunnel and backend services are running */ // import path from 'path'; // import { fileURLToPath } from 'url'; // const __filename = fileURLToPath(import.meta.url); // const __dirname = path.dirname(__filename); export default async function globalSetup() { console.log('🔧 Setting up real API integration test environment...'); // Root directory for reference if needed later // const rootDir = path.resolve(__dirname, '../../../../../..'); try { // Check if SSH tunnel is running by testing Oracle port console.log('📡 Checking SSH tunnel status...'); try { const response = await fetch('http://localhost:8000/health', { timeout: 5000 }); const health = await response.json(); if (health.database === 'connected') { console.log('✅ SSH tunnel appears to be working (database connected)'); } else { console.log('âš ī¸ Database not connected - SSH tunnel may need to be started manually'); } } catch (error) { console.log('âš ī¸ Could not check tunnel status - continuing anyway'); } // Check backend health console.log('đŸĨ Checking backend health...'); try { const healthResponse = await fetch('http://localhost:8000/health', { timeout: 10000 }); if (!healthResponse.ok) { throw new Error(`Backend health check failed: ${healthResponse.status}`); } const healthData = await healthResponse.json(); console.log('✅ Backend health check passed:', healthData); } catch (error) { console.error('❌ Backend health check failed:', error.message); throw new Error('Backend is not available for integration tests'); } // Check frontend availability console.log('🌐 Checking frontend availability...'); try { const frontendResponse = await fetch('http://localhost:3001', { timeout: 10000 }); if (!frontendResponse.ok) { throw new Error(`Frontend not available: ${frontendResponse.status}`); } console.log('✅ Frontend is available'); } catch (error) { console.error('❌ Frontend availability check failed:', error.message); throw new Error('Frontend is not available for integration tests'); } // Validate environment variables console.log('🔐 Validating environment configuration...'); const requiredEnvVars = [ 'ORACLE_USER', 'ORACLE_PASSWORD', 'ORACLE_HOST', 'ORACLE_PORT', 'ORACLE_SID' ]; const missingVars = requiredEnvVars.filter(varName => !process.env[varName]); if (missingVars.length > 0) { console.warn('âš ī¸ Missing environment variables:', missingVars.join(', ')); console.log('â„šī¸ Some tests may use default values'); } else { console.log('✅ All required environment variables are set'); } // Test database connectivity through backend console.log('đŸ—„ī¸ Testing database connectivity...'); try { const dbTestResponse = await fetch('http://localhost:8000/api/companies', { timeout: 15000 }); if (dbTestResponse.ok) { const companies = await dbTestResponse.json(); console.log(`✅ Database connectivity verified (${companies.length} companies found)`); // Check if ROMFAST is available const romfast = companies.find(c => c.id_firma === 'ROMFAST'); if (romfast) { console.log('✅ ROMFAST company data available for testing'); } else { console.warn('âš ī¸ ROMFAST company not found in test data'); } } else { console.warn('âš ī¸ Database connectivity test returned:', dbTestResponse.status); } } catch (error) { console.warn('âš ī¸ Database connectivity test failed:', error.message); console.log('â„šī¸ Tests will proceed but may fail if database is not accessible'); } console.log('đŸŽ¯ Global setup completed successfully'); // Store setup metadata for tests global.__INTEGRATION_SETUP__ = { timestamp: new Date().toISOString(), backend: 'http://localhost:8000', frontend: 'http://localhost:3001', sshTunnelActive: true, environmentValidated: true }; } catch (error) { console.error('❌ Global setup encountered error:', error.message); console.log('â„šī¸ Continuing with tests - they may fail if services are not available'); // Don't fail setup - let individual tests handle service unavailability global.__INTEGRATION_SETUP__ = { timestamp: new Date().toISOString(), backend: 'http://localhost:8000', frontend: 'http://localhost:3001', sshTunnelActive: false, environmentValidated: false, setupError: error.message }; } }