


















@@ -36,6 +36,29 @@ vi.mock("undici", () => {
36363737let resolveDiscordRestFetch: typeof import("./rest-fetch.js").resolveDiscordRestFetch;
383839+type MockWithCalls = {
40+mock: { calls: unknown[][] };
41+};
42+43+function objectArgAt(
44+mock: MockWithCalls,
45+callIndex: number,
46+argIndex: number,
47+): Record<string, unknown> {
48+const value = mock.mock.calls[callIndex]?.[argIndex];
49+if (value === undefined || value === null || typeof value !== "object" || Array.isArray(value)) {
50+throw new Error(`expected call ${callIndex} argument ${argIndex} to be an object`);
51+}
52+return value as Record<string, unknown>;
53+}
54+55+function recordField(value: unknown, field: string): Record<string, unknown> {
56+if (value === undefined || value === null || typeof value !== "object" || Array.isArray(value)) {
57+throw new Error(`expected ${field} to be an object`);
58+}
59+return value as Record<string, unknown>;
60+}
61+3962describe("resolveDiscordRestFetch", () => {
4063beforeAll(async () => {
4164({ resolveDiscordRestFetch } = await import("./rest-fetch.js"));
@@ -60,21 +83,16 @@ describe("resolveDiscordRestFetch", () => {
60836184await fetcher("https://discord.com/api/v10/oauth2/applications/@me");
628563-expect(proxyAgentSpy).toHaveBeenCalledWith(
64-expect.objectContaining({
65-uri: "http://127.0.0.1:8080",
66-allowH2: false,
67-}),
68-);
69-expect(undiciFetchMock).toHaveBeenCalledWith(
86+const proxyOptions = objectArgAt(proxyAgentSpy, 0, 0);
87+expect(proxyOptions.uri).toBe("http://127.0.0.1:8080");
88+expect(proxyOptions.allowH2).toBe(false);
89+expect(undiciFetchMock.mock.calls[0]?.[0]).toBe(
7090"https://discord.com/api/v10/oauth2/applications/@me",
71-expect.objectContaining({
72-dispatcher: expect.objectContaining({
73-uri: "http://127.0.0.1:8080",
74-options: expect.objectContaining({ allowH2: false }),
75-}),
76-}),
7791);
92+const fetchOptions = objectArgAt(undiciFetchMock, 0, 1);
93+const dispatcher = recordField(fetchOptions.dispatcher, "dispatcher");
94+expect(dispatcher.uri).toBe("http://127.0.0.1:8080");
95+expect(recordField(dispatcher.options, "dispatcher.options").allowH2).toBe(false);
7896expect(runtime.log).toHaveBeenCalledWith("discord: rest proxy enabled");
7997expect(runtime.error).not.toHaveBeenCalled();
8098});
@@ -103,7 +121,7 @@ describe("resolveDiscordRestFetch", () => {
103121104122expect(fetcher).toBe(fetch);
105123expect(proxyAgentSpy).not.toHaveBeenCalled();
106-expect(runtime.error).toHaveBeenCalledWith(expect.stringContaining("loopback host"));
124+expect(String(runtime.error.mock.calls[0]?.[0])).toContain("loopback host");
107125expect(runtime.log).not.toHaveBeenCalled();
108126});
109127@@ -119,12 +137,9 @@ describe("resolveDiscordRestFetch", () => {
119137120138await fetcher("https://discord.com/api/v10/oauth2/applications/@me");
121139122-expect(proxyAgentSpy).toHaveBeenCalledWith(
123-expect.objectContaining({
124-uri: "http://[::1]:8080",
125-allowH2: false,
126-}),
127-);
140+const proxyOptions = objectArgAt(proxyAgentSpy, 0, 0);
141+expect(proxyOptions.uri).toBe("http://[::1]:8080");
142+expect(proxyOptions.allowH2).toBe(false);
128143expect(runtime.error).not.toHaveBeenCalled();
129144});
130145@@ -139,22 +154,20 @@ describe("resolveDiscordRestFetch", () => {
139154const fetcher = resolveDiscordRestFetch(undefined, runtime);
140155await fetcher("https://discord.com/api/v10/oauth2/applications/@me");
141156142-expect(agentSpy).toHaveBeenCalledWith(
143-expect.objectContaining({
144-allowH2: false,
145-connect: expect.objectContaining({ lookup: expect.any(Function) }),
146-}),
147-);
148-expect(undiciFetchMock).toHaveBeenCalledWith(
157+const agentOptions = objectArgAt(agentSpy, 0, 0);
158+expect(agentOptions.allowH2).toBe(false);
159+expect(typeof recordField(agentOptions.connect, "connect").lookup).toBe("function");
160+expect(undiciFetchMock.mock.calls[0]?.[0]).toBe(
149161"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-}),
162+);
163+const fetchOptions = objectArgAt(undiciFetchMock, 0, 1);
164+const dispatcherOptions = recordField(
165+recordField(fetchOptions.dispatcher, "dispatcher").options,
166+"dispatcher.options",
167+);
168+expect(dispatcherOptions.allowH2).toBe(false);
169+expect(typeof recordField(dispatcherOptions.connect, "dispatcher.options.connect").lookup).toBe(
170+"function",
158171);
159172expect(runtime.log).not.toHaveBeenCalled();
160173});
@@ -172,12 +185,9 @@ describe("resolveDiscordRestFetch", () => {
172185const fetcher = resolveDiscordRestFetch(undefined, runtime);
173186await fetcher("https://discord.com/api/v10/oauth2/applications/@me");
174187175-expect(proxyAgentSpy).toHaveBeenCalledWith(
176-expect.objectContaining({
177-uri: "http://127.0.0.1:7777",
178-allowH2: false,
179-}),
180-);
188+const proxyOptions = objectArgAt(proxyAgentSpy, 0, 0);
189+expect(proxyOptions.uri).toBe("http://127.0.0.1:7777");
190+expect(proxyOptions.allowH2).toBe(false);
181191expect(runtime.log).toHaveBeenCalledWith("discord: rest proxy enabled");
182192});
183193});
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。