@@ -7,12 +7,27 @@ const handleDiscordActionMock = vi
77 . mockResolvedValue ( { content : [ ] , details : { ok : true } } ) ;
88const { handleDiscordMessageAction } = await import ( "./handle-action.js" ) ;
99
10+ function discordConfig ( actions ?: Record < string , boolean > ) : OpenClawConfig {
11+ return {
12+ channels : { discord : { token : "tok" , ...( actions ? { actions } : { } ) } } ,
13+ } as OpenClawConfig ;
14+ }
15+
16+ function defaultActionOptions ( ) {
17+ return {
18+ mediaAccess : undefined ,
19+ mediaLocalRoots : undefined ,
20+ mediaReadFile : undefined ,
21+ } ;
22+ }
23+
1024describe ( "handleDiscordMessageAction" , ( ) => {
1125 beforeEach ( ( ) => {
1226 handleDiscordActionMock . mockClear ( ) ;
1327 } ) ;
1428
1529 it ( "uses trusted requesterSenderId for moderation and ignores params senderUserId" , async ( ) => {
30+ const cfg = discordConfig ( { moderation : true } ) ;
1631 await handleDiscordMessageAction ( {
1732 action : "timeout" ,
1833 params : {
@@ -21,9 +36,7 @@ describe("handleDiscordMessageAction", () => {
2136 durationMin : 5 ,
2237 senderUserId : "spoofed-admin-id" ,
2338 } ,
24- cfg : {
25- channels : { discord : { token : "tok" , actions : { moderation : true } } } ,
26- } as OpenClawConfig ,
39+ cfg,
2740 requesterSenderId : "trusted-sender-id" ,
2841 toolContext : { currentChannelProvider : "discord" } ,
2942 } ) ;
@@ -36,26 +49,19 @@ describe("handleDiscordMessageAction", () => {
3649 durationMinutes : 5 ,
3750 senderUserId : "trusted-sender-id" ,
3851 } ) ,
39- expect . objectContaining ( {
40- channels : {
41- discord : expect . objectContaining ( {
42- token : "tok" ,
43- } ) ,
44- } ,
45- } ) ,
52+ cfg ,
4653 ) ;
4754 } ) ;
4855
4956 it ( "falls back to toolContext.currentMessageId for reactions" , async ( ) => {
57+ const cfg = discordConfig ( ) ;
5058 await handleDiscordMessageAction ( {
5159 action : "react" ,
5260 params : {
5361 channelId : "123" ,
5462 emoji : "ok" ,
5563 } ,
56- cfg : {
57- channels : { discord : { token : "tok" } } ,
58- } as OpenClawConfig ,
64+ cfg,
5965 toolContext : { currentMessageId : "9001" } ,
6066 } ) ;
6167
@@ -66,20 +72,19 @@ describe("handleDiscordMessageAction", () => {
6672 messageId : "9001" ,
6773 emoji : "ok" ,
6874 } ) ,
69- expect . any ( Object ) ,
70- expect . any ( Object ) ,
75+ cfg ,
76+ defaultActionOptions ( ) ,
7177 ) ;
7278 } ) ;
7379
7480 it ( "falls back to Discord toolContext.currentChannelId for reaction targets" , async ( ) => {
81+ const cfg = discordConfig ( ) ;
7582 await handleDiscordMessageAction ( {
7683 action : "react" ,
7784 params : {
7885 emoji : "ok" ,
7986 } ,
80- cfg : {
81- channels : { discord : { token : "tok" } } ,
82- } as OpenClawConfig ,
87+ cfg,
8388 toolContext : {
8489 currentChannelProvider : "discord" ,
8590 currentChannelId : "user:U1" ,
@@ -94,20 +99,19 @@ describe("handleDiscordMessageAction", () => {
9499 messageId : "9001" ,
95100 emoji : "ok" ,
96101 } ) ,
97- expect . any ( Object ) ,
98- expect . any ( Object ) ,
102+ cfg ,
103+ defaultActionOptions ( ) ,
99104 ) ;
100105 } ) ;
101106
102107 it ( "falls back to Discord toolContext.currentChannelId for sends" , async ( ) => {
108+ const cfg = discordConfig ( ) ;
103109 await handleDiscordMessageAction ( {
104110 action : "send" ,
105111 params : {
106112 message : "hello" ,
107113 } ,
108- cfg : {
109- channels : { discord : { token : "tok" } } ,
110- } as OpenClawConfig ,
114+ cfg,
111115 toolContext : {
112116 currentChannelProvider : "discord" ,
113117 currentChannelId : "channel:123" ,
@@ -120,8 +124,8 @@ describe("handleDiscordMessageAction", () => {
120124 to : "channel:123" ,
121125 content : "hello" ,
122126 } ) ,
123- expect . any ( Object ) ,
124- expect . any ( Object ) ,
127+ cfg ,
128+ defaultActionOptions ( ) ,
125129 ) ;
126130 } ) ;
127131
@@ -131,6 +135,7 @@ describe("handleDiscordMessageAction", () => {
131135 localRoots : [ "/tmp/agent-root" ] ,
132136 readFile : mediaReadFile ,
133137 } ;
138+ const cfg = discordConfig ( ) ;
134139
135140 await handleDiscordMessageAction ( {
136141 action : "upload-file" ,
@@ -144,9 +149,7 @@ describe("handleDiscordMessageAction", () => {
144149 __sessionKey : "session-1" ,
145150 __agentId : "agent-1" ,
146151 } ,
147- cfg : {
148- channels : { discord : { token : "tok" } } ,
149- } as OpenClawConfig ,
152+ cfg,
150153 mediaAccess,
151154 mediaLocalRoots : [ "/tmp/agent-root" ] ,
152155 mediaReadFile,
@@ -164,7 +167,7 @@ describe("handleDiscordMessageAction", () => {
164167 __sessionKey : "session-1" ,
165168 __agentId : "agent-1" ,
166169 } ) ,
167- expect . any ( Object ) ,
170+ cfg ,
168171 {
169172 mediaAccess,
170173 mediaLocalRoots : [ "/tmp/agent-root" ] ,
@@ -174,14 +177,13 @@ describe("handleDiscordMessageAction", () => {
174177 } ) ;
175178
176179 it ( "falls back to Discord toolContext.currentChannelId for upload-file" , async ( ) => {
180+ const cfg = discordConfig ( ) ;
177181 await handleDiscordMessageAction ( {
178182 action : "upload-file" ,
179183 params : {
180184 path : "/tmp/agent-root/image.png" ,
181185 } ,
182- cfg : {
183- channels : { discord : { token : "tok" } } ,
184- } as OpenClawConfig ,
186+ cfg,
185187 toolContext : {
186188 currentChannelProvider : "discord" ,
187189 currentChannelId : "channel:123" ,
@@ -195,8 +197,8 @@ describe("handleDiscordMessageAction", () => {
195197 content : "" ,
196198 mediaUrl : "/tmp/agent-root/image.png" ,
197199 } ) ,
198- expect . any ( Object ) ,
199- expect . any ( Object ) ,
200+ cfg ,
201+ defaultActionOptions ( ) ,
200202 ) ;
201203 } ) ;
202204
@@ -207,9 +209,7 @@ describe("handleDiscordMessageAction", () => {
207209 params : {
208210 to : "channel:123" ,
209211 } ,
210- cfg : {
211- channels : { discord : { token : "tok" } } ,
212- } as OpenClawConfig ,
212+ cfg : discordConfig ( ) ,
213213 } ) ,
214214 ) . rejects . toThrow ( / u p l o a d - f i l e r e q u i r e s f i l e P a t h , p a t h , o r m e d i a / i) ;
215215
@@ -218,6 +218,7 @@ describe("handleDiscordMessageAction", () => {
218218
219219 it ( "maps thread-reply filePath to Discord threadReply with media read context" , async ( ) => {
220220 const mediaReadFile = vi . fn ( async ( ) => Buffer . from ( "report" ) ) ;
221+ const cfg = discordConfig ( { threads : true } ) ;
221222
222223 await handleDiscordMessageAction ( {
223224 action : "thread-reply" ,
@@ -226,9 +227,7 @@ describe("handleDiscordMessageAction", () => {
226227 message : "thread update" ,
227228 filePath : "/tmp/agent-root/report.md" ,
228229 } ,
229- cfg : {
230- channels : { discord : { token : "tok" , actions : { threads : true } } } ,
231- } as OpenClawConfig ,
230+ cfg,
232231 mediaLocalRoots : [ "/tmp/agent-root" ] ,
233232 mediaReadFile,
234233 } ) ;
@@ -240,26 +239,26 @@ describe("handleDiscordMessageAction", () => {
240239 content : "thread update" ,
241240 mediaUrl : "/tmp/agent-root/report.md" ,
242241 } ) ,
243- expect . any ( Object ) ,
242+ cfg ,
244243 {
245244 mediaLocalRoots : [ "/tmp/agent-root" ] ,
245+ mediaAccess : undefined ,
246246 mediaReadFile,
247247 } ,
248248 ) ;
249249 } ) ;
250250
251251 it ( "forwards top-level components on sends" , async ( ) => {
252252 const components = { blocks : [ { type : "text" , text : "Pick one" } ] } ;
253+ const cfg = discordConfig ( ) ;
253254
254255 await handleDiscordMessageAction ( {
255256 action : "send" ,
256257 params : {
257258 message : "hello" ,
258259 components,
259260 } ,
260- cfg : {
261- channels : { discord : { token : "tok" } } ,
262- } as OpenClawConfig ,
261+ cfg,
263262 toolContext : {
264263 currentChannelProvider : "discord" ,
265264 currentChannelId : "channel:123" ,
@@ -273,8 +272,8 @@ describe("handleDiscordMessageAction", () => {
273272 content : "hello" ,
274273 components,
275274 } ) ,
276- expect . any ( Object ) ,
277- expect . any ( Object ) ,
275+ cfg ,
276+ defaultActionOptions ( ) ,
278277 ) ;
279278 } ) ;
280279
@@ -285,9 +284,7 @@ describe("handleDiscordMessageAction", () => {
285284 params : {
286285 message : "hello" ,
287286 } ,
288- cfg : {
289- channels : { discord : { token : "tok" } } ,
290- } as OpenClawConfig ,
287+ cfg : discordConfig ( ) ,
291288 toolContext : {
292289 currentChannelProvider : "telegram" ,
293290 currentChannelId : "channel:123" ,
@@ -305,9 +302,7 @@ describe("handleDiscordMessageAction", () => {
305302 params : {
306303 emoji : "ok" ,
307304 } ,
308- cfg : {
309- channels : { discord : { token : "tok" } } ,
310- } as OpenClawConfig ,
305+ cfg : discordConfig ( ) ,
311306 toolContext : {
312307 currentChannelProvider : "telegram" ,
313308 currentChannelId : "user:U1" ,
@@ -327,9 +322,7 @@ describe("handleDiscordMessageAction", () => {
327322 channelId : "123" ,
328323 emoji : "ok" ,
329324 } ,
330- cfg : {
331- channels : { discord : { token : "tok" } } ,
332- } as OpenClawConfig ,
325+ cfg : discordConfig ( ) ,
333326 } ) ,
334327 ) . rejects . toThrow ( / m e s s a g e I d r e q u i r e d / i) ;
335328
0 commit comments