* fix(slack): use named import for @slack/bolt App class
The default import `import bolt from '@slack/bolt'` followed by
`const { App } = bolt` doesn't work correctly in Bun due to ESM/CJS
interop issues. The default export comes through as a function rather
than the module object.
Switching to a named import `import { App } from '@slack/bolt'`
resolves the issue and allows the Slack provider to start successfully.
* fix(slack): align Bolt mock with named App export
---------
Co-authored-by: Peter Steinberger <steipete@gmail.com>
295 lines
7.8 KiB
TypeScript
295 lines
7.8 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import { monitorSlackProvider } from "./monitor.js";
|
|
|
|
const sendMock = vi.fn();
|
|
const replyMock = vi.fn();
|
|
const updateLastRouteMock = vi.fn();
|
|
const reactMock = vi.fn();
|
|
let config: Record<string, unknown> = {};
|
|
const getSlackHandlers = () =>
|
|
(
|
|
globalThis as {
|
|
__slackHandlers?: Map<string, (args: unknown) => Promise<void>>;
|
|
}
|
|
).__slackHandlers;
|
|
const getSlackClient = () =>
|
|
(globalThis as { __slackClient?: Record<string, unknown> }).__slackClient;
|
|
|
|
vi.mock("../config/config.js", async (importOriginal) => {
|
|
const actual = await importOriginal<typeof import("../config/config.js")>();
|
|
return {
|
|
...actual,
|
|
loadConfig: () => config,
|
|
};
|
|
});
|
|
|
|
vi.mock("../auto-reply/reply.js", () => ({
|
|
getReplyFromConfig: (...args: unknown[]) => replyMock(...args),
|
|
}));
|
|
|
|
vi.mock("./send.js", () => ({
|
|
sendMessageSlack: (...args: unknown[]) => sendMock(...args),
|
|
}));
|
|
|
|
vi.mock("../config/sessions.js", () => ({
|
|
resolveStorePath: vi.fn(() => "/tmp/clawdbot-sessions.json"),
|
|
updateLastRoute: (...args: unknown[]) => updateLastRouteMock(...args),
|
|
resolveSessionKey: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("@slack/bolt", () => {
|
|
const handlers = new Map<string, (args: unknown) => Promise<void>>();
|
|
(globalThis as { __slackHandlers?: typeof handlers }).__slackHandlers =
|
|
handlers;
|
|
const client = {
|
|
auth: { test: vi.fn().mockResolvedValue({ user_id: "bot-user" }) },
|
|
conversations: {
|
|
info: vi.fn().mockResolvedValue({
|
|
channel: { name: "dm", is_im: true },
|
|
}),
|
|
},
|
|
users: {
|
|
info: vi.fn().mockResolvedValue({
|
|
user: { profile: { display_name: "Ada" } },
|
|
}),
|
|
},
|
|
reactions: {
|
|
add: (...args: unknown[]) => reactMock(...args),
|
|
},
|
|
};
|
|
(globalThis as { __slackClient?: typeof client }).__slackClient = client;
|
|
class App {
|
|
client = client;
|
|
event(name: string, handler: (args: unknown) => Promise<void>) {
|
|
handlers.set(name, handler);
|
|
}
|
|
command() {
|
|
/* no-op */
|
|
}
|
|
start = vi.fn().mockResolvedValue(undefined);
|
|
stop = vi.fn().mockResolvedValue(undefined);
|
|
}
|
|
return { App, default: { App } };
|
|
});
|
|
|
|
const flush = () => new Promise((resolve) => setTimeout(resolve, 0));
|
|
|
|
async function waitForEvent(name: string) {
|
|
for (let i = 0; i < 10; i += 1) {
|
|
if (getSlackHandlers()?.has(name)) return;
|
|
await flush();
|
|
}
|
|
}
|
|
|
|
beforeEach(() => {
|
|
config = {
|
|
messages: {
|
|
responsePrefix: "PFX",
|
|
ackReaction: "👀",
|
|
ackReactionScope: "group-mentions",
|
|
},
|
|
slack: { dm: { enabled: true }, groupDm: { enabled: false } },
|
|
routing: { allowFrom: [] },
|
|
};
|
|
sendMock.mockReset().mockResolvedValue(undefined);
|
|
replyMock.mockReset();
|
|
updateLastRouteMock.mockReset();
|
|
reactMock.mockReset();
|
|
});
|
|
|
|
describe("monitorSlackProvider tool results", () => {
|
|
it("sends tool summaries with responsePrefix", async () => {
|
|
replyMock.mockImplementation(async (_ctx, opts) => {
|
|
await opts?.onToolResult?.({ text: "tool update" });
|
|
return { text: "final reply" };
|
|
});
|
|
|
|
const controller = new AbortController();
|
|
const run = monitorSlackProvider({
|
|
botToken: "bot-token",
|
|
appToken: "app-token",
|
|
abortSignal: controller.signal,
|
|
});
|
|
|
|
await waitForEvent("message");
|
|
const handler = getSlackHandlers()?.get("message");
|
|
if (!handler) throw new Error("Slack message handler not registered");
|
|
|
|
await handler({
|
|
event: {
|
|
type: "message",
|
|
user: "U1",
|
|
text: "hello",
|
|
ts: "123",
|
|
channel: "C1",
|
|
channel_type: "im",
|
|
},
|
|
});
|
|
|
|
await flush();
|
|
controller.abort();
|
|
await run;
|
|
|
|
expect(sendMock).toHaveBeenCalledTimes(2);
|
|
expect(sendMock.mock.calls[0][1]).toBe("PFX tool update");
|
|
expect(sendMock.mock.calls[1][1]).toBe("PFX final reply");
|
|
});
|
|
|
|
it("accepts channel messages when mentionPatterns match", async () => {
|
|
config = {
|
|
messages: { responsePrefix: "PFX" },
|
|
slack: {
|
|
dm: { enabled: true },
|
|
groupDm: { enabled: false },
|
|
channels: { C1: { allow: true, requireMention: true } },
|
|
},
|
|
routing: {
|
|
allowFrom: [],
|
|
groupChat: { mentionPatterns: ["\\bclawd\\b"] },
|
|
},
|
|
};
|
|
replyMock.mockResolvedValue({ text: "hi" });
|
|
|
|
const controller = new AbortController();
|
|
const run = monitorSlackProvider({
|
|
botToken: "bot-token",
|
|
appToken: "app-token",
|
|
abortSignal: controller.signal,
|
|
});
|
|
|
|
await waitForEvent("message");
|
|
const handler = getSlackHandlers()?.get("message");
|
|
if (!handler) throw new Error("Slack message handler not registered");
|
|
|
|
await handler({
|
|
event: {
|
|
type: "message",
|
|
user: "U1",
|
|
text: "clawd: hello",
|
|
ts: "123",
|
|
channel: "C1",
|
|
channel_type: "channel",
|
|
},
|
|
});
|
|
|
|
await flush();
|
|
controller.abort();
|
|
await run;
|
|
|
|
expect(replyMock).toHaveBeenCalledTimes(1);
|
|
expect(replyMock.mock.calls[0][0].WasMentioned).toBe(true);
|
|
});
|
|
|
|
it("threads replies when incoming message is in a thread", async () => {
|
|
replyMock.mockResolvedValue({ text: "thread reply" });
|
|
|
|
const controller = new AbortController();
|
|
const run = monitorSlackProvider({
|
|
botToken: "bot-token",
|
|
appToken: "app-token",
|
|
abortSignal: controller.signal,
|
|
});
|
|
|
|
await waitForEvent("message");
|
|
const handler = getSlackHandlers()?.get("message");
|
|
if (!handler) throw new Error("Slack message handler not registered");
|
|
|
|
await handler({
|
|
event: {
|
|
type: "message",
|
|
user: "U1",
|
|
text: "hello",
|
|
ts: "123",
|
|
thread_ts: "456",
|
|
channel: "C1",
|
|
channel_type: "im",
|
|
},
|
|
});
|
|
|
|
await flush();
|
|
controller.abort();
|
|
await run;
|
|
|
|
expect(sendMock).toHaveBeenCalledTimes(1);
|
|
expect(sendMock.mock.calls[0][2]).toMatchObject({ threadTs: "456" });
|
|
});
|
|
|
|
it("keeps replies in channel root when message is not threaded", async () => {
|
|
replyMock.mockResolvedValue({ text: "root reply" });
|
|
|
|
const controller = new AbortController();
|
|
const run = monitorSlackProvider({
|
|
botToken: "bot-token",
|
|
appToken: "app-token",
|
|
abortSignal: controller.signal,
|
|
});
|
|
|
|
await waitForEvent("message");
|
|
const handler = getSlackHandlers()?.get("message");
|
|
if (!handler) throw new Error("Slack message handler not registered");
|
|
|
|
await handler({
|
|
event: {
|
|
type: "message",
|
|
user: "U1",
|
|
text: "hello",
|
|
ts: "789",
|
|
channel: "C1",
|
|
channel_type: "im",
|
|
},
|
|
});
|
|
|
|
await flush();
|
|
controller.abort();
|
|
await run;
|
|
|
|
expect(sendMock).toHaveBeenCalledTimes(1);
|
|
expect(sendMock.mock.calls[0][2]).toMatchObject({ threadTs: undefined });
|
|
});
|
|
|
|
it("reacts to mention-gated room messages when ackReaction is enabled", async () => {
|
|
replyMock.mockResolvedValue(undefined);
|
|
const client = getSlackClient();
|
|
if (!client) throw new Error("Slack client not registered");
|
|
const conversations = client.conversations as {
|
|
info: ReturnType<typeof vi.fn>;
|
|
};
|
|
conversations.info.mockResolvedValueOnce({
|
|
channel: { name: "general", is_channel: true },
|
|
});
|
|
|
|
const controller = new AbortController();
|
|
const run = monitorSlackProvider({
|
|
botToken: "bot-token",
|
|
appToken: "app-token",
|
|
abortSignal: controller.signal,
|
|
});
|
|
|
|
await waitForEvent("message");
|
|
const handler = getSlackHandlers()?.get("message");
|
|
if (!handler) throw new Error("Slack message handler not registered");
|
|
|
|
await handler({
|
|
event: {
|
|
type: "message",
|
|
user: "U1",
|
|
text: "<@bot-user> hello",
|
|
ts: "456",
|
|
channel: "C1",
|
|
channel_type: "channel",
|
|
},
|
|
});
|
|
|
|
await flush();
|
|
controller.abort();
|
|
await run;
|
|
|
|
expect(reactMock).toHaveBeenCalledWith({
|
|
channel: "C1",
|
|
timestamp: "456",
|
|
name: "👀",
|
|
});
|
|
});
|
|
});
|