@@ -3728,72 +3728,113 @@ describe("deliverOutboundPayloads", () => {
37283728 ] ) ;
37293729 } ) ;
37303730
3731- it ( "uses adapter-provided formatted senders and scoped media roots when available" , async ( ) => {
3732- const sendText = vi . fn ( async ( { text } : { text : string } ) => ( {
3733- channel : "line" as const ,
3734- messageId : `fallback:${ text } ` ,
3735- } ) ) ;
3736- const sendMedia = vi . fn ( async ( { text } : { text : string } ) => ( {
3737- channel : "line" as const ,
3738- messageId : `media:${ text } ` ,
3739- } ) ) ;
3740- const sendFormattedText = vi . fn ( async ( { text } : { text : string } ) => [
3741- { channel : "line" as const , messageId : `fmt:${ text } :1` } ,
3742- { channel : "line" as const , messageId : `fmt:${ text } :2` } ,
3743- ] ) ;
3744- const sendFormattedMedia = vi . fn (
3745- async ( { text } : { text : string ; mediaLocalRoots ?: readonly string [ ] } ) => ( {
3731+ it . each ( [
3732+ { ordinaryMedia : true , caption : "photo" , mediaUrls : [ "file:///tmp/f.png" ] } ,
3733+ { ordinaryMedia : false , caption : "photo" , mediaUrls : [ "file:///tmp/f.png" ] } ,
3734+ { ordinaryMedia : false , caption : "" , mediaUrls : [ "file:///tmp/f.png" ] } ,
3735+ {
3736+ ordinaryMedia : false ,
3737+ caption : "album" ,
3738+ mediaUrls : [ "file:///tmp/f.png" , "file:///tmp/g.png" ] ,
3739+ } ,
3740+ ] ) (
3741+ "uses formatted senders and scoped roots ($ordinaryMedia, $caption)" ,
3742+ async ( { ordinaryMedia, caption, mediaUrls } ) => {
3743+ const sendText = vi . fn ( async ( { text } : { text : string } ) => ( {
37463744 channel : "line" as const ,
3747- messageId : `fmt-media:${ text } ` ,
3748- } ) ,
3749- ) ;
3750- setTestOutbound ( { sendText, sendMedia, sendFormattedText, sendFormattedMedia } , "line" ) ;
3751-
3752- const textResults = await deliverOutboundPayloads ( {
3753- cfg : { channels : { line : { } } } as OpenClawConfig ,
3754- channel : "line" ,
3755- to : "U123" ,
3756- accountId : "default" ,
3757- payloads : [ { text : "hello **boss**" } ] ,
3758- } ) ;
3745+ messageId : `fallback:${ text } ` ,
3746+ } ) ) ;
3747+ const sendMedia = vi . fn ( async ( { text } : { text : string } ) => ( {
3748+ channel : "line" as const ,
3749+ messageId : `media:${ text } ` ,
3750+ } ) ) ;
3751+ const sendFormattedText = vi . fn ( async ( { text } : { text : string } ) => [
3752+ { channel : "line" as const , messageId : `fmt:${ text } :1` } ,
3753+ { channel : "line" as const , messageId : `fmt:${ text } :2` } ,
3754+ ] ) ;
3755+ const sendFormattedMedia = vi . fn (
3756+ async ( {
3757+ text,
3758+ mediaUrl,
3759+ } : {
3760+ text : string ;
3761+ mediaUrl : string ;
3762+ mediaLocalRoots ?: readonly string [ ] ;
3763+ } ) => ( {
3764+ channel : "line" as const ,
3765+ messageId : `fmt-media:${ mediaUrl } :${ text } ` ,
3766+ } ) ,
3767+ ) ;
3768+ setTestOutbound (
3769+ {
3770+ sendText,
3771+ ...( ordinaryMedia ? { sendMedia } : { } ) ,
3772+ sendFormattedText,
3773+ sendFormattedMedia,
3774+ } ,
3775+ "line" ,
3776+ ) ;
37593777
3760- expect ( sendFormattedText ) . toHaveBeenCalledTimes ( 1 ) ;
3761- const formattedTextOptions = requireMockCallArg ( sendFormattedText , "sendFormattedText" ) as
3762- | { accountId ?: unknown ; text ?: unknown ; to ?: unknown }
3763- | undefined ;
3764- expect ( formattedTextOptions ?. to ) . toBe ( "U123" ) ;
3765- expect ( formattedTextOptions ?. text ) . toBe ( "hello **boss**" ) ;
3766- expect ( formattedTextOptions ?. accountId ) . toBe ( "default" ) ;
3767- expect ( sendText ) . not . toHaveBeenCalled ( ) ;
3768- expect ( textResults . map ( ( entry ) => entry . messageId ) ) . toEqual ( [
3769- "fmt:hello **boss**:1" ,
3770- "fmt:hello **boss**:2" ,
3771- ] ) ;
3778+ const textResults = await deliverOutboundPayloads ( {
3779+ cfg : { channels : { line : { } } } as OpenClawConfig ,
3780+ channel : "line" ,
3781+ to : "U123" ,
3782+ accountId : "default" ,
3783+ payloads : [ { text : "hello **boss**" } ] ,
3784+ } ) ;
37723785
3773- const cfg = { channels : { line : { } } } as OpenClawConfig ;
3774- await deliverOutboundPayloads ( {
3775- cfg,
3776- channel : "line" ,
3777- to : "U123" ,
3778- payloads : [ { text : "photo" , mediaUrl : "file:///tmp/f.png" } ] ,
3779- session : { agentId : "work" } ,
3780- } ) ;
3786+ expect ( sendFormattedText ) . toHaveBeenCalledTimes ( 1 ) ;
3787+ const formattedTextOptions = requireMockCallArg ( sendFormattedText , "sendFormattedText" ) as
3788+ | { accountId ?: unknown ; text ?: unknown ; to ?: unknown }
3789+ | undefined ;
3790+ expect ( formattedTextOptions ?. to ) . toBe ( "U123" ) ;
3791+ expect ( formattedTextOptions ?. text ) . toBe ( "hello **boss**" ) ;
3792+ expect ( formattedTextOptions ?. accountId ) . toBe ( "default" ) ;
3793+ expect ( sendText ) . not . toHaveBeenCalled ( ) ;
3794+ expect ( textResults . map ( ( entry ) => entry . messageId ) ) . toEqual ( [
3795+ "fmt:hello **boss**:1" ,
3796+ "fmt:hello **boss**:2" ,
3797+ ] ) ;
3798+
3799+ const cfg = { channels : { line : { } } } as OpenClawConfig ;
3800+ const onDeliveredPayload = vi . fn ( ) ;
3801+ const mediaResults = await deliverOutboundPayloads ( {
3802+ cfg,
3803+ channel : "line" ,
3804+ to : "U123" ,
3805+ payloads : [ { text : caption , mediaUrls } ] ,
3806+ session : { agentId : "work" } ,
3807+ onDeliveredPayload,
3808+ } ) ;
37813809
3782- expect ( sendFormattedMedia ) . toHaveBeenCalledTimes ( 1 ) ;
3783- const sendFormattedMediaCall = requireMockCallArg ( sendFormattedMedia , "sendFormattedMedia" ) as
3784- | { mediaLocalRoots ?: string [ ] ; mediaUrl ?: unknown ; text ?: unknown ; to ?: unknown }
3785- | undefined ;
3786- expect ( sendFormattedMediaCall ?. to ) . toBe ( "U123" ) ;
3787- expect ( sendFormattedMediaCall ?. text ) . toBe ( "photo" ) ;
3788- expect ( sendFormattedMediaCall ?. mediaUrl ) . toBe ( "file:///tmp/f.png" ) ;
3789- expect ( sendFormattedMediaCall ?. mediaLocalRoots ) . toContain ( expectedPreferredTmpRoot ) ;
3790- expect (
3791- sendFormattedMediaCall ?. mediaLocalRoots ?. some ( ( root ) =>
3792- root . endsWith ( path . join ( ".openclaw" , "workspace-work" ) ) ,
3793- ) ,
3794- ) . toBe ( true ) ;
3795- expect ( sendMedia ) . not . toHaveBeenCalled ( ) ;
3796- } ) ;
3810+ expect ( sendFormattedMedia ) . toHaveBeenCalledTimes ( mediaUrls . length ) ;
3811+ const sendFormattedMediaCall = requireMockCallArg (
3812+ sendFormattedMedia ,
3813+ "sendFormattedMedia" ,
3814+ ) as
3815+ | { mediaLocalRoots ?: string [ ] ; mediaUrl ?: unknown ; text ?: unknown ; to ?: unknown }
3816+ | undefined ;
3817+ expect ( sendFormattedMediaCall ?. to ) . toBe ( "U123" ) ;
3818+ expect ( sendFormattedMediaCall ?. text ) . toBe ( caption ) ;
3819+ expect ( sendFormattedMediaCall ?. mediaUrl ) . toBe ( mediaUrls [ 0 ] ) ;
3820+ expect ( sendFormattedMedia . mock . calls . map ( ( [ call ] ) => [ call . text , call . mediaUrl ] ) ) . toEqual (
3821+ mediaUrls . map ( ( mediaUrl , index ) => [ index === 0 ? caption : "" , mediaUrl ] ) ,
3822+ ) ;
3823+ expect ( mediaResults . map ( ( entry ) => entry . messageId ) ) . toEqual (
3824+ mediaUrls . map ( ( mediaUrl , index ) => `fmt-media:${ mediaUrl } :${ index === 0 ? caption : "" } ` ) ,
3825+ ) ;
3826+ expect ( onDeliveredPayload ) . toHaveBeenCalledWith (
3827+ expect . objectContaining ( { text : caption , mediaUrls } ) ,
3828+ ) ;
3829+ expect ( sendFormattedMediaCall ?. mediaLocalRoots ) . toContain ( expectedPreferredTmpRoot ) ;
3830+ expect (
3831+ sendFormattedMediaCall ?. mediaLocalRoots ?. some ( ( root ) =>
3832+ root . endsWith ( path . join ( ".openclaw" , "workspace-work" ) ) ,
3833+ ) ,
3834+ ) . toBe ( true ) ;
3835+ expect ( sendMedia ) . not . toHaveBeenCalled ( ) ;
3836+ } ,
3837+ ) ;
37973838
37983839 it ( "persists formatted sub-send results before a later adapter chunk fails" , async ( ) => {
37993840 const firstResult = { channel : "line" as const , messageId : "fmt-1" } ;
@@ -6046,7 +6087,7 @@ describe("deliverOutboundPayloads", () => {
60466087 } ) ;
60476088 const warnCall = requireMockCall ( logMocks . warn , "warn" ) ;
60486089 expect ( warnCall [ 0 ] ) . toBe (
6049- "Plugin outbound adapter does not implement sendMedia; media URLs will be dropped and text fallback will be used" ,
6090+ "Plugin outbound adapter does not implement sendMedia or sendFormattedMedia ; media URLs will be dropped and text fallback will be used" ,
60506091 ) ;
60516092 const warnContext = warnCall [ 1 ] as { channel ?: unknown ; mediaCount ?: unknown } | undefined ;
60526093 expect ( warnContext ?. channel ) . toBe ( "matrix" ) ;
@@ -6071,7 +6112,7 @@ describe("deliverOutboundPayloads", () => {
60716112 expect ( requireMockCallArg ( sendText , "sendText" ) . text ) . toBe ( "caption" ) ;
60726113 const warnCall = requireMockCall ( logMocks . warn , "warn" ) ;
60736114 expect ( warnCall [ 0 ] ) . toBe (
6074- "Plugin outbound adapter does not implement sendMedia; media URLs will be dropped and text fallback will be used" ,
6115+ "Plugin outbound adapter does not implement sendMedia or sendFormattedMedia ; media URLs will be dropped and text fallback will be used" ,
60756116 ) ;
60766117 const warnContext = warnCall [ 1 ] as { channel ?: unknown ; mediaCount ?: unknown } | undefined ;
60776118 expect ( warnContext ?. channel ) . toBe ( "matrix" ) ;
@@ -6091,13 +6132,13 @@ describe("deliverOutboundPayloads", () => {
60916132 payloads : [ { text : " " , mediaUrl : "https://example.com/file.png" } ] ,
60926133 } ) ,
60936134 ) . rejects . toThrow (
6094- "Plugin outbound adapter does not implement sendMedia and no text fallback is available for media payload" ,
6135+ "Plugin outbound adapter does not implement sendMedia or sendFormattedMedia and no text fallback is available for media payload" ,
60956136 ) ;
60966137
60976138 expect ( sendText ) . not . toHaveBeenCalled ( ) ;
60986139 const warnCall = requireMockCall ( logMocks . warn , "warn" ) ;
60996140 expect ( warnCall [ 0 ] ) . toBe (
6100- "Plugin outbound adapter does not implement sendMedia; media URLs will be dropped and text fallback will be used" ,
6141+ "Plugin outbound adapter does not implement sendMedia or sendFormattedMedia ; media URLs will be dropped and text fallback will be used" ,
61016142 ) ;
61026143 const warnContext = warnCall [ 1 ] as { channel ?: unknown ; mediaCount ?: unknown } | undefined ;
61036144 expect ( warnContext ?. channel ) . toBe ( "matrix" ) ;
0 commit comments