// What berry's tests share, wherever they are: an extension's tests may import it too, as a bare specifier. /** Wait for `check` to hold, failing after `timeout` milliseconds. */ export async function until( check: () => boolean | Promise, { timeout = 5_000, message = "timed out waiting" } = {}, ): Promise { const deadline = Date.now() + timeout; while (!(await check())) { if (Date.now() > deadline) throw new Error(message); await new Promise((resolve) => setTimeout(resolve, 5)); } } /** A temporary directory, by its real path, gone once its `await using` scope ends. */ export async function tempDir( options: Deno.MakeTempOptions = {}, ): Promise<{ path: string } & AsyncDisposable> { const path = await Deno.realPath(await Deno.makeTempDir({ prefix: "berry-test-", ...options })); return { path, [Symbol.asyncDispose]: () => Deno.remove(path, { recursive: true }).catch(() => {}), }; } /** Whether `command` is on this machine's PATH, for skipping tests that need it. */ export function onPath(command: string): boolean { return (Deno.env.get("PATH") ?? "").split(":").some((dir) => { try { return Deno.statSync(`${dir}/${command}`).isFile; } catch { return false; } }); } /** A valid 1×1 PNG, as base64. */ export const PNG = "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR4nGP4z/D/PwAG/gL+DHWJ3gAAAABJRU5ErkJggg==";