withRequest
uses #tcm/host (withCall)
Run fn as if answering a request, for tests of server functions: no network (sessions kept in memory).
import { withCall } from "#tcm/host"; export async function withRequest<T>( init: { method?: string; url?: string; headers?: Record<string, string>; cookies?: Record<string, string>; ip?: string; }, fn: () => T | Promise<T>, ): Promise<{ value: T; setCookies: string[] }> { const cookieHeader = Object.entries(init.cookies ?? {}) .map(([k, v]) => `${k}=${encodeURIComponent(v)}`) .join("; "); const headers = { ...init.headers, ...(cookieHeader ? { cookie: cookieHeader } : {}) }; const r = await withCall( { method: init.method ?? "POST", url: init.url ?? "/", headers, ip: init.ip ?? "127.0.0.1", signal: new AbortController().signal, }, { secure: false }, async () => fn(), ); if (!r.ok) throw r.error; return { value: r.value, setCookies: r.setCookies }; }