| 6 | const setupEndpoint = `${workerUrl}/api/setup-r2-for-tests`; |
| 7 | |
| 8 | async function globalSetup(config: FullConfig) { |
| 9 | console.log('--- Starting Global Setup: Populating R2 via Endpoint ---'); |
| 10 | |
| 11 | // Add a delay to allow the web server to fully start |
| 12 | // Playwright's webServer.url check confirms availability, but sometimes |
| 13 | // internal initialization might still be happening. |
| 14 | console.log('Waiting 3 seconds for server to stabilize...'); |
| 15 | await new Promise(resolve => setTimeout(resolve, 3000)); |
| 16 | |
| 17 | try { |
| 18 | console.log(`Sending POST request to ${setupEndpoint}...`); |
| 19 | const response = await fetch(setupEndpoint, { |
| 20 | method: 'POST', |
| 21 | headers: { |
| 22 | 'Content-Type': 'application/json', |
| 23 | // Add any other headers required by your endpoint if necessary |
| 24 | }, |
| 25 | // No body needed as the endpoint knows what to do |
| 26 | }); |
| 27 | |
| 28 | const responseBodyText = await response.text(); // Read body once |
| 29 | console.log(`Global Setup: Received response status ${response.status}`); |
| 30 | console.log(`Global Setup: Received response body: ${responseBodyText}`); |
| 31 | |
| 32 | |
| 33 | if (!response.ok) { |
| 34 | throw new Error(`Failed to setup R2 via endpoint: ${response.status} ${response.statusText} - Body: ${responseBodyText}`); |
| 35 | } |
| 36 | |
| 37 | // Try parsing JSON only if response is ok |
| 38 | try { |
| 39 | const result = JSON.parse(responseBodyText); |
| 40 | console.log('Global Setup: R2 Population Result:', result.message); |
| 41 | } catch (parseError) { |
| 42 | console.error("Global Setup: Failed to parse JSON response from setup endpoint."); |
| 43 | throw new Error(`Failed to parse JSON response: ${parseError} - Original Body: ${responseBodyText}`); |
| 44 | } |
| 45 | |
| 46 | console.log('--- Global Setup Finished Successfully ---'); |
| 47 | |
| 48 | } catch (error) { |
| 49 | console.error('Error during global setup (calling R2 population endpoint):', error); |
| 50 | // Throw error to stop test execution if setup fails critically |
| 51 | throw new Error(`Global setup failed: ${error}`); |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | export default globalSetup; |