Slack: add send blocks behavior tests

This commit is contained in:
Colin
2026-02-16 12:45:07 -05:00
committed by Peter Steinberger
parent e8a1d4171d
commit 82d132f1ba

View File

@@ -0,0 +1,65 @@
import type { WebClient } from "@slack/web-api";
import { describe, expect, it, vi } from "vitest";
vi.mock("../config/config.js", () => ({
loadConfig: () => ({}),
}));
vi.mock("./accounts.js", () => ({
resolveSlackAccount: () => ({
accountId: "default",
botToken: "xoxb-test",
botTokenSource: "config",
config: {},
}),
}));
const { sendMessageSlack } = await import("./send.js");
function createClient() {
return {
conversations: {
open: vi.fn(async () => ({ channel: { id: "D123" } })),
},
chat: {
postMessage: vi.fn(async () => ({ ts: "171234.567" })),
},
} as unknown as WebClient & {
conversations: { open: ReturnType<typeof vi.fn> };
chat: { postMessage: ReturnType<typeof vi.fn> };
};
}
describe("sendMessageSlack blocks", () => {
it("posts blocks with fallback text when message is empty", async () => {
const client = createClient();
const result = await sendMessageSlack("channel:C123", "", {
token: "xoxb-test",
client,
blocks: [{ type: "divider" }],
});
expect(client.conversations.open).not.toHaveBeenCalled();
expect(client.chat.postMessage).toHaveBeenCalledWith(
expect.objectContaining({
channel: "C123",
text: " ",
blocks: [{ type: "divider" }],
}),
);
expect(result).toEqual({ messageId: "171234.567", channelId: "C123" });
});
it("rejects blocks combined with mediaUrl", async () => {
const client = createClient();
await expect(
sendMessageSlack("channel:C123", "hi", {
token: "xoxb-test",
client,
mediaUrl: "https://example.com/image.png",
blocks: [{ type: "divider" }],
}),
).rejects.toThrow(/does not support blocks with mediaUrl/i);
expect(client.chat.postMessage).not.toHaveBeenCalled();
});
});