diff --git a/src/cli/gateway.sigterm.test.ts b/src/cli/gateway.sigterm.test.ts deleted file mode 100644 index 6a4df1db75f..00000000000 --- a/src/cli/gateway.sigterm.test.ts +++ /dev/null @@ -1,8 +0,0 @@ -import { describe, it } from "vitest"; - -describe("gateway SIGTERM", () => { - it.skip("covered by runGatewayLoop signal tests in src/cli/gateway-cli/run-loop.test.ts", () => { - // Kept as a placeholder to document why the old child-process integration - // case was retired: it duplicated run-loop signal coverage at high runtime cost. - }); -}); diff --git a/src/discord/monitor/message-handler.bot-self-filter.test.ts b/src/discord/monitor/message-handler.bot-self-filter.test.ts index b3442f89618..7f5b2276987 100644 --- a/src/discord/monitor/message-handler.bot-self-filter.test.ts +++ b/src/discord/monitor/message-handler.bot-self-filter.test.ts @@ -1,8 +1,20 @@ -import { describe, it, vi } from "vitest"; +import { describe, expect, it, vi } from "vitest"; import type { OpenClawConfig } from "../../config/types.js"; -import { createDiscordMessageHandler } from "./message-handler.js"; import { createNoopThreadBindingManager } from "./thread-bindings.js"; +const preflightDiscordMessageMock = vi.hoisted(() => vi.fn()); +const processDiscordMessageMock = vi.hoisted(() => vi.fn()); + +vi.mock("./message-handler.preflight.js", () => ({ + preflightDiscordMessage: preflightDiscordMessageMock, +})); + +vi.mock("./message-handler.process.js", () => ({ + processDiscordMessage: processDiscordMessageMock, +})); + +const { createDiscordMessageHandler } = await import("./message-handler.js"); + const BOT_USER_ID = "bot-123"; function createHandlerParams(overrides?: Partial<{ botUserId: string }>) { @@ -14,6 +26,11 @@ function createHandlerParams(overrides?: Partial<{ botUserId: string }>) { groupPolicy: "allowlist", }, }, + messages: { + inbound: { + debounceMs: 0, + }, + }, }; return { cfg, @@ -39,35 +56,74 @@ function createHandlerParams(overrides?: Partial<{ botUserId: string }>) { }; } -function createMessageData(authorId: string) { +function createMessageData(authorId: string, channelId = "ch-1") { return { + author: { id: authorId, bot: authorId === BOT_USER_ID }, message: { id: "msg-1", author: { id: authorId, bot: authorId === BOT_USER_ID }, content: "hello", - channel_id: "ch-1", + channel_id: channelId, }, - channel_id: "ch-1", + channel_id: channelId, + }; +} + +function createPreflightContext(channelId = "ch-1") { + return { + data: { + channel_id: channelId, + message: { + id: `msg-${channelId}`, + channel_id: channelId, + attachments: [], + }, + }, + message: { + id: `msg-${channelId}`, + channel_id: channelId, + attachments: [], + }, + route: { + sessionKey: `agent:main:discord:channel:${channelId}`, + }, + baseSessionKey: `agent:main:discord:channel:${channelId}`, + messageChannelId: channelId, }; } describe("createDiscordMessageHandler bot-self filter", () => { - it("skips bot-own messages before debouncer", async () => { + it("skips bot-own messages before the debounce queue", async () => { + preflightDiscordMessageMock.mockReset(); + processDiscordMessageMock.mockReset(); + const handler = createDiscordMessageHandler(createHandlerParams()); - await handler(createMessageData(BOT_USER_ID) as never, {} as never); + + await expect( + handler(createMessageData(BOT_USER_ID) as never, {} as never), + ).resolves.toBeUndefined(); + + expect(preflightDiscordMessageMock).not.toHaveBeenCalled(); + expect(processDiscordMessageMock).not.toHaveBeenCalled(); }); - it("processes messages from other users", async () => { + it("enqueues non-bot messages for processing", async () => { + preflightDiscordMessageMock.mockReset(); + processDiscordMessageMock.mockReset(); + preflightDiscordMessageMock.mockImplementation( + async (params: { data: { channel_id: string } }) => + createPreflightContext(params.data.channel_id), + ); + const handler = createDiscordMessageHandler(createHandlerParams()); - try { - await handler( - createMessageData("user-456") as never, - { - fetchChannel: vi.fn().mockResolvedValue(null), - } as never, - ); - } catch { - // Expected: pipeline fails without full mock, but it passed the filter. - } + + await expect( + handler(createMessageData("user-456") as never, {} as never), + ).resolves.toBeUndefined(); + + await vi.waitFor(() => { + expect(preflightDiscordMessageMock).toHaveBeenCalledTimes(1); + expect(processDiscordMessageMock).toHaveBeenCalledTimes(1); + }); }); });