From 5e03a5d7d56d4aba757db15f60cb7f36003a552b Mon Sep 17 00:00:00 2001 From: Shakker Date: Mon, 11 May 2026 03:08:51 +0100 Subject: [PATCH] test: tighten slack client option assertions --- extensions/slack/src/client.test.ts | 117 +++++++++++++++++----------- 1 file changed, 71 insertions(+), 46 deletions(-) diff --git a/extensions/slack/src/client.test.ts b/extensions/slack/src/client.test.ts index f22bc822bf3b..58d9a5356d9a 100644 --- a/extensions/slack/src/client.test.ts +++ b/extensions/slack/src/client.test.ts @@ -23,6 +23,32 @@ let SLACK_DEFAULT_RETRY_OPTIONS: typeof import("./client.js").SLACK_DEFAULT_RETR let SLACK_WRITE_RETRY_OPTIONS: typeof import("./client.js").SLACK_WRITE_RETRY_OPTIONS; let WebClient: ReturnType; +const PROXY_KEYS = [ + "HTTPS_PROXY", + "HTTP_PROXY", + "https_proxy", + "http_proxy", + "NO_PROXY", + "no_proxy", +] as const; +const originalEnv = { ...process.env }; + +function clearProxyEnvForTest() { + for (const key of PROXY_KEYS) { + delete process.env[key]; + } +} + +function restoreProxyEnvForTest() { + for (const key of PROXY_KEYS) { + if (originalEnv[key] !== undefined) { + process.env[key] = originalEnv[key]; + } else { + delete process.env[key]; + } + } +} + function requireAgent(options: T): NonNullable { if (!options.agent) { throw new Error("expected proxy agent"); @@ -66,15 +92,30 @@ describe("slack web client config", () => { }); it("passes merged options into WebClient", () => { - createSlackWebClient("xoxb-test", { timeout: 1234 }); + const customAgent = {} as never; - expect(WebClient).toHaveBeenCalledWith( - "xoxb-test", - expect.objectContaining({ - timeout: 1234, + createSlackWebClient("xoxb-test", { timeout: 1234, agent: customAgent }); + + expect(WebClient).toHaveBeenCalledWith("xoxb-test", { + agent: customAgent, + retryConfig: SLACK_DEFAULT_RETRY_OPTIONS, + timeout: 1234, + }); + }); + + it("applies the default retry config when constructing a client without proxy env", () => { + clearProxyEnvForTest(); + try { + createSlackWebClient("xoxb-test", { timeout: 1234 }); + + expect(WebClient).toHaveBeenCalledWith("xoxb-test", { + agent: undefined, retryConfig: SLACK_DEFAULT_RETRY_OPTIONS, - }), - ); + timeout: 1234, + }); + } finally { + restoreProxyEnvForTest(); + } }); it("applies the write retry config when none is provided", () => { @@ -96,31 +137,34 @@ describe("slack web client config", () => { }); it("passes no-retry config into the write client by default", () => { - createSlackWriteClient("xoxb-test", { timeout: 4321 }); + const customAgent = {} as never; - expect(WebClient).toHaveBeenCalledWith( - "xoxb-test", - expect.objectContaining({ - maxRequestConcurrency: 1, - timeout: 4321, - retryConfig: SLACK_WRITE_RETRY_OPTIONS, - }), - ); + createSlackWriteClient("xoxb-test", { timeout: 4321, agent: customAgent }); + + expect(WebClient).toHaveBeenCalledWith("xoxb-test", { + agent: customAgent, + maxRequestConcurrency: 1, + retryConfig: SLACK_WRITE_RETRY_OPTIONS, + timeout: 4321, + }); }); it("reuses default write clients per token", () => { - const first = getSlackWriteClient("xoxb-test"); - const second = getSlackWriteClient("xoxb-test"); + clearProxyEnvForTest(); + try { + const first = getSlackWriteClient("xoxb-test"); + const second = getSlackWriteClient("xoxb-test"); - expect(second).toBe(first); - expect(WebClient).toHaveBeenCalledTimes(1); - expect(WebClient).toHaveBeenCalledWith( - "xoxb-test", - expect.objectContaining({ + expect(second).toBe(first); + expect(WebClient).toHaveBeenCalledTimes(1); + expect(WebClient).toHaveBeenCalledWith("xoxb-test", { + agent: undefined, maxRequestConcurrency: 1, retryConfig: SLACK_WRITE_RETRY_OPTIONS, - }), - ); + }); + } finally { + restoreProxyEnvForTest(); + } }); it("keeps default write clients separated by token", () => { @@ -144,31 +188,12 @@ describe("slack web client config", () => { }); describe("slack proxy agent", () => { - const originalEnv = { ...process.env }; - - const PROXY_KEYS = [ - "HTTPS_PROXY", - "HTTP_PROXY", - "https_proxy", - "http_proxy", - "NO_PROXY", - "no_proxy", - ]; - beforeEach(() => { - for (const key of PROXY_KEYS) { - delete process.env[key]; - } + clearProxyEnvForTest(); }); afterEach(() => { - for (const key of PROXY_KEYS) { - if (originalEnv[key] !== undefined) { - process.env[key] = originalEnv[key]; - } else { - delete process.env[key]; - } - } + restoreProxyEnvForTest(); }); it("sets agent from HTTPS_PROXY env var", () => {