// The app's client signed in to a fake berry, in a happy-dom page. import { type FakeBerry, fakeBerry, withDom } from "@berry/util/dom.ts"; import type { Window } from "happy-dom"; import { Scope } from "./ui.ts"; export type Page = { window: Window; server: FakeBerry; /** Disposed once the test ends. */ scope: Scope; settle(): Promise; }; export function withBerry( test: (page: Page) => Promise, { url, globals }: { url?: string; globals?: Record } = {}, ): Promise { const server = fakeBerry(); return withDom(async (window) => { const { setToken } = await import("./client.ts"); const scope = new Scope(); setToken("test"); try { await test({ window, server, scope, settle: () => window.happyDOM.waitUntilComplete() }); } finally { scope.dispose(); setToken(null); } }, { url, globals: { ...server.globals, ...globals } }); }