/**
 * Races a promise against a timeout.
 * Resolves to the promise value or "timeout" if the deadline expires.
 */
export function raceTimeout(promise, ms) {
    let timer;
    const timeout = new Promise((resolve) => {
        timer = setTimeout(() => resolve("timeout"), ms);
    });
    return Promise.race([promise, timeout]).finally(() => clearTimeout(timer));
}
const CLOSE_TIMEOUT_MS = 5_000;
async function settleWithTimeout(promise, timeoutMs) {
    let timeoutId;
    const timeout = new Promise((resolve) => {
        timeoutId = setTimeout(resolve, timeoutMs);
    });
    try {
        await Promise.race([promise.catch(() => { }), timeout]);
    }
    finally {
        if (timeoutId)
            clearTimeout(timeoutId);
    }
}
export async function closeV3(v3) {
    if (!v3)
        return;
    const isBrowserbase = v3.isBrowserbase;
    if (isBrowserbase) {
        try {
            await settleWithTimeout(v3.context.conn.send("Browser.close"), CLOSE_TIMEOUT_MS);
        }
        catch {
            // best-effort cleanup
        }
    }
    await settleWithTimeout(v3.close(), CLOSE_TIMEOUT_MS);
}
//# sourceMappingURL=testUtils.js.map