@@ -144,6 +144,7 @@ function createPlan(
144144async function dispatchObservedPlatformReply ( params : {
145145 payload : { text : string ; mediaUrls ?: string [ ] } ;
146146 platform : Pick < AdmittedWebInboundMessage [ "platform" ] , "reply" | "sendMedia" > ;
147+ deferredMediaUrls ?: string [ ] ;
147148} ) {
148149 const pluginHook = vi . fn ( ) ;
149150 const internalHook = vi . fn ( ) ;
@@ -157,11 +158,31 @@ async function dispatchObservedPlatformReply(params: {
157158 initializeGlobalHookRunner ( registry ) ;
158159 registerInternalHook ( "message:sent" , internalHook ) ;
159160 const { context, plan, replyLogger } = createPlan ( deliverWebReply , params . platform ) ;
161+ const onDelivered = vi . fn ( plan . delivery . onDelivered ) ;
160162 let deliveryResult : unknown ;
163+ let settledResult : unknown ;
161164 const dispatchReplyWithBufferedBlockDispatcher = vi . fn ( async ( dispatch ) => {
162165 dispatch . replyOptions ?. onAgentRunStart ?.( "run-wa-partial" ) ;
163- deliveryResult = await dispatch . dispatcherOptions . deliver ( params . payload , { kind : "final" } ) ;
164- return { queuedFinal : true , counts : { tool : 0 , block : 0 , final : 1 } } ;
166+ if ( params . deferredMediaUrls ) {
167+ const deferred = await dispatch . dispatcherOptions . deliver (
168+ { text : "tool images" , mediaUrls : params . deferredMediaUrls } ,
169+ { kind : "tool" } ,
170+ ) ;
171+ expect ( deferred ) . toMatchObject ( {
172+ visibleReplySent : false ,
173+ finalization : expect . any ( Promise ) ,
174+ } ) ;
175+ expect ( onDelivered ) . not . toHaveBeenCalled ( ) ;
176+ }
177+ try {
178+ deliveryResult = await dispatch . dispatcherOptions . deliver ( params . payload , { kind : "final" } ) ;
179+ } finally {
180+ settledResult = await dispatch . dispatcherOptions . onSettled ?.( ) ;
181+ }
182+ return {
183+ queuedFinal : true ,
184+ counts : { tool : params . deferredMediaUrls ? 1 : 0 , block : 0 , final : 1 } ,
185+ } ;
165186 } ) ;
166187 const failure = await dispatchChannelInboundReply ( {
167188 cfg : { channels : { whatsapp : { } } } as never ,
@@ -174,11 +195,19 @@ async function dispatchObservedPlatformReply(params: {
174195 recordInboundSession : async ( ) => undefined ,
175196 dispatchReplyWithBufferedBlockDispatcher,
176197 dispatcherOptions : plan . dispatcherOptions ,
177- delivery : plan . delivery ,
198+ delivery : { ... plan . delivery , onDelivered } ,
178199 replyOptions : plan . replyOptions ,
179200 replyResolver : plan . replyResolver ,
180201 } ) . catch ( ( caught : unknown ) => caught ) ;
181- return { deliveryResult, failure, internalHook, pluginHook, replyLogger } ;
202+ return {
203+ deliveryResult,
204+ failure,
205+ internalHook,
206+ pluginHook,
207+ replyLogger,
208+ onDelivered,
209+ settledResult,
210+ } ;
182211}
183212
184213afterEach ( ( ) => {
@@ -187,6 +216,96 @@ afterEach(() => {
187216} ) ;
188217
189218describe ( "WhatsApp canonical message_sent delivery" , ( ) => {
219+ it . each ( [
220+ {
221+ replacement : [ "/tmp/a.jpg" ] ,
222+ sent : [ "/tmp/a.jpg" , "/tmp/b.jpg" ] ,
223+ deferredIds : [ "sent-2" ] ,
224+ replacementId : "sent-1" ,
225+ } ,
226+ {
227+ replacement : [ "/tmp/a.jpg" , "/tmp/b.jpg" ] ,
228+ sent : [ "/tmp/a.jpg" , "/tmp/b.jpg" ] ,
229+ deferredIds : [ ] ,
230+ replacementId : "sent-1" ,
231+ } ,
232+ {
233+ replacement : [ "/tmp/c.jpg" ] ,
234+ sent : [ "/tmp/a.jpg" , "/tmp/b.jpg" , "/tmp/c.jpg" ] ,
235+ deferredIds : [ "sent-1" , "sent-2" ] ,
236+ replacementId : "sent-3" ,
237+ } ,
238+ {
239+ deferredMedia : [ "/tmp/a.jpg" , "/tmp/b.jpg" , "/tmp/c.jpg" ] ,
240+ replacement : [ "/tmp/b.jpg" ] ,
241+ sent : [ "/tmp/b.jpg" , "/tmp/a.jpg" , "/tmp/c.jpg" ] ,
242+ deferredIds : [ "sent-2" , "sent-3" ] ,
243+ replacementId : "sent-1" ,
244+ } ,
245+ ] ) (
246+ "finalizes every deferred attachment through core after replacement $replacement" ,
247+ async ( {
248+ replacement,
249+ sent : expectedSent ,
250+ deferredIds,
251+ replacementId,
252+ deferredMedia = [ "/tmp/a.jpg" , "/tmp/b.jpg" ] ,
253+ } ) => {
254+ const sent : string [ ] = [ ] ;
255+ vi . mocked ( loadWebMedia ) . mockImplementation ( async ( url ) => ( {
256+ buffer : Buffer . from ( url ) ,
257+ contentType : "image/jpeg" ,
258+ kind : "image" ,
259+ } ) ) ;
260+ const sendMedia = vi . fn < AdmittedWebInboundMessage [ "platform" ] [ "sendMedia" ] > (
261+ async ( content ) => {
262+ if ( ! ( "image" in content ) || ! Buffer . isBuffer ( content . image ) ) {
263+ throw new Error ( "expected image transport payload" ) ;
264+ }
265+ sent . push ( content . image . toString ( ) ) ;
266+ return normalizeWhatsAppSendResult (
267+ { key : { id : `sent-${ sent . length } ` } } as WAMessage ,
268+ "media" ,
269+ ) ;
270+ } ,
271+ ) ;
272+ const reply = vi . fn < AdmittedWebInboundMessage [ "platform" ] [ "reply" ] > ( ) ;
273+ const { failure, pluginHook, internalHook, onDelivered, settledResult } =
274+ await dispatchObservedPlatformReply ( {
275+ payload : { text : "Here are the images" , mediaUrls : replacement } ,
276+ deferredMediaUrls : deferredMedia ,
277+ platform : { reply, sendMedia } ,
278+ } ) ;
279+
280+ expect ( failure ) . toMatchObject ( { dispatched : true } ) ;
281+ expect ( sent ) . toEqual ( expectedSent ) ;
282+ expect ( reply ) . not . toHaveBeenCalled ( ) ;
283+ expect ( settledResult ) . toEqual ( { visibleReplySent : true } ) ;
284+ expect ( onDelivered ) . toHaveBeenCalledTimes ( 2 ) ;
285+ const [ , info , finalized ] = onDelivered . mock . calls [ 1 ] ! ;
286+ expect ( info . kind ) . toBe ( "tool" ) ;
287+ expect ( finalized ?. finalization ) . toBeUndefined ( ) ;
288+ if ( deferredIds . length ) {
289+ expect ( finalized ) . toMatchObject ( {
290+ visibleReplySent : true ,
291+ content : "" ,
292+ messageIds : deferredIds ,
293+ receipt : { platformMessageIds : deferredIds } ,
294+ } ) ;
295+ } else {
296+ expect ( finalized ) . toEqual ( { visibleReplySent : false , finalization : undefined } ) ;
297+ }
298+ const observedIds = [ replacementId , ...deferredIds . slice ( 0 , 1 ) ] ;
299+ await vi . waitFor ( ( ) => {
300+ expect ( pluginHook ) . toHaveBeenCalledTimes ( observedIds . length ) ;
301+ expect ( internalHook ) . toHaveBeenCalledTimes ( observedIds . length ) ;
302+ } ) ;
303+ expect ( pluginHook . mock . calls . map ( ( [ event ] ) => [ event . messageId , event . success ] ) ) . toEqual (
304+ observedIds . map ( ( id ) => [ id , true ] ) ,
305+ ) ;
306+ } ,
307+ ) ;
308+
190309 it ( "emits one receipt-backed event with session and run correlation for multipart media" , async ( ) => {
191310 const pluginHook = vi . fn ( ) ;
192311 const internalHook = vi . fn ( ) ;
0 commit comments