@@ -29,6 +29,7 @@ const onePixelPng = Buffer.from(
2929const onePixelPngBase64 = onePixelPng . toString ( "base64" ) ;
3030const wrappedOnePixelPngBase64 = onePixelPngBase64 . match ( / .{ 1 , 24 } / g) ?. join ( "\r\n" ) ?? "" ;
3131const parameterizedPngDataUrl = `data:image/png;charset=utf-8;name=../../ignored.svg;base64,${ wrappedOnePixelPngBase64 } ` ;
32+ const csvBase64 = Buffer . from ( "name,value\nexample,1\n" ) . toString ( "base64" ) ;
3233
3334function firstMockArg (
3435 mock : { mock : { calls : readonly unknown [ ] [ ] } } ,
@@ -106,10 +107,29 @@ describe("runMessageAction media behavior", () => {
106107 await resetMessageActionMediaMocks ( ) ;
107108 } ) ;
108109
109- it . each ( [ "send" , "sendAttachment" , "reply" , "upload-file" , "setGroupIcon" ] as const ) (
110- "normalizes parameterized, line-wrapped image data URLs for %s" ,
111- async ( action ) => {
112- const args : Record < string , unknown > = { buffer : parameterizedPngDataUrl } ;
110+ it . each (
111+ ( [ "send" , "sendAttachment" , "reply" , "upload-file" , "setGroupIcon" ] as const ) . flatMap (
112+ ( action ) => [
113+ {
114+ action,
115+ buffer : parameterizedPngDataUrl ,
116+ base64 : onePixelPngBase64 ,
117+ contentType : "image/png" ,
118+ filename : "attachment.png" ,
119+ } ,
120+ {
121+ action,
122+ buffer : `data:text/csv;base64,${ csvBase64 } ` ,
123+ base64 : csvBase64 ,
124+ contentType : "text/csv" ,
125+ filename : "attachment.csv" ,
126+ } ,
127+ ] ,
128+ ) ,
129+ ) (
130+ "normalizes $contentType data URLs and infers filenames for $action" ,
131+ async ( { action, buffer, base64, contentType, filename } ) => {
132+ const args : Record < string , unknown > = { buffer } ;
113133
114134 await hydrateAttachmentParamsForAction ( {
115135 cfg : { } ,
@@ -120,14 +140,13 @@ describe("runMessageAction media behavior", () => {
120140 mediaPolicy : { mode : "host" } ,
121141 } ) ;
122142
123- expect ( args . contentType ) . toBe ( "image/png" ) ;
143+ expect ( args . contentType ) . toBe ( contentType ) ;
144+ expect ( args . filename ) . toBe ( filename ) ;
124145 if ( action === "send" ) {
125146 expect ( args . media ) . toBe ( "buffer://message-send/attachment" ) ;
126- expect ( args . filename ) . toBe ( "attachment.png" ) ;
127147 } else {
128- expect ( canonicalizeBase64 ( String ( args . buffer ) ) ) . toBe ( onePixelPngBase64 ) ;
148+ expect ( canonicalizeBase64 ( String ( args . buffer ) ) ) . toBe ( base64 ) ;
129149 }
130- expect ( args . filename ) . not . toBe ( "../../ignored.svg" ) ;
131150 } ,
132151 ) ;
133152
@@ -329,6 +348,7 @@ describe("runMessageAction media behavior", () => {
329348
330349 const payload = requireActionPayload ( result ) ;
331350 expect ( payload . contentType ) . toBe ( "image/png" ) ;
351+ expect ( payload . filename ) . toBe ( "attachment.png" ) ;
332352 expect ( canonicalizeBase64 ( String ( payload . buffer ) ) ) . toBe ( onePixelPngBase64 ) ;
333353 } ,
334354 ) ;
@@ -617,6 +637,7 @@ describe("runMessageAction media behavior", () => {
617637
618638 const handlerParams = firstMockArg ( handleActionMock , "handleAction" ) ;
619639 expect ( handlerParams . contentType ) . toBe ( "image/png" ) ;
640+ expect ( handlerParams . filename ) . toBe ( "attachment.png" ) ;
620641 expect ( canonicalizeBase64 ( String ( handlerParams . buffer ) ) ) . toBe ( onePixelPngBase64 ) ;
621642 } ) ;
622643
0 commit comments