@@ -260,6 +260,75 @@ describe("createDraftStreamLoop", () => {
260260 expect ( sendOrEditStreamMessage ) . toHaveBeenNthCalledWith ( 2 , "hello" ) ;
261261 } ) ;
262262
263+ it ( "preserves concurrent update text when sendOrEditStreamMessage returns false" , async ( ) => {
264+ let deliver ! : ( ( ) => void ) | undefined ;
265+ const deliverPromise = new Promise < void > ( ( resolve ) => {
266+ deliver = resolve ;
267+ } ) ;
268+ const capturedArgs : string [ ] = [ ] ;
269+
270+ const sendOrEditStreamMessage = vi
271+ . fn < ( text : string ) => Promise < boolean > > ( )
272+ . mockImplementationOnce ( async ( text : string ) => {
273+ capturedArgs . push ( text ) ;
274+ await deliverPromise ;
275+ return false ;
276+ } )
277+ . mockImplementationOnce ( async ( text : string ) => {
278+ capturedArgs . push ( text ) ;
279+ return true ;
280+ } ) ;
281+
282+ const loop = createDraftStreamLoop ( {
283+ throttleMs : 0 ,
284+ isStopped : ( ) => false ,
285+ sendOrEditStreamMessage,
286+ } ) ;
287+
288+ loop . update ( "initial" ) ;
289+ await flushMicrotasks ( ) ;
290+
291+ loop . update ( "concurrent" ) ;
292+ deliver ! ( ) ;
293+ await loop . flush ( ) ;
294+
295+ expect ( capturedArgs [ 1 ] ) . toBe ( "concurrent" ) ;
296+ } ) ;
297+
298+ it ( "preserves generic pending updates when consecutive sends return false" , async ( ) => {
299+ type Update = { text : string ; blocks : string [ ] } ;
300+ const initial = { text : "initial" , blocks : [ "initial-blocks" ] } ;
301+ const latest = { text : "latest" , blocks : [ "latest-blocks" ] } ;
302+ let releaseFirst : ( ( ) => void ) | undefined ;
303+ const firstSend = new Promise < void > ( ( resolve ) => {
304+ releaseFirst = resolve ;
305+ } ) ;
306+ const sendOrEditStreamMessage = vi
307+ . fn < ( update : Update ) => Promise < boolean > > ( )
308+ . mockImplementationOnce ( async ( ) => {
309+ await firstSend ;
310+ return false ;
311+ } )
312+ . mockResolvedValueOnce ( false )
313+ . mockResolvedValueOnce ( true ) ;
314+ const loop = createDraftStreamLoop < Update > ( {
315+ throttleMs : 0 ,
316+ isStopped : ( ) => false ,
317+ emptyValue : { text : "" , blocks : [ ] } ,
318+ isEmpty : ( update ) => ! update . text ,
319+ sendOrEditStreamMessage,
320+ } ) ;
321+
322+ loop . update ( initial ) ;
323+ await flushMicrotasks ( ) ;
324+ loop . update ( latest ) ;
325+ releaseFirst ?.( ) ;
326+ await loop . flush ( ) ;
327+ await loop . flush ( ) ;
328+
329+ expect ( sendOrEditStreamMessage . mock . calls ) . toEqual ( [ [ initial ] , [ latest ] , [ latest ] ] ) ;
330+ } ) ;
331+
263332 it ( "keeps generic payload fields atomic while newer updates queue" , async ( ) => {
264333 type Update = { text : string ; blocks : string [ ] } ;
265334 let releaseFirst : ( ( ) => void ) | undefined ;
0 commit comments