From fb072ea8ca8521836210fba14b66a7eaf02c0c2b Mon Sep 17 00:00:00 2001 From: Shakker Date: Sun, 10 May 2026 21:21:36 +0100 Subject: [PATCH] test: tighten discord gateway proxy assertions --- .../src/monitor/provider.proxy.test.ts | 65 ++++++++++--------- 1 file changed, 34 insertions(+), 31 deletions(-) diff --git a/extensions/discord/src/monitor/provider.proxy.test.ts b/extensions/discord/src/monitor/provider.proxy.test.ts index 6b3076fc422..cda46e91401 100644 --- a/extensions/discord/src/monitor/provider.proxy.test.ts +++ b/extensions/discord/src/monitor/provider.proxy.test.ts @@ -278,8 +278,9 @@ describe("createDiscordGatewayPlugin", () => { expect((plugin as unknown as { gatewayInfo?: { url?: string } }).gatewayInfo?.url).toBe( "wss://gateway.discord.gg/", ); - expect(runtime.log).toHaveBeenCalledWith( - expect.stringContaining("discord: gateway metadata lookup failed transiently"), + expect(runtime.log).toHaveBeenCalledTimes(1); + expect(String(runtime.log.mock.calls[0]?.[0])).toContain( + "discord: gateway metadata lookup failed transiently", ); } @@ -326,12 +327,13 @@ describe("createDiscordGatewayPlugin", () => { await registerGatewayClientWithMetadata({ plugin, fetchMock: globalFetchMock }); - expect(globalFetchMock).toHaveBeenCalledWith( - "https://discord.com/api/v10/gateway/bot", - expect.objectContaining({ - headers: { Authorization: "Bot token-123" }, - }), - ); + expect(globalFetchMock).toHaveBeenCalledTimes(1); + const fetchInit = globalFetchMock.mock.calls[0]?.[1] as + | { headers?: Record; signal?: unknown } + | undefined; + expect(globalFetchMock.mock.calls[0]?.[0]).toBe("https://discord.com/api/v10/gateway/bot"); + expect(fetchInit?.headers).toEqual({ Authorization: "Bot token-123" }); + expect(fetchInit?.signal).toBeInstanceOf(AbortSignal); expect(baseRegisterClientSpy).toHaveBeenCalledTimes(1); }); @@ -346,16 +348,14 @@ describe("createDiscordGatewayPlugin", () => { .createWebSocket; createWebSocket("wss://gateway.discord.gg"); - expect(httpsAgentSpy).toHaveBeenCalledWith( - expect.objectContaining({ lookup: expect.any(Function) }), - ); - expect(webSocketSpy).toHaveBeenCalledWith( - "wss://gateway.discord.gg", - expect.objectContaining({ - agent: getLastAgent(), - handshakeTimeout: 30_000, - }), - ); + expect(httpsAgentSpy).toHaveBeenCalledTimes(1); + const httpsAgentOptions = httpsAgentSpy.mock.calls[0]?.[0] as { lookup?: unknown } | undefined; + expect(Object.keys(httpsAgentOptions ?? {})).toEqual(["lookup"]); + expect(typeof httpsAgentOptions?.lookup).toBe("function"); + expect(webSocketSpy).toHaveBeenCalledWith("wss://gateway.discord.gg", { + agent: getLastAgent(), + handshakeTimeout: 30_000, + }); expect(wsProxyAgentSpy).not.toHaveBeenCalled(); }); @@ -467,10 +467,10 @@ describe("createDiscordGatewayPlugin", () => { createWebSocket("wss://gateway.discord.gg"); expect(wsProxyAgentSpy).toHaveBeenCalledWith("http://127.0.0.1:8080"); - expect(webSocketSpy).toHaveBeenCalledWith( - "wss://gateway.discord.gg", - expect.objectContaining({ agent: getLastProxyAgent(), handshakeTimeout: 30_000 }), - ); + expect(webSocketSpy).toHaveBeenCalledWith("wss://gateway.discord.gg", { + agent: getLastProxyAgent(), + handshakeTimeout: 30_000, + }); expect(runtime.log).toHaveBeenCalledWith("discord: gateway proxy enabled"); expect(runtime.error).not.toHaveBeenCalled(); }); @@ -498,12 +498,13 @@ describe("createDiscordGatewayPlugin", () => { await registerGatewayClientWithMetadata({ plugin, fetchMock: globalFetchMock }); - expect(globalFetchMock).toHaveBeenCalledWith( - "https://discord.com/api/v10/gateway/bot", - expect.objectContaining({ - headers: { Authorization: "Bot token-123" }, - }), - ); + expect(globalFetchMock).toHaveBeenCalledTimes(1); + const fetchInit = globalFetchMock.mock.calls[0]?.[1] as + | { headers?: Record; signal?: unknown } + | undefined; + expect(globalFetchMock.mock.calls[0]?.[0]).toBe("https://discord.com/api/v10/gateway/bot"); + expect(fetchInit?.headers).toEqual({ Authorization: "Bot token-123" }); + expect(fetchInit?.signal).toBeInstanceOf(AbortSignal); expect(baseRegisterClientSpy).toHaveBeenCalledTimes(1); }); @@ -546,7 +547,8 @@ describe("createDiscordGatewayPlugin", () => { }); expect(Object.getPrototypeOf(plugin)).not.toBe(GatewayPlugin.prototype); - expect(runtime.error).toHaveBeenCalledWith(expect.stringContaining("loopback host")); + expect(runtime.error).toHaveBeenCalledTimes(1); + expect(String(runtime.error.mock.calls[0]?.[0])).toContain("loopback host"); expect(runtime.log).not.toHaveBeenCalled(); }); @@ -577,8 +579,9 @@ describe("createDiscordGatewayPlugin", () => { expect((plugin as unknown as { gatewayInfo?: { url?: string } }).gatewayInfo?.url).toBe( "wss://gateway.discord.gg/", ); - expect(runtime.log).toHaveBeenCalledWith( - expect.stringContaining("discord: gateway metadata lookup failed transiently"), + expect(runtime.log).toHaveBeenCalledTimes(1); + expect(String(runtime.log.mock.calls[0]?.[0])).toContain( + "discord: gateway metadata lookup failed transiently", ); });