@@ -46,6 +46,24 @@ function mockOptions(
4646 return value as Record < string , unknown > ;
4747}
4848
49+ function createMatrixReceipt (
50+ parts : Array < { messageId : string ; kind : "text" | "media" | "voice" ; replyToId ?: string } > ,
51+ ) {
52+ const firstPart = parts [ 0 ] ;
53+ return {
54+ primaryPlatformMessageId : firstPart ?. messageId ,
55+ platformMessageIds : parts . map ( ( { messageId } ) => messageId ) ,
56+ parts : parts . map ( ( { messageId, kind, replyToId } , index ) => ( {
57+ platformMessageId : messageId ,
58+ kind,
59+ index,
60+ ...( replyToId ? { replyToId } : { } ) ,
61+ } ) ) ,
62+ ...( firstPart ?. replyToId ? { replyToId : firstPart . replyToId } : { } ) ,
63+ sentAt : 1 ,
64+ } ;
65+ }
66+
4967describe ( "matrixOutbound cfg threading" , ( ) => {
5068 beforeEach ( ( ) => {
5169 mocks . sendMessageMatrix . mockReset ( ) ;
@@ -109,6 +127,44 @@ describe("matrixOutbound cfg threading", () => {
109127 expect ( options . replyToId ) . toBe ( "$reply" ) ;
110128 } ) ;
111129
130+ it . each ( [ "sendText" , "sendMedia" ] as const ) (
131+ "preserves the complete Matrix sender result through %s" ,
132+ async ( method ) => {
133+ const receipt = createMatrixReceipt ( [
134+ { messageId : "$first" , kind : "media" , replyToId : "$reply" } ,
135+ { messageId : "$last" , kind : "text" } ,
136+ ] ) ;
137+ mocks . sendMessageMatrix . mockResolvedValueOnce ( {
138+ messageId : "$last" ,
139+ roomId : "!room:example" ,
140+ primaryMessageId : "$first" ,
141+ receipt,
142+ content : "first\nlast" ,
143+ } ) ;
144+ const send = matrixOutbound [ method ] ;
145+ if ( ! send ) {
146+ throw new Error ( `matrixOutbound.${ method } missing` ) ;
147+ }
148+
149+ const result = await send ( {
150+ cfg : { } as OpenClawConfig ,
151+ to : "room:!room:example" ,
152+ text : "first\nlast" ,
153+ mediaUrl : "file:///tmp/photo.png" ,
154+ accountId : "default" ,
155+ } ) ;
156+
157+ expect ( result ) . toMatchObject ( {
158+ channel : "matrix" ,
159+ messageId : "$last" ,
160+ roomId : "!room:example" ,
161+ primaryMessageId : "$first" ,
162+ content : "first\nlast" ,
163+ } ) ;
164+ expect ( result . receipt ) . toBe ( receipt ) ;
165+ } ,
166+ ) ;
167+
112168 it ( "passes resolved cfg to sendMessageMatrix for media sends" , async ( ) => {
113169 const cfg = {
114170 channels : {
@@ -473,6 +529,65 @@ describe("matrixOutbound cfg threading", () => {
473529 expect ( mockOptions ( mocks . sendMessageMatrix , "sendMessageMatrix" , 1 ) . threadId ) . toBe ( "$thread" ) ;
474530 } ) ;
475531
532+ it ( "combines media payload receipts without inventing replies on later events" , async ( ) => {
533+ const firstReceipt = createMatrixReceipt ( [
534+ { messageId : "$image" , kind : "media" , replyToId : "$reply" } ,
535+ { messageId : "$caption-overflow" , kind : "text" } ,
536+ ] ) ;
537+ const secondReceipt = createMatrixReceipt ( [ { messageId : "$second-image" , kind : "media" } ] ) ;
538+ mocks . sendMessageMatrix
539+ . mockResolvedValueOnce ( {
540+ messageId : "$caption-overflow" ,
541+ roomId : "!room:example" ,
542+ primaryMessageId : "$image" ,
543+ receipt : firstReceipt ,
544+ content : "caption\noverflow" ,
545+ } )
546+ . mockResolvedValueOnce ( {
547+ messageId : "$second-image" ,
548+ roomId : "!room:example" ,
549+ primaryMessageId : "$second-image" ,
550+ receipt : secondReceipt ,
551+ content : "second image" ,
552+ } ) ;
553+
554+ const result = await matrixOutbound . sendPayload ! ( {
555+ cfg : { } as OpenClawConfig ,
556+ to : "room:!room:example" ,
557+ text : "caption" ,
558+ payload : {
559+ text : "caption" ,
560+ mediaUrls : [ "file:///tmp/a.png" , "file:///tmp/b.png" ] ,
561+ } ,
562+ accountId : "default" ,
563+ replyToId : "$reply" ,
564+ replyToIdSource : "implicit" ,
565+ replyToMode : "first" ,
566+ } ) ;
567+
568+ expect ( mockOptions ( mocks . sendMessageMatrix , "sendMessageMatrix" , 0 ) . replyToId ) . toBe ( "$reply" ) ;
569+ expect ( mockOptions ( mocks . sendMessageMatrix , "sendMessageMatrix" , 1 ) . replyToId ) . toBeUndefined ( ) ;
570+ expect ( result ) . toMatchObject ( {
571+ channel : "matrix" ,
572+ messageId : "$second-image" ,
573+ primaryMessageId : "$image" ,
574+ content : "caption\noverflow\nsecond image" ,
575+ } ) ;
576+ expect ( result . receipt ?. primaryPlatformMessageId ) . toBe ( "$image" ) ;
577+ expect ( result . receipt ?. platformMessageIds ) . toEqual ( [
578+ "$image" ,
579+ "$caption-overflow" ,
580+ "$second-image" ,
581+ ] ) ;
582+ expect ( result . receipt ?. parts ) . toMatchObject ( [
583+ { platformMessageId : "$image" , kind : "media" , index : 0 , replyToId : "$reply" } ,
584+ { platformMessageId : "$caption-overflow" , kind : "text" , index : 1 } ,
585+ { platformMessageId : "$second-image" , kind : "media" , index : 2 } ,
586+ ] ) ;
587+ expect ( result . receipt ?. parts [ 1 ] ) . not . toHaveProperty ( "replyToId" ) ;
588+ expect ( result . receipt ?. parts [ 2 ] ) . not . toHaveProperty ( "replyToId" ) ;
589+ } ) ;
590+
476591 it ( "sends mediaUrls with extraContent only on first item" , async ( ) => {
477592 const cfg = {
478593 channels : {
0 commit comments