From fe5cc8f3baf5163b2c8468ab46206e4b4a1a6749 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Sat, 14 Feb 2026 19:58:20 +0000 Subject: [PATCH] refactor(test): dedupe discord category handler setup --- ...ends-status-replies-responseprefix.test.ts | 181 +++++++----------- 1 file changed, 67 insertions(+), 114 deletions(-) diff --git a/src/discord/monitor.tool-result.sends-status-replies-responseprefix.test.ts b/src/discord/monitor.tool-result.sends-status-replies-responseprefix.test.ts index 160e93b45b6..7433ab45891 100644 --- a/src/discord/monitor.tool-result.sends-status-replies-responseprefix.test.ts +++ b/src/discord/monitor.tool-result.sends-status-replies-responseprefix.test.ts @@ -5,6 +5,8 @@ import { createDiscordMessageHandler } from "./monitor.js"; import { __resetDiscordChannelInfoCacheForTest } from "./monitor/message-utils.js"; import { __resetDiscordThreadStarterCacheForTest } from "./monitor/threading.js"; +type Config = ReturnType; + const sendMock = vi.fn(); const reactMock = vi.fn(); const updateLastRouteMock = vi.fn(); @@ -54,6 +56,66 @@ beforeEach(() => { __resetDiscordThreadStarterCacheForTest(); }); +const CATEGORY_GUILD_CFG = { + agents: { + defaults: { + model: "anthropic/claude-opus-4-5", + workspace: "/tmp/openclaw", + }, + }, + session: { store: "/tmp/openclaw-sessions.json" }, + channels: { + discord: { + dm: { enabled: true, policy: "open" }, + guilds: { + "*": { + requireMention: false, + channels: { c1: { allow: true } }, + }, + }, + }, + }, + routing: { allowFrom: [] }, +} as Config; + +function createCategoryGuildHandler() { + return createDiscordMessageHandler({ + cfg: CATEGORY_GUILD_CFG, + discordConfig: CATEGORY_GUILD_CFG.channels.discord, + accountId: "default", + token: "token", + runtime: { + log: vi.fn(), + error: vi.fn(), + exit: (code: number): never => { + throw new Error(`exit ${code}`); + }, + }, + botUserId: "bot-id", + guildHistories: new Map(), + historyLimit: 0, + mediaMaxBytes: 10_000, + textLimit: 2000, + replyToMode: "off", + dmEnabled: true, + groupDmEnabled: false, + guildEntries: { + "*": { requireMention: false, channels: { c1: { allow: true } } }, + }, + }); +} + +function createCategoryGuildClient() { + return { + fetchChannel: vi.fn().mockResolvedValue({ + type: ChannelType.GuildText, + name: "general", + parentId: "category-1", + }), + rest: { get: vi.fn() }, + } as unknown as Client; +} + describe("discord tool result dispatch", () => { it("sends status replies with responsePrefix", async () => { const cfg = { @@ -286,7 +348,6 @@ describe("discord tool result dispatch", () => { }); it("uses channel id allowlists for non-thread channels with categories", async () => { - const { createDiscordMessageHandler } = await import("./monitor.js"); let capturedCtx: { SessionKey?: string } | undefined; dispatchMock.mockImplementationOnce(async ({ ctx, dispatcher }) => { capturedCtx = ctx; @@ -294,61 +355,8 @@ describe("discord tool result dispatch", () => { return { queuedFinal: true, counts: { final: 1 } }; }); - const cfg = { - agents: { - defaults: { - model: "anthropic/claude-opus-4-5", - workspace: "/tmp/openclaw", - }, - }, - session: { store: "/tmp/openclaw-sessions.json" }, - channels: { - discord: { - dm: { enabled: true, policy: "open" }, - guilds: { - "*": { - requireMention: false, - channels: { c1: { allow: true } }, - }, - }, - }, - }, - routing: { allowFrom: [] }, - } as ReturnType; - - const handler = createDiscordMessageHandler({ - cfg, - discordConfig: cfg.channels.discord, - accountId: "default", - token: "token", - runtime: { - log: vi.fn(), - error: vi.fn(), - exit: (code: number): never => { - throw new Error(`exit ${code}`); - }, - }, - botUserId: "bot-id", - guildHistories: new Map(), - historyLimit: 0, - mediaMaxBytes: 10_000, - textLimit: 2000, - replyToMode: "off", - dmEnabled: true, - groupDmEnabled: false, - guildEntries: { - "*": { requireMention: false, channels: { c1: { allow: true } } }, - }, - }); - - const client = { - fetchChannel: vi.fn().mockResolvedValue({ - type: ChannelType.GuildText, - name: "general", - parentId: "category-1", - }), - rest: { get: vi.fn() }, - } as unknown as Client; + const handler = createCategoryGuildHandler(); + const client = createCategoryGuildClient(); await handler( { @@ -377,7 +385,6 @@ describe("discord tool result dispatch", () => { }); it("prefixes group bodies with sender label", async () => { - const { createDiscordMessageHandler } = await import("./monitor.js"); let capturedBody = ""; dispatchMock.mockImplementationOnce(async ({ ctx, dispatcher }) => { capturedBody = ctx.Body ?? ""; @@ -385,61 +392,8 @@ describe("discord tool result dispatch", () => { return { queuedFinal: true, counts: { final: 1 } }; }); - const cfg = { - agents: { - defaults: { - model: "anthropic/claude-opus-4-5", - workspace: "/tmp/openclaw", - }, - }, - session: { store: "/tmp/openclaw-sessions.json" }, - channels: { - discord: { - dm: { enabled: true, policy: "open" }, - guilds: { - "*": { - requireMention: false, - channels: { c1: { allow: true } }, - }, - }, - }, - }, - routing: { allowFrom: [] }, - } as ReturnType; - - const handler = createDiscordMessageHandler({ - cfg, - discordConfig: cfg.channels.discord, - accountId: "default", - token: "token", - runtime: { - log: vi.fn(), - error: vi.fn(), - exit: (code: number): never => { - throw new Error(`exit ${code}`); - }, - }, - botUserId: "bot-id", - guildHistories: new Map(), - historyLimit: 0, - mediaMaxBytes: 10_000, - textLimit: 2000, - replyToMode: "off", - dmEnabled: true, - groupDmEnabled: false, - guildEntries: { - "*": { requireMention: false, channels: { c1: { allow: true } } }, - }, - }); - - const client = { - fetchChannel: vi.fn().mockResolvedValue({ - type: ChannelType.GuildText, - name: "general", - parentId: "category-1", - }), - rest: { get: vi.fn() }, - } as unknown as Client; + const handler = createCategoryGuildHandler(); + const client = createCategoryGuildClient(); await handler( { @@ -468,7 +422,6 @@ describe("discord tool result dispatch", () => { }); it("replies with pairing code and sender id when dmPolicy is pairing", async () => { - const { createDiscordMessageHandler } = await import("./monitor.js"); const cfg = { agents: { defaults: { @@ -480,7 +433,7 @@ describe("discord tool result dispatch", () => { channels: { discord: { dm: { enabled: true, policy: "pairing", allowFrom: [] } }, }, - } as ReturnType; + } as Config; const handler = createDiscordMessageHandler({ cfg,