import { describe, expect, it, vi } from "vitest"; const { undiciFetchMock, proxyAgentSpy } = vi.hoisted(() => ({ undiciFetchMock: vi.fn(), proxyAgentSpy: vi.fn(), })); vi.mock("undici", () => { class ProxyAgent { proxyUrl: string; constructor(proxyUrl: string) { if (proxyUrl === "bad-proxy") { throw new Error("bad proxy"); } this.proxyUrl = proxyUrl; proxyAgentSpy(proxyUrl); } } return { ProxyAgent, fetch: undiciFetchMock, }; }); describe("resolveDiscordRestFetch", () => { it("uses undici proxy fetch when a proxy URL is configured", async () => { const runtime = { log: vi.fn(), error: vi.fn(), exit: vi.fn(), } as const; undiciFetchMock.mockReset().mockResolvedValue(new Response("ok", { status: 200 })); proxyAgentSpy.mockReset(); const { __testing } = await import("./provider.js"); const fetcher = __testing.resolveDiscordRestFetch("http://proxy.test:8080", runtime); await fetcher("https://discord.com/api/v10/oauth2/applications/@me"); expect(proxyAgentSpy).toHaveBeenCalledWith("http://proxy.test:8080"); expect(undiciFetchMock).toHaveBeenCalledWith( "https://discord.com/api/v10/oauth2/applications/@me", expect.objectContaining({ dispatcher: expect.objectContaining({ proxyUrl: "http://proxy.test:8080" }), }), ); expect(runtime.log).toHaveBeenCalledWith("discord: rest proxy enabled"); expect(runtime.error).not.toHaveBeenCalled(); }); it("falls back to global fetch when proxy URL is invalid", async () => { const runtime = { log: vi.fn(), error: vi.fn(), exit: vi.fn(), } as const; const { __testing } = await import("./provider.js"); const fetcher = __testing.resolveDiscordRestFetch("bad-proxy", runtime); expect(fetcher).toBe(fetch); expect(runtime.error).toHaveBeenCalled(); expect(runtime.log).not.toHaveBeenCalled(); }); });