From 4494844d17c7c8a6a27e5dbcbfa7a403a51a38b9 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Tue, 3 Mar 2026 01:41:08 +0000 Subject: [PATCH] refactor(tests): dedupe discord monitor e2e fixtures --- ...messages-mentionpatterns-match.e2e.test.ts | 87 ++++++++----------- 1 file changed, 38 insertions(+), 49 deletions(-) diff --git a/src/discord/monitor.tool-result.accepts-guild-messages-mentionpatterns-match.e2e.test.ts b/src/discord/monitor.tool-result.accepts-guild-messages-mentionpatterns-match.e2e.test.ts index a4007d8c6..1de585a38 100644 --- a/src/discord/monitor.tool-result.accepts-guild-messages-mentionpatterns-match.e2e.test.ts +++ b/src/discord/monitor.tool-result.accepts-guild-messages-mentionpatterns-match.e2e.test.ts @@ -138,6 +138,14 @@ function createDefaultThreadConfig(): LoadedConfig { } as LoadedConfig; } +function createGuildChannelPolicyConfig(requireMention: boolean) { + return { + dm: { enabled: true, policy: "open" as const }, + groupPolicy: "open" as const, + guilds: { "*": { requireMention } }, + }; +} + function createMentionRequiredGuildConfig( params: { messages?: LoadedConfig["messages"]; @@ -151,13 +159,7 @@ function createMentionRequiredGuildConfig( }, }, session: { store: "/tmp/openclaw-sessions.json" }, - channels: { - discord: { - dm: { enabled: true, policy: "open" }, - groupPolicy: "open", - guilds: { "*": { requireMention: true } }, - }, - }, + channels: { discord: createGuildChannelPolicyConfig(true) }, ...(params.messages ? { messages: params.messages } : {}), } as LoadedConfig; } @@ -177,18 +179,13 @@ function createGuildMessageEvent(params: { messagePatch?: Record; eventPatch?: Record; }) { + const messageBase = createDiscordMessageMeta(); return { message: { id: params.messageId, content: params.content, channelId: "c1", - timestamp: new Date().toISOString(), - type: MessageType.Default, - attachments: [], - embeds: [], - mentionedEveryone: false, - mentionedUsers: [], - mentionedRoles: [], + ...messageBase, author: { id: "u1", bot: false, username: "Ada" }, ...params.messagePatch, }, @@ -200,6 +197,18 @@ function createGuildMessageEvent(params: { }; } +function createDiscordMessageMeta() { + return { + timestamp: new Date().toISOString(), + type: MessageType.Default, + attachments: [], + embeds: [], + mentionedEveryone: false, + mentionedUsers: [], + mentionedRoles: [], + }; +} + function createThreadChannel(params: { includeStarter?: boolean } = {}) { return { type: ChannelType.GuildText, @@ -245,19 +254,14 @@ function createThreadClient( } function createThreadEvent(messageId: string, channel?: unknown) { + const messageBase = createDiscordMessageMeta(); return { message: { id: messageId, content: "thread reply", channelId: "t1", channel, - timestamp: new Date().toISOString(), - type: MessageType.Default, - attachments: [], - embeds: [], - mentionedEveryone: false, - mentionedUsers: [], - mentionedRoles: [], + ...messageBase, author: { id: "u2", bot: false, username: "Bob", tag: "Bob#2" }, }, author: { id: "u2", bot: false, username: "Bob", tag: "Bob#2" }, @@ -267,6 +271,15 @@ function createThreadEvent(messageId: string, channel?: unknown) { }; } +function captureThreadDispatchCtx() { + return captureNextDispatchCtx<{ + SessionKey?: string; + ParentSessionKey?: string; + ThreadStarterBody?: string; + ThreadLabel?: string; + }>(); +} + describe("discord tool result dispatch", () => { it( "accepts guild messages when mentionPatterns match", @@ -361,13 +374,7 @@ describe("discord tool result dispatch", () => { id: "m2", channelId: "c1", content: "bot reply", - timestamp: new Date().toISOString(), - type: MessageType.Default, - attachments: [], - embeds: [], - mentionedEveryone: false, - mentionedUsers: [], - mentionedRoles: [], + ...createDiscordMessageMeta(), author: { id: "bot-id", bot: true, username: "OpenClaw" }, }, }, @@ -393,12 +400,7 @@ describe("discord tool result dispatch", () => { }); it("forks thread sessions and injects starter context", async () => { - const getCapturedCtx = captureNextDispatchCtx<{ - SessionKey?: string; - ParentSessionKey?: string; - ThreadStarterBody?: string; - ThreadLabel?: string; - }>(); + const getCapturedCtx = captureThreadDispatchCtx(); const cfg = createDefaultThreadConfig(); const handler = await createHandler(cfg); const threadChannel = createThreadChannel({ includeStarter: true }); @@ -441,23 +443,10 @@ describe("discord tool result dispatch", () => { }); it("treats forum threads as distinct sessions without channel payloads", async () => { - const getCapturedCtx = captureNextDispatchCtx<{ - SessionKey?: string; - ParentSessionKey?: string; - ThreadStarterBody?: string; - ThreadLabel?: string; - }>(); + const getCapturedCtx = captureThreadDispatchCtx(); const cfg = { - agent: { model: "anthropic/claude-opus-4-5", workspace: "/tmp/openclaw" }, - session: { store: "/tmp/openclaw-sessions.json" }, - channels: { - discord: { - dm: { enabled: true, policy: "open" }, - groupPolicy: "open", - guilds: { "*": { requireMention: false } }, - }, - }, + ...createDefaultThreadConfig(), routing: { allowFrom: [] }, } as ReturnType;