import { execSync } from 'node:child_process' import { existsSync, readFileSync } from 'node:fs' import path from 'node:path' import { fileURLToPath } from 'node:url' const __filename = fileURLToPath(import.meta.url) const __dirname = path.dirname(__filename) // Playwright globalSetup — runs once before all e2e tests. // // Responsibilities: // 1. Verify api/.env exists (Laravel needs APP_KEY etc.) // 2. Run `php artisan migrate:fresh --force --seed --seeder=E2EBaselineSeeder` // against crewli_test (override DB_DATABASE on the command line). // The seeder writes seeded IDs to api/storage/app/e2e-fixtures.json. // 3. Verify the fixtures file was produced. // // Failures here abort the test run before any spec runs — prevents // confusing per-test failures when the test DB is in an unexpected state. // // Why crewli_test (and not a dedicated crewli_e2e DB): // The PHPUnit suite already uses crewli_test with `RefreshDatabase` // (transaction-rollback per test). Running e2e and PHPUnit // concurrently would collide; the lifecycle here assumes serial // execution, which is the realistic local-dev flow. Documented in // dev-docs/ARCH-TESTING.md §5 (mock-vs-real-backend). const REPO_ROOT = path.resolve(__dirname, '..', '..', '..', '..') const API_DIR = path.join(REPO_ROOT, 'api') const FIXTURES_PATH = path.join(API_DIR, 'storage', 'app', 'e2e-fixtures.json') export default async function globalSetup(): Promise { console.log('[e2e setup] Repo root:', REPO_ROOT) console.log('[e2e setup] API dir:', API_DIR) if (!existsSync(path.join(API_DIR, '.env'))) { throw new Error( '[e2e setup] api/.env not found. Run `cp api/.env.example api/.env` and configure DB_* + APP_KEY.', ) } console.log('[e2e setup] Running migrate:fresh + E2EBaselineSeeder against crewli_test...') // --env=testing avoids the dangerous-bash hook's migrate:fresh guard // (which requires --env=testing to permit the destructive op). // DB_DATABASE override forces crewli_test even though the .env may // point at the dev DB. try { execSync( 'DB_DATABASE=crewli_test php artisan --env=testing migrate:fresh --force --seed --seeder="Database\\\\Seeders\\\\E2EBaselineSeeder"', { cwd: API_DIR, stdio: 'inherit', env: { ...process.env, DB_DATABASE: 'crewli_test' }, }, ) } catch (err) { throw new Error(`[e2e setup] migrate:fresh + seed failed: ${(err as Error).message}`) } if (!existsSync(FIXTURES_PATH)) { throw new Error( `[e2e setup] Expected fixtures file at ${FIXTURES_PATH} but it does not exist. ` + 'Did E2EBaselineSeeder run? Check the artisan output above.', ) } const fixtures = JSON.parse(readFileSync(FIXTURES_PATH, 'utf-8')) as Record console.log('[e2e setup] Fixtures ready:', Object.keys(fixtures).join(', ')) }