import '../assert_dev_env.js';
import { spawn_backend } from './spawn_backend.js';
import { bootstrap } from '../transports/bootstrap.js';
import { create_fetch_transport } from '../transports/fetch_transport.js';
/**
 * Spawn the test binary described by `config`, bootstrap a keeper, and
 * return the enriched handle.
 *
 * The keeper transport is constructed against `config.base_url` with no
 * initial cookies; `bootstrap()` populates its jar with the session
 * cookie returned by `POST {config.bootstrap_path}`. Subsequent calls
 * against `bootstrapped.keeper_transport` are authenticated as keeper.
 *
 * Mirrors the composition `default_cross_process_setup`'s caller would
 * otherwise hand-roll in every consumer's `globalSetup`.
 */
export const bootstrap_backend = async (config) => {
    const handle = await spawn_backend(config);
    try {
        const keeper_transport = create_fetch_transport({ base_url: config.base_url });
        const keeper = await bootstrap({ transport: keeper_transport, config });
        return {
            ...handle,
            keeper_transport,
            keeper_account: keeper.account,
            keeper_actor: keeper.actor,
            keeper_cookies: keeper.cookies,
        };
    }
    catch (err) {
        await handle.teardown();
        throw err;
    }
};
