import { describe, expect, test } from "bun:test"; import { RPC_ERROR_CODES } from "../src/errors.ts"; import { RPC_IDENTITY_HEADER } from "../src/identity.ts"; import { httpTransport, rpcPath } from "../src/http.ts"; const target = { app: "billing", service: "billing", procedure: "createInvoice" }; function transportWith(handler: (request: Request) => Response | Promise) { return httpTransport({ resolveOrigin: () => "http://billing.test", fetch: (async (input: RequestInfo | URL, init?: RequestInit) => handler(new Request(input, init))) as typeof fetch, }); } describe("httpTransport", () => { test("posts to the private endpoint with identity", async () => { const transport = transportWith(async (request) => { expect(request.url).toBe("http://billing.test/__wrnexus/rpc/billing/createInvoice"); expect(request.headers.get(RPC_IDENTITY_HEADER)).toBe("token"); expect(request.headers.get("x-wrnexus-internal")).toBe("1"); expect(await request.json()).toEqual({ amountCents: 5 }); return Response.json({ ok: true, value: { invoiceId: "inv_1" } }); }); expect(await transport.call(target, { amountCents: 5 }, { identity: "token" })).toEqual({ ok: true, value: { invoiceId: "inv_1" }, }); }); test("classifies unavailable and malformed responses safely", async () => { const unavailable = transportWith(() => new Response("busy", { status: 503 })); const failed = await unavailable.call(target, {}, {}); expect(failed).toMatchObject({ code: RPC_ERROR_CODES.transport, retryable: true }); const malformed = transportWith(() => Response.json({ hello: "world" })); expect(await malformed.call(target, {}, {})).toMatchObject({ code: RPC_ERROR_CODES.malformed, retryable: false, }); }); test("uses the stable reserved path", () => { expect(rpcPath("billing", "createInvoice")).toBe("/__wrnexus/rpc/billing/createInvoice"); }); });