@@ -456,6 +456,63 @@ describe("Two-Level Tenant Dispatch", () => {
456456 await redis . quit ( ) ;
457457 }
458458 ) ;
459+
460+ redisTest (
461+ "should not re-populate legacy master queue when completing messages" ,
462+ { timeout : 20000 } ,
463+ async ( { redisOptions } ) => {
464+ const keys = new DefaultFairQueueKeyProducer ( { prefix : "test" } ) ;
465+ const redis = createRedisClient ( redisOptions ) ;
466+
467+ // Simulate pre-deploy state: queue with 2 messages in old master queue
468+ const queueId = "tenant:t1:queue:legacy-noreinsert" ;
469+ const queueKey = keys . queueKey ( queueId ) ;
470+ const queueItemsKey = keys . queueItemsKey ( queueId ) ;
471+ const masterQueueKey = keys . masterQueueKey ( 0 ) ;
472+ const timestamp = Date . now ( ) ;
473+
474+ for ( let i = 0 ; i < 2 ; i ++ ) {
475+ const msg : StoredMessage < TestPayload > = {
476+ id : `legacy-msg-${ i } ` ,
477+ queueId,
478+ tenantId : "t1" ,
479+ payload : { value : `msg-${ i } ` } ,
480+ timestamp : timestamp + i ,
481+ attempt : 1 ,
482+ } ;
483+ await redis . zadd ( queueKey , timestamp + i , `legacy-msg-${ i } ` ) ;
484+ await redis . hset ( queueItemsKey , `legacy-msg-${ i } ` , JSON . stringify ( msg ) ) ;
485+ }
486+ await redis . zadd ( masterQueueKey , timestamp , queueId ) ;
487+
488+ const processed : string [ ] = [ ] ;
489+ const helper = new TestHelper ( redisOptions , keys ) ;
490+
491+ helper . onMessage ( async ( ctx ) => {
492+ processed . push ( ctx . message . payload . value ) ;
493+ await ctx . complete ( ) ;
494+
495+ // After completing the first message, the queue still has 1 message
496+ // The legacy master queue should NOT be re-populated
497+ if ( processed . length === 1 ) {
498+ const masterCount = await redis . zcard ( masterQueueKey ) ;
499+ // Should be 0 (drained) not 1 (re-added)
500+ expect ( masterCount ) . toBe ( 0 ) ;
501+ }
502+ } ) ;
503+ helper . start ( ) ;
504+
505+ await waitFor ( ( ) => processed . length === 2 , 10000 ) ;
506+ expect ( processed ) . toHaveLength ( 2 ) ;
507+
508+ // Final check: master queue should be completely empty
509+ const masterFinal = await redis . zcard ( masterQueueKey ) ;
510+ expect ( masterFinal ) . toBe ( 0 ) ;
511+
512+ await helper . close ( ) ;
513+ await redis . quit ( ) ;
514+ }
515+ ) ;
459516 } ) ;
460517
461518 describe ( "DRR selectQueuesFromDispatch" , ( ) => {
0 commit comments