test: tighten slack client option assertions

This commit is contained in:
Shakker
2026-05-11 03:08:51 +01:00
parent b62fded4c2
commit 5e03a5d7d5

View File

@@ -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<typeof vi.fn>;
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<T extends { agent?: unknown }>(options: T): NonNullable<T["agent"]> {
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", () => {