11// Discord tests cover native command reply plugin behavior.
2- import { describe , expect , it , vi } from "vitest" ;
2+ import { beforeEach , describe , expect , it , vi } from "vitest" ;
33import { Container , TextDisplay } from "../internal/discord.js" ;
4+ import { createDiscordLoopbackRest } from "../send.test-harness.js" ;
45import {
56 deliverDiscordInteractionReply ,
67 hasRenderableReplyPayload ,
78 settleDiscordInteractionWithoutVisibleReply ,
89} from "./native-command-reply.js" ;
910
11+ const loadWebMediaMock = vi . hoisted ( ( ) => vi . fn ( ) ) ;
12+ vi . mock ( "openclaw/plugin-sdk/web-media" , ( ) => ( {
13+ loadWebMedia : loadWebMediaMock ,
14+ } ) ) ;
15+
1016function createInteraction ( ) {
1117 return {
1218 reply : vi . fn ( ) . mockResolvedValue ( { ok : true } ) ,
@@ -15,6 +21,10 @@ function createInteraction() {
1521}
1622
1723describe ( "deliverDiscordInteractionReply" , ( ) => {
24+ beforeEach ( ( ) => {
25+ loadWebMediaMock . mockReset ( ) ;
26+ } ) ;
27+
1828 it ( "sends component-only native command replies as follow-ups" , async ( ) => {
1929 const interaction = createInteraction ( ) ;
2030 const components = [ new Container ( [ new TextDisplay ( "Pick a model" ) ] ) ] ;
@@ -67,6 +77,45 @@ describe("deliverDiscordInteractionReply", () => {
6777 } ) ;
6878 expect ( interaction . followUp ) . not . toHaveBeenCalled ( ) ;
6979 } ) ;
80+
81+ it ( "sends the detected WebP media type across a real interaction multipart request" , async ( ) => {
82+ const loopback = await createDiscordLoopbackRest ( ) ;
83+ loadWebMediaMock . mockResolvedValue ( {
84+ buffer : Buffer . from ( "webp" ) ,
85+ fileName : "sticker.webp" ,
86+ contentType : "image/webp" ,
87+ kind : "image" ,
88+ } ) ;
89+ const interaction = {
90+ reply : vi . fn ( async ( data : unknown ) =>
91+ loopback . rest . post ( "/interactions/123/token/callback" , {
92+ body : { type : 4 , data } ,
93+ } ) ,
94+ ) ,
95+ followUp : vi . fn ( ) ,
96+ } ;
97+
98+ try {
99+ await deliverDiscordInteractionReply ( {
100+ interaction : interaction as never ,
101+ payload : {
102+ text : "sticker" ,
103+ mediaUrls : [ "file:///tmp/sticker.webp" ] ,
104+ } ,
105+ textLimit : 2000 ,
106+ preferFollowUp : false ,
107+ chunkMode : "length" ,
108+ } ) ;
109+
110+ const upload = loopback . requests . find ( ( request ) => request . method === "POST" ) ;
111+ expect ( upload ?. path ) . toContain ( "/interactions/123/token/callback" ) ;
112+ expect ( upload ?. contentType ) . toMatch ( / ^ m u l t i p a r t \/ f o r m - d a t a ; b o u n d a r y = / ) ;
113+ expect ( upload ?. body ) . toContain ( 'name="files[0]"; filename="sticker.webp"' ) ;
114+ expect ( upload ?. body ) . toContain ( "Content-Type: image/webp" ) ;
115+ } finally {
116+ await loopback . close ( ) ;
117+ }
118+ } ) ;
70119} ) ;
71120
72121describe ( "settleDiscordInteractionWithoutVisibleReply" , ( ) => {
0 commit comments