@@ -13,6 +13,7 @@ import {
1313 emitTelegramMessageSentHooks ,
1414 expectDeliverRepliesParams ,
1515 expectRecordFields ,
16+ loadSessionStore ,
1617 mockCallArg ,
1718 mockDefaultSessionEntry ,
1819 readLatestAssistantTextByIdentity ,
@@ -114,6 +115,141 @@ describeTelegramDispatch("dispatchTelegramMessage draft-finalization", () => {
114115 expectRecordFields ( mockCallArg ( editMessageTelegram , 0 , 3 ) , { buttons } ) ;
115116 } ) ;
116117
118+ it ( "keeps DM Web App buttons when finalizing the streamed preview" , async ( ) => {
119+ const { answerDraftStream } = setupDraftStreams ( { answerMessageId : 2001 } ) ;
120+ dispatchReplyWithBufferedBlockDispatcher . mockImplementation (
121+ async ( { dispatcherOptions, replyOptions } ) => {
122+ await replyOptions ?. onPartialReply ?.( { text : "Opening the app" } ) ;
123+ await dispatcherOptions . deliver (
124+ {
125+ text : "Open the app" ,
126+ presentation : {
127+ blocks : [
128+ {
129+ type : "buttons" ,
130+ buttons : [
131+ {
132+ label : "Launch" ,
133+ action : { type : "web-app" , url : "https://example.com/app" } ,
134+ } ,
135+ ] ,
136+ } ,
137+ ] ,
138+ } ,
139+ } ,
140+ { kind : "final" } ,
141+ ) ;
142+ return { queuedFinal : true } ;
143+ } ,
144+ ) ;
145+
146+ await dispatchWithContext ( {
147+ context : createContext ( ) ,
148+ streamMode : "partial" ,
149+ telegramCfg : { richMessages : true , streaming : { mode : "partial" } } ,
150+ } ) ;
151+
152+ expect ( answerDraftStream . update ) . toHaveBeenLastCalledWith (
153+ "Open the app" ,
154+ expect . objectContaining ( { onPlatformSendDispatch : expect . any ( Function ) } ) ,
155+ ) ;
156+ expectRecordFields ( mockCallArg ( editMessageTelegram , 0 , 3 ) , {
157+ buttons : [ [ { text : "Launch" , web_app : { url : "https://example.com/app" } } ] ] ,
158+ } ) ;
159+ expect ( deliverReplies ) . not . toHaveBeenCalled ( ) ;
160+ } ) ;
161+
162+ it ( "renders dropped controls in the finalized streamed preview" , async ( ) => {
163+ const { answerDraftStream } = setupDraftStreams ( { answerMessageId : 2001 } ) ;
164+ dispatchReplyWithBufferedBlockDispatcher . mockImplementation (
165+ async ( { dispatcherOptions, replyOptions } ) => {
166+ await replyOptions ?. onPartialReply ?.( { text : "Preparing controls" } ) ;
167+ await dispatcherOptions . deliver (
168+ {
169+ text : "Choose" ,
170+ presentation : {
171+ blocks : [
172+ {
173+ type : "buttons" ,
174+ buttons : [ { label : "Copy manually" , value : "x" . repeat ( 65 ) } ] ,
175+ } ,
176+ ] ,
177+ } ,
178+ } ,
179+ { kind : "final" } ,
180+ ) ;
181+ return { queuedFinal : true } ;
182+ } ,
183+ ) ;
184+
185+ await dispatchWithContext ( {
186+ context : createContext ( ) ,
187+ streamMode : "partial" ,
188+ telegramCfg : { richMessages : true , streaming : { mode : "partial" } } ,
189+ } ) ;
190+
191+ expect ( answerDraftStream . update ) . toHaveBeenLastCalledWith (
192+ "Choose\n\n- Copy manually" ,
193+ expect . objectContaining ( { onPlatformSendDispatch : expect . any ( Function ) } ) ,
194+ ) ;
195+ expect ( deliverReplies ) . not . toHaveBeenCalled ( ) ;
196+ } ) ;
197+
198+ it ( "keeps mixed controls intact through buffered finalization" , async ( ) => {
199+ const { answerDraftStream } = setupDraftStreams ( { answerMessageId : 2001 } ) ;
200+ loadSessionStore . mockReturnValue ( { s1 : { reasoningLevel : "stream" } } ) ;
201+ deliverReplies
202+ . mockResolvedValueOnce ( { delivered : false } )
203+ . mockResolvedValueOnce ( { delivered : true } ) ;
204+ dispatchReplyWithBufferedBlockDispatcher . mockImplementation ( async ( { dispatcherOptions } ) => {
205+ await dispatcherOptions . deliver (
206+ { text : "<think>first attempt</think>" , isReasoning : true } ,
207+ { kind : "block" } ,
208+ ) ;
209+ await dispatcherOptions . deliver (
210+ {
211+ presentation : {
212+ blocks : [
213+ {
214+ type : "buttons" ,
215+ buttons : [
216+ { label : "Retry" , value : "retry" } ,
217+ { label : "Copy manually" , value : "x" . repeat ( 65 ) } ,
218+ ] ,
219+ } ,
220+ ] ,
221+ } ,
222+ } ,
223+ { kind : "final" } ,
224+ ) ;
225+ await dispatcherOptions . deliver (
226+ { text : "<think>second attempt</think>" , isReasoning : true } ,
227+ { kind : "block" } ,
228+ ) ;
229+ return { queuedFinal : true } ;
230+ } ) ;
231+
232+ await dispatchWithContext ( {
233+ context : createContext ( {
234+ ctxPayload : { SessionKey : "s1" } as TelegramMessageContext [ "ctxPayload" ] ,
235+ } ) ,
236+ } ) ;
237+
238+ expect ( answerDraftStream . update ) . toHaveBeenCalledWith (
239+ "- Copy manually" ,
240+ expect . objectContaining ( { onPlatformSendDispatch : expect . any ( Function ) } ) ,
241+ ) ;
242+ expectRecordFields ( mockCallArg ( editMessageTelegram , 0 , 3 ) , {
243+ buttons : [ [ { text : "Retry" , callback_data : "retry" } ] ] ,
244+ } ) ;
245+ const fallbackReplies = deliverReplies . mock . calls . flatMap ( ( call ) =>
246+ ( ( call [ 0 ] as { replies ?: Array < { text ?: string } > } ) . replies ?? [ ] ) . map (
247+ ( reply ) => reply . text ,
248+ ) ,
249+ ) ;
250+ expect ( fallbackReplies ) . not . toContain ( "- Copy manually" ) ;
251+ } ) ;
252+
117253 it ( "finalizes an ordinary block-only draft when no final follows" , async ( ) => {
118254 const { answerDraftStream } = setupDraftStreams ( { answerMessageId : 2001 } ) ;
119255 dispatchReplyWithBufferedBlockDispatcher . mockImplementation ( async ( { dispatcherOptions } ) => {
0 commit comments