@@ -1587,19 +1587,48 @@ describe("dispatchTelegramMessage draft streaming", () => {
15871587 } ) ;
15881588
15891589 describe ( "non-streaming media dedup" , ( ) => {
1590+ const finalDeliveryPayload = ( ) => {
1591+ for ( const [ params ] of deliverInboundReplyWithMessageSendContext . mock . calls ) {
1592+ if ( params . info . kind === "final" ) {
1593+ return params . payload ;
1594+ }
1595+ }
1596+ throw new Error ( "missing final delivery" ) ;
1597+ } ;
1598+
15901599 it ( "deduplicates block-sent media from final reply" , async ( ) => {
1591- sendMessageTelegram . mockResolvedValue ( { message_id : 100 } ) ;
1600+ deliverReplies . mockResolvedValue ( { delivered : true } ) ;
15921601 deliverInboundReplyWithMessageSendContext . mockResolvedValue ( {
15931602 status : "handled_visible" ,
15941603 delivery : { messageIds : [ "101" ] , visibleReplySent : true } ,
15951604 } ) ;
15961605 dispatchReplyWithBufferedBlockDispatcher . mockImplementation ( async ( { dispatcherOptions } ) => {
1597- // Block delivers media successfully
1606+ await dispatcherOptions . deliver ( { mediaUrls : [ "/tmp/cat.jpg" ] } , { kind : "block" } ) ;
15981607 await dispatcherOptions . deliver (
15991608 { text : "Here is the image" , mediaUrls : [ "/tmp/cat.jpg" ] } ,
1600- { kind : "block " } ,
1609+ { kind : "final " } ,
16011610 ) ;
1602- // Final reply has same media — should be deduped
1611+ return { queuedFinal : true } ;
1612+ } ) ;
1613+
1614+ await dispatchWithContext ( {
1615+ context : createContext ( ) ,
1616+ streamMode : "off" ,
1617+ telegramDeps : telegramDepsForTest ,
1618+ } ) ;
1619+
1620+ expect ( finalDeliveryPayload ( ) . mediaUrls ) . toEqual ( [ ] ) ;
1621+ } ) ;
1622+
1623+ it ( "preserves final media when block delivery reports no visible send" , async ( ) => {
1624+ deliverReplies . mockResolvedValueOnce ( { delivered : false } ) ;
1625+ deliverReplies . mockResolvedValue ( { delivered : true } ) ;
1626+ deliverInboundReplyWithMessageSendContext . mockResolvedValue ( {
1627+ status : "handled_visible" ,
1628+ delivery : { messageIds : [ "101" ] , visibleReplySent : true } ,
1629+ } ) ;
1630+ dispatchReplyWithBufferedBlockDispatcher . mockImplementation ( async ( { dispatcherOptions } ) => {
1631+ await dispatcherOptions . deliver ( { mediaUrls : [ "/tmp/cat.jpg" ] } , { kind : "block" } ) ;
16031632 await dispatcherOptions . deliver (
16041633 { text : "Here is the image" , mediaUrls : [ "/tmp/cat.jpg" ] } ,
16051634 { kind : "final" } ,
@@ -1613,12 +1642,7 @@ describe("dispatchTelegramMessage draft streaming", () => {
16131642 telegramDeps : telegramDepsForTest ,
16141643 } ) ;
16151644
1616- // Final delivery should have empty mediaUrls (deduped)
1617- const finalCall = deliverInboundReplyWithMessageSendContext . mock . calls . find (
1618- ( c : unknown [ ] ) => ( c [ 0 ] as { info : { kind : string } } ) . info . kind === "final" ,
1619- ) ;
1620- expect ( finalCall ) . toBeDefined ( ) ;
1621- expect ( ( finalCall ! [ 0 ] as { payload : { mediaUrls : string [ ] } } ) . payload . mediaUrls ) . toEqual ( [ ] ) ;
1645+ expect ( finalDeliveryPayload ( ) . mediaUrls ) . toEqual ( [ "/tmp/cat.jpg" ] ) ;
16221646 } ) ;
16231647
16241648 it ( "preserves final media when block delivery fails" , async ( ) => {
@@ -1629,16 +1653,9 @@ describe("dispatchTelegramMessage draft streaming", () => {
16291653 delivery : { messageIds : [ "101" ] , visibleReplySent : true } ,
16301654 } ) ;
16311655 dispatchReplyWithBufferedBlockDispatcher . mockImplementation ( async ( { dispatcherOptions } ) => {
1632- // Block delivery will throw (deliverReplies rejects)
16331656 try {
1634- await dispatcherOptions . deliver (
1635- { text : "Here is the image" , mediaUrls : [ "/tmp/cat.jpg" ] } ,
1636- { kind : "block" } ,
1637- ) ;
1638- } catch {
1639- // Block send failed — simulates dispatcher .catch() behavior
1640- }
1641- // Final reply has same media — should NOT be deduped since block failed
1657+ await dispatcherOptions . deliver ( { mediaUrls : [ "/tmp/cat.jpg" ] } , { kind : "block" } ) ;
1658+ } catch { }
16421659 await dispatcherOptions . deliver (
16431660 { text : "Here is the image" , mediaUrls : [ "/tmp/cat.jpg" ] } ,
16441661 { kind : "final" } ,
@@ -1652,14 +1669,7 @@ describe("dispatchTelegramMessage draft streaming", () => {
16521669 telegramDeps : telegramDepsForTest ,
16531670 } ) ;
16541671
1655- // Final delivery should still have media since block failed
1656- const finalCall = deliverInboundReplyWithMessageSendContext . mock . calls . find (
1657- ( c : unknown [ ] ) => ( c [ 0 ] as { info : { kind : string } } ) . info . kind === "final" ,
1658- ) ;
1659- expect ( finalCall ) . toBeDefined ( ) ;
1660- expect ( ( finalCall ! [ 0 ] as { payload : { mediaUrls : string [ ] } } ) . payload . mediaUrls ) . toEqual ( [
1661- "/tmp/cat.jpg" ,
1662- ] ) ;
1672+ expect ( finalDeliveryPayload ( ) . mediaUrls ) . toEqual ( [ "/tmp/cat.jpg" ] ) ;
16631673 } ) ;
16641674 } ) ;
16651675} ) ;
0 commit comments