import { execSync } from 'child_process'; import { writeFileSync } from 'fs'; import { join, dirname } from 'path'; import { fileURLToPath } from 'url'; const __filename = fileURLToPath(import.meta.url); const __dirname = dirname(__filename); /** * Global setup for E2E tests. * Seeds a test activation token before tests run. */ async function globalSetup() { console.warn('🌱 Seeding test activation token...'); try { // Call the backend command to create a test token // Project root is 2 levels up from frontend/e2e/ const projectRoot = join(__dirname, '../..'); const composeFile = join(projectRoot, 'compose.yaml'); const result = execSync( `docker compose -f "${composeFile}" exec -T php php bin/console app:dev:create-test-activation-token --email=e2e-test@example.com 2>&1`, { encoding: 'utf-8' } ); // Extract the token from the output // Output format: "Token f9174245-9766-4ef1-b6e9-a6795aa2da04" const tokenMatch = result.match(/Token\s+([a-f0-9-]{36})/i); if (!tokenMatch) { console.error('❌ Could not extract token from output:', result); throw new Error('Failed to extract token from command output'); } const token = tokenMatch[1]; console.warn(`✅ Test token created: ${token}`); // Write the token to a file for tests to use const tokenFile = join(__dirname, '.test-token'); writeFileSync(tokenFile, token); console.warn('✅ Token saved to .test-token file'); } catch (error) { console.error('❌ Failed to seed test token:', error); // Don't throw - tests can still run with skipped token-dependent tests console.warn('⚠️ Tests requiring valid tokens will be skipped'); } } export default globalSetup;