






















@@ -1,22 +1,34 @@
11import { beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
223-const { undiciFetchMock, proxyAgentSpy } = vi.hoisted(() => ({
3+const { undiciFetchMock, agentSpy, proxyAgentSpy } = vi.hoisted(() => ({
44undiciFetchMock: vi.fn(),
5+agentSpy: vi.fn(),
56proxyAgentSpy: vi.fn(),
67}));
7889vi.mock("undici", () => {
10+class Agent {
11+options: unknown;
12+constructor(options?: unknown) {
13+this.options = options;
14+agentSpy(options);
15+}
16+}
917class ProxyAgent {
10-proxyUrl: string;
11-constructor(proxyUrl: string) {
12-if (proxyUrl === "bad-proxy") {
18+options: unknown;
19+uri: string;
20+constructor(options: string | { uri: string; allowH2?: boolean }) {
21+const resolved = typeof options === "string" ? { uri: options } : options;
22+if (resolved.uri === "bad-proxy") {
1323throw new Error("bad proxy");
1424}
15-this.proxyUrl = proxyUrl;
16-proxyAgentSpy(proxyUrl);
25+this.options = resolved;
26+this.uri = resolved.uri;
27+proxyAgentSpy(resolved);
1728}
1829}
1930return {
31+ Agent,
2032 ProxyAgent,
2133fetch: undiciFetchMock,
2234};
@@ -32,6 +44,7 @@ describe("resolveDiscordRestFetch", () => {
3244beforeEach(() => {
3345vi.unstubAllEnvs();
3446undiciFetchMock.mockReset();
47+agentSpy.mockReset();
3548proxyAgentSpy.mockReset();
3649});
3750@@ -47,11 +60,19 @@ describe("resolveDiscordRestFetch", () => {
47604861await fetcher("https://discord.com/api/v10/oauth2/applications/@me");
496250-expect(proxyAgentSpy).toHaveBeenCalledWith("http://127.0.0.1:8080");
63+expect(proxyAgentSpy).toHaveBeenCalledWith(
64+expect.objectContaining({
65+uri: "http://127.0.0.1:8080",
66+allowH2: false,
67+}),
68+);
5169expect(undiciFetchMock).toHaveBeenCalledWith(
5270"https://discord.com/api/v10/oauth2/applications/@me",
5371expect.objectContaining({
54-dispatcher: expect.objectContaining({ proxyUrl: "http://127.0.0.1:8080" }),
72+dispatcher: expect.objectContaining({
73+uri: "http://127.0.0.1:8080",
74+options: expect.objectContaining({ allowH2: false }),
75+}),
5576}),
5677);
5778expect(runtime.log).toHaveBeenCalledWith("discord: rest proxy enabled");
@@ -98,10 +119,46 @@ describe("resolveDiscordRestFetch", () => {
9811999120await fetcher("https://discord.com/api/v10/oauth2/applications/@me");
100121101-expect(proxyAgentSpy).toHaveBeenCalledWith("http://[::1]:8080");
122+expect(proxyAgentSpy).toHaveBeenCalledWith(
123+expect.objectContaining({
124+uri: "http://[::1]:8080",
125+allowH2: false,
126+}),
127+);
102128expect(runtime.error).not.toHaveBeenCalled();
103129});
104130131+it("uses undici Agent with IPv4-first lookup when no discord proxy URL is configured", async () => {
132+const runtime = {
133+log: vi.fn(),
134+error: vi.fn(),
135+exit: vi.fn(),
136+} as const;
137+undiciFetchMock.mockResolvedValue(new Response("ok", { status: 200 }));
138+139+const fetcher = resolveDiscordRestFetch(undefined, runtime);
140+await fetcher("https://discord.com/api/v10/oauth2/applications/@me");
141+142+expect(agentSpy).toHaveBeenCalledWith(
143+expect.objectContaining({
144+allowH2: false,
145+connect: expect.objectContaining({ lookup: expect.any(Function) }),
146+}),
147+);
148+expect(undiciFetchMock).toHaveBeenCalledWith(
149+"https://discord.com/api/v10/oauth2/applications/@me",
150+expect.objectContaining({
151+dispatcher: expect.objectContaining({
152+options: expect.objectContaining({
153+allowH2: false,
154+connect: expect.objectContaining({ lookup: expect.any(Function) }),
155+}),
156+}),
157+}),
158+);
159+expect(runtime.log).not.toHaveBeenCalled();
160+});
161+105162it("uses debug proxy env when no discord proxy URL is configured", async () => {
106163vi.stubEnv("OPENCLAW_DEBUG_PROXY_ENABLED", "1");
107164vi.stubEnv("OPENCLAW_DEBUG_PROXY_URL", "http://127.0.0.1:7777");
@@ -115,7 +172,12 @@ describe("resolveDiscordRestFetch", () => {
115172const fetcher = resolveDiscordRestFetch(undefined, runtime);
116173await fetcher("https://discord.com/api/v10/oauth2/applications/@me");
117174118-expect(proxyAgentSpy).toHaveBeenCalledWith("http://127.0.0.1:7777");
175+expect(proxyAgentSpy).toHaveBeenCalledWith(
176+expect.objectContaining({
177+uri: "http://127.0.0.1:7777",
178+allowH2: false,
179+}),
180+);
119181expect(runtime.log).toHaveBeenCalledWith("discord: rest proxy enabled");
120182});
121183});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。