/** * General test helpers */ /** * Sleep for a specified number of milliseconds */ export function sleep(ms: number): Promise { return new Promise((resolve) => setTimeout(resolve, ms)); } /** * Wait for a condition to be true */ export async function waitFor( condition: () => boolean | Promise, timeout = 5000, interval = 100 ): Promise { const start = Date.now(); while (Date.now() - start < timeout) { if (await condition()) { return; } await sleep(interval); } throw new Error(`Condition not met within ${timeout}ms`); } /** * Retry a function with exponential backoff */ export async function retry( fn: () => Promise, maxAttempts = 3, initialDelay = 100 ): Promise { let lastError: Error | undefined; for (let attempt = 1; attempt <= maxAttempts; attempt++) { try { return await fn(); } catch (error) { lastError = error as Error; if (attempt < maxAttempts) { const delay = initialDelay * Math.pow(2, attempt - 1); await sleep(delay); } } } throw lastError || new Error('Retry failed'); }