@@ -3,6 +3,7 @@ import fs from "node:fs";
33import os from "node:os" ;
44import path from "node:path" ;
55import type { AnyMessageContent , MiscMessageGenerationOptions , WAMessage } from "baileys" ;
6+ import { isChannelPartialDeliveryError } from "openclaw/plugin-sdk/channel-inbound" ;
67import { listMessageReceiptPlatformIds } from "openclaw/plugin-sdk/channel-outbound" ;
78import { afterEach , beforeEach , describe , expect , it , vi } from "vitest" ;
89import { prepareWhatsAppOutboundMedia } from "../outbound-media-contract.js" ;
@@ -472,6 +473,104 @@ describe("createWebSendApi", () => {
472473 ] ) ;
473474 } ) ;
474475
476+ it ( "preserves the accepted voice receipt when its visible caption send disconnects" , async ( ) => {
477+ const captionError = new Error ( "connection closed while sending the voice caption" ) ;
478+ sendMessage
479+ . mockResolvedValueOnce ( { key : { id : "voice-accepted-1" } } )
480+ . mockRejectedValueOnce ( captionError ) ;
481+
482+ const failure = await api
483+ . sendMessage ( "+1555" , "voice text" , Buffer . from ( "voice" ) , "audio/ogg" )
484+ . catch ( ( caught : unknown ) => caught ) ;
485+
486+ expect ( sendMessage ) . toHaveBeenCalledTimes ( 2 ) ;
487+ expect ( isChannelPartialDeliveryError ( failure ) ) . toBe ( true ) ;
488+ if ( ! isChannelPartialDeliveryError ( failure ) ) {
489+ throw new Error ( "accepted voice delivery did not preserve its partial receipt" ) ;
490+ }
491+ expect ( failure . deliveryResult . visibleReplySent ) . toBe ( true ) ;
492+ expect ( failure . deliveryResult . messageIds ) . toEqual ( [ "voice-accepted-1" ] ) ;
493+ expect ( failure . deliveryResult . receipt ?. platformMessageIds ) . toEqual ( [ "voice-accepted-1" ] ) ;
494+ expect ( failure ) . toHaveProperty ( "cause" , captionError ) ;
495+ } ) ;
496+
497+ it ( "preserves the accepted voice receipt when caption mention resolution fails" , async ( ) => {
498+ const mentionError = new Error ( "group metadata lookup disconnected" ) ;
499+ api = createWebSendApi ( {
500+ sock : { sendMessage, sendPresenceUpdate } ,
501+ defaultAccountId : "main" ,
502+ resolveOutboundMentions : vi . fn ( ) . mockRejectedValue ( mentionError ) ,
503+ } ) ;
504+ sendMessage . mockResolvedValueOnce ( { key : { id : "voice-accepted-2" } } ) ;
505+
506+ const failure = await api
507+ . sendMessage ( "+1555" , "voice text" , Buffer . from ( "voice" ) , "audio/ogg" )
508+ . catch ( ( caught : unknown ) => caught ) ;
509+
510+ expect ( sendMessage ) . toHaveBeenCalledOnce ( ) ;
511+ expect ( isChannelPartialDeliveryError ( failure ) ) . toBe ( true ) ;
512+ if ( ! isChannelPartialDeliveryError ( failure ) ) {
513+ throw new Error ( "accepted voice delivery was lost during mention resolution" ) ;
514+ }
515+ expect ( failure . deliveryResult . messageIds ) . toEqual ( [ "voice-accepted-2" ] ) ;
516+ expect ( failure ) . toHaveProperty ( "cause" , mentionError ) ;
517+ } ) ;
518+
519+ it . each ( [
520+ {
521+ kind : "text" ,
522+ send : ( sendApi : ReturnType < typeof createWebSendApi > ) => sendApi . sendMessage ( "+1555" , "hello" ) ,
523+ } ,
524+ {
525+ kind : "poll" ,
526+ send : ( sendApi : ReturnType < typeof createWebSendApi > ) =>
527+ sendApi . sendPoll ( "+1555" , { question : "Q?" , options : [ "a" , "b" ] } ) ,
528+ } ,
529+ ] ) ( "retains the accepted $kind receipt when activity recording fails" , async ( { kind, send } ) => {
530+ const activityError = new Error ( "channel activity bookkeeping disconnected" ) ;
531+ sendMessage . mockResolvedValueOnce ( { key : { id : `${ kind } -accepted` } } ) ;
532+ recordChannelActivity . mockImplementationOnce ( ( ) => {
533+ throw activityError ;
534+ } ) ;
535+
536+ const failure = await send ( api ) . catch ( ( caught : unknown ) => caught ) ;
537+
538+ expect ( sendMessage ) . toHaveBeenCalledOnce ( ) ;
539+ expect ( isChannelPartialDeliveryError ( failure ) ) . toBe ( true ) ;
540+ if ( ! isChannelPartialDeliveryError ( failure ) ) {
541+ throw new Error ( "accepted provider delivery was lost during activity bookkeeping" ) ;
542+ }
543+ expect ( failure . deliveryResult . messageIds ) . toEqual ( [ `${ kind } -accepted` ] ) ;
544+ expect ( failure . deliveryResult . receipt ?. platformMessageIds ) . toEqual ( [ `${ kind } -accepted` ] ) ;
545+ expect ( failure ) . toHaveProperty ( "cause" , activityError ) ;
546+ } ) ;
547+
548+ it ( "retains both accepted voice receipts when later activity recording fails" , async ( ) => {
549+ const activityError = new Error ( "channel activity bookkeeping disconnected" ) ;
550+ sendMessage
551+ . mockResolvedValueOnce ( { key : { id : "voice-accepted-3" } } )
552+ . mockResolvedValueOnce ( { key : { id : "caption-accepted-3" } } ) ;
553+ recordChannelActivity . mockImplementationOnce ( ( ) => {
554+ throw activityError ;
555+ } ) ;
556+
557+ const failure = await api
558+ . sendMessage ( "+1555" , "voice text" , Buffer . from ( "voice" ) , "audio/ogg" )
559+ . catch ( ( caught : unknown ) => caught ) ;
560+
561+ expect ( sendMessage ) . toHaveBeenCalledTimes ( 2 ) ;
562+ expect ( isChannelPartialDeliveryError ( failure ) ) . toBe ( true ) ;
563+ if ( ! isChannelPartialDeliveryError ( failure ) ) {
564+ throw new Error ( "accepted voice and caption deliveries were lost" ) ;
565+ }
566+ expect ( failure . deliveryResult . messageIds ) . toEqual ( [ "voice-accepted-3" , "caption-accepted-3" ] ) ;
567+ expect ( failure . deliveryResult . receipt ?. platformMessageIds ) . toEqual ( [
568+ "voice-accepted-3" ,
569+ "caption-accepted-3" ,
570+ ] ) ;
571+ expect ( failure ) . toHaveProperty ( "cause" , activityError ) ;
572+ } ) ;
573+
475574 it ( "supports video media and gifPlayback option" , async ( ) => {
476575 const payload = Buffer . from ( "vid" ) ;
477576 await api . sendMessage ( "+1555" , "cap" , payload , "video/mp4" , { gifPlayback : true } ) ;
0 commit comments