| 157 | * Wait for the server to respond to health checks. |
| 158 | */ |
| 159 | export async function waitForServer(info: ServerInfo, timeoutMs: number): Promise<void> { |
| 160 | const startTime = Date.now() |
| 161 | |
| 162 | while (Date.now() - startTime < timeoutMs) { |
| 163 | try { |
| 164 | const response = await fetch(`${info.url}/api`) |
| 165 | if (response.ok) { |
| 166 | return |
| 167 | } |
| 168 | } catch { |
| 169 | // Server not ready yet |
| 170 | } |
| 171 | |
| 172 | await sleep(HEALTH_CHECK_INTERVAL_MS) |
| 173 | } |
| 174 | |
| 175 | throw new Error(`Server failed to start within ${timeoutMs}ms at ${info.url}`) |
| 176 | } |
| 177 | |
| 178 | function sleep(ms: number): Promise<void> { |
| 179 | return new Promise(resolve => setTimeout(resolve, ms)) |