import { expect, test } from "bun:test"; import { getRequestContext } from "@wrnexus/core"; import { withServerFnRequestContext } from "../src/index.ts"; // The server-function RPC path ("/__wrnexus/rpc", what server.fn() calls from // the browser) is intercepted before fetchHandler in the dev server and would // otherwise run entirely outside the AsyncLocalStorage wrap. This is the // property that was broken: a handler invoked through that path must see the // request context, not `undefined`. test("a handler invoked through the server-function RPC path can read the request context", async () => { let seen: unknown; const innerHandler = async (request: Request): Promise => { seen = getRequestContext(); return new Response("ok"); }; const wrapped = withServerFnRequestContext(innerHandler); const request = new Request("http://localhost/__wrnexus/rpc", { method: "POST" }); await wrapped(request); expect(seen).toBeDefined(); expect((seen as { req: Request }).req).toBe(request); }); test("there is no context outside the wrap", () => { expect(getRequestContext()).toBeUndefined(); });