import { describe, expect, test } from "bun:test"; import { createSealClientForUrl } from "./factory.js"; import { formatCookieHeader, resolveSealCustomHeaders } from "./request-headers.js"; describe("formatCookieHeader", () => { test("returns undefined when there are no cookies", () => { expect(formatCookieHeader(undefined)).toBeUndefined(); expect(formatCookieHeader({})).toBeUndefined(); }); test("joins cookies with a semicolon and a space", () => { expect(formatCookieHeader({ a: "1", b: "2" })).toBe("a=1; b=2"); }); }); describe("resolveSealCustomHeaders", () => { test("returns an empty map when no custom headers are configured", () => { expect(resolveSealCustomHeaders(undefined)).toEqual({}); }); test("keeps configured headers and renders cookies", () => { expect(resolveSealCustomHeaders({ headers: { "X-Tenant": "acme" }, cookies: { sdp_session: "token" } })).toEqual({ "X-Tenant": "acme", Cookie: "sdp_session=token" }); }); test("lets configured cookies replace a raw Cookie header", () => { expect(resolveSealCustomHeaders({ headers: { Cookie: "stale=1" }, cookies: { fresh: "2" } })).toEqual({ Cookie: "fresh=2" }); }); }); describe("createSealClientForUrl", () => { test("sends custom headers and keeps the session Authorization", async () => { const originalFetch = globalThis.fetch; let sent: Headers | undefined; globalThis.fetch = (async (input: RequestInfo | URL, init?: RequestInit) => { const request = input instanceof Request ? input : new Request(input, init); sent = new Headers(request.headers); return new Response(JSON.stringify({ data: {} }), { headers: { "content-type": "application/json" } }); }) as typeof fetch; try { const client = createSealClientForUrl("https://corp-a.sealai.cc", "session-token", { Cookie: "sdp_session=token", Authorization: "Bearer stale" }); await client.get("api/v1/auth/me"); } finally { globalThis.fetch = originalFetch; } expect(sent?.get("cookie")).toBe("sdp_session=token"); expect(sent?.get("authorization")).toBe("Bearer session-token"); }); });